Skip to content

Commit

Permalink
implemented unlock, read, save sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
g3gg0 committed Nov 9, 2022
1 parent 8d2daa7 commit fe216b4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 21 deletions.
63 changes: 59 additions & 4 deletions applications/main/nfc/scenes/nfc_scene_nfcv_unlock.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ typedef enum {
NfcSceneNfcVUnlockStateNotSupportedCard,
} NfcSceneNfcVUnlockState;

bool nfc_scene_nfcv_unlock_worker_callback(NfcWorkerEvent event, void* context) {
static bool nfc_scene_nfcv_unlock_worker_callback(NfcWorkerEvent event, void* context) {
Nfc* nfc = context;

if(event == NfcWorkerEventNfcVPassKey) {
Expand All @@ -19,13 +19,53 @@ bool nfc_scene_nfcv_unlock_worker_callback(NfcWorkerEvent event, void* context)
}
return true;
}
/*
static void nfc_scene_nfcv_unlock_button_callback(GuiButtonType event, InputType type, void* context) {
furi_assert(context);
furi_assert(type);
Nfc* nfc = context;
if(event == GuiButtonTypeCenter) {
if(nfc_worker_get_state(nfc->worker) == NfcWorkerStateNfcVUnlockAndSave) {
nfc_worker_stop(nfc->worker);
nfc_worker_start(
nfc->worker,
NfcWorkerStateNfcVUnlock,
&nfc->dev->dev_data,
nfc_scene_nfcv_unlock_worker_callback,
nfc);
widget_add_button_element(
nfc->widget,
GuiButtonTypeCenter,
"Autosave",
nfc_scene_nfcv_unlock_button_callback,
nfc);
} else {
nfc_worker_stop(nfc->worker);
nfc_worker_start(
nfc->worker,
NfcWorkerStateNfcVUnlockAndSave,
&nfc->dev->dev_data,
nfc_scene_nfcv_unlock_worker_callback,
nfc);
widget_add_button_element(
nfc->widget,
GuiButtonTypeCenter,
"Unlock",
nfc_scene_nfcv_unlock_button_callback,
nfc);
}
notification_message(nfc->notifications, &sequence_single_vibro);
}
}*/

void nfc_scene_nfcv_unlock_popup_callback(void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
}

void nfc_scene_nfcv_unlock_set_state(Nfc* nfc, NfcSceneNfcVUnlockState state) {
FuriHalNfcDevData* nfc_data = &(nfc->dev->dev_data.nfc_data);
NfcVData* nfcv_data = &(nfc->dev->dev_data.nfcv_data);

uint32_t curr_state =
Expand All @@ -40,14 +80,28 @@ void nfc_scene_nfcv_unlock_set_state(Nfc* nfc, NfcSceneNfcVUnlockState state) {
} else if(state == NfcSceneNfcVUnlockStateUnlocked) {
popup_reset(popup);

if(nfc_worker_get_state(nfc->worker) == NfcWorkerStateNfcVUnlockAndSave) {
nfc_text_store_set(nfc, "SLIX-L_%02X%02X%02X%02X%02X%02X%02X%02X",
nfc_data->uid[7], nfc_data->uid[6], nfc_data->uid[5], nfc_data->uid[4],
nfc_data->uid[3], nfc_data->uid[2], nfc_data->uid[1], nfc_data->uid[0]);

nfc->dev->format = NfcDeviceSaveFormatSlixL;

if(nfc_device_save(nfc->dev, nfc->text_store)) {
popup_set_header(popup, "Successfully\nsaved", 94, 3, AlignCenter, AlignTop);
} else {
popup_set_header(popup, "Unlocked but\nsave failed!", 94, 3, AlignCenter, AlignTop);
}
} else {
popup_set_header(popup, "Successfully\nunlocked", 94, 3, AlignCenter, AlignTop);
}

notification_message(nfc->notifications, &sequence_success);

popup_set_header(popup, "Successfully\nUnlocked!", 94, 3, AlignCenter, AlignTop);
popup_set_icon(popup, 0, 6, &I_RFIDDolphinSuccess_108x57);
popup_set_context(popup, nfc);
popup_set_callback(popup, nfc_scene_nfcv_unlock_popup_callback);
popup_set_timeout(popup, 1500);
//popup_enable_timeout(popup);

view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
DOLPHIN_DEED(DolphinDeedNfcReadSuccess);
Expand Down Expand Up @@ -85,10 +139,11 @@ void nfc_scene_nfcv_unlock_on_enter(void* context) {
// Setup view
nfc_scene_nfcv_unlock_set_state(nfc, NfcSceneNfcVUnlockStateDetecting);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);

// Start worker
nfc_worker_start(
nfc->worker,
NfcWorkerStateNfcVUnlock,
NfcWorkerStateNfcVUnlockAndSave,
&nfc->dev->dev_data,
nfc_scene_nfcv_unlock_worker_callback,
nfc);
Expand Down
4 changes: 2 additions & 2 deletions firmware/targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
entry,status,name,type,params
Version,+,7.4,,
Version,+,7.5,,
Header,+,applications/services/bt/bt_service/bt.h,,
Header,+,applications/services/cli/cli.h,,
Header,+,applications/services/cli/cli_vcp.h,,
Expand Down Expand Up @@ -1960,7 +1960,7 @@ Function,-,nfca_signal_encode,void,"NfcaSignal*, uint8_t*, uint16_t, uint8_t*"
Function,-,nfca_signal_free,void,NfcaSignal*
Function,-,nfcv_inventory,ReturnCode,uint8_t*
Function,-,nfcv_read_blocks,ReturnCode,"NfcVReader*, NfcVData*"
Function,-,nfcv_read_sysinfo,ReturnCode,NfcVData*
Function,-,nfcv_read_sysinfo,ReturnCode,"FuriHalNfcDevData*, NfcVData*"
Function,+,notification_internal_message,void,"NotificationApp*, const NotificationSequence*"
Function,+,notification_internal_message_block,void,"NotificationApp*, const NotificationSequence*"
Function,+,notification_message,void,"NotificationApp*, const NotificationSequence*"
Expand Down
28 changes: 21 additions & 7 deletions lib/nfc/nfc_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ int32_t nfc_worker_task(void* context) {
nfc_worker_analyze_reader(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateNfcVUnlock) {
nfc_worker_nfcv_unlock(nfc_worker);
} else if(nfc_worker->state == NfcWorkerStateNfcVUnlockAndSave) {
nfc_worker_nfcv_unlock(nfc_worker);
}
furi_hal_nfc_sleep();
nfc_worker_change_state(nfc_worker, NfcWorkerStateReady);
Expand All @@ -133,7 +135,8 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) {

furi_hal_nfc_sleep();

while(nfc_worker->state == NfcWorkerStateNfcVUnlock) {
while((nfc_worker->state == NfcWorkerStateNfcVUnlock) ||
(nfc_worker->state == NfcWorkerStateNfcVUnlockAndSave)) {

furi_hal_nfc_exit_sleep();
furi_hal_nfc_ll_txrx_on();
Expand Down Expand Up @@ -195,12 +198,26 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) {
}
if(ret == ERR_NONE) {
/* unlock succesful */
furi_hal_console_printf(" => success, wait for chip to disappear.\r\n");
nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context);
if(nfc_worker->state == NfcWorkerStateNfcVUnlockAndSave) {
NfcVReader reader;

if(!slix_l_read_card(&reader, &nfc_worker->dev_data->nfc_data, nfcv_data)) {
furi_hal_console_printf(" => failed, wait for chip to disappear.\r\n");
snprintf(nfcv_data->error, sizeof(nfcv_data->error), "Read card\nfailed");
nfc_worker->callback(NfcWorkerEventWrongCardDetected, nfc_worker->context);
} else {
furi_hal_console_printf(" => success, wait for chip to disappear.\r\n");
nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context);
}
} else {
furi_hal_console_printf(" => success, wait for chip to disappear.\r\n");
nfc_worker->callback(NfcWorkerEventCardDetected, nfc_worker->context);
}

while(slix_l_get_random(NULL) == ERR_NONE) {
furi_delay_ms(100);
}

} else {
/* unlock failed */
furi_hal_console_printf(" => failed, wait for chip to disappear.\r\n");
Expand Down Expand Up @@ -235,7 +252,6 @@ void nfc_worker_nfcv_unlock(NfcWorker* nfc_worker) {
static bool nfc_worker_read_slix_l(NfcWorker* nfc_worker, FuriHalNfcTxRxContext* tx_rx) {
bool read_success = false;
NfcVReader reader = {};
NfcVData data = {};

if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug)) {
reader_analyzer_prepare_tx_rx(nfc_worker->reader_analyzer, tx_rx, false);
Expand All @@ -244,9 +260,7 @@ static bool nfc_worker_read_slix_l(NfcWorker* nfc_worker, FuriHalNfcTxRxContext*

do {
if(!furi_hal_nfc_detect(&nfc_worker->dev_data->nfc_data, 200)) break;
if(!slix_l_read_card(&reader, &data)) break;
// Copy data
nfc_worker->dev_data->nfcv_data = data;
if(!slix_l_read_card(&reader, &nfc_worker->dev_data->nfc_data, &nfc_worker->dev_data->nfcv_data)) break;
read_success = true;
} while(false);

Expand Down
1 change: 1 addition & 0 deletions lib/nfc/nfc_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef enum {
NfcWorkerStateMfClassicDictAttack,
NfcWorkerStateAnalyzeReader,
NfcWorkerStateNfcVUnlock,
NfcWorkerStateNfcVUnlockAndSave,
// Debug
NfcWorkerStateEmulateApdu,
NfcWorkerStateField,
Expand Down
8 changes: 7 additions & 1 deletion lib/nfc/protocols/nfcv.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ReturnCode nfcv_read_blocks(
return ERR_NONE;
}

ReturnCode nfcv_read_sysinfo(NfcVData* data) {
ReturnCode nfcv_read_sysinfo(FuriHalNfcDevData* nfc_data, NfcVData* data) {
uint8_t rxBuf[32];
uint16_t received = 0;

Expand All @@ -65,6 +65,12 @@ ReturnCode nfcv_read_sysinfo(NfcVData* data) {
rxBuf, sizeof(rxBuf), &received);

if(ret == ERR_NONE) {
nfc_data->type = FuriHalNfcTypeV;
nfc_data->uid_len = 8;
/* UID is stored reversed in this structure */
for(int pos = 0; pos < nfc_data->uid_len; pos++) {
nfc_data->uid[pos] = rxBuf[2 + (7 - pos)];
}
data->dsfid = rxBuf[10];
data->afi = rxBuf[11];
data->block_num = rxBuf[12] + 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/nfc/protocols/nfcv.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ typedef struct {
} NfcVReader;

ReturnCode nfcv_read_blocks(NfcVReader* reader, NfcVData* data);
ReturnCode nfcv_read_sysinfo(NfcVData* data);
ReturnCode nfcv_read_sysinfo(FuriHalNfcDevData* nfc_data, NfcVData* data);
ReturnCode nfcv_inventory(uint8_t* uid);

11 changes: 6 additions & 5 deletions lib/nfc/protocols/slix_l.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ bool slix_l_check_card_type(uint8_t UID0, uint8_t UID1, uint8_t UID2) {

bool slix_l_read_card(
NfcVReader* reader,
NfcVData* data) {
FuriHalNfcDevData* nfc_data,
NfcVData* nfcv_data) {
furi_assert(reader);
furi_assert(data);
furi_assert(nfcv_data);

if(nfcv_read_sysinfo(data) != ERR_NONE) {
if(nfcv_read_sysinfo(nfc_data, nfcv_data) != ERR_NONE) {
return false;
}

reader->blocks_to_read = data->block_num;
return (nfcv_read_blocks(reader, data) == ERR_NONE);
reader->blocks_to_read = nfcv_data->block_num;
return (nfcv_read_blocks(reader, nfcv_data) == ERR_NONE);
}

ReturnCode slix_l_get_random(uint8_t* rand) {
Expand Down
2 changes: 1 addition & 1 deletion lib/nfc/protocols/slix_l.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


bool slix_l_check_card_type(uint8_t UID0, uint8_t UID1, uint8_t UID2);
bool slix_l_read_card(NfcVReader* reader, NfcVData* data);
bool slix_l_read_card(NfcVReader* reader, FuriHalNfcDevData* nfc_data, NfcVData* data);

ReturnCode slix_l_get_random(uint8_t* rand);
ReturnCode slix_l_unlock(uint32_t id, uint8_t* rand, uint32_t password);
Expand Down

0 comments on commit fe216b4

Please sign in to comment.