2020-10-18 22:09:48 +00:00
|
|
|
#include "nfc.h"
|
2020-11-12 09:44:35 +00:00
|
|
|
#include "nfc_i.h"
|
|
|
|
#include "nfc_worker.h"
|
2020-10-18 22:09:48 +00:00
|
|
|
|
|
|
|
void nfc_draw_callback(CanvasApi* canvas, void* context) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(canvas);
|
|
|
|
furi_assert(context);
|
|
|
|
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = context;
|
2020-10-18 22:09:48 +00:00
|
|
|
|
|
|
|
dispatcher_lock(nfc->dispatcher);
|
|
|
|
canvas->clear(canvas);
|
|
|
|
canvas->set_color(canvas, ColorBlack);
|
|
|
|
canvas->set_font(canvas, FontPrimary);
|
2020-10-20 11:05:50 +00:00
|
|
|
|
2020-10-20 13:58:41 +00:00
|
|
|
if(nfc->screen == 0) {
|
2020-10-20 13:49:52 +00:00
|
|
|
char status[128 / 8];
|
|
|
|
if(nfc->ret == ERR_WRONG_STATE)
|
|
|
|
canvas->draw_str(canvas, 2, 16, "NFC Wrong State");
|
|
|
|
else if(nfc->ret == ERR_PARAM)
|
|
|
|
canvas->draw_str(canvas, 2, 16, "NFC Wrong Param");
|
|
|
|
else if(nfc->ret == ERR_IO)
|
|
|
|
canvas->draw_str(canvas, 2, 16, "NFC IO Error");
|
|
|
|
else if(nfc->ret == ERR_NONE)
|
|
|
|
canvas->draw_str(canvas, 2, 16, "NFC Device Found");
|
|
|
|
else if(nfc->ret == ERR_TIMEOUT)
|
|
|
|
canvas->draw_str(canvas, 2, 16, "NFC Timeout");
|
|
|
|
else
|
|
|
|
canvas->draw_str(canvas, 2, 16, "NFC error");
|
|
|
|
|
2020-11-12 09:44:35 +00:00
|
|
|
canvas->set_font(canvas, FontSecondary);
|
|
|
|
snprintf(status, sizeof(status), "Found: %d", nfc->devCnt);
|
|
|
|
if(nfc->devCnt > 0) {
|
|
|
|
canvas->draw_str(canvas, 2, 32, status);
|
|
|
|
canvas->draw_str(canvas, 2, 42, nfc->current);
|
|
|
|
|
|
|
|
snprintf(
|
|
|
|
status,
|
|
|
|
sizeof(status),
|
|
|
|
"ATQA:%d SAK:%d",
|
|
|
|
nfc->first_atqa.anticollisionInfo,
|
|
|
|
nfc->first_sak.sak);
|
|
|
|
canvas->draw_str(canvas, 2, 52, status);
|
|
|
|
}
|
2020-10-20 13:49:52 +00:00
|
|
|
} else {
|
|
|
|
canvas->draw_str(canvas, 2, 16, "Not implemented");
|
|
|
|
}
|
|
|
|
|
2020-10-18 22:09:48 +00:00
|
|
|
dispatcher_unlock(nfc->dispatcher);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfc_input_callback(InputEvent* event, void* context) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(event);
|
|
|
|
furi_assert(context);
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = context;
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-11-12 09:44:35 +00:00
|
|
|
if(!event->state || event->input != InputBack) return;
|
2020-10-18 22:09:48 +00:00
|
|
|
|
|
|
|
widget_enabled_set(nfc->widget, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfc_test_callback(void* context) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(context);
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = context;
|
2020-10-18 22:09:48 +00:00
|
|
|
|
|
|
|
dispatcher_lock(nfc->dispatcher);
|
2020-10-21 10:59:52 +00:00
|
|
|
|
2020-10-21 10:52:51 +00:00
|
|
|
nfc->screen = 0;
|
|
|
|
widget_enabled_set(nfc->widget, true);
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-11-16 15:26:34 +00:00
|
|
|
// TODO only for workaround
|
|
|
|
if(nfc->ret != ERR_NONE) {
|
|
|
|
nfc->ret = rfalNfcInitialize();
|
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|
|
|
|
|
2020-10-21 10:52:51 +00:00
|
|
|
if(nfc->ret == ERR_NONE && !nfc->worker) {
|
|
|
|
// TODO change to fuirac_start
|
2020-10-18 22:09:48 +00:00
|
|
|
nfc->worker = osThreadNew(nfc_worker_task, nfc, &nfc->worker_attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatcher_unlock(nfc->dispatcher);
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:44:35 +00:00
|
|
|
void nfc_field_on_callback(void* context) {
|
2020-11-16 15:26:34 +00:00
|
|
|
furi_assert(context);
|
|
|
|
Nfc* nfc = context;
|
|
|
|
|
|
|
|
// TODO only for workaround
|
|
|
|
if(nfc->ret != ERR_NONE) {
|
|
|
|
nfc->ret = rfalNfcInitialize();
|
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:44:35 +00:00
|
|
|
st25r3916OscOn();
|
|
|
|
st25r3916TxRxOn();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfc_field_off_callback(void* context) {
|
2020-11-16 15:26:34 +00:00
|
|
|
furi_assert(context);
|
|
|
|
Nfc* nfc = context;
|
|
|
|
|
|
|
|
// TODO only for workaround
|
|
|
|
if(nfc->ret != ERR_NONE) {
|
|
|
|
nfc->ret = rfalNfcInitialize();
|
|
|
|
rfalLowPowerModeStart();
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:44:35 +00:00
|
|
|
st25r3916TxRxOff();
|
|
|
|
}
|
|
|
|
|
2020-10-18 22:09:48 +00:00
|
|
|
void nfc_read_callback(void* context) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(context);
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = context;
|
2020-10-20 13:49:52 +00:00
|
|
|
nfc->screen = 1;
|
|
|
|
widget_enabled_set(nfc->widget, true);
|
2020-10-18 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nfc_write_callback(void* context) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(context);
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = context;
|
2020-10-20 13:49:52 +00:00
|
|
|
nfc->screen = 1;
|
|
|
|
widget_enabled_set(nfc->widget, true);
|
2020-10-18 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nfc_bridge_callback(void* context) {
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_assert(context);
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = context;
|
2020-10-20 13:49:52 +00:00
|
|
|
nfc->screen = 1;
|
|
|
|
widget_enabled_set(nfc->widget, true);
|
2020-10-18 22:09:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc_alloc() {
|
|
|
|
Nfc* nfc = furi_alloc(sizeof(Nfc));
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-10-18 22:55:41 +00:00
|
|
|
nfc->dispatcher = dispatcher_alloc(32, sizeof(NfcMessage));
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-10-26 17:00:17 +00:00
|
|
|
nfc->icon = assets_icons_get(A_NFC_14);
|
2020-10-18 22:09:48 +00:00
|
|
|
nfc->widget = widget_alloc();
|
|
|
|
widget_draw_callback_set(nfc->widget, nfc_draw_callback, nfc);
|
|
|
|
widget_input_callback_set(nfc->widget, nfc_input_callback, nfc);
|
|
|
|
|
2020-10-26 17:00:17 +00:00
|
|
|
nfc->menu_vm = furi_open("menu");
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_check(nfc->menu_vm);
|
2020-10-26 17:00:17 +00:00
|
|
|
|
|
|
|
nfc->menu = menu_item_alloc_menu("NFC", nfc->icon);
|
2020-10-18 22:09:48 +00:00
|
|
|
menu_item_subitem_add(
|
|
|
|
nfc->menu, menu_item_alloc_function("Test", NULL, nfc_test_callback, nfc));
|
2020-11-12 09:44:35 +00:00
|
|
|
menu_item_subitem_add(
|
|
|
|
nfc->menu, menu_item_alloc_function("Field On", NULL, nfc_field_on_callback, nfc));
|
|
|
|
menu_item_subitem_add(
|
|
|
|
nfc->menu, menu_item_alloc_function("Field Off", NULL, nfc_field_off_callback, nfc));
|
2020-10-18 22:09:48 +00:00
|
|
|
menu_item_subitem_add(
|
|
|
|
nfc->menu, menu_item_alloc_function("Read", NULL, nfc_read_callback, nfc));
|
|
|
|
menu_item_subitem_add(
|
|
|
|
nfc->menu, menu_item_alloc_function("Write", NULL, nfc_write_callback, nfc));
|
|
|
|
menu_item_subitem_add(
|
|
|
|
nfc->menu, menu_item_alloc_function("Brdige", NULL, nfc_bridge_callback, nfc));
|
|
|
|
|
|
|
|
nfc->worker_attr.name = "nfc_worker";
|
|
|
|
// nfc->worker_attr.attr_bits = osThreadJoinable;
|
|
|
|
nfc->worker_attr.stack_size = 4096;
|
|
|
|
return nfc;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nfc_task(void* p) {
|
2020-10-18 22:55:41 +00:00
|
|
|
Nfc* nfc = nfc_alloc();
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-10-26 17:00:17 +00:00
|
|
|
FuriRecordSubscriber* gui_record = furi_open_deprecated("gui", false, false, NULL, NULL, NULL);
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_check(gui_record);
|
2020-10-26 17:00:17 +00:00
|
|
|
GuiApi* gui = furi_take(gui_record);
|
2020-10-29 06:27:17 +00:00
|
|
|
furi_check(gui);
|
2020-10-18 22:09:48 +00:00
|
|
|
widget_enabled_set(nfc->widget, false);
|
2020-10-26 17:00:17 +00:00
|
|
|
gui->add_widget(gui, nfc->widget, GuiLayerFullscreen);
|
|
|
|
furi_commit(gui_record);
|
2020-10-20 13:20:53 +00:00
|
|
|
|
2020-10-26 17:00:17 +00:00
|
|
|
with_value_mutex(
|
|
|
|
nfc->menu_vm, (Menu * menu) { menu_item_add(menu, nfc->menu); });
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-10-20 14:27:54 +00:00
|
|
|
if(!furi_create("nfc", nfc)) {
|
|
|
|
printf("[nfc_task] cannot create nfc record\n");
|
2020-10-18 22:09:48 +00:00
|
|
|
furiac_exit(NULL);
|
|
|
|
}
|
|
|
|
|
2020-11-16 15:26:34 +00:00
|
|
|
// TODO only for workaround
|
|
|
|
nfc->ret = ERR_WRONG_STATE;
|
2020-10-18 22:09:48 +00:00
|
|
|
|
2020-10-26 17:00:17 +00:00
|
|
|
furiac_ready();
|
|
|
|
|
2020-10-18 22:55:41 +00:00
|
|
|
NfcMessage message;
|
2020-10-18 22:09:48 +00:00
|
|
|
while(1) {
|
|
|
|
dispatcher_recieve(nfc->dispatcher, (Message*)&message);
|
|
|
|
|
|
|
|
if(message.base.type == MessageTypeExit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|