Skip to content

Commit

Permalink
Merge pull request #4 from acegoal07/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
acegoal07 authored Jan 20, 2025
2 parents 37afb21 + 7cf4f73 commit 7700a6e
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 117 deletions.
4 changes: 2 additions & 2 deletions application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ App(
name="NFC Comparator",
apptype=FlipperAppType.EXTERNAL,
entry_point="nfc_comparator_main",
stack_size=4 * 1024,
stack_size=2 * 1024,
fap_category="NFC",
fap_version="1.0",
fap_version="1.1",
fap_icon="assets/Comparator_10px.png",
fap_description="A tool to check if a physical NFC card is the same as a saved one",
fap_author="acegoal07",
Expand Down
186 changes: 97 additions & 89 deletions lib/reader_worker/nfc_comparator_reader_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ NfcComparatorReaderWorker* nfc_comparator_reader_worker_alloc() {
NfcComparatorReaderWorker* nfc_comparator_reader_worker =
malloc(sizeof(NfcComparatorReaderWorker));
nfc_comparator_reader_worker->nfc = nfc_alloc();
nfc_comparator_reader_worker->nfc_scanner =
nfc_scanner_alloc(nfc_comparator_reader_worker->nfc);
nfc_comparator_reader_worker->thread = furi_thread_alloc_ex(
"NfcComparatorReaderWorker",
4096,
1024,
nfc_comparator_reader_worker_task,
nfc_comparator_reader_worker);
nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped;
Expand All @@ -19,11 +17,29 @@ void nfc_comparator_reader_worker_free(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
nfc_free(nfc_comparator_reader_worker->nfc);
nfc_scanner_free(nfc_comparator_reader_worker->nfc_scanner);
furi_thread_free(nfc_comparator_reader_worker->thread);
nfc_device_free(nfc_comparator_reader_worker->loaded_nfc_card);
free(nfc_comparator_reader_worker);
}

void nfc_comparator_reader_worker_stop(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
if(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) {
nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped;
furi_thread_join(nfc_comparator_reader_worker->thread);
}
}

void nfc_comparator_reader_worker_start(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
if(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Stopped) {
nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning;
furi_thread_start(nfc_comparator_reader_worker->thread);
}
}

void nfc_comparator_reader_worker_scanner_callback(NfcScannerEvent event, void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
Expand All @@ -48,80 +64,90 @@ NfcCommand nfc_comparator_reader_worker_poller_callback(NfcGenericEvent event, v

int32_t nfc_comparator_reader_worker_task(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
nfc_comparator_reader_worker->nfc_scanner =
nfc_scanner_alloc(nfc_comparator_reader_worker->nfc);

nfc_scanner_start(
nfc_comparator_reader_worker->nfc_scanner,
nfc_comparator_reader_worker_scanner_callback,
nfc_comparator_reader_worker);

nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning;
while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Scanning) {
furi_delay_ms(100);
}
nfc_scanner_stop(nfc_comparator_reader_worker->nfc_scanner);

NfcPoller* nfc_poller = nfc_poller_alloc(
nfc_comparator_reader_worker->nfc, nfc_comparator_reader_worker->protocol[0]);

nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Polling;
nfc_poller_start(
nfc_poller, nfc_comparator_reader_worker_poller_callback, nfc_comparator_reader_worker);

while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Polling) {
furi_delay_ms(100);
}

nfc_poller_stop(nfc_poller);

NfcDevice* data = nfc_device_alloc();
nfc_device_set_data(
data,
nfc_comparator_reader_worker->protocol[0],
(NfcDeviceData*)nfc_poller_get_data(nfc_poller));

nfc_comparator_reader_worker->compare_checks.protocol =
nfc_device_get_protocol_name(nfc_device_get_protocol(data)) ==
while(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) {
switch(nfc_comparator_reader_worker->state) {
case NfcComparatorReaderWorkerState_Scanning: {
NfcScanner* nfc_scanner = nfc_scanner_alloc(nfc_comparator_reader_worker->nfc);
nfc_scanner_start(
nfc_scanner,
nfc_comparator_reader_worker_scanner_callback,
nfc_comparator_reader_worker);
while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Scanning) {
furi_delay_ms(100);
}
nfc_scanner_stop(nfc_scanner);
break;
}
case NfcComparatorReaderWorkerState_Polling: {
NfcPoller* nfc_poller = nfc_poller_alloc(
nfc_comparator_reader_worker->nfc, nfc_comparator_reader_worker->protocol[0]);
nfc_poller_start(
nfc_poller,
nfc_comparator_reader_worker_poller_callback,
nfc_comparator_reader_worker);
while(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Polling) {
furi_delay_ms(100);
}
nfc_poller_stop(nfc_poller);
nfc_comparator_reader_worker->scanned_nfc_card = nfc_device_alloc();
nfc_device_set_data(
nfc_comparator_reader_worker->scanned_nfc_card,
nfc_comparator_reader_worker->protocol[0],
(NfcDeviceData*)nfc_poller_get_data(nfc_poller));
nfc_poller_free(nfc_poller);
break;
}
case NfcComparatorReaderWorkerState_Comparing: {
nfc_comparator_reader_worker->compare_checks.protocol =
nfc_device_get_protocol_name(
nfc_device_get_protocol(nfc_comparator_reader_worker->loaded_nfc_card)) ?
true :
false;

nfc_poller_free(nfc_poller);

size_t poller_uid_len = 0;
const uint8_t* poller_uid = nfc_device_get_uid(data, &poller_uid_len);
FuriString* poller_uid_str = furi_string_alloc();
for(size_t i = 0; i < poller_uid_len; i++) {
furi_string_utf8_push(poller_uid_str, poller_uid[i]);
nfc_device_get_protocol(nfc_comparator_reader_worker->scanned_nfc_card)) ==
nfc_device_get_protocol_name(
nfc_device_get_protocol(nfc_comparator_reader_worker->loaded_nfc_card)) ?
true :
false;

size_t poller_uid_len = 0;
const uint8_t* poller_uid =
nfc_device_get_uid(nfc_comparator_reader_worker->scanned_nfc_card, &poller_uid_len);
FuriString* poller_uid_str = furi_string_alloc();
for(size_t i = 0; i < poller_uid_len; i++) {
furi_string_utf8_push(poller_uid_str, poller_uid[i]);
}

nfc_device_free(nfc_comparator_reader_worker->scanned_nfc_card);

size_t loaded_uid_len = 0;
const uint8_t* loaded_uid =
nfc_device_get_uid(nfc_comparator_reader_worker->loaded_nfc_card, &loaded_uid_len);
FuriString* loaded_uid_str = furi_string_alloc();
for(size_t i = 0; i < loaded_uid_len; i++) {
furi_string_utf8_push(loaded_uid_str, loaded_uid[i]);
}

nfc_comparator_reader_worker->compare_checks.uid =
furi_string_cmpi(loaded_uid_str, poller_uid_str) == 0 ? true : false;

nfc_comparator_reader_worker->compare_checks.uid_length =
poller_uid_len == loaded_uid_len ? true : false;

furi_string_free(poller_uid_str);
furi_string_free(loaded_uid_str);

nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped;
break;
}
default:
break;
}
}

nfc_device_free(data);

size_t loaded_uid_len = 0;
const uint8_t* loaded_uid =
nfc_device_get_uid(nfc_comparator_reader_worker->loaded_nfc_card, &loaded_uid_len);
FuriString* loaded_uid_str = furi_string_alloc();
for(size_t i = 0; i < loaded_uid_len; i++) {
furi_string_utf8_push(loaded_uid_str, loaded_uid[i]);
}

nfc_comparator_reader_worker->compare_checks.uid =
furi_string_cmpi(loaded_uid_str, poller_uid_str) == 0 ? true : false;

furi_string_free(poller_uid_str);
furi_string_free(loaded_uid_str);

nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped;

return 0;
}

void nfc_comparator_reader_worker_set_compare_nfc_device(void* context, NfcDevice* nfc_device) {
void nfc_comparator_reader_worker_set_loaded_nfc_card(void* context, const char* path_to_nfc_card) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
nfc_comparator_reader_worker->loaded_nfc_card = nfc_device;
nfc_comparator_reader_worker->loaded_nfc_card = nfc_device_alloc();
nfc_device_load(nfc_comparator_reader_worker->loaded_nfc_card, path_to_nfc_card);
}

bool nfc_comparator_reader_worker_is_running(void* context) {
Expand All @@ -135,24 +161,6 @@ NfcComparatorReaderWorkerState nfc_comparator_reader_worker_get_state(void* cont
return nfc_comparator_reader_worker->state;
}

void nfc_comparator_reader_worker_stop(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
if(nfc_comparator_reader_worker->state != NfcComparatorReaderWorkerState_Stopped) {
nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Stopped;
}
furi_thread_join(nfc_comparator_reader_worker->thread);
}

void nfc_comparator_reader_worker_start(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
furi_assert(nfc_comparator_reader_worker);
if(nfc_comparator_reader_worker->state == NfcComparatorReaderWorkerState_Stopped) {
nfc_comparator_reader_worker->state = NfcComparatorReaderWorkerState_Scanning;
furi_thread_start(nfc_comparator_reader_worker->thread);
}
}

NfcComparatorReaderWorkerCompareChecks
nfc_comparator_reader_worker_get_compare_checks(void* context) {
NfcComparatorReaderWorker* nfc_comparator_reader_worker = context;
Expand Down
11 changes: 7 additions & 4 deletions lib/reader_worker/nfc_comparator_reader_worker.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include <furi.h>
#include <furi_hal.h>

#include <nfc/nfc.h>
#include <nfc/nfc_device.h>
#include <nfc/nfc_poller.h>
Expand All @@ -15,6 +16,7 @@ typedef enum {

typedef struct {
bool uid;
bool uid_length;
bool protocol;
} NfcComparatorReaderWorkerCompareChecks;

Expand All @@ -24,22 +26,23 @@ typedef struct {
NfcProtocol* protocol;
NfcComparatorReaderWorkerState state;
NfcDevice* loaded_nfc_card;
NfcDevice* scanned_nfc_card;
NfcPoller* nfc_poller;
NfcScanner* nfc_scanner;
NfcComparatorReaderWorkerCompareChecks compare_checks;
} NfcComparatorReaderWorker;

NfcComparatorReaderWorker* nfc_comparator_reader_worker_alloc();
void nfc_comparator_reader_worker_free(void* context);
void nfc_comparator_reader_worker_stop(void* context);
void nfc_comparator_reader_worker_start(void* context);

void nfc_comparator_reader_worker_scanner_callback(NfcScannerEvent event, void* context);
NfcCommand nfc_comparator_reader_worker_poller_callback(NfcGenericEvent event, void* context);

int32_t nfc_comparator_reader_worker_task(void* context);
void nfc_comparator_reader_worker_set_compare_nfc_device(void* context, NfcDevice* nfc_device);

void nfc_comparator_reader_worker_set_loaded_nfc_card(void* context, const char* path_to_nfc_card);
bool nfc_comparator_reader_worker_is_running(void* context);
NfcComparatorReaderWorkerState nfc_comparator_reader_worker_get_state(void* context);
void nfc_comparator_reader_worker_stop(void* context);
void nfc_comparator_reader_worker_start(void* context);
NfcComparatorReaderWorkerCompareChecks
nfc_comparator_reader_worker_get_compare_checks(void* context);
3 changes: 0 additions & 3 deletions nfc_comparator.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ static void nfc_comparator_free(NfcComparator* nfc_comparator) {
submenu_free(nfc_comparator->submenu);
file_browser_free(nfc_comparator->file_browser);
furi_string_free(nfc_comparator->file_browser_output);
if(nfc_comparator->loaded_nfc_card) {
nfc_device_free(nfc_comparator->loaded_nfc_card);
}
popup_free(nfc_comparator->popup);

free(nfc_comparator);
Expand Down
4 changes: 1 addition & 3 deletions nfc_comparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
#include <furi.h>
#include <furi_hal.h>

#include <string.h>

#include "nfc_comparator_icons.h"

#include <gui/gui.h>
Expand Down Expand Up @@ -40,7 +38,7 @@ typedef struct {
FileBrowser* file_browser;
FuriString* file_browser_output;
Popup* popup;
NfcDevice* loaded_nfc_card;
NfcComparatorReaderWorker* worker;
} NfcComparator;

#define NFC_ITEM_LOCATION "/ext/nfc/"
19 changes: 8 additions & 11 deletions scenes/nfc_comparator_scene_comparator.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@

#include "../nfc_comparator.h"

void nfc_comparator_comparator_menu_callback(void* context, uint32_t index) {
NfcComparator* nfc_comparator = context;
scene_manager_handle_custom_event(nfc_comparator->scene_manager, index);
}

void nfc_comparator_comparator_scene_on_enter(void* context) {
NfcComparator* nfc_comparator = context;
furi_assert(nfc_comparator);
Expand All @@ -14,7 +9,8 @@ void nfc_comparator_comparator_scene_on_enter(void* context) {
view_dispatcher_switch_to_view(nfc_comparator->view_dispatcher, NfcComparatorView_Popup);

NfcComparatorReaderWorker* worker = nfc_comparator_reader_worker_alloc();
nfc_comparator_reader_worker_set_compare_nfc_device(worker, nfc_comparator->loaded_nfc_card);
nfc_comparator_reader_worker_set_loaded_nfc_card(
worker, furi_string_get_cstr(nfc_comparator->file_browser_output));
nfc_comparator_reader_worker_start(worker);

while(nfc_comparator_reader_worker_is_running(worker)) {
Expand All @@ -33,6 +29,7 @@ void nfc_comparator_comparator_scene_on_enter(void* context) {
}
furi_delay_ms(100);
}

nfc_comparator_reader_worker_stop(worker);

popup_set_header(nfc_comparator->popup, "Compare Results", 64, 5, AlignCenter, AlignTop);
Expand All @@ -44,23 +41,23 @@ void nfc_comparator_comparator_scene_on_enter(void* context) {
FuriString* comparator = furi_string_alloc();
furi_string_printf(
comparator,
"UID: %s\nProtocol: %s",
"UID: %s\nUID length: %s\nProtocol: %s",
checks.uid ? "Match" : "Mismatch",
checks.uid_length ? "Match" : "Mismatch",
checks.protocol ? "Match" : "Mismatch");

char result_buffer[158];
strncpy(result_buffer, furi_string_get_cstr(comparator), sizeof(result_buffer) - 1);

furi_string_free(comparator);

popup_set_text(nfc_comparator->popup, result_buffer, 64, 30, AlignCenter, AlignCenter);
popup_set_text(nfc_comparator->popup, result_buffer, 64, 35, AlignCenter, AlignCenter);
}

bool nfc_comparator_comparator_scene_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
UNUSED(event);
bool consumed = false;
return consumed;
UNUSED(context);
return false;
}

void nfc_comparator_comparator_scene_on_exit(void* context) {
Expand Down
2 changes: 1 addition & 1 deletion scenes/nfc_comparator_scene_main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void nfc_comparator_main_menu_scene_on_enter(void* context) {
NfcComparatorMainMenu_StartComparator,
nfc_comparator_main_menu_menu_callback,
nfc_comparator,
!nfc_comparator->loaded_nfc_card,
furi_string_empty(nfc_comparator->file_browser_output),
"No NFC\ncard selected");

submenu_add_item(
Expand Down
4 changes: 0 additions & 4 deletions scenes/nfc_comparator_scene_select_nfc_card.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

void nfc_comparator_select_nfc_card_menu_callback(void* context) {
NfcComparator* nfc_comparator = context;
nfc_comparator->loaded_nfc_card = nfc_device_alloc();
nfc_device_load(
nfc_comparator->loaded_nfc_card, furi_string_get_cstr(nfc_comparator->file_browser_output));
furi_string_reset(nfc_comparator->file_browser_output);
scene_manager_previous_scene(nfc_comparator->scene_manager);
}

Expand Down

0 comments on commit 7700a6e

Please sign in to comment.