Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 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 <string.h> |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 24 | #include <unistd.h> |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 25 | #include "android_logmsg.h" |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 26 | #include "hal_fd.h" |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 27 | #include "halcore.h" |
| 28 | |
| 29 | extern void HalCoreCallback(void* context, uint32_t event, const void* d, |
| 30 | size_t length); |
| 31 | extern bool I2cOpenLayer(void* dev, HAL_CALLBACK callb, HALHANDLE* pHandle); |
| 32 | extern void I2cCloseLayer(); |
| 33 | |
| 34 | typedef 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 42 | static void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data); |
| 43 | static void halWrapperCallback(uint8_t event, uint8_t event_status); |
| 44 | |
| 45 | nfc_stack_callback_t* mHalWrapperCallback = NULL; |
| 46 | nfc_stack_data_callback_t* mHalWrapperDataCallback = NULL; |
| 47 | hal_wrapper_state_e mHalWrapperState = HAL_WRAPPER_STATE_CLOSED; |
| 48 | HALHANDLE mHalHandle = NULL; |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 49 | |
| 50 | uint8_t mClfMode; |
| 51 | uint8_t mFwUpdateTaskMask; |
| 52 | int mRetryFwDwl; |
| 53 | uint8_t mFwUpdateResMask = 0; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 54 | uint8_t* ConfigBuffer = NULL; |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 55 | uint8_t mError_count = 0; |
| 56 | bool mIsActiveRW = false; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 57 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 58 | pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER; |
| 59 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 60 | static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80, |
| 61 | 0x8A, 0x00, 0x00, 0x04}; |
| 62 | |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 63 | static const uint8_t nciHeaderPropSetConfig[9] = {0x2F, 0x02, 0x98, 0x04, 0x00, |
| 64 | 0x14, 0x01, 0x00, 0x92}; |
| 65 | static uint8_t nciPropEnableFwDbgTraces[256]; |
| 66 | static uint8_t nciPropGetFwDbgTracesConfig[] = {0x2F, 0x02, 0x05, 0x03, |
| 67 | 0x00, 0x14, 0x01, 0x00}; |
| 68 | bool mReadFwConfigDone = false; |
| 69 | |
Arach MOHAMMED BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 70 | bool mHciCreditLent = false; |
Arach MOHAMMED BRAHIM | cf7bd92 | 2019-05-09 15:40:17 +0200 | [diff] [blame] | 71 | bool mfactoryReset = false; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 72 | bool ready_flag = 0; |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 73 | bool mTimerStarted = false; |
| 74 | bool forceRecover = false; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 75 | |
| 76 | void 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 | |
| 84 | void 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 91 | bool 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 98 | mFwUpdateResMask = hal_fd_init(); |
| 99 | mRetryFwDwl = 5; |
| 100 | mFwUpdateTaskMask = 0; |
| 101 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 102 | mHalWrapperState = HAL_WRAPPER_STATE_OPEN; |
Arach MOHAMMED BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 103 | mHciCreditLent = false; |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 104 | mReadFwConfigDone = false; |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 105 | mError_count = 0; |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 106 | |
| 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 BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 124 | int 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 127 | |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 128 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 138 | I2cCloseLayer(); |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 139 | if (call_cb) mHalWrapperCallback(HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 140 | |
| 141 | return 1; |
| 142 | } |
| 143 | |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 144 | void hal_wrapper_send_core_config_prop() { |
| 145 | long retlen = 0; |
| 146 | int isfound = 0; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 147 | |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 148 | // 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 166 | } |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 167 | } |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 168 | |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 169 | void 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 | |
| 181 | void hal_wrapper_send_config() { |
| 182 | hal_wrapper_send_core_config_prop(); |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 183 | mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 184 | hal_wrapper_send_vs_config(); |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 185 | } |
| 186 | |
Arach MOHAMMED BRAHIM | cf7bd92 | 2019-05-09 15:40:17 +0200 | [diff] [blame] | 187 | void hal_wrapper_factoryReset() { |
| 188 | mfactoryReset = true; |
| 189 | STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset); |
| 190 | } |
Arach MOHAMMED BRAHIM | 0b1b8ab | 2019-05-29 18:10:20 +0200 | [diff] [blame] | 191 | |
| 192 | void 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 197 | void 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 200 | uint8_t coreResetCmd[] = {0x20, 0x00, 0x01, 0x01}; |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 201 | unsigned long num = 0; |
Arach MOHAMMED BRAHIM | 42be702 | 2020-04-15 11:58:56 +0200 | [diff] [blame] | 202 | int nciPropEnableFwDbgTraces_size = sizeof(nciPropEnableFwDbgTraces); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 203 | |
| 204 | switch (mHalWrapperState) { |
| 205 | case HAL_WRAPPER_STATE_CLOSED: // 0 |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 206 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSED", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 207 | break; |
| 208 | case HAL_WRAPPER_STATE_OPEN: // 1 |
| 209 | // CORE_RESET_NTF |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 210 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN", __func__); |
| 211 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 212 | if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) { |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 213 | mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode); |
Arach MOHAMMED BRAHIM | cf7bd92 | 2019-05-09 15:40:17 +0200 | [diff] [blame] | 214 | |
| 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 225 | STLOG_HAL_V( |
| 226 | "%s - mFwUpdateTaskMask = %d, mClfMode = %d, mRetryFwDwl = %d", |
| 227 | __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl); |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 228 | // 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 281 | } |
| 282 | } else { |
| 283 | mHalWrapperDataCallback(data_len, p_data); |
| 284 | } |
| 285 | break; |
| 286 | case HAL_WRAPPER_STATE_OPEN_CPLT: // 2 |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 287 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT", |
| 288 | __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 289 | // 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 BRAHIM | ed64c1b | 2018-07-25 16:45:04 +0200 | [diff] [blame] | 292 | STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 293 | // Send PROP_NFC_MODE_SET_CMD(ON) |
| 294 | if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn, |
| 295 | sizeof(propNfcModeSetCmdOn), 100)) { |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 296 | STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed", |
| 297 | __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 298 | } |
| 299 | mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON; |
| 300 | } else { |
| 301 | mHalWrapperDataCallback(data_len, p_data); |
| 302 | } |
| 303 | break; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 304 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 305 | case HAL_WRAPPER_STATE_NFC_ENABLE_ON: // 3 |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 306 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON", |
| 307 | __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 308 | // 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 BRAHIM | ed64c1b | 2018-07-25 16:45:04 +0200 | [diff] [blame] | 318 | STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 319 | if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) { |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 320 | STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 321 | } |
| 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 BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 326 | // 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-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 332 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 333 | mHalWrapperState = HAL_WRAPPER_STATE_READY; |
| 334 | mHalWrapperDataCallback(data_len, p_data); |
| 335 | } |
| 336 | break; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 337 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 338 | 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-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 342 | if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) { |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 343 | HalSendDownstreamStopTimer(mHalHandle); |
| 344 | set_ready(1); |
| 345 | |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 346 | STLOG_HAL_V("%s - Received config RSP, read FW dDBG config", __func__); |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 347 | } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) { |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 348 | // CORE_CONN_CREDITS_NTF |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 349 | 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 BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 363 | } else if (p_data[0] == 0x4f) { |
| 364 | // PROP_RSP |
| 365 | if (mReadFwConfigDone == true) { |
| 366 | mReadFwConfigDone = false; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 367 | HalSendDownstreamStopTimer(mHalHandle); |
| 368 | set_ready(1); |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 369 | // 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 BRAHIM | 42be702 | 2020-04-15 11:58:56 +0200 | [diff] [blame] | 393 | if ((9 + p_data[6]) < sizeof(nciPropEnableFwDbgTraces)) { |
| 394 | nciPropEnableFwDbgTraces_size = 9 + p_data[6]; |
| 395 | } |
| 396 | |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 397 | confNeeded = false; |
| 398 | |
| 399 | if (!HalSendDownstream(mHalHandle, nciPropEnableFwDbgTraces, |
Arach MOHAMMED BRAHIM | 42be702 | 2020-04-15 11:58:56 +0200 | [diff] [blame] | 400 | nciPropEnableFwDbgTraces_size)) { |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 401 | 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-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 413 | } |
| 414 | break; |
| 415 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 416 | case HAL_WRAPPER_STATE_READY: // 5 |
| 417 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_READY", __func__); |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 418 | if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) { |
Arach MOHAMMED BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 419 | 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 BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 433 | } else if ((p_data[0] == 0x6f) && (p_data[1] == 0x05)) { |
| 434 | // start timer |
| 435 | mTimerStarted = true; |
Jack Yu | b33f02f | 2020-06-23 17:08:11 +0800 | [diff] [blame^] | 436 | HalSendDownstreamTimer(mHalHandle, 5000); |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 437 | 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 BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 464 | } |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 465 | mHalWrapperDataCallback(data_len, p_data); |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 466 | } else if (forceRecover == true) { |
| 467 | forceRecover = false; |
| 468 | mHalWrapperDataCallback(data_len, p_data); |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 469 | } else { |
| 470 | STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__); |
| 471 | } |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 472 | break; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 473 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 474 | case HAL_WRAPPER_STATE_CLOSING: // 6 |
| 475 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSING", |
| 476 | __func__); |
Arach MOHAMMED BRAHIM | 0b1b8ab | 2019-05-29 18:10:20 +0200 | [diff] [blame] | 477 | hal_fd_close(); |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 478 | if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) { |
Arach MOHAMMED BRAHIM | 7469e92 | 2018-11-29 18:47:50 +0100 | [diff] [blame] | 479 | // intercept this expected message, don t forward. |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 480 | mHalWrapperState = HAL_WRAPPER_STATE_CLOSED; |
Arach MOHAMMED BRAHIM | 7469e92 | 2018-11-29 18:47:50 +0100 | [diff] [blame] | 481 | } else { |
| 482 | mHalWrapperDataCallback(data_len, p_data); |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 483 | } |
| 484 | break; |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 485 | |
| 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | static void halWrapperCallback(uint8_t event, uint8_t event_status) { |
| 507 | uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00}; |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 508 | uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01}; |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 509 | |
| 510 | switch (mHalWrapperState) { |
| 511 | case HAL_WRAPPER_STATE_CLOSED: |
Arach MOHAMMED BRAHIM | d9214f1 | 2018-11-29 14:54:52 +0100 | [diff] [blame] | 512 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 517 | break; |
| 518 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 519 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 530 | case HAL_WRAPPER_STATE_NFC_ENABLE_ON: |
| 531 | if (event == HAL_WRAPPER_TIMEOUT_EVT) { |
| 532 | // timeout |
| 533 | // Send CORE_INIT_CMD |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 534 | STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 535 | if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) { |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 536 | STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 537 | } |
| 538 | return; |
| 539 | } |
| 540 | break; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 541 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 550 | |
Arach MOHAMMED BRAHIM | 507dcb0 | 2019-09-13 10:30:38 +0200 | [diff] [blame] | 551 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 567 | default: |
| 568 | break; |
| 569 | } |
| 570 | |
| 571 | mHalWrapperCallback(event, event_status); |
| 572 | } |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 573 | |
| 574 | /******************************************************************************* |
| 575 | ** |
| 576 | ** Function nfc_set_state |
| 577 | ** |
| 578 | ** Description Set the state of NFC stack |
| 579 | ** |
| 580 | ** Returns void |
| 581 | ** |
| 582 | *******************************************************************************/ |
| 583 | void 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 | } |