blob: d8cd9cf6b8d3c06202aa968e1d487204716daa84 [file] [log] [blame]
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +02001/******************************************************************************
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>
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +020024#include <unistd.h>
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020025#include "android_logmsg.h"
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010026#include "hal_fd.h"
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020027#include "halcore.h"
28
29extern void HalCoreCallback(void* context, uint32_t event, const void* d,
30 size_t length);
31extern bool I2cOpenLayer(void* dev, HAL_CALLBACK callb, HALHANDLE* pHandle);
32extern void I2cCloseLayer();
33
34typedef 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020042static void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data);
43static void halWrapperCallback(uint8_t event, uint8_t event_status);
44
45nfc_stack_callback_t* mHalWrapperCallback = NULL;
46nfc_stack_data_callback_t* mHalWrapperDataCallback = NULL;
47hal_wrapper_state_e mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
48HALHANDLE mHalHandle = NULL;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010049
50uint8_t mClfMode;
51uint8_t mFwUpdateTaskMask;
52int mRetryFwDwl;
53uint8_t mFwUpdateResMask = 0;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020054uint8_t* ConfigBuffer = NULL;
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +020055uint8_t mError_count = 0;
56bool mIsActiveRW = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020057pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
58pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
59
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010060static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80,
61 0x8A, 0x00, 0x00, 0x04};
62
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +010063static const uint8_t nciHeaderPropSetConfig[9] = {0x2F, 0x02, 0x98, 0x04, 0x00,
64 0x14, 0x01, 0x00, 0x92};
65static uint8_t nciPropEnableFwDbgTraces[256];
66static uint8_t nciPropGetFwDbgTracesConfig[] = {0x2F, 0x02, 0x05, 0x03,
67 0x00, 0x14, 0x01, 0x00};
68bool mReadFwConfigDone = false;
69
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +020070bool mHciCreditLent = false;
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +020071bool mfactoryReset = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020072bool ready_flag = 0;
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +020073bool mTimerStarted = false;
74bool forceRecover = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020075
76void 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
84void 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020091bool 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
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010098 mFwUpdateResMask = hal_fd_init();
99 mRetryFwDwl = 5;
100 mFwUpdateTaskMask = 0;
101
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200102 mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200103 mHciCreditLent = false;
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100104 mReadFwConfigDone = false;
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200105 mError_count = 0;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200106
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
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200124int 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};
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200127
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200128 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200138 I2cCloseLayer();
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200139 if (call_cb) mHalWrapperCallback(HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200140
141 return 1;
142}
143
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200144void hal_wrapper_send_core_config_prop() {
145 long retlen = 0;
146 int isfound = 0;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100147
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200148 // 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;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100166 }
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200167}
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100168
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200169void 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
181void hal_wrapper_send_config() {
182 hal_wrapper_send_core_config_prop();
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100183 mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200184 hal_wrapper_send_vs_config();
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100185}
186
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +0200187void hal_wrapper_factoryReset() {
188 mfactoryReset = true;
189 STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset);
190}
Arach MOHAMMED BRAHIM0b1b8ab2019-05-29 18:10:20 +0200191
192void 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}
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200197void 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};
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100200 uint8_t coreResetCmd[] = {0x20, 0x00, 0x01, 0x01};
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100201 unsigned long num = 0;
Arach MOHAMMED BRAHIM42be7022020-04-15 11:58:56 +0200202 int nciPropEnableFwDbgTraces_size = sizeof(nciPropEnableFwDbgTraces);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200203
204 switch (mHalWrapperState) {
205 case HAL_WRAPPER_STATE_CLOSED: // 0
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100206 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSED", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200207 break;
208 case HAL_WRAPPER_STATE_OPEN: // 1
209 // CORE_RESET_NTF
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100210 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN", __func__);
211
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200212 if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100213 mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode);
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +0200214
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 }
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100225 STLOG_HAL_V(
226 "%s - mFwUpdateTaskMask = %d, mClfMode = %d, mRetryFwDwl = %d",
227 __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl);
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100228 // 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 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200281 }
282 } else {
283 mHalWrapperDataCallback(data_len, p_data);
284 }
285 break;
286 case HAL_WRAPPER_STATE_OPEN_CPLT: // 2
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100287 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT",
288 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200289 // CORE_INIT_RSP
290 if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
291 } else if ((p_data[0] == 0x60) && (p_data[1] == 0x06)) {
Arach MOHAMMED BRAHIMed64c1b2018-07-25 16:45:04 +0200292 STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200293 // Send PROP_NFC_MODE_SET_CMD(ON)
294 if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn,
295 sizeof(propNfcModeSetCmdOn), 100)) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200296 STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed",
297 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200298 }
299 mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON;
300 } else {
301 mHalWrapperDataCallback(data_len, p_data);
302 }
303 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100304
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200305 case HAL_WRAPPER_STATE_NFC_ENABLE_ON: // 3
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100306 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON",
307 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200308 // 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
Arach MOHAMMED BRAHIMed64c1b2018-07-25 16:45:04 +0200318 STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200319 if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200320 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200321 }
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__);
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200326 // 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 }
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100332
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200333 mHalWrapperState = HAL_WRAPPER_STATE_READY;
334 mHalWrapperDataCallback(data_len, p_data);
335 }
336 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100337
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100338 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
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100342 if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) {
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200343 HalSendDownstreamStopTimer(mHalHandle);
344 set_ready(1);
345
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100346 STLOG_HAL_V("%s - Received config RSP, read FW dDBG config", __func__);
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100347 } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100348 // CORE_CONN_CREDITS_NTF
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100349 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);
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100363 } else if (p_data[0] == 0x4f) {
364 // PROP_RSP
365 if (mReadFwConfigDone == true) {
366 mReadFwConfigDone = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200367 HalSendDownstreamStopTimer(mHalHandle);
368 set_ready(1);
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100369 // 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);
Arach MOHAMMED BRAHIM42be7022020-04-15 11:58:56 +0200393 if ((9 + p_data[6]) < sizeof(nciPropEnableFwDbgTraces)) {
394 nciPropEnableFwDbgTraces_size = 9 + p_data[6];
395 }
396
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100397 confNeeded = false;
398
399 if (!HalSendDownstream(mHalHandle, nciPropEnableFwDbgTraces,
Arach MOHAMMED BRAHIM42be7022020-04-15 11:58:56 +0200400 nciPropEnableFwDbgTraces_size)) {
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100401 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;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100413 }
414 break;
415
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100416 case HAL_WRAPPER_STATE_READY: // 5
417 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_READY", __func__);
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200418 if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) {
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200419 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 }
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200433 } else if ((p_data[0] == 0x6f) && (p_data[1] == 0x05)) {
434 // start timer
435 mTimerStarted = true;
Jack Yub33f02f2020-06-23 17:08:11 +0800436 HalSendDownstreamTimer(mHalHandle, 5000);
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200437 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 }
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200464 }
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200465 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200466 } else if (forceRecover == true) {
467 forceRecover = false;
468 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200469 } else {
470 STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__);
471 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200472 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100473
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100474 case HAL_WRAPPER_STATE_CLOSING: // 6
475 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSING",
476 __func__);
Arach MOHAMMED BRAHIM0b1b8ab2019-05-29 18:10:20 +0200477 hal_fd_close();
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200478 if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
Arach MOHAMMED BRAHIM7469e922018-11-29 18:47:50 +0100479 // intercept this expected message, don t forward.
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200480 mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
Arach MOHAMMED BRAHIM7469e922018-11-29 18:47:50 +0100481 } else {
482 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200483 }
484 break;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100485
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;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200503 }
504}
505
506static void halWrapperCallback(uint8_t event, uint8_t event_status) {
507 uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200508 uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200509
510 switch (mHalWrapperState) {
511 case HAL_WRAPPER_STATE_CLOSED:
Arach MOHAMMED BRAHIMd9214f12018-11-29 14:54:52 +0100512 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
513 STLOG_HAL_D("NFC-NCI HAL: %s Timeout. Close anyway", __func__);
514 HalSendDownstreamStopTimer(mHalHandle);
515 return;
516 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200517 break;
518
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100519 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200530 case HAL_WRAPPER_STATE_NFC_ENABLE_ON:
531 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
532 // timeout
533 // Send CORE_INIT_CMD
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200534 STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200535 if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200536 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200537 }
538 return;
539 }
540 break;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200541 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;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200550
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200551 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200567 default:
568 break;
569 }
570
571 mHalWrapperCallback(event, event_status);
572}
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100573
574/*******************************************************************************
575 **
576 ** Function nfc_set_state
577 **
578 ** Description Set the state of NFC stack
579 **
580 ** Returns void
581 **
582 *******************************************************************************/
583void 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}