• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2017 ST Microelectronics S.A.
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  ******************************************************************************/
19 #define LOG_TAG "NfcNciHalWrapper"
20 #include <cutils/properties.h>
21 #include <errno.h>
22 #include <hardware/nfc.h>
23 #include <log/log.h>
24 #include <string.h>
25 #include <unistd.h>
26 
27 #include "android_logmsg.h"
28 #include "hal_fd.h"
29 #include "halcore.h"
30 #include "st21nfc_dev.h"
31 
32 extern void HalCoreCallback(void* context, uint32_t event, const void* d,
33                             size_t length);
34 extern bool I2cOpenLayer(void* dev, HAL_CALLBACK callb, HALHANDLE* pHandle);
35 extern void I2cCloseLayer();
36 extern void I2cRecovery();
37 
38 static void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data);
39 static void halWrapperCallback(uint8_t event, uint8_t event_status);
40 
41 nfc_stack_callback_t* mHalWrapperCallback = NULL;
42 nfc_stack_data_callback_t* mHalWrapperDataCallback = NULL;
43 hal_wrapper_state_e mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
44 HALHANDLE mHalHandle = NULL;
45 
46 uint8_t mClfMode;
47 uint8_t mFwUpdateTaskMask;
48 int mRetryFwDwl;
49 uint8_t mFwUpdateResMask = 0;
50 uint8_t* ConfigBuffer = NULL;
51 uint8_t mError_count = 0;
52 bool mIsActiveRW = false;
53 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54 pthread_mutex_t mutex_activerw = PTHREAD_MUTEX_INITIALIZER;
55 pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
56 
57 static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80,
58                                      0x8A, 0x00, 0x00, 0x04};
59 
60 static const uint8_t nciHeaderPropSetConfig[9] = {0x2F, 0x02, 0x98, 0x04, 0x00,
61                                                   0x14, 0x01, 0x00, 0x92};
62 static uint8_t nciPropEnableFwDbgTraces[256];
63 static uint8_t nciPropGetFwDbgTracesConfig[] = {0x2F, 0x02, 0x05, 0x03,
64                                                 0x00, 0x14, 0x01, 0x00};
65 static bool isDebuggable;
66 
67 bool mReadFwConfigDone = false;
68 
69 bool mHciCreditLent = false;
70 bool mfactoryReset = false;
71 bool ready_flag = 0;
72 bool mTimerStarted = false;
73 bool forceRecover = false;
74 
wait_ready()75 void wait_ready() {
76   pthread_mutex_lock(&mutex);
77   while (!ready_flag) {
78     pthread_cond_wait(&ready_cond, &mutex);
79   }
80   pthread_mutex_unlock(&mutex);
81 }
82 
set_ready(bool ready)83 void set_ready(bool ready) {
84   pthread_mutex_lock(&mutex);
85   ready_flag = ready;
86   pthread_cond_signal(&ready_cond);
87   pthread_mutex_unlock(&mutex);
88 }
89 
hal_wrapper_open(st21nfc_dev_t * dev,nfc_stack_callback_t * p_cback,nfc_stack_data_callback_t * p_data_cback,HALHANDLE * pHandle)90 bool hal_wrapper_open(st21nfc_dev_t* dev, nfc_stack_callback_t* p_cback,
91                       nfc_stack_data_callback_t* p_data_cback,
92                       HALHANDLE* pHandle) {
93   bool result;
94 
95   STLOG_HAL_D("%s", __func__);
96 
97   mFwUpdateResMask = hal_fd_init();
98   mRetryFwDwl = 5;
99   mFwUpdateTaskMask = 0;
100 
101   mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
102   mHciCreditLent = false;
103   mReadFwConfigDone = false;
104   mError_count = 0;
105 
106   mHalWrapperCallback = p_cback;
107   mHalWrapperDataCallback = p_data_cback;
108 
109   dev->p_data_cback = halWrapperDataCallback;
110   dev->p_cback = halWrapperCallback;
111 
112   result = I2cOpenLayer(dev, HalCoreCallback, pHandle);
113 
114   if (!result || !(*pHandle)) {
115     return -1;  // We are doomed, stop it here, NOW !
116   }
117 
118   isDebuggable = property_get_int32("ro.debuggable", 0);
119   mHalHandle = *pHandle;
120 
121   HalSendDownstreamTimer(mHalHandle, 10000);
122 
123   return 1;
124 }
125 
hal_wrapper_close(int call_cb,int nfc_mode)126 int hal_wrapper_close(int call_cb, int nfc_mode) {
127   STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD(%d)", __func__, nfc_mode);
128   uint8_t propNfcModeSetCmdQb[] = {0x2f, 0x02, 0x02, 0x02, (uint8_t)nfc_mode};
129 
130   mHalWrapperState = HAL_WRAPPER_STATE_CLOSING;
131   // Send PROP_NFC_MODE_SET_CMD
132   if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdQb,
133                               sizeof(propNfcModeSetCmdQb), 40)) {
134     STLOG_HAL_E("NFC-NCI HAL: %s  HalSendDownstreamTimer failed", __func__);
135     return -1;
136   }
137   // Let the CLF receive and process this
138   usleep(50000);
139 
140   I2cCloseLayer();
141   if (call_cb) mHalWrapperCallback(HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
142 
143   return 1;
144 }
145 
hal_wrapper_send_core_config_prop()146 void hal_wrapper_send_core_config_prop() {
147   long retlen = 0;
148   int isfound = 0;
149 
150   // allocate buffer for setting parameters
151   ConfigBuffer = (uint8_t*)malloc(256 * sizeof(uint8_t));
152   if (ConfigBuffer != NULL) {
153     isfound = GetByteArrayValue(NAME_CORE_CONF_PROP, (char*)ConfigBuffer, 256,
154                                 &retlen);
155 
156     if (isfound > 0) {
157       STLOG_HAL_V("%s - Enter", __func__);
158       set_ready(0);
159 
160       if (!HalSendDownstreamTimer(mHalHandle, ConfigBuffer, retlen, 500)) {
161         STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
162       }
163       mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
164       wait_ready();
165     }
166     free(ConfigBuffer);
167     ConfigBuffer = NULL;
168   }
169 }
170 
hal_wrapper_send_vs_config()171 void hal_wrapper_send_vs_config() {
172   STLOG_HAL_V("%s - Enter", __func__);
173   set_ready(0);
174 
175   if (!HalSendDownstreamTimer(mHalHandle, nciPropGetFwDbgTracesConfig,
176                               sizeof(nciPropGetFwDbgTracesConfig), 500)) {
177     STLOG_HAL_E("%s - SendDownstream failed", __func__);
178   }
179   mReadFwConfigDone = true;
180   wait_ready();
181 }
182 
hal_wrapper_send_config()183 void hal_wrapper_send_config() {
184   hal_wrapper_send_core_config_prop();
185   mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
186   hal_wrapper_send_vs_config();
187 }
188 
hal_wrapper_factoryReset()189 void hal_wrapper_factoryReset() {
190   mfactoryReset = true;
191   STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset);
192 }
193 
hal_wrapper_update_complete()194 void hal_wrapper_update_complete() {
195   STLOG_HAL_V("%s ", __func__);
196   mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_OK);
197   mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT;
198 }
halWrapperDataCallback(uint16_t data_len,uint8_t * p_data)199 void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data) {
200   uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
201   uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
202   uint8_t coreResetCmd[] = {0x20, 0x00, 0x01, 0x01};
203   unsigned long num = 0;
204   unsigned long swp_log = 0;
205   unsigned long rf_log = 0;
206   int nciPropEnableFwDbgTraces_size = sizeof(nciPropEnableFwDbgTraces);
207 
208   switch (mHalWrapperState) {
209     case HAL_WRAPPER_STATE_CLOSED:  // 0
210       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSED", __func__);
211       break;
212     case HAL_WRAPPER_STATE_OPEN:  // 1
213       // CORE_RESET_NTF
214       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN", __func__);
215 
216       if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
217         mIsActiveRW = false;
218         mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode);
219 
220         if (mfactoryReset == true) {
221           STLOG_HAL_V(
222               "%s - first boot after factory reset detected - start FW update",
223               __func__);
224           if ((mFwUpdateResMask & FW_PATCH_AVAILABLE) &&
225               (mFwUpdateResMask & FW_CUSTOM_PARAM_AVAILABLE)) {
226             mFwUpdateTaskMask = FW_UPDATE_NEEDED | CONF_UPDATE_NEEDED;
227             mfactoryReset = false;
228           }
229         }
230         STLOG_HAL_V(
231             "%s - mFwUpdateTaskMask = %d,  mClfMode = %d,  mRetryFwDwl = %d",
232             __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl);
233         // CLF in MODE LOADER & Update needed.
234         if (mClfMode == FT_CLF_MODE_LOADER) {
235           HalSendDownstreamStopTimer(mHalHandle);
236           STLOG_HAL_V("%s --- CLF mode is LOADER ---", __func__);
237 
238           if (mRetryFwDwl == 0) {
239             STLOG_HAL_V(
240                 "%s - Reached maximum nb of retries, FW update failed, exiting",
241                 __func__);
242             mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
243             I2cCloseLayer();
244           } else {
245             STLOG_HAL_V("%s - Send APDU_GET_ATR_CMD", __func__);
246             mRetryFwDwl--;
247             if (!HalSendDownstreamTimer(mHalHandle, ApduGetAtr,
248                                         sizeof(ApduGetAtr),
249                                         FW_TIMER_DURATION)) {
250               STLOG_HAL_E("%s - SendDownstream failed", __func__);
251             }
252             mHalWrapperState = HAL_WRAPPER_STATE_UPDATE;
253           }
254         } else if (mFwUpdateTaskMask == 0 || mRetryFwDwl == 0) {
255           STLOG_HAL_V("%s - Proceeding with normal startup", __func__);
256           if (p_data[3] == 0x01) {
257             // Normal mode, start HAL
258             mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_OK);
259             mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT;
260           } else {
261             // No more retries or CLF not in correct mode
262             mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
263           }
264           // CLF in MODE ROUTER & Update needed.
265         } else if (mClfMode == FT_CLF_MODE_ROUTER) {
266           if ((mFwUpdateTaskMask & FW_UPDATE_NEEDED) &&
267               (mFwUpdateResMask & FW_PATCH_AVAILABLE)) {
268             STLOG_HAL_V(
269                 "%s - CLF in ROUTER mode, FW update needed, try upgrade FW -",
270                 __func__);
271             mRetryFwDwl--;
272 
273             if (!HalSendDownstream(mHalHandle, coreResetCmd,
274                                    sizeof(coreResetCmd))) {
275               STLOG_HAL_E("%s - SendDownstream failed", __func__);
276             }
277             mHalWrapperState = HAL_WRAPPER_STATE_EXIT_HIBERNATE_INTERNAL;
278           } else if ((mFwUpdateTaskMask & CONF_UPDATE_NEEDED) &&
279                      (mFwUpdateResMask & FW_CUSTOM_PARAM_AVAILABLE)) {
280             if (!HalSendDownstream(mHalHandle, coreResetCmd,
281                                    sizeof(coreResetCmd))) {
282               STLOG_HAL_E("%s - SendDownstream failed", __func__);
283             }
284             mHalWrapperState = HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM;
285           } else if ((mFwUpdateTaskMask & UWB_CONF_UPDATE_NEEDED) &&
286                      (mFwUpdateResMask & FW_UWB_PARAM_AVAILABLE)) {
287             if (!HalSendDownstream(mHalHandle, coreResetCmd,
288                                    sizeof(coreResetCmd))) {
289               STLOG_HAL_E("%s - SendDownstream failed", __func__);
290             }
291             mHalWrapperState = HAL_WRAPPER_STATE_APPLY_UWB_PARAM;
292           }
293         }
294       } else {
295         mHalWrapperDataCallback(data_len, p_data);
296       }
297       break;
298     case HAL_WRAPPER_STATE_OPEN_CPLT:  // 2
299       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT",
300                   __func__);
301       // CORE_INIT_RSP
302       if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
303       } else if ((p_data[0] == 0x60) && (p_data[1] == 0x06)) {
304         STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__);
305         // Send PROP_NFC_MODE_SET_CMD(ON)
306         if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn,
307                                     sizeof(propNfcModeSetCmdOn), 100)) {
308           STLOG_HAL_E("NFC-NCI HAL: %s  HalSendDownstreamTimer failed",
309                       __func__);
310         }
311         mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON;
312       } else {
313         mHalWrapperDataCallback(data_len, p_data);
314       }
315       break;
316 
317     case HAL_WRAPPER_STATE_NFC_ENABLE_ON:  // 3
318       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON",
319                   __func__);
320       // PROP_NFC_MODE_SET_RSP
321       if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
322         // DO nothing: wait for core_reset_ntf or timer timeout
323       }
324       // CORE_RESET_NTF
325       else if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
326         // Stop timer
327         HalSendDownstreamStopTimer(mHalHandle);
328         if (forceRecover == true) {
329           forceRecover = false;
330           mHalWrapperDataCallback(data_len, p_data);
331           break;
332         }
333 
334         // Send CORE_INIT_CMD
335         STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
336         if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
337           STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
338         }
339       }
340       // CORE_INIT_RSP
341       else if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
342         STLOG_HAL_D("%s - NFC mode enabled", __func__);
343         // Do we need to lend a credit ?
344         if (p_data[13] == 0x00) {
345           STLOG_HAL_D("%s - 1 credit lent", __func__);
346           p_data[13] = 0x01;
347           mHciCreditLent = true;
348         }
349 
350         mHalWrapperState = HAL_WRAPPER_STATE_READY;
351         mHalWrapperDataCallback(data_len, p_data);
352       }
353       break;
354 
355     case HAL_WRAPPER_STATE_PROP_CONFIG:  // 4
356       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG",
357                   __func__);
358       // CORE_SET_CONFIG_RSP
359       if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) {
360         HalSendDownstreamStopTimer(mHalHandle);
361         set_ready(1);
362 
363         STLOG_HAL_V("%s - Received config RSP, read FW dDBG config", __func__);
364       } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
365         // CORE_CONN_CREDITS_NTF
366         if (p_data[4] == 0x01) {  // HCI connection
367           mHciCreditLent = false;
368           STLOG_HAL_D("%s - credit returned", __func__);
369           if (p_data[5] == 0x01) {
370             // no need to send this.
371             break;
372           } else {
373             if (p_data[5] != 0x00 && p_data[5] != 0xFF) {
374               // send with 1 less
375               p_data[5]--;
376             }
377           }
378         }
379         mHalWrapperDataCallback(data_len, p_data);
380       } else if (p_data[0] == 0x4f) {
381         // PROP_RSP
382         if (mReadFwConfigDone == true) {
383           mReadFwConfigDone = false;
384           HalSendDownstreamStopTimer(mHalHandle);
385           set_ready(1);
386           // NFC_STATUS_OK
387           if (p_data[3] == 0x00) {
388             bool confNeeded = false;
389             bool firmware_debug_enabled =
390                 property_get_int32("persist.vendor.nfc.firmware_debug_enabled", 0);
391 
392             // Check if FW DBG shall be set
393             if (GetNumValue(NAME_STNFC_FW_DEBUG_ENABLED, &num, sizeof(num)) ||
394                 isDebuggable) {
395               if (firmware_debug_enabled) num = 1;
396 
397               if (num == 1) {
398                 GetNumValue(NAME_STNFC_FW_SWP_LOG_SIZE, &swp_log,
399                             sizeof(swp_log));
400                 GetNumValue(NAME_STNFC_FW_RF_LOG_SIZE, &rf_log, sizeof(rf_log));
401               }
402               // limit swp and rf payload length between 4 and 30.
403               if (swp_log > 30)
404                 swp_log = 30;
405               else if (swp_log < 4)
406                 swp_log = 4;
407 
408               if (rf_log > 30)
409                 rf_log = 30;
410               else if (rf_log < 4)
411                 rf_log = 4;
412 
413               if ((rf_log || swp_log) &&
414                   ((p_data[15] != rf_log) || (p_data[17] != swp_log))) {
415                 STLOG_HAL_D("%s - FW DBG payload traces changes needed",
416                             __func__);
417                 confNeeded = true;
418               }
419 
420               // If conf file indicate set needed and not yet enabled
421               if ((num == 1) && (p_data[7] == 0x00)) {
422                 STLOG_HAL_D("%s - FW DBG traces enabling needed", __func__);
423                 nciPropEnableFwDbgTraces[9] = 0x01;
424                 confNeeded = true;
425               } else if ((num == 0) && (p_data[7] == 0x01)) {
426                 STLOG_HAL_D("%s - FW DBG traces disabling needed", __func__);
427                 nciPropEnableFwDbgTraces[9] = 0x00;
428                 confNeeded = true;
429               } else {
430                 STLOG_HAL_D(
431                     "%s - No FW DBG traces enable/disable change needed",
432                     __func__);
433               }
434 
435               if (data_len < 9 || p_data[6] == 0 || p_data[6] < (data_len - 7)
436                   || p_data[6] > (sizeof(nciPropEnableFwDbgTraces) - 9)) {
437                 if (confNeeded) {
438                   android_errorWriteLog(0x534e4554, "169328517");
439                   confNeeded = false;
440                 }
441               }
442 
443               if (confNeeded) {
444                 memcpy(nciPropEnableFwDbgTraces, nciHeaderPropSetConfig, 9);
445                 memcpy(&nciPropEnableFwDbgTraces[10], &p_data[8],
446                        p_data[6] - 1);
447                 if (rf_log || swp_log) {
448                   nciPropEnableFwDbgTraces[9] = (uint8_t)num;
449                   nciPropEnableFwDbgTraces[17] = (uint8_t)rf_log;
450                   nciPropEnableFwDbgTraces[19] = (uint8_t)swp_log;
451                 }
452                 if ((9 + p_data[6]) < sizeof(nciPropEnableFwDbgTraces)) {
453                   nciPropEnableFwDbgTraces_size = 9 + p_data[6];
454                 }
455 
456                 confNeeded = false;
457 
458                 if (!HalSendDownstream(mHalHandle, nciPropEnableFwDbgTraces,
459                                        nciPropEnableFwDbgTraces_size)) {
460                   STLOG_HAL_E("%s - SendDownstream failed", __func__);
461                 }
462 
463                 break;
464               }
465             }
466           }
467         }
468 
469         // Exit state, all processing done
470         mHalWrapperCallback(HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_OK);
471         mHalWrapperState = HAL_WRAPPER_STATE_READY;
472       }
473       break;
474 
475     case HAL_WRAPPER_STATE_READY:  // 5
476       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_READY", __func__);
477       if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) {
478         if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
479           if (p_data[4] == 0x01) {  // HCI connection
480             mHciCreditLent = false;
481             STLOG_HAL_D("%s - credit returned", __func__);
482             if (p_data[5] == 0x01) {
483               // no need to send this.
484               break;
485             } else {
486               if (p_data[5] != 0x00 && p_data[5] != 0xFF) {
487                 // send with 1 less
488                 p_data[5]--;
489               }
490             }
491           }
492         } else if ((p_data[0] == 0x6f) && (p_data[1] == 0x05)) {
493           (void)pthread_mutex_lock(&mutex_activerw);
494           // start timer
495           mTimerStarted = true;
496           HalSendDownstreamTimer(mHalHandle, 5000);
497           mIsActiveRW = true;
498           (void)pthread_mutex_unlock(&mutex_activerw);
499         } else if ((p_data[0] == 0x6f) && (p_data[1] == 0x06)) {
500           (void)pthread_mutex_lock(&mutex_activerw);
501           // stop timer
502           if (mTimerStarted) {
503             HalSendDownstreamStopTimer(mHalHandle);
504             mTimerStarted = false;
505           }
506           if(mIsActiveRW == true) {
507             mIsActiveRW = false;
508           } else {
509             mError_count ++;
510             STLOG_HAL_E("Error Act -> Act count=%d", mError_count);
511             if(mError_count > 20) {
512               mError_count = 0;
513               STLOG_HAL_E("NFC Recovery Start");
514               mTimerStarted = true;
515               HalSendDownstreamTimer(mHalHandle, 1);
516             }
517           }
518           (void)pthread_mutex_unlock(&mutex_activerw);
519         } else if (((p_data[0] == 0x61) && (p_data[1] == 0x05)) ||
520                    ((p_data[0] == 0x61) && (p_data[1] == 0x03))) {
521           mError_count = 0;
522           // stop timer
523           if (mTimerStarted) {
524             HalSendDownstreamStopTimer(mHalHandle);
525             mTimerStarted = false;
526           }
527         } else if (data_len >= 4 && p_data[0] == 0x60 && p_data[1] == 0x07) {
528           if (p_data[3] == 0xE1) {
529             // Core Generic Error - Buffer Overflow Ntf - Restart all
530             STLOG_HAL_E("Core Generic Error - restart");
531             p_data[0] = 0x60;
532             p_data[1] = 0x00;
533             p_data[2] = 0x03;
534             p_data[3] = 0xE1;
535             p_data[4] = 0x00;
536             p_data[5] = 0x00;
537             data_len = 0x6;
538           } else if (p_data[3] == 0xE6) {
539             unsigned long hal_ctrl_clk = 0;
540             GetNumValue(NAME_STNFC_CONTROL_CLK, &hal_ctrl_clk,
541                         sizeof(hal_ctrl_clk));
542             if (hal_ctrl_clk) {
543               STLOG_HAL_E("%s - Clock Error - restart", __func__);
544               // Core Generic Error
545               p_data[0] = 0x60;
546               p_data[1] = 0x00;
547               p_data[2] = 0x03;
548               p_data[3] = 0xE6;
549               p_data[4] = 0x00;
550               p_data[5] = 0x00;
551               data_len = 0x6;
552             }
553           }
554         }
555         mHalWrapperDataCallback(data_len, p_data);
556       } else {
557         STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__);
558       }
559       break;
560 
561     case HAL_WRAPPER_STATE_CLOSING:  // 6
562       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSING",
563                   __func__);
564       hal_fd_close();
565       if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
566         // intercept this expected message, don t forward.
567         mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
568       } else {
569         mHalWrapperDataCallback(data_len, p_data);
570       }
571       break;
572 
573     case HAL_WRAPPER_STATE_EXIT_HIBERNATE_INTERNAL:  // 6
574       STLOG_HAL_V(
575           "%s - mHalWrapperState = HAL_WRAPPER_STATE_EXIT_HIBERNATE_INTERNAL",
576           __func__);
577       ExitHibernateHandler(mHalHandle, data_len, p_data);
578       break;
579 
580     case HAL_WRAPPER_STATE_UPDATE:  // 7
581       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_UPDATE", __func__);
582       UpdateHandler(mHalHandle, data_len, p_data);
583       break;
584     case HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM:  // 8
585       STLOG_HAL_V(
586           "%s - mHalWrapperState = HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM",
587           __func__);
588       ApplyCustomParamHandler(mHalHandle, data_len, p_data);
589       break;
590     case HAL_WRAPPER_STATE_APPLY_UWB_PARAM:  // 9
591       STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_APPLY_UWB_PARAM",
592                   __func__);
593       ApplyUwbParamHandler(mHalHandle, data_len, p_data);
594       break;
595     case HAL_WRAPPER_STATE_SET_ACTIVERW_TIMER:  // 10
596       (void)pthread_mutex_lock(&mutex_activerw);
597       if (mIsActiveRW == true) {
598         STLOG_HAL_D(
599             "%s - mHalWrapperState = "
600             "HAL_WRAPPER_STATE_SET_ACTIVERW_TIMER",
601             __func__);
602         // start timer
603         mTimerStarted = true;
604         HalSendDownstreamTimer(mHalHandle, 5000);
605         // Chip state should back to Active
606         // at screen off state.
607       }
608       (void)pthread_mutex_unlock(&mutex_activerw);
609       mHalWrapperState = HAL_WRAPPER_STATE_READY;
610       mHalWrapperDataCallback(data_len, p_data);
611       break;
612   }
613 }
614 
halWrapperCallback(uint8_t event,uint8_t event_status)615 static void halWrapperCallback(uint8_t event, __attribute__((unused))uint8_t event_status) {
616   uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
617 
618   switch (mHalWrapperState) {
619     case HAL_WRAPPER_STATE_CLOSING:
620       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
621         STLOG_HAL_D("NFC-NCI HAL: %s  Timeout. Close anyway", __func__);
622         HalSendDownstreamStopTimer(mHalHandle);
623         hal_fd_close();
624         mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
625         return;
626       }
627       break;
628 
629     case HAL_WRAPPER_STATE_OPEN:
630       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
631         STLOG_HAL_D("NFC-NCI HAL: %s  Timeout accessing the CLF.", __func__);
632         HalSendDownstreamStopTimer(mHalHandle);
633         I2cRecovery();
634         return;
635       }
636       break;
637 
638     case HAL_WRAPPER_STATE_CLOSED:
639       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
640         STLOG_HAL_D("NFC-NCI HAL: %s  Timeout. Close anyway", __func__);
641         HalSendDownstreamStopTimer(mHalHandle);
642         return;
643       }
644       break;
645 
646     case HAL_WRAPPER_STATE_UPDATE:
647       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
648         STLOG_HAL_E("%s - Timer for FW update procedure timeout, retry",
649                     __func__);
650         HalSendDownstreamStopTimer(mHalHandle);
651         resetHandlerState();
652         I2cResetPulse();
653         mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
654       }
655       break;
656 
657     case HAL_WRAPPER_STATE_NFC_ENABLE_ON:
658       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
659         // timeout
660         // Send CORE_INIT_CMD
661         STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
662         if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
663           STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
664         }
665         return;
666       }
667       break;
668     case HAL_WRAPPER_STATE_PROP_CONFIG:
669       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
670         STLOG_HAL_E("%s - Timer when sending conf parameters, retry", __func__);
671         HalSendDownstreamStopTimer(mHalHandle);
672         resetHandlerState();
673         I2cResetPulse();
674         mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
675       }
676       break;
677 
678     case HAL_WRAPPER_STATE_READY:
679       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
680         if (mTimerStarted) {
681           STLOG_HAL_E("NFC-NCI HAL: %s  Timeout.. Recover", __func__);
682           STLOG_HAL_E("%s mIsActiveRW = %d", __func__, mIsActiveRW);
683           HalSendDownstreamStopTimer(mHalHandle);
684           mTimerStarted = false;
685           forceRecover = true;
686           resetHandlerState();
687           I2cResetPulse();
688           mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
689 
690         }
691         return;
692       }
693       break;
694 
695     default:
696       break;
697   }
698 
699   mHalWrapperCallback(event, event_status);
700 }
701 
702 /*******************************************************************************
703  **
704  ** Function         nfc_set_state
705  **
706  ** Description      Set the state of NFC stack
707  **
708  ** Returns          void
709  **
710  *******************************************************************************/
hal_wrapper_set_state(hal_wrapper_state_e new_wrapper_state)711 void hal_wrapper_set_state(hal_wrapper_state_e new_wrapper_state) {
712   ALOGD("nfc_set_state %d->%d", mHalWrapperState, new_wrapper_state);
713 
714   mHalWrapperState = new_wrapper_state;
715 }
716