app.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. ViewFrequencySettings,
  37. ViewModulationSettings,
  38. ViewLast, /* Just a sentinel to wrap around. */
  39. } ProtoViewCurrentView;
  40. typedef struct {
  41. const char *name;
  42. FuriHalSubGhzPreset preset;
  43. } ProtoViewModulation;
  44. extern ProtoViewModulation ProtoViewModulations[]; /* In app_subghz.c */
  45. /* This is the context of our subghz worker and associated thread.
  46. * It receives data and we get our protocol "feed" callback called
  47. * with the level (1 or 0) and duration. */
  48. struct ProtoViewTxRx {
  49. SubGhzWorker* worker; /* Our background worker. */
  50. SubGhzEnvironment* environment;
  51. SubGhzReceiver* receiver;
  52. TxRxState txrx_state; /* Receiving, idle or sleeping? */
  53. };
  54. typedef struct ProtoViewTxRx ProtoViewTxRx;
  55. struct ProtoViewApp {
  56. /* GUI */
  57. Gui *gui;
  58. ViewPort *view_port; /* We just use a raw viewport and we render
  59. everything into the low level canvas. */
  60. ProtoViewCurrentView current_view; /* Active view ID. */
  61. FuriMessageQueue *event_queue; /* Keypress events go here. */
  62. /* Radio related. */
  63. ProtoViewTxRx *txrx; /* Radio state. */
  64. SubGhzSetting *setting; /* A list of valid frequencies. */
  65. /* Generic app state. */
  66. int running; /* Once false exists the app. */
  67. uint32_t signal_bestlen; /* Longest coherent signal observed so far. */
  68. /* Raw view apps state. */
  69. uint32_t us_scale; /* microseconds per pixel. */
  70. uint32_t signal_offset; /* Long press left/right panning in raw view. */
  71. /* Configuration view app state. */
  72. uint32_t frequency; /* Current frequency. */
  73. uint8_t modulation; /* Current modulation ID, array index in the
  74. ProtoViewModulations table. */
  75. };
  76. /* This stucture is filled by the decoder for specific protocols with the
  77. * informations about the message. ProtoView will display such information
  78. * in the message info view. */
  79. #define PROTOVIEW_MSG_STR_LEN 16
  80. typedef struct ProtoViewMsgInfo {
  81. char name[PROTOVIEW_MSG_STR_LEN]; /* Protocol name and version. */
  82. char raw[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific raw representation.*/
  83. /* The following is what the decoder wants to show to user. Each decoder
  84. * can use the number of fileds it needs. */
  85. char info1[16]; /* Protocol specific decoded string, line 1. */
  86. char info2[16]; /* Protocol specific decoded string, line 2. */
  87. char info3[16]; /* Protocol specific decoded string, line 3. */
  88. uint64_t len; /* Bits found. */
  89. } ProtoViewMsgInfo;
  90. typedef struct ProtoViewDecoder {
  91. const char *name; /* Protocol name. */
  92. bool (*decode)(uint8_t *bits, uint64_t numbits, ProtoViewMsgInfo *info);
  93. } ProtoViewDecoder;
  94. extern RawSamplesBuffer *RawSamples, *DetectedSamples;
  95. /* app_radio.c */
  96. void radio_begin(ProtoViewApp* app);
  97. uint32_t radio_rx(ProtoViewApp* app);
  98. void radio_idle(ProtoViewApp* app);
  99. void radio_rx_end(ProtoViewApp* app);
  100. void radio_sleep(ProtoViewApp* app);
  101. /* signal.c */
  102. uint32_t duration_delta(uint32_t a, uint32_t b);
  103. void scan_for_signal(ProtoViewApp *app);
  104. bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos);
  105. bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits);
  106. uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, const char *bits);
  107. /* view_*.c */
  108. void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app);
  109. void process_input_raw_pulses(ProtoViewApp *app, InputEvent input);
  110. void render_view_settings(Canvas *const canvas, ProtoViewApp *app);
  111. void process_input_settings(ProtoViewApp *app, InputEvent input);
  112. /* ui.c */
  113. void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);