Skip to content

Commit

Permalink
Merge branch 'ofw_dev' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xMasterX committed Nov 26, 2023
2 parents cea4237 + f9101d8 commit a7b2427
Show file tree
Hide file tree
Showing 18 changed files with 406 additions and 26 deletions.
15 changes: 15 additions & 0 deletions applications/debug/unit_tests/nfc/nfc_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,19 @@ NfcError nfc_iso15693_listener_tx_sof(Nfc* instance) {
return NfcErrorNone;
}

NfcError nfc_felica_listener_set_sensf_res_data(
Nfc* instance,
const uint8_t* idm,
const uint8_t idm_len,
const uint8_t* pmm,
const uint8_t pmm_len) {
furi_assert(instance);
furi_assert(idm);
furi_assert(pmm);
furi_assert(idm_len == 8);
furi_assert(pmm_len == 8);

return NfcErrorNone;
}

#endif
10 changes: 8 additions & 2 deletions applications/main/nfc/helpers/protocol_support/felica/felica.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,14 @@ static bool nfc_scene_saved_menu_on_event_felica(NfcApp* instance, uint32_t even
return false;
}

static void nfc_scene_emulate_on_enter_felica(NfcApp* instance) {
const FelicaData* data = nfc_device_get_data(instance->nfc_device, NfcProtocolFelica);
instance->listener = nfc_listener_alloc(instance->nfc, NfcProtocolFelica, data);
nfc_listener_start(instance->listener, NULL, NULL);
}

const NfcProtocolSupportBase nfc_protocol_support_felica = {
.features = NfcProtocolFeatureNone,
.features = NfcProtocolFeatureEmulateUid,

.scene_info =
{
Expand Down Expand Up @@ -102,7 +108,7 @@ const NfcProtocolSupportBase nfc_protocol_support_felica = {
},
.scene_emulate =
{
.on_enter = nfc_protocol_support_common_on_enter_empty,
.on_enter = nfc_scene_emulate_on_enter_felica,
.on_event = nfc_protocol_support_common_on_event_empty,
},
};
41 changes: 27 additions & 14 deletions lib/nfc/helpers/nfc_data_generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,23 @@ static void nfc_generate_mf_classic_uid(uint8_t* uid, uint8_t length) {
static void
nfc_generate_mf_classic_common(MfClassicData* data, uint8_t uid_len, MfClassicType type) {
data->iso14443_3a_data->uid_len = uid_len;
data->iso14443_3a_data->atqa[0] = 0x44;
data->iso14443_3a_data->atqa[0] = 0x00;
data->iso14443_3a_data->atqa[1] = 0x00;
data->iso14443_3a_data->sak = 0x08;
data->iso14443_3a_data->sak = 0x00;
// Calculate the proper ATQA and SAK
if(uid_len == 7) {
data->iso14443_3a_data->atqa[0] |= 0x40;
}
if(type == MfClassicType1k) {
data->iso14443_3a_data->atqa[0] |= 0x04;
data->iso14443_3a_data->sak = 0x08;
} else if(type == MfClassicType4k) {
data->iso14443_3a_data->atqa[0] |= 0x02;
data->iso14443_3a_data->sak = 0x18;
} else if(type == MfClassicTypeMini) {
data->iso14443_3a_data->atqa[0] |= 0x08;
data->iso14443_3a_data->sak = 0x09;
}
data->type = type;
}

Expand All @@ -343,6 +357,11 @@ static void nfc_generate_mf_classic_sector_trailer(MfClassicData* data, uint8_t
sec_tr->access_bits.data[2] = 0x80;
sec_tr->access_bits.data[3] = 0x69; // Nice

for(int i = 0; i < 6; i++) {
sec_tr->key_a.data[i] = 0xFF;
sec_tr->key_b.data[i] = 0xFF;
}

mf_classic_set_block_read(data, block, &data->block[block]);
mf_classic_set_key_found(
data, mf_classic_get_sector_by_block(block), MfClassicKeyTypeA, 0xFFFFFFFFFFFF);
Expand Down Expand Up @@ -396,41 +415,35 @@ static void nfc_generate_mf_classic(NfcDevice* nfc_device, uint8_t uid_len, MfCl

uint16_t block_num = mf_classic_get_total_block_num(type);
if(type == MfClassicType4k) {
// Set every block to 0xFF
// Set every block to 0x00
for(uint16_t i = 1; i < block_num; i++) {
if(mf_classic_is_sector_trailer(i)) {
nfc_generate_mf_classic_sector_trailer(mfc_data, i);
} else {
memset(&mfc_data->block[i].data, 0xFF, 16);
memset(&mfc_data->block[i].data, 0x00, 16);
}
mf_classic_set_block_read(mfc_data, i, &mfc_data->block[i]);
}
// Set SAK to 18
mfc_data->iso14443_3a_data->sak = 0x18;
} else if(type == MfClassicType1k) {
// Set every block to 0xFF
// Set every block to 0x00
for(uint16_t i = 1; i < block_num; i++) {
if(mf_classic_is_sector_trailer(i)) {
nfc_generate_mf_classic_sector_trailer(mfc_data, i);
} else {
memset(&mfc_data->block[i].data, 0xFF, 16);
memset(&mfc_data->block[i].data, 0x00, 16);
}
mf_classic_set_block_read(mfc_data, i, &mfc_data->block[i]);
}
// Set SAK to 08
mfc_data->iso14443_3a_data->sak = 0x08;
} else if(type == MfClassicTypeMini) {
// Set every block to 0xFF
// Set every block to 0x00
for(uint16_t i = 1; i < block_num; i++) {
if(mf_classic_is_sector_trailer(i)) {
nfc_generate_mf_classic_sector_trailer(mfc_data, i);
} else {
memset(&mfc_data->block[i].data, 0xFF, 16);
memset(&mfc_data->block[i].data, 0x00, 16);
}
mf_classic_set_block_read(mfc_data, i, &mfc_data->block[i]);
}
// Set SAK to 09
mfc_data->iso14443_3a_data->sak = 0x09;
}

nfc_generate_mf_classic_block_0(
Expand Down
18 changes: 17 additions & 1 deletion lib/nfc/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static const FuriHalNfcTech nfc_tech_table[NfcModeNum][NfcTechNum] = {
[NfcTechIso14443a] = FuriHalNfcTechIso14443a,
[NfcTechIso14443b] = FuriHalNfcTechInvalid,
[NfcTechIso15693] = FuriHalNfcTechIso15693,
[NfcTechFelica] = FuriHalNfcTechInvalid,
[NfcTechFelica] = FuriHalNfcTechFelica,
},
};

Expand Down Expand Up @@ -646,4 +646,20 @@ NfcError nfc_iso15693_listener_tx_sof(Nfc* instance) {
return ret;
}

NfcError nfc_felica_listener_set_sensf_res_data(
Nfc* instance,
const uint8_t* idm,
const uint8_t idm_len,
const uint8_t* pmm,
const uint8_t pmm_len) {
furi_assert(instance);
furi_assert(idm);
furi_assert(pmm);

FuriHalNfcError error =
furi_hal_nfc_felica_listener_set_sensf_res_data(idm, idm_len, pmm, pmm_len);
instance->comm_state = NfcCommStateIdle;
return nfc_process_hal_error(error);
}

#endif // APP_UNIT_TESTS
19 changes: 19 additions & 0 deletions lib/nfc/nfc.h
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,25 @@ NfcError nfc_iso14443a_listener_set_col_res_data(
uint8_t* atqa,
uint8_t sak);

/**
* @brief Set FeliCa collision resolution parameters in listener mode.
*
* Configures the NFC hardware for automatic collision resolution.
*
* @param[in,out] instance pointer to the instance to be configured.
* @param[in] idm pointer to a byte array containing the IDm.
* @param[in] idm_len IDm length in bytes.
* @param[in] pmm pointer to a byte array containing the PMm.
* @param[in] pmm_len PMm length in bytes.
* @returns NfcErrorNone on success, any other error code on failure.
*/
NfcError nfc_felica_listener_set_sensf_res_data(
Nfc* instance,
const uint8_t* idm,
const uint8_t idm_len,
const uint8_t* pmm,
const uint8_t pmm_len);

/**
* @brief Send ISO15693 Start of Frame pattern in listener mode
*
Expand Down
2 changes: 2 additions & 0 deletions lib/nfc/protocols/felica/felica.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern "C" {
#define FELICA_FDT_POLL_FC (10000U)
#define FELICA_POLL_POLL_MIN_US (1280U)

#define FELICA_FDT_LISTEN_FC (1172)

#define FELICA_SYSTEM_CODE_CODE (0xFFFFU)
#define FELICA_TIME_SLOT_1 (0x00U)
#define FELICA_TIME_SLOT_2 (0x01U)
Expand Down
79 changes: 79 additions & 0 deletions lib/nfc/protocols/felica/felica_listener.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "felica_listener_i.h"

#include "nfc/protocols/nfc_listener_base.h"

#define FELICA_LISTENER_MAX_BUFFER_SIZE (64)
#define TAG "Felica"

FelicaListener* felica_listener_alloc(Nfc* nfc, FelicaData* data) {
furi_assert(nfc);
furi_assert(data);

FelicaListener* instance = malloc(sizeof(FelicaListener));
instance->nfc = nfc;
instance->data = data;
instance->tx_buffer = bit_buffer_alloc(FELICA_LISTENER_MAX_BUFFER_SIZE);
instance->rx_buffer = bit_buffer_alloc(FELICA_LISTENER_MAX_BUFFER_SIZE);

nfc_set_fdt_listen_fc(instance->nfc, FELICA_FDT_LISTEN_FC);

nfc_config(instance->nfc, NfcModeListener, NfcTechFelica);
nfc_felica_listener_set_sensf_res_data(
nfc, data->idm.data, sizeof(data->idm), data->pmm.data, sizeof(data->pmm));

return instance;
}

void felica_listener_free(FelicaListener* instance) {
furi_assert(instance);
furi_assert(instance->tx_buffer);

bit_buffer_free(instance->tx_buffer);
bit_buffer_free(instance->rx_buffer);
free(instance);
}

void felica_listener_set_callback(
FelicaListener* listener,
NfcGenericCallback callback,
void* context) {
UNUSED(listener);
UNUSED(callback);
UNUSED(context);
}

const FelicaData* felica_listener_get_data(const FelicaListener* instance) {
furi_assert(instance);
furi_assert(instance->data);

return instance->data;
}

NfcCommand felica_listener_run(NfcGenericEvent event, void* context) {
furi_assert(context);
furi_assert(event.protocol == NfcProtocolInvalid);
furi_assert(event.event_data);

FelicaListener* instance = context;
NfcEvent* nfc_event = event.event_data;
NfcCommand command = NfcCommandContinue;

if(nfc_event->type == NfcEventTypeListenerActivated) {
instance->state = Felica_ListenerStateActivated;
FURI_LOG_D(TAG, "Activated");
} else if(nfc_event->type == NfcEventTypeFieldOff) {
instance->state = Felica_ListenerStateIdle;
FURI_LOG_D(TAG, "Field Off");
} else if(nfc_event->type == NfcEventTypeRxEnd) {
FURI_LOG_D(TAG, "Rx Done");
}
return command;
}

const NfcListenerBase nfc_listener_felica = {
.alloc = (NfcListenerAlloc)felica_listener_alloc,
.free = (NfcListenerFree)felica_listener_free,
.set_callback = (NfcListenerSetCallback)felica_listener_set_callback,
.get_data = (NfcListenerGetData)felica_listener_get_data,
.run = (NfcListenerRun)felica_listener_run,
};
14 changes: 14 additions & 0 deletions lib/nfc/protocols/felica/felica_listener.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include "felica.h"
#include <lib/nfc/nfc.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct FelicaListener FelicaListener;

#ifdef __cplusplus
}
#endif
13 changes: 13 additions & 0 deletions lib/nfc/protocols/felica/felica_listener_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <nfc/protocols/nfc_listener_base.h>

#ifdef __cplusplus
extern "C" {
#endif

extern const NfcListenerBase nfc_listener_felica;

#ifdef __cplusplus
}
#endif
21 changes: 21 additions & 0 deletions lib/nfc/protocols/felica/felica_listener_i.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "felica_listener.h"

#include <nfc/protocols/nfc_generic_event.h>

typedef enum {
Felica_ListenerStateIdle,
Felica_ListenerStateActivated,
} FelicaListenerState;

struct FelicaListener {
Nfc* nfc;
FelicaData* data;
FelicaListenerState state;

BitBuffer* tx_buffer;
BitBuffer* rx_buffer;

NfcGenericEvent generic_event;
NfcGenericCallback callback;
void* context;
};
13 changes: 7 additions & 6 deletions lib/nfc/protocols/mf_classic/mf_classic.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ static bool mf_classic_is_allowed_access_sector_trailer(
uint8_t* access_bits_arr = sec_tr->access_bits.data;
uint8_t AC = ((access_bits_arr[1] >> 5) & 0x04) | ((access_bits_arr[2] >> 2) & 0x02) |
((access_bits_arr[2] >> 7) & 0x01);
FURI_LOG_T("NFC", "AC: %02X", AC);

switch(action) {
case MfClassicActionKeyARead: {
Expand All @@ -615,20 +616,20 @@ static bool mf_classic_is_allowed_access_sector_trailer(
case MfClassicActionKeyBWrite: {
return (
(key_type == MfClassicKeyTypeA && (AC == 0x00 || AC == 0x01)) ||
(key_type == MfClassicKeyTypeB && (AC == 0x04 || AC == 0x03)));
(key_type == MfClassicKeyTypeB &&
(AC == 0x00 || AC == 0x04 || AC == 0x03 || AC == 0x01)));
}
case MfClassicActionKeyBRead: {
return (key_type == MfClassicKeyTypeA && (AC == 0x00 || AC == 0x02 || AC == 0x01));
return (key_type == MfClassicKeyTypeA && (AC == 0x00 || AC == 0x02 || AC == 0x01)) ||
(key_type == MfClassicKeyTypeB && (AC == 0x00 || AC == 0x02 || AC == 0x01));
}
case MfClassicActionACRead: {
return (
(key_type == MfClassicKeyTypeA) ||
(key_type == MfClassicKeyTypeB && !(AC == 0x00 || AC == 0x02 || AC == 0x01)));
return ((key_type == MfClassicKeyTypeA) || (key_type == MfClassicKeyTypeB));
}
case MfClassicActionACWrite: {
return (
(key_type == MfClassicKeyTypeA && (AC == 0x01)) ||
(key_type == MfClassicKeyTypeB && (AC == 0x03 || AC == 0x05)));
(key_type == MfClassicKeyTypeB && (AC == 0x01 || AC == 0x03 || AC == 0x05)));
}
default:
return false;
Expand Down
2 changes: 2 additions & 0 deletions lib/nfc/protocols/mf_classic/mf_classic.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ extern "C" {
#define MF_CLASSIC_CMD_HALT_LSB (0x00)
#define MF_CLASSIC_CMD_ACK (0x0A)
#define MF_CLASSIC_CMD_NACK (0x00)
#define MF_CLASSIC_CMD_NACK_TRANSFER_INVALID (0x04)
#define MF_CLASSIC_CMD_NACK_TRANSFER_CRC_ERROR (0x01)

#define MF_CLASSIC_TOTAL_SECTORS_MAX (40)
#define MF_CLASSIC_TOTAL_BLOCKS_MAX (256)
Expand Down
Loading

0 comments on commit a7b2427

Please sign in to comment.