subghz_scene_read_raw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. static void subghz_scene_read_raw_update_statusbar(void* context) {
  8. furi_assert(context);
  9. SubGhz* subghz = context;
  10. string_t frequency_str;
  11. string_t modulation_str;
  12. string_init(frequency_str);
  13. string_init(modulation_str);
  14. subghz_get_frequency_modulation(subghz, frequency_str, modulation_str);
  15. subghz_read_raw_add_data_statusbar(
  16. subghz->subghz_read_raw, string_get_cstr(frequency_str), string_get_cstr(modulation_str));
  17. string_clear(frequency_str);
  18. string_clear(modulation_str);
  19. }
  20. void subghz_scene_read_raw_callback(SubghzCustomEvent event, void* context) {
  21. furi_assert(context);
  22. SubGhz* subghz = context;
  23. view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
  24. }
  25. void subghz_scene_read_raw_callback_end_tx(void* context) {
  26. furi_assert(context);
  27. SubGhz* subghz = context;
  28. view_dispatcher_send_custom_event(
  29. subghz->view_dispatcher, SubghzCustomEventViewReadRAWSendStop);
  30. }
  31. void subghz_scene_read_raw_on_enter(void* context) {
  32. SubGhz* subghz = context;
  33. if(subghz->txrx->rx_key_state == SubGhzRxKeyStateBack) {
  34. subghz_read_raw_set_status(subghz->subghz_read_raw, SubghzReadRAWStatusIDLE, "");
  35. } else if(subghz->txrx->rx_key_state == SubGhzRxKeyStateRAWLoad) {
  36. subghz_read_raw_set_status(
  37. subghz->subghz_read_raw, SubghzReadRAWStatusTX, subghz->file_name);
  38. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  39. } else {
  40. subghz_read_raw_set_status(subghz->subghz_read_raw, SubghzReadRAWStatusStart, "");
  41. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  42. }
  43. subghz_scene_read_raw_update_statusbar(subghz);
  44. subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);
  45. subghz->txrx->protocol_result = subghz_parser_get_by_name(subghz->txrx->parser, "RAW");
  46. furi_assert(subghz->txrx->protocol_result);
  47. subghz_worker_set_pair_callback(
  48. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_raw_parse);
  49. subghz_protocol_raw_file_encoder_worker_set_callback_end(
  50. (SubGhzProtocolRAW*)subghz->txrx->protocol_result,
  51. subghz_scene_read_raw_callback_end_tx,
  52. subghz);
  53. view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewReadRAW);
  54. }
  55. bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
  56. SubGhz* subghz = context;
  57. if(event.type == SceneManagerEventTypeCustom) {
  58. switch(event.event) {
  59. case SubghzCustomEventViewReadRAWBack:
  60. //Stop TX
  61. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  62. subghz_tx_stop(subghz);
  63. subghz_sleep(subghz);
  64. }
  65. //Stop RX
  66. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  67. subghz_rx_end(subghz);
  68. subghz_sleep(subghz);
  69. };
  70. //Stop save file
  71. subghz_protocol_raw_save_to_file_stop(
  72. (SubGhzProtocolRAW*)subghz->txrx->protocol_result);
  73. subghz->state_notifications = SubGhzNotificationStateIDLE;
  74. //needed save?
  75. if((subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) ||
  76. (subghz->txrx->rx_key_state == SubGhzRxKeyStateBack)) {
  77. subghz->txrx->rx_key_state = SubGhzRxKeyStateExit;
  78. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  79. } else {
  80. //Restore default setting
  81. subghz->txrx->frequency = subghz_frequencies[subghz_frequencies_433_92];
  82. subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
  83. scene_manager_search_and_switch_to_previous_scene(
  84. subghz->scene_manager, SubGhzSceneStart);
  85. }
  86. return true;
  87. break;
  88. case SubghzCustomEventViewReadRAWConfig:
  89. scene_manager_set_scene_state(
  90. subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
  91. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
  92. return true;
  93. break;
  94. case SubghzCustomEventViewReadRAWErase:
  95. subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
  96. return true;
  97. break;
  98. case SubghzCustomEventViewReadRAWVibro:
  99. notification_message(subghz->notifications, &sequence_single_vibro);
  100. return true;
  101. break;
  102. case SubghzCustomEventViewReadRAWSendStart:
  103. //set the path to read the file
  104. if(strcmp(
  105. subghz_protocol_raw_get_last_file_name(
  106. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  107. "")) {
  108. string_t temp_str;
  109. string_init_printf(
  110. temp_str,
  111. "%s",
  112. subghz_protocol_raw_get_last_file_name(
  113. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  114. path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
  115. strlcpy(
  116. subghz->file_name,
  117. string_get_cstr(temp_str),
  118. strlen(string_get_cstr(temp_str)) + 1);
  119. string_printf(
  120. temp_str,
  121. "%s/%s%s",
  122. SUBGHZ_APP_PATH_FOLDER,
  123. subghz->file_name,
  124. SUBGHZ_APP_EXTENSION);
  125. subghz_protocol_raw_set_last_file_name(
  126. (SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
  127. string_clear(temp_str);
  128. //start send
  129. subghz->state_notifications = SubGhzNotificationStateIDLE;
  130. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  131. subghz_rx_end(subghz);
  132. }
  133. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  134. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  135. if(!subghz_tx_start(subghz)) {
  136. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  137. } else {
  138. subghz->state_notifications = SubGhzNotificationStateTX;
  139. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  140. }
  141. }
  142. }
  143. return true;
  144. break;
  145. case SubghzCustomEventViewReadRAWSendStop:
  146. subghz->state_notifications = SubGhzNotificationStateIDLE;
  147. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  148. subghz_tx_stop(subghz);
  149. subghz_sleep(subghz);
  150. }
  151. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  152. return true;
  153. break;
  154. case SubghzCustomEventViewReadRAWIDLE:
  155. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  156. subghz_rx_end(subghz);
  157. subghz_sleep(subghz);
  158. };
  159. subghz_protocol_raw_save_to_file_stop(
  160. (SubGhzProtocolRAW*)subghz->txrx->protocol_result);
  161. subghz->state_notifications = SubGhzNotificationStateIDLE;
  162. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  163. return true;
  164. break;
  165. case SubghzCustomEventViewReadRAWREC:
  166. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  167. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  168. } else {
  169. subghz_get_preset_name(subghz, subghz->error_str);
  170. if(subghz_protocol_raw_save_to_file_init(
  171. (SubGhzProtocolRAW*)subghz->txrx->protocol_result,
  172. RAW_FILE_NAME,
  173. subghz->txrx->frequency,
  174. string_get_cstr(subghz->error_str))) {
  175. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  176. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  177. subghz_begin(subghz, subghz->txrx->preset);
  178. subghz_rx(subghz, subghz->txrx->frequency);
  179. }
  180. subghz->state_notifications = SubGhzNotificationStateRX;
  181. } else {
  182. string_set(subghz->error_str, "No SD card");
  183. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  184. }
  185. }
  186. return true;
  187. break;
  188. case SubghzCustomEventViewReadRAWSave:
  189. if(strcmp(
  190. subghz_protocol_raw_get_last_file_name(
  191. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  192. "")) {
  193. string_t temp_str;
  194. string_init_printf(
  195. temp_str,
  196. "%s",
  197. subghz_protocol_raw_get_last_file_name(
  198. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  199. path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
  200. strlcpy(
  201. subghz->file_name,
  202. string_get_cstr(temp_str),
  203. strlen(string_get_cstr(temp_str)) + 1);
  204. //set the path to read the file
  205. string_printf(
  206. temp_str,
  207. "%s/%s%s",
  208. SUBGHZ_APP_PATH_FOLDER,
  209. subghz->file_name,
  210. SUBGHZ_APP_EXTENSION);
  211. subghz_protocol_raw_set_last_file_name(
  212. (SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
  213. string_clear(temp_str);
  214. scene_manager_set_scene_state(
  215. subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
  216. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  217. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  218. }
  219. return true;
  220. break;
  221. default:
  222. break;
  223. }
  224. } else if(event.type == SceneManagerEventTypeTick) {
  225. switch(subghz->state_notifications) {
  226. case SubGhzNotificationStateRX:
  227. notification_message(subghz->notifications, &sequence_blink_blue_10);
  228. subghz_read_raw_update_sample_write(
  229. subghz->subghz_read_raw,
  230. subghz_protocol_raw_get_sample_write(
  231. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  232. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  233. break;
  234. case SubGhzNotificationStateTX:
  235. notification_message(subghz->notifications, &sequence_blink_green_10);
  236. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  237. break;
  238. default:
  239. break;
  240. }
  241. }
  242. return false;
  243. }
  244. void subghz_scene_read_raw_on_exit(void* context) {
  245. SubGhz* subghz = context;
  246. //Stop CC1101
  247. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  248. subghz_rx_end(subghz);
  249. subghz_sleep(subghz);
  250. };
  251. subghz->state_notifications = SubGhzNotificationStateIDLE;
  252. //Сallback restoration
  253. subghz_worker_set_pair_callback(
  254. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_parse);
  255. }