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