subghz_i.c 8.5 KB

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