blob: f68718983f892e2db0e55729294ce8e6cdf8c5b4 [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>
George Changa009f4e2020-12-29 16:57:45 +080023#include <log/log.h>
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020024#include <string.h>
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +020025#include <unistd.h>
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020026#include "android_logmsg.h"
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010027#include "hal_fd.h"
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020028#include "halcore.h"
29
30extern void HalCoreCallback(void* context, uint32_t event, const void* d,
31 size_t length);
32extern bool I2cOpenLayer(void* dev, HAL_CALLBACK callb, HALHANDLE* pHandle);
33extern void I2cCloseLayer();
34
35typedef 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020043static void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data);
44static void halWrapperCallback(uint8_t event, uint8_t event_status);
45
46nfc_stack_callback_t* mHalWrapperCallback = NULL;
47nfc_stack_data_callback_t* mHalWrapperDataCallback = NULL;
48hal_wrapper_state_e mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
49HALHANDLE mHalHandle = NULL;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010050
51uint8_t mClfMode;
52uint8_t mFwUpdateTaskMask;
53int mRetryFwDwl;
54uint8_t mFwUpdateResMask = 0;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020055uint8_t* ConfigBuffer = NULL;
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +020056uint8_t mError_count = 0;
57bool mIsActiveRW = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020058pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
59pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
60
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010061static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80,
62 0x8A, 0x00, 0x00, 0x04};
63
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +010064static const uint8_t nciHeaderPropSetConfig[9] = {0x2F, 0x02, 0x98, 0x04, 0x00,
65 0x14, 0x01, 0x00, 0x92};
66static uint8_t nciPropEnableFwDbgTraces[256];
67static uint8_t nciPropGetFwDbgTracesConfig[] = {0x2F, 0x02, 0x05, 0x03,
68 0x00, 0x14, 0x01, 0x00};
69bool mReadFwConfigDone = false;
70
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +020071bool mHciCreditLent = false;
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +020072bool mfactoryReset = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020073bool ready_flag = 0;
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +020074bool mTimerStarted = false;
75bool forceRecover = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020076
77void 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
85void 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020092bool 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
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010099 mFwUpdateResMask = hal_fd_init();
100 mRetryFwDwl = 5;
101 mFwUpdateTaskMask = 0;
102
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200103 mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200104 mHciCreditLent = false;
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100105 mReadFwConfigDone = false;
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200106 mError_count = 0;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200107
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
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200125int 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};
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200128
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200129 mHalWrapperState = HAL_WRAPPER_STATE_CLOSING;
130 // Send PROP_NFC_MODE_SET_CMD
131 if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdQb,
Arach MOHAMMED BRAHIMb9c65c12020-08-05 10:11:11 +0200132 sizeof(propNfcModeSetCmdQb), 40)) {
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200133 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200139 I2cCloseLayer();
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200140 if (call_cb) mHalWrapperCallback(HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200141
142 return 1;
143}
144
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200145void hal_wrapper_send_core_config_prop() {
146 long retlen = 0;
147 int isfound = 0;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100148
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200149 // 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;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100167 }
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200168}
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100169
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200170void 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
182void hal_wrapper_send_config() {
183 hal_wrapper_send_core_config_prop();
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100184 mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200185 hal_wrapper_send_vs_config();
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100186}
187
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +0200188void hal_wrapper_factoryReset() {
189 mfactoryReset = true;
190 STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset);
191}
Arach MOHAMMED BRAHIM0b1b8ab2019-05-29 18:10:20 +0200192
193void 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}
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200198void 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};
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100201 uint8_t coreResetCmd[] = {0x20, 0x00, 0x01, 0x01};
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100202 unsigned long num = 0;
Arach MOHAMMED BRAHIM53fedaa2020-04-15 11:58:56 +0200203 int nciPropEnableFwDbgTraces_size = sizeof(nciPropEnableFwDbgTraces);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200204
205 switch (mHalWrapperState) {
206 case HAL_WRAPPER_STATE_CLOSED: // 0
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100207 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSED", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200208 break;
209 case HAL_WRAPPER_STATE_OPEN: // 1
210 // CORE_RESET_NTF
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100211 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN", __func__);
212
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200213 if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100214 mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode);
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +0200215
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 }
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100226 STLOG_HAL_V(
227 "%s - mFwUpdateTaskMask = %d, mClfMode = %d, mRetryFwDwl = %d",
228 __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl);
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100229 // 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 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200282 }
283 } else {
284 mHalWrapperDataCallback(data_len, p_data);
285 }
286 break;
287 case HAL_WRAPPER_STATE_OPEN_CPLT: // 2
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100288 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT",
289 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200290 // CORE_INIT_RSP
291 if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
292 } else if ((p_data[0] == 0x60) && (p_data[1] == 0x06)) {
Arach MOHAMMED BRAHIMed64c1b2018-07-25 16:45:04 +0200293 STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200294 // Send PROP_NFC_MODE_SET_CMD(ON)
295 if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn,
296 sizeof(propNfcModeSetCmdOn), 100)) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200297 STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed",
298 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200299 }
300 mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON;
301 } else {
302 mHalWrapperDataCallback(data_len, p_data);
303 }
304 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100305
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200306 case HAL_WRAPPER_STATE_NFC_ENABLE_ON: // 3
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100307 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON",
308 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200309 // 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
Arach MOHAMMED BRAHIMed64c1b2018-07-25 16:45:04 +0200319 STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200320 if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200321 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200322 }
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__);
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200327 // 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 }
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100333
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200334 mHalWrapperState = HAL_WRAPPER_STATE_READY;
335 mHalWrapperDataCallback(data_len, p_data);
336 }
337 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100338
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100339 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
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100343 if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) {
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200344 HalSendDownstreamStopTimer(mHalHandle);
345 set_ready(1);
346
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100347 STLOG_HAL_V("%s - Received config RSP, read FW dDBG config", __func__);
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100348 } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100349 // CORE_CONN_CREDITS_NTF
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100350 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);
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100364 } else if (p_data[0] == 0x4f) {
365 // PROP_RSP
366 if (mReadFwConfigDone == true) {
367 mReadFwConfigDone = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200368 HalSendDownstreamStopTimer(mHalHandle);
369 set_ready(1);
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100370 // 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
George Changa009f4e2020-12-29 16:57:45 +0800390 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
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100398 if (confNeeded) {
399 memcpy(nciPropEnableFwDbgTraces, nciHeaderPropSetConfig, 9);
400 memcpy(&nciPropEnableFwDbgTraces[10], &p_data[8],
401 p_data[6] - 1);
Arach MOHAMMED BRAHIM53fedaa2020-04-15 11:58:56 +0200402 if ((9 + p_data[6]) < sizeof(nciPropEnableFwDbgTraces)) {
403 nciPropEnableFwDbgTraces_size = 9 + p_data[6];
404 }
405
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100406 confNeeded = false;
407
408 if (!HalSendDownstream(mHalHandle, nciPropEnableFwDbgTraces,
Arach MOHAMMED BRAHIM53fedaa2020-04-15 11:58:56 +0200409 nciPropEnableFwDbgTraces_size)) {
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100410 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;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100422 }
423 break;
424
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100425 case HAL_WRAPPER_STATE_READY: // 5
426 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_READY", __func__);
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200427 if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) {
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200428 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 }
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200442 } else if ((p_data[0] == 0x6f) && (p_data[1] == 0x05)) {
443 // start timer
444 mTimerStarted = true;
Jack Yub5ec4292020-06-23 17:08:11 +0800445 HalSendDownstreamTimer(mHalHandle, 5000);
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200446 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 }
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200473 }
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200474 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200475 } else if (forceRecover == true) {
476 forceRecover = false;
477 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200478 } else {
479 STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__);
480 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200481 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100482
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100483 case HAL_WRAPPER_STATE_CLOSING: // 6
484 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSING",
485 __func__);
Arach MOHAMMED BRAHIM0b1b8ab2019-05-29 18:10:20 +0200486 hal_fd_close();
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200487 if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
Arach MOHAMMED BRAHIM7469e922018-11-29 18:47:50 +0100488 // intercept this expected message, don t forward.
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200489 mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
Arach MOHAMMED BRAHIM7469e922018-11-29 18:47:50 +0100490 } else {
491 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200492 }
493 break;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100494
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;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200512 }
513}
514
515static void halWrapperCallback(uint8_t event, uint8_t event_status) {
516 uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200517 uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200518
519 switch (mHalWrapperState) {
Arach MOHAMMED BRAHIMb9c65c12020-08-05 10:11:11 +0200520 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200530 case HAL_WRAPPER_STATE_CLOSED:
Arach MOHAMMED BRAHIMd9214f12018-11-29 14:54:52 +0100531 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
532 STLOG_HAL_D("NFC-NCI HAL: %s Timeout. Close anyway", __func__);
533 HalSendDownstreamStopTimer(mHalHandle);
534 return;
535 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200536 break;
537
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100538 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200549 case HAL_WRAPPER_STATE_NFC_ENABLE_ON:
550 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
551 // timeout
552 // Send CORE_INIT_CMD
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200553 STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200554 if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200555 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200556 }
557 return;
558 }
559 break;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200560 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;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200569
Arach MOHAMMED BRAHIM507dcb02019-09-13 10:30:38 +0200570 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
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200586 default:
587 break;
588 }
589
590 mHalWrapperCallback(event, event_status);
591}
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100592
593/*******************************************************************************
594 **
595 ** Function nfc_set_state
596 **
597 ** Description Set the state of NFC stack
598 **
599 ** Returns void
600 **
601 *******************************************************************************/
602void 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}