mirror of
https://github.com/UberGuidoZ/Flipper.git
synced 2026-09-18 17:01:29 +00:00
Removing FAP files (use FlipC or Official store!)
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
Copyright (c) 2022-2023 Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright notice,
|
||||
this list of conditions and the following disclaimer.
|
||||
|
||||
* Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,103 @@
|
||||
ProtoView is a digital signal detection and visualization tool for the
|
||||
[Flipper Zero](https://flipperzero.one/). The Flipper is able to identify
|
||||
a great deal of RF protocols, however when the exact protocol is not
|
||||
implemented (and there are many proprietary ones, such as the ones of
|
||||
the car keys), the curious person is left wondering what the device is
|
||||
sending at all. Using ProtoView she or he can visualize the high and low pulses
|
||||
like in the example image below (showing a Volkswagen key in 2FSK):
|
||||
|
||||

|
||||
|
||||
This is often enough to make an initial idea about the encoding used
|
||||
and if the selected modulation is correct.
|
||||
|
||||
The secondary goal of ProtoView is to provide a somewhat-documented application
|
||||
for the Flipper (even if ProtoView is a pretty atypical application: doesn't make use of the standard widgets and other abstractions provded by the framework).
|
||||
Many apps dealing with the *subghz subsystem* (the Flipper
|
||||
abstraction to work with the [CC1101 chip](https://www.ti.com/product/CC1101))
|
||||
tend to be complicated and completely undocumented. This is unfortunately
|
||||
true for the firmware of the device itself. It's a shame because especially
|
||||
in the case of code that talks with hardware peripherals there are tons
|
||||
of assumptions and hard-gained lessons that can [only be captured by comments and are in the code only implicitly](http://antirez.com/news/124).
|
||||
|
||||
However, the Flipper firmware source code is well written even if it
|
||||
lacks comments and documentation, so it is possible to make some ideas of
|
||||
how things work just grepping inside.
|
||||
|
||||
# Detection algorithm
|
||||
|
||||
In order to show unknown signals, the application attempts to understand if
|
||||
the samples obtained by the Flipper API (a series of pulses that are high
|
||||
or low, and with different duration in microseconds) look like belonging to
|
||||
a legitimate signal, and aren't just noise.
|
||||
|
||||
We can't make assumptions about
|
||||
the encoding and the data rate of the communication, so we use a simple
|
||||
but relatively effective algorithm. As we check the signal, we try to detect
|
||||
long parts of it that are composed of pulses roughly classifiable into
|
||||
a maximum of three different classes of lengths, plus or minus 10%. Most
|
||||
encodings are somewhat self-clocked, so they tend to have just two or
|
||||
three classes of pulse lengths.
|
||||
|
||||
However often pulses of the same theoretical
|
||||
length have slightly different lenghts in the case of high and low level
|
||||
(RF on or off), so we classify them separately for robustness.
|
||||
|
||||
# Usage
|
||||
|
||||
The application shows the longest coherent signal detected so far.
|
||||
|
||||
* The OK button resets the current signal.
|
||||
* The UP and DOWN buttons change the scale. Default is 100us per pixel.
|
||||
* The LEFT and RIGHT buttons switch to settings.
|
||||
|
||||
Under the detected sequence, you will see a small triangle marking a
|
||||
specific sample. This mark means that the sequence looked coherent up
|
||||
to that point, and starting from there it could be just noise.
|
||||
|
||||
In the bottom-right corner the application displays an amount of time
|
||||
in microseconds. This is the average length of the shortest pulse length
|
||||
detected among the three classes. Usually the *data rate* of the protocol
|
||||
is something like `1000000/this-number*2`, but it depends on the encoding
|
||||
and could actually be `1000000/this-number*N` with `N > 2` (here 1000000
|
||||
is the number of microseconds in one second, and N is the number of clock
|
||||
cycles needed to represent a bit).
|
||||
|
||||
Things to investigate:
|
||||
|
||||
* Many cheap remotes (gate openers, remotes, ...) are on the 433.92Mhz or nearby and use OOK modulation.
|
||||
* Weather stations are often too in the 433.92Mhz OOK.
|
||||
* For car keys, try 443.92 OOK650 and 868.35 Mhz in OOK or 2FSK.
|
||||
|
||||
# Installing the app from source
|
||||
|
||||
* Download the Flipper Zero dev kit and build it:
|
||||
```
|
||||
mkdir -p ~/flipperZero/official/
|
||||
cd ~/flipperZero/official/
|
||||
git clone --recursive https://github.com/flipperdevices/flipperzero-firmware.git ./
|
||||
./fbt
|
||||
```
|
||||
* Copy this application folder in `official/application_user`.
|
||||
* Connect your Flipper via USB.
|
||||
* Build and install with: `./fbt launch_app APPSRC=protoview`.
|
||||
|
||||
# Installing the binary file (no build needed)
|
||||
|
||||
Drop the `protoview.fap` file you can find in the `binaries` folder into the
|
||||
following Flipper Zero location:
|
||||
|
||||
/ext/apps/Tools
|
||||
|
||||
The `ext` part means that we are in the SD card. So if you don't want
|
||||
to use the Android (or other) application to upload the file,
|
||||
you can just take out the SD card, insert it in your computer,
|
||||
copy the fine into `apps/Tools`, and that's it.
|
||||
|
||||
# License
|
||||
|
||||
The code is released under the BSD license.
|
||||
|
||||
# Disclaimer
|
||||
|
||||
This application is only provided as an educational tool. The author is not liable in case the application is used to reverse engineer protocols protected by IP or for any other illegal purpose.
|
||||
@@ -0,0 +1,20 @@
|
||||
Core improvements
|
||||
=================
|
||||
|
||||
- Detection of non Manchester and non RZ encoded signals. Not sure if there are any signals that are not self clocked widely used in RF. Note that the current approach already detects encodings using short high + long low and long high + short low to encode 0 and 1. In addition to the current classifier, it is possible to add one that checks for a sequence of pulses that are all multiples of some base length. This should detect, for instance, even NRZ encodings where 1 and 0 are just clocked as they are.
|
||||
|
||||
- Views on-enter on-exit.
|
||||
|
||||
Features
|
||||
========
|
||||
|
||||
- Help screen (with press ok for next page).
|
||||
- Detect the line code used and try to decode the message as hex dump.
|
||||
- Pressing right/left you browse different modes:
|
||||
* Current best signal pulse classes.
|
||||
* Raw square wave display. Central button freezes and resumes (toggle). When frozen we display "paused" (inverted) on the low part of the screen.
|
||||
|
||||
Screens sequence (user can navigate with <- and ->):
|
||||
|
||||
(default)
|
||||
[settings] <> [freq] <> [pulses view] <> [raw square view] <> [signal info]
|
||||
@@ -0,0 +1,225 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
|
||||
RawSamplesBuffer *RawSamples, *DetectedSamples;
|
||||
extern const SubGhzProtocolRegistry protoview_protocol_registry;
|
||||
|
||||
/* Draw some text with a border. If the outside color is black and the inside
|
||||
* color is white, it just writes the border of the text, but the function can
|
||||
* also be used to write a bold variation of the font setting both the
|
||||
* colors to black, or alternatively to write a black text with a white
|
||||
* border so that it is visible if there are black stuff on the background. */
|
||||
/* The callback actually just passes the control to the actual active
|
||||
* view callback, after setting up basic stuff like cleaning the screen
|
||||
* and setting color to black. */
|
||||
static void render_callback(Canvas *const canvas, void *ctx) {
|
||||
ProtoViewApp *app = ctx;
|
||||
|
||||
/* Clear screen. */
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_box(canvas, 0, 0, 127, 63);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
|
||||
/* Call who is in charge right now. */
|
||||
switch(app->current_view) {
|
||||
case ViewRawPulses: render_view_raw_pulses(canvas,app); break;
|
||||
case ViewInfo: render_view_info(canvas,app); break;
|
||||
case ViewFrequencySettings:
|
||||
case ViewModulationSettings:
|
||||
render_view_settings(canvas,app); break;
|
||||
case ViewLast: furi_crash(TAG " ViewLast selected"); break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Here all we do is putting the events into the queue that will be handled
|
||||
* in the while() loop of the app entry point function. */
|
||||
static void input_callback(InputEvent* input_event, void* ctx)
|
||||
{
|
||||
ProtoViewApp *app = ctx;
|
||||
furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
|
||||
}
|
||||
|
||||
/* Allocate the application state and initialize a number of stuff.
|
||||
* This is called in the entry point to create the application state. */
|
||||
ProtoViewApp* protoview_app_alloc() {
|
||||
ProtoViewApp *app = malloc(sizeof(ProtoViewApp));
|
||||
|
||||
// Init shared data structures
|
||||
RawSamples = raw_samples_alloc();
|
||||
DetectedSamples = raw_samples_alloc();
|
||||
|
||||
//init setting
|
||||
app->setting = subghz_setting_alloc();
|
||||
subghz_setting_load(app->setting, EXT_PATH("subghz/assets/setting_user"));
|
||||
|
||||
// GUI
|
||||
app->gui = furi_record_open(RECORD_GUI);
|
||||
app->view_port = view_port_alloc();
|
||||
view_port_draw_callback_set(app->view_port, render_callback, app);
|
||||
view_port_input_callback_set(app->view_port, input_callback, app);
|
||||
gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
|
||||
app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
|
||||
app->current_view = ViewRawPulses;
|
||||
|
||||
// Signal found and visualization defaults
|
||||
app->signal_bestlen = 0;
|
||||
app->signal_decoded = false;
|
||||
app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
|
||||
app->signal_offset = 0;
|
||||
|
||||
//init Worker & Protocol
|
||||
app->txrx = malloc(sizeof(ProtoViewTxRx));
|
||||
|
||||
/* Setup rx worker and environment. */
|
||||
app->txrx->worker = subghz_worker_alloc();
|
||||
app->txrx->environment = subghz_environment_alloc();
|
||||
subghz_environment_set_protocol_registry(
|
||||
app->txrx->environment, (void*)&protoview_protocol_registry);
|
||||
app->txrx->receiver = subghz_receiver_alloc_init(app->txrx->environment);
|
||||
|
||||
subghz_receiver_set_filter(app->txrx->receiver, SubGhzProtocolFlag_Decodable);
|
||||
subghz_worker_set_overrun_callback(
|
||||
app->txrx->worker, (SubGhzWorkerOverrunCallback)subghz_receiver_reset);
|
||||
subghz_worker_set_pair_callback(
|
||||
app->txrx->worker, (SubGhzWorkerPairCallback)subghz_receiver_decode);
|
||||
subghz_worker_set_context(app->txrx->worker, app->txrx->receiver);
|
||||
|
||||
app->frequency = subghz_setting_get_default_frequency(app->setting);
|
||||
app->modulation = 0; /* Defaults to ProtoViewModulations[0]. */
|
||||
|
||||
furi_hal_power_suppress_charge_enter();
|
||||
app->running = 1;
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
/* Free what the application allocated. It is not clear to me if the
|
||||
* Flipper OS, once the application exits, will be able to reclaim space
|
||||
* even if we forget to free something here. */
|
||||
void protoview_app_free(ProtoViewApp *app) {
|
||||
furi_assert(app);
|
||||
|
||||
// Put CC1101 on sleep.
|
||||
radio_sleep(app);
|
||||
|
||||
// View related.
|
||||
view_port_enabled_set(app->view_port, false);
|
||||
gui_remove_view_port(app->gui, app->view_port);
|
||||
view_port_free(app->view_port);
|
||||
furi_record_close(RECORD_GUI);
|
||||
furi_message_queue_free(app->event_queue);
|
||||
app->gui = NULL;
|
||||
|
||||
// Frequency setting.
|
||||
subghz_setting_free(app->setting);
|
||||
|
||||
// Worker stuff.
|
||||
subghz_receiver_free(app->txrx->receiver);
|
||||
subghz_environment_free(app->txrx->environment);
|
||||
subghz_worker_free(app->txrx->worker);
|
||||
free(app->txrx);
|
||||
|
||||
// Raw samples buffers.
|
||||
raw_samples_free(RawSamples);
|
||||
raw_samples_free(DetectedSamples);
|
||||
furi_hal_power_suppress_charge_exit();
|
||||
|
||||
free(app);
|
||||
}
|
||||
|
||||
/* Called periodically. Do signal processing here. Data we process here
|
||||
* will be later displayed by the render callback. The side effect of this
|
||||
* function is to scan for signals and set DetectedSamples. */
|
||||
static void timer_callback(void *ctx) {
|
||||
ProtoViewApp *app = ctx;
|
||||
scan_for_signal(app);
|
||||
}
|
||||
|
||||
int32_t protoview_app_entry(void* p) {
|
||||
UNUSED(p);
|
||||
ProtoViewApp *app = protoview_app_alloc();
|
||||
|
||||
/* Create a timer. We do data analysis in the callback. */
|
||||
FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
|
||||
furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
|
||||
|
||||
/* Start listening to signals immediately. */
|
||||
radio_begin(app);
|
||||
radio_rx(app);
|
||||
|
||||
/* This is the main event loop: here we get the events that are pushed
|
||||
* in the queue by input_callback(), and process them one after the
|
||||
* other. The timeout is 100 milliseconds, so if not input is received
|
||||
* before such time, we exit the queue_get() function and call
|
||||
* view_port_update() in order to refresh our screen content. */
|
||||
InputEvent input;
|
||||
while(app->running) {
|
||||
FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
|
||||
if (qstat == FuriStatusOk) {
|
||||
if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
|
||||
input.type, input.key);
|
||||
|
||||
/* Handle navigation here. Then handle view-specific inputs
|
||||
* in the view specific handling function. */
|
||||
if (input.type == InputTypeShort &&
|
||||
input.key == InputKeyBack)
|
||||
{
|
||||
/* Exit the app. */
|
||||
app->running = 0;
|
||||
} else if (input.type == InputTypeShort &&
|
||||
input.key == InputKeyRight)
|
||||
{
|
||||
/* Go to the next view. */
|
||||
app->current_view++;
|
||||
if (app->current_view == ViewLast) app->current_view = 0;
|
||||
} else if (input.type == InputTypeShort &&
|
||||
input.key == InputKeyLeft)
|
||||
{
|
||||
/* Go to the previous view. */
|
||||
if (app->current_view == 0)
|
||||
app->current_view = ViewLast-1;
|
||||
else
|
||||
app->current_view--;
|
||||
} else {
|
||||
/* This is where we pass the control to the currently
|
||||
* active view input processing. */
|
||||
switch(app->current_view) {
|
||||
case ViewRawPulses:
|
||||
process_input_raw_pulses(app,input);
|
||||
break;
|
||||
case ViewInfo:
|
||||
process_input_info(app,input);
|
||||
break;
|
||||
case ViewFrequencySettings:
|
||||
case ViewModulationSettings:
|
||||
process_input_settings(app,input);
|
||||
break;
|
||||
case ViewLast: furi_crash(TAG " ViewLast selected"); break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/* Useful to understand if the app is still alive when it
|
||||
* does not respond because of bugs. */
|
||||
if (DEBUG_MSG) {
|
||||
static int c = 0; c++;
|
||||
if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
|
||||
}
|
||||
}
|
||||
view_port_update(app->view_port);
|
||||
}
|
||||
|
||||
/* App no longer running. Shut down and free. */
|
||||
if (app->txrx->txrx_state == TxRxStateRx) {
|
||||
FURI_LOG_E(TAG, "Putting CC1101 to sleep before exiting.");
|
||||
radio_rx_end(app);
|
||||
radio_sleep(app);
|
||||
}
|
||||
|
||||
furi_timer_free(timer);
|
||||
protoview_app_free(app);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include <input/input.h>
|
||||
#include <gui/gui.h>
|
||||
#include <stdlib.h>
|
||||
#include <gui/gui.h>
|
||||
#include <gui/view_dispatcher.h>
|
||||
#include <gui/scene_manager.h>
|
||||
#include <gui/modules/submenu.h>
|
||||
#include <gui/modules/variable_item_list.h>
|
||||
#include <gui/modules/widget.h>
|
||||
#include <notification/notification_messages.h>
|
||||
#include <lib/subghz/subghz_setting.h>
|
||||
#include <lib/subghz/subghz_worker.h>
|
||||
#include <lib/subghz/receiver.h>
|
||||
#include <lib/subghz/transmitter.h>
|
||||
#include <lib/subghz/registry.h>
|
||||
#include "app_buffer.h"
|
||||
|
||||
#define TAG "ProtoView"
|
||||
#define PROTOVIEW_RAW_VIEW_DEFAULT_SCALE 100
|
||||
#define BITMAP_SEEK_NOT_FOUND UINT32_MAX
|
||||
|
||||
#define DEBUG_MSG 1
|
||||
|
||||
typedef struct ProtoViewApp ProtoViewApp;
|
||||
|
||||
/* Subghz system state */
|
||||
typedef enum {
|
||||
TxRxStateIDLE,
|
||||
TxRxStateRx,
|
||||
TxRxStateSleep,
|
||||
} TxRxState;
|
||||
|
||||
/* Currently active view. */
|
||||
typedef enum {
|
||||
ViewRawPulses,
|
||||
ViewInfo,
|
||||
ViewFrequencySettings,
|
||||
ViewModulationSettings,
|
||||
ViewLast, /* Just a sentinel to wrap around. */
|
||||
} ProtoViewCurrentView;
|
||||
|
||||
typedef struct {
|
||||
const char *name;
|
||||
FuriHalSubGhzPreset preset;
|
||||
uint8_t *custom;
|
||||
} ProtoViewModulation;
|
||||
|
||||
extern ProtoViewModulation ProtoViewModulations[]; /* In app_subghz.c */
|
||||
|
||||
/* This is the context of our subghz worker and associated thread.
|
||||
* It receives data and we get our protocol "feed" callback called
|
||||
* with the level (1 or 0) and duration. */
|
||||
struct ProtoViewTxRx {
|
||||
SubGhzWorker* worker; /* Our background worker. */
|
||||
SubGhzEnvironment* environment;
|
||||
SubGhzReceiver* receiver;
|
||||
TxRxState txrx_state; /* Receiving, idle or sleeping? */
|
||||
};
|
||||
|
||||
typedef struct ProtoViewTxRx ProtoViewTxRx;
|
||||
|
||||
/* This stucture is filled by the decoder for specific protocols with the
|
||||
* informations about the message. ProtoView will display such information
|
||||
* in the message info view. */
|
||||
#define PROTOVIEW_MSG_STR_LEN 32
|
||||
typedef struct ProtoViewMsgInfo {
|
||||
char name[PROTOVIEW_MSG_STR_LEN]; /* Protocol name and version. */
|
||||
char raw[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific raw representation.*/
|
||||
/* The following is what the decoder wants to show to user. Each decoder
|
||||
* can use the number of fileds it needs. */
|
||||
char info1[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific info line 1. */
|
||||
char info2[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific info line 2. */
|
||||
char info3[PROTOVIEW_MSG_STR_LEN]; /* Protocol specific info line 3. */
|
||||
uint64_t len; /* Bits consumed from the stream. */
|
||||
} ProtoViewMsgInfo;
|
||||
|
||||
struct ProtoViewApp {
|
||||
/* GUI */
|
||||
Gui *gui;
|
||||
ViewPort *view_port; /* We just use a raw viewport and we render
|
||||
everything into the low level canvas. */
|
||||
ProtoViewCurrentView current_view; /* Active view ID. */
|
||||
FuriMessageQueue *event_queue; /* Keypress events go here. */
|
||||
|
||||
/* Radio related. */
|
||||
ProtoViewTxRx *txrx; /* Radio state. */
|
||||
SubGhzSetting *setting; /* A list of valid frequencies. */
|
||||
|
||||
/* Generic app state. */
|
||||
int running; /* Once false exists the app. */
|
||||
uint32_t signal_bestlen; /* Longest coherent signal observed so far. */
|
||||
bool signal_decoded; /* Was the current signal decoded? */
|
||||
ProtoViewMsgInfo signal_info; /* Decoded message, if signal_decoded true. */
|
||||
|
||||
/* Raw view apps state. */
|
||||
uint32_t us_scale; /* microseconds per pixel. */
|
||||
uint32_t signal_offset; /* Long press left/right panning in raw view. */
|
||||
|
||||
/* Configuration view app state. */
|
||||
uint32_t frequency; /* Current frequency. */
|
||||
uint8_t modulation; /* Current modulation ID, array index in the
|
||||
ProtoViewModulations table. */
|
||||
};
|
||||
|
||||
typedef struct ProtoViewDecoder {
|
||||
const char *name; /* Protocol name. */
|
||||
/* The decode function takes a buffer that is actually a bitmap, with
|
||||
* high and low levels represented as 0 and 1. The number of high/low
|
||||
* pulses represented by the bitmap is passed as the 'numbits' argument,
|
||||
* while 'numbytes' represents the total size of the bitmap pointed by
|
||||
* 'bits'. So 'numbytes' is mainly useful to pass as argument to other
|
||||
* functions that perform bit extraction with bound checking, such as
|
||||
* bitmap_get() and so forth. */
|
||||
bool (*decode)(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info);
|
||||
} ProtoViewDecoder;
|
||||
|
||||
extern RawSamplesBuffer *RawSamples, *DetectedSamples;
|
||||
|
||||
/* app_radio.c */
|
||||
void radio_begin(ProtoViewApp* app);
|
||||
uint32_t radio_rx(ProtoViewApp* app);
|
||||
void radio_idle(ProtoViewApp* app);
|
||||
void radio_rx_end(ProtoViewApp* app);
|
||||
void radio_sleep(ProtoViewApp* app);
|
||||
|
||||
/* signal.c */
|
||||
uint32_t duration_delta(uint32_t a, uint32_t b);
|
||||
void reset_current_signal(ProtoViewApp *app);
|
||||
void scan_for_signal(ProtoViewApp *app);
|
||||
bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos);
|
||||
void bitmap_set(uint8_t *b, uint32_t blen, uint32_t bitpos, bool val);
|
||||
void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat);
|
||||
void bitmap_invert_bytes_bits(uint8_t *p, uint32_t len);
|
||||
bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits);
|
||||
uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, uint32_t maxbits, const char *bits);
|
||||
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);
|
||||
|
||||
/* view_*.c */
|
||||
void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app);
|
||||
void process_input_raw_pulses(ProtoViewApp *app, InputEvent input);
|
||||
void render_view_settings(Canvas *const canvas, ProtoViewApp *app);
|
||||
void process_input_settings(ProtoViewApp *app, InputEvent input);
|
||||
void render_view_info(Canvas *const canvas, ProtoViewApp *app);
|
||||
void process_input_info(ProtoViewApp *app, InputEvent input);
|
||||
|
||||
/* ui.c */
|
||||
void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color);
|
||||
@@ -0,0 +1,73 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <furi/core/string.h>
|
||||
#include <furi.h>
|
||||
#include <furi_hal.h>
|
||||
#include "app_buffer.h"
|
||||
|
||||
/* Allocate and initialize a samples buffer. */
|
||||
RawSamplesBuffer *raw_samples_alloc(void) {
|
||||
RawSamplesBuffer *buf = malloc(sizeof(*buf));
|
||||
buf->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
|
||||
raw_samples_reset(buf);
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* Free a sample buffer. Should be called when the mutex is released. */
|
||||
void raw_samples_free(RawSamplesBuffer *s) {
|
||||
furi_mutex_free(s->mutex);
|
||||
free(s);
|
||||
}
|
||||
|
||||
/* This just set all the samples to zero and also resets the internal
|
||||
* index. There is no need to call it after raw_samples_alloc(), but only
|
||||
* when one wants to reset the whole buffer of samples. */
|
||||
void raw_samples_reset(RawSamplesBuffer *s) {
|
||||
furi_mutex_acquire(s->mutex,FuriWaitForever);
|
||||
s->total = RAW_SAMPLES_NUM;
|
||||
s->idx = 0;
|
||||
s->short_pulse_dur = 0;
|
||||
memset(s->level,0,sizeof(s->level));
|
||||
memset(s->dur,0,sizeof(s->dur));
|
||||
furi_mutex_release(s->mutex);
|
||||
}
|
||||
|
||||
/* Set the raw sample internal index so that what is currently at
|
||||
* offset 'offset', will appear to be at 0 index. */
|
||||
void raw_samples_center(RawSamplesBuffer *s, uint32_t offset) {
|
||||
s->idx = (s->idx+offset) % RAW_SAMPLES_NUM;
|
||||
}
|
||||
|
||||
/* Add the specified sample in the circular buffer. */
|
||||
void raw_samples_add(RawSamplesBuffer *s, bool level, uint32_t dur) {
|
||||
furi_mutex_acquire(s->mutex,FuriWaitForever);
|
||||
s->level[s->idx] = level;
|
||||
s->dur[s->idx] = dur;
|
||||
s->idx = (s->idx+1) % RAW_SAMPLES_NUM;
|
||||
furi_mutex_release(s->mutex);
|
||||
}
|
||||
|
||||
/* Get the sample from the buffer. It is possible to use out of range indexes
|
||||
* as 'idx' because the modulo operation will rewind back from the start. */
|
||||
void raw_samples_get(RawSamplesBuffer *s, uint32_t idx, bool *level, uint32_t *dur)
|
||||
{
|
||||
furi_mutex_acquire(s->mutex,FuriWaitForever);
|
||||
idx = (s->idx + idx) % RAW_SAMPLES_NUM;
|
||||
*level = s->level[idx];
|
||||
*dur = s->dur[idx];
|
||||
furi_mutex_release(s->mutex);
|
||||
}
|
||||
|
||||
/* Copy one buffer to the other, including current index. */
|
||||
void raw_samples_copy(RawSamplesBuffer *dst, RawSamplesBuffer *src) {
|
||||
furi_mutex_acquire(src->mutex,FuriWaitForever);
|
||||
furi_mutex_acquire(dst->mutex,FuriWaitForever);
|
||||
dst->idx = src->idx;
|
||||
dst->short_pulse_dur = src->short_pulse_dur;
|
||||
memcpy(dst->level,src->level,sizeof(dst->level));
|
||||
memcpy(dst->dur,src->dur,sizeof(dst->dur));
|
||||
furi_mutex_release(src->mutex);
|
||||
furi_mutex_release(dst->mutex);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
/* Our circular buffer of raw samples, used in order to display
|
||||
* the signal. */
|
||||
|
||||
#define RAW_SAMPLES_NUM 2048 /* Use a power of two: we take the modulo
|
||||
of the index quite often to normalize inside
|
||||
the range, and division is slow. */
|
||||
|
||||
typedef struct RawSamplesBuffer {
|
||||
FuriMutex *mutex;
|
||||
uint8_t level[RAW_SAMPLES_NUM];
|
||||
uint32_t dur[RAW_SAMPLES_NUM];
|
||||
uint32_t idx; /* Current idx (next to write). */
|
||||
uint32_t total; /* Total samples: same as RAW_SAMPLES_NUM, we provide
|
||||
this field for a cleaner interface with the user, but
|
||||
we always use RAW_SAMPLES_NUM when taking the modulo so
|
||||
the compiler can optimize % as bit masking. */
|
||||
/* Signal features. */
|
||||
uint32_t short_pulse_dur; /* Duration of the shortest pulse. */
|
||||
} RawSamplesBuffer;
|
||||
|
||||
RawSamplesBuffer *raw_samples_alloc(void);
|
||||
void raw_samples_reset(RawSamplesBuffer *s);
|
||||
void raw_samples_center(RawSamplesBuffer *s, uint32_t offset);
|
||||
void raw_samples_add(RawSamplesBuffer *s, bool level, uint32_t dur);
|
||||
void raw_samples_get(RawSamplesBuffer *s, uint32_t idx, bool *level, uint32_t *dur);
|
||||
void raw_samples_copy(RawSamplesBuffer *dst, RawSamplesBuffer *src);
|
||||
void raw_samples_free(RawSamplesBuffer *s);
|
||||
@@ -0,0 +1,85 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
#include "custom_presets.h"
|
||||
|
||||
#include <flipper_format/flipper_format_i.h>
|
||||
|
||||
ProtoViewModulation ProtoViewModulations[] = {
|
||||
{"OOK 650Khz", FuriHalSubGhzPresetOok650Async, NULL},
|
||||
{"OOK 270Khz", FuriHalSubGhzPresetOok270Async, NULL},
|
||||
{"2FSK 2.38Khz", FuriHalSubGhzPreset2FSKDev238Async, NULL},
|
||||
{"2FSK 47.6Khz", FuriHalSubGhzPreset2FSKDev476Async, NULL},
|
||||
{"MSK", FuriHalSubGhzPresetMSK99_97KbAsync, NULL},
|
||||
{"GFSK", FuriHalSubGhzPresetGFSK9_99KbAsync, NULL},
|
||||
{"FSK for TPMS", 0, (uint8_t*)protoview_subghz_tpms_async_regs},
|
||||
{NULL, 0, NULL} /* End of list sentinel. */
|
||||
};
|
||||
|
||||
/* Called after the application initialization in order to setup the
|
||||
* subghz system and put it into idle state. If the user wants to start
|
||||
* receiving we will call radio_rx() to start a receiving worker and
|
||||
* associated thread. */
|
||||
void radio_begin(ProtoViewApp* app) {
|
||||
furi_assert(app);
|
||||
furi_hal_subghz_reset();
|
||||
furi_hal_subghz_idle();
|
||||
|
||||
/* The CC1101 preset can be either one of the standard presets, if
|
||||
* the modulation "custom" field is NULL, or a custom preset we
|
||||
* defined in custom_presets.h. */
|
||||
if (ProtoViewModulations[app->modulation].custom == NULL)
|
||||
furi_hal_subghz_load_preset(ProtoViewModulations[app->modulation].preset);
|
||||
else
|
||||
furi_hal_subghz_load_custom_preset(ProtoViewModulations[app->modulation].custom);
|
||||
furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
||||
app->txrx->txrx_state = TxRxStateIDLE;
|
||||
}
|
||||
|
||||
/* Setup subghz to start receiving using a background worker. */
|
||||
uint32_t radio_rx(ProtoViewApp* app) {
|
||||
furi_assert(app);
|
||||
if(!furi_hal_subghz_is_frequency_valid(app->frequency)) {
|
||||
furi_crash(TAG" Incorrect RX frequency.");
|
||||
}
|
||||
|
||||
if (app->txrx->txrx_state == TxRxStateRx) return app->frequency;
|
||||
|
||||
furi_hal_subghz_idle(); /* Put it into idle state in case it is sleeping. */
|
||||
uint32_t value = furi_hal_subghz_set_frequency_and_path(app->frequency);
|
||||
FURI_LOG_E(TAG, "Switched to frequency: %lu", value);
|
||||
furi_hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
|
||||
furi_hal_subghz_flush_rx();
|
||||
furi_hal_subghz_rx();
|
||||
|
||||
furi_hal_subghz_start_async_rx(subghz_worker_rx_callback, app->txrx->worker);
|
||||
subghz_worker_start(app->txrx->worker);
|
||||
app->txrx->txrx_state = TxRxStateRx;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* Stop subghz worker (if active), put radio on idle state. */
|
||||
void radio_rx_end(ProtoViewApp* app) {
|
||||
furi_assert(app);
|
||||
if (app->txrx->txrx_state == TxRxStateRx) {
|
||||
if(subghz_worker_is_running(app->txrx->worker)) {
|
||||
subghz_worker_stop(app->txrx->worker);
|
||||
furi_hal_subghz_stop_async_rx();
|
||||
}
|
||||
}
|
||||
furi_hal_subghz_idle();
|
||||
app->txrx->txrx_state = TxRxStateIDLE;
|
||||
}
|
||||
|
||||
/* Put radio on sleep. */
|
||||
void radio_sleep(ProtoViewApp* app) {
|
||||
furi_assert(app);
|
||||
if (app->txrx->txrx_state == TxRxStateRx) {
|
||||
/* We can't go from having an active RX worker to sleeping.
|
||||
* Stop the RX subsystems first. */
|
||||
radio_rx_end(app);
|
||||
}
|
||||
furi_hal_subghz_sleep();
|
||||
app->txrx->txrx_state = TxRxStateSleep;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 116 B |
@@ -0,0 +1,12 @@
|
||||
App(
|
||||
appid="protoview",
|
||||
name="Protocols visualizer",
|
||||
apptype=FlipperAppType.EXTERNAL,
|
||||
entry_point="protoview_app_entry",
|
||||
cdefines=["APP_PROTOVIEW"],
|
||||
requires=["gui"],
|
||||
stack_size=8 * 1024,
|
||||
order=50,
|
||||
fap_icon="appicon.png",
|
||||
fap_category="Misc_Extra",
|
||||
)
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <cc1101.h>
|
||||
|
||||
static uint8_t protoview_subghz_tpms_async_regs[][2] = {
|
||||
/* GPIO GD0 */
|
||||
{CC1101_IOCFG0, 0x0D}, // GD0 as async serial data output/input
|
||||
|
||||
/* Frequency Synthesizer Control */
|
||||
{CC1101_FSCTRL1, 0x06}, // IF = (26*10^6) / (2^10) * 0x06 = 152343.75Hz
|
||||
|
||||
/* Packet engine */
|
||||
{CC1101_PKTCTRL0, 0x32}, // Async, continious, no whitening
|
||||
{CC1101_PKTCTRL1, 0x04},
|
||||
|
||||
// // Modem Configuration
|
||||
{CC1101_MDMCFG0, 0x00},
|
||||
{CC1101_MDMCFG1, 0x02},
|
||||
{CC1101_MDMCFG2, 0x04}, // Format 2-FSK/FM, No preamble/sync, Disable (current optimized). Other code reading TPMS uses GFSK, but should be the same when in RX mode.
|
||||
{CC1101_MDMCFG3, 0x93}, // Data rate is 20kBaud
|
||||
{CC1101_MDMCFG4, 0x59}, // Rx bandwidth filter is 325 kHz
|
||||
{CC1101_DEVIATN, 0x41}, // Deviation 28.56 kHz
|
||||
|
||||
/* Main Radio Control State Machine */
|
||||
{CC1101_MCSM0, 0x18}, // Autocalibrate on idle-to-rx/tx, PO_TIMEOUT is 64 cycles(149-155us)
|
||||
|
||||
/* Frequency Offset Compensation Configuration */
|
||||
{CC1101_FOCCFG,
|
||||
0x16}, // no frequency offset compensation, POST_K same as PRE_K, PRE_K is 4K, GATE is off
|
||||
|
||||
/* Automatic Gain Control */
|
||||
{CC1101_AGCCTRL0,
|
||||
0x91}, //10 - Medium hysteresis, medium asymmetric dead zone, medium gain ; 01 - 16 samples agc; 00 - Normal AGC, 01 - 8dB boundary
|
||||
{CC1101_AGCCTRL1,
|
||||
0x00}, // 0; 0 - LNA 2 gain is decreased to minimum before decreasing LNA gain; 00 - Relative carrier sense threshold disabled; 0000 - RSSI to MAIN_TARGET
|
||||
{CC1101_AGCCTRL2, 0x07}, // 00 - DVGA all; 000 - MAX LNA+LNA2; 111 - MAIN_TARGET 42 dB
|
||||
|
||||
/* Wake on radio and timeouts control */
|
||||
{CC1101_WORCTRL, 0xFB}, // WOR_RES is 2^15 periods (0.91 - 0.94 s) 16.5 - 17.2 hours
|
||||
|
||||
/* Frontend configuration */
|
||||
{CC1101_FREND0, 0x10}, // Adjusts current TX LO buffer
|
||||
{CC1101_FREND1, 0x56},
|
||||
|
||||
/* End */
|
||||
{0, 0},
|
||||
};
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 84 KiB |
@@ -0,0 +1,120 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <lib/flipper_format/flipper_format_i.h>
|
||||
#include <furi/core/string.h>
|
||||
#include <lib/subghz/registry.h>
|
||||
#include <lib/subghz/protocols/base.h>
|
||||
#include "app_buffer.h"
|
||||
|
||||
#define TAG "PROTOVIEW-protocol"
|
||||
|
||||
const SubGhzProtocol subghz_protocol_protoview;
|
||||
|
||||
/* The feed() method puts data in the RawSamples global (protected by
|
||||
* a mutex). */
|
||||
extern RawSamplesBuffer *RawSamples;
|
||||
|
||||
/* This is totally dummy: we just define the decoder base for the async
|
||||
* system to work but we don't really use it if not to collect raw
|
||||
* data via the feed() method. */
|
||||
typedef struct SubGhzProtocolDecoderprotoview {
|
||||
SubGhzProtocolDecoderBase base;
|
||||
} SubGhzProtocolDecoderprotoview;
|
||||
|
||||
void* subghz_protocol_decoder_protoview_alloc(SubGhzEnvironment* environment) {
|
||||
UNUSED(environment);
|
||||
|
||||
SubGhzProtocolDecoderprotoview* instance =
|
||||
malloc(sizeof(SubGhzProtocolDecoderprotoview));
|
||||
instance->base.protocol = &subghz_protocol_protoview;
|
||||
return instance;
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_protoview_free(void* context) {
|
||||
furi_assert(context);
|
||||
SubGhzProtocolDecoderprotoview* instance = context;
|
||||
free(instance);
|
||||
}
|
||||
|
||||
void subghz_protocol_decoder_protoview_reset(void* context) {
|
||||
furi_assert(context);
|
||||
}
|
||||
|
||||
/* That's the only thig we really use of the protocol decoder
|
||||
* implementation. We avoid the subghz provided abstractions and put
|
||||
* the data in our simple abstraction: the RawSamples circular buffer. */
|
||||
void subghz_protocol_decoder_protoview_feed(void* context, bool level, uint32_t duration) {
|
||||
furi_assert(context);
|
||||
UNUSED(context);
|
||||
|
||||
/* Add data to the circular buffer. */
|
||||
raw_samples_add(RawSamples, level, duration);
|
||||
// FURI_LOG_E(TAG, "FEED: %d %d", (int)level, (int)duration);
|
||||
return;
|
||||
}
|
||||
|
||||
/* The only scope of this method is to avoid duplicated messages in the
|
||||
* Subghz history, which we don't use. */
|
||||
uint8_t subghz_protocol_decoder_protoview_get_hash_data(void* context) {
|
||||
furi_assert(context);
|
||||
return 123;
|
||||
}
|
||||
|
||||
/* Not used. */
|
||||
bool subghz_protocol_decoder_protoview_serialize(
|
||||
void* context,
|
||||
FlipperFormat* flipper_format,
|
||||
SubGhzRadioPreset* preset)
|
||||
{
|
||||
UNUSED(context);
|
||||
UNUSED(flipper_format);
|
||||
UNUSED(preset);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* Not used. */
|
||||
bool subghz_protocol_decoder_protoview_deserialize(void* context, FlipperFormat* flipper_format)
|
||||
{
|
||||
UNUSED(context);
|
||||
UNUSED(flipper_format);
|
||||
return false;
|
||||
}
|
||||
|
||||
void subhz_protocol_decoder_protoview_get_string(void* context, FuriString* output)
|
||||
{
|
||||
furi_assert(context);
|
||||
furi_string_cat_printf(output, "Protoview");
|
||||
}
|
||||
|
||||
const SubGhzProtocolDecoder subghz_protocol_protoview_decoder = {
|
||||
.alloc = subghz_protocol_decoder_protoview_alloc,
|
||||
.free = subghz_protocol_decoder_protoview_free,
|
||||
.reset = subghz_protocol_decoder_protoview_reset,
|
||||
.feed = subghz_protocol_decoder_protoview_feed,
|
||||
.get_hash_data = subghz_protocol_decoder_protoview_get_hash_data,
|
||||
.serialize = subghz_protocol_decoder_protoview_serialize,
|
||||
.deserialize = subghz_protocol_decoder_protoview_deserialize,
|
||||
.get_string = subhz_protocol_decoder_protoview_get_string,
|
||||
};
|
||||
|
||||
/* Well, we don't really target a specific protocol. So let's put flags
|
||||
* that make sense. */
|
||||
const SubGhzProtocol subghz_protocol_protoview = {
|
||||
.name = "Protoview",
|
||||
.type = SubGhzProtocolTypeStatic,
|
||||
.flag = SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_Decodable,
|
||||
.decoder = &subghz_protocol_protoview_decoder,
|
||||
};
|
||||
|
||||
/* Our table has just the single dummy protocol we defined for the
|
||||
* sake of data collection. */
|
||||
const SubGhzProtocol* protoview_protocol_registry_items[] = {
|
||||
&subghz_protocol_protoview,
|
||||
};
|
||||
|
||||
const SubGhzProtocolRegistry protoview_protocol_registry = {
|
||||
.items = protoview_protocol_registry_items,
|
||||
.size = COUNT_OF(protoview_protocol_registry_items)
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
/* PT/SC remotes. Usually 443.92 Mhz OOK.
|
||||
*
|
||||
* This line code is used in many remotes such as Princeton chips
|
||||
* named PT<number>, Silian Microelectronics SC5262 and others.
|
||||
* Basically every 4 pulsee represent a bit, where 1000 means 0, and
|
||||
* 1110 means 1. Usually we can read 24 bits of data.
|
||||
* In this specific implementation we check for a prelude that is
|
||||
* 1 bit high, 31 bits low, but the check is relaxed. */
|
||||
|
||||
#include "../app.h"
|
||||
|
||||
static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
|
||||
if (numbits < 30) return false;
|
||||
const char *sync_patterns[3] = {
|
||||
"10000000000000000000000000000001", /* 30 zero bits. */
|
||||
"100000000000000000000000000000001", /* 31 zero bits. */
|
||||
"1000000000000000000000000000000001", /* 32 zero bits. */
|
||||
};
|
||||
|
||||
uint32_t off;
|
||||
int j;
|
||||
for (j = 0; j < 3; j++) {
|
||||
off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_patterns[j]);
|
||||
if (off != BITMAP_SEEK_NOT_FOUND) break;
|
||||
}
|
||||
if (off == BITMAP_SEEK_NOT_FOUND) return false;
|
||||
if (DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 preamble at: %lu",off);
|
||||
off += strlen(sync_patterns[j])-1;
|
||||
|
||||
uint8_t d[3]; /* 24 bits of data. */
|
||||
uint32_t decoded =
|
||||
convert_from_line_code(d,sizeof(d),bits,numbytes,off,"1000","1110");
|
||||
|
||||
if (DEBUG_MSG) FURI_LOG_E(TAG, "B4B1 decoded: %lu",decoded);
|
||||
if (decoded != 24) return false;
|
||||
snprintf(info->name,PROTOVIEW_MSG_STR_LEN,"PT/SC remote");
|
||||
snprintf(info->raw,PROTOVIEW_MSG_STR_LEN,"%02X%02X%02X",d[0],d[1],d[2]);
|
||||
info->len = off+(4*24);
|
||||
return true;
|
||||
}
|
||||
|
||||
ProtoViewDecoder B4B1Decoder = {
|
||||
"B4B1", decode
|
||||
};
|
||||
@@ -0,0 +1,65 @@
|
||||
/* Oregon remote termometers. Usually 443.92 Mhz OOK.
|
||||
*
|
||||
* The protocol is described here:
|
||||
* https://wmrx00.sourceforge.net/Arduino/OregonScientific-RF-Protocols.pdf
|
||||
* This implementation is not very complete. */
|
||||
|
||||
#include "../app.h"
|
||||
|
||||
static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
|
||||
if (numbits < 32) return false;
|
||||
const char *sync_pattern = "01100110" "01100110" "10010110" "10010110";
|
||||
uint64_t off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_pattern);
|
||||
if (off == BITMAP_SEEK_NOT_FOUND) return false;
|
||||
FURI_LOG_E(TAG, "Oregon2 preamble+sync found");
|
||||
|
||||
off += 32; /* Skip preamble. */
|
||||
|
||||
uint8_t buffer[8], raw[8] = {0};
|
||||
uint32_t decoded =
|
||||
convert_from_line_code(buffer,sizeof(buffer),bits,numbytes,off,"1001","0110");
|
||||
FURI_LOG_E(TAG, "Oregon2 decoded bits: %lu", decoded);
|
||||
|
||||
if (decoded < 11*4) return false; /* Minimum len to extract some data. */
|
||||
|
||||
char temp[3] = {0}, deviceid[2] = {0}, hum[2] = {0};
|
||||
for (int j = 0; j < 64; j += 4) {
|
||||
uint8_t nib[1];
|
||||
nib[0] = (bitmap_get(buffer,8,j+0) |
|
||||
bitmap_get(buffer,8,j+1) << 1 |
|
||||
bitmap_get(buffer,8,j+2) << 2 |
|
||||
bitmap_get(buffer,8,j+3) << 3);
|
||||
if (DEBUG_MSG) FURI_LOG_E(TAG, "Not inverted nibble[%d]: %x", j/4, (unsigned int)nib[0]);
|
||||
raw[j/8] |= nib[0] << (4-(j%4));
|
||||
switch(j/4) {
|
||||
case 1: deviceid[0] |= nib[0]; break;
|
||||
case 0: deviceid[0] |= nib[0] << 4; break;
|
||||
case 3: deviceid[1] |= nib[0]; break;
|
||||
case 2: deviceid[1] |= nib[0] << 4; break;
|
||||
case 10: temp[0] = nib[0]; break;
|
||||
/* Fixme: take the temperature sign from nibble 11. */
|
||||
case 9: temp[1] = nib[0]; break;
|
||||
case 8: temp[2] = nib[0]; break;
|
||||
case 13: hum[0] = nib[0]; break;
|
||||
case 12: hum[1] = nib[0]; break;
|
||||
}
|
||||
}
|
||||
|
||||
snprintf(info->name,sizeof(info->name),"%s","Oregon v2.1");
|
||||
/* The following line crashes the Flipper because of broken
|
||||
* snprintf() implementation. */
|
||||
snprintf(info->raw,sizeof(info->raw),"%02X%02X%02X%02X%02X%02X%02X%02X",
|
||||
raw[0],raw[1],raw[2],raw[3],raw[4],raw[5],
|
||||
raw[6],raw[7]);
|
||||
snprintf(info->info1,sizeof(info->info1),"Sensor ID %02X%02X",
|
||||
deviceid[0], deviceid[1]);
|
||||
snprintf(info->info2,sizeof(info->info2),"Temperature %d%d.%d",
|
||||
temp[0],temp[1],temp[2]);
|
||||
snprintf(info->info3,sizeof(info->info3),"Humidity %d%d",
|
||||
hum[0],hum[1]);
|
||||
return true;
|
||||
}
|
||||
|
||||
ProtoViewDecoder Oregon2Decoder = {
|
||||
"Oregon2", decode
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
11001100110011001100110011001100110011001100110011001100110 (Preamble)
|
||||
10 01 01 10 10 01 01 10 (Sync)
|
||||
01 10 10 01 10 01 10 01 01 10 10 01 01 10 01 10 10 01 01 10 10 01 10 01 10 01 10 01 10 01 10 01 01 10 10 01 10 01 10 01 01 10 01 10 01 10 01 10 01 10 01 10 10 01 01 10 01 10 10 01 10 01 10 01 10 01 10 01 01 10 10 01 10 01 01 10 01 10 10 01 01 10 10 01 10 01 10 01 10 01 10 01 10 01 11 0
|
||||
|
||||
We need to seek the following bytes: 01100110 01100110 10010110 10010110
|
||||
0x66 0x66 96 96
|
||||
@@ -0,0 +1,63 @@
|
||||
/* Renault tires TPMS. Usually 443.92 Mhz FSK.
|
||||
*
|
||||
* Preamble + marshal-encoded bits. 9 Bytes in total if we don't
|
||||
* count the preamble. */
|
||||
|
||||
#include "../app.h"
|
||||
|
||||
#define USE_TEST_VECTOR 0
|
||||
static const char *test_vector =
|
||||
"10101010" "10101010" "10101010" "10101001" // Preamble + sync.
|
||||
|
||||
/* The following is marshal encoded, so each two characters are
|
||||
* actaully one bit. 01 = 1, 10 = 0. */
|
||||
"010110010110" // Flags.
|
||||
"10011001101010011001" // Pressure, multiply by 0.75 to obtain kpa.
|
||||
// 244 kpa here.
|
||||
"1010010110011010" // Temperature, subtract 30 to obtain celsius. 22C here.
|
||||
"1001010101101001"
|
||||
"0101100110010101"
|
||||
"1001010101100110" // Tire ID. 0x7AD779 here.
|
||||
"0101010101010101"
|
||||
"0101010101010101" // Two FF bytes (usually). Unknown.
|
||||
"0110010101010101"; // CRC8 with (poly 7, initialization 0).
|
||||
|
||||
static bool decode(uint8_t *bits, uint32_t numbytes, uint32_t numbits, ProtoViewMsgInfo *info) {
|
||||
|
||||
if (USE_TEST_VECTOR) { /* Test vector to check that decoding works. */
|
||||
bitmap_set_pattern(bits,numbytes,test_vector);
|
||||
numbits = strlen(test_vector);
|
||||
}
|
||||
|
||||
if (numbits < 13*8) return false;
|
||||
|
||||
const char *sync_pattern = "10101010" "10101010" "10101010" "10101001";
|
||||
uint64_t off = bitmap_seek_bits(bits,numbytes,0,numbits,sync_pattern);
|
||||
if (off == BITMAP_SEEK_NOT_FOUND) return false;
|
||||
FURI_LOG_E(TAG, "Renault TPMS preamble+sync found");
|
||||
|
||||
off += 32; /* Skip preamble. */
|
||||
|
||||
uint8_t raw[9];
|
||||
uint32_t decoded =
|
||||
convert_from_line_code(raw,sizeof(raw),bits,numbytes,off,
|
||||
"10","01"); /* Manchester. */
|
||||
FURI_LOG_E(TAG, "Renault TPMS decoded bits: %lu", decoded);
|
||||
|
||||
if (decoded < 8*9) return false; /* Require the full 9 bytes. */
|
||||
|
||||
float kpa = 0.75 *((uint32_t)((raw[0]&3)<<8) | raw[1]);
|
||||
int temp = raw[2]-30;
|
||||
|
||||
snprintf(info->name,sizeof(info->name),"%s","Renault TPMS");
|
||||
snprintf(info->raw,sizeof(info->raw),"%02X%02X%02X%02X%02X%02X%02X%02X%02X",
|
||||
raw[0],raw[1],raw[2],raw[3],raw[4],raw[5],
|
||||
raw[6],raw[7],raw[8]);
|
||||
snprintf(info->info1,sizeof(info->info1),"Pressure %.2f kpa", (double)kpa);
|
||||
snprintf(info->info2,sizeof(info->info2),"Temperature %d C", temp);
|
||||
return true;
|
||||
}
|
||||
|
||||
ProtoViewDecoder RenaultTPMSDecoder = {
|
||||
"Renault TPMS", decode
|
||||
};
|
||||
@@ -0,0 +1,420 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
|
||||
bool decode_signal(RawSamplesBuffer *s, uint64_t len, ProtoViewMsgInfo *info);
|
||||
void initialize_msg_info(ProtoViewMsgInfo *i);
|
||||
|
||||
/* =============================================================================
|
||||
* Raw signal detection
|
||||
* ===========================================================================*/
|
||||
|
||||
/* Return the time difference between a and b, always >= 0 since
|
||||
* the absolute value is returned. */
|
||||
uint32_t duration_delta(uint32_t a, uint32_t b) {
|
||||
return a > b ? a - b : b - a;
|
||||
}
|
||||
|
||||
/* Reset the current signal, so that a new one can be detected. */
|
||||
void reset_current_signal(ProtoViewApp *app) {
|
||||
app->signal_bestlen = 0;
|
||||
app->signal_offset = 0;
|
||||
app->signal_decoded = false;
|
||||
raw_samples_reset(DetectedSamples);
|
||||
raw_samples_reset(RawSamples);
|
||||
}
|
||||
|
||||
/* This function starts scanning samples at offset idx looking for the
|
||||
* longest run of pulses, either high or low, that are not much different
|
||||
* from each other, for a maximum of three duration classes.
|
||||
* So for instance 50 successive pulses that are roughly long 340us or 670us
|
||||
* will be sensed as a coherent signal (example: 312, 361, 700, 334, 667, ...)
|
||||
*
|
||||
* The classes are counted separtely for high and low signals (RF on / off)
|
||||
* because many devices tend to have different pulse lenghts depending on
|
||||
* the level of the pulse.
|
||||
*
|
||||
* For instance Oregon2 sensors, in the case of protocol 2.1 will send
|
||||
* pulses of ~400us (RF on) VS ~580us (RF off). */
|
||||
#define SEARCH_CLASSES 3
|
||||
uint32_t search_coherent_signal(RawSamplesBuffer *s, uint32_t idx) {
|
||||
struct {
|
||||
uint32_t dur[2]; /* dur[0] = low, dur[1] = high */
|
||||
uint32_t count[2]; /* Associated observed frequency. */
|
||||
} classes[SEARCH_CLASSES];
|
||||
|
||||
memset(classes,0,sizeof(classes));
|
||||
uint32_t minlen = 30, maxlen = 4000; /* Depends on data rate, here we
|
||||
allow for high and low. */
|
||||
uint32_t len = 0; /* Observed len of coherent samples. */
|
||||
s->short_pulse_dur = 0;
|
||||
for (uint32_t j = idx; j < idx+500; j++) {
|
||||
bool level;
|
||||
uint32_t dur;
|
||||
raw_samples_get(s, j, &level, &dur);
|
||||
if (dur < minlen || dur > maxlen) break; /* return. */
|
||||
|
||||
/* Let's see if it matches a class we already have or if we
|
||||
* can populate a new (yet empty) class. */
|
||||
uint32_t k;
|
||||
for (k = 0; k < SEARCH_CLASSES; k++) {
|
||||
if (classes[k].count[level] == 0) {
|
||||
classes[k].dur[level] = dur;
|
||||
classes[k].count[level] = 1;
|
||||
break; /* Sample accepted. */
|
||||
} else {
|
||||
uint32_t classavg = classes[k].dur[level];
|
||||
uint32_t count = classes[k].count[level];
|
||||
uint32_t delta = duration_delta(dur,classavg);
|
||||
/* Is the difference in duration between this signal and
|
||||
* the class we are inspecting less than a given percentage?
|
||||
* If so, accept this signal. */
|
||||
if (delta < classavg/8) { /* 100%/8 = 12%. */
|
||||
/* It is useful to compute the average of the class
|
||||
* we are observing. We know how many samples we got so
|
||||
* far, so we can recompute the average easily.
|
||||
* By always having a better estimate of the pulse len
|
||||
* we can avoid missing next samples in case the first
|
||||
* observed samples are too off. */
|
||||
classavg = ((classavg * count) + dur) / (count+1);
|
||||
classes[k].dur[level] = classavg;
|
||||
classes[k].count[level]++;
|
||||
break; /* Sample accepted. */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (k == SEARCH_CLASSES) break; /* No match, return. */
|
||||
|
||||
/* If we are here, we accepted this sample. Try with the next
|
||||
* one. */
|
||||
len++;
|
||||
}
|
||||
|
||||
/* Update the buffer setting the shortest pulse we found
|
||||
* among the three classes. This will be used when scaling
|
||||
* for visualization. */
|
||||
uint32_t short_dur[2] = {0,0};
|
||||
for (int j = 0; j < SEARCH_CLASSES; j++) {
|
||||
for (int level = 0; level < 2; level++) {
|
||||
if (classes[j].dur[level] == 0) continue;
|
||||
if (classes[j].count[level] < 3) continue;
|
||||
if (short_dur[level] == 0 ||
|
||||
short_dur[level] > classes[j].dur[level])
|
||||
{
|
||||
short_dur[level] = classes[j].dur[level];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Use the average between high and low short pulses duration.
|
||||
* Often they are a bit different, and using the average is more robust
|
||||
* when we do decoding sampling at short_pulse_dur intervals. */
|
||||
if (short_dur[0] == 0) short_dur[0] = short_dur[1];
|
||||
if (short_dur[1] == 0) short_dur[1] = short_dur[0];
|
||||
s->short_pulse_dur = (short_dur[0]+short_dur[1])/2;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
/* Search the buffer with the stored signal (last N samples received)
|
||||
* in order to find a coherent signal. If a signal that does not appear to
|
||||
* be just noise is found, it is set in DetectedSamples global signal
|
||||
* buffer, that is what is rendered on the screen. */
|
||||
void scan_for_signal(ProtoViewApp *app) {
|
||||
/* We need to work on a copy: the RawSamples buffer is populated
|
||||
* by the background thread receiving data. */
|
||||
RawSamplesBuffer *copy = raw_samples_alloc();
|
||||
raw_samples_copy(copy,RawSamples);
|
||||
|
||||
/* Try to seek on data that looks to have a regular high low high low
|
||||
* pattern. */
|
||||
uint32_t minlen = 13; /* Min run of coherent samples. Up to
|
||||
12 samples it's very easy to mistake
|
||||
noise for signal. */
|
||||
|
||||
ProtoViewMsgInfo *info = malloc(sizeof(ProtoViewMsgInfo));
|
||||
uint32_t i = 0;
|
||||
|
||||
while (i < copy->total-1) {
|
||||
uint32_t thislen = search_coherent_signal(copy,i);
|
||||
|
||||
/* For messages that are long enough, attempt decoding. */
|
||||
if (thislen > minlen) {
|
||||
initialize_msg_info(info);
|
||||
uint32_t saved_idx = copy->idx; /* Save index, see later. */
|
||||
/* decode_signal() expects the detected signal to start
|
||||
* from index .*/
|
||||
raw_samples_center(copy,i);
|
||||
bool decoded = decode_signal(copy,thislen,info);
|
||||
copy->idx = saved_idx; /* Restore the index as we are scanning
|
||||
the signal in the loop. */
|
||||
|
||||
/* Accept this signal as the new signal if either it's longer
|
||||
* than the previous one, or the previous one was unknown and
|
||||
* this is decoded. */
|
||||
if (thislen > app->signal_bestlen ||
|
||||
(app->signal_decoded == false && decoded))
|
||||
{
|
||||
app->signal_info = *info;
|
||||
app->signal_bestlen = thislen;
|
||||
app->signal_decoded = decoded;
|
||||
raw_samples_copy(DetectedSamples,copy);
|
||||
raw_samples_center(DetectedSamples,i);
|
||||
FURI_LOG_E(TAG, "Displayed sample updated (%d samples %lu us)",
|
||||
(int)thislen, DetectedSamples->short_pulse_dur);
|
||||
}
|
||||
}
|
||||
i += thislen ? thislen : 1;
|
||||
}
|
||||
raw_samples_free(copy);
|
||||
free(info);
|
||||
}
|
||||
|
||||
/* =============================================================================
|
||||
* Decoding
|
||||
*
|
||||
* The following code will translates the raw singals as received by
|
||||
* the CC1101 into logical signals: a bitmap of 0s and 1s sampled at
|
||||
* the detected data clock interval.
|
||||
*
|
||||
* Then the converted signal is passed to the protocols decoders, that look
|
||||
* for protocol-specific information. We stop at the first decoder that is
|
||||
* able to decode the data, so protocols here should be registered in
|
||||
* order of complexity and specificity, with the generic ones at the end.
|
||||
* ===========================================================================*/
|
||||
|
||||
/* Set the 'bitpos' bit to value 'val', in the specified bitmap
|
||||
* 'b' of len 'blen'.
|
||||
* Out of range bits will silently be discarded. */
|
||||
void bitmap_set(uint8_t *b, uint32_t blen, uint32_t bitpos, bool val) {
|
||||
uint32_t byte = bitpos/8;
|
||||
uint32_t bit = 7-(bitpos&7);
|
||||
if (byte >= blen) return;
|
||||
if (val)
|
||||
b[byte] |= 1<<bit;
|
||||
else
|
||||
b[byte] &= ~(1<<bit);
|
||||
}
|
||||
|
||||
/* Get the bit 'bitpos' of the bitmap 'b' of 'blen' bytes.
|
||||
* Out of range bits return false (not bit set). */
|
||||
bool bitmap_get(uint8_t *b, uint32_t blen, uint32_t bitpos) {
|
||||
uint32_t byte = bitpos/8;
|
||||
uint32_t bit = 7-(bitpos&7);
|
||||
if (byte >= blen) return 0;
|
||||
return (b[byte] & (1<<bit)) != 0;
|
||||
}
|
||||
|
||||
/* We decode bits assuming the first bit we receive is the LSB
|
||||
* (see bitmap_set/get functions). Many devices send data
|
||||
* encoded in the reverse way. */
|
||||
void bitmap_invert_bytes_bits(uint8_t *p, uint32_t len) {
|
||||
for (uint32_t j = 0; j < len*8; j += 8) {
|
||||
bool bits[8];
|
||||
for (int i = 0; i < 8; i++) bits[i] = bitmap_get(p,len,j+i);
|
||||
for (int i = 0; i < 8; i++) bitmap_set(p,len,j+i,bits[7-i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Return true if the specified sequence of bits, provided as a string in the
|
||||
* form "11010110..." is found in the 'b' bitmap of 'blen' bits at 'bitpos'
|
||||
* position. */
|
||||
bool bitmap_match_bits(uint8_t *b, uint32_t blen, uint32_t bitpos, const char *bits) {
|
||||
for (size_t j = 0; bits[j]; j++) {
|
||||
bool expected = (bits[j] == '1') ? true : false;
|
||||
if (bitmap_get(b,blen,bitpos+j) != expected) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Search for the specified bit sequence (see bitmap_match_bits() for details)
|
||||
* in the bitmap 'b' of 'blen' bytes, looking forward at most 'maxbits' ahead.
|
||||
* Returns the offset (in bits) of the match, or BITMAP_SEEK_NOT_FOUND if not
|
||||
* found.
|
||||
*
|
||||
* Note: there are better algorithms, such as Boyer-Moore. Here we hope that
|
||||
* for the kind of patterns we search we'll have a lot of early stops so
|
||||
* we use a vanilla approach. */
|
||||
uint32_t bitmap_seek_bits(uint8_t *b, uint32_t blen, uint32_t startpos, uint32_t maxbits, const char *bits) {
|
||||
uint32_t endpos = startpos+blen*8;
|
||||
uint32_t end2 = startpos+maxbits;
|
||||
if (end2 < endpos) endpos = end2;
|
||||
for (uint32_t j = startpos; j < endpos; j++)
|
||||
if (bitmap_match_bits(b,blen,j,bits)) return j;
|
||||
return BITMAP_SEEK_NOT_FOUND;
|
||||
}
|
||||
|
||||
/* Set the pattern 'pat' into the bitmap 'b' of max length 'blen' bytes.
|
||||
* The pattern is given as a string of 0s and 1s characters, like "01101001".
|
||||
* This function is useful in order to set the test vectors in the protocol
|
||||
* decoders, to see if the decoding works regardless of the fact we are able
|
||||
* to actually receive a given signal. */
|
||||
void bitmap_set_pattern(uint8_t *b, uint32_t blen, const char *pat) {
|
||||
uint32_t i = 0;
|
||||
while(pat[i]) {
|
||||
bitmap_set(b,blen,i,pat[i] == '1');
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
/* Take the raw signal and turn it into a sequence of bits inside the
|
||||
* buffer 'b'. Note that such 0s and 1s are NOT the actual data in the
|
||||
* signal, but is just a low level representation of the line code. Basically
|
||||
* if the short pulse we find in the signal is 320us, we convert high and
|
||||
* low levels in the raw sample in this way:
|
||||
*
|
||||
* If for instance we see a high level lasting ~600 us, we will add
|
||||
* two 1s bit. If then the signal goes down for 330us, we will add one zero,
|
||||
* and so forth. So for each period of high and low we find the closest
|
||||
* multiple and set the relevant number of bits.
|
||||
*
|
||||
* In case of a short pulse of 320us detected, 320*2 is the closest to a
|
||||
* high pulse of 600us, so 2 bits will be set.
|
||||
*
|
||||
* In other terms what this function does is sampling the signal at
|
||||
* fixed 'rate' intervals.
|
||||
*
|
||||
* This representation makes it simple to decode the signal at a higher
|
||||
* level later, translating it from Marshal coding or other line codes
|
||||
* to the actual bits/bytes.
|
||||
*
|
||||
* The 'idx' argument marks the detected signal start index into the
|
||||
* raw samples buffer. The 'count' tells the function how many raw
|
||||
* samples to convert into bits. The function returns the number of
|
||||
* bits set into the buffer 'b'. The 'rate' argument, in microseconds, is
|
||||
* the detected short-pulse duration. We expect the line code to be
|
||||
* meaningful when interpreted at multiples of 'rate'. */
|
||||
uint32_t convert_signal_to_bits(uint8_t *b, uint32_t blen, RawSamplesBuffer *s, uint32_t idx, uint32_t count, uint32_t rate) {
|
||||
if (rate == 0) return 0; /* We can't perform the conversion. */
|
||||
uint32_t bitpos = 0;
|
||||
for (uint32_t j = 0; j < count; j++) {
|
||||
uint32_t dur;
|
||||
bool level;
|
||||
raw_samples_get(s, j+idx, &level, &dur);
|
||||
|
||||
uint32_t numbits = dur / rate; /* full bits that surely fit. */
|
||||
uint32_t rest = dur % rate; /* How much we are left with. */
|
||||
if (rest > rate/2) numbits++; /* There is another one. */
|
||||
|
||||
/* Limit how much a single sample can spawn. There are likely no
|
||||
* protocols doing such long pulses when the rate is low. */
|
||||
if (numbits > 1024) numbits = 1024;
|
||||
|
||||
if (0) /* Super verbose, so not under the DEBUG_MSG define. */
|
||||
FURI_LOG_E(TAG, "%lu converted into %lu (%d) bits",
|
||||
dur,numbits,(int)level);
|
||||
|
||||
/* If the signal is too short, let's claim it an interference
|
||||
* and ignore it completely. */
|
||||
if (numbits == 0) continue;
|
||||
|
||||
while(numbits--) bitmap_set(b,blen,bitpos++,level);
|
||||
}
|
||||
return bitpos;
|
||||
}
|
||||
|
||||
/* This function converts the line code used to the final data representation.
|
||||
* The representation is put inside 'buf', for up to 'buflen' bytes of total
|
||||
* data. For instance in order to convert manchester I can use "10" and "01"
|
||||
* as zero and one patterns. It is possible to use "?" inside patterns in
|
||||
* order to skip certain bits. For instance certain devices encode data twice,
|
||||
* with each bit encoded in manchester encoding and then in its reversed
|
||||
* representation. In such a case I could use "10??" and "01??".
|
||||
*
|
||||
* The function returns the number of bits converted. It will stop as soon
|
||||
* as it finds a pattern that does not match zero or one patterns, or when
|
||||
* the end of the bitmap pointed by 'bits' is reached (the length is
|
||||
* specified in bytes by the caller, via the 'len' parameters).
|
||||
*
|
||||
* The decoding starts at the specified offset (in bits) 'off'. */
|
||||
uint32_t convert_from_line_code(uint8_t *buf, uint64_t buflen, uint8_t *bits, uint32_t len, uint32_t off, const char *zero_pattern, const char *one_pattern)
|
||||
{
|
||||
uint32_t decoded = 0; /* Number of bits extracted. */
|
||||
len *= 8; /* Convert bytes to bits. */
|
||||
while(off < len) {
|
||||
bool bitval;
|
||||
if (bitmap_match_bits(bits,len,off,zero_pattern)) {
|
||||
bitval = false;
|
||||
off += strlen(zero_pattern);
|
||||
} else if (bitmap_match_bits(bits,len,off,one_pattern)) {
|
||||
bitval = true;
|
||||
off += strlen(one_pattern);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
bitmap_set(buf,buflen,decoded++,bitval);
|
||||
if (decoded/8 == buflen) break; /* No space left on target buffer. */
|
||||
}
|
||||
return decoded;
|
||||
}
|
||||
|
||||
/* Supported protocols go here, with the relevant implementation inside
|
||||
* protocols/<name>.c */
|
||||
|
||||
extern ProtoViewDecoder Oregon2Decoder;
|
||||
extern ProtoViewDecoder B4B1Decoder;
|
||||
extern ProtoViewDecoder RenaultTPMSDecoder;
|
||||
|
||||
ProtoViewDecoder *Decoders[] = {
|
||||
&Oregon2Decoder, /* Oregon sensors v2.1 protocol. */
|
||||
&B4B1Decoder, /* PT, SC, ... 24 bits remotes. */
|
||||
&RenaultTPMSDecoder, /* Renault TPMS. */
|
||||
NULL
|
||||
};
|
||||
|
||||
/* Reset the message info structure before passing it to the decoding
|
||||
* functions. */
|
||||
void initialize_msg_info(ProtoViewMsgInfo *i) {
|
||||
memset(i,0,sizeof(ProtoViewMsgInfo));
|
||||
}
|
||||
|
||||
/* This function is called when a new signal is detected. It converts it
|
||||
* to a bitstream, and the calls the protocol specific functions for
|
||||
* decoding. If the signal was decoded correctly by some protocol, true
|
||||
* is returned. Otherwise false is returned. */
|
||||
bool decode_signal(RawSamplesBuffer *s, uint64_t len, ProtoViewMsgInfo *info) {
|
||||
uint32_t bitmap_bits_size = 4096*8;
|
||||
uint32_t bitmap_size = bitmap_bits_size/8;
|
||||
|
||||
/* We call the decoders with an offset a few bits before the actual
|
||||
* signal detected and for a len of a few bits after its end. */
|
||||
uint32_t before_after_bits = 2;
|
||||
|
||||
uint8_t *bitmap = malloc(bitmap_size);
|
||||
uint32_t bits = convert_signal_to_bits(bitmap,bitmap_size,s,-before_after_bits,len+before_after_bits*2,s->short_pulse_dur);
|
||||
|
||||
if (DEBUG_MSG) { /* Useful for debugging purposes. Don't remove. */
|
||||
char *str = malloc(1024);
|
||||
uint32_t j;
|
||||
for (j = 0; j < bits && j < 1023; j++) {
|
||||
str[j] = bitmap_get(bitmap,bitmap_size,j) ? '1' : '0';
|
||||
}
|
||||
str[j] = 0;
|
||||
FURI_LOG_E(TAG, "%lu bits sampled: %s", bits, str);
|
||||
free(str);
|
||||
}
|
||||
|
||||
/* Try all the decoders available. */
|
||||
int j = 0;
|
||||
|
||||
bool decoded = false;
|
||||
while(Decoders[j]) {
|
||||
uint32_t start_time = furi_get_tick();
|
||||
decoded = Decoders[j]->decode(bitmap,bitmap_size,bits,info);
|
||||
uint32_t delta = furi_get_tick() - start_time;
|
||||
FURI_LOG_E(TAG, "Decoder %s took %lu ms",
|
||||
Decoders[j]->name, (unsigned long)delta);
|
||||
if (decoded) break;
|
||||
j++;
|
||||
}
|
||||
|
||||
if (!decoded) {
|
||||
FURI_LOG_E(TAG, "No decoding possible");
|
||||
} else {
|
||||
FURI_LOG_E(TAG, "Decoded %s, raw=%s info=[%s,%s,%s]", info->name, info->raw, info->info1, info->info2, info->info3);
|
||||
}
|
||||
free(bitmap);
|
||||
return decoded;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
|
||||
void canvas_draw_str_with_border(Canvas* canvas, uint8_t x, uint8_t y, const char* str, Color text_color, Color border_color)
|
||||
{
|
||||
struct {
|
||||
uint8_t x; uint8_t y;
|
||||
} dir[8] = {
|
||||
{-1,-1},
|
||||
{0,-1},
|
||||
{1,-1},
|
||||
{1,0},
|
||||
{1,1},
|
||||
{0,1},
|
||||
{-1,1},
|
||||
{-1,0}
|
||||
};
|
||||
|
||||
/* Rotate in all the directions writing the same string to create a
|
||||
* border, then write the actual string in the other color in the
|
||||
* middle. */
|
||||
canvas_set_color(canvas, border_color);
|
||||
for (int j = 0; j < 8; j++)
|
||||
canvas_draw_str(canvas,x+dir[j].x,y+dir[j].y,str);
|
||||
canvas_set_color(canvas, text_color);
|
||||
canvas_draw_str(canvas,x,y,str);
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
|
||||
/* Renders the view with the detected message information. */
|
||||
void render_view_info(Canvas *const canvas, ProtoViewApp *app) {
|
||||
if (app->signal_decoded == false) {
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas, 30,36,"No signal decoded");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Protocol name as title. */
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
uint8_t y = 8, lineheight = 10;
|
||||
canvas_draw_str(canvas, 0, y, app->signal_info.name);
|
||||
y += lineheight;
|
||||
|
||||
/* Info fields. */
|
||||
char buf[128];
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
if (app->signal_info.raw[0]) {
|
||||
snprintf(buf,sizeof(buf),"Raw: %s", app->signal_info.raw);
|
||||
canvas_draw_str(canvas, 0, y, buf);
|
||||
y += lineheight;
|
||||
}
|
||||
canvas_draw_str(canvas, 0, y, app->signal_info.info1);
|
||||
y += lineheight;
|
||||
canvas_draw_str(canvas, 0, y, app->signal_info.info2);
|
||||
y += lineheight;
|
||||
canvas_draw_str(canvas, 0, y, app->signal_info.info3);
|
||||
y += lineheight;
|
||||
}
|
||||
|
||||
/* Handle input for the settings view. */
|
||||
void process_input_info(ProtoViewApp *app, InputEvent input) {
|
||||
UNUSED(app);
|
||||
UNUSED(input);
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
|
||||
/* Render the received signal.
|
||||
*
|
||||
* The screen of the flipper is 128 x 64. Even using 4 pixels per line
|
||||
* (where low level signal is one pixel high, high level is 4 pixels
|
||||
* high) and 4 pixels of spacing between the different lines, we can
|
||||
* plot comfortably 8 lines.
|
||||
*
|
||||
* The 'idx' argument is the first sample to render in the circular
|
||||
* buffer. */
|
||||
void render_signal(ProtoViewApp *app, Canvas *const canvas, RawSamplesBuffer *buf, uint32_t idx) {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
|
||||
int rows = 8;
|
||||
uint32_t time_per_pixel = app->us_scale;
|
||||
uint32_t start_idx = idx;
|
||||
bool level = 0;
|
||||
uint32_t dur = 0, sample_num = 0;
|
||||
for (int row = 0; row < rows ; row++) {
|
||||
for (int x = 0; x < 128; x++) {
|
||||
int y = 3 + row*8;
|
||||
if (dur < time_per_pixel/2) {
|
||||
/* Get more data. */
|
||||
raw_samples_get(buf, idx++, &level, &dur);
|
||||
sample_num++;
|
||||
}
|
||||
|
||||
canvas_draw_line(canvas, x,y,x,y-(level*3));
|
||||
|
||||
/* Write a small triangle under the last sample detected. */
|
||||
if (app->signal_bestlen != 0 &&
|
||||
sample_num+start_idx == app->signal_bestlen+1)
|
||||
{
|
||||
canvas_draw_dot(canvas,x,y+2);
|
||||
canvas_draw_dot(canvas,x-1,y+3);
|
||||
canvas_draw_dot(canvas,x,y+3);
|
||||
canvas_draw_dot(canvas,x+1,y+3);
|
||||
sample_num++; /* Make sure we don't mark the next, too. */
|
||||
}
|
||||
|
||||
/* Remove from the current level duration the time we
|
||||
* just plot. */
|
||||
if (dur > time_per_pixel)
|
||||
dur -= time_per_pixel;
|
||||
else
|
||||
dur = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Raw pulses rendering. This is our default view. */
|
||||
void render_view_raw_pulses(Canvas *const canvas, ProtoViewApp *app) {
|
||||
/* Show signal. */
|
||||
render_signal(app, canvas, DetectedSamples, app->signal_offset);
|
||||
|
||||
/* Show signal information. */
|
||||
char buf[64];
|
||||
snprintf(buf,sizeof(buf),"%luus",
|
||||
(unsigned long)DetectedSamples->short_pulse_dur);
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str_with_border(canvas, 97, 63, buf, ColorWhite, ColorBlack);
|
||||
if (app->signal_decoded) {
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str_with_border(canvas, 1, 61, app->signal_info.name, ColorWhite, ColorBlack);
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle input for the raw pulses view. */
|
||||
void process_input_raw_pulses(ProtoViewApp *app, InputEvent input) {
|
||||
if (input.type == InputTypeRepeat) {
|
||||
/* Handle panning of the signal window. Long pressing
|
||||
* right will show successive samples, long pressing left
|
||||
* previous samples. */
|
||||
if (input.key == InputKeyRight) app->signal_offset++;
|
||||
else if (input.key == InputKeyLeft) app->signal_offset--;
|
||||
else if (input.key == InputKeyOk) {
|
||||
app->signal_offset = 0;
|
||||
app->us_scale = PROTOVIEW_RAW_VIEW_DEFAULT_SCALE;
|
||||
}
|
||||
} else if (input.type == InputTypeShort) {
|
||||
if (input.key == InputKeyOk) {
|
||||
/* Reset the current sample to capture the next. */
|
||||
reset_current_signal(app);
|
||||
} else if (input.key == InputKeyDown) {
|
||||
/* Rescaling. The set becomes finer under 50us per pixel. */
|
||||
uint32_t scale_step = app->us_scale >= 50 ? 50 : 10;
|
||||
if (app->us_scale < 500) app->us_scale += scale_step;
|
||||
} else if (input.key == InputKeyUp) {
|
||||
uint32_t scale_step = app->us_scale > 50 ? 50 : 10;
|
||||
if (app->us_scale > 10) app->us_scale -= scale_step;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/* Copyright (C) 2022-2023 Salvatore Sanfilippo -- All Rights Reserved
|
||||
* See the LICENSE file for information about the license. */
|
||||
|
||||
#include "app.h"
|
||||
|
||||
/* Renders a single view with frequency and modulation setting. However
|
||||
* this are logically two different views, and only one of the settings
|
||||
* will be highlighted. */
|
||||
void render_view_settings(Canvas *const canvas, ProtoViewApp *app) {
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
if (app->current_view == ViewFrequencySettings)
|
||||
canvas_draw_str_with_border(canvas,1,10,"Frequency",ColorWhite,ColorBlack);
|
||||
else
|
||||
canvas_draw_str(canvas,1,10,"Frequency");
|
||||
|
||||
if (app->current_view == ViewModulationSettings)
|
||||
canvas_draw_str_with_border(canvas,70,10,"Modulation",ColorWhite,ColorBlack);
|
||||
else
|
||||
canvas_draw_str(canvas,70,10,"Modulation");
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
canvas_draw_str(canvas,10,61,"Use up and down to modify");
|
||||
|
||||
/* Show frequency. We can use big numbers font since it's just a number. */
|
||||
if (app->current_view == ViewFrequencySettings) {
|
||||
char buf[16];
|
||||
snprintf(buf,sizeof(buf),"%.2f",(double)app->frequency/1000000);
|
||||
canvas_set_font(canvas, FontBigNumbers);
|
||||
canvas_draw_str(canvas, 30, 40, buf);
|
||||
} else if (app->current_view == ViewModulationSettings) {
|
||||
int current = app->modulation;
|
||||
canvas_set_font(canvas, FontPrimary);
|
||||
canvas_draw_str(canvas, 33, 39, ProtoViewModulations[current].name);
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle input for the settings view. */
|
||||
void process_input_settings(ProtoViewApp *app, InputEvent input) {
|
||||
if (input.type == InputTypeLong && input.key == InputKeyOk) {
|
||||
/* Long pressing to OK sets the default frequency and
|
||||
* modulation. */
|
||||
app->frequency = subghz_setting_get_default_frequency(app->setting);
|
||||
app->modulation = 0;
|
||||
} else if (input.type == InputTypePress &&
|
||||
(input.key != InputKeyDown || input.key != InputKeyUp))
|
||||
{
|
||||
/* Handle up and down to change frequency or modulation. */
|
||||
if (app->current_view == ViewFrequencySettings) {
|
||||
size_t curidx = 0, i;
|
||||
size_t count = subghz_setting_get_frequency_count(app->setting);
|
||||
|
||||
/* Scan the list of frequencies to check for the index of the
|
||||
* currently set frequency. */
|
||||
for(i = 0; i < count; i++) {
|
||||
uint32_t freq = subghz_setting_get_frequency(app->setting,i);
|
||||
if (freq == app->frequency) {
|
||||
curidx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == count) return; /* Should never happen. */
|
||||
|
||||
if (input.key == InputKeyUp) {
|
||||
curidx = (curidx+1) % count;
|
||||
} else if (input.key == InputKeyDown) {
|
||||
curidx = curidx == 0 ? count-1 : curidx-1;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
app->frequency = subghz_setting_get_frequency(app->setting,curidx);
|
||||
} else if (app->current_view == ViewModulationSettings) {
|
||||
uint32_t count = 0;
|
||||
uint32_t modid = app->modulation;
|
||||
|
||||
while(ProtoViewModulations[count].name != NULL) count++;
|
||||
if (input.key == InputKeyUp) {
|
||||
modid = (modid+1) % count;
|
||||
} else if (input.key == InputKeyDown) {
|
||||
modid = modid == 0 ? count-1 : modid-1;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
app->modulation = modid;
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Apply changes. */
|
||||
FURI_LOG_E(TAG, "Setting view, setting frequency/modulation to %lu %s", app->frequency, ProtoViewModulations[app->modulation].name);
|
||||
radio_rx_end(app);
|
||||
radio_begin(app);
|
||||
radio_rx(app);
|
||||
}
|
||||
Reference in New Issue
Block a user