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.

224 lines
8.9 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 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. // ZLOGI("-----%d %d", p_datachannel->conn_handle, BLE_CONN_HANDLE_INVALID);
  79. break;
  80. case BLE_GATTS_EVT_WRITE:
  81. on_write(p_nus, p_ble_evt);
  82. break;
  83. default:
  84. // No implementation needed.
  85. break;
  86. }
  87. }
  88. bool zdatachannel_is_connected() {
  89. // ZLOGI("%d %d", p_datachannel->conn_handle, BLE_CONN_HANDLE_INVALID);
  90. return p_datachannel->conn_handle != (int16_t)BLE_CONN_HANDLE_INVALID;
  91. }
  92. uint32_t zdatachannel_init(zdatachannel_t *p_nus, zdatachannel_init_t const *p_nus_init) {
  93. p_datachannel = p_nus;
  94. p_datachannel->conn_handle = BLE_CONN_HANDLE_INVALID;
  95. ret_code_t err_code;
  96. ble_uuid_t ble_uuid;
  97. ble_uuid128_t nus_base_uuid = NUS_BASE_UUID;
  98. ble_add_char_params_t add_char_params;
  99. VERIFY_PARAM_NOT_NULL(p_nus);
  100. VERIFY_PARAM_NOT_NULL(p_nus_init);
  101. // Initialize the service structure.
  102. p_nus->data_handler = p_nus_init->data_handler;
  103. /**@snippet [Adding proprietary Service to the SoftDevice] */
  104. // Add a custom base UUID.
  105. err_code = sd_ble_uuid_vs_add(&nus_base_uuid, &p_nus->uuid_type);
  106. VERIFY_SUCCESS(err_code);
  107. ble_uuid.type = p_nus->uuid_type;
  108. ble_uuid.uuid = ZDATACHANNEL_UUID_NUS_SERVICE;
  109. // Add the service.
  110. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_nus->service_handle);
  111. /**@snippet [Adding proprietary Service to the SoftDevice] */
  112. VERIFY_SUCCESS(err_code);
  113. // Add the RX Characteristic.
  114. memset(&add_char_params, 0, sizeof(add_char_params));
  115. add_char_params.uuid = ZDATACHANNEL_RX_CHARACTERISTIC;
  116. add_char_params.uuid_type = p_nus->uuid_type;
  117. add_char_params.max_len = ZDATACHANNEL_MAX_RX_CHAR_LEN;
  118. add_char_params.init_len = sizeof(uint8_t);
  119. add_char_params.is_var_len = true;
  120. add_char_params.char_props.write = 1;
  121. add_char_params.char_props.write_wo_resp = 1;
  122. add_char_params.read_access = SEC_NO_ACCESS;
  123. add_char_params.write_access = SEC_OPEN;
  124. VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_rx_handles));
  125. // Add the TX Characteristic.
  126. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  127. memset(&add_char_params, 0, sizeof(add_char_params));
  128. add_char_params.uuid = ZDATACHANNEL_TX_CHARACTERISTIC;
  129. add_char_params.uuid_type = p_nus->uuid_type;
  130. add_char_params.max_len = ZDATACHANNEL_MAX_TX_CHAR_LEN;
  131. add_char_params.init_len = sizeof(uint8_t);
  132. add_char_params.is_var_len = true;
  133. add_char_params.char_props.notify = 1;
  134. add_char_params.read_access = SEC_OPEN;
  135. add_char_params.write_access = SEC_OPEN;
  136. add_char_params.cccd_write_access = SEC_OPEN;
  137. VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->cmd_tx_handles));
  138. // Add the TX Characteristic.
  139. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  140. memset(&add_char_params, 0, sizeof(add_char_params));
  141. add_char_params.uuid = ZDATACHANNEL_DATABLOCK_TX_CHARACTERISTIC;
  142. add_char_params.uuid_type = p_nus->uuid_type;
  143. add_char_params.max_len = ZDATACHANNEL_DATABLOCK_TX_CHAR_LEN;
  144. add_char_params.init_len = sizeof(uint8_t);
  145. add_char_params.is_var_len = true;
  146. add_char_params.char_props.notify = 1;
  147. add_char_params.read_access = SEC_OPEN;
  148. add_char_params.write_access = SEC_OPEN;
  149. add_char_params.cccd_write_access = SEC_OPEN;
  150. VERIFY_SUCCESS(characteristic_add(p_nus->service_handle, &add_char_params, &p_nus->datablock_tx_handles));
  151. return NRF_SUCCESS;
  152. /**@snippet [Adding proprietary characteristic to the SoftDevice] */
  153. }
  154. uint32_t zdatachannel_data_send(uint8_t *p_data, uint16_t *p_length) {
  155. ble_gatts_hvx_params_t hvx_params;
  156. if (!p_datachannel->cmd_tx_channel_is_notification_enabled) {
  157. return NRF_ERROR_INVALID_STATE;
  158. }
  159. if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
  160. return NRF_ERROR_INVALID_PARAM;
  161. }
  162. memset(&hvx_params, 0, sizeof(hvx_params));
  163. hvx_params.handle = p_datachannel->cmd_tx_handles.value_handle;
  164. hvx_params.p_data = p_data;
  165. hvx_params.p_len = p_length;
  166. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  167. return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
  168. }
  169. uint32_t zdatachannel_block_data_send(uint8_t *p_data, uint16_t *p_length) {
  170. ble_gatts_hvx_params_t hvx_params;
  171. if (!p_datachannel->datablock_tx_channel_is_notification_enabled) {
  172. return NRF_ERROR_INVALID_STATE;
  173. }
  174. if (*p_length > ZDATACHANNEL_MAX_DATA_LEN) {
  175. return NRF_ERROR_INVALID_PARAM;
  176. }
  177. memset(&hvx_params, 0, sizeof(hvx_params));
  178. hvx_params.handle = p_datachannel->datablock_tx_handles.value_handle;
  179. hvx_params.p_data = p_data;
  180. hvx_params.p_len = p_length;
  181. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  182. return sd_ble_gatts_hvx(p_datachannel->conn_handle, &hvx_params);
  183. }