action_subghz.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // Methods for Sub-GHz transmission
  2. #include <flipper_format/flipper_format_i.h>
  3. #include "helpers/subghz_txrx.h"
  4. #include "action_i.h"
  5. #include "quac.h"
  6. typedef struct SubGhzNeedSaveContext {
  7. App* app;
  8. SubGhzTxRx* txrx;
  9. const FuriString* file_path;
  10. } SubGhzNeedSaveContext;
  11. void action_subghz_need_save_callback(void* context) {
  12. FURI_LOG_I(TAG, "Saving udpated subghz signal");
  13. SubGhzNeedSaveContext* savectx = (SubGhzNeedSaveContext*)context;
  14. FlipperFormat* ff = subghz_txrx_get_fff_data(savectx->txrx);
  15. Stream* ff_stream = flipper_format_get_raw_stream(ff);
  16. flipper_format_delete_key(ff, "Repeat");
  17. flipper_format_delete_key(ff, "Manufacture");
  18. do {
  19. if(!storage_simply_remove(
  20. savectx->app->storage, furi_string_get_cstr(savectx->file_path))) {
  21. FURI_LOG_E(TAG, "Failed to delete subghz file before re-save");
  22. break;
  23. }
  24. stream_seek(ff_stream, 0, StreamOffsetFromStart);
  25. stream_save_to_file(
  26. ff_stream,
  27. savectx->app->storage,
  28. furi_string_get_cstr(savectx->file_path),
  29. FSOM_CREATE_ALWAYS);
  30. if(storage_common_stat(
  31. savectx->app->storage, furi_string_get_cstr(savectx->file_path), NULL) != FSE_OK) {
  32. FURI_LOG_E(TAG, "Error verifying new subghz file after re-save");
  33. break;
  34. }
  35. } while(0);
  36. }
  37. static void action_subghz_raw_end_callback(void* context) {
  38. FURI_LOG_I(TAG, "Stopping TX on RAW");
  39. furi_assert(context);
  40. FuriThread* thread = context;
  41. furi_thread_flags_set(furi_thread_get_id(thread), 0);
  42. }
  43. void action_subghz_tx(void* context, const FuriString* action_path, FuriString* error) {
  44. App* app = context;
  45. const char* file_name = furi_string_get_cstr(action_path);
  46. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  47. SubGhzTxRx* txrx = subghz_txrx_alloc();
  48. SubGhzNeedSaveContext save_context = {app, txrx, action_path};
  49. subghz_txrx_set_need_save_callback(txrx, action_subghz_need_save_callback, &save_context);
  50. Stream* fff_data_stream = flipper_format_get_raw_stream(subghz_txrx_get_fff_data(txrx));
  51. stream_clean(fff_data_stream);
  52. FuriString* preset_name = furi_string_alloc();
  53. FuriString* protocol_name = furi_string_alloc();
  54. bool is_raw = false;
  55. FuriString* temp_str;
  56. temp_str = furi_string_alloc();
  57. uint32_t temp_data32;
  58. uint32_t frequency = 0;
  59. FURI_LOG_I(TAG, "SUBGHZ: Action starting...");
  60. do {
  61. if(!flipper_format_file_open_existing(fff_data_file, file_name)) {
  62. FURI_LOG_E(TAG, "Error opening %s", file_name);
  63. ACTION_SET_ERROR("SUBGHZ: Error opening %s", file_name);
  64. break;
  65. }
  66. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  67. FURI_LOG_E(TAG, "Missing or incorrect header");
  68. ACTION_SET_ERROR("SUBGHZ: Missing or incorrect header");
  69. break;
  70. }
  71. if(((!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_KEY_FILE_TYPE)) ||
  72. (!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE))) &&
  73. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  74. } else {
  75. FURI_LOG_E(TAG, "Type or version mismatch");
  76. ACTION_SET_ERROR("SUBGHZ: Type or version mismatch");
  77. break;
  78. }
  79. SubGhzSetting* setting = subghz_txrx_get_setting(txrx);
  80. if(!flipper_format_read_uint32(fff_data_file, "Frequency", &frequency, 1)) {
  81. FURI_LOG_W(TAG, "Missing Frequency. Setting default frequency");
  82. frequency = subghz_setting_get_default_frequency(setting);
  83. } else if(!subghz_txrx_radio_device_is_frequecy_valid(txrx, frequency)) {
  84. FURI_LOG_E(TAG, "Frequency not supported on the chosen radio module");
  85. ACTION_SET_ERROR("SUBGHZ: Frequency not supported");
  86. break;
  87. }
  88. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  89. FURI_LOG_E(TAG, "Missing Preset");
  90. ACTION_SET_ERROR("SUBGHZ: Missing preset");
  91. break;
  92. }
  93. furi_string_set_str(
  94. temp_str, subghz_txrx_get_preset_name(txrx, furi_string_get_cstr(temp_str)));
  95. if(!strcmp(furi_string_get_cstr(temp_str), "")) {
  96. FURI_LOG_E(TAG, "Unknown preset");
  97. ACTION_SET_ERROR("SUBGHZ: Unknown preset");
  98. break;
  99. }
  100. if(!strcmp(furi_string_get_cstr(temp_str), "CUSTOM")) {
  101. subghz_setting_delete_custom_preset(setting, furi_string_get_cstr(temp_str));
  102. if(!subghz_setting_load_custom_preset(
  103. setting, furi_string_get_cstr(temp_str), fff_data_file)) {
  104. FURI_LOG_E(TAG, "Missing Custom preset");
  105. ACTION_SET_ERROR("SUBGHZ: Missing Custom preset");
  106. break;
  107. }
  108. }
  109. furi_string_set(preset_name, temp_str);
  110. size_t preset_index =
  111. subghz_setting_get_inx_preset_by_name(setting, furi_string_get_cstr(preset_name));
  112. subghz_txrx_set_preset(
  113. txrx,
  114. furi_string_get_cstr(preset_name),
  115. frequency,
  116. subghz_setting_get_preset_data(setting, preset_index),
  117. subghz_setting_get_preset_data_size(setting, preset_index));
  118. // Load Protocol
  119. if(!flipper_format_read_string(fff_data_file, "Protocol", protocol_name)) {
  120. FURI_LOG_E(TAG, "Missing protocol");
  121. ACTION_SET_ERROR("SUBGHZ: Missing protocol");
  122. break;
  123. }
  124. FlipperFormat* fff_data = subghz_txrx_get_fff_data(txrx);
  125. if(!strcmp(furi_string_get_cstr(protocol_name), "RAW")) {
  126. subghz_protocol_raw_gen_fff_data(
  127. fff_data, file_name, subghz_txrx_radio_device_get_name(txrx));
  128. is_raw = true;
  129. } else {
  130. stream_copy_full(
  131. flipper_format_get_raw_stream(fff_data_file),
  132. flipper_format_get_raw_stream(fff_data));
  133. }
  134. if(subghz_txrx_load_decoder_by_name_protocol(txrx, furi_string_get_cstr(protocol_name))) {
  135. SubGhzProtocolStatus status =
  136. subghz_protocol_decoder_base_deserialize(subghz_txrx_get_decoder(txrx), fff_data);
  137. if(status != SubGhzProtocolStatusOk) {
  138. break;
  139. }
  140. } else {
  141. FURI_LOG_E(TAG, "Protocol not found: %s", furi_string_get_cstr(protocol_name));
  142. break;
  143. }
  144. } while(false);
  145. flipper_format_file_close(fff_data_file);
  146. flipper_format_free(fff_data_file);
  147. if(subghz_txrx_tx_start(txrx, subghz_txrx_get_fff_data(txrx)) != SubGhzTxRxStartTxStateOk) {
  148. FURI_LOG_E(TAG, "Failed to start TX");
  149. }
  150. if(is_raw) {
  151. subghz_txrx_set_raw_file_encoder_worker_callback_end(
  152. txrx, action_subghz_raw_end_callback, furi_thread_get_current());
  153. furi_thread_flags_wait(0, FuriFlagWaitAll, FuriWaitForever);
  154. } else {
  155. furi_delay_ms(app->settings.subghz_duration);
  156. }
  157. FURI_LOG_I(TAG, "SUBGHZ: Action complete.");
  158. // This will call need_save_callback, if necessary
  159. subghz_txrx_stop(txrx);
  160. subghz_txrx_free(txrx);
  161. furi_string_free(preset_name);
  162. furi_string_free(protocol_name);
  163. furi_string_free(temp_str);
  164. }