blob: 75585213a0c27ba3e6131973ac090c92ea896748 [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;
55pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
56pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
57
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010058static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80,
59 0x8A, 0x00, 0x00, 0x04};
60
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +010061static const uint8_t nciHeaderPropSetConfig[9] = {0x2F, 0x02, 0x98, 0x04, 0x00,
62 0x14, 0x01, 0x00, 0x92};
63static uint8_t nciPropEnableFwDbgTraces[256];
64static uint8_t nciPropGetFwDbgTracesConfig[] = {0x2F, 0x02, 0x05, 0x03,
65 0x00, 0x14, 0x01, 0x00};
66bool mReadFwConfigDone = false;
67
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +020068bool mHciCreditLent = false;
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +020069bool mfactoryReset = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +020070bool ready_flag = 0;
71
72void wait_ready() {
73 pthread_mutex_lock(&mutex);
74 while (!ready_flag) {
75 pthread_cond_wait(&ready_cond, &mutex);
76 }
77 pthread_mutex_unlock(&mutex);
78}
79
80void set_ready(bool ready) {
81 pthread_mutex_lock(&mutex);
82 ready_flag = ready;
83 pthread_cond_signal(&ready_cond);
84 pthread_mutex_unlock(&mutex);
85}
86
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020087bool hal_wrapper_open(st21nfc_dev_t* dev, nfc_stack_callback_t* p_cback,
88 nfc_stack_data_callback_t* p_data_cback,
89 HALHANDLE* pHandle) {
90 bool result;
91
92 STLOG_HAL_D("%s", __func__);
93
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +010094 mFwUpdateResMask = hal_fd_init();
95 mRetryFwDwl = 5;
96 mFwUpdateTaskMask = 0;
97
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +020098 mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +020099 mHciCreditLent = false;
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100100 mReadFwConfigDone = false;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200101
102 mHalWrapperCallback = p_cback;
103 mHalWrapperDataCallback = p_data_cback;
104
105 dev->p_data_cback = halWrapperDataCallback;
106 dev->p_cback = halWrapperCallback;
107
108 result = I2cOpenLayer(dev, HalCoreCallback, pHandle);
109
110 if (!result || !(*pHandle)) {
111 return -1; // We are doomed, stop it here, NOW !
112 }
113
114 mHalHandle = *pHandle;
115
116 return 1;
117}
118
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200119int hal_wrapper_close(int call_cb, int nfc_mode) {
120 STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD(%d)", __func__, nfc_mode);
121 uint8_t propNfcModeSetCmdQb[] = {0x2f, 0x02, 0x02, 0x02, (uint8_t)nfc_mode};
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200122
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200123 mHalWrapperState = HAL_WRAPPER_STATE_CLOSING;
124 // Send PROP_NFC_MODE_SET_CMD
125 if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdQb,
126 sizeof(propNfcModeSetCmdQb), 100)) {
127 STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed", __func__);
128 return -1;
129 }
130 // Let the CLF receive and process this
131 usleep(50000);
132
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200133 I2cCloseLayer();
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200134 if (call_cb) mHalWrapperCallback(HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200135
136 return 1;
137}
138
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200139void hal_wrapper_send_core_config_prop() {
140 long retlen = 0;
141 int isfound = 0;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100142
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200143 // allocate buffer for setting parameters
144 ConfigBuffer = (uint8_t*)malloc(256 * sizeof(uint8_t));
145 if (ConfigBuffer != NULL) {
146 isfound = GetByteArrayValue(NAME_CORE_CONF_PROP, (char*)ConfigBuffer, 256,
147 &retlen);
148
149 if (isfound > 0) {
150 STLOG_HAL_V("%s - Enter", __func__);
151 set_ready(0);
152
153 if (!HalSendDownstreamTimer(mHalHandle, ConfigBuffer, retlen, 500)) {
154 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
155 }
156 mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
157 wait_ready();
158 }
159 free(ConfigBuffer);
160 ConfigBuffer = NULL;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100161 }
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200162}
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100163
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200164void hal_wrapper_send_vs_config() {
165 STLOG_HAL_V("%s - Enter", __func__);
166 set_ready(0);
167
168 if (!HalSendDownstreamTimer(mHalHandle, nciPropGetFwDbgTracesConfig,
169 sizeof(nciPropGetFwDbgTracesConfig), 500)) {
170 STLOG_HAL_E("%s - SendDownstream failed", __func__);
171 }
172 mReadFwConfigDone = true;
173 wait_ready();
174}
175
176void hal_wrapper_send_config() {
177 hal_wrapper_send_core_config_prop();
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100178 mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200179 hal_wrapper_send_vs_config();
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100180}
181
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +0200182void hal_wrapper_factoryReset() {
183 mfactoryReset = true;
184 STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset);
185}
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200186void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data) {
187 uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
188 uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100189 uint8_t coreResetCmd[] = {0x20, 0x00, 0x01, 0x01};
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100190 unsigned long num = 0;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200191
192 switch (mHalWrapperState) {
193 case HAL_WRAPPER_STATE_CLOSED: // 0
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100194 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSED", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200195 break;
196 case HAL_WRAPPER_STATE_OPEN: // 1
197 // CORE_RESET_NTF
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100198 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN", __func__);
199
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200200 if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100201 mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode);
Arach MOHAMMED BRAHIMcf7bd922019-05-09 15:40:17 +0200202
203 if (mfactoryReset == true) {
204 STLOG_HAL_V(
205 "%s - first boot after factory reset detected - start FW update",
206 __func__);
207 if ((mFwUpdateResMask & FW_PATCH_AVAILABLE) &&
208 (mFwUpdateResMask & FW_CUSTOM_PARAM_AVAILABLE)) {
209 mFwUpdateTaskMask = FW_UPDATE_NEEDED | CONF_UPDATE_NEEDED;
210 mfactoryReset = false;
211 }
212 }
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100213 STLOG_HAL_V(
214 "%s - mFwUpdateTaskMask = %d, mClfMode = %d, mRetryFwDwl = %d",
215 __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl);
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100216 // CLF in MODE LOADER & Update needed.
217 if (mClfMode == FT_CLF_MODE_LOADER) {
218 HalSendDownstreamStopTimer(mHalHandle);
219 STLOG_HAL_V("%s --- CLF mode is LOADER ---", __func__);
220
221 if (mRetryFwDwl == 0) {
222 STLOG_HAL_V(
223 "%s - Reached maximum nb of retries, FW update failed, exiting",
224 __func__);
225 mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
226 I2cCloseLayer();
227 } else {
228 STLOG_HAL_V("%s - Send APDU_GET_ATR_CMD", __func__);
229 mRetryFwDwl--;
230 if (!HalSendDownstreamTimer(mHalHandle, ApduGetAtr,
231 sizeof(ApduGetAtr),
232 FW_TIMER_DURATION)) {
233 STLOG_HAL_E("%s - SendDownstream failed", __func__);
234 }
235 mHalWrapperState = HAL_WRAPPER_STATE_UPDATE;
236 }
237 } else if (mFwUpdateTaskMask == 0 || mRetryFwDwl == 0) {
238 STLOG_HAL_V("%s - Proceeding with normal startup", __func__);
239 if (p_data[3] == 0x01) {
240 // Normal mode, start HAL
241 mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_OK);
242 mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT;
243 } else {
244 // No more retries or CLF not in correct mode
245 mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
246 }
247 // CLF in MODE ROUTER & Update needed.
248 } else if (mClfMode == FT_CLF_MODE_ROUTER) {
249 if ((mFwUpdateTaskMask & FW_UPDATE_NEEDED) &&
250 (mFwUpdateResMask & FW_PATCH_AVAILABLE)) {
251 STLOG_HAL_V(
252 "%s - CLF in ROUTER mode, FW update needed, try upgrade FW -",
253 __func__);
254 mRetryFwDwl--;
255
256 if (!HalSendDownstream(mHalHandle, coreResetCmd,
257 sizeof(coreResetCmd))) {
258 STLOG_HAL_E("%s - SendDownstream failed", __func__);
259 }
260 mHalWrapperState = HAL_WRAPPER_STATE_EXIT_HIBERNATE_INTERNAL;
261 } else if ((mFwUpdateTaskMask & CONF_UPDATE_NEEDED) &&
262 (mFwUpdateResMask & FW_CUSTOM_PARAM_AVAILABLE)) {
263 if (!HalSendDownstream(mHalHandle, coreResetCmd,
264 sizeof(coreResetCmd))) {
265 STLOG_HAL_E("%s - SendDownstream failed", __func__);
266 }
267 mHalWrapperState = HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM;
268 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200269 }
270 } else {
271 mHalWrapperDataCallback(data_len, p_data);
272 }
273 break;
274 case HAL_WRAPPER_STATE_OPEN_CPLT: // 2
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100275 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT",
276 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200277 // CORE_INIT_RSP
278 if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
279 } else if ((p_data[0] == 0x60) && (p_data[1] == 0x06)) {
Arach MOHAMMED BRAHIMed64c1b2018-07-25 16:45:04 +0200280 STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200281 // Send PROP_NFC_MODE_SET_CMD(ON)
282 if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn,
283 sizeof(propNfcModeSetCmdOn), 100)) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200284 STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed",
285 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200286 }
287 mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON;
288 } else {
289 mHalWrapperDataCallback(data_len, p_data);
290 }
291 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100292
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200293 case HAL_WRAPPER_STATE_NFC_ENABLE_ON: // 3
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100294 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON",
295 __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200296 // PROP_NFC_MODE_SET_RSP
297 if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
298 // DO nothing: wait for core_reset_ntf or timer timeout
299 }
300 // CORE_RESET_NTF
301 else if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
302 // Stop timer
303 HalSendDownstreamStopTimer(mHalHandle);
304
305 // Send CORE_INIT_CMD
Arach MOHAMMED BRAHIMed64c1b2018-07-25 16:45:04 +0200306 STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200307 if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200308 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200309 }
310 }
311 // CORE_INIT_RSP
312 else if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
313 STLOG_HAL_D("%s - NFC mode enabled", __func__);
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200314 // Do we need to lend a credit ?
315 if (p_data[13] == 0x00) {
316 STLOG_HAL_D("%s - 1 credit lent", __func__);
317 p_data[13] = 0x01;
318 mHciCreditLent = true;
319 }
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100320
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200321 mHalWrapperState = HAL_WRAPPER_STATE_READY;
322 mHalWrapperDataCallback(data_len, p_data);
323 }
324 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100325
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100326 case HAL_WRAPPER_STATE_PROP_CONFIG: // 4
327 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG",
328 __func__);
329 // CORE_SET_CONFIG_RSP
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100330 if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) {
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200331 HalSendDownstreamStopTimer(mHalHandle);
332 set_ready(1);
333
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100334 STLOG_HAL_V("%s - Received config RSP, read FW dDBG config", __func__);
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100335 } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100336 // CORE_CONN_CREDITS_NTF
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100337 if (p_data[4] == 0x01) { // HCI connection
338 mHciCreditLent = false;
339 STLOG_HAL_D("%s - credit returned", __func__);
340 if (p_data[5] == 0x01) {
341 // no need to send this.
342 break;
343 } else {
344 if (p_data[5] != 0x00 && p_data[5] != 0xFF) {
345 // send with 1 less
346 p_data[5]--;
347 }
348 }
349 }
350 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100351 } else if (p_data[0] == 0x4f) {
352 // PROP_RSP
353 if (mReadFwConfigDone == true) {
354 mReadFwConfigDone = false;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200355 HalSendDownstreamStopTimer(mHalHandle);
356 set_ready(1);
Arach MOHAMMED BRAHIMcb8da7f2019-02-15 16:20:29 +0100357 // NFC_STATUS_OK
358 if (p_data[3] == 0x00) {
359 bool confNeeded = false;
360
361 // Check if FW DBG shall be set
362 if (GetNumValue(NAME_STNFC_FW_DEBUG_ENABLED, &num, sizeof(num))) {
363 // If conf file indicate set needed and not yet enabled
364 if ((num == 1) && (p_data[7] == 0x00)) {
365 STLOG_HAL_D("%s - FW DBG traces enabling needed", __func__);
366 nciPropEnableFwDbgTraces[9] = 0x01;
367 confNeeded = true;
368 } else if ((num == 0) && (p_data[7] == 0x01)) {
369 STLOG_HAL_D("%s - FW DBG traces disabling needed", __func__);
370 nciPropEnableFwDbgTraces[9] = 0x00;
371 confNeeded = true;
372 } else {
373 STLOG_HAL_D("%s - No changes in FW DBG traces config needed",
374 __func__);
375 }
376
377 if (confNeeded) {
378 memcpy(nciPropEnableFwDbgTraces, nciHeaderPropSetConfig, 9);
379 memcpy(&nciPropEnableFwDbgTraces[10], &p_data[8],
380 p_data[6] - 1);
381 confNeeded = false;
382
383 if (!HalSendDownstream(mHalHandle, nciPropEnableFwDbgTraces,
384 sizeof(nciPropEnableFwDbgTraces))) {
385 STLOG_HAL_E("%s - SendDownstream failed", __func__);
386 }
387
388 break;
389 }
390 }
391 }
392 }
393
394 // Exit state, all processing done
395 mHalWrapperCallback(HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_OK);
396 mHalWrapperState = HAL_WRAPPER_STATE_READY;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100397 }
398 break;
399
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100400 case HAL_WRAPPER_STATE_READY: // 5
401 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_READY", __func__);
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200402 if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) {
Arach MOHAMMED BRAHIM449661b2018-09-24 17:02:54 +0200403 if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
404 if (p_data[4] == 0x01) { // HCI connection
405 mHciCreditLent = false;
406 STLOG_HAL_D("%s - credit returned", __func__);
407 if (p_data[5] == 0x01) {
408 // no need to send this.
409 break;
410 } else {
411 if (p_data[5] != 0x00 && p_data[5] != 0xFF) {
412 // send with 1 less
413 p_data[5]--;
414 }
415 }
416 }
417 }
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200418 mHalWrapperDataCallback(data_len, p_data);
419 } else {
420 STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__);
421 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200422 break;
Alexandra Ducati-Manas9030f512018-11-22 09:22:43 +0100423
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100424 case HAL_WRAPPER_STATE_CLOSING: // 6
425 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSING",
426 __func__);
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200427 if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
Arach MOHAMMED BRAHIM7469e922018-11-29 18:47:50 +0100428 // intercept this expected message, don t forward.
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200429 mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
Arach MOHAMMED BRAHIM7469e922018-11-29 18:47:50 +0100430 } else {
431 mHalWrapperDataCallback(data_len, p_data);
Arach MOHAMMED BRAHIMef5b4d92018-09-24 16:07:04 +0200432 }
433 break;
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100434
435 case HAL_WRAPPER_STATE_EXIT_HIBERNATE_INTERNAL: // 6
436 STLOG_HAL_V(
437 "%s - mHalWrapperState = HAL_WRAPPER_STATE_EXIT_HIBERNATE_INTERNAL",
438 __func__);
439 ExitHibernateHandler(mHalHandle, data_len, p_data);
440 break;
441
442 case HAL_WRAPPER_STATE_UPDATE: // 7
443 STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_UPDATE", __func__);
444 UpdateHandler(mHalHandle, data_len, p_data);
445 break;
446 case HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM: // 8
447 STLOG_HAL_V(
448 "%s - mHalWrapperState = HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM",
449 __func__);
450 ApplyCustomParamHandler(mHalHandle, data_len, p_data);
451 break;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200452 }
453}
454
455static void halWrapperCallback(uint8_t event, uint8_t event_status) {
456 uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
457
458 switch (mHalWrapperState) {
459 case HAL_WRAPPER_STATE_CLOSED:
Arach MOHAMMED BRAHIMd9214f12018-11-29 14:54:52 +0100460 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
461 STLOG_HAL_D("NFC-NCI HAL: %s Timeout. Close anyway", __func__);
462 HalSendDownstreamStopTimer(mHalHandle);
463 return;
464 }
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200465 break;
466
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100467 case HAL_WRAPPER_STATE_UPDATE:
468 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
469 STLOG_HAL_E("%s - Timer for FW update procedure timeout, retry",
470 __func__);
471 HalSendDownstreamStopTimer(mHalHandle);
472 resetHandlerState();
473 I2cResetPulse();
474 mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
475 }
476 break;
477
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200478 case HAL_WRAPPER_STATE_NFC_ENABLE_ON:
479 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
480 // timeout
481 // Send CORE_INIT_CMD
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200482 STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200483 if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
Arach MOHAMMED BRAHIM838910b2018-08-16 16:05:24 +0200484 STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200485 }
486 return;
487 }
488 break;
Arach MOHAMMED BRAHIMdbc5db02019-05-07 12:26:29 +0200489 case HAL_WRAPPER_STATE_PROP_CONFIG:
490 if (event == HAL_WRAPPER_TIMEOUT_EVT) {
491 STLOG_HAL_E("%s - Timer when sending conf parameters, retry", __func__);
492 HalSendDownstreamStopTimer(mHalHandle);
493 resetHandlerState();
494 I2cResetPulse();
495 mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
496 }
497 break;
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200498
Arach MOHAMMED BRAHIM7c2cf792018-05-09 10:04:24 +0200499 default:
500 break;
501 }
502
503 mHalWrapperCallback(event, event_status);
504}
Arach MOHAMMED BRAHIM72381432019-02-11 18:03:27 +0100505
506/*******************************************************************************
507 **
508 ** Function nfc_set_state
509 **
510 ** Description Set the state of NFC stack
511 **
512 ** Returns void
513 **
514 *******************************************************************************/
515void hal_wrapper_set_state(hal_wrapper_state_e new_wrapper_state) {
516 ALOGD("nfc_set_state %d->%d", mHalWrapperState, new_wrapper_state);
517
518 mHalWrapperState = new_wrapper_state;
519}