app.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #pragma once
  4. #include <furi.h>
  5. #include <furi_hal.h>
  6. #include <input/input.h>
  7. #include <gui/gui.h>
  8. #include <stdlib.h>
  9. #include <gui/gui.h>
  10. #include <gui/view_dispatcher.h>
  11. #include <gui/scene_manager.h>
  12. #include <gui/modules/submenu.h>
  13. #include <gui/modules/variable_item_list.h>
  14. #include <gui/modules/widget.h>
  15. #include <gui/modules/text_input.h>
  16. #include <notification/notification_messages.h>
  17. #include <lib/subghz/subghz_setting.h>
  18. #include <lib/subghz/registry.h>
  19. #include "raw_samples.h"
  20. #include "helpers/radio_device_loader.h"
  21. #define TAG "ProtoView"
  22. #define PROTOVIEW_RAW_VIEW_DEFAULT_SCALE 100 // 100us is 1 pixel by default
  23. #define BITMAP_SEEK_NOT_FOUND UINT32_MAX // Returned by function as sentinel
  24. #define PROTOVIEW_VIEW_PRIVDATA_LEN 64 // View specific private data len
  25. #define DEBUG_MSG 0
  26. /* Forward declarations. */
  27. typedef struct ProtoViewApp ProtoViewApp;
  28. typedef struct ProtoViewMsgInfo ProtoViewMsgInfo;
  29. typedef struct ProtoViewFieldSet ProtoViewFieldSet;
  30. typedef struct ProtoViewDecoder ProtoViewDecoder;
  31. /* ============================== enumerations ============================== */
  32. /* Subghz system state */
  33. typedef enum {
  34. TxRxStateIDLE,
  35. TxRxStateRx,
  36. TxRxStateTx,
  37. TxRxStateSleep,
  38. } TxRxState;
  39. /* Currently active view. */
  40. typedef enum {
  41. ViewRawPulses,
  42. ViewInfo,
  43. ViewFrequencySettings,
  44. ViewModulationSettings,
  45. ViewBuildMessage,
  46. ViewDirectSampling,
  47. ViewLast, /* Just a sentinel to wrap around. */
  48. /* The following are special views that are not iterated, but
  49. * have meaning for the API. */
  50. ViewGoNext,
  51. ViewGoPrev,
  52. } ProtoViewCurrentView;
  53. /* ================================== RX/TX ================================= */
  54. typedef struct {
  55. const char *name; // Name to show to the user.
  56. const char *id; // Identifier in the Flipper API/file.
  57. FuriHalSubGhzPreset preset; // The preset ID.
  58. uint8_t *custom; /* If not null, a set of registers for
  59. the CC1101, specifying a custom preset.*/
  60. uint32_t duration_filter; /* Ignore pulses and gaps that are less
  61. than the specified microseconds. This
  62. depends on the data rate. */
  63. } ProtoViewModulation;
  64. extern ProtoViewModulation ProtoViewModulations[]; /* In app_subghz.c */
  65. /* This is the context of our subghz worker and associated thread.
  66. * It receives data and we get our protocol "feed" callback called
  67. * with the level (1 or 0) and duration. */
  68. struct ProtoViewTxRx {
  69. bool freq_mod_changed; /* The user changed frequency and/or modulation
  70. from the interface. There is to restart the
  71. radio with the right parameters. */
  72. TxRxState txrx_state; /* Receiving, idle or sleeping? */
  73. /* Timer sampling mode state. */
  74. bool debug_timer_sampling; /* Read data from GDO0 in a busy loop. Only
  75. for testing. */
  76. uint32_t last_g0_change_time; /* Last high->low (or reverse) switch. */
  77. bool last_g0_value; /* Current value (high or low): we are
  78. checking the duration in the timer
  79. handler. */
  80. };
  81. typedef struct ProtoViewTxRx ProtoViewTxRx;
  82. /* ============================== Main app state ============================ */
  83. #define ALERT_MAX_LEN 32
  84. struct ProtoViewApp {
  85. /* GUI */
  86. Gui *gui;
  87. NotificationApp *notification;
  88. ViewPort *view_port; /* We just use a raw viewport and we render
  89. everything into the low level canvas. */
  90. ProtoViewCurrentView current_view; /* Active left-right view ID. */
  91. FuriMutex *view_updating_mutex; /* The Flipper GUI calls the screen redraw
  92. callback in a different thread. We
  93. use this mutex to protect the redraw
  94. from changes in app->view_privdata. */
  95. int current_subview[ViewLast]; /* Active up-down subview ID. */
  96. FuriMessageQueue *event_queue; /* Keypress events go here. */
  97. /* Input text state. */
  98. ViewDispatcher *view_dispatcher; /* Used only when we want to show
  99. the text_input view for a moment.
  100. Otherwise it is set to null. */
  101. TextInput *text_input;
  102. bool show_text_input;
  103. char *text_input_buffer;
  104. uint32_t text_input_buffer_len;
  105. void (*text_input_done_callback)(void*);
  106. /* Alert state. */
  107. uint32_t alert_dismiss_time; /* Millisecond when the alert will be
  108. no longer shown. Or zero if the alert
  109. is currently not set at all. */
  110. char alert_text[ALERT_MAX_LEN]; /* Alert content. */
  111. /* Radio related. */
  112. ProtoViewTxRx *txrx; /* Radio state. */
  113. SubGhzSetting *setting; /* A list of valid frequencies. */
  114. const SubGhzDevice* radio_device;
  115. /* Generic app state. */
  116. int running; /* Once false exists the app. */
  117. uint32_t signal_bestlen; /* Longest coherent signal observed so far. */
  118. uint32_t signal_last_scan_idx; /* Index of the buffer last time we
  119. performed the scan. */
  120. bool signal_decoded; /* Was the current signal decoded? */
  121. ProtoViewMsgInfo *msg_info; /* Decoded message info if not NULL. */
  122. bool direct_sampling_enabled; /* This special view needs an explicit
  123. acknowledge to work. */
  124. void *view_privdata; /* This is a piece of memory of total size
  125. PROTOVIEW_VIEW_PRIVDATA_LEN that it is
  126. initialized to zero when we switch to
  127. a a new view. While the view we are using
  128. is the same, it can be used by the view to
  129. store any kind of info inside, just casting
  130. the pointer to a few specific-data structure. */
  131. /* Raw view apps state. */
  132. uint32_t us_scale; /* microseconds per pixel. */
  133. uint32_t signal_offset; /* Long press left/right panning in raw view. */
  134. /* Configuration view app state. */
  135. uint32_t frequency; /* Current frequency. */
  136. uint8_t modulation; /* Current modulation ID, array index in the
  137. ProtoViewModulations table. */
  138. };
  139. /* =========================== Protocols decoders =========================== */
  140. /* This stucture is filled by the decoder for specific protocols with the
  141. * informations about the message. ProtoView will display such information
  142. * in the message info view. */
  143. #define PROTOVIEW_MSG_STR_LEN 32
  144. typedef struct ProtoViewMsgInfo {
  145. ProtoViewDecoder *decoder; /* The decoder that decoded the message. */
  146. ProtoViewFieldSet *fieldset; /* Decoded fields. */
  147. /* Low level information of the detected signal: the following are filled
  148. * by the protocol decoding function: */
  149. uint32_t start_off; /* Pulses start offset in the bitmap. */
  150. uint32_t pulses_count; /* Number of pulses of the full message. */
  151. /* The following are passed already filled to the decoder. */
  152. uint32_t short_pulse_dur; /* Microseconds duration of the short pulse. */
  153. /* The following are filled by ProtoView core after the decoder returned
  154. * success. */
  155. uint8_t *bits; /* Bitmap with the signal. */
  156. uint32_t bits_bytes; /* Number of full bytes in the bitmap, that
  157. is 'pulses_count/8' rounded to the next
  158. integer. */
  159. } ProtoViewMsgInfo;
  160. /* This structures describe a set of protocol fields. It is used by decoders
  161. * supporting message building to receive and return information about the
  162. * protocol. */
  163. typedef enum {
  164. FieldTypeStr,
  165. FieldTypeSignedInt,
  166. FieldTypeUnsignedInt,
  167. FieldTypeBinary,
  168. FieldTypeHex,
  169. FieldTypeBytes,
  170. FieldTypeFloat,
  171. } ProtoViewFieldType;
  172. typedef struct {
  173. ProtoViewFieldType type;
  174. uint32_t len; // Depends on type:
  175. // Bits for integers (signed,unsigned,binary,hex).
  176. // Number of characters for strings.
  177. // Number of nibbles for bytes (1 for each 4 bits).
  178. // Number of digits after dot for floats.
  179. char *name; // Field name.
  180. union {
  181. char *str; // String type.
  182. int64_t value; // Signed integer type.
  183. uint64_t uvalue; // Unsigned integer type.
  184. uint8_t *bytes; // Raw bytes type.
  185. float fvalue; // Float type.
  186. };
  187. } ProtoViewField;
  188. typedef struct ProtoViewFieldSet {
  189. ProtoViewField **fields;
  190. uint32_t numfields;
  191. } ProtoViewFieldSet;
  192. typedef struct ProtoViewDecoder {
  193. const char *name; /* Protocol name. */
  194. /* The decode function takes a buffer that is actually a bitmap, with
  195. * high and low levels represented as 0 and 1. The number of high/low
  196. * pulses represented by the bitmap is passed as the 'numbits' argument,
  197. * while 'numbytes' represents the total size of the bitmap pointed by
  198. * 'bits'. So 'numbytes' is mainly useful to pass as argument to other
  199. * functions that perform bit extraction with bound checking, such as
  200. * bitmap_get() and so forth. */
  201. bool (*decode)(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info);
  202. /* This method is used by the decoder to return the fields it needs
  203. * in order to build a new message. This way the message builder view
  204. * can ask the user to fill the right set of fields of the specified
  205. * type. */
  206. void (*get_fields)(ProtoViewFieldSet *fields);
  207. /* This method takes the fields supported by the decoder, and
  208. * renders a message in 'samples'. */
  209. void (*build_message)(RawSamplesBuffer *samples, ProtoViewFieldSet *fields);
  210. } ProtoViewDecoder;
  211. extern RawSamplesBuffer *RawSamples, *DetectedSamples;
  212. /* app_subghz.c */
  213. void radio_begin(ProtoViewApp* app);
  214. uint32_t radio_rx(ProtoViewApp* app);
  215. void radio_idle(ProtoViewApp* app);
  216. void radio_rx_end(ProtoViewApp* app);
  217. void radio_sleep(ProtoViewApp* app);
  218. void raw_sampling_worker_start(ProtoViewApp *app);
  219. void raw_sampling_worker_stop(ProtoViewApp *app);
  220. void radio_tx_signal(ProtoViewApp *app, FuriHalSubGhzAsyncTxCallback data_feeder, void *ctx);
  221. void protoview_rx_callback(bool level, uint32_t duration, void* context);
  222. /* signal.c */
  223. uint32_t duration_delta(uint32_t a, uint32_t b);
  224. void reset_current_signal(ProtoViewApp *app);
  225. void scan_for_signal(ProtoViewApp *app,RawSamplesBuffer *source,uint32_t min_duration);
  226. bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos);
  227. void bitmap_set(uint8_t *b, uint32_t blen, uint32_t bitpos, bool val);
  228. void bitmap_copy(uint8_t *d, uint32_t dlen, uint32_t doff, uint8_t *s, uint32_t slen, uint32_t soff, uint32_t count);
  229. void bitmap_set_pattern(uint8_t *b, uint32_t blen, uint32_t off, const char *pat);
  230. void bitmap_reverse_bytes_bits(uint8_t *p, uint32_t len);
  231. bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits);
  232. uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, uint32_t maxbits, const char *bits);
  233. bool bitmap_match_bitmap(uint8_t *b1, uint32_t b1len, uint32_t b1off,
  234. uint8_t *b2, uint32_t b2len, uint32_t b2off,
  235. uint32_t cmplen);
  236. void bitmap_to_string(char *dst, uint8_t *b, uint32_t blen,
  237. uint32_t off, uint32_t len);
  238. uint32_t convert_from_line_code(uint8_t *buf, uint64_t buflen, uint8_t *bits, uint32_t len, uint32_t offset, const char *zero_pattern, const char *one_pattern);
  239. uint32_t convert_from_diff_manchester(uint8_t *buf, uint64_t buflen, uint8_t *bits, uint32_t len, uint32_t off, bool previous);
  240. void init_msg_info(ProtoViewMsgInfo *i, ProtoViewApp *app);
  241. void free_msg_info(ProtoViewMsgInfo *i);
  242. /* signal_file.c */
  243. bool save_signal(ProtoViewApp *app, const char *filename);
  244. /* view_*.c */
  245. void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app);
  246. void process_input_raw_pulses(ProtoViewApp *app, InputEvent input);
  247. void render_view_settings(Canvas *const canvas, ProtoViewApp *app);
  248. void process_input_settings(ProtoViewApp *app, InputEvent input);
  249. void render_view_info(Canvas *const canvas, ProtoViewApp *app);
  250. void process_input_info(ProtoViewApp *app, InputEvent input);
  251. void render_view_direct_sampling(Canvas *const canvas, ProtoViewApp *app);
  252. void process_input_direct_sampling(ProtoViewApp *app, InputEvent input);
  253. void render_view_build_message(Canvas *const canvas, ProtoViewApp *app);
  254. void process_input_build_message(ProtoViewApp *app, InputEvent input);
  255. void view_enter_build_message(ProtoViewApp *app);
  256. void view_exit_build_message(ProtoViewApp *app);
  257. void view_enter_direct_sampling(ProtoViewApp *app);
  258. void view_exit_direct_sampling(ProtoViewApp *app);
  259. void view_exit_settings(ProtoViewApp *app);
  260. void view_exit_info(ProtoViewApp *app);
  261. void adjust_raw_view_scale(ProtoViewApp *app, uint32_t short_pulse_dur);
  262. /* ui.c */
  263. int ui_get_current_subview(ProtoViewApp *app);
  264. void ui_show_available_subviews(Canvas *canvas, ProtoViewApp *app, int last_subview);
  265. bool ui_process_subview_updown(ProtoViewApp *app, InputEvent input, int last_subview);
  266. void ui_show_keyboard(ProtoViewApp *app, char *buffer, uint32_t buflen,
  267. void (*done_callback)(void*));
  268. void ui_dismiss_keyboard(ProtoViewApp *app);
  269. void ui_show_alert(ProtoViewApp *app, const char *text, uint32_t ttl);
  270. void ui_dismiss_alert(ProtoViewApp *app);
  271. void ui_draw_alert_if_needed(Canvas *canvas, ProtoViewApp *app);
  272. void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);
  273. /* fields.c */
  274. void fieldset_free(ProtoViewFieldSet *fs);
  275. ProtoViewFieldSet *fieldset_new(void);
  276. void fieldset_add_int(ProtoViewFieldSet *fs, const char *name, int64_t val, uint8_t bits);
  277. void fieldset_add_uint(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits);
  278. void fieldset_add_hex(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits);
  279. void fieldset_add_bin(ProtoViewFieldSet *fs, const char *name, uint64_t uval, uint8_t bits);
  280. void fieldset_add_str(ProtoViewFieldSet *fs, const char *name, const char *s, size_t len);
  281. void fieldset_add_bytes(ProtoViewFieldSet *fs, const char *name, const uint8_t *bytes, uint32_t count);
  282. void fieldset_add_float(ProtoViewFieldSet *fs, const char *name, float val, uint32_t digits_after_dot);
  283. const char *field_get_type_name(ProtoViewField *f);
  284. int field_to_string(char *buf, size_t len, ProtoViewField *f);
  285. bool field_set_from_string(ProtoViewField *f, char *buf, size_t len);
  286. bool field_incr_value(ProtoViewField *f, int incr);
  287. void fieldset_copy_matching_fields(ProtoViewFieldSet *dst, ProtoViewFieldSet *src);
  288. void field_set_from_field(ProtoViewField *dst, ProtoViewField *src);
  289. /* crc.c */
  290. uint8_t crc8(const uint8_t *data, size_t len, uint8_t init, uint8_t poly);
  291. uint8_t sum_bytes(const uint8_t *data, size_t len, uint8_t init);
  292. uint8_t xor_bytes(const uint8_t *data, size_t len, uint8_t init);