nrf_gzp_host.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /**
  2. * Copyright (c) 2009 - 2020, 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. #elif defined(__GNUC__)
  210. static const uint32_t database[GZP_DEVICE_PARAMS_STORAGE_SIZE / 4] __attribute__((section(".gzll_paring")))
  211. #else
  212. static const uint32_t database[GZP_DEVICE_PARAMS_STORAGE_SIZE / 4] __attribute__((at(GZP_PARAMS_DB_ADR)))
  213. #endif
  214. = {
  215. #define STATIC_INIT_VALUE 0xFFFFFFFF
  216. #define STATIC_INIT_COUNT (GZP_DEVICE_PARAMS_STORAGE_SIZE / 4)
  217. #define INIT_1 STATIC_INIT_VALUE,
  218. #define INIT_4 REP4(INIT_1)
  219. #define INIT_16 REP4(INIT_4)
  220. #define INIT_64 REP4(INIT_16)
  221. #define INIT_256 REP4(INIT_64)
  222. #define INIT_1024 REP4(INIT_256)
  223. #if (STATIC_INIT_COUNT == 256)
  224. INIT_256
  225. #elif (STATIC_INIT_COUNT == 1024)
  226. INIT_1024
  227. #else
  228. #error Gazell Pairing Library database not initialized properly!
  229. #endif
  230. }; ///< Database for storing keys.
  231. /** @} */
  232. /******************************************************************************/
  233. // Implementation: Host-specific API functions
  234. /******************************************************************************/
  235. void gzp_init()
  236. {
  237. uint8_t system_address[GZP_SYSTEM_ADDRESS_WIDTH];
  238. // Read "chip id", of which 4 bytes (GZP_SYSTEM_ADDRESS_WIDTH)
  239. // are used as system address
  240. gzp_host_chip_id_read(system_address, GZP_SYSTEM_ADDRESS_WIDTH);
  241. // Set up radio parameters (addresses and channel subset) from system_address
  242. (void)gzp_update_radio_params(system_address);
  243. // Only "data pipe" enabled by default
  244. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << GZP_DATA_PIPE));
  245. gzp_pairing_enabled_f = false;
  246. gzp_address_exchanged_f = false;
  247. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  248. gzp_encrypted_user_data_length = 0;
  249. // Infinite RX timeout
  250. gzll_set_rx_timeout(0);
  251. }
  252. void gzp_pairing_enable(bool enable)
  253. {
  254. if (gzp_pairing_enabled_f != enable)
  255. {
  256. gzll_goto_idle();
  257. if (enable)
  258. {
  259. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << GZP_PAIRING_PIPE));
  260. }
  261. else
  262. {
  263. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() & ~(1 << GZP_PAIRING_PIPE));
  264. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  265. }
  266. gzp_pairing_enabled_f = enable;
  267. gzll_rx_start();
  268. }
  269. }
  270. void gzp_host_execute()
  271. {
  272. bool gzp_packet_received = false;
  273. uint32_t payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
  274. uint8_t rx_payload[NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH];
  275. gzp_address_exchanged_f = false;
  276. if (nrf_gzll_get_rx_fifo_packet_count(GZP_PAIRING_PIPE) > 0)
  277. {
  278. gzp_packet_received = nrf_gzll_fetch_packet_from_rx_fifo(GZP_PAIRING_PIPE, rx_payload, &payload_length);
  279. }
  280. if (!gzp_packet_received && (gzp_encrypted_user_data_length == 0))
  281. {
  282. if (nrf_gzll_get_rx_fifo_packet_count(GZP_DATA_PIPE) > 0)
  283. {
  284. gzp_packet_received = nrf_gzll_fetch_packet_from_rx_fifo(GZP_DATA_PIPE, rx_payload, &payload_length);
  285. }
  286. }
  287. if (gzp_packet_received)
  288. {
  289. //lint -save -esym(644,rx_payload) //may not have been initialized
  290. switch (rx_payload[0])
  291. {
  292. case GZP_CMD_HOST_ADDRESS_REQ:
  293. gzp_process_address_req(rx_payload);
  294. break;
  295. #ifndef GZP_CRYPT_DISABLE
  296. case GZP_CMD_HOST_ID_REQ:
  297. gzp_process_id_req(rx_payload);
  298. break;
  299. case GZP_CMD_HOST_ID_FETCH:
  300. gzp_process_id_fetch(rx_payload);
  301. break;
  302. case GZP_CMD_KEY_UPDATE_PREPARE:
  303. gzp_process_key_update_prepare();
  304. break;
  305. case GZP_CMD_KEY_UPDATE:
  306. gzp_process_key_update(rx_payload);
  307. break;
  308. case GZP_CMD_ENCRYPTED_USER_DATA:
  309. gzp_process_encrypted_user_data(rx_payload, payload_length);
  310. break;
  311. #endif
  312. case GZP_CMD_FETCH_RESP:
  313. default:
  314. break;
  315. }
  316. }
  317. // Restart reception if "not proximity backoff" period has elapsed
  318. if (!nrf_gzll_is_enabled())
  319. {
  320. gzll_set_rx_timeout(0);
  321. if (gzp_pairing_enabled_f)
  322. {
  323. (void)nrf_gzll_set_rx_pipes_enabled(nrf_gzll_get_rx_pipes_enabled() | (1 << GZP_PAIRING_PIPE));
  324. }
  325. gzll_rx_start();
  326. }
  327. #ifndef GZP_CRYPT_DISABLE
  328. gzp_session_counter_inc();
  329. #endif
  330. }
  331. void gzll_rx_start(void)
  332. {
  333. if (nrf_gzll_get_mode() != NRF_GZLL_MODE_HOST)
  334. {
  335. gzll_goto_idle();
  336. (void)nrf_gzll_set_mode(NRF_GZLL_MODE_HOST);
  337. }
  338. if (!nrf_gzll_is_enabled())
  339. {
  340. (void)nrf_gzll_enable();
  341. }
  342. }
  343. bool gzp_id_req_received()
  344. {
  345. return (gzp_id_req_stat != GZP_ID_REQ_IDLE);
  346. }
  347. void gzp_id_req_reject()
  348. {
  349. if (gzp_id_req_received())
  350. {
  351. gzp_id_req_stat = GZP_ID_REQ_PENDING_AND_REJECTED;
  352. }
  353. }
  354. void gzp_id_req_grant()
  355. {
  356. if (gzp_id_req_received())
  357. {
  358. gzp_id_req_stat = GZP_ID_REQ_PENDING_AND_GRANTED;
  359. }
  360. }
  361. void gzp_id_req_cancel()
  362. {
  363. if (gzp_id_req_received())
  364. {
  365. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  366. }
  367. }
  368. //-----------------------------------------------------------------------------
  369. // Implementation: Static functions
  370. //-----------------------------------------------------------------------------
  371. static void gzp_process_address_req(uint8_t* gzp_req)
  372. {
  373. uint8_t temp_rx_pipes;
  374. uint8_t pairing_resp[GZP_CMD_HOST_ADDRESS_RESP_PAYLOAD_LENGTH];
  375. uint32_t rx_payload_length = NRF_GZLL_CONST_MAX_PAYLOAD_LENGTH;
  376. gzp_address_exchanged_f = false;
  377. gzll_goto_idle();
  378. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  379. temp_rx_pipes = nrf_gzll_get_rx_pipes_enabled();
  380. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  381. // If requesting Device within close proximity
  382. if (prev_gzp_rx_info.rssi >= GZP_HOST_RX_POWER_THRESHOLD)
  383. {
  384. (void)nrf_gzll_set_rx_pipes_enabled(0);
  385. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  386. gzll_set_rx_timeout(GZP_CLOSE_PROXIMITY_BACKOFF_RX_TIMEOUT);
  387. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  388. gzll_rx_fifo_flush();
  389. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  390. // Start "proximity" back off period
  391. gzll_rx_start();
  392. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  393. while (nrf_gzll_is_enabled())
  394. {}
  395. // Build pairing response packet
  396. pairing_resp[0] = (uint8_t)GZP_CMD_HOST_ADDRESS_RESP;
  397. gzp_host_chip_id_read(&pairing_resp[GZP_CMD_HOST_ADDRESS_RESP_ADDRESS], GZP_SYSTEM_ADDRESS_WIDTH);
  398. (void)nrf_gzll_add_packet_to_tx_fifo(0, &pairing_resp[0], GZP_CMD_HOST_ADDRESS_RESP_PAYLOAD_LENGTH);
  399. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  400. gzll_set_rx_timeout(GZP_STEP1_RX_TIMEOUT);
  401. // Enable only pairing pipe when waiting for pairing request step 1
  402. (void)nrf_gzll_set_rx_pipes_enabled((1 << GZP_PAIRING_PIPE));
  403. gzll_rx_start();
  404. while (nrf_gzll_is_enabled())
  405. {
  406. if (nrf_gzll_get_rx_fifo_packet_count(GZP_PAIRING_PIPE))
  407. {
  408. (void)nrf_gzll_fetch_packet_from_rx_fifo(GZP_PAIRING_PIPE, &gzp_req[0], &rx_payload_length);
  409. // Validate step 1 of pairing request
  410. if (gzp_req[0] == (uint8_t)GZP_CMD_HOST_ADDRESS_FETCH)
  411. {
  412. gzp_address_exchanged_f = true;
  413. }
  414. }
  415. }
  416. gzll_tx_fifo_flush();
  417. gzll_rx_fifo_flush();
  418. gzll_set_rx_timeout(0);
  419. (void)nrf_gzll_set_rx_pipes_enabled(temp_rx_pipes);
  420. // Return to normal operation
  421. gzll_rx_start();
  422. }
  423. else
  424. {
  425. (void)nrf_gzll_set_rx_pipes_enabled(temp_rx_pipes & ~(1 << GZP_PAIRING_PIPE));
  426. gzll_set_rx_timeout(GZP_NOT_PROXIMITY_BACKOFF_RX_TIMEOUT);
  427. // Start "not proximity" backoff period
  428. gzll_rx_start();
  429. }
  430. }
  431. static void gzp_preload_ack(uint8_t* src, uint8_t length, uint8_t pipe)
  432. {
  433. gzll_goto_idle();
  434. gzll_tx_fifo_flush();
  435. (void)nrf_gzll_add_packet_to_tx_fifo(pipe, src, length);
  436. gzll_rx_start();
  437. }
  438. static void gzll_set_rx_timeout(uint32_t timeout)
  439. {
  440. timeout *= 2; // * 2 as gzll_set_rx_timeout() takes RX_PERIODS as input, which equals 2 timeslots.
  441. nrf_gzll_set_auto_disable(timeout);
  442. }
  443. bool gzp_address_exchanged()
  444. {
  445. return gzp_address_exchanged_f;
  446. }
  447. #ifndef GZP_CRYPT_DISABLE
  448. bool gzp_crypt_user_data_received()
  449. {
  450. return (gzp_encrypted_user_data_length > 0);
  451. }
  452. bool gzp_crypt_user_data_read(uint8_t* dst, uint8_t* length)
  453. {
  454. if (gzp_encrypted_user_data_length > 0)
  455. {
  456. memcpy(dst, (void*)gzp_encrypted_user_data, gzp_encrypted_user_data_length);
  457. if (length != NULL)
  458. {
  459. *length = gzp_encrypted_user_data_length;
  460. }
  461. gzp_encrypted_user_data_length = 0;
  462. return true;
  463. }
  464. else
  465. {
  466. return false;
  467. }
  468. }
  469. static void gzp_session_counter_inc()
  470. {
  471. uint8_t i;
  472. for (i = 0; i < GZP_SESSION_TOKEN_LENGTH; i++)
  473. {
  474. gzp_session_counter[i]++;
  475. if (gzp_session_counter[i] != 0)
  476. {
  477. break;
  478. }
  479. }
  480. }
  481. static void gzp_get_session_counter(uint8_t* dst)
  482. {
  483. memcpy(dst, (void*)gzp_session_counter, GZP_SESSION_TOKEN_LENGTH);
  484. }
  485. static void gzp_set_host_id(const uint8_t* src)
  486. {
  487. if (*((uint8_t*)database) == 0xff)
  488. {
  489. nrf_nvmc_write_bytes(GZP_PARAMS_STORAGE_ADR + 1, src, GZP_HOST_ID_LENGTH);
  490. nrf_nvmc_write_byte(GZP_PARAMS_STORAGE_ADR, 0x00);
  491. }
  492. }
  493. void gzp_get_host_id(uint8_t *dst)
  494. {
  495. memcpy(dst, (uint8_t*)GZP_PARAMS_STORAGE_ADR + 1, GZP_HOST_ID_LENGTH);
  496. }
  497. static void gzp_process_id_req(uint8_t* rx_payload)
  498. {
  499. int i;
  500. uint8_t temp_host_id[GZP_HOST_ID_LENGTH];
  501. if (gzp_pairing_enabled_f)
  502. {
  503. if (!gzp_id_req_received())
  504. {
  505. gzp_crypt_set_session_token(&rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN]);
  506. gzp_id_req_stat = GZP_ID_REQ_PENDING;
  507. }
  508. gzp_get_host_id(temp_host_id);
  509. // Added:
  510. for (i = 0; i < GZP_HOST_ID_LENGTH; i++)
  511. {
  512. if (temp_host_id[i] != 0xFF)
  513. {
  514. break;
  515. }
  516. }
  517. if (i == GZP_HOST_ID_LENGTH) // If host not generated yet
  518. {
  519. gzp_get_session_counter(temp_host_id);
  520. #if (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_SESSION_TOKEN_LENGTH);
  522. #else //(GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH)
  523. gzp_xor_cipher(temp_host_id, temp_host_id, &rx_payload[GZP_CMD_HOST_ID_REQ_SESSION_TOKEN], GZP_HOST_ID_LENGTH);
  524. #endif //(GZP_HOST_ID_LENGTH > GZP_SESSION_TOKEN_LENGTH)
  525. gzp_set_host_id(temp_host_id);
  526. }
  527. }
  528. }
  529. static void gzp_process_id_fetch(uint8_t* rx_payload)
  530. {
  531. uint8_t tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH];
  532. if (gzp_id_req_received())
  533. {
  534. gzp_crypt_select_key(GZP_ID_EXCHANGE);
  535. gzp_crypt(&rx_payload[1], &rx_payload[1], GZP_CMD_HOST_ID_FETCH_PAYLOAD_LENGTH - 1);
  536. if (gzp_validate_id(&rx_payload[GZP_CMD_HOST_ID_FETCH_VALIDATION_ID]))
  537. {
  538. switch (gzp_id_req_stat)
  539. {
  540. case GZP_ID_REQ_PENDING_AND_GRANTED:
  541. tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_STATUS] = (uint8_t)GZP_ID_RESP_GRANTED;
  542. gzp_get_host_id(&tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_HOST_ID]);
  543. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  544. break;
  545. case GZP_ID_REQ_PENDING_AND_REJECTED:
  546. tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_STATUS] = (uint8_t)GZP_ID_RESP_REJECTED;
  547. gzp_id_req_stat = GZP_ID_REQ_IDLE;
  548. break;
  549. case GZP_ID_REQ_PENDING:
  550. default:
  551. tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_STATUS] = (uint8_t)GZP_ID_RESP_PENDING;
  552. break;
  553. }
  554. tx_payload[0] = (uint8_t)GZP_CMD_HOST_ID_FETCH_RESP;
  555. gzp_add_validation_id(&tx_payload[GZP_CMD_HOST_ID_FETCH_RESP_VALIDATION_ID]);
  556. gzp_crypt(&tx_payload[1], &tx_payload[1], GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH - 1);
  557. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  558. gzp_preload_ack(tx_payload, GZP_CMD_HOST_ID_FETCH_RESP_PAYLOAD_LENGTH, GZP_DATA_PIPE);
  559. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  560. }
  561. }
  562. }
  563. static void gzp_process_key_update_prepare()
  564. {
  565. uint8_t tx_payload[GZP_CMD_KEY_UPDATE_PREPARE_RESP_PAYLOAD_LENGTH];
  566. tx_payload[0] = (uint8_t)GZP_CMD_KEY_UPDATE_PREPARE_RESP;
  567. gzp_get_session_counter(&tx_payload[GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN]);
  568. // Update session token if no ID request is pending
  569. if (!gzp_id_req_received())
  570. {
  571. gzp_crypt_set_session_token(&tx_payload[GZP_CMD_KEY_UPDATE_PREPARE_RESP_SESSION_TOKEN]);
  572. }
  573. gzp_preload_ack(tx_payload, GZP_CMD_KEY_UPDATE_PREPARE_RESP_PAYLOAD_LENGTH, GZP_DATA_PIPE);
  574. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  575. }
  576. static void gzp_process_key_update(uint8_t* rx_payload)
  577. {
  578. gzp_crypt_select_key(GZP_KEY_EXCHANGE);
  579. gzp_crypt(&rx_payload[1], &rx_payload[1], GZP_CMD_KEY_UPDATE_PAYLOAD_LENGTH - 1);
  580. if (gzp_validate_id(&rx_payload[GZP_CMD_KEY_UPDATE_VALIDATION_ID]))
  581. {
  582. gzp_crypt_set_dyn_key(&rx_payload[GZP_CMD_KEY_UPDATE_NEW_KEY]);
  583. }
  584. }
  585. static void gzp_process_encrypted_user_data(uint8_t* rx_payload, uint8_t length)
  586. {
  587. uint8_t tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_PAYLOAD_LENGTH];
  588. if (gzp_id_req_received())
  589. {
  590. gzp_crypt_select_key(GZP_ID_EXCHANGE);
  591. }
  592. else
  593. {
  594. gzp_crypt_select_key(GZP_DATA_EXCHANGE);
  595. }
  596. gzp_crypt(&rx_payload[1], &rx_payload[1], length - 1);
  597. if (gzp_validate_id(&rx_payload[GZP_CMD_ENCRYPTED_USER_DATA_VALIDATION_ID]))
  598. {
  599. gzp_encrypted_user_data_length = length - GZP_ENCRYPTED_USER_DATA_PACKET_OVERHEAD;
  600. memcpy((void*)gzp_encrypted_user_data, &rx_payload[GZP_CMD_ENCRYPTED_USER_DATA_PAYLOAD], gzp_encrypted_user_data_length);
  601. }
  602. // Build response packet
  603. tx_payload[0] = (uint8_t)GZP_CMD_ENCRYPTED_USER_DATA_RESP;
  604. gzp_add_validation_id(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_VALIDATION_ID]);
  605. 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);
  606. gzp_get_session_counter(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN]);
  607. // Update "session token" only if no ID request is pending
  608. if (!gzp_id_req_received())
  609. {
  610. gzp_crypt_set_session_token(&tx_payload[GZP_CMD_ENCRYPTED_USER_DATA_RESP_SESSION_TOKEN]);
  611. }
  612. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  613. gzp_preload_ack(tx_payload, GZP_CMD_ENCRYPTED_USER_DATA_RESP_PAYLOAD_LENGTH, GZP_DATA_PIPE);
  614. ASSERT(nrf_gzll_get_error_code() == NRF_GZLL_ERROR_CODE_NO_ERROR);
  615. }
  616. //-----------------------------------------------------------------------------
  617. // Function added during LE1 -> nRF51 port
  618. //-----------------------------------------------------------------------------
  619. static void gzll_goto_idle()
  620. {
  621. nrf_gzll_disable();
  622. while (nrf_gzll_is_enabled())
  623. {}
  624. }
  625. static void gzll_tx_fifo_flush(void)
  626. {
  627. int i;
  628. for (i = 0; i < NRF_GZLL_CONST_PIPE_COUNT; i++)
  629. {
  630. (void)nrf_gzll_flush_tx_fifo(i);
  631. }
  632. }
  633. static void gzll_rx_fifo_flush(void)
  634. {
  635. int i;
  636. for (i = 0; i < NRF_GZLL_CONST_PIPE_COUNT; i++)
  637. {
  638. (void)nrf_gzll_flush_rx_fifo(i);
  639. }
  640. }
  641. /******************************************************************************/
  642. // Implementation: Gazell callback functions
  643. /******************************************************************************/
  644. void nrf_gzll_device_tx_failed(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
  645. {
  646. }
  647. void nrf_gzll_device_tx_success(uint32_t pipe, nrf_gzll_device_tx_info_t tx_info)
  648. {
  649. }
  650. void nrf_gzll_disabled(void)
  651. {
  652. }
  653. void nrf_gzll_host_rx_data_ready(uint32_t pipe, nrf_gzll_host_rx_info_t rx_info)
  654. {
  655. if (pipe == GZP_PAIRING_PIPE)
  656. {
  657. prev_gzp_rx_info = rx_info;
  658. }
  659. }
  660. /** @} */
  661. /** @} */
  662. #endif