subghz_scene_read_raw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
  23. strncpy(subghz->file_name, string_get_cstr(temp_str), SUBGHZ_MAX_LEN_NAME);
  24. ret = true;
  25. } while(false);
  26. string_clear(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. string_t frequency_str;
  33. string_t modulation_str;
  34. string_init(frequency_str);
  35. string_init(modulation_str);
  36. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  37. subghz_read_raw_add_data_statusbar(
  38. subghz->subghz_read_raw, string_get_cstr(frequency_str), string_get_cstr(modulation_str));
  39. string_clear(frequency_str);
  40. string_clear(modulation_str);
  41. }
  42. void subghz_scene_read_raw_callback(SubGhzCustomEvent event, void* context) {
  43. furi_assert(context);
  44. SubGhz* subghz = context;
  45. view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
  46. }
  47. void subghz_scene_read_raw_callback_end_tx(void* context) {
  48. furi_assert(context);
  49. SubGhz* subghz = context;
  50. view_dispatcher_send_custom_event(
  51. subghz->view_dispatcher, SubGhzCustomEventViewReadRAWSendStop);
  52. }
  53. void subghz_scene_read_raw_on_enter(void* context) {
  54. SubGhz* subghz = context;
  55. switch(subghz->txrx->rx_key_state) {
  56. case SubGhzRxKeyStateBack:
  57. subghz_read_raw_set_status(subghz->subghz_read_raw, SubGhzReadRAWStatusIDLE, "");
  58. break;
  59. case SubGhzRxKeyStateRAWLoad:
  60. subghz_read_raw_set_status(
  61. subghz->subghz_read_raw, SubGhzReadRAWStatusLoadKeyTX, subghz->file_name);
  62. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  63. break;
  64. case SubGhzRxKeyStateRAWSave:
  65. subghz_read_raw_set_status(
  66. subghz->subghz_read_raw, SubGhzReadRAWStatusSaveKey, subghz->file_name);
  67. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  68. break;
  69. default:
  70. subghz_read_raw_set_status(subghz->subghz_read_raw, SubGhzReadRAWStatusStart, "");
  71. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  72. break;
  73. }
  74. subghz_scene_read_raw_update_statusbar(subghz);
  75. //set callback view raw
  76. subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);
  77. subghz->txrx->decoder_result =
  78. subghz_receiver_search_decoder_base_by_name(subghz->txrx->receiver, "RAW");
  79. furi_assert(subghz->txrx->decoder_result);
  80. //set filter RAW feed
  81. subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_RAW);
  82. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdReadRAW);
  83. }
  84. bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
  85. SubGhz* subghz = context;
  86. if(event.type == SceneManagerEventTypeCustom) {
  87. switch(event.event) {
  88. case SubGhzCustomEventViewReadRAWBack:
  89. //Stop TX
  90. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  91. subghz_tx_stop(subghz);
  92. subghz_sleep(subghz);
  93. }
  94. //Stop RX
  95. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  96. subghz_rx_end(subghz);
  97. subghz_sleep(subghz);
  98. };
  99. //Stop save file
  100. subghz_protocol_raw_save_to_file_stop(
  101. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  102. subghz->state_notifications = SubGhzNotificationStateIDLE;
  103. //needed save?
  104. if((subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) ||
  105. (subghz->txrx->rx_key_state == SubGhzRxKeyStateBack)) {
  106. subghz->txrx->rx_key_state = SubGhzRxKeyStateExit;
  107. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  108. } else {
  109. //Restore default setting
  110. subghz->txrx->frequency = subghz_setting_get_frequency(
  111. subghz->setting, subghz_setting_get_frequency_default_index(subghz->setting));
  112. subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
  113. if(!scene_manager_search_and_switch_to_previous_scene(
  114. subghz->scene_manager, SubGhzSceneSaved)) {
  115. if(!scene_manager_search_and_switch_to_previous_scene(
  116. subghz->scene_manager, SubGhzSceneStart)) {
  117. scene_manager_stop(subghz->scene_manager);
  118. view_dispatcher_stop(subghz->view_dispatcher);
  119. }
  120. }
  121. }
  122. return true;
  123. break;
  124. case SubGhzCustomEventViewReadRAWTXRXStop:
  125. //Stop TX
  126. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  127. subghz_tx_stop(subghz);
  128. subghz_sleep(subghz);
  129. }
  130. //Stop RX
  131. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  132. subghz_rx_end(subghz);
  133. subghz_sleep(subghz);
  134. };
  135. subghz->state_notifications = SubGhzNotificationStateIDLE;
  136. return true;
  137. break;
  138. case SubGhzCustomEventViewReadRAWConfig:
  139. scene_manager_set_scene_state(
  140. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  141. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  142. return true;
  143. break;
  144. case SubGhzCustomEventViewReadRAWErase:
  145. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  146. return true;
  147. break;
  148. case SubGhzCustomEventViewReadRAWVibro:
  149. notification_message(subghz->notifications, &sequence_single_vibro);
  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. subghz_protocol_raw_save_to_file_stop(
  203. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  204. subghz_protocol_raw_gen_fff_data(subghz->txrx->fff_data, RAW_FILE_NAME);
  205. subghz->state_notifications = SubGhzNotificationStateIDLE;
  206. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  207. return true;
  208. break;
  209. case SubGhzCustomEventViewReadRAWREC:
  210. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  211. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  212. } else {
  213. //subghz_get_preset_name(subghz, subghz->error_str);
  214. if(subghz_protocol_raw_save_to_file_init(
  215. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
  216. RAW_FILE_NAME,
  217. subghz->txrx->frequency,
  218. subghz->txrx->preset)) {
  219. DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
  220. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  221. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  222. subghz_begin(subghz, subghz->txrx->preset);
  223. subghz_rx(subghz, subghz->txrx->frequency);
  224. }
  225. subghz->state_notifications = SubGhzNotificationStateRX;
  226. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  227. } else {
  228. string_set(subghz->error_str, "Function requires\nan SD card.");
  229. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  230. }
  231. }
  232. return true;
  233. break;
  234. case SubGhzCustomEventViewReadRAWSave:
  235. if(subghz_scene_read_raw_update_filename(subghz)) {
  236. scene_manager_set_scene_state(
  237. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSetRAW);
  238. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  239. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  240. }
  241. return true;
  242. break;
  243. default:
  244. break;
  245. }
  246. } else if(event.type == SceneManagerEventTypeTick) {
  247. switch(subghz->state_notifications) {
  248. case SubGhzNotificationStateRX:
  249. notification_message(subghz->notifications, &sequence_blink_blue_10);
  250. subghz_read_raw_update_sample_write(
  251. subghz->subghz_read_raw,
  252. subghz_protocol_raw_get_sample_write(
  253. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result));
  254. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  255. break;
  256. case SubGhzNotificationStateTX:
  257. notification_message(subghz->notifications, &sequence_blink_green_10);
  258. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. return false;
  265. }
  266. void subghz_scene_read_raw_on_exit(void* context) {
  267. SubGhz* subghz = context;
  268. //Stop CC1101
  269. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  270. subghz_rx_end(subghz);
  271. subghz_sleep(subghz);
  272. };
  273. subghz->state_notifications = SubGhzNotificationStateIDLE;
  274. //filter restoration
  275. subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_Decodable);
  276. }