serial_service.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. FuriMutex* 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(
  42. furi_mutex_acquire(serial_svc->buff_size_mtx, FuriWaitForever) ==
  43. FuriStatusOk);
  44. if(attribute_modified->Attr_Data_Length > serial_svc->bytes_ready_to_receive) {
  45. FURI_LOG_W(
  46. TAG,
  47. "Received %d, while was ready to receive %d bytes. Can lead to buffer overflow!",
  48. attribute_modified->Attr_Data_Length,
  49. serial_svc->bytes_ready_to_receive);
  50. }
  51. serial_svc->bytes_ready_to_receive -= MIN(
  52. serial_svc->bytes_ready_to_receive, attribute_modified->Attr_Data_Length);
  53. SerialServiceEvent event = {
  54. .event = SerialServiceEventTypeDataReceived,
  55. .data = {
  56. .buffer = attribute_modified->Attr_Data,
  57. .size = attribute_modified->Attr_Data_Length,
  58. }};
  59. uint32_t buff_free_size = serial_svc->callback(event, serial_svc->context);
  60. FURI_LOG_D(TAG, "Available buff size: %d", buff_free_size);
  61. furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
  62. }
  63. ret = SVCCTL_EvtAckFlowEnable;
  64. }
  65. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  66. FURI_LOG_T(TAG, "Ack received", blecore_evt->ecode);
  67. if(serial_svc->callback) {
  68. SerialServiceEvent event = {
  69. .event = SerialServiceEventTypeDataSent,
  70. };
  71. serial_svc->callback(event, serial_svc->context);
  72. }
  73. ret = SVCCTL_EvtAckFlowEnable;
  74. }
  75. }
  76. return ret;
  77. }
  78. void serial_svc_start() {
  79. tBleStatus status;
  80. serial_svc = malloc(sizeof(SerialSvc));
  81. // Register event handler
  82. SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
  83. // Add service
  84. status = aci_gatt_add_service(
  85. UUID_TYPE_128, (Service_UUID_t*)service_uuid, PRIMARY_SERVICE, 10, &serial_svc->svc_handle);
  86. if(status) {
  87. FURI_LOG_E(TAG, "Failed to add Serial service: %d", status);
  88. }
  89. // Add RX characteristics
  90. status = aci_gatt_add_char(
  91. serial_svc->svc_handle,
  92. UUID_TYPE_128,
  93. (const Char_UUID_t*)char_rx_uuid,
  94. SERIAL_SVC_DATA_LEN_MAX,
  95. CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
  96. ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
  97. GATT_NOTIFY_ATTRIBUTE_WRITE,
  98. 10,
  99. CHAR_VALUE_LEN_VARIABLE,
  100. &serial_svc->rx_char_handle);
  101. if(status) {
  102. FURI_LOG_E(TAG, "Failed to add RX characteristic: %d", status);
  103. }
  104. // Add TX characteristic
  105. status = aci_gatt_add_char(
  106. serial_svc->svc_handle,
  107. UUID_TYPE_128,
  108. (const Char_UUID_t*)char_tx_uuid,
  109. SERIAL_SVC_DATA_LEN_MAX,
  110. CHAR_PROP_READ | CHAR_PROP_INDICATE,
  111. ATTR_PERMISSION_AUTHEN_READ,
  112. GATT_DONT_NOTIFY_EVENTS,
  113. 10,
  114. CHAR_VALUE_LEN_VARIABLE,
  115. &serial_svc->tx_char_handle);
  116. if(status) {
  117. FURI_LOG_E(TAG, "Failed to add TX characteristic: %d", status);
  118. }
  119. // Add Flow Control characteristic
  120. status = aci_gatt_add_char(
  121. serial_svc->svc_handle,
  122. UUID_TYPE_128,
  123. (const Char_UUID_t*)flow_ctrl_uuid,
  124. sizeof(uint32_t),
  125. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  126. ATTR_PERMISSION_AUTHEN_READ,
  127. GATT_DONT_NOTIFY_EVENTS,
  128. 10,
  129. CHAR_VALUE_LEN_CONSTANT,
  130. &serial_svc->flow_ctrl_char_handle);
  131. if(status) {
  132. FURI_LOG_E(TAG, "Failed to add Flow Control characteristic: %d", status);
  133. }
  134. // Allocate buffer size mutex
  135. serial_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
  136. }
  137. void serial_svc_set_callbacks(
  138. uint16_t buff_size,
  139. SerialServiceEventCallback callback,
  140. void* context) {
  141. furi_assert(serial_svc);
  142. serial_svc->callback = callback;
  143. serial_svc->context = context;
  144. serial_svc->buff_size = buff_size;
  145. serial_svc->bytes_ready_to_receive = buff_size;
  146. uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
  147. aci_gatt_update_char_value(
  148. serial_svc->svc_handle,
  149. serial_svc->flow_ctrl_char_handle,
  150. 0,
  151. sizeof(uint32_t),
  152. (uint8_t*)&buff_size_reversed);
  153. }
  154. void serial_svc_notify_buffer_is_empty() {
  155. furi_assert(serial_svc);
  156. furi_assert(serial_svc->buff_size_mtx);
  157. furi_check(furi_mutex_acquire(serial_svc->buff_size_mtx, FuriWaitForever) == FuriStatusOk);
  158. if(serial_svc->bytes_ready_to_receive == 0) {
  159. FURI_LOG_D(TAG, "Buffer is empty. Notifying client");
  160. serial_svc->bytes_ready_to_receive = serial_svc->buff_size;
  161. uint32_t buff_size_reversed = REVERSE_BYTES_U32(serial_svc->buff_size);
  162. aci_gatt_update_char_value(
  163. serial_svc->svc_handle,
  164. serial_svc->flow_ctrl_char_handle,
  165. 0,
  166. sizeof(uint32_t),
  167. (uint8_t*)&buff_size_reversed);
  168. }
  169. furi_check(furi_mutex_release(serial_svc->buff_size_mtx) == FuriStatusOk);
  170. }
  171. void serial_svc_stop() {
  172. tBleStatus status;
  173. if(serial_svc) {
  174. // Delete characteristics
  175. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->tx_char_handle);
  176. if(status) {
  177. FURI_LOG_E(TAG, "Failed to delete TX characteristic: %d", status);
  178. }
  179. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->rx_char_handle);
  180. if(status) {
  181. FURI_LOG_E(TAG, "Failed to delete RX characteristic: %d", status);
  182. }
  183. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->flow_ctrl_char_handle);
  184. if(status) {
  185. FURI_LOG_E(TAG, "Failed to delete Flow Control characteristic: %d", status);
  186. }
  187. // Delete service
  188. status = aci_gatt_del_service(serial_svc->svc_handle);
  189. if(status) {
  190. FURI_LOG_E(TAG, "Failed to delete Serial service: %d", status);
  191. }
  192. // Delete buffer size mutex
  193. furi_mutex_free(serial_svc->buff_size_mtx);
  194. free(serial_svc);
  195. serial_svc = NULL;
  196. }
  197. }
  198. bool serial_svc_is_started() {
  199. return serial_svc != NULL;
  200. }
  201. bool serial_svc_update_tx(uint8_t* data, uint16_t data_len) {
  202. if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
  203. return false;
  204. }
  205. for(uint16_t remained = data_len; remained > 0;) {
  206. uint8_t value_len = MIN(SERIAL_SVC_CHAR_VALUE_LEN_MAX, remained);
  207. uint16_t value_offset = data_len - remained;
  208. remained -= value_len;
  209. tBleStatus result = aci_gatt_update_char_value_ext(
  210. 0,
  211. serial_svc->svc_handle,
  212. serial_svc->tx_char_handle,
  213. remained ? 0x00 : 0x02,
  214. data_len,
  215. value_offset,
  216. value_len,
  217. data + value_offset);
  218. if(result) {
  219. FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result);
  220. return false;
  221. }
  222. }
  223. return true;
  224. }