Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[FL-2369] NFC refactoring #1095

Merged
merged 33 commits into from
Apr 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4f8a2ff
nfc: clean up scenes
gornekich Mar 29, 2022
e4e3a6b
nfc worker: remove field on from worker
gornekich Mar 29, 2022
9c9e64d
nfc worker: move full data exchange to furi hal
gornekich Mar 29, 2022
4bc23cb
nfc_device: check UID length
gornekich Apr 1, 2022
f6c5a10
nfc protocol: introduce mifare common API
gornekich Mar 29, 2022
76d8fed
nfc: move common data to furi hal nfc
gornekich Apr 4, 2022
e58a590
nfc: rename emv_decoder -> emv
gornekich Apr 4, 2022
b3b3ffb
nfc: move emv data structure to emv lib
gornekich Apr 4, 2022
869ed46
nfc: remove deactivate after detection
gornekich Apr 4, 2022
2d59f1d
nfc: rework furi hal nfc detect
gornekich Apr 4, 2022
ee4bca9
nfc: clean up CLI commands and type
gornekich Apr 4, 2022
23584db
nfc: remove unused includes and function
gornekich Apr 4, 2022
18d5bfa
nfc: add TxRxType enum
gornekich Apr 4, 2022
250867a
nfc: read mifare ultralight refactoring
gornekich Apr 5, 2022
9a94eff
nfc: refactore mifare ultralight start
gornekich Apr 6, 2022
eac23d1
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 13, 2022
e403f5c
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 15, 2022
778512f
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 17, 2022
d9fed7c
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 18, 2022
3fd5782
rfal: fix custom data exchange
gornekich Apr 18, 2022
01ed549
nfc: refactor read bank card
gornekich Apr 18, 2022
d26a9cc
nfc: refactor read emv application
gornekich Apr 18, 2022
8afaaad
nfc: refactor emv test emulation
gornekich Apr 18, 2022
9738e4f
nfc: refactor uid emulation
gornekich Apr 18, 2022
5744747
nfc: add limit to uid emulation logger
gornekich Apr 18, 2022
e4a2ddb
Merge branch 'dev' into gornek/2369_nfc_refactoring
gornekich Apr 18, 2022
a4d2393
fix source formatting
gornekich Apr 19, 2022
8b39fdf
Merge branch 'dev' into gornek/2369_nfc_refactoring
gornekich Apr 19, 2022
f97e4f6
Merge remote-tracking branch 'origin/dev' into gornek/2369_nfc_refact…
gornekich Apr 19, 2022
a30be16
furi_hal_nfc: fix data exchange full
gornekich Apr 19, 2022
fd290c7
nfc: fix mifare ultralight type load
gornekich Apr 19, 2022
e2386df
Merge branch 'dev' into gornek/2369_nfc_refactoring
gornekich Apr 19, 2022
e8bce8b
Merge branch 'dev' into gornek/2369_nfc_refactoring
skotopes Apr 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions applications/nfc/nfc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@

bool nfc_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
return scene_manager_handle_custom_event(nfc->scene_manager, event);
}

bool nfc_back_event_callback(void* context) {
furi_assert(context);
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
return scene_manager_handle_back_event(nfc->scene_manager);
}

void nfc_tick_event_callback(void* context) {
furi_assert(context);
Nfc* nfc = (Nfc*)context;
Nfc* nfc = context;
scene_manager_handle_tick_event(nfc->scene_manager);
}

Expand Down
44 changes: 19 additions & 25 deletions applications/nfc/nfc_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,35 @@ static void nfc_cli_print_usage() {
}
}

void nfc_cli_detect(Cli* cli, string_t args) {
static void nfc_cli_detect(Cli* cli, string_t args) {
// Check if nfc worker is not busy
if(furi_hal_nfc_is_busy()) {
printf("Nfc is busy\r\n");
return;
}
rfalNfcDevice* dev_list;
uint8_t dev_cnt = 0;

FuriHalNfcDevData dev_data = {};
bool cmd_exit = false;
furi_hal_nfc_exit_sleep();
printf("Detecting nfc...\r\nPress Ctrl+C to abort\r\n");
while(!cmd_exit) {
cmd_exit |= cli_cmd_interrupt_received(cli);
cmd_exit |= furi_hal_nfc_detect(&dev_list, &dev_cnt, 400, true);
if(dev_cnt > 0) {
printf("Found %d devices\r\n", dev_cnt);
for(uint8_t i = 0; i < dev_cnt; i++) {
printf("%d found: %s ", i + 1, nfc_get_rfal_type(dev_list[i].type));
if(dev_list[i].type == RFAL_NFC_LISTEN_TYPE_NFCA) {
printf("type: %s, ", nfc_get_nfca_type(dev_list[i].dev.nfca.type));
}
printf("UID length: %d, UID:", dev_list[i].nfcidLen);
for(uint8_t j = 0; j < dev_list[i].nfcidLen; j++) {
printf("%02X", dev_list[i].nfcid[j]);
}
printf("\r\n");
if(furi_hal_nfc_detect(&dev_data, 400)) {
printf("found: %s ", nfc_get_dev_type(dev_data.type));
printf("UID length: %d, UID:", dev_data.uid_len);
for(size_t i = 0; i < dev_data.uid_len; i++) {
printf("%02X", dev_data.uid[i]);
}
printf("\r\n");
break;
}
furi_hal_nfc_sleep();
osDelay(50);
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}

void nfc_cli_emulate(Cli* cli, string_t args) {
static void nfc_cli_emulate(Cli* cli, string_t args) {
// Check if nfc worker is not busy
if(furi_hal_nfc_is_busy()) {
printf("Nfc is busy\r\n");
Expand All @@ -60,26 +55,25 @@ void nfc_cli_emulate(Cli* cli, string_t args) {
printf("Emulating NFC-A Type: T2T UID: 36 9C E7 B1 0A C1 34 SAK: 00 ATQA: 00/44\r\n");
printf("Press Ctrl+C to abort\r\n");

NfcDeviceCommonData params = {
FuriHalNfcDevData params = {
.uid = {0x36, 0x9C, 0xe7, 0xb1, 0x0A, 0xC1, 0x34},
.uid_len = 7,
.atqa = {0x44, 0x00},
.sak = 0x00,
.device = NfcDeviceNfca,
.protocol = NfcDeviceProtocolMifareUl,
.type = FuriHalNfcTypeA,
};

while(!cli_cmd_interrupt_received(cli)) {
if(furi_hal_nfc_listen(params.uid, params.uid_len, params.atqa, params.sak, false, 100)) {
printf("Reader detected\r\n");
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}
osDelay(50);
}
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}

void nfc_cli_field(Cli* cli, string_t args) {
static void nfc_cli_field(Cli* cli, string_t args) {
// Check if nfc worker is not busy
if(furi_hal_nfc_is_busy()) {
printf("Nfc is busy\r\n");
Expand All @@ -97,7 +91,7 @@ void nfc_cli_field(Cli* cli, string_t args) {
}

furi_hal_nfc_field_off();
furi_hal_nfc_deactivate();
furi_hal_nfc_sleep();
}

static void nfc_cli(Cli* cli, string_t args, void* context) {
Expand Down
29 changes: 15 additions & 14 deletions applications/nfc/nfc_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,39 +41,39 @@ static void nfc_device_prepare_format_string(NfcDevice* dev, string_t format_str
static bool nfc_device_parse_format_string(NfcDevice* dev, string_t format_string) {
if(string_start_with_str_p(format_string, "UID")) {
dev->format = NfcDeviceSaveFormatUid;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolUnknown;
dev->dev_data.protocol = NfcDeviceProtocolUnknown;
return true;
}
if(string_start_with_str_p(format_string, "Bank card")) {
dev->format = NfcDeviceSaveFormatBankCard;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolEMV;
dev->dev_data.protocol = NfcDeviceProtocolEMV;
return true;
}
// Check Mifare Ultralight types
for(MfUltralightType type = MfUltralightTypeUnknown; type < MfUltralightTypeNum; type++) {
if(string_start_with_str_p(format_string, nfc_mf_ul_type(type, true))) {
if(string_equal_str_p(format_string, nfc_mf_ul_type(type, true))) {
dev->format = NfcDeviceSaveFormatMifareUl;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareUl;
dev->dev_data.protocol = NfcDeviceProtocolMifareUl;
dev->dev_data.mf_ul_data.type = type;
return true;
}
}
if(string_start_with_str_p(format_string, "Mifare Classic")) {
dev->format = NfcDeviceSaveFormatMifareClassic;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareClassic;
dev->dev_data.protocol = NfcDeviceProtocolMifareClassic;
return true;
}
if(string_start_with_str_p(format_string, "Mifare DESFire")) {
dev->format = NfcDeviceSaveFormatMifareDesfire;
dev->dev_data.nfc_data.protocol = NfcDeviceProtocolMifareDesfire;
dev->dev_data.protocol = NfcDeviceProtocolMifareDesfire;
return true;
}
return false;
}

static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
bool saved = false;
MifareUlData* data = &dev->dev_data.mf_ul_data;
MfUltralightData* data = &dev->dev_data.mf_ul_data;
string_t temp_str;
string_init(temp_str);

Expand Down Expand Up @@ -122,7 +122,7 @@ static bool nfc_device_save_mifare_ul_data(FlipperFormat* file, NfcDevice* dev)

bool nfc_device_load_mifare_ul_data(FlipperFormat* file, NfcDevice* dev) {
bool parsed = false;
MifareUlData* data = &dev->dev_data.mf_ul_data;
MfUltralightData* data = &dev->dev_data.mf_ul_data;
string_t temp_str;
string_init(temp_str);

Expand Down Expand Up @@ -548,7 +548,7 @@ bool nfc_device_load_mifare_df_data(FlipperFormat* file, NfcDevice* dev) {

static bool nfc_device_save_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
bool saved = false;
NfcEmvData* data = &dev->dev_data.emv_data;
EmvData* data = &dev->dev_data.emv_data;
uint32_t data_temp = 0;

do {
Expand Down Expand Up @@ -577,8 +577,8 @@ static bool nfc_device_save_bank_card_data(FlipperFormat* file, NfcDevice* dev)

bool nfc_device_load_bank_card_data(FlipperFormat* file, NfcDevice* dev) {
bool parsed = false;
NfcEmvData* data = &dev->dev_data.emv_data;
memset(data, 0, sizeof(NfcEmvData));
EmvData* data = &dev->dev_data.emv_data;
memset(data, 0, sizeof(EmvData));
uint32_t data_cnt = 0;
string_t temp_str;
string_init(temp_str);
Expand Down Expand Up @@ -700,7 +700,7 @@ static bool nfc_device_save_file(

bool saved = false;
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
NfcDeviceCommonData* data = &dev->dev_data.nfc_data;
FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
string_t temp_str;
string_init(temp_str);

Expand Down Expand Up @@ -758,7 +758,7 @@ bool nfc_device_save_shadow(NfcDevice* dev, const char* dev_name) {
static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
bool parsed = false;
FlipperFormat* file = flipper_format_file_alloc(dev->storage);
NfcDeviceCommonData* data = &dev->dev_data.nfc_data;
FuriHalNfcDevData* data = &dev->dev_data.nfc_data;
uint32_t data_cnt = 0;
string_t temp_str;
string_init(temp_str);
Expand Down Expand Up @@ -789,6 +789,7 @@ static bool nfc_device_load_data(NfcDevice* dev, string_t path) {
if(!nfc_device_parse_format_string(dev, temp_str)) break;
// Read and parse UID, ATQA and SAK
if(!flipper_format_get_value_count(file, "UID", &data_cnt)) break;
if(!(data_cnt == 4 || data_cnt == 7)) break;
data->uid_len = data_cnt;
if(!flipper_format_read_hex(file, "UID", data->uid, data->uid_len)) break;
if(!flipper_format_read_hex(file, "ATQA", data->atqa, 2)) break;
Expand Down Expand Up @@ -863,7 +864,7 @@ bool nfc_file_select(NfcDevice* dev) {
}

void nfc_device_data_clear(NfcDeviceData* dev_data) {
if(dev_data->nfc_data.protocol == NfcDeviceProtocolMifareDesfire) {
if(dev_data->protocol == NfcDeviceProtocolMifareDesfire) {
mf_df_clear(&dev_data->mf_df_data);
}
}
Expand Down
37 changes: 6 additions & 31 deletions applications/nfc/nfc_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <storage/storage.h>
#include <dialogs/dialogs.h>

#include <furi_hal_nfc.h>
#include <lib/nfc_protocols/emv.h>
#include <lib/nfc_protocols/mifare_ultralight.h>
#include <lib/nfc_protocols/mifare_classic.h>
#include <lib/nfc_protocols/mifare_desfire.h>
Expand All @@ -17,13 +19,6 @@
#define NFC_APP_EXTENSION ".nfc"
#define NFC_APP_SHADOW_EXTENSION ".shd"

typedef enum {
NfcDeviceNfca,
NfcDeviceNfcb,
NfcDeviceNfcf,
NfcDeviceNfcv,
} NfcDeviceType;

typedef enum {
NfcDeviceProtocolUnknown,
NfcDeviceProtocolEMV,
Expand All @@ -40,38 +35,18 @@ typedef enum {
NfcDeviceSaveFormatMifareDesfire,
} NfcDeviceSaveFormat;

typedef struct {
uint8_t uid_len;
uint8_t uid[10];
uint8_t atqa[2];
uint8_t sak;
NfcDeviceType device;
NfcProtocol protocol;
} NfcDeviceCommonData;

typedef struct {
char name[32];
uint8_t aid[16];
uint16_t aid_len;
uint8_t number[10];
uint8_t number_len;
uint8_t exp_mon;
uint8_t exp_year;
uint16_t country_code;
uint16_t currency_code;
} NfcEmvData;

typedef struct {
uint8_t data[NFC_READER_DATA_MAX_SIZE];
uint16_t size;
} NfcReaderRequestData;

typedef struct {
NfcDeviceCommonData nfc_data;
FuriHalNfcDevData nfc_data;
NfcProtocol protocol;
NfcReaderRequestData reader_data;
union {
NfcEmvData emv_data;
MifareUlData mf_ul_data;
EmvData emv_data;
MfUltralightData mf_ul_data;
MfClassicData mf_classic_data;
MifareDesfireData mf_df_data;
};
Expand Down
2 changes: 1 addition & 1 deletion applications/nfc/nfc_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct Nfc {
NotificationApp* notifications;
SceneManager* scene_manager;
NfcDevice* dev;
NfcDeviceCommonData dev_edit_data;
FuriHalNfcDevData dev_edit_data;

char text_store[NFC_TEXT_STORE_SIZE + 1];
string_t text_box_store;
Expand Down
44 changes: 5 additions & 39 deletions applications/nfc/nfc_types.c
Original file line number Diff line number Diff line change
@@ -1,48 +1,14 @@
#include "nfc_types.h"

const char* nfc_get_rfal_type(rfalNfcDevType type) {
if(type == RFAL_NFC_LISTEN_TYPE_NFCA) {
const char* nfc_get_dev_type(FuriHalNfcType type) {
if(type == FuriHalNfcTypeA) {
return "NFC-A";
} else if(type == RFAL_NFC_LISTEN_TYPE_NFCB) {
} else if(type == FuriHalNfcTypeB) {
return "NFC-B";
} else if(type == RFAL_NFC_LISTEN_TYPE_NFCF) {
} else if(type == FuriHalNfcTypeF) {
return "NFC-F";
} else if(type == RFAL_NFC_LISTEN_TYPE_NFCV) {
} else if(type == FuriHalNfcTypeV) {
return "NFC-V";
} else if(type == RFAL_NFC_LISTEN_TYPE_ST25TB) {
return "NFC-ST25TB";
} else if(type == RFAL_NFC_LISTEN_TYPE_AP2P) {
return "NFC-AP2P";
} else {
return "Unknown";
}
}

const char* nfc_get_dev_type(NfcDeviceType type) {
if(type == NfcDeviceNfca) {
return "NFC-A";
} else if(type == NfcDeviceNfcb) {
return "NFC-B";
} else if(type == NfcDeviceNfcf) {
return "NFC-F";
} else if(type == NfcDeviceNfcv) {
return "NFC-V";
} else {
return "Unknown";
}
}

const char* nfc_get_nfca_type(rfalNfcaListenDeviceType type) {
if(type == RFAL_NFCA_T1T) {
return "T1T";
} else if(type == RFAL_NFCA_T2T) {
return "T2T";
} else if(type == RFAL_NFCA_T4T) {
return "T4T";
} else if(type == RFAL_NFCA_NFCDEP) {
return "NFCDEP";
} else if(type == RFAL_NFCA_T4T_NFCDEP) {
return "T4T_NFCDEP";
} else {
return "Unknown";
}
Expand Down
12 changes: 2 additions & 10 deletions applications/nfc/nfc_types.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
#pragma once

#include "st_errno.h"
#include "rfal_nfc.h"
#include "nfc_device.h"

#include <gui/view_dispatcher.h>
#include "nfc_worker.h"

const char* nfc_get_rfal_type(rfalNfcDevType type);

const char* nfc_get_dev_type(NfcDeviceType type);

const char* nfc_get_nfca_type(rfalNfcaListenDeviceType type);
const char* nfc_get_dev_type(FuriHalNfcType type);

const char* nfc_guess_protocol(NfcProtocol protocol);

Expand Down
Loading