seos_service.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #include "seos_service.h"
  2. #include "app_common.h"
  3. //#include <ble/ble.h>
  4. #include <furi_ble/event_dispatcher.h>
  5. #include <furi_ble/gatt.h>
  6. #include "ble_vs_codes.h"
  7. #include "ble_gatt_aci.h"
  8. #include <furi.h>
  9. #include "seos_service_uuid.inc"
  10. #include <stdint.h>
  11. #define TAG "BtSeosSvc"
  12. typedef enum {
  13. SeosSvcGattCharacteristicRxTx = 0,
  14. SeosSvcGattCharacteristicCount,
  15. } SeosSvcGattCharacteristicId;
  16. typedef struct {
  17. const void* data_ptr;
  18. uint16_t data_len;
  19. } SeosSvcDataWrapper;
  20. static bool
  21. ble_svc_seos_data_callback(const void* context, const uint8_t** data, uint16_t* data_len) {
  22. FURI_LOG_D(TAG, "ble_svc_seos_data_callback");
  23. const SeosSvcDataWrapper* report_data = context;
  24. if(data) {
  25. *data = report_data->data_ptr;
  26. *data_len = report_data->data_len;
  27. } else {
  28. *data_len = BLE_SVC_SEOS_DATA_LEN_MAX;
  29. }
  30. return false;
  31. }
  32. static const BleGattCharacteristicParams ble_svc_seos_chars[SeosSvcGattCharacteristicCount] = {
  33. [SeosSvcGattCharacteristicRxTx] =
  34. {.name = "SEOS",
  35. .data_prop_type = FlipperGattCharacteristicDataCallback,
  36. .data.callback.fn = ble_svc_seos_data_callback,
  37. .data.callback.context = NULL,
  38. //.max_length = BLE_SVC_SEOS_DATA_LEN_MAX,
  39. .uuid.Char_UUID_128 = BLE_SVC_SEOS_CHAR_UUID,
  40. .uuid_type = UUID_TYPE_128,
  41. .char_properties = CHAR_PROP_WRITE_WITHOUT_RESP | CHAR_PROP_NOTIFY,
  42. .security_permissions = ATTR_PERMISSION_NONE,
  43. .gatt_evt_mask = GATT_NOTIFY_ATTRIBUTE_WRITE,
  44. .is_variable = CHAR_VALUE_LEN_VARIABLE},
  45. };
  46. struct BleServiceSeos {
  47. uint16_t svc_handle;
  48. BleGattCharacteristicInstance chars[SeosSvcGattCharacteristicCount];
  49. FuriMutex* buff_size_mtx;
  50. uint32_t buff_size;
  51. uint16_t bytes_ready_to_receive;
  52. SeosServiceEventCallback callback;
  53. void* context;
  54. GapSvcEventHandler* event_handler;
  55. };
  56. static BleEventAckStatus ble_svc_seos_event_handler(void* event, void* context) {
  57. FURI_LOG_D(TAG, "ble_svc_seos_event_handler");
  58. BleServiceSeos* seos_svc = (BleServiceSeos*)context;
  59. BleEventAckStatus ret = BleEventNotAck;
  60. hci_event_pckt* event_pckt = (hci_event_pckt*)(((hci_uart_pckt*)event)->data);
  61. evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
  62. aci_gatt_attribute_modified_event_rp0* attribute_modified;
  63. if(event_pckt->evt == HCI_LE_META_EVT_CODE) {
  64. // FURI_LOG_D(TAG, "ble_svc_seos_event_handler HCI_LE_META_EVT_CODE %02x", event_pckt->data[0]);
  65. } else if(event_pckt->evt == HCI_DISCONNECTION_COMPLETE_EVT_CODE) {
  66. } else if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
  67. if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
  68. FURI_LOG_D(TAG, "Process modification events");
  69. attribute_modified = (aci_gatt_attribute_modified_event_rp0*)blecore_evt->data;
  70. if(attribute_modified->Attr_Handle ==
  71. seos_svc->chars[SeosSvcGattCharacteristicRxTx].handle + 2) {
  72. // Descriptor handle
  73. ret = BleEventAckFlowEnable;
  74. if(attribute_modified->Attr_Data_Length == 2) {
  75. uint16_t* value = (uint16_t*)attribute_modified->Attr_Data;
  76. FURI_LOG_D(TAG, "descriptor event %04x", *value);
  77. if(*value == 1) { // ENABLE_NOTIFICATION_VALUE)
  78. uint8_t select_header[] = {0xc0, 0x00, 0xa4, 0x04, 0x00};
  79. SeosSvcDataWrapper report_data = {
  80. .data_ptr = select_header, .data_len = sizeof(select_header)};
  81. ble_gatt_characteristic_update(
  82. seos_svc->svc_handle,
  83. &seos_svc->chars[SeosSvcGattCharacteristicRxTx],
  84. &report_data);
  85. }
  86. } else {
  87. FURI_LOG_D(
  88. TAG, "descriptor event %d bytes", attribute_modified->Attr_Data_Length);
  89. }
  90. } else if(
  91. attribute_modified->Attr_Handle ==
  92. seos_svc->chars[SeosSvcGattCharacteristicRxTx].handle + 1) {
  93. FURI_LOG_D(TAG, "Received %d bytes", attribute_modified->Attr_Data_Length);
  94. if(seos_svc->callback) {
  95. furi_check(
  96. furi_mutex_acquire(seos_svc->buff_size_mtx, FuriWaitForever) ==
  97. FuriStatusOk);
  98. if(attribute_modified->Attr_Data_Length > seos_svc->bytes_ready_to_receive) {
  99. FURI_LOG_W(
  100. TAG,
  101. "Received %d, while was ready to receive %d bytes. Can lead to buffer overflow!",
  102. attribute_modified->Attr_Data_Length,
  103. seos_svc->bytes_ready_to_receive);
  104. }
  105. seos_svc->bytes_ready_to_receive -= MIN(
  106. seos_svc->bytes_ready_to_receive, attribute_modified->Attr_Data_Length);
  107. SeosServiceEvent event = {
  108. .event = SeosServiceEventTypeDataReceived,
  109. .data = {
  110. .buffer = attribute_modified->Attr_Data,
  111. .size = attribute_modified->Attr_Data_Length,
  112. }};
  113. uint32_t buff_free_size = seos_svc->callback(event, seos_svc->context);
  114. FURI_LOG_D(TAG, "Available buff size: %ld", buff_free_size);
  115. furi_check(furi_mutex_release(seos_svc->buff_size_mtx) == FuriStatusOk);
  116. } else {
  117. FURI_LOG_W(TAG, "No seos_cvs->callback defined");
  118. }
  119. ret = BleEventAckFlowEnable;
  120. }
  121. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  122. FURI_LOG_T(TAG, "Ack received");
  123. if(seos_svc->callback) {
  124. SeosServiceEvent event = {
  125. .event = SeosServiceEventTypeDataSent,
  126. };
  127. seos_svc->callback(event, seos_svc->context);
  128. }
  129. ret = BleEventAckFlowEnable;
  130. } else {
  131. FURI_LOG_D(
  132. TAG,
  133. "ble_svc_seos_event_handler unhandled blecore_evt->ecode %d",
  134. blecore_evt->ecode);
  135. }
  136. } else {
  137. FURI_LOG_D(
  138. TAG, "ble_svc_seos_event_handler unhandled event_pckt->evt %d", event_pckt->evt);
  139. }
  140. return ret;
  141. }
  142. BleServiceSeos* ble_svc_seos_start(void) {
  143. FURI_LOG_D(TAG, "ble_svc_seos_start");
  144. BleServiceSeos* seos_svc = malloc(sizeof(BleServiceSeos));
  145. seos_svc->event_handler =
  146. ble_event_dispatcher_register_svc_handler(ble_svc_seos_event_handler, seos_svc);
  147. if(!ble_gatt_service_add(
  148. UUID_TYPE_128, &service_uuid, PRIMARY_SERVICE, 12, &seos_svc->svc_handle)) {
  149. free(seos_svc);
  150. return NULL;
  151. }
  152. for(uint8_t i = 0; i < SeosSvcGattCharacteristicCount; i++) {
  153. FURI_LOG_D(TAG, "ble_gatt_characteristic_init %d", i);
  154. ble_gatt_characteristic_init(
  155. seos_svc->svc_handle, &ble_svc_seos_chars[i], &seos_svc->chars[i]);
  156. }
  157. seos_svc->buff_size_mtx = furi_mutex_alloc(FuriMutexTypeNormal);
  158. return seos_svc;
  159. }
  160. void ble_svc_seos_set_callbacks(
  161. BleServiceSeos* seos_svc,
  162. uint16_t buff_size,
  163. SeosServiceEventCallback callback,
  164. void* context) {
  165. furi_check(seos_svc);
  166. FURI_LOG_D(TAG, "ble_svc_seos_set_callbacks");
  167. seos_svc->callback = callback;
  168. seos_svc->context = context;
  169. seos_svc->buff_size = buff_size;
  170. seos_svc->bytes_ready_to_receive = buff_size;
  171. /*
  172. uint32_t buff_size_reversed = REVERSE_BYTES_U32(seos_svc->buff_size);
  173. ble_gatt_characteristic_update(
  174. seos_svc->svc_handle,
  175. &seos_svc->chars[SeosSvcGattCharacteristicFlowCtrl],
  176. &buff_size_reversed);
  177. */
  178. }
  179. void ble_svc_seos_notify_buffer_is_empty(BleServiceSeos* seos_svc) {
  180. furi_check(seos_svc);
  181. furi_check(seos_svc->buff_size_mtx);
  182. FURI_LOG_D(TAG, "ble_svc_seos_notify_buffer_is_empty");
  183. furi_check(furi_mutex_acquire(seos_svc->buff_size_mtx, FuriWaitForever) == FuriStatusOk);
  184. if(seos_svc->bytes_ready_to_receive == 0) {
  185. FURI_LOG_D(TAG, "Buffer is empty. Notifying client");
  186. seos_svc->bytes_ready_to_receive = seos_svc->buff_size;
  187. /*
  188. uint32_t buff_size_reversed = REVERSE_BYTES_U32(seos_svc->buff_size);
  189. ble_gatt_characteristic_update(
  190. seos_svc->svc_handle,
  191. &seos_svc->chars[SeosSvcGattCharacteristicFlowCtrl],
  192. &buff_size_reversed);
  193. */
  194. }
  195. furi_check(furi_mutex_release(seos_svc->buff_size_mtx) == FuriStatusOk);
  196. }
  197. void ble_svc_seos_stop(BleServiceSeos* seos_svc) {
  198. furi_check(seos_svc);
  199. ble_event_dispatcher_unregister_svc_handler(seos_svc->event_handler);
  200. for(uint8_t i = 0; i < SeosSvcGattCharacteristicCount; i++) {
  201. ble_gatt_characteristic_delete(seos_svc->svc_handle, &seos_svc->chars[i]);
  202. }
  203. ble_gatt_service_delete(seos_svc->svc_handle);
  204. furi_mutex_free(seos_svc->buff_size_mtx);
  205. free(seos_svc);
  206. }
  207. bool ble_svc_seos_update_tx(BleServiceSeos* seos_svc, uint8_t* data, uint16_t data_len) {
  208. FURI_LOG_D(TAG, "ble_svc_seos_update_tx");
  209. UNUSED(seos_svc);
  210. UNUSED(data);
  211. if(data_len > BLE_SVC_SEOS_DATA_LEN_MAX) {
  212. return false;
  213. }
  214. /*
  215. for(uint16_t remained = data_len; remained > 0;) {
  216. uint8_t value_len = MIN(BLE_SVC_SEOS_CHAR_VALUE_LEN_MAX, remained);
  217. uint16_t value_offset = data_len - remained;
  218. remained -= value_len;
  219. tBleStatus result = aci_gatt_update_char_value_ext(
  220. 0,
  221. seos_svc->svc_handle,
  222. seos_svc->chars[SeosSvcGattCharacteristicTx].handle,
  223. remained ? 0x00 : 0x02,
  224. data_len,
  225. value_offset,
  226. value_len,
  227. data + value_offset);
  228. if(result) {
  229. FURI_LOG_E(TAG, "Failed updating TX characteristic: %d", result);
  230. return false;
  231. }
  232. }
  233. */
  234. return true;
  235. }