serial_service.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. typedef struct {
  7. uint16_t svc_handle;
  8. uint16_t rx_char_handle;
  9. uint16_t tx_char_handle;
  10. SerialSvcDataReceivedCallback on_received_cb;
  11. SerialSvcDataSentCallback on_sent_cb;
  12. void* context;
  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, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  17. static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  18. static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
  19. SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
  20. hci_event_pckt* event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)event)->data);
  21. evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
  22. aci_gatt_attribute_modified_event_rp0* attribute_modified;
  23. if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
  24. if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
  25. attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
  26. if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 2) {
  27. // Descriptor handle
  28. ret = SVCCTL_EvtAckFlowEnable;
  29. FURI_LOG_D(SERIAL_SERVICE_TAG, "RX descriptor event");
  30. } else if(attribute_modified->Attr_Handle == serial_svc->rx_char_handle + 1) {
  31. FURI_LOG_D(SERIAL_SERVICE_TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
  32. if(serial_svc->on_received_cb) {
  33. serial_svc->on_received_cb(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length, serial_svc->context);
  34. }
  35. ret = SVCCTL_EvtAckFlowEnable;
  36. }
  37. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  38. FURI_LOG_D(SERIAL_SERVICE_TAG, "Ack received", blecore_evt->ecode);
  39. if(serial_svc->on_sent_cb) {
  40. serial_svc->on_sent_cb(serial_svc->context);
  41. }
  42. ret = SVCCTL_EvtAckFlowEnable;
  43. }
  44. }
  45. return ret;
  46. }
  47. void serial_svc_start() {
  48. tBleStatus status;
  49. serial_svc = furi_alloc(sizeof(SerialSvc));
  50. // Register event handler
  51. SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
  52. // Add service
  53. status = aci_gatt_add_service(UUID_TYPE_128, (Service_UUID_t *)service_uuid, PRIMARY_SERVICE, 6, &serial_svc->svc_handle);
  54. if(status) {
  55. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add Serial service: %d", status);
  56. }
  57. // Add RX characteristics
  58. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
  59. SERIAL_SVC_DATA_LEN_MAX,
  60. CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
  61. ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
  62. GATT_NOTIFY_ATTRIBUTE_WRITE,
  63. 10,
  64. CHAR_VALUE_LEN_VARIABLE,
  65. &serial_svc->rx_char_handle);
  66. if(status) {
  67. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
  68. }
  69. // Add TX characteristic
  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_READ | CHAR_PROP_INDICATE,
  73. ATTR_PERMISSION_AUTHEN_READ,
  74. GATT_DONT_NOTIFY_EVENTS,
  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. }
  82. void serial_svc_set_callbacks(SerialSvcDataReceivedCallback on_received_cb, SerialSvcDataSentCallback on_sent_cb, void* context) {
  83. serial_svc->on_received_cb = on_received_cb;
  84. serial_svc->on_sent_cb = on_sent_cb;
  85. serial_svc->context = context;
  86. }
  87. void serial_svc_stop() {
  88. tBleStatus status;
  89. if(serial_svc) {
  90. // Delete characteristics
  91. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->tx_char_handle);
  92. if(status) {
  93. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete TX characteristic: %d", status);
  94. }
  95. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->rx_char_handle);
  96. if(status) {
  97. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete RX characteristic: %d", status);
  98. }
  99. // Delete service
  100. status = aci_gatt_del_service(serial_svc->svc_handle);
  101. if(status) {
  102. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete Serial service: %d", status);
  103. }
  104. free(serial_svc);
  105. serial_svc = NULL;
  106. }
  107. }
  108. bool serial_svc_update_tx(uint8_t* data, uint8_t data_len) {
  109. if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
  110. return false;
  111. }
  112. FURI_LOG_D(SERIAL_SERVICE_TAG, "Updating char %d len", data_len);
  113. tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle,
  114. serial_svc->tx_char_handle,
  115. 0,
  116. data_len,
  117. data);
  118. if(result) {
  119. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating TX characteristic: %d", result);
  120. }
  121. return result != BLE_STATUS_SUCCESS;
  122. }