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; |
| 55 | pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; |
| 56 | pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER; |
| 57 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 58 | static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80, |
| 59 | 0x8A, 0x00, 0x00, 0x04}; |
| 60 | |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 61 | static const uint8_t nciHeaderPropSetConfig[9] = {0x2F, 0x02, 0x98, 0x04, 0x00, |
| 62 | 0x14, 0x01, 0x00, 0x92}; |
| 63 | static uint8_t nciPropEnableFwDbgTraces[256]; |
| 64 | static uint8_t nciPropGetFwDbgTracesConfig[] = {0x2F, 0x02, 0x05, 0x03, |
| 65 | 0x00, 0x14, 0x01, 0x00}; |
| 66 | bool mReadFwConfigDone = false; |
| 67 | |
Arach MOHAMMED BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 68 | bool mHciCreditLent = false; |
Arach MOHAMMED BRAHIM | cf7bd92 | 2019-05-09 15:40:17 +0200 | [diff] [blame^] | 69 | bool mfactoryReset = false; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 70 | bool ready_flag = 0; |
| 71 | |
| 72 | void 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 | |
| 80 | void 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 87 | bool 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 94 | mFwUpdateResMask = hal_fd_init(); |
| 95 | mRetryFwDwl = 5; |
| 96 | mFwUpdateTaskMask = 0; |
| 97 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 98 | mHalWrapperState = HAL_WRAPPER_STATE_OPEN; |
Arach MOHAMMED BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 99 | mHciCreditLent = false; |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 100 | mReadFwConfigDone = false; |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 101 | |
| 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 BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 119 | int 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 122 | |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 123 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 133 | I2cCloseLayer(); |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 134 | 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] | 135 | |
| 136 | return 1; |
| 137 | } |
| 138 | |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 139 | void hal_wrapper_send_core_config_prop() { |
| 140 | long retlen = 0; |
| 141 | int isfound = 0; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 142 | |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 143 | // 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 161 | } |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 162 | } |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 163 | |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 164 | void 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 | |
| 176 | void hal_wrapper_send_config() { |
| 177 | hal_wrapper_send_core_config_prop(); |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 178 | mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 179 | hal_wrapper_send_vs_config(); |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 180 | } |
| 181 | |
Arach MOHAMMED BRAHIM | cf7bd92 | 2019-05-09 15:40:17 +0200 | [diff] [blame^] | 182 | void hal_wrapper_factoryReset() { |
| 183 | mfactoryReset = true; |
| 184 | STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset); |
| 185 | } |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 186 | void 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 189 | uint8_t coreResetCmd[] = {0x20, 0x00, 0x01, 0x01}; |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 190 | unsigned long num = 0; |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 191 | |
| 192 | switch (mHalWrapperState) { |
| 193 | case HAL_WRAPPER_STATE_CLOSED: // 0 |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 194 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSED", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 195 | break; |
| 196 | case HAL_WRAPPER_STATE_OPEN: // 1 |
| 197 | // CORE_RESET_NTF |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 198 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN", __func__); |
| 199 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 200 | if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) { |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 201 | mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode); |
Arach MOHAMMED BRAHIM | cf7bd92 | 2019-05-09 15:40:17 +0200 | [diff] [blame^] | 202 | |
| 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 BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 213 | STLOG_HAL_V( |
| 214 | "%s - mFwUpdateTaskMask = %d, mClfMode = %d, mRetryFwDwl = %d", |
| 215 | __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl); |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 216 | // 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 269 | } |
| 270 | } else { |
| 271 | mHalWrapperDataCallback(data_len, p_data); |
| 272 | } |
| 273 | break; |
| 274 | case HAL_WRAPPER_STATE_OPEN_CPLT: // 2 |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 275 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT", |
| 276 | __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 277 | // 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 BRAHIM | ed64c1b | 2018-07-25 16:45:04 +0200 | [diff] [blame] | 280 | STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 281 | // Send PROP_NFC_MODE_SET_CMD(ON) |
| 282 | if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn, |
| 283 | sizeof(propNfcModeSetCmdOn), 100)) { |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 284 | STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed", |
| 285 | __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 286 | } |
| 287 | mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON; |
| 288 | } else { |
| 289 | mHalWrapperDataCallback(data_len, p_data); |
| 290 | } |
| 291 | break; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 292 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 293 | case HAL_WRAPPER_STATE_NFC_ENABLE_ON: // 3 |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 294 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON", |
| 295 | __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 296 | // 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 BRAHIM | ed64c1b | 2018-07-25 16:45:04 +0200 | [diff] [blame] | 306 | STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 307 | if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) { |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 308 | STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 309 | } |
| 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 BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 314 | // 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-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 320 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 321 | mHalWrapperState = HAL_WRAPPER_STATE_READY; |
| 322 | mHalWrapperDataCallback(data_len, p_data); |
| 323 | } |
| 324 | break; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 325 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 326 | 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-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 330 | if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) { |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 331 | HalSendDownstreamStopTimer(mHalHandle); |
| 332 | set_ready(1); |
| 333 | |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 334 | 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] | 335 | } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) { |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 336 | // CORE_CONN_CREDITS_NTF |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 337 | 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 BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 351 | } else if (p_data[0] == 0x4f) { |
| 352 | // PROP_RSP |
| 353 | if (mReadFwConfigDone == true) { |
| 354 | mReadFwConfigDone = false; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 355 | HalSendDownstreamStopTimer(mHalHandle); |
| 356 | set_ready(1); |
Arach MOHAMMED BRAHIM | cb8da7f | 2019-02-15 16:20:29 +0100 | [diff] [blame] | 357 | // 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-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 397 | } |
| 398 | break; |
| 399 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 400 | case HAL_WRAPPER_STATE_READY: // 5 |
| 401 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_READY", __func__); |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 402 | if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) { |
Arach MOHAMMED BRAHIM | 449661b | 2018-09-24 17:02:54 +0200 | [diff] [blame] | 403 | 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 BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 418 | mHalWrapperDataCallback(data_len, p_data); |
| 419 | } else { |
| 420 | STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__); |
| 421 | } |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 422 | break; |
Alexandra Ducati-Manas | 9030f51 | 2018-11-22 09:22:43 +0100 | [diff] [blame] | 423 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 424 | case HAL_WRAPPER_STATE_CLOSING: // 6 |
| 425 | STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_CLOSING", |
| 426 | __func__); |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 427 | if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) { |
Arach MOHAMMED BRAHIM | 7469e92 | 2018-11-29 18:47:50 +0100 | [diff] [blame] | 428 | // intercept this expected message, don t forward. |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 429 | mHalWrapperState = HAL_WRAPPER_STATE_CLOSED; |
Arach MOHAMMED BRAHIM | 7469e92 | 2018-11-29 18:47:50 +0100 | [diff] [blame] | 430 | } else { |
| 431 | mHalWrapperDataCallback(data_len, p_data); |
Arach MOHAMMED BRAHIM | ef5b4d9 | 2018-09-24 16:07:04 +0200 | [diff] [blame] | 432 | } |
| 433 | break; |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 434 | |
| 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
| 455 | static 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 BRAHIM | d9214f1 | 2018-11-29 14:54:52 +0100 | [diff] [blame] | 460 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 465 | break; |
| 466 | |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 467 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 478 | case HAL_WRAPPER_STATE_NFC_ENABLE_ON: |
| 479 | if (event == HAL_WRAPPER_TIMEOUT_EVT) { |
| 480 | // timeout |
| 481 | // Send CORE_INIT_CMD |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 482 | STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 483 | if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) { |
Arach MOHAMMED BRAHIM | 838910b | 2018-08-16 16:05:24 +0200 | [diff] [blame] | 484 | STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__); |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 485 | } |
| 486 | return; |
| 487 | } |
| 488 | break; |
Arach MOHAMMED BRAHIM | dbc5db0 | 2019-05-07 12:26:29 +0200 | [diff] [blame] | 489 | 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 BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 498 | |
Arach MOHAMMED BRAHIM | 7c2cf79 | 2018-05-09 10:04:24 +0200 | [diff] [blame] | 499 | default: |
| 500 | break; |
| 501 | } |
| 502 | |
| 503 | mHalWrapperCallback(event, event_status); |
| 504 | } |
Arach MOHAMMED BRAHIM | 7238143 | 2019-02-11 18:03:27 +0100 | [diff] [blame] | 505 | |
| 506 | /******************************************************************************* |
| 507 | ** |
| 508 | ** Function nfc_set_state |
| 509 | ** |
| 510 | ** Description Set the state of NFC stack |
| 511 | ** |
| 512 | ** Returns void |
| 513 | ** |
| 514 | *******************************************************************************/ |
| 515 | void 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 | } |