subghz_scene_read_raw.c 11 KB

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