app.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. RawSamplesBuffer *RawSamples, *DetectedSamples;
  5. extern const SubGhzProtocolRegistry protoview_protocol_registry;
  6. /* Draw some text with a border. If the outside color is black and the inside
  7. * color is white, it just writes the border of the text, but the function can
  8. * also be used to write a bold variation of the font setting both the
  9. * colors to black, or alternatively to write a black text with a white
  10. * border so that it is visible if there are black stuff on the background. */
  11. /* The callback actually just passes the control to the actual active
  12. * view callback, after setting up basic stuff like cleaning the screen
  13. * and setting color to black. */
  14. static void render_callback(Canvas *const canvas, void *ctx) {
  15. ProtoViewApp *app = ctx;
  16. /* Clear screen. */
  17. canvas_set_color(canvas, ColorWhite);
  18. canvas_draw_box(canvas, 0, 0, 127, 63);
  19. canvas_set_color(canvas, ColorBlack);
  20. canvas_set_font(canvas, FontPrimary);
  21. /* Call who is in charge right now. */
  22. switch(app->current_view) {
  23. case ViewRawPulses: render_view_raw_pulses(canvas,app); break;
  24. case ViewInfo: render_view_info(canvas,app); break;
  25. case ViewFrequencySettings:
  26. case ViewModulationSettings:
  27. render_view_settings(canvas,app); break;
  28. case ViewDirectSampling: render_view_direct_sampling(canvas,app); break;
  29. case ViewBuildMessage: render_view_build_message(canvas,app); break;
  30. default: furi_crash(TAG "Invalid view selected"); break;
  31. }
  32. /* Draw the alert box if set. */
  33. ui_draw_alert_if_needed(canvas, app);
  34. }
  35. /* Here all we do is putting the events into the queue that will be handled
  36. * in the while() loop of the app entry point function. */
  37. static void input_callback(InputEvent* input_event, void* ctx)
  38. {
  39. ProtoViewApp *app = ctx;
  40. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  41. }
  42. /* Called to switch view (when left/right is pressed). Handles
  43. * changing the current view ID and calling the enter/exit view
  44. * callbacks if needed.
  45. *
  46. * The 'switchto' parameter can be the identifier of a view, or the
  47. * special views ViewGoNext and ViewGoPrev in order to move to
  48. * the logical next/prev view. */
  49. static void app_switch_view(ProtoViewApp *app, ProtoViewCurrentView switchto) {
  50. /* Switch to the specified view. */
  51. ProtoViewCurrentView old = app->current_view;
  52. if (switchto == ViewGoNext) {
  53. app->current_view++;
  54. if (app->current_view == ViewLast) app->current_view = 0;
  55. } else if (switchto == ViewGoPrev) {
  56. if (app->current_view == 0)
  57. app->current_view = ViewLast-1;
  58. else
  59. app->current_view--;
  60. } else {
  61. app->current_view = switchto;
  62. }
  63. ProtoViewCurrentView new = app->current_view;
  64. /* Call the exit view callbacks. */
  65. if (old == ViewDirectSampling) view_exit_direct_sampling(app);
  66. if (old == ViewBuildMessage) view_exit_build_message(app);
  67. if (old == ViewInfo) view_exit_info(app);
  68. /* The frequency/modulation settings are actually a single view:
  69. * as long as the user stays between the two modes of this view we
  70. * don't need to call the exit-view callback. */
  71. if ((old == ViewFrequencySettings && new != ViewModulationSettings) ||
  72. (old == ViewModulationSettings && new != ViewFrequencySettings))
  73. view_exit_settings(app);
  74. /* Reset the view private data each time, before calling the enter
  75. * callbacks that may want to setup some state. */
  76. memset(app->view_privdata,0,PROTOVIEW_VIEW_PRIVDATA_LEN);
  77. /* Call the enter view callbacks after all the exit callback
  78. * of the old view was already executed. */
  79. if (new == ViewDirectSampling) view_enter_direct_sampling(app);
  80. if (new == ViewBuildMessage) view_enter_build_message(app);
  81. /* Set the current subview of the view we just left to zero. This is
  82. * the main subview of the old view. When we re-enter the view we are
  83. * lefting, we want to see the main thing again. */
  84. app->current_subview[old] = 0;
  85. /* If there is an alert on screen, dismiss it: if the user is
  86. * switching view she already read it. */
  87. ui_dismiss_alert(app);
  88. }
  89. /* Allocate the application state and initialize a number of stuff.
  90. * This is called in the entry point to create the application state. */
  91. ProtoViewApp* protoview_app_alloc() {
  92. ProtoViewApp *app = malloc(sizeof(ProtoViewApp));
  93. // Init shared data structures
  94. RawSamples = raw_samples_alloc();
  95. DetectedSamples = raw_samples_alloc();
  96. //init setting
  97. app->setting = subghz_setting_alloc();
  98. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  99. // GUI
  100. app->gui = furi_record_open(RECORD_GUI);
  101. app->notification = furi_record_open(RECORD_NOTIFICATION);
  102. app->view_port = view_port_alloc();
  103. view_port_draw_callback_set(app->view_port, render_callback, app);
  104. view_port_input_callback_set(app->view_port, input_callback, app);
  105. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  106. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  107. app->view_dispatcher = NULL;
  108. app->text_input = NULL;
  109. app->show_text_input = false;
  110. app->alert_dismiss_time = 0;
  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 state. */
  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->frequency = subghz_setting_get_default_frequency(app->setting);
  131. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  132. furi_hal_power_suppress_charge_enter();
  133. app->running = 1;
  134. return app;
  135. }
  136. /* Free what the application allocated. It is not clear to me if the
  137. * Flipper OS, once the application exits, will be able to reclaim space
  138. * even if we forget to free something here. */
  139. void protoview_app_free(ProtoViewApp *app) {
  140. furi_assert(app);
  141. // Put CC1101 on sleep, this also restores charging.
  142. radio_sleep(app);
  143. // View related.
  144. view_port_enabled_set(app->view_port, false);
  145. gui_remove_view_port(app->gui, app->view_port);
  146. view_port_free(app->view_port);
  147. furi_record_close(RECORD_GUI);
  148. furi_record_close(RECORD_NOTIFICATION);
  149. furi_message_queue_free(app->event_queue);
  150. app->gui = NULL;
  151. // Frequency setting.
  152. subghz_setting_free(app->setting);
  153. // Worker stuff.
  154. free(app->txrx);
  155. // Raw samples buffers.
  156. raw_samples_free(RawSamples);
  157. raw_samples_free(DetectedSamples);
  158. furi_hal_power_suppress_charge_exit();
  159. free(app);
  160. }
  161. /* Called periodically. Do signal processing here. Data we process here
  162. * will be later displayed by the render callback. The side effect of this
  163. * function is to scan for signals and set DetectedSamples. */
  164. static void timer_callback(void *ctx) {
  165. ProtoViewApp *app = ctx;
  166. uint32_t delta, lastidx = app->signal_last_scan_idx;
  167. /* scan_for_signal(), called by this function, deals with a
  168. * circular buffer. To never miss anything, even if a signal spawns
  169. * cross-boundaries, it is enough if we scan each time the buffer fills
  170. * for 50% more compared to the last scan. Thanks to this check we
  171. * can avoid scanning too many times to just find the same data. */
  172. if (lastidx < RawSamples->idx) {
  173. delta = RawSamples->idx - lastidx;
  174. } else {
  175. delta = RawSamples->total - lastidx + RawSamples->idx;
  176. }
  177. if (delta < RawSamples->total/2) return;
  178. app->signal_last_scan_idx = RawSamples->idx;
  179. scan_for_signal(app,RawSamples);
  180. }
  181. /* This is the navigation callback we use in the view dispatcher used
  182. * to display the "text input" widget, that is the keyboard to get text.
  183. * The text input view is implemented to ignore the "back" short press,
  184. * so the event is not consumed and is handled by the view dispatcher.
  185. * However the view dispatcher implementation has the strange behavior that
  186. * if no navigation callback is set, it will not stop when handling back.
  187. *
  188. * We just need a dummy callback returning false. We believe the
  189. * implementation should be changed and if no callback is set, it should be
  190. * the same as returning false. */
  191. static bool keyboard_view_dispatcher_navigation_callback(void *ctx) {
  192. UNUSED(ctx);
  193. return false;
  194. }
  195. /* App entry point, as specified in application.fam. */
  196. int32_t protoview_app_entry(void* p) {
  197. UNUSED(p);
  198. ProtoViewApp *app = protoview_app_alloc();
  199. /* Create a timer. We do data analysis in the callback. */
  200. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  201. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 8);
  202. /* Start listening to signals immediately. */
  203. radio_begin(app);
  204. radio_rx(app);
  205. /* This is the main event loop: here we get the events that are pushed
  206. * in the queue by input_callback(), and process them one after the
  207. * other. The timeout is 100 milliseconds, so if not input is received
  208. * before such time, we exit the queue_get() function and call
  209. * view_port_update() in order to refresh our screen content. */
  210. InputEvent input;
  211. while(app->running) {
  212. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  213. if (qstat == FuriStatusOk) {
  214. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  215. input.type, input.key);
  216. /* Handle navigation here. Then handle view-specific inputs
  217. * in the view specific handling function. */
  218. if (input.type == InputTypeShort &&
  219. input.key == InputKeyBack)
  220. {
  221. if (app->current_view != ViewRawPulses) {
  222. /* If this is not the main app view, go there. */
  223. app_switch_view(app,ViewRawPulses);
  224. } else {
  225. /* If we are in the main app view, warn the user
  226. * they needs to long press to really quit. */
  227. ui_show_alert(app,"Long press to exit",1000);
  228. }
  229. } else if (input.type == InputTypeLong &&
  230. input.key == InputKeyBack)
  231. {
  232. app->running = 0;
  233. } else if (input.type == InputTypeShort &&
  234. input.key == InputKeyRight &&
  235. ui_get_current_subview(app) == 0)
  236. {
  237. /* Go to the next view. */
  238. app_switch_view(app,ViewGoNext);
  239. } else if (input.type == InputTypeShort &&
  240. input.key == InputKeyLeft &&
  241. ui_get_current_subview(app) == 0)
  242. {
  243. /* Go to the previous view. */
  244. app_switch_view(app,ViewGoPrev);
  245. } else {
  246. /* This is where we pass the control to the currently
  247. * active view input processing. */
  248. switch(app->current_view) {
  249. case ViewRawPulses:
  250. process_input_raw_pulses(app,input);
  251. break;
  252. case ViewInfo:
  253. process_input_info(app,input);
  254. break;
  255. case ViewFrequencySettings:
  256. case ViewModulationSettings:
  257. process_input_settings(app,input);
  258. break;
  259. case ViewDirectSampling:
  260. process_input_direct_sampling(app,input);
  261. break;
  262. case ViewBuildMessage:
  263. process_input_build_message(app,input);
  264. break;
  265. default: furi_crash(TAG "Invalid view selected"); break;
  266. }
  267. }
  268. } else {
  269. /* Useful to understand if the app is still alive when it
  270. * does not respond because of bugs. */
  271. if (DEBUG_MSG) {
  272. static int c = 0; c++;
  273. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  274. }
  275. }
  276. if (app->show_text_input) {
  277. /* Remove our viewport: we need to use a view dispatcher
  278. * in order to show the standard Flipper keyboard. */
  279. gui_remove_view_port(app->gui, app->view_port);
  280. /* Allocate a view dispatcher, add a text input view to it,
  281. * and activate it. */
  282. app->view_dispatcher = view_dispatcher_alloc();
  283. view_dispatcher_enable_queue(app->view_dispatcher);
  284. /* We need to set a navigation callback for the view dispatcher
  285. * otherwise when the user presses back on the keyboard to
  286. * abort, the dispatcher will not stop. */
  287. view_dispatcher_set_navigation_event_callback(
  288. app->view_dispatcher,
  289. keyboard_view_dispatcher_navigation_callback);
  290. app->text_input = text_input_alloc();
  291. view_dispatcher_set_event_callback_context(app->view_dispatcher,app);
  292. view_dispatcher_add_view(app->view_dispatcher, 0, text_input_get_view(app->text_input));
  293. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  294. /* Setup the text input view. The different parameters are set
  295. * in the app structure by the view that wanted to show the
  296. * input text. The callback, buffer and buffer len must be set. */
  297. text_input_set_header_text(app->text_input, "Save signal filename");
  298. text_input_set_result_callback(
  299. app->text_input,
  300. app->text_input_done_callback,
  301. app,
  302. app->text_input_buffer,
  303. app->text_input_buffer_len,
  304. false);
  305. /* Run the dispatcher with the keyboard. */
  306. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  307. view_dispatcher_run(app->view_dispatcher);
  308. /* Undo all it: remove the view from the dispatcher, free it
  309. * so that it removes itself from the current gui, finally
  310. * restore our viewport. */
  311. view_dispatcher_remove_view(app->view_dispatcher, 0);
  312. text_input_free(app->text_input);
  313. view_dispatcher_free(app->view_dispatcher);
  314. app->view_dispatcher = NULL;
  315. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  316. app->show_text_input = false;
  317. } else {
  318. view_port_update(app->view_port);
  319. }
  320. }
  321. /* App no longer running. Shut down and free. */
  322. if (app->txrx->txrx_state == TxRxStateRx) {
  323. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  324. radio_rx_end(app);
  325. radio_sleep(app);
  326. }
  327. furi_timer_free(timer);
  328. protoview_app_free(app);
  329. return 0;
  330. }