app.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /* Allocate the application state and initialize a number of stuff.
  61. * This is called in the entry point to create the application state. */
  62. ProtoViewApp* protoview_app_alloc() {
  63. ProtoViewApp *app = malloc(sizeof(ProtoViewApp));
  64. // Init shared data structures
  65. RawSamples = raw_samples_alloc();
  66. DetectedSamples = raw_samples_alloc();
  67. //init setting
  68. app->setting = subghz_setting_alloc();
  69. subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
  70. // GUI
  71. app->gui = furi_record_open(RECORD_GUI);
  72. app->view_port = view_port_alloc();
  73. view_port_draw_callback_set(app->view_port, render_callback, app);
  74. view_port_input_callback_set(app->view_port, input_callback, app);
  75. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  76. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  77. app->current_view = ViewRawPulses;
  78. // Signal found and visualization defaults
  79. app->signal_bestlen = 0;
  80. app->signal_decoded = false;
  81. app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
  82. app->signal_offset = 0;
  83. //init Worker & Protocol
  84. app->txrx = malloc(sizeof(ProtoViewTxRx));
  85. /* Setup rx worker and environment. */
  86. app->txrx->worker = subghz_worker_alloc();
  87. #ifdef PROTOVIEW_DISABLE_SUBGHZ_FILTER
  88. app->txrx->worker->filter_running = 0;
  89. #endif
  90. app->txrx->environment = subghz_environment_alloc();
  91. subghz_environment_set_protocol_registry(
  92. app->txrx->environment, (void*)&protoview_protocol_registry);
  93. app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
  94. subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
  95. subghz_worker_set_overrun_callback(
  96. app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
  97. subghz_worker_set_pair_callback(
  98. app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
  99. subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
  100. app->frequency = subghz_setting_get_default_frequency(app->setting);
  101. app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
  102. furi_hal_power_suppress_charge_enter();
  103. app->running = 1;
  104. return app;
  105. }
  106. /* Free what the application allocated. It is not clear to me if the
  107. * Flipper OS, once the application exits, will be able to reclaim space
  108. * even if we forget to free something here. */
  109. void protoview_app_free(ProtoViewApp *app) {
  110. furi_assert(app);
  111. // Put CC1101 on sleep.
  112. radio_sleep(app);
  113. // View related.
  114. view_port_enabled_set(app->view_port, false);
  115. gui_remove_view_port(app->gui, app->view_port);
  116. view_port_free(app->view_port);
  117. furi_record_close(RECORD_GUI);
  118. furi_message_queue_free(app->event_queue);
  119. app->gui = NULL;
  120. // Frequency setting.
  121. subghz_setting_free(app->setting);
  122. // Worker stuff.
  123. subghz_receiver_free(app->txrx->receiver);
  124. subghz_environment_free(app->txrx->environment);
  125. subghz_worker_free(app->txrx->worker);
  126. free(app->txrx);
  127. // Raw samples buffers.
  128. raw_samples_free(RawSamples);
  129. raw_samples_free(DetectedSamples);
  130. furi_hal_power_suppress_charge_exit();
  131. free(app);
  132. }
  133. /* Called periodically. Do signal processing here. Data we process here
  134. * will be later displayed by the render callback. The side effect of this
  135. * function is to scan for signals and set DetectedSamples. */
  136. static void timer_callback(void *ctx) {
  137. ProtoViewApp *app = ctx;
  138. scan_for_signal(app);
  139. }
  140. int32_t protoview_app_entry(void* p) {
  141. UNUSED(p);
  142. ProtoViewApp *app = protoview_app_alloc();
  143. /* Create a timer. We do data analysis in the callback. */
  144. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  145. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  146. /* Start listening to signals immediately. */
  147. radio_begin(app);
  148. radio_rx(app);
  149. /* This is the main event loop: here we get the events that are pushed
  150. * in the queue by input_callback(), and process them one after the
  151. * other. The timeout is 100 milliseconds, so if not input is received
  152. * before such time, we exit the queue_get() function and call
  153. * view_port_update() in order to refresh our screen content. */
  154. InputEvent input;
  155. while(app->running) {
  156. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  157. if (qstat == FuriStatusOk) {
  158. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  159. input.type, input.key);
  160. /* Handle navigation here. Then handle view-specific inputs
  161. * in the view specific handling function. */
  162. if (input.type == InputTypeShort &&
  163. input.key == InputKeyBack)
  164. {
  165. /* Exit the app. */
  166. app->running = 0;
  167. } else if (input.type == InputTypeShort &&
  168. input.key == InputKeyRight)
  169. {
  170. /* Go to the next view. */
  171. app->current_view++;
  172. if (app->current_view == ViewLast) app->current_view = 0;
  173. } else if (input.type == InputTypeShort &&
  174. input.key == InputKeyLeft)
  175. {
  176. /* Go to the previous view. */
  177. if (app->current_view == 0)
  178. app->current_view = ViewLast-1;
  179. else
  180. app->current_view--;
  181. } else {
  182. /* This is where we pass the control to the currently
  183. * active view input processing. */
  184. switch(app->current_view) {
  185. case ViewRawPulses:
  186. process_input_raw_pulses(app,input);
  187. break;
  188. case ViewInfo:
  189. process_input_info(app,input);
  190. break;
  191. case ViewFrequencySettings:
  192. case ViewModulationSettings:
  193. process_input_settings(app,input);
  194. break;
  195. case ViewDirectSampling:
  196. process_input_direct_sampling(app,input);
  197. break;
  198. case ViewLast: furi_crash(TAG " ViewLast selected"); break;
  199. }
  200. }
  201. } else {
  202. /* Useful to understand if the app is still alive when it
  203. * does not respond because of bugs. */
  204. if (DEBUG_MSG) {
  205. static int c = 0; c++;
  206. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  207. }
  208. }
  209. view_port_update(app->view_port);
  210. }
  211. /* App no longer running. Shut down and free. */
  212. if (app->txrx->txrx_state == TxRxStateRx) {
  213. FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
  214. radio_rx_end(app);
  215. radio_sleep(app);
  216. }
  217. furi_timer_free(timer);
  218. protoview_app_free(app);
  219. return 0;
  220. }