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,149 @@
#include "iso14443_4a.h"
#include "iso14443_4a_render.h"
#include <nfc/protocols/iso14443_4a/iso14443_4a_poller.h>
#include <nfc/protocols/iso14443_4a/iso14443_4a_listener.h>
#include "nfc/nfc_app_i.h"
#include "../nfc_protocol_support_common.h"
#include "../nfc_protocol_support_gui_common.h"
static void nfc_scene_info_on_enter_iso14443_4a(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const Iso14443_4aData* data = nfc_device_get_data(device, NfcProtocolIso14443_4a);
FuriString* temp_str = furi_string_alloc();
furi_string_cat_printf(
temp_str, "\e#%s\n", nfc_device_get_name(device, NfcDeviceNameTypeFull));
nfc_render_iso14443_4a_info(data, NfcProtocolFormatTypeFull, temp_str);
widget_add_text_scroll_element(
instance->widget, 0, 0, 128, 64, furi_string_get_cstr(temp_str));
furi_string_free(temp_str);
}
static NfcCommand
nfc_scene_read_poller_callback_iso14443_4a(NfcGenericEvent event, void* context) {
furi_assert(event.protocol == NfcProtocolIso14443_4a);
NfcApp* instance = context;
const Iso14443_4aPollerEvent* iso14443_4a_event = event.event_data;
if(iso14443_4a_event->type == Iso14443_4aPollerEventTypeReady) {
nfc_device_set_data(
instance->nfc_device, NfcProtocolIso14443_4a, nfc_poller_get_data(instance->poller));
view_dispatcher_send_custom_event(instance->view_dispatcher, NfcCustomEventPollerSuccess);
return NfcCommandStop;
}
return NfcCommandContinue;
}
static void nfc_scene_read_on_enter_iso14443_4a(NfcApp* instance) {
nfc_poller_start(instance->poller, nfc_scene_read_poller_callback_iso14443_4a, instance);
}
static void nfc_scene_read_success_on_enter_iso14443_4a(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const Iso14443_4aData* data = nfc_device_get_data(device, NfcProtocolIso14443_4a);
FuriString* temp_str = furi_string_alloc();
furi_string_cat_printf(
temp_str, "\e#%s\n", nfc_device_get_name(device, NfcDeviceNameTypeFull));
nfc_render_iso14443_4a_info(data, NfcProtocolFormatTypeShort, 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_saved_menu_on_enter_iso14443_4a(NfcApp* instance) {
UNUSED(instance);
}
NfcCommand nfc_scene_emulate_listener_callback_iso14443_4a(NfcGenericEvent event, void* context) {
furi_assert(context);
furi_assert(event.protocol == NfcProtocolIso14443_4a);
furi_assert(event.event_data);
NfcApp* nfc = context;
Iso14443_4aListenerEvent* iso14443_4a_event = event.event_data;
if(iso14443_4a_event->type == Iso14443_4aListenerEventTypeReceivedData) {
if(furi_string_size(nfc->text_box_store) < NFC_LOG_SIZE_MAX) {
furi_string_cat_printf(nfc->text_box_store, "R:");
for(size_t i = 0; i < bit_buffer_get_size_bytes(iso14443_4a_event->data->buffer);
i++) {
furi_string_cat_printf(
nfc->text_box_store,
" %02X",
bit_buffer_get_byte(iso14443_4a_event->data->buffer, i));
}
furi_string_push_back(nfc->text_box_store, '\n');
view_dispatcher_send_custom_event(nfc->view_dispatcher, NfcCustomEventListenerUpdate);
}
}
return NfcCommandContinue;
}
static void nfc_scene_emulate_on_enter_iso14443_4a(NfcApp* instance) {
const Iso14443_4aData* data =
nfc_device_get_data(instance->nfc_device, NfcProtocolIso14443_4a);
instance->listener = nfc_listener_alloc(instance->nfc, NfcProtocolIso14443_4a, data);
nfc_listener_start(
instance->listener, nfc_scene_emulate_listener_callback_iso14443_4a, instance);
}
static bool nfc_scene_read_menu_on_event_iso14443_4a(NfcApp* instance, uint32_t event) {
if(event == SubmenuIndexCommonEmulate) {
scene_manager_next_scene(instance->scene_manager, NfcSceneEmulate);
return true;
}
return false;
}
const NfcProtocolSupportBase nfc_protocol_support_iso14443_4a = {
.features = NfcProtocolFeatureEmulateUid | NfcProtocolFeatureEditUid,
.scene_info =
{
.on_enter = nfc_scene_info_on_enter_iso14443_4a,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_read =
{
.on_enter = nfc_scene_read_on_enter_iso14443_4a,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_read_menu =
{
.on_enter = nfc_protocol_support_common_on_enter_empty,
.on_event = nfc_scene_read_menu_on_event_iso14443_4a,
},
.scene_read_success =
{
.on_enter = nfc_scene_read_success_on_enter_iso14443_4a,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_saved_menu =
{
.on_enter = nfc_scene_saved_menu_on_enter_iso14443_4a,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.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_iso14443_4a,
.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_iso14443_4a;
@@ -0,0 +1,7 @@
#pragma once
#include <nfc/protocols/iso14443_4a/iso14443_4a.h>
#include "iso14443_4a.h"
NfcCommand nfc_scene_emulate_listener_callback_iso14443_4a(NfcGenericEvent event, void* context);
@@ -0,0 +1,84 @@
#include "iso14443_4a_render.h"
#include "../iso14443_3a/iso14443_3a_render.h"
void nfc_render_iso14443_4a_info(
const Iso14443_4aData* data,
NfcProtocolFormatType format_type,
FuriString* str) {
nfc_render_iso14443_4a_brief(data, str);
if(format_type != NfcProtocolFormatTypeFull) return;
nfc_render_iso14443_4a_extra(data, str);
}
void nfc_render_iso14443_4a_brief(const Iso14443_4aData* data, FuriString* str) {
nfc_render_iso14443_3a_brief(iso14443_4a_get_base_data(data), str);
}
void nfc_render_iso14443_4a_extra(const Iso14443_4aData* data, FuriString* str) {
furi_string_cat_printf(str, "\n\e#Protocol info\n");
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRateBoth106Kbit)) {
furi_string_cat(str, "Bit rate PICC <-> PCD:\n 106 kBit/s supported\n");
} else {
furi_string_cat(str, "Bit rate PICC -> PCD:\n");
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRatePiccToPcd212Kbit)) {
furi_string_cat(str, " 212 kBit/s supported\n");
}
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRatePiccToPcd424Kbit)) {
furi_string_cat(str, " 424 kBit/s supported\n");
}
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRatePiccToPcd848Kbit)) {
furi_string_cat(str, " 848 kBit/s supported\n");
}
furi_string_cat(str, "Bit rate PICC <- PCD:\n");
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRatePcdToPicc212Kbit)) {
furi_string_cat(str, " 212 kBit/s supported\n");
}
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRatePcdToPicc424Kbit)) {
furi_string_cat(str, " 424 kBit/s supported\n");
}
if(iso14443_4a_supports_bit_rate(data, Iso14443_4aBitRatePcdToPicc848Kbit)) {
furi_string_cat(str, " 848 kBit/s supported\n");
}
}
furi_string_cat(str, "Max frame size: ");
const uint16_t max_frame_size = iso14443_4a_get_frame_size_max(data);
if(max_frame_size != 0) {
furi_string_cat_printf(str, "%u bytes\n", max_frame_size);
} else {
furi_string_cat(str, "? (RFU)\n");
}
const uint32_t fwt_fc = iso14443_4a_get_fwt_fc_max(data);
if(fwt_fc != 0) {
furi_string_cat_printf(str, "Max waiting time: %4.2g s\n", (double)(fwt_fc / 13.56e6));
}
const char* nad_support_str =
iso14443_4a_supports_frame_option(data, Iso14443_4aFrameOptionNad) ? "" : "not ";
furi_string_cat_printf(str, "NAD: %ssupported\n", nad_support_str);
const char* cid_support_str =
iso14443_4a_supports_frame_option(data, Iso14443_4aFrameOptionCid) ? "" : "not ";
furi_string_cat_printf(str, "CID: %ssupported", cid_support_str);
uint32_t hist_bytes_count;
const uint8_t* hist_bytes = iso14443_4a_get_historical_bytes(data, &hist_bytes_count);
if(hist_bytes_count > 0) {
furi_string_cat_printf(str, "\n\e#Historical bytes\nRaw:");
for(size_t i = 0; i < hist_bytes_count; ++i) {
furi_string_cat_printf(str, " %02X", hist_bytes[i]);
}
}
furi_string_cat(str, "\n\e#ISO14443-3A data");
nfc_render_iso14443_3a_extra(iso14443_4a_get_base_data(data), str);
}
@@ -0,0 +1,14 @@
#pragma once
#include <nfc/protocols/iso14443_4a/iso14443_4a.h>
#include "../nfc_protocol_support_render_common.h"
void nfc_render_iso14443_4a_info(
const Iso14443_4aData* data,
NfcProtocolFormatType format_type,
FuriString* str);
void nfc_render_iso14443_4a_brief(const Iso14443_4aData* data, FuriString* str);
void nfc_render_iso14443_4a_extra(const Iso14443_4aData* data, FuriString* str);