Skip to content

Commit

Permalink
[FL-3640] NFC: Felica UID emulation (#3190)
Browse files Browse the repository at this point in the history
* Added basic template of Felica listener
* Raw nfc felica listener functions
* Added functions to setup chip for felica listener
* Cleanup function templates from unnecessary parts
* Removed todo comment
* Updated api versions
* Adjusted chip config for felica
* Set proper chip passive target mode for felica
* Added felica function to unit tests
* Update furi_hal_nfc_felica.c
* Removed duplication

Co-authored-by: gornekich <[email protected]>
  • Loading branch information
RebornedBrain and gornekich authored Nov 26, 2023
1 parent 4b3e8ab commit 1c3cbec
Show file tree
Hide file tree
Showing 13 changed files with 346 additions and 4 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,
},
};
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;
};
2 changes: 2 additions & 0 deletions lib/nfc/protocols/nfc_listener_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nfc/protocols/mf_ultralight/mf_ultralight_listener_defs.h>
#include <nfc/protocols/mf_classic/mf_classic_listener_defs.h>
#include <nfc/protocols/slix/slix_listener_defs.h>
#include <nfc/protocols/felica/felica_listener_defs.h>

const NfcListenerBase* nfc_listeners_api[NfcProtocolNum] = {
[NfcProtocolIso14443_3a] = &nfc_listener_iso14443_3a,
Expand All @@ -18,4 +19,5 @@ const NfcListenerBase* nfc_listeners_api[NfcProtocolNum] = {
[NfcProtocolMfDesfire] = NULL,
[NfcProtocolSlix] = &nfc_listener_slix,
[NfcProtocolSt25tb] = NULL,
[NfcProtocolFelica] = &nfc_listener_felica,
};
2 changes: 2 additions & 0 deletions targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
Expand Up @@ -1287,6 +1287,7 @@ Function,+,furi_hal_nfc_abort,FuriHalNfcError,
Function,+,furi_hal_nfc_acquire,FuriHalNfcError,
Function,+,furi_hal_nfc_event_start,FuriHalNfcError,
Function,+,furi_hal_nfc_event_stop,FuriHalNfcError,
Function,+,furi_hal_nfc_felica_listener_set_sensf_res_data,FuriHalNfcError,"const uint8_t*, const uint8_t, const uint8_t*, const uint8_t"
Function,+,furi_hal_nfc_field_detect_start,FuriHalNfcError,
Function,+,furi_hal_nfc_field_detect_stop,FuriHalNfcError,
Function,+,furi_hal_nfc_field_is_present,_Bool,
Expand Down Expand Up @@ -2316,6 +2317,7 @@ Function,+,nfc_dict_get_next_key,_Bool,"NfcDict*, uint8_t*, size_t"
Function,+,nfc_dict_get_total_keys,uint32_t,NfcDict*
Function,+,nfc_dict_is_key_present,_Bool,"NfcDict*, const uint8_t*, size_t"
Function,+,nfc_dict_rewind,_Bool,NfcDict*
Function,+,nfc_felica_listener_set_sensf_res_data,NfcError,"Nfc*, const uint8_t*, const uint8_t, const uint8_t*, const uint8_t"
Function,+,nfc_free,void,Nfc*
Function,+,nfc_iso14443a_listener_set_col_res_data,NfcError,"Nfc*, uint8_t*, uint8_t, uint8_t*, uint8_t"
Function,+,nfc_iso14443a_listener_tx_custom_parity,NfcError,"Nfc*, const BitBuffer*"
Expand Down
Loading

0 comments on commit 1c3cbec

Please sign in to comment.