NFC magic cards support (#1966)

* nfc magic: introduce nfc app to work with magic cards
* nfc: add nfc device functions to API
* nfc magic: add bacis scenes
* nfc magic: add wrong card and write confirm scenes
* nfc magic: introduce magic lib
* nfc magic: write magic lib
* nfc magic: add write commands to magic lib
* nfc magic: work on worker
* furi_hal_nfc: add bits data exchage method to API
* nfc magic: rework with new API
* nfc magic: add check and wipe scenes
* nfc magic: add icons, gui fixes
* nfc: format python src

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2022-11-04 11:01:44 +04:00
committed by GitHub
parent 3bd74b7f01
commit bf8fd71c00
35 changed files with 1693 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
App(
appid="nfc_magic",
name="Nfc Magic",
apptype=FlipperAppType.EXTERNAL,
entry_point="nfc_magic_app",
requires=[
"storage",
"gui",
],
stack_size=4 * 1024,
order=30,
fap_icon="../../../assets/icons/Archive/125_10px.png",
fap_category="Tools",
fap_private_libs=[
Lib(
name="magic",
),
],
fap_icon_assets="assets",
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

View File

@@ -0,0 +1,214 @@
#include "magic.h"
#include <furi_hal_nfc.h>
#define TAG "Magic"
#define MAGIC_CMD_WUPA (0x40)
#define MAGIC_CMD_WIPE (0x41)
#define MAGIC_CMD_READ (0x43)
#define MAGIC_CMD_WRITE (0x43)
#define MAGIC_MIFARE_READ_CMD (0x30)
#define MAGIC_MIFARE_WRITE_CMD (0xA0)
#define MAGIC_ACK (0x0A)
#define MAGIC_BUFFER_SIZE (32)
bool magic_wupa() {
bool magic_activated = false;
uint8_t tx_data[MAGIC_BUFFER_SIZE] = {};
uint8_t rx_data[MAGIC_BUFFER_SIZE] = {};
uint16_t rx_len = 0;
FuriHalNfcReturn ret = 0;
do {
// Setup nfc poller
furi_hal_nfc_exit_sleep();
furi_hal_nfc_ll_txrx_on();
furi_hal_nfc_ll_poll();
ret = furi_hal_nfc_ll_set_mode(
FuriHalNfcModePollNfca, FuriHalNfcBitrate106, FuriHalNfcBitrate106);
if(ret != FuriHalNfcReturnOk) break;
furi_hal_nfc_ll_set_fdt_listen(FURI_HAL_NFC_LL_FDT_LISTEN_NFCA_POLLER);
furi_hal_nfc_ll_set_fdt_poll(FURI_HAL_NFC_LL_FDT_POLL_NFCA_POLLER);
furi_hal_nfc_ll_set_error_handling(FuriHalNfcErrorHandlingNfc);
furi_hal_nfc_ll_set_guard_time(FURI_HAL_NFC_LL_GT_NFCA);
// Start communication
tx_data[0] = MAGIC_CMD_WUPA;
ret = furi_hal_nfc_ll_txrx_bits(
tx_data,
7,
rx_data,
sizeof(rx_data),
&rx_len,
FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_TX_MANUAL | FURI_HAL_NFC_LL_TXRX_FLAGS_AGC_ON |
FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_RX_KEEP,
furi_hal_nfc_ll_ms2fc(20));
if(ret != FuriHalNfcReturnIncompleteByte) break;
if(rx_len != 4) break;
if(rx_data[0] != MAGIC_ACK) break;
magic_activated = true;
} while(false);
if(!magic_activated) {
furi_hal_nfc_ll_txrx_off();
furi_hal_nfc_start_sleep();
}
return magic_activated;
}
bool magic_data_access_cmd() {
bool write_cmd_success = false;
uint8_t tx_data[MAGIC_BUFFER_SIZE] = {};
uint8_t rx_data[MAGIC_BUFFER_SIZE] = {};
uint16_t rx_len = 0;
FuriHalNfcReturn ret = 0;
do {
tx_data[0] = MAGIC_CMD_WRITE;
ret = furi_hal_nfc_ll_txrx_bits(
tx_data,
8,
rx_data,
sizeof(rx_data),
&rx_len,
FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_TX_MANUAL | FURI_HAL_NFC_LL_TXRX_FLAGS_AGC_ON |
FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_RX_KEEP,
furi_hal_nfc_ll_ms2fc(20));
if(ret != FuriHalNfcReturnIncompleteByte) break;
if(rx_len != 4) break;
if(rx_data[0] != MAGIC_ACK) break;
write_cmd_success = true;
} while(false);
if(!write_cmd_success) {
furi_hal_nfc_ll_txrx_off();
furi_hal_nfc_start_sleep();
}
return write_cmd_success;
}
bool magic_read_block(uint8_t block_num, MfClassicBlock* data) {
furi_assert(data);
bool read_success = false;
uint8_t tx_data[MAGIC_BUFFER_SIZE] = {};
uint8_t rx_data[MAGIC_BUFFER_SIZE] = {};
uint16_t rx_len = 0;
FuriHalNfcReturn ret = 0;
do {
tx_data[0] = MAGIC_MIFARE_READ_CMD;
tx_data[1] = block_num;
ret = furi_hal_nfc_ll_txrx_bits(
tx_data,
2 * 8,
rx_data,
sizeof(rx_data),
&rx_len,
FURI_HAL_NFC_LL_TXRX_FLAGS_AGC_ON,
furi_hal_nfc_ll_ms2fc(20));
if(ret != FuriHalNfcReturnOk) break;
if(rx_len != 16 * 8) break;
memcpy(data->value, rx_data, sizeof(data->value));
read_success = true;
} while(false);
if(!read_success) {
furi_hal_nfc_ll_txrx_off();
furi_hal_nfc_start_sleep();
}
return read_success;
}
bool magic_write_blk(uint8_t block_num, MfClassicBlock* data) {
furi_assert(data);
bool write_success = false;
uint8_t tx_data[MAGIC_BUFFER_SIZE] = {};
uint8_t rx_data[MAGIC_BUFFER_SIZE] = {};
uint16_t rx_len = 0;
FuriHalNfcReturn ret = 0;
do {
tx_data[0] = MAGIC_MIFARE_WRITE_CMD;
tx_data[1] = block_num;
ret = furi_hal_nfc_ll_txrx_bits(
tx_data,
2 * 8,
rx_data,
sizeof(rx_data),
&rx_len,
FURI_HAL_NFC_LL_TXRX_FLAGS_AGC_ON | FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_RX_KEEP,
furi_hal_nfc_ll_ms2fc(20));
if(ret != FuriHalNfcReturnIncompleteByte) break;
if(rx_len != 4) break;
if(rx_data[0] != MAGIC_ACK) break;
memcpy(tx_data, data->value, sizeof(data->value));
ret = furi_hal_nfc_ll_txrx_bits(
tx_data,
16 * 8,
rx_data,
sizeof(rx_data),
&rx_len,
FURI_HAL_NFC_LL_TXRX_FLAGS_AGC_ON | FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_RX_KEEP,
furi_hal_nfc_ll_ms2fc(20));
if(ret != FuriHalNfcReturnIncompleteByte) break;
if(rx_len != 4) break;
if(rx_data[0] != MAGIC_ACK) break;
write_success = true;
} while(false);
if(!write_success) {
furi_hal_nfc_ll_txrx_off();
furi_hal_nfc_start_sleep();
}
return write_success;
}
bool magic_wipe() {
bool wipe_success = false;
uint8_t tx_data[MAGIC_BUFFER_SIZE] = {};
uint8_t rx_data[MAGIC_BUFFER_SIZE] = {};
uint16_t rx_len = 0;
FuriHalNfcReturn ret = 0;
do {
tx_data[0] = MAGIC_CMD_WIPE;
ret = furi_hal_nfc_ll_txrx_bits(
tx_data,
8,
rx_data,
sizeof(rx_data),
&rx_len,
FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_TX_MANUAL | FURI_HAL_NFC_LL_TXRX_FLAGS_AGC_ON |
FURI_HAL_NFC_LL_TXRX_FLAGS_CRC_RX_KEEP,
furi_hal_nfc_ll_ms2fc(2000));
if(ret != FuriHalNfcReturnIncompleteByte) break;
if(rx_len != 4) break;
if(rx_data[0] != MAGIC_ACK) break;
wipe_success = true;
} while(false);
return wipe_success;
}
void magic_deactivate() {
furi_hal_nfc_ll_txrx_off();
furi_hal_nfc_start_sleep();
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <lib/nfc/protocols/mifare_classic.h>
bool magic_wupa();
bool magic_read_block(uint8_t block_num, MfClassicBlock* data);
bool magic_data_access_cmd();
bool magic_write_blk(uint8_t block_num, MfClassicBlock* data);
bool magic_wipe();
void magic_deactivate();

View File

@@ -0,0 +1,169 @@
#include "nfc_magic_i.h"
bool nfc_magic_custom_event_callback(void* context, uint32_t event) {
furi_assert(context);
NfcMagic* nfc_magic = context;
return scene_manager_handle_custom_event(nfc_magic->scene_manager, event);
}
bool nfc_magic_back_event_callback(void* context) {
furi_assert(context);
NfcMagic* nfc_magic = context;
return scene_manager_handle_back_event(nfc_magic->scene_manager);
}
void nfc_magic_tick_event_callback(void* context) {
furi_assert(context);
NfcMagic* nfc_magic = context;
scene_manager_handle_tick_event(nfc_magic->scene_manager);
}
void nfc_magic_show_loading_popup(void* context, bool show) {
NfcMagic* nfc_magic = context;
TaskHandle_t timer_task = xTaskGetHandle(configTIMER_SERVICE_TASK_NAME);
if(show) {
// Raise timer priority so that animations can play
vTaskPrioritySet(timer_task, configMAX_PRIORITIES - 1);
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewLoading);
} else {
// Restore default timer priority
vTaskPrioritySet(timer_task, configTIMER_TASK_PRIORITY);
}
}
NfcMagic* nfc_magic_alloc() {
NfcMagic* nfc_magic = malloc(sizeof(NfcMagic));
nfc_magic->worker = nfc_magic_worker_alloc();
nfc_magic->view_dispatcher = view_dispatcher_alloc();
nfc_magic->scene_manager = scene_manager_alloc(&nfc_magic_scene_handlers, nfc_magic);
view_dispatcher_enable_queue(nfc_magic->view_dispatcher);
view_dispatcher_set_event_callback_context(nfc_magic->view_dispatcher, nfc_magic);
view_dispatcher_set_custom_event_callback(
nfc_magic->view_dispatcher, nfc_magic_custom_event_callback);
view_dispatcher_set_navigation_event_callback(
nfc_magic->view_dispatcher, nfc_magic_back_event_callback);
view_dispatcher_set_tick_event_callback(
nfc_magic->view_dispatcher, nfc_magic_tick_event_callback, 100);
// Nfc device
nfc_magic->nfc_dev = nfc_device_alloc();
// Open GUI record
nfc_magic->gui = furi_record_open(RECORD_GUI);
view_dispatcher_attach_to_gui(
nfc_magic->view_dispatcher, nfc_magic->gui, ViewDispatcherTypeFullscreen);
// Open Notification record
nfc_magic->notifications = furi_record_open(RECORD_NOTIFICATION);
// Submenu
nfc_magic->submenu = submenu_alloc();
view_dispatcher_add_view(
nfc_magic->view_dispatcher, NfcMagicViewMenu, submenu_get_view(nfc_magic->submenu));
// Popup
nfc_magic->popup = popup_alloc();
view_dispatcher_add_view(
nfc_magic->view_dispatcher, NfcMagicViewPopup, popup_get_view(nfc_magic->popup));
// Loading
nfc_magic->loading = loading_alloc();
view_dispatcher_add_view(
nfc_magic->view_dispatcher, NfcMagicViewLoading, loading_get_view(nfc_magic->loading));
// Text Input
nfc_magic->text_input = text_input_alloc();
view_dispatcher_add_view(
nfc_magic->view_dispatcher,
NfcMagicViewTextInput,
text_input_get_view(nfc_magic->text_input));
// Custom Widget
nfc_magic->widget = widget_alloc();
view_dispatcher_add_view(
nfc_magic->view_dispatcher, NfcMagicViewWidget, widget_get_view(nfc_magic->widget));
return nfc_magic;
}
void nfc_magic_free(NfcMagic* nfc_magic) {
furi_assert(nfc_magic);
// Nfc device
nfc_device_free(nfc_magic->nfc_dev);
// Submenu
view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewMenu);
submenu_free(nfc_magic->submenu);
// Popup
view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
popup_free(nfc_magic->popup);
// Loading
view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewLoading);
loading_free(nfc_magic->loading);
// TextInput
view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewTextInput);
text_input_free(nfc_magic->text_input);
// Custom Widget
view_dispatcher_remove_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
widget_free(nfc_magic->widget);
// Worker
nfc_magic_worker_stop(nfc_magic->worker);
nfc_magic_worker_free(nfc_magic->worker);
// View Dispatcher
view_dispatcher_free(nfc_magic->view_dispatcher);
// Scene Manager
scene_manager_free(nfc_magic->scene_manager);
// GUI
furi_record_close(RECORD_GUI);
nfc_magic->gui = NULL;
// Notifications
furi_record_close(RECORD_NOTIFICATION);
nfc_magic->notifications = NULL;
free(nfc_magic);
}
static const NotificationSequence nfc_magic_sequence_blink_start_blue = {
&message_blink_start_10,
&message_blink_set_color_blue,
&message_do_not_reset,
NULL,
};
static const NotificationSequence nfc_magic_sequence_blink_stop = {
&message_blink_stop,
NULL,
};
void nfc_magic_blink_start(NfcMagic* nfc_magic) {
notification_message(nfc_magic->notifications, &nfc_magic_sequence_blink_start_blue);
}
void nfc_magic_blink_stop(NfcMagic* nfc_magic) {
notification_message(nfc_magic->notifications, &nfc_magic_sequence_blink_stop);
}
int32_t nfc_magic_app(void* p) {
UNUSED(p);
NfcMagic* nfc_magic = nfc_magic_alloc();
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneStart);
view_dispatcher_run(nfc_magic->view_dispatcher);
nfc_magic_free(nfc_magic);
return 0;
}

View File

@@ -0,0 +1,3 @@
#pragma once
typedef struct NfcMagic NfcMagic;

View File

@@ -0,0 +1,77 @@
#pragma once
#include "nfc_magic.h"
#include "nfc_magic_worker.h"
#include "lib/magic/magic.h"
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <notification/notification_messages.h>
#include <gui/modules/submenu.h>
#include <gui/modules/popup.h>
#include <gui/modules/loading.h>
#include <gui/modules/text_input.h>
#include <gui/modules/widget.h>
#include <input/input.h>
#include "scenes/nfc_magic_scene.h"
#include <storage/storage.h>
#include <lib/toolbox/path.h>
#include <lib/nfc/nfc_device.h>
#include "nfc_magic_icons.h"
enum NfcMagicCustomEvent {
// Reserve first 100 events for button types and indexes, starting from 0
NfcMagicCustomEventReserved = 100,
NfcMagicCustomEventViewExit,
NfcMagicCustomEventWorkerExit,
NfcMagicCustomEventByteInputDone,
NfcMagicCustomEventTextInputDone,
};
struct NfcMagic {
NfcMagicWorker* worker;
ViewDispatcher* view_dispatcher;
Gui* gui;
NotificationApp* notifications;
SceneManager* scene_manager;
// NfcMagicDevice* dev;
NfcDevice* nfc_dev;
FuriString* text_box_store;
// Common Views
Submenu* submenu;
Popup* popup;
Loading* loading;
TextInput* text_input;
Widget* widget;
};
typedef enum {
NfcMagicViewMenu,
NfcMagicViewPopup,
NfcMagicViewLoading,
NfcMagicViewTextInput,
NfcMagicViewWidget,
} NfcMagicView;
NfcMagic* nfc_magic_alloc();
void nfc_magic_text_store_set(NfcMagic* nfc_magic, const char* text, ...);
void nfc_magic_text_store_clear(NfcMagic* nfc_magic);
void nfc_magic_blink_start(NfcMagic* nfc_magic);
void nfc_magic_blink_stop(NfcMagic* nfc_magic);
void nfc_magic_show_loading_popup(void* context, bool show);

View File

@@ -0,0 +1,174 @@
#include "nfc_magic_worker_i.h"
#include "lib/magic/magic.h"
#define TAG "NfcMagicWorker"
static void
nfc_magic_worker_change_state(NfcMagicWorker* nfc_magic_worker, NfcMagicWorkerState state) {
furi_assert(nfc_magic_worker);
nfc_magic_worker->state = state;
}
NfcMagicWorker* nfc_magic_worker_alloc() {
NfcMagicWorker* nfc_magic_worker = malloc(sizeof(NfcMagicWorker));
// Worker thread attributes
nfc_magic_worker->thread = furi_thread_alloc();
furi_thread_set_name(nfc_magic_worker->thread, "NfcMagicWorker");
furi_thread_set_stack_size(nfc_magic_worker->thread, 8192);
furi_thread_set_callback(nfc_magic_worker->thread, nfc_magic_worker_task);
furi_thread_set_context(nfc_magic_worker->thread, nfc_magic_worker);
nfc_magic_worker->callback = NULL;
nfc_magic_worker->context = NULL;
nfc_magic_worker_change_state(nfc_magic_worker, NfcMagicWorkerStateReady);
return nfc_magic_worker;
}
void nfc_magic_worker_free(NfcMagicWorker* nfc_magic_worker) {
furi_assert(nfc_magic_worker);
furi_thread_free(nfc_magic_worker->thread);
free(nfc_magic_worker);
}
void nfc_magic_worker_stop(NfcMagicWorker* nfc_magic_worker) {
furi_assert(nfc_magic_worker);
nfc_magic_worker_change_state(nfc_magic_worker, NfcMagicWorkerStateStop);
furi_thread_join(nfc_magic_worker->thread);
}
void nfc_magic_worker_start(
NfcMagicWorker* nfc_magic_worker,
NfcMagicWorkerState state,
NfcDeviceData* dev_data,
NfcMagicWorkerCallback callback,
void* context) {
furi_assert(nfc_magic_worker);
furi_assert(dev_data);
nfc_magic_worker->callback = callback;
nfc_magic_worker->context = context;
nfc_magic_worker->dev_data = dev_data;
nfc_magic_worker_change_state(nfc_magic_worker, state);
furi_thread_start(nfc_magic_worker->thread);
}
int32_t nfc_magic_worker_task(void* context) {
NfcMagicWorker* nfc_magic_worker = context;
if(nfc_magic_worker->state == NfcMagicWorkerStateCheck) {
nfc_magic_worker_check(nfc_magic_worker);
} else if(nfc_magic_worker->state == NfcMagicWorkerStateWrite) {
nfc_magic_worker_write(nfc_magic_worker);
} else if(nfc_magic_worker->state == NfcMagicWorkerStateWipe) {
nfc_magic_worker_wipe(nfc_magic_worker);
}
nfc_magic_worker_change_state(nfc_magic_worker, NfcMagicWorkerStateReady);
return 0;
}
void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker) {
bool card_found_notified = false;
FuriHalNfcDevData nfc_data = {};
MfClassicData* src_data = &nfc_magic_worker->dev_data->mf_classic_data;
while(nfc_magic_worker->state == NfcMagicWorkerStateWrite) {
if(furi_hal_nfc_detect(&nfc_data, 200)) {
if(!card_found_notified) {
nfc_magic_worker->callback(
NfcMagicWorkerEventCardDetected, nfc_magic_worker->context);
card_found_notified = true;
}
furi_hal_nfc_sleep();
if(!magic_wupa()) {
FURI_LOG_E(TAG, "Not Magic card");
nfc_magic_worker->callback(
NfcMagicWorkerEventWrongCard, nfc_magic_worker->context);
break;
}
if(!magic_data_access_cmd()) {
FURI_LOG_E(TAG, "Not Magic card");
nfc_magic_worker->callback(
NfcMagicWorkerEventWrongCard, nfc_magic_worker->context);
break;
}
for(size_t i = 0; i < 64; i++) {
FURI_LOG_D(TAG, "Writing block %d", i);
if(!magic_write_blk(i, &src_data->block[i])) {
FURI_LOG_E(TAG, "Failed to write %d block", i);
nfc_magic_worker->callback(NfcMagicWorkerEventFail, nfc_magic_worker->context);
break;
}
}
nfc_magic_worker->callback(NfcMagicWorkerEventSuccess, nfc_magic_worker->context);
break;
} else {
if(card_found_notified) {
nfc_magic_worker->callback(
NfcMagicWorkerEventNoCardDetected, nfc_magic_worker->context);
card_found_notified = false;
}
}
furi_delay_ms(300);
}
magic_deactivate();
}
void nfc_magic_worker_check(NfcMagicWorker* nfc_magic_worker) {
bool card_found_notified = false;
while(nfc_magic_worker->state == NfcMagicWorkerStateCheck) {
if(magic_wupa()) {
if(!card_found_notified) {
nfc_magic_worker->callback(
NfcMagicWorkerEventCardDetected, nfc_magic_worker->context);
card_found_notified = true;
}
nfc_magic_worker->callback(NfcMagicWorkerEventSuccess, nfc_magic_worker->context);
break;
} else {
if(card_found_notified) {
nfc_magic_worker->callback(
NfcMagicWorkerEventNoCardDetected, nfc_magic_worker->context);
card_found_notified = false;
}
}
furi_delay_ms(300);
}
magic_deactivate();
}
void nfc_magic_worker_wipe(NfcMagicWorker* nfc_magic_worker) {
MfClassicBlock block;
memset(&block, 0, sizeof(MfClassicBlock));
block.value[0] = 0x01;
block.value[1] = 0x02;
block.value[2] = 0x03;
block.value[3] = 0x04;
block.value[4] = 0x04;
block.value[5] = 0x08;
block.value[6] = 0x04;
while(nfc_magic_worker->state == NfcMagicWorkerStateWipe) {
magic_deactivate();
furi_delay_ms(300);
if(!magic_wupa()) continue;
if(!magic_wipe()) continue;
if(!magic_data_access_cmd()) continue;
if(!magic_write_blk(0, &block)) continue;
nfc_magic_worker->callback(NfcMagicWorkerEventSuccess, nfc_magic_worker->context);
magic_deactivate();
break;
}
magic_deactivate();
}

View File

@@ -0,0 +1,38 @@
#pragma once
#include <lib/nfc/nfc_device.h>
typedef struct NfcMagicWorker NfcMagicWorker;
typedef enum {
NfcMagicWorkerStateReady,
NfcMagicWorkerStateCheck,
NfcMagicWorkerStateWrite,
NfcMagicWorkerStateWipe,
NfcMagicWorkerStateStop,
} NfcMagicWorkerState;
typedef enum {
NfcMagicWorkerEventSuccess,
NfcMagicWorkerEventFail,
NfcMagicWorkerEventCardDetected,
NfcMagicWorkerEventNoCardDetected,
NfcMagicWorkerEventWrongCard,
} NfcMagicWorkerEvent;
typedef bool (*NfcMagicWorkerCallback)(NfcMagicWorkerEvent event, void* context);
NfcMagicWorker* nfc_magic_worker_alloc();
void nfc_magic_worker_free(NfcMagicWorker* nfc_magic_worker);
void nfc_magic_worker_stop(NfcMagicWorker* nfc_magic_worker);
void nfc_magic_worker_start(
NfcMagicWorker* nfc_magic_worker,
NfcMagicWorkerState state,
NfcDeviceData* dev_data,
NfcMagicWorkerCallback callback,
void* context);

View File

@@ -0,0 +1,24 @@
#pragma once
#include <furi.h>
#include "nfc_magic_worker.h"
struct NfcMagicWorker {
FuriThread* thread;
NfcDeviceData* dev_data;
NfcMagicWorkerCallback callback;
void* context;
NfcMagicWorkerState state;
};
int32_t nfc_magic_worker_task(void* context);
void nfc_magic_worker_check(NfcMagicWorker* nfc_magic_worker);
void nfc_magic_worker_write(NfcMagicWorker* nfc_magic_worker);
void nfc_magic_worker_wipe(NfcMagicWorker* nfc_magic_worker);

View File

@@ -0,0 +1,30 @@
#include "nfc_magic_scene.h"
// Generate scene on_enter handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
void (*const nfc_magic_on_enter_handlers[])(void*) = {
#include "nfc_magic_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_event handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
bool (*const nfc_magic_on_event_handlers[])(void* context, SceneManagerEvent event) = {
#include "nfc_magic_scene_config.h"
};
#undef ADD_SCENE
// Generate scene on_exit handlers array
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
void (*const nfc_magic_on_exit_handlers[])(void* context) = {
#include "nfc_magic_scene_config.h"
};
#undef ADD_SCENE
// Initialize scene handlers configuration structure
const SceneManagerHandlers nfc_magic_scene_handlers = {
.on_enter_handlers = nfc_magic_on_enter_handlers,
.on_event_handlers = nfc_magic_on_event_handlers,
.on_exit_handlers = nfc_magic_on_exit_handlers,
.scene_num = NfcMagicSceneNum,
};

View File

@@ -0,0 +1,29 @@
#pragma once
#include <gui/scene_manager.h>
// Generate scene id and total number
#define ADD_SCENE(prefix, name, id) NfcMagicScene##id,
typedef enum {
#include "nfc_magic_scene_config.h"
NfcMagicSceneNum,
} NfcMagicScene;
#undef ADD_SCENE
extern const SceneManagerHandlers nfc_magic_scene_handlers;
// Generate scene on_enter handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
#include "nfc_magic_scene_config.h"
#undef ADD_SCENE
// Generate scene on_event handlers declaration
#define ADD_SCENE(prefix, name, id) \
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
#include "nfc_magic_scene_config.h"
#undef ADD_SCENE
// Generate scene on_exit handlers declaration
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
#include "nfc_magic_scene_config.h"
#undef ADD_SCENE

View File

@@ -0,0 +1,87 @@
#include "../nfc_magic_i.h"
enum {
NfcMagicSceneCheckStateCardSearch,
NfcMagicSceneCheckStateCardFound,
};
bool nfc_magic_check_worker_callback(NfcMagicWorkerEvent event, void* context) {
furi_assert(context);
NfcMagic* nfc_magic = context;
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, event);
return true;
}
static void nfc_magic_scene_check_setup_view(NfcMagic* nfc_magic) {
Popup* popup = nfc_magic->popup;
popup_reset(popup);
uint32_t state = scene_manager_get_scene_state(nfc_magic->scene_manager, NfcMagicSceneCheck);
if(state == NfcMagicSceneCheckStateCardSearch) {
popup_set_icon(nfc_magic->popup, 0, 8, &I_NFC_manual_60x50);
popup_set_text(
nfc_magic->popup, "Apply card to\nthe back", 128, 32, AlignRight, AlignCenter);
} else {
popup_set_icon(popup, 12, 23, &I_Loading_24);
popup_set_header(popup, "Checking\nDon't move...", 52, 32, AlignLeft, AlignCenter);
}
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
}
void nfc_magic_scene_check_on_enter(void* context) {
NfcMagic* nfc_magic = context;
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneCheck, NfcMagicSceneCheckStateCardSearch);
nfc_magic_scene_check_setup_view(nfc_magic);
// Setup and start worker
nfc_magic_worker_start(
nfc_magic->worker,
NfcMagicWorkerStateCheck,
&nfc_magic->nfc_dev->dev_data,
nfc_magic_check_worker_callback,
nfc_magic);
nfc_magic_blink_start(nfc_magic);
}
bool nfc_magic_scene_check_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcMagicWorkerEventSuccess) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneMagicInfo);
consumed = true;
} else if(event.event == NfcMagicWorkerEventWrongCard) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneNotMagic);
consumed = true;
} else if(event.event == NfcMagicWorkerEventCardDetected) {
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneCheck, NfcMagicSceneCheckStateCardFound);
nfc_magic_scene_check_setup_view(nfc_magic);
consumed = true;
} else if(event.event == NfcMagicWorkerEventNoCardDetected) {
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneCheck, NfcMagicSceneCheckStateCardSearch);
nfc_magic_scene_check_setup_view(nfc_magic);
consumed = true;
}
}
return consumed;
}
void nfc_magic_scene_check_on_exit(void* context) {
NfcMagic* nfc_magic = context;
nfc_magic_worker_stop(nfc_magic->worker);
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneCheck, NfcMagicSceneCheckStateCardSearch);
// Clear view
popup_reset(nfc_magic->popup);
nfc_magic_blink_stop(nfc_magic);
}

View File

@@ -0,0 +1,12 @@
ADD_SCENE(nfc_magic, start, Start)
ADD_SCENE(nfc_magic, file_select, FileSelect)
ADD_SCENE(nfc_magic, write_confirm, WriteConfirm)
ADD_SCENE(nfc_magic, wrong_card, WrongCard)
ADD_SCENE(nfc_magic, write, Write)
ADD_SCENE(nfc_magic, write_fail, WriteFail)
ADD_SCENE(nfc_magic, success, Success)
ADD_SCENE(nfc_magic, check, Check)
ADD_SCENE(nfc_magic, not_magic, NotMagic)
ADD_SCENE(nfc_magic, magic_info, MagicInfo)
ADD_SCENE(nfc_magic, wipe, Wipe)
ADD_SCENE(nfc_magic, wipe_fail, WipeFail)

View File

@@ -0,0 +1,34 @@
#include "../nfc_magic_i.h"
static bool nfc_magic_scene_file_select_is_file_suitable(NfcDevice* nfc_dev) {
return (nfc_dev->format == NfcDeviceSaveFormatMifareClassic) &&
(nfc_dev->dev_data.mf_classic_data.type == MfClassicType1k) &&
(nfc_dev->dev_data.nfc_data.uid_len == 4);
}
void nfc_magic_scene_file_select_on_enter(void* context) {
NfcMagic* nfc_magic = context;
// Process file_select return
nfc_device_set_loading_callback(nfc_magic->nfc_dev, nfc_magic_show_loading_popup, nfc_magic);
if(nfc_file_select(nfc_magic->nfc_dev)) {
if(nfc_magic_scene_file_select_is_file_suitable(nfc_magic->nfc_dev)) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneWriteConfirm);
} else {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneWrongCard);
}
} else {
scene_manager_previous_scene(nfc_magic->scene_manager);
}
}
bool nfc_magic_scene_file_select_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
return false;
}
void nfc_magic_scene_file_select_on_exit(void* context) {
NfcMagic* nfc_magic = context;
nfc_device_set_loading_callback(nfc_magic->nfc_dev, NULL, nfc_magic);
}

View File

@@ -0,0 +1,45 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_magic_info_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
NfcMagic* nfc_magic = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, result);
}
}
void nfc_magic_scene_magic_info_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Widget* widget = nfc_magic->widget;
notification_message(nfc_magic->notifications, &sequence_success);
widget_add_icon_element(widget, 73, 17, &I_DolphinCommon_56x48);
widget_add_string_element(
widget, 3, 4, AlignLeft, AlignTop, FontPrimary, "Magic card detected");
widget_add_button_element(
widget, GuiButtonTypeLeft, "Retry", nfc_magic_scene_magic_info_widget_callback, nfc_magic);
// Setup and start worker
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
}
bool nfc_magic_scene_magic_info_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_previous_scene(nfc_magic->scene_manager);
}
}
return consumed;
}
void nfc_magic_scene_magic_info_on_exit(void* context) {
NfcMagic* nfc_magic = context;
widget_reset(nfc_magic->widget);
}

View File

@@ -0,0 +1,44 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_not_magic_widget_callback(GuiButtonType result, InputType type, void* context) {
NfcMagic* nfc_magic = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, result);
}
}
void nfc_magic_scene_not_magic_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Widget* widget = nfc_magic->widget;
notification_message(nfc_magic->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, "Not a magic\ncard");
widget_add_button_element(
widget, GuiButtonTypeLeft, "Retry", nfc_magic_scene_not_magic_widget_callback, nfc_magic);
// Setup and start worker
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
}
bool nfc_magic_scene_not_magic_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_previous_scene(nfc_magic->scene_manager);
}
}
return consumed;
}
void nfc_magic_scene_not_magic_on_exit(void* context) {
NfcMagic* nfc_magic = context;
widget_reset(nfc_magic->widget);
}

View File

@@ -0,0 +1,61 @@
#include "../nfc_magic_i.h"
enum SubmenuIndex {
SubmenuIndexCheck,
SubmenuIndexWriteGen1A,
SubmenuIndexWipe,
};
void nfc_magic_scene_start_submenu_callback(void* context, uint32_t index) {
NfcMagic* nfc_magic = context;
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, index);
}
void nfc_magic_scene_start_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Submenu* submenu = nfc_magic->submenu;
submenu_add_item(
submenu,
"Check Magic Tag",
SubmenuIndexCheck,
nfc_magic_scene_start_submenu_callback,
nfc_magic);
submenu_add_item(
submenu,
"Write Gen1A",
SubmenuIndexWriteGen1A,
nfc_magic_scene_start_submenu_callback,
nfc_magic);
submenu_add_item(
submenu, "Wipe", SubmenuIndexWipe, nfc_magic_scene_start_submenu_callback, nfc_magic);
submenu_set_selected_item(
submenu, scene_manager_get_scene_state(nfc_magic->scene_manager, NfcMagicSceneStart));
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewMenu);
}
bool nfc_magic_scene_start_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexCheck) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneCheck);
consumed = true;
} else if(event.event == SubmenuIndexWriteGen1A) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneFileSelect);
consumed = true;
} else if(event.event == SubmenuIndexWipe) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneWipe);
consumed = true;
}
scene_manager_set_scene_state(nfc_magic->scene_manager, NfcMagicSceneStart, event.event);
}
return consumed;
}
void nfc_magic_scene_start_on_exit(void* context) {
NfcMagic* nfc_magic = context;
submenu_reset(nfc_magic->submenu);
}

View File

@@ -0,0 +1,42 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_success_popup_callback(void* context) {
NfcMagic* nfc_magic = context;
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, NfcMagicCustomEventViewExit);
}
void nfc_magic_scene_success_on_enter(void* context) {
NfcMagic* nfc_magic = context;
notification_message(nfc_magic->notifications, &sequence_success);
Popup* popup = nfc_magic->popup;
popup_set_icon(popup, 32, 5, &I_DolphinNice_96x59);
popup_set_header(popup, "Success!", 10, 20, AlignLeft, AlignBottom);
popup_set_timeout(popup, 1500);
popup_set_context(popup, nfc_magic);
popup_set_callback(popup, nfc_magic_scene_success_popup_callback);
popup_enable_timeout(popup);
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
}
bool nfc_magic_scene_success_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcMagicCustomEventViewExit) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc_magic->scene_manager, NfcMagicSceneStart);
}
}
return consumed;
}
void nfc_magic_scene_success_on_exit(void* context) {
NfcMagic* nfc_magic = context;
// Clear view
popup_reset(nfc_magic->popup);
}

View File

@@ -0,0 +1,90 @@
#include "../nfc_magic_i.h"
enum {
NfcMagicSceneWipeStateCardSearch,
NfcMagicSceneWipeStateCardFound,
};
bool nfc_magic_wipe_worker_callback(NfcMagicWorkerEvent event, void* context) {
furi_assert(context);
NfcMagic* nfc_magic = context;
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, event);
return true;
}
static void nfc_magic_scene_wipe_setup_view(NfcMagic* nfc_magic) {
Popup* popup = nfc_magic->popup;
popup_reset(popup);
uint32_t state = scene_manager_get_scene_state(nfc_magic->scene_manager, NfcMagicSceneWipe);
if(state == NfcMagicSceneWipeStateCardSearch) {
popup_set_icon(nfc_magic->popup, 0, 8, &I_NFC_manual_60x50);
popup_set_text(
nfc_magic->popup, "Apply card to\nthe back", 128, 32, AlignRight, AlignCenter);
} else {
popup_set_icon(popup, 12, 23, &I_Loading_24);
popup_set_header(popup, "Wiping\nDon't move...", 52, 32, AlignLeft, AlignCenter);
}
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
}
void nfc_magic_scene_wipe_on_enter(void* context) {
NfcMagic* nfc_magic = context;
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
nfc_magic_scene_wipe_setup_view(nfc_magic);
// Setup and start worker
nfc_magic_worker_start(
nfc_magic->worker,
NfcMagicWorkerStateWipe,
&nfc_magic->nfc_dev->dev_data,
nfc_magic_wipe_worker_callback,
nfc_magic);
nfc_magic_blink_start(nfc_magic);
}
bool nfc_magic_scene_wipe_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcMagicWorkerEventSuccess) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneSuccess);
consumed = true;
} else if(event.event == NfcMagicWorkerEventFail) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneWipeFail);
consumed = true;
} else if(event.event == NfcMagicWorkerEventWrongCard) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneNotMagic);
consumed = true;
} else if(event.event == NfcMagicWorkerEventCardDetected) {
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardFound);
nfc_magic_scene_wipe_setup_view(nfc_magic);
consumed = true;
} else if(event.event == NfcMagicWorkerEventNoCardDetected) {
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
nfc_magic_scene_wipe_setup_view(nfc_magic);
consumed = true;
}
}
return consumed;
}
void nfc_magic_scene_wipe_on_exit(void* context) {
NfcMagic* nfc_magic = context;
nfc_magic_worker_stop(nfc_magic->worker);
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWipe, NfcMagicSceneWipeStateCardSearch);
// Clear view
popup_reset(nfc_magic->popup);
nfc_magic_blink_stop(nfc_magic);
}

View File

@@ -0,0 +1,41 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_wipe_fail_widget_callback(GuiButtonType result, InputType type, void* context) {
NfcMagic* nfc_magic = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, result);
}
}
void nfc_magic_scene_wipe_fail_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Widget* widget = nfc_magic->widget;
notification_message(nfc_magic->notifications, &sequence_error);
widget_add_icon_element(widget, 73, 17, &I_DolphinCommon_56x48);
widget_add_string_element(widget, 3, 4, AlignLeft, AlignTop, FontPrimary, "Wipe failed");
widget_add_button_element(
widget, GuiButtonTypeLeft, "Retry", nfc_magic_scene_wipe_fail_widget_callback, nfc_magic);
// Setup and start worker
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
}
bool nfc_magic_scene_wipe_fail_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_previous_scene(nfc_magic->scene_manager);
}
}
return consumed;
}
void nfc_magic_scene_wipe_fail_on_exit(void* context) {
NfcMagic* nfc_magic = context;
widget_reset(nfc_magic->widget);
}

View File

@@ -0,0 +1,90 @@
#include "../nfc_magic_i.h"
enum {
NfcMagicSceneWriteStateCardSearch,
NfcMagicSceneWriteStateCardFound,
};
bool nfc_magic_write_worker_callback(NfcMagicWorkerEvent event, void* context) {
furi_assert(context);
NfcMagic* nfc_magic = context;
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, event);
return true;
}
static void nfc_magic_scene_write_setup_view(NfcMagic* nfc_magic) {
Popup* popup = nfc_magic->popup;
popup_reset(popup);
uint32_t state = scene_manager_get_scene_state(nfc_magic->scene_manager, NfcMagicSceneWrite);
if(state == NfcMagicSceneWriteStateCardSearch) {
popup_set_text(
nfc_magic->popup, "Apply card to\nthe back", 128, 32, AlignRight, AlignCenter);
popup_set_icon(nfc_magic->popup, 0, 8, &I_NFC_manual_60x50);
} else {
popup_set_icon(popup, 12, 23, &I_Loading_24);
popup_set_header(popup, "Writing\nDon't move...", 52, 32, AlignLeft, AlignCenter);
}
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewPopup);
}
void nfc_magic_scene_write_on_enter(void* context) {
NfcMagic* nfc_magic = context;
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWrite, NfcMagicSceneWriteStateCardSearch);
nfc_magic_scene_write_setup_view(nfc_magic);
// Setup and start worker
nfc_magic_worker_start(
nfc_magic->worker,
NfcMagicWorkerStateWrite,
&nfc_magic->nfc_dev->dev_data,
nfc_magic_write_worker_callback,
nfc_magic);
nfc_magic_blink_start(nfc_magic);
}
bool nfc_magic_scene_write_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == NfcMagicWorkerEventSuccess) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneSuccess);
consumed = true;
} else if(event.event == NfcMagicWorkerEventFail) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneWriteFail);
consumed = true;
} else if(event.event == NfcMagicWorkerEventWrongCard) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneNotMagic);
consumed = true;
} else if(event.event == NfcMagicWorkerEventCardDetected) {
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWrite, NfcMagicSceneWriteStateCardFound);
nfc_magic_scene_write_setup_view(nfc_magic);
consumed = true;
} else if(event.event == NfcMagicWorkerEventNoCardDetected) {
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWrite, NfcMagicSceneWriteStateCardSearch);
nfc_magic_scene_write_setup_view(nfc_magic);
consumed = true;
}
}
return consumed;
}
void nfc_magic_scene_write_on_exit(void* context) {
NfcMagic* nfc_magic = context;
nfc_magic_worker_stop(nfc_magic->worker);
scene_manager_set_scene_state(
nfc_magic->scene_manager, NfcMagicSceneWrite, NfcMagicSceneWriteStateCardSearch);
// Clear view
popup_reset(nfc_magic->popup);
nfc_magic_blink_stop(nfc_magic);
}

View File

@@ -0,0 +1,64 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_write_confirm_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
NfcMagic* nfc_magic = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, result);
}
}
void nfc_magic_scene_write_confirm_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Widget* widget = nfc_magic->widget;
widget_add_string_element(widget, 3, 0, AlignLeft, AlignTop, FontPrimary, "Risky operation");
widget_add_text_box_element(
widget,
0,
13,
128,
54,
AlignLeft,
AlignTop,
"Writing to this card will change manufacturer block. On some cards it may not be rewritten",
false);
widget_add_button_element(
widget,
GuiButtonTypeCenter,
"Continue",
nfc_magic_scene_write_confirm_widget_callback,
nfc_magic);
widget_add_button_element(
widget,
GuiButtonTypeLeft,
"Back",
nfc_magic_scene_write_confirm_widget_callback,
nfc_magic);
// Setup and start worker
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
}
bool nfc_magic_scene_write_confirm_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_previous_scene(nfc_magic->scene_manager);
} else if(event.event == GuiButtonTypeCenter) {
scene_manager_next_scene(nfc_magic->scene_manager, NfcMagicSceneWrite);
consumed = true;
}
}
return consumed;
}
void nfc_magic_scene_write_confirm_on_exit(void* context) {
NfcMagic* nfc_magic = context;
widget_reset(nfc_magic->widget);
}

View File

@@ -0,0 +1,58 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_write_fail_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
NfcMagic* nfc_magic = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, result);
}
}
void nfc_magic_scene_write_fail_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Widget* widget = nfc_magic->widget;
notification_message(nfc_magic->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_magic_scene_write_fail_widget_callback, nfc_magic);
// Setup and start worker
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
}
bool nfc_magic_scene_write_fail_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc_magic->scene_manager, NfcMagicSceneStart);
}
} else if(event.type == SceneManagerEventTypeBack) {
consumed = scene_manager_search_and_switch_to_previous_scene(
nfc_magic->scene_manager, NfcMagicSceneStart);
}
return consumed;
}
void nfc_magic_scene_write_fail_on_exit(void* context) {
NfcMagic* nfc_magic = context;
widget_reset(nfc_magic->widget);
}

View File

@@ -0,0 +1,53 @@
#include "../nfc_magic_i.h"
void nfc_magic_scene_wrong_card_widget_callback(
GuiButtonType result,
InputType type,
void* context) {
NfcMagic* nfc_magic = context;
if(type == InputTypeShort) {
view_dispatcher_send_custom_event(nfc_magic->view_dispatcher, result);
}
}
void nfc_magic_scene_wrong_card_on_enter(void* context) {
NfcMagic* nfc_magic = context;
Widget* widget = nfc_magic->widget;
notification_message(nfc_magic->notifications, &sequence_error);
widget_add_icon_element(widget, 73, 17, &I_DolphinCommon_56x48);
widget_add_string_element(
widget, 1, 4, AlignLeft, AlignTop, FontPrimary, "This is wrong card");
widget_add_string_multiline_element(
widget,
1,
17,
AlignLeft,
AlignTop,
FontSecondary,
"Writing is supported\nonly for 4 bytes UID\nMifare CLassic 1k");
widget_add_button_element(
widget, GuiButtonTypeLeft, "Retry", nfc_magic_scene_wrong_card_widget_callback, nfc_magic);
// Setup and start worker
view_dispatcher_switch_to_view(nfc_magic->view_dispatcher, NfcMagicViewWidget);
}
bool nfc_magic_scene_wrong_card_on_event(void* context, SceneManagerEvent event) {
NfcMagic* nfc_magic = context;
bool consumed = false;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == GuiButtonTypeLeft) {
consumed = scene_manager_previous_scene(nfc_magic->scene_manager);
}
}
return consumed;
}
void nfc_magic_scene_wrong_card_on_exit(void* context) {
NfcMagic* nfc_magic = context;
widget_reset(nfc_magic->widget);
}