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.

225 lines
9.0 KiB

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