[FL-3810] Felica emulation (#3673)

* Moved some structs and defs from poller to generic felica
* Buffer size increased for transferring more data
* Felica HAL Tx function implemented
* Some structs and fields for listener
* Raw listener implementation
* Added new event for felica activation
* Proper config fot listener added
* Moved some structs from poller in order to use them in listener too
* New function for calculating MAC
* Listener data structures and function definitions
* Private listener functions implementation added
* Raw felica listener logic implementation added
* Fix total sector count both for poller and listener
* Defined type for write handlers
* New logic for write operations added
* Removed old commented code
* Splitted read logic into several separate functions
* New type added and some fields to instance
* New logic of read command implemented
* Defines added for response codes
* Functions moved to private namespace
* Function visibility changed and some cleanups
* Update felica_listener.c, felica_listener_i.c, and felica_listener_i.h
* Some type adjustments
* Moved frame_exchange function to private namespace
* Error handling added
* Function to get data_ptr for write request added
* Missing declaration added
* Add processing of nfc errors
* write_with_mac is a local variable now
* Adjustments to MAC calculation logic
* Values replaced with defines
* Update nfc_transport.c with felica logic
* Sync felica poller added for unit tests
* Felica unit_tests and data dump added
* Fixed proper reading of MAC_A block when it is 1st
* Macro definitions for MC added
* Function simplified
* More defines
* CRC check for incomming packets added
* Readonly logic adjusted
* Block write validation adjusted
* New logic for ID block writing
* Some cleanups
* New logic of moving across the block list with different element length
* Some cleanups
* Adjusted requires_mac logic to cover all blocks needed
* Cleanups and renaming
* New block list validation logic
* Block list logic iteration simplified
* Some asserts and checks added
* Replaced MC[2] checks with macros
* Marked def values as unsigned
* Removed old code
* Removed commented function declarations
* Changed protected block in felica test card dump and adjusted tests
* Fixes after merge
* Moved defines to header
* Now we allocate memory for max possible response pack in any case
* Some renaming and documentation
* Bump api symbols
* Set feature to emulate full for felica
* Removed 'More' button and added MoreInfo feature which adds this button back
* Types renamed
* Removed unnecessary code
* Reformat comments
* Fixing missing signatures
* Replaced crash with error log and return value
* Format doxygen comments

Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
RebornedBrain
2024-06-08 17:24:51 +03:00
committed by GitHub
parent ba3beeddeb
commit 467e973da2
20 changed files with 1443 additions and 80 deletions

View File

@@ -3,6 +3,9 @@
#include <lib/nfc/nfc.h>
#include <lib/nfc/helpers/iso14443_crc.h>
#include <lib/nfc/protocols/iso14443_3a/iso14443_3a.h>
#include <lib/nfc/protocols/felica/felica.h>
#include <lib/nfc/helpers/felica_crc.h>
#include <lib/nfc/protocols/felica/felica_poller_sync.h>
#include <furi/furi.h>
@@ -50,11 +53,31 @@ typedef struct {
Iso14443_3aSelResp sel_resp[2];
} Iso14443_3aColResData;
typedef struct {
uint8_t length;
uint8_t polling_cmd;
uint16_t system_code;
uint8_t request_code;
uint8_t time_slot;
} FelicaPollingRequest;
typedef struct {
uint8_t code;
FelicaIDm idm;
FelicaPMm pmm;
} FelicaSensfResData;
typedef struct {
uint16_t system_code;
FelicaSensfResData sens_res;
} FelicaPTMemory;
struct Nfc {
NfcState state;
Iso14443_3aColResStatus col_res_status;
Iso14443_3aColResData col_res_data;
FelicaPTMemory pt_memory;
bool software_col_res_required;
NfcEventCallback callback;
@@ -243,6 +266,21 @@ static void nfc_worker_listener_pass_col_res(Nfc* instance, uint8_t* rx_data, ui
NfcEvent event = {.type = NfcEventTypeListenerActivated};
instance->callback(event, instance->context);
processed = true;
}
} else if(rx_bits == 8 * 8) {
FelicaPollingRequest* request = (FelicaPollingRequest*)rx_data;
if(request->system_code == instance->pt_memory.system_code) {
uint8_t response_size = sizeof(FelicaSensfResData) + 1;
bit_buffer_reset(tx_buffer);
bit_buffer_append_byte(tx_buffer, response_size);
bit_buffer_append_bytes(
tx_buffer, (uint8_t*)&instance->pt_memory.sens_res, sizeof(FelicaSensfResData));
felica_crc_append(tx_buffer);
nfc_listener_tx(instance, tx_buffer);
instance->col_res_status = Iso14443_3aColResStatusDone;
NfcEvent event = {.type = NfcEventTypeListenerActivated};
instance->callback(event, instance->context);
processed = true;
}
}
@@ -470,6 +508,12 @@ NfcError nfc_felica_listener_set_sensf_res_data(
furi_assert(idm_len == 8);
furi_assert(pmm_len == 8);
instance->pt_memory.system_code = 0xFFFF;
instance->pt_memory.sens_res.code = 0x01;
instance->software_col_res_required = true;
memcpy(instance->pt_memory.sens_res.idm.data, idm, idm_len);
memcpy(instance->pt_memory.sens_res.pmm.data, pmm, pmm_len);
return NfcErrorNone;
}