app.h 14 KB

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