app.c 16 KB

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