nrf_ble_escs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. /**
  2. * Copyright (c) 2016 - 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. #include "nrf_ble_escs.h"
  41. #include <string.h>
  42. #include "es_app_config.h"
  43. #ifdef BLE_HANDLER_DEBUG
  44. #include "SEGGER_RTT.h"
  45. #define DEBUG_PRINTF SEGGER_RTT_printf
  46. #else
  47. #define DEBUG_PRINTF(...)
  48. #endif
  49. #define EID_BUFF_SIZE 64
  50. typedef struct
  51. {
  52. uint16_t val_handle;
  53. uint16_t uuid;
  54. } val_handle_to_uuid_t;
  55. static ble_add_char_params_t BROADCAST_CAP_CHAR_INIT =
  56. {
  57. .uuid = BLE_UUID_ESCS_BROADCAST_CAP_CHAR,
  58. .read_access = SEC_OPEN,
  59. .char_props =
  60. {
  61. .read = 1,
  62. },
  63. .is_defered_read = true,
  64. .is_var_len = true,
  65. .init_len = NRF_BLE_ESCS_BROADCAST_CAP_LEN,
  66. .max_len = NRF_BLE_ESCS_BROADCAST_CAP_LEN,
  67. };
  68. static ble_add_char_params_t ACTIVE_SLOT_CHAR_INIT =
  69. {
  70. .uuid = BLE_UUID_ESCS_ACTIVE_SLOT_CHAR,
  71. .read_access = SEC_OPEN,
  72. .write_access = SEC_OPEN,
  73. .char_props =
  74. {
  75. .read = 1,
  76. .write = 1,
  77. },
  78. .is_defered_read = true,
  79. .is_defered_write = true,
  80. .is_value_user = true,
  81. .init_len = sizeof(nrf_ble_escs_active_slot_t),
  82. .max_len = sizeof(nrf_ble_escs_active_slot_t),
  83. };
  84. static ble_add_char_params_t ADV_INTERVAL_CHAR_INIT =
  85. {
  86. .uuid = BLE_UUID_ESCS_ADV_INTERVAL_CHAR,
  87. .read_access = SEC_OPEN,
  88. .write_access = SEC_OPEN,
  89. .char_props =
  90. {
  91. .read = 1,
  92. .write = 1,
  93. },
  94. .is_defered_read = true,
  95. .is_defered_write = true,
  96. .init_len = sizeof(nrf_ble_escs_adv_interval_t),
  97. .max_len = sizeof(nrf_ble_escs_adv_interval_t),
  98. };
  99. static ble_add_char_params_t RADIO_TX_PWR_CHAR_INIT =
  100. {
  101. .uuid = BLE_UUID_ESCS_RADIO_TX_PWR_CHAR,
  102. .read_access = SEC_OPEN,
  103. .write_access = SEC_OPEN,
  104. .char_props =
  105. {
  106. .read = 1,
  107. .write = 1,
  108. },
  109. .is_defered_read = true,
  110. .is_defered_write = true,
  111. .init_len = sizeof(nrf_ble_escs_radio_tx_pwr_t),
  112. .max_len = sizeof(nrf_ble_escs_radio_tx_pwr_t),
  113. };
  114. static ble_add_char_params_t ADV_TX_PWR_CHAR_INIT =
  115. {
  116. .uuid = BLE_UUID_ESCS_ADV_TX_PWR_CHAR,
  117. .read_access = SEC_OPEN,
  118. .write_access = SEC_OPEN,
  119. .char_props =
  120. {
  121. .read = 1,
  122. .write = 1,
  123. },
  124. .is_defered_read = true,
  125. .is_defered_write = true,
  126. .init_len = sizeof(nrf_ble_escs_adv_tx_pwr_t),
  127. .max_len = sizeof(nrf_ble_escs_adv_tx_pwr_t),
  128. };
  129. static ble_add_char_params_t LOCK_STATE_CHAR_INIT =
  130. {
  131. .uuid = BLE_UUID_ESCS_LOCK_STATE_CHAR,
  132. .read_access = SEC_OPEN,
  133. .write_access = SEC_OPEN,
  134. .char_props =
  135. {
  136. .read = 1,
  137. .write = 1,
  138. },
  139. .is_defered_read = true,
  140. .is_defered_write = true,
  141. .is_var_len = true,
  142. .is_value_user = true,
  143. .init_len = 1,
  144. .max_len = 17,
  145. };
  146. static ble_add_char_params_t UNLOCK_CHAR_INIT =
  147. {
  148. .uuid = BLE_UUID_ESCS_UNLOCK_CHAR,
  149. .read_access = SEC_OPEN,
  150. .write_access = SEC_OPEN,
  151. .char_props =
  152. {
  153. .read = 1,
  154. .write = 1,
  155. },
  156. .is_defered_read = true,
  157. .is_defered_write = true,
  158. .init_len = 1,
  159. .max_len = ESCS_AES_KEY_SIZE,
  160. };
  161. static ble_add_char_params_t PUBLIC_ECDH_KEY_CHAR_INIT =
  162. {
  163. .uuid = BLE_UUID_ESCS_PUBLIC_ECDH_KEY_CHAR,
  164. .read_access = SEC_OPEN,
  165. .char_props =
  166. {
  167. .read = 1,
  168. },
  169. .is_defered_read = true,
  170. .is_var_len = true,
  171. .init_len = 1,
  172. .max_len = ESCS_ECDH_KEY_SIZE,
  173. };
  174. static ble_add_char_params_t EID_ID_KEY_CHAR_INIT =
  175. {
  176. .uuid = BLE_UUID_ESCS_EID_ID_KEY_CHAR,
  177. .read_access = SEC_OPEN,
  178. .char_props =
  179. {
  180. .read = 1,
  181. },
  182. .is_defered_read = true,
  183. .is_var_len = true,
  184. .init_len = 1,
  185. .max_len = ESCS_AES_KEY_SIZE,
  186. };
  187. static ble_add_char_params_t RW_ADV_SLOT_CHAR_INIT =
  188. {
  189. .uuid = BLE_UUID_ESCS_RW_ADV_SLOT_CHAR,
  190. .read_access = SEC_OPEN,
  191. .write_access = SEC_OPEN,
  192. .char_props =
  193. {
  194. .read = 1,
  195. .write = 1,
  196. },
  197. .is_defered_read = true,
  198. .is_defered_write = true,
  199. .is_var_len = true,
  200. .max_len = ESCS_ADV_SLOT_CHAR_LENGTH_MAX,
  201. };
  202. static ble_add_char_params_t FACTORY_RESET_CHAR_INIT =
  203. {
  204. .uuid = BLE_UUID_ESCS_FACTORY_RESET_CHAR,
  205. .write_access = SEC_OPEN,
  206. .char_props =
  207. {
  208. .write = 1,
  209. },
  210. .is_defered_write = true,
  211. .init_len = sizeof(nrf_ble_escs_factory_reset_t),
  212. .max_len = sizeof(nrf_ble_escs_factory_reset_t),
  213. };
  214. static ble_add_char_params_t REMAIN_CONNECTABLE_CHAR_INIT =
  215. {
  216. .uuid = BLE_UUID_ESCS_REMAIN_CONNECTABLE_CHAR,
  217. .read_access = SEC_OPEN,
  218. .write_access = SEC_OPEN,
  219. .char_props =
  220. {
  221. .read = 1,
  222. .write = 1,
  223. },
  224. .is_defered_read = true,
  225. .is_defered_write = true,
  226. .init_len = 1,
  227. .max_len = 1,
  228. };
  229. static val_handle_to_uuid_t m_handle_to_uuid_map[BLE_ESCS_NUMBER_OF_CHARACTERISTICS]; //!< Map from handle to UUID.
  230. static uint8_t m_handle_to_uuid_map_idx = 0; //!< Index of map from handle to UUID.
  231. static uint8_t m_eid_mem[EID_BUFF_SIZE] = {0}; //!< Memory buffer used for EID writes.
  232. static ble_user_mem_block_t m_eid_mem_block =
  233. {
  234. .p_mem = m_eid_mem,
  235. .len = EID_BUFF_SIZE
  236. }; //!< Memory block used for EID writes.
  237. /**@brief Function for adding characteristic to Eddystone service.
  238. *
  239. * @param[in] p_escs Eddystone Configuration Service structure.
  240. * @param[in] p_escs_init Information needed to initialize the service.
  241. * @param[in] p_char_init Information needed to initialize the characteristic.
  242. *
  243. * @return NRF_SUCCESS on success, otherwise an error code.
  244. */
  245. static uint32_t char_add(ble_add_char_params_t * p_char_init,
  246. nrf_ble_escs_t * p_escs,
  247. void * p_value,
  248. ble_gatts_char_handles_t * p_handles)
  249. {
  250. uint32_t err_code;
  251. VERIFY_PARAM_NOT_NULL(p_char_init);
  252. VERIFY_PARAM_NOT_NULL(p_escs);
  253. VERIFY_PARAM_NOT_NULL(p_value);
  254. VERIFY_PARAM_NOT_NULL(p_handles);
  255. p_char_init->uuid_type = p_escs->uuid_type;
  256. p_char_init->p_init_value = p_value;
  257. err_code = characteristic_add(p_escs->service_handle,
  258. p_char_init,
  259. p_handles);
  260. if (err_code == NRF_SUCCESS)
  261. {
  262. ASSERT(m_handle_to_uuid_map_idx < BLE_ESCS_NUMBER_OF_CHARACTERISTICS);
  263. m_handle_to_uuid_map[m_handle_to_uuid_map_idx].val_handle = p_handles->value_handle;
  264. m_handle_to_uuid_map[m_handle_to_uuid_map_idx].uuid = p_char_init->uuid;
  265. m_handle_to_uuid_map_idx++;
  266. }
  267. return err_code;
  268. }
  269. /**@brief Function for handling the @ref BLE_GAP_EVT_CONNECTED event from the SoftDevice.
  270. *
  271. * @param[in] p_escs Eddystone Configuration Service structure.
  272. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  273. */
  274. static void on_connect(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  275. {
  276. VERIFY_PARAM_NOT_NULL_VOID(p_escs);
  277. p_escs->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  278. }
  279. /**@brief Function for handling the @ref BLE_GAP_EVT_DISCONNECTED event from the SoftDevice.
  280. *
  281. * @param[in] p_escs Eddystone Configuration Service structure.
  282. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  283. */
  284. static void on_disconnect(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  285. {
  286. VERIFY_PARAM_NOT_NULL_VOID(p_escs);
  287. UNUSED_PARAMETER(p_ble_evt);
  288. p_escs->conn_handle = BLE_CONN_HANDLE_INVALID;
  289. }
  290. static uint32_t get_evt_type_for_handle(uint16_t handle, uint16_t * p_uuid)
  291. {
  292. VERIFY_PARAM_NOT_NULL(p_uuid);
  293. for (uint8_t i = 0; i < BLE_ESCS_NUMBER_OF_CHARACTERISTICS; ++i)
  294. {
  295. if (m_handle_to_uuid_map[i].val_handle == handle)
  296. {
  297. *p_uuid = m_handle_to_uuid_map[i].uuid;
  298. return NRF_SUCCESS;
  299. }
  300. }
  301. return NRF_ERROR_NOT_FOUND;
  302. }
  303. /**@brief Function for handling the @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: BLE_GATTS_AUTHORIZE_TYPE_WRITE event from the SoftDevice.
  304. *
  305. * @param[in] p_escs Eddystone Configuration Service structure.
  306. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  307. */
  308. static ret_code_t on_write(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  309. {
  310. uint32_t err_code;
  311. uint16_t write_evt_uuid = 0;
  312. VERIFY_PARAM_NOT_NULL(p_escs);
  313. VERIFY_PARAM_NOT_NULL(p_ble_evt);
  314. ble_gatts_evt_write_t const * p_evt_write =
  315. &p_ble_evt->evt.gatts_evt.params.authorize_request.request.write;
  316. err_code = get_evt_type_for_handle(p_evt_write->handle, &write_evt_uuid);
  317. RETURN_IF_ERROR(err_code);
  318. p_escs->write_evt_handler(p_escs,
  319. write_evt_uuid,
  320. p_evt_write->handle,
  321. p_evt_write->data,
  322. p_evt_write->len);
  323. return NRF_SUCCESS;
  324. }
  325. /**@brief Function for handling the @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST: BLE_GATTS_AUTHORIZE_TYPE_WRITE: event from the SoftDevice.
  326. *
  327. * @param[in] p_escs Eddystone Configuration Service structure.
  328. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  329. */
  330. static void on_long_write(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  331. {
  332. static uint16_t write_evt_uuid;
  333. static bool write_evt_uuid_set = false;
  334. uint32_t err_code;
  335. VERIFY_PARAM_NOT_NULL_VOID(p_escs);
  336. VERIFY_PARAM_NOT_NULL_VOID(p_ble_evt);
  337. ble_gatts_evt_write_t const * p_evt_write =
  338. &p_ble_evt->evt.gatts_evt.params.authorize_request.request.write;
  339. ble_gatts_rw_authorize_reply_params_t reply = {0};
  340. if (p_evt_write->op == BLE_GATTS_OP_PREP_WRITE_REQ)
  341. {
  342. err_code = get_evt_type_for_handle(p_evt_write->handle, &write_evt_uuid);
  343. APP_ERROR_CHECK(err_code);
  344. write_evt_uuid_set = true;
  345. reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  346. reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  347. reply.params.write.update = 0;
  348. reply.params.write.offset = 0;
  349. reply.params.write.len = p_evt_write->len;
  350. reply.params.write.p_data = NULL;
  351. err_code = sd_ble_gatts_rw_authorize_reply(p_escs->conn_handle, &reply);
  352. APP_ERROR_CHECK(err_code);
  353. }
  354. else if (p_evt_write->op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
  355. {
  356. uint8_t value_buffer[ESCS_ADV_SLOT_CHAR_LENGTH_MAX] = {0};
  357. ble_gatts_value_t value =
  358. {
  359. .len = sizeof(value_buffer),
  360. .offset = 0,
  361. .p_value = &(value_buffer[0])
  362. };
  363. ASSERT(write_evt_uuid_set);
  364. write_evt_uuid_set = false;
  365. reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  366. reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  367. reply.params.write.update = 0;
  368. reply.params.write.offset = 0;
  369. reply.params.write.len = p_evt_write->len;
  370. reply.params.write.p_data = NULL;
  371. err_code = sd_ble_gatts_rw_authorize_reply(p_escs->conn_handle, &reply);
  372. APP_ERROR_CHECK(err_code);
  373. // Now that the value has been accepted using 'sd_ble_gatts_rw_authorize_reply', it can be found in the database.
  374. err_code = sd_ble_gatts_value_get( p_escs->conn_handle,
  375. p_escs->rw_adv_slot_handles.value_handle,
  376. &value);
  377. APP_ERROR_CHECK(err_code);
  378. p_escs->write_evt_handler(p_escs,
  379. write_evt_uuid,
  380. p_evt_write->handle,
  381. value.p_value,
  382. value.len);
  383. }
  384. else
  385. {
  386. }
  387. }
  388. /**@brief Function for handling events from the SoftDevice related to long writes.
  389. *
  390. * @param[in] p_escs Eddystone Configuration Service structure.
  391. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  392. */
  393. static ret_code_t on_read(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  394. {
  395. VERIFY_PARAM_NOT_NULL(p_escs);
  396. VERIFY_PARAM_NOT_NULL(p_ble_evt);
  397. ret_code_t err_code;
  398. uint16_t read_evt_uuid = 0;
  399. uint16_t val_handle = p_ble_evt->evt.gatts_evt.params.authorize_request.request.read.handle;
  400. err_code = get_evt_type_for_handle(val_handle, &read_evt_uuid);
  401. RETURN_IF_ERROR(err_code);
  402. p_escs->read_evt_handler(p_escs, read_evt_uuid, val_handle);
  403. return NRF_SUCCESS;
  404. }
  405. static ret_code_t on_rw_authorize_req(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  406. {
  407. ret_code_t err_code;
  408. VERIFY_PARAM_NOT_NULL(p_escs);
  409. VERIFY_PARAM_NOT_NULL(p_ble_evt);
  410. ble_gatts_evt_rw_authorize_request_t const * ar =
  411. &p_ble_evt->evt.gatts_evt.params.authorize_request;
  412. if (ar->type == BLE_GATTS_AUTHORIZE_TYPE_READ)
  413. {
  414. err_code = on_read(p_escs, p_ble_evt);
  415. RETURN_IF_ERROR(err_code);
  416. }
  417. else if (ar->type == BLE_GATTS_AUTHORIZE_TYPE_WRITE)
  418. {
  419. if (ar->request.write.op == BLE_GATTS_OP_WRITE_REQ
  420. || ar->request.write.op == BLE_GATTS_OP_WRITE_CMD)
  421. {
  422. err_code = on_write(p_escs, p_ble_evt);
  423. RETURN_IF_ERROR(err_code);
  424. }
  425. else if (ar->request.write.op == BLE_GATTS_OP_PREP_WRITE_REQ
  426. || ar->request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_NOW)
  427. {
  428. on_long_write(p_escs, p_ble_evt);
  429. }
  430. else if (ar->request.write.op == BLE_GATTS_OP_EXEC_WRITE_REQ_CANCEL)
  431. {
  432. ble_gatts_rw_authorize_reply_params_t auth_reply;
  433. memset(&auth_reply, 0, sizeof(auth_reply));
  434. auth_reply.type = BLE_GATTS_AUTHORIZE_TYPE_WRITE;
  435. auth_reply.params.write.gatt_status = BLE_GATT_STATUS_SUCCESS;
  436. err_code = sd_ble_gatts_rw_authorize_reply(p_ble_evt->evt.gatts_evt.conn_handle, &auth_reply);
  437. VERIFY_SUCCESS(err_code);
  438. }
  439. else
  440. {
  441. }
  442. }
  443. else
  444. {
  445. return NRF_ERROR_INVALID_STATE;
  446. }
  447. return NRF_SUCCESS;
  448. }
  449. ret_code_t nrf_ble_escs_on_ble_evt(nrf_ble_escs_t * p_escs, ble_evt_t const * p_ble_evt)
  450. {
  451. ret_code_t err_code;
  452. VERIFY_PARAM_NOT_NULL(p_escs);
  453. VERIFY_PARAM_NOT_NULL(p_ble_evt);
  454. switch (p_ble_evt->header.evt_id)
  455. {
  456. case BLE_GAP_EVT_CONNECTED:
  457. on_connect(p_escs, p_ble_evt);
  458. break;
  459. case BLE_GAP_EVT_DISCONNECTED:
  460. on_disconnect(p_escs, p_ble_evt);
  461. break;
  462. case BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST:
  463. err_code = on_rw_authorize_req(p_escs, p_ble_evt);
  464. VERIFY_SUCCESS(err_code);
  465. break;
  466. // BLE_EVT_USER_MEM_REQUEST & BLE_EVT_USER_MEM_RELEASE are for long writes to the RW ADV slot characteristic
  467. case BLE_EVT_USER_MEM_REQUEST:
  468. err_code = sd_ble_user_mem_reply(p_escs->conn_handle, &m_eid_mem_block);
  469. VERIFY_SUCCESS(err_code);
  470. break;
  471. case BLE_EVT_USER_MEM_RELEASE:
  472. break;
  473. default:
  474. // No implementation needed.
  475. break;
  476. }
  477. return NRF_SUCCESS;
  478. }
  479. ret_code_t nrf_ble_escs_init(nrf_ble_escs_t * p_escs, const nrf_ble_escs_init_t * p_escs_init)
  480. {
  481. uint32_t err_code;
  482. ble_uuid_t ble_uuid;
  483. ble_uuid128_t ecs_base_uuid = ESCS_BASE_UUID;
  484. uint8_t zero_val = 0;
  485. VERIFY_PARAM_NOT_NULL(p_escs);
  486. VERIFY_PARAM_NOT_NULL(p_escs_init);
  487. // Initialize the service structure.
  488. p_escs->conn_handle = BLE_CONN_HANDLE_INVALID;
  489. p_escs->write_evt_handler = p_escs_init->write_evt_handler;
  490. p_escs->read_evt_handler = p_escs_init->read_evt_handler;
  491. // Add a custom base UUID.
  492. err_code = sd_ble_uuid_vs_add(&ecs_base_uuid, &p_escs->uuid_type);
  493. VERIFY_SUCCESS(err_code);
  494. ble_uuid.type = p_escs->uuid_type;
  495. ble_uuid.uuid = BLE_UUID_ESCS_SERVICE;
  496. // Add the service.
  497. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  498. &ble_uuid,
  499. &p_escs->service_handle);
  500. VERIFY_SUCCESS(err_code);
  501. m_handle_to_uuid_map_idx = 0;
  502. // Set up initial values for characteristics
  503. // Eddystone spec requires big endian
  504. nrf_ble_escs_broadcast_cap_t temp = p_escs_init->p_init_vals->broadcast_cap;
  505. temp.supp_frame_types = BYTES_SWAP_16BIT(temp.supp_frame_types);
  506. nrf_ble_escs_adv_interval_t temp_interval = p_escs_init->p_init_vals->adv_interval;
  507. temp_interval = BYTES_SWAP_16BIT(temp_interval);
  508. // Adding chracteristics
  509. err_code = char_add(&BROADCAST_CAP_CHAR_INIT, p_escs,
  510. &temp, &p_escs->broadcast_cap_handles);
  511. VERIFY_SUCCESS(err_code);
  512. err_code = char_add(&ACTIVE_SLOT_CHAR_INIT, p_escs,
  513. p_escs->p_active_slot, &p_escs->active_slot_handles);
  514. VERIFY_SUCCESS(err_code);
  515. err_code = char_add(&ADV_INTERVAL_CHAR_INIT, p_escs,
  516. &temp_interval, &p_escs->adv_interval_handles);
  517. VERIFY_SUCCESS(err_code);
  518. err_code = char_add(&RADIO_TX_PWR_CHAR_INIT, p_escs,
  519. &(p_escs_init->p_init_vals->radio_tx_pwr), &p_escs->radio_tx_pwr_handles);
  520. VERIFY_SUCCESS(err_code);
  521. err_code = char_add(&ADV_TX_PWR_CHAR_INIT, p_escs,
  522. &(p_escs_init->p_init_vals->adv_tx_pwr), &p_escs->adv_tx_pwr_handles);
  523. VERIFY_SUCCESS(err_code);
  524. err_code = char_add(&LOCK_STATE_CHAR_INIT, p_escs,
  525. &p_escs->lock_state, &p_escs->lock_state_handles);
  526. VERIFY_SUCCESS(err_code);
  527. err_code = char_add(&UNLOCK_CHAR_INIT, p_escs,
  528. &zero_val, &p_escs->unlock_handles);
  529. VERIFY_SUCCESS(err_code);
  530. err_code = char_add(&PUBLIC_ECDH_KEY_CHAR_INIT, p_escs,
  531. &zero_val, &p_escs->pub_ecdh_key_handles);
  532. VERIFY_SUCCESS(err_code);
  533. err_code = char_add(&EID_ID_KEY_CHAR_INIT, p_escs,
  534. &zero_val, &p_escs->eid_id_key_handles);
  535. VERIFY_SUCCESS(err_code);
  536. err_code = char_add(&RW_ADV_SLOT_CHAR_INIT, p_escs,
  537. &zero_val, &p_escs->rw_adv_slot_handles);
  538. VERIFY_SUCCESS(err_code);
  539. err_code = char_add(&FACTORY_RESET_CHAR_INIT, p_escs,
  540. &(p_escs_init->p_init_vals->factory_reset), &p_escs->factory_reset_handles);
  541. VERIFY_SUCCESS(err_code);
  542. err_code = char_add(&REMAIN_CONNECTABLE_CHAR_INIT, p_escs,
  543. &(p_escs_init->p_init_vals->remain_connectable.r_is_non_connectable_supported),
  544. &p_escs->remain_connectable_handles);
  545. VERIFY_SUCCESS(err_code);
  546. return NRF_SUCCESS;
  547. }