subghz_scene_read_raw.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  209. } else {
  210. if(scene_manager_has_previous_scene(
  211. subghz->scene_manager, SubGhzSceneSaved) ||
  212. !scene_manager_has_previous_scene(
  213. subghz->scene_manager, SubGhzSceneStart)) {
  214. DOLPHIN_DEED(DolphinDeedSubGhzSend);
  215. }
  216. // set callback end tx
  217. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  218. (SubGhzProtocolEncoderRAW*)subghz_transmitter_get_protocol_instance(
  219. subghz->txrx->transmitter),
  220. subghz_scene_read_raw_callback_end_tx,
  221. subghz);
  222. subghz->state_notifications = SubGhzNotificationStateTx;
  223. }
  224. }
  225. } else {
  226. if(!scene_manager_search_and_switch_to_previous_scene(
  227. subghz->scene_manager, SubGhzSceneStart)) {
  228. scene_manager_stop(subghz->scene_manager);
  229. view_dispatcher_stop(subghz->view_dispatcher);
  230. }
  231. }
  232. consumed = true;
  233. break;
  234. case SubGhzCustomEventViewReadRAWSendStop:
  235. subghz->state_notifications = SubGhzNotificationStateIDLE;
  236. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  237. subghz_speaker_unmute(subghz);
  238. subghz_tx_stop(subghz);
  239. subghz_sleep(subghz);
  240. }
  241. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  242. consumed = true;
  243. break;
  244. case SubGhzCustomEventViewReadRAWIDLE:
  245. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  246. subghz_rx_end(subghz);
  247. subghz_sleep(subghz);
  248. };
  249. size_t spl_count = subghz_protocol_raw_get_sample_write(
  250. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  251. subghz_protocol_raw_save_to_file_stop(
  252. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  253. FuriString* temp_str;
  254. temp_str = furi_string_alloc();
  255. furi_string_printf(
  256. temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, RAW_FILE_NAME, SUBGHZ_APP_EXTENSION);
  257. subghz_protocol_raw_gen_fff_data(
  258. subghz->txrx->fff_data, furi_string_get_cstr(temp_str));
  259. furi_string_free(temp_str);
  260. if(spl_count > 0) {
  261. notification_message(subghz->notifications, &sequence_set_green_255);
  262. } else {
  263. notification_message(subghz->notifications, &sequence_reset_rgb);
  264. }
  265. subghz->state_notifications = SubGhzNotificationStateIDLE;
  266. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  267. consumed = true;
  268. break;
  269. case SubGhzCustomEventViewReadRAWREC:
  270. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  271. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  272. } else {
  273. subghz->txrx->raw_threshold_rssi_low_count = RAW_THRESHOLD_RSSI_LOW_COUNT;
  274. if(subghz_protocol_raw_save_to_file_init(
  275. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
  276. RAW_FILE_NAME,
  277. subghz->txrx->preset)) {
  278. DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
  279. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  280. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  281. subghz_begin(
  282. subghz,
  283. subghz_setting_get_preset_data_by_name(
  284. subghz->setting,
  285. furi_string_get_cstr(subghz->txrx->preset->name)));
  286. subghz_rx(subghz, subghz->txrx->preset->frequency);
  287. }
  288. subghz->state_notifications = SubGhzNotificationStateRx;
  289. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  290. } else {
  291. furi_string_set(subghz->error_str, "Function requires\nan SD card.");
  292. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  293. }
  294. }
  295. consumed = true;
  296. break;
  297. case SubGhzCustomEventViewReadRAWSave:
  298. if(subghz_file_available(subghz) && subghz_scene_read_raw_update_filename(subghz)) {
  299. scene_manager_set_scene_state(
  300. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSetRAW);
  301. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  302. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  303. } else {
  304. if(!scene_manager_search_and_switch_to_previous_scene(
  305. subghz->scene_manager, SubGhzSceneStart)) {
  306. scene_manager_stop(subghz->scene_manager);
  307. view_dispatcher_stop(subghz->view_dispatcher);
  308. }
  309. }
  310. consumed = true;
  311. break;
  312. default:
  313. break;
  314. }
  315. } else if(event.type == SceneManagerEventTypeTick) {
  316. switch(subghz->state_notifications) {
  317. case SubGhzNotificationStateRx:
  318. notification_message(subghz->notifications, &sequence_blink_cyan_10);
  319. subghz_read_raw_update_sample_write(
  320. subghz->subghz_read_raw,
  321. subghz_protocol_raw_get_sample_write(
  322. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result));
  323. float rssi = furi_hal_subghz_get_rssi();
  324. if(float_is_equal(subghz->txrx->raw_threshold_rssi, SUBGHZ_RAW_TRESHOLD_MIN)) {
  325. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, true);
  326. subghz_protocol_raw_save_to_file_pause(
  327. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, false);
  328. } else {
  329. if(rssi < subghz->txrx->raw_threshold_rssi) {
  330. subghz->txrx->raw_threshold_rssi_low_count++;
  331. if(subghz->txrx->raw_threshold_rssi_low_count > RAW_THRESHOLD_RSSI_LOW_COUNT) {
  332. subghz->txrx->raw_threshold_rssi_low_count = RAW_THRESHOLD_RSSI_LOW_COUNT;
  333. }
  334. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, false);
  335. } else {
  336. subghz->txrx->raw_threshold_rssi_low_count = 0;
  337. }
  338. if(subghz->txrx->raw_threshold_rssi_low_count == RAW_THRESHOLD_RSSI_LOW_COUNT) {
  339. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, false);
  340. subghz_protocol_raw_save_to_file_pause(
  341. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, true);
  342. subghz_speaker_mute(subghz);
  343. } else {
  344. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, rssi, true);
  345. subghz_protocol_raw_save_to_file_pause(
  346. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result, false);
  347. subghz_speaker_unmute(subghz);
  348. }
  349. }
  350. break;
  351. case SubGhzNotificationStateTx:
  352. notification_message(subghz->notifications, &sequence_blink_magenta_10);
  353. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  354. break;
  355. default:
  356. break;
  357. }
  358. }
  359. return consumed;
  360. }
  361. void subghz_scene_read_raw_on_exit(void* context) {
  362. SubGhz* subghz = context;
  363. //Stop CC1101
  364. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  365. subghz_rx_end(subghz);
  366. subghz_sleep(subghz);
  367. };
  368. subghz->state_notifications = SubGhzNotificationStateIDLE;
  369. notification_message(subghz->notifications, &sequence_reset_rgb);
  370. //filter restoration
  371. subghz_receiver_set_filter(subghz->txrx->receiver, subghz->txrx->filter);
  372. }