nrf_gzp_host.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. /**
  2. * Copyright (c) 2009 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /**
  41. * @file
  42. * @brief Implementation of Gazell Pairing Library (gzp), Host functions.
  43. * @defgroup gzp_source_host Gazell Pairing Host implementation
  44. * @{
  45. * @ingroup gzp_04_source
  46. */
  47. #include "nrf_gzp.h"
  48. #include "nrf_gzll.h"
  49. #include <string.h>
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include "nrf_assert.h"
  53. #include "nrf_ecb.h"
  54. #include "nrf_nvmc.h"
  55. //lint -esym(40, GZP_PARAMS_STORAGE_ADR) "Undeclared identifier"
  56. #define GZP_PARAMS_DB_ADR GZP_PARAMS_STORAGE_ADR // Address of the GZP parameters flash page.
  57. /******************************************************************************/
  58. /** @name Typedefs
  59. * @{ */
  60. /******************************************************************************/
  61. /**
  62. * Definition of internal states.
  63. */
  64. typedef enum
  65. {
  66. GZP_ID_REQ_IDLE, ///< No Host ID request received from Device.
  67. GZP_ID_REQ_PENDING, ///< Host ID request received and waiting on application to grant/reject.
  68. GZP_ID_REQ_PENDING_AND_GRANTED, ///< Host ID request received and granted by application.
  69. GZP_ID_REQ_PENDING_AND_REJECTED, ///< Host ID request received and rejected by application.
  70. } gzp_id_req_stat_t;
  71. /** @} */
  72. /******************************************************************************/
  73. /** @name Internal (static) function prototypes
  74. * @{ */
  75. /******************************************************************************/
  76. /**
  77. * Function for incrementing internal session counter.
  78. */
  79. static void gzp_session_counter_inc(void);
  80. /**
  81. * Function for reading value of internal session counter.
  82. * @param dst Current session counter.
  83. */
  84. static void gzp_get_session_counter(uint8_t* dst);
  85. /**
  86. * Function processing received "system address request" from Device.
  87. *
  88. * @param gzp_req Pointer to RX payload containing system address request.
  89. */
  90. static void gzp_process_address_req(uint8_t* gzp_req);
  91. /**
  92. * Function to process Host ID request from device.
  93. *
  94. * The Host shall retrieve the Host ID from NVM, or shall generate if
  95. * it does not yet exist.
  96. *
  97. * @param rx_payload Pointer to rx_payload contaning Host ID request.
  98. */
  99. static void gzp_process_id_req(uint8_t* rx_payload);
  100. /**
  101. * Function to process Host ID fetch request from Device.
  102. *
  103. * The Device fetches the Host ID after the Host has generated/retrieved
  104. * the Host ID.
  105. *
  106. * @param rx_payload Pointer to rx_payload contaning Host ID fetch request.
  107. */
  108. static void gzp_process_id_fetch(uint8_t* rx_payload);
  109. /**
  110. * Function to process Key Update Prepare packet.
  111. *
  112. * Device requests the Session Token to be used for the Key Update request.
  113. */
  114. static void gzp_process_key_update_prepare(void);
  115. /**
  116. * Function to process Key Update packet.
  117. *
  118. * Device requests a Key Update and sends a new Dynamic Key. The Dynamic Key is
  119. * updated on the Host.
  120. *
  121. * @param rx_payload Pointer to rx_payload containing Key Update request.
  122. */
  123. static void gzp_process_key_update(uint8_t* rx_payload);
  124. /**
  125. * Function to process received Encrypted User packet.
  126. *
  127. * @param rx_payload Pointer to rx_payload containing the encrypted user data.
  128. * @param length Length of encrypted user data.
  129. */
  130. static void gzp_process_encrypted_user_data(uint8_t* rx_payload, uint8_t length);
  131. /**
  132. * Function to preload the payload for the next ACK.
  133. *
  134. * @param src Pointer to source payload.
  135. * @param length Length of source payload.
  136. * @param pipe Pipe for the ACK payload.
  137. */
  138. static void gzp_preload_ack(uint8_t* src, uint8_t length, uint8_t pipe);
  139. /**
  140. * Function for reading the Chip ID from non-volatile memory.
  141. *
  142. * The chip ID is used for the system address.
  143. *
  144. * If the Chip ID is not yet created a random Chip ID is created and
  145. * written to non-volatile memory. Note that the term chip ID is used as
  146. * the factory programmed chip sequence number was used for the system
  147. * address in nRF24LU ICs.
  148. *
  149. * @param dst Address to copy Host ID to.
  150. * @param[in] n Number of bytes in the Host ID.
  151. */
  152. void gzp_host_chip_id_read(uint8_t *dst, uint8_t n);
  153. /**
  154. * Function to set the Host ID.
  155. *
  156. * Writes the Host ID to non-volatile memory.
  157. * @param src Address of the Host ID to copy from.
  158. */
  159. static void gzp_set_host_id(const uint8_t* src);
  160. /**
  161. * Function to request disabling of Gazell and wait for it to be disabled.
  162. *
  163. * Emulates legacy gzll_goto_idle().
  164. */
  165. static void gzll_goto_idle(void);
  166. /**
  167. * Flush all TX FIFOs.
  168. *
  169. * Emulates legacy gzll_tx_fifo_flush().
  170. */
  171. static void gzll_tx_fifo_flush(void);
  172. /**
  173. * Flush all RX FIFOs.
  174. *
  175. * Emulates legacy gzll_rx_fifo_flush().
  176. */
  177. static void gzll_rx_fifo_flush(void);
  178. /**
  179. * Set a timeout for the reception of packets on the Gazell Host.
  180. *
  181. * Emulates legacy Gazell function: gzll_set_param(GZLL_PARAM_RX_TIMEOUT, x).
  182. *
  183. * @param timeout Timeout in number of legacy "RX periods"
  184. * (1 RX period = 2 timeslot periods).
  185. */
  186. static void gzll_set_rx_timeout(uint32_t timeout);
  187. /** @} */
  188. /******************************************************************************/
  189. /** @name Internal (static) variabls
  190. * @{ */
  191. /******************************************************************************/
  192. static gzp_id_req_stat_t gzp_id_req_stat; ///< Current state of Host ID request.
  193. static bool gzp_pairing_enabled_f; ///< True if Host is paired with a device.
  194. static bool gzp_address_exchanged_f; ///< True if Host has exchanged a system address with a device and thus pairing has begun.
  195. static uint8_t gzp_session_counter[GZP_SESSION_TOKEN_LENGTH]; ///< Session counter used for key generation and update.
  196. static bool gzp_encrypted_user_data[GZP_ENCRYPTED_USER_DATA_MAX_LENGTH]; ///< Placeholder for encrypted data from Device.
  197. static uint8_t gzp_encrypted_user_data_length; ///< Length of gzp_encrypted_user_data. Zero implies no data received.
  198. static nrf_gzll_host_rx_info_t prev_gzp_rx_info = {0, 0}; ///< RSSI and status of ACK payload transmission of previous Gazell packet.
  199. // Define Macro to make array initialization nicer
  200. #define REP4(X) X X X X
  201. #if defined(__ICCARM__)
  202. #if GZP_PARAMS_DB_ADR == 0x1000
  203. static const uint32_t database[GZP_DEVICE_PARAMS_STORAGE_SIZE/4] @ "gzp_dev_data"
  204. #elif GZP_PARAMS_DB_ADR == 0x15000
  205. static const uint32_t database[GZP_DEVICE_PARAMS_STORAGE_SIZE/4] @ "gzp_dev_data_sd"
  206. #else
  207. #error
  208. #endif
  209. #else
  210. static const uint32_t database[GZP_DEVICE_PARAMS_STORAGE_SIZE / 4] __attribute__((at(GZP_PARAMS_DB_ADR)))
  211. #endif
  212. = {
  213. #define STATIC_INIT_VALUE 0xFFFFFFFF
  214. #define STATIC_INIT_COUNT (GZP_DEVICE_PARAMS_STORAGE_SIZE / 4)
  215. #define INIT_1 STATIC_INIT_VALUE,
  216. #define INIT_4 REP4(INIT_1)
  217. #define INIT_16 REP4(INIT_4)
  218. #define INIT_64 REP4(INIT_16)
  219. #define INIT_256 REP4(INIT_64)
  220. #define INIT_1024 REP4(INIT_256)
  221. #if (STATIC_INIT_COUNT == 256)
  222. INIT_256
  223. #elif (STATIC_INIT_COUNT == 1024)
  224. INIT_1024
  225. #else
  226. #error Gazell Pairing Library database not initialized properly!
  227. #endif
  228. }; ///< Database for storing keys.
  229. /** @} */
  230. /******************************************************************************/
  231. // Implementation: Host-specific API functions
  232. /******************************************************************************/
  233. void gzp_init()
  234. {
  235. uint8_t system_address[GZP_SYSTEM_ADDRESS_WIDTH];
  236. // Read "chip id", of which 4 bytes (GZP_SYSTEM_ADDRESS_WIDTH)
  237. // are used as system address
  238. gzp_host_chip_id_read(system_address, GZP_SYSTEM_ADDRESS_WIDTH);
  239. // Set up radio parameters (addresses and channel subset) from system_address
  240. (void)gzp_update_radio_params(system_address);
  241. // Only "data pipe" enabled by default
  242. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << GZP_DATA_PIPE));
  243. gzp_pairing_enabled_f = false;
  244. gzp_address_exchanged_f = false;
  245. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  246. gzp_encrypted_user_data_length = 0;
  247. // Infinite RX timeout
  248. gzll_set_rx_timeout(0);
  249. }
  250. void gzp_pairing_enable(bool enable)
  251. {
  252. if (gzp_pairing_enabled_f != enable)
  253. {
  254. gzll_goto_idle();
  255. if (enable)
  256. {
  257. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << GZP_PAIRING_PIPE));
  258. }
  259. else
  260. {
  261. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() & ~(1 << GZP_PAIRING_PIPE));
  262. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  263. }
  264. gzp_pairing_enabled_f = enable;
  265. gzll_rx_start();
  266. }
  267. }
  268. void gzp_host_execute()
  269. {
  270. bool gzp_packet_received = false;
  271. uint32_t payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
  272. uint8_t rx_payload[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH];
  273. gzp_address_exchanged_f = false;
  274. if (nrf_gzll_get_rx_fifo_packet_count(GZP_PAIRING_PIPE) > 0)
  275. {
  276. gzp_packet_received = nrf_gzll_fetch_packet_from_rx_fifo(GZP_PAIRING_PIPE, rx_payload, &payload_length);
  277. }
  278. if (!gzp_packet_received && (gzp_encrypted_user_data_length == 0))
  279. {
  280. if (nrf_gzll_get_rx_fifo_packet_count(GZP_DATA_PIPE) > 0)
  281. {
  282. gzp_packet_received = nrf_gzll_fetch_packet_from_rx_fifo(GZP_DATA_PIPE, rx_payload, &payload_length);
  283. }
  284. }
  285. if (gzp_packet_received)
  286. {
  287. //lint -save -esym(644,rx_payload) //may not have been initialized
  288. switch (rx_payload[0])
  289. {
  290. case GZP_CMD_HOST_ADDRESS_REQ:
  291. gzp_process_address_req(rx_payload);
  292. break;
  293. #ifndef GZP_CRYPT_DISABLE
  294. case GZP_CMD_HOST_ID_REQ:
  295. gzp_process_id_req(rx_payload);
  296. break;
  297. case GZP_CMD_HOST_ID_FETCH:
  298. gzp_process_id_fetch(rx_payload);
  299. break;
  300. case GZP_CMD_KEY_UPDATE_PREPARE:
  301. gzp_process_key_update_prepare();
  302. break;
  303. case GZP_CMD_KEY_UPDATE:
  304. gzp_process_key_update(rx_payload);
  305. break;
  306. case GZP_CMD_ENCRYPTED_USER_DATA:
  307. gzp_process_encrypted_user_data(rx_payload, payload_length);
  308. break;
  309. #endif
  310. case GZP_CMD_FETCH_RESP:
  311. default:
  312. break;
  313. }
  314. }
  315. // Restart reception if "not proximity backoff" period has elapsed
  316. if (!nrf_gzll_is_enabled())
  317. {
  318. gzll_set_rx_timeout(0);
  319. if (gzp_pairing_enabled_f)
  320. {
  321. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << GZP_PAIRING_PIPE));
  322. }
  323. gzll_rx_start();
  324. }
  325. #ifndef GZP_CRYPT_DISABLE
  326. gzp_session_counter_inc();
  327. #endif
  328. }
  329. void gzll_rx_start(void)
  330. {
  331. if (nrf_gzll_get_mode() != NRF_GZLL_MODE_HOST)
  332. {
  333. gzll_goto_idle();
  334. (void)nrf_gzll_set_mode(NRF_GZLL_MODE_HOST);
  335. }
  336. if (!nrf_gzll_is_enabled())
  337. {
  338. (void)nrf_gzll_enable();
  339. }
  340. }
  341. bool gzp_id_req_received()
  342. {
  343. return (gzp_id_req_stat != GZP_ID_REQ_IDLE);
  344. }
  345. void gzp_id_req_reject()
  346. {
  347. if (gzp_id_req_received())
  348. {
  349. gzp_id_req_stat = GZP_ID_REQ_PENDING_AND_REJECTED;
  350. }
  351. }
  352. void gzp_id_req_grant()
  353. {
  354. if (gzp_id_req_received())
  355. {
  356. gzp_id_req_stat = GZP_ID_REQ_PENDING_AND_GRANTED;
  357. }
  358. }
  359. void gzp_id_req_cancel()
  360. {
  361. if (gzp_id_req_received())
  362. {
  363. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  364. }
  365. }
  366. //-----------------------------------------------------------------------------
  367. // Implementation: Static functions
  368. //-----------------------------------------------------------------------------
  369. static void gzp_process_address_req(uint8_t* gzp_req)
  370. {
  371. uint8_t temp_rx_pipes;
  372. uint8_t pairing_resp[GZP_CMD_HOST_ADDRESS_RESP_PAYLOAD_LENGTH];
  373. uint32_t rx_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
  374. gzp_address_exchanged_f = false;
  375. gzll_goto_idle();
  376. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  377. temp_rx_pipes = nrf_gzll_get_rx_pipes_enabled();
  378. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  379. // If requesting Device within close proximity
  380. if (prev_gzp_rx_info.rssi >= GZP_HOST_RX_POWER_THRESHOLD)
  381. {
  382. (void)nrf_gzll_set_rx_pipes_enabled(0);
  383. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  384. gzll_set_rx_timeout(GZP_CLOSE_PROXIMITY_BACKOFF_RX_TIMEOUT);
  385. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  386. gzll_rx_fifo_flush();
  387. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  388. // Start "proximity" back off period
  389. gzll_rx_start();
  390. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  391. while (nrf_gzll_is_enabled())
  392. {}
  393. // Build pairing response packet
  394. pairing_resp[0] = (uint8_t)GZP_CMD_HOST_ADDRESS_RESP;
  395. gzp_host_chip_id_read(&pairing_resp[GZP_CMD_HOST_ADDRESS_RESP_ADDRESS], GZP_SYSTEM_ADDRESS_WIDTH);
  396. (void)nrf_gzll_add_packet_to_tx_fifo(0, &pairing_resp[0], GZP_CMD_HOST_ADDRESS_RESP_PAYLOAD_LENGTH);
  397. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  398. gzll_set_rx_timeout(GZP_STEP1_RX_TIMEOUT);
  399. // Enable only pairing pipe when waiting for pairing request step 1
  400. (void)nrf_gzll_set_rx_pipes_enabled((1 << GZP_PAIRING_PIPE));
  401. gzll_rx_start();
  402. while (nrf_gzll_is_enabled())
  403. {
  404. if (nrf_gzll_get_rx_fifo_packet_count(GZP_PAIRING_PIPE))
  405. {
  406. (void)nrf_gzll_fetch_packet_from_rx_fifo(GZP_PAIRING_PIPE, &gzp_req[0], &rx_payload_length);
  407. // Validate step 1 of pairing request
  408. if (gzp_req[0] == (uint8_t)GZP_CMD_HOST_ADDRESS_FETCH)
  409. {
  410. gzp_address_exchanged_f = true;
  411. }
  412. }
  413. }
  414. gzll_tx_fifo_flush();
  415. gzll_rx_fifo_flush();
  416. gzll_set_rx_timeout(0);
  417. (void)nrf_gzll_set_rx_pipes_enabled(temp_rx_pipes);
  418. // Return to normal operation
  419. gzll_rx_start();
  420. }
  421. else
  422. {
  423. (void)nrf_gzll_set_rx_pipes_enabled(temp_rx_pipes & ~(1 << GZP_PAIRING_PIPE));
  424. gzll_set_rx_timeout(GZP_NOT_PROXIMITY_BACKOFF_RX_TIMEOUT);
  425. // Start "not proximity" backoff period
  426. gzll_rx_start();
  427. }
  428. }
  429. static void gzp_preload_ack(uint8_t* src, uint8_t length, uint8_t pipe)
  430. {
  431. gzll_goto_idle();
  432. gzll_tx_fifo_flush();
  433. (void)nrf_gzll_add_packet_to_tx_fifo(pipe, src, length);
  434. gzll_rx_start();
  435. }
  436. static void gzll_set_rx_timeout(uint32_t timeout)
  437. {
  438. timeout *= 2; // * 2 as gzll_set_rx_timeout() takes RX_PERIODS as input, which equals 2 timeslots.
  439. nrf_gzll_set_auto_disable(timeout);
  440. }
  441. bool gzp_address_exchanged()
  442. {
  443. return gzp_address_exchanged_f;
  444. }
  445. #ifndef GZP_CRYPT_DISABLE
  446. bool gzp_crypt_user_data_received()
  447. {
  448. return (gzp_encrypted_user_data_length > 0);
  449. }
  450. bool gzp_crypt_user_data_read(uint8_t* dst, uint8_t* length)
  451. {
  452. if (gzp_encrypted_user_data_length > 0)
  453. {
  454. memcpy(dst, (void*)gzp_encrypted_user_data, gzp_encrypted_user_data_length);
  455. if (length != NULL)
  456. {
  457. *length = gzp_encrypted_user_data_length;
  458. }
  459. gzp_encrypted_user_data_length = 0;
  460. return true;
  461. }
  462. else
  463. {
  464. return false;
  465. }
  466. }
  467. static void gzp_session_counter_inc()
  468. {
  469. uint8_t i;
  470. for (i = 0; i < GZP_SESSION_TOKEN_LENGTH; i++)
  471. {
  472. gzp_session_counter[i]++;
  473. if (gzp_session_counter[i] != 0)
  474. {
  475. break;
  476. }
  477. }
  478. }
  479. static void gzp_get_session_counter(uint8_t* dst)
  480. {
  481. memcpy(dst, (void*)gzp_session_counter, GZP_SESSION_TOKEN_LENGTH);
  482. }
  483. static void gzp_set_host_id(const uint8_t* src)
  484. {
  485. if (*((uint8_t*)database) == 0xff)
  486. {
  487. nrf_nvmc_write_bytes(GZP_PARAMS_STORAGE_ADR + 1, src, GZP_HOST_ID_LENGTH);
  488. nrf_nvmc_write_byte(GZP_PARAMS_STORAGE_ADR, 0x00);
  489. }
  490. }
  491. void gzp_get_host_id(uint8_t *dst)
  492. {
  493. memcpy(dst, (uint8_t*)GZP_PARAMS_STORAGE_ADR + 1, GZP_HOST_ID_LENGTH);
  494. }
  495. static void gzp_process_id_req(uint8_t* rx_payload)
  496. {
  497. int i;
  498. uint8_t temp_host_id[GZP_HOST_ID_LENGTH];
  499. if (gzp_pairing_enabled_f)
  500. {
  501. if (!gzp_id_req_received())
  502. {
  503. gzp_crypt_set_session_token(&rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN]);
  504. gzp_id_req_stat = GZP_ID_REQ_PENDING;
  505. }
  506. gzp_get_host_id(temp_host_id);
  507. // Added:
  508. for (i = 0; i < GZP_HOST_ID_LENGTH; i++)
  509. {
  510. if (temp_host_id[i] != 0xFF)
  511. {
  512. break;
  513. }
  514. }
  515. if (i == GZP_HOST_ID_LENGTH) // If host not generated yet
  516. {
  517. gzp_get_session_counter(temp_host_id);
  518. #if (GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH)
  519. gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_SESSION_TOKEN_LENGTH);
  520. #else //(GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH)
  521. gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_HOST_ID_LENGTH);
  522. #endif //(GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH)
  523. gzp_set_host_id(temp_host_id);
  524. }
  525. }
  526. }
  527. static void gzp_process_id_fetch(uint8_t* rx_payload)
  528. {
  529. uint8_t tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH];
  530. if (gzp_id_req_received())
  531. {
  532. gzp_crypt_select_key(GZP_ID_EXCHANGE);
  533. gzp_crypt(&rx_payload[1], &rx_payload[1], GZP_CMD_HOST_ID_FETCH_PAYLOAD_LENGTH - 1);
  534. if (gzp_validate_id(&rx_payload[GZP_CMD_HOST_ID_FETCH_VALIDATION_ID]))
  535. {
  536. switch (gzp_id_req_stat)
  537. {
  538. case GZP_ID_REQ_PENDING_AND_GRANTED:
  539. tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_STATUS] = (uint8_t)GZP_ID_RESP_GRANTED;
  540. gzp_get_host_id(&tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_HOST_ID]);
  541. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  542. break;
  543. case GZP_ID_REQ_PENDING_AND_REJECTED:
  544. tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_STATUS] = (uint8_t)GZP_ID_RESP_REJECTED;
  545. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  546. break;
  547. case GZP_ID_REQ_PENDING:
  548. default:
  549. tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_STATUS] = (uint8_t)GZP_ID_RESP_PENDING;
  550. break;
  551. }
  552. tx_payload[0] = (uint8_t)GZP_CMD_HOST_ID_FETCH_RESP;
  553. gzp_add_validation_id(&tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_VALIDATION_ID]);
  554. gzp_crypt(&tx_payload[1], &tx_payload[1], GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH - 1);
  555. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  556. gzp_preload_ack(tx_payload, GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH, GZP_DATA_PIPE);
  557. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  558. }
  559. }
  560. }
  561. static void gzp_process_key_update_prepare()
  562. {
  563. uint8_t tx_payload[GZP_CMD_KEY_UPDATE_PREPARE_RESP_PAYLOAD_LENGTH];
  564. tx_payload[0] = (uint8_t)GZP_CMD_KEY_UPDATE_PREPARE_RESP;
  565. gzp_get_session_counter(&tx_payload[GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN]);
  566. // Update session token if no ID request is pending
  567. if (!gzp_id_req_received())
  568. {
  569. gzp_crypt_set_session_token(&tx_payload[GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN]);
  570. }
  571. gzp_preload_ack(tx_payload, GZP_CMD_KEY_UPDATE_PREPARE_RESP_PAYLOAD_LENGTH, GZP_DATA_PIPE);
  572. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  573. }
  574. static void gzp_process_key_update(uint8_t* rx_payload)
  575. {
  576. gzp_crypt_select_key(GZP_KEY_EXCHANGE);
  577. gzp_crypt(&rx_payload[1], &rx_payload[1], GZP_CMD_KEY_UPDATE_PAYLOAD_LENGTH - 1);
  578. if (gzp_validate_id(&rx_payload[GZP_CMD_KEY_UPDATE_VALIDATION_ID]))
  579. {
  580. gzp_crypt_set_dyn_key(&rx_payload[GZP_CMD_KEY_UPDATE_NEW_KEY]);
  581. }
  582. }
  583. static void gzp_process_encrypted_user_data(uint8_t* rx_payload, uint8_t length)
  584. {
  585. uint8_t tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_PAYLOAD_LENGTH];
  586. if (gzp_id_req_received())
  587. {
  588. gzp_crypt_select_key(GZP_ID_EXCHANGE);
  589. }
  590. else
  591. {
  592. gzp_crypt_select_key(GZP_DATA_EXCHANGE);
  593. }
  594. gzp_crypt(&rx_payload[1], &rx_payload[1], length - 1);
  595. if (gzp_validate_id(&rx_payload[GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID]))
  596. {
  597. gzp_encrypted_user_data_length = length - GZP_ENCRYPTED_USER_DATA_PACKET_OVERHEAD;
  598. memcpy((void*)gzp_encrypted_user_data, &rx_payload[GZP_CMD_ENCRYPTED_USER_DATA_PAYLOAD], gzp_encrypted_user_data_length);
  599. }
  600. // Build response packet
  601. tx_payload[0] = (uint8_t)GZP_CMD_ENCRYPTED_USER_DATA_RESP;
  602. gzp_add_validation_id(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID]);
  603. gzp_crypt(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID], &tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID], GZP_VALIDATION_ID_LENGTH);
  604. gzp_get_session_counter(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN]);
  605. // Update "session token" only if no ID request is pending
  606. if (!gzp_id_req_received())
  607. {
  608. gzp_crypt_set_session_token(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN]);
  609. }
  610. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  611. gzp_preload_ack(tx_payload, GZP_CMD_ENCRYPTED_USER_DATA_RESP_PAYLOAD_LENGTH, GZP_DATA_PIPE);
  612. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  613. }
  614. //-----------------------------------------------------------------------------
  615. // Function added during LE1 -> nRF51 port
  616. //-----------------------------------------------------------------------------
  617. static void gzll_goto_idle()
  618. {
  619. nrf_gzll_disable();
  620. while (nrf_gzll_is_enabled())
  621. {}
  622. }
  623. static void gzll_tx_fifo_flush(void)
  624. {
  625. int i;
  626. for (i = 0; i < NRF_GZLL_CONST_PIPE_COUNT; i++)
  627. {
  628. (void)nrf_gzll_flush_tx_fifo(i);
  629. }
  630. }
  631. static void gzll_rx_fifo_flush(void)
  632. {
  633. int i;
  634. for (i = 0; i < NRF_GZLL_CONST_PIPE_COUNT; i++)
  635. {
  636. (void)nrf_gzll_flush_rx_fifo(i);
  637. }
  638. }
  639. /******************************************************************************/
  640. // Implementation: Gazell callback functions
  641. /******************************************************************************/
  642. void nrf_gzll_device_tx_failed(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
  643. {
  644. }
  645. void nrf_gzll_device_tx_success(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
  646. {
  647. }
  648. void nrf_gzll_disabled(void)
  649. {
  650. }
  651. void nrf_gzll_host_rx_data_ready(uint32_t pipe, nrf_gzll_host_rx_info_t rx_info)
  652. {
  653. if (pipe == GZP_PAIRING_PIPE)
  654. {
  655. prev_gzp_rx_info = rx_info;
  656. }
  657. }
  658. /** @} */
  659. /** @} */
  660. #endif