You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
8.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. #include "zdatachannel_service.h"
  2. #include "ble.h"
  3. #include "ble_srv_common.h"
  4. #include "nrf_log.h"
  5. #include "sdk_common.h"
  6. #include "znordic.h"
  7. #define ZDATACHANNEL_UUID_NUS_SERVICE 0x0001 /**< The UUID of the Nordic UART Service. */
  8. #define ZDATACHANNEL_TX_CHARACTERISTIC 0x0003 /**< The UUID of the TX Characteristic. */
  9. #define ZDATACHANNEL_RX_CHARACTERISTIC 0x0002 /**< The UUID of the RX Characteristic. */
  10. #define ZDATACHANNEL_DATABLOCK_TX_CHARACTERISTIC 0x0004 /**< The UUID of the RX Characteristic. */
  11. #define ZDATACHANNEL_MAX_RX_CHAR_LEN ZDATACHANNEL_MAX_DATA_LEN /**< Maximum length of the RX Characteristic (in bytes). */
  12. #define ZDATACHANNEL_MAX_TX_CHAR_LEN ZDATACHANNEL_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */
  13. #define ZDATACHANNEL_DATABLOCK_TX_CHAR_LEN ZDATACHANNEL_MAX_DATA_LEN /**< Maximum length of the TX Characteristic (in bytes). */
  14. #define NUS_BASE_UUID \
  15. { \
  16. { 0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x00, 0x00, 0x40, 0x6E } \
  17. } /**< Used vendor specific UUID. */
  18. static zdatachannel_t *p_datachannel;
  19. static bool notification_enable(uint16_t conn_handle, uint16_t cccd_handle) {
  20. ret_code_t err_code;
  21. ble_gatts_value_t gatts_val;
  22. uint8_t cccd_value[2];
  23. memset(&gatts_val, 0, sizeof(ble_gatts_value_t));
  24. gatts_val.p_value = cccd_value;
  25. gatts_val.len = sizeof(cccd_value);
  26. gatts_val.offset = 0;
  27. err_code = sd_ble_gatts_value_get(conn_handle, cccd_handle, &gatts_val);
  28. if ((err_code == NRF_SUCCESS) && ble_srv_is_notification_enabled(gatts_val.p_value)) {
  29. return true;
  30. }
  31. return false;
  32. }
  33. /**@brief Function for handling the @ref BLE_GATTS_EVT_WRITE event from the SoftDevice.
  34. *
  35. * @param[in] p_nus Nordic UART Service structure.
  36. * @param[in] p_ble_evt Pointer to the event received from BLE stack.
  37. */
  38. static void on_write(zdatachannel_t *p_nus, ble_evt_t const *p_ble_evt) {
  39. zdatachannel_evt_t evt;
  40. ble_gatts_evt_write_t const *p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  41. memset(&evt, 0, sizeof(zdatachannel_evt_t));
  42. if ((p_evt_write->handle == p_nus->cmd_tx_handles.cccd_handle) && (p_evt_write->len == 2)) {
  43. if (ble_srv_is_notification_enabled(p_evt_write->data)) {
  44. p_nus->cmd_tx_channel_is_notification_enabled = true;
  45. } else {
  46. p_nus->cmd_tx_channel_is_notification_enabled = false;
  47. }
  48. } else if ((p_evt_write->handle == p_nus->datablock_tx_handles.cccd_handle) && (p_evt_write->len == 2)) {
  49. if (ble_srv_is_notification_enabled(p_evt_write->data)) {
  50. p_nus->datablock_tx_channel_is_notification_enabled = true;
  51. } else {
  52. p_nus->datablock_tx_channel_is_notification_enabled = false;
  53. }
  54. } else if ((p_evt_write->handle == p_nus->cmd_rx_handles.value_handle) && (p_nus->data_handler != NULL)) {
  55. evt.type = ZDATACHANNEL_EVT_RX_DATA;
  56. evt.params.rx_data.p_data = p_evt_write->data;
  57. evt.params.rx_data.length = p_evt_write->len;
  58. p_nus->data_handler(&evt);
  59. } else {
  60. // Do Nothing. This event is not relevant for this service.
  61. }
  62. }
  63. void zdatachannel_on_ble_evt(ble_evt_t const *p_ble_evt, void *p_context) {
  64. if ((p_context == NULL) || (p_ble_evt == NULL)) {
  65. return;
  66. }
  67. zdatachannel_t *p_nus = (zdatachannel_t *)p_context;
  68. switch (p_ble_evt->header.evt_id) {
  69. case BLE_GAP_EVT_CONNECTED:
  70. ZLOGI("BLE_GAP_EVT_CONNECTED");
  71. p_nus->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  72. p_nus->cmd_tx_channel_is_notification_enabled = notification_enable(p_ble_evt->evt.gap_evt.conn_handle, p_nus->cmd_tx_handles.cccd_handle);
  73. p_nus->datablock_tx_channel_is_notification_enabled = notification_enable(p_ble_evt->evt.gap_evt.conn_handle, p_nus->cmd_tx_handles.cccd_handle);
  74. break;
  75. case BLE_GAP_EVT_DISCONNECTED:
  76. ZLOGI("BLE_GAP_EVT_DISCONNECTED");
  77. p_nus->conn_handle = BLE_CONN_HANDLE_INVALID;
  78. break;
  79. case BLE_GATTS_EVT_WRITE:
  80. on_write(p_nus, p_ble_evt);
  81. break;
  82. default:
  83. // No implementation needed.
  84. break;
  85. }
  86. }
  87. bool zdatachannel_is_connected() { return p_datachannel->conn_handle != BLE_CONN_HANDLE_INVALID; }
  88. uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_nus_init) {
  89. p_datachannel = p_nus;
  90. ret_code_t err_code;
  91. ble_uuid_t ble_uuid;
  92. ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
  93. ble_add_char_params_t add_char_params;
  94. VERIFY_PARAM_NOT_NULL(p_nus);
  95. VERIFY_PARAM_NOT_NULL(p_nus_init);
  96. // Initialize the service structure.
  97. p_nus->data_handler = p_nus_init->data_handler;
  98. /**@snippet [Adding proprietary Service to the SoftDevice] */
  99. // Add a custom base UUID.
  100. err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
  101. VERIFY_SUCCESS(err_code);
  102. ble_uuid.type = p_nus->uuid_type;
  103. ble_uuid.uuid = ZDATACHANNEL_UUID_NUS_SERVICE;
  104. // Add the service.
  105. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_nus->service_handle);
  106. /**@snippet [Adding proprietary Service to the SoftDevice] */
  107. VERIFY_SUCCESS(err_code);
  108. // Add the RX Characteristic.
  109. memset(&add_char_params, 0, sizeof(add_char_params));
  110. add_char_params.uuid = ZDATACHANNEL_RX_CHARACTERISTIC;
  111. add_char_params.uuid_type = p_nus->uuid_type;
  112. add_char_params.max_len = ZDATACHANNEL_MAX_RX_CHAR_LEN;
  113. add_char_params.init_len = sizeof(uint8_t);
  114. add_char_params.is_var_len = true;
  115. add_char_params.char_props.write = 1;
  116. add_char_params.char_props.write_wo_resp = 1;
  117. add_char_params.read_access = SEC_NO_ACCESS;
  118. add_char_params.write_access = SEC_OPEN;
  119. VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_rx_handles));
  120. // Add the TX Characteristic.
  121. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  122. memset(&add_char_params, 0, sizeof(add_char_params));
  123. add_char_params.uuid = ZDATACHANNEL_TX_CHARACTERISTIC;
  124. add_char_params.uuid_type = p_nus->uuid_type;
  125. add_char_params.max_len = ZDATACHANNEL_MAX_TX_CHAR_LEN;
  126. add_char_params.init_len = sizeof(uint8_t);
  127. add_char_params.is_var_len = true;
  128. add_char_params.char_props.notify = 1;
  129. add_char_params.read_access = SEC_OPEN;
  130. add_char_params.write_access = SEC_OPEN;
  131. add_char_params.cccd_write_access = SEC_OPEN;
  132. VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_tx_handles));
  133. // Add the TX Characteristic.
  134. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  135. memset(&add_char_params, 0, sizeof(add_char_params));
  136. add_char_params.uuid = ZDATACHANNEL_DATABLOCK_TX_CHARACTERISTIC;
  137. add_char_params.uuid_type = p_nus->uuid_type;
  138. add_char_params.max_len = ZDATACHANNEL_DATABLOCK_TX_CHAR_LEN;
  139. add_char_params.init_len = sizeof(uint8_t);
  140. add_char_params.is_var_len = true;
  141. add_char_params.char_props.notify = 1;
  142. add_char_params.read_access = SEC_OPEN;
  143. add_char_params.write_access = SEC_OPEN;
  144. add_char_params.cccd_write_access = SEC_OPEN;
  145. VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->datablock_tx_handles));
  146. return NRF_SUCCESS;
  147. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  148. }
  149. uint32_t zdatachannel_data_send(uint8_t *p_data, uint16_t *p_length) {
  150. ble_gatts_hvx_params_t hvx_params;
  151. if (!p_datachannel->cmd_tx_channel_is_notification_enabled) {
  152. return NRF_ERROR_INVALID_STATE;
  153. }
  154. if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
  155. return NRF_ERROR_INVALID_PARAM;
  156. }
  157. memset(&hvx_params, 0, sizeof(hvx_params));
  158. hvx_params.handle = p_datachannel->cmd_tx_handles.value_handle;
  159. hvx_params.p_data = p_data;
  160. hvx_params.p_len = p_length;
  161. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  162. return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
  163. }
  164. uint32_t zdatachannel_block_data_send(uint8_t *p_data, uint16_t *p_length) {
  165. ble_gatts_hvx_params_t hvx_params;
  166. if (!p_datachannel->datablock_tx_channel_is_notification_enabled) {
  167. return NRF_ERROR_INVALID_STATE;
  168. }
  169. if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
  170. return NRF_ERROR_INVALID_PARAM;
  171. }
  172. memset(&hvx_params, 0, sizeof(hvx_params));
  173. hvx_params.handle = p_datachannel->datablock_tx_handles.value_handle;
  174. hvx_params.p_data = p_data;
  175. hvx_params.p_len = p_length;
  176. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  177. return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
  178. }