nrf_gzp_host.c 21 KB

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