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,223 @@
|
||||
#include "iso14443_3b_i.h"
|
||||
|
||||
#include <furi.h>
|
||||
#include <nfc/protocols/nfc_device_base_i.h>
|
||||
|
||||
#include <nfc/nfc_common.h>
|
||||
#include <nfc/helpers/iso14443_crc.h>
|
||||
|
||||
#define ISO14443_3B_PROTOCOL_NAME "ISO14443-3B"
|
||||
#define ISO14443_3B_DEVICE_NAME "ISO14443-3B (Unknown)"
|
||||
|
||||
#define ISO14443_3B_APP_DATA_KEY "Application data"
|
||||
#define ISO14443_3B_PROTOCOL_INFO_KEY "Protocol info"
|
||||
|
||||
#define ISO14443_3B_FDT_POLL_DEFAULT_FC (ISO14443_3B_FDT_POLL_FC)
|
||||
|
||||
const NfcDeviceBase nfc_device_iso14443_3b = {
|
||||
.protocol_name = ISO14443_3B_PROTOCOL_NAME,
|
||||
.alloc = (NfcDeviceAlloc)iso14443_3b_alloc,
|
||||
.free = (NfcDeviceFree)iso14443_3b_free,
|
||||
.reset = (NfcDeviceReset)iso14443_3b_reset,
|
||||
.copy = (NfcDeviceCopy)iso14443_3b_copy,
|
||||
.verify = (NfcDeviceVerify)iso14443_3b_verify,
|
||||
.load = (NfcDeviceLoad)iso14443_3b_load,
|
||||
.save = (NfcDeviceSave)iso14443_3b_save,
|
||||
.is_equal = (NfcDeviceEqual)iso14443_3b_is_equal,
|
||||
.get_name = (NfcDeviceGetName)iso14443_3b_get_device_name,
|
||||
.get_uid = (NfcDeviceGetUid)iso14443_3b_get_uid,
|
||||
.set_uid = (NfcDeviceSetUid)iso14443_3b_set_uid,
|
||||
.get_base_data = (NfcDeviceGetBaseData)iso14443_3b_get_base_data,
|
||||
};
|
||||
|
||||
Iso14443_3bData* iso14443_3b_alloc() {
|
||||
Iso14443_3bData* data = malloc(sizeof(Iso14443_3bData));
|
||||
return data;
|
||||
}
|
||||
|
||||
void iso14443_3b_free(Iso14443_3bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
free(data);
|
||||
}
|
||||
|
||||
void iso14443_3b_reset(Iso14443_3bData* data) {
|
||||
memset(data, 0, sizeof(Iso14443_3bData));
|
||||
}
|
||||
|
||||
void iso14443_3b_copy(Iso14443_3bData* data, const Iso14443_3bData* other) {
|
||||
furi_assert(data);
|
||||
furi_assert(other);
|
||||
|
||||
*data = *other;
|
||||
}
|
||||
|
||||
bool iso14443_3b_verify(Iso14443_3bData* data, const FuriString* device_type) {
|
||||
UNUSED(data);
|
||||
UNUSED(device_type);
|
||||
// No support for old ISO14443-3B
|
||||
return false;
|
||||
}
|
||||
|
||||
bool iso14443_3b_load(Iso14443_3bData* data, FlipperFormat* ff, uint32_t version) {
|
||||
furi_assert(data);
|
||||
|
||||
bool parsed = false;
|
||||
|
||||
do {
|
||||
if(version < NFC_UNIFIED_FORMAT_VERSION) break;
|
||||
|
||||
if(!flipper_format_read_hex(
|
||||
ff, ISO14443_3B_APP_DATA_KEY, data->app_data, ISO14443_3B_APP_DATA_SIZE))
|
||||
break;
|
||||
if(!flipper_format_read_hex(
|
||||
ff,
|
||||
ISO14443_3B_PROTOCOL_INFO_KEY,
|
||||
(uint8_t*)&data->protocol_info,
|
||||
sizeof(Iso14443_3bProtocolInfo)))
|
||||
break;
|
||||
|
||||
parsed = true;
|
||||
} while(false);
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
bool iso14443_3b_save(const Iso14443_3bData* data, FlipperFormat* ff) {
|
||||
furi_assert(data);
|
||||
|
||||
bool saved = false;
|
||||
|
||||
do {
|
||||
if(!flipper_format_write_comment_cstr(ff, ISO14443_3B_PROTOCOL_NAME " specific data"))
|
||||
break;
|
||||
if(!flipper_format_write_hex(
|
||||
ff, ISO14443_3B_APP_DATA_KEY, data->app_data, ISO14443_3B_APP_DATA_SIZE))
|
||||
break;
|
||||
if(!flipper_format_write_hex(
|
||||
ff,
|
||||
ISO14443_3B_PROTOCOL_INFO_KEY,
|
||||
(uint8_t*)&data->protocol_info,
|
||||
sizeof(Iso14443_3bProtocolInfo)))
|
||||
break;
|
||||
saved = true;
|
||||
} while(false);
|
||||
|
||||
return saved;
|
||||
}
|
||||
|
||||
bool iso14443_3b_is_equal(const Iso14443_3bData* data, const Iso14443_3bData* other) {
|
||||
furi_assert(data);
|
||||
furi_assert(other);
|
||||
|
||||
return memcmp(data, other, sizeof(Iso14443_3bData)) == 0;
|
||||
}
|
||||
|
||||
const char* iso14443_3b_get_device_name(const Iso14443_3bData* data, NfcDeviceNameType name_type) {
|
||||
UNUSED(data);
|
||||
UNUSED(name_type);
|
||||
|
||||
return ISO14443_3B_DEVICE_NAME;
|
||||
}
|
||||
|
||||
const uint8_t* iso14443_3b_get_uid(const Iso14443_3bData* data, size_t* uid_len) {
|
||||
furi_assert(data);
|
||||
furi_assert(uid_len);
|
||||
|
||||
*uid_len = ISO14443_3B_UID_SIZE;
|
||||
return data->uid;
|
||||
}
|
||||
|
||||
bool iso14443_3b_set_uid(Iso14443_3bData* data, const uint8_t* uid, size_t uid_len) {
|
||||
furi_assert(data);
|
||||
|
||||
const bool uid_valid = uid_len == ISO14443_3B_UID_SIZE;
|
||||
|
||||
if(uid_valid) {
|
||||
memcpy(data->uid, uid, uid_len);
|
||||
}
|
||||
|
||||
return uid_valid;
|
||||
}
|
||||
|
||||
Iso14443_3bData* iso14443_3b_get_base_data(const Iso14443_3bData* data) {
|
||||
UNUSED(data);
|
||||
furi_crash("No base data");
|
||||
}
|
||||
|
||||
bool iso14443_3b_supports_iso14443_4(const Iso14443_3bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
return data->protocol_info.protocol_type == 0x01;
|
||||
}
|
||||
|
||||
bool iso14443_3b_supports_bit_rate(const Iso14443_3bData* data, Iso14443_3bBitRate bit_rate) {
|
||||
furi_assert(data);
|
||||
|
||||
const uint8_t capability = data->protocol_info.bit_rate_capability;
|
||||
|
||||
switch(bit_rate) {
|
||||
case Iso14443_3bBitRateBoth106Kbit:
|
||||
return capability == ISO14443_3B_BIT_RATE_BOTH_106KBIT;
|
||||
case Iso14443_3bBitRatePiccToPcd212Kbit:
|
||||
return capability & ISO14443_3B_BIT_RATE_PICC_TO_PCD_212KBIT;
|
||||
case Iso14443_3bBitRatePiccToPcd424Kbit:
|
||||
return capability & ISO14443_3B_BIT_RATE_PICC_TO_PCD_424KBIT;
|
||||
case Iso14443_3bBitRatePiccToPcd848Kbit:
|
||||
return capability & ISO14443_3B_BIT_RATE_PICC_TO_PCD_848KBIT;
|
||||
case Iso14443_3bBitRatePcdToPicc212Kbit:
|
||||
return capability & ISO14443_3B_BIT_RATE_PCD_TO_PICC_212KBIT;
|
||||
case Iso14443_3bBitRatePcdToPicc424Kbit:
|
||||
return capability & ISO14443_3B_BIT_RATE_PCD_TO_PICC_424KBIT;
|
||||
case Iso14443_3bBitRatePcdToPicc848Kbit:
|
||||
return capability & ISO14443_3B_BIT_RATE_PCD_TO_PICC_848KBIT;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool iso14443_3b_supports_frame_option(const Iso14443_3bData* data, Iso14443_3bFrameOption option) {
|
||||
furi_assert(data);
|
||||
|
||||
switch(option) {
|
||||
case Iso14443_3bFrameOptionNad:
|
||||
return data->protocol_info.fo & ISO14443_3B_FRAME_OPTION_NAD;
|
||||
case Iso14443_3bFrameOptionCid:
|
||||
return data->protocol_info.fo & ISO14443_3B_FRAME_OPTION_CID;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const uint8_t* iso14443_3b_get_application_data(const Iso14443_3bData* data, size_t* data_size) {
|
||||
furi_assert(data);
|
||||
furi_assert(data_size);
|
||||
|
||||
*data_size = ISO14443_3B_APP_DATA_SIZE;
|
||||
return data->app_data;
|
||||
}
|
||||
|
||||
uint16_t iso14443_3b_get_frame_size_max(const Iso14443_3bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
const uint8_t fs_bits = data->protocol_info.max_frame_size;
|
||||
|
||||
if(fs_bits < 5) {
|
||||
return fs_bits * 8 + 16;
|
||||
} else if(fs_bits == 5) {
|
||||
return 64;
|
||||
} else if(fs_bits == 6) {
|
||||
return 96;
|
||||
} else if(fs_bits < 13) {
|
||||
return 128U << (fs_bits - 7);
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t iso14443_3b_get_fwt_fc_max(const Iso14443_3bData* data) {
|
||||
furi_assert(data);
|
||||
|
||||
const uint8_t fwi = data->protocol_info.fwi;
|
||||
return fwi < 0x0F ? 4096UL << fwi : ISO14443_3B_FDT_POLL_DEFAULT_FC;
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/nfc_device_base.h>
|
||||
|
||||
#include <core/string.h>
|
||||
#include <toolbox/bit_buffer.h>
|
||||
#include <flipper_format/flipper_format.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
Iso14443_3bErrorNone,
|
||||
Iso14443_3bErrorNotPresent,
|
||||
Iso14443_3bErrorColResFailed,
|
||||
Iso14443_3bErrorBufferOverflow,
|
||||
Iso14443_3bErrorCommunication,
|
||||
Iso14443_3bErrorFieldOff,
|
||||
Iso14443_3bErrorWrongCrc,
|
||||
Iso14443_3bErrorTimeout,
|
||||
} Iso14443_3bError;
|
||||
|
||||
typedef enum {
|
||||
Iso14443_3bBitRateBoth106Kbit,
|
||||
Iso14443_3bBitRatePiccToPcd212Kbit,
|
||||
Iso14443_3bBitRatePiccToPcd424Kbit,
|
||||
Iso14443_3bBitRatePiccToPcd848Kbit,
|
||||
Iso14443_3bBitRatePcdToPicc212Kbit,
|
||||
Iso14443_3bBitRatePcdToPicc424Kbit,
|
||||
Iso14443_3bBitRatePcdToPicc848Kbit,
|
||||
} Iso14443_3bBitRate;
|
||||
|
||||
typedef enum {
|
||||
Iso14443_3bFrameOptionNad,
|
||||
Iso14443_3bFrameOptionCid,
|
||||
} Iso14443_3bFrameOption;
|
||||
|
||||
typedef struct Iso14443_3bData Iso14443_3bData;
|
||||
|
||||
// Virtual methods
|
||||
|
||||
Iso14443_3bData* iso14443_3b_alloc();
|
||||
|
||||
void iso14443_3b_free(Iso14443_3bData* data);
|
||||
|
||||
void iso14443_3b_reset(Iso14443_3bData* data);
|
||||
|
||||
void iso14443_3b_copy(Iso14443_3bData* data, const Iso14443_3bData* other);
|
||||
|
||||
bool iso14443_3b_verify(Iso14443_3bData* data, const FuriString* device_type);
|
||||
|
||||
bool iso14443_3b_load(Iso14443_3bData* data, FlipperFormat* ff, uint32_t version);
|
||||
|
||||
bool iso14443_3b_save(const Iso14443_3bData* data, FlipperFormat* ff);
|
||||
|
||||
bool iso14443_3b_is_equal(const Iso14443_3bData* data, const Iso14443_3bData* other);
|
||||
|
||||
const char* iso14443_3b_get_device_name(const Iso14443_3bData* data, NfcDeviceNameType name_type);
|
||||
|
||||
const uint8_t* iso14443_3b_get_uid(const Iso14443_3bData* data, size_t* uid_len);
|
||||
|
||||
bool iso14443_3b_set_uid(Iso14443_3bData* data, const uint8_t* uid, size_t uid_len);
|
||||
|
||||
Iso14443_3bData* iso14443_3b_get_base_data(const Iso14443_3bData* data);
|
||||
|
||||
// Getters and tests
|
||||
|
||||
bool iso14443_3b_supports_iso14443_4(const Iso14443_3bData* data);
|
||||
|
||||
bool iso14443_3b_supports_bit_rate(const Iso14443_3bData* data, Iso14443_3bBitRate bit_rate);
|
||||
|
||||
bool iso14443_3b_supports_frame_option(const Iso14443_3bData* data, Iso14443_3bFrameOption option);
|
||||
|
||||
const uint8_t* iso14443_3b_get_application_data(const Iso14443_3bData* data, size_t* data_size);
|
||||
|
||||
uint16_t iso14443_3b_get_frame_size_max(const Iso14443_3bData* data);
|
||||
|
||||
uint32_t iso14443_3b_get_fwt_fc_max(const Iso14443_3bData* data);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/nfc_device_base_i.h>
|
||||
|
||||
extern const NfcDeviceBase nfc_device_iso14443_3b;
|
||||
@@ -0,0 +1 @@
|
||||
#include "iso14443_3b_i.h"
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include "iso14443_3b.h"
|
||||
|
||||
#define ISO14443_3B_UID_SIZE (4U)
|
||||
#define ISO14443_3B_APP_DATA_SIZE (4U)
|
||||
|
||||
#define ISO14443_3B_GUARD_TIME_US (5000U)
|
||||
#define ISO14443_3B_FDT_POLL_FC (9000U)
|
||||
#define ISO14443_3B_POLL_POLL_MIN_US (1280U)
|
||||
|
||||
#define ISO14443_3B_BIT_RATE_BOTH_106KBIT (0U << 0)
|
||||
#define ISO14443_3B_BIT_RATE_PCD_TO_PICC_212KBIT (1U << 0)
|
||||
#define ISO14443_3B_BIT_RATE_PCD_TO_PICC_424KBIT (1U << 1)
|
||||
#define ISO14443_3B_BIT_RATE_PCD_TO_PICC_848KBIT (1U << 2)
|
||||
#define ISO14443_3B_BIT_RATE_PICC_TO_PCD_212KBIT (1U << 4)
|
||||
#define ISO14443_3B_BIT_RATE_PICC_TO_PCD_424KBIT (1U << 5)
|
||||
#define ISO14443_3B_BIT_RATE_PICC_TO_PCD_848KBIT (1U << 6)
|
||||
#define ISO14443_3B_BIT_RATE_BOTH_SAME_COMPULSORY (1U << 7)
|
||||
|
||||
#define ISO14443_3B_FRAME_OPTION_NAD (1U << 1)
|
||||
#define ISO14443_3B_FRAME_OPTION_CID (1U << 0)
|
||||
|
||||
typedef struct {
|
||||
uint8_t bit_rate_capability;
|
||||
uint8_t protocol_type : 4;
|
||||
uint8_t max_frame_size : 4;
|
||||
uint8_t fo : 2;
|
||||
uint8_t adc : 2;
|
||||
uint8_t fwi : 4;
|
||||
} Iso14443_3bProtocolInfo;
|
||||
|
||||
struct Iso14443_3bData {
|
||||
uint8_t uid[ISO14443_3B_UID_SIZE];
|
||||
uint8_t app_data[ISO14443_3B_APP_DATA_SIZE];
|
||||
Iso14443_3bProtocolInfo protocol_info;
|
||||
};
|
||||
@@ -0,0 +1,121 @@
|
||||
#include "iso14443_3b_poller_i.h"
|
||||
|
||||
#include <nfc/protocols/nfc_poller_base.h>
|
||||
|
||||
#include <furi.h>
|
||||
|
||||
#define TAG "ISO14443_3bPoller"
|
||||
|
||||
const Iso14443_3bData* iso14443_3b_poller_get_data(Iso14443_3bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->data);
|
||||
|
||||
return instance->data;
|
||||
}
|
||||
|
||||
static Iso14443_3bPoller* iso14443_3b_poller_alloc(Nfc* nfc) {
|
||||
furi_assert(nfc);
|
||||
|
||||
Iso14443_3bPoller* instance = malloc(sizeof(Iso14443_3bPoller));
|
||||
instance->nfc = nfc;
|
||||
instance->tx_buffer = bit_buffer_alloc(ISO14443_3B_POLLER_MAX_BUFFER_SIZE);
|
||||
instance->rx_buffer = bit_buffer_alloc(ISO14443_3B_POLLER_MAX_BUFFER_SIZE);
|
||||
|
||||
nfc_config(instance->nfc, NfcModePoller, NfcTechIso14443b);
|
||||
nfc_set_guard_time_us(instance->nfc, ISO14443_3B_GUARD_TIME_US);
|
||||
nfc_set_fdt_poll_fc(instance->nfc, ISO14443_3B_FDT_POLL_FC);
|
||||
nfc_set_fdt_poll_poll_us(instance->nfc, ISO14443_3B_POLL_POLL_MIN_US);
|
||||
instance->data = iso14443_3b_alloc();
|
||||
|
||||
instance->iso14443_3b_event.data = &instance->iso14443_3b_event_data;
|
||||
instance->general_event.protocol = NfcProtocolIso14443_3b;
|
||||
instance->general_event.event_data = &instance->iso14443_3b_event;
|
||||
instance->general_event.instance = instance;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
static void iso14443_3b_poller_free(Iso14443_3bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
furi_assert(instance->tx_buffer);
|
||||
furi_assert(instance->rx_buffer);
|
||||
furi_assert(instance->data);
|
||||
|
||||
bit_buffer_free(instance->tx_buffer);
|
||||
bit_buffer_free(instance->rx_buffer);
|
||||
iso14443_3b_free(instance->data);
|
||||
free(instance);
|
||||
}
|
||||
|
||||
static void iso14443_3b_poller_set_callback(
|
||||
Iso14443_3bPoller* instance,
|
||||
NfcGenericCallback callback,
|
||||
void* context) {
|
||||
furi_assert(instance);
|
||||
furi_assert(callback);
|
||||
|
||||
instance->callback = callback;
|
||||
instance->context = context;
|
||||
}
|
||||
|
||||
static NfcCommand iso14443_3b_poller_run(NfcGenericEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
furi_assert(event.protocol == NfcProtocolInvalid);
|
||||
furi_assert(event.event_data);
|
||||
|
||||
Iso14443_3bPoller* instance = context;
|
||||
NfcEvent* nfc_event = event.event_data;
|
||||
NfcCommand command = NfcCommandContinue;
|
||||
|
||||
if(nfc_event->type == NfcEventTypePollerReady) {
|
||||
if(instance->state != Iso14443_3bPollerStateActivated) {
|
||||
Iso14443_3bError error = iso14443_3b_poller_async_activate(instance, instance->data);
|
||||
if(error == Iso14443_3bErrorNone) {
|
||||
instance->iso14443_3b_event.type = Iso14443_3bPollerEventTypeReady;
|
||||
instance->iso14443_3b_event_data.error = error;
|
||||
command = instance->callback(instance->general_event, instance->context);
|
||||
} else {
|
||||
instance->iso14443_3b_event.type = Iso14443_3bPollerEventTypeError;
|
||||
instance->iso14443_3b_event_data.error = error;
|
||||
command = instance->callback(instance->general_event, instance->context);
|
||||
// Add delay to switch context
|
||||
furi_delay_ms(100);
|
||||
}
|
||||
} else {
|
||||
instance->iso14443_3b_event.type = Iso14443_3bPollerEventTypeReady;
|
||||
instance->iso14443_3b_event_data.error = Iso14443_3bErrorNone;
|
||||
command = instance->callback(instance->general_event, instance->context);
|
||||
}
|
||||
}
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
static bool iso14443_3b_poller_detect(NfcGenericEvent event, void* context) {
|
||||
furi_assert(context);
|
||||
furi_assert(event.event_data);
|
||||
furi_assert(event.instance);
|
||||
furi_assert(event.protocol == NfcProtocolInvalid);
|
||||
|
||||
bool protocol_detected = false;
|
||||
Iso14443_3bPoller* instance = context;
|
||||
NfcEvent* nfc_event = event.event_data;
|
||||
furi_assert(instance->state == Iso14443_3bPollerStateIdle);
|
||||
|
||||
if(nfc_event->type == NfcEventTypePollerReady) {
|
||||
Iso14443_3bError error = iso14443_3b_poller_async_activate(instance, instance->data);
|
||||
protocol_detected = (error == Iso14443_3bErrorNone);
|
||||
}
|
||||
|
||||
return protocol_detected;
|
||||
}
|
||||
|
||||
const NfcPollerBase nfc_poller_iso14443_3b = {
|
||||
.alloc = (NfcPollerAlloc)iso14443_3b_poller_alloc,
|
||||
.free = (NfcPollerFree)iso14443_3b_poller_free,
|
||||
.set_callback = (NfcPollerSetCallback)iso14443_3b_poller_set_callback,
|
||||
.run = (NfcPollerRun)iso14443_3b_poller_run,
|
||||
.detect = (NfcPollerDetect)iso14443_3b_poller_detect,
|
||||
.get_data = (NfcPollerGetData)iso14443_3b_poller_get_data,
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#include "iso14443_3b.h"
|
||||
#include <lib/nfc/nfc.h>
|
||||
|
||||
#include <nfc/nfc_poller.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct Iso14443_3bPoller Iso14443_3bPoller;
|
||||
|
||||
typedef enum {
|
||||
Iso14443_3bPollerEventTypeError,
|
||||
Iso14443_3bPollerEventTypeReady,
|
||||
} Iso14443_3bPollerEventType;
|
||||
|
||||
typedef struct {
|
||||
Iso14443_3bError error;
|
||||
} Iso14443_3bPollerEventData;
|
||||
|
||||
typedef struct {
|
||||
Iso14443_3bPollerEventType type;
|
||||
Iso14443_3bPollerEventData* data;
|
||||
} Iso14443_3bPollerEvent;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <nfc/protocols/nfc_poller_base.h>
|
||||
|
||||
extern const NfcPollerBase nfc_poller_iso14443_3b;
|
||||
@@ -0,0 +1,194 @@
|
||||
#include "iso14443_3b_poller_i.h"
|
||||
|
||||
#include <nfc/helpers/iso14443_crc.h>
|
||||
|
||||
#define TAG "Iso14443_3bPoller"
|
||||
|
||||
#define ISO14443_3B_ATTRIB_FRAME_SIZE_256 (0x08)
|
||||
|
||||
static Iso14443_3bError iso14443_3b_poller_process_error(NfcError error) {
|
||||
switch(error) {
|
||||
case NfcErrorNone:
|
||||
return Iso14443_3bErrorNone;
|
||||
case NfcErrorTimeout:
|
||||
return Iso14443_3bErrorTimeout;
|
||||
default:
|
||||
return Iso14443_3bErrorNotPresent;
|
||||
}
|
||||
}
|
||||
|
||||
static Iso14443_3bError iso14443_3b_poller_prepare_trx(Iso14443_3bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
if(instance->state == Iso14443_3bPollerStateIdle) {
|
||||
return iso14443_3b_poller_async_activate(instance, NULL);
|
||||
}
|
||||
|
||||
return Iso14443_3bErrorNone;
|
||||
}
|
||||
|
||||
static Iso14443_3bError iso14443_3b_poller_frame_exchange(
|
||||
Iso14443_3bPoller* instance,
|
||||
const BitBuffer* tx_buffer,
|
||||
BitBuffer* rx_buffer,
|
||||
uint32_t fwt) {
|
||||
furi_assert(instance);
|
||||
|
||||
const size_t tx_bytes = bit_buffer_get_size_bytes(tx_buffer);
|
||||
furi_assert(
|
||||
tx_bytes <= bit_buffer_get_capacity_bytes(instance->tx_buffer) - ISO14443_CRC_SIZE);
|
||||
|
||||
bit_buffer_copy(instance->tx_buffer, tx_buffer);
|
||||
iso14443_crc_append(Iso14443CrcTypeB, instance->tx_buffer);
|
||||
|
||||
Iso14443_3bError ret = Iso14443_3bErrorNone;
|
||||
|
||||
do {
|
||||
NfcError error =
|
||||
nfc_poller_trx(instance->nfc, instance->tx_buffer, instance->rx_buffer, fwt);
|
||||
if(error != NfcErrorNone) {
|
||||
ret = iso14443_3b_poller_process_error(error);
|
||||
break;
|
||||
}
|
||||
|
||||
bit_buffer_copy(rx_buffer, instance->rx_buffer);
|
||||
if(!iso14443_crc_check(Iso14443CrcTypeB, instance->rx_buffer)) {
|
||||
ret = Iso14443_3bErrorWrongCrc;
|
||||
break;
|
||||
}
|
||||
|
||||
iso14443_crc_trim(rx_buffer);
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Iso14443_3bError
|
||||
iso14443_3b_poller_async_activate(Iso14443_3bPoller* instance, Iso14443_3bData* data) {
|
||||
furi_assert(instance);
|
||||
furi_assert(instance->nfc);
|
||||
|
||||
iso14443_3b_reset(data);
|
||||
|
||||
Iso14443_3bError ret;
|
||||
|
||||
do {
|
||||
instance->state = Iso14443_3bPollerStateColResInProgress;
|
||||
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
bit_buffer_reset(instance->rx_buffer);
|
||||
|
||||
// Send REQB
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x05);
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x00);
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x08);
|
||||
|
||||
ret = iso14443_3b_poller_frame_exchange(
|
||||
instance, instance->tx_buffer, instance->rx_buffer, ISO14443_3B_FDT_POLL_FC);
|
||||
if(ret != Iso14443_3bErrorNone) {
|
||||
instance->state = Iso14443_3bPollerStateColResFailed;
|
||||
break;
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
uint8_t flag;
|
||||
uint8_t uid[ISO14443_3B_UID_SIZE];
|
||||
uint8_t app_data[ISO14443_3B_APP_DATA_SIZE];
|
||||
Iso14443_3bProtocolInfo protocol_info;
|
||||
} Iso14443_3bAtqBLayout;
|
||||
|
||||
if(bit_buffer_get_size_bytes(instance->rx_buffer) != sizeof(Iso14443_3bAtqBLayout)) {
|
||||
FURI_LOG_D(TAG, "Unexpected REQB response");
|
||||
instance->state = Iso14443_3bPollerStateColResFailed;
|
||||
ret = Iso14443_3bErrorCommunication;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->state = Iso14443_3bPollerStateActivationInProgress;
|
||||
|
||||
const Iso14443_3bAtqBLayout* atqb =
|
||||
(const Iso14443_3bAtqBLayout*)bit_buffer_get_data(instance->rx_buffer);
|
||||
|
||||
memcpy(data->uid, atqb->uid, ISO14443_3B_UID_SIZE);
|
||||
memcpy(data->app_data, atqb->app_data, ISO14443_3B_APP_DATA_SIZE);
|
||||
|
||||
data->protocol_info = atqb->protocol_info;
|
||||
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
bit_buffer_reset(instance->rx_buffer);
|
||||
|
||||
// Send ATTRIB
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x1d);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, data->uid, ISO14443_3B_UID_SIZE);
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x00);
|
||||
bit_buffer_append_byte(instance->tx_buffer, ISO14443_3B_ATTRIB_FRAME_SIZE_256);
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x01);
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x00);
|
||||
|
||||
ret = iso14443_3b_poller_frame_exchange(
|
||||
instance, instance->tx_buffer, instance->rx_buffer, iso14443_3b_get_fwt_fc_max(data));
|
||||
if(ret != Iso14443_3bErrorNone) {
|
||||
instance->state = Iso14443_3bPollerStateActivationFailed;
|
||||
break;
|
||||
}
|
||||
|
||||
if(bit_buffer_get_size_bytes(instance->rx_buffer) != 1 ||
|
||||
bit_buffer_get_byte(instance->rx_buffer, 0) != 0) {
|
||||
FURI_LOG_D(TAG, "Unexpected ATTRIB response");
|
||||
instance->state = Iso14443_3bPollerStateActivationFailed;
|
||||
ret = Iso14443_3bErrorCommunication;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->state = Iso14443_3bPollerStateActivated;
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Iso14443_3bError iso14443_3b_poller_halt(Iso14443_3bPoller* instance) {
|
||||
furi_assert(instance);
|
||||
|
||||
bit_buffer_reset(instance->tx_buffer);
|
||||
bit_buffer_reset(instance->rx_buffer);
|
||||
|
||||
bit_buffer_append_byte(instance->tx_buffer, 0x50);
|
||||
bit_buffer_append_bytes(instance->tx_buffer, instance->data->uid, ISO14443_3B_UID_SIZE);
|
||||
|
||||
Iso14443_3bError ret;
|
||||
|
||||
do {
|
||||
ret = iso14443_3b_poller_frame_exchange(
|
||||
instance, instance->tx_buffer, instance->rx_buffer, ISO14443_3B_FDT_POLL_FC);
|
||||
if(ret != Iso14443_3bErrorNone) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(bit_buffer_get_size_bytes(instance->rx_buffer) != sizeof(uint8_t) ||
|
||||
bit_buffer_get_byte(instance->rx_buffer, 0) != 0) {
|
||||
ret = Iso14443_3bErrorCommunication;
|
||||
break;
|
||||
}
|
||||
|
||||
instance->state = Iso14443_3bPollerStateIdle;
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
Iso14443_3bError iso14443_3b_poller_send_frame(
|
||||
Iso14443_3bPoller* instance,
|
||||
const BitBuffer* tx_buffer,
|
||||
BitBuffer* rx_buffer) {
|
||||
Iso14443_3bError ret;
|
||||
|
||||
do {
|
||||
ret = iso14443_3b_poller_prepare_trx(instance);
|
||||
if(ret != Iso14443_3bErrorNone) break;
|
||||
|
||||
ret = iso14443_3b_poller_frame_exchange(
|
||||
instance, tx_buffer, rx_buffer, iso14443_3b_get_fwt_fc_max(instance->data));
|
||||
} while(false);
|
||||
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include "iso14443_3b_poller.h"
|
||||
#include "iso14443_3b_i.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ISO14443_3B_POLLER_MAX_BUFFER_SIZE (256U)
|
||||
|
||||
typedef enum {
|
||||
Iso14443_3bPollerStateIdle,
|
||||
Iso14443_3bPollerStateColResInProgress,
|
||||
Iso14443_3bPollerStateColResFailed,
|
||||
Iso14443_3bPollerStateActivationInProgress,
|
||||
Iso14443_3bPollerStateActivationFailed,
|
||||
Iso14443_3bPollerStateActivated,
|
||||
} Iso14443_3bPollerState;
|
||||
|
||||
struct Iso14443_3bPoller {
|
||||
Nfc* nfc;
|
||||
Iso14443_3bPollerState state;
|
||||
Iso14443_3bData* data;
|
||||
BitBuffer* tx_buffer;
|
||||
BitBuffer* rx_buffer;
|
||||
|
||||
NfcGenericEvent general_event;
|
||||
Iso14443_3bPollerEvent iso14443_3b_event;
|
||||
Iso14443_3bPollerEventData iso14443_3b_event_data;
|
||||
NfcGenericCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
const Iso14443_3bData* iso14443_3b_poller_get_data(Iso14443_3bPoller* instance);
|
||||
|
||||
Iso14443_3bError
|
||||
iso14443_3b_poller_async_activate(Iso14443_3bPoller* instance, Iso14443_3bData* data);
|
||||
|
||||
Iso14443_3bError iso14443_3b_poller_halt(Iso14443_3bPoller* instance);
|
||||
|
||||
Iso14443_3bError iso14443_3b_poller_send_frame(
|
||||
Iso14443_3bPoller* instance,
|
||||
const BitBuffer* tx_buffer,
|
||||
BitBuffer* rx_buffer);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user