[FL-1919] NFC rework with Flipper File Format (#756)

* nfc: allocate nfc device on heap
* nfc: rework save with flipper file format
* nfc: rework nfc device load with flipper file
* nfc: save AID length and data
* nfc: remove file worker usage
* nfc: format sources
* nfc: rework with flipper file format addons
* assets: update EMV resources with flipper file format
* nfc: rework EMV resources parsing with new file format
* assets: fix EMV AID file format
* nfc: fix nfc_device usage

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-11-09 00:55:06 +03:00
committed by GitHub
parent 54dc16134d
commit 90d450368c
36 changed files with 1008 additions and 1070 deletions

View File

@@ -1,49 +1,80 @@
#include "nfc_emv_parser.h"
#include <lib/flipper_file/flipper_file.h>
#include <file-worker.h>
static const char* nfc_resources_header = "Flipper EMV resources";
static const uint32_t nfc_resources_file_version = 1;
static bool
nfc_emv_parser_get_value(const char* file_path, string_t key, char delimiter, string_t value) {
bool found = false;
FileWorker* file_worker = file_worker_alloc(true);
static bool nfc_emv_parser_search_data(
Storage* storage,
const char* file_name,
string_t key,
string_t data) {
bool parsed = false;
FlipperFile* file = flipper_file_alloc(storage);
string_t temp_str;
string_init(temp_str);
if(file_worker_open(file_worker, file_path, FSAM_READ, FSOM_OPEN_EXISTING)) {
if(file_worker_get_value_from_key(file_worker, key, delimiter, value)) {
found = true;
}
}
do {
// Open file
if(!flipper_file_open_existing(file, file_name)) break;
// Read file header and version
uint32_t version = 0;
if(!flipper_file_read_header(file, temp_str, &version)) break;
if(string_cmp_str(temp_str, nfc_resources_header) ||
(version != nfc_resources_file_version))
break;
if(!flipper_file_read_string(file, string_get_cstr(key), data)) break;
parsed = true;
} while(false);
file_worker_close(file_worker);
file_worker_free(file_worker);
return found;
string_clear(temp_str);
flipper_file_free(file);
return parsed;
}
bool nfc_emv_parser_get_aid_name(uint8_t* aid, uint8_t aid_len, string_t aid_name) {
bool result = false;
bool nfc_emv_parser_get_aid_name(
Storage* storage,
uint8_t* aid,
uint8_t aid_len,
string_t aid_name) {
furi_assert(storage);
bool parsed = false;
string_t key;
string_init(key);
for(uint8_t i = 0; i < aid_len; i++) {
string_cat_printf(key, "%02X", aid[i]);
}
result = nfc_emv_parser_get_value("/ext/nfc/emv/aid.nfc", key, ' ', aid_name);
if(nfc_emv_parser_search_data(storage, "/ext/nfc/emv/aid.nfc", key, aid_name)) {
parsed = true;
}
string_clear(key);
return result;
return parsed;
}
bool nfc_emv_parser_get_country_name(uint16_t country_code, string_t country_name) {
bool result = false;
bool nfc_emv_parser_get_country_name(
Storage* storage,
uint16_t country_code,
string_t country_name) {
bool parsed = false;
string_t key;
string_init_printf(key, "%04X", country_code);
result = nfc_emv_parser_get_value("/ext/nfc/emv/country_code.nfc", key, ' ', country_name);
if(nfc_emv_parser_search_data(storage, "/ext/nfc/emv/country_code.nfc", key, country_name)) {
parsed = true;
}
string_clear(key);
return result;
return parsed;
}
bool nfc_emv_parser_get_currency_name(uint16_t currency_code, string_t currency_name) {
bool result = false;
bool nfc_emv_parser_get_currency_name(
Storage* storage,
uint16_t currency_code,
string_t currency_name) {
bool parsed = false;
string_t key;
string_init_printf(key, "%04X", currency_code);
result = nfc_emv_parser_get_value("/ext/nfc/emv/currency_code.nfc", key, ' ', currency_name);
if(nfc_emv_parser_search_data(storage, "/ext/nfc/emv/currency_code.nfc", key, currency_name)) {
parsed = true;
}
string_clear(key);
return result;
return parsed;
}

View File

@@ -3,25 +3,39 @@
#include <stdint.h>
#include <stdbool.h>
#include <m-string.h>
#include <storage/storage.h>
/** Get EMV application name by number
* @param storage Storage instance
* @param aid - AID number array
* @param aid_len - AID length
* @param aid_name - string to keep AID name
* @return - true if AID found, false otherwies
*/
bool nfc_emv_parser_get_aid_name(uint8_t* aid, uint8_t aid_len, string_t aid_name);
bool nfc_emv_parser_get_aid_name(
Storage* storage,
uint8_t* aid,
uint8_t aid_len,
string_t aid_name);
/** Get country name by country code
* @param storage Storage instance
* @param country_code - ISO 3166 country code
* @param country_name - string to keep country name
* @return - true if country found, false otherwies
*/
bool nfc_emv_parser_get_country_name(uint16_t country_code, string_t country_name);
bool nfc_emv_parser_get_country_name(
Storage* storage,
uint16_t country_code,
string_t country_name);
/** Get currency name by currency code
* @param storage Storage instance
* @param currency_code - ISO 3166 currency code
* @param currency_name - string to keep currency name
* @return - true if currency found, false otherwies
*/
bool nfc_emv_parser_get_currency_name(uint16_t currency_code, string_t currency_name);
bool nfc_emv_parser_get_currency_name(
Storage* storage,
uint16_t currency_code,
string_t currency_name);