flipperzero-firmware/lib/lfrfid/lfrfid_worker.c

168 lines
5.4 KiB
C
Raw Normal View History

[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-23 15:57:39 +00:00
#include <furi.h>
#include <furi_hal.h>
#include <atomic.h>
#include "lfrfid_worker_i.h"
typedef enum {
LFRFIDEventStopThread = (1 << 0),
LFRFIDEventStopMode = (1 << 1),
LFRFIDEventRead = (1 << 2),
LFRFIDEventWrite = (1 << 3),
LFRFIDEventEmulate = (1 << 4),
LFRFIDEventReadRaw = (1 << 5),
LFRFIDEventEmulateRaw = (1 << 6),
LFRFIDEventAll =
(LFRFIDEventStopThread | LFRFIDEventStopMode | LFRFIDEventRead | LFRFIDEventWrite |
LFRFIDEventEmulate | LFRFIDEventReadRaw | LFRFIDEventEmulateRaw),
} LFRFIDEventType;
static int32_t lfrfid_worker_thread(void* thread_context);
LFRFIDWorker* lfrfid_worker_alloc(ProtocolDict* dict) {
furi_assert(dict);
LFRFIDWorker* worker = malloc(sizeof(LFRFIDWorker));
worker->mode_index = LFRFIDWorkerIdle;
worker->read_cb = NULL;
worker->write_cb = NULL;
worker->cb_ctx = NULL;
worker->raw_filename = NULL;
worker->mode_storage = NULL;
worker->thread = furi_thread_alloc();
furi_thread_set_name(worker->thread, "lfrfid_worker");
furi_thread_set_callback(worker->thread, lfrfid_worker_thread);
furi_thread_set_context(worker->thread, worker);
furi_thread_set_stack_size(worker->thread, 2048);
worker->protocols = dict;
return worker;
}
void lfrfid_worker_free(LFRFIDWorker* worker) {
if(worker->raw_filename) {
free(worker->raw_filename);
}
furi_thread_free(worker->thread);
free(worker);
}
void lfrfid_worker_read_start(
LFRFIDWorker* worker,
LFRFIDWorkerReadType type,
LFRFIDWorkerReadCallback callback,
void* context) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
worker->read_type = type;
worker->read_cb = callback;
worker->cb_ctx = context;
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventRead);
}
void lfrfid_worker_write_start(
LFRFIDWorker* worker,
LFRFIDProtocol protocol,
LFRFIDWorkerWriteCallback callback,
void* context) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
worker->protocol = protocol;
worker->write_cb = callback;
worker->cb_ctx = context;
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventWrite);
}
void lfrfid_worker_emulate_start(LFRFIDWorker* worker, LFRFIDProtocol protocol) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
worker->protocol = protocol;
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventEmulate);
}
void lfrfid_worker_set_filename(LFRFIDWorker* worker, const char* filename) {
if(worker->raw_filename) {
free(worker->raw_filename);
}
worker->raw_filename = strdup(filename);
}
void lfrfid_worker_read_raw_start(
LFRFIDWorker* worker,
const char* filename,
LFRFIDWorkerReadType type,
LFRFIDWorkerReadRawCallback callback,
void* context) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
worker->read_type = type;
worker->read_raw_cb = callback;
worker->cb_ctx = context;
lfrfid_worker_set_filename(worker, filename);
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventReadRaw);
}
void lfrfid_worker_emulate_raw_start(
LFRFIDWorker* worker,
const char* filename,
LFRFIDWorkerEmulateRawCallback callback,
void* context) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
lfrfid_worker_set_filename(worker, filename);
worker->emulate_raw_cb = callback;
worker->cb_ctx = context;
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventEmulateRaw);
}
void lfrfid_worker_stop(LFRFIDWorker* worker) {
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventStopMode);
}
void lfrfid_worker_start_thread(LFRFIDWorker* worker) {
furi_thread_start(worker->thread);
}
void lfrfid_worker_stop_thread(LFRFIDWorker* worker) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
furi_thread_flags_set(furi_thread_get_id(worker->thread), LFRFIDEventStopThread);
furi_thread_join(worker->thread);
}
bool lfrfid_worker_check_for_stop(LFRFIDWorker* worker) {
UNUSED(worker);
uint32_t flags = furi_thread_flags_get();
return (flags & LFRFIDEventStopMode);
}
size_t lfrfid_worker_dict_get_data_size(LFRFIDWorker* worker, LFRFIDProtocol protocol) {
furi_assert(worker->mode_index == LFRFIDWorkerIdle);
return protocol_dict_get_data_size(worker->protocols, protocol);
}
static int32_t lfrfid_worker_thread(void* thread_context) {
LFRFIDWorker* worker = thread_context;
while(true) {
[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-23 15:57:39 +00:00
uint32_t flags = furi_thread_flags_wait(LFRFIDEventAll, FuriFlagWaitAny, FuriWaitForever);
if(flags != FuriFlagErrorTimeout) {
// stop thread
if(flags & LFRFIDEventStopThread) break;
// switch mode
if(flags & LFRFIDEventRead) worker->mode_index = LFRFIDWorkerRead;
if(flags & LFRFIDEventWrite) worker->mode_index = LFRFIDWorkerWrite;
if(flags & LFRFIDEventEmulate) worker->mode_index = LFRFIDWorkerEmulate;
if(flags & LFRFIDEventReadRaw) worker->mode_index = LFRFIDWorkerReadRaw;
if(flags & LFRFIDEventEmulateRaw) worker->mode_index = LFRFIDWorkerEmulateRaw;
// do mode, if it exists
if(lfrfid_worker_modes[worker->mode_index].process) {
lfrfid_worker_modes[worker->mode_index].process(worker);
}
// reset mode
worker->mode_index = LFRFIDWorkerIdle;
}
}
return 0;
}