bt_debug_app.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "bt_debug_app.h"
  2. #include <furi_hal_bt.h>
  3. #define TAG "BtDebugApp"
  4. enum BtDebugSubmenuIndex {
  5. BtDebugSubmenuIndexCarrierTest,
  6. BtDebugSubmenuIndexPacketTest,
  7. };
  8. void bt_debug_submenu_callback(void* context, uint32_t index) {
  9. furi_assert(context);
  10. BtDebugApp* app = context;
  11. if(index == BtDebugSubmenuIndexCarrierTest) {
  12. view_dispatcher_switch_to_view(app->view_dispatcher, BtDebugAppViewCarrierTest);
  13. } else if(index == BtDebugSubmenuIndexPacketTest) {
  14. view_dispatcher_switch_to_view(app->view_dispatcher, BtDebugAppViewPacketTest);
  15. }
  16. }
  17. uint32_t bt_debug_exit(void* context) {
  18. UNUSED(context);
  19. return VIEW_NONE;
  20. }
  21. uint32_t bt_debug_start_view(void* context) {
  22. UNUSED(context);
  23. return BtDebugAppViewSubmenu;
  24. }
  25. BtDebugApp* bt_debug_app_alloc() {
  26. BtDebugApp* app = malloc(sizeof(BtDebugApp));
  27. // Gui
  28. app->gui = furi_record_open(RECORD_GUI);
  29. // View dispatcher
  30. app->view_dispatcher = view_dispatcher_alloc();
  31. view_dispatcher_enable_queue(app->view_dispatcher);
  32. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  33. // Views
  34. app->submenu = submenu_alloc();
  35. submenu_add_item(
  36. app->submenu,
  37. "Carrier test",
  38. BtDebugSubmenuIndexCarrierTest,
  39. bt_debug_submenu_callback,
  40. app);
  41. submenu_add_item(
  42. app->submenu, "Packet test", BtDebugSubmenuIndexPacketTest, bt_debug_submenu_callback, app);
  43. view_set_previous_callback(submenu_get_view(app->submenu), bt_debug_exit);
  44. view_dispatcher_add_view(
  45. app->view_dispatcher, BtDebugAppViewSubmenu, submenu_get_view(app->submenu));
  46. app->bt_carrier_test = bt_carrier_test_alloc();
  47. view_set_previous_callback(
  48. bt_carrier_test_get_view(app->bt_carrier_test), bt_debug_start_view);
  49. view_dispatcher_add_view(
  50. app->view_dispatcher,
  51. BtDebugAppViewCarrierTest,
  52. bt_carrier_test_get_view(app->bt_carrier_test));
  53. app->bt_packet_test = bt_packet_test_alloc();
  54. view_set_previous_callback(bt_packet_test_get_view(app->bt_packet_test), bt_debug_start_view);
  55. view_dispatcher_add_view(
  56. app->view_dispatcher,
  57. BtDebugAppViewPacketTest,
  58. bt_packet_test_get_view(app->bt_packet_test));
  59. // Switch to menu
  60. view_dispatcher_switch_to_view(app->view_dispatcher, BtDebugAppViewSubmenu);
  61. return app;
  62. }
  63. void bt_debug_app_free(BtDebugApp* app) {
  64. furi_assert(app);
  65. // Free views
  66. view_dispatcher_remove_view(app->view_dispatcher, BtDebugAppViewSubmenu);
  67. submenu_free(app->submenu);
  68. view_dispatcher_remove_view(app->view_dispatcher, BtDebugAppViewCarrierTest);
  69. bt_carrier_test_free(app->bt_carrier_test);
  70. view_dispatcher_remove_view(app->view_dispatcher, BtDebugAppViewPacketTest);
  71. bt_packet_test_free(app->bt_packet_test);
  72. view_dispatcher_free(app->view_dispatcher);
  73. // Close gui record
  74. furi_record_close(RECORD_GUI);
  75. app->gui = NULL;
  76. // Free rest
  77. free(app);
  78. }
  79. int32_t bt_debug_app(void* p) {
  80. UNUSED(p);
  81. if(!furi_hal_bt_is_testing_supported()) {
  82. FURI_LOG_E(TAG, "Incorrect radio stack: radio testing features are absent.");
  83. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  84. dialog_message_show_storage_error(dialogs, "Incorrect\nRadioStack");
  85. return 255;
  86. }
  87. BtDebugApp* app = bt_debug_app_alloc();
  88. // Was bt active?
  89. const bool was_active = furi_hal_bt_is_active();
  90. // Stop advertising
  91. furi_hal_bt_stop_advertising();
  92. view_dispatcher_run(app->view_dispatcher);
  93. // Restart advertising
  94. if(was_active) {
  95. furi_hal_bt_start_advertising();
  96. }
  97. bt_debug_app_free(app);
  98. return 0;
  99. }