furi-hal-bt-hid.c 6.8 KB

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