subghz_scene_rpc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "../subghz_i.h"
  2. typedef enum {
  3. SubGhzRpcStateIdle,
  4. SubGhzRpcStateLoaded,
  5. SubGhzRpcStateTx,
  6. } SubGhzRpcState;
  7. void subghz_scene_rpc_on_enter(void* context) {
  8. SubGhz* subghz = context;
  9. Popup* popup = subghz->popup;
  10. popup_set_header(popup, "Sub-GHz", 89, 42, AlignCenter, AlignBottom);
  11. popup_set_text(popup, "RPC mode", 89, 44, AlignCenter, AlignTop);
  12. popup_set_icon(popup, 0, 12, &I_RFIDDolphinSend_97x61);
  13. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdPopup);
  14. scene_manager_set_scene_state(subghz->scene_manager, SubGhzSceneRpc, SubGhzRpcStateIdle);
  15. notification_message(subghz->notifications, &sequence_display_backlight_on);
  16. }
  17. bool subghz_scene_rpc_on_event(void* context, SceneManagerEvent event) {
  18. SubGhz* subghz = context;
  19. Popup* popup = subghz->popup;
  20. bool consumed = false;
  21. SubGhzRpcState state = scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneRpc);
  22. if(event.type == SceneManagerEventTypeCustom) {
  23. consumed = true;
  24. if(event.event == SubGhzCustomEventSceneExit) {
  25. scene_manager_stop(subghz->scene_manager);
  26. view_dispatcher_stop(subghz->view_dispatcher);
  27. rpc_system_app_confirm(subghz->rpc_ctx, RpcAppEventAppExit, true);
  28. } else if(event.event == SubGhzCustomEventSceneRpcSessionClose) {
  29. scene_manager_stop(subghz->scene_manager);
  30. view_dispatcher_stop(subghz->view_dispatcher);
  31. } else if(event.event == SubGhzCustomEventSceneRpcButtonPress) {
  32. bool result = false;
  33. if((state == SubGhzRpcStateLoaded)) {
  34. result = subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx));
  35. state = SubGhzRpcStateTx;
  36. if(result) subghz_blink_start(subghz);
  37. }
  38. if(!result) {
  39. rpc_system_app_set_error_code(subghz->rpc_ctx, SubGhzErrorTypeOnlyRX);
  40. rpc_system_app_set_error_text(
  41. subghz->rpc_ctx,
  42. "Transmission on this frequency is restricted in your region");
  43. }
  44. rpc_system_app_confirm(subghz->rpc_ctx, RpcAppEventButtonPress, result);
  45. } else if(event.event == SubGhzCustomEventSceneRpcButtonRelease) {
  46. bool result = false;
  47. if(state == SubGhzRpcStateTx) {
  48. subghz_txrx_stop(subghz->txrx);
  49. subghz_blink_stop(subghz);
  50. state = SubGhzRpcStateIdle;
  51. result = true;
  52. }
  53. rpc_system_app_confirm(subghz->rpc_ctx, RpcAppEventButtonRelease, result);
  54. } else if(event.event == SubGhzCustomEventSceneRpcLoad) {
  55. bool result = false;
  56. const char* arg = rpc_system_app_get_data(subghz->rpc_ctx);
  57. if(arg && (state == SubGhzRpcStateIdle)) {
  58. if(subghz_key_load(subghz, arg, false)) {
  59. scene_manager_set_scene_state(
  60. subghz->scene_manager, SubGhzSceneRpc, SubGhzRpcStateLoaded);
  61. furi_string_set(subghz->file_path, arg);
  62. result = true;
  63. FuriString* file_name;
  64. file_name = furi_string_alloc();
  65. path_extract_filename(subghz->file_path, file_name, true);
  66. snprintf(
  67. subghz->file_name_tmp,
  68. SUBGHZ_MAX_LEN_NAME,
  69. "loaded\n%s",
  70. furi_string_get_cstr(file_name));
  71. popup_set_text(popup, subghz->file_name_tmp, 89, 44, AlignCenter, AlignTop);
  72. furi_string_free(file_name);
  73. } else {
  74. rpc_system_app_set_error_code(subghz->rpc_ctx, SubGhzErrorTypeParseFile);
  75. rpc_system_app_set_error_text(subghz->rpc_ctx, "Cannot parse file");
  76. }
  77. }
  78. rpc_system_app_confirm(subghz->rpc_ctx, RpcAppEventLoadFile, result);
  79. }
  80. }
  81. return consumed;
  82. }
  83. void subghz_scene_rpc_on_exit(void* context) {
  84. SubGhz* subghz = context;
  85. SubGhzRpcState state = scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneRpc);
  86. if(state != SubGhzRpcStateIdle) {
  87. subghz_txrx_stop(subghz->txrx);
  88. subghz_blink_stop(subghz);
  89. }
  90. Popup* popup = subghz->popup;
  91. popup_set_header(popup, NULL, 0, 0, AlignCenter, AlignBottom);
  92. popup_set_text(popup, NULL, 0, 0, AlignCenter, AlignTop);
  93. popup_set_icon(popup, 0, 0, NULL);
  94. }