ble_hid.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. #include "ble_hid.h"
  2. #include <furi_hal_usb_hid.h>
  3. #include <services/dev_info_service.h>
  4. #include <services/battery_service.h>
  5. #include "ble_hid_svc.h"
  6. #include <furi.h>
  7. #include <usb_hid.h>
  8. #include <ble/ble.h>
  9. #define HID_INFO_BASE_USB_SPECIFICATION (0x0101)
  10. #define HID_INFO_COUNTRY_CODE (0x00)
  11. #define BLE_PROFILE_HID_INFO_FLAG_REMOTE_WAKE_MSK (0x01)
  12. #define BLE_PROFILE_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK (0x02)
  13. #define BLE_PROFILE_HID_KB_MAX_KEYS (6)
  14. #define BLE_PROFILE_CONSUMER_MAX_KEYS (1)
  15. // Report ids cant be 0
  16. enum HidReportId {
  17. ReportIdKeyboard = 1,
  18. ReportIdMouse = 2,
  19. ReportIdConsumer = 3,
  20. };
  21. // Report numbers corresponded to the report id with an offset of 1
  22. enum HidInputNumber {
  23. ReportNumberKeyboard = 0,
  24. ReportNumberMouse = 1,
  25. ReportNumberConsumer = 2,
  26. };
  27. typedef struct {
  28. uint8_t mods;
  29. uint8_t reserved;
  30. uint8_t key[BLE_PROFILE_HID_KB_MAX_KEYS];
  31. } FURI_PACKED FuriHalBtHidKbReport;
  32. typedef struct {
  33. uint8_t btn;
  34. int8_t x;
  35. int8_t y;
  36. int8_t wheel;
  37. } FURI_PACKED FuriHalBtHidMouseReport;
  38. typedef struct {
  39. uint16_t key[BLE_PROFILE_CONSUMER_MAX_KEYS];
  40. } FURI_PACKED FuriHalBtHidConsumerReport;
  41. // keyboard+mouse+consumer hid report
  42. static const uint8_t ble_profile_hid_report_map_data[] = {
  43. // Keyboard Report
  44. HID_USAGE_PAGE(HID_PAGE_DESKTOP),
  45. HID_USAGE(HID_DESKTOP_KEYBOARD),
  46. HID_COLLECTION(HID_APPLICATION_COLLECTION),
  47. HID_REPORT_ID(ReportIdKeyboard),
  48. HID_USAGE_PAGE(HID_DESKTOP_KEYPAD),
  49. HID_USAGE_MINIMUM(HID_KEYBOARD_L_CTRL),
  50. HID_USAGE_MAXIMUM(HID_KEYBOARD_R_GUI),
  51. HID_LOGICAL_MINIMUM(0),
  52. HID_LOGICAL_MAXIMUM(1),
  53. HID_REPORT_SIZE(1),
  54. HID_REPORT_COUNT(8),
  55. HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  56. HID_REPORT_COUNT(1),
  57. HID_REPORT_SIZE(8),
  58. HID_INPUT(HID_IOF_CONSTANT | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  59. HID_USAGE_PAGE(HID_PAGE_LED),
  60. HID_REPORT_COUNT(8),
  61. HID_REPORT_SIZE(1),
  62. HID_USAGE_MINIMUM(1),
  63. HID_USAGE_MAXIMUM(8),
  64. HID_OUTPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  65. HID_REPORT_COUNT(BLE_PROFILE_HID_KB_MAX_KEYS),
  66. HID_REPORT_SIZE(8),
  67. HID_LOGICAL_MINIMUM(0),
  68. HID_LOGICAL_MAXIMUM(101),
  69. HID_USAGE_PAGE(HID_DESKTOP_KEYPAD),
  70. HID_USAGE_MINIMUM(0),
  71. HID_USAGE_MAXIMUM(101),
  72. HID_INPUT(HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  73. HID_END_COLLECTION,
  74. // Mouse Report
  75. HID_USAGE_PAGE(HID_PAGE_DESKTOP),
  76. HID_USAGE(HID_DESKTOP_MOUSE),
  77. HID_COLLECTION(HID_APPLICATION_COLLECTION),
  78. HID_USAGE(HID_DESKTOP_POINTER),
  79. HID_COLLECTION(HID_PHYSICAL_COLLECTION),
  80. HID_REPORT_ID(ReportIdMouse),
  81. HID_USAGE_PAGE(HID_PAGE_BUTTON),
  82. HID_USAGE_MINIMUM(1),
  83. HID_USAGE_MAXIMUM(3),
  84. HID_LOGICAL_MINIMUM(0),
  85. HID_LOGICAL_MAXIMUM(1),
  86. HID_REPORT_COUNT(3),
  87. HID_REPORT_SIZE(1),
  88. HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  89. HID_REPORT_SIZE(1),
  90. HID_REPORT_COUNT(5),
  91. HID_INPUT(HID_IOF_CONSTANT | HID_IOF_VARIABLE | HID_IOF_ABSOLUTE),
  92. HID_USAGE_PAGE(HID_PAGE_DESKTOP),
  93. HID_USAGE(HID_DESKTOP_X),
  94. HID_USAGE(HID_DESKTOP_Y),
  95. HID_USAGE(HID_DESKTOP_WHEEL),
  96. HID_LOGICAL_MINIMUM(-127),
  97. HID_LOGICAL_MAXIMUM(127),
  98. HID_REPORT_SIZE(8),
  99. HID_REPORT_COUNT(3),
  100. HID_INPUT(HID_IOF_DATA | HID_IOF_VARIABLE | HID_IOF_RELATIVE),
  101. HID_END_COLLECTION,
  102. HID_END_COLLECTION,
  103. // Consumer Report
  104. HID_USAGE_PAGE(HID_PAGE_CONSUMER),
  105. HID_USAGE(HID_CONSUMER_CONTROL),
  106. HID_COLLECTION(HID_APPLICATION_COLLECTION),
  107. HID_REPORT_ID(ReportIdConsumer),
  108. HID_LOGICAL_MINIMUM(0),
  109. HID_RI_LOGICAL_MAXIMUM(16, 0x3FF),
  110. HID_USAGE_MINIMUM(0),
  111. HID_RI_USAGE_MAXIMUM(16, 0x3FF),
  112. HID_REPORT_COUNT(BLE_PROFILE_CONSUMER_MAX_KEYS),
  113. HID_REPORT_SIZE(16),
  114. HID_INPUT(HID_IOF_DATA | HID_IOF_ARRAY | HID_IOF_ABSOLUTE),
  115. HID_END_COLLECTION,
  116. };
  117. typedef struct {
  118. FuriHalBleProfileBase base;
  119. FuriHalBtHidKbReport* kb_report;
  120. FuriHalBtHidMouseReport* mouse_report;
  121. FuriHalBtHidConsumerReport* consumer_report;
  122. BleServiceBattery* battery_svc;
  123. BleServiceDevInfo* dev_info_svc;
  124. BleServiceHid* hid_svc;
  125. } BleProfileHid;
  126. _Static_assert(offsetof(BleProfileHid, base) == 0, "Wrong layout");
  127. static FuriHalBleProfileBase* ble_profile_hid_start(FuriHalBleProfileParams profile_params) {
  128. UNUSED(profile_params);
  129. BleProfileHid* profile = malloc(sizeof(BleProfileHid));
  130. profile->base.config = ble_profile_hid;
  131. profile->battery_svc = ble_svc_battery_start(true);
  132. profile->dev_info_svc = ble_svc_dev_info_start();
  133. profile->hid_svc = ble_svc_hid_start();
  134. // Configure HID Keyboard
  135. profile->kb_report = malloc(sizeof(FuriHalBtHidKbReport));
  136. profile->mouse_report = malloc(sizeof(FuriHalBtHidMouseReport));
  137. profile->consumer_report = malloc(sizeof(FuriHalBtHidConsumerReport));
  138. // Configure Report Map characteristic
  139. ble_svc_hid_update_report_map(
  140. profile->hid_svc,
  141. ble_profile_hid_report_map_data,
  142. sizeof(ble_profile_hid_report_map_data));
  143. // Configure HID Information characteristic
  144. uint8_t hid_info_val[4] = {
  145. HID_INFO_BASE_USB_SPECIFICATION & 0x00ff,
  146. (HID_INFO_BASE_USB_SPECIFICATION & 0xff00) >> 8,
  147. HID_INFO_COUNTRY_CODE,
  148. BLE_PROFILE_HID_INFO_FLAG_REMOTE_WAKE_MSK |
  149. BLE_PROFILE_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK,
  150. };
  151. ble_svc_hid_update_info(profile->hid_svc, hid_info_val);
  152. return &profile->base;
  153. }
  154. static void ble_profile_hid_stop(FuriHalBleProfileBase* profile) {
  155. furi_check(profile);
  156. furi_check(profile->config == ble_profile_hid);
  157. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  158. ble_svc_battery_stop(hid_profile->battery_svc);
  159. ble_svc_dev_info_stop(hid_profile->dev_info_svc);
  160. ble_svc_hid_stop(hid_profile->hid_svc);
  161. free(hid_profile->kb_report);
  162. free(hid_profile->mouse_report);
  163. free(hid_profile->consumer_report);
  164. }
  165. bool ble_profile_hid_kb_press(FuriHalBleProfileBase* profile, uint16_t button) {
  166. furi_check(profile);
  167. furi_check(profile->config == ble_profile_hid);
  168. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  169. FuriHalBtHidKbReport* kb_report = hid_profile->kb_report;
  170. for(uint8_t i = 0; i < BLE_PROFILE_HID_KB_MAX_KEYS; i++) {
  171. if(kb_report->key[i] == 0) {
  172. kb_report->key[i] = button & 0xFF;
  173. break;
  174. }
  175. }
  176. kb_report->mods |= (button >> 8);
  177. return ble_svc_hid_update_input_report(
  178. hid_profile->hid_svc,
  179. ReportNumberKeyboard,
  180. (uint8_t*)kb_report,
  181. sizeof(FuriHalBtHidKbReport));
  182. }
  183. bool ble_profile_hid_kb_release(FuriHalBleProfileBase* profile, uint16_t button) {
  184. furi_check(profile);
  185. furi_check(profile->config == ble_profile_hid);
  186. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  187. FuriHalBtHidKbReport* kb_report = hid_profile->kb_report;
  188. for(uint8_t i = 0; i < BLE_PROFILE_HID_KB_MAX_KEYS; i++) {
  189. if(kb_report->key[i] == (button & 0xFF)) {
  190. kb_report->key[i] = 0;
  191. break;
  192. }
  193. }
  194. kb_report->mods &= ~(button >> 8);
  195. return ble_svc_hid_update_input_report(
  196. hid_profile->hid_svc,
  197. ReportNumberKeyboard,
  198. (uint8_t*)kb_report,
  199. sizeof(FuriHalBtHidKbReport));
  200. }
  201. bool ble_profile_hid_kb_release_all(FuriHalBleProfileBase* profile) {
  202. furi_check(profile);
  203. furi_check(profile->config == ble_profile_hid);
  204. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  205. FuriHalBtHidKbReport* kb_report = hid_profile->kb_report;
  206. for(uint8_t i = 0; i < BLE_PROFILE_HID_KB_MAX_KEYS; i++) {
  207. kb_report->key[i] = 0;
  208. }
  209. kb_report->mods = 0;
  210. return ble_svc_hid_update_input_report(
  211. hid_profile->hid_svc,
  212. ReportNumberKeyboard,
  213. (uint8_t*)kb_report,
  214. sizeof(FuriHalBtHidKbReport));
  215. }
  216. bool ble_profile_hid_consumer_key_press(FuriHalBleProfileBase* profile, uint16_t button) {
  217. furi_check(profile);
  218. furi_check(profile->config == ble_profile_hid);
  219. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  220. FuriHalBtHidConsumerReport* consumer_report = hid_profile->consumer_report;
  221. for(uint8_t i = 0; i < BLE_PROFILE_CONSUMER_MAX_KEYS; i++) { //-V1008
  222. if(consumer_report->key[i] == 0) {
  223. consumer_report->key[i] = button;
  224. break;
  225. }
  226. }
  227. return ble_svc_hid_update_input_report(
  228. hid_profile->hid_svc,
  229. ReportNumberConsumer,
  230. (uint8_t*)consumer_report,
  231. sizeof(FuriHalBtHidConsumerReport));
  232. }
  233. bool ble_profile_hid_consumer_key_release(FuriHalBleProfileBase* profile, uint16_t button) {
  234. furi_check(profile);
  235. furi_check(profile->config == ble_profile_hid);
  236. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  237. FuriHalBtHidConsumerReport* consumer_report = hid_profile->consumer_report;
  238. for(uint8_t i = 0; i < BLE_PROFILE_CONSUMER_MAX_KEYS; i++) { //-V1008
  239. if(consumer_report->key[i] == button) {
  240. consumer_report->key[i] = 0;
  241. break;
  242. }
  243. }
  244. return ble_svc_hid_update_input_report(
  245. hid_profile->hid_svc,
  246. ReportNumberConsumer,
  247. (uint8_t*)consumer_report,
  248. sizeof(FuriHalBtHidConsumerReport));
  249. }
  250. bool ble_profile_hid_consumer_key_release_all(FuriHalBleProfileBase* profile) {
  251. furi_check(profile);
  252. furi_check(profile->config == ble_profile_hid);
  253. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  254. FuriHalBtHidConsumerReport* consumer_report = hid_profile->consumer_report;
  255. for(uint8_t i = 0; i < BLE_PROFILE_CONSUMER_MAX_KEYS; i++) { //-V1008
  256. consumer_report->key[i] = 0;
  257. }
  258. return ble_svc_hid_update_input_report(
  259. hid_profile->hid_svc,
  260. ReportNumberConsumer,
  261. (uint8_t*)consumer_report,
  262. sizeof(FuriHalBtHidConsumerReport));
  263. }
  264. bool ble_profile_hid_mouse_move(FuriHalBleProfileBase* profile, int8_t dx, int8_t dy) {
  265. furi_check(profile);
  266. furi_check(profile->config == ble_profile_hid);
  267. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  268. FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
  269. mouse_report->x = dx;
  270. mouse_report->y = dy;
  271. bool state = ble_svc_hid_update_input_report(
  272. hid_profile->hid_svc,
  273. ReportNumberMouse,
  274. (uint8_t*)mouse_report,
  275. sizeof(FuriHalBtHidMouseReport));
  276. mouse_report->x = 0;
  277. mouse_report->y = 0;
  278. return state;
  279. }
  280. bool ble_profile_hid_mouse_press(FuriHalBleProfileBase* profile, uint8_t button) {
  281. furi_check(profile);
  282. furi_check(profile->config == ble_profile_hid);
  283. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  284. FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
  285. mouse_report->btn |= button;
  286. return ble_svc_hid_update_input_report(
  287. hid_profile->hid_svc,
  288. ReportNumberMouse,
  289. (uint8_t*)mouse_report,
  290. sizeof(FuriHalBtHidMouseReport));
  291. }
  292. bool ble_profile_hid_mouse_release(FuriHalBleProfileBase* profile, uint8_t button) {
  293. furi_check(profile);
  294. furi_check(profile->config == ble_profile_hid);
  295. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  296. FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
  297. mouse_report->btn &= ~button;
  298. return ble_svc_hid_update_input_report(
  299. hid_profile->hid_svc,
  300. ReportNumberMouse,
  301. (uint8_t*)mouse_report,
  302. sizeof(FuriHalBtHidMouseReport));
  303. }
  304. bool ble_profile_hid_mouse_release_all(FuriHalBleProfileBase* profile) {
  305. furi_check(profile);
  306. furi_check(profile->config == ble_profile_hid);
  307. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  308. FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
  309. mouse_report->btn = 0;
  310. return ble_svc_hid_update_input_report(
  311. hid_profile->hid_svc,
  312. ReportNumberMouse,
  313. (uint8_t*)mouse_report,
  314. sizeof(FuriHalBtHidMouseReport));
  315. }
  316. bool ble_profile_hid_mouse_scroll(FuriHalBleProfileBase* profile, int8_t delta) {
  317. furi_check(profile);
  318. furi_check(profile->config == ble_profile_hid);
  319. BleProfileHid* hid_profile = (BleProfileHid*)profile;
  320. FuriHalBtHidMouseReport* mouse_report = hid_profile->mouse_report;
  321. mouse_report->wheel = delta;
  322. bool state = ble_svc_hid_update_input_report(
  323. hid_profile->hid_svc,
  324. ReportNumberMouse,
  325. (uint8_t*)mouse_report,
  326. sizeof(FuriHalBtHidMouseReport));
  327. mouse_report->wheel = 0;
  328. return state;
  329. }
  330. static GapConfig template_config = {
  331. .adv_service_uuid = HUMAN_INTERFACE_DEVICE_SERVICE_UUID,
  332. .appearance_char = GAP_APPEARANCE_KEYBOARD,
  333. .bonding_mode = true,
  334. .pairing_method = GapPairingPinCodeVerifyYesNo,
  335. .conn_param =
  336. {
  337. .conn_int_min = 0x18, // 30 ms
  338. .conn_int_max = 0x24, // 45 ms
  339. .slave_latency = 0,
  340. .supervisor_timeout = 0,
  341. },
  342. };
  343. static void ble_profile_hid_get_config(GapConfig* config, FuriHalBleProfileParams profile_params) {
  344. BleProfileHidParams* hid_profile_params = profile_params;
  345. furi_check(config);
  346. memcpy(config, &template_config, sizeof(GapConfig));
  347. // Set mac address
  348. memcpy(config->mac_address, hid_profile_params->mac, sizeof(config->mac_address));
  349. // Set advertise name
  350. config->adv_name[0] = furi_hal_version_get_ble_local_device_name_ptr()[0];
  351. strlcpy(config->adv_name + 1, hid_profile_params->name, sizeof(config->adv_name) - 1);
  352. // Set bonding mode
  353. config->bonding_mode = hid_profile_params->bonding;
  354. // Set pairing method
  355. config->pairing_method = hid_profile_params->pairing;
  356. }
  357. static const FuriHalBleProfileTemplate profile_callbacks = {
  358. .start = ble_profile_hid_start,
  359. .stop = ble_profile_hid_stop,
  360. .get_gap_config = ble_profile_hid_get_config,
  361. };
  362. const FuriHalBleProfileTemplate* ble_profile_hid = &profile_callbacks;