mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-06-05 18:41:54 -07:00
NFC refactoring (#3050)
"A long time ago in a galaxy far, far away...." we started NFC subsystem refactoring. Starring: - @gornekich - NFC refactoring project lead, architect, senior developer - @gsurkov - architect, senior developer - @RebornedBrain - senior developer Supporting roles: - @skotopes, @DrZlo13, @hedger - general architecture advisors, code review - @Astrrra, @doomwastaken, @Hellitron, @ImagineVagon333 - quality assurance Special thanks: @bettse, @pcunning, @nxv, @noproto, @AloneLiberty and everyone else who has been helping us all this time and contributing valuable knowledges, ideas and source code.
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
#include "iso14443_4b_i.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <nfc/protocols/nfc_device_base_i.h>
|
||||
|
||||
#define ISO14443_4B_PROTOCOL_NAME "ISO14443-4B"
|
||||
#define ISO14443_4B_DEVICE_NAME "ISO14443-4B (Unknown)"
|
||||
|
||||
const NfcDeviceBase nfc_device_iso14443_4b = {
|
||||
.protocol_name = ISO14443_4B_PROTOCOL_NAME,
|
||||
.alloc = (NfcDeviceAlloc)iso14443_4b_alloc,
|
||||
.free = (NfcDeviceFree)iso14443_4b_free,
|
||||
.reset = (NfcDeviceReset)iso14443_4b_reset,
|
||||
.copy = (NfcDeviceCopy)iso14443_4b_copy,
|
||||
.verify = (NfcDeviceVerify)iso14443_4b_verify,
|
||||
.load = (NfcDeviceLoad)iso14443_4b_load,
|
||||
.save = (NfcDeviceSave)iso14443_4b_save,
|
||||
.is_equal = (NfcDeviceEqual)iso14443_4b_is_equal,
|
||||
.get_name = (NfcDeviceGetName)iso14443_4b_get_device_name,
|
||||
.get_uid = (NfcDeviceGetUid)iso14443_4b_get_uid,
|
||||
.set_uid = (NfcDeviceSetUid)iso14443_4b_set_uid,
|
||||
.get_base_data = (NfcDeviceGetBaseData)iso14443_4b_get_base_data,
|
||||
};
|
||||
|
||||
Iso14443_4bData* iso14443_4b_alloc() {
|
||||
Iso14443_4bData* data = malloc(sizeof(Iso14443_4bData));
|
||||
|
||||
data->iso14443_3b_data = iso14443_3b_alloc();
|
||||
return data;
|
||||
}
|
||||
|
||||
void iso14443_4b_free(Iso14443_4bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
iso14443_3b_free(data->iso14443_3b_data);
|
||||
free(data);
|
||||
}
|
||||
|
||||
void iso14443_4b_reset(Iso14443_4bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
iso14443_3b_reset(data->iso14443_3b_data);
|
||||
}
|
||||
|
||||
void iso14443_4b_copy(Iso14443_4bData* data, const Iso14443_4bData* other) {
|
||||
furi_assert(data);
|
||||
furi_assert(other);
|
||||
|
||||
iso14443_3b_copy(data->iso14443_3b_data, other->iso14443_3b_data);
|
||||
}
|
||||
|
||||
bool iso14443_4b_verify(Iso14443_4bData* data, const FuriString* device_type) {
|
||||
UNUSED(data);
|
||||
UNUSED(device_type);
|
||||
|
||||
// Empty, unified file format only
|
||||
return false;
|
||||
}
|
||||
|
||||
bool iso14443_4b_load(Iso14443_4bData* data, FlipperFormat* ff, uint32_t version) {
|
||||
furi_assert(data);
|
||||
return iso14443_3b_load(data->iso14443_3b_data, ff, version);
|
||||
}
|
||||
|
||||
bool iso14443_4b_save(const Iso14443_4bData* data, FlipperFormat* ff) {
|
||||
furi_assert(data);
|
||||
return iso14443_3b_save(data->iso14443_3b_data, ff);
|
||||
}
|
||||
|
||||
bool iso14443_4b_is_equal(const Iso14443_4bData* data, const Iso14443_4bData* other) {
|
||||
return iso14443_3b_is_equal(data->iso14443_3b_data, other->iso14443_3b_data);
|
||||
}
|
||||
|
||||
const char* iso14443_4b_get_device_name(const Iso14443_4bData* data, NfcDeviceNameType name_type) {
|
||||
UNUSED(data);
|
||||
UNUSED(name_type);
|
||||
return ISO14443_4B_DEVICE_NAME;
|
||||
}
|
||||
|
||||
const uint8_t* iso14443_4b_get_uid(const Iso14443_4bData* data, size_t* uid_len) {
|
||||
return iso14443_3b_get_uid(data->iso14443_3b_data, uid_len);
|
||||
}
|
||||
|
||||
bool iso14443_4b_set_uid(Iso14443_4bData* data, const uint8_t* uid, size_t uid_len) {
|
||||
furi_assert(data);
|
||||
|
||||
return iso14443_3b_set_uid(data->iso14443_3b_data, uid, uid_len);
|
||||
}
|
||||
|
||||
Iso14443_3bData* iso14443_4b_get_base_data(const Iso14443_4bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
return data->iso14443_3b_data;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/iso14443_3b/iso14443_3b.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
Iso14443_4bErrorNone,
|
||||
Iso14443_4bErrorNotPresent,
|
||||
Iso14443_4bErrorProtocol,
|
||||
Iso14443_4bErrorTimeout,
|
||||
} Iso14443_4bError;
|
||||
|
||||
typedef struct Iso14443_4bData Iso14443_4bData;
|
||||
|
||||
// Virtual methods
|
||||
|
||||
Iso14443_4bData* iso14443_4b_alloc();
|
||||
|
||||
void iso14443_4b_free(Iso14443_4bData* data);
|
||||
|
||||
void iso14443_4b_reset(Iso14443_4bData* data);
|
||||
|
||||
void iso14443_4b_copy(Iso14443_4bData* data, const Iso14443_4bData* other);
|
||||
|
||||
bool iso14443_4b_verify(Iso14443_4bData* data, const FuriString* device_type);
|
||||
|
||||
bool iso14443_4b_load(Iso14443_4bData* data, FlipperFormat* ff, uint32_t version);
|
||||
|
||||
bool iso14443_4b_save(const Iso14443_4bData* data, FlipperFormat* ff);
|
||||
|
||||
bool iso14443_4b_is_equal(const Iso14443_4bData* data, const Iso14443_4bData* other);
|
||||
|
||||
const char* iso14443_4b_get_device_name(const Iso14443_4bData* data, NfcDeviceNameType name_type);
|
||||
|
||||
const uint8_t* iso14443_4b_get_uid(const Iso14443_4bData* data, size_t* uid_len);
|
||||
|
||||
bool iso14443_4b_set_uid(Iso14443_4bData* data, const uint8_t* uid, size_t uid_len);
|
||||
|
||||
Iso14443_3bData* iso14443_4b_get_base_data(const Iso14443_4bData* data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/nfc_device_base_i.h>
|
||||
|
||||
extern const NfcDeviceBase nfc_device_iso14443_4b;
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "iso14443_4b_i.h"
|
||||
|
||||
Iso14443_4bError iso14443_4b_process_error(Iso14443_3bError error) {
|
||||
switch(error) {
|
||||
case Iso14443_3bErrorNone:
|
||||
return Iso14443_4bErrorNone;
|
||||
case Iso14443_3bErrorNotPresent:
|
||||
return Iso14443_4bErrorNotPresent;
|
||||
case Iso14443_3bErrorColResFailed:
|
||||
case Iso14443_3bErrorCommunication:
|
||||
case Iso14443_3bErrorWrongCrc:
|
||||
return Iso14443_4bErrorProtocol;
|
||||
case Iso14443_3bErrorTimeout:
|
||||
return Iso14443_4bErrorTimeout;
|
||||
default:
|
||||
return Iso14443_4bErrorProtocol;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "iso14443_4b.h"
|
||||
|
||||
struct Iso14443_4bData {
|
||||
Iso14443_3bData* iso14443_3b_data;
|
||||
};
|
||||
|
||||
Iso14443_4bError iso14443_4b_process_error(Iso14443_3bError error);
|
||||
@@ -0,0 +1,138 @@
|
||||
#include "iso14443_4b_poller_i.h"
|
||||
|
||||
#include <nfc/protocols/nfc_poller_base.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#define TAG "Iso14443_4bPoller"
|
||||
|
||||
#define ISO14443_4A_POLLER_BUF_SIZE (256U)
|
||||
|
||||
typedef NfcCommand (*Iso14443_4bPollerStateHandler)(Iso14443_4bPoller* instance);
|
||||
|
||||
const Iso14443_4bData* iso14443_4b_poller_get_data(Iso14443_4bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
return instance->data;
|
||||
}
|
||||
|
||||
static Iso14443_4bPoller* iso14443_4b_poller_alloc(Iso14443_3bPoller* iso14443_3b_poller) {
|
||||
Iso14443_4bPoller* instance = malloc(sizeof(Iso14443_4bPoller));
|
||||
instance->iso14443_3b_poller = iso14443_3b_poller;
|
||||
instance->data = iso14443_4b_alloc();
|
||||
instance->iso14443_4_layer = iso14443_4_layer_alloc();
|
||||
instance->tx_buffer = bit_buffer_alloc(ISO14443_4A_POLLER_BUF_SIZE);
|
||||
instance->rx_buffer = bit_buffer_alloc(ISO14443_4A_POLLER_BUF_SIZE);
|
||||
|
||||
instance->iso14443_4b_event.data = &instance->iso14443_4b_event_data;
|
||||
|
||||
instance->general_event.protocol = NfcProtocolIso14443_4b;
|
||||
instance->general_event.event_data = &instance->iso14443_4b_event;
|
||||
instance->general_event.instance = instance;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void iso14443_4b_poller_free(Iso14443_4bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
iso14443_4b_free(instance->data);
|
||||
iso14443_4_layer_free(instance->iso14443_4_layer);
|
||||
bit_buffer_free(instance->tx_buffer);
|
||||
bit_buffer_free(instance->rx_buffer);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
static NfcCommand iso14443_4b_poller_handler_idle(Iso14443_4bPoller* instance) {
|
||||
iso14443_3b_copy(
|
||||
instance->data->iso14443_3b_data,
|
||||
iso14443_3b_poller_get_data(instance->iso14443_3b_poller));
|
||||
|
||||
iso14443_4_layer_reset(instance->iso14443_4_layer);
|
||||
instance->poller_state = Iso14443_4bPollerStateReady;
|
||||
return NfcCommandContinue;
|
||||
}
|
||||
|
||||
static NfcCommand iso14443_4b_poller_handler_error(Iso14443_4bPoller* instance) {
|
||||
iso14443_3b_poller_halt(instance->iso14443_3b_poller);
|
||||
instance->iso14443_4b_event_data.error = instance->error;
|
||||
NfcCommand command = instance->callback(instance->general_event, instance->context);
|
||||
instance->poller_state = Iso14443_4bPollerStateIdle;
|
||||
return command;
|
||||
}
|
||||
|
||||
static NfcCommand iso14443_4b_poller_handler_ready(Iso14443_4bPoller* instance) {
|
||||
instance->iso14443_4b_event.type = Iso14443_4bPollerEventTypeReady;
|
||||
NfcCommand command = instance->callback(instance->general_event, instance->context);
|
||||
return command;
|
||||
}
|
||||
|
||||
static const Iso14443_4bPollerStateHandler
|
||||
iso14443_4b_poller_state_handler[Iso14443_4bPollerStateNum] = {
|
||||
[Iso14443_4bPollerStateIdle] = iso14443_4b_poller_handler_idle,
|
||||
[Iso14443_4bPollerStateError] = iso14443_4b_poller_handler_error,
|
||||
[Iso14443_4bPollerStateReady] = iso14443_4b_poller_handler_ready,
|
||||
};
|
||||
|
||||
static void iso14443_4b_poller_set_callback(
|
||||
Iso14443_4bPoller* instance,
|
||||
NfcGenericCallback callback,
|
||||
void* context) {
|
||||
furi_assert(instance);
|
||||
furi_assert(callback);
|
||||
|
||||
instance->callback = callback;
|
||||
instance->context = context;
|
||||
}
|
||||
|
||||
static NfcCommand iso14443_4b_poller_run(NfcGenericEvent event, void* context) {
|
||||
furi_assert(event.protocol == NfcProtocolIso14443_3b);
|
||||
|
||||
Iso14443_4bPoller* instance = context;
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->callback);
|
||||
|
||||
Iso14443_3bPollerEvent* iso14443_3b_event = event.event_data;
|
||||
furi_assert(iso14443_3b_event);
|
||||
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
if(iso14443_3b_event->type == Iso14443_3bPollerEventTypeReady) {
|
||||
command = iso14443_4b_poller_state_handler[instance->poller_state](instance);
|
||||
} else if(iso14443_3b_event->type == Iso14443_3bPollerEventTypeError) {
|
||||
instance->iso14443_4b_event.type = Iso14443_4bPollerEventTypeError;
|
||||
command = instance->callback(instance->general_event, instance->context);
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
static bool iso14443_4b_poller_detect(NfcGenericEvent event, void* context) {
|
||||
furi_assert(event.protocol == NfcProtocolIso14443_3b);
|
||||
|
||||
const Iso14443_4bPoller* instance = context;
|
||||
furi_assert(instance);
|
||||
|
||||
const Iso14443_3bPollerEvent* iso14443_3b_event = event.event_data;
|
||||
furi_assert(iso14443_3b_event);
|
||||
iso14443_3b_copy(
|
||||
instance->data->iso14443_3b_data,
|
||||
iso14443_3b_poller_get_data(instance->iso14443_3b_poller));
|
||||
|
||||
bool protocol_detected = false;
|
||||
|
||||
if(iso14443_3b_event->type == Iso14443_3bPollerEventTypeReady) {
|
||||
protocol_detected = iso14443_3b_supports_iso14443_4(instance->data->iso14443_3b_data);
|
||||
}
|
||||
|
||||
return protocol_detected;
|
||||
}
|
||||
|
||||
const NfcPollerBase nfc_poller_iso14443_4b = {
|
||||
.alloc = (NfcPollerAlloc)iso14443_4b_poller_alloc,
|
||||
.free = (NfcPollerFree)iso14443_4b_poller_free,
|
||||
.set_callback = (NfcPollerSetCallback)iso14443_4b_poller_set_callback,
|
||||
.run = (NfcPollerRun)iso14443_4b_poller_run,
|
||||
.detect = (NfcPollerDetect)iso14443_4b_poller_detect,
|
||||
.get_data = (NfcPollerGetData)iso14443_4b_poller_get_data,
|
||||
};
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <lib/nfc/protocols/iso14443_3b/iso14443_3b_poller.h>
|
||||
|
||||
#include "iso14443_4b.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Iso14443_4bPoller Iso14443_4bPoller;
|
||||
|
||||
typedef enum {
|
||||
Iso14443_4bPollerEventTypeError,
|
||||
Iso14443_4bPollerEventTypeReady,
|
||||
} Iso14443_4bPollerEventType;
|
||||
|
||||
typedef struct {
|
||||
Iso14443_4bError error;
|
||||
} Iso14443_4bPollerEventData;
|
||||
|
||||
typedef struct {
|
||||
Iso14443_4bPollerEventType type;
|
||||
Iso14443_4bPollerEventData* data;
|
||||
} Iso14443_4bPollerEvent;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/nfc_poller_base.h>
|
||||
|
||||
extern const NfcPollerBase nfc_poller_iso14443_4b;
|
||||
@@ -0,0 +1,45 @@
|
||||
#include "iso14443_4b_poller_i.h"
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#include "iso14443_4b_i.h"
|
||||
|
||||
#define TAG "Iso14443_4bPoller"
|
||||
|
||||
Iso14443_4bError iso14443_4b_poller_halt(Iso14443_4bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
iso14443_3b_poller_halt(instance->iso14443_3b_poller);
|
||||
instance->poller_state = Iso14443_4bPollerStateIdle;
|
||||
|
||||
return Iso14443_4bErrorNone;
|
||||
}
|
||||
|
||||
Iso14443_4bError iso14443_4b_poller_send_block(
|
||||
Iso14443_4bPoller* instance,
|
||||
const BitBuffer* tx_buffer,
|
||||
BitBuffer* rx_buffer) {
|
||||
furi_assert(instance);
|
||||
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
iso14443_4_layer_encode_block(instance->iso14443_4_layer, tx_buffer, instance->tx_buffer);
|
||||
|
||||
Iso14443_4bError error = Iso14443_4bErrorNone;
|
||||
|
||||
do {
|
||||
Iso14443_3bError iso14443_3b_error = iso14443_3b_poller_send_frame(
|
||||
instance->iso14443_3b_poller, instance->tx_buffer, instance->rx_buffer);
|
||||
|
||||
if(iso14443_3b_error != Iso14443_3bErrorNone) {
|
||||
error = iso14443_4b_process_error(iso14443_3b_error);
|
||||
break;
|
||||
|
||||
} else if(!iso14443_4_layer_decode_block(
|
||||
instance->iso14443_4_layer, rx_buffer, instance->rx_buffer)) {
|
||||
error = Iso14443_4bErrorProtocol;
|
||||
break;
|
||||
}
|
||||
} while(false);
|
||||
|
||||
return error;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/iso14443_3b/iso14443_3b_poller_i.h>
|
||||
#include <nfc/helpers/iso14443_4_layer.h>
|
||||
|
||||
#include "iso14443_4b_poller.h"
|
||||
#include "iso14443_4b_i.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
Iso14443_4bPollerStateIdle,
|
||||
Iso14443_4bPollerStateError,
|
||||
Iso14443_4bPollerStateReady,
|
||||
|
||||
Iso14443_4bPollerStateNum,
|
||||
} Iso14443_4bPollerState;
|
||||
|
||||
typedef enum {
|
||||
Iso14443_4bPollerSessionStateIdle,
|
||||
Iso14443_4bPollerSessionStateActive,
|
||||
Iso14443_4bPollerSessionStateStopRequest,
|
||||
} Iso14443_4bPollerSessionState;
|
||||
|
||||
struct Iso14443_4bPoller {
|
||||
Iso14443_3bPoller* iso14443_3b_poller;
|
||||
Iso14443_4bPollerState poller_state;
|
||||
Iso14443_4bPollerSessionState session_state;
|
||||
Iso14443_4bError error;
|
||||
Iso14443_4bData* data;
|
||||
Iso14443_4Layer* iso14443_4_layer;
|
||||
BitBuffer* tx_buffer;
|
||||
BitBuffer* rx_buffer;
|
||||
Iso14443_4bPollerEventData iso14443_4b_event_data;
|
||||
Iso14443_4bPollerEvent iso14443_4b_event;
|
||||
NfcGenericEvent general_event;
|
||||
NfcGenericCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
Iso14443_4bError iso14443_4b_process_error(Iso14443_3bError error);
|
||||
|
||||
const Iso14443_4bData* iso14443_4b_poller_get_data(Iso14443_4bPoller* instance);
|
||||
|
||||
Iso14443_4bError iso14443_4b_poller_halt(Iso14443_4bPoller* instance);
|
||||
|
||||
Iso14443_4bError iso14443_4b_poller_send_block(
|
||||
Iso14443_4bPoller* instance,
|
||||
const BitBuffer* tx_buffer,
|
||||
BitBuffer* rx_buffer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user