flipperzero-firmware/applications/nfc/views/nfc_detect.c
gornekich a0e1e42f2d
[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
2021-06-30 20:43:29 +03:00

142 lines
4.1 KiB
C
Executable File

#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;
}