serial_service.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "serial_service.h"
  2. #include "app_common.h"
  3. #include "ble.h"
  4. #include <furi.h>
  5. #define TAG "BtSerialSvc"
  6. typedef struct {
  7. uint16_t svc_handle;
  8. uint16_t rx_char_handle;
  9. uint16_t tx_char_handle;
  10. uint16_t flow_ctrl_char_handle;
  11. osMutexId_t buff_size_mtx;
  12. uint32_t buff_size;
  13. uint16_t bytes_ready_to_receive;
  14. SerialSvcDataReceivedCallback on_received_cb;
  15. SerialSvcDataSentCallback on_sent_cb;
  16. void* context;
  17. } SerialSvc;
  18. static SerialSvc* serial_svc = NULL;
  19. static const uint8_t service_uuid[] = {0x00, 0x00, 0xfe, 0x60, 0xcc, 0x7a, 0x48, 0x2a, 0x98, 0x4a, 0x7f, 0x2e, 0xd5, 0xb3, 0xe5, 0x8f};
  20. static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  21. static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  22. static const uint8_t flow_ctrl_uuid[] = {0x00, 0x00, 0xfe, 0x63, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  23. static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
  24. SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
  25. hci_event_pckt* event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)event)->data);
  26. evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
  27. aci_gatt_attribute_modified_event_rp0* attribute_modified;
  28. if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
  29. if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
  30. attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
  31. if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 2) {
  32. // Descriptor handle
  33. ret = SVCCTL_EvtAckFlowEnable;
  34. FURI_LOG_D(TAG, "RX descriptor event");
  35. } else if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 1) {
  36. FURI_LOG_D(TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
  37. if(serial_svc->on_received_cb) {
  38. furi_check(osMutexAcquire(serial_svc->buff_size_mtx, osWaitForever) == osOK);
  39. if(attribute_modified->Attr_Data_Length > serial_svc->bytes_ready_to_receive) {
  40. FURI_LOG_W(
  41. TAG, "Received %d, while was ready to receive %d bytes. Can lead to buffer overflow!",
  42. attribute_modified->Attr_Data_Length, serial_svc->bytes_ready_to_receive);
  43. }
  44. serial_svc->bytes_ready_to_receive -= MIN(serial_svc->bytes_ready_to_receive, attribute_modified->Attr_Data_Length);
  45. uint32_t buff_free_size =
  46. serial_svc->on_received_cb(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, serial_svc->context);
  47. FURI_LOG_D(TAG, "Available buff size: %d", buff_free_size);
  48. furi_check(osMutexRelease(serial_svc->buff_size_mtx) == osOK);
  49. }
  50. ret = SVCCTL_EvtAckFlowEnable;
  51. }
  52. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  53. FURI_LOG_D(TAG, "Ack received", blecore_evt->ecode);
  54. if(serial_svc->on_sent_cb) {
  55. serial_svc->on_sent_cb(serial_svc->context);
  56. }
  57. ret = SVCCTL_EvtAckFlowEnable;
  58. }
  59. }
  60. return ret;
  61. }
  62. void serial_svc_start() {
  63. tBleStatus status;
  64. serial_svc = furi_alloc(sizeof(SerialSvc));
  65. // Register event handler
  66. SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
  67. // Add service
  68. status = aci_gatt_add_service(UUID_TYPE_128, (Service_UUID_t *)service_uuid, PRIMARY_SERVICE, 10, &serial_svc->svc_handle);
  69. if(status) {
  70. FURI_LOG_E(TAG, "Failed to add Serial service: %d", status);
  71. }
  72. // Add RX characteristics
  73. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
  74. SERIAL_SVC_DATA_LEN_MAX,
  75. CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
  76. ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
  77. GATT_NOTIFY_ATTRIBUTE_WRITE,
  78. 10,
  79. CHAR_VALUE_LEN_VARIABLE,
  80. &serial_svc->rx_char_handle);
  81. if(status) {
  82. FURI_LOG_E(TAG, "Failed to add RX characteristic: %d", status);
  83. }
  84. // Add TX characteristic
  85. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
  86. SERIAL_SVC_DATA_LEN_MAX,
  87. CHAR_PROP_READ | CHAR_PROP_INDICATE,
  88. ATTR_PERMISSION_AUTHEN_READ,
  89. GATT_DONT_NOTIFY_EVENTS,
  90. 10,
  91. CHAR_VALUE_LEN_VARIABLE,
  92. &serial_svc->tx_char_handle);
  93. if(status) {
  94. FURI_LOG_E(TAG, "Failed to add TX characteristic: %d", status);
  95. }
  96. // Add Flow Control characteristic
  97. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)flow_ctrl_uuid,
  98. sizeof(uint32_t),
  99. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  100. ATTR_PERMISSION_AUTHEN_READ,
  101. GATT_DONT_NOTIFY_EVENTS,
  102. 10,
  103. CHAR_VALUE_LEN_CONSTANT,
  104. &serial_svc->flow_ctrl_char_handle);
  105. if(status) {
  106. FURI_LOG_E(TAG, "Failed to add Flow Control characteristic: %d", status);
  107. }
  108. // Allocate buffer size mutex
  109. serial_svc->buff_size_mtx = osMutexNew(NULL);
  110. }
  111. void serial_svc_set_callbacks(uint16_t buff_size, SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
  112. furi_assert(serial_svc);
  113. serial_svc->on_received_cb = on_received_cb;
  114. serial_svc->on_sent_cb = on_sent_cb;
  115. serial_svc->context = context;
  116. serial_svc->buff_size = buff_size;
  117. serial_svc->bytes_ready_to_receive = buff_size;
  118. uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
  119. aci_gatt_update_char_value(serial_svc->svc_handle, serial_svc->flow_ctrl_char_handle, 0, sizeof(uint32_t), (uint8_t*)&buff_size_reversed);
  120. }
  121. void serial_svc_notify_buffer_is_empty() {
  122. furi_assert(serial_svc);
  123. furi_assert(serial_svc->buff_size_mtx);
  124. furi_check(osMutexAcquire(serial_svc->buff_size_mtx, osWaitForever) == osOK);
  125. if(serial_svc->bytes_ready_to_receive == 0) {
  126. FURI_LOG_D(TAG, "Buffer is empty. Notifying client");
  127. serial_svc->bytes_ready_to_receive = serial_svc->buff_size;
  128. uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
  129. aci_gatt_update_char_value(serial_svc->svc_handle, serial_svc->flow_ctrl_char_handle, 0, sizeof(uint32_t), (uint8_t*)&buff_size_reversed);
  130. }
  131. furi_check(osMutexRelease(serial_svc->buff_size_mtx) == osOK);
  132. }
  133. void serial_svc_stop() {
  134. tBleStatus status;
  135. if(serial_svc) {
  136. // Delete characteristics
  137. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->tx_char_handle);
  138. if(status) {
  139. FURI_LOG_E(TAG, "Failed to delete TX characteristic: %d", status);
  140. }
  141. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->rx_char_handle);
  142. if(status) {
  143. FURI_LOG_E(TAG, "Failed to delete RX characteristic: %d", status);
  144. }
  145. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->flow_ctrl_char_handle);
  146. if(status) {
  147. FURI_LOG_E(TAG, "Failed to delete Flow Control characteristic: %d", status);
  148. }
  149. // Delete service
  150. status = aci_gatt_del_service(serial_svc->svc_handle);
  151. if(status) {
  152. FURI_LOG_E(TAG, "Failed to delete Serial service: %d", status);
  153. }
  154. // Delete buffer size mutex
  155. osMutexDelete(serial_svc->buff_size_mtx);
  156. free(serial_svc);
  157. serial_svc = NULL;
  158. }
  159. }
  160. bool serial_svc_update_tx(uint8_t* data, uint8_t data_len) {
  161. if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
  162. return false;
  163. }
  164. tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle,
  165. serial_svc->tx_char_handle,
  166. 0,
  167. data_len,
  168. data);
  169. if(result) {
  170. FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result);
  171. }
  172. return result != BLE_STATUS_SUCCESS;
  173. }