• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2018-2023 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #define LOG_TAG "NxpEseHal"
19 #include <EseTransport.h>
20 #include <cutils/properties.h>
21 #include <ese_config.h>
22 #include <ese_logs.h>
23 #include <log/log.h>
24 #include <phNxpEseFeatures.h>
25 #include <phNxpEsePal.h>
26 #include <phNxpEseProto7816_3.h>
27 #include <phNxpEse_Internal.h>
28 
29 #define RECEIVE_PACKET_SOF 0xA5
30 #define CHAINED_PACKET_WITHSEQN 0x60
31 #define CHAINED_PACKET_WITHOUTSEQN 0x20
32 #define PH_PAL_ESE_PRINT_PACKET_TX(data, len) \
33   ({ phPalEse_print_packet("SEND", data, len); })
34 #define PH_PAL_ESE_PRINT_PACKET_RX(data, len) \
35   ({ phPalEse_print_packet("RECV", data, len); })
36 /* 32K(0x8000) Datasize + 10(0xA) Byte Max Header Size + 1 byte negative
37  * testcase support */
38 #define MAX_SUPPORTED_DATA_SIZE 0x800B
39 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
40                                int nNbBytesToRead);
41 static int phNxpEse_readPacket_legacy(void* pDevHandle, uint8_t* pBuffer,
42                                       int nNbBytesToRead);
43 
44 static ESESTATUS phNxpEse_checkJcopDwnldState(void);
45 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state);
46 static ESESTATUS phNxpEse_checkFWDwnldStatus(void);
47 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer);
48 static unsigned char* phNxpEse_GgetTimerTlvBuffer(unsigned char* timer_buffer,
49                                                   unsigned int value);
50 static __inline bool phNxpEse_isColdResetRequired(phNxpEse_initMode mode,
51                                                   ESESTATUS status);
52 static int poll_sof_chained_delay = 0;
53 static phNxpEse_OsVersion_t sOsVersion = INVALID_OS_VERSION;
54 /* To Overwrite the value of wtx_counter_limit from config file*/
55 static unsigned long int app_wtx_cnt = RESET_APP_WTX_COUNT;
56 
57 /*********************** Global Variables *************************************/
58 
59 /* ESE Context structure */
60 phNxpEse_Context_t nxpese_ctxt;
61 
62 uint8_t ese_log_level = 0;
63 /******************************************************************************
64  * Function         phNxpEse_SetEndPoint_Cntxt
65  *
66  * Description      This function is called set the SE endpoint
67  *
68  * Returns          None
69  *
70  ******************************************************************************/
71 
phNxpEse_SetEndPoint_Cntxt(uint8_t uEndPoint)72 ESESTATUS phNxpEse_SetEndPoint_Cntxt(uint8_t uEndPoint) {
73   ESESTATUS status = ESESTATUS_FAILED;
74   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
75     status = phNxpEseProto7816_SetEndPoint(uEndPoint);
76     if (status == ESESTATUS_SUCCESS) {
77       nxpese_ctxt.nadInfo.nadRx = nadInfoRx_ptr[uEndPoint];
78       nxpese_ctxt.nadInfo.nadTx = nadInfoTx_ptr[uEndPoint];
79       nxpese_ctxt.endPointInfo = uEndPoint;
80     }
81     NXP_LOG_ESE_D("%s: Endpoint=%d", __FUNCTION__, uEndPoint);
82   } else {
83     NXP_LOG_ESE_E("%s- Function not supported", __FUNCTION__);
84   }
85   return status;
86 }
87 
88 /******************************************************************************
89  * Function         phNxpEse_ResetEndPoint_Cntxt
90  *
91  * Description      This function is called to reset the SE endpoint
92  *
93  * Returns          None
94  *
95  ******************************************************************************/
phNxpEse_ResetEndPoint_Cntxt(uint8_t uEndPoint)96 ESESTATUS phNxpEse_ResetEndPoint_Cntxt(uint8_t uEndPoint) {
97   ESESTATUS status = ESESTATUS_FAILED;
98   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
99     status = phNxpEseProto7816_ResetEndPoint(uEndPoint);
100   } else {
101     NXP_LOG_ESE_E("%s- Function not supported", __FUNCTION__);
102   }
103   return status;
104 }
105 /******************************************************************************
106  * Function         phNxpLog_InitializeLogLevel
107  *
108  * Description      This function is called during phNxpEse_init to initialize
109  *                  debug log level.
110  *
111  * Returns          None
112  *
113  ******************************************************************************/
114 
phNxpLog_InitializeLogLevel()115 void phNxpLog_InitializeLogLevel() {
116   ese_log_level = EseConfig::getUnsigned(
117       NAME_SE_LOG_LEVEL, NXPESE_LOGLEVEL_DEBUG /*default level*/);
118 
119   char valueStr[PROPERTY_VALUE_MAX] = {0};
120   int len = property_get("vendor.ese.debug_enabled", valueStr, "");
121   if (len > 0) {
122     // let Android property override .conf variable
123     unsigned debug_enabled = 0;
124     sscanf(valueStr, "%u", &debug_enabled);
125     ese_log_level = debug_enabled;
126   }
127 
128   NXP_LOG_ESE_I("%s: level=%u", __func__, ese_log_level);
129 }
130 
131 /******************************************************************************
132  * Function         phNxpEse_init
133  *
134  * Description      This function is called by Jni/phNxpEse_open during the
135  *                  initialization of the ESE. It initializes protocol stack
136  *                  instance variable
137  *
138  * Returns          This function return ESESTATUS_SUCCESS (0) in case of
139  *                  success In case of failure returns other failure value.
140  *
141  ******************************************************************************/
phNxpEse_init(phNxpEse_initParams initParams)142 ESESTATUS phNxpEse_init(phNxpEse_initParams initParams) {
143   ESESTATUS wConfigStatus = ESESTATUS_FAILED;
144   unsigned long int num, ifsd_value = 0;
145   unsigned long maxTimer = 0;
146   uint8_t retry = 0;
147   phNxpEseProto7816InitParam_t protoInitParam;
148   phNxpEse_memset(&protoInitParam, 0x00, sizeof(phNxpEseProto7816InitParam_t));
149   /* STATUS_OPEN */
150   nxpese_ctxt.EseLibStatus = ESE_STATUS_OPEN;
151 
152   if (app_wtx_cnt > RESET_APP_WTX_COUNT) {
153     protoInitParam.wtx_counter_limit = app_wtx_cnt;
154     NXP_LOG_ESE_D("Wtx_counter limit from app setting - %lu",
155                   protoInitParam.wtx_counter_limit);
156   } else {
157     protoInitParam.wtx_counter_limit = EseConfig::getUnsigned(
158         NAME_NXP_WTX_COUNT_VALUE, PH_PROTO_WTX_DEFAULT_COUNT);
159     NXP_LOG_ESE_D("Wtx_counter read from config file - %lu",
160                   protoInitParam.wtx_counter_limit);
161   }
162   if (EseConfig::hasKey(NAME_RNACK_RETRY_DELAY)) {
163     num = EseConfig::getUnsigned(NAME_RNACK_RETRY_DELAY);
164     nxpese_ctxt.invalidFrame_Rnack_Delay = num;
165     NXP_LOG_ESE_D("Rnack retry_delay read from config file - %lu", num);
166   } else {
167     nxpese_ctxt.invalidFrame_Rnack_Delay = 7000;
168   }
169   if (EseConfig::hasKey(NAME_NXP_MAX_RNACK_RETRY)) {
170     protoInitParam.rnack_retry_limit =
171         EseConfig::getUnsigned(NAME_NXP_MAX_RNACK_RETRY);
172   } else {
173     protoInitParam.rnack_retry_limit = MAX_RNACK_RETRY_LIMIT;
174   }
175   if (ESE_MODE_NORMAL ==
176       initParams.initMode) /* TZ/Normal wired mode should come here*/
177   {
178     if (EseConfig::hasKey(NAME_NXP_SPI_INTF_RST_ENABLE)) {
179       protoInitParam.interfaceReset =
180           (EseConfig::getUnsigned(NAME_NXP_SPI_INTF_RST_ENABLE) == 1) ? true
181                                                                       : false;
182     } else {
183       protoInitParam.interfaceReset = true;
184     }
185   } else /* OSU mode, no interface reset is required */
186   {
187     if (phNxpEse_doResetProtection(true)) {
188       NXP_LOG_ESE_E("%s Reset Protection failed. returning...", __FUNCTION__);
189       return ESESTATUS_FAILED;
190     }
191     protoInitParam.interfaceReset = false;
192   }
193   if (EseConfig::hasKey(NAME_NXP_WTX_NTF_COUNT)) {
194     num = EseConfig::getUnsigned(NAME_NXP_WTX_NTF_COUNT);
195     protoInitParam.wtx_ntf_limit = num;
196     NXP_LOG_ESE_D("Wtx_ntf limit from config file - %lu",
197                   protoInitParam.wtx_ntf_limit);
198   } else {
199     protoInitParam.wtx_ntf_limit = PH_DEFAULT_WTX_NTF_LIMIT;
200   }
201   nxpese_ctxt.fPtr_WtxNtf = initParams.fPtr_WtxNtf;
202   /* Sharing lib context for fetching secure timer values */
203   protoInitParam.pSecureTimerParams =
204       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams;
205 
206   NXP_LOG_ESE_D("%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
207                 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
208                 nxpese_ctxt.secureTimerParams.secureTimer2,
209                 nxpese_ctxt.secureTimerParams.secureTimer3);
210 
211   phNxpEse_GetMaxTimer(&maxTimer);
212 #ifdef SPM_INTEGRATED
213   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
214     wConfigStatus = phNxpEse_SPM_DisablePwrControl(maxTimer);
215     if (wConfigStatus != ESESTATUS_SUCCESS) {
216       NXP_LOG_ESE_E("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
217     }
218   }
219 #endif
220   do {
221     /* T=1 Protocol layer open */
222     wConfigStatus = phNxpEseProto7816_Open(protoInitParam);
223     if (phNxpEse_isColdResetRequired(initParams.initMode, wConfigStatus))
224       phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
225   } while (phNxpEse_isColdResetRequired(initParams.initMode, wConfigStatus) &&
226            retry++ < 1);
227   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
228     if (ESESTATUS_TRANSCEIVE_FAILED == wConfigStatus ||
229         ESESTATUS_FAILED == wConfigStatus) {
230       nxpese_ctxt.EseLibStatus = ESE_STATUS_RECOVERY;
231     }
232   }
233 
234   if (ESESTATUS_SUCCESS == wConfigStatus) {
235     NXP_LOG_ESE_D("phNxpEseProto7816_Open completed >>>>>");
236     /* Retrieving the IFS-D value configured in the config file and applying to
237      * Card */
238     if ((nxpese_ctxt.endPointInfo == END_POINT_ESE) &&
239         (EseConfig::hasKey(NAME_NXP_ESE_IFSD_VALUE))) {
240       ifsd_value = EseConfig::getUnsigned(NAME_NXP_ESE_IFSD_VALUE);
241       if ((0xFFFF > ifsd_value) && (ifsd_value > 0)) {
242         NXP_LOG_ESE_D(
243             "phNxpEseProto7816_SetIFS IFS adjustment requested with %ld",
244             ifsd_value);
245         phNxpEse_setIfs(ifsd_value);
246       } else {
247         NXP_LOG_ESE_D(
248             "phNxpEseProto7816_SetIFS IFS adjustment argument invalid");
249       }
250     } else if ((nxpese_ctxt.endPointInfo == END_POINT_EUICC) &&
251                (EseConfig::hasKey(NAME_NXP_EUICC_IFSD_VALUE))) {
252       ifsd_value = EseConfig::getUnsigned(NAME_NXP_EUICC_IFSD_VALUE);
253       if ((0xFFFF > ifsd_value) && (ifsd_value > 0)) {
254         NXP_LOG_ESE_D(
255             "phNxpEseProto7816_SetIFS IFS adjustment requested with %ld",
256             ifsd_value);
257         phNxpEse_setIfs(ifsd_value);
258       } else {
259         NXP_LOG_ESE_D(
260             "phNxpEseProto7816_SetIFS IFS adjustment argument invalid");
261       }
262     }
263   } else {
264     NXP_LOG_ESE_E("phNxpEseProto7816_Open failed with status = %x",
265                   wConfigStatus);
266   }
267 
268   return wConfigStatus;
269 }
270 
271 /******************************************************************************
272  * Function         phNxpEse_open
273  *
274  * Description      This function is called by Jni during the
275  *                  initialization of the ESE. It opens the physical connection
276  *                  with ESE and creates required NAME_NXP_MAX_RNACK_RETRY
277  *                  client thread for operation.
278  * Returns          This function return ESESTATUS_SUCCESS (0) in case of
279  *                  success. In case of failure returns other failure values.
280  *
281  ******************************************************************************/
phNxpEse_open(phNxpEse_initParams initParams)282 ESESTATUS phNxpEse_open(phNxpEse_initParams initParams) {
283   phPalEse_Config_t tPalConfig;
284   ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
285   unsigned long int num = 0, tpm_enable = 0;
286   char ese_dev_node[64];
287   std::string ese_node;
288 #ifdef SPM_INTEGRATED
289   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
290   spm_state_t current_spm_state = SPM_STATE_INVALID;
291 #endif
292   /* initialize trace level */
293   phNxpLog_InitializeLogLevel();
294 
295   phPalEse_initTimer();
296 
297   NXP_LOG_ESE_D("phNxpEse_open Enter");
298   /*When spi channel is already opened return status as FAILED*/
299   if (nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE) {
300     NXP_LOG_ESE_D("already opened\n");
301     return ESESTATUS_BUSY;
302   }
303 
304   phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
305   phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
306 
307   NXP_LOG_ESE_D("MW SEAccessKit Version");
308   NXP_LOG_ESE_D("Android Version:0x%x", NXP_ANDROID_VER);
309   NXP_LOG_ESE_D("Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
310   NXP_LOG_ESE_D("Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
311 
312   if (EseConfig::hasKey(NAME_NXP_OS_VERSION)) {
313     num = EseConfig::getUnsigned(NAME_NXP_OS_VERSION);
314     NXP_LOG_ESE_D("Chip type read from config file - %lu", num);
315     sOsVersion = (num == 1) ? OS_VERSION_4_0
316                             : ((num == 2) ? OS_VERSION_5_1 : OS_VERSION_5_2);
317   } else {
318     sOsVersion = OS_VERSION_5_2;
319     NXP_LOG_ESE_D("Chip type not defined in config file osVersion- %d",
320                   sOsVersion);
321   }
322   if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
323     tpm_enable = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
324     NXP_LOG_ESE_D(
325         "SPI Throughput measurement enable/disable read from config file - %lu",
326         tpm_enable);
327   } else {
328     NXP_LOG_ESE_D("SPI Throughput not defined in config file - %lu",
329                   tpm_enable);
330   }
331 #if (NXP_POWER_SCHEME_SUPPORT == true)
332   if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
333     num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
334     nxpese_ctxt.pwr_scheme = num;
335     NXP_LOG_ESE_D("Power scheme read from config file - %lu", num);
336   } else {
337     nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
338     NXP_LOG_ESE_D("Power scheme not defined in config file - %lu", num);
339   }
340 #else
341   nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
342   tpm_enable = 0x00;
343 #endif
344 
345   if (EseConfig::hasKey(NAME_NXP_NAD_POLL_RETRY_TIME)) {
346     num = EseConfig::getUnsigned(NAME_NXP_NAD_POLL_RETRY_TIME);
347     nxpese_ctxt.nadPollingRetryTime = num;
348   } else {
349     nxpese_ctxt.nadPollingRetryTime = 5;
350   }
351 
352   NXP_LOG_ESE_D("Nad poll retry time in us - %lu us",
353                 nxpese_ctxt.nadPollingRetryTime * GET_WAKE_UP_DELAY() *
354                     NAD_POLLING_SCALER);
355 
356   /*Read device node path*/
357   ese_node = EseConfig::getString(NAME_NXP_ESE_DEV_NODE, "/dev/pn81a");
358   strlcpy(ese_dev_node, ese_node.c_str(), sizeof(ese_dev_node));
359   tPalConfig.pDevName = (int8_t*)ese_dev_node;
360 
361   /* Initialize PAL layer */
362   wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
363   if (wConfigStatus != ESESTATUS_SUCCESS) {
364     NXP_LOG_ESE_E("phPalEse_Init Failed");
365     if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
366       if (ESESTATUS_DRIVER_BUSY == wConfigStatus)
367         NXP_LOG_ESE_E("Ese Driver is Busy!!!");
368     }
369     goto clean_and_return;
370   }
371   /* Copying device handle to ESE Lib context*/
372   nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
373   if (ESE_PROTOCOL_MEDIA_SPI == initParams.mediaType) {
374     NXP_LOG_ESE_D("Inform eSE about the starting of trusted Mode");
375     wConfigStatus =
376         phPalEse_ioctl(phPalEse_e_SetSecureMode, tPalConfig.pDevHandle, 0x01);
377     if (ESESTATUS_SUCCESS != wConfigStatus) goto clean_and_return_2;
378   }
379 #ifdef SPM_INTEGRATED
380   /* Get the Access of ESE*/
381   wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
382   if (wSpmStatus != ESESTATUS_SUCCESS) {
383     NXP_LOG_ESE_E("phNxpEse_SPM_Init Failed");
384     wConfigStatus = ESESTATUS_FAILED;
385     goto clean_and_return_2;
386   }
387   wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
388   if (wSpmStatus != ESESTATUS_SUCCESS) {
389     NXP_LOG_ESE_E(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
390     wConfigStatus = ESESTATUS_FAILED;
391     goto clean_and_return_1;
392   }
393   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
394     wConfigStatus = phNxpEse_checkFWDwnldStatus();
395     if (wConfigStatus != ESESTATUS_SUCCESS) {
396       NXP_LOG_ESE_E("Failed to open SPI due to VEN pin used by FW download \n");
397       wConfigStatus = ESESTATUS_FAILED;
398       goto clean_and_return_1;
399     }
400   }
401   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
402   if (wSpmStatus != ESESTATUS_SUCCESS) {
403     NXP_LOG_ESE_E(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
404     wConfigStatus = ESESTATUS_FAILED;
405     goto clean_and_return_1;
406   } else {
407     if (((current_spm_state & SPM_STATE_SPI) |
408          (current_spm_state & SPM_STATE_SPI_PRIO)) &&
409         !(current_spm_state & SPM_STATE_SPI_FAILED)) {
410       NXP_LOG_ESE_E(" %s : SPI is already opened...second instance not allowed",
411                     __FUNCTION__);
412       wConfigStatus = ESESTATUS_FAILED;
413       goto clean_and_return_1;
414     }
415   }
416   if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
417     NXP_LOG_ESE_E(" %s : Denying to open JCOP Download in progress",
418                   __FUNCTION__);
419     wConfigStatus = ESESTATUS_FAILED;
420     goto clean_and_return_1;
421   }
422   phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams,
423                   sizeof(phNxpEse_initParams));
424   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
425     /* Updating ESE power state based on the init mode */
426     if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
427       NXP_LOG_ESE_D("%s Init mode ---->OSU", __FUNCTION__);
428       wConfigStatus = phNxpEse_checkJcopDwnldState();
429       if (wConfigStatus != ESESTATUS_SUCCESS) {
430         NXP_LOG_ESE_E("phNxpEse_checkJcopDwnldState failed");
431         goto clean_and_return_1;
432       }
433     }
434   }
435   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_ENABLE);
436   if (wSpmStatus != ESESTATUS_SUCCESS) {
437     NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: enabling power Failed");
438     if (wSpmStatus == ESESTATUS_BUSY) {
439       wConfigStatus = ESESTATUS_BUSY;
440     } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
441       wConfigStatus = ESESTATUS_DWNLD_BUSY;
442     } else {
443       wConfigStatus = ESESTATUS_FAILED;
444     }
445     goto clean_and_return;
446   } else {
447     NXP_LOG_ESE_D("nxpese_ctxt.spm_power_state true");
448     nxpese_ctxt.spm_power_state = true;
449   }
450 #endif
451   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
452     if (tpm_enable) {
453       wConfigStatus = phPalEse_ioctl(phPalEse_e_EnableThroughputMeasurement,
454                                      nxpese_ctxt.pDevHandle, 0);
455       if (wConfigStatus != ESESTATUS_SUCCESS) {
456         NXP_LOG_ESE_E("phPalEse_IoCtl Failed");
457         goto clean_and_return;
458       }
459     }
460   }
461   NXP_LOG_ESE_D("wConfigStatus %x", wConfigStatus);
462   return wConfigStatus;
463 
464 clean_and_return:
465 #ifdef SPM_INTEGRATED
466   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
467   if (wSpmStatus != ESESTATUS_SUCCESS) {
468     NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: disabling power Failed");
469   }
470 clean_and_return_1:
471   phNxpEse_SPM_DeInit();
472 clean_and_return_2:
473 #endif
474   if (NULL != nxpese_ctxt.pDevHandle) {
475     phPalEse_close(nxpese_ctxt.pDevHandle);
476     phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
477   }
478   nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
479   nxpese_ctxt.spm_power_state = false;
480   return ESESTATUS_FAILED;
481 }
482 
483 /******************************************************************************
484  * \ingroup spi_libese
485  *
486  * \brief  Check if libese has opened
487  *
488  * \retval return false if it is close, otherwise true.
489  **
490  ******************************************************************************/
phNxpEse_isOpen()491 bool phNxpEse_isOpen() { return nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE; }
492 
493 /******************************************************************************
494  * Function         phNxpEse_openPrioSession
495  *
496  * Description      This function is called by Jni during the
497  *                  initialization of the ESE. It opens the physical connection
498  *                  with ESE () and creates required client thread for
499  *                  operation.  This will get priority access to ESE for timeout
500  duration.
501 
502  * Returns          This function return ESESTATUS_SUCCESS (0) in case of
503  success
504  *                  In case of failure returns other failure value.
505  *
506  ******************************************************************************/
phNxpEse_openPrioSession(phNxpEse_initParams initParams)507 ESESTATUS phNxpEse_openPrioSession(phNxpEse_initParams initParams) {
508   phPalEse_Config_t tPalConfig;
509   ESESTATUS wConfigStatus = ESESTATUS_SUCCESS;
510   unsigned long int num = 0, tpm_enable = 0;
511 
512   /* initialize trace level */
513   phNxpLog_InitializeLogLevel();
514   NXP_LOG_ESE_D("phNxpEse_openPrioSession Enter");
515 #ifdef SPM_INTEGRATED
516   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
517   spm_state_t current_spm_state = SPM_STATE_INVALID;
518 #endif
519   phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
520   phNxpEse_memset(&tPalConfig, 0x00, sizeof(tPalConfig));
521 
522   NXP_LOG_ESE_D("MW SEAccessKit Version");
523   NXP_LOG_ESE_D("Android Version:0x%x", NXP_ANDROID_VER);
524   NXP_LOG_ESE_D("Major Version:0x%x", ESELIB_MW_VERSION_MAJ);
525   NXP_LOG_ESE_D("Minor Version:0x%x", ESELIB_MW_VERSION_MIN);
526 
527 #if (NXP_POWER_SCHEME_SUPPORT == true)
528   if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
529     num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
530     nxpese_ctxt.pwr_scheme = num;
531     NXP_LOG_ESE_D("Power scheme read from config file - %lu", num);
532   } else
533 #endif
534   {
535     nxpese_ctxt.pwr_scheme = PN67T_POWER_SCHEME;
536     NXP_LOG_ESE_D("Power scheme not defined in config file - %lu", num);
537   }
538   if (EseConfig::hasKey(NAME_NXP_TP_MEASUREMENT)) {
539     tpm_enable = EseConfig::getUnsigned(NAME_NXP_TP_MEASUREMENT);
540     NXP_LOG_ESE_D(
541         "SPI Throughput measurement enable/disable read from config file - %lu",
542         tpm_enable);
543   } else {
544     NXP_LOG_ESE_D("SPI Throughput not defined in config file - %lu", num);
545   }
546 
547   tPalConfig.pDevName = (int8_t*)"/dev/p73";
548 
549   /* Initialize PAL layer */
550   wConfigStatus = phPalEse_open_and_configure(&tPalConfig);
551   if (wConfigStatus != ESESTATUS_SUCCESS) {
552     NXP_LOG_ESE_E("phPalEse_Init Failed");
553     goto clean_and_return;
554   }
555   /* Copying device handle to hal context*/
556   nxpese_ctxt.pDevHandle = tPalConfig.pDevHandle;
557 
558 #ifdef SPM_INTEGRATED
559   /* Get the Access of ESE*/
560   wSpmStatus = phNxpEse_SPM_Init(nxpese_ctxt.pDevHandle);
561   if (wSpmStatus != ESESTATUS_SUCCESS) {
562     NXP_LOG_ESE_E("phNxpEse_SPM_Init Failed");
563     wConfigStatus = ESESTATUS_FAILED;
564     goto clean_and_return_2;
565   }
566   wSpmStatus = phNxpEse_SPM_SetPwrScheme(nxpese_ctxt.pwr_scheme);
567   if (wSpmStatus != ESESTATUS_SUCCESS) {
568     NXP_LOG_ESE_E(" %s : phNxpEse_SPM_SetPwrScheme Failed", __FUNCTION__);
569     wConfigStatus = ESESTATUS_FAILED;
570     goto clean_and_return_1;
571   }
572   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
573   if (wSpmStatus != ESESTATUS_SUCCESS) {
574     NXP_LOG_ESE_E(" %s : phNxpEse_SPM_GetPwrState Failed", __FUNCTION__);
575     wConfigStatus = ESESTATUS_FAILED;
576     goto clean_and_return_1;
577   } else {
578     if ((current_spm_state & SPM_STATE_SPI) |
579         (current_spm_state & SPM_STATE_SPI_PRIO)) {
580       NXP_LOG_ESE_E(" %s : SPI is already opened...second instance not allowed",
581                     __FUNCTION__);
582       wConfigStatus = ESESTATUS_FAILED;
583       goto clean_and_return_1;
584     }
585     if (current_spm_state & SPM_STATE_JCOP_DWNLD) {
586       NXP_LOG_ESE_E(" %s : Denying to open JCOP Download in progress",
587                     __FUNCTION__);
588       wConfigStatus = ESESTATUS_FAILED;
589       goto clean_and_return_1;
590     }
591     if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
592       wConfigStatus = phNxpEse_checkFWDwnldStatus();
593       if (wConfigStatus != ESESTATUS_SUCCESS) {
594         NXP_LOG_ESE_D(
595             "Failed to open SPI due to VEN pin used by FW download \n");
596         wConfigStatus = ESESTATUS_FAILED;
597         goto clean_and_return_1;
598       }
599     }
600   }
601   phNxpEse_memcpy(&nxpese_ctxt.initParams, &initParams.initMode,
602                   sizeof(phNxpEse_initParams));
603   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
604     /* Updating ESE power state based on the init mode */
605     if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
606       wConfigStatus = phNxpEse_checkJcopDwnldState();
607       if (wConfigStatus != ESESTATUS_SUCCESS) {
608         NXP_LOG_ESE_E("phNxpEse_checkJcopDwnldState failed");
609         goto clean_and_return_1;
610       }
611     }
612   }
613   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_PRIO_ENABLE);
614   if (wSpmStatus != ESESTATUS_SUCCESS) {
615     NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: enabling power for spi prio Failed");
616     if (wSpmStatus == ESESTATUS_BUSY) {
617       wConfigStatus = ESESTATUS_BUSY;
618     } else if (wSpmStatus == ESESTATUS_DWNLD_BUSY) {
619       wConfigStatus = ESESTATUS_DWNLD_BUSY;
620     } else {
621       wConfigStatus = ESESTATUS_FAILED;
622     }
623     goto clean_and_return;
624   } else {
625     NXP_LOG_ESE_E("nxpese_ctxt.spm_power_state true");
626     nxpese_ctxt.spm_power_state = true;
627   }
628 #endif
629 
630 #ifndef SPM_INTEGRATED
631   wConfigStatus =
632       phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
633   if (wConfigStatus != ESESTATUS_SUCCESS) {
634     NXP_LOG_ESE_E("phPalEse_IoCtl Failed");
635     goto clean_and_return;
636   }
637 #endif
638   wConfigStatus =
639       phPalEse_ioctl(phPalEse_e_EnableLog, nxpese_ctxt.pDevHandle, 0);
640   if (wConfigStatus != ESESTATUS_SUCCESS) {
641     NXP_LOG_ESE_E("phPalEse_IoCtl Failed");
642     goto clean_and_return;
643   }
644   wConfigStatus =
645       phPalEse_ioctl(phPalEse_e_EnablePollMode, nxpese_ctxt.pDevHandle, 1);
646   if (wConfigStatus != ESESTATUS_SUCCESS) {
647     NXP_LOG_ESE_E("phPalEse_IoCtl Failed");
648     goto clean_and_return;
649   }
650   NXP_LOG_ESE_D("wConfigStatus %x", wConfigStatus);
651   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
652     if (tpm_enable) {
653       wConfigStatus = phPalEse_ioctl(phPalEse_e_EnableThroughputMeasurement,
654                                      nxpese_ctxt.pDevHandle, 0);
655       if (wConfigStatus != ESESTATUS_SUCCESS) {
656         NXP_LOG_ESE_E("phPalEse_IoCtl Failed");
657         goto clean_and_return;
658       }
659     }
660   }
661   return wConfigStatus;
662 
663 clean_and_return:
664 #ifdef SPM_INTEGRATED
665   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
666   if (wSpmStatus != ESESTATUS_SUCCESS) {
667     NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: disabling power Failed");
668   }
669 clean_and_return_1:
670   phNxpEse_SPM_DeInit();
671 clean_and_return_2:
672 #endif
673   if (NULL != nxpese_ctxt.pDevHandle) {
674     phPalEse_close(nxpese_ctxt.pDevHandle);
675     phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
676   }
677   nxpese_ctxt.EseLibStatus = ESE_STATUS_CLOSE;
678   nxpese_ctxt.spm_power_state = false;
679   return ESESTATUS_FAILED;
680 }
681 
682 /******************************************************************************
683  * Function         phNxpEse_setJcopDwnldState
684  *
685  * Description      This function is  used to check whether JCOP OS
686  *                  download can be started or not.
687  *
688  * Returns          returns  ESESTATUS_SUCCESS or ESESTATUS_FAILED
689  *
690  ******************************************************************************/
phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state)691 static ESESTATUS phNxpEse_setJcopDwnldState(phNxpEse_JcopDwnldState state) {
692   ESESTATUS wConfigStatus = ESESTATUS_FAILED;
693   NXP_LOG_ESE_D("phNxpEse_setJcopDwnldState Enter");
694 
695   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
696     wConfigStatus = phNxpEse_SPM_SetJcopDwnldState(state);
697   } else {
698     NXP_LOG_ESE_E("%s function not supported", __FUNCTION__);
699   }
700   return wConfigStatus;
701 }
702 
703 /******************************************************************************
704  * Function         phNxpEse_checkJcopDwnldState
705  *
706  * Description      This function is  used to check whether JCOP OS
707  *                  download can be started or not.
708  *
709  * Returns          returns  ESESTATUS_SUCCESS or ESESTATUS_BUSY
710  *
711  ******************************************************************************/
phNxpEse_checkJcopDwnldState(void)712 static ESESTATUS phNxpEse_checkJcopDwnldState(void) {
713   NXP_LOG_ESE_D("phNxpEse_checkJcopDwnld Enter");
714   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
715   spm_state_t current_spm_state = SPM_STATE_INVALID;
716   uint8_t ese_dwnld_retry = 0x00;
717   ESESTATUS status = ESESTATUS_FAILED;
718 
719   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
720   if (wSpmStatus == ESESTATUS_SUCCESS) {
721     /* Check current_spm_state and update config/Spm status*/
722     if ((current_spm_state & SPM_STATE_JCOP_DWNLD) ||
723         (current_spm_state & SPM_STATE_WIRED))
724       return ESESTATUS_BUSY;
725 
726     status = phNxpEse_setJcopDwnldState(JCP_DWNLD_INIT);
727     if (status == ESESTATUS_SUCCESS) {
728       while (ese_dwnld_retry < ESE_JCOP_OS_DWNLD_RETRY_CNT) {
729         NXP_LOG_ESE_D("ESE_JCOP_OS_DWNLD_RETRY_CNT retry count");
730         wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
731         if (wSpmStatus == ESESTATUS_SUCCESS) {
732           if ((current_spm_state & SPM_STATE_JCOP_DWNLD)) {
733             status = ESESTATUS_SUCCESS;
734             break;
735           }
736         } else {
737           status = ESESTATUS_FAILED;
738           break;
739         }
740         phNxpEse_Sleep(
741             200000); /*sleep for 200 ms checking for jcop dwnld status*/
742         ese_dwnld_retry++;
743       }
744     }
745   }
746 
747   NXP_LOG_ESE_D("phNxpEse_checkJcopDwnldState status %x", status);
748   return status;
749 }
750 
751 /******************************************************************************
752  * Function         phNxpEse_Transceive
753  *
754  * Description      This function update the len and provided buffer
755  *
756  * Returns          On Success ESESTATUS_SUCCESS else proper error code
757  *
758  ******************************************************************************/
phNxpEse_Transceive(phNxpEse_data * pCmd,phNxpEse_data * pRsp)759 ESESTATUS phNxpEse_Transceive(phNxpEse_data* pCmd, phNxpEse_data* pRsp) {
760   ESESTATUS status = ESESTATUS_FAILED;
761 
762   if ((NULL == pCmd) || (NULL == pRsp)) return ESESTATUS_INVALID_PARAMETER;
763 
764   if ((pCmd->len == 0) || pCmd->p_data == NULL) {
765     NXP_LOG_ESE_E(" phNxpEse_Transceive - Invalid Parameter no data\n");
766     return ESESTATUS_INVALID_PARAMETER;
767   } else if (pCmd->len > MAX_SUPPORTED_DATA_SIZE) {
768     NXP_LOG_ESE_E(" phNxpEse_Transceive - Invalid data size \n");
769     return ESESTATUS_INVALID_RECEIVE_LENGTH;
770   } else if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
771     NXP_LOG_ESE_E(" %s ESE Not Initialized \n", __FUNCTION__);
772     return ESESTATUS_NOT_INITIALISED;
773   } else if ((ESE_STATUS_BUSY == nxpese_ctxt.EseLibStatus)) {
774     NXP_LOG_ESE_E(" %s ESE - BUSY \n", __FUNCTION__);
775     return ESESTATUS_BUSY;
776   } else if ((ESE_STATUS_RECOVERY == nxpese_ctxt.EseLibStatus)) {
777     NXP_LOG_ESE_E(" %s ESE - RECOVERY \n", __FUNCTION__);
778     return ESESTATUS_RECOVERY_STARTED;
779   } else {
780     nxpese_ctxt.EseLibStatus = ESE_STATUS_BUSY;
781     status = phNxpEseProto7816_Transceive((phNxpEse_data*)pCmd,
782                                           (phNxpEse_data*)pRsp);
783     if (ESESTATUS_SUCCESS != status) {
784       NXP_LOG_ESE_E(" %s phNxpEseProto7816_Transceive- Failed \n",
785                     __FUNCTION__);
786       if (ESESTATUS_TRANSCEIVE_FAILED == status) {
787         /*MAX WTX reached*/
788         nxpese_ctxt.EseLibStatus = ESE_STATUS_RECOVERY;
789       } else {
790         /*Timeout/ No response*/
791         nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
792       }
793     } else {
794       nxpese_ctxt.EseLibStatus = ESE_STATUS_IDLE;
795     }
796     nxpese_ctxt.rnack_sent = false;
797 
798     NXP_LOG_ESE_D(" %s Exit status 0x%x \n", __FUNCTION__, status);
799     return status;
800   }
801 }
802 /******************************************************************************
803  * Function         phNxpEse_coldReset
804  *
805  * Description      This function power cycles the ESE
806  *                  (cold reset by prop. FW command) interface by
807  *                  talking to NFC HAL
808  *
809  *                  Note:
810  *                  After cold reset, phNxpEse_init need to be called to
811  *                  reset the host AP T=1 stack parameters
812  *
813  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
814  *successful else
815  *                  ESESTATUS_FAILED(1)
816  ******************************************************************************/
phNxpEse_coldReset(void)817 ESESTATUS phNxpEse_coldReset(void) {
818   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
819   NXP_LOG_ESE_D(" %s Enter \n", __FUNCTION__);
820   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
821     wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
822   } else {
823     wSpmStatus = ESESTATUS_FAILED;
824     NXP_LOG_ESE_E(" %s Function not supported \n", __FUNCTION__);
825   }
826   NXP_LOG_ESE_D(" %s Exit status 0x%x \n", __FUNCTION__, wSpmStatus);
827   return wSpmStatus;
828 }
829 
830 /******************************************************************************
831  * Function         phNxpEse_reset
832  *
833  * Description      This function reset the ESE interface and free all
834  *
835  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
836  *successful else
837  *                  ESESTATUS_FAILED(1)
838  ******************************************************************************/
phNxpEse_reset(void)839 ESESTATUS phNxpEse_reset(void) {
840   ESESTATUS status = ESESTATUS_FAILED;
841   unsigned long maxTimer = 0;
842 #ifdef SPM_INTEGRATED
843   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
844 #endif
845 
846   /* TBD : Call the ioctl to reset the ESE */
847   NXP_LOG_ESE_D(" %s Enter \n", __FUNCTION__);
848   /* Do an interface reset, don't wait to see if JCOP went through a full power
849    * cycle or not */
850   status = phNxpEseProto7816_IntfReset(
851       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
852   if (status) {
853     NXP_LOG_ESE_E("%s Ese status Failed", __FUNCTION__);
854   }
855 
856   NXP_LOG_ESE_D("%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
857                 __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
858                 nxpese_ctxt.secureTimerParams.secureTimer2,
859                 nxpese_ctxt.secureTimerParams.secureTimer3);
860   phNxpEse_GetMaxTimer(&maxTimer);
861 #ifdef SPM_INTEGRATED
862   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
863     status = phNxpEse_SPM_DisablePwrControl(maxTimer);
864     if (status != ESESTATUS_SUCCESS) {
865       NXP_LOG_ESE_E("%s phNxpEse_SPM_DisablePwrControl: failed", __FUNCTION__);
866     }
867   }
868   if ((nxpese_ctxt.pwr_scheme == PN67T_POWER_SCHEME) ||
869       (nxpese_ctxt.pwr_scheme == PN80T_LEGACY_SCHEME)) {
870     wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
871     if (wSpmStatus != ESESTATUS_SUCCESS) {
872       NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: reset Failed");
873     }
874   }
875 #else
876   /* if arg ==2 (hard reset)
877    * if arg ==1 (soft reset)
878    */
879   status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
880   if (status != ESESTATUS_SUCCESS) {
881     NXP_LOG_ESE_E("phNxpEse_reset Failed");
882   }
883 #endif
884   NXP_LOG_ESE_D(" %s Exit \n", __FUNCTION__);
885   return status;
886 }
887 
888 /******************************************************************************
889  * Function         phNxpEse_resetJcopUpdate
890  *
891  * Description      This function reset the ESE interface during JCOP Update
892  *
893  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
894  *successful else
895  *                  ESESTATUS_FAILED(1)
896  ******************************************************************************/
phNxpEse_resetJcopUpdate(void)897 ESESTATUS phNxpEse_resetJcopUpdate(void) {
898   ESESTATUS status = ESESTATUS_SUCCESS;
899   uint8_t retry = 0;
900 #ifdef SPM_INTEGRATED
901   unsigned long int num = 0;
902 #endif
903 
904   /* TBD : Call the ioctl to reset the  */
905   NXP_LOG_ESE_D(" %s Enter \n", __FUNCTION__);
906 
907   /* Reset interface after every reset irrespective of
908   whether JCOP did a full power cycle or not. */
909   do {
910     status = phNxpEseProto7816_Reset();
911     if (status != ESESTATUS_SUCCESS) phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
912   } while (status != ESESTATUS_SUCCESS && retry++ < 1);
913 
914   /* Retrieving the IFS-D value configured in the config file and applying to
915    * Card */
916   if (EseConfig::hasKey(NAME_NXP_ESE_IFSD_VALUE)) {
917     unsigned long int ifsd_value = 0;
918     ifsd_value = EseConfig::getUnsigned(NAME_NXP_ESE_IFSD_VALUE);
919     if ((0xFFFF > ifsd_value) && (ifsd_value > 0)) {
920       NXP_LOG_ESE_D(
921           "phNxpEseProto7816_SetIFS IFS adjustment requested with %ld",
922           ifsd_value);
923       phNxpEse_setIfs(ifsd_value);
924     } else {
925       NXP_LOG_ESE_D("phNxpEseProto7816_SetIFS IFS adjustment argument invalid");
926     }
927   }
928 #ifdef SPM_INTEGRATED
929 #if (NXP_POWER_SCHEME_SUPPORT == true)
930   if (EseConfig::hasKey(NAME_NXP_POWER_SCHEME)) {
931     num = EseConfig::getUnsigned(NAME_NXP_POWER_SCHEME);
932     if ((num == 1) || (num == 2)) {
933       NXP_LOG_ESE_D(" %s Call Config Pwr Reset \n", __FUNCTION__);
934       status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
935       if (status != ESESTATUS_SUCCESS) {
936         NXP_LOG_ESE_E("phNxpEse_resetJcopUpdate: reset Failed");
937         status = ESESTATUS_FAILED;
938       }
939     } else if (num == 3) {
940       NXP_LOG_ESE_D(" %s Call eSE Chip Reset \n", __FUNCTION__);
941       status = phNxpEse_chipReset();
942       if (status != ESESTATUS_SUCCESS) {
943         NXP_LOG_ESE_E("phNxpEse_resetJcopUpdate: chip reset Failed");
944         status = ESESTATUS_FAILED;
945       }
946     } else {
947       NXP_LOG_ESE_D(" %s Invalid Power scheme \n", __FUNCTION__);
948     }
949   }
950 #else
951   {
952     status = phNxpEse_SPM_ConfigPwr(SPM_POWER_RESET);
953     if (status != ESESTATUS_SUCCESS) {
954       NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: reset Failed");
955       status = ESESTATUS_FAILED;
956     }
957   }
958 #endif
959 #else
960   /* if arg ==2 (hard reset)
961    * if arg ==1 (soft reset)
962    */
963   status = phPalEse_ioctl(phPalEse_e_ResetDevice, nxpese_ctxt.pDevHandle, 2);
964   if (status != ESESTATUS_SUCCESS) {
965     NXP_LOG_ESE_E("phNxpEse_resetJcopUpdate Failed");
966   }
967 #endif
968 
969   NXP_LOG_ESE_D(" %s Exit \n", __FUNCTION__);
970   return status;
971 }
972 /******************************************************************************
973  * Function         phNxpEse_EndOfApdu
974  *
975  * Description      This function is used to send S-frame to indicate
976  *END_OF_APDU
977  *
978  * Returns          It returns ESESTATUS_SUCCESS (0) if the operation is
979  *successful else
980  *                  ESESTATUS_FAILED(1)
981  *
982  ******************************************************************************/
phNxpEse_EndOfApdu(void)983 ESESTATUS phNxpEse_EndOfApdu(void) {
984   ESESTATUS status = ESESTATUS_SUCCESS;
985 #if (NXP_ESE_END_OF_SESSION == true)
986   status = phNxpEseProto7816_Close(
987       (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
988 #endif
989   return status;
990 }
991 
992 /******************************************************************************
993  * Function         phNxpEse_chipReset
994  *
995  * Description      This function is used to reset the ESE.
996  *
997  * Returns          Always return ESESTATUS_SUCCESS (0).
998  *
999  ******************************************************************************/
phNxpEse_chipReset(void)1000 ESESTATUS phNxpEse_chipReset(void) {
1001   ESESTATUS status = ESESTATUS_FAILED;
1002   ESESTATUS bStatus = ESESTATUS_FAILED;
1003   if (nxpese_ctxt.pwr_scheme == PN80T_EXT_PMU_SCHEME) {
1004     bStatus = phNxpEseProto7816_Reset();
1005     if (!bStatus) {
1006       NXP_LOG_ESE_E(
1007           "Inside phNxpEse_chipReset, phNxpEseProto7816_Reset Failed");
1008     }
1009     status = phPalEse_ioctl(phPalEse_e_ChipRst, nxpese_ctxt.pDevHandle, 6);
1010     if (status != ESESTATUS_SUCCESS) {
1011       NXP_LOG_ESE_E("phNxpEse_chipReset  Failed");
1012     }
1013   } else {
1014     NXP_LOG_ESE_D("phNxpEse_chipReset is not supported in legacy power scheme");
1015   }
1016   return status;
1017 }
1018 
1019 /******************************************************************************
1020  * Function         phNxpEse_GetOsMode
1021  *
1022  * Description      This function is used to get OS mode(JCOP/OSU)
1023  *
1024  * Returns          0x01 : JCOP_MODE
1025  *                  0x02 : OSU_MODE
1026  *
1027  ******************************************************************************/
phNxpEse_GetOsMode(void)1028 phNxpEseProto7816_OsType_t phNxpEse_GetOsMode(void) {
1029   return phNxpEseProto7816_GetOsMode();
1030 }
1031 
1032 /******************************************************************************
1033  * Function         phNxpEse_isColdResetRequired
1034  *
1035  * Description      This function determines whether hard reset recovery is
1036  *                  required or not on protocol recovery failure.
1037  * Returns          TRUE(required)/FALSE(not required).
1038  *
1039  ******************************************************************************/
phNxpEse_isColdResetRequired(phNxpEse_initMode mode,ESESTATUS status)1040 static __inline bool phNxpEse_isColdResetRequired(phNxpEse_initMode mode,
1041                                                   ESESTATUS status) {
1042   return (mode == ESE_MODE_OSU && status != ESESTATUS_SUCCESS);
1043 }
1044 
1045 /******************************************************************************
1046  * Function         phNxpEse_doResetProtection
1047  *
1048  * Description      This function enables/disables reset protection
1049  *
1050  * Returns          SUCCESS(0)/FAIL(-1).
1051  *
1052  ******************************************************************************/
phNxpEse_doResetProtection(bool flag)1053 ESESTATUS phNxpEse_doResetProtection(bool flag) {
1054   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1055   NXP_LOG_ESE_D(" %s Enter \n", __FUNCTION__);
1056   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1057     wSpmStatus = phPalEse_ioctl(phPalEse_e_ResetProtection,
1058                                 nxpese_ctxt.pDevHandle, flag);
1059   } else {
1060     wSpmStatus = ESESTATUS_FAILED;
1061     NXP_LOG_ESE_E(" %s Function not supported \n", __FUNCTION__);
1062   }
1063   NXP_LOG_ESE_D(" %s Exit status 0x%x \n", __FUNCTION__, wSpmStatus);
1064   return wSpmStatus;
1065 }
1066 
1067 /******************************************************************************
1068  * Function         phNxpEse_deInit
1069  *
1070  * Description      This function de-initializes all the ESE protocol params
1071  *
1072  * Returns          Always return ESESTATUS_SUCCESS (0).
1073  *
1074  ******************************************************************************/
phNxpEse_deInit(void)1075 ESESTATUS phNxpEse_deInit(void) {
1076   ESESTATUS status = ESESTATUS_SUCCESS;
1077   unsigned long maxTimer = 0;
1078   unsigned long num = 0;
1079   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0 &&
1080       (ESE_STATUS_RECOVERY == nxpese_ctxt.EseLibStatus) &&
1081       ESE_PROTOCOL_MEDIA_SPI != nxpese_ctxt.initParams.mediaType) {
1082     return status;
1083   }
1084   if (nxpese_ctxt.initParams.initMode == ESE_MODE_OSU) {
1085     phNxpEse_doResetProtection(false);
1086   }
1087   /*TODO : to be removed after JCOP fix*/
1088   if (EseConfig::hasKey(NAME_NXP_VISO_DPD_ENABLED)) {
1089     num = EseConfig::getUnsigned(NAME_NXP_VISO_DPD_ENABLED);
1090   }
1091   if (num == 0 && nxpese_ctxt.nadInfo.nadRx == EUICC_NAD_RX) {
1092     // do nothing
1093   } else {
1094     status = phNxpEseProto7816_Close(
1095         (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
1096     if (status == ESESTATUS_SUCCESS) {
1097       NXP_LOG_ESE_D("%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x",
1098                     __FUNCTION__, nxpese_ctxt.secureTimerParams.secureTimer1,
1099                     nxpese_ctxt.secureTimerParams.secureTimer2,
1100                     nxpese_ctxt.secureTimerParams.secureTimer3);
1101       phNxpEse_GetMaxTimer(&maxTimer);
1102 #ifdef SPM_INTEGRATED
1103       if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
1104         status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1105         if (status != ESESTATUS_SUCCESS) {
1106           NXP_LOG_ESE_E("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1107         }
1108       } else {
1109         NXP_LOG_ESE_D("Interface reset for DPD");
1110         status = phNxpEseProto7816_IntfReset(
1111             (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
1112         if (status != ESESTATUS_SUCCESS) {
1113           NXP_LOG_ESE_E("%s IntfReset Failed ", __FUNCTION__);
1114         }
1115       }
1116 #endif
1117     }
1118   }
1119   return status;
1120 }
1121 
1122 /******************************************************************************
1123  * Function         phNxpEse_close
1124  *
1125  * Description      This function close the ESE interface and free all
1126  *                  resources.
1127  *
1128  * Returns          Always return ESESTATUS_SUCCESS (0).
1129  *
1130  ******************************************************************************/
phNxpEse_close(ESESTATUS deInitStatus)1131 ESESTATUS phNxpEse_close(ESESTATUS deInitStatus) {
1132   ESESTATUS status = ESESTATUS_SUCCESS;
1133   NXP_LOG_ESE_D("phNxpEse_close Enter");
1134 
1135   phPalEse_deInitTimer();
1136 
1137   if ((ESE_STATUS_CLOSE == nxpese_ctxt.EseLibStatus)) {
1138     NXP_LOG_ESE_E(" %s ESE Not Initialized \n", __FUNCTION__);
1139     return ESESTATUS_NOT_INITIALISED;
1140   }
1141 
1142 #ifdef SPM_INTEGRATED
1143   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1144 #endif
1145 
1146 #ifdef SPM_INTEGRATED
1147   /* Release the Access of  */
1148   wSpmStatus = phNxpEse_SPM_ConfigPwr(SPM_POWER_DISABLE);
1149   if (wSpmStatus != ESESTATUS_SUCCESS) {
1150     NXP_LOG_ESE_E("phNxpEse_SPM_ConfigPwr: disabling power Failed");
1151   } else {
1152     nxpese_ctxt.spm_power_state = false;
1153   }
1154 
1155   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
1156     if (ESE_MODE_OSU == nxpese_ctxt.initParams.initMode) {
1157       status = phNxpEse_setJcopDwnldState(JCP_SPI_DWNLD_COMPLETE);
1158       if (status != ESESTATUS_SUCCESS) {
1159         NXP_LOG_ESE_E("%s: phNxpEse_setJcopDwnldState failed", __FUNCTION__);
1160       }
1161     }
1162   } else {
1163     if (NULL != nxpese_ctxt.pDevHandle) {
1164       if (ESE_PROTOCOL_MEDIA_SPI == nxpese_ctxt.initParams.mediaType) {
1165         NXP_LOG_ESE_D("Inform eSE that trusted Mode is over");
1166         status = phPalEse_ioctl(phPalEse_e_SetSecureMode,
1167                                 nxpese_ctxt.pDevHandle, 0x00);
1168         if (status != ESESTATUS_SUCCESS) {
1169           NXP_LOG_ESE_E("%s: phPalEse_e_SetSecureMode failed", __FUNCTION__);
1170         }
1171         if (ESESTATUS_SUCCESS != phNxpEseProto7816_CloseAllSessions()) {
1172           NXP_LOG_ESE_D("eSE not responding perform hard reset");
1173           phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
1174         }
1175       } else {
1176         if (nxpese_ctxt.EseLibStatus == ESE_STATUS_RECOVERY ||
1177             (deInitStatus == ESESTATUS_RESPONSE_TIMEOUT) ||
1178             ESESTATUS_SUCCESS != phNxpEseProto7816_CloseAllSessions()) {
1179           NXP_LOG_ESE_D("eSE not responding perform hard reset");
1180           phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
1181         }
1182       }
1183       NXP_LOG_ESE_D("Interface reset for DPD");
1184       status = phNxpEseProto7816_IntfReset(
1185           (phNxpEseProto7816SecureTimer_t*)&nxpese_ctxt.secureTimerParams);
1186       if (status == ESESTATUS_TRANSCEIVE_FAILED || status == ESESTATUS_FAILED) {
1187         NXP_LOG_ESE_E("%s IntfReset Failed, perform hard reset", __FUNCTION__);
1188         // max wtx or no response of interface reset after protocol recovery
1189         phNxpEse_SPM_ConfigPwr(SPM_RECOVERY_RESET);
1190       }
1191     }
1192   }
1193 
1194   wSpmStatus = phNxpEse_SPM_DeInit();
1195   if (wSpmStatus != ESESTATUS_SUCCESS) {
1196     NXP_LOG_ESE_E("phNxpEse_SPM_DeInit Failed");
1197   }
1198 #endif
1199   if (NULL != nxpese_ctxt.pDevHandle) {
1200     phPalEse_close(nxpese_ctxt.pDevHandle);
1201     phNxpEse_memset(&nxpese_ctxt, 0x00, sizeof(nxpese_ctxt));
1202     NXP_LOG_ESE_D("phNxpEse_close - ESE Context deinit completed");
1203   }
1204   /* Return success always */
1205   return status;
1206 }
1207 
1208 /******************************************************************************
1209  * Function         phNxpEse_read
1210  *
1211  * Description      This function write the data to ESE through physical
1212  *                  interface (e.g. I2C) using the  driver interface.
1213  *                  Before sending the data to ESE, phNxpEse_write_ext
1214  *                  is called to check if there is any extension processing
1215  *                  is required for the SPI packet being sent out.
1216  *
1217  * Returns          It returns ESESTATUS_SUCCESS (0) if read successful else
1218  *                  ESESTATUS_FAILED(1)
1219  *
1220  ******************************************************************************/
phNxpEse_read(uint32_t * data_len,uint8_t ** pp_data)1221 ESESTATUS phNxpEse_read(uint32_t* data_len, uint8_t** pp_data) {
1222   ESESTATUS status = ESESTATUS_SUCCESS;
1223   int ret = -1;
1224 
1225   NXP_LOG_ESE_D("%s Enter ..", __FUNCTION__);
1226 
1227   ret = phNxpEse_readPacket(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_read_buff,
1228                             MAX_DATA_LEN);
1229   if (ret < 0) {
1230     NXP_LOG_ESE_E("PAL Read status error status = %x", status);
1231     *data_len = 2;
1232     *pp_data = nxpese_ctxt.p_read_buff;
1233     status = ESESTATUS_FAILED;
1234   } else {
1235     if (ret > MAX_DATA_LEN) {
1236       NXP_LOG_ESE_E(
1237           "%s PAL Read buffer length(%x) is greater than MAX_DATA_LEN(%x) ",
1238           __FUNCTION__, ret, MAX_DATA_LEN);
1239       PH_PAL_ESE_PRINT_PACKET_RX(nxpese_ctxt.p_read_buff,
1240                                  (uint16_t)MAX_DATA_LEN);
1241     } else {
1242       PH_PAL_ESE_PRINT_PACKET_RX(nxpese_ctxt.p_read_buff, (uint16_t)ret);
1243     }
1244     *data_len = ret;
1245     *pp_data = nxpese_ctxt.p_read_buff;
1246     status = ESESTATUS_SUCCESS;
1247   }
1248 
1249   NXP_LOG_ESE_D("%s Exit", __FUNCTION__);
1250   return status;
1251 }
1252 
1253 /******************************************************************************
1254  * Function         phNxpEse_readPacket
1255  *
1256  * Description      This function Reads requested number of bytes from
1257  *                  pn547 device into given buffer.
1258  *
1259  * Returns          nNbBytesToRead- number of successfully read bytes
1260  *                  -1        - read operation failure
1261  *
1262  ******************************************************************************/
phNxpEse_readPacket(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)1263 static int phNxpEse_readPacket(void* pDevHandle, uint8_t* pBuffer,
1264                                int nNbBytesToRead) {
1265   bool flushData = false;
1266   int ret = -1;
1267   int sof_counter = 0; /* one read may take 1 ms*/
1268   int total_count = 0, numBytesToRead = 0, headerIndex = 0;
1269 
1270   NXP_LOG_ESE_D("%s Enter", __FUNCTION__);
1271   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1272     int max_sof_counter = 0;
1273     /*Max retry to get SOF in case of chaining*/
1274     if (poll_sof_chained_delay == 1) {
1275       /*Wait Max for 1.3 sec before retry/recovery*/
1276       /*(max_sof_counter(1300) * 10 us) = 1.3 sec */
1277       max_sof_counter = ESE_POLL_TIMEOUT * 10;
1278     }
1279     /*Max retry to get SOF in case of Non-chaining*/
1280     else {
1281       /*wait based on config option */
1282       /*(nadPollingRetryTime * WAKE_UP_DELAY_SN1xx * NAD_POLLING_SCALER_SN1xx)*/
1283       max_sof_counter = ((ESE_POLL_TIMEOUT * 1000) /
1284                          (nxpese_ctxt.nadPollingRetryTime *
1285                           GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER));
1286     }
1287     if (nxpese_ctxt.rnack_sent) {
1288       phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1289     }
1290     NXP_LOG_ESE_D(
1291         "read() max_sof_counter: "
1292         "%X ESE_POLL_TIMEOUT %2X",
1293         max_sof_counter, ESE_POLL_TIMEOUT);
1294     do {
1295       ret = -1;
1296       ret = phPalEse_read(pDevHandle, pBuffer, 2);
1297       if (ret < 0) {
1298         /*Polling for read on spi, hence Debug log*/
1299         NXP_LOG_ESE_D("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1300       } else {
1301         if ((pBuffer[0] == nxpese_ctxt.nadInfo.nadRx) ||
1302             (pBuffer[0] == RECEIVE_PACKET_SOF)) {
1303           /* Read the HEADER of one byte*/
1304           NXP_LOG_ESE_D("%s Read HDR SOF + PCB", __FUNCTION__);
1305           numBytesToRead = 1; /*Read only INF LEN*/
1306           headerIndex = 1;
1307           break;
1308         } else if (((pBuffer[0] == 0x00) || (pBuffer[0] == 0xFF)) &&
1309                    ((pBuffer[1] == nxpese_ctxt.nadInfo.nadRx) ||
1310                     (pBuffer[1] == RECEIVE_PACKET_SOF))) {
1311           /* Read the HEADER of Two bytes*/
1312           NXP_LOG_ESE_D("%s Read HDR only SOF", __FUNCTION__);
1313           pBuffer[0] = pBuffer[1];
1314           numBytesToRead = 2; /*Read PCB + INF LEN*/
1315           headerIndex = 0;
1316           break;
1317         } else if (((pBuffer[0] == 0x00) && (pBuffer[1] == 0x00)) ||
1318                    ((pBuffer[0] == 0xFF) && (pBuffer[1] == 0xFF))) {
1319           // LOG(ERROR) << StringPrintf("_spi_read() Buf[0]: %X Buf[1]: %X",
1320           // pBuffer[0], pBuffer[1]);
1321         } else if (ret >= 0) { /* Corruption happened during the receipt from
1322                                   Card, go flush out the data */
1323           NXP_LOG_ESE_E("_spi_read() Corruption Buf[0]: %X Buf[1]: %X ..len=%d",
1324                         pBuffer[0], pBuffer[1], ret);
1325           break;
1326         }
1327       }
1328       /*If it is Chained packet wait for 100 usec*/
1329       if (poll_sof_chained_delay == 1) {
1330         NXP_LOG_ESE_D("%s Chained Pkt, delay read %dus", __FUNCTION__,
1331                       GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1332         phPalEse_BusyWait(GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1333       } else {
1334         /*DLOG_IF(INFO, ese_log_level)
1335          << StringPrintf("%s Normal Pkt, delay read %dus", __FUNCTION__,
1336          WAKE_UP_DELAY_SN1xx * NAD_POLLING_SCALER_SN1xx);*/
1337         phPalEse_BusyWait(nxpese_ctxt.nadPollingRetryTime *
1338                           GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1339       }
1340       sof_counter++;
1341     } while (sof_counter < max_sof_counter);
1342 
1343     /*SOF Read timeout happened, go for frame retransmission*/
1344     if (sof_counter == max_sof_counter) {
1345       ret = -1;
1346     }
1347     if (ret < 0) {
1348       /*In case of IO Error*/
1349       ret = -2;
1350       pBuffer[0] = 0x64;
1351       pBuffer[1] = 0xFF;
1352     } else if ((pBuffer[0] == nxpese_ctxt.nadInfo.nadRx) ||
1353                (pBuffer[0] == RECEIVE_PACKET_SOF)) {
1354       NXP_LOG_ESE_D("%s SOF FOUND", __FUNCTION__);
1355       /* Read the HEADER of one/Two bytes based on how two bytes read A5 PCB or
1356        * 00 A5*/
1357       ret =
1358           phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
1359       if (ret < 0) {
1360         NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1361         flushData = true;
1362       } else {
1363         if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
1364             (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
1365           poll_sof_chained_delay = 1;
1366           NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1367                         poll_sof_chained_delay);
1368         } else {
1369           poll_sof_chained_delay = 0;
1370           NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1371                         poll_sof_chained_delay);
1372         }
1373         total_count = 3;
1374         uint8_t pcb;
1375         phNxpEseProto7816_PCB_bits_t pcb_bits;
1376         pcb = pBuffer[PH_PROPTO_7816_PCB_OFFSET];
1377 
1378         phNxpEse_memset(&pcb_bits, 0x00, sizeof(phNxpEseProto7816_PCB_bits_t));
1379         phNxpEse_memcpy(&pcb_bits, &pcb, sizeof(uint8_t));
1380 
1381         /*For I-Frame Only*/
1382         if (0 == pcb_bits.msb) {
1383           if (pBuffer[2] != EXTENDED_FRAME_MARKER) {
1384             nNbBytesToRead = (pBuffer[2] & 0x000000FF);
1385             headerIndex = 3;
1386           } else {
1387             ret = phPalEse_read(pDevHandle, &pBuffer[3], 2);
1388             if (ret < 0) {
1389               NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1390               flushData = true;
1391             } else {
1392               nNbBytesToRead = (pBuffer[3] << 8);
1393               nNbBytesToRead = nNbBytesToRead | pBuffer[4];
1394               /*If I-Frame received with invalid length respond with RNACK*/
1395               if ((nNbBytesToRead == 0) || (nNbBytesToRead > MAX_DATA_LEN) ||
1396                   (nNbBytesToRead > phNxpEseProto7816_GetIfs())) {
1397                 NXP_LOG_ESE_D("I-Frame with invalid len == %d", nNbBytesToRead);
1398                 flushData = true;
1399               } else {
1400                 NXP_LOG_ESE_E("_spi_read() [HDR]EXTENDED_FRAME_MARKER, ret=%d",
1401                               ret);
1402                 total_count += 2;
1403                 headerIndex = 5;
1404               }
1405             }
1406           }
1407         } else {
1408           /*For Non-IFrame*/
1409           nNbBytesToRead = (pBuffer[2] & 0x000000FF);
1410           headerIndex = 3;
1411         }
1412         if (!flushData) {
1413           /* Read the Complete data + one byte CRC*/
1414           ret = phPalEse_read(pDevHandle, &pBuffer[headerIndex],
1415                               (nNbBytesToRead + 1));
1416           if (ret < 0) {
1417             NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1418             ret = -1;
1419           } else {
1420             ret = (total_count + (nNbBytesToRead + 1));
1421           }
1422           nxpese_ctxt.rnack_sent = false;
1423         }
1424       }
1425     } else {
1426       flushData = true;
1427     }
1428     if (flushData) {
1429       /* Received corrupted frame:
1430          Flushing out data in the Rx buffer so that Card can switch the mode */
1431       uint16_t ifsd_size = phNxpEseProto7816_GetIfs();
1432       uint32_t total_frame_size = 0;
1433       NXP_LOG_ESE_E("_spi_read() corrupted, IFSD size=%d flushing it out!!",
1434                     ifsd_size);
1435       /* If a non-zero byte is received while polling for NAD byte and the byte
1436          is not a valid NAD byte (0xA5 or 0xB4): 1)  Read & discard (without
1437          de-asserting SPI CS line) : a.  Max IFSD size + 5 (remaining four
1438          prologue + one LRC bytes) bytes from eSE  if max IFS size is greater
1439          than 254 bytes OR b.  Max IFSD size + 3 (remaining two prologue + one
1440          LRC bytes) bytes from eSE  if max IFS size is less than 255 bytes.
1441          2) Send R-NACK to request eSE to re-transmit the frame*/
1442       if (ifsd_size > IFSC_SIZE_SEND) {
1443         total_frame_size = ifsd_size + 4;
1444       } else {
1445         total_frame_size = ifsd_size + 2;
1446       }
1447       nxpese_ctxt.rnack_sent = true;
1448       phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1449       ret = phPalEse_read(pDevHandle, &pBuffer[2], total_frame_size);
1450       if (ret < 0) {
1451         NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1452       } else { /* LRC fail expected for this frame to send R-NACK*/
1453         NXP_LOG_ESE_D(
1454             "_spi_read() SUCCESS  ret : %X LRC fail expected for this frame",
1455             ret);
1456         PH_PAL_ESE_PRINT_PACKET_RX(pBuffer, ret);
1457       }
1458       pBuffer[0] = 0x90;
1459       pBuffer[1] = RECEIVE_PACKET_SOF;
1460       ret = 0x02;
1461       phPalEse_sleep(nxpese_ctxt.invalidFrame_Rnack_Delay);
1462     }
1463   } else {
1464     ret = phNxpEse_readPacket_legacy(pDevHandle, pBuffer, nNbBytesToRead);
1465   }
1466   NXP_LOG_ESE_D("%s Exit ret = %d", __FUNCTION__, ret);
1467   return ret;
1468 }
1469 
1470 /******************************************************************************
1471  * Function         phNxpEse_readPacket_legacy
1472  *
1473  * Description      This function Reads requested number of bytes from
1474  *                  pn547 device into given buffer.
1475  *
1476  * Returns          nNbBytesToRead- number of successfully read bytes
1477  *                  -1        - read operation failure
1478  *
1479  ******************************************************************************/
phNxpEse_readPacket_legacy(void * pDevHandle,uint8_t * pBuffer,int nNbBytesToRead)1480 static int phNxpEse_readPacket_legacy(void* pDevHandle, uint8_t* pBuffer,
1481                                       int nNbBytesToRead) {
1482   int ret = -1;
1483   int sof_counter = 0; /* one read may take 1 ms*/
1484   int total_count = 0, numBytesToRead = 0, headerIndex = 0;
1485   do {
1486     sof_counter++;
1487     ret = -1;
1488     ret = phPalEse_read(pDevHandle, pBuffer, 2);
1489     if (ret < 0) {
1490       /*Polling for read on spi, hence Debug log*/
1491       NXP_LOG_ESE_D("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1492     }
1493     if (pBuffer[0] == RECEIVE_PACKET_SOF) {
1494       /* Read the HEADER of one byte*/
1495       NXP_LOG_ESE_D("%s Read HDR", __FUNCTION__);
1496       numBytesToRead = 1;
1497       headerIndex = 1;
1498       break;
1499     } else if (pBuffer[1] == RECEIVE_PACKET_SOF) {
1500       /* Read the HEADER of Two bytes*/
1501       NXP_LOG_ESE_D("%s Read HDR", __FUNCTION__);
1502       pBuffer[0] = RECEIVE_PACKET_SOF;
1503       numBytesToRead = 2;
1504       headerIndex = 0;
1505       break;
1506     }
1507     /*If it is Chained packet wait for 100 usec*/
1508     if (poll_sof_chained_delay == 1) {
1509       NXP_LOG_ESE_D("%s Chained Pkt, delay read %dus", __FUNCTION__,
1510                     GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1511       phPalEse_sleep(GET_WAKE_UP_DELAY() * CHAINED_PKT_SCALER);
1512     } else {
1513       NXP_LOG_ESE_D("%s Normal Pkt, delay read %dus", __FUNCTION__,
1514                     GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1515       phPalEse_sleep(GET_WAKE_UP_DELAY() * NAD_POLLING_SCALER);
1516     }
1517   } while (sof_counter < ESE_NAD_POLLING_MAX);
1518   if (pBuffer[0] == RECEIVE_PACKET_SOF) {
1519     NXP_LOG_ESE_D("%s SOF FOUND", __FUNCTION__);
1520     /* Read the HEADER of one/Two bytes based on how two bytes read A5 PCB or
1521      * 00 A5*/
1522     ret = phPalEse_read(pDevHandle, &pBuffer[1 + headerIndex], numBytesToRead);
1523     if (ret < 0) {
1524       NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1525     }
1526     if ((pBuffer[1] == CHAINED_PACKET_WITHOUTSEQN) ||
1527         (pBuffer[1] == CHAINED_PACKET_WITHSEQN)) {
1528       poll_sof_chained_delay = 1;
1529       NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1530                     poll_sof_chained_delay);
1531     } else {
1532       poll_sof_chained_delay = 0;
1533       NXP_LOG_ESE_D("poll_sof_chained_delay value is %d ",
1534                     poll_sof_chained_delay);
1535     }
1536     total_count = 3;
1537     nNbBytesToRead = (pBuffer[2] & 0x000000FF);
1538     /* Read the Complete data + one byte CRC*/
1539     ret = phPalEse_read(pDevHandle, &pBuffer[3], (nNbBytesToRead + 1));
1540     if (ret < 0) {
1541       NXP_LOG_ESE_E("_spi_read() [HDR]errno : %x ret : %X", errno, ret);
1542       ret = -1;
1543     } else {
1544       ret = (total_count + (nNbBytesToRead + 1));
1545     }
1546   } else if (ret < 0) {
1547     /*In case of IO Error*/
1548     ret = -2;
1549     pBuffer[0] = 0x64;
1550     pBuffer[1] = 0xFF;
1551   } else {
1552     ret = -1;
1553   }
1554   return ret;
1555 }
1556 
1557 /******************************************************************************
1558  * Function         phNxpEse_WriteFrame
1559  *
1560  * Description      This is the actual function which is being called by
1561  *                  phNxpEse_write. This function writes the data to ESE.
1562  *                  It waits till write callback provide the result of write
1563  *                  process.
1564  *
1565  * Returns          It returns ESESTATUS_SUCCESS (0) if write successful else
1566  *                  ESESTATUS_FAILED(1)
1567  *
1568  ******************************************************************************/
phNxpEse_WriteFrame(uint32_t data_len,uint8_t * p_data)1569 ESESTATUS phNxpEse_WriteFrame(uint32_t data_len, uint8_t* p_data) {
1570   ESESTATUS status = ESESTATUS_INVALID_PARAMETER;
1571   int32_t dwNoBytesWrRd = 0;
1572   NXP_LOG_ESE_D("Enter %s ", __FUNCTION__);
1573   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1574     /* TODO where to set the nad id */
1575     p_data[0] = nxpese_ctxt.nadInfo.nadTx;
1576   } else {
1577     p_data[0] = ESE_NAD_TX;
1578   }
1579   /* Create local copy of cmd_data */
1580   phNxpEse_memcpy(nxpese_ctxt.p_cmd_data, p_data, data_len);
1581   nxpese_ctxt.cmd_len = data_len;
1582 
1583   // eSE requires around 200 usec to switch from tx to rx mode
1584   // As per the observation, debug logs when enabled introduces around
1585   // same amount of delay, therefore below explicit delay is required
1586   // only if debug logs are disabled
1587   if (ese_log_level < NXPESE_LOGLEVEL_DEBUG) phPalEse_BusyWait(200 /*usecs*/);
1588 
1589   dwNoBytesWrRd = phPalEse_write(nxpese_ctxt.pDevHandle, nxpese_ctxt.p_cmd_data,
1590                                  nxpese_ctxt.cmd_len);
1591   if (-1 == dwNoBytesWrRd) {
1592     NXP_LOG_ESE_E(" - Error in SPI Write.....%d\n", errno);
1593     status = ESESTATUS_FAILED;
1594   } else {
1595     status = ESESTATUS_SUCCESS;
1596     PH_PAL_ESE_PRINT_PACKET_TX(nxpese_ctxt.p_cmd_data, nxpese_ctxt.cmd_len);
1597   }
1598   NXP_LOG_ESE_I("Exit %s status %x\n", __FUNCTION__, status);
1599   return status;
1600 }
1601 
1602 /******************************************************************************
1603  * Function         phNxpEse_getAtr
1604  *
1605  * Description      This function retrieves ATR bytes from 7816-3 layer
1606  *Update.
1607  *
1608  * Returns          It returns ESESTATUS_SUCCESS (0) if write successful else
1609  *                  ESESTATUS_FAILED(1
1610  *
1611  ******************************************************************************/
phNxpEse_getAtr(phNxpEse_data * pATR)1612 ESESTATUS phNxpEse_getAtr(phNxpEse_data* pATR) {
1613   ESESTATUS status = ESESTATUS_FAILED;
1614   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1615     status = phNxpEseProto7816_getAtr(pATR);
1616   } else {
1617     NXP_LOG_ESE_E(" %s - Function not supported\n", __FUNCTION__);
1618   }
1619   return status;
1620 }
1621 
1622 /******************************************************************************
1623  * Function         phNxpEse_setIfs
1624  *
1625  * Description      This function sets the IFS size to 240/254 support JCOP OS
1626  *Update.
1627  *
1628  * Returns          Always return ESESTATUS_SUCCESS (0).
1629  *
1630  ******************************************************************************/
phNxpEse_setIfs(uint16_t IFS_Size)1631 ESESTATUS phNxpEse_setIfs(uint16_t IFS_Size) {
1632   phNxpEseProto7816_SetIfs(IFS_Size);
1633   return ESESTATUS_SUCCESS;
1634 }
1635 
1636 /******************************************************************************
1637  * Function         phNxpEse_Sleep
1638  *
1639  * Description      This function  suspends execution of the calling thread for
1640  *           (at least) usec microseconds
1641  *
1642  * Returns          Always return ESESTATUS_SUCCESS (0).
1643  *
1644  ******************************************************************************/
phNxpEse_Sleep(uint32_t usec)1645 ESESTATUS phNxpEse_Sleep(uint32_t usec) {
1646   phPalEse_sleep(usec);
1647   return ESESTATUS_SUCCESS;
1648 }
1649 
1650 /******************************************************************************
1651  * Function         phNxpEse_memset
1652  *
1653  * Description      This function updates destination buffer with val
1654  *                  data in len size
1655  *
1656  * Returns          Always return ESESTATUS_SUCCESS (0).
1657  *
1658  ******************************************************************************/
phNxpEse_memset(void * buff,int val,size_t len)1659 void* phNxpEse_memset(void* buff, int val, size_t len) {
1660   return phPalEse_memset(buff, val, len);
1661 }
1662 
1663 /******************************************************************************
1664  * Function         phNxpEse_memcpy
1665  *
1666  * Description      This function copies source buffer to  destination buffer
1667  *                  data in len size
1668  *
1669  * Returns          Return pointer to allocated memory location.
1670  *
1671  ******************************************************************************/
phNxpEse_memcpy(void * dest,const void * src,size_t len)1672 void* phNxpEse_memcpy(void* dest, const void* src, size_t len) {
1673   return phPalEse_memcpy(dest, src, len);
1674 }
1675 
1676 /******************************************************************************
1677  * Function         phNxpEse_Memalloc
1678  *
1679  * Description      This function allocation memory
1680  *
1681  * Returns          Return pointer to allocated memory or NULL.
1682  *
1683  ******************************************************************************/
phNxpEse_memalloc(uint32_t size)1684 void* phNxpEse_memalloc(uint32_t size) {
1685   return phPalEse_memalloc(size);
1686   ;
1687 }
1688 
1689 /******************************************************************************
1690  * Function         phNxpEse_calloc
1691  *
1692  * Description      This is utility function for runtime heap memory allocation
1693  *
1694  * Returns          Return pointer to allocated memory or NULL.
1695  *
1696  ******************************************************************************/
phNxpEse_calloc(size_t datatype,size_t size)1697 void* phNxpEse_calloc(size_t datatype, size_t size) {
1698   return phPalEse_calloc(datatype, size);
1699 }
1700 
1701 /******************************************************************************
1702  * Function         phNxpEse_free
1703  *
1704  * Description      This function de-allocation memory
1705  *
1706  * Returns         void.
1707  *
1708  ******************************************************************************/
phNxpEse_free(void * ptr)1709 void phNxpEse_free(void* ptr) { return phPalEse_free(ptr); }
1710 
1711 /******************************************************************************
1712  * Function         phNxpEse_GetMaxTimer
1713  *
1714  * Description      This function finds out the max. timer value returned from
1715  *JCOP
1716  *
1717  * Returns          void.
1718  *
1719  ******************************************************************************/
phNxpEse_GetMaxTimer(unsigned long * pMaxTimer)1720 static void phNxpEse_GetMaxTimer(unsigned long* pMaxTimer) {
1721   /* Finding the max. of the timer value */
1722   *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer1;
1723   if (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer2)
1724     *pMaxTimer = nxpese_ctxt.secureTimerParams.secureTimer2;
1725   *pMaxTimer = (*pMaxTimer < nxpese_ctxt.secureTimerParams.secureTimer3)
1726                    ? (nxpese_ctxt.secureTimerParams.secureTimer3)
1727                    : *pMaxTimer;
1728 
1729   /* Converting timer to millisecond from sec */
1730   *pMaxTimer = SECOND_TO_MILLISECOND(*pMaxTimer);
1731   /* Add extra 5% to the timer */
1732   *pMaxTimer +=
1733       CONVERT_TO_PERCENTAGE(*pMaxTimer, ADDITIONAL_SECURE_TIME_PERCENTAGE);
1734   NXP_LOG_ESE_D("%s Max timer value = %lu", __FUNCTION__, *pMaxTimer);
1735   return;
1736 }
1737 
1738 /******************************************************************************
1739  * Function         phNxpEseP61_DisablePwrCntrl
1740  *
1741  * Description      This function disables eSE GPIO power off/on control
1742  *                  when enabled
1743  *
1744  * Returns         SUCCESS/FAIL.
1745  *
1746  ******************************************************************************/
phNxpEse_DisablePwrCntrl(void)1747 ESESTATUS phNxpEse_DisablePwrCntrl(void) {
1748   ESESTATUS status = ESESTATUS_SUCCESS;
1749   unsigned long maxTimer = 0;
1750   NXP_LOG_ESE_E("%s Enter", __FUNCTION__);
1751   phNxpEse_GetMaxTimer(&maxTimer);
1752   if (GET_CHIP_OS_VERSION() == OS_VERSION_4_0) {
1753     status = phNxpEse_SPM_DisablePwrControl(maxTimer);
1754     if (status != ESESTATUS_SUCCESS) {
1755       NXP_LOG_ESE_E("%s phNxpEseP61_DisablePwrCntrl: failed", __FUNCTION__);
1756     }
1757   } else {
1758     NXP_LOG_ESE_E("%s phNxpEseP61_DisablePwrCntrl: not supported",
1759                   __FUNCTION__);
1760     status = ESESTATUS_FAILED;
1761   }
1762   return status;
1763 }
1764 
1765 /******************************************************************************
1766  * Function         phNxpEse_getOsVersion
1767  *
1768  * Description      This function returns OS version from config file &
1769  *                  runtime from ATR response
1770  *
1771  * Returns         SUCCESS/FAIL.
1772  *
1773  ******************************************************************************/
phNxpEse_getOsVersion()1774 phNxpEse_OsVersion_t phNxpEse_getOsVersion() { return sOsVersion; }
1775 
1776 /******************************************************************************
1777  * Function         phNxpEse_setOsVersion
1778  *
1779  * Description      This function sets chip type based on ATR response
1780  *
1781  * Returns         None.
1782  *
1783  ******************************************************************************/
phNxpEse_setOsVersion(phNxpEse_OsVersion_t chipType)1784 void phNxpEse_setOsVersion(phNxpEse_OsVersion_t chipType) {
1785   sOsVersion = chipType;
1786 }
1787 
1788 /******************************************************************************
1789  * Function         phNxpEse_checkFWDwnldStatus
1790  *
1791  * Description      This function is  used to  check whether FW download
1792  *                  is completed or not.
1793  *
1794  * Returns          returns  ESESTATUS_SUCCESS or ESESTATUS_BUSY
1795  *
1796  ******************************************************************************/
phNxpEse_checkFWDwnldStatus(void)1797 static ESESTATUS phNxpEse_checkFWDwnldStatus(void) {
1798   NXP_LOG_ESE_D("phNxpEse_checkFWDwnldStatus Enter");
1799   ESESTATUS wSpmStatus = ESESTATUS_SUCCESS;
1800   spm_state_t current_spm_state = SPM_STATE_INVALID;
1801   uint8_t ese_dwnld_retry = 0x00;
1802   ESESTATUS status = ESESTATUS_FAILED;
1803 
1804   wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
1805   if (wSpmStatus == ESESTATUS_SUCCESS) {
1806     /* Check current_spm_state and update config/Spm status*/
1807     while (ese_dwnld_retry < ESE_FW_DWNLD_RETRY_CNT) {
1808       NXP_LOG_ESE_D("ESE_FW_DWNLD_RETRY_CNT retry count");
1809       wSpmStatus = phNxpEse_SPM_GetState(&current_spm_state);
1810       if (wSpmStatus == ESESTATUS_SUCCESS) {
1811         if ((current_spm_state & SPM_STATE_DWNLD)) {
1812           status = ESESTATUS_FAILED;
1813         } else {
1814           NXP_LOG_ESE_E("Exit polling no FW Download ..");
1815           status = ESESTATUS_SUCCESS;
1816           break;
1817         }
1818       } else {
1819         status = ESESTATUS_FAILED;
1820         break;
1821       }
1822       phNxpEse_Sleep(500000); /*sleep for 500 ms checking for fw dwnld status*/
1823       ese_dwnld_retry++;
1824     }
1825   }
1826 
1827   NXP_LOG_ESE_D("phNxpEse_checkFWDwnldStatus status %x", status);
1828   return status;
1829 }
1830 
1831 /******************************************************************************
1832  * Function         phNxpEse_GetEseStatus(unsigned char *timer_buffer)
1833  *
1834  * Description      This function returns the all three timer
1835  * Timeout buffer length should be minimum 18 bytes. Response will be in below
1836  format:
1837  * <0xF1><Len><Timer Value><0xF2><Len><Timer Value><0xF3><Len><Timer Value>
1838  *
1839  * Returns         SUCCESS/FAIL.
1840  * ESESTATUS_SUCCESS if 0xF1 or 0xF2 tag timeout >= 0 & 0xF3 == 0
1841  * ESESTATUS_BUSY if 0xF3 tag timeout > 0
1842  * ESESTATUS_FAILED if any other error
1843 
1844  ******************************************************************************/
phNxpEse_GetEseStatus(phNxpEse_data * timer_buffer)1845 ESESTATUS phNxpEse_GetEseStatus(phNxpEse_data* timer_buffer) {
1846   ESESTATUS status = ESESTATUS_FAILED;
1847 
1848   phNxpEse_SecureTimer_t secureTimerParams;
1849   uint8_t* temp_timer_buffer = NULL;
1850   NXP_LOG_ESE_D("%s Enter", __FUNCTION__);
1851   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1852     NXP_LOG_ESE_E("%s function not supported", __FUNCTION__);
1853     return status;
1854   }
1855   if (timer_buffer != NULL) {
1856     timer_buffer->len =
1857         (sizeof(secureTimerParams.secureTimer1) +
1858          sizeof(secureTimerParams.secureTimer2) +
1859          sizeof(secureTimerParams.secureTimer3)) +
1860         PH_PROPTO_7816_FRAME_LENGTH_OFFSET * PH_PROPTO_7816_FRAME_LENGTH_OFFSET;
1861     temp_timer_buffer = (uint8_t*)phNxpEse_memalloc(timer_buffer->len);
1862     timer_buffer->p_data = temp_timer_buffer;
1863 
1864     phNxpEse_memcpy(&secureTimerParams, &nxpese_ctxt.secureTimerParams,
1865                     sizeof(phNxpEse_SecureTimer_t));
1866 
1867     NXP_LOG_ESE_D(
1868         "%s secureTimer1 0x%x secureTimer2 0x%x secureTimer3 0x%x len = %d",
1869         __FUNCTION__, secureTimerParams.secureTimer1,
1870         secureTimerParams.secureTimer2, secureTimerParams.secureTimer3,
1871         timer_buffer->len);
1872 
1873     *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER1;
1874     *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer1);
1875     temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1876         temp_timer_buffer, secureTimerParams.secureTimer1);
1877     if (temp_timer_buffer != NULL) {
1878       *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER2;
1879       *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer2);
1880       temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1881           temp_timer_buffer, secureTimerParams.secureTimer2);
1882       if (temp_timer_buffer != NULL) {
1883         *temp_timer_buffer++ = PH_PROPTO_7816_SFRAME_TIMER3;
1884         *temp_timer_buffer++ = sizeof(secureTimerParams.secureTimer3);
1885         temp_timer_buffer = phNxpEse_GgetTimerTlvBuffer(
1886             temp_timer_buffer, secureTimerParams.secureTimer3);
1887         if (temp_timer_buffer != NULL) {
1888           if (secureTimerParams.secureTimer3 > 0) {
1889             status = ESESTATUS_BUSY;
1890           } else {
1891             status = ESESTATUS_SUCCESS;
1892           }
1893         }
1894       }
1895     }
1896   } else {
1897     NXP_LOG_ESE_E("%s Invalid timer buffer ", __FUNCTION__);
1898   }
1899 
1900   NXP_LOG_ESE_D("%s Exit status = 0x%x", __FUNCTION__, status);
1901   return status;
1902 }
1903 
phNxpEse_GgetTimerTlvBuffer(uint8_t * timer_buffer,unsigned int value)1904 static unsigned char* phNxpEse_GgetTimerTlvBuffer(uint8_t* timer_buffer,
1905                                                   unsigned int value) {
1906   short int count = 0, shift = 3;
1907   unsigned int mask = 0x000000FF;
1908   if (GET_CHIP_OS_VERSION() != OS_VERSION_4_0) {
1909     NXP_LOG_ESE_E("%s function not supported", __FUNCTION__);
1910   } else {
1911     NXP_LOG_ESE_D("value = %x \n", value);
1912     for (count = 0; count < 4; count++) {
1913       if (timer_buffer != NULL) {
1914         *timer_buffer = (value >> (shift * 8) & mask);
1915         NXP_LOG_ESE_D("*timer_buffer=0x%x shift=0x%x", *timer_buffer, shift);
1916         timer_buffer++;
1917         shift--;
1918       } else {
1919         break;
1920       }
1921     }
1922   }
1923   return timer_buffer;
1924 }
1925 
1926 /******************************************************************************
1927  * Function         phNxpEse_NotifySEWtxRequest
1928  *
1929  * Description      This function notifies SE hal service if it registers
1930  *                  about WTX ongoing & end status
1931  *
1932  * Returns          None
1933  *
1934  ******************************************************************************/
phNxpEse_NotifySEWtxRequest(phNxpEse_wtxState state)1935 void phNxpEse_NotifySEWtxRequest(phNxpEse_wtxState state) {
1936   if (nxpese_ctxt.fPtr_WtxNtf) {
1937     (nxpese_ctxt.fPtr_WtxNtf)(state);
1938   } else {
1939     NXP_LOG_ESE_E("%s function not supported", __FUNCTION__);
1940   }
1941 }
1942 
1943 /******************************************************************************
1944  * Function         phNxpEse_setWtxCountLimit
1945  *
1946  * Description      This function sets the counter limit for wtx
1947  *
1948  * Returns          None
1949  *
1950  ******************************************************************************/
phNxpEse_setWtxCountLimit(unsigned long int wtxCount)1951 void phNxpEse_setWtxCountLimit(unsigned long int wtxCount) {
1952   app_wtx_cnt = wtxCount;
1953 }
1954 
1955 /******************************************************************************
1956  * Function         phNxpEse_isPriorityAccessEnabled
1957  *
1958  * Description      This function returns whether priority channel enabled or
1959  *                  not.
1960  *
1961  * Returns          Priority Access enabled(1)/disabled(0).
1962  *
1963  ******************************************************************************/
phNxpEse_isPriorityAccessEnabled(void)1964 bool phNxpEse_isPriorityAccessEnabled(void) {
1965   uint8_t isPriorityAccess = 0;
1966   if (EseConfig::hasKey(NAME_NXP_SE_PRIORITY_ACCESS)) {
1967     isPriorityAccess = EseConfig::getUnsigned(NAME_NXP_SE_PRIORITY_ACCESS);
1968   }
1969   NXP_LOG_ESE_D("Reserve channel enabled = %d", isPriorityAccess);
1970   return (isPriorityAccess != 0);
1971 }
1972