serial_service.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "serial_service.h"
  2. #include "app_common.h"
  3. #include "ble.h"
  4. #include <furi.h>
  5. #define SERIAL_SERVICE_TAG "serial service"
  6. #define SERIAL_SVC_DATA_LEN_MAX 245
  7. typedef struct {
  8. uint16_t svc_handle;
  9. uint16_t rx_char_handle;
  10. uint16_t tx_char_handle;
  11. RpcSession* rpc_session;
  12. osSemaphoreId_t rpc_sem;
  13. } SerialSvc;
  14. static SerialSvc* serial_svc;
  15. static const uint8_t service_uuid[] = {0x00, 0x00, 0xfe, 0x60, 0xcc, 0x7a, 0x48, 0x2a, 0x98, 0x4a, 0x7f, 0x2e, 0xd5, 0xb3, 0xe5, 0x8f};
  16. static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  17. static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  18. void serial_svc_rpc_send_bytes_callback(void* context, uint8_t* bytes, size_t bytes_len) {
  19. size_t bytes_sent = 0;
  20. while(bytes_sent < bytes_len) {
  21. size_t bytes_remain = bytes_len - bytes_sent;
  22. if(bytes_remain > SERIAL_SVC_DATA_LEN_MAX) {
  23. serial_svc_update_rx(&bytes[bytes_sent], SERIAL_SVC_DATA_LEN_MAX);
  24. bytes_sent += SERIAL_SVC_DATA_LEN_MAX;
  25. } else {
  26. serial_svc_update_rx(&bytes[bytes_sent], bytes_remain);
  27. bytes_sent += bytes_remain;
  28. }
  29. osSemaphoreAcquire(serial_svc->rpc_sem, osWaitForever);
  30. }
  31. }
  32. static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
  33. SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
  34. hci_event_pckt* event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)event)->data);
  35. evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
  36. aci_gatt_attribute_modified_event_rp0* attribute_modified;
  37. if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
  38. if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
  39. attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
  40. if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 2) {
  41. // Descriptor handle
  42. ret = SVCCTL_EvtAckFlowEnable;
  43. FURI_LOG_D(SERIAL_SERVICE_TAG, "TX descriptor event");
  44. } else if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 1) {
  45. FURI_LOG_D(SERIAL_SERVICE_TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
  46. rpc_feed_bytes(serial_svc->rpc_session, attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, 1000);
  47. // serial_svc_update_rx(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length);
  48. ret = SVCCTL_EvtAckFlowEnable;
  49. }
  50. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  51. osSemaphoreRelease(serial_svc->rpc_sem);
  52. FURI_LOG_D(SERIAL_SERVICE_TAG, "Ack received", blecore_evt->ecode);
  53. ret = SVCCTL_EvtAckFlowEnable;
  54. }
  55. }
  56. return ret;
  57. }
  58. void serial_svc_start(Rpc* rpc) {
  59. tBleStatus status;
  60. serial_svc = furi_alloc(sizeof(SerialSvc));
  61. serial_svc->rpc_sem = osSemaphoreNew(1, 0, NULL);
  62. // Register event handler
  63. SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
  64. // Add service
  65. status = aci_gatt_add_service(UUID_TYPE_128, (Service_UUID_t *)service_uuid, PRIMARY_SERVICE, 6, &serial_svc->svc_handle);
  66. if(status) {
  67. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add Serial service: %d", status);
  68. }
  69. // Add TX characteristics
  70. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
  71. SERIAL_SVC_DATA_LEN_MAX,
  72. CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
  73. ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
  74. GATT_NOTIFY_ATTRIBUTE_WRITE,
  75. 10,
  76. CHAR_VALUE_LEN_VARIABLE,
  77. &serial_svc->tx_char_handle);
  78. if(status) {
  79. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add TX characteristic: %d", status);
  80. }
  81. // Add RX characteristic
  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_READ | CHAR_PROP_INDICATE,
  85. ATTR_PERMISSION_AUTHEN_READ,
  86. GATT_DONT_NOTIFY_EVENTS,
  87. 10,
  88. CHAR_VALUE_LEN_VARIABLE,
  89. &serial_svc->rx_char_handle);
  90. if(status) {
  91. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
  92. }
  93. }
  94. void serial_svc_stop() {
  95. tBleStatus status;
  96. if(serial_svc) {
  97. // Delete characteristics
  98. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->tx_char_handle);
  99. if(status) {
  100. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete TX characteristic: %d", status);
  101. }
  102. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->rx_char_handle);
  103. if(status) {
  104. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete RX characteristic: %d", status);
  105. }
  106. // Delete service
  107. status = aci_gatt_del_service(serial_svc->svc_handle);
  108. if(status) {
  109. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete Serial service: %d", status);
  110. }
  111. free(serial_svc);
  112. serial_svc = NULL;
  113. }
  114. }
  115. void serial_svc_set_rpc_session(RpcSession* rpc_session) {
  116. furi_assert(rpc_session);
  117. // Set session
  118. serial_svc->rpc_session = rpc_session;
  119. // Set callback
  120. rpc_set_send_bytes_callback(serial_svc->rpc_session, serial_svc_rpc_send_bytes_callback, NULL);
  121. }
  122. bool serial_svc_update_rx(uint8_t* data, uint8_t data_len) {
  123. if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
  124. return false;
  125. }
  126. FURI_LOG_D(SERIAL_SERVICE_TAG, "Updating char %d len", data_len);
  127. tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle,
  128. serial_svc->rx_char_handle,
  129. 0,
  130. data_len,
  131. data);
  132. if(result) {
  133. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating RX characteristic: %d", result);
  134. }
  135. return result != BLE_STATUS_SUCCESS;
  136. }