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_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  49. view_dispatcher_set_navigation_event_callback(app->view_dispatcher, hid_back_event_callback);
  50. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  51. // Scene Manager
  52. app->scene_manager = scene_manager_alloc(&hid_scene_handlers, app);
  53. // Keynote view
  54. app->hid_keynote = hid_keynote_alloc(app);
  55. view_dispatcher_add_view(
  56. app->view_dispatcher, HidViewKeynote, hid_keynote_get_view(app->hid_keynote));
  57. // Open Keynote view
  58. app->view_id = HidViewKeynote;
  59. view_set_previous_callback(hid_keynote_get_view(app->hid_keynote), hid_exit);
  60. hid_keynote_set_orientation(app->hid_keynote, false);
  61. view_dispatcher_switch_to_view(app->view_dispatcher, HidViewKeynote);
  62. return app;
  63. }
  64. Hid* hid_app_alloc_view(void* context) {
  65. furi_assert(context);
  66. Hid* app = context;
  67. return app;
  68. }
  69. void hid_free(Hid* app) {
  70. furi_assert(app);
  71. // Reset notification
  72. #ifdef HID_TRANSPORT_BLE
  73. notification_internal_message(app->notifications, &sequence_reset_blue);
  74. #endif
  75. // Free views
  76. view_dispatcher_remove_view(app->view_dispatcher, HidViewKeynote);
  77. hid_keynote_free(app->hid_keynote);
  78. scene_manager_free(app->scene_manager);
  79. view_dispatcher_free(app->view_dispatcher);
  80. // Close records
  81. furi_record_close(RECORD_GUI);
  82. app->gui = NULL;
  83. furi_record_close(RECORD_NOTIFICATION);
  84. app->notifications = NULL;
  85. furi_record_close(RECORD_BT);
  86. app->bt = NULL;
  87. // Free rest
  88. free(app);
  89. }
  90. int32_t bt_hid_kodi(void* p) {
  91. UNUSED(p);
  92. Hid* app = hid_alloc();
  93. app = hid_app_alloc_view(app);
  94. FURI_LOG_D("HID", "Starting as BLE app");
  95. bt_disconnect(app->bt);
  96. // Wait 2nd core to update nvm storage
  97. furi_delay_ms(200);
  98. // Migrate data from old sd-card folder
  99. Storage* storage = furi_record_open(RECORD_STORAGE);
  100. storage_common_migrate(
  101. storage,
  102. EXT_PATH("apps/Bluetooth/" HID_BT_KEYS_STORAGE_NAME),
  103. APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
  104. bt_keys_storage_set_storage_path(app->bt, APP_DATA_PATH(HID_BT_KEYS_STORAGE_NAME));
  105. furi_record_close(RECORD_STORAGE);
  106. app->ble_hid_profile = bt_profile_start(app->bt, ble_profile_hid, NULL);
  107. furi_check(app->ble_hid_profile);
  108. furi_hal_bt_start_advertising();
  109. bt_set_status_changed_callback(app->bt, bt_hid_connection_status_changed_callback, app);
  110. scene_manager_next_scene(app->scene_manager, HidSceneMain);
  111. view_dispatcher_run(app->view_dispatcher);
  112. bt_set_status_changed_callback(app->bt, NULL, NULL);
  113. bt_disconnect(app->bt);
  114. // Wait 2nd core to update nvm storage
  115. furi_delay_ms(200);
  116. bt_keys_storage_set_default_path(app->bt);
  117. furi_check(bt_profile_restore_default(app->bt));
  118. hid_free(app);
  119. return 0;
  120. }