app.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. }
  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->view_port = view_port_alloc();
  102. view_port_draw_callback_set(app->view_port, render_callback, app);
  103. view_port_input_callback_set(app->view_port, input_callback, app);
  104. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  105. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  106. app->current_view = ViewRawPulses;
  107. for (int j = 0; j < ViewLast; j++) app->current_subview[j] = 0;
  108. app->direct_sampling_enabled = false;
  109. // Signal found and visualization defaults
  110. app->signal_bestlen = 0;
  111. app->signal_last_scan_idx = 0;
  112. app->signal_decoded = false;
  113. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  114. app->signal_offset = 0;
  115. app->msg_info = NULL;
  116. //init Worker & Protocol
  117. app->txrx = malloc(sizeof(ProtoViewTxRx));
  118. /* Setup rx worker and environment. */
  119. app->txrx->freq_mod_changed = false;
  120. app->txrx->debug_timer_sampling = false;
  121. app->txrx->last_g0_change_time = DWT->CYCCNT;
  122. app->txrx->last_g0_value = false;
  123. app->txrx->worker = subghz_worker_alloc();
  124. #ifdef PROTOVIEW_DISABLE_SUBGHZ_FILTER
  125. app->txrx->worker->filter_running = 0;
  126. #endif
  127. app->txrx->environment = subghz_environment_alloc();
  128. subghz_environment_set_protocol_registry(
  129. app->txrx->environment, (void*)&protoview_protocol_registry);
  130. app->txrx->receiver =
  131. subghz_receiver_alloc_init(app->txrx->environment);
  132. subghz_receiver_set_filter(app->txrx->receiver,
  133. SubGhzProtocolFlag_Decodable);
  134. subghz_worker_set_overrun_callback(
  135. app->txrx->worker,
  136. (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  137. subghz_worker_set_pair_callback(
  138. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  139. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  140. app->frequency = subghz_setting_get_default_frequency(app->setting);
  141. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  142. furi_hal_power_suppress_charge_enter();
  143. app->running = 1;
  144. return app;
  145. }
  146. /* Free what the application allocated. It is not clear to me if the
  147. * Flipper OS, once the application exits, will be able to reclaim space
  148. * even if we forget to free something here. */
  149. void protoview_app_free(ProtoViewApp *app) {
  150. furi_assert(app);
  151. // Put CC1101 on sleep.
  152. radio_sleep(app);
  153. // View related.
  154. view_port_enabled_set(app->view_port, false);
  155. gui_remove_view_port(app->gui, app->view_port);
  156. view_port_free(app->view_port);
  157. furi_record_close(RECORD_GUI);
  158. furi_message_queue_free(app->event_queue);
  159. app->gui = NULL;
  160. // Frequency setting.
  161. subghz_setting_free(app->setting);
  162. // Worker stuff.
  163. if (!app->txrx->debug_timer_sampling) {
  164. subghz_receiver_free(app->txrx->receiver);
  165. subghz_environment_free(app->txrx->environment);
  166. subghz_worker_free(app->txrx->worker);
  167. }
  168. free(app->txrx);
  169. // Raw samples buffers.
  170. raw_samples_free(RawSamples);
  171. raw_samples_free(DetectedSamples);
  172. furi_hal_power_suppress_charge_exit();
  173. free(app);
  174. }
  175. /* Called periodically. Do signal processing here. Data we process here
  176. * will be later displayed by the render callback. The side effect of this
  177. * function is to scan for signals and set DetectedSamples. */
  178. static void timer_callback(void *ctx) {
  179. ProtoViewApp *app = ctx;
  180. uint32_t delta, lastidx = app->signal_last_scan_idx;
  181. /* scan_for_signal(), called by this function, deals with a
  182. * circular buffer. To never miss anything, even if a signal spawns
  183. * cross-boundaries, it is enough if we scan each time the buffer fills
  184. * for 50% more compared to the last scan. Thanks to this check we
  185. * can avoid scanning too many times to just find the same data. */
  186. if (lastidx < RawSamples->idx) {
  187. delta = RawSamples->idx - lastidx;
  188. } else {
  189. delta = RawSamples->total - lastidx + RawSamples->idx;
  190. }
  191. if (delta < RawSamples->total/2) return;
  192. app->signal_last_scan_idx = RawSamples->idx;
  193. scan_for_signal(app);
  194. }
  195. int32_t protoview_app_entry(void* p) {
  196. UNUSED(p);
  197. ProtoViewApp *app = protoview_app_alloc();
  198. /* Create a timer. We do data analysis in the callback. */
  199. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  200. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 8);
  201. /* Start listening to signals immediately. */
  202. radio_begin(app);
  203. radio_rx(app);
  204. /* This is the main event loop: here we get the events that are pushed
  205. * in the queue by input_callback(), and process them one after the
  206. * other. The timeout is 100 milliseconds, so if not input is received
  207. * before such time, we exit the queue_get() function and call
  208. * view_port_update() in order to refresh our screen content. */
  209. InputEvent input;
  210. while(app->running) {
  211. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  212. if (qstat == FuriStatusOk) {
  213. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  214. input.type, input.key);
  215. /* Handle navigation here. Then handle view-specific inputs
  216. * in the view specific handling function. */
  217. if (input.type == InputTypeShort &&
  218. input.key == InputKeyBack)
  219. {
  220. /* Exit the app. */
  221. app->running = 0;
  222. } else if (input.type == InputTypeShort &&
  223. input.key == InputKeyRight &&
  224. get_current_subview(app) == 0)
  225. {
  226. /* Go to the next view. */
  227. app_switch_view(app,AppNextView);
  228. } else if (input.type == InputTypeShort &&
  229. input.key == InputKeyLeft &&
  230. get_current_subview(app) == 0)
  231. {
  232. /* Go to the previous view. */
  233. app_switch_view(app,AppPrevView);
  234. } else {
  235. /* This is where we pass the control to the currently
  236. * active view input processing. */
  237. switch(app->current_view) {
  238. case ViewRawPulses:
  239. process_input_raw_pulses(app,input);
  240. break;
  241. case ViewInfo:
  242. process_input_info(app,input);
  243. break;
  244. case ViewFrequencySettings:
  245. case ViewModulationSettings:
  246. process_input_settings(app,input);
  247. break;
  248. case ViewDirectSampling:
  249. process_input_direct_sampling(app,input);
  250. break;
  251. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  252. }
  253. }
  254. } else {
  255. /* Useful to understand if the app is still alive when it
  256. * does not respond because of bugs. */
  257. if (DEBUG_MSG) {
  258. static int c = 0; c++;
  259. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  260. }
  261. }
  262. view_port_update(app->view_port);
  263. }
  264. /* App no longer running. Shut down and free. */
  265. if (app->txrx->txrx_state == TxRxStateRx) {
  266. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  267. radio_rx_end(app);
  268. radio_sleep(app);
  269. }
  270. furi_timer_free(timer);
  271. protoview_app_free(app);
  272. return 0;
  273. }