NFC: Add Felica info scenes and properly clear felica_data

This commit is contained in:
nullableVoidPtr
2022-12-10 12:15:09 +11:00
parent 68b3cc233b
commit 6c7cd0960c
14 changed files with 654 additions and 113 deletions

View File

@@ -40,10 +40,23 @@
#include <m-array.h>
ARRAY_DEF(FelicaAreaPath, FelicaArea*, M_PTR_OPLIST);
ARRAY_DEF(FuriStringStack, FuriString*, M_PTR_OPLIST);
ARRAY_DEF(MfClassicUserKeys, char*, M_PTR_OPLIST);
#define NFC_TEXT_STORE_SIZE 128
typedef struct {
FuriStringStack_t strings;
FelicaSystem* selected_system;
FelicaAreaPath_t selected_areas;
FelicaService* selected_service;
} FelicaSelectState;
typedef enum {
NfcRpcStateIdle,
NfcRpcStateEmulating,
@@ -66,6 +79,7 @@ struct Nfc {
FuriString* text_box_store;
uint8_t byte_input_store[6];
MfClassicUserKeys_t mfc_key_strs; // Used in MFC key listing
FelicaSelectState felica_select;
void* rpc_ctx;
NfcRpcState rpc_state;

View File

@@ -46,6 +46,9 @@ ADD_SCENE(nfc, mf_classic_wrong_card, MfClassicWrongCard)
ADD_SCENE(nfc, emv_read_success, EmvReadSuccess)
ADD_SCENE(nfc, emv_menu, EmvMenu)
ADD_SCENE(nfc, felica_read_success, FelicaReadSuccess)
ADD_SCENE(nfc, felica_menu, FelicaMenu)
ADD_SCENE(nfc, felica_info_select, FelicaInfoSelect)
ADD_SCENE(nfc, felica_service_data, FelicaServiceData)
ADD_SCENE(nfc, emulate_apdu_sequence, EmulateApduSequence)
ADD_SCENE(nfc, device_info, DeviceInfo)
ADD_SCENE(nfc, delete, Delete)

View File

@@ -39,6 +39,8 @@ void nfc_scene_delete_on_enter(void* context) {
furi_string_set(temp_str, nfc_mf_classic_type(nfc->dev->dev_data.mf_classic_data.type));
} else if(protocol == NfcDeviceProtocolMifareDesfire) {
furi_string_set(temp_str, "MIFARE DESFire");
} else if(protocol == NfcDeviceProtocolFelica) {
furi_string_set(temp_str, "FeliCa");
} else {
furi_string_set(temp_str, "Unknown ISO tag");
}

View File

@@ -0,0 +1,124 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
#define SYSTEM_EVENT
void nfc_scene_felica_info_select_submenu_callback(void* context, uint32_t index) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
}
void nfc_scene_felica_info_select_on_enter(void* context) {
Nfc* nfc = context;
Submenu* submenu = nfc->submenu;
FelicaData* data = &nfc->dev->dev_data.felica_data;
FelicaSelectState* state = &nfc->felica_select;
FuriStringStack_init(nfc->felica_select.strings);
FelicaAreaPath_init(nfc->felica_select.selected_areas);
submenu_add_item(submenu, "[Actions]", 0, nfc_scene_felica_info_select_submenu_callback, nfc);
uint8_t i = 1;
if(state->selected_system == NULL) {
FelicaSystemList_it_t it;
for(FelicaSystemList_it(it, data->systems); !FelicaSystemList_end_p(it); FelicaSystemList_next(it)) {
FelicaSystem* current_system = *FelicaSystemList_ref(it);
FuriString* system_name = felica_get_system_name(current_system);
submenu_add_item(
submenu,
furi_string_get_cstr(system_name),
i++,
nfc_scene_felica_info_select_submenu_callback,
nfc);
FuriStringStack_push_back(state->strings, system_name);
}
} else if(FelicaAreaPath_size(state->selected_areas) == 0) {
FelicaArea* area_0 = &state->selected_system->root_area;
FelicaNodeList_it_t it;
for(FelicaNodeList_it(it, area_0->nodes); !FelicaNodeList_end_p(it); FelicaNodeList_next(it)) {
FelicaNode* node = *FelicaNodeList_ref(it);
if(node->type == FelicaNodeTypeArea) {
FuriString* area_name = furi_string_alloc_printf("Area %d", node->ptr.area->number);
submenu_add_item(
submenu, furi_string_get_cstr(area_name), i++, nfc_scene_felica_info_select_submenu_callback, nfc);
FuriStringStack_push_back(state->strings, area_name);
} else {
uint16_t service_code = node->ptr.service->number << 6;
FuriString* service_name = furi_string_alloc_printf("Service %04X", service_code);
submenu_add_item(
submenu, furi_string_get_cstr(service_name), i++, nfc_scene_felica_info_select_submenu_callback, nfc);
FuriStringStack_push_back(state->strings, service_name);
}
}
}
state->selected_service = NULL;
submenu_set_selected_item(
nfc->submenu, scene_manager_get_scene_state(nfc->scene_manager, NfcSceneFelicaInfoSelect));
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
}
bool nfc_scene_felica_info_select_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
FelicaData* data = &nfc->dev->dev_data.felica_data;
FelicaSelectState* state = &nfc->felica_select;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
uint8_t index = event.event;
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneMfUltralightMenu, index);
if(index == 0) {
return false;
}
index -= 1;
if(state->selected_system == NULL) {
state->selected_system = *FelicaSystemList_get(data->systems, index);
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaInfoSelect);
consumed = true;
} else {
FelicaNode* selected_node = NULL;
if (FelicaAreaPath_size(state->selected_areas) == 0) {
selected_node = *FelicaNodeList_get(state->selected_system->root_area.nodes, index);
} else {
FelicaArea* current_area = *FelicaAreaPath_back(state->selected_areas);
selected_node = *FelicaNodeList_get(current_area->nodes, index);
}
if(selected_node->type == FelicaNodeTypeArea) {
FelicaAreaPath_push_back(state->selected_areas, selected_node->ptr.area);
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaInfoSelect);
consumed = true;
} else if(selected_node->type == FelicaNodeTypeService) {
state->selected_service = selected_node->ptr.service;
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaServiceData);
consumed = true;
}
}
} else if(event.type == SceneManagerEventTypeBack) {
if(FelicaAreaPath_size(state->selected_areas) <= 1) {
FelicaAreaPath_clear(state->selected_areas);
state->selected_system = NULL;
} else {
FelicaAreaPath_pop_back(NULL, state->selected_areas);
}
consumed = scene_manager_previous_scene(nfc->scene_manager);
}
return consumed;
}
void nfc_scene_felica_info_select_on_exit(void* context) {
Nfc* nfc = context;
// Clear view
FelicaAreaPath_clear(nfc->felica_select.selected_areas);
FuriStringStack_it_t it;
for(FuriStringStack_it(it, nfc->felica_select.strings); !FuriStringStack_end_p(it); FuriStringStack_next(it)) {
furi_string_free(*FuriStringStack_ref(it));
}
submenu_reset(nfc->submenu);
}

View File

@@ -0,0 +1,84 @@
#include "../nfc_i.h"
#include <dolphin/dolphin.h>
enum SubmenuIndex {
/*
SubmenuIndexUnlock,
SubmenuIndexSave,
SubmenuIndexEmulate,
*/
SubmenuIndexInfo,
};
void nfc_scene_felica_menu_submenu_callback(void* context, uint32_t index) {
Nfc* nfc = context;
view_dispatcher_send_custom_event(nfc->view_dispatcher, index);
}
void nfc_scene_felica_menu_on_enter(void* context) {
Nfc* nfc = context;
Submenu* submenu = nfc->submenu;
// FelicaData* data = &nfc->dev->dev_data.felica_data;
/*
submenu_add_item(
submenu, "Unlock", SubmenuIndexUnlock, nfc_scene_felica_menu_submenu_callback, nfc);
submenu_add_item(
submenu, "Save", SubmenuIndexSave, nfc_scene_felica_menu_submenu_callback, nfc);
submenu_add_item(
submenu, "Emulate", SubmenuIndexEmulate, nfc_scene_felica_menu_submenu_callback, nfc);
*/
submenu_add_item(
submenu, "Info", SubmenuIndexInfo, nfc_scene_felica_menu_submenu_callback, nfc);
submenu_set_selected_item(
nfc->submenu, scene_manager_get_scene_state(nfc->scene_manager, NfcSceneFelicaMenu));
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewMenu);
}
bool nfc_scene_felica_menu_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
/*
if(event.event == SubmenuIndexSave) {
nfc->dev->format = NfcDeviceSaveFormatFelica;
// Clear device name
nfc_device_set_name(nfc->dev, "");
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveName);
consumed = true;
} else if(event.event == SubmenuIndexEmulate) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaEmulate);
if(scene_manager_has_previous_scene(nfc->scene_manager, NfcSceneSetType)) {
DOLPHIN_DEED(DolphinDeedNfcAddEmulate);
} else {
DOLPHIN_DEED(DolphinDeedNfcEmulate);
}
consumed = true;
} else if(event.event == SubmenuIndexUnlock) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaUnlockMenu);
consumed = true;
} else
*/
if(event.event == SubmenuIndexInfo) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaInfoSelect);
consumed = true;
}
scene_manager_set_scene_state(nfc->scene_manager, NfcSceneFelicaMenu, event.event);
} else if(event.type == SceneManagerEventTypeBack) {
consumed = scene_manager_previous_scene(nfc->scene_manager);
}
return consumed;
}
void nfc_scene_felica_menu_on_exit(void* context) {
Nfc* nfc = context;
// Clear view
submenu_reset(nfc->submenu);
}

View File

@@ -20,6 +20,8 @@ void nfc_scene_felica_read_success_on_enter(void* context) {
Widget* widget = nfc->widget;
widget_add_button_element(
widget, GuiButtonTypeLeft, "Retry", nfc_scene_felica_read_success_widget_callback, nfc);
widget_add_button_element(
widget, GuiButtonTypeRight, "More", nfc_scene_felica_read_success_widget_callback, nfc);
FuriString* temp_str = NULL;
if(furi_string_size(nfc->dev->dev_data.parsed_data)) {
@@ -27,8 +29,9 @@ void nfc_scene_felica_read_success_on_enter(void* context) {
} else {
temp_str = furi_string_alloc_printf("\e#%s", nfc_felica_type(felica_data->type));
FelicaSystem* current_system = felica_data->systems;
while(current_system) {
FelicaSystemList_it_t it;
for(FelicaSystemList_it(it, felica_data->systems); !FelicaSystemList_end_p(it); FelicaSystemList_next(it)) {
FelicaSystem* current_system = *FelicaSystemList_ref(it);
furi_string_cat_printf(
temp_str, "\nSystem %04X (#%d):", current_system->code, current_system->number);
furi_string_cat_printf(temp_str, "\nIDm:\n ");
@@ -39,8 +42,6 @@ void nfc_scene_felica_read_success_on_enter(void* context) {
for(size_t i = 0; i < 8; i++) {
furi_string_cat_printf(temp_str, "%02X", current_system->pmm[i]);
}
current_system = current_system->next;
}
}
@@ -61,7 +62,7 @@ bool nfc_scene_felica_read_success_on_event(void* context, SceneManagerEvent eve
scene_manager_next_scene(nfc->scene_manager, NfcSceneRetryConfirm);
consumed = true;
} else if(event.event == GuiButtonTypeRight) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneNfcaMenu);
scene_manager_next_scene(nfc->scene_manager, NfcSceneFelicaMenu);
consumed = true;
}
} else if(event.type == SceneManagerEventTypeBack) {

View File

@@ -0,0 +1,143 @@
#include "../nfc_i.h"
void nfc_scene_felica_service_data_on_enter(void* context) {
Nfc* nfc = context;
FelicaSelectState* select_state = &nfc->felica_select;
FelicaSystem* system = select_state->selected_system;
FelicaService* service = select_state->selected_service;
TextBox* text_box = nfc->text_box;
if(system->code == LITE_SYSTEM_CODE && service->number == 0) {
text_box_set_font(text_box, TextBoxFontHex);
furi_string_cat_str(nfc->text_box_store, "S_PAD:\n");
for(int i = 0; i < REG_LITE_BLOCK; i++) {
FelicaBlock* block = *FelicaBlockList_cget(service->blocks, i);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
if(block != NULL) {
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
} else {
furi_string_cat_printf(nfc->text_box_store, "???? ");
}
}
}
furi_string_cat_str(nfc->text_box_store, "REG:\n");
FelicaBlock* block = *FelicaBlockList_cget(service->blocks, REG_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
if(block != NULL) {
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
} else {
furi_string_cat_printf(nfc->text_box_store, "???? ");
}
}
furi_string_cat_str(nfc->text_box_store, "RC:\n");
block = *FelicaBlockList_cget(service->blocks, RC_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "MAC:\n");
block = *FelicaBlockList_cget(service->blocks, MAC_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "ID:\n");
block = *FelicaBlockList_cget(service->blocks, ID_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "D_ID:\n");
block = *FelicaBlockList_cget(service->blocks, DEVICE_ID_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "CKV:\n");
block = *FelicaBlockList_cget(service->blocks, CARD_KEY_VER_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "MC:\n");
block = *FelicaBlockList_cget(service->blocks, MEM_CONFIG_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "WCNT:\n");
block = *FelicaBlockList_cget(service->blocks, WRITE_COUNT_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "MAC_A:\n");
block = *FelicaBlockList_cget(service->blocks, MAC_A_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
furi_string_cat_str(nfc->text_box_store, "CRC_CHECK:\n");
block = *FelicaBlockList_cget(service->blocks, CRC_CHECK_LITE_BLOCK);
for(uint16_t i = 0; i < FELICA_BLOCK_SIZE; i += 2) {
if(!(i % 8) && i) {
furi_string_push_back(nfc->text_box_store, '\n');
}
furi_string_cat_printf(nfc->text_box_store, "%02X%02X ", block->data[i], block->data[i + 1]);
}
}
text_box_set_text(text_box, furi_string_get_cstr(nfc->text_box_store));
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextBox);
}
bool nfc_scene_felica_service_data_on_event(void* context, SceneManagerEvent event) {
Nfc* nfc = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeBack) {
consumed = scene_manager_previous_scene(nfc->scene_manager);
}
return consumed;
}
void nfc_scene_felica_service_data_on_exit(void* context) {
Nfc* nfc = context;
// Clean view
text_box_reset(nfc->text_box);
furi_string_reset(nfc->text_box_store);
}