furi_hal_bt_hid.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "furi_hal_bt_hid.h"
  2. #include "dev_info_service.h"
  3. #include "battery_service.h"
  4. #include "hid_service.h"
  5. #include <furi.h>
  6. #define FURI_HAL_BT_INFO_BASE_USB_SPECIFICATION (0x0101)
  7. #define FURI_HAL_BT_INFO_COUNTRY_CODE (0x00)
  8. #define FURI_HAL_BT_HID_INFO_FLAG_REMOTE_WAKE_MSK (0x01)
  9. #define FURI_HAL_BT_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK (0x02)
  10. #define FURI_HAL_BT_HID_KB_KEYS_MAX (6)
  11. typedef struct {
  12. // uint8_t report_id;
  13. uint8_t mods;
  14. uint8_t reserved;
  15. uint8_t key[FURI_HAL_BT_HID_KB_KEYS_MAX];
  16. } FuriHalBtHidKbReport;
  17. typedef struct {
  18. uint8_t report_id;
  19. uint8_t key;
  20. } FuriHalBtHidMediaReport;
  21. // TODO make composite HID device
  22. static uint8_t furi_hal_bt_hid_report_map_data[] = {
  23. 0x05,
  24. 0x01, // Usage Page (Generic Desktop)
  25. 0x09,
  26. 0x06, // Usage (Keyboard)
  27. 0xA1,
  28. 0x01, // Collection (Application)
  29. // 0x85, 0x01, // Report ID (1)
  30. 0x05,
  31. 0x07, // Usage Page (Key Codes)
  32. 0x19,
  33. 0xe0, // Usage Minimum (224)
  34. 0x29,
  35. 0xe7, // Usage Maximum (231)
  36. 0x15,
  37. 0x00, // Logical Minimum (0)
  38. 0x25,
  39. 0x01, // Logical Maximum (1)
  40. 0x75,
  41. 0x01, // Report Size (1)
  42. 0x95,
  43. 0x08, // Report Count (8)
  44. 0x81,
  45. 0x02, // Input (Data, Variable, Absolute)
  46. 0x95,
  47. 0x01, // Report Count (1)
  48. 0x75,
  49. 0x08, // Report Size (8)
  50. 0x81,
  51. 0x01, // Input (Constant) reserved byte(1)
  52. 0x95,
  53. 0x05, // Report Count (5)
  54. 0x75,
  55. 0x01, // Report Size (1)
  56. 0x05,
  57. 0x08, // Usage Page (Page# for LEDs)
  58. 0x19,
  59. 0x01, // Usage Minimum (1)
  60. 0x29,
  61. 0x05, // Usage Maximum (5)
  62. 0x91,
  63. 0x02, // Output (Data, Variable, Absolute), Led report
  64. 0x95,
  65. 0x01, // Report Count (1)
  66. 0x75,
  67. 0x03, // Report Size (3)
  68. 0x91,
  69. 0x01, // Output (Data, Variable, Absolute), Led report padding
  70. 0x95,
  71. 0x06, // Report Count (6)
  72. 0x75,
  73. 0x08, // Report Size (8)
  74. 0x15,
  75. 0x00, // Logical Minimum (0)
  76. 0x25,
  77. 0x65, // Logical Maximum (101)
  78. 0x05,
  79. 0x07, // Usage Page (Key codes)
  80. 0x19,
  81. 0x00, // Usage Minimum (0)
  82. 0x29,
  83. 0x65, // Usage Maximum (101)
  84. 0x81,
  85. 0x00, // Input (Data, Array) Key array(6 bytes)
  86. 0x09,
  87. 0x05, // Usage (Vendor Defined)
  88. 0x15,
  89. 0x00, // Logical Minimum (0)
  90. 0x26,
  91. 0xFF,
  92. 0x00, // Logical Maximum (255)
  93. 0x75,
  94. 0x08, // Report Size (8 bit)
  95. 0x95,
  96. 0x02, // Report Count (2)
  97. 0xB1,
  98. 0x02, // Feature (Data, Variable, Absolute)
  99. 0xC0, // End Collection (Application)
  100. // 0x05, 0x0C, // Usage Page (Consumer)
  101. // 0x09, 0x01, // Usage (Consumer Control)
  102. // 0xA1, 0x01, // Collection (Application)
  103. // 0x85, 0x02, // Report ID (2)
  104. // 0x05, 0x0C, // Usage Page (Consumer)
  105. // 0x15, 0x00, // Logical Minimum (0)
  106. // 0x25, 0x01, // Logical Maximum (1)
  107. // 0x75, 0x01, // Report Size (1)
  108. // 0x95, 0x07, // Report Count (7)
  109. // 0x09, 0xB5, // Usage (Scan Next Track)
  110. // 0x09, 0xB6, // Usage (Scan Previous Track)
  111. // 0x09, 0xB7, // Usage (Stop)
  112. // 0x09, 0xB8, // Usage (Eject)
  113. // 0x09, 0xCD, // Usage (Play/Pause)
  114. // 0x09, 0xE2, // Usage (Mute)
  115. // 0x09, 0xE9, // Usage (Volume Increment)
  116. // 0x09, 0xEA, // Usage (Volume Decrement)
  117. // 0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
  118. // 0xC0, // End Collection
  119. };
  120. FuriHalBtHidKbReport* kb_report = NULL;
  121. FuriHalBtHidMediaReport* media_report = NULL;
  122. void furi_hal_bt_hid_start() {
  123. // Start device info
  124. if(!dev_info_svc_is_started()) {
  125. dev_info_svc_start();
  126. }
  127. // Start battery service
  128. if(!battery_svc_is_started()) {
  129. battery_svc_start();
  130. }
  131. // Start HID service
  132. if(!hid_svc_is_started()) {
  133. hid_svc_start();
  134. }
  135. // Configure HID Keyboard
  136. kb_report = malloc(sizeof(FuriHalBtHidKbReport));
  137. media_report = malloc(sizeof(FuriHalBtHidMediaReport));
  138. // Configure Report Map characteristic
  139. hid_svc_update_report_map(
  140. furi_hal_bt_hid_report_map_data, sizeof(furi_hal_bt_hid_report_map_data));
  141. // Configure HID Information characteristic
  142. uint8_t hid_info_val[4] = {
  143. FURI_HAL_BT_INFO_BASE_USB_SPECIFICATION & 0x00ff,
  144. (FURI_HAL_BT_INFO_BASE_USB_SPECIFICATION & 0xff00) >> 8,
  145. FURI_HAL_BT_INFO_COUNTRY_CODE,
  146. FURI_HAL_BT_HID_INFO_FLAG_REMOTE_WAKE_MSK |
  147. FURI_HAL_BT_HID_INFO_FLAG_NORMALLY_CONNECTABLE_MSK,
  148. };
  149. hid_svc_update_info(hid_info_val, sizeof(hid_info_val));
  150. }
  151. void furi_hal_bt_hid_stop() {
  152. furi_assert(kb_report);
  153. // Stop all services
  154. if(dev_info_svc_is_started()) {
  155. dev_info_svc_stop();
  156. }
  157. if(battery_svc_is_started()) {
  158. battery_svc_stop();
  159. }
  160. if(hid_svc_is_started()) {
  161. hid_svc_stop();
  162. }
  163. free(kb_report);
  164. free(media_report);
  165. media_report = NULL;
  166. kb_report = NULL;
  167. }
  168. bool furi_hal_bt_hid_kb_press(uint16_t button) {
  169. furi_assert(kb_report);
  170. // kb_report->report_id = 0x01;
  171. for(uint8_t i = 0; i < FURI_HAL_BT_HID_KB_KEYS_MAX; i++) {
  172. if(kb_report->key[i] == 0) {
  173. kb_report->key[i] = button & 0xFF;
  174. break;
  175. }
  176. }
  177. kb_report->mods |= (button >> 8);
  178. return hid_svc_update_input_report((uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));
  179. }
  180. bool furi_hal_bt_hid_kb_release(uint16_t button) {
  181. furi_assert(kb_report);
  182. // kb_report->report_id = 0x01;
  183. for(uint8_t i = 0; i < FURI_HAL_BT_HID_KB_KEYS_MAX; i++) {
  184. if(kb_report->key[i] == (button & 0xFF)) {
  185. kb_report->key[i] = 0;
  186. break;
  187. }
  188. }
  189. kb_report->mods &= ~(button >> 8);
  190. return hid_svc_update_input_report((uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));
  191. }
  192. bool furi_hal_bt_hid_kb_release_all() {
  193. furi_assert(kb_report);
  194. // kb_report->report_id = 0x01;
  195. memset(kb_report, 0, sizeof(FuriHalBtHidKbReport));
  196. return hid_svc_update_input_report((uint8_t*)kb_report, sizeof(FuriHalBtHidKbReport));
  197. }
  198. bool furi_hal_bt_hid_media_press(uint8_t button) {
  199. furi_assert(media_report);
  200. media_report->report_id = 0x02;
  201. media_report->key |= (0x01 << button);
  202. return hid_svc_update_input_report((uint8_t*)media_report, sizeof(FuriHalBtHidMediaReport));
  203. }
  204. bool furi_hal_bt_hid_media_release(uint8_t button) {
  205. furi_assert(media_report);
  206. media_report->report_id = 0x02;
  207. media_report->key &= ~(0x01 << button);
  208. return hid_svc_update_input_report((uint8_t*)media_report, sizeof(FuriHalBtHidMediaReport));
  209. }
  210. bool furi_hal_bt_hid_media_release_all() {
  211. furi_assert(media_report);
  212. media_report->report_id = 0x02;
  213. media_report->key = 0x00;
  214. return hid_svc_update_input_report((uint8_t*)media_report, sizeof(FuriHalBtHidMediaReport));
  215. }