subghz_scene_read_raw.c 17 KB

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