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.

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