ble_ots.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /**
  2. * Copyright (c) 2017 - 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 <stdint.h>
  41. #include <string.h>
  42. #include "ble_ots.h"
  43. #include "ble_ots_object.h"
  44. #include "ble_ots_oacp.h"
  45. #include "fds.h"
  46. #define BLE_OTS_MAX_OBJ_TYPE_SIZE 16
  47. #define BLE_OTS_OBJ_CHANGED_SIZE 7
  48. #define OTS_INVALID_ID 0xFF
  49. #define SIZE_DATE_TIME 7
  50. #define BLE_OTS_INVALID_OCTET_OFFSET BLE_GATT_STATUS_ATTERR_APP_BEGIN + 1
  51. #define BLE_OTS_INVALID_METADATA BLE_GATT_STATUS_ATTERR_APP_BEGIN + 2
  52. #define BLE_OTS_CONCURRENCY_LIMIT_EXCEEDED BLE_GATT_STATUS_ATTERR_APP_BEGIN + 3
  53. #define BLE_OTS_OACP_SUPPORT_FEATURE_CREATE_bp 0
  54. #define BLE_OTS_OACP_SUPPORT_FEATURE_DELETE_bp 1
  55. #define BLE_OTS_OACP_SUPPORT_FEATURE_CALC_CHECKSUM_bp 2
  56. #define BLE_OTS_OACP_SUPPORT_FEATURE_EXECUTE_bp 3
  57. #define BLE_OTS_OACP_SUPPORT_FEATURE_READ_bp 4
  58. #define BLE_OTS_OACP_SUPPORT_FEATURE_WRITE_bp 5
  59. #define BLE_OTS_OACP_SUPPORT_FEATURE_APPEND_bp 6
  60. #define BLE_OTS_OACP_SUPPORT_FEATURE_TRUNCATE_bp 7
  61. #define BLE_OTS_OACP_SUPPORT_FEATURE_PATCH_bp 8
  62. #define BLE_OTS_OACP_SUPPORT_FEATURE_ABORT_bp 9
  63. #define BLE_OTS_OLCP_SUPPORT_FEATURE_GOTO_bp 0
  64. #define BLE_OTS_OLCP_SUPPORT_FEATURE_ORDER_bp 1
  65. #define BLE_OTS_OLCP_SUPPORT_FEATURE_REQ_NUM_OBJECTS_bp 2
  66. #define BLE_OTS_OLCP_SUPPORT_FEATURE_CLEAR_MARKING_bp 3
  67. /**@brief Function for adding the features characteristic.
  68. *
  69. * @param[in] p_ots Object Transfer Service structure.
  70. * @param[in] read_access Read security level for the feature characteristic.
  71. * @param[in] p_feature_char_handles Pointer to the handles for the feature characteristic.
  72. */
  73. static uint32_t feature_char_add(ble_ots_t * const p_ots,
  74. security_req_t read_access,
  75. ble_gatts_char_handles_t * p_feature_char_handles)
  76. {
  77. ble_add_char_params_t add_char_params;
  78. uint8_t feature[BLE_OTS_FEATURE_LEN];
  79. uint32_t oacp_features;
  80. uint32_t olcp_features;
  81. oacp_features = 0;
  82. oacp_features |= (1 << BLE_OTS_OACP_SUPPORT_FEATURE_READ_bp);
  83. oacp_features |= (1 << BLE_OTS_OACP_SUPPORT_FEATURE_APPEND_bp);
  84. oacp_features |= (1 << BLE_OTS_OACP_SUPPORT_FEATURE_TRUNCATE_bp);
  85. oacp_features |= (1 << BLE_OTS_OACP_SUPPORT_FEATURE_PATCH_bp);
  86. oacp_features |= (1 << BLE_OTS_OACP_SUPPORT_FEATURE_ABORT_bp);
  87. olcp_features = 0;
  88. uint32_t i = 0;
  89. i += uint32_encode(oacp_features, &feature[i]);
  90. i += uint32_encode(olcp_features, &feature[i]);
  91. memset(&add_char_params, 0, sizeof(add_char_params));
  92. add_char_params.uuid = BLE_UUID_OTS_FEATURES;
  93. add_char_params.uuid_type = BLE_UUID_TYPE_BLE;
  94. add_char_params.max_len = BLE_OTS_FEATURE_LEN;
  95. add_char_params.init_len = BLE_OTS_FEATURE_LEN;
  96. add_char_params.p_init_value = &feature[0];
  97. add_char_params.char_props.read = 1;
  98. add_char_params.read_access = read_access;
  99. return characteristic_add(p_ots->service_handle,
  100. &add_char_params,
  101. p_feature_char_handles);
  102. }
  103. /**@brief Function for handling the Connect event.
  104. *
  105. * @param[in] p_ots Object Transfer Service structure.
  106. * @param[in] p_ble_evt Event received from the BLE stack.
  107. */
  108. static void on_connect(ble_ots_t * p_ots, ble_evt_t const * p_ble_evt)
  109. {
  110. ret_code_t err_code;
  111. p_ots->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  112. err_code = nrf_ble_gq_conn_handle_register(p_ots->p_gatt_queue,
  113. p_ble_evt->evt.gap_evt.conn_handle);
  114. if ((p_ots->error_handler != NULL) &&
  115. (err_code != NRF_SUCCESS))
  116. {
  117. p_ots->error_handler(err_code);
  118. }
  119. }
  120. /**@brief Function for handling the BLE_GAP_EVT_DISCONNECTED event.
  121. *
  122. * @param[in] p_ots OTS Service Structure.
  123. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  124. */
  125. static void on_disconnect(ble_ots_t * p_ots, ble_evt_t const * p_ble_evt)
  126. {
  127. p_ots->conn_handle = BLE_CONN_HANDLE_INVALID;
  128. }
  129. uint32_t ble_ots_init(ble_ots_t * p_ots, ble_ots_init_t * p_ots_init)
  130. {
  131. if ((p_ots == NULL) || (p_ots_init == NULL))
  132. {
  133. return NRF_ERROR_NULL;
  134. }
  135. if (p_ots_init->p_object == NULL)
  136. {
  137. return NRF_ERROR_NULL;
  138. }
  139. if (p_ots_init->p_gatt_queue == NULL)
  140. {
  141. return NRF_ERROR_NULL;
  142. }
  143. p_ots->conn_handle = BLE_CONN_HANDLE_INVALID;
  144. p_ots->error_handler = p_ots_init->error_handler;
  145. p_ots->evt_handler = p_ots_init->evt_handler;
  146. p_ots->p_current_object = p_ots_init->p_object;
  147. p_ots->error_handler = p_ots_init->error_handler;
  148. p_ots->evt_handler = p_ots_init->evt_handler;
  149. p_ots->p_current_object = p_ots_init->p_object;
  150. p_ots->p_gatt_queue = p_ots_init->p_gatt_queue;
  151. ble_uuid_t service_uuid =
  152. {
  153. .uuid = BLE_UUID_OTS_SERVICE,
  154. .type = BLE_UUID_TYPE_BLE
  155. };
  156. uint32_t err_code;
  157. err_code = fds_init();
  158. if (err_code != NRF_SUCCESS)
  159. {
  160. return err_code;
  161. }
  162. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY,
  163. &service_uuid,
  164. &(p_ots->service_handle));
  165. if (err_code != NRF_SUCCESS)
  166. {
  167. return err_code;
  168. }
  169. err_code = feature_char_add(p_ots, p_ots_init->feature_char_read_access, &p_ots->feature_handles);
  170. if (err_code != NRF_SUCCESS)
  171. {
  172. return err_code;
  173. }
  174. err_code = ble_ots_object_representation_init(&p_ots->object_chars, &p_ots_init->object_chars_init);
  175. if (err_code != NRF_SUCCESS)
  176. {
  177. return err_code;
  178. }
  179. err_code = ble_ots_oacp_init(&p_ots->oacp_chars, &p_ots_init->oacp_init);
  180. if (err_code != NRF_SUCCESS)
  181. {
  182. return err_code;
  183. }
  184. p_ots->oacp_chars.ots_l2cap.conn_mps = p_ots_init->rx_mps;
  185. p_ots->oacp_chars.ots_l2cap.conn_mtu = p_ots_init->rx_mtu;
  186. return NRF_SUCCESS;
  187. }
  188. void ble_ots_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  189. {
  190. ble_ots_t * p_ots;
  191. p_ots = (ble_ots_t*) p_context;
  192. if ((p_ots == NULL) || (p_ble_evt == NULL))
  193. {
  194. return;
  195. }
  196. ble_ots_oacp_on_ble_evt(&p_ots->oacp_chars, p_ble_evt);
  197. ble_ots_object_on_ble_evt(&p_ots->object_chars, p_ble_evt);
  198. switch (p_ble_evt->header.evt_id)
  199. {
  200. case BLE_GAP_EVT_CONNECTED:
  201. on_connect(p_ots, p_ble_evt);
  202. break;
  203. case BLE_GAP_EVT_DISCONNECTED:
  204. on_disconnect(p_ots, p_ble_evt);
  205. break;
  206. default:
  207. // No implementation needed.
  208. break;
  209. }
  210. }