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