ble_dis.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Copyright (c) 2012 - 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. /* Attention!
  41. * To maintain compliance with Nordic Semiconductor ASA's Bluetooth profile
  42. * qualification listings, this section of source code must not be modified.
  43. */
  44. #include "sdk_common.h"
  45. #if NRF_MODULE_ENABLED(BLE_DIS)
  46. #include "ble_dis.h"
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include "app_error.h"
  50. #include "ble_gatts.h"
  51. #include "ble_srv_common.h"
  52. #define BLE_DIS_SYS_ID_LEN 8 /**< Length of System ID Characteristic Value. */
  53. #define BLE_DIS_PNP_ID_LEN 7 /**< Length of Pnp ID Characteristic Value. */
  54. static uint16_t service_handle;
  55. static ble_gatts_char_handles_t manufact_name_handles;
  56. static ble_gatts_char_handles_t model_num_handles;
  57. static ble_gatts_char_handles_t serial_num_handles;
  58. static ble_gatts_char_handles_t hw_rev_handles;
  59. static ble_gatts_char_handles_t fw_rev_handles;
  60. static ble_gatts_char_handles_t sw_rev_handles;
  61. static ble_gatts_char_handles_t sys_id_handles;
  62. static ble_gatts_char_handles_t reg_cert_data_list_handles;
  63. static ble_gatts_char_handles_t pnp_id_handles;
  64. /**@brief Function for encoding a System ID.
  65. *
  66. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  67. * @param[in] p_sys_id System ID to be encoded.
  68. */
  69. static void sys_id_encode(uint8_t * p_encoded_buffer, ble_dis_sys_id_t const * p_sys_id)
  70. {
  71. APP_ERROR_CHECK_BOOL(p_sys_id != NULL);
  72. APP_ERROR_CHECK_BOOL(p_encoded_buffer != NULL);
  73. p_encoded_buffer[0] = (p_sys_id->manufacturer_id & 0x00000000FF);
  74. p_encoded_buffer[1] = (p_sys_id->manufacturer_id & 0x000000FF00) >> 8;
  75. p_encoded_buffer[2] = (p_sys_id->manufacturer_id & 0x0000FF0000) >> 16;
  76. p_encoded_buffer[3] = (p_sys_id->manufacturer_id & 0x00FF000000) >> 24;
  77. p_encoded_buffer[4] = (p_sys_id->manufacturer_id & 0xFF00000000) >> 32;
  78. p_encoded_buffer[5] = (p_sys_id->organizationally_unique_id & 0x0000FF);
  79. p_encoded_buffer[6] = (p_sys_id->organizationally_unique_id & 0x00FF00) >> 8;
  80. p_encoded_buffer[7] = (p_sys_id->organizationally_unique_id & 0xFF0000) >> 16;
  81. }
  82. /**@brief Function for encoding a PnP ID.
  83. *
  84. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  85. * @param[in] p_pnp_id PnP ID to be encoded.
  86. */
  87. static void pnp_id_encode(uint8_t * p_encoded_buffer, ble_dis_pnp_id_t const * p_pnp_id)
  88. {
  89. uint8_t len = 0;
  90. APP_ERROR_CHECK_BOOL(p_pnp_id != NULL);
  91. APP_ERROR_CHECK_BOOL(p_encoded_buffer != NULL);
  92. p_encoded_buffer[len++] = p_pnp_id->vendor_id_source;
  93. len += uint16_encode(p_pnp_id->vendor_id, &p_encoded_buffer[len]);
  94. len += uint16_encode(p_pnp_id->product_id, &p_encoded_buffer[len]);
  95. len += uint16_encode(p_pnp_id->product_version, &p_encoded_buffer[len]);
  96. APP_ERROR_CHECK_BOOL(len == BLE_DIS_PNP_ID_LEN);
  97. }
  98. /**@brief Function for adding the Characteristic.
  99. *
  100. * @param[in] uuid UUID of characteristic to be added.
  101. * @param[in] p_char_value Initial value of characteristic to be added.
  102. * @param[in] char_len Length of initial value. This will also be the maximum value.
  103. * @param[in] rd_sec Security requirement for reading characteristic value.
  104. * @param[out] p_handles Handles of new characteristic.
  105. *
  106. * @return NRF_SUCCESS on success, otherwise an error code.
  107. */
  108. static uint32_t char_add(uint16_t uuid,
  109. uint8_t * p_char_value,
  110. uint16_t char_len,
  111. security_req_t const rd_sec,
  112. ble_gatts_char_handles_t * p_handles)
  113. {
  114. ble_add_char_params_t add_char_params;
  115. APP_ERROR_CHECK_BOOL(p_char_value != NULL);
  116. APP_ERROR_CHECK_BOOL(char_len > 0);
  117. memset(&add_char_params, 0, sizeof(add_char_params));
  118. add_char_params.uuid = uuid;
  119. add_char_params.max_len = char_len;
  120. add_char_params.init_len = char_len;
  121. add_char_params.p_init_value = p_char_value;
  122. add_char_params.char_props.read = 1;
  123. add_char_params.read_access = rd_sec;
  124. return characteristic_add(service_handle, &add_char_params, p_handles);
  125. }
  126. uint32_t ble_dis_init(ble_dis_init_t const * p_dis_init)
  127. {
  128. uint32_t err_code;
  129. ble_uuid_t ble_uuid;
  130. // Add service
  131. BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_UUID_DEVICE_INFORMATION_SERVICE);
  132. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &service_handle);
  133. if (err_code != NRF_SUCCESS)
  134. {
  135. return err_code;
  136. }
  137. // Add characteristics
  138. if (p_dis_init->manufact_name_str.length > 0)
  139. {
  140. err_code = char_add(BLE_UUID_MANUFACTURER_NAME_STRING_CHAR,
  141. p_dis_init->manufact_name_str.p_str,
  142. p_dis_init->manufact_name_str.length,
  143. p_dis_init->dis_char_rd_sec,
  144. &manufact_name_handles);
  145. if (err_code != NRF_SUCCESS)
  146. {
  147. return err_code;
  148. }
  149. }
  150. if (p_dis_init->model_num_str.length > 0)
  151. {
  152. err_code = char_add(BLE_UUID_MODEL_NUMBER_STRING_CHAR,
  153. p_dis_init->model_num_str.p_str,
  154. p_dis_init->model_num_str.length,
  155. p_dis_init->dis_char_rd_sec,
  156. &model_num_handles);
  157. if (err_code != NRF_SUCCESS)
  158. {
  159. return err_code;
  160. }
  161. }
  162. if (p_dis_init->serial_num_str.length > 0)
  163. {
  164. err_code = char_add(BLE_UUID_SERIAL_NUMBER_STRING_CHAR,
  165. p_dis_init->serial_num_str.p_str,
  166. p_dis_init->serial_num_str.length,
  167. p_dis_init->dis_char_rd_sec,
  168. &serial_num_handles);
  169. if (err_code != NRF_SUCCESS)
  170. {
  171. return err_code;
  172. }
  173. }
  174. if (p_dis_init->hw_rev_str.length > 0)
  175. {
  176. err_code = char_add(BLE_UUID_HARDWARE_REVISION_STRING_CHAR,
  177. p_dis_init->hw_rev_str.p_str,
  178. p_dis_init->hw_rev_str.length,
  179. p_dis_init->dis_char_rd_sec,
  180. &hw_rev_handles);
  181. if (err_code != NRF_SUCCESS)
  182. {
  183. return err_code;
  184. }
  185. }
  186. if (p_dis_init->fw_rev_str.length > 0)
  187. {
  188. err_code = char_add(BLE_UUID_FIRMWARE_REVISION_STRING_CHAR,
  189. p_dis_init->fw_rev_str.p_str,
  190. p_dis_init->fw_rev_str.length,
  191. p_dis_init->dis_char_rd_sec,
  192. &fw_rev_handles);
  193. if (err_code != NRF_SUCCESS)
  194. {
  195. return err_code;
  196. }
  197. }
  198. if (p_dis_init->sw_rev_str.length > 0)
  199. {
  200. err_code = char_add(BLE_UUID_SOFTWARE_REVISION_STRING_CHAR,
  201. p_dis_init->sw_rev_str.p_str,
  202. p_dis_init->sw_rev_str.length,
  203. p_dis_init->dis_char_rd_sec,
  204. &sw_rev_handles);
  205. if (err_code != NRF_SUCCESS)
  206. {
  207. return err_code;
  208. }
  209. }
  210. if (p_dis_init->p_sys_id != NULL)
  211. {
  212. uint8_t encoded_sys_id[BLE_DIS_SYS_ID_LEN];
  213. sys_id_encode(encoded_sys_id, p_dis_init->p_sys_id);
  214. err_code = char_add(BLE_UUID_SYSTEM_ID_CHAR,
  215. encoded_sys_id,
  216. BLE_DIS_SYS_ID_LEN,
  217. p_dis_init->dis_char_rd_sec,
  218. &sys_id_handles);
  219. if (err_code != NRF_SUCCESS)
  220. {
  221. return err_code;
  222. }
  223. }
  224. if (p_dis_init->p_reg_cert_data_list != NULL)
  225. {
  226. err_code = char_add(BLE_UUID_IEEE_REGULATORY_CERTIFICATION_DATA_LIST_CHAR,
  227. p_dis_init->p_reg_cert_data_list->p_list,
  228. p_dis_init->p_reg_cert_data_list->list_len,
  229. p_dis_init->dis_char_rd_sec,
  230. &reg_cert_data_list_handles);
  231. if (err_code != NRF_SUCCESS)
  232. {
  233. return err_code;
  234. }
  235. }
  236. if (p_dis_init->p_pnp_id != NULL)
  237. {
  238. uint8_t encoded_pnp_id[BLE_DIS_PNP_ID_LEN];
  239. pnp_id_encode(encoded_pnp_id, p_dis_init->p_pnp_id);
  240. err_code = char_add(BLE_UUID_PNP_ID_CHAR,
  241. encoded_pnp_id,
  242. BLE_DIS_PNP_ID_LEN,
  243. p_dis_init->dis_char_rd_sec,
  244. &pnp_id_handles);
  245. if (err_code != NRF_SUCCESS)
  246. {
  247. return err_code;
  248. }
  249. }
  250. return NRF_SUCCESS;
  251. }
  252. #endif // NRF_MODULE_ENABLED(BLE_DIS)