bt_mouse.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "bt_mouse.h"
  2. #include "../tracking/main_loop.h"
  3. #include <furi.h>
  4. #include <furi_hal_bt.h>
  5. #include <furi_hal_usb_hid.h>
  6. #include <profiles/serial_profile.h>
  7. #include <extra_profiles/hid_profile.h>
  8. #include <bt/bt_service/bt.h>
  9. #include <gui/elements.h>
  10. #include <notification/notification.h>
  11. #include <notification/notification_messages.h>
  12. #include <storage/storage.h>
  13. typedef struct ButtonEvent {
  14. int8_t button;
  15. bool state;
  16. } ButtonEvent;
  17. #define BTN_EVT_QUEUE_SIZE 32
  18. struct BtMouse {
  19. FuriHalBleProfileBase* hid;
  20. View* view;
  21. ViewDispatcher* view_dispatcher;
  22. Bt* bt;
  23. NotificationApp* notifications;
  24. FuriMutex* mutex;
  25. FuriThread* thread;
  26. bool connected;
  27. // Current mouse state
  28. uint8_t btn;
  29. int dx;
  30. int dy;
  31. int wheel;
  32. // Circular buffer;
  33. // (qhead == qtail) means either empty or overflow.
  34. // We'll ignore overflow and treat it as empty.
  35. int qhead;
  36. int qtail;
  37. ButtonEvent queue[BTN_EVT_QUEUE_SIZE];
  38. };
  39. static const BleProfileHidParams ble_hid_params = {
  40. .device_name_prefix = "AirMouse",
  41. .mac_xor = 0x0001,
  42. };
  43. #define BT_MOUSE_FLAG_INPUT_EVENT (1UL << 0)
  44. #define BT_MOUSE_FLAG_KILL_THREAD (1UL << 1)
  45. #define BT_MOUSE_FLAG_ALL (BT_MOUSE_FLAG_INPUT_EVENT | BT_MOUSE_FLAG_KILL_THREAD)
  46. #define MOUSE_SCROLL 2
  47. #define HID_BT_KEYS_STORAGE_NAME ".bt_hid.keys"
  48. static void bt_mouse_notify_event(BtMouse* bt_mouse) {
  49. FuriThreadId thread_id = furi_thread_get_id(bt_mouse->thread);
  50. furi_assert(thread_id);
  51. furi_thread_flags_set(thread_id, BT_MOUSE_FLAG_INPUT_EVENT);
  52. }
  53. static void bt_mouse_draw_callback(Canvas* canvas, void* context) {
  54. UNUSED(context);
  55. canvas_clear(canvas);
  56. canvas_set_font(canvas, FontPrimary);
  57. canvas_draw_str(canvas, 0, 10, "Bluetooth Mouse mode");
  58. canvas_set_font(canvas, FontSecondary);
  59. canvas_draw_str(canvas, 0, 63, "Hold [back] to exit");
  60. }
  61. static void bt_mouse_button_state(BtMouse* bt_mouse, int8_t button, bool state) {
  62. ButtonEvent event;
  63. event.button = button;
  64. event.state = state;
  65. if(bt_mouse->connected) {
  66. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  67. bt_mouse->queue[bt_mouse->qtail++] = event;
  68. bt_mouse->qtail %= BTN_EVT_QUEUE_SIZE;
  69. furi_mutex_release(bt_mouse->mutex);
  70. bt_mouse_notify_event(bt_mouse);
  71. }
  72. }
  73. static void bt_mouse_process(BtMouse* bt_mouse, InputEvent* event) {
  74. with_view_model(
  75. bt_mouse->view,
  76. void* model,
  77. {
  78. UNUSED(model);
  79. if(event->key == InputKeyUp) {
  80. if(event->type == InputTypePress) {
  81. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_LEFT, true);
  82. } else if(event->type == InputTypeRelease) {
  83. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_LEFT, false);
  84. }
  85. } else if(event->key == InputKeyDown) {
  86. if(event->type == InputTypePress) {
  87. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_RIGHT, true);
  88. } else if(event->type == InputTypeRelease) {
  89. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_RIGHT, false);
  90. }
  91. } else if(event->key == InputKeyOk) {
  92. if(event->type == InputTypePress) {
  93. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_WHEEL, true);
  94. } else if(event->type == InputTypeRelease) {
  95. bt_mouse_button_state(bt_mouse, HID_MOUSE_BTN_WHEEL, false);
  96. }
  97. } else if(event->key == InputKeyRight) {
  98. if(event->type == InputTypePress || event->type == InputTypeRepeat) {
  99. bt_mouse->wheel = MOUSE_SCROLL;
  100. }
  101. } else if(event->key == InputKeyLeft) {
  102. if(event->type == InputTypePress || event->type == InputTypeRepeat) {
  103. bt_mouse->wheel = -MOUSE_SCROLL;
  104. }
  105. }
  106. },
  107. true);
  108. }
  109. static bool bt_mouse_input_callback(InputEvent* event, void* context) {
  110. furi_assert(context);
  111. BtMouse* bt_mouse = context;
  112. bool consumed = false;
  113. if(event->type == InputTypeLong && event->key == InputKeyBack) {
  114. ble_profile_hid_mouse_release_all(bt_mouse->hid);
  115. } else {
  116. bt_mouse_process(bt_mouse, event);
  117. consumed = true;
  118. }
  119. return consumed;
  120. }
  121. void bt_mouse_connection_status_changed_callback(BtStatus status, void* context) {
  122. furi_assert(context);
  123. BtMouse* bt_mouse = context;
  124. bt_mouse->connected = (status == BtStatusConnected);
  125. if(!bt_mouse->notifications) {
  126. tracking_end();
  127. return;
  128. }
  129. if(bt_mouse->connected) {
  130. notification_internal_message(bt_mouse->notifications, &sequence_set_blue_255);
  131. tracking_begin();
  132. } else {
  133. tracking_end();
  134. notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
  135. }
  136. }
  137. bool bt_mouse_move(int8_t dx, int8_t dy, void* context) {
  138. furi_assert(context);
  139. BtMouse* bt_mouse = context;
  140. if(bt_mouse->connected) {
  141. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  142. bt_mouse->dx += dx;
  143. bt_mouse->dy += dy;
  144. furi_mutex_release(bt_mouse->mutex);
  145. bt_mouse_notify_event(bt_mouse);
  146. }
  147. return true;
  148. }
  149. static int8_t clamp(int t) {
  150. if(t < -128) {
  151. return -128;
  152. } else if(t > 127) {
  153. return 127;
  154. }
  155. return t;
  156. }
  157. static int32_t bt_mouse_thread_callback(void* context) {
  158. furi_assert(context);
  159. BtMouse* bt_mouse = (BtMouse*)context;
  160. while(1) {
  161. uint32_t flags =
  162. furi_thread_flags_wait(BT_MOUSE_FLAG_ALL, FuriFlagWaitAny, FuriWaitForever);
  163. if(flags & BT_MOUSE_FLAG_KILL_THREAD) {
  164. break;
  165. }
  166. if(flags & BT_MOUSE_FLAG_INPUT_EVENT) {
  167. furi_mutex_acquire(bt_mouse->mutex, FuriWaitForever);
  168. ButtonEvent event;
  169. bool send_buttons = false;
  170. if(bt_mouse->qhead != bt_mouse->qtail) {
  171. event = bt_mouse->queue[bt_mouse->qhead++];
  172. bt_mouse->qhead %= BTN_EVT_QUEUE_SIZE;
  173. send_buttons = true;
  174. }
  175. int8_t dx = clamp(bt_mouse->dx);
  176. bt_mouse->dx -= dx;
  177. int8_t dy = clamp(bt_mouse->dy);
  178. bt_mouse->dy -= dy;
  179. int8_t wheel = clamp(bt_mouse->wheel);
  180. bt_mouse->wheel -= wheel;
  181. furi_mutex_release(bt_mouse->mutex);
  182. if(bt_mouse->connected && send_buttons) {
  183. if(event.state) {
  184. ble_profile_hid_mouse_press(bt_mouse->hid, event.button);
  185. } else {
  186. ble_profile_hid_mouse_release(bt_mouse->hid, event.button);
  187. }
  188. }
  189. if(bt_mouse->connected && (dx != 0 || dy != 0)) {
  190. ble_profile_hid_mouse_move(bt_mouse->hid, dx, dy);
  191. }
  192. if(bt_mouse->connected && wheel != 0) {
  193. ble_profile_hid_mouse_scroll(bt_mouse->hid, wheel);
  194. }
  195. }
  196. }
  197. return 0;
  198. }
  199. void bt_mouse_thread_start(BtMouse* bt_mouse) {
  200. furi_assert(bt_mouse);
  201. bt_mouse->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  202. bt_mouse->thread = furi_thread_alloc();
  203. furi_thread_set_name(bt_mouse->thread, "BtSender");
  204. furi_thread_set_stack_size(bt_mouse->thread, 1024);
  205. furi_thread_set_context(bt_mouse->thread, bt_mouse);
  206. furi_thread_set_callback(bt_mouse->thread, bt_mouse_thread_callback);
  207. furi_thread_start(bt_mouse->thread);
  208. }
  209. void bt_mouse_thread_stop(BtMouse* bt_mouse) {
  210. furi_assert(bt_mouse);
  211. FuriThreadId thread_id = furi_thread_get_id(bt_mouse->thread);
  212. furi_assert(thread_id);
  213. furi_thread_flags_set(thread_id, BT_MOUSE_FLAG_KILL_THREAD);
  214. furi_thread_join(bt_mouse->thread);
  215. furi_thread_free(bt_mouse->thread);
  216. furi_mutex_free(bt_mouse->mutex);
  217. bt_mouse->mutex = NULL;
  218. bt_mouse->thread = NULL;
  219. }
  220. void bt_mouse_tick_event_callback(void* context) {
  221. furi_assert(context);
  222. tracking_step(bt_mouse_move, context);
  223. }
  224. void bt_mouse_enter_callback(void* context) {
  225. furi_assert(context);
  226. BtMouse* bt_mouse = context;
  227. bt_mouse->bt = furi_record_open(RECORD_BT);
  228. bt_disconnect(bt_mouse->bt);
  229. furi_delay_ms(200);
  230. bt_keys_storage_set_storage_path(bt_mouse->bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
  231. bt_mouse->notifications = furi_record_open(RECORD_NOTIFICATION);
  232. bt_set_status_changed_callback(
  233. bt_mouse->bt, bt_mouse_connection_status_changed_callback, bt_mouse);
  234. bt_mouse->hid = bt_profile_start(bt_mouse->bt, ble_profile_hid, (void*)&ble_hid_params);
  235. furi_assert(bt_mouse->hid);
  236. furi_hal_bt_start_advertising();
  237. bt_mouse_thread_start(bt_mouse);
  238. view_dispatcher_set_event_callback_context(bt_mouse->view_dispatcher, bt_mouse);
  239. view_dispatcher_set_tick_event_callback(
  240. bt_mouse->view_dispatcher, bt_mouse_tick_event_callback, furi_ms_to_ticks(10));
  241. }
  242. void bt_mouse_remove_pairing(void) {
  243. Bt* bt = furi_record_open(RECORD_BT);
  244. bt_disconnect(bt);
  245. furi_delay_ms(200);
  246. furi_hal_bt_stop_advertising();
  247. bt_keys_storage_set_storage_path(bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
  248. bt_forget_bonded_devices(bt);
  249. furi_delay_ms(200);
  250. bt_keys_storage_set_default_path(bt);
  251. furi_check(bt_profile_restore_default(bt));
  252. furi_record_close(RECORD_BT);
  253. }
  254. void bt_mouse_exit_callback(void* context) {
  255. furi_assert(context);
  256. BtMouse* bt_mouse = context;
  257. view_dispatcher_set_tick_event_callback(bt_mouse->view_dispatcher, NULL, FuriWaitForever);
  258. bt_mouse_thread_stop(bt_mouse);
  259. tracking_end();
  260. notification_internal_message(bt_mouse->notifications, &sequence_reset_blue);
  261. bt_set_status_changed_callback(bt_mouse->bt, NULL, NULL);
  262. bt_disconnect(bt_mouse->bt);
  263. furi_delay_ms(200);
  264. bt_keys_storage_set_default_path(bt_mouse->bt);
  265. furi_hal_bt_stop_advertising();
  266. bt_profile_restore_default(bt_mouse->bt);
  267. furi_record_close(RECORD_NOTIFICATION);
  268. bt_mouse->notifications = NULL;
  269. furi_record_close(RECORD_BT);
  270. bt_mouse->bt = NULL;
  271. }
  272. BtMouse* bt_mouse_alloc(ViewDispatcher* view_dispatcher) {
  273. BtMouse* bt_mouse = malloc(sizeof(BtMouse));
  274. memset(bt_mouse, 0, sizeof(BtMouse));
  275. bt_mouse->view = view_alloc();
  276. bt_mouse->view_dispatcher = view_dispatcher;
  277. view_set_context(bt_mouse->view, bt_mouse);
  278. view_set_draw_callback(bt_mouse->view, bt_mouse_draw_callback);
  279. view_set_input_callback(bt_mouse->view, bt_mouse_input_callback);
  280. view_set_enter_callback(bt_mouse->view, bt_mouse_enter_callback);
  281. view_set_exit_callback(bt_mouse->view, bt_mouse_exit_callback);
  282. return bt_mouse;
  283. }
  284. void bt_mouse_free(BtMouse* bt_mouse) {
  285. furi_assert(bt_mouse);
  286. view_free(bt_mouse->view);
  287. free(bt_mouse);
  288. }
  289. View* bt_mouse_get_view(BtMouse* bt_mouse) {
  290. furi_assert(bt_mouse);
  291. return bt_mouse->view;
  292. }