chen
2024-11-01 631a90c1116fa33382a88a747c89bf761bc0fa9b
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
#include <string.h>
#include "se_api.h"
#include "phdriver.h"
#include "mk_trace.h"
#include "mk_misc.h"
 
/*********************** Global Variables *************************************/
SE_context g_ese_ctx = {0};
 
/**
 * First read, use the default length(0xFE).
 * Ohters read, use the IFSD REQ length, if this REQ is success.
 * This memory cannot be freed, unitl this process died.
 */
 
static void init_se_ctx_from_config(void);
 
/*****************************************************************************
 * @Function    se_open
 * @Param       init_mode - init mode for normal OMA or download mode
 * @Returns     This function return ESESTATUS_SUCCES (0) in case of success.
 ******************************************************************************/
ESESTATUS se_open(SE_init_mode init_mode)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    phdriver_open();
 
    // memset(&g_ese_ctx, 0x00, sizeof(g_ese_ctx));
    init_se_ctx_from_config();
 
    status = ESESTATUS_SUCCESS;
    g_ese_ctx.ESE_lib_status = ESESTATUS_OPENED;
 
    return status;
}
 
/*****************************************************************************
 * @Function    se_init
 * @Param       init_mode - init mode for normal OMA or download mode
 * @Returns     This function return ESESTATUS_SUCCES (0) in case of success.
 ******************************************************************************/
ESESTATUS se_init(SE_init_mode init_mode)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
    short cnt;
 
    LOG_INFO(TRACE_MODULE_SE, "%s: enter\r\n", __FUNCTION__);
 
    status = se_open(init_mode);
 
    if (ESESTATUS_SUCCESS != status)
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: open device failed, errno = %d\r\n", __FUNCTION__, errno);
        goto cleanup;
    }
 
    cnt = 0;
    do
    {
        status = t1_CIP_req();
        if ((ESESTATUS_SUCCESS == status) && (g_ese_ctx.max_IFSC != 0))
        {
            break;
        }
        cnt++;
        if (cnt >= 1)
        {
            LOG_INFO(TRACE_MODULE_SE, "%s: CIP REQ failed[%d]\r\n", __FUNCTION__, status);
            goto cleanup;
        }
        else
        {
            sys_timer_delay_ms(100);
        }
    } while (1);
 
    if (!g_ese_ctx.is_T1_ext_hdr_len)
    {
        goto cleanup;
    }
 
    status = t1_IFSD_req();
    if (ESESTATUS_SUCCESS != status)
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: IFSD REQ failed[%d]\r\n", __FUNCTION__, status);
        status = ESESTATUS_SUCCESS;
        goto cleanup;
    }
 
cleanup:
    return status;
}
 
ESESTATUS se_status(void)
{
    return g_ese_ctx.ESE_lib_status;
}
 
/*****************************************************************************
 * @Function    se_transmit_receive
 * @Param       p_cmd  - Command to eSE
 *              p_resp - Response from eSE (Returned data to be freed after copying)
 * @Returns     On Success ESESTATUS_SUCCESS else an error code.
 *****************************************************************************/
ESESTATUS se_transmit_receive(SE_data *p_cmd, SE_data *p_resp)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    if ((NULL == p_cmd) || (NULL == p_resp))
    {
        status = ESESTATUS_INVALID_PARAMETER;
        goto cleanup;
    }
 
    if ((0 == p_cmd->len) || (NULL == p_cmd->p_data))
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: Invalid Parameter no data\r\n", __FUNCTION__);
        status = ESESTATUS_INVALID_PARAMETER;
        goto cleanup;
    }
    else if ((ESESTATUS_CLOSED == g_ese_ctx.ESE_lib_status))
    {
        LOG_INFO(TRACE_MODULE_SE, "%s ESE Not Initialized\r\n", __FUNCTION__);
        status = ESESTATUS_NOT_INITIALISED;
        goto cleanup;
    }
 
    status = t1_transmit_receive_apdu(p_cmd->p_data, p_cmd->len);
    if (ESESTATUS_SUCCESS == status)
    {
        status = t1_receive_data_and_get(&p_resp->p_data, &p_resp->len);
    }
    else
    {
        LOG_INFO(TRACE_MODULE_SE, "%s failed, status = %d\n\r", __FUNCTION__, status);
        p_resp->p_data = NULL;
    }
    // LOG_INFO(TRACE_MODULE_SE, "%s Processing complete\n\r", __FUNCTION__);
 
cleanup:
 
    return status;
}
 
/*****************************************************************************
 * @Function    se_deinit
 * @Returns     ESESTATUS_SUCCESS Always return ESESTATUS_SUCCESS (0).
 *****************************************************************************/
ESESTATUS se_deinit(void)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    if ((ESESTATUS_CLOSED == g_ese_ctx.ESE_lib_status))
    {
        LOG_INFO(TRACE_MODULE_SE, " %s ESE Not Initialized \n", __FUNCTION__);
        status = ESESTATUS_NOT_INITIALISED;
        goto cleanup;
    }
 
    status = t1_prop_end_apdu_req();
    if (ESESTATUS_SUCCESS != status)
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: END_APUD REQ failed[%d]", __FUNCTION__, status);
        LOG_INFO(TRACE_MODULE_SE, "%s: IntfReset (CIP)", __FUNCTION__);
        status = t1_CIP_req();
        if (status != ESESTATUS_SUCCESS)
        {
            LOG_INFO(TRACE_MODULE_SE, "%s: IntfReset Failed", __FUNCTION__);
        }
    }
    else
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: END_APUD REQ success", __FUNCTION__);
    }
 
cleanup:
 
    status = se_close();
 
    return status;
}
 
/*****************************************************************************
 * @Function    se_close
 * @Returns     This function return ESESTATUS_SUCCES (0) in case of success.
 *****************************************************************************/
ESESTATUS se_close(void)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    if ((ESESTATUS_CLOSED == g_ese_ctx.ESE_lib_status))
    {
        LOG_INFO(TRACE_MODULE_SE, " %s ESE Not Opened", __FUNCTION__);
        status = ESESTATUS_CLOSED;
        return status;
    }
 
    status = ESESTATUS_SUCCESS;
    return status;
}
 
static void init_se_ctx_from_config(void)
{
    g_ese_ctx.max_write_retry_cnt = 10;
    g_ese_ctx.write_retry_time = 1000;
    // WTX is 1s, read error will delay 1ms and retry, so the max retry is 1010
    g_ese_ctx.max_read_retry_cnt = 1010;
    g_ese_ctx.read_retry_time = 1000;
    g_ese_ctx.max_IFSD = 258;
 
    g_ese_ctx.max_recovery_cnt = 3;
 
    g_ese_ctx.max_blk_retry_cnt = 3;
    g_ese_ctx.max_wtx_cnt = 30;
}
 
/******************************************************************
 * @Function    se_transmit_only
 * @Param       p_cmd - Command to eSE
 * @Returns     On Success ESESTATUS_SUCCESS else an error code.
 *****************************************************************/
ESESTATUS se_transmit_only(SE_data *p_cmd)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    if (NULL == p_cmd) // || (NULL == pRsp))
    {
        status = ESESTATUS_INVALID_PARAMETER;
        goto cleanup;
    }
 
    if ((0 == p_cmd->len) || (NULL == p_cmd->p_data))
    {
        LOG_INFO(TRACE_MODULE_SE, "%s: Invalid Parameter no data", __FUNCTION__);
        status = ESESTATUS_INVALID_PARAMETER;
        goto cleanup;
    }
    else if ((ESESTATUS_CLOSED == g_ese_ctx.ESE_lib_status))
    {
        LOG_INFO(TRACE_MODULE_SE, "%s ESE Not Initialized", __FUNCTION__);
        status = ESESTATUS_NOT_INITIALISED;
        goto cleanup;
    }
 
    status = t1_transmit_only_apdu(p_cmd->p_data, p_cmd->len);
    if (ESESTATUS_SUCCESS == status)
    {
    }
    else
    {
        LOG_INFO(TRACE_MODULE_SE, "%s failed, status = %d\n\r", __FUNCTION__, status);
        // pRsp->p_data = NULL;
    }
    // LOG_INFO(TRACE_MODULE_SE, "%s Processing complete\n\r", __FUNCTION__);
 
cleanup:
 
    return status;
}
 
/*****************************************************************************
 * @Function    se_receive_only
 * @Param      p_resp - Response from eSE (Returned data to be freed after copying)
 * @Returns     On Success ESESTATUS_SUCCESS else an error code.
 *****************************************************************************/
ESESTATUS se_receive_only(SE_data *p_resp)
{
    ESESTATUS status = ESESTATUS_SUCCESS;
 
    if (NULL == p_resp) // || (NULL == pRsp))
    {
        status = ESESTATUS_INVALID_PARAMETER;
        goto cleanup;
    }
 
    if ((ESESTATUS_CLOSED == g_ese_ctx.ESE_lib_status))
    {
        LOG_INFO(TRACE_MODULE_SE, "%s ESE Not Initialized\r\n", __FUNCTION__);
        status = ESESTATUS_NOT_INITIALISED;
        goto cleanup;
    }
 
    status = t1_receive_only_apdu();
    if (ESESTATUS_NO_DATA_TO_RECEIVE == status)
    {
        // LOG_INFO(TRACE_MODULE_SE, "%s : NO data from SE, status = %d\r\r", __FUNCTION__, status);
    }
    else if (ESESTATUS_SUCCESS == status)
    {
        status = t1_receive_data_and_get(&p_resp->p_data, &p_resp->len);
        // LOG_INFO(TRACE_MODULE_SE, "%s Processing complete\r\n", __FUNCTION__);
    }
    else
    {
        // LOG_INFO(TRACE_MODULE_SE, "%s failed, status = %d\r\n", __FUNCTION__, status);
        p_resp->p_data = NULL;
    }
 
cleanup:
 
    return status;
}