action_subghz.c 7.3 KB

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