subghz_scene_read_raw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #include "../subghz_i.h"
  2. #include "../views/subghz_read_raw.h"
  3. #include <lib/subghz/protocols/subghz_protocol_raw.h>
  4. #include <lib/subghz/subghz_parser.h>
  5. #include <lib/toolbox/path.h>
  6. #define RAW_FILE_NAME "Raw_temp"
  7. bool subghz_scene_read_raw_update_filename(SubGhz* subghz) {
  8. bool ret = false;
  9. //set the path to read the file
  10. if(strcmp(
  11. subghz_protocol_raw_get_last_file_name(
  12. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  13. "")) {
  14. string_t temp_str;
  15. string_init_printf(
  16. temp_str,
  17. "%s",
  18. subghz_protocol_raw_get_last_file_name(
  19. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  20. path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
  21. strcpy(subghz->file_name, string_get_cstr(temp_str));
  22. string_printf(
  23. temp_str, "%s/%s%s", SUBGHZ_APP_PATH_FOLDER, subghz->file_name, SUBGHZ_APP_EXTENSION);
  24. subghz_protocol_raw_set_last_file_name(
  25. (SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
  26. string_clear(temp_str);
  27. ret = true;
  28. }
  29. return ret;
  30. }
  31. static void subghz_scene_read_raw_update_statusbar(void* context) {
  32. furi_assert(context);
  33. SubGhz* subghz = context;
  34. string_t frequency_str;
  35. string_t modulation_str;
  36. string_init(frequency_str);
  37. string_init(modulation_str);
  38. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  39. subghz_read_raw_add_data_statusbar(
  40. subghz->subghz_read_raw, string_get_cstr(frequency_str), string_get_cstr(modulation_str));
  41. string_clear(frequency_str);
  42. string_clear(modulation_str);
  43. }
  44. void subghz_scene_read_raw_callback(SubghzCustomEvent event, void* context) {
  45. furi_assert(context);
  46. SubGhz* subghz = context;
  47. view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
  48. }
  49. void subghz_scene_read_raw_callback_end_tx(void* context) {
  50. furi_assert(context);
  51. SubGhz* subghz = context;
  52. view_dispatcher_send_custom_event(
  53. subghz->view_dispatcher, SubghzCustomEventViewReadRAWSendStop);
  54. }
  55. void subghz_scene_read_raw_on_enter(void* context) {
  56. SubGhz* subghz = context;
  57. switch(subghz->txrx->rx_key_state) {
  58. case SubGhzRxKeyStateBack:
  59. subghz_read_raw_set_status(subghz->subghz_read_raw, SubghzReadRAWStatusIDLE, "");
  60. break;
  61. case SubGhzRxKeyStateRAWLoad:
  62. subghz_read_raw_set_status(
  63. subghz->subghz_read_raw, SubghzReadRAWStatusLoadKeyTX, subghz->file_name);
  64. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  65. break;
  66. case SubGhzRxKeyStateRAWSave:
  67. subghz_read_raw_set_status(
  68. subghz->subghz_read_raw, SubghzReadRAWStatusSaveKey, subghz->file_name);
  69. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  70. break;
  71. default:
  72. subghz_read_raw_set_status(subghz->subghz_read_raw, SubghzReadRAWStatusStart, "");
  73. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  74. break;
  75. }
  76. subghz_scene_read_raw_update_statusbar(subghz);
  77. subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);
  78. subghz->txrx->protocol_result = subghz_parser_get_by_name(subghz->txrx->parser, "RAW");
  79. furi_assert(subghz->txrx->protocol_result);
  80. subghz_worker_set_pair_callback(
  81. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_raw_parse);
  82. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  83. (SubGhzProtocolRAW*)subghz->txrx->protocol_result,
  84. subghz_scene_read_raw_callback_end_tx,
  85. subghz);
  86. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewReadRAW);
  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. (SubGhzProtocolRAW*)subghz->txrx->protocol_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_frequencies[subghz_frequencies_433_92];
  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. return true;
  150. break;
  151. case SubghzCustomEventViewReadRAWVibro:
  152. notification_message(subghz->notifications, &sequence_single_vibro);
  153. return true;
  154. break;
  155. case SubghzCustomEventViewReadRAWMore:
  156. if(subghz_scene_read_raw_update_filename(subghz)) {
  157. scene_manager_set_scene_state(
  158. subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
  159. subghz->txrx->rx_key_state = SubGhzRxKeyStateRAWLoad;
  160. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneMoreRAW);
  161. return true;
  162. } else {
  163. furi_crash(NULL);
  164. }
  165. break;
  166. case SubghzCustomEventViewReadRAWSendStart:
  167. if(subghz_scene_read_raw_update_filename(subghz)) {
  168. //start send
  169. subghz->state_notifications = SubGhzNotificationStateIDLE;
  170. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  171. subghz_rx_end(subghz);
  172. }
  173. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  174. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  175. if(!subghz_tx_start(subghz)) {
  176. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  177. } else {
  178. subghz->state_notifications = SubGhzNotificationStateTX;
  179. }
  180. }
  181. }
  182. return true;
  183. break;
  184. case SubghzCustomEventViewReadRAWSendStop:
  185. subghz->state_notifications = SubGhzNotificationStateIDLE;
  186. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  187. subghz_tx_stop(subghz);
  188. subghz_sleep(subghz);
  189. }
  190. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  191. return true;
  192. break;
  193. case SubghzCustomEventViewReadRAWIDLE:
  194. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  195. subghz_rx_end(subghz);
  196. subghz_sleep(subghz);
  197. };
  198. subghz_protocol_raw_save_to_file_stop(
  199. (SubGhzProtocolRAW*)subghz->txrx->protocol_result);
  200. subghz->state_notifications = SubGhzNotificationStateIDLE;
  201. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  202. return true;
  203. break;
  204. case SubghzCustomEventViewReadRAWREC:
  205. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  206. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  207. } else {
  208. subghz_get_preset_name(subghz, subghz->error_str);
  209. if(subghz_protocol_raw_save_to_file_init(
  210. (SubGhzProtocolRAW*)subghz->txrx->protocol_result,
  211. RAW_FILE_NAME,
  212. subghz->txrx->frequency,
  213. string_get_cstr(subghz->error_str))) {
  214. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  215. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  216. subghz_begin(subghz, subghz->txrx->preset);
  217. subghz_rx(subghz, subghz->txrx->frequency);
  218. }
  219. subghz->state_notifications = SubGhzNotificationStateRX;
  220. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  221. } else {
  222. string_set(subghz->error_str, "Function requires\nan SD card.");
  223. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  224. }
  225. }
  226. return true;
  227. break;
  228. case SubghzCustomEventViewReadRAWSave:
  229. if(subghz_scene_read_raw_update_filename(subghz)) {
  230. scene_manager_set_scene_state(
  231. subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
  232. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  233. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  234. }
  235. return true;
  236. break;
  237. default:
  238. break;
  239. }
  240. } else if(event.type == SceneManagerEventTypeTick) {
  241. switch(subghz->state_notifications) {
  242. case SubGhzNotificationStateRX:
  243. notification_message(subghz->notifications, &sequence_blink_blue_10);
  244. subghz_read_raw_update_sample_write(
  245. subghz->subghz_read_raw,
  246. subghz_protocol_raw_get_sample_write(
  247. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  248. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  249. break;
  250. case SubGhzNotificationStateTX:
  251. notification_message(subghz->notifications, &sequence_blink_green_10);
  252. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  253. break;
  254. default:
  255. break;
  256. }
  257. }
  258. return false;
  259. }
  260. void subghz_scene_read_raw_on_exit(void* context) {
  261. SubGhz* subghz = context;
  262. //Stop CC1101
  263. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  264. subghz_rx_end(subghz);
  265. subghz_sleep(subghz);
  266. };
  267. subghz->state_notifications = SubGhzNotificationStateIDLE;
  268. //Сallback restoration
  269. subghz_worker_set_pair_callback(
  270. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_parse);
  271. }