yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
 
#include <errno.h>
#include <string.h>
#include <stdlib.h>
 
#include "phdriver.h"
#include "se_api.h"
#include "mk_misc.h"
#include "mk_trace.h"
 
/******************************************************************************
 * @Function     t1_read_no_wait
 * @Param        p_data_len - point to data length.
 * @Returns      On success return ESESTATUS_SUCCESS, else ESESTATUS_xxx.
 ******************************************************************************/
static ESESTATUS t1_read_no_wait(uint16_t *p_data_len)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
    int ret = -1;
    uint16_t total_cnt = 0, num_bytes_to_read = 0, read_index = 0;
 
#ifdef USE_SPI_SPLIT_READ
    const uint8_t hdrLen = get_header_len(false) + 1;
#else
    const uint8_t hdrLen = g_ese_ctx.max_IFSD;
#endif
 
#ifdef DBG_LEVEL_STACK
    if (gDumpStackFlag)
    {
        dumpCallstack();
    }
    gDumpStackFlag = true;
#endif
 
    // memset(gp_data_rx, 0x00, sizeof(gp_data_rx));
 
    // sys_timer_delay_ms(1); // 10
 
    ret = phdriver_read(gp_data_rx, 1);
 
    if ((ret < 0) || (NAD_C2DT != gp_data_rx[0]))
    {
        return ESESTATUS_NO_DATA_TO_RECEIVE;
    }
    else
    {
        ret = phdriver_read(gp_data_rx + 1, (hdrLen - 1));
 
        if (g_ese_ctx.is_T1_ext_hdr_len)
        {
            num_bytes_to_read = (uint16_t)(((uint16_t)gp_data_rx[T1_FRAME_LEN_OFFSET]) << 8);
            num_bytes_to_read |= gp_data_rx[T1_FRAME_LEN_OFFSET2];
 
            // CRC has 2 bytes, so the second read len is INF len + 1
            if (CRC == g_atr.checksum)
            {
                num_bytes_to_read += 1;
            }
        }
        else
        {
            // LRC has 1 bytes, so the second read len is INF len
            num_bytes_to_read = gp_data_rx[T1_FRAME_LEN_OFFSET];
        }
        read_index = hdrLen;
        total_cnt += hdrLen;
    }
 
#ifdef USE_SPI_SPLIT_READ
    if (ret < 0)
    {
        if ((-1 == ret) && (4 == errno))
        {
            // Interrupted system call, is NFC_DLD_FLUSH
            status = ESESTATUS_FATAL_ERROR;
        }
        else
        {
            status = ESESTATUS_FAILED;
        }
    }
    else
    {
        if (0 != num_bytes_to_read)
        {
            ret = phdriver_read(&gp_data_rx[read_index], num_bytes_to_read);
            if (ret < 0)
            {
                status = ESESTATUS_PH_IO;
            }
            else
            {
                total_cnt += num_bytes_to_read;
            }
        }
    }
#endif
 
    if (ESESTATUS_SUCCESS != status)
    {
        *p_data_len = 0;
    }
    else
    {
        *p_data_len = total_cnt;
    }
 
    return status;
}
 
/******************************************************************************
 * @Function     response_no_wait_process
 * @Returns      On success return ESESTATUS_SUCCESS or else ESESTATUS error.
 ******************************************************************************/
static ESESTATUS response_no_wait_process(void)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
    uint16_t data_len = 0;
 
    status = t1_read_no_wait(&data_len);
    if (status == ESESTATUS_NO_DATA_TO_RECEIVE)
    {
        return ESESTATUS_NO_DATA_TO_RECEIVE;
    }
 
    if (ESESTATUS_SUCCESS == status)
    {
        status = t1_chk_epilogue(gp_data_rx, 0, data_len);
        if (status == ESESTATUS_SUCCESS)
        {
            status = t1_decode_frame(data_len);
 
            if (ESESTATUS_SUCCESS == status)
            {
            }
        }
        else
        {
            status = do_T1_recovery_at_undecode();
        }
    }
    else if (ESESTATUS_FATAL_ERROR == status)
    {
        g_ese_ctx.next_T1_state = STATE_IDEL;
    }
    else
    {
        status = do_T1_recovery_at_undecode();
    }
 
    return status;
}
 
/******************************************************************************
 * @Function     transmit_only_process
 * @Returns      On success return ESESTATUS_SUCCESS or else ESESTATUS error.
 ******************************************************************************/
static ESESTATUS transmit_only_process(void)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    g_ese_ctx.is_read_done = false;
 
    g_recv_total_len = 0;
    while (STATE_IDEL != g_ese_ctx.next_T1_state)
    {
        switch (g_ese_ctx.p_T1_params->tx_frame_type)
        {
            case IFRAME:
            {
                status = t1_send_Iframe();
                break;
            }
 
            case RFRAME:
            {
                status = t1_send_Rframe();
                break;
            }
 
            case SFRAME:
            {
                status = t1_send_Sframe(); // just send
                break;
            }
            case FRAME_TYPE_INVALID_MAX:
            case FRAME_TYPE_INVALID_MIN:
            {
                status = ESESTATUS_INVALID_PARAMETER;
                LOG_INFO(TRACE_MODULE_SE, "%s : invalid T1 frame type: %d\r\n", __FUNCTION__, g_ese_ctx.p_T1_params->tx_frame_type);
            }
        }
 
        if (ESESTATUS_SUCCESS == status)
        {
            if ((g_ese_ctx.p_T1_params->is_device_chaining == true) && (g_ese_ctx.next_T1_state != STATE_IDEL))
            {
#ifdef DBG_LEVEL_STACK
                gDumpStackFlag = false;
#endif
                g_ese_ctx.p_T1_params->blk_retry_cnt = 0;
                status = response_process();
                // status = response_no_wait_process();
            }
            else
            {
                break;
            }
        }
        else
        {
            if (((ESESTATUS_PH_IO != status) && (ESESTATUS_MEM_EXCEPTION != status)) || (g_ese_ctx.p_T1_params->blk_retry_cnt >= g_ese_ctx.max_blk_retry_cnt))
            {
                LOG_INFO(TRACE_MODULE_SE, "%s: send frame failed, frameType = %d, status = %d, retryCnt = %d\r\n", __FUNCTION__,
                         g_ese_ctx.p_T1_params->tx_frame_type, status, g_ese_ctx.max_blk_retry_cnt);
                g_ese_ctx.next_T1_state = STATE_IDEL;
                break;
            }
            else
            {
                // resend
                g_ese_ctx.p_T1_params->blk_retry_cnt++;
            }
        }
    }
 
    return status;
}
 
/******************************************************************************
 * @Function     receive_only_process
 * @Returns      On success return ESESTATUS_SUCCESS or else ESESTATUS error.
 ******************************************************************************/
static ESESTATUS receive_only_process(void)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
#ifdef DBG_LEVEL_STACK
    gDumpStackFlag = false;
#endif
    g_ese_ctx.p_T1_params->blk_retry_cnt = 0;
    status = response_no_wait_process();
 
    while ((status != ESESTATUS_NO_DATA_TO_RECEIVE) && (g_ese_ctx.next_T1_state != STATE_IDEL))
    {
        if (g_ese_ctx.p_T1_params->last_rx_frame_type == IFRAME)
        {
            if ((g_ese_ctx.p_T1_params->is_card_chaining == true) && (g_ese_ctx.next_T1_state == R_ACK))
            {
                status = Transmit_receive_process();
                // status = transmit_only_process();
            }
            break;
        }
        else // if (g_ese_ctx.p_T1_params->last_rx_frame_type == RFRAME)
        {
            transmit_only_process();
            status = response_no_wait_process();
        }
    }
 
    return status;
}
 
/******************************************************************************
 * @Function     t1_transmit_only_apdu
 * @Param        p_cmd_apdu - 7816-4 APDU that shall be sent.
 *               cmd_len    - Length of the apduCmd to be sent.
 * @Returns      On success return ESESTATUS_SUCCESS or else ESESTATUS error.
 ******************************************************************************/
ESESTATUS t1_transmit_only_apdu(uint8_t *p_cmd_apdu, uint16_t cmd_len)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
    status = init_Iframe_from_cmd_apdu(p_cmd_apdu, cmd_len);
    if (ESESTATUS_SUCCESS != status)
    {
        goto cleanup;
    }
 
    status = transmit_only_process();
 
    if (ESESTATUS_SUCCESS != status)
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: failed, status = %d\r\n", __FUNCTION__, status);
    }
    else
    {
        // if success,after receiving the resp,execu deinit_Iframe_from_cmd_apdu
        return status;
    }
 
cleanup:
    deinit_Iframe_from_cmd_apdu();
    return status;
}
 
/******************************************************************************
 * @Function     t1_receive_only_apdu
 * @Returns      On success return ESESTATUS_SUCCESS or else ESESTATUS error.
 ******************************************************************************/
ESESTATUS t1_receive_only_apdu(void)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    status = receive_only_process(); // response_no_wait_process();
 
    if (ESESTATUS_NO_DATA_TO_RECEIVE == status)
    {
        return ESESTATUS_NO_DATA_TO_RECEIVE;
    }
 
    if (ESESTATUS_SUCCESS != status)
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: failed, status = %d\r\n", __FUNCTION__, status);
    }
 
    deinit_Iframe_from_cmd_apdu();
    return status;
}