subghz_scene_read_raw.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #include <float_tools.h>
  7. #define RAW_FILE_NAME "Raw_signal_"
  8. #define TAG "SubGhzSceneReadRAW"
  9. #define RAW_THRESHOLD_RSSI_LOW_COUNT 10
  10. bool subghz_scene_read_raw_update_filename(SubGhz* subghz) {
  11. bool ret = false;
  12. //set the path to read the file
  13. FuriString* temp_str;
  14. temp_str = furi_string_alloc();
  15. do {
  16. if(!flipper_format_rewind(subghz->txrx->fff_data)) {
  17. FURI_LOG_E(TAG, "Rewind error");
  18. break;
  19. }
  20. if(!flipper_format_read_string(subghz->txrx->fff_data, "File_name", temp_str)) {
  21. FURI_LOG_E(TAG, "Missing File_name");
  22. break;
  23. }
  24. furi_string_set(subghz->file_path, temp_str);
  25. ret = true;
  26. } while(false);
  27. furi_string_free(temp_str);
  28. return ret;
  29. }
  30. static void subghz_scene_read_raw_update_statusbar(void* context) {
  31. furi_assert(context);
  32. SubGhz* subghz = context;
  33. FuriString* frequency_str;
  34. FuriString* modulation_str;
  35. frequency_str = furi_string_alloc();
  36. modulation_str = furi_string_alloc();
  37. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  38. subghz_read_raw_add_data_statusbar(
  39. subghz->subghz_read_raw,
  40. furi_string_get_cstr(frequency_str),
  41. furi_string_get_cstr(modulation_str));
  42. furi_string_free(frequency_str);
  43. furi_string_free(modulation_str);
  44. }
  45. void subghz_scene_read_raw_callback(SubGhzCustomEvent event, void* context) {
  46. furi_assert(context);
  47. SubGhz* subghz = context;
  48. view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
  49. }
  50. void subghz_scene_read_raw_callback_end_tx(void* context) {
  51. furi_assert(context);
  52. SubGhz* subghz = context;
  53. view_dispatcher_send_custom_event(
  54. subghz->view_dispatcher, SubGhzCustomEventViewReadRAWSendStop);
  55. }
  56. void subghz_scene_read_raw_on_enter(void* context) {
  57. SubGhz* subghz = context;
  58. FuriString* file_name;
  59. file_name = furi_string_alloc();
  60. switch(subghz->txrx->rx_key_state) {
  61. case SubGhzRxKeyStateBack:
  62. subghz_read_raw_set_status(
  63. subghz->subghz_read_raw, SubGhzReadRAWStatusIDLE, "", subghz->txrx->raw_threshold_rssi);
  64. break;
  65. case SubGhzRxKeyStateRAWLoad:
  66. path_extract_filename(subghz->file_path, file_name, true);
  67. subghz_read_raw_set_status(
  68. subghz->subghz_read_raw,
  69. SubGhzReadRAWStatusLoadKeyTX,
  70. furi_string_get_cstr(file_name),
  71. subghz->txrx->raw_threshold_rssi);
  72. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  73. break;
  74. case SubGhzRxKeyStateRAWSave:
  75. path_extract_filename(subghz->file_path, file_name, true);
  76. subghz_read_raw_set_status(
  77. subghz->subghz_read_raw,
  78. SubGhzReadRAWStatusSaveKey,
  79. furi_string_get_cstr(file_name),
  80. subghz->txrx->raw_threshold_rssi);
  81. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  82. break;
  83. default:
  84. subghz_read_raw_set_status(
  85. subghz->subghz_read_raw,
  86. SubGhzReadRAWStatusStart,
  87. "",
  88. subghz->txrx->raw_threshold_rssi);
  89. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  90. break;
  91. }
  92. furi_string_free(file_name);
  93. subghz_scene_read_raw_update_statusbar(subghz);
  94. //set callback view raw
  95. subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);
  96. subghz->txrx->decoder_result = subghz_receiver_search_decoder_base_by_name(
  97. subghz->txrx->receiver, SUBGHZ_PROTOCOL_RAW_NAME);
  98. furi_assert(subghz->txrx->decoder_result);
  99. //set filter RAW feed
  100. subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_RAW);
  101. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdReadRAW);
  102. }
  103. bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
  104. SubGhz* subghz = context;
  105. bool consumed = false;
  106. if(event.type == SceneManagerEventTypeCustom) {
  107. switch(event.event) {
  108. case SubGhzCustomEventViewReadRAWBack:
  109. //Stop TX
  110. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  111. subghz_tx_stop(subghz);
  112. subghz_sleep(subghz);
  113. }
  114. //Stop RX
  115. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  116. subghz_rx_end(subghz);
  117. subghz_sleep(subghz);
  118. };
  119. //Stop save file
  120. subghz_protocol_raw_save_to_file_stop(
  121. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  122. subghz->state_notifications = SubGhzNotificationStateIDLE;
  123. //needed save?
  124. if((subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) ||
  125. (subghz->txrx->rx_key_state == SubGhzRxKeyStateBack)) {
  126. subghz->txrx->rx_key_state = SubGhzRxKeyStateExit;
  127. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  128. } else {
  129. //Restore default setting
  130. subghz_preset_init(
  131. subghz,
  132. "AM650",
  133. subghz_setting_get_default_frequency(subghz->setting),
  134. NULL,
  135. 0);
  136. if(!scene_manager_search_and_switch_to_previous_scene(
  137. subghz->scene_manager, SubGhzSceneSaved)) {
  138. if(!scene_manager_search_and_switch_to_previous_scene(
  139. subghz->scene_manager, SubGhzSceneStart)) {
  140. scene_manager_stop(subghz->scene_manager);
  141. view_dispatcher_stop(subghz->view_dispatcher);
  142. }
  143. }
  144. }
  145. consumed = true;
  146. break;
  147. case SubGhzCustomEventViewReadRAWTXRXStop:
  148. //Stop TX
  149. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  150. subghz_tx_stop(subghz);
  151. subghz_sleep(subghz);
  152. }
  153. //Stop RX
  154. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  155. subghz_rx_end(subghz);
  156. subghz_sleep(subghz);
  157. };
  158. subghz->state_notifications = SubGhzNotificationStateIDLE;
  159. consumed = true;
  160. break;
  161. case SubGhzCustomEventViewReadRAWConfig:
  162. scene_manager_set_scene_state(
  163. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  164. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  165. consumed = true;
  166. break;
  167. case SubGhzCustomEventViewReadRAWErase:
  168. if(subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) {
  169. if(subghz_scene_read_raw_update_filename(subghz)) {
  170. furi_string_set(subghz->file_path_tmp, subghz->file_path);
  171. subghz_delete_file(subghz);
  172. }
  173. }
  174. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  175. notification_message(subghz->notifications, &sequence_reset_rgb);
  176. consumed = true;
  177. break;
  178. case SubGhzCustomEventViewReadRAWMore:
  179. if(subghz_file_available(subghz)) {
  180. if(subghz_scene_read_raw_update_filename(subghz)) {
  181. scene_manager_set_scene_state(
  182. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  183. subghz->txrx->rx_key_state = SubGhzRxKeyStateRAWLoad;
  184. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneMoreRAW);
  185. consumed = true;
  186. } else {
  187. furi_crash("SubGhz: RAW file name update error.");
  188. }
  189. } else {
  190. if(!scene_manager_search_and_switch_to_previous_scene(
  191. subghz->scene_manager, SubGhzSceneStart)) {
  192. scene_manager_stop(subghz->scene_manager);
  193. view_dispatcher_stop(subghz->view_dispatcher);
  194. }
  195. }
  196. break;
  197. case SubGhzCustomEventViewReadRAWSendStart:
  198. if(subghz_file_available(subghz) && subghz_scene_read_raw_update_filename(subghz)) {
  199. //start send
  200. subghz->state_notifications = SubGhzNotificationStateIDLE;
  201. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  202. subghz_rx_end(subghz);
  203. }
  204. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  205. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  206. if(!subghz_tx_start(subghz, subghz->txrx->fff_data)) {
  207. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  208. subghz_read_raw_set_status(
  209. subghz->subghz_read_raw,
  210. SubGhzReadRAWStatusIDLE,
  211. "",
  212. subghz->txrx->raw_threshold_rssi);
  213. } else {
  214. if(scene_manager_has_previous_scene(
  215. subghz->scene_manager, SubGhzSceneSaved) ||
  216. !scene_manager_has_previous_scene(
  217. subghz->scene_manager, SubGhzSceneStart)) {
  218. DOLPHIN_DEED(DolphinDeedSubGhzSend);
  219. }
  220. // set callback end tx
  221. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  222. (SubGhzProtocolEncoderRAW*)subghz_transmitter_get_protocol_instance(
  223. subghz->txrx->transmitter),
  224. subghz_scene_read_raw_callback_end_tx,
  225. subghz);
  226. subghz->state_notifications = SubGhzNotificationStateTx;
  227. }
  228. }
  229. } else {
  230. if(!scene_manager_search_and_switch_to_previous_scene(
  231. subghz->scene_manager, SubGhzSceneStart)) {
  232. scene_manager_stop(subghz->scene_manager);
  233. view_dispatcher_stop(subghz->view_dispatcher);
  234. }
  235. }
  236. consumed = true;
  237. break;
  238. case SubGhzCustomEventViewReadRAWSendStop:
  239. subghz->state_notifications = SubGhzNotificationStateIDLE;
  240. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  241. subghz_speaker_unmute(subghz);
  242. subghz_tx_stop(subghz);
  243. subghz_sleep(subghz);
  244. }
  245. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  246. consumed = true;
  247. break;
  248. case SubGhzCustomEventViewReadRAWIDLE:
  249. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  250. subghz_rx_end(subghz);
  251. subghz_sleep(subghz);
  252. };
  253. size_t spl_count = subghz_protocol_raw_get_sample_write(
  254. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  255. subghz_protocol_raw_save_to_file_stop(
  256. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  257. FuriString* temp_str;
  258. temp_str = furi_string_alloc();
  259. furi_string_printf(
  260. temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, RAW_FILE_NAME, SUBGHZ_APP_EXTENSION);
  261. subghz_protocol_raw_gen_fff_data(
  262. subghz->txrx->fff_data, furi_string_get_cstr(temp_str));
  263. furi_string_free(temp_str);
  264. if(spl_count > 0) {
  265. notification_message(subghz->notifications, &sequence_set_green_255);
  266. } else {
  267. notification_message(subghz->notifications, &sequence_reset_rgb);
  268. }
  269. subghz->state_notifications = SubGhzNotificationStateIDLE;
  270. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  271. consumed = true;
  272. break;
  273. case SubGhzCustomEventViewReadRAWREC:
  274. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  275. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  276. } else {
  277. subghz->txrx->raw_threshold_rssi_low_count = RAW_THRESHOLD_RSSI_LOW_COUNT;
  278. if(subghz_protocol_raw_save_to_file_init(
  279. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
  280. RAW_FILE_NAME,
  281. subghz->txrx->preset)) {
  282. DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
  283. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  284. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  285. subghz_begin(
  286. subghz,
  287. subghz_setting_get_preset_data_by_name(
  288. subghz->setting,
  289. furi_string_get_cstr(subghz->txrx->preset->name)));
  290. subghz_rx(subghz, subghz->txrx->preset->frequency);
  291. }
  292. subghz->state_notifications = SubGhzNotificationStateRx;
  293. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  294. } else {
  295. furi_string_set(subghz->error_str, "Function requires\nan SD card.");
  296. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  297. }
  298. }
  299. consumed = true;
  300. break;
  301. case SubGhzCustomEventViewReadRAWSave:
  302. if(subghz_file_available(subghz) && subghz_scene_read_raw_update_filename(subghz)) {
  303. scene_manager_set_scene_state(
  304. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSetRAW);
  305. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  306. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  307. } else {
  308. if(!scene_manager_search_and_switch_to_previous_scene(
  309. subghz->scene_manager, SubGhzSceneStart)) {
  310. scene_manager_stop(subghz->scene_manager);
  311. view_dispatcher_stop(subghz->view_dispatcher);
  312. }
  313. }
  314. consumed = true;
  315. break;
  316. default:
  317. break;
  318. }
  319. } else if(event.type == SceneManagerEventTypeTick) {
  320. switch(subghz->state_notifications) {
  321. case SubGhzNotificationStateRx:
  322. notification_message(subghz->notifications, &sequence_blink_cyan_10);
  323. subghz_read_raw_update_sample_write(
  324. subghz->subghz_read_raw,
  325. subghz_protocol_raw_get_sample_write(
  326. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result));
  327. float rssi = furi_hal_subghz_get_rssi();
  328. if(float_is_equal(subghz->txrx->raw_threshold_rssi, SUBGHZ_RAW_TRESHOLD_MIN)) {
  329. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, true);
  330. subghz_protocol_raw_save_to_file_pause(
  331. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, false);
  332. } else {
  333. if(rssi < subghz->txrx->raw_threshold_rssi) {
  334. subghz->txrx->raw_threshold_rssi_low_count++;
  335. if(subghz->txrx->raw_threshold_rssi_low_count > RAW_THRESHOLD_RSSI_LOW_COUNT) {
  336. subghz->txrx->raw_threshold_rssi_low_count = RAW_THRESHOLD_RSSI_LOW_COUNT;
  337. }
  338. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, false);
  339. } else {
  340. subghz->txrx->raw_threshold_rssi_low_count = 0;
  341. }
  342. if(subghz->txrx->raw_threshold_rssi_low_count == RAW_THRESHOLD_RSSI_LOW_COUNT) {
  343. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, false);
  344. subghz_protocol_raw_save_to_file_pause(
  345. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, true);
  346. subghz_speaker_mute(subghz);
  347. } else {
  348. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, true);
  349. subghz_protocol_raw_save_to_file_pause(
  350. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, false);
  351. subghz_speaker_unmute(subghz);
  352. }
  353. }
  354. break;
  355. case SubGhzNotificationStateTx:
  356. notification_message(subghz->notifications, &sequence_blink_magenta_10);
  357. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  358. break;
  359. default:
  360. break;
  361. }
  362. }
  363. return consumed;
  364. }
  365. void subghz_scene_read_raw_on_exit(void* context) {
  366. SubGhz* subghz = context;
  367. //Stop CC1101
  368. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  369. subghz_rx_end(subghz);
  370. subghz_sleep(subghz);
  371. };
  372. subghz->state_notifications = SubGhzNotificationStateIDLE;
  373. notification_message(subghz->notifications, &sequence_reset_rgb);
  374. //filter restoration
  375. subghz_receiver_set_filter(subghz->txrx->receiver, subghz->txrx->filter);
  376. }