Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/gornek/nfc_refactoring_v1' into …
Browse files Browse the repository at this point in the history
…gsurkov/3594_nfc_refactoring_docs
  • Loading branch information
gornekich committed Oct 16, 2023
2 parents 81d55d9 + a966c33 commit cfc6093
Show file tree
Hide file tree
Showing 21 changed files with 1,017 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ void nfc_render_iso14443_4a_extra(const Iso14443_4aData* data, FuriString* str)
}
}

furi_string_cat(str, "\n\e#ISO14444-3A data");
furi_string_cat(str, "\n\e#ISO14443-3A data");
nfc_render_iso14443_3a_extra(iso14443_4a_get_base_data(data), str);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ void nfc_render_mf_desfire_info(

if(format_type != NfcProtocolFormatTypeFull) return;

furi_string_cat(str, "\n\e#ISO144443-4 data");
furi_string_cat(str, "\n\e#ISO14443-4 data");
nfc_render_iso14443_4a_extra(mf_desfire_get_base_data(data), str);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "mf_classic/mf_classic.h"
#include "mf_desfire/mf_desfire.h"
#include "slix/slix.h"
#include "st25tb/st25tb.h"

/**
* @brief Array of pointers to concrete protocol support implementations.
Expand All @@ -39,5 +40,6 @@ const NfcProtocolSupportBase* nfc_protocol_support[NfcProtocolNum] = {
[NfcProtocolMfClassic] = &nfc_protocol_support_mf_classic,
[NfcProtocolMfDesfire] = &nfc_protocol_support_mf_desfire,
[NfcProtocolSlix] = &nfc_protocol_support_slix,
[NfcProtocolSt25tb] = &nfc_protocol_support_st25tb,
/* Add new protocol support implementations here */
};
110 changes: 110 additions & 0 deletions applications/main/nfc/helpers/protocol_support/st25tb/st25tb.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <nfc/protocols/nfc_protocol.h>
#include <nfc/protocols/st25tb/st25tb_poller.h>

#include "nfc/nfc_app_i.h"
#include "st25tb_render.h"
#include "../nfc_protocol_support_base.h"
#include "../nfc_protocol_support_gui_common.h"

static void nfc_scene_info_on_enter_st25tb(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const St25tbData* data = nfc_device_get_data(device, NfcProtocolSt25tb);

FuriString* temp_str = furi_string_alloc();
furi_string_cat_printf(
temp_str, "\e#%s\n", nfc_device_get_name(device, NfcDeviceNameTypeFull));
nfc_render_st25tb_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_st25tb(NfcGenericEvent event, void* context) {
furi_assert(event.protocol == NfcProtocolSt25tb);

NfcApp* instance = context;
const St25tbPollerEvent* st25tb_event = event.event_data;

if(st25tb_event->type == St25tbPollerEventTypeReady) {
nfc_device_set_data(
instance->nfc_device, NfcProtocolSt25tb, 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_st25tb(NfcApp* instance) {
nfc_poller_start(instance->poller, nfc_scene_read_poller_callback_st25tb, instance);
}

static void nfc_scene_read_success_on_enter_st25tb(NfcApp* instance) {
const NfcDevice* device = instance->nfc_device;
const St25tbData* data = nfc_device_get_data(device, NfcProtocolSt25tb);

FuriString* temp_str = furi_string_alloc();
furi_string_cat_printf(
temp_str, "\e#%s\n", nfc_device_get_name(device, NfcDeviceNameTypeFull));
nfc_render_st25tb_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 bool nfc_scene_info_on_event_st25tb(NfcApp* instance, uint32_t event) {
if(event == GuiButtonTypeRight) {
scene_manager_next_scene(instance->scene_manager, NfcSceneNotImplemented);
return true;
}

return false;
}

static bool nfc_scene_saved_menu_on_event_st25tb(NfcApp* instance, uint32_t event) {
if(event == SubmenuIndexCommonEdit) {
scene_manager_next_scene(instance->scene_manager, NfcSceneSetUid);
return true;
}

return false;
}

const NfcProtocolSupportBase nfc_protocol_support_st25tb = {
.features = NfcProtocolFeatureNone, // TODO: Implement better UID editing,

.scene_info =
{
.on_enter = nfc_scene_info_on_enter_st25tb,
.on_event = nfc_scene_info_on_event_st25tb,
},
.scene_read =
{
.on_enter = nfc_scene_read_on_enter_st25tb,
.on_event = NULL,
},
.scene_read_menu =
{
.on_enter = nfc_protocol_support_common_on_enter_empty,
.on_event = nfc_protocol_support_common_on_event_empty,
},
.scene_read_success =
{
.on_enter = nfc_scene_read_success_on_enter_st25tb,
.on_event = NULL,
},
.scene_saved_menu =
{
.on_enter = nfc_protocol_support_common_on_enter_empty,
.on_event = nfc_scene_saved_menu_on_event_st25tb,
},
.scene_emulate =
{
.on_enter = NULL,
.on_event = NULL,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#include "../nfc_protocol_support_base.h"

extern const NfcProtocolSupportBase nfc_protocol_support_st25tb;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "st25tb_render.h"
#include <nfc/protocols/st25tb/st25tb.h>

void nfc_render_st25tb_info(
const St25tbData* data,
NfcProtocolFormatType format_type,
FuriString* str) {
furi_string_cat_printf(str, "UID");

for(size_t i = 0; i < ST25TB_UID_SIZE; i++) {
furi_string_cat_printf(str, " %02X", data->uid[i]);
}

if(format_type == NfcProtocolFormatTypeFull) {
furi_string_cat_printf(str, "\nSys. OTP: %08lX", data->system_otp_block);
furi_string_cat_printf(str, "\nBlocks:");
for(size_t i = 0; i < st25tb_get_block_count(data->type); i += 2) {
furi_string_cat_printf(
str, "\n %02X %08lX %08lX", i, data->blocks[i], data->blocks[i + 1]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once

#include <nfc/protocols/st25tb/st25tb.h>

#include "../nfc_protocol_support_render_common.h"

void nfc_render_st25tb_info(
const St25tbData* data,
NfcProtocolFormatType format_type,
FuriString* str);
16 changes: 16 additions & 0 deletions firmware/targets/f7/api_symbols.csv
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ Header,+,lib/nfc/protocols/mf_ultralight/mf_ultralight_listener.h,,
Header,+,lib/nfc/protocols/mf_ultralight/mf_ultralight_poller.h,,
Header,+,lib/nfc/protocols/mf_ultralight/mf_ultralight_poller_sync_api.h,,
Header,+,lib/nfc/protocols/slix/slix.h,,
Header,+,lib/nfc/protocols/st25tb/st25tb.h,,
Header,+,lib/nfc/protocols/st25tb/st25tb_poller.h,,
Header,+,lib/one_wire/maxim_crc.h,,
Header,+,lib/one_wire/one_wire_host.h,,
Header,+,lib/one_wire/one_wire_slave.h,,
Expand Down Expand Up @@ -2611,6 +2613,19 @@ Function,+,srand,void,unsigned
Function,-,srand48,void,long
Function,-,srandom,void,unsigned
Function,+,sscanf,int,"const char*, const char*, ..."
Function,+,st25tb_alloc,St25tbData*,
Function,+,st25tb_copy,void,"St25tbData*, const St25tbData*"
Function,+,st25tb_free,void,St25tbData*
Function,+,st25tb_get_base_data,St25tbData*,const St25tbData*
Function,+,st25tb_get_block_count,uint8_t,St25tbType
Function,+,st25tb_get_device_name,const char*,"const St25tbData*, NfcDeviceNameType"
Function,+,st25tb_get_uid,const uint8_t*,"const St25tbData*, size_t*"
Function,+,st25tb_is_equal,_Bool,"const St25tbData*, const St25tbData*"
Function,+,st25tb_load,_Bool,"St25tbData*, FlipperFormat*, uint32_t"
Function,+,st25tb_reset,void,St25tbData*
Function,+,st25tb_save,_Bool,"const St25tbData*, FlipperFormat*"
Function,+,st25tb_set_uid,_Bool,"St25tbData*, const uint8_t*, size_t"
Function,+,st25tb_verify,_Bool,"St25tbData*, const FuriString*"
Function,+,storage_common_copy,FS_Error,"Storage*, const char*, const char*"
Function,+,storage_common_exists,_Bool,"Storage*, const char*"
Function,+,storage_common_fs_info,FS_Error,"Storage*, const char*, uint64_t*, uint64_t*"
Expand Down Expand Up @@ -3444,6 +3459,7 @@ Variable,+,message_vibro_on,const NotificationMessage,
Variable,-,nfc_device_mf_classic,const NfcDeviceBase,
Variable,-,nfc_device_mf_desfire,const NfcDeviceBase,
Variable,-,nfc_device_mf_ultralight,const NfcDeviceBase,
Variable,-,nfc_device_st25tb,const NfcDeviceBase,
Variable,+,sequence_audiovisual_alert,const NotificationSequence,
Variable,+,sequence_blink_blue_10,const NotificationSequence,
Variable,+,sequence_blink_blue_100,const NotificationSequence,
Expand Down
2 changes: 2 additions & 0 deletions lib/nfc/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ env.Append(
File("protocols/mf_classic/mf_classic.h"),
File("protocols/mf_desfire/mf_desfire.h"),
File("protocols/slix/slix.h"),
File("protocols/st25tb/st25tb.h"),
# Pollers
File("protocols/iso14443_3a/iso14443_3a_poller.h"),
File("protocols/iso14443_3b/iso14443_3b_poller.h"),
Expand All @@ -28,6 +29,7 @@ env.Append(
File("protocols/mf_ultralight/mf_ultralight_poller.h"),
File("protocols/mf_classic/mf_classic_poller.h"),
File("protocols/mf_desfire/mf_desfire_poller.h"),
File("protocols/st25tb/st25tb_poller.h"),
# Listeners
File("protocols/iso14443_3a/iso14443_3a_listener.h"),
File("protocols/iso14443_4a/iso14443_4a_listener.h"),
Expand Down
2 changes: 2 additions & 0 deletions lib/nfc/protocols/nfc_device_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <nfc/protocols/mf_classic/mf_classic.h>
#include <nfc/protocols/mf_desfire/mf_desfire.h>
#include <nfc/protocols/slix/slix_device_defs.h>
#include <nfc/protocols/st25tb/st25tb.h>

/**
* @brief List of registered NFC device implementations.
Expand All @@ -40,5 +41,6 @@ const NfcDeviceBase* nfc_devices[NfcProtocolNum] = {
[NfcProtocolMfClassic] = &nfc_device_mf_classic,
[NfcProtocolMfDesfire] = &nfc_device_mf_desfire,
[NfcProtocolSlix] = &nfc_device_slix,
[NfcProtocolSt25tb] = &nfc_device_st25tb,
/* Add new protocols here */
};
1 change: 1 addition & 0 deletions lib/nfc/protocols/nfc_listener_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ const NfcListenerBase* nfc_listeners_api[NfcProtocolNum] = {
[NfcProtocolMfClassic] = &mf_classic_listener,
[NfcProtocolMfDesfire] = NULL,
[NfcProtocolSlix] = &nfc_listener_slix,
[NfcProtocolSt25tb] = NULL,
};
2 changes: 2 additions & 0 deletions lib/nfc/protocols/nfc_poller_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <nfc/protocols/mf_classic/mf_classic_poller_defs.h>
#include <nfc/protocols/mf_desfire/mf_desfire_poller_defs.h>
#include <nfc/protocols/slix/slix_poller_defs.h>
#include <nfc/protocols/st25tb/st25tb_poller_defs.h>

const NfcPollerBase* nfc_pollers_api[NfcProtocolNum] = {
[NfcProtocolIso14443_3a] = &nfc_poller_iso14443_3a,
Expand All @@ -23,4 +24,5 @@ const NfcPollerBase* nfc_pollers_api[NfcProtocolNum] = {
[NfcProtocolMfDesfire] = &mf_desfire_poller,
[NfcProtocolSlix] = &nfc_poller_slix,
/* Add new pollers here */
[NfcProtocolSt25tb] = &nfc_poller_st25tb,
};
12 changes: 9 additions & 3 deletions lib/nfc/protocols/nfc_protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
*
* (Start)
* |
* +------------------------+-----------+---------+
* | | | |
* ISO14443-3A ISO14443-3B Felica ISO15693-3
* +------------------------+-----------+---------+------------+
* | | | | |
* ISO14443-3A ISO14443-3B Felica ISO15693-3 ST25TB
* | | |
* +---------------+-------------+ ISO14443-4B SLIX
* | | |
Expand Down Expand Up @@ -140,6 +140,12 @@ static const NfcProtocolTreeNode nfc_protocol_nodes[NfcProtocolNum] = {
.children_num = 0,
.children_protocol = NULL,
},
[NfcProtocolSt25tb] =
{
.parent_protocol = NfcProtocolInvalid,
.children_num = 0,
.children_protocol = NULL,
},
/* Add new protocols here */
};

Expand Down
1 change: 1 addition & 0 deletions lib/nfc/protocols/nfc_protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ typedef enum {
NfcProtocolMfClassic,
NfcProtocolMfDesfire,
NfcProtocolSlix,
NfcProtocolSt25tb,
/* Add new protocols here */

NfcProtocolNum, /**< Special value representing the number of available protocols. */
Expand Down
Loading

0 comments on commit cfc6093

Please sign in to comment.