serial_service.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. } SerialSvc;
  12. static SerialSvc* serial_svc;
  13. static const uint8_t service_uuid[] = {0x00, 0x00, 0xfe, 0x60, 0xcc, 0x7a, 0x48, 0x2a, 0x98, 0x4a, 0x7f, 0x2e, 0xd5, 0xb3, 0xe5, 0x8f};
  14. static const uint8_t char_rx_uuid[] = {0x00, 0x00, 0xfe, 0x61, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  15. static const uint8_t char_tx_uuid[] = {0x00, 0x00, 0xfe, 0x62, 0x8e, 0x22, 0x45, 0x41, 0x9d, 0x4c, 0x21, 0xed, 0xae, 0x82, 0xed, 0x19};
  16. static SVCCTL_EvtAckStatus_t serial_svc_event_handler(void *event) {
  17. SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
  18. hci_event_pckt* event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)event)->data);
  19. evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
  20. aci_gatt_attribute_modified_event_rp0* attribute_modified;
  21. if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
  22. if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
  23. attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
  24. if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 2) {
  25. // Descriptor handle
  26. ret = SVCCTL_EvtAckFlowEnable;
  27. FURI_LOG_D(SERIAL_SERVICE_TAG, "TX descriptor event");
  28. } else if(attribute_modified->Attr_Handle == serial_svc->tx_char_handle + 1) {
  29. FURI_LOG_D(SERIAL_SERVICE_TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
  30. serial_svc_update_rx(attribute_modified->Attr_Data, attribute_modified->Attr_Data_Length);
  31. ret = SVCCTL_EvtAckFlowEnable;
  32. }
  33. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  34. FURI_LOG_D(SERIAL_SERVICE_TAG, "Ack received", blecore_evt->ecode);
  35. ret = SVCCTL_EvtAckFlowEnable;
  36. }
  37. }
  38. return ret;
  39. }
  40. void serial_svc_start() {
  41. tBleStatus status;
  42. serial_svc = furi_alloc(sizeof(SerialSvc));
  43. // Register event handler
  44. SVCCTL_RegisterSvcHandler(serial_svc_event_handler);
  45. // Add service
  46. status = aci_gatt_add_service(UUID_TYPE_128, (Service_UUID_t *)service_uuid, PRIMARY_SERVICE, 6, &serial_svc->svc_handle);
  47. if(status) {
  48. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add Serial service: %d", status);
  49. }
  50. // Add TX characteristics
  51. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_tx_uuid,
  52. SERIAL_SVC_DATA_LEN_MAX,
  53. CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_WRITE | CHAR_PROP_READ,
  54. ATTR_PERMISSION_AUTHEN_READ | ATTR_PERMISSION_AUTHEN_WRITE,
  55. GATT_NOTIFY_ATTRIBUTE_WRITE,
  56. 10,
  57. CHAR_VALUE_LEN_VARIABLE,
  58. &serial_svc->tx_char_handle);
  59. if(status) {
  60. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add TX characteristic: %d", status);
  61. }
  62. // Add RX characteristic
  63. status = aci_gatt_add_char(serial_svc->svc_handle, UUID_TYPE_128, (const Char_UUID_t*)char_rx_uuid,
  64. SERIAL_SVC_DATA_LEN_MAX,
  65. CHAR_PROP_READ | CHAR_PROP_INDICATE,
  66. ATTR_PERMISSION_AUTHEN_READ,
  67. GATT_DONT_NOTIFY_EVENTS,
  68. 10,
  69. CHAR_VALUE_LEN_VARIABLE,
  70. &serial_svc->rx_char_handle);
  71. if(status) {
  72. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to add RX characteristic: %d", status);
  73. }
  74. }
  75. void serial_svc_stop() {
  76. tBleStatus status;
  77. if(serial_svc) {
  78. // Delete characteristics
  79. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->tx_char_handle);
  80. if(status) {
  81. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete TX characteristic: %d", status);
  82. }
  83. status = aci_gatt_del_char(serial_svc->svc_handle, serial_svc->rx_char_handle);
  84. if(status) {
  85. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete RX characteristic: %d", status);
  86. }
  87. // Delete service
  88. status = aci_gatt_del_service(serial_svc->svc_handle);
  89. if(status) {
  90. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed to delete Serial service: %d", status);
  91. }
  92. free(serial_svc);
  93. serial_svc = NULL;
  94. }
  95. }
  96. bool serial_svc_update_rx(uint8_t* data, uint8_t data_len) {
  97. if(data_len > SERIAL_SVC_DATA_LEN_MAX) {
  98. return false;
  99. }
  100. tBleStatus result = aci_gatt_update_char_value(serial_svc->svc_handle,
  101. serial_svc->rx_char_handle,
  102. 0,
  103. data_len,
  104. data);
  105. if(result) {
  106. FURI_LOG_E(SERIAL_SERVICE_TAG, "Failed updating RX characteristic: %d", result);
  107. }
  108. return result != BLE_STATUS_SUCCESS;
  109. }