2021-10-03 13:36:05 +03:00
|
|
|
/**
|
2022-01-05 19:10:18 +03:00
|
|
|
* @file furi_hal_nfc.h
|
2021-10-03 13:36:05 +03:00
|
|
|
* NFC HAL API
|
|
|
|
*/
|
|
|
|
|
2021-08-08 21:03:25 +03:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <rfal_nfc.h>
|
|
|
|
#include <st_errno.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2022-05-24 17:00:15 +03:00
|
|
|
#include <lib/nfc_protocols/nfca.h>
|
|
|
|
|
2021-08-08 21:03:25 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define FURI_HAL_NFC_UID_MAX_LEN 10
|
2022-06-09 11:35:34 +03:00
|
|
|
#define FURI_HAL_NFC_DATA_BUFF_SIZE (512)
|
2022-03-24 01:14:34 +03:00
|
|
|
#define FURI_HAL_NFC_PARITY_BUFF_SIZE (FURI_HAL_NFC_DATA_BUFF_SIZE / 8)
|
2021-08-08 21:03:25 +03:00
|
|
|
|
2022-02-02 22:59:28 +03:00
|
|
|
#define FURI_HAL_NFC_TXRX_DEFAULT \
|
|
|
|
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
|
2022-04-19 18:23:58 +03:00
|
|
|
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO)
|
2022-02-02 22:59:28 +03:00
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
#define FURI_HAL_NFC_TX_DEFAULT_RX_NO_CRC \
|
|
|
|
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_AUTO | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
|
2022-04-19 18:23:58 +03:00
|
|
|
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO)
|
2022-03-24 01:14:34 +03:00
|
|
|
|
|
|
|
#define FURI_HAL_NFC_TXRX_WITH_PAR \
|
|
|
|
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
|
2022-04-19 18:23:58 +03:00
|
|
|
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_AUTO)
|
2022-03-24 01:14:34 +03:00
|
|
|
|
2022-02-02 22:59:28 +03:00
|
|
|
#define FURI_HAL_NFC_TXRX_RAW \
|
2022-03-24 01:14:34 +03:00
|
|
|
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_KEEP | \
|
2022-04-19 18:23:58 +03:00
|
|
|
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_KEEP | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE)
|
|
|
|
|
2022-05-26 15:55:29 +03:00
|
|
|
#define FURI_HAL_NFC_TX_RAW_RX_DEFAULT \
|
|
|
|
((uint32_t)RFAL_TXRX_FLAGS_CRC_TX_MANUAL | (uint32_t)RFAL_TXRX_FLAGS_CRC_RX_REMV | \
|
|
|
|
(uint32_t)RFAL_TXRX_FLAGS_PAR_RX_REMV | (uint32_t)RFAL_TXRX_FLAGS_PAR_TX_NONE)
|
|
|
|
|
2022-04-19 18:23:58 +03:00
|
|
|
typedef enum {
|
|
|
|
FuriHalNfcTxRxTypeDefault,
|
|
|
|
FuriHalNfcTxRxTypeRxNoCrc,
|
|
|
|
FuriHalNfcTxRxTypeRxKeepPar,
|
|
|
|
FuriHalNfcTxRxTypeRaw,
|
2022-05-24 17:00:15 +03:00
|
|
|
FuriHalNfcTxRxTypeRxRaw,
|
|
|
|
FuriHalNfcTxRxTransparent,
|
2022-04-19 18:23:58 +03:00
|
|
|
} FuriHalNfcTxRxType;
|
2022-02-02 22:59:28 +03:00
|
|
|
|
|
|
|
typedef bool (*FuriHalNfcEmulateCallback)(
|
|
|
|
uint8_t* buff_rx,
|
|
|
|
uint16_t buff_rx_len,
|
|
|
|
uint8_t* buff_tx,
|
|
|
|
uint16_t* buff_tx_len,
|
|
|
|
uint32_t* flags,
|
|
|
|
void* context);
|
|
|
|
|
2022-04-19 18:23:58 +03:00
|
|
|
typedef enum {
|
|
|
|
FuriHalNfcTypeA,
|
|
|
|
FuriHalNfcTypeB,
|
|
|
|
FuriHalNfcTypeF,
|
|
|
|
FuriHalNfcTypeV,
|
|
|
|
} FuriHalNfcType;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
FuriHalNfcInterfaceRf,
|
|
|
|
FuriHalNfcInterfaceIsoDep,
|
|
|
|
FuriHalNfcInterfaceNfcDep,
|
|
|
|
} FuriHalNfcInterface;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
FuriHalNfcType type;
|
|
|
|
FuriHalNfcInterface interface;
|
|
|
|
uint8_t uid_len;
|
|
|
|
uint8_t uid[10];
|
|
|
|
uint32_t cuid;
|
|
|
|
uint8_t atqa[2];
|
|
|
|
uint8_t sak;
|
|
|
|
} FuriHalNfcDevData;
|
|
|
|
|
2022-06-09 11:35:34 +03:00
|
|
|
typedef void (
|
|
|
|
*FuriHalNfcTxRxSniffCallback)(uint8_t* data, uint16_t bits, bool crc_dropped, void* context);
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
typedef struct {
|
|
|
|
uint8_t tx_data[FURI_HAL_NFC_DATA_BUFF_SIZE];
|
|
|
|
uint8_t tx_parity[FURI_HAL_NFC_PARITY_BUFF_SIZE];
|
|
|
|
uint16_t tx_bits;
|
|
|
|
uint8_t rx_data[FURI_HAL_NFC_DATA_BUFF_SIZE];
|
|
|
|
uint8_t rx_parity[FURI_HAL_NFC_PARITY_BUFF_SIZE];
|
|
|
|
uint16_t rx_bits;
|
2022-04-19 18:23:58 +03:00
|
|
|
FuriHalNfcTxRxType tx_rx_type;
|
2022-05-24 17:00:15 +03:00
|
|
|
NfcaSignal* nfca_signal;
|
2022-06-09 11:35:34 +03:00
|
|
|
|
|
|
|
FuriHalNfcTxRxSniffCallback sniff_tx;
|
|
|
|
FuriHalNfcTxRxSniffCallback sniff_rx;
|
|
|
|
void* sniff_context;
|
2022-03-24 01:14:34 +03:00
|
|
|
} FuriHalNfcTxRxContext;
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Init nfc
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
|
|
|
void furi_hal_nfc_init();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** Check if nfc worker is busy
|
|
|
|
*
|
|
|
|
* @return true if busy
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
|
|
|
bool furi_hal_nfc_is_busy();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC field on
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
|
|
|
void furi_hal_nfc_field_on();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC field off
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
|
|
|
void furi_hal_nfc_field_off();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC start sleep
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
|
|
|
void furi_hal_nfc_start_sleep();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC stop sleep
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
|
|
|
void furi_hal_nfc_exit_sleep();
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC poll
|
|
|
|
*
|
|
|
|
* @param dev_list pointer to rfalNfcDevice buffer
|
|
|
|
* @param dev_cnt pointer device count
|
|
|
|
* @param timeout timeout in ms
|
|
|
|
* @param deactivate deactivate flag
|
|
|
|
*
|
|
|
|
* @return true on success
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
2022-04-19 18:23:58 +03:00
|
|
|
bool furi_hal_nfc_detect(FuriHalNfcDevData* nfc_data, uint32_t timeout);
|
2021-08-08 21:03:25 +03:00
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
/** Activate NFC-A tag
|
|
|
|
*
|
|
|
|
* @param timeout timeout in ms
|
|
|
|
* @param cuid pointer to 32bit uid
|
|
|
|
*
|
|
|
|
* @return true on succeess
|
|
|
|
*/
|
|
|
|
bool furi_hal_nfc_activate_nfca(uint32_t timeout, uint32_t* cuid);
|
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC listen
|
|
|
|
*
|
|
|
|
* @param uid pointer to uid buffer
|
|
|
|
* @param uid_len uid length
|
|
|
|
* @param atqa pointer to atqa
|
|
|
|
* @param sak sak
|
|
|
|
* @param activate_after_sak activate after sak flag
|
|
|
|
* @param timeout timeout in ms
|
|
|
|
*
|
|
|
|
* @return true on success
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
2022-01-05 19:10:18 +03:00
|
|
|
bool furi_hal_nfc_listen(
|
|
|
|
uint8_t* uid,
|
|
|
|
uint8_t uid_len,
|
|
|
|
uint8_t* atqa,
|
|
|
|
uint8_t sak,
|
|
|
|
bool activate_after_sak,
|
|
|
|
uint32_t timeout);
|
2021-08-17 00:45:04 +03:00
|
|
|
|
2022-07-03 17:51:50 +03:00
|
|
|
/** Start Target Listen mode
|
|
|
|
* @note RFAL free implementation
|
|
|
|
*
|
|
|
|
* @param nfc_data FuriHalNfcDevData instance
|
|
|
|
*/
|
|
|
|
void furi_hal_nfc_listen_start(FuriHalNfcDevData* nfc_data);
|
|
|
|
|
|
|
|
/** Read data in Target Listen mode
|
|
|
|
* @note Must be called only after furi_hal_nfc_listen_start()
|
|
|
|
*
|
|
|
|
* @param tx_rx FuriHalNfcTxRxContext instance
|
|
|
|
* @param timeout_ms timeout im ms
|
|
|
|
*
|
|
|
|
* @return true on not empty receive
|
|
|
|
*/
|
|
|
|
bool furi_hal_nfc_listen_rx(FuriHalNfcTxRxContext* tx_rx, uint32_t timeout_ms);
|
|
|
|
|
|
|
|
/** Set Target in Sleep state */
|
|
|
|
void furi_hal_nfc_listen_sleep();
|
|
|
|
|
|
|
|
/** Emulate NFC-A Target
|
|
|
|
* @note RFAL based implementation
|
|
|
|
*
|
|
|
|
* @param uid NFC-A UID
|
|
|
|
* @param uid_len NFC-A UID length
|
|
|
|
* @param atqa NFC-A ATQA
|
|
|
|
* @param sak NFC-A SAK
|
|
|
|
* @param callback FuriHalNfcEmulateCallback instance
|
|
|
|
* @param context pointer to context for callback
|
|
|
|
* @param timeout timeout in ms
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2022-02-02 22:59:28 +03:00
|
|
|
bool furi_hal_nfc_emulate_nfca(
|
|
|
|
uint8_t* uid,
|
|
|
|
uint8_t uid_len,
|
|
|
|
uint8_t* atqa,
|
|
|
|
uint8_t sak,
|
|
|
|
FuriHalNfcEmulateCallback callback,
|
|
|
|
void* context,
|
|
|
|
uint32_t timeout);
|
|
|
|
|
2022-03-24 01:14:34 +03:00
|
|
|
/** NFC data exchange
|
|
|
|
*
|
|
|
|
* @param tx_rx_ctx FuriHalNfcTxRxContext instance
|
|
|
|
*
|
|
|
|
* @return true on success
|
|
|
|
*/
|
2022-04-19 18:23:58 +03:00
|
|
|
bool furi_hal_nfc_tx_rx(FuriHalNfcTxRxContext* tx_rx, uint16_t timeout_ms);
|
|
|
|
|
|
|
|
/** NFC data full exhange
|
|
|
|
*
|
2022-06-09 11:35:34 +03:00
|
|
|
* @param tx_rx_ctx FuriHalNfcTxRxContext instance
|
2022-04-19 18:23:58 +03:00
|
|
|
*
|
2022-06-09 11:35:34 +03:00
|
|
|
* @return true on success
|
2022-04-19 18:23:58 +03:00
|
|
|
*/
|
2022-06-09 11:35:34 +03:00
|
|
|
bool furi_hal_nfc_tx_rx_full(FuriHalNfcTxRxContext* tx_rx);
|
2021-12-21 15:33:17 +03:00
|
|
|
|
2021-10-03 13:36:05 +03:00
|
|
|
/** NFC deactivate and start sleep
|
2021-08-08 21:03:25 +03:00
|
|
|
*/
|
2022-04-19 18:23:58 +03:00
|
|
|
void furi_hal_nfc_sleep();
|
2021-08-08 21:03:25 +03:00
|
|
|
|
2022-02-02 22:59:28 +03:00
|
|
|
void furi_hal_nfc_stop();
|
|
|
|
|
2021-08-08 21:03:25 +03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|