app.c 17 KB

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