hid_service.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "hid_service.h"
  2. #include "app_common.h"
  3. #include <ble/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[HID_SVC_REPORT_COUNT];
  10. uint16_t report_ref_desc_handle[HID_SVC_REPORT_COUNT];
  11. uint16_t report_map_char_handle;
  12. uint16_t info_char_handle;
  13. uint16_t ctrl_point_char_handle;
  14. } HIDSvc;
  15. static HIDSvc* hid_svc = NULL;
  16. static SVCCTL_EvtAckStatus_t hid_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. // Process modification events
  24. ret = SVCCTL_EvtAckFlowEnable;
  25. } else if(blecore_evt->ecode == ACI_GATT_SERVER_CONFIRMATION_VSEVT_CODE) {
  26. // Process notification confirmation
  27. ret = SVCCTL_EvtAckFlowEnable;
  28. }
  29. }
  30. return ret;
  31. }
  32. void hid_svc_start() {
  33. tBleStatus status;
  34. hid_svc = malloc(sizeof(HIDSvc));
  35. Service_UUID_t svc_uuid = {};
  36. Char_Desc_Uuid_t desc_uuid = {};
  37. Char_UUID_t char_uuid = {};
  38. // Register event handler
  39. SVCCTL_RegisterSvcHandler(hid_svc_event_handler);
  40. // Add service
  41. svc_uuid.Service_UUID_16 = HUMAN_INTERFACE_DEVICE_SERVICE_UUID;
  42. /**
  43. * Add Human Interface Device Service
  44. */
  45. status = aci_gatt_add_service(
  46. UUID_TYPE_16,
  47. &svc_uuid,
  48. PRIMARY_SERVICE,
  49. 2 + /* protocol mode */
  50. (4 * HID_SVC_INPUT_REPORT_COUNT) + (3 * HID_SVC_OUTPUT_REPORT_COUNT) +
  51. (3 * HID_SVC_FEATURE_REPORT_COUNT) + 1 + 2 + 2 +
  52. 2, /* Service + Report Map + HID Information + HID Control Point */
  53. &hid_svc->svc_handle);
  54. if(status) {
  55. FURI_LOG_E(TAG, "Failed to add HID service: %d", status);
  56. }
  57. // Add Protocol mode characteristics
  58. char_uuid.Char_UUID_16 = PROTOCOL_MODE_CHAR_UUID;
  59. status = aci_gatt_add_char(
  60. hid_svc->svc_handle,
  61. UUID_TYPE_16,
  62. &char_uuid,
  63. 1,
  64. CHAR_PROP_READ | CHAR_PROP_WRITE_WITHOUT_RESP,
  65. ATTR_PERMISSION_NONE,
  66. GATT_NOTIFY_ATTRIBUTE_WRITE,
  67. 10,
  68. CHAR_VALUE_LEN_CONSTANT,
  69. &hid_svc->protocol_mode_char_handle);
  70. if(status) {
  71. FURI_LOG_E(TAG, "Failed to add protocol mode characteristic: %d", status);
  72. }
  73. // Update Protocol mode characteristic
  74. uint8_t protocol_mode = 1;
  75. status = aci_gatt_update_char_value(
  76. hid_svc->svc_handle, hid_svc->protocol_mode_char_handle, 0, 1, &protocol_mode);
  77. if(status) {
  78. FURI_LOG_E(TAG, "Failed to update protocol mode characteristic: %d", status);
  79. }
  80. #if(HID_SVC_REPORT_COUNT != 0)
  81. for(uint8_t i = 0; i < HID_SVC_REPORT_COUNT; i++) {
  82. if(i < HID_SVC_INPUT_REPORT_COUNT) { //-V547
  83. uint8_t buf[2] = {i + 1, 1}; // 1 input
  84. char_uuid.Char_UUID_16 = REPORT_CHAR_UUID;
  85. status = aci_gatt_add_char(
  86. hid_svc->svc_handle,
  87. UUID_TYPE_16,
  88. &char_uuid,
  89. HID_SVC_REPORT_MAX_LEN,
  90. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  91. ATTR_PERMISSION_NONE,
  92. GATT_DONT_NOTIFY_EVENTS,
  93. 10,
  94. CHAR_VALUE_LEN_VARIABLE,
  95. &(hid_svc->report_char_handle[i]));
  96. if(status) {
  97. FURI_LOG_E(TAG, "Failed to add report characteristic: %d", status);
  98. }
  99. desc_uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID;
  100. status = aci_gatt_add_char_desc(
  101. hid_svc->svc_handle,
  102. hid_svc->report_char_handle[i],
  103. UUID_TYPE_16,
  104. &desc_uuid,
  105. HID_SVC_REPORT_REF_LEN,
  106. HID_SVC_REPORT_REF_LEN,
  107. buf,
  108. ATTR_PERMISSION_NONE,
  109. ATTR_ACCESS_READ_WRITE,
  110. GATT_DONT_NOTIFY_EVENTS,
  111. MIN_ENCRY_KEY_SIZE,
  112. CHAR_VALUE_LEN_CONSTANT,
  113. &(hid_svc->report_ref_desc_handle[i]));
  114. if(status) {
  115. FURI_LOG_E(TAG, "Failed to add report reference descriptor: %d", status);
  116. }
  117. } else if((i - HID_SVC_INPUT_REPORT_COUNT) < HID_SVC_OUTPUT_REPORT_COUNT) {
  118. uint8_t buf[2] = {i + 1, 2}; // 2 output
  119. char_uuid.Char_UUID_16 = REPORT_CHAR_UUID;
  120. status = aci_gatt_add_char(
  121. hid_svc->svc_handle,
  122. UUID_TYPE_16,
  123. &char_uuid,
  124. HID_SVC_REPORT_MAX_LEN,
  125. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  126. ATTR_PERMISSION_NONE,
  127. GATT_DONT_NOTIFY_EVENTS,
  128. 10,
  129. CHAR_VALUE_LEN_VARIABLE,
  130. &(hid_svc->report_char_handle[i]));
  131. if(status) {
  132. FURI_LOG_E(TAG, "Failed to add report characteristic: %d", status);
  133. }
  134. desc_uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID;
  135. status = aci_gatt_add_char_desc(
  136. hid_svc->svc_handle,
  137. hid_svc->report_char_handle[i],
  138. UUID_TYPE_16,
  139. &desc_uuid,
  140. HID_SVC_REPORT_REF_LEN,
  141. HID_SVC_REPORT_REF_LEN,
  142. buf,
  143. ATTR_PERMISSION_NONE,
  144. ATTR_ACCESS_READ_WRITE,
  145. GATT_DONT_NOTIFY_EVENTS,
  146. MIN_ENCRY_KEY_SIZE,
  147. CHAR_VALUE_LEN_CONSTANT,
  148. &(hid_svc->report_ref_desc_handle[i]));
  149. if(status) {
  150. FURI_LOG_E(TAG, "Failed to add report reference descriptor: %d", status);
  151. }
  152. } else {
  153. uint8_t buf[2] = {i + 1, 3}; // 3 feature
  154. char_uuid.Char_UUID_16 = REPORT_CHAR_UUID;
  155. status = aci_gatt_add_char(
  156. hid_svc->svc_handle,
  157. UUID_TYPE_16,
  158. &char_uuid,
  159. HID_SVC_REPORT_MAX_LEN,
  160. CHAR_PROP_READ | CHAR_PROP_NOTIFY,
  161. ATTR_PERMISSION_NONE,
  162. GATT_DONT_NOTIFY_EVENTS,
  163. 10,
  164. CHAR_VALUE_LEN_VARIABLE,
  165. &(hid_svc->report_char_handle[i]));
  166. if(status) {
  167. FURI_LOG_E(TAG, "Failed to add report characteristic: %d", status);
  168. }
  169. desc_uuid.Char_UUID_16 = REPORT_REFERENCE_DESCRIPTOR_UUID;
  170. status = aci_gatt_add_char_desc(
  171. hid_svc->svc_handle,
  172. hid_svc->report_char_handle[i],
  173. UUID_TYPE_16,
  174. &desc_uuid,
  175. HID_SVC_REPORT_REF_LEN,
  176. HID_SVC_REPORT_REF_LEN,
  177. buf,
  178. ATTR_PERMISSION_NONE,
  179. ATTR_ACCESS_READ_WRITE,
  180. GATT_DONT_NOTIFY_EVENTS,
  181. MIN_ENCRY_KEY_SIZE,
  182. CHAR_VALUE_LEN_CONSTANT,
  183. &(hid_svc->report_ref_desc_handle[i]));
  184. if(status) {
  185. FURI_LOG_E(TAG, "Failed to add report reference descriptor: %d", status);
  186. }
  187. }
  188. }
  189. #endif
  190. // Add Report Map characteristic
  191. char_uuid.Char_UUID_16 = REPORT_MAP_CHAR_UUID;
  192. status = aci_gatt_add_char(
  193. hid_svc->svc_handle,
  194. UUID_TYPE_16,
  195. &char_uuid,
  196. HID_SVC_REPORT_MAP_MAX_LEN,
  197. CHAR_PROP_READ,
  198. ATTR_PERMISSION_NONE,
  199. GATT_DONT_NOTIFY_EVENTS,
  200. 10,
  201. CHAR_VALUE_LEN_VARIABLE,
  202. &hid_svc->report_map_char_handle);
  203. if(status) {
  204. FURI_LOG_E(TAG, "Failed to add report map characteristic: %d", status);
  205. }
  206. // Add Information characteristic
  207. char_uuid.Char_UUID_16 = HID_INFORMATION_CHAR_UUID;
  208. status = aci_gatt_add_char(
  209. hid_svc->svc_handle,
  210. UUID_TYPE_16,
  211. &char_uuid,
  212. HID_SVC_INFO_LEN,
  213. CHAR_PROP_READ,
  214. ATTR_PERMISSION_NONE,
  215. GATT_DONT_NOTIFY_EVENTS,
  216. 10,
  217. CHAR_VALUE_LEN_CONSTANT,
  218. &hid_svc->info_char_handle);
  219. if(status) {
  220. FURI_LOG_E(TAG, "Failed to add information characteristic: %d", status);
  221. }
  222. // Add Control Point characteristic
  223. char_uuid.Char_UUID_16 = HID_CONTROL_POINT_CHAR_UUID;
  224. status = aci_gatt_add_char(
  225. hid_svc->svc_handle,
  226. UUID_TYPE_16,
  227. &char_uuid,
  228. HID_SVC_CONTROL_POINT_LEN,
  229. CHAR_PROP_WRITE_WITHOUT_RESP,
  230. ATTR_PERMISSION_NONE,
  231. GATT_NOTIFY_ATTRIBUTE_WRITE,
  232. 10,
  233. CHAR_VALUE_LEN_CONSTANT,
  234. &hid_svc->ctrl_point_char_handle);
  235. if(status) {
  236. FURI_LOG_E(TAG, "Failed to add control point characteristic: %d", status);
  237. }
  238. }
  239. bool hid_svc_update_report_map(const uint8_t* data, uint16_t len) {
  240. furi_assert(data);
  241. furi_assert(hid_svc);
  242. tBleStatus status = aci_gatt_update_char_value(
  243. hid_svc->svc_handle, hid_svc->report_map_char_handle, 0, len, data);
  244. if(status) {
  245. FURI_LOG_E(TAG, "Failed updating report map characteristic: %d", status);
  246. return false;
  247. }
  248. return true;
  249. }
  250. bool hid_svc_update_input_report(uint8_t input_report_num, uint8_t* data, uint16_t len) {
  251. furi_assert(data);
  252. furi_assert(hid_svc);
  253. tBleStatus status = aci_gatt_update_char_value(
  254. hid_svc->svc_handle, hid_svc->report_char_handle[input_report_num], 0, len, data);
  255. if(status) {
  256. FURI_LOG_E(TAG, "Failed updating report characteristic: %d", status);
  257. return false;
  258. }
  259. return true;
  260. }
  261. bool hid_svc_update_info(uint8_t* data, uint16_t len) {
  262. furi_assert(data);
  263. furi_assert(hid_svc);
  264. tBleStatus status =
  265. aci_gatt_update_char_value(hid_svc->svc_handle, hid_svc->info_char_handle, 0, len, data);
  266. if(status) {
  267. FURI_LOG_E(TAG, "Failed updating info characteristic: %d", status);
  268. return false;
  269. }
  270. return true;
  271. }
  272. bool hid_svc_is_started() {
  273. return hid_svc != NULL;
  274. }
  275. void hid_svc_stop() {
  276. tBleStatus status;
  277. if(hid_svc) {
  278. // Delete characteristics
  279. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->report_map_char_handle);
  280. if(status) {
  281. FURI_LOG_E(TAG, "Failed to delete Report Map characteristic: %d", status);
  282. }
  283. #if(HID_SVC_INPUT_REPORT_COUNT != 0)
  284. for(uint8_t i = 0; i < HID_SVC_REPORT_COUNT; i++) {
  285. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->report_char_handle[i]);
  286. if(status) {
  287. FURI_LOG_E(TAG, "Failed to delete Report characteristic: %d", status);
  288. }
  289. }
  290. #endif
  291. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->protocol_mode_char_handle);
  292. if(status) {
  293. FURI_LOG_E(TAG, "Failed to delete Protocol Mode characteristic: %d", status);
  294. }
  295. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->info_char_handle);
  296. if(status) {
  297. FURI_LOG_E(TAG, "Failed to delete Information characteristic: %d", status);
  298. }
  299. status = aci_gatt_del_char(hid_svc->svc_handle, hid_svc->ctrl_point_char_handle);
  300. if(status) {
  301. FURI_LOG_E(TAG, "Failed to delete Control Point characteristic: %d", status);
  302. }
  303. // Delete service
  304. status = aci_gatt_del_service(hid_svc->svc_handle);
  305. if(status) {
  306. FURI_LOG_E(TAG, "Failed to delete HID service: %d", status);
  307. }
  308. // Delete buffer size mutex
  309. free(hid_svc);
  310. hid_svc = NULL;
  311. }
  312. }