[FL-1479] New NFC app (#544)

* view: add custom event callback
* nfc: rework nfc worker
* gui: introduce view navigator
* nfc_scences: introduce nfc scenes
* nfc: add start scene
* lib: add C application scene template
* nfc: move nfc views to separate directory
* view_dispatcher: add support for view_navigator
* nfc views: rework nfc views
* nfc scenes: add nfc application scenes
* nfc: rework nfc main thread
* view_dispatcher: add separate event for search back scene
* nfc: set worker result address at worker start
* nfc: update read nfc scenes
* view_navigator: rework with M-LIB container
* view_dispatcher: check that all views were freed
* nfc: add debug menu with all functions
* nfc read scene: add notification on success
* api-hal-nfc: add API for UID emulation
* nfc: add nfc emulation UID scene
* assets: add NFC assets
* nfc: update read and emulate scenes UI
* nfc: fix memory leak
* rfal: set custom analog configuration
This commit is contained in:
gornekich
2021-06-30 20:43:29 +03:00
committed by GitHub
parent 7a13391b2b
commit a0e1e42f2d
57 changed files with 2489 additions and 1089 deletions

View File

@@ -0,0 +1,142 @@
#include "nfc_detect.h"
#include <furi.h>
#include <api-hal.h>
#include <input/input.h>
#include "../nfc_i.h"
struct NfcDetect {
NfcCommon* nfc_common;
View* view;
};
typedef struct {
bool found;
NfcDeviceData data;
} NfcDetectModel;
void nfc_detect_draw(Canvas* canvas, NfcDetectModel* model) {
char buffer[32];
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
if(model->found) {
canvas_draw_str(canvas, 0, 12, "Found");
canvas_draw_str(canvas, 32, 12, nfc_get_dev_type(model->data.device));
canvas_set_font(canvas, FontSecondary);
if(model->data.protocol != NfcDeviceProtocolUnknown) {
canvas_draw_str(canvas, 0, 22, nfc_get_protocol(model->data.protocol));
}
// Display UID
for(uint8_t i = 0; i < model->data.uid_len; i++) {
snprintf(buffer + (i * 2), sizeof(buffer) - (i * 2), "%02X", model->data.uid[i]);
buffer[model->data.uid_len * 2] = 0;
}
canvas_draw_str(canvas, 0, 32, "UID: ");
canvas_draw_str(canvas, 22, 32, buffer);
// Display ATQA and SAK
snprintf(
buffer,
sizeof(buffer),
"ATQA: %02X %02X SAK: %02X",
model->data.atqa[1],
model->data.atqa[0],
model->data.sak);
canvas_draw_str(canvas, 0, 42, buffer);
} else {
canvas_draw_str(canvas, 0, 12, "Searching");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 22, "Place card to the back");
}
}
bool nfc_detect_input(InputEvent* event, void* context) {
if(event->key == InputKeyBack) {
return false;
}
return true;
}
void nfc_detect_worker_callback(void* context) {
furi_assert(context);
NfcDetect* nfc_detect = (NfcDetect*)context;
view_dispatcher_send_custom_event(nfc_detect->nfc_common->view_dispatcher, NfcEventDetect);
}
bool nfc_detect_view_custom(uint32_t event, void* context) {
furi_assert(context);
NfcDetect* nfc_detect = (NfcDetect*)context;
if(event == NfcEventDetect) {
NfcDeviceData* data = (NfcDeviceData*)&nfc_detect->nfc_common->worker_result;
with_view_model(
nfc_detect->view, (NfcDetectModel * model) {
model->found = true;
model->data = *data;
return true;
});
// TODO add and configure next view model
return false;
}
return false;
}
void nfc_detect_enter(void* context) {
furi_assert(context);
NfcDetect* nfc_detect = (NfcDetect*)context;
with_view_model(
nfc_detect->view, (NfcDetectModel * model) {
model->found = false;
model->data.protocol = NfcDeviceProtocolUnknown;
return true;
});
nfc_worker_start(
nfc_detect->nfc_common->worker,
NfcWorkerStateDetect,
&nfc_detect->nfc_common->worker_result,
nfc_detect_worker_callback,
nfc_detect);
}
void nfc_detect_exit(void* context) {
furi_assert(context);
NfcDetect* nfc_detect = (NfcDetect*)context;
nfc_worker_stop(nfc_detect->nfc_common->worker);
}
NfcDetect* nfc_detect_alloc(NfcCommon* nfc_common) {
furi_assert(nfc_common);
NfcDetect* nfc_detect = furi_alloc(sizeof(NfcDetect));
nfc_detect->nfc_common = nfc_common;
// View allocation and configuration
nfc_detect->view = view_alloc();
view_allocate_model(nfc_detect->view, ViewModelTypeLockFree, sizeof(NfcDetectModel));
view_set_context(nfc_detect->view, nfc_detect);
view_set_draw_callback(nfc_detect->view, (ViewDrawCallback)nfc_detect_draw);
view_set_input_callback(nfc_detect->view, nfc_detect_input);
view_set_custom_callback(nfc_detect->view, nfc_detect_view_custom);
view_set_enter_callback(nfc_detect->view, nfc_detect_enter);
view_set_exit_callback(nfc_detect->view, nfc_detect_exit);
return nfc_detect;
}
void nfc_detect_free(NfcDetect* nfc_detect) {
furi_assert(nfc_detect);
view_free(nfc_detect->view);
free(nfc_detect);
}
View* nfc_detect_get_view(NfcDetect* nfc_detect) {
furi_assert(nfc_detect);
return nfc_detect->view;
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <gui/view.h>
#include "../nfc_types.h"
typedef struct NfcDetect NfcDetect;
NfcDetect* nfc_detect_alloc(NfcCommon* nfc_common);
void nfc_detect_free(NfcDetect* nfc_detect);
View* nfc_detect_get_view(NfcDetect* nfc_detect);

View File

@@ -0,0 +1,79 @@
#include "nfc_emulate.h"
#include <furi.h>
#include <api-hal.h>
#include <input/input.h>
#include "../nfc_i.h"
struct NfcEmulate {
NfcCommon* nfc_common;
View* view;
};
void nfc_emulate_draw(Canvas* canvas, void* model) {
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 0, 12, "Emulating NFC-A");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 22, "Type: T2T");
canvas_draw_str(canvas, 2, 32, "UID length: 7");
canvas_draw_str(canvas, 2, 42, "UID: 36 9C E7 B1 0A C1 34");
canvas_draw_str(canvas, 2, 52, "SAK: 00 ATQA: 00/44");
}
bool nfc_emulate_input(InputEvent* event, void* context) {
if(event->key == InputKeyBack) {
return false;
}
return true;
}
void nfc_emulate_enter(void* context) {
furi_assert(context);
NfcEmulate* nfc_emulate = (NfcEmulate*)context;
nfc_worker_start(
nfc_emulate->nfc_common->worker,
NfcWorkerStateEmulate,
&nfc_emulate->nfc_common->worker_result,
NULL,
NULL);
}
void nfc_emulate_exit(void* context) {
furi_assert(context);
NfcEmulate* nfc_emulate = (NfcEmulate*)context;
nfc_worker_stop(nfc_emulate->nfc_common->worker);
}
NfcEmulate* nfc_emulate_alloc(NfcCommon* nfc_common) {
furi_assert(nfc_common);
NfcEmulate* nfc_emulate = furi_alloc(sizeof(NfcEmulate));
nfc_emulate->nfc_common = nfc_common;
// View allocation and configuration
nfc_emulate->view = view_alloc();
view_set_context(nfc_emulate->view, nfc_emulate);
view_set_draw_callback(nfc_emulate->view, (ViewDrawCallback)nfc_emulate_draw);
view_set_input_callback(nfc_emulate->view, nfc_emulate_input);
view_set_enter_callback(nfc_emulate->view, nfc_emulate_enter);
view_set_exit_callback(nfc_emulate->view, nfc_emulate_exit);
return nfc_emulate;
}
void nfc_emulate_free(NfcEmulate* nfc_emulate) {
furi_assert(nfc_emulate);
view_free(nfc_emulate->view);
free(nfc_emulate);
}
View* nfc_emulate_get_view(NfcEmulate* nfc_emulate) {
furi_assert(nfc_emulate);
return nfc_emulate->view;
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <gui/view.h>
#include "../nfc_types.h"
typedef struct NfcEmulate NfcEmulate;
NfcEmulate* nfc_emulate_alloc(NfcCommon* nfc_common);
void nfc_emulate_free(NfcEmulate* nfc_emulate);
View* nfc_emulate_get_view(NfcEmulate* nfc_emulate);

133
applications/nfc/views/nfc_emv.c Executable file
View File

@@ -0,0 +1,133 @@
#include "nfc_emv.h"
#include <furi.h>
#include <api-hal.h>
#include <input/input.h>
#include "../nfc_i.h"
struct NfcEmv {
NfcCommon* nfc_common;
View* view;
};
typedef struct {
bool found;
NfcEmvData emv_data;
} NfcEmvModel;
void nfc_emv_draw(Canvas* canvas, NfcEmvModel* model) {
char buffer[32];
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
if(model->found) {
canvas_draw_str(canvas, 0, 12, "Found EMV card");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 22, "Type:");
snprintf(buffer, sizeof(buffer), "%s", model->emv_data.name);
canvas_draw_str(canvas, 2, 32, buffer);
snprintf(buffer, sizeof(buffer), "Number:\n");
canvas_draw_str(canvas, 2, 42, buffer);
uint8_t card_num_len = sizeof(model->emv_data.number);
for(uint8_t i = 0; i < card_num_len; i++) {
snprintf(
buffer + (i * 2), sizeof(buffer) - (i * 2), "%02X", model->emv_data.number[i]);
}
buffer[card_num_len * 2] = 0;
canvas_draw_str(canvas, 2, 52, buffer);
} else {
canvas_draw_str(canvas, 0, 12, "Searching");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 22, "Place card to the back");
}
}
bool nfc_emv_input(InputEvent* event, void* context) {
if(event->key == InputKeyBack) {
return false;
}
return true;
}
void nfc_emv_worker_callback(void* context) {
furi_assert(context);
NfcEmv* nfc_emv = (NfcEmv*)context;
view_dispatcher_send_custom_event(nfc_emv->nfc_common->view_dispatcher, NfcEventEmv);
}
bool nfc_emv_custom(uint32_t event, void* context) {
furi_assert(context);
NfcEmv* nfc_emv = (NfcEmv*)context;
if(event == NfcEventEmv) {
NfcEmvData* data = (NfcEmvData*)&nfc_emv->nfc_common->worker_result;
with_view_model(
nfc_emv->view, (NfcEmvModel * model) {
model->found = true;
model->emv_data = *data;
return true;
});
// TODO add and configure next view model
return true;
}
return false;
}
void nfc_emv_enter(void* context) {
furi_assert(context);
NfcEmv* nfc_emv = (NfcEmv*)context;
with_view_model(
nfc_emv->view, (NfcEmvModel * model) {
model->found = false;
return true;
});
nfc_worker_start(
nfc_emv->nfc_common->worker,
NfcWorkerStateReadEMV,
&nfc_emv->nfc_common->worker_result,
nfc_emv_worker_callback,
nfc_emv);
}
void nfc_emv_exit(void* context) {
furi_assert(context);
NfcEmv* nfc_emv = (NfcEmv*)context;
nfc_worker_stop(nfc_emv->nfc_common->worker);
}
NfcEmv* nfc_emv_alloc(NfcCommon* nfc_common) {
furi_assert(nfc_common);
NfcEmv* nfc_emv = furi_alloc(sizeof(NfcEmv));
nfc_emv->nfc_common = nfc_common;
// View allocation and configuration
nfc_emv->view = view_alloc();
view_allocate_model(nfc_emv->view, ViewModelTypeLockFree, sizeof(NfcEmvModel));
view_set_context(nfc_emv->view, nfc_emv);
view_set_draw_callback(nfc_emv->view, (ViewDrawCallback)nfc_emv_draw);
view_set_input_callback(nfc_emv->view, nfc_emv_input);
view_set_custom_callback(nfc_emv->view, nfc_emv_custom);
view_set_enter_callback(nfc_emv->view, nfc_emv_enter);
view_set_exit_callback(nfc_emv->view, nfc_emv_exit);
return nfc_emv;
}
void nfc_emv_free(NfcEmv* nfc_emv) {
furi_assert(nfc_emv);
view_free(nfc_emv->view);
free(nfc_emv);
}
View* nfc_emv_get_view(NfcEmv* nfc_emv) {
furi_assert(nfc_emv);
return nfc_emv->view;
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <gui/view.h>
#include "../nfc_types.h"
typedef struct NfcEmv NfcEmv;
NfcEmv* nfc_emv_alloc(NfcCommon* nfc_common);
void nfc_emv_free(NfcEmv* nfc_emv);
View* nfc_emv_get_view(NfcEmv* nfc_emv);

View File

@@ -0,0 +1,162 @@
#include "nfc_mifare_ul.h"
#include <furi.h>
#include <api-hal.h>
#include <input/input.h>
#include "../nfc_i.h"
struct NfcMifareUl {
NfcCommon* nfc_common;
View* view;
};
typedef struct {
bool found;
NfcMifareUlData nfc_mf_ul_data;
} NfcMifareUlModel;
void nfc_mifare_ul_draw(Canvas* canvas, NfcMifareUlModel* model) {
char buffer[32];
canvas_clear(canvas);
canvas_set_font(canvas, FontPrimary);
if(model->found) {
canvas_draw_str(canvas, 0, 12, "Found Mifare Ultralight");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 22, "UID:");
for(uint8_t i = 0; i < model->nfc_mf_ul_data.nfc_data.uid_len; i++) {
snprintf(
buffer + (i * 2),
sizeof(buffer) - (i * 2),
"%02X",
model->nfc_mf_ul_data.nfc_data.uid[i]);
}
buffer[model->nfc_mf_ul_data.nfc_data.uid_len * 2] = 0;
canvas_draw_str(canvas, 18, 22, buffer);
uint8_t man_bl_size = sizeof(model->nfc_mf_ul_data.man_block);
canvas_draw_str(canvas, 2, 32, "Manufacturer block:");
for(uint8_t i = 0; i < man_bl_size / 2; i++) {
snprintf(
buffer + (i * 2),
sizeof(buffer) - (i * 2),
"%02X",
model->nfc_mf_ul_data.man_block[i]);
}
buffer[man_bl_size] = 0;
canvas_draw_str(canvas, 2, 42, buffer);
for(uint8_t i = 0; i < man_bl_size / 2; i++) {
snprintf(
buffer + (i * 2),
sizeof(buffer) - (i * 2),
"%02X",
model->nfc_mf_ul_data.man_block[man_bl_size / 2 + i]);
}
buffer[man_bl_size] = 0;
canvas_draw_str(canvas, 2, 52, buffer);
canvas_draw_str(canvas, 2, 62, "OTP: ");
for(uint8_t i = 0; i < sizeof(model->nfc_mf_ul_data.otp); i++) {
snprintf(
buffer + (i * 2), sizeof(buffer) - (i * 2), "%02X", model->nfc_mf_ul_data.otp[i]);
}
buffer[sizeof(model->nfc_mf_ul_data.otp) * 2] = 0;
canvas_draw_str(canvas, 22, 62, buffer);
} else {
canvas_draw_str(canvas, 0, 12, "Searching");
canvas_set_font(canvas, FontSecondary);
canvas_draw_str(canvas, 2, 22, "Place card to the back");
}
}
bool nfc_mifare_ul_input(InputEvent* event, void* context) {
if(event->key == InputKeyBack) {
return false;
}
return true;
}
void nfc_mifare_ul_worker_callback(void* context) {
furi_assert(context);
NfcMifareUl* nfc_mifare_ul = (NfcMifareUl*)context;
view_dispatcher_send_custom_event(
nfc_mifare_ul->nfc_common->view_dispatcher, NfcEventMifareUl);
}
bool nfc_mifare_ul_custom(uint32_t event, void* context) {
furi_assert(context);
NfcMifareUl* nfc_mifare_ul = (NfcMifareUl*)context;
if(event == NfcEventMifareUl) {
NfcMifareUlData* data = (NfcMifareUlData*)&nfc_mifare_ul->nfc_common->worker_result;
with_view_model(
nfc_mifare_ul->view, (NfcMifareUlModel * model) {
model->found = true;
model->nfc_mf_ul_data = *data;
return true;
});
// TODO add and configure next view model
return true;
}
return false;
}
void nfc_mifare_ul_enter(void* context) {
furi_assert(context);
NfcMifareUl* nfc_mifare_ul = (NfcMifareUl*)context;
with_view_model(
nfc_mifare_ul->view, (NfcMifareUlModel * m) {
m->found = false;
return true;
});
nfc_worker_start(
nfc_mifare_ul->nfc_common->worker,
NfcWorkerStateReadMfUltralight,
&nfc_mifare_ul->nfc_common->worker_result,
nfc_mifare_ul_worker_callback,
nfc_mifare_ul);
}
void nfc_mifare_ul_exit(void* context) {
furi_assert(context);
NfcMifareUl* nfc_mifare_ul = (NfcMifareUl*)context;
nfc_worker_stop(nfc_mifare_ul->nfc_common->worker);
}
NfcMifareUl* nfc_mifare_ul_alloc(NfcCommon* nfc_common) {
furi_assert(nfc_common);
NfcMifareUl* nfc_mifare_ul = furi_alloc(sizeof(NfcMifareUl));
nfc_mifare_ul->nfc_common = nfc_common;
// View allocation and configuration
nfc_mifare_ul->view = view_alloc();
view_allocate_model(nfc_mifare_ul->view, ViewModelTypeLockFree, sizeof(NfcMifareUlModel));
view_set_context(nfc_mifare_ul->view, nfc_mifare_ul);
view_set_draw_callback(nfc_mifare_ul->view, (ViewDrawCallback)nfc_mifare_ul_draw);
view_set_input_callback(nfc_mifare_ul->view, nfc_mifare_ul_input);
view_set_custom_callback(nfc_mifare_ul->view, nfc_mifare_ul_custom);
view_set_enter_callback(nfc_mifare_ul->view, nfc_mifare_ul_enter);
view_set_exit_callback(nfc_mifare_ul->view, nfc_mifare_ul_exit);
return nfc_mifare_ul;
}
void nfc_mifare_ul_free(NfcMifareUl* nfc_mifare_ul) {
furi_assert(nfc_mifare_ul);
view_free(nfc_mifare_ul->view);
free(nfc_mifare_ul);
}
View* nfc_mifare_ul_get_view(NfcMifareUl* nfc_mifare_ul) {
furi_assert(nfc_mifare_ul);
return nfc_mifare_ul->view;
}

View File

@@ -0,0 +1,12 @@
#pragma once
#include <gui/view.h>
#include "../nfc_types.h"
typedef struct NfcMifareUl NfcMifareUl;
NfcMifareUl* nfc_mifare_ul_alloc(NfcCommon* nfc_common);
void nfc_mifare_ul_free(NfcMifareUl* nfc_mifare_ul);
View* nfc_mifare_ul_get_view(NfcMifareUl* nfc_mifare_ul);