subghz_i.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "subghz_i.h"
  2. //#include "assets_icons.h"
  3. #include "subghz/types.h"
  4. #include <math.h>
  5. #include <furi.h>
  6. #include <furi_hal.h>
  7. #include <input/input.h>
  8. #include <gui/elements.h>
  9. #include <notification/notification.h>
  10. #include <notification/notification_messages.h>
  11. #include <flipper_format/flipper_format.h>
  12. //#include "views/receiver.h"
  13. #include <flipper_format/flipper_format_i.h>
  14. #include <lib/toolbox/stream/stream.h>
  15. #include <lib/subghz/protocols/raw.h>
  16. //#define TAG "SubGhz"
  17. /*
  18. void subghz_set_default_preset(SubGhz* subghz) {
  19. furi_assert(subghz);
  20. subghz_txrx_set_preset(
  21. subghz->txrx,
  22. "AM650",
  23. subghz_setting_get_default_frequency(subghz_txrx_get_setting(subghz->txrx)),
  24. NULL,
  25. 0);
  26. }*/
  27. /*bool subghz_tx_start(SubGhz* subghz, FlipperFormat* flipper_format) {
  28. switch(subghz_txrx_tx_start(subghz->txrx, flipper_format)) {
  29. case SubGhzTxRxStartTxStateErrorParserOthers:
  30. dialog_message_show_storage_error(
  31. subghz->dialogs, "Error in protocol\nparameters\ndescription");
  32. break;
  33. case SubGhzTxRxStartTxStateErrorOnlyRx:
  34. FURI_LOG_D(TAG, 'Cannot send, only RX possible');
  35. break;
  36. default:
  37. return true;
  38. break;
  39. }
  40. return false;
  41. }*/
  42. bool subghz_key_load(SubGhz* subghz, const char* file_path) { //, bool show_dialog) {
  43. furi_assert(subghz);
  44. furi_assert(file_path);
  45. Storage* storage = furi_record_open(RECORD_STORAGE);
  46. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  47. Stream* fff_data_stream =
  48. flipper_format_get_raw_stream(subghz_txrx_get_fff_data(subghz->txrx));
  49. SubGhzLoadKeyState load_key_state = SubGhzLoadKeyStateParseErr;
  50. FuriString* temp_str = furi_string_alloc();
  51. uint32_t temp_data32;
  52. do {
  53. stream_clean(fff_data_stream);
  54. if(!flipper_format_file_open_existing(fff_data_file, file_path)) {
  55. FURI_LOG_E(TAG, "Error open file %s", file_path);
  56. break;
  57. }
  58. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  59. FURI_LOG_E(TAG, "Missing or incorrect header");
  60. break;
  61. }
  62. if(((!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_KEY_FILE_TYPE)) ||
  63. (!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE))) &&
  64. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  65. } else {
  66. FURI_LOG_E(TAG, "Type or version mismatch");
  67. break;
  68. }
  69. //Load frequency
  70. if(!flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) {
  71. FURI_LOG_E(TAG, "Missing Frequency");
  72. break;
  73. }
  74. if(!subghz_txrx_radio_device_is_frequecy_valid(subghz->txrx, temp_data32)) {
  75. FURI_LOG_E(TAG, "Frequency not supported");
  76. break;
  77. }
  78. //Load preset
  79. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  80. FURI_LOG_E(TAG, "Missing Preset");
  81. break;
  82. }
  83. furi_string_set_str(
  84. temp_str, subghz_txrx_get_preset_name(subghz->txrx, furi_string_get_cstr(temp_str)));
  85. if(!strcmp(furi_string_get_cstr(temp_str), "")) {
  86. break;
  87. }
  88. SubGhzSetting* setting = subghz_txrx_get_setting(subghz->txrx);
  89. if(!strcmp(furi_string_get_cstr(temp_str), "CUSTOM")) {
  90. //TODO FL-3551: add Custom_preset_module
  91. //delete preset if it already exists
  92. subghz_setting_delete_custom_preset(setting, furi_string_get_cstr(temp_str));
  93. //load custom preset from file
  94. if(!subghz_setting_load_custom_preset(
  95. setting, furi_string_get_cstr(temp_str), fff_data_file)) {
  96. FURI_LOG_E(TAG, "Missing Custom preset");
  97. break;
  98. }
  99. }
  100. size_t preset_index =
  101. subghz_setting_get_inx_preset_by_name(setting, furi_string_get_cstr(temp_str));
  102. subghz_txrx_set_preset(
  103. subghz->txrx,
  104. furi_string_get_cstr(temp_str),
  105. temp_data32,
  106. subghz_setting_get_preset_data(setting, preset_index),
  107. subghz_setting_get_preset_data_size(setting, preset_index));
  108. //Load protocol
  109. if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) {
  110. FURI_LOG_E(TAG, "Missing Protocol");
  111. break;
  112. }
  113. FlipperFormat* fff_data = subghz_txrx_get_fff_data(subghz->txrx);
  114. if(!strcmp(furi_string_get_cstr(temp_str), "RAW")) {
  115. //if RAW
  116. subghz->load_type_file = SubGhzLoadTypeFileRaw;
  117. subghz_protocol_raw_gen_fff_data(
  118. fff_data, file_path, subghz_txrx_radio_device_get_name(subghz->txrx));
  119. } else {
  120. subghz->load_type_file = SubGhzLoadTypeFileKey;
  121. stream_copy_full(
  122. flipper_format_get_raw_stream(fff_data_file),
  123. flipper_format_get_raw_stream(fff_data));
  124. }
  125. if(subghz_txrx_load_decoder_by_name_protocol(
  126. subghz->txrx, furi_string_get_cstr(temp_str))) {
  127. SubGhzProtocolStatus status = subghz_protocol_decoder_base_deserialize(
  128. subghz_txrx_get_decoder(subghz->txrx), fff_data);
  129. if(status != SubGhzProtocolStatusOk) {
  130. load_key_state = SubGhzLoadKeyStateProtocolDescriptionErr;
  131. break;
  132. }
  133. } else {
  134. FURI_LOG_E(TAG, "Protocol not found");
  135. break;
  136. }
  137. load_key_state = SubGhzLoadKeyStateOK;
  138. } while(0);
  139. furi_string_free(temp_str);
  140. flipper_format_free(fff_data_file);
  141. furi_record_close(RECORD_STORAGE);
  142. switch(load_key_state) {
  143. case SubGhzLoadKeyStateParseErr:
  144. //if(show_dialog) {
  145. // dialog_message_show_storage_error(subghz->dialogs, "Cannot parse\nfile");
  146. //}
  147. return false;
  148. case SubGhzLoadKeyStateProtocolDescriptionErr:
  149. //if(show_dialog) {
  150. // dialog_message_show_storage_error(
  151. // subghz->dialogs, "Error in protocol\nparameters\ndescription");
  152. //}
  153. return false;
  154. case SubGhzLoadKeyStateOK:
  155. return true;
  156. default:
  157. furi_crash("SubGhz: Unknown load_key_state.");
  158. return false;
  159. }
  160. return false;
  161. }
  162. /*SubGhzLoadTypeFile subghz_get_load_type_file(SubGhz* subghz) {
  163. furi_assert(subghz);
  164. return subghz->load_type_file;
  165. }*/
  166. bool subghz_load_protocol_from_file(SubGhz* subghz) {
  167. furi_assert(subghz);
  168. //FuriString* file_path = furi_string_alloc();
  169. bool res = false;
  170. /*DialogsFileBrowserOptions browser_options;
  171. dialog_file_browser_set_basic_options(
  172. &browser_options, SUBGHZ_APP_FILENAME_EXTENSION, &I_sub1_10px);
  173. browser_options.base_path = SUBGHZ_APP_FOLDER;
  174. // Input events and views are managed by file_select
  175. bool res = dialog_file_browser_show(
  176. subghz->dialogs, subghz->file_path, subghz->file_path, &browser_options);
  177. if(res) {*/
  178. //res = subghz_key_load(subghz, furi_string_get_cstr(subghz->file_path), true);
  179. res = subghz_key_load(subghz, MEAL_PAGER_TMP_FILE); //, true);
  180. //}
  181. //furi_string_free(file_path);
  182. return res;
  183. }
  184. /*bool subghz_file_available(SubGhz* subghz) {
  185. furi_assert(subghz);
  186. bool ret = true;
  187. Storage* storage = furi_record_open(RECORD_STORAGE);
  188. FS_Error fs_result =
  189. storage_common_stat(storage, furi_string_get_cstr(subghz->file_path), NULL);
  190. if(fs_result != FSE_OK) {
  191. dialog_message_show_storage_error(subghz->dialogs, "File not available\n file/directory");
  192. ret = false;
  193. }
  194. furi_record_close(RECORD_STORAGE);
  195. return ret;
  196. }*/
  197. /*bool subghz_path_is_file(FuriString* path) {
  198. return furi_string_end_with(path, SUBGHZ_APP_FILENAME_EXTENSION);
  199. }*/
  200. /*void subghz_lock(SubGhz* subghz) {
  201. furi_assert(subghz);
  202. subghz->lock = SubGhzLockOn;
  203. }*/
  204. /*void subghz_unlock(SubGhz* subghz) {
  205. furi_assert(subghz);
  206. subghz->lock = SubGhzLockOff;
  207. }*/
  208. /*bool subghz_is_locked(SubGhz* subghz) {
  209. furi_assert(subghz);
  210. return (subghz->lock == SubGhzLockOn);
  211. }*/
  212. /*void subghz_rx_key_state_set(SubGhz* subghz, SubGhzRxKeyState state) {
  213. furi_assert(subghz);
  214. subghz->rx_key_state = state;
  215. }*/
  216. /*SubGhzRxKeyState subghz_rx_key_state_get(SubGhz* subghz) {
  217. furi_assert(subghz);
  218. return subghz->rx_key_state;
  219. }*/