app.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 <notification/notification_messages.h>
  16. #include <lib/subghz/subghz_setting.h>
  17. #include <lib/subghz/subghz_worker.h>
  18. #include <lib/subghz/receiver.h>
  19. #include <lib/subghz/transmitter.h>
  20. #include <lib/subghz/registry.h>
  21. #include "app_buffer.h"
  22. #define TAG "ProtoView"
  23. #define PROTOVIEW_RAW_VIEW_DEFAULT_SCALE 100
  24. #define BITMAP_SEEK_NOT_FOUND UINT32_MAX
  25. #define DEBUG_MSG 1
  26. typedef struct ProtoViewApp ProtoViewApp;
  27. /* Subghz system state */
  28. typedef enum {
  29. TxRxStateIDLE,
  30. TxRxStateRx,
  31. TxRxStateSleep,
  32. } TxRxState;
  33. /* Currently active view. */
  34. typedef enum {
  35. ViewRawPulses,
  36. ViewInfo,
  37. ViewFrequencySettings,
  38. ViewModulationSettings,
  39. ViewLast, /* Just a sentinel to wrap around. */
  40. } ProtoViewCurrentView;
  41. typedef struct {
  42. const char *name;
  43. FuriHalSubGhzPreset preset;
  44. uint8_t *custom;
  45. } ProtoViewModulation;
  46. extern ProtoViewModulation ProtoViewModulations[]; /* In app_subghz.c */
  47. /* This is the context of our subghz worker and associated thread.
  48. * It receives data and we get our protocol "feed" callback called
  49. * with the level (1 or 0) and duration. */
  50. struct ProtoViewTxRx {
  51. SubGhzWorker* worker; /* Our background worker. */
  52. SubGhzEnvironment* environment;
  53. SubGhzReceiver* receiver;
  54. TxRxState txrx_state; /* Receiving, idle or sleeping? */
  55. };
  56. typedef struct ProtoViewTxRx ProtoViewTxRx;
  57. /* This stucture is filled by the decoder for specific protocols with the
  58. * informations about the message. ProtoView will display such information
  59. * in the message info view. */
  60. #define PROTOVIEW_MSG_STR_LEN 32
  61. typedef struct ProtoViewMsgInfo {
  62. char name[PROTOVIEW_MSG_STR_LEN]; /* Protocol name and version. */
  63. char raw[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific raw representation.*/
  64. /* The following is what the decoder wants to show to user. Each decoder
  65. * can use the number of fileds it needs. */
  66. char info1[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific info line 1. */
  67. char info2[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific info line 2. */
  68. char info3[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific info line 3. */
  69. uint64_t len; /* Bits consumed from the stream. */
  70. } ProtoViewMsgInfo;
  71. struct ProtoViewApp {
  72. /* GUI */
  73. Gui *gui;
  74. ViewPort *view_port; /* We just use a raw viewport and we render
  75. everything into the low level canvas. */
  76. ProtoViewCurrentView current_view; /* Active view ID. */
  77. FuriMessageQueue *event_queue; /* Keypress events go here. */
  78. /* Radio related. */
  79. ProtoViewTxRx *txrx; /* Radio state. */
  80. SubGhzSetting *setting; /* A list of valid frequencies. */
  81. /* Generic app state. */
  82. int running; /* Once false exists the app. */
  83. uint32_t signal_bestlen; /* Longest coherent signal observed so far. */
  84. bool signal_decoded; /* Was the current signal decoded? */
  85. ProtoViewMsgInfo signal_info; /* Decoded message, if signal_decoded true. */
  86. /* Raw view apps state. */
  87. uint32_t us_scale; /* microseconds per pixel. */
  88. uint32_t signal_offset; /* Long press left/right panning in raw view. */
  89. /* Configuration view app state. */
  90. uint32_t frequency; /* Current frequency. */
  91. uint8_t modulation; /* Current modulation ID, array index in the
  92. ProtoViewModulations table. */
  93. };
  94. typedef struct ProtoViewDecoder {
  95. const char *name; /* Protocol name. */
  96. /* The decode function takes a buffer that is actually a bitmap, with
  97. * high and low levels represented as 0 and 1. The number of high/low
  98. * pulses represented by the bitmap is passed as the 'numbits' argument,
  99. * while 'numbytes' represents the total size of the bitmap pointed by
  100. * 'bits'. So 'numbytes' is mainly useful to pass as argument to other
  101. * functions that perform bit extraction with bound checking, such as
  102. * bitmap_get() and so forth. */
  103. bool (*decode)(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info);
  104. } ProtoViewDecoder;
  105. extern RawSamplesBuffer *RawSamples, *DetectedSamples;
  106. /* app_radio.c */
  107. void radio_begin(ProtoViewApp* app);
  108. uint32_t radio_rx(ProtoViewApp* app);
  109. void radio_idle(ProtoViewApp* app);
  110. void radio_rx_end(ProtoViewApp* app);
  111. void radio_sleep(ProtoViewApp* app);
  112. /* signal.c */
  113. uint32_t duration_delta(uint32_t a, uint32_t b);
  114. void reset_current_signal(ProtoViewApp *app);
  115. void scan_for_signal(ProtoViewApp *app);
  116. bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos);
  117. void bitmap_set(uint8_t *b, uint32_t blen, uint32_t bitpos, bool val);
  118. void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat);
  119. void bitmap_invert_bytes_bits(uint8_t *p, uint32_t len);
  120. bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits);
  121. uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, const char *bits);
  122. 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);
  123. /* view_*.c */
  124. void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app);
  125. void process_input_raw_pulses(ProtoViewApp *app, InputEvent input);
  126. void render_view_settings(Canvas *const canvas, ProtoViewApp *app);
  127. void process_input_settings(ProtoViewApp *app, InputEvent input);
  128. void render_view_info(Canvas *const canvas, ProtoViewApp *app);
  129. void process_input_info(ProtoViewApp *app, InputEvent input);
  130. /* ui.c */
  131. void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);