subghz_scene_read_raw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 SubghzCustomEventViewReadRAWSendStart:
  95. //set the path to read the file
  96. if(strcmp(
  97. subghz_protocol_raw_get_last_file_name(
  98. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  99. "")) {
  100. string_t temp_str;
  101. string_init_printf(
  102. temp_str,
  103. "%s",
  104. subghz_protocol_raw_get_last_file_name(
  105. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  106. path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
  107. strlcpy(
  108. subghz->file_name,
  109. string_get_cstr(temp_str),
  110. strlen(string_get_cstr(temp_str)) + 1);
  111. string_printf(
  112. temp_str,
  113. "%s/%s%s",
  114. SUBGHZ_APP_PATH_FOLDER,
  115. subghz->file_name,
  116. SUBGHZ_APP_EXTENSION);
  117. subghz_protocol_raw_set_last_file_name(
  118. (SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
  119. string_clear(temp_str);
  120. //start send
  121. subghz->state_notifications = SubGhzNotificationStateIDLE;
  122. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  123. subghz_rx_end(subghz);
  124. }
  125. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  126. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  127. if(!subghz_tx_start(subghz)) {
  128. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowOnlyRx);
  129. } else {
  130. subghz->state_notifications = SubGhzNotificationStateTX;
  131. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  132. }
  133. }
  134. }
  135. return true;
  136. break;
  137. case SubghzCustomEventViewReadRAWSendStop:
  138. subghz->state_notifications = SubGhzNotificationStateIDLE;
  139. if(subghz->txrx->txrx_state == SubGhzTxRxStateTx) {
  140. subghz_tx_stop(subghz);
  141. subghz_sleep(subghz);
  142. }
  143. subghz_read_raw_stop_send(subghz->subghz_read_raw);
  144. return true;
  145. break;
  146. case SubghzCustomEventViewReadRAWIDLE:
  147. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  148. subghz_rx_end(subghz);
  149. subghz_sleep(subghz);
  150. };
  151. subghz_protocol_raw_save_to_file_stop(
  152. (SubGhzProtocolRAW*)subghz->txrx->protocol_result);
  153. subghz->state_notifications = SubGhzNotificationStateIDLE;
  154. subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
  155. return true;
  156. break;
  157. case SubghzCustomEventViewReadRAWREC:
  158. if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
  159. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
  160. } else {
  161. subghz_get_preset_name(subghz, subghz->error_str);
  162. if(subghz_protocol_raw_save_to_file_init(
  163. (SubGhzProtocolRAW*)subghz->txrx->protocol_result,
  164. RAW_FILE_NAME,
  165. subghz->txrx->frequency,
  166. string_get_cstr(subghz->error_str))) {
  167. if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
  168. (subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
  169. subghz_begin(subghz, subghz->txrx->preset);
  170. subghz_rx(subghz, subghz->txrx->frequency);
  171. }
  172. subghz->state_notifications = SubGhzNotificationStateRX;
  173. } else {
  174. string_set(subghz->error_str, "No SD card");
  175. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
  176. }
  177. }
  178. return true;
  179. break;
  180. case SubghzCustomEventViewReadRAWSave:
  181. if(strcmp(
  182. subghz_protocol_raw_get_last_file_name(
  183. (SubGhzProtocolRAW*)subghz->txrx->protocol_result),
  184. "")) {
  185. string_t temp_str;
  186. string_init_printf(
  187. temp_str,
  188. "%s",
  189. subghz_protocol_raw_get_last_file_name(
  190. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  191. path_extract_filename_no_ext(string_get_cstr(temp_str), temp_str);
  192. strlcpy(
  193. subghz->file_name,
  194. string_get_cstr(temp_str),
  195. strlen(string_get_cstr(temp_str)) + 1);
  196. //set the path to read the file
  197. string_printf(
  198. temp_str,
  199. "%s/%s%s",
  200. SUBGHZ_APP_PATH_FOLDER,
  201. subghz->file_name,
  202. SUBGHZ_APP_EXTENSION);
  203. subghz_protocol_raw_set_last_file_name(
  204. (SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
  205. string_clear(temp_str);
  206. scene_manager_set_scene_state(
  207. subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
  208. subghz->txrx->rx_key_state = SubGhzRxKeyStateBack;
  209. scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
  210. }
  211. return true;
  212. break;
  213. default:
  214. break;
  215. }
  216. } else if(event.type == SceneManagerEventTypeTick) {
  217. switch(subghz->state_notifications) {
  218. case SubGhzNotificationStateRX:
  219. notification_message(subghz->notifications, &sequence_blink_blue_10);
  220. subghz_read_raw_update_sample_write(
  221. subghz->subghz_read_raw,
  222. subghz_protocol_raw_get_sample_write(
  223. (SubGhzProtocolRAW*)subghz->txrx->protocol_result));
  224. subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
  225. break;
  226. case SubGhzNotificationStateTX:
  227. notification_message(subghz->notifications, &sequence_blink_green_10);
  228. subghz_read_raw_update_sin(subghz->subghz_read_raw);
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. return false;
  235. }
  236. void subghz_scene_read_raw_on_exit(void* context) {
  237. SubGhz* subghz = context;
  238. //Stop CC1101
  239. if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
  240. subghz_rx_end(subghz);
  241. subghz_sleep(subghz);
  242. };
  243. subghz->state_notifications = SubGhzNotificationStateIDLE;
  244. //Сallback restoration
  245. subghz_worker_set_pair_callback(
  246. subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_parse);
  247. }