app.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include "app.h"
  4. /* If this define is enabled, ProtoView is going to mess with the
  5. * otherwise opaque SubGhzWorker structure in order to disable
  6. * its filter for samples shorter than a given amount (30us at the
  7. * time I'm writing this comment).
  8. *
  9. * This structure must be taken in sync with the one of the firmware. */
  10. #define PROTOVIEW_DISABLE_SUBGHZ_FILTER 0
  11. #ifdef PROTOVIEW_DISABLE_SUBGHZ_FILTER
  12. struct SubGhzWorker {
  13. FuriThread* thread;
  14. FuriStreamBuffer* stream;
  15. volatile bool running;
  16. volatile bool overrun;
  17. LevelDuration filter_level_duration;
  18. bool filter_running;
  19. uint16_t filter_duration;
  20. SubGhzWorkerOverrunCallback overrun_callback;
  21. SubGhzWorkerPairCallback pair_callback;
  22. void* context;
  23. };
  24. #endif
  25. RawSamplesBuffer *RawSamples, *DetectedSamples;
  26. extern const SubGhzProtocolRegistry protoview_protocol_registry;
  27. /* Draw some text with a border. If the outside color is black and the inside
  28. * color is white, it just writes the border of the text, but the function can
  29. * also be used to write a bold variation of the font setting both the
  30. * colors to black, or alternatively to write a black text with a white
  31. * border so that it is visible if there are black stuff on the background. */
  32. /* The callback actually just passes the control to the actual active
  33. * view callback, after setting up basic stuff like cleaning the screen
  34. * and setting color to black. */
  35. static void render_callback(Canvas *const canvas, void *ctx) {
  36. ProtoViewApp *app = ctx;
  37. /* Clear screen. */
  38. canvas_set_color(canvas, ColorWhite);
  39. canvas_draw_box(canvas, 0, 0, 127, 63);
  40. canvas_set_color(canvas, ColorBlack);
  41. canvas_set_font(canvas, FontPrimary);
  42. /* Call who is in charge right now. */
  43. switch(app->current_view) {
  44. case ViewRawPulses: render_view_raw_pulses(canvas,app); break;
  45. case ViewInfo: render_view_info(canvas,app); break;
  46. case ViewFrequencySettings:
  47. case ViewModulationSettings:
  48. render_view_settings(canvas,app); break;
  49. case ViewDirectSampling: render_view_direct_sampling(canvas,app); break;
  50. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  51. }
  52. }
  53. /* Here all we do is putting the events into the queue that will be handled
  54. * in the while() loop of the app entry point function. */
  55. static void input_callback(InputEvent* input_event, void* ctx)
  56. {
  57. ProtoViewApp *app = ctx;
  58. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  59. }
  60. /* Called to switch view (when left/right is pressed). Handles
  61. * changing the current view ID and calling the enter/exit view
  62. * callbacks if needed. */
  63. static void app_switch_view(ProtoViewApp *app, SwitchViewDirection dir) {
  64. ProtoViewCurrentView old = app->current_view;
  65. if (dir == AppNextView) {
  66. app->current_view++;
  67. if (app->current_view == ViewLast) app->current_view = 0;
  68. } else if (dir == AppPrevView) {
  69. if (app->current_view == 0)
  70. app->current_view = ViewLast-1;
  71. else
  72. app->current_view--;
  73. }
  74. ProtoViewCurrentView new = app->current_view;
  75. /* Call the enter/exit view callbacks if needed. */
  76. if (old == ViewDirectSampling) view_exit_direct_sampling(app);
  77. if (new == ViewDirectSampling) view_enter_direct_sampling(app);
  78. /* The frequency/modulation settings are actually a single view:
  79. * as long as the user stays between the two modes of this view we
  80. * don't need to call the exit-view callback. */
  81. if ((old == ViewFrequencySettings && new != ViewModulationSettings) ||
  82. (old == ViewModulationSettings && new != ViewFrequencySettings))
  83. view_exit_settings(app);
  84. /* Set the current subview of the view we just left to zero, that is
  85. * the main subview of the view. When re re-enter it we want to see
  86. * the main thing. */
  87. app->current_subview[old] = 0;
  88. memset(app->view_privdata,0,PROTOVIEW_VIEW_PRIVDATA_LEN);
  89. }
  90. /* Allocate the application state and initialize a number of stuff.
  91. * This is called in the entry point to create the application state. */
  92. ProtoViewApp* protoview_app_alloc() {
  93. ProtoViewApp *app = malloc(sizeof(ProtoViewApp));
  94. // Init shared data structures
  95. RawSamples = raw_samples_alloc();
  96. DetectedSamples = raw_samples_alloc();
  97. //init setting
  98. app->setting = subghz_setting_alloc();
  99. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  100. // GUI
  101. app->gui = furi_record_open(RECORD_GUI);
  102. app->notification = furi_record_open(RECORD_NOTIFICATION);
  103. app->view_port = view_port_alloc();
  104. view_port_draw_callback_set(app->view_port, render_callback, app);
  105. view_port_input_callback_set(app->view_port, input_callback, app);
  106. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  107. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  108. app->view_dispatcher = NULL;
  109. app->text_input = NULL;
  110. app->show_text_input = false;
  111. app->current_view = ViewRawPulses;
  112. for (int j = 0; j < ViewLast; j++) app->current_subview[j] = 0;
  113. app->direct_sampling_enabled = false;
  114. app->view_privdata = malloc(PROTOVIEW_VIEW_PRIVDATA_LEN);
  115. memset(app->view_privdata,0,PROTOVIEW_VIEW_PRIVDATA_LEN);
  116. // Signal found and visualization defaults
  117. app->signal_bestlen = 0;
  118. app->signal_last_scan_idx = 0;
  119. app->signal_decoded = false;
  120. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  121. app->signal_offset = 0;
  122. app->msg_info = NULL;
  123. // Init Worker & Protocol
  124. app->txrx = malloc(sizeof(ProtoViewTxRx));
  125. /* Setup rx worker and environment. */
  126. app->txrx->freq_mod_changed = false;
  127. app->txrx->debug_timer_sampling = false;
  128. app->txrx->last_g0_change_time = DWT->CYCCNT;
  129. app->txrx->last_g0_value = false;
  130. app->txrx->worker = subghz_worker_alloc();
  131. #ifdef PROTOVIEW_DISABLE_SUBGHZ_FILTER
  132. app->txrx->worker->filter_running = 0;
  133. #endif
  134. app->txrx->environment = subghz_environment_alloc();
  135. subghz_environment_set_protocol_registry(
  136. app->txrx->environment, (void*)&protoview_protocol_registry);
  137. app->txrx->receiver =
  138. subghz_receiver_alloc_init(app->txrx->environment);
  139. subghz_receiver_set_filter(app->txrx->receiver,
  140. SubGhzProtocolFlag_Decodable);
  141. subghz_worker_set_overrun_callback(
  142. app->txrx->worker,
  143. (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  144. subghz_worker_set_pair_callback(
  145. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  146. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  147. app->frequency = subghz_setting_get_default_frequency(app->setting);
  148. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  149. furi_hal_power_suppress_charge_enter();
  150. app->running = 1;
  151. return app;
  152. }
  153. /* Free what the application allocated. It is not clear to me if the
  154. * Flipper OS, once the application exits, will be able to reclaim space
  155. * even if we forget to free something here. */
  156. void protoview_app_free(ProtoViewApp *app) {
  157. furi_assert(app);
  158. // Put CC1101 on sleep, this also restores charging.
  159. radio_sleep(app);
  160. // View related.
  161. view_port_enabled_set(app->view_port, false);
  162. gui_remove_view_port(app->gui, app->view_port);
  163. view_port_free(app->view_port);
  164. furi_record_close(RECORD_GUI);
  165. furi_record_close(RECORD_NOTIFICATION);
  166. furi_message_queue_free(app->event_queue);
  167. app->gui = NULL;
  168. // Frequency setting.
  169. subghz_setting_free(app->setting);
  170. // Worker stuff.
  171. if (!app->txrx->debug_timer_sampling) {
  172. subghz_receiver_free(app->txrx->receiver);
  173. subghz_environment_free(app->txrx->environment);
  174. subghz_worker_free(app->txrx->worker);
  175. }
  176. free(app->txrx);
  177. // Raw samples buffers.
  178. raw_samples_free(RawSamples);
  179. raw_samples_free(DetectedSamples);
  180. furi_hal_power_suppress_charge_exit();
  181. free(app);
  182. }
  183. /* Called periodically. Do signal processing here. Data we process here
  184. * will be later displayed by the render callback. The side effect of this
  185. * function is to scan for signals and set DetectedSamples. */
  186. static void timer_callback(void *ctx) {
  187. ProtoViewApp *app = ctx;
  188. uint32_t delta, lastidx = app->signal_last_scan_idx;
  189. /* scan_for_signal(), called by this function, deals with a
  190. * circular buffer. To never miss anything, even if a signal spawns
  191. * cross-boundaries, it is enough if we scan each time the buffer fills
  192. * for 50% more compared to the last scan. Thanks to this check we
  193. * can avoid scanning too many times to just find the same data. */
  194. if (lastidx < RawSamples->idx) {
  195. delta = RawSamples->idx - lastidx;
  196. } else {
  197. delta = RawSamples->total - lastidx + RawSamples->idx;
  198. }
  199. if (delta < RawSamples->total/2) return;
  200. app->signal_last_scan_idx = RawSamples->idx;
  201. scan_for_signal(app);
  202. }
  203. int32_t protoview_app_entry(void* p) {
  204. UNUSED(p);
  205. ProtoViewApp *app = protoview_app_alloc();
  206. /* Create a timer. We do data analysis in the callback. */
  207. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  208. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 8);
  209. /* Start listening to signals immediately. */
  210. radio_begin(app);
  211. radio_rx(app);
  212. /* This is the main event loop: here we get the events that are pushed
  213. * in the queue by input_callback(), and process them one after the
  214. * other. The timeout is 100 milliseconds, so if not input is received
  215. * before such time, we exit the queue_get() function and call
  216. * view_port_update() in order to refresh our screen content. */
  217. InputEvent input;
  218. while(app->running) {
  219. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  220. if (qstat == FuriStatusOk) {
  221. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  222. input.type, input.key);
  223. /* Handle navigation here. Then handle view-specific inputs
  224. * in the view specific handling function. */
  225. if (input.type == InputTypeShort &&
  226. input.key == InputKeyBack)
  227. {
  228. /* Exit the app. */
  229. app->running = 0;
  230. } else if (input.type == InputTypeShort &&
  231. input.key == InputKeyRight &&
  232. get_current_subview(app) == 0)
  233. {
  234. /* Go to the next view. */
  235. app_switch_view(app,AppNextView);
  236. } else if (input.type == InputTypeShort &&
  237. input.key == InputKeyLeft &&
  238. get_current_subview(app) == 0)
  239. {
  240. /* Go to the previous view. */
  241. app_switch_view(app,AppPrevView);
  242. } else {
  243. /* This is where we pass the control to the currently
  244. * active view input processing. */
  245. switch(app->current_view) {
  246. case ViewRawPulses:
  247. process_input_raw_pulses(app,input);
  248. break;
  249. case ViewInfo:
  250. process_input_info(app,input);
  251. break;
  252. case ViewFrequencySettings:
  253. case ViewModulationSettings:
  254. process_input_settings(app,input);
  255. break;
  256. case ViewDirectSampling:
  257. process_input_direct_sampling(app,input);
  258. break;
  259. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  260. }
  261. }
  262. } else {
  263. /* Useful to understand if the app is still alive when it
  264. * does not respond because of bugs. */
  265. if (DEBUG_MSG) {
  266. static int c = 0; c++;
  267. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  268. }
  269. }
  270. if (app->show_text_input) {
  271. /* Remove our viewport: we need to use a view dispatcher
  272. * in order to show the standard Flipper keyboard. */
  273. gui_remove_view_port(app->gui, app->view_port);
  274. /* Allocate a view dispatcher, add a text input view to it,
  275. * and activate it. */
  276. app->view_dispatcher = view_dispatcher_alloc();
  277. view_dispatcher_enable_queue(app->view_dispatcher);
  278. app->text_input = text_input_alloc();
  279. view_dispatcher_set_event_callback_context(app->view_dispatcher,app);
  280. view_dispatcher_add_view(app->view_dispatcher, 0, text_input_get_view(app->text_input));
  281. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  282. /* Setup the text input view. The different parameters are set
  283. * in the app structure by the view that wanted to show the
  284. * input text. The callback, buffer and buffer len must be set. */
  285. text_input_set_header_text(app->text_input, "Save signal filename");
  286. text_input_set_result_callback(
  287. app->text_input,
  288. app->text_input_done_callback,
  289. app,
  290. app->text_input_buffer,
  291. app->text_input_buffer_len,
  292. false);
  293. /* Run the dispatcher with the keyboard. */
  294. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  295. view_dispatcher_run(app->view_dispatcher);
  296. /* Undo all it: remove the view from the dispatcher, free it
  297. * so that it removes itself from the current gui, finally
  298. * restore our viewport. */
  299. view_dispatcher_remove_view(app->view_dispatcher, 0);
  300. text_input_free(app->text_input);
  301. view_dispatcher_free(app->view_dispatcher);
  302. app->view_dispatcher = NULL;
  303. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  304. app->show_text_input = false;
  305. } else {
  306. view_port_update(app->view_port);
  307. }
  308. }
  309. /* App no longer running. Shut down and free. */
  310. if (app->txrx->txrx_state == TxRxStateRx) {
  311. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  312. radio_rx_end(app);
  313. radio_sleep(app);
  314. }
  315. furi_timer_free(timer);
  316. protoview_app_free(app);
  317. return 0;
  318. }