subghz_scene_read_raw.c 13 KB

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