blob: 099d99b1e34602668d5326edb361b23ac32349a7 [file] [log] [blame]
/******************************************************************************
*
* Copyright (C) 2017 ST Microelectronics S.A.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
******************************************************************************/
#define LOG_TAG "NfcNciHalWrapper"
#include <cutils/properties.h>
#include <errno.h>
#include <hardware/nfc.h>
#include <string.h>
#include <unistd.h>
#include "android_logmsg.h"
#include "halcore.h"
extern void HalCoreCallback(void* context, uint32_t event, const void* d,
size_t length);
extern bool I2cOpenLayer(void* dev, HAL_CALLBACK callb, HALHANDLE* pHandle);
extern void I2cCloseLayer();
typedef struct {
struct nfc_nci_device nci_device; // nci_device must be first struct member
// below declarations are private variables within HAL
nfc_stack_callback_t* p_cback;
nfc_stack_data_callback_t* p_data_cback;
HALHANDLE hHAL;
} st21nfc_dev_t;
typedef enum {
HAL_WRAPPER_STATE_CLOSED,
HAL_WRAPPER_STATE_OPEN,
HAL_WRAPPER_STATE_OPEN_CPLT,
HAL_WRAPPER_STATE_NFC_ENABLE_ON,
HAL_WRAPPER_STATE_PROP_CONFIG,
HAL_WRAPPER_STATE_READY,
HAL_WRAPPER_STATE_CLOSING
} hal_wrapper_state_e;
static void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data);
static void halWrapperCallback(uint8_t event, uint8_t event_status);
nfc_stack_callback_t* mHalWrapperCallback = NULL;
nfc_stack_data_callback_t* mHalWrapperDataCallback = NULL;
hal_wrapper_state_e mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
HALHANDLE mHalHandle = NULL;
bool mHciCreditLent = false;
bool hal_wrapper_open(st21nfc_dev_t* dev, nfc_stack_callback_t* p_cback,
nfc_stack_data_callback_t* p_data_cback,
HALHANDLE* pHandle) {
bool result;
STLOG_HAL_D("%s", __func__);
mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
mHciCreditLent = false;
mHalWrapperCallback = p_cback;
mHalWrapperDataCallback = p_data_cback;
dev->p_data_cback = halWrapperDataCallback;
dev->p_cback = halWrapperCallback;
result = I2cOpenLayer(dev, HalCoreCallback, pHandle);
if (!result || !(*pHandle)) {
return -1; // We are doomed, stop it here, NOW !
}
mHalHandle = *pHandle;
return 1;
}
int hal_wrapper_close(int call_cb, int nfc_mode) {
STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD(%d)", __func__, nfc_mode);
uint8_t propNfcModeSetCmdQb[] = {0x2f, 0x02, 0x02, 0x02, (uint8_t)nfc_mode};
mHalWrapperState = HAL_WRAPPER_STATE_CLOSING;
// Send PROP_NFC_MODE_SET_CMD
if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdQb,
sizeof(propNfcModeSetCmdQb), 100)) {
STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed", __func__);
return -1;
}
// Let the CLF receive and process this
usleep(50000);
I2cCloseLayer();
if (call_cb) mHalWrapperCallback(HAL_NFC_CLOSE_CPLT_EVT, HAL_NFC_STATUS_OK);
return 1;
}
void hal_wrapper_send_prop_config(){
uint8_t coreSetConfig[] = {0x20, 0x02, 0x07, 0x02, 0xa1, 0x01, 0x19, 0xa2, 0x01, 0x14};
//Send prop config and merge mode
STLOG_HAL_V("%s - Sending CORE_SET_CONFIG(LPS+Merge)", __func__);
if (!HalSendDownstream(mHalHandle, coreSetConfig, sizeof(coreSetConfig))) {
STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
}
mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
}
void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data) {
uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
STLOG_HAL_V("%s - mHalWrapperState = %d", __func__, mHalWrapperState);
switch (mHalWrapperState) {
case HAL_WRAPPER_STATE_CLOSED: // 0
break;
case HAL_WRAPPER_STATE_OPEN: // 1
// CORE_RESET_NTF
if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
if (p_data[3] == 0x01) {
mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_OK);
mHalWrapperState = HAL_WRAPPER_STATE_OPEN_CPLT;
} else if (p_data[3] == 0xa1) // Loader mode
{
mHalWrapperCallback(HAL_NFC_OPEN_CPLT_EVT, HAL_NFC_STATUS_FAILED);
}
} else {
mHalWrapperDataCallback(data_len, p_data);
}
break;
case HAL_WRAPPER_STATE_OPEN_CPLT: // 2
// CORE_INIT_RSP
if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
} else if ((p_data[0] == 0x60) && (p_data[1] == 0x06)) {
STLOG_HAL_V("%s - Sending PROP_NFC_MODE_SET_CMD", __func__);
// Send PROP_NFC_MODE_SET_CMD(ON)
if (!HalSendDownstreamTimer(mHalHandle, propNfcModeSetCmdOn,
sizeof(propNfcModeSetCmdOn), 100)) {
STLOG_HAL_E("NFC-NCI HAL: %s HalSendDownstreamTimer failed",
__func__);
}
mHalWrapperState = HAL_WRAPPER_STATE_NFC_ENABLE_ON;
} else {
mHalWrapperDataCallback(data_len, p_data);
}
break;
case HAL_WRAPPER_STATE_NFC_ENABLE_ON: // 3
// PROP_NFC_MODE_SET_RSP
if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
// DO nothing: wait for core_reset_ntf or timer timeout
}
// CORE_RESET_NTF
else if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
// Stop timer
HalSendDownstreamStopTimer(mHalHandle);
// Send CORE_INIT_CMD
STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
}
}
// CORE_INIT_RSP
else if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
STLOG_HAL_D("%s - NFC mode enabled", __func__);
// Do we need to lend a credit ?
if (p_data[13] == 0x00) {
STLOG_HAL_D("%s - 1 credit lent", __func__);
p_data[13] = 0x01;
mHciCreditLent = true;
}
mHalWrapperState = HAL_WRAPPER_STATE_READY;
mHalWrapperDataCallback(data_len, p_data);
}
break;
case HAL_WRAPPER_STATE_PROP_CONFIG: //4
if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) {
STLOG_HAL_V(
"%s - Received config RSP, deliver CORE_INIT_RSP to upper layer",
__func__);
mHalWrapperCallback(HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_OK);
mHalWrapperState = HAL_WRAPPER_STATE_READY;
} else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
if (p_data[4] == 0x01) { // HCI connection
mHciCreditLent = false;
STLOG_HAL_D("%s - credit returned", __func__);
if (p_data[5] == 0x01) {
// no need to send this.
break;
} else {
if (p_data[5] != 0x00 && p_data[5] != 0xFF) {
// send with 1 less
p_data[5]--;
}
}
}
mHalWrapperDataCallback(data_len, p_data);
}
break;
case HAL_WRAPPER_STATE_READY://5
if (!((p_data[0] == 0x60) && (p_data[3] == 0xa0))) {
if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
if (p_data[4] == 0x01) { // HCI connection
mHciCreditLent = false;
STLOG_HAL_D("%s - credit returned", __func__);
if (p_data[5] == 0x01) {
// no need to send this.
break;
} else {
if (p_data[5] != 0x00 && p_data[5] != 0xFF) {
// send with 1 less
p_data[5]--;
}
}
}
}
mHalWrapperDataCallback(data_len, p_data);
} else {
STLOG_HAL_V("%s - Core reset notification - Nfc mode ", __func__);
}
break;
case HAL_WRAPPER_STATE_CLOSING://6
if ((p_data[0] == 0x4f) && (p_data[1] == 0x02)) {
// intercept this expected message, don t forward.
mHalWrapperState = HAL_WRAPPER_STATE_CLOSED;
} else {
mHalWrapperDataCallback(data_len, p_data);
}
break;
}
}
static void halWrapperCallback(uint8_t event, uint8_t event_status) {
uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
switch (mHalWrapperState) {
case HAL_WRAPPER_STATE_CLOSED:
if (event == HAL_WRAPPER_TIMEOUT_EVT) {
STLOG_HAL_D("NFC-NCI HAL: %s Timeout. Close anyway", __func__);
HalSendDownstreamStopTimer(mHalHandle);
return;
}
break;
case HAL_WRAPPER_STATE_NFC_ENABLE_ON:
if (event == HAL_WRAPPER_TIMEOUT_EVT) {
// timeout
// Send CORE_INIT_CMD
STLOG_HAL_V("%s - Sending CORE_INIT_CMD", __func__);
if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
STLOG_HAL_E("NFC-NCI HAL: %s SendDownstream failed", __func__);
}
return;
}
break;
default:
break;
}
mHalWrapperCallback(event, event_status);
}