Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ST25TB poller refining + write support #3239

Merged
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ static NfcCommand nfc_scene_read_poller_callback_st25tb(NfcGenericEvent event, v
NfcApp* instance = context;
const St25tbPollerEvent* st25tb_event = event.event_data;

if(st25tb_event->type == St25tbPollerEventTypeReady) {
if(st25tb_event->type == St25tbPollerEventTypeRequestMode) {
st25tb_event->data->mode_request.mode = St25tbPollerModeRead;
} else if(st25tb_event->type == St25tbPollerEventTypeSuccess) {
nfc_device_set_data(
instance->nfc_device, NfcProtocolSt25tb, nfc_poller_get_data(instance->poller));
view_dispatcher_send_custom_event(instance->view_dispatcher, NfcCustomEventPollerSuccess);
return NfcCommandStop;
} else if(st25tb_event->type == St25tbPollerEventTypeFailure) {
view_dispatcher_send_custom_event(instance->view_dispatcher, NfcCustomEventPollerFailure);
return NfcCommandStop;
}

return NfcCommandContinue;
Expand Down
1 change: 1 addition & 0 deletions lib/nfc/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ env.Append(
File("protocols/iso14443_3a/iso14443_3a_poller_sync.h"),
File("protocols/mf_ultralight/mf_ultralight_poller_sync.h"),
File("protocols/mf_classic/mf_classic_poller_sync.h"),
File("protocols/st25tb/st25tb_poller_sync.h"),
# Misc
File("helpers/nfc_util.h"),
File("helpers/iso14443_crc.h"),
Expand Down
20 changes: 20 additions & 0 deletions lib/nfc/protocols/st25tb/st25tb.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,23 @@ St25tbData* st25tb_get_base_data(const St25tbData* data) {
UNUSED(data);
furi_crash("No base data");
}

St25tbType st25tb_get_type_from_uid(const uint8_t* uid) {
switch(uid[2] >> 2) {
case 0x0:
case 0x3:
return St25tbTypeX4k;
case 0x4:
return St25tbTypeX512;
case 0x6:
return St25tbType512Ac;
case 0x7:
return St25tbType04k;
case 0xc:
return St25tbType512At;
case 0xf:
return St25tbType02k;
default:
furi_crash("unsupported st25tb type");
}
}
5 changes: 3 additions & 2 deletions lib/nfc/protocols/st25tb/st25tb.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <toolbox/bit_buffer.h>
#include <nfc/protocols/nfc_device_base_i.h>

#ifdef __cplusplus
Expand All @@ -27,6 +26,7 @@ typedef enum {
St25tbErrorFieldOff,
St25tbErrorWrongCrc,
St25tbErrorTimeout,
St25tbErrorWriteFailed,
} St25tbError;

typedef enum {
Expand All @@ -44,7 +44,6 @@ typedef struct {
St25tbType type;
uint32_t blocks[ST25TB_MAX_BLOCKS];
uint32_t system_otp_block;
uint8_t chip_id;
} St25tbData;

extern const NfcDeviceBase nfc_device_st25tb;
Expand Down Expand Up @@ -75,6 +74,8 @@ bool st25tb_set_uid(St25tbData* data, const uint8_t* uid, size_t uid_len);

St25tbData* st25tb_get_base_data(const St25tbData* data);

St25tbType st25tb_get_type_from_uid(const uint8_t* uid);

#ifdef __cplusplus
}
#endif
153 changes: 129 additions & 24 deletions lib/nfc/protocols/st25tb/st25tb_poller.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
#include "protocols/nfc_protocol.h"
#include "protocols/st25tb/st25tb.h"
#include "st25tb_poller.h"
#include "st25tb_poller_i.h"

#include <nfc/protocols/nfc_poller_base.h>

#include <furi.h>

#define TAG "ST25TBPoller"

typedef NfcCommand (*St25tbPollerStateHandler)(St25tbPoller* instance);

const St25tbData* st25tb_poller_get_data(St25tbPoller* instance) {
furi_assert(instance);
furi_assert(instance->data);
Expand All @@ -20,6 +19,7 @@ static St25tbPoller* st25tb_poller_alloc(Nfc* nfc) {

St25tbPoller* instance = malloc(sizeof(St25tbPoller));
instance->nfc = nfc;
instance->state = St25tbPollerStateSelect;
instance->tx_buffer = bit_buffer_alloc(ST25TB_POLLER_MAX_BUFFER_SIZE);
instance->rx_buffer = bit_buffer_alloc(ST25TB_POLLER_MAX_BUFFER_SIZE);

Expand Down Expand Up @@ -60,6 +60,127 @@ static void
instance->context = context;
}

static NfcCommand st25tb_poller_select_handler(St25tbPoller* instance) {
NfcCommand command = NfcCommandContinue;

do {
St25tbError error = st25tb_poller_select(instance, NULL);
if(error != St25tbErrorNone) {
instance->state = St25tbPollerStateFailure;
instance->st25tb_event_data.error = error;
break;
}

instance->st25tb_event.type = St25tbPollerEventTypeReady;
command = instance->callback(instance->general_event, instance->context);
instance->state = St25tbPollerStateRequestMode;
} while(false);

return command;
}

static NfcCommand st25tb_poller_request_mode_handler(St25tbPoller* instance) {
NfcCommand command = NfcCommandContinue;
instance->st25tb_event.type = St25tbPollerEventTypeRequestMode;
command = instance->callback(instance->general_event, instance->context);

St25tbPollerEventDataModeRequest* mode_request_data =
&instance->st25tb_event_data.mode_request;

furi_assert(mode_request_data->mode < St25tbPollerModeNum);

if(mode_request_data->mode == St25tbPollerModeRead) {
instance->state = St25tbPollerStateRead;
instance->poller_ctx.read.current_block = 0;
} else {
instance->state = St25tbPollerStateWrite;
instance->poller_ctx.write.block_number =
mode_request_data->params.write_params.block_number;
instance->poller_ctx.write.block_data = mode_request_data->params.write_params.block_data;
}

return command;
}

static NfcCommand st25tb_poller_read_handler(St25tbPoller* instance) {
St25tbError error = St25tbErrorNone;

do {
uint8_t total_blocks = st25tb_get_block_count(instance->data->type);
uint8_t* current_block = &instance->poller_ctx.read.current_block;
if(*current_block == total_blocks) {
error = st25tb_poller_read_block(
instance, &instance->data->system_otp_block, ST25TB_SYSTEM_OTP_BLOCK);
if(error != St25tbErrorNone) {
FURI_LOG_E(TAG, "Failed to read OTP block");
instance->state = St25tbPollerStateFailure;
instance->st25tb_event_data.error = error;
break;
} else {
instance->state = St25tbPollerStateSuccess;
break;
}
} else {
error = st25tb_poller_read_block(
instance, &instance->data->blocks[*current_block], *current_block);
if(error != St25tbErrorNone) {
FURI_LOG_E(TAG, "Failed to read block %d", *current_block);
instance->state = St25tbPollerStateFailure;
instance->st25tb_event_data.error = error;
break;
}

*current_block += 1;
}
} while(false);

return NfcCommandContinue;
}

static NfcCommand st25tb_poller_write_handler(St25tbPoller* instance) {
St25tbPollerWriteContext* write_ctx = &instance->poller_ctx.write;
St25tbError error =
st25tb_poller_write_block(instance, write_ctx->block_data, write_ctx->block_number);

if(error == St25tbErrorNone) {
instance->state = St25tbPollerStateSuccess;
} else {
instance->state = St25tbPollerStateFailure;
instance->st25tb_event_data.error = error;
}

return NfcCommandContinue;
}

NfcCommand st25tb_poller_success_handler(St25tbPoller* instance) {
NfcCommand command = NfcCommandContinue;
instance->st25tb_event.type = St25tbPollerEventTypeSuccess;
command = instance->callback(instance->general_event, instance->context);
furi_delay_ms(100);
instance->state = St25tbPollerStateSelect;

return command;
}

NfcCommand st25tb_poller_failure_handler(St25tbPoller* instance) {
NfcCommand command = NfcCommandContinue;
instance->st25tb_event.type = St25tbPollerEventTypeFailure;
command = instance->callback(instance->general_event, instance->context);
furi_delay_ms(100);
instance->state = St25tbPollerStateSelect;

return command;
}

static St25tbPollerStateHandler st25tb_poller_state_handlers[St25tbPollerStateNum] = {
[St25tbPollerStateSelect] = st25tb_poller_select_handler,
[St25tbPollerStateRequestMode] = st25tb_poller_request_mode_handler,
[St25tbPollerStateRead] = st25tb_poller_read_handler,
[St25tbPollerStateWrite] = st25tb_poller_write_handler,
[St25tbPollerStateSuccess] = st25tb_poller_success_handler,
[St25tbPollerStateFailure] = st25tb_poller_failure_handler,
};

static NfcCommand st25tb_poller_run(NfcGenericEvent event, void* context) {
furi_assert(context);
furi_assert(event.protocol == NfcProtocolInvalid);
Expand All @@ -69,26 +190,10 @@ static NfcCommand st25tb_poller_run(NfcGenericEvent event, void* context) {
NfcEvent* nfc_event = event.event_data;
NfcCommand command = NfcCommandContinue;

if(nfc_event->type == NfcEventTypePollerReady) {
if(instance->state != St25tbPollerStateActivated) {
St25tbError error = st25tb_poller_activate(instance, instance->data);
furi_assert(instance->state < St25tbPollerStateNum);

if(error == St25tbErrorNone) {
instance->st25tb_event.type = St25tbPollerEventTypeReady;
instance->st25tb_event_data.error = error;
command = instance->callback(instance->general_event, instance->context);
} else {
instance->st25tb_event.type = St25tbPollerEventTypeError;
instance->st25tb_event_data.error = error;
command = instance->callback(instance->general_event, instance->context);
// Add delay to switch context
furi_delay_ms(100);
}
} else {
instance->st25tb_event.type = St25tbPollerEventTypeReady;
instance->st25tb_event_data.error = St25tbErrorNone;
command = instance->callback(instance->general_event, instance->context);
}
if(nfc_event->type == NfcEventTypePollerReady) {
command = st25tb_poller_state_handlers[instance->state](instance);
}

return command;
Expand All @@ -103,7 +208,7 @@ static bool st25tb_poller_detect(NfcGenericEvent event, void* context) {
bool protocol_detected = false;
St25tbPoller* instance = context;
NfcEvent* nfc_event = event.event_data;
furi_assert(instance->state == St25tbPollerStateIdle);
furi_assert(instance->state == St25tbPollerStateSelect);

if(nfc_event->type == NfcEventTypePollerReady) {
St25tbError error = st25tb_poller_initiate(instance, NULL);
Expand Down
40 changes: 35 additions & 5 deletions lib/nfc/protocols/st25tb/st25tb_poller.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,47 @@
#include "st25tb.h"
#include <lib/nfc/nfc.h>

#include <nfc/nfc_poller.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct St25tbPoller St25tbPoller;

typedef enum {
St25tbPollerEventTypeError,
St25tbPollerEventTypeReady,
St25tbPollerEventTypeRequestMode,
St25tbPollerEventTypeFailure,
St25tbPollerEventTypeSuccess,
} St25tbPollerEventType;

typedef struct {
St25tbType type;
} St25tbPollerReadyData;

typedef enum {
St25tbPollerModeRead,
St25tbPollerModeWrite,

St25tbPollerModeNum,
} St25tbPollerMode;

typedef struct {
uint8_t block_number;
uint32_t block_data;
} St25tbPollerEventDataModeRequestWriteParams;

typedef union {
St25tbPollerEventDataModeRequestWriteParams write_params;
} St25tbPollerEventDataModeRequestParams;

typedef struct {
St25tbPollerMode mode;
St25tbPollerEventDataModeRequestParams params;
} St25tbPollerEventDataModeRequest;

typedef union {
St25tbPollerReadyData ready;
St25tbPollerEventDataModeRequest mode_request;
St25tbError error;
} St25tbPollerEventData;

Expand All @@ -31,15 +58,18 @@ St25tbError st25tb_poller_send_frame(
BitBuffer* rx_buffer,
uint32_t fwt);

St25tbError st25tb_poller_initiate(St25tbPoller* instance, uint8_t* chip_id);
St25tbError st25tb_poller_initiate(St25tbPoller* instance, uint8_t* chip_id_ptr);

St25tbError st25tb_poller_activate(St25tbPoller* instance, St25tbData* data);
St25tbError st25tb_poller_select(St25tbPoller* instance, uint8_t* chip_id_ptr);

St25tbError st25tb_poller_get_uid(St25tbPoller* instance, uint8_t* uid);

St25tbError
st25tb_poller_read_block(St25tbPoller* instance, uint32_t* block, uint8_t block_number);

St25tbError
st25tb_poller_write_block(St25tbPoller* instance, uint32_t block, uint8_t block_number);

St25tbError st25tb_poller_halt(St25tbPoller* instance);

#ifdef __cplusplus
Expand Down
Loading