action_subghz.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // Methods for Sub-GHz transmission
  2. #include <flipper_format/flipper_format_i.h>
  3. #include <path.h>
  4. #include <string.h>
  5. #include "helpers/subghz_txrx.h"
  6. #include <lib/subghz/blocks/custom_btn.h>
  7. #include <lib/subghz/protocols/raw.h>
  8. #include "action_i.h"
  9. #include "quac.h"
  10. #define SUBGHZ_DIR_PATH EXT_PATH("subghz/")
  11. typedef struct SubGhzNeedSaveContext {
  12. App* app;
  13. SubGhzTxRx* txrx;
  14. FuriString* file_path;
  15. } SubGhzNeedSaveContext;
  16. void action_subghz_need_save_callback(void* context) {
  17. FURI_LOG_I(TAG, "Saving udpated subghz signal");
  18. SubGhzNeedSaveContext* savectx = (SubGhzNeedSaveContext*)context;
  19. FlipperFormat* ff = subghz_txrx_get_fff_data(savectx->txrx);
  20. Stream* ff_stream = flipper_format_get_raw_stream(ff);
  21. flipper_format_delete_key(ff, "Repeat");
  22. //flipper_format_delete_key(ff, "Manufacture");
  23. do {
  24. if(!storage_simply_remove(
  25. savectx->app->storage, furi_string_get_cstr(savectx->file_path))) {
  26. FURI_LOG_E(TAG, "Failed to delete subghz file before re-save");
  27. break;
  28. }
  29. stream_seek(ff_stream, 0, StreamOffsetFromStart);
  30. stream_save_to_file(
  31. ff_stream,
  32. savectx->app->storage,
  33. furi_string_get_cstr(savectx->file_path),
  34. FSOM_CREATE_ALWAYS);
  35. if(storage_common_stat(
  36. savectx->app->storage, furi_string_get_cstr(savectx->file_path), NULL) != FSE_OK) {
  37. FURI_LOG_E(TAG, "Error verifying new subghz file after re-save");
  38. break;
  39. }
  40. } while(0);
  41. // Update original .sub file.
  42. //In case when rolling code was used in Quac we must update original .sub file with actual rolling code counter
  43. // Take file name from quac_app path
  44. FuriString* quac_filename = furi_string_alloc();
  45. furi_string_reset(quac_filename);
  46. path_extract_filename(savectx->file_path, quac_filename, false);
  47. FURI_LOG_I(TAG, "Extracted quac filename: %s", furi_string_get_cstr(quac_filename));
  48. //create new char string with full path (dir+filename) to original subghz folder
  49. char* full_subghz_file_name =
  50. malloc(1 + strlen(SUBGHZ_DIR_PATH) + strlen(furi_string_get_cstr(quac_filename)));
  51. strcpy(full_subghz_file_name, SUBGHZ_DIR_PATH);
  52. strcat(full_subghz_file_name, furi_string_get_cstr(quac_filename));
  53. FURI_LOG_I(TAG, "Full path to safe file: %s", full_subghz_file_name);
  54. //Save subghz file to original subghz location
  55. do {
  56. if(!storage_simply_remove(savectx->app->storage, full_subghz_file_name)) {
  57. FURI_LOG_E(
  58. TAG, "Failed to delete subghz file before re-save in original SUBGHZ location");
  59. break;
  60. }
  61. stream_seek(ff_stream, 0, StreamOffsetFromStart);
  62. stream_save_to_file(
  63. ff_stream, savectx->app->storage, full_subghz_file_name, FSOM_CREATE_ALWAYS);
  64. if(storage_common_stat(savectx->app->storage, full_subghz_file_name, NULL) != FSE_OK) {
  65. FURI_LOG_E(
  66. TAG, "Error verifying new subghz file after re-save in original SUBGHZ location");
  67. break;
  68. }
  69. } while(0);
  70. free(full_subghz_file_name);
  71. furi_string_free(quac_filename);
  72. }
  73. static void action_subghz_raw_end_callback(void* context) {
  74. FURI_LOG_I(TAG, "Stopping TX on RAW");
  75. furi_assert(context);
  76. FuriThread* thread = context;
  77. furi_thread_flags_set(furi_thread_get_id(thread), 0);
  78. }
  79. void action_subghz_tx(void* context, FuriString* action_path, FuriString* error) {
  80. App* app = context;
  81. const char* file_name = furi_string_get_cstr(action_path);
  82. FlipperFormat* fff_data_file = flipper_format_file_alloc(app->storage);
  83. SubGhzTxRx* txrx = subghz_txrx_alloc();
  84. SubGhzNeedSaveContext save_context = {app, txrx, action_path};
  85. subghz_txrx_set_need_save_callback(txrx, action_subghz_need_save_callback, &save_context);
  86. Stream* fff_data_stream = flipper_format_get_raw_stream(subghz_txrx_get_fff_data(txrx));
  87. stream_clean(fff_data_stream);
  88. FuriString* preset_name = furi_string_alloc();
  89. FuriString* protocol_name = furi_string_alloc();
  90. bool is_raw = false;
  91. subghz_custom_btns_reset();
  92. FuriString* temp_str;
  93. temp_str = furi_string_alloc();
  94. uint32_t temp_data32;
  95. uint32_t frequency = 0;
  96. FURI_LOG_I(TAG, "SUBGHZ: Action starting...");
  97. do {
  98. if(!flipper_format_file_open_existing(fff_data_file, file_name)) {
  99. FURI_LOG_E(TAG, "Error opening %s", file_name);
  100. ACTION_SET_ERROR("SUBGHZ: Error opening %s", file_name);
  101. break;
  102. }
  103. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  104. FURI_LOG_E(TAG, "Missing or incorrect header");
  105. ACTION_SET_ERROR("SUBGHZ: Missing or incorrect header");
  106. break;
  107. }
  108. if(((!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_KEY_FILE_TYPE)) ||
  109. (!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE))) &&
  110. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  111. } else {
  112. FURI_LOG_E(TAG, "Type or version mismatch");
  113. ACTION_SET_ERROR("SUBGHZ: Type or version mismatch");
  114. break;
  115. }
  116. SubGhzSetting* setting = subghz_txrx_get_setting(txrx);
  117. if(!flipper_format_read_uint32(fff_data_file, "Frequency", &frequency, 1)) {
  118. FURI_LOG_W(TAG, "Missing Frequency. Setting default frequency");
  119. frequency = subghz_setting_get_default_frequency(setting);
  120. } else if(!subghz_txrx_radio_device_is_frequecy_valid(txrx, frequency)) {
  121. FURI_LOG_E(TAG, "Frequency not supported on the chosen radio module");
  122. ACTION_SET_ERROR("SUBGHZ: Frequency not supported");
  123. break;
  124. }
  125. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  126. FURI_LOG_E(TAG, "Missing Preset");
  127. ACTION_SET_ERROR("SUBGHZ: Missing preset");
  128. break;
  129. }
  130. furi_string_set_str(
  131. temp_str, subghz_txrx_get_preset_name(txrx, furi_string_get_cstr(temp_str)));
  132. if(!strcmp(furi_string_get_cstr(temp_str), "")) {
  133. FURI_LOG_E(TAG, "Unknown preset");
  134. ACTION_SET_ERROR("SUBGHZ: Unknown preset");
  135. break;
  136. }
  137. if(!strcmp(furi_string_get_cstr(temp_str), "CUSTOM")) {
  138. subghz_setting_delete_custom_preset(setting, furi_string_get_cstr(temp_str));
  139. if(!subghz_setting_load_custom_preset(
  140. setting, furi_string_get_cstr(temp_str), fff_data_file)) {
  141. FURI_LOG_E(TAG, "Missing Custom preset");
  142. ACTION_SET_ERROR("SUBGHZ: Missing Custom preset");
  143. break;
  144. }
  145. }
  146. furi_string_set(preset_name, temp_str);
  147. size_t preset_index =
  148. subghz_setting_get_inx_preset_by_name(setting, furi_string_get_cstr(preset_name));
  149. subghz_txrx_set_preset(
  150. txrx,
  151. furi_string_get_cstr(preset_name),
  152. frequency,
  153. subghz_setting_get_preset_data(setting, preset_index),
  154. subghz_setting_get_preset_data_size(setting, preset_index));
  155. // Load Protocol
  156. if(!flipper_format_read_string(fff_data_file, "Protocol", protocol_name)) {
  157. FURI_LOG_E(TAG, "Missing protocol");
  158. ACTION_SET_ERROR("SUBGHZ: Missing protocol");
  159. break;
  160. }
  161. FlipperFormat* fff_data = subghz_txrx_get_fff_data(txrx);
  162. if(!strcmp(furi_string_get_cstr(protocol_name), "RAW")) {
  163. subghz_protocol_raw_gen_fff_data(
  164. fff_data, file_name, subghz_txrx_radio_device_get_name(txrx));
  165. is_raw = true;
  166. } else {
  167. stream_copy_full(
  168. flipper_format_get_raw_stream(fff_data_file),
  169. flipper_format_get_raw_stream(fff_data));
  170. }
  171. if(subghz_txrx_load_decoder_by_name_protocol(txrx, furi_string_get_cstr(protocol_name))) {
  172. SubGhzProtocolStatus status =
  173. subghz_protocol_decoder_base_deserialize(subghz_txrx_get_decoder(txrx), fff_data);
  174. if(status != SubGhzProtocolStatusOk) {
  175. break;
  176. }
  177. } else {
  178. FURI_LOG_E(TAG, "Protocol not found: %s", furi_string_get_cstr(protocol_name));
  179. break;
  180. }
  181. } while(false);
  182. flipper_format_file_close(fff_data_file);
  183. flipper_format_free(fff_data_file);
  184. if(is_raw) {
  185. subghz_txrx_set_raw_file_encoder_worker_callback_end(
  186. txrx, action_subghz_raw_end_callback, furi_thread_get_current());
  187. }
  188. if(subghz_txrx_tx_start(txrx, subghz_txrx_get_fff_data(txrx)) != SubGhzTxRxStartTxStateOk) {
  189. FURI_LOG_E(TAG, "Failed to start TX");
  190. }
  191. if(is_raw) {
  192. // Wait for action_subghz_raw_end_callback() to notify us
  193. furi_thread_flags_wait(0, FuriFlagWaitAll, FuriWaitForever);
  194. } else {
  195. // TODO: Should this be based on a Setting?
  196. furi_delay_ms(1500);
  197. }
  198. FURI_LOG_I(TAG, "SUBGHZ: Action complete.");
  199. // This will call need_save_callback, if necessary
  200. subghz_txrx_stop(txrx);
  201. subghz_custom_btns_reset();
  202. subghz_txrx_free(txrx);
  203. furi_string_free(preset_name);
  204. furi_string_free(protocol_name);
  205. furi_string_free(temp_str);
  206. }