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