flipperzero-firmware/applications/nfc/nfc.c

224 lines
9.1 KiB
C
Raw Normal View History

#include "nfc_i.h"
2020-10-18 22:09:48 +00:00
uint32_t nfc_view_exit(void* context) {
furi_assert(context);
2020-10-18 22:55:41 +00:00
Nfc* nfc = context;
NfcMessage message;
message.type = NfcMessageTypeStop;
furi_check(osMessageQueuePut(nfc->message_queue, &message, 0, osWaitForever) == osOK);
return VIEW_NONE;
2020-10-18 22:09:48 +00:00
}
Nfc* nfc_alloc() {
Nfc* nfc = furi_alloc(sizeof(Nfc));
2020-10-18 22:09:48 +00:00
nfc->message_queue = osMessageQueueNew(8, sizeof(NfcMessage), NULL);
nfc->cli_message_queue = osMessageQueueNew(1, sizeof(NfcMessage), NULL);
nfc->worker = nfc_worker_alloc(nfc->message_queue);
2020-10-18 22:09:48 +00:00
nfc->icon = assets_icons_get(A_NFC_14);
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
nfc->menu_vm = furi_record_open("menu");
2020-11-16 15:26:34 +00:00
nfc->menu = menu_item_alloc_menu("NFC", nfc->icon);
menu_item_subitem_add(
nfc->menu, menu_item_alloc_function("Detect", NULL, nfc_menu_detect_callback, nfc));
menu_item_subitem_add(
nfc->menu, menu_item_alloc_function("Emulate", NULL, nfc_menu_emulate_callback, nfc));
menu_item_subitem_add(
nfc->menu, menu_item_alloc_function("Field", NULL, nfc_menu_field_callback, nfc));
nfc->view_detect = view_alloc();
view_set_context(nfc->view_detect, nfc);
view_set_draw_callback(nfc->view_detect, nfc_view_read_draw);
view_set_previous_callback(nfc->view_detect, nfc_view_exit);
view_allocate_model(nfc->view_detect, ViewModelTypeLocking, sizeof(NfcViewReadModel));
nfc->view_emulate = view_alloc();
view_set_context(nfc->view_emulate, nfc);
view_set_draw_callback(nfc->view_emulate, nfc_view_emulate_draw);
view_set_previous_callback(nfc->view_emulate, nfc_view_exit);
nfc->view_field = view_alloc();
view_set_context(nfc->view_field, nfc);
view_set_draw_callback(nfc->view_field, nfc_view_field_draw);
view_set_previous_callback(nfc->view_field, nfc_view_exit);
nfc->view_cli = view_alloc();
view_set_context(nfc->view_cli, nfc);
view_set_draw_callback(nfc->view_cli, nfc_view_cli_draw);
nfc->view_error = view_alloc();
view_set_context(nfc->view_error, nfc);
view_set_draw_callback(nfc->view_error, nfc_view_error_draw);
view_set_previous_callback(nfc->view_error, nfc_view_exit);
view_allocate_model(nfc->view_error, ViewModelTypeLockFree, sizeof(NfcViewErrorModel));
nfc->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewRead, nfc->view_detect);
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewEmulate, nfc->view_emulate);
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewField, nfc->view_field);
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewCli, nfc->view_cli);
view_dispatcher_add_view(nfc->view_dispatcher, NfcViewError, nfc->view_error);
2020-11-16 15:26:34 +00:00
return nfc;
}
void nfc_menu_detect_callback(void* context) {
2020-11-16 15:26:34 +00:00
furi_assert(context);
Nfc* nfc = context;
NfcMessage message;
message.type = NfcMessageTypeDetect;
furi_check(osMessageQueuePut(nfc->message_queue, &message, 0, osWaitForever) == osOK);
}
void nfc_menu_emulate_callback(void* context) {
furi_assert(context);
2020-10-18 22:55:41 +00:00
Nfc* nfc = context;
NfcMessage message;
message.type = NfcMessageTypeEmulate;
furi_check(osMessageQueuePut(nfc->message_queue, &message, 0, osWaitForever) == osOK);
2020-10-18 22:09:48 +00:00
}
void nfc_menu_field_callback(void* context) {
furi_assert(context);
2020-10-18 22:55:41 +00:00
Nfc* nfc = context;
NfcMessage message;
message.type = NfcMessageTypeField;
furi_check(osMessageQueuePut(nfc->message_queue, &message, 0, osWaitForever) == osOK);
2020-10-18 22:09:48 +00:00
}
void nfc_menu_field_off_callback(void* context) {
furi_assert(context);
2020-10-18 22:55:41 +00:00
Nfc* nfc = context;
NfcMessage message;
message.type = NfcMessageTypeField;
furi_check(osMessageQueuePut(nfc->message_queue, &message, 0, osWaitForever) == osOK);
2020-10-18 22:09:48 +00:00
}
void nfc_cli_detect(string_t args, void* context) {
furi_assert(context);
Nfc* nfc = context;
NfcWorkerState state = nfc_worker_get_state(nfc->worker);
if(state != NfcWorkerStateReady) {
printf("Nfc is busy");
return;
}
NfcMessage message;
message.type = NfcMessageTypeDetectCliCmd;
furi_check(osMessageQueuePut(nfc->message_queue, &message, 0, osWaitForever) == osOK);
// Wait until nfc task send response
furi_check(osMessageQueueGet(nfc->cli_message_queue, &message, NULL, osWaitForever) == osOK);
if(message.type == NfcMessageTypeDeviceFound) {
if(message.device.type == NfcDeviceTypeNfca) {
printf(
"Found NFC-A, type: %s, UID length: %d, UID:",
nfc_get_nfca_type(message.device.nfca.type),
message.device.nfca.nfcId1Len);
for(uint8_t i = 0; i < message.device.nfca.nfcId1Len; i++) {
printf("%02X", message.device.nfca.nfcId1[i]);
}
printf(
" SAK: %02X ATQA: %02X/%02X",
message.device.nfca.selRes.sak,
message.device.nfca.sensRes.anticollisionInfo,
message.device.nfca.sensRes.platformInfo);
} else if(message.device.type == NfcDeviceTypeNfcb) {
printf("Found NFC-B, UID length: %d, UID:", RFAL_NFCB_NFCID0_LEN);
for(uint8_t i = 0; i < RFAL_NFCB_NFCID0_LEN; i++) {
printf("%02X", message.device.nfcb.sensbRes.nfcid0[i]);
}
} else if(message.device.type == NfcDeviceTypeNfcv) {
printf("Found NFC-V, UID length: %d, UID:", RFAL_NFCV_UID_LEN);
for(uint8_t i = 0; i < RFAL_NFCV_UID_LEN; i++) {
printf("%02X", message.device.nfcv.InvRes.UID[i]);
}
} else if(message.device.type == NfcDeviceTypeNfcf) {
printf("Found NFC-F, UID length: %d, UID:", RFAL_NFCF_NFCID2_LEN);
for(uint8_t i = 0; i < RFAL_NFCF_NFCID2_LEN; i++) {
printf("%02X", message.device.nfcf.sensfRes.NFCID2[i]);
}
}
} else {
printf("Device not found");
}
}
void nfc_start(Nfc* nfc, NfcView view_id, NfcWorkerState worker_state) {
NfcWorkerState state = nfc_worker_get_state(nfc->worker);
if(state == NfcWorkerStateBroken) {
with_view_model(
nfc->view_error, (NfcViewErrorModel * model) {
model->error = nfc_worker_get_error(nfc->worker);
return true;
});
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewError);
} else if(state == NfcWorkerStateReady) {
view_dispatcher_switch_to_view(nfc->view_dispatcher, view_id);
nfc_worker_start(nfc->worker, worker_state);
}
2020-10-18 22:09:48 +00:00
}
int32_t nfc_task(void* p) {
2020-10-18 22:55:41 +00:00
Nfc* nfc = nfc_alloc();
2020-10-18 22:09:48 +00:00
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
Gui* gui = furi_record_open("gui");
view_dispatcher_attach_to_gui(nfc->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
2020-10-20 13:20:53 +00:00
nfc->cli = furi_record_open("cli");
if(nfc->cli) {
cli_add_command(nfc->cli, "nfc_detect", nfc_cli_detect, nfc);
}
with_value_mutex(
nfc->menu_vm, (Menu * menu) { menu_item_add(menu, nfc->menu); });
2020-10-18 22:09:48 +00:00
[FL-140] Core api dynamic records (#296) * SYSTEM: tickless mode with deep sleep. * Move FreeRTOS ticks to lptim2 * API: move all sumbodules init routines to one place. Timebase: working lptim2 at tick source. * API Timebase: lp-timer routines, timer access safe zones prediction and synchronization. FreeRTOS: adjust configuration for tickless mode. * NFC: support for tickless mode. * API Timebase: improve tick error handling in IRQ. Apploader: use insomnia mode to run applications. * BLE: prevent sleep while core2 starting * HAL: nap while in insomnia mode * init records work * try to implement record delete * tests and flapp * flapp subsystem * new core functions to get app stat, simplify core code * fix thread termination * add strdup to core * fix tests * Refactoring: remove all unusued parts, update API usage, aggreagate API sources and headers, new record storage * Refactoring: update furi record api usage, cleanup code * Fix broken merge for freertos apps * Core, Target: fix compilation warnings * Drop firmware target local * HAL Timebase, Power, Clock: semaphore guarded access to clock and power modes, better sleep mode. * SD-Filesystem: wait for all deps to arrive before adding widget. Core, BLE: disable debug dump to serial. * delete old app example-ipc * delete old app fatfs list * fix strobe app, add input header * delete old display driver * comment old app qr-code * fix sd-card test, add forced widget update * remove unused new core test * increase heap to 128k * comment and assert old core tests * fix syntax Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
2021-01-20 16:09:26 +00:00
furi_record_create("nfc", nfc);
2020-10-18 22:55:41 +00:00
NfcMessage message;
2020-10-18 22:09:48 +00:00
while(1) {
furi_check(osMessageQueueGet(nfc->message_queue, &message, NULL, osWaitForever) == osOK);
if(message.type == NfcMessageTypeDetect) {
with_view_model(
nfc->view_detect, (NfcViewReadModel * model) {
model->found = false;
return true;
});
nfc_start(nfc, NfcViewRead, NfcWorkerStatePoll);
} else if(message.type == NfcMessageTypeDetectCliCmd) {
nfc_start(nfc, NfcViewCli, NfcWorkerStatePollOnce);
} else if(message.type == NfcMessageTypeEmulate) {
nfc_start(nfc, NfcViewEmulate, NfcWorkerStateEmulate);
} else if(message.type == NfcMessageTypeField) {
nfc_start(nfc, NfcViewField, NfcWorkerStateField);
} else if(message.type == NfcMessageTypeStop) {
nfc_worker_stop(nfc->worker);
} else if(message.type == NfcMessageTypeDeviceFound) {
NfcWorkerState state = nfc_worker_get_state(nfc->worker);
if(state == NfcWorkerStatePollOnce) {
view_dispatcher_switch_to_view(nfc->view_dispatcher, VIEW_NONE);
furi_check(
osMessageQueuePut(nfc->cli_message_queue, &message, 0, osWaitForever) == osOK);
nfc_worker_stop(nfc->worker);
} else {
with_view_model(
nfc->view_detect, (NfcViewReadModel * model) {
model->found = true;
model->device = message.device;
return true;
});
}
} else if(message.type == NfcMessageTypeDeviceNotFound) {
NfcWorkerState state = nfc_worker_get_state(nfc->worker);
if(state == NfcWorkerStatePollOnce) {
view_dispatcher_switch_to_view(nfc->view_dispatcher, VIEW_NONE);
furi_check(
osMessageQueuePut(nfc->cli_message_queue, &message, 0, osWaitForever) == osOK);
nfc_worker_stop(nfc->worker);
} else {
with_view_model(
nfc->view_detect, (NfcViewReadModel * model) {
model->found = false;
return true;
});
}
2020-10-18 22:09:48 +00:00
}
}
return 0;
2020-10-18 22:09:48 +00:00
}