hid_service.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include "hid_service.h"
  2. #include "app_common.h"
  3. #include "ble.h"
  4. #include <furi.h>
  5. #define TAG "BtHid"
  6. typedef struct {
  7. uint16_t svc_handle;
  8. uint16_t protocol_mode_char_handle;
  9. uint16_t report_char_handle;
  10. uint16_t report_ref_desc_handle;
  11. uint16_t report_map_char_handle;
  12. uint16_t keyboard_boot_char_handle;
  13. uint16_t info_char_handle;
  14. uint16_t ctrl_point_char_handle;
  15. } HIDSvc;
  16. static HIDSvc* hid_svc = NULL;
  17. static SVCCTL_EvtAckStatus_t hid_svc_event_handler(void *event) {
  18. SVCCTL_EvtAckStatus_t ret = SVCCTL_EvtNotAck;
  19. hci_event_pckt* event_pckt = (hci_event_pckt *)(((hci_uart_pckt*)event)->data);
  20. evt_blecore_aci* blecore_evt = (evt_blecore_aci*)event_pckt->data;
  21. // aci_gatt_attribute_modified_event_rp0* attribute_modified;
  22. if(event_pckt->evt == HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE) {
  23. if(blecore_evt->ecode == ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE) {
  24. // Process modification events
  25. ret = SVCCTL_EvtAckFlowEnable;
  26. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  27. // Process notification confirmation
  28. ret = SVCCTL_EvtAckFlowEnable;
  29. }
  30. }
  31. return ret;
  32. }
  33. void hid_svc_start() {
  34. tBleStatus status;
  35. hid_svc = furi_alloc(sizeof(HIDSvc));
  36. Service_UUID_t svc_uuid = {};
  37. Char_Desc_Uuid_t desc_uuid = {};
  38. Char_UUID_t char_uuid = {};
  39. // Register event handler
  40. SVCCTL_RegisterSvcHandler(hid_svc_event_handler);
  41. // Add service
  42. svc_uuid.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID;
  43. status = aci_gatt_add_service(UUID_TYPE_16,
  44. &svc_uuid,
  45. PRIMARY_SERVICE,
  46. 30,
  47. &hid_svc->svc_handle);
  48. if(status) {
  49. FURI_LOG_E(TAG, "Failed to add HID service: %d", status);
  50. }
  51. // Add Protocol mode characterstics
  52. char_uuid.Char_UUID_16 = PROTOCOL_MODE_CHAR_UUID;
  53. status = aci_gatt_add_char(hid_svc->svc_handle,
  54. UUID_TYPE_16,
  55. &char_uuid,
  56. 1,
  57. CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
  58. ATTR_PERMISSION_NONE,
  59. GATT_NOTIFY_ATTRIBUTE_WRITE,
  60. 10,
  61. CHAR_VALUE_LEN_CONSTANT,
  62. &hid_svc->protocol_mode_char_handle);
  63. if(status) {
  64. FURI_LOG_E(TAG, "Failed to add protocol mode characteristic: %d", status);
  65. }
  66. // Update Protocol mode characteristic
  67. uint8_t protocol_mode = 1;
  68. status = aci_gatt_update_char_value(hid_svc->svc_handle,
  69. hid_svc->protocol_mode_char_handle,
  70. 0,
  71. 1,
  72. &protocol_mode);
  73. if(status) {
  74. FURI_LOG_E(TAG, "Failed to update protocol mode characteristic: %d", status);
  75. }
  76. // Add Report characterstics
  77. char_uuid.Char_UUID_16 = REPORT_CHAR_UUID;
  78. status = aci_gatt_add_char(hid_svc->svc_handle,
  79. UUID_TYPE_16,
  80. &char_uuid,
  81. HID_SVC_REPORT_MAX_LEN,
  82. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  83. ATTR_PERMISSION_NONE,
  84. GATT_DONT_NOTIFY_EVENTS,
  85. 10,
  86. CHAR_VALUE_LEN_VARIABLE,
  87. &hid_svc->report_char_handle);
  88. if(status) {
  89. FURI_LOG_E(TAG, "Failed to add report characteristic: %d", status);
  90. }
  91. // Add Report descriptor
  92. uint8_t desc_val[] = {0x00, 0x01};
  93. desc_uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID;
  94. status = aci_gatt_add_char_desc(hid_svc->svc_handle,
  95. hid_svc->report_char_handle,
  96. UUID_TYPE_16,
  97. &desc_uuid,
  98. HID_SVC_REPORT_REF_LEN,
  99. HID_SVC_REPORT_REF_LEN,
  100. desc_val,
  101. ATTR_PERMISSION_NONE,
  102. ATTR_ACCESS_READ_ONLY,
  103. GATT_DONT_NOTIFY_EVENTS,
  104. MIN_ENCRY_KEY_SIZE,
  105. CHAR_VALUE_LEN_CONSTANT,
  106. &hid_svc->report_ref_desc_handle);
  107. if(status) {
  108. FURI_LOG_E(TAG, "Failed to add report reference descriptor: %d", status);
  109. }
  110. // Add Report Map characteristic
  111. char_uuid.Char_UUID_16 = REPORT_MAP_CHAR_UUID;
  112. status = aci_gatt_add_char(hid_svc->svc_handle,
  113. UUID_TYPE_16,
  114. &char_uuid,
  115. HID_SVC_REPORT_MAP_MAX_LEN,
  116. CHAR_PROP_READ,
  117. ATTR_PERMISSION_NONE,
  118. GATT_DONT_NOTIFY_EVENTS,
  119. 10,
  120. CHAR_VALUE_LEN_VARIABLE,
  121. &hid_svc->report_map_char_handle);
  122. if(status) {
  123. FURI_LOG_E(TAG, "Failed to add report map characteristic: %d", status);
  124. }
  125. // Add Boot Keyboard characteristic
  126. char_uuid.Char_UUID_16 = BOOT_KEYBOARD_INPUT_REPORT_CHAR_UUID;
  127. status = aci_gatt_add_char(hid_svc->svc_handle,
  128. UUID_TYPE_16,
  129. &char_uuid,
  130. HID_SVC_BOOT_KEYBOARD_INPUT_REPORT_MAX_LEN,
  131. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  132. ATTR_PERMISSION_NONE,
  133. GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP,
  134. 10,
  135. CHAR_VALUE_LEN_VARIABLE,
  136. &hid_svc->keyboard_boot_char_handle);
  137. if(status) {
  138. FURI_LOG_E(TAG, "Failed to add report map characteristic: %d", status);
  139. }
  140. // Add Information characteristic
  141. char_uuid.Char_UUID_16 = HID_INFORMATION_CHAR_UUID;
  142. status = aci_gatt_add_char(hid_svc->svc_handle,
  143. UUID_TYPE_16,
  144. &char_uuid,
  145. HID_SVC_INFO_LEN,
  146. CHAR_PROP_READ,
  147. ATTR_PERMISSION_NONE,
  148. GATT_DONT_NOTIFY_EVENTS,
  149. 10,
  150. CHAR_VALUE_LEN_CONSTANT,
  151. &hid_svc->info_char_handle);
  152. if(status) {
  153. FURI_LOG_E(TAG, "Failed to add information characteristic: %d", status);
  154. }
  155. // Add Control Point characteristic
  156. char_uuid.Char_UUID_16 = HID_CONTROL_POINT_CHAR_UUID;
  157. status = aci_gatt_add_char(hid_svc->svc_handle,
  158. UUID_TYPE_16,
  159. &char_uuid,
  160. HID_SVC_CONTROL_POINT_LEN,
  161. CHAR_PROP_WRITE_WITHOUT_RESP,
  162. ATTR_PERMISSION_NONE,
  163. GATT_NOTIFY_ATTRIBUTE_WRITE,
  164. 10,
  165. CHAR_VALUE_LEN_CONSTANT,
  166. &hid_svc->ctrl_point_char_handle);
  167. if(status) {
  168. FURI_LOG_E(TAG, "Failed to add control point characteristic: %d", status);
  169. }
  170. }
  171. bool hid_svc_update_report_map(uint8_t* data, uint16_t len) {
  172. furi_assert(data);
  173. furi_assert(hid_svc);
  174. tBleStatus status = aci_gatt_update_char_value(hid_svc->svc_handle,
  175. hid_svc->report_map_char_handle,
  176. 0,
  177. len,
  178. data);
  179. if(status) {
  180. FURI_LOG_E(TAG, "Failed updating report map characteristic");
  181. return false;
  182. }
  183. return true;
  184. }
  185. bool hid_svc_update_input_report(uint8_t* data, uint16_t len) {
  186. furi_assert(data);
  187. furi_assert(hid_svc);
  188. tBleStatus status = aci_gatt_update_char_value(hid_svc->svc_handle,
  189. hid_svc->report_char_handle,
  190. 0,
  191. len,
  192. data);
  193. if(status) {
  194. FURI_LOG_E(TAG, "Failed updating report characteristic");
  195. return false;
  196. }
  197. return true;
  198. }
  199. bool hid_svc_update_info(uint8_t* data, uint16_t len) {
  200. furi_assert(data);
  201. furi_assert(hid_svc);
  202. tBleStatus status = aci_gatt_update_char_value(hid_svc->svc_handle,
  203. hid_svc->info_char_handle,
  204. 0,
  205. len,
  206. data);
  207. if(status) {
  208. FURI_LOG_E(TAG, "Failed updating info characteristic");
  209. return false;
  210. }
  211. return true;
  212. }
  213. bool hid_svc_is_started() {
  214. return hid_svc != NULL;
  215. }
  216. void hid_svc_stop() {
  217. tBleStatus status;
  218. if(hid_svc) {
  219. // Delete characteristics
  220. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->report_map_char_handle);
  221. if(status) {
  222. FURI_LOG_E(TAG, "Failed to delete Report Map characteristic: %d", status);
  223. }
  224. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->report_char_handle);
  225. if(status) {
  226. FURI_LOG_E(TAG, "Failed to delete Report characteristic: %d", status);
  227. }
  228. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->protocol_mode_char_handle);
  229. if(status) {
  230. FURI_LOG_E(TAG, "Failed to delete Protocol Mode characteristic: %d", status);
  231. }
  232. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->keyboard_boot_char_handle);
  233. if(status) {
  234. FURI_LOG_E(TAG, "Failed to delete Keyboard Boot characteristic: %d", status);
  235. }
  236. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->info_char_handle);
  237. if(status) {
  238. FURI_LOG_E(TAG, "Failed to delete Information characteristic: %d", status);
  239. }
  240. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->ctrl_point_char_handle);
  241. if(status) {
  242. FURI_LOG_E(TAG, "Failed to delete Control Point characteristic: %d", status);
  243. }
  244. // Delete service
  245. status = aci_gatt_del_service(hid_svc->svc_handle);
  246. if(status) {
  247. FURI_LOG_E(TAG, "Failed to delete HID service: %d", status);
  248. }
  249. // Delete buffer size mutex
  250. free(hid_svc);
  251. hid_svc = NULL;
  252. }
  253. }