subghz_scene_read_raw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. strncpy(subghz->file_path, string_get_cstr(temp_str), SUBGHZ_MAX_LEN_NAME);
  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_no_ext(subghz->file_path, file_name);
  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_no_ext(subghz->file_path, file_name);
  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. if(event.type == SceneManagerEventTypeCustom) {
  91. switch(event.event) {
  92. case SubGhzCustomEventViewReadRAWBack:
  93. //Stop TX
  94. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  95. subghz_tx_stop(subghz);
  96. subghz_sleep(subghz);
  97. }
  98. //Stop RX
  99. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  100. subghz_rx_end(subghz);
  101. subghz_sleep(subghz);
  102. };
  103. //Stop save file
  104. subghz_protocol_raw_save_to_file_stop(
  105. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  106. subghz->state_notifications = SubGhzNotificationStateIDLE;
  107. //needed save?
  108. if((subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) ||
  109. (subghz->txrx->rx_key_state == SubGhzRxKeyStateBack)) {
  110. subghz->txrx->rx_key_state = SubGhzRxKeyStateExit;
  111. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  112. } else {
  113. //Restore default setting
  114. subghz->txrx->frequency = subghz_setting_get_default_frequency(subghz->setting);
  115. subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
  116. if(!scene_manager_search_and_switch_to_previous_scene(
  117. subghz->scene_manager, SubGhzSceneSaved)) {
  118. if(!scene_manager_search_and_switch_to_previous_scene(
  119. subghz->scene_manager, SubGhzSceneStart)) {
  120. scene_manager_stop(subghz->scene_manager);
  121. view_dispatcher_stop(subghz->view_dispatcher);
  122. }
  123. }
  124. }
  125. return true;
  126. break;
  127. case SubGhzCustomEventViewReadRAWTXRXStop:
  128. //Stop TX
  129. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  130. subghz_tx_stop(subghz);
  131. subghz_sleep(subghz);
  132. }
  133. //Stop RX
  134. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  135. subghz_rx_end(subghz);
  136. subghz_sleep(subghz);
  137. };
  138. subghz->state_notifications = SubGhzNotificationStateIDLE;
  139. return true;
  140. break;
  141. case SubGhzCustomEventViewReadRAWConfig:
  142. scene_manager_set_scene_state(
  143. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  144. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  145. return true;
  146. break;
  147. case SubGhzCustomEventViewReadRAWErase:
  148. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  149. notification_message(subghz->notifications, &sequence_reset_rgb);
  150. return true;
  151. break;
  152. case SubGhzCustomEventViewReadRAWMore:
  153. if(subghz_scene_read_raw_update_filename(subghz)) {
  154. scene_manager_set_scene_state(
  155. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  156. subghz->txrx->rx_key_state = SubGhzRxKeyStateRAWLoad;
  157. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneMoreRAW);
  158. return true;
  159. } else {
  160. furi_crash("SugGhz: RAW file name update error.");
  161. }
  162. break;
  163. case SubGhzCustomEventViewReadRAWSendStart:
  164. if(subghz_scene_read_raw_update_filename(subghz)) {
  165. //start send
  166. subghz->state_notifications = SubGhzNotificationStateIDLE;
  167. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  168. subghz_rx_end(subghz);
  169. }
  170. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  171. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  172. //ToDo FIX
  173. if(!subghz_tx_start(subghz, subghz->txrx->fff_data)) {
  174. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  175. } else {
  176. DOLPHIN_DEED(DolphinDeedSubGhzSend);
  177. // set callback end tx
  178. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  179. (SubGhzProtocolEncoderRAW*)subghz->txrx->transmitter->protocol_instance,
  180. subghz_scene_read_raw_callback_end_tx,
  181. subghz);
  182. subghz->state_notifications = SubGhzNotificationStateTx;
  183. }
  184. }
  185. }
  186. return true;
  187. break;
  188. case SubGhzCustomEventViewReadRAWSendStop:
  189. subghz->state_notifications = SubGhzNotificationStateIDLE;
  190. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  191. subghz_tx_stop(subghz);
  192. subghz_sleep(subghz);
  193. }
  194. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  195. return true;
  196. break;
  197. case SubGhzCustomEventViewReadRAWIDLE:
  198. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  199. subghz_rx_end(subghz);
  200. subghz_sleep(subghz);
  201. };
  202. size_t spl_count = subghz_protocol_raw_get_sample_write(
  203. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  204. subghz_protocol_raw_save_to_file_stop(
  205. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  206. string_t temp_str;
  207. string_init(temp_str);
  208. string_printf(
  209. temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, RAW_FILE_NAME, SUBGHZ_APP_EXTENSION);
  210. subghz_protocol_raw_gen_fff_data(subghz->txrx->fff_data, string_get_cstr(temp_str));
  211. string_clear(temp_str);
  212. if(spl_count > 0) {
  213. notification_message(subghz->notifications, &sequence_set_green_255);
  214. } else {
  215. notification_message(subghz->notifications, &sequence_reset_rgb);
  216. }
  217. subghz->state_notifications = SubGhzNotificationStateIDLE;
  218. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  219. return true;
  220. break;
  221. case SubGhzCustomEventViewReadRAWREC:
  222. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  223. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  224. } else {
  225. //subghz_get_preset_name(subghz, subghz->error_str);
  226. if(subghz_protocol_raw_save_to_file_init(
  227. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
  228. RAW_FILE_NAME,
  229. subghz->txrx->frequency,
  230. subghz->txrx->preset)) {
  231. DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
  232. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  233. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  234. subghz_begin(subghz, subghz->txrx->preset);
  235. subghz_rx(subghz, subghz->txrx->frequency);
  236. }
  237. subghz->state_notifications = SubGhzNotificationStateRx;
  238. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  239. } else {
  240. string_set(subghz->error_str, "Function requires\nan SD card.");
  241. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  242. }
  243. }
  244. return true;
  245. break;
  246. case SubGhzCustomEventViewReadRAWSave:
  247. if(subghz_scene_read_raw_update_filename(subghz)) {
  248. scene_manager_set_scene_state(
  249. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSetRAW);
  250. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  251. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  252. }
  253. return true;
  254. break;
  255. default:
  256. break;
  257. }
  258. } else if(event.type == SceneManagerEventTypeTick) {
  259. switch(subghz->state_notifications) {
  260. case SubGhzNotificationStateRx:
  261. notification_message(subghz->notifications, &sequence_blink_cyan_10);
  262. subghz_read_raw_update_sample_write(
  263. subghz->subghz_read_raw,
  264. subghz_protocol_raw_get_sample_write(
  265. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result));
  266. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  267. break;
  268. case SubGhzNotificationStateTx:
  269. notification_message(subghz->notifications, &sequence_blink_magenta_10);
  270. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  271. break;
  272. default:
  273. break;
  274. }
  275. }
  276. return false;
  277. }
  278. void subghz_scene_read_raw_on_exit(void* context) {
  279. SubGhz* subghz = context;
  280. //Stop CC1101
  281. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  282. subghz_rx_end(subghz);
  283. subghz_sleep(subghz);
  284. };
  285. subghz->state_notifications = SubGhzNotificationStateIDLE;
  286. notification_message(subghz->notifications, &sequence_reset_rgb);
  287. //filter restoration
  288. subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_Decodable);
  289. }