subghz_i.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. void subghz_set_default_preset(SubGhz* subghz) {
  18. furi_assert(subghz);
  19. subghz_txrx_set_preset(
  20. subghz->txrx,
  21. "AM650",
  22. subghz_setting_get_default_frequency(subghz_txrx_get_setting(subghz->txrx)),
  23. NULL,
  24. 0);
  25. }
  26. void subghz_blink_start(SubGhz* subghz) {
  27. furi_assert(subghz);
  28. notification_message(subghz->notifications, &sequence_blink_stop);
  29. notification_message(subghz->notifications, &sequence_blink_start_magenta);
  30. }
  31. void subghz_blink_stop(SubGhz* subghz) {
  32. furi_assert(subghz);
  33. notification_message(subghz->notifications, &sequence_blink_stop);
  34. }
  35. bool subghz_tx_start(SubGhz* subghz, FlipperFormat* flipper_format) {
  36. switch(subghz_txrx_tx_start(subghz->txrx, flipper_format)) {
  37. case SubGhzTxRxStartTxStateErrorParserOthers:
  38. dialog_message_show_storage_error(
  39. subghz->dialogs, "Error in protocol\nparameters\ndescription");
  40. break;
  41. case SubGhzTxRxStartTxStateErrorOnlyRx:
  42. subghz_dialog_message_show_only_rx(subghz);
  43. break;
  44. default:
  45. return true;
  46. break;
  47. }
  48. return false;
  49. }
  50. void subghz_dialog_message_show_only_rx(SubGhz* subghz) {
  51. DialogsApp* dialogs = subghz->dialogs;
  52. DialogMessage* message = dialog_message_alloc();
  53. const char* header_text = "Transmission is blocked";
  54. const char* message_text = "Transmission on\nthis frequency is\nrestricted in\nyour region";
  55. if(!furi_hal_region_is_provisioned()) {
  56. header_text = "Firmware update needed";
  57. message_text = "Please update\nfirmware before\nusing this feature\nflipp.dev/upd";
  58. }
  59. dialog_message_set_header(message, header_text, 63, 3, AlignCenter, AlignTop);
  60. dialog_message_set_text(message, message_text, 0, 17, AlignLeft, AlignTop);
  61. dialog_message_set_icon(message, &I_DolphinCommon_56x48, 72, 17);
  62. dialog_message_show(dialogs, message);
  63. dialog_message_free(message);
  64. }
  65. bool subghz_key_load(SubGhz* subghz, const char* file_path, bool show_dialog) {
  66. furi_assert(subghz);
  67. furi_assert(file_path);
  68. Storage* storage = furi_record_open(RECORD_STORAGE);
  69. FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
  70. Stream* fff_data_stream =
  71. flipper_format_get_raw_stream(subghz_txrx_get_fff_data(subghz->txrx));
  72. SubGhzLoadKeyState load_key_state = SubGhzLoadKeyStateParseErr;
  73. FuriString* temp_str = furi_string_alloc();
  74. uint32_t temp_data32;
  75. do {
  76. stream_clean(fff_data_stream);
  77. if(!flipper_format_file_open_existing(fff_data_file, file_path)) {
  78. FURI_LOG_E(TAG, "Error open file %s", file_path);
  79. break;
  80. }
  81. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  82. FURI_LOG_E(TAG, "Missing or incorrect header");
  83. break;
  84. }
  85. if(((!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_KEY_FILE_TYPE)) ||
  86. (!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE))) &&
  87. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  88. } else {
  89. FURI_LOG_E(TAG, "Type or version mismatch");
  90. break;
  91. }
  92. //Load frequency
  93. if(!flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) {
  94. FURI_LOG_E(TAG, "Missing Frequency");
  95. break;
  96. }
  97. if(!furi_hal_subghz_is_frequency_valid(temp_data32)) {
  98. FURI_LOG_E(TAG, "Frequency not supported");
  99. break;
  100. }
  101. //Load preset
  102. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  103. FURI_LOG_E(TAG, "Missing Preset");
  104. break;
  105. }
  106. furi_string_set_str(
  107. temp_str, subghz_txrx_get_preset_name(subghz->txrx, furi_string_get_cstr(temp_str)));
  108. if(!strcmp(furi_string_get_cstr(temp_str), "")) {
  109. break;
  110. }
  111. SubGhzSetting* setting = subghz_txrx_get_setting(subghz->txrx);
  112. if(!strcmp(furi_string_get_cstr(temp_str), "CUSTOM")) {
  113. //Todo add Custom_preset_module
  114. //delete preset if it already exists
  115. subghz_setting_delete_custom_preset(setting, furi_string_get_cstr(temp_str));
  116. //load custom preset from file
  117. if(!subghz_setting_load_custom_preset(
  118. setting, furi_string_get_cstr(temp_str), fff_data_file)) {
  119. FURI_LOG_E(TAG, "Missing Custom preset");
  120. break;
  121. }
  122. }
  123. size_t preset_index =
  124. subghz_setting_get_inx_preset_by_name(setting, furi_string_get_cstr(temp_str));
  125. subghz_txrx_set_preset(
  126. subghz->txrx,
  127. furi_string_get_cstr(temp_str),
  128. temp_data32,
  129. subghz_setting_get_preset_data(setting, preset_index),
  130. subghz_setting_get_preset_data_size(setting, preset_index));
  131. //Load protocol
  132. if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) {
  133. FURI_LOG_E(TAG, "Missing Protocol");
  134. break;
  135. }
  136. FlipperFormat* fff_data = subghz_txrx_get_fff_data(subghz->txrx);
  137. if(!strcmp(furi_string_get_cstr(temp_str), "RAW")) {
  138. //if RAW
  139. subghz->load_type_file = SubGhzLoadTypeFileRaw;
  140. subghz_protocol_raw_gen_fff_data(fff_data, file_path);
  141. } else {
  142. subghz->load_type_file = SubGhzLoadTypeFileKey;
  143. stream_copy_full(
  144. flipper_format_get_raw_stream(fff_data_file),
  145. flipper_format_get_raw_stream(fff_data));
  146. }
  147. if(subghz_txrx_load_decoder_by_name_protocol(
  148. subghz->txrx, furi_string_get_cstr(temp_str))) {
  149. SubGhzProtocolStatus status = subghz_protocol_decoder_base_deserialize(
  150. subghz_txrx_get_decoder(subghz->txrx), fff_data);
  151. if(status != SubGhzProtocolStatusOk) {
  152. load_key_state = SubGhzLoadKeyStateProtocolDescriptionErr;
  153. break;
  154. }
  155. } else {
  156. FURI_LOG_E(TAG, "Protocol not found");
  157. break;
  158. }
  159. load_key_state = SubGhzLoadKeyStateOK;
  160. } while(0);
  161. furi_string_free(temp_str);
  162. flipper_format_free(fff_data_file);
  163. furi_record_close(RECORD_STORAGE);
  164. switch(load_key_state) {
  165. case SubGhzLoadKeyStateParseErr:
  166. if(show_dialog) {
  167. dialog_message_show_storage_error(subghz->dialogs, "Cannot parse\nfile");
  168. }
  169. return false;
  170. case SubGhzLoadKeyStateProtocolDescriptionErr:
  171. if(show_dialog) {
  172. dialog_message_show_storage_error(
  173. subghz->dialogs, "Error in protocol\nparameters\ndescription");
  174. }
  175. return false;
  176. case SubGhzLoadKeyStateOK:
  177. return true;
  178. default:
  179. furi_crash("SubGhz: Unknown load_key_state.");
  180. return false;
  181. }
  182. }
  183. SubGhzLoadTypeFile subghz_get_load_type_file(SubGhz* subghz) {
  184. furi_assert(subghz);
  185. return subghz->load_type_file;
  186. }
  187. bool subghz_get_next_name_file(SubGhz* subghz, uint8_t max_len) {
  188. furi_assert(subghz);
  189. Storage* storage = furi_record_open(RECORD_STORAGE);
  190. FuriString* temp_str = furi_string_alloc();
  191. FuriString* file_name = furi_string_alloc();
  192. FuriString* file_path = furi_string_alloc();
  193. bool res = false;
  194. if(subghz_path_is_file(subghz->file_path)) {
  195. //get the name of the next free file
  196. path_extract_filename(subghz->file_path, file_name, true);
  197. path_extract_dirname(furi_string_get_cstr(subghz->file_path), file_path);
  198. storage_get_next_filename(
  199. storage,
  200. furi_string_get_cstr(file_path),
  201. furi_string_get_cstr(file_name),
  202. SUBGHZ_APP_EXTENSION,
  203. file_name,
  204. max_len);
  205. furi_string_printf(
  206. temp_str,
  207. "%s/%s%s",
  208. furi_string_get_cstr(file_path),
  209. furi_string_get_cstr(file_name),
  210. SUBGHZ_APP_EXTENSION);
  211. furi_string_set(subghz->file_path, temp_str);
  212. res = true;
  213. }
  214. furi_string_free(temp_str);
  215. furi_string_free(file_path);
  216. furi_string_free(file_name);
  217. furi_record_close(RECORD_STORAGE);
  218. return res;
  219. }
  220. bool subghz_save_protocol_to_file(
  221. SubGhz* subghz,
  222. FlipperFormat* flipper_format,
  223. const char* dev_file_name) {
  224. furi_assert(subghz);
  225. furi_assert(flipper_format);
  226. furi_assert(dev_file_name);
  227. Storage* storage = furi_record_open(RECORD_STORAGE);
  228. Stream* flipper_format_stream = flipper_format_get_raw_stream(flipper_format);
  229. bool saved = false;
  230. FuriString* file_dir = furi_string_alloc();
  231. path_extract_dirname(dev_file_name, file_dir);
  232. do {
  233. //removing additional fields
  234. flipper_format_delete_key(flipper_format, "Repeat");
  235. flipper_format_delete_key(flipper_format, "Manufacture");
  236. // Create subghz folder directory if necessary
  237. if(!storage_simply_mkdir(storage, furi_string_get_cstr(file_dir))) {
  238. dialog_message_show_storage_error(subghz->dialogs, "Cannot create\nfolder");
  239. break;
  240. }
  241. if(!storage_simply_remove(storage, dev_file_name)) {
  242. break;
  243. }
  244. //ToDo check Write
  245. stream_seek(flipper_format_stream, 0, StreamOffsetFromStart);
  246. stream_save_to_file(flipper_format_stream, storage, dev_file_name, FSOM_CREATE_ALWAYS);
  247. saved = true;
  248. } while(0);
  249. furi_string_free(file_dir);
  250. furi_record_close(RECORD_STORAGE);
  251. return saved;
  252. }
  253. void subghz_save_to_file(void* context) {
  254. furi_assert(context);
  255. SubGhz* subghz = context;
  256. if(subghz_path_is_file(subghz->file_path)) {
  257. subghz_save_protocol_to_file(
  258. subghz,
  259. subghz_txrx_get_fff_data(subghz->txrx),
  260. furi_string_get_cstr(subghz->file_path));
  261. }
  262. }
  263. bool subghz_load_protocol_from_file(SubGhz* subghz) {
  264. furi_assert(subghz);
  265. FuriString* file_path = furi_string_alloc();
  266. DialogsFileBrowserOptions browser_options;
  267. dialog_file_browser_set_basic_options(&browser_options, SUBGHZ_APP_EXTENSION, &I_sub1_10px);
  268. browser_options.base_path = SUBGHZ_APP_FOLDER;
  269. // Input events and views are managed by file_select
  270. bool res = dialog_file_browser_show(
  271. subghz->dialogs, subghz->file_path, subghz->file_path, &browser_options);
  272. if(res) {
  273. res = subghz_key_load(subghz, furi_string_get_cstr(subghz->file_path), true);
  274. }
  275. furi_string_free(file_path);
  276. return res;
  277. }
  278. bool subghz_rename_file(SubGhz* subghz) {
  279. furi_assert(subghz);
  280. bool ret = true;
  281. Storage* storage = furi_record_open(RECORD_STORAGE);
  282. if(furi_string_cmp(subghz->file_path_tmp, subghz->file_path)) {
  283. FS_Error fs_result = storage_common_rename(
  284. storage,
  285. furi_string_get_cstr(subghz->file_path_tmp),
  286. furi_string_get_cstr(subghz->file_path));
  287. if(fs_result != FSE_OK) {
  288. dialog_message_show_storage_error(subghz->dialogs, "Cannot rename\n file/directory");
  289. ret = false;
  290. }
  291. }
  292. furi_record_close(RECORD_STORAGE);
  293. return ret;
  294. }
  295. bool subghz_file_available(SubGhz* subghz) {
  296. furi_assert(subghz);
  297. bool ret = true;
  298. Storage* storage = furi_record_open(RECORD_STORAGE);
  299. FS_Error fs_result =
  300. storage_common_stat(storage, furi_string_get_cstr(subghz->file_path), NULL);
  301. if(fs_result != FSE_OK) {
  302. dialog_message_show_storage_error(subghz->dialogs, "File not available\n file/directory");
  303. ret = false;
  304. }
  305. furi_record_close(RECORD_STORAGE);
  306. return ret;
  307. }
  308. bool subghz_delete_file(SubGhz* subghz) {
  309. furi_assert(subghz);
  310. Storage* storage = furi_record_open(RECORD_STORAGE);
  311. bool result = storage_simply_remove(storage, furi_string_get_cstr(subghz->file_path_tmp));
  312. furi_record_close(RECORD_STORAGE);
  313. subghz_file_name_clear(subghz);
  314. return result;
  315. }
  316. void subghz_file_name_clear(SubGhz* subghz) {
  317. furi_assert(subghz);
  318. furi_string_set(subghz->file_path, SUBGHZ_APP_FOLDER);
  319. furi_string_reset(subghz->file_path_tmp);
  320. }
  321. bool subghz_path_is_file(FuriString* path) {
  322. return furi_string_end_with(path, SUBGHZ_APP_EXTENSION);
  323. }
  324. void subghz_lock(SubGhz* subghz) {
  325. furi_assert(subghz);
  326. subghz->lock = SubGhzLockOn;
  327. }
  328. void subghz_unlock(SubGhz* subghz) {
  329. furi_assert(subghz);
  330. subghz->lock = SubGhzLockOff;
  331. }
  332. bool subghz_is_locked(SubGhz* subghz) {
  333. furi_assert(subghz);
  334. return (subghz->lock == SubGhzLockOn);
  335. }
  336. void subghz_rx_key_state_set(SubGhz* subghz, SubGhzRxKeyState state) {
  337. furi_assert(subghz);
  338. subghz->rx_key_state = state;
  339. }
  340. SubGhzRxKeyState subghz_rx_key_state_get(SubGhz* subghz) {
  341. furi_assert(subghz);
  342. return subghz->rx_key_state;
  343. }