2022-09-03 12:25:36 +00:00
|
|
|
#include "detect_reader.h"
|
2022-10-28 14:08:50 +00:00
|
|
|
#include <assets_icons.h>
|
2022-09-03 12:25:36 +00:00
|
|
|
#include <gui/elements.h>
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
#define DETECT_READER_UID_MAX_LEN (10)
|
|
|
|
|
2022-09-03 12:25:36 +00:00
|
|
|
struct DetectReader {
|
|
|
|
View* view;
|
|
|
|
DetectReaderDoneCallback callback;
|
|
|
|
void* context;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint16_t nonces;
|
2022-10-06 16:58:17 +00:00
|
|
|
uint16_t nonces_max;
|
|
|
|
DetectReaderState state;
|
2022-10-28 16:10:16 +00:00
|
|
|
FuriString* uid_str;
|
2022-09-03 12:25:36 +00:00
|
|
|
} DetectReaderViewModel;
|
|
|
|
|
|
|
|
static void detect_reader_draw_callback(Canvas* canvas, void* model) {
|
|
|
|
DetectReaderViewModel* m = model;
|
|
|
|
char text[32] = {};
|
|
|
|
|
2022-10-06 16:58:17 +00:00
|
|
|
// Draw header and icon
|
|
|
|
canvas_draw_icon(canvas, 0, 16, &I_Modern_reader_18x34);
|
|
|
|
if(m->state == DetectReaderStateStart) {
|
|
|
|
snprintf(text, sizeof(text), "Touch the reader");
|
|
|
|
canvas_draw_icon(canvas, 21, 13, &I_Move_flipper_26x39);
|
2022-10-28 16:10:16 +00:00
|
|
|
if(furi_string_size(m->uid_str)) {
|
|
|
|
elements_multiline_text_aligned(
|
|
|
|
canvas, 64, 64, AlignCenter, AlignBottom, furi_string_get_cstr(m->uid_str));
|
|
|
|
}
|
2022-10-06 16:58:17 +00:00
|
|
|
} else if(m->state == DetectReaderStateReaderDetected) {
|
|
|
|
snprintf(text, sizeof(text), "Move the Flipper away");
|
|
|
|
canvas_draw_icon(canvas, 24, 25, &I_Release_arrow_18x15);
|
|
|
|
} else if(m->state == DetectReaderStateReaderLost) {
|
|
|
|
snprintf(text, sizeof(text), "Touch the reader again");
|
|
|
|
canvas_draw_icon(canvas, 21, 13, &I_Move_flipper_26x39);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas_draw_str_aligned(canvas, 64, 0, AlignCenter, AlignTop, text);
|
2022-09-03 12:25:36 +00:00
|
|
|
|
2022-10-06 16:58:17 +00:00
|
|
|
// Draw collected nonces
|
|
|
|
if(m->state == DetectReaderStateStart) {
|
2022-09-03 12:25:36 +00:00
|
|
|
canvas_set_font(canvas, FontPrimary);
|
2022-10-06 16:58:17 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 51, 22, AlignLeft, AlignTop, "Emulating...");
|
2022-09-03 12:25:36 +00:00
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2022-10-06 16:58:17 +00:00
|
|
|
canvas_draw_str_aligned(canvas, 51, 35, AlignLeft, AlignTop, "MIFARE MFkey32");
|
2022-09-03 12:25:36 +00:00
|
|
|
} else {
|
2022-10-06 16:58:17 +00:00
|
|
|
if(m->state == DetectReaderStateDone) {
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
canvas_draw_str_aligned(canvas, 51, 22, AlignLeft, AlignTop, "Completed!");
|
2024-01-14 05:47:38 +00:00
|
|
|
canvas_draw_icon(canvas, 24, 23, &I_check_big_20x17);
|
2022-10-06 16:58:17 +00:00
|
|
|
} else {
|
|
|
|
canvas_set_font(canvas, FontPrimary);
|
|
|
|
canvas_draw_str_aligned(canvas, 51, 22, AlignLeft, AlignTop, "Collecting...");
|
|
|
|
}
|
2022-09-03 12:25:36 +00:00
|
|
|
canvas_set_font(canvas, FontSecondary);
|
2022-10-06 16:58:17 +00:00
|
|
|
snprintf(text, sizeof(text), "Nonce pairs: %d/%d", m->nonces, m->nonces_max);
|
|
|
|
canvas_draw_str_aligned(canvas, 51, 35, AlignLeft, AlignTop, text);
|
|
|
|
}
|
|
|
|
// Draw button
|
|
|
|
if(m->nonces > 0) {
|
|
|
|
elements_button_center(canvas, "Done");
|
2022-09-03 12:25:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool detect_reader_input_callback(InputEvent* event, void* context) {
|
|
|
|
DetectReader* detect_reader = context;
|
|
|
|
furi_assert(detect_reader->callback);
|
|
|
|
bool consumed = false;
|
|
|
|
|
|
|
|
uint8_t nonces = 0;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
detect_reader->view, DetectReaderViewModel * model, { nonces = model->nonces; }, false);
|
2022-09-03 12:25:36 +00:00
|
|
|
|
|
|
|
if(event->type == InputTypeShort) {
|
2022-10-06 16:58:17 +00:00
|
|
|
if(event->key == InputKeyOk) {
|
2022-09-03 12:25:36 +00:00
|
|
|
if(nonces > 0) {
|
|
|
|
detect_reader->callback(detect_reader->context);
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|
|
|
|
|
2024-03-25 10:53:32 +00:00
|
|
|
DetectReader* detect_reader_alloc(void) {
|
2022-09-03 12:25:36 +00:00
|
|
|
DetectReader* detect_reader = malloc(sizeof(DetectReader));
|
|
|
|
detect_reader->view = view_alloc();
|
|
|
|
view_allocate_model(detect_reader->view, ViewModelTypeLocking, sizeof(DetectReaderViewModel));
|
|
|
|
view_set_draw_callback(detect_reader->view, detect_reader_draw_callback);
|
|
|
|
view_set_input_callback(detect_reader->view, detect_reader_input_callback);
|
|
|
|
view_set_context(detect_reader->view, detect_reader);
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
with_view_model(
|
|
|
|
detect_reader->view,
|
|
|
|
DetectReaderViewModel * model,
|
|
|
|
{ model->uid_str = furi_string_alloc(); },
|
|
|
|
false);
|
|
|
|
|
2022-09-03 12:25:36 +00:00
|
|
|
return detect_reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
void detect_reader_free(DetectReader* detect_reader) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
|
2022-10-28 16:10:16 +00:00
|
|
|
with_view_model(
|
|
|
|
detect_reader->view,
|
|
|
|
DetectReaderViewModel * model,
|
|
|
|
{ furi_string_free(model->uid_str); },
|
|
|
|
false);
|
|
|
|
|
2022-09-03 12:25:36 +00:00
|
|
|
view_free(detect_reader->view);
|
|
|
|
free(detect_reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void detect_reader_reset(DetectReader* detect_reader) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
detect_reader->view,
|
|
|
|
DetectReaderViewModel * model,
|
|
|
|
{
|
2022-09-03 12:25:36 +00:00
|
|
|
model->nonces = 0;
|
2022-10-06 16:58:17 +00:00
|
|
|
model->nonces_max = 0;
|
|
|
|
model->state = DetectReaderStateStart;
|
2022-10-28 16:10:16 +00:00
|
|
|
furi_string_reset(model->uid_str);
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2022-09-03 12:25:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
View* detect_reader_get_view(DetectReader* detect_reader) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
|
|
|
|
return detect_reader->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
void detect_reader_set_callback(
|
|
|
|
DetectReader* detect_reader,
|
|
|
|
DetectReaderDoneCallback callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
furi_assert(callback);
|
|
|
|
|
|
|
|
detect_reader->callback = callback;
|
|
|
|
detect_reader->context = context;
|
|
|
|
}
|
|
|
|
|
2022-10-06 16:58:17 +00:00
|
|
|
void detect_reader_set_nonces_max(DetectReader* detect_reader, uint16_t nonces_max) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
detect_reader->view,
|
|
|
|
DetectReaderViewModel * model,
|
|
|
|
{ model->nonces_max = nonces_max; },
|
|
|
|
false);
|
2022-10-06 16:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void detect_reader_set_nonces_collected(DetectReader* detect_reader, uint16_t nonces_collected) {
|
2022-09-03 12:25:36 +00:00
|
|
|
furi_assert(detect_reader);
|
2022-10-06 16:58:17 +00:00
|
|
|
|
2022-09-03 12:25:36 +00:00
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
detect_reader->view,
|
|
|
|
DetectReaderViewModel * model,
|
NFC refactoring (#3050)
"A long time ago in a galaxy far, far away...." we started NFC subsystem refactoring.
Starring:
- @gornekich - NFC refactoring project lead, architect, senior developer
- @gsurkov - architect, senior developer
- @RebornedBrain - senior developer
Supporting roles:
- @skotopes, @DrZlo13, @hedger - general architecture advisors, code review
- @Astrrra, @doomwastaken, @Hellitron, @ImagineVagon333 - quality assurance
Special thanks:
@bettse, @pcunning, @nxv, @noproto, @AloneLiberty and everyone else who has been helping us all this time and contributing valuable knowledges, ideas and source code.
2023-10-24 03:08:09 +00:00
|
|
|
{
|
|
|
|
model->nonces = nonces_collected;
|
|
|
|
model->state = DetectReaderStateReaderDetected;
|
|
|
|
},
|
2022-10-08 17:38:29 +00:00
|
|
|
false);
|
2022-09-03 12:25:36 +00:00
|
|
|
}
|
2022-10-06 16:58:17 +00:00
|
|
|
|
|
|
|
void detect_reader_set_state(DetectReader* detect_reader, DetectReaderState state) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
detect_reader->view, DetectReaderViewModel * model, { model->state = state; }, true);
|
2022-10-06 16:58:17 +00:00
|
|
|
}
|
2022-10-28 16:10:16 +00:00
|
|
|
|
|
|
|
void detect_reader_set_uid(DetectReader* detect_reader, uint8_t* uid, uint8_t uid_len) {
|
|
|
|
furi_assert(detect_reader);
|
|
|
|
furi_assert(uid);
|
|
|
|
furi_assert(uid_len < DETECT_READER_UID_MAX_LEN);
|
|
|
|
with_view_model(
|
|
|
|
detect_reader->view,
|
|
|
|
DetectReaderViewModel * model,
|
|
|
|
{
|
|
|
|
furi_string_set_str(model->uid_str, "UID:");
|
|
|
|
for(size_t i = 0; i < uid_len; i++) {
|
|
|
|
furi_string_cat_printf(model->uid_str, " %02X", uid[i]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
true);
|
|
|
|
}
|