app.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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++) app->current_subview[j] = 0;
  129. app->direct_sampling_enabled = false;
  130. app->view_privdata = malloc(PROTOVIEW_VIEW_PRIVDATA_LEN);
  131. memset(app->view_privdata, 0, PROTOVIEW_VIEW_PRIVDATA_LEN);
  132. // Signal found and visualization defaults
  133. app->signal_bestlen = 0;
  134. app->signal_last_scan_idx = 0;
  135. app->signal_decoded = false;
  136. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  137. app->signal_offset = 0;
  138. app->msg_info = NULL;
  139. // Init Worker & Protocol
  140. app->txrx = malloc(sizeof(ProtoViewTxRx));
  141. /* Setup rx state. */
  142. app->txrx->freq_mod_changed = false;
  143. app->txrx->debug_timer_sampling = false;
  144. app->txrx->last_g0_change_time = DWT->CYCCNT;
  145. app->txrx->last_g0_value = false;
  146. app->frequency = subghz_setting_get_default_frequency(app->setting);
  147. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  148. // Init & set radio_device
  149. subghz_devices_init();
  150. app->radio_device =
  151. radio_device_loader_set(app->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  152. subghz_devices_reset(app->radio_device);
  153. subghz_devices_idle(app->radio_device);
  154. app->running = 1;
  155. return app;
  156. }
  157. /* Free what the application allocated. It is not clear to me if the
  158. * Flipper OS, once the application exits, will be able to reclaim space
  159. * even if we forget to free something here. */
  160. void protoview_app_free(ProtoViewApp* app) {
  161. furi_assert(app);
  162. subghz_devices_sleep(app->radio_device);
  163. radio_device_loader_end(app->radio_device);
  164. subghz_devices_deinit();
  165. // View related.
  166. view_port_enabled_set(app->view_port, false);
  167. gui_remove_view_port(app->gui, app->view_port);
  168. view_port_free(app->view_port);
  169. furi_record_close(RECORD_GUI);
  170. furi_record_close(RECORD_NOTIFICATION);
  171. furi_message_queue_free(app->event_queue);
  172. furi_mutex_free(app->view_updating_mutex);
  173. app->gui = NULL;
  174. // Frequency setting.
  175. subghz_setting_free(app->setting);
  176. // Worker stuff.
  177. free(app->txrx);
  178. // Raw samples buffers.
  179. raw_samples_free(RawSamples);
  180. raw_samples_free(DetectedSamples);
  181. furi_hal_power_suppress_charge_exit();
  182. free(app);
  183. }
  184. /* Called periodically. Do signal processing here. Data we process here
  185. * will be later displayed by the render callback. The side effect of this
  186. * function is to scan for signals and set DetectedSamples. */
  187. static void timer_callback(void* ctx) {
  188. ProtoViewApp* app = ctx;
  189. uint32_t delta, lastidx = app->signal_last_scan_idx;
  190. /* scan_for_signal(), called by this function, deals with a
  191. * circular buffer. To never miss anything, even if a signal spawns
  192. * cross-boundaries, it is enough if we scan each time the buffer fills
  193. * for 50% more compared to the last scan. Thanks to this check we
  194. * can avoid scanning too many times to just find the same data. */
  195. if(lastidx < RawSamples->idx) {
  196. delta = RawSamples->idx - lastidx;
  197. } else {
  198. delta = RawSamples->total - lastidx + RawSamples->idx;
  199. }
  200. if(delta < RawSamples->total / 2) return;
  201. app->signal_last_scan_idx = RawSamples->idx;
  202. scan_for_signal(app, RawSamples, ProtoViewModulations[app->modulation].duration_filter);
  203. }
  204. /* This is the navigation callback we use in the view dispatcher used
  205. * to display the "text input" widget, that is the keyboard to get text.
  206. * The text input view is implemented to ignore the "back" short press,
  207. * so the event is not consumed and is handled by the view dispatcher.
  208. * However the view dispatcher implementation has the strange behavior that
  209. * if no navigation callback is set, it will not stop when handling back.
  210. *
  211. * We just need a dummy callback returning false. We believe the
  212. * implementation should be changed and if no callback is set, it should be
  213. * the same as returning false. */
  214. static bool keyboard_view_dispatcher_navigation_callback(void* ctx) {
  215. UNUSED(ctx);
  216. return false;
  217. }
  218. /* App entry point, as specified in application.fam. */
  219. int32_t protoview_app_entry(void* p) {
  220. UNUSED(p);
  221. ProtoViewApp* app = protoview_app_alloc();
  222. /* Create a timer. We do data analysis in the callback. */
  223. FuriTimer* timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  224. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 8);
  225. /* Start listening to signals immediately. */
  226. radio_begin(app);
  227. radio_rx(app);
  228. /* This is the main event loop: here we get the events that are pushed
  229. * in the queue by input_callback(), and process them one after the
  230. * other. The timeout is 100 milliseconds, so if not input is received
  231. * before such time, we exit the queue_get() function and call
  232. * view_port_update() in order to refresh our screen content. */
  233. InputEvent input;
  234. while(app->running) {
  235. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  236. if(qstat == FuriStatusOk) {
  237. if(DEBUG_MSG)
  238. FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u", input.type, input.key);
  239. /* Handle navigation here. Then handle view-specific inputs
  240. * in the view specific handling function. */
  241. if(input.type == InputTypeShort && input.key == InputKeyBack) {
  242. if(app->current_view != ViewRawPulses) {
  243. /* If this is not the main app view, go there. */
  244. app_switch_view(app, ViewRawPulses);
  245. } else {
  246. /* If we are in the main app view, warn the user
  247. * they needs to long press to really quit. */
  248. ui_show_alert(app, "Long press to exit", 1000);
  249. }
  250. } else if(input.type == InputTypeLong && input.key == InputKeyBack) {
  251. app->running = 0;
  252. } else if(
  253. input.type == InputTypeShort && input.key == InputKeyRight &&
  254. ui_get_current_subview(app) == 0) {
  255. /* Go to the next view. */
  256. app_switch_view(app, ViewGoNext);
  257. } else if(
  258. input.type == InputTypeShort && input.key == InputKeyLeft &&
  259. ui_get_current_subview(app) == 0) {
  260. /* Go to the previous view. */
  261. app_switch_view(app, ViewGoPrev);
  262. } else {
  263. /* This is where we pass the control to the currently
  264. * active view input processing. */
  265. switch(app->current_view) {
  266. case ViewRawPulses:
  267. process_input_raw_pulses(app, input);
  268. break;
  269. case ViewInfo:
  270. process_input_info(app, input);
  271. break;
  272. case ViewFrequencySettings:
  273. case ViewModulationSettings:
  274. process_input_settings(app, input);
  275. break;
  276. case ViewDirectSampling:
  277. process_input_direct_sampling(app, input);
  278. break;
  279. case ViewBuildMessage:
  280. process_input_build_message(app, input);
  281. break;
  282. default:
  283. furi_crash(TAG "Invalid view selected");
  284. break;
  285. }
  286. }
  287. } else {
  288. /* Useful to understand if the app is still alive when it
  289. * does not respond because of bugs. */
  290. if(DEBUG_MSG) {
  291. static int c = 0;
  292. c++;
  293. if(!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  294. }
  295. }
  296. if(app->show_text_input) {
  297. /* Remove our viewport: we need to use a view dispatcher
  298. * in order to show the standard Flipper keyboard. */
  299. gui_remove_view_port(app->gui, app->view_port);
  300. /* Allocate a view dispatcher, add a text input view to it,
  301. * and activate it. */
  302. app->view_dispatcher = view_dispatcher_alloc();
  303. /* We need to set a navigation callback for the view dispatcher
  304. * otherwise when the user presses back on the keyboard to
  305. * abort, the dispatcher will not stop. */
  306. view_dispatcher_set_navigation_event_callback(
  307. app->view_dispatcher, keyboard_view_dispatcher_navigation_callback);
  308. app->text_input = text_input_alloc();
  309. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  310. view_dispatcher_add_view(
  311. app->view_dispatcher, 0, text_input_get_view(app->text_input));
  312. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  313. /* Setup the text input view. The different parameters are set
  314. * in the app structure by the view that wanted to show the
  315. * input text. The callback, buffer and buffer len must be set. */
  316. text_input_set_header_text(app->text_input, "Save signal filename");
  317. text_input_set_result_callback(
  318. app->text_input,
  319. app->text_input_done_callback,
  320. app,
  321. app->text_input_buffer,
  322. app->text_input_buffer_len,
  323. false);
  324. /* Run the dispatcher with the keyboard. */
  325. view_dispatcher_attach_to_gui(
  326. app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  327. view_dispatcher_run(app->view_dispatcher);
  328. /* Undo all it: remove the view from the dispatcher, free it
  329. * so that it removes itself from the current gui, finally
  330. * restore our viewport. */
  331. view_dispatcher_remove_view(app->view_dispatcher, 0);
  332. text_input_free(app->text_input);
  333. view_dispatcher_free(app->view_dispatcher);
  334. app->view_dispatcher = NULL;
  335. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  336. app->show_text_input = false;
  337. } else {
  338. view_port_update(app->view_port);
  339. }
  340. }
  341. /* App no longer running. Shut down and free. */
  342. if(app->txrx->txrx_state == TxRxStateRx) {
  343. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  344. radio_rx_end(app);
  345. radio_sleep(app);
  346. }
  347. furi_timer_free(timer);
  348. protoview_app_free(app);
  349. return 0;
  350. }