21#include "ledger_protocol.h"
22#include "seproxyhal_protocol.h"
24#include "os_io_seph_cmd.h"
29typedef enum nfc_state_e {
31 NFC_STATE_INITIALIZED,
39typedef struct nfc_ledger_data_s {
42 ledger_protocol_t protocol_data;
54static void nfc_ledger_send_rapdu(uint8_t *buffer, uint16_t length, uint32_t timeout_ms);
57static uint8_t NFC_LEDGER_io_buffer[OS_IO_BUFFER_SIZE + 1];
60static nfc_ledger_data_t nfc_ledger_data;
62 nfc_ledger_protocol_chunk_buffer[156 + 2];
65static void nfc_ledger_send_rapdu(uint8_t *buffer, uint16_t length, uint32_t timeout_ms)
71 hdr[0] = SEPROXYHAL_TAG_NFC_RAPDU;
74 os_io_tx_cmd(OS_IO_PACKET_TYPE_SEPH, hdr, 3, NULL);
75 os_io_tx_cmd(OS_IO_PACKET_TYPE_SEPH, buffer, length, NULL);
84 memset(&nfc_ledger_data, 0,
sizeof(nfc_ledger_data));
85 nfc_ledger_data.state = NFC_STATE_INITIALIZED;
86 LOG_IO(
"NFC_LEDGER_init deep\n");
89 LOG_IO(
"NFC_LEDGER_init\n");
95 LOG_IO(
"NFC_LEDGER_start %d\n", mode);
97 if ((nfc_ledger_data.state == NFC_STATE_INITIALIZED) || (nfc_ledger_data.mode != mode)) {
98 memset(&nfc_ledger_data.protocol_data, 0,
sizeof(nfc_ledger_data.protocol_data));
99 nfc_ledger_data.protocol_data.mtu = 0;
100 nfc_ledger_data.mode = mode;
101 nfc_ledger_data.state = NFC_STATE_STARTED;
103 if ((nfc_ledger_data.state == NFC_STATE_STARTED)
104 && (os_setting_get(OS_SETTING_FEATURES, NULL, 0) & OS_SETTING_FEATURES_NFC_ENABLED)) {
106 ledger_protocol_result_t result
107 = LEDGER_PROTOCOL_init(&nfc_ledger_data.protocol_data, OS_IO_PACKET_TYPE_NFC_APDU);
108 if (result != LP_SUCCESS) {
111 os_io_nfc_cmd_power(SEPROXYHAL_TAG_NFC_POWER_ON_CE);
112 nfc_ledger_data.state = NFC_STATE_RUNNING;
115 ledger_protocol_result_t result = LEDGER_PROTOCOL_init(&nfc_ledger_data.protocol_data,
116 OS_IO_PACKET_TYPE_NFC_APDU_RSP);
117 if (result != LP_SUCCESS) {
120 os_io_nfc_cmd_power(SEPROXYHAL_TAG_NFC_POWER_ON_READER);
121 nfc_ledger_data.state = NFC_STATE_RUNNING;
131 if (nfc_ledger_data.state == NFC_STATE_RUNNING) {
132 os_io_nfc_cmd_power(SEPROXYHAL_TAG_NFC_POWER_OFF);
133 nfc_ledger_data.state = NFC_STATE_INITIALIZED;
138 uint16_t seph_buffer_length,
139 uint8_t *apdu_buffer,
140 uint16_t apdu_buffer_max_length)
144 if (nfc_ledger_data.state == NFC_STATE_RUNNING) {
146 if (seph_buffer_length
147 && !seph_parse_header(&seph_buffer[1], seph_buffer_length - 1, &seph)) {
152 ledger_protocol_result_t result
153 = LEDGER_PROTOCOL_rx(&nfc_ledger_data.protocol_data,
156 nfc_ledger_protocol_chunk_buffer,
157 sizeof(nfc_ledger_protocol_chunk_buffer),
158 NFC_LEDGER_io_buffer,
159 sizeof(NFC_LEDGER_io_buffer),
160 sizeof(nfc_ledger_protocol_chunk_buffer));
161 if (result != LP_SUCCESS) {
166 if (nfc_ledger_data.protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) {
167 if ((apdu_buffer_max_length < nfc_ledger_data.protocol_data.rx_apdu_length)
168 || (
sizeof(NFC_LEDGER_io_buffer) < nfc_ledger_data.protocol_data.rx_apdu_length)) {
173 NFC_LEDGER_io_buffer,
174 nfc_ledger_data.protocol_data.rx_apdu_length);
175 status = nfc_ledger_data.protocol_data.rx_apdu_length;
177 nfc_ledger_data.protocol_data.rx_apdu_status = APDU_STATUS_WAITING;
185uint32_t
NFC_LEDGER_send(
const uint8_t *packet, uint16_t packet_length, uint32_t timeout_ms)
189 if (nfc_ledger_data.state == NFC_STATE_RUNNING) {
190 ledger_protocol_result_t result
191 = LEDGER_PROTOCOL_tx(&nfc_ledger_data.protocol_data,
194 nfc_ledger_protocol_chunk_buffer,
195 sizeof(nfc_ledger_protocol_chunk_buffer),
196 sizeof(nfc_ledger_protocol_chunk_buffer));
197 if (result != LP_SUCCESS) {
201 if (nfc_ledger_data.protocol_data.tx_chunk_length >= 2) {
202 nfc_ledger_send_rapdu(nfc_ledger_protocol_chunk_buffer,
203 nfc_ledger_data.protocol_data.tx_chunk_length,
207 while (nfc_ledger_data.protocol_data.tx_apdu_buffer) {
208 ledger_protocol_result_t result
209 = LEDGER_PROTOCOL_tx(&nfc_ledger_data.protocol_data,
212 nfc_ledger_protocol_chunk_buffer,
213 sizeof(nfc_ledger_protocol_chunk_buffer),
214 sizeof(nfc_ledger_protocol_chunk_buffer));
215 if (result != LP_SUCCESS) {
219 if (nfc_ledger_data.protocol_data.tx_chunk_length >= 2) {
220 nfc_ledger_send_rapdu(nfc_ledger_protocol_chunk_buffer,
221 nfc_ledger_data.protocol_data.tx_chunk_length,
void NFC_LEDGER_stop(void)
void NFC_LEDGER_init(uint8_t force_restart)
int NFC_LEDGER_rx_seph_apdu_evt(uint8_t *seph_buffer, uint16_t seph_buffer_length, uint8_t *apdu_buffer, uint16_t apdu_buffer_max_length)
uint32_t NFC_LEDGER_send(const uint8_t *packet, uint16_t packet_length, uint32_t timeout_ms)
void NFC_LEDGER_start(uint8_t mode)
@ NFC_LEDGER_MODE_CARD_EMULATION