subghz_scene_read_raw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. strcpy(subghz->file_name, string_get_cstr(temp_str));
  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_frequencies[subghz_frequencies_433_92];
  111. subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
  112. if(!scene_manager_search_and_switch_to_previous_scene(
  113. subghz->scene_manager, SubGhzSceneSaved)) {
  114. if(!scene_manager_search_and_switch_to_previous_scene(
  115. subghz->scene_manager, SubGhzSceneStart)) {
  116. scene_manager_stop(subghz->scene_manager);
  117. view_dispatcher_stop(subghz->view_dispatcher);
  118. }
  119. }
  120. }
  121. return true;
  122. break;
  123. case SubGhzCustomEventViewReadRAWTXRXStop:
  124. //Stop TX
  125. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  126. subghz_tx_stop(subghz);
  127. subghz_sleep(subghz);
  128. }
  129. //Stop RX
  130. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  131. subghz_rx_end(subghz);
  132. subghz_sleep(subghz);
  133. };
  134. subghz->state_notifications = SubGhzNotificationStateIDLE;
  135. return true;
  136. break;
  137. case SubGhzCustomEventViewReadRAWConfig:
  138. scene_manager_set_scene_state(
  139. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  140. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  141. return true;
  142. break;
  143. case SubGhzCustomEventViewReadRAWErase:
  144. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  145. return true;
  146. break;
  147. case SubGhzCustomEventViewReadRAWVibro:
  148. notification_message(subghz->notifications, &sequence_single_vibro);
  149. return true;
  150. break;
  151. case SubGhzCustomEventViewReadRAWMore:
  152. if(subghz_scene_read_raw_update_filename(subghz)) {
  153. scene_manager_set_scene_state(
  154. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSet);
  155. subghz->txrx->rx_key_state = SubGhzRxKeyStateRAWLoad;
  156. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneMoreRAW);
  157. return true;
  158. } else {
  159. furi_crash(NULL);
  160. }
  161. break;
  162. case SubGhzCustomEventViewReadRAWSendStart:
  163. if(subghz_scene_read_raw_update_filename(subghz)) {
  164. //start send
  165. subghz->state_notifications = SubGhzNotificationStateIDLE;
  166. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  167. subghz_rx_end(subghz);
  168. }
  169. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  170. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  171. //ToDo FIX
  172. if(!subghz_tx_start(subghz, subghz->txrx->fff_data)) {
  173. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  174. } else {
  175. DOLPHIN_DEED(DolphinDeedSubGhzSend);
  176. // set callback end tx
  177. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  178. (SubGhzProtocolEncoderRAW*)subghz->txrx->transmitter->protocol_instance,
  179. subghz_scene_read_raw_callback_end_tx,
  180. subghz);
  181. subghz->state_notifications = SubGhzNotificationStateTX;
  182. }
  183. }
  184. }
  185. return true;
  186. break;
  187. case SubGhzCustomEventViewReadRAWSendStop:
  188. subghz->state_notifications = SubGhzNotificationStateIDLE;
  189. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  190. subghz_tx_stop(subghz);
  191. subghz_sleep(subghz);
  192. }
  193. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  194. return true;
  195. break;
  196. case SubGhzCustomEventViewReadRAWIDLE:
  197. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  198. subghz_rx_end(subghz);
  199. subghz_sleep(subghz);
  200. };
  201. subghz_protocol_raw_save_to_file_stop(
  202. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result);
  203. subghz_protocol_raw_gen_fff_data(subghz->txrx->fff_data, RAW_FILE_NAME);
  204. subghz->state_notifications = SubGhzNotificationStateIDLE;
  205. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  206. return true;
  207. break;
  208. case SubGhzCustomEventViewReadRAWREC:
  209. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  210. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  211. } else {
  212. //subghz_get_preset_name(subghz, subghz->error_str);
  213. if(subghz_protocol_raw_save_to_file_init(
  214. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
  215. RAW_FILE_NAME,
  216. subghz->txrx->frequency,
  217. subghz->txrx->preset)) {
  218. DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
  219. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  220. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  221. subghz_begin(subghz, subghz->txrx->preset);
  222. subghz_rx(subghz, subghz->txrx->frequency);
  223. }
  224. subghz->state_notifications = SubGhzNotificationStateRX;
  225. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  226. } else {
  227. string_set(subghz->error_str, "Function requires\nan SD card.");
  228. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  229. }
  230. }
  231. return true;
  232. break;
  233. case SubGhzCustomEventViewReadRAWSave:
  234. if(subghz_scene_read_raw_update_filename(subghz)) {
  235. scene_manager_set_scene_state(
  236. subghz->scene_manager, SubGhzSceneReadRAW, SubGhzCustomEventManagerSetRAW);
  237. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  238. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  239. }
  240. return true;
  241. break;
  242. default:
  243. break;
  244. }
  245. } else if(event.type == SceneManagerEventTypeTick) {
  246. switch(subghz->state_notifications) {
  247. case SubGhzNotificationStateRX:
  248. notification_message(subghz->notifications, &sequence_blink_blue_10);
  249. subghz_read_raw_update_sample_write(
  250. subghz->subghz_read_raw,
  251. subghz_protocol_raw_get_sample_write(
  252. (SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result));
  253. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  254. break;
  255. case SubGhzNotificationStateTX:
  256. notification_message(subghz->notifications, &sequence_blink_green_10);
  257. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. return false;
  264. }
  265. void subghz_scene_read_raw_on_exit(void* context) {
  266. SubGhz* subghz = context;
  267. //Stop CC1101
  268. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  269. subghz_rx_end(subghz);
  270. subghz_sleep(subghz);
  271. };
  272. subghz->state_notifications = SubGhzNotificationStateIDLE;
  273. //filter restoration
  274. subghz_receiver_set_filter(subghz->txrx->receiver, SubGhzProtocolFlag_Decodable);
  275. }