hid.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include "hid.h"
  2. #include <extra_profiles/hid_profile.h>
  3. #include <profiles/serial_profile.h>
  4. #include "views.h"
  5. #include <notification/notification_messages.h>
  6. #include "hid_icons.h"
  7. #define TAG "HidApp"
  8. bool hid_custom_event_callback(void* context, uint32_t event) {
  9. furi_assert(context);
  10. Hid* app = context;
  11. return scene_manager_handle_custom_event(app->scene_manager, event);
  12. }
  13. bool hid_back_event_callback(void* context) {
  14. furi_assert(context);
  15. //Hid* app = context;
  16. FURI_LOG_D("HID", "Back event");
  17. /*app->view_id = HidViewSubmenu;
  18. view_dispatcher_switch_to_view(app->view_dispatcher, HidViewSubmenu);*/
  19. return true;
  20. }
  21. static void bt_hid_connection_status_changed_callback(BtStatus status, void* context) {
  22. furi_assert(context);
  23. Hid* hid = context;
  24. bool connected = (status == BtStatusConnected);
  25. #ifdef HID_TRANSPORT_BLE
  26. if(connected) {
  27. notification_internal_message(hid->notifications, &sequence_set_blue_255);
  28. } else {
  29. notification_internal_message(hid->notifications, &sequence_reset_blue);
  30. }
  31. #endif
  32. hid_keynote_set_connected_status(hid->hid_keynote, connected);
  33. }
  34. static uint32_t hid_exit(void* context) {
  35. UNUSED(context);
  36. return VIEW_NONE;
  37. }
  38. Hid* hid_alloc() {
  39. Hid* app = malloc(sizeof(Hid));
  40. // Gui
  41. app->gui = furi_record_open(RECORD_GUI);
  42. // Bt
  43. app->bt = furi_record_open(RECORD_BT);
  44. // Notifications
  45. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  46. // View dispatcher
  47. app->view_dispatcher = view_dispatcher_alloc();
  48. view_dispatcher_enable_queue(app->view_dispatcher);
  49. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  50. view_dispatcher_set_navigation_event_callback(app->view_dispatcher, hid_back_event_callback);
  51. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  52. // Scene Manager
  53. app->scene_manager = scene_manager_alloc(&hid_scene_handlers, app);
  54. // Keynote view
  55. app->hid_keynote = hid_keynote_alloc(app);
  56. view_dispatcher_add_view(
  57. app->view_dispatcher, HidViewKeynote, hid_keynote_get_view(app->hid_keynote));
  58. // Open Keynote view
  59. app->view_id = HidViewKeynote;
  60. view_set_previous_callback(hid_keynote_get_view(app->hid_keynote), hid_exit);
  61. hid_keynote_set_orientation(app->hid_keynote, false);
  62. view_dispatcher_switch_to_view(app->view_dispatcher, HidViewKeynote);
  63. return app;
  64. }
  65. Hid* hid_app_alloc_view(void* context) {
  66. furi_assert(context);
  67. Hid* app = context;
  68. return app;
  69. }
  70. void hid_free(Hid* app) {
  71. furi_assert(app);
  72. // Reset notification
  73. #ifdef HID_TRANSPORT_BLE
  74. notification_internal_message(app->notifications, &sequence_reset_blue);
  75. #endif
  76. // Free views
  77. view_dispatcher_remove_view(app->view_dispatcher, HidViewKeynote);
  78. hid_keynote_free(app->hid_keynote);
  79. scene_manager_free(app->scene_manager);
  80. view_dispatcher_free(app->view_dispatcher);
  81. // Close records
  82. furi_record_close(RECORD_GUI);
  83. app->gui = NULL;
  84. furi_record_close(RECORD_NOTIFICATION);
  85. app->notifications = NULL;
  86. furi_record_close(RECORD_BT);
  87. app->bt = NULL;
  88. // Free rest
  89. free(app);
  90. }
  91. int32_t bt_hid_kodi(void* p) {
  92. UNUSED(p);
  93. Hid* app = hid_alloc();
  94. app = hid_app_alloc_view(app);
  95. FURI_LOG_D("HID", "Starting as BLE app");
  96. bt_disconnect(app->bt);
  97. // Wait 2nd core to update nvm storage
  98. furi_delay_ms(200);
  99. // Migrate data from old sd-card folder
  100. Storage* storage = furi_record_open(RECORD_STORAGE);
  101. storage_common_migrate(
  102. storage,
  103. EXT_PATH("apps/Bluetooth/" HID_BT_KEYS_STORAGE_NAME),
  104. APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
  105. bt_keys_storage_set_storage_path(app->bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
  106. furi_record_close(RECORD_STORAGE);
  107. app->ble_hid_profile = bt_profile_start(app->bt, ble_profile_hid, NULL);
  108. furi_check(app->ble_hid_profile);
  109. furi_hal_bt_start_advertising();
  110. bt_set_status_changed_callback(app->bt, bt_hid_connection_status_changed_callback, app);
  111. scene_manager_next_scene(app->scene_manager, HidSceneMain);
  112. view_dispatcher_run(app->view_dispatcher);
  113. bt_set_status_changed_callback(app->bt, NULL, NULL);
  114. bt_disconnect(app->bt);
  115. // Wait 2nd core to update nvm storage
  116. furi_delay_ms(200);
  117. bt_keys_storage_set_default_path(app->bt);
  118. furi_check(bt_profile_restore_default(app->bt));
  119. hid_free(app);
  120. return 0;
  121. }