tile_gatt_db.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * NOTICE
  3. *
  4. * Copyright 2020 Tile Inc. All Rights Reserved.
  5. * All code or other information included in the accompanying files ("Tile Source Material")
  6. * is PROPRIETARY information of Tile Inc. ("Tile") and access and use of the Tile Source Material
  7. * is subject to these terms. The Tile Source Material may only be used for demonstration purposes,
  8. * and may not be otherwise distributed or made available to others, including for commercial purposes.
  9. * Without limiting the foregoing , you understand and agree that no production use
  10. * of the Tile Source Material is allowed without a Tile ID properly obtained under a separate
  11. * agreement with Tile.
  12. * You also understand and agree that Tile may terminate the limited rights granted under these terms
  13. * at any time in its discretion.
  14. * All Tile Source Material is provided AS-IS without warranty of any kind.
  15. * Tile does not warrant that the Tile Source Material will be error-free or fit for your purposes.
  16. * Tile will not be liable for any damages resulting from your use of or inability to use
  17. * the Tile Source Material.
  18. *
  19. * Support: firmware_support@tile.com
  20. */
  21. /**
  22. * @file tile_gatt_db.c
  23. * @brief Set up the Tile GATT service
  24. */
  25. #include "sdk_common.h"
  26. #if NRF_MODULE_ENABLED(TILE_SUPPORT)
  27. #include "tile_gatt_db.h"
  28. #include "app_error.h"
  29. #include "ble.h"
  30. #include "tile_lib.h"
  31. #include <stdint.h>
  32. uint16_t tile_get_adv_uuid(void);
  33. /**
  34. * @brief Initialize Tile GATT database
  35. *
  36. * @param[out] p_service Service structure. Will be populated with handles.
  37. */
  38. void tile_gatt_db_init(tile_gatt_db_t *p_service)
  39. {
  40. uint32_t err_code;
  41. /* Add Tile service */
  42. ble_uuid_t ble_uuid;
  43. BLE_UUID_BLE_ASSIGN(ble_uuid, TILE_ACTIVATED_UUID);
  44. /* Add Tile base UUID */
  45. uint8_t ble_type;
  46. ble_uuid128_t base_uuid = TILE_SVC_BASE_UUID;
  47. err_code = sd_ble_uuid_vs_add(&base_uuid, &ble_type);
  48. APP_ERROR_CHECK(err_code);
  49. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_service->service_handle);
  50. APP_ERROR_CHECK(err_code);
  51. /**************************
  52. * Tile ID characteristic *
  53. **************************/
  54. ble_uuid_t tile_id_uuid =
  55. {
  56. .uuid = 0x0007,
  57. .type = ble_type
  58. };
  59. ble_gatts_char_md_t tile_id_char_md =
  60. {
  61. .char_props =
  62. {
  63. .read = 1, /* Tile ID is read only */
  64. }
  65. };
  66. ble_gatts_attr_md_t tile_id_attr_md =
  67. {
  68. .read_perm = {1,1}, /* Sec mode open */
  69. .vloc = BLE_GATTS_VLOC_STACK /* Allocate the value in the SoftDevice */
  70. };
  71. uint8_t id[8] = {0};
  72. ble_gatts_attr_t tile_id_value =
  73. {
  74. .p_uuid = &tile_id_uuid, /* Tile ID UUID */
  75. .p_attr_md = &tile_id_attr_md, /* Attribute metadata */
  76. .init_len = TILE_ID_LEN, /* Initial length */
  77. .init_offs = 0, /* No offset */
  78. .max_len = TILE_ID_LEN, /* Maximum length */
  79. .p_value = id /* Zero array as initial value */
  80. };
  81. ble_gatts_char_handles_t char_handles;
  82. err_code = sd_ble_gatts_characteristic_add(p_service->service_handle,
  83. &tile_id_char_md,
  84. &tile_id_value,
  85. &char_handles);
  86. APP_ERROR_CHECK(err_code);
  87. /* Save handle */
  88. p_service->characteristic_handles[TILE_ID_CHAR] = char_handles.value_handle;
  89. /**************************
  90. * TOA CMD characteristic *
  91. **************************/
  92. ble_uuid_t toa_cmd_uuid =
  93. {
  94. .uuid = 0x18,
  95. .type = ble_type
  96. };
  97. ble_gatts_char_md_t toa_cmd_char_md =
  98. {
  99. .char_props =
  100. {
  101. .write_wo_resp = 1, /* TOA CMD is write w/o response */
  102. }
  103. };
  104. ble_gatts_attr_md_t toa_cmd_attr_md =
  105. {
  106. .write_perm = {1,1}, /* Sec mode open */
  107. .vlen = 1, /* This is a variable length attribute */
  108. .vloc = BLE_GATTS_VLOC_STACK /* Allocate the value in the SoftDevice */
  109. };
  110. ble_gatts_attr_t toa_cmd_value =
  111. {
  112. .p_uuid = &toa_cmd_uuid, /* TOA CMD UUID */
  113. .p_attr_md = &toa_cmd_attr_md, /* Attribute metadata */
  114. .init_len = 0, /* Initially zero length */
  115. .init_offs = 0, /* No offset */
  116. .max_len = TILE_TOA_CMD_CHAR_LEN, /* Maximum length */
  117. .p_value = NULL /* No initial value */
  118. };
  119. err_code = sd_ble_gatts_characteristic_add(p_service->service_handle,
  120. &toa_cmd_char_md,
  121. &toa_cmd_value,
  122. &char_handles);
  123. APP_ERROR_CHECK(err_code);
  124. /* Save value handle */
  125. p_service->characteristic_handles[TILE_TOA_CMD_CHAR] = char_handles.value_handle;
  126. /**************************
  127. * TOA RSP characteristic *
  128. **************************/
  129. ble_uuid_t toa_rsp_uuid =
  130. {
  131. .uuid = 0x19,
  132. .type = ble_type
  133. };
  134. ble_gatts_attr_md_t toa_rsp_cccd_md =
  135. {
  136. .read_perm = {1,1}, /* Sec mode open */
  137. .write_perm = {1,1}, /* Sec mode open */
  138. .vloc = BLE_GATTS_VLOC_STACK /* Value stored in SoftDevice */
  139. };
  140. ble_gatts_char_md_t toa_rsp_char_md = {
  141. .char_props =
  142. {
  143. .notify = 1, /* TOA RSP uses notifications */
  144. },
  145. .p_cccd_md = &toa_rsp_cccd_md
  146. };
  147. ble_gatts_attr_md_t toa_rsp_attr_md =
  148. {
  149. .read_perm = {1,1}, /* Sec mode open */
  150. .vlen = 1, /* Variable length attribute */
  151. .vloc = BLE_GATTS_VLOC_STACK /* Value stored in SoftDevice */
  152. };
  153. ble_gatts_attr_t toa_rsp_value =
  154. {
  155. .p_uuid = &toa_rsp_uuid, /* TOA RSP UUID */
  156. .p_attr_md = &toa_rsp_attr_md, /* Attribute metadata */
  157. .init_len = 0, /* Initially zero length */
  158. .init_offs = 0, /* No offset */
  159. .max_len = TILE_TOA_CMD_CHAR_LEN, /* Maximum length */
  160. .p_value = NULL /* No initial value */
  161. };
  162. err_code = sd_ble_gatts_characteristic_add(p_service->service_handle,
  163. &toa_rsp_char_md,
  164. &toa_rsp_value,
  165. &char_handles);
  166. APP_ERROR_CHECK(err_code);
  167. p_service->characteristic_handles[TILE_TOA_RSP_CHAR] = char_handles.value_handle;
  168. p_service->characteristic_handles[TILE_TOA_RSP_CCCD] = char_handles.cccd_handle;
  169. }
  170. #endif // NRF_MODULE_ENABLED(TILE_SUPPORT)