flipperzero-firmware/lib/toolbox/protocols/protocol_dict.c
SG 9bfb641d3e
[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>
2022-08-24 00:57:39 +09:00

226 lines
6.5 KiB
C

#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);
}