[FL-2529][FL-1628] New LF-RFID subsystem (#1601)

* Makefile: unit tests pack
* RFID: pulse joiner and its unit test
* Move pulse protocol helpers to appropriate place
* Drop pulse_joiner tests
* Generic protocol, protocols dictionary, unit test
* Protocol dict unit test
* iButton: protocols dictionary
* Lib: varint
* Lib: profiler
* Unit test: varint
* rfid: worker mockup
* LFRFID: em4100 unit test
* Storage: file_exist function
* rfid: fsk osc
* rfid: generic fsk demodulator
* rfid: protocol em4100
* rfid: protocol h10301
* rfid: protocol io prox xsf
* Unit test: rfid protocols
* rfid: new hal
* rfid: raw worker
* Unit test: fix error output
* rfid: worker
* rfid: plain c cli
* fw: migrate to scons
* lfrfid: full io prox support
* unit test: io prox protocol
* SubGHZ: move bit defines to source
* FSK oscillator: level duration compability
* libs: bit manipulation library
* lfrfid: ioprox protocol, use bit library and new level duration method of FSK ocillator
* bit lib: unit tests
* Bit lib: parity tests, remove every nth bit, copy bits
* Lfrfid: awid protocol
* bit lib: uint16 and uint32 getters, unit tests
* lfrfid: FDX-B read, draft version
* Minunit: better memeq assert
* bit lib: reverse, print, print regions
* Protocol dict: get protocol features, get protocol validate count
* lfrfid worker: improved read
* lfrfid raw worker: psk support
* Cli: rfid plain C cli
* protocol AWID: render
* protocol em4100: render
* protocol h10301: render
* protocol indala26: support every indala 26 scramble
* Protocol IO Prox: render
* Protocol FDX-B: advanced read
* lfrfid: remove unused test function
* lfrfid: fix os primitives
* bit lib: crc16 and unit tests
* FDX-B: save data
* lfrfid worker: increase stream size. Alloc raw worker only when needed.
* lfrfid: indala26 emulation
* lfrfid: prepare to write
* lfrfid: fdx-b emulation
* lfrfid: awid, ioprox write
* lfrfid: write t55xx w\o validation
* lfrfid: better t55xx block0 handling
* lfrfid: use new t5577 functions in worker
* lfrfid: improve protocol description
* lfrfid: write and verify
* lfrfid: delete cpp cli
* lfrfid: improve worker usage
* lfrfid-app: step to new worker
* lfrfid: old indala (I40134) load fallback
* lfrfid: indala26, recover wrong synced data
* lfrfid: remove old worker
* lfrfid app: dummy read screen
* lfrfid app: less dummy read screen
* lfrfid: generic 96-bit HID protocol (covers up to HID 37-bit)
* rename
* lfrfid: improve indala26 read
* lfrfid: generic 192-bit HID protocol (covers all HID extended)
* lfrfid: TODO about HID render
* lfrfid: new protocol FDX-A
* lfrfid-app: correct worker stop on exit
* misc fixes
* lfrfid: FDX-A and HID distinguishability has been fixed.
* lfrfid: decode HID size header and render it (#1612)
* lfrfid: rename HID96 and HID192 to HIDProx and HIDExt
* lfrfid: extra actions scene
* lfrfid: decode generic HID Proximity size lazily (#1618)
* lib: stream of data buffers concept
* lfrfid: raw file helper
* lfrfid: changed raw worker api
* lfrfid: packed varint pair
* lfrfid: read stream speedup
* lfrfid app: show read mode
* Documentation
* lfrfid app: raw read gui
* lfrfid app: storage check for raw read
* memleak fix
* review fixes
* lfrfid app: read blink color
* lfrfid app: reset key name after read
* review fixes
* lfrfid app: fix copypasted text
* review fixes
* lfrfid: disable debug gpio
* lfrfid: card detection events
* lfrfid: change validation color from magenta to green
* Update core_defines.
* lfrfid: prefix fdx-b id by zeroes
* lfrfid: parse up to 43-bit HID Proximity keys (#1640)
* Fbt: downgrade toolchain and fix PS1
* lfrfid: fix unit tests
* lfrfid app: remove printf
* lfrfid: indala26, use bit 55 as data
* lfrfid: indala26, better brief format
* lfrfid: indala26, loading fallback
* lfrfid: read timing tuning

Co-authored-by: James Ide <ide@users.noreply.github.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2022-08-24 01:57:39 +10:00
committed by GitHub
parent f92127c0a7
commit 9bfb641d3e
179 changed files with 10234 additions and 4804 deletions

145
lib/toolbox/buffer_stream.c Normal file
View File

@@ -0,0 +1,145 @@
#include "buffer_stream.h"
#include <stream_buffer.h>
struct Buffer {
volatile bool occupied;
volatile size_t size;
uint8_t* data;
size_t max_data_size;
};
struct BufferStream {
size_t stream_overrun_count;
StreamBufferHandle_t stream;
size_t index;
Buffer* buffers;
size_t max_buffers_count;
};
bool buffer_write(Buffer* buffer, const uint8_t* data, size_t size) {
if(buffer->occupied) {
return false;
}
if((buffer->size + size) > buffer->max_data_size) {
return false;
}
memcpy(buffer->data + buffer->size, data, size);
buffer->size += size;
return true;
}
uint8_t* buffer_get_data(Buffer* buffer) {
return buffer->data;
}
size_t buffer_get_size(Buffer* buffer) {
return buffer->size;
}
void buffer_reset(Buffer* buffer) {
buffer->occupied = false;
buffer->size = 0;
}
BufferStream* buffer_stream_alloc(size_t buffer_size, size_t buffers_count) {
furi_assert(buffer_size > 0);
furi_assert(buffers_count > 0);
BufferStream* buffer_stream = malloc(sizeof(BufferStream));
buffer_stream->max_buffers_count = buffers_count;
buffer_stream->buffers = malloc(sizeof(Buffer) * buffer_stream->max_buffers_count);
for(size_t i = 0; i < buffer_stream->max_buffers_count; i++) {
buffer_stream->buffers[i].occupied = false;
buffer_stream->buffers[i].size = 0;
buffer_stream->buffers[i].data = malloc(buffer_size);
buffer_stream->buffers[i].max_data_size = buffer_size;
}
buffer_stream->stream = xStreamBufferCreate(
sizeof(BufferStream*) * buffer_stream->max_buffers_count, sizeof(BufferStream*));
buffer_stream->stream_overrun_count = 0;
buffer_stream->index = 0;
return buffer_stream;
}
void buffer_stream_free(BufferStream* buffer_stream) {
for(size_t i = 0; i < buffer_stream->max_buffers_count; i++) {
free(buffer_stream->buffers[i].data);
}
vStreamBufferDelete(buffer_stream->stream);
free(buffer_stream->buffers);
free(buffer_stream);
}
static inline int8_t buffer_stream_get_free_buffer(BufferStream* buffer_stream) {
int8_t id = -1;
for(size_t i = 0; i < buffer_stream->max_buffers_count; i++) {
if(buffer_stream->buffers[i].occupied == false) {
id = i;
break;
}
}
return id;
}
bool buffer_stream_send_from_isr(
BufferStream* buffer_stream,
const uint8_t* data,
size_t size,
BaseType_t* const task_woken) {
Buffer* buffer = &buffer_stream->buffers[buffer_stream->index];
bool result = true;
// write to buffer
if(!buffer_write(buffer, data, size)) {
// if buffer is full - send it
buffer->occupied = true;
// we always have space for buffer in stream
xStreamBufferSendFromISR(buffer_stream->stream, &buffer, sizeof(Buffer*), task_woken);
// get new buffer from the pool
int8_t index = buffer_stream_get_free_buffer(buffer_stream);
// check that we have valid buffer
if(index == -1) {
// no free buffer
buffer_stream->stream_overrun_count++;
result = false;
} else {
// write to new buffer
buffer_stream->index = index;
buffer = &buffer_stream->buffers[buffer_stream->index];
buffer_write(buffer, data, size);
}
}
return result;
}
Buffer* buffer_stream_receive(BufferStream* buffer_stream, TickType_t timeout) {
Buffer* buffer;
size_t size = xStreamBufferReceive(buffer_stream->stream, &buffer, sizeof(Buffer*), timeout);
if(size == sizeof(Buffer*)) {
return buffer;
} else {
return NULL;
}
}
size_t buffer_stream_get_overrun_count(BufferStream* buffer_stream) {
return buffer_stream->stream_overrun_count;
}
void buffer_stream_reset(BufferStream* buffer_stream) {
FURI_CRITICAL_ENTER();
BaseType_t xReturn = xStreamBufferReset(buffer_stream->stream);
furi_assert(xReturn == pdPASS);
UNUSED(xReturn);
buffer_stream->stream_overrun_count = 0;
for(size_t i = 0; i < buffer_stream->max_buffers_count; i++) {
buffer_reset(&buffer_stream->buffers[i]);
}
FURI_CRITICAL_EXIT();
}

View File

@@ -0,0 +1,94 @@
/**
* @file buffer_stream.h
*
* This file implements the concept of a buffer stream.
* Data is written to the buffer until the buffer is full.
* Then the buffer pointer is written to the stream, and the new write buffer is taken from the buffer pool.
* After the buffer has been read by the receiving thread, it is sent to the free buffer pool.
*
* This will speed up sending large chunks of data between threads, compared to using a stream directly.
*/
#pragma once
#include <furi.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Buffer Buffer;
/**
* @brief Get buffer data pointer
* @param buffer
* @return uint8_t*
*/
uint8_t* buffer_get_data(Buffer* buffer);
/**
* @brief Get buffer size
* @param buffer
* @return size_t
*/
size_t buffer_get_size(Buffer* buffer);
/**
* @brief Reset buffer and send to free buffer pool
* @param buffer
*/
void buffer_reset(Buffer* buffer);
typedef struct BufferStream BufferStream;
/**
* @brief Allocate a new BufferStream instance
* @param buffer_size
* @param buffers_count
* @return BufferStream*
*/
BufferStream* buffer_stream_alloc(size_t buffer_size, size_t buffers_count);
/**
* @brief Free a BufferStream instance
* @param buffer_stream
*/
void buffer_stream_free(BufferStream* buffer_stream);
/**
* @brief Write data to buffer stream, from ISR context
* Data will be written to the buffer until the buffer is full, and only then will the buffer be sent.
* @param buffer_stream
* @param data
* @param size
* @param task_woken
* @return bool
*/
bool buffer_stream_send_from_isr(
BufferStream* buffer_stream,
const uint8_t* data,
size_t size,
BaseType_t* const task_woken);
/**
* @brief Receive buffer from stream
* @param buffer_stream
* @param timeout
* @return Buffer*
*/
Buffer* buffer_stream_receive(BufferStream* buffer_stream, TickType_t timeout);
/**
* @brief Get stream overrun count
* @param buffer_stream
* @return size_t
*/
size_t buffer_stream_get_overrun_count(BufferStream* buffer_stream);
/**
* @brief Reset stream and buffer pool
* @param buffer_stream
*/
void buffer_stream_reset(BufferStream* buffer_stream);
#ifdef __cplusplus
}
#endif

87
lib/toolbox/profiler.c Normal file
View File

@@ -0,0 +1,87 @@
#include "profiler.h"
#include <stdlib.h>
#include <m-dict.h>
#include <furi.h>
#include <furi_hal_gpio.h>
typedef struct {
uint32_t start;
uint32_t length;
uint32_t count;
} ProfilerRecord;
DICT_DEF2(ProfilerRecordDict, const char*, M_CSTR_OPLIST, ProfilerRecord, M_POD_OPLIST)
#define M_OPL_ProfilerRecord_t() DICT_OPLIST(ProfilerRecord, M_CSTR_OPLIST, M_POD_OPLIST)
struct Profiler {
ProfilerRecordDict_t records;
};
Profiler* profiler_alloc() {
Profiler* profiler = malloc(sizeof(Profiler));
ProfilerRecordDict_init(profiler->records);
return profiler;
}
void profiler_free(Profiler* profiler) {
ProfilerRecordDict_clear(profiler->records);
free(profiler);
}
void profiler_prealloc(Profiler* profiler, const char* key) {
ProfilerRecord record = {
.start = 0,
.length = 0,
.count = 0,
};
ProfilerRecordDict_set_at(profiler->records, key, record);
}
void profiler_start(Profiler* profiler, const char* key) {
ProfilerRecord* record = ProfilerRecordDict_get(profiler->records, key);
if(record == NULL) {
profiler_prealloc(profiler, key);
record = ProfilerRecordDict_get(profiler->records, key);
}
furi_check(record->start == 0);
record->start = DWT->CYCCNT;
}
void profiler_stop(Profiler* profiler, const char* key) {
ProfilerRecord* record = ProfilerRecordDict_get(profiler->records, key);
furi_check(record != NULL);
record->length += DWT->CYCCNT - record->start;
record->start = 0;
record->count++;
}
void profiler_dump(Profiler* profiler) {
printf("Profiler:\r\n");
ProfilerRecordDict_it_t it;
for(ProfilerRecordDict_it(it, profiler->records); !ProfilerRecordDict_end_p(it);
ProfilerRecordDict_next(it)) {
const ProfilerRecordDict_itref_t* itref = ProfilerRecordDict_cref(it);
uint32_t count = itref->value.count;
uint32_t clocks = itref->value.length;
double us = (double)clocks / (double)64.0;
double ms = (double)clocks / (double)64000.0;
double s = (double)clocks / (double)64000000.0;
printf("\t%s[%lu]: %f s, %f ms, %f us, %lu clk\r\n", itref->key, count, s, ms, us, clocks);
if(count > 1) {
us /= (double)count;
ms /= (double)count;
s /= (double)count;
clocks /= count;
printf("\t%s[1]: %f s, %f ms, %f us, %lu clk\r\n", itref->key, s, ms, us, clocks);
}
}
}

23
lib/toolbox/profiler.h Normal file
View File

@@ -0,0 +1,23 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Profiler Profiler;
Profiler* profiler_alloc();
void profiler_free(Profiler* profiler);
void profiler_prealloc(Profiler* profiler, const char* key);
void profiler_start(Profiler* profiler, const char* key);
void profiler_stop(Profiler* profiler, const char* key);
void profiler_dump(Profiler* profiler);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,46 @@
#pragma once
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <lib/toolbox/level_duration.h>
#include <mlib/m-string.h>
typedef void* (*ProtocolAlloc)(void);
typedef void (*ProtocolFree)(void* protocol);
typedef uint8_t* (*ProtocolGetData)(void* protocol);
typedef void (*ProtocolDecoderStart)(void* protocol);
typedef bool (*ProtocolDecoderFeed)(void* protocol, bool level, uint32_t duration);
typedef bool (*ProtocolEncoderStart)(void* protocol);
typedef LevelDuration (*ProtocolEncoderYield)(void* protocol);
typedef void (*ProtocolRenderData)(void* protocol, string_t result);
typedef bool (*ProtocolWriteData)(void* protocol, void* data);
typedef struct {
ProtocolDecoderStart start;
ProtocolDecoderFeed feed;
} ProtocolDecoder;
typedef struct {
ProtocolEncoderStart start;
ProtocolEncoderYield yield;
} ProtocolEncoder;
typedef struct {
const size_t data_size;
const char* name;
const char* manufacturer;
const uint32_t features;
const uint8_t validate_count;
ProtocolAlloc alloc;
ProtocolFree free;
ProtocolGetData get_data;
ProtocolDecoder decoder;
ProtocolEncoder encoder;
ProtocolRenderData render_data;
ProtocolRenderData render_brief_data;
ProtocolWriteData write_data;
} ProtocolBase;

View File

@@ -0,0 +1,226 @@
#include <furi.h>
#include "protocol_dict.h"
struct ProtocolDict {
const ProtocolBase** base;
size_t count;
void** data;
};
ProtocolDict* protocol_dict_alloc(const ProtocolBase** protocols, size_t count) {
ProtocolDict* dict = malloc(sizeof(ProtocolDict));
dict->base = protocols;
dict->count = count;
dict->data = malloc(sizeof(void*) * dict->count);
for(size_t i = 0; i < dict->count; i++) {
dict->data[i] = dict->base[i]->alloc();
}
return dict;
}
void protocol_dict_free(ProtocolDict* dict) {
for(size_t i = 0; i < dict->count; i++) {
dict->base[i]->free(dict->data[i]);
}
free(dict->data);
free(dict);
}
void protocol_dict_set_data(
ProtocolDict* dict,
size_t protocol_index,
const uint8_t* data,
size_t data_size) {
furi_assert(protocol_index < dict->count);
furi_assert(dict->base[protocol_index]->get_data != NULL);
uint8_t* protocol_data = dict->base[protocol_index]->get_data(dict->data[protocol_index]);
size_t protocol_data_size = dict->base[protocol_index]->data_size;
furi_check(data_size >= protocol_data_size);
memcpy(protocol_data, data, protocol_data_size);
}
void protocol_dict_get_data(
ProtocolDict* dict,
size_t protocol_index,
uint8_t* data,
size_t data_size) {
furi_assert(protocol_index < dict->count);
furi_assert(dict->base[protocol_index]->get_data != NULL);
uint8_t* protocol_data = dict->base[protocol_index]->get_data(dict->data[protocol_index]);
size_t protocol_data_size = dict->base[protocol_index]->data_size;
furi_check(data_size >= protocol_data_size);
memcpy(data, protocol_data, protocol_data_size);
}
size_t protocol_dict_get_data_size(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
return dict->base[protocol_index]->data_size;
}
size_t protocol_dict_get_max_data_size(ProtocolDict* dict) {
size_t max_data_size = 0;
for(size_t i = 0; i < dict->count; i++) {
size_t data_size = dict->base[i]->data_size;
if(data_size > max_data_size) {
max_data_size = data_size;
}
}
return max_data_size;
}
const char* protocol_dict_get_name(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
return dict->base[protocol_index]->name;
}
const char* protocol_dict_get_manufacturer(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
return dict->base[protocol_index]->manufacturer;
}
void protocol_dict_decoders_start(ProtocolDict* dict) {
for(size_t i = 0; i < dict->count; i++) {
ProtocolDecoderStart fn = dict->base[i]->decoder.start;
if(fn) {
fn(dict->data[i]);
}
}
}
uint32_t protocol_dict_get_features(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
return dict->base[protocol_index]->features;
}
ProtocolId protocol_dict_decoders_feed(ProtocolDict* dict, bool level, uint32_t duration) {
bool done = false;
ProtocolId ready_protocol_id = PROTOCOL_NO;
for(size_t i = 0; i < dict->count; i++) {
ProtocolDecoderFeed fn = dict->base[i]->decoder.feed;
if(fn) {
if(fn(dict->data[i], level, duration)) {
if(!done) {
ready_protocol_id = i;
done = true;
}
}
}
}
return ready_protocol_id;
}
ProtocolId protocol_dict_decoders_feed_by_feature(
ProtocolDict* dict,
uint32_t feature,
bool level,
uint32_t duration) {
bool done = false;
ProtocolId ready_protocol_id = PROTOCOL_NO;
for(size_t i = 0; i < dict->count; i++) {
uint32_t features = dict->base[i]->features;
if(features & feature) {
ProtocolDecoderFeed fn = dict->base[i]->decoder.feed;
if(fn) {
if(fn(dict->data[i], level, duration)) {
if(!done) {
ready_protocol_id = i;
done = true;
}
}
}
}
}
return ready_protocol_id;
}
ProtocolId protocol_dict_decoders_feed_by_id(
ProtocolDict* dict,
size_t protocol_index,
bool level,
uint32_t duration) {
furi_assert(protocol_index < dict->count);
ProtocolId ready_protocol_id = PROTOCOL_NO;
ProtocolDecoderFeed fn = dict->base[protocol_index]->decoder.feed;
if(fn) {
if(fn(dict->data[protocol_index], level, duration)) {
ready_protocol_id = protocol_index;
}
}
return ready_protocol_id;
}
bool protocol_dict_encoder_start(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
ProtocolEncoderStart fn = dict->base[protocol_index]->encoder.start;
if(fn) {
return fn(dict->data[protocol_index]);
} else {
return false;
}
}
LevelDuration protocol_dict_encoder_yield(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
ProtocolEncoderYield fn = dict->base[protocol_index]->encoder.yield;
if(fn) {
return fn(dict->data[protocol_index]);
} else {
return level_duration_reset();
}
}
void protocol_dict_render_data(ProtocolDict* dict, string_t result, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
ProtocolRenderData fn = dict->base[protocol_index]->render_data;
if(fn) {
return fn(dict->data[protocol_index], result);
}
}
void protocol_dict_render_brief_data(ProtocolDict* dict, string_t result, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
ProtocolRenderData fn = dict->base[protocol_index]->render_brief_data;
if(fn) {
return fn(dict->data[protocol_index], result);
}
}
uint32_t protocol_dict_get_validate_count(ProtocolDict* dict, size_t protocol_index) {
furi_assert(protocol_index < dict->count);
return dict->base[protocol_index]->validate_count;
}
ProtocolId protocol_dict_get_protocol_by_name(ProtocolDict* dict, const char* name) {
for(size_t i = 0; i < dict->count; i++) {
if(strcmp(name, protocol_dict_get_name(dict, i)) == 0) {
return i;
}
}
return PROTOCOL_NO;
}
bool protocol_dict_get_write_data(ProtocolDict* dict, size_t protocol_index, void* data) {
furi_assert(protocol_index < dict->count);
ProtocolWriteData fn = dict->base[protocol_index]->write_data;
furi_assert(fn);
return fn(dict->data[protocol_index], data);
}

View File

@@ -0,0 +1,73 @@
#pragma once
#include "protocol.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct ProtocolDict ProtocolDict;
typedef int32_t ProtocolId;
#define PROTOCOL_NO (-1)
#define PROTOCOL_ALL_FEATURES (0xFFFFFFFF)
ProtocolDict* protocol_dict_alloc(const ProtocolBase** protocols, size_t protocol_count);
void protocol_dict_free(ProtocolDict* dict);
void protocol_dict_set_data(
ProtocolDict* dict,
size_t protocol_index,
const uint8_t* data,
size_t data_size);
void protocol_dict_get_data(
ProtocolDict* dict,
size_t protocol_index,
uint8_t* data,
size_t data_size);
size_t protocol_dict_get_data_size(ProtocolDict* dict, size_t protocol_index);
size_t protocol_dict_get_max_data_size(ProtocolDict* dict);
const char* protocol_dict_get_name(ProtocolDict* dict, size_t protocol_index);
const char* protocol_dict_get_manufacturer(ProtocolDict* dict, size_t protocol_index);
void protocol_dict_decoders_start(ProtocolDict* dict);
uint32_t protocol_dict_get_features(ProtocolDict* dict, size_t protocol_index);
ProtocolId protocol_dict_decoders_feed(ProtocolDict* dict, bool level, uint32_t duration);
ProtocolId protocol_dict_decoders_feed_by_feature(
ProtocolDict* dict,
uint32_t feature,
bool level,
uint32_t duration);
ProtocolId protocol_dict_decoders_feed_by_id(
ProtocolDict* dict,
size_t protocol_index,
bool level,
uint32_t duration);
bool protocol_dict_encoder_start(ProtocolDict* dict, size_t protocol_index);
LevelDuration protocol_dict_encoder_yield(ProtocolDict* dict, size_t protocol_index);
void protocol_dict_render_data(ProtocolDict* dict, string_t result, size_t protocol_index);
void protocol_dict_render_brief_data(ProtocolDict* dict, string_t result, size_t protocol_index);
uint32_t protocol_dict_get_validate_count(ProtocolDict* dict, size_t protocol_index);
ProtocolId protocol_dict_get_protocol_by_name(ProtocolDict* dict, const char* name);
bool protocol_dict_get_write_data(ProtocolDict* dict, size_t protocol_index, void* data);
#ifdef __cplusplus
}
#endif

117
lib/toolbox/pulse_joiner.c Normal file
View File

@@ -0,0 +1,117 @@
#include "pulse_joiner.h"
#include <furi.h>
#define PULSE_MAX_COUNT 6
typedef struct {
bool polarity;
uint16_t time;
} Pulse;
struct PulseJoiner {
size_t pulse_index;
Pulse pulses[PULSE_MAX_COUNT];
};
PulseJoiner* pulse_joiner_alloc() {
PulseJoiner* pulse_joiner = malloc(sizeof(PulseJoiner));
pulse_joiner->pulse_index = 0;
for(uint8_t i = 0; i < PULSE_MAX_COUNT; i++) {
pulse_joiner->pulses[i].polarity = false;
pulse_joiner->pulses[i].time = 0;
}
return pulse_joiner;
}
void pulse_joiner_free(PulseJoiner* pulse_joiner) {
free(pulse_joiner);
}
bool pulse_joiner_push_pulse(PulseJoiner* pulse_joiner, bool polarity, size_t period, size_t pulse) {
bool result = false;
furi_check((pulse_joiner->pulse_index + 1) < PULSE_MAX_COUNT);
if(polarity == false && pulse_joiner->pulse_index == 0) {
// first negative pulse is omitted
} else {
pulse_joiner->pulses[pulse_joiner->pulse_index].polarity = polarity;
pulse_joiner->pulses[pulse_joiner->pulse_index].time = pulse;
pulse_joiner->pulse_index++;
}
if(period > pulse) {
pulse_joiner->pulses[pulse_joiner->pulse_index].polarity = !polarity;
pulse_joiner->pulses[pulse_joiner->pulse_index].time = period - pulse;
pulse_joiner->pulse_index++;
}
if(pulse_joiner->pulse_index >= 4) {
// we know that first pulse is always high
// so we wait 2 edges, hi2low and next low2hi
uint8_t edges_count = 0;
bool last_polarity = pulse_joiner->pulses[0].polarity;
for(uint8_t i = 1; i < pulse_joiner->pulse_index; i++) {
if(pulse_joiner->pulses[i].polarity != last_polarity) {
edges_count++;
last_polarity = pulse_joiner->pulses[i].polarity;
}
}
if(edges_count >= 2) {
result = true;
}
}
return result;
}
void pulse_joiner_pop_pulse(PulseJoiner* pulse_joiner, size_t* period, size_t* pulse) {
furi_check(pulse_joiner->pulse_index <= (PULSE_MAX_COUNT + 1));
uint16_t tmp_period = 0;
uint16_t tmp_pulse = 0;
uint8_t edges_count = 0;
bool last_polarity = pulse_joiner->pulses[0].polarity;
uint8_t next_fist_pulse = 0;
for(uint8_t i = 0; i < PULSE_MAX_COUNT; i++) {
// count edges
if(pulse_joiner->pulses[i].polarity != last_polarity) {
edges_count++;
last_polarity = pulse_joiner->pulses[i].polarity;
}
// wait for 2 edges
if(edges_count == 2) {
next_fist_pulse = i;
break;
}
// sum pulse time
if(pulse_joiner->pulses[i].polarity) {
tmp_period += pulse_joiner->pulses[i].time;
tmp_pulse += pulse_joiner->pulses[i].time;
} else {
tmp_period += pulse_joiner->pulses[i].time;
}
pulse_joiner->pulse_index--;
}
*period = tmp_period;
*pulse = tmp_pulse;
// remove counted periods and shift data
for(uint8_t i = 0; i < PULSE_MAX_COUNT; i++) {
if((next_fist_pulse + i) < PULSE_MAX_COUNT) {
pulse_joiner->pulses[i].polarity = pulse_joiner->pulses[next_fist_pulse + i].polarity;
pulse_joiner->pulses[i].time = pulse_joiner->pulses[next_fist_pulse + i].time;
} else {
break;
}
}
}

View File

@@ -0,0 +1,46 @@
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct PulseJoiner PulseJoiner;
/**
* @brief Alloc PulseJoiner
*
* @return PulseJoiner*
*/
PulseJoiner* pulse_joiner_alloc();
/**
* @brief Free PulseJoiner
*
* @param pulse_joiner
*/
void pulse_joiner_free(PulseJoiner* pulse_joiner);
/**
* @brief Push timer pulse. First negative pulse is ommited.
*
* @param polarity pulse polarity: true = high2low, false = low2high
* @param period overall period time in timer clicks
* @param pulse pulse time in timer clicks
*
* @return true - next pulse can and must be popped immediatly
*/
bool pulse_joiner_push_pulse(PulseJoiner* pulse_joiner, bool polarity, size_t period, size_t pulse);
/**
* @brief Get the next timer pulse. Call only if push_pulse returns true.
*
* @param period overall period time in timer clicks
* @param pulse pulse time in timer clicks
*/
void pulse_joiner_pop_pulse(PulseJoiner* pulse_joiner, size_t* period, size_t* pulse);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,55 @@
#include "pulse_glue.h"
struct PulseGlue {
int32_t hi_period;
int32_t low_period;
int32_t next_hi_period;
};
PulseGlue* pulse_glue_alloc() {
PulseGlue* pulse_glue = malloc(sizeof(PulseGlue));
pulse_glue_reset(pulse_glue);
return pulse_glue;
}
void pulse_glue_free(PulseGlue* pulse_glue) {
free(pulse_glue);
}
void pulse_glue_reset(PulseGlue* pulse_glue) {
pulse_glue->hi_period = 0;
pulse_glue->low_period = 0;
pulse_glue->next_hi_period = 0;
}
bool pulse_glue_push(PulseGlue* pulse_glue, bool polarity, uint32_t length) {
bool pop_ready = false;
if(polarity) {
if(pulse_glue->low_period == 0) {
// stage 1, accumulate hi period
pulse_glue->hi_period += length;
} else {
// stage 3, accumulate next hi period and be ready for pulse_glue_pop
pulse_glue->next_hi_period = length;
// data is ready
pop_ready = true;
}
} else {
if(pulse_glue->hi_period != 0) {
// stage 2, accumulate low period
pulse_glue->low_period += length;
}
}
return pop_ready;
}
void pulse_glue_pop(PulseGlue* pulse_glue, uint32_t* length, uint32_t* period) {
*length = pulse_glue->hi_period + pulse_glue->low_period;
*period = pulse_glue->hi_period;
pulse_glue->hi_period = pulse_glue->next_hi_period;
pulse_glue->low_period = 0;
pulse_glue->next_hi_period = 0;
}

View File

@@ -0,0 +1,26 @@
/**
* @file pulse_glue.h
*
* Simple tool to glue separated pulses to corret
*/
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct PulseGlue PulseGlue;
PulseGlue* pulse_glue_alloc();
void pulse_glue_free(PulseGlue* pulse_glue);
void pulse_glue_reset(PulseGlue* pulse_glue);
bool pulse_glue_push(PulseGlue* pulse_glue, bool polarity, uint32_t length);
void pulse_glue_pop(PulseGlue* pulse_glue, uint32_t* length, uint32_t* period);
#ifdef __cplusplus
}
#endif

76
lib/toolbox/varint.c Normal file
View File

@@ -0,0 +1,76 @@
#include "varint.h"
size_t varint_uint32_pack(uint32_t value, uint8_t* output) {
uint8_t* start = output;
while(value >= 0x80) {
*output++ = (value | 0x80);
value >>= 7;
}
*output++ = value;
return output - start;
}
size_t varint_uint32_unpack(uint32_t* value, const uint8_t* input, size_t input_size) {
size_t i;
uint32_t parsed = 0;
for(i = 0; i < input_size; i++) {
parsed |= (input[i] & 0x7F) << (7 * i);
if(!(input[i] & 0x80)) {
break;
}
}
*value = parsed;
return i + 1;
}
size_t varint_uint32_length(uint32_t value) {
size_t size = 0;
while(value >= 0x80) {
value >>= 7;
size++;
}
size++;
return size;
}
size_t varint_int32_pack(int32_t value, uint8_t* output) {
uint32_t v;
if(value >= 0) {
v = value * 2;
} else {
v = (value * -2) - 1;
}
return varint_uint32_pack(v, output);
}
size_t varint_int32_unpack(int32_t* value, const uint8_t* input, size_t input_size) {
uint32_t v;
size_t size = varint_uint32_unpack(&v, input, input_size);
if(v & 1) {
*value = (int32_t)(v + 1) / (-2);
} else {
*value = v / 2;
}
return size;
}
size_t varint_int32_length(int32_t value) {
uint32_t v;
if(value >= 0) {
v = value * 2;
} else {
v = (value * -2) - 1;
}
return varint_uint32_length(v);
}

35
lib/toolbox/varint.h Normal file
View File

@@ -0,0 +1,35 @@
#pragma once
#include <stdint.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Pack uint32 to varint
* @param value value from UINT32_MIN to UINT32_MAX
* @param output output array, need to be at least 5 bytes long
* @return size_t
*/
size_t varint_uint32_pack(uint32_t value, uint8_t* output);
size_t varint_uint32_unpack(uint32_t* value, const uint8_t* input, size_t input_size);
size_t varint_uint32_length(uint32_t value);
/**
* Pack int32 to varint
* @param value value from (INT32_MIN / 2 + 1) to INT32_MAX
* @param output output array, need to be at least 5 bytes long
* @return size_t
*/
size_t varint_int32_pack(int32_t value, uint8_t* output);
size_t varint_int32_unpack(int32_t* value, const uint8_t* input, size_t input_size);
size_t varint_int32_length(int32_t value);
#ifdef __cplusplus
}
#endif