NFC refactoring (#3050)

"A long time ago in a galaxy far, far away...." we started NFC subsystem refactoring.

Starring:

- @gornekich - NFC refactoring project lead, architect, senior developer
- @gsurkov - architect, senior developer
- @RebornedBrain - senior developer

Supporting roles:

- @skotopes, @DrZlo13, @hedger - general architecture advisors, code review
- @Astrrra, @doomwastaken, @Hellitron, @ImagineVagon333 - quality assurance

Special thanks:

@bettse, @pcunning, @nxv, @noproto, @AloneLiberty and everyone else who has been helping us all this time and contributing valuable knowledges, ideas and source code.
This commit is contained in:
gornekich
2023-10-24 12:08:09 +09:00
committed by GitHub
parent 35c903494c
commit d92b0a82cc
514 changed files with 41487 additions and 68124 deletions
@@ -0,0 +1,196 @@
#include "mf_ultralight.h"
#include "mf_ultralight_render.h"
#include <nfc/protocols/mf_ultralight/mf_ultralight_poller.h>
#include "nfc/nfc_app_i.h"
#include "../nfc_protocol_support_common.h"
#include "../nfc_protocol_support_gui_common.h"
enum {
SubmenuIndexUnlock = SubmenuIndexCommonMax,
SubmenuIndexUnlockByReader,
SubmenuIndexUnlockByPassword,
};
static void nfc_scene_info_on_enter_mf_ultralight(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const MfUltralightData* data = nfc_device_get_data(device, NfcProtocolMfUltralight);
FuriString* temp_str = furi_string_alloc();
furi_string_cat_printf(
temp_str, "\e#%s\n", nfc_device_get_name(device, NfcDeviceNameTypeFull));
nfc_render_mf_ultralight_info(data, NfcProtocolFormatTypeFull, temp_str);
widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 52, furi_string_get_cstr(temp_str));
furi_string_free(temp_str);
}
static void nfc_scene_more_info_on_enter_mf_ultralight(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const MfUltralightData* mfu = nfc_device_get_data(device, NfcProtocolMfUltralight);
furi_string_reset(instance->text_box_store);
nfc_render_mf_ultralight_dump(mfu, instance->text_box_store);
text_box_set_font(instance->text_box, TextBoxFontHex);
text_box_set_text(instance->text_box, furi_string_get_cstr(instance->text_box_store));
view_dispatcher_switch_to_view(instance->view_dispatcher, NfcViewTextBox);
}
static NfcCommand
nfc_scene_read_poller_callback_mf_ultralight(NfcGenericEvent event, void* context) {
furi_assert(event.protocol == NfcProtocolMfUltralight);
NfcApp* instance = context;
const MfUltralightPollerEvent* mf_ultralight_event = event.event_data;
if(mf_ultralight_event->type == MfUltralightPollerEventTypeReadSuccess) {
nfc_device_set_data(
instance->nfc_device, NfcProtocolMfUltralight, nfc_poller_get_data(instance->poller));
view_dispatcher_send_custom_event(instance->view_dispatcher, NfcCustomEventPollerSuccess);
return NfcCommandStop;
} else if(mf_ultralight_event->type == MfUltralightPollerEventTypeAuthRequest) {
nfc_device_set_data(
instance->nfc_device, NfcProtocolMfUltralight, nfc_poller_get_data(instance->poller));
const MfUltralightData* data =
nfc_device_get_data(instance->nfc_device, NfcProtocolMfUltralight);
if(instance->mf_ul_auth->type == MfUltralightAuthTypeXiaomi) {
if(mf_ultralight_generate_xiaomi_pass(
instance->mf_ul_auth,
data->iso14443_3a_data->uid,
data->iso14443_3a_data->uid_len)) {
mf_ultralight_event->data->auth_context.skip_auth = false;
}
} else if(instance->mf_ul_auth->type == MfUltralightAuthTypeAmiibo) {
if(mf_ultralight_generate_amiibo_pass(
instance->mf_ul_auth,
data->iso14443_3a_data->uid,
data->iso14443_3a_data->uid_len)) {
mf_ultralight_event->data->auth_context.skip_auth = false;
}
} else if(
instance->mf_ul_auth->type == MfUltralightAuthTypeManual ||
instance->mf_ul_auth->type == MfUltralightAuthTypeReader) {
mf_ultralight_event->data->auth_context.skip_auth = false;
} else {
mf_ultralight_event->data->auth_context.skip_auth = true;
}
if(!mf_ultralight_event->data->auth_context.skip_auth) {
mf_ultralight_event->data->auth_context.password = instance->mf_ul_auth->password;
}
} else if(mf_ultralight_event->type == MfUltralightPollerEventTypeAuthSuccess) {
instance->mf_ul_auth->pack = mf_ultralight_event->data->auth_context.pack;
}
return NfcCommandContinue;
}
static void nfc_scene_read_on_enter_mf_ultralight(NfcApp* instance) {
nfc_poller_start(instance->poller, nfc_scene_read_poller_callback_mf_ultralight, instance);
}
static void nfc_scene_read_and_saved_menu_on_enter_mf_ultralight(NfcApp* instance) {
Submenu* submenu = instance->submenu;
const MfUltralightData* data =
nfc_device_get_data(instance->nfc_device, NfcProtocolMfUltralight);
if(!mf_ultralight_is_all_data_read(data)) {
submenu_add_item(
submenu,
"Unlock",
SubmenuIndexUnlock,
nfc_protocol_support_common_submenu_callback,
instance);
}
}
static void nfc_scene_read_success_on_enter_mf_ultralight(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const MfUltralightData* data = nfc_device_get_data(device, NfcProtocolMfUltralight);
FuriString* temp_str = furi_string_alloc();
bool unlocked =
scene_manager_has_previous_scene(instance->scene_manager, NfcSceneMfUltralightUnlockWarn);
if(unlocked) {
nfc_render_mf_ultralight_pwd_pack(data, temp_str);
} else {
furi_string_cat_printf(
temp_str, "\e#%s\n", nfc_device_get_name(device, NfcDeviceNameTypeFull));
nfc_render_mf_ultralight_info(data, NfcProtocolFormatTypeShort, temp_str);
}
mf_ultralight_auth_reset(instance->mf_ul_auth);
widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 52, furi_string_get_cstr(temp_str));
furi_string_free(temp_str);
}
static void nfc_scene_emulate_on_enter_mf_ultralight(NfcApp* instance) {
const MfUltralightData* data =
nfc_device_get_data(instance->nfc_device, NfcProtocolMfUltralight);
instance->listener = nfc_listener_alloc(instance->nfc, NfcProtocolMfUltralight, data);
nfc_listener_start(instance->listener, NULL, NULL);
}
static bool
nfc_scene_read_and_saved_menu_on_event_mf_ultralight(NfcApp* instance, uint32_t event) {
if(event == SubmenuIndexUnlock) {
scene_manager_next_scene(instance->scene_manager, NfcSceneMfUltralightUnlockMenu);
return true;
}
return false;
}
const NfcProtocolSupportBase nfc_protocol_support_mf_ultralight = {
.features = NfcProtocolFeatureEmulateFull | NfcProtocolFeatureMoreInfo,
.scene_info =
{
.on_enter = nfc_scene_info_on_enter_mf_ultralight,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_more_info =
{
.on_enter = nfc_scene_more_info_on_enter_mf_ultralight,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_read =
{
.on_enter = nfc_scene_read_on_enter_mf_ultralight,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_read_menu =
{
.on_enter = nfc_scene_read_and_saved_menu_on_enter_mf_ultralight,
.on_event = nfc_scene_read_and_saved_menu_on_event_mf_ultralight,
},
.scene_read_success =
{
.on_enter = nfc_scene_read_success_on_enter_mf_ultralight,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_saved_menu =
{
.on_enter = nfc_scene_read_and_saved_menu_on_enter_mf_ultralight,
.on_event = nfc_scene_read_and_saved_menu_on_event_mf_ultralight,
},
.scene_save_name =
{
.on_enter = nfc_protocol_support_common_on_enter_empty,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_emulate =
{
.on_enter = nfc_scene_emulate_on_enter_mf_ultralight,
.on_event = nfc_protocol_support_common_on_event_empty,
},
};
@@ -0,0 +1,5 @@
#pragma once
#include "../nfc_protocol_support_base.h"
extern const NfcProtocolSupportBase nfc_protocol_support_mf_ultralight;
@@ -0,0 +1,45 @@
#include "mf_ultralight_render.h"
#include "../iso14443_3a/iso14443_3a_render.h"
static void nfc_render_mf_ultralight_pages_count(const MfUltralightData* data, FuriString* str) {
furi_string_cat_printf(str, "\nPages Read: %u/%u", data->pages_read, data->pages_total);
if(data->pages_read != data->pages_total) {
furi_string_cat_printf(str, "\nPassword-protected pages!");
}
}
void nfc_render_mf_ultralight_pwd_pack(const MfUltralightData* data, FuriString* str) {
bool all_pages = mf_ultralight_is_all_data_read(data);
furi_string_cat_printf(str, "\e#%s pages unlocked!", all_pages ? "All" : "Not all");
MfUltralightConfigPages* config;
mf_ultralight_get_config_page(data, &config);
furi_string_cat_printf(str, "\nPassword: ");
nfc_render_iso14443_3a_format_bytes(
str, config->password.data, MF_ULTRALIGHT_AUTH_PASSWORD_SIZE);
furi_string_cat_printf(str, "\nPACK: ");
nfc_render_iso14443_3a_format_bytes(str, config->pack.data, MF_ULTRALIGHT_AUTH_PACK_SIZE);
nfc_render_mf_ultralight_pages_count(data, str);
}
void nfc_render_mf_ultralight_info(
const MfUltralightData* data,
NfcProtocolFormatType format_type,
FuriString* str) {
nfc_render_iso14443_3a_info(data->iso14443_3a_data, format_type, str);
nfc_render_mf_ultralight_pages_count(data, str);
}
void nfc_render_mf_ultralight_dump(const MfUltralightData* data, FuriString* str) {
for(size_t i = 0; i < data->pages_read; i++) {
const uint8_t* page_data = data->page[i].data;
for(size_t j = 0; j < MF_ULTRALIGHT_PAGE_SIZE; j += 2) {
furi_string_cat_printf(str, "%02X%02X ", page_data[j], page_data[j + 1]);
}
}
}
@@ -0,0 +1,14 @@
#pragma once
#include <nfc/protocols/mf_ultralight/mf_ultralight.h>
#include "../nfc_protocol_support_render_common.h"
void nfc_render_mf_ultralight_info(
const MfUltralightData* data,
NfcProtocolFormatType format_type,
FuriString* str);
void nfc_render_mf_ultralight_dump(const MfUltralightData* data, FuriString* str);
void nfc_render_mf_ultralight_pwd_pack(const MfUltralightData* data, FuriString* str);