[FL-2933] Mf Classic initial write, update, detect reader (#1941)

* nfc: introduce nfc write
* nfc: add write logic
* nfc worker: add write state
* nfc: add mfc update logic
* nfc: add update success logic
* nfc: add custom card for detect reader
* nfc: update write logic
* nfc: add halt command, add notifications
* nfc: add write fail scene
* nfc: fixes and clean up
* nfc: fix navigation ad notifications
* nfc: fix detect reader nfc data setter

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2022-10-28 20:10:16 +04:00
committed by GitHub
parent 09b622d4ae
commit 93a6e17ce5
23 changed files with 949 additions and 93 deletions

View File

@@ -36,6 +36,12 @@ ADD_SCENE(nfc, mf_classic_keys_list, MfClassicKeysList)
ADD_SCENE(nfc, mf_classic_keys_delete, MfClassicKeysDelete)
ADD_SCENE(nfc, mf_classic_keys_warn_duplicate, MfClassicKeysWarnDuplicate)
ADD_SCENE(nfc, mf_classic_dict_attack, MfClassicDictAttack)
ADD_SCENE(nfc, mf_classic_write, MfClassicWrite)
ADD_SCENE(nfc, mf_classic_write_success, MfClassicWriteSuccess)
ADD_SCENE(nfc, mf_classic_write_fail, MfClassicWriteFail)
ADD_SCENE(nfc, mf_classic_update, MfClassicUpdate)
ADD_SCENE(nfc, mf_classic_update_success, MfClassicUpdateSuccess)
ADD_SCENE(nfc, mf_classic_wrong_card, MfClassicWrongCard)
ADD_SCENE(nfc, emv_read_success, EmvReadSuccess)
ADD_SCENE(nfc, emv_menu, EmvMenu)
ADD_SCENE(nfc, emulate_apdu_sequence, EmulateApduSequence)

View File

@@ -28,6 +28,11 @@ void nfc_scene_detect_reader_on_enter(void* context) {
detect_reader_set_callback(nfc->detect_reader, nfc_scene_detect_reader_callback, nfc);
detect_reader_set_nonces_max(nfc->detect_reader, NFC_SCENE_DETECT_READER_PAIR_NONCES_MAX);
NfcDeviceData* dev_data = &nfc->dev->dev_data;
if(dev_data->nfc_data.uid_len) {
detect_reader_set_uid(
nfc->detect_reader, dev_data->nfc_data.uid, dev_data->nfc_data.uid_len);
}
// Store number of collected nonces in scene state
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneDetectReader, 0);

View File

@@ -0,0 +1,98 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
enum {
NfcSceneMfClassicUpdateStateCardSearch,
NfcSceneMfClassicUpdateStateCardFound,
};
bool nfc_mf_classic_update_worker_callback(NfcWorkerEvent event, void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, event);
return true;
}
static void nfc_scene_mf_classic_update_setup_view(Nfc* nfc) {
Popup* popup = nfc->popup;
popup_reset(popup);
uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfClassicUpdate);
if(state == NfcSceneMfClassicUpdateStateCardSearch) {
popup_set_text(
nfc->popup, "Apply the initial\ncard only", 128, 32, AlignRight, AlignCenter);
popup_set_icon(nfc->popup, 0, 8, &I_NFC_manual_60x50);
} else {
popup_set_header(popup, "Updating\nDon't move...", 52, 32, AlignLeft, AlignCenter);
popup_set_icon(popup, 12, 23, &A_Loading_24);
}
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}
void nfc_scene_mf_classic_update_on_enter(void* context) {
Nfc* nfc = context;
DOLPHIN_DEED(DolphinDeedNfcEmulate);
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneMfClassicUpdate, NfcSceneMfClassicUpdateStateCardSearch);
nfc_scene_mf_classic_update_setup_view(nfc);
// Setup and start worker
nfc_worker_start(
nfc->worker,
NfcWorkerStateMfClassicUpdate,
&nfc->dev->dev_data,
nfc_mf_classic_update_worker_callback,
nfc);
nfc_blink_emulate_start(nfc);
}
bool nfc_scene_mf_classic_update_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcWorkerEventSuccess) {
nfc_worker_stop(nfc->worker);
if(nfc_device_save_shadow(nfc->dev, nfc->dev->dev_name)) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicUpdateSuccess);
} else {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicWrongCard);
}
consumed = true;
} else if(event.event == NfcWorkerEventWrongCard) {
nfc_worker_stop(nfc->worker);
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicWrongCard);
consumed = true;
} else if(event.event == NfcWorkerEventCardDetected) {
scene_manager_set_scene_state(
nfc->scene_manager,
NfcSceneMfClassicUpdate,
NfcSceneMfClassicUpdateStateCardFound);
nfc_scene_mf_classic_update_setup_view(nfc);
consumed = true;
} else if(event.event == NfcWorkerEventNoCardDetected) {
scene_manager_set_scene_state(
nfc->scene_manager,
NfcSceneMfClassicUpdate,
NfcSceneMfClassicUpdateStateCardSearch);
nfc_scene_mf_classic_update_setup_view(nfc);
consumed = true;
}
}
return consumed;
}
void nfc_scene_mf_classic_update_on_exit(void* context) {
Nfc* nfc = context;
nfc_worker_stop(nfc->worker);
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneMfClassicUpdate, NfcSceneMfClassicUpdateStateCardSearch);
// Clear view
popup_reset(nfc->popup);
nfc_blink_stop(nfc);
}

View File

@@ -0,0 +1,44 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_scene_mf_classic_update_success_popup_callback(void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
}
void nfc_scene_mf_classic_update_success_on_enter(void* context) {
Nfc* nfc = context;
DOLPHIN_DEED(DolphinDeedNfcSave);
notification_message(nfc->notifications, &sequence_success);
Popup* popup = nfc->popup;
popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
popup_set_header(popup, "Updated!", 11, 20, AlignLeft, AlignBottom);
popup_set_timeout(popup, 1500);
popup_set_context(popup, nfc);
popup_set_callback(popup, nfc_scene_mf_classic_update_success_popup_callback);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}
bool nfc_scene_mf_classic_update_success_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventViewExit) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneFileSelect);
}
}
return consumed;
}
void nfc_scene_mf_classic_update_success_on_exit(void* context) {
Nfc* nfc = context;
// Clear view
popup_reset(nfc->popup);
}

View File

@@ -0,0 +1,92 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
enum {
NfcSceneMfClassicWriteStateCardSearch,
NfcSceneMfClassicWriteStateCardFound,
};
bool nfc_mf_classic_write_worker_callback(NfcWorkerEvent event, void* context) {
furi_assert(context);
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, event);
return true;
}
static void nfc_scene_mf_classic_write_setup_view(Nfc* nfc) {
Popup* popup = nfc->popup;
popup_reset(popup);
uint32_t state = scene_manager_get_scene_state(nfc->scene_manager, NfcSceneMfClassicWrite);
if(state == NfcSceneMfClassicWriteStateCardSearch) {
popup_set_text(
nfc->popup, "Apply the initial\ncard only", 128, 32, AlignRight, AlignCenter);
popup_set_icon(nfc->popup, 0, 8, &I_NFC_manual_60x50);
} else {
popup_set_header(popup, "Writing\nDon't move...", 52, 32, AlignLeft, AlignCenter);
popup_set_icon(popup, 12, 23, &A_Loading_24);
}
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}
void nfc_scene_mf_classic_write_on_enter(void* context) {
Nfc* nfc = context;
DOLPHIN_DEED(DolphinDeedNfcEmulate);
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneMfClassicWrite, NfcSceneMfClassicWriteStateCardSearch);
nfc_scene_mf_classic_write_setup_view(nfc);
// Setup and start worker
nfc_worker_start(
nfc->worker,
NfcWorkerStateMfClassicWrite,
&nfc->dev->dev_data,
nfc_mf_classic_write_worker_callback,
nfc);
nfc_blink_emulate_start(nfc);
}
bool nfc_scene_mf_classic_write_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcWorkerEventSuccess) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicWriteSuccess);
consumed = true;
} else if(event.event == NfcWorkerEventFail) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicWriteFail);
consumed = true;
} else if(event.event == NfcWorkerEventWrongCard) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicWrongCard);
consumed = true;
} else if(event.event == NfcWorkerEventCardDetected) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneMfClassicWrite, NfcSceneMfClassicWriteStateCardFound);
nfc_scene_mf_classic_write_setup_view(nfc);
consumed = true;
} else if(event.event == NfcWorkerEventNoCardDetected) {
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneMfClassicWrite, NfcSceneMfClassicWriteStateCardSearch);
nfc_scene_mf_classic_write_setup_view(nfc);
consumed = true;
}
}
return consumed;
}
void nfc_scene_mf_classic_write_on_exit(void* context) {
Nfc* nfc = context;
nfc_worker_stop(nfc->worker);
scene_manager_set_scene_state(
nfc->scene_manager, NfcSceneMfClassicWrite, NfcSceneMfClassicWriteStateCardSearch);
// Clear view
popup_reset(nfc->popup);
nfc_blink_stop(nfc);
}

View File

@@ -0,0 +1,58 @@
#include "../nfc_i.h"
void nfc_scene_mf_classic_write_fail_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
Nfc* nfc = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc->view_dispatcher, result);
}
}
void nfc_scene_mf_classic_write_fail_on_enter(void* context) {
Nfc* nfc = context;
Widget* widget = nfc->widget;
notification_message(nfc->notifications, &sequence_error);
widget_add_icon_element(widget, 72, 17, &I_DolphinCommon_56x48);
widget_add_string_element(
widget, 7, 4, AlignLeft, AlignTop, FontPrimary, "Writing gone wrong!");
widget_add_string_multiline_element(
widget,
7,
17,
AlignLeft,
AlignTop,
FontSecondary,
"Not all sectors\nwere written\ncorrectly.");
widget_add_button_element(
widget, GuiButtonTypeLeft, "Finish", nfc_scene_mf_classic_write_fail_widget_callback, nfc);
// Setup and start worker
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget);
}
bool nfc_scene_mf_classic_write_fail_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneFileSelect);
}
} else if(event.type == SceneManagerEventTypeBack) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneSavedMenu);
}
return consumed;
}
void nfc_scene_mf_classic_write_fail_on_exit(void* context) {
Nfc* nfc = context;
widget_reset(nfc->widget);
}

View File

@@ -0,0 +1,44 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
void nfc_scene_mf_classic_write_success_popup_callback(void* context) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventViewExit);
}
void nfc_scene_mf_classic_write_success_on_enter(void* context) {
Nfc* nfc = context;
DOLPHIN_DEED(DolphinDeedNfcSave);
notification_message(nfc->notifications, &sequence_success);
Popup* popup = nfc->popup;
popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
popup_set_header(popup, "Successfully\nwritten", 13, 22, AlignLeft, AlignBottom);
popup_set_timeout(popup, 1500);
popup_set_context(popup, nfc);
popup_set_callback(popup, nfc_scene_mf_classic_write_success_popup_callback);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewPopup);
}
bool nfc_scene_mf_classic_write_success_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcCustomEventViewExit) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc->scene_manager, NfcSceneFileSelect);
}
}
return consumed;
}
void nfc_scene_mf_classic_write_success_on_exit(void* context) {
Nfc* nfc = context;
// Clear view
popup_reset(nfc->popup);
}

View File

@@ -0,0 +1,53 @@
#include "../nfc_i.h"
void nfc_scene_mf_classic_wrong_card_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
Nfc* nfc = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc->view_dispatcher, result);
}
}
void nfc_scene_mf_classic_wrong_card_on_enter(void* context) {
Nfc* nfc = context;
Widget* widget = nfc->widget;
notification_message(nfc->notifications, &sequence_error);
widget_add_icon_element(widget, 73, 17, &I_DolphinCommon_56x48);
widget_add_string_element(
widget, 3, 4, AlignLeft, AlignTop, FontPrimary, "This is wrong card");
widget_add_string_multiline_element(
widget,
4,
17,
AlignLeft,
AlignTop,
FontSecondary,
"Data management\nis only possible\nwith initial card");
widget_add_button_element(
widget, GuiButtonTypeLeft, "Retry", nfc_scene_mf_classic_wrong_card_widget_callback, nfc);
// Setup and start worker
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewWidget);
}
bool nfc_scene_mf_classic_wrong_card_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_previous_scene(nfc->scene_manager);
}
}
return consumed;
}
void nfc_scene_mf_classic_wrong_card_on_exit(void* context) {
Nfc* nfc = context;
widget_reset(nfc->widget);
}

View File

@@ -4,6 +4,9 @@
enum SubmenuIndex {
SubmenuIndexEmulate,
SubmenuIndexEditUid,
SubmenuIndexDetectReader,
SubmenuIndexWrite,
SubmenuIndexUpdate,
SubmenuIndexRename,
SubmenuIndexDelete,
SubmenuIndexInfo,
@@ -42,6 +45,28 @@ void nfc_scene_saved_menu_on_enter(void* context) {
submenu_add_item(
submenu, "Emulate", SubmenuIndexEmulate, nfc_scene_saved_menu_submenu_callback, nfc);
}
if(nfc->dev->format == NfcDeviceSaveFormatMifareClassic) {
if(!mf_classic_is_card_read(&nfc->dev->dev_data.mf_classic_data)) {
submenu_add_item(
submenu,
"Detect reader",
SubmenuIndexDetectReader,
nfc_scene_saved_menu_submenu_callback,
nfc);
}
submenu_add_item(
submenu,
"Write To Initial Card",
SubmenuIndexWrite,
nfc_scene_saved_menu_submenu_callback,
nfc);
submenu_add_item(
submenu,
"Update From Initial Card",
SubmenuIndexUpdate,
nfc_scene_saved_menu_submenu_callback,
nfc);
}
submenu_add_item(
submenu, "Info", SubmenuIndexInfo, nfc_scene_saved_menu_submenu_callback, nfc);
if(nfc->dev->shadow_file_exist) {
@@ -79,6 +104,15 @@ bool nfc_scene_saved_menu_on_event(void* context, SceneManagerEvent event) {
}
DOLPHIN_DEED(DolphinDeedNfcEmulate);
consumed = true;
} else if(event.event == SubmenuIndexDetectReader) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneDetectReader);
consumed = true;
} else if(event.event == SubmenuIndexWrite) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicWrite);
consumed = true;
} else if(event.event == SubmenuIndexUpdate) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneMfClassicUpdate);
consumed = true;
} else if(event.event == SubmenuIndexRename) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName);
consumed = true;

View File

@@ -53,6 +53,7 @@ bool nfc_scene_start_on_event(void* context, SceneManagerEvent event) {
} else if(event.event == SubmenuIndexDetectReader) {
bool sd_exist = storage_sd_status(nfc->dev->storage) == FSE_OK;
if(sd_exist) {
nfc_device_data_clear(&nfc->dev->dev_data);
scene_manager_next_scene(nfc->scene_manager, NfcSceneDetectReader);
DOLPHIN_DEED(DolphinDeedNfcDetectReader);
} else {

View File

@@ -2,6 +2,8 @@
#include <assets_icons.h>
#include <gui/elements.h>
#define DETECT_READER_UID_MAX_LEN (10)
struct DetectReader {
View* view;
DetectReaderDoneCallback callback;
@@ -12,6 +14,7 @@ typedef struct {
uint16_t nonces;
uint16_t nonces_max;
DetectReaderState state;
FuriString* uid_str;
} DetectReaderViewModel;
static void detect_reader_draw_callback(Canvas* canvas, void* model) {
@@ -23,6 +26,10 @@ static void detect_reader_draw_callback(Canvas* canvas, void* model) {
if(m->state == DetectReaderStateStart) {
snprintf(text, sizeof(text), "Touch the reader");
canvas_draw_icon(canvas, 21, 13, &I_Move_flipper_26x39);
if(furi_string_size(m->uid_str)) {
elements_multiline_text_aligned(
canvas, 64, 64, AlignCenter, AlignBottom, furi_string_get_cstr(m->uid_str));
}
} else if(m->state == DetectReaderStateReaderDetected) {
snprintf(text, sizeof(text), "Move the Flipper away");
canvas_draw_icon(canvas, 24, 25, &I_Release_arrow_18x15);
@@ -86,12 +93,24 @@ DetectReader* detect_reader_alloc() {
view_set_input_callback(detect_reader->view, detect_reader_input_callback);
view_set_context(detect_reader->view, detect_reader);
with_view_model(
detect_reader->view,
DetectReaderViewModel * model,
{ model->uid_str = furi_string_alloc(); },
false);
return detect_reader;
}
void detect_reader_free(DetectReader* detect_reader) {
furi_assert(detect_reader);
with_view_model(
detect_reader->view,
DetectReaderViewModel * model,
{ furi_string_free(model->uid_str); },
false);
view_free(detect_reader->view);
free(detect_reader);
}
@@ -106,6 +125,7 @@ void detect_reader_reset(DetectReader* detect_reader) {
model->nonces = 0;
model->nonces_max = 0;
model->state = DetectReaderStateStart;
furi_string_reset(model->uid_str);
},
false);
}
@@ -152,3 +172,19 @@ void detect_reader_set_state(DetectReader* detect_reader, DetectReaderState stat
with_view_model(
detect_reader->view, DetectReaderViewModel * model, { model->state = state; }, true);
}
void detect_reader_set_uid(DetectReader* detect_reader, uint8_t* uid, uint8_t uid_len) {
furi_assert(detect_reader);
furi_assert(uid);
furi_assert(uid_len < DETECT_READER_UID_MAX_LEN);
with_view_model(
detect_reader->view,
DetectReaderViewModel * model,
{
furi_string_set_str(model->uid_str, "UID:");
for(size_t i = 0; i < uid_len; i++) {
furi_string_cat_printf(model->uid_str, " %02X", uid[i]);
}
},
true);
}

View File

@@ -32,3 +32,5 @@ void detect_reader_set_nonces_max(DetectReader* detect_reader, uint16_t nonces_m
void detect_reader_set_nonces_collected(DetectReader* detect_reader, uint16_t nonces_collected);
void detect_reader_set_state(DetectReader* detect_reader, DetectReaderState state);
void detect_reader_set_uid(DetectReader* detect_reader, uint8_t* uid, uint8_t uid_len);