subghz_scene_read_raw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. #include "../subghz_i.h"
  2. #include "../views/subghz_read_raw.h"
  3. #include <dolphin/dolphin.h>
  4. #include <lib/subghz/protocols/raw.h>
  5. #include <lib/toolbox/path.h>
  6. #define RAW_FILE_NAME "Raw_signal_"
  7. #define TAG "SubGhzSceneReadRAW"
  8. bool subghz_scene_read_raw_update_filename(SubGhz* subghz) {
  9. bool ret = false;
  10. //set the path to read the file
  11. FuriString* temp_str;
  12. temp_str = furi_string_alloc();
  13. do {
  14. FlipperFormat* fff_data = subghz_txrx_get_fff_data(subghz->txrx);
  15. if(!flipper_format_rewind(fff_data)) {
  16. FURI_LOG_E(TAG, "Rewind error");
  17. break;
  18. }
  19. if(!flipper_format_read_string(fff_data, "File_name", temp_str)) {
  20. FURI_LOG_E(TAG, "Missing File_name");
  21. break;
  22. }
  23. furi_string_set(subghz->file_path, temp_str);
  24. ret = true;
  25. } while(false);
  26. furi_string_free(temp_str);
  27. return ret;
  28. }
  29. static void subghz_scene_read_raw_update_statusbar(void* context) {
  30. furi_assert(context);
  31. SubGhz* subghz = context;
  32. FuriString* frequency_str = furi_string_alloc();
  33. FuriString* modulation_str = furi_string_alloc();
  34. subghz_txrx_get_frequency_and_modulation(subghz->txrx, frequency_str, modulation_str);
  35. subghz_read_raw_add_data_statusbar(
  36. subghz->subghz_read_raw,
  37. furi_string_get_cstr(frequency_str),
  38. furi_string_get_cstr(modulation_str));
  39. furi_string_free(frequency_str);
  40. furi_string_free(modulation_str);
  41. }
  42. void subghz_scene_read_raw_callback(SubGhzCustomEvent event, void* context) {
  43. furi_assert(context);
  44. SubGhz* subghz = context;
  45. view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
  46. }
  47. void subghz_scene_read_raw_callback_end_tx(void* context) {
  48. furi_assert(context);
  49. SubGhz* subghz = context;
  50. view_dispatcher_send_custom_event(
  51. subghz->view_dispatcher, SubGhzCustomEventViewReadRAWSendStop);
  52. }
  53. void subghz_scene_read_raw_on_enter(void* context) {
  54. SubGhz* subghz = context;
  55. FuriString* file_name = furi_string_alloc();
  56. float threshold_rssi = subghz_threshold_rssi_get(subghz->threshold_rssi);
  57. switch(subghz_rx_key_state_get(subghz)) {
  58. case SubGhzRxKeyStateBack:
  59. subghz_read_raw_set_status(
  60. subghz->subghz_read_raw, SubGhzReadRAWStatusIDLE, "", threshold_rssi);
  61. break;
  62. case SubGhzRxKeyStateRAWLoad:
  63. path_extract_filename(subghz->file_path, file_name, true);
  64. subghz_read_raw_set_status(
  65. subghz->subghz_read_raw,
  66. SubGhzReadRAWStatusLoadKeyTX,
  67. furi_string_get_cstr(file_name),
  68. threshold_rssi);
  69. break;
  70. case SubGhzRxKeyStateRAWSave:
  71. path_extract_filename(subghz->file_path, file_name, true);
  72. subghz_read_raw_set_status(
  73. subghz->subghz_read_raw,
  74. SubGhzReadRAWStatusSaveKey,
  75. furi_string_get_cstr(file_name),
  76. threshold_rssi);
  77. break;
  78. default:
  79. subghz_read_raw_set_status(
  80. subghz->subghz_read_raw, SubGhzReadRAWStatusStart, "", threshold_rssi);
  81. break;
  82. }
  83. if(subghz_rx_key_state_get(subghz) != SubGhzRxKeyStateBack) {
  84. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateIDLE);
  85. }
  86. furi_string_free(file_name);
  87. subghz_scene_read_raw_update_statusbar(subghz);
  88. //set callback view raw
  89. subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);
  90. furi_check(subghz_txrx_load_decoder_by_name_protocol(subghz->txrx, SUBGHZ_PROTOCOL_RAW_NAME));
  91. //set filter RAW feed
  92. subghz_txrx_receiver_set_filter(subghz->txrx, SubGhzProtocolFlag_RAW);
  93. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdReadRAW);
  94. }
  95. bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
  96. SubGhz* subghz = context;
  97. bool consumed = false;
  98. SubGhzProtocolDecoderRAW* decoder_raw =
  99. (SubGhzProtocolDecoderRAW*)subghz_txrx_get_decoder(subghz->txrx);
  100. if(event.type == SceneManagerEventTypeCustom) {
  101. switch(event.event) {
  102. case SubGhzCustomEventViewReadRAWBack:
  103. subghz_txrx_stop(subghz->txrx);
  104. //Stop save file
  105. subghz_protocol_raw_save_to_file_stop(decoder_raw);
  106. subghz->state_notifications = SubGhzNotificationStateIDLE;
  107. //needed save?
  108. if((subghz_rx_key_state_get(subghz) == SubGhzRxKeyStateAddKey) ||
  109. (subghz_rx_key_state_get(subghz) == SubGhzRxKeyStateBack)) {
  110. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateExit);
  111. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  112. } else {
  113. //Restore default setting
  114. subghz_set_default_preset(subghz);
  115. if(!scene_manager_search_and_switch_to_previous_scene(
  116. subghz->scene_manager, SubGhzSceneSaved)) {
  117. if(!scene_manager_search_and_switch_to_previous_scene(
  118. subghz->scene_manager, SubGhzSceneStart)) {
  119. scene_manager_stop(subghz->scene_manager);
  120. view_dispatcher_stop(subghz->view_dispatcher);
  121. }
  122. }
  123. }
  124. consumed = true;
  125. break;
  126. case SubGhzCustomEventViewReadRAWTXRXStop:
  127. subghz_txrx_stop(subghz->txrx);
  128. subghz->state_notifications = SubGhzNotificationStateIDLE;
  129. consumed = true;
  130. break;
  131. case SubGhzCustomEventViewReadRAWConfig:
  132. scene_manager_set_scene_state(
  133. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  134. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  135. consumed = true;
  136. break;
  137. case SubGhzCustomEventViewReadRAWErase:
  138. if(subghz_rx_key_state_get(subghz) == SubGhzRxKeyStateAddKey) {
  139. if(subghz_scene_read_raw_update_filename(subghz)) {
  140. furi_string_set(subghz->file_path_tmp, subghz->file_path);
  141. subghz_delete_file(subghz);
  142. }
  143. }
  144. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateIDLE);
  145. notification_message(subghz->notifications, &sequence_reset_rgb);
  146. consumed = true;
  147. break;
  148. case SubGhzCustomEventViewReadRAWMore:
  149. if(subghz_file_available(subghz)) {
  150. if(subghz_scene_read_raw_update_filename(subghz)) {
  151. scene_manager_set_scene_state(
  152. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  153. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateRAWLoad);
  154. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneMoreRAW);
  155. consumed = true;
  156. } else {
  157. furi_crash("SubGhz: RAW file name update error.");
  158. }
  159. } else {
  160. if(!scene_manager_search_and_switch_to_previous_scene(
  161. subghz->scene_manager, SubGhzSceneStart)) {
  162. scene_manager_stop(subghz->scene_manager);
  163. view_dispatcher_stop(subghz->view_dispatcher);
  164. }
  165. }
  166. break;
  167. case SubGhzCustomEventViewReadRAWSendStart:
  168. if(subghz_file_available(subghz) && subghz_scene_read_raw_update_filename(subghz)) {
  169. //start send
  170. subghz->state_notifications = SubGhzNotificationStateIDLE;
  171. if(!subghz_tx_start(subghz, subghz_txrx_get_fff_data(subghz->txrx))) {
  172. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateBack);
  173. subghz_read_raw_set_status(
  174. subghz->subghz_read_raw,
  175. SubGhzReadRAWStatusIDLE,
  176. "",
  177. subghz_threshold_rssi_get(subghz->threshold_rssi));
  178. } else {
  179. if(scene_manager_has_previous_scene(subghz->scene_manager, SubGhzSceneSaved) ||
  180. !scene_manager_has_previous_scene(subghz->scene_manager, SubGhzSceneStart)) {
  181. DOLPHIN_DEED(DolphinDeedSubGhzSend);
  182. }
  183. // set callback end tx
  184. subghz_txrx_set_raw_file_encoder_worker_callback_end(
  185. subghz->txrx, subghz_scene_read_raw_callback_end_tx, subghz);
  186. subghz->state_notifications = SubGhzNotificationStateTx;
  187. }
  188. } else {
  189. if(!scene_manager_search_and_switch_to_previous_scene(
  190. subghz->scene_manager, SubGhzSceneStart)) {
  191. scene_manager_stop(subghz->scene_manager);
  192. view_dispatcher_stop(subghz->view_dispatcher);
  193. }
  194. }
  195. consumed = true;
  196. break;
  197. case SubGhzCustomEventViewReadRAWSendStop:
  198. subghz->state_notifications = SubGhzNotificationStateIDLE;
  199. subghz_txrx_stop(subghz->txrx);
  200. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  201. consumed = true;
  202. break;
  203. case SubGhzCustomEventViewReadRAWIDLE:
  204. subghz_txrx_stop(subghz->txrx);
  205. size_t spl_count = subghz_protocol_raw_get_sample_write(decoder_raw);
  206. subghz_protocol_raw_save_to_file_stop(decoder_raw);
  207. FuriString* temp_str = furi_string_alloc();
  208. furi_string_printf(
  209. temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, RAW_FILE_NAME, SUBGHZ_APP_EXTENSION);
  210. subghz_protocol_raw_gen_fff_data(
  211. subghz_txrx_get_fff_data(subghz->txrx), furi_string_get_cstr(temp_str));
  212. furi_string_free(temp_str);
  213. if(spl_count > 0) {
  214. notification_message(subghz->notifications, &sequence_set_green_255);
  215. } else {
  216. notification_message(subghz->notifications, &sequence_reset_rgb);
  217. }
  218. subghz->state_notifications = SubGhzNotificationStateIDLE;
  219. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateAddKey);
  220. consumed = true;
  221. break;
  222. case SubGhzCustomEventViewReadRAWREC:
  223. if(subghz_rx_key_state_get(subghz) != SubGhzRxKeyStateIDLE) {
  224. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  225. } else {
  226. SubGhzRadioPreset preset = subghz_txrx_get_preset(subghz->txrx);
  227. if(subghz_protocol_raw_save_to_file_init(decoder_raw, RAW_FILE_NAME, &preset)) {
  228. DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
  229. subghz_txrx_rx_start(subghz->txrx);
  230. subghz->state_notifications = SubGhzNotificationStateRx;
  231. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateAddKey);
  232. } else {
  233. furi_string_set(subghz->error_str, "Function requires\nan SD card.");
  234. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  235. }
  236. }
  237. consumed = true;
  238. break;
  239. case SubGhzCustomEventViewReadRAWSave:
  240. if(subghz_file_available(subghz) && subghz_scene_read_raw_update_filename(subghz)) {
  241. scene_manager_set_scene_state(
  242. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSetRAW);
  243. subghz_rx_key_state_set(subghz, SubGhzRxKeyStateBack);
  244. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  245. } else {
  246. if(!scene_manager_search_and_switch_to_previous_scene(
  247. subghz->scene_manager, SubGhzSceneStart)) {
  248. scene_manager_stop(subghz->scene_manager);
  249. view_dispatcher_stop(subghz->view_dispatcher);
  250. }
  251. }
  252. consumed = true;
  253. break;
  254. default:
  255. break;
  256. }
  257. } else if(event.type == SceneManagerEventTypeTick) {
  258. switch(subghz->state_notifications) {
  259. case SubGhzNotificationStateRx:
  260. notification_message(subghz->notifications, &sequence_blink_cyan_10);
  261. subghz_read_raw_update_sample_write(
  262. subghz->subghz_read_raw, subghz_protocol_raw_get_sample_write(decoder_raw));
  263. SubGhzThresholdRssiData ret_rssi =
  264. subghz_threshold_get_rssi_data(subghz->threshold_rssi);
  265. subghz_read_raw_add_data_rssi(
  266. subghz->subghz_read_raw, ret_rssi.rssi, ret_rssi.is_above);
  267. subghz_protocol_raw_save_to_file_pause(decoder_raw, !ret_rssi.is_above);
  268. break;
  269. case SubGhzNotificationStateTx:
  270. notification_message(subghz->notifications, &sequence_blink_magenta_10);
  271. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  272. break;
  273. default:
  274. break;
  275. }
  276. }
  277. return consumed;
  278. }
  279. void subghz_scene_read_raw_on_exit(void* context) {
  280. SubGhz* subghz = context;
  281. //Stop CC1101
  282. subghz_txrx_stop(subghz->txrx);
  283. subghz->state_notifications = SubGhzNotificationStateIDLE;
  284. notification_message(subghz->notifications, &sequence_reset_rgb);
  285. //filter restoration
  286. subghz_txrx_receiver_set_filter(subghz->txrx, subghz->filter);
  287. }