serial_service.c 9.4 KB

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