serial_service.c 8.9 KB

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