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_default_frequency(subghz->setting);
  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("SugGhz: RAW file name update error.");
  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(
  204. subghz->txrx->fff_data, SUBGHZ_APP_FOLDER, 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. }