rpc_debug_app.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "rpc_debug_app.h"
  2. #include <core/log.h>
  3. #include <string.h>
  4. static bool rpc_debug_app_custom_event_callback(void* context, uint32_t event) {
  5. furi_assert(context);
  6. RpcDebugApp* app = context;
  7. return scene_manager_handle_custom_event(app->scene_manager, event);
  8. }
  9. static bool rpc_debug_app_back_event_callback(void* context) {
  10. furi_assert(context);
  11. RpcDebugApp* app = context;
  12. return scene_manager_handle_back_event(app->scene_manager);
  13. }
  14. static void rpc_debug_app_tick_event_callback(void* context) {
  15. furi_assert(context);
  16. RpcDebugApp* app = context;
  17. scene_manager_handle_tick_event(app->scene_manager);
  18. }
  19. static void rpc_debug_app_rpc_command_callback(RpcAppSystemEvent event, void* context) {
  20. furi_assert(context);
  21. RpcDebugApp* app = context;
  22. furi_assert(app->rpc);
  23. if(event == RpcAppEventSessionClose) {
  24. scene_manager_stop(app->scene_manager);
  25. view_dispatcher_stop(app->view_dispatcher);
  26. rpc_system_app_set_callback(app->rpc, NULL, NULL);
  27. app->rpc = NULL;
  28. } else if(event == RpcAppEventAppExit) {
  29. scene_manager_stop(app->scene_manager);
  30. view_dispatcher_stop(app->view_dispatcher);
  31. rpc_system_app_confirm(app->rpc, RpcAppEventAppExit, true);
  32. } else {
  33. rpc_system_app_confirm(app->rpc, event, false);
  34. }
  35. }
  36. static bool rpc_debug_app_rpc_init_rpc(RpcDebugApp* app, const char* args) {
  37. bool ret = false;
  38. if(args && strlen(args)) {
  39. uint32_t rpc = 0;
  40. if(sscanf(args, "RPC %lX", &rpc) == 1) {
  41. app->rpc = (RpcAppSystem*)rpc;
  42. rpc_system_app_set_callback(app->rpc, rpc_debug_app_rpc_command_callback, app);
  43. rpc_system_app_send_started(app->rpc);
  44. ret = true;
  45. }
  46. }
  47. return ret;
  48. }
  49. static RpcDebugApp* rpc_debug_app_alloc() {
  50. RpcDebugApp* app = malloc(sizeof(RpcDebugApp));
  51. app->gui = furi_record_open(RECORD_GUI);
  52. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  53. app->scene_manager = scene_manager_alloc(&rpc_debug_app_scene_handlers, app);
  54. app->view_dispatcher = view_dispatcher_alloc();
  55. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  56. view_dispatcher_set_custom_event_callback(
  57. app->view_dispatcher, rpc_debug_app_custom_event_callback);
  58. view_dispatcher_set_navigation_event_callback(
  59. app->view_dispatcher, rpc_debug_app_back_event_callback);
  60. view_dispatcher_set_tick_event_callback(
  61. app->view_dispatcher, rpc_debug_app_tick_event_callback, 100);
  62. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  63. view_dispatcher_enable_queue(app->view_dispatcher);
  64. app->widget = widget_alloc();
  65. view_dispatcher_add_view(
  66. app->view_dispatcher, RpcDebugAppViewWidget, widget_get_view(app->widget));
  67. app->submenu = submenu_alloc();
  68. view_dispatcher_add_view(
  69. app->view_dispatcher, RpcDebugAppViewSubmenu, submenu_get_view(app->submenu));
  70. app->text_box = text_box_alloc();
  71. view_dispatcher_add_view(
  72. app->view_dispatcher, RpcDebugAppViewTextBox, text_box_get_view(app->text_box));
  73. app->text_input = text_input_alloc();
  74. view_dispatcher_add_view(
  75. app->view_dispatcher, RpcDebugAppViewTextInput, text_input_get_view(app->text_input));
  76. app->byte_input = byte_input_alloc();
  77. view_dispatcher_add_view(
  78. app->view_dispatcher, RpcDebugAppViewByteInput, byte_input_get_view(app->byte_input));
  79. return app;
  80. }
  81. static void rpc_debug_app_free(RpcDebugApp* app) {
  82. view_dispatcher_remove_view(app->view_dispatcher, RpcDebugAppViewByteInput);
  83. view_dispatcher_remove_view(app->view_dispatcher, RpcDebugAppViewTextInput);
  84. view_dispatcher_remove_view(app->view_dispatcher, RpcDebugAppViewTextBox);
  85. view_dispatcher_remove_view(app->view_dispatcher, RpcDebugAppViewSubmenu);
  86. view_dispatcher_remove_view(app->view_dispatcher, RpcDebugAppViewWidget);
  87. free(app->byte_input);
  88. free(app->text_input);
  89. free(app->text_box);
  90. free(app->submenu);
  91. free(app->widget);
  92. free(app->scene_manager);
  93. free(app->view_dispatcher);
  94. furi_record_close(RECORD_NOTIFICATION);
  95. app->notifications = NULL;
  96. furi_record_close(RECORD_GUI);
  97. app->gui = NULL;
  98. if(app->rpc) {
  99. rpc_system_app_set_callback(app->rpc, NULL, NULL);
  100. rpc_system_app_send_exited(app->rpc);
  101. app->rpc = NULL;
  102. }
  103. free(app);
  104. }
  105. int32_t rpc_debug_app(void* args) {
  106. RpcDebugApp* app = rpc_debug_app_alloc();
  107. if(rpc_debug_app_rpc_init_rpc(app, args)) {
  108. notification_message(app->notifications, &sequence_display_backlight_on);
  109. scene_manager_next_scene(app->scene_manager, RpcDebugAppSceneStart);
  110. } else {
  111. scene_manager_next_scene(app->scene_manager, RpcDebugAppSceneStartDummy);
  112. }
  113. view_dispatcher_run(app->view_dispatcher);
  114. rpc_debug_app_free(app);
  115. return 0;
  116. }