nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /* |
nxpandroid | 34627bd | 2016-05-27 15:52:30 +0530 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Suhas Suresh | e29f24d | 2018-04-25 16:33:03 +0530 | [diff] [blame] | 16 | #include <android-base/stringprintf.h> |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 17 | #include <base/logging.h> |
Ganesh Deva | 20b20a5 | 2019-09-04 16:48:11 +0530 | [diff] [blame] | 18 | #include <log/log.h> |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 19 | |
| 20 | #include <nfc_api.h> |
Nikhil Chhabra | 9ac2629 | 2018-01-10 11:57:21 +0530 | [diff] [blame] | 21 | #include <nfc_int.h> |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 22 | #include <phFriNfc_MifareStdTimer.h> |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 23 | #include <phNfcCompId.h> |
| 24 | #include <phNxpExtns_MifareStd.h> |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 25 | #include <rw_api.h> |
| 26 | #include <signal.h> |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 27 | |
Suhas Suresh | e29f24d | 2018-04-25 16:33:03 +0530 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | |
Suhas Suresh | fa0df86 | 2018-05-07 11:53:43 +0530 | [diff] [blame] | 30 | extern bool nfc_debug_enabled; |
| 31 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 32 | phNxpExtns_Context_t gphNxpExtns_Context; |
| 33 | phNciNfc_TransceiveInfo_t tNciTranscvInfo; |
| 34 | phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt = NULL; |
| 35 | phFriNfc_NdefMap_t* NdefMap = NULL; |
| 36 | phLibNfc_NdefInfo_t NdefInfo; |
| 37 | #if (NXP_EXTNS == TRUE) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 38 | pthread_mutex_t SharedDataMutex = PTHREAD_MUTEX_INITIALIZER; |
| 39 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 40 | uint8_t current_key[6] = {0}; |
| 41 | phNci_mfc_auth_cmd_t gAuthCmdBuf; |
| 42 | phFriNfc_MifareStdTimer_t mTimerInfo; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 43 | |
| 44 | STATIC NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo, |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 45 | uint8_t* buff, uint16_t* buffSz); |
| 46 | STATIC NFCSTATUS |
| 47 | phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 48 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf); |
| 49 | STATIC NFCSTATUS |
| 50 | phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 51 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf); |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 52 | STATIC NFCSTATUS phLibNfc_SendAuthCmd( |
| 53 | phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 54 | phNciNfc_TransceiveInfo_t* tNciTranscvInfo) __attribute__((unused)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 55 | STATIC NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType, |
| 56 | phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 57 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf); |
| 58 | STATIC NFCSTATUS |
| 59 | phLibNfc_MifareMap(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 60 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf); |
| 61 | STATIC NFCSTATUS |
| 62 | phLibNfc_ChkAuthCmdMFC(phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey); |
| 63 | STATIC NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey); |
| 64 | STATIC void phLibNfc_CalSectorAddress(uint8_t* Sector_Address); |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 65 | STATIC NFCSTATUS phNciNfc_MfCreateAuthCmdHdr( |
| 66 | phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t bBlockAddr, uint8_t* buff, |
| 67 | uint16_t* buffSz); |
| 68 | STATIC NFCSTATUS phNciNfc_MfCreateXchgDataHdr( |
| 69 | phNciNfc_TransceiveInfo_t tTranscvInfo, uint8_t* buff, uint16_t* buffSz); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 70 | STATIC NFCSTATUS |
| 71 | phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 72 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf); |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 73 | STATIC NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo, |
| 74 | NFCSTATUS wStatus); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 75 | STATIC NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz); |
| 76 | STATIC NFCSTATUS |
| 77 | phFriNfc_NdefSmtCrd_Reset__(phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, |
| 78 | uint8_t* SendRecvBuffer, uint16_t* SendRecvBuffLen); |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 79 | STATIC NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData, |
| 80 | uint32_t* PacketDataLength, |
| 81 | uint8_t Offset, |
| 82 | phFriNfc_NdefMap_t* pNdefMap, |
| 83 | uint8_t bNdefReq); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 84 | STATIC void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status); |
| 85 | STATIC void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status); |
| 86 | STATIC void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status); |
| 87 | STATIC void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 88 | STATIC void Mfc_CheckNdef_timeoutcb_Routine(union sigval); |
| 89 | |
| 90 | /******************************************************************************* |
| 91 | ** |
| 92 | ** Function phNxpExtns_MfcModuleDeInit |
| 93 | ** |
| 94 | ** Description It Deinitializes the Mifare module. |
| 95 | ** |
| 96 | ** Frees all the memory occupied by Mifare module |
| 97 | ** |
| 98 | ** Returns: |
| 99 | ** NFCSTATUS_SUCCESS - if successfully deinitialize |
| 100 | ** NFCSTATUS_FAILED - otherwise |
| 101 | ** |
| 102 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 103 | NFCSTATUS phNxpExtns_MfcModuleDeInit(void) { |
| 104 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 105 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 106 | if (NdefMap != NULL) { |
| 107 | if (NdefMap->psRemoteDevInfo != NULL) { |
| 108 | free(NdefMap->psRemoteDevInfo); |
| 109 | NdefMap->psRemoteDevInfo = NULL; |
| 110 | } |
| 111 | if (NdefMap->SendRecvBuf != NULL) { |
| 112 | free(NdefMap->SendRecvBuf); |
| 113 | NdefMap->SendRecvBuf = NULL; |
| 114 | } |
| 115 | if (NdefMap->SendRecvLength != NULL) { |
| 116 | free(NdefMap->SendRecvLength); |
| 117 | NdefMap->SendRecvLength = NULL; |
| 118 | } |
| 119 | if (NdefMap->DataCount != NULL) { |
| 120 | free(NdefMap->DataCount); |
| 121 | NdefMap->DataCount = NULL; |
| 122 | } |
| 123 | if (NdefMap->pTransceiveInfo != NULL) { |
| 124 | if (NdefMap->pTransceiveInfo->sSendData.buffer != NULL) { |
| 125 | free(NdefMap->pTransceiveInfo->sSendData.buffer); |
| 126 | NdefMap->pTransceiveInfo->sSendData.buffer = NULL; |
| 127 | } |
| 128 | if (NdefMap->pTransceiveInfo->sRecvData.buffer != NULL) { |
| 129 | free(NdefMap->pTransceiveInfo->sRecvData.buffer); |
| 130 | NdefMap->pTransceiveInfo->sRecvData.buffer = NULL; |
| 131 | } |
| 132 | free(NdefMap->pTransceiveInfo); |
| 133 | NdefMap->pTransceiveInfo = NULL; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 134 | } |
| 135 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 136 | free(NdefMap); |
| 137 | NdefMap = NULL; |
| 138 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 139 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 140 | if (tNciTranscvInfo.tSendData.pBuff != NULL) { |
| 141 | free(tNciTranscvInfo.tSendData.pBuff); |
| 142 | tNciTranscvInfo.tSendData.pBuff = NULL; |
| 143 | } |
| 144 | |
| 145 | if (NdefSmtCrdFmt != NULL) { |
| 146 | free(NdefSmtCrdFmt); |
| 147 | NdefSmtCrdFmt = NULL; |
| 148 | } |
| 149 | #if (NXP_EXTNS == TRUE) |
| 150 | pthread_mutex_lock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 151 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 152 | if (NULL != NdefInfo.psUpperNdefMsg) { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 153 | free(NdefInfo.psUpperNdefMsg); |
| 154 | NdefInfo.psUpperNdefMsg = NULL; |
| 155 | } |
| 156 | #if (NXP_EXTNS == TRUE) |
| 157 | pthread_mutex_unlock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 158 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 159 | if (NULL != gAuthCmdBuf.pauth_cmd) { |
| 160 | if (NULL != gAuthCmdBuf.pauth_cmd->buffer) { |
| 161 | free(gAuthCmdBuf.pauth_cmd->buffer); |
| 162 | gAuthCmdBuf.pauth_cmd->buffer = NULL; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 163 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 164 | free(gAuthCmdBuf.pauth_cmd); |
| 165 | gAuthCmdBuf.pauth_cmd = NULL; |
| 166 | } |
| 167 | status = NFCSTATUS_SUCCESS; |
| 168 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /******************************************************************************* |
| 172 | ** |
| 173 | ** Function phNxpExtns_MfcModuleInit |
| 174 | ** |
| 175 | ** Description It Initializes the memroy and global variables related |
| 176 | ** to Mifare module. |
| 177 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 178 | ** Reset all the global variables and allocate memory for |
| 179 | *Mifare module |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 180 | ** |
| 181 | ** Returns: |
| 182 | ** NFCSTATUS_SUCCESS - if successfully deinitialize |
| 183 | ** NFCSTATUS_FAILED - otherwise |
| 184 | ** |
| 185 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 186 | NFCSTATUS phNxpExtns_MfcModuleInit(void) { |
| 187 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 188 | gphNxpExtns_Context.writecmdFlag = false; |
| 189 | gphNxpExtns_Context.RawWriteCallBack = false; |
| 190 | gphNxpExtns_Context.CallBackCtxt = NULL; |
| 191 | gphNxpExtns_Context.CallBackMifare = NULL; |
| 192 | gphNxpExtns_Context.ExtnsConnect = false; |
| 193 | gphNxpExtns_Context.ExtnsDeactivate = false; |
| 194 | gphNxpExtns_Context.ExtnsCallBack = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 195 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 196 | NdefMap = (phFriNfc_NdefMap_t *)malloc(sizeof(phFriNfc_NdefMap_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 197 | if (NULL == NdefMap) { |
| 198 | goto clean_and_return; |
| 199 | } |
| 200 | memset(NdefMap, 0, sizeof(phFriNfc_NdefMap_t)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 201 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 202 | NdefMap->psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t *)malloc(sizeof(phLibNfc_sRemoteDevInformation_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 203 | if (NULL == NdefMap->psRemoteDevInfo) { |
| 204 | goto clean_and_return; |
| 205 | } |
| 206 | memset(NdefMap->psRemoteDevInfo, 0, sizeof(phLibNfc_sRemoteDevInformation_t)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 207 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 208 | NdefMap->SendRecvBuf = (uint8_t *)malloc((uint32_t)(MAX_BUFF_SIZE * 2)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 209 | if (NULL == NdefMap->SendRecvBuf) { |
| 210 | goto clean_and_return; |
| 211 | } |
| 212 | memset(NdefMap->SendRecvBuf, 0, (MAX_BUFF_SIZE * 2)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 213 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 214 | NdefMap->SendRecvLength = (uint16_t *)malloc(sizeof(uint16_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 215 | if (NULL == NdefMap->SendRecvLength) { |
| 216 | goto clean_and_return; |
| 217 | } |
| 218 | memset(NdefMap->SendRecvLength, 0, sizeof(uint16_t)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 219 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 220 | NdefMap->DataCount = (uint16_t *)malloc(sizeof(uint16_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 221 | if (NULL == NdefMap->DataCount) { |
| 222 | goto clean_and_return; |
| 223 | } |
| 224 | memset(NdefMap->DataCount, 0, sizeof(uint16_t)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 225 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 226 | NdefMap->pTransceiveInfo = (phNfc_sTransceiveInfo_t *)malloc(sizeof(phNfc_sTransceiveInfo_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 227 | if (NULL == NdefMap->pTransceiveInfo) { |
| 228 | goto clean_and_return; |
| 229 | } |
| 230 | memset(NdefMap->pTransceiveInfo, 0, sizeof(phNfc_sTransceiveInfo_t)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 231 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 232 | tNciTranscvInfo.tSendData.pBuff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE); |
| 233 | if (NULL == tNciTranscvInfo.tSendData.pBuff) { |
| 234 | goto clean_and_return; |
| 235 | } |
| 236 | memset(tNciTranscvInfo.tSendData.pBuff, 0, MAX_BUFF_SIZE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 237 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 238 | NdefMap->pTransceiveInfo->sSendData.buffer = |
| 239 | (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE); |
| 240 | if (NdefMap->pTransceiveInfo->sSendData.buffer == NULL) { |
| 241 | goto clean_and_return; |
| 242 | } |
| 243 | memset(NdefMap->pTransceiveInfo->sSendData.buffer, 0, MAX_BUFF_SIZE); |
| 244 | NdefMap->pTransceiveInfo->sSendData.length = MAX_BUFF_SIZE; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 245 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 246 | NdefMap->pTransceiveInfo->sRecvData.buffer = (uint8_t*)malloc( |
| 247 | (uint32_t)MAX_BUFF_SIZE); /* size should be same as sRecvData */ |
| 248 | if (NdefMap->pTransceiveInfo->sRecvData.buffer == NULL) { |
| 249 | goto clean_and_return; |
| 250 | } |
| 251 | memset(NdefMap->pTransceiveInfo->sRecvData.buffer, 0, MAX_BUFF_SIZE); |
| 252 | NdefMap->pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 253 | |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 254 | NdefSmtCrdFmt = (phFriNfc_sNdefSmtCrdFmt_t *)malloc(sizeof(phFriNfc_sNdefSmtCrdFmt_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 255 | if (NdefSmtCrdFmt == NULL) { |
| 256 | goto clean_and_return; |
| 257 | } |
| 258 | memset(NdefSmtCrdFmt, 0, sizeof(phFriNfc_sNdefSmtCrdFmt_t)); |
| 259 | #if (NXP_EXTNS == TRUE) |
| 260 | pthread_mutex_lock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 261 | #endif |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 262 | NdefInfo.psUpperNdefMsg = (phNfc_sData_t *) malloc(sizeof(phNfc_sData_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 263 | if (NULL == NdefInfo.psUpperNdefMsg) { |
| 264 | goto clean_and_return; |
| 265 | } |
| 266 | memset(NdefInfo.psUpperNdefMsg, 0, sizeof(phNfc_sData_t)); |
| 267 | memset(&gAuthCmdBuf, 0, sizeof(phNci_mfc_auth_cmd_t)); |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 268 | gAuthCmdBuf.pauth_cmd = (phNfc_sData_t *)malloc(sizeof(phNfc_sData_t)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 269 | if (NULL == gAuthCmdBuf.pauth_cmd) { |
| 270 | goto clean_and_return; |
| 271 | } |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 272 | gAuthCmdBuf.pauth_cmd->buffer = (uint8_t *)malloc((uint32_t)NCI_MAX_DATA_LEN); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 273 | if (NULL == gAuthCmdBuf.pauth_cmd->buffer) { |
| 274 | goto clean_and_return; |
| 275 | } |
| 276 | status = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 277 | |
| 278 | clean_and_return: |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 279 | #if (NXP_EXTNS == TRUE) |
| 280 | pthread_mutex_unlock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 281 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 282 | if (status != NFCSTATUS_SUCCESS) { |
| 283 | LOG(ERROR) << StringPrintf("CRIT: Memory Allocation failed for MFC!"); |
| 284 | phNxpExtns_MfcModuleDeInit(); |
| 285 | } |
| 286 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /******************************************************************************* |
| 290 | ** |
| 291 | ** Function Mfc_CheckNdef |
| 292 | ** |
| 293 | ** Description It triggers NDEF detection for Mifare Classic Tag. |
| 294 | ** |
| 295 | ** |
| 296 | ** Returns NFCSTATUS_SUCCESS - if successfully initiated |
| 297 | ** NFCSTATUS_FAILED - otherwise |
| 298 | ** |
| 299 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 300 | NFCSTATUS Mfc_CheckNdef(void) { |
| 301 | NFCSTATUS status = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 302 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 303 | EXTNS_SetCallBackFlag(false); |
| 304 | /* Set Completion Routine for CheckNdef */ |
| 305 | NdefMap->CompletionRoutine[0].CompletionRoutine = |
| 306 | Mfc_CheckNdef_Completion_Routine; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 307 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 308 | gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process; |
| 309 | gphNxpExtns_Context.CallBackCtxt = NdefMap; |
| 310 | status = phFriNfc_MifareStdMap_H_Reset(NdefMap); |
| 311 | if (NFCSTATUS_SUCCESS == status) { |
| 312 | status = phFriNfc_MifareStdMap_ChkNdef(NdefMap); |
| 313 | if (status == NFCSTATUS_PENDING) { |
| 314 | status = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 315 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 316 | } |
| 317 | /*Start a timer for MIFARE Check Ndef response callback handler*/ |
| 318 | if (NFCSTATUS_SUCCESS == status) { |
| 319 | memset(&mTimerInfo, 0, sizeof(mTimerInfo)); |
| 320 | mTimerInfo.mCb = Mfc_CheckNdef_timeoutcb_Routine; |
| 321 | mTimerInfo.mtimeout = (uint32_t)PH_FRINFC_CHECK_NDEF_TIMEOUT; |
| 322 | status = phFriNfc_MifareStd_StartTimer(&mTimerInfo); |
| 323 | } |
| 324 | if (status != NFCSTATUS_SUCCESS) { |
| 325 | status = NFCSTATUS_FAILED; |
| 326 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 327 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 328 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /******************************************************************************* |
| 332 | ** |
| 333 | ** Function Mfc_CheckNdef_Completion_Routine |
| 334 | ** |
| 335 | ** Description Notify NDEF detection for Mifare Classic Tag to JNI |
| 336 | ** |
| 337 | ** Upon completion of NDEF detection, a |
| 338 | ** NFA_NDEF_DETECT_EVT will be sent, to notify the application |
| 339 | ** of the NDEF attributes (NDEF total memory size, current |
| 340 | ** size, etc.). |
| 341 | ** |
| 342 | ** Returns: void |
| 343 | ** |
| 344 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 345 | STATIC void Mfc_CheckNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) { |
| 346 | (void)NdefCtxt; |
| 347 | tNFA_CONN_EVT_DATA conn_evt_data; |
| 348 | NFCSTATUS timer_status = NFCSTATUS_FAILED; |
| 349 | conn_evt_data.ndef_detect.status = status; |
| 350 | // stopping checkndef timer if running |
| 351 | timer_status = phFriNfc_MifareStd_StopTimer(&mTimerInfo); |
| 352 | if (timer_status != NFCSTATUS_SUCCESS) { |
| 353 | LOG(ERROR) << StringPrintf("Failed to stop timer"); |
| 354 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 355 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 356 | if (NFCSTATUS_SUCCESS == status) { |
| 357 | /* NDef Tag Detected */ |
| 358 | conn_evt_data.ndef_detect.protocol = NFC_PROTOCOL_MIFARE; |
| 359 | phFrinfc_MifareClassic_GetContainerSize( |
| 360 | NdefMap, (uint32_t*)&(conn_evt_data.ndef_detect.max_size), |
| 361 | (uint32_t*)&(conn_evt_data.ndef_detect.cur_size)); |
| 362 | NdefInfo.NdefLength = conn_evt_data.ndef_detect.max_size; |
| 363 | /* update local flags */ |
| 364 | NdefInfo.is_ndef = 1; |
| 365 | NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size; |
| 366 | if (PH_NDEFMAP_CARD_STATE_READ_ONLY == NdefMap->CardState) { |
Ganesh Deva | 15e35d4 | 2020-02-18 10:40:37 +0530 | [diff] [blame] | 367 | DLOG_IF(INFO, nfc_debug_enabled) |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 368 | << StringPrintf("Mfc_CheckNdef_Completion_Routine : READ_ONLY_CARD"); |
| 369 | conn_evt_data.ndef_detect.flags = RW_NDEF_FL_READ_ONLY; |
| 370 | } else { |
| 371 | conn_evt_data.ndef_detect.flags = |
| 372 | RW_NDEF_FL_SUPPORTED | RW_NDEF_FL_FORMATED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 373 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 374 | } else { |
| 375 | /* NDEF Detection failed for other reasons */ |
| 376 | conn_evt_data.ndef_detect.cur_size = 0; |
| 377 | conn_evt_data.ndef_detect.max_size = 0; |
| 378 | conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 379 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 380 | /* update local flags */ |
| 381 | NdefInfo.is_ndef = 0; |
| 382 | NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size; |
| 383 | } |
| 384 | (*gphNxpExtns_Context.p_conn_cback)(NFA_NDEF_DETECT_EVT, &conn_evt_data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 385 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 386 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 387 | } |
| 388 | /******************************************************************************* |
| 389 | ** |
| 390 | ** Function Mfc_ReadNdef_Completion_Routine |
| 391 | ** |
| 392 | ** Description Notify NDEF read completion for Mifare Classic Tag to JNI |
| 393 | ** |
| 394 | ** Upon completion of NDEF read, a |
| 395 | ** NFA_READ_CPLT_EVT will be sent, to notify the application |
| 396 | ** with the NDEF data and status |
| 397 | ** |
| 398 | ** Returns: void |
| 399 | ** |
| 400 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 401 | STATIC void Mfc_ReadNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) { |
| 402 | (void)NdefCtxt; |
| 403 | tNFA_CONN_EVT_DATA conn_evt_data; |
| 404 | tNFA_NDEF_EVT_DATA p_data; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 405 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 406 | conn_evt_data.status = status; |
| 407 | #if (NXP_EXTNS == TRUE) |
| 408 | pthread_mutex_lock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 409 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 410 | if (NFCSTATUS_SUCCESS == status) { |
| 411 | p_data.ndef_data.len = NdefInfo.psUpperNdefMsg->length; |
| 412 | p_data.ndef_data.p_data = NdefInfo.psUpperNdefMsg->buffer; |
| 413 | (*gphNxpExtns_Context.p_ndef_cback)(NFA_NDEF_DATA_EVT, &p_data); |
| 414 | } else { |
| 415 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 416 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 417 | (*gphNxpExtns_Context.p_conn_cback)(NFA_READ_CPLT_EVT, &conn_evt_data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 418 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 419 | if (NdefInfo.psUpperNdefMsg->buffer != NULL) { |
| 420 | free(NdefInfo.psUpperNdefMsg->buffer); |
| 421 | NdefInfo.psUpperNdefMsg->buffer = NULL; |
| 422 | } |
| 423 | #if (NXP_EXTNS == TRUE) |
| 424 | pthread_mutex_unlock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 425 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 426 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /******************************************************************************* |
| 430 | ** |
| 431 | ** Function Mfc_WriteNdef_Completion_Routine |
| 432 | ** |
| 433 | ** Description Notify NDEF write completion for Mifare Classic Tag to JNI |
| 434 | ** |
| 435 | ** Upon completion of NDEF write, a |
| 436 | ** NFA_WRITE_CPLT_EVT will be sent along with status |
| 437 | ** |
| 438 | ** Returns: void |
| 439 | ** |
| 440 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 441 | STATIC void Mfc_WriteNdef_Completion_Routine(void* NdefCtxt, NFCSTATUS status) { |
| 442 | (void)NdefCtxt; |
| 443 | tNFA_CONN_EVT_DATA conn_evt_data; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 444 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 445 | conn_evt_data.status = status; |
| 446 | (*gphNxpExtns_Context.p_conn_cback)(NFA_WRITE_CPLT_EVT, &conn_evt_data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 447 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 448 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /******************************************************************************* |
| 452 | ** |
| 453 | ** Function Mfc_FormatNdef_Completion_Routine |
| 454 | ** |
| 455 | ** Description Notify NDEF format completion for Mifare Classic Tag to JNI |
| 456 | ** |
| 457 | ** Upon completion of NDEF format, a |
| 458 | ** NFA_FORMAT_CPLT_EVT will be sent along with status |
| 459 | ** |
| 460 | ** Returns: void |
| 461 | ** |
| 462 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 463 | STATIC void Mfc_FormatNdef_Completion_Routine(void* NdefCtxt, |
| 464 | NFCSTATUS status) { |
| 465 | (void)NdefCtxt; |
| 466 | tNFA_CONN_EVT_DATA conn_evt_data; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 467 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 468 | conn_evt_data.status = status; |
| 469 | (*gphNxpExtns_Context.p_conn_cback)(NFA_FORMAT_CPLT_EVT, &conn_evt_data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 470 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 471 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | /******************************************************************************* |
| 475 | ** |
| 476 | ** Function phFriNfc_ValidateParams |
| 477 | ** |
| 478 | ** Description This function is a common function which validates NdefRd |
| 479 | ** and NdefWr parameters. |
| 480 | ** |
| 481 | ** Returns NFCSTATUS_SUCCESS - All the params are valid |
| 482 | ** NFCSTATUS_FAILED - otherwise |
| 483 | ** |
| 484 | *******************************************************************************/ |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 485 | STATIC NFCSTATUS phFriNfc_ValidateParams(uint8_t* PacketData, |
| 486 | uint32_t* PacketDataLength, |
| 487 | uint8_t Offset, |
| 488 | phFriNfc_NdefMap_t* pNdefMap, |
| 489 | uint8_t bNdefReq) { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 490 | if ((pNdefMap == NULL) || (PacketData == NULL) || |
| 491 | (PacketDataLength == NULL)) { |
| 492 | return NFCSTATUS_FAILED; |
| 493 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 494 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 495 | if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INVALID) { |
| 496 | return NFCSTATUS_FAILED; |
| 497 | } |
| 498 | |
| 499 | if (bNdefReq == PH_FRINFC_NDEF_READ_REQ) { |
| 500 | if ((Offset != PH_FRINFC_NDEFMAP_SEEK_CUR) && |
| 501 | (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) { |
| 502 | return NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 503 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 504 | if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_INITIALIZED) { |
| 505 | pNdefMap->NumOfBytesRead = PacketDataLength; |
| 506 | *pNdefMap->NumOfBytesRead = 0; |
| 507 | return NFCSTATUS_EOF_NDEF_CONTAINER_REACHED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 508 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 509 | if ((pNdefMap->PrevOperation == PH_FRINFC_NDEFMAP_WRITE_OPE) && |
| 510 | (Offset != PH_FRINFC_NDEFMAP_SEEK_BEGIN)) { |
| 511 | return NFCSTATUS_FAILED; /* return INVALID_DEVICE_REQUEST */ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 512 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 513 | if (Offset == PH_FRINFC_NDEFMAP_SEEK_BEGIN) { |
| 514 | pNdefMap->ApduBuffIndex = 0; |
| 515 | *pNdefMap->DataCount = 0; |
| 516 | } else if ((pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_BEGIN) || |
| 517 | (pNdefMap->bPrevReadMode == PH_FRINFC_NDEFMAP_SEEK_CUR)) { |
| 518 | } else { |
| 519 | return NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 520 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 521 | } else if (bNdefReq == PH_FRINFC_NDEF_WRITE_REQ) { |
| 522 | if (pNdefMap->CardState == PH_NDEFMAP_CARD_STATE_READ_ONLY) { |
| 523 | pNdefMap->WrNdefPacketLength = PacketDataLength; |
| 524 | *pNdefMap->WrNdefPacketLength = 0x00; |
| 525 | return NFCSTATUS_NOT_ALLOWED; |
| 526 | } |
| 527 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 528 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 529 | return NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /******************************************************************************* |
| 533 | ** |
| 534 | ** Function Mfc_SetRdOnly_Completion_Routine |
| 535 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 536 | ** Description Notify NDEF read only completion for Mifare Classic Tag to |
| 537 | *JNI |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 538 | ** |
| 539 | ** Upon completion of NDEF format, a |
| 540 | ** NFA_SET_TAG_RO_EVT will be sent along with status |
| 541 | ** |
| 542 | ** Returns: void |
| 543 | ** |
| 544 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 545 | STATIC void Mfc_SetRdOnly_Completion_Routine(void* NdefCtxt, NFCSTATUS status) { |
| 546 | (void)NdefCtxt; |
| 547 | tNFA_CONN_EVT_DATA conn_evt_data; |
| 548 | LOG(ERROR) << StringPrintf("%s status = 0x%x", __func__, status); |
| 549 | conn_evt_data.status = status; |
| 550 | (*gphNxpExtns_Context.p_conn_cback)(NFA_SET_TAG_RO_EVT, &conn_evt_data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 551 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 552 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /******************************************************************************* |
| 556 | ** |
| 557 | ** Function Mfc_SetReadOnly |
| 558 | ** |
| 559 | ** |
| 560 | ** Description: It triggers ConvertToReadOnly for Mifare Classic Tag. |
| 561 | ** |
| 562 | ** Returns: |
| 563 | ** NFCSTATUS_SUCCESS if successfully initiated |
| 564 | ** NFCSTATUS_FAILED otherwise |
| 565 | ** |
| 566 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 567 | NFCSTATUS Mfc_SetReadOnly(uint8_t* secrtkey, uint8_t len) { |
Ganesh Deva | 15e35d4 | 2020-02-18 10:40:37 +0530 | [diff] [blame] | 568 | DLOG_IF(INFO, nfc_debug_enabled) |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 569 | << StringPrintf("%s Entering ", __func__); |
| 570 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 571 | uint8_t mif_secrete_key[6] = {0}; |
| 572 | uint8_t id = 0; |
| 573 | EXTNS_SetCallBackFlag(false); |
| 574 | memcpy(mif_secrete_key, secrtkey, len); |
| 575 | gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process; |
| 576 | gphNxpExtns_Context.CallBackCtxt = NdefMap; |
| 577 | for (id = 0; id < len; id++) { |
| 578 | DLOG_IF(INFO, nfc_debug_enabled) |
| 579 | << StringPrintf("secrtkey[%d] = 0x%x", id, secrtkey[id]); |
| 580 | DLOG_IF(INFO, nfc_debug_enabled) |
| 581 | << StringPrintf("mif_secrete_key[%d] = 0x%x", id, mif_secrete_key[id]); |
| 582 | } |
| 583 | /* Set Completion Routine for ReadNdef */ |
| 584 | NdefMap->CompletionRoutine[0].CompletionRoutine = |
| 585 | Mfc_SetRdOnly_Completion_Routine; |
| 586 | if (NdefInfo.is_ndef == 0) { |
| 587 | status = NFCSTATUS_NON_NDEF_COMPLIANT; |
| 588 | goto Mfc_SetRdOnly; |
| 589 | } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) { |
| 590 | #if (NXP_EXTNS == TRUE) |
| 591 | pthread_mutex_lock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 592 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 593 | NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize; |
| 594 | #if (NXP_EXTNS == TRUE) |
| 595 | pthread_mutex_unlock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 596 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 597 | status = NFCSTATUS_SUCCESS; |
| 598 | goto Mfc_SetRdOnly; |
| 599 | } else { |
| 600 | status = phFriNfc_MifareStdMap_ConvertToReadOnly(NdefMap, mif_secrete_key); |
| 601 | } |
| 602 | if (NFCSTATUS_PENDING == status) { |
| 603 | status = NFCSTATUS_SUCCESS; |
| 604 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 605 | |
| 606 | Mfc_SetRdOnly: |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 607 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /******************************************************************************* |
| 611 | ** |
| 612 | ** Function Mfc_ReadNdef |
| 613 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 614 | ** Description It triggers receiving of the NDEF message from Mifare |
| 615 | *Classic Tag. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 616 | ** |
| 617 | ** |
| 618 | ** Returns: |
| 619 | ** NFCSTATUS_SUCCESS - if successfully initiated |
| 620 | ** NFCSTATUS_FAILED - otherwise |
| 621 | ** |
| 622 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 623 | NFCSTATUS Mfc_ReadNdef(void) { |
| 624 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 625 | uint8_t* PacketData = NULL; |
| 626 | uint32_t* PacketDataLength = NULL; |
| 627 | phLibNfc_Ndef_EOffset_t Offset; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 628 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 629 | EXTNS_SetCallBackFlag(false); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 630 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 631 | Offset = phLibNfc_Ndef_EBegin; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 632 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 633 | gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process; |
| 634 | gphNxpExtns_Context.CallBackCtxt = NdefMap; |
| 635 | #if (NXP_EXTNS == TRUE) |
| 636 | pthread_mutex_lock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 637 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 638 | if (NdefInfo.is_ndef == 0) { |
| 639 | status = NFCSTATUS_NON_NDEF_COMPLIANT; |
| 640 | goto Mfc_RdNdefEnd; |
| 641 | } else if ((NdefInfo.is_ndef == 1) && (NdefInfo.NdefActualSize == 0)) { |
| 642 | NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize; |
| 643 | status = NFCSTATUS_SUCCESS; |
| 644 | goto Mfc_RdNdefEnd; |
| 645 | } else { |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 646 | NdefInfo.psUpperNdefMsg->buffer = (uint8_t *)malloc(NdefInfo.NdefActualSize); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 647 | if (NULL == NdefInfo.psUpperNdefMsg->buffer) { |
| 648 | goto Mfc_RdNdefEnd; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 649 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 650 | NdefInfo.psUpperNdefMsg->length = NdefInfo.NdefActualSize; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 651 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 652 | /* Set Completion Routine for ReadNdef */ |
| 653 | NdefMap->CompletionRoutine[1].CompletionRoutine = |
| 654 | Mfc_ReadNdef_Completion_Routine; |
nxf50051 | cd8d582 | 2019-03-20 15:32:57 +0530 | [diff] [blame] | 655 | NdefInfo.NdefContinueRead = (uint8_t)PH_FRINFC_NDEFMAP_SEEK_BEGIN; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 656 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 657 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 658 | PacketData = NdefInfo.psUpperNdefMsg->buffer; |
| 659 | PacketDataLength = (uint32_t*)&(NdefInfo.psUpperNdefMsg->length); |
| 660 | NdefMap->bCurrReadMode = Offset; |
| 661 | status = phFriNfc_ValidateParams(PacketData, PacketDataLength, Offset, |
| 662 | NdefMap, PH_FRINFC_NDEF_READ_REQ); |
| 663 | if (status != NFCSTATUS_SUCCESS) { |
| 664 | goto Mfc_RdNdefEnd; |
| 665 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 666 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 667 | status = phFriNfc_MifareStdMap_RdNdef(NdefMap, PacketData, PacketDataLength, |
| 668 | Offset); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 669 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 670 | if (NFCSTATUS_INSUFFICIENT_STORAGE == status) { |
| 671 | NdefInfo.psUpperNdefMsg->length = 0x00; |
| 672 | status = NFCSTATUS_SUCCESS; |
| 673 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 674 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 675 | if (NFCSTATUS_PENDING == status) { |
| 676 | status = NFCSTATUS_SUCCESS; |
| 677 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 678 | |
| 679 | Mfc_RdNdefEnd: |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 680 | if (status != NFCSTATUS_SUCCESS) { |
| 681 | if (NULL != NdefInfo.psUpperNdefMsg->buffer) { |
| 682 | free(NdefInfo.psUpperNdefMsg->buffer); |
| 683 | NdefInfo.psUpperNdefMsg->buffer = NULL; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 684 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 685 | status = NFCSTATUS_FAILED; |
| 686 | } |
| 687 | #if (NXP_EXTNS == TRUE) |
| 688 | pthread_mutex_unlock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 689 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 690 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 691 | } |
| 692 | /******************************************************************************* |
| 693 | ** |
| 694 | ** Function Mfc_PresenceCheck |
| 695 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 696 | ** Description It triggers receiving of the NDEF message from Mifare |
| 697 | *Classic Tag. |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 698 | ** |
| 699 | ** |
| 700 | ** Returns: |
| 701 | ** NFCSTATUS_SUCCESS - if successfully initiated |
| 702 | ** NFCSTATUS_FAILED - otherwise |
| 703 | ** |
| 704 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 705 | NFCSTATUS Mfc_PresenceCheck(void) { |
| 706 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 707 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 708 | if (gAuthCmdBuf.auth_status == true) { |
| 709 | EXTNS_SetCallBackFlag(false); |
| 710 | status = nativeNfcExtns_doTransceive(gAuthCmdBuf.pauth_cmd->buffer, |
| 711 | gAuthCmdBuf.pauth_cmd->length); |
| 712 | if (status != NFCSTATUS_PENDING) { |
| 713 | gAuthCmdBuf.auth_sent = false; |
| 714 | status = NFCSTATUS_FAILED; |
| 715 | } else { |
| 716 | gAuthCmdBuf.auth_sent = true; |
| 717 | status = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 718 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 719 | } else { |
| 720 | status = NFCSTATUS_NOT_ALLOWED; |
| 721 | } |
Ganesh Deva | 15e35d4 | 2020-02-18 10:40:37 +0530 | [diff] [blame] | 722 | DLOG_IF(INFO, nfc_debug_enabled) |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 723 | << StringPrintf("%s status = 0x%x", __func__, status); |
| 724 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 725 | } |
| 726 | /******************************************************************************* |
| 727 | ** |
| 728 | ** Function Mfc_WriteNdef |
| 729 | ** |
| 730 | ** Description It triggers the NDEF data write to Mifare Classic Tag. |
| 731 | ** |
| 732 | ** |
| 733 | ** Returns: |
| 734 | ** NFCSTATUS_SUCCESS - if successfully initiated |
| 735 | ** NFCSTATUS_FAILED - otherwise |
| 736 | ** |
| 737 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 738 | NFCSTATUS Mfc_WriteNdef(uint8_t* p_data, uint32_t len) { |
| 739 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
| 740 | uint8_t* PacketData = NULL; |
| 741 | uint32_t* PacketDataLength = NULL; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 742 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 743 | if (p_data == NULL || len == 0) { |
| 744 | LOG(ERROR) << StringPrintf("MFC Error: Invalid Parameters to Ndef Write"); |
| 745 | status = NFCSTATUS_FAILED; |
| 746 | goto Mfc_WrNdefEnd; |
| 747 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 748 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 749 | EXTNS_SetCallBackFlag(false); |
| 750 | gphNxpExtns_Context.CallBackMifare = phFriNfc_MifareStdMap_Process; |
| 751 | gphNxpExtns_Context.CallBackCtxt = NdefMap; |
| 752 | #if (NXP_EXTNS == TRUE) |
| 753 | pthread_mutex_lock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 754 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 755 | if (NdefInfo.is_ndef == PH_LIBNFC_INTERNAL_CHK_NDEF_NOT_DONE) { |
| 756 | status = NFCSTATUS_REJECTED; |
| 757 | goto Mfc_WrNdefEnd; |
| 758 | } else if (NdefInfo.is_ndef == 0) { |
| 759 | status = NFCSTATUS_NON_NDEF_COMPLIANT; |
| 760 | goto Mfc_WrNdefEnd; |
| 761 | } else if (len > NdefInfo.NdefLength) { |
| 762 | status = NFCSTATUS_NOT_ENOUGH_MEMORY; |
| 763 | goto Mfc_WrNdefEnd; |
| 764 | } else { |
| 765 | NdefInfo.psUpperNdefMsg->buffer = p_data; |
| 766 | NdefInfo.psUpperNdefMsg->length = len; |
| 767 | |
| 768 | NdefInfo.AppWrLength = len; |
| 769 | NdefMap->CompletionRoutine[2].CompletionRoutine = |
| 770 | Mfc_WriteNdef_Completion_Routine; |
| 771 | if (0 == len) { |
| 772 | /* TODO: Erase the Tag */ |
| 773 | } else { |
| 774 | NdefMap->ApduBuffIndex = 0x00; |
| 775 | *NdefMap->DataCount = 0x00; |
| 776 | PacketData = NdefInfo.psUpperNdefMsg->buffer; |
| 777 | PacketDataLength = &(NdefInfo.dwWrLength); |
| 778 | NdefMap->WrNdefPacketLength = PacketDataLength; |
| 779 | NdefInfo.dwWrLength = len; |
| 780 | |
| 781 | status = phFriNfc_ValidateParams(PacketData, PacketDataLength, 0, NdefMap, |
| 782 | PH_FRINFC_NDEF_WRITE_REQ); |
| 783 | if (status != NFCSTATUS_SUCCESS) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 784 | goto Mfc_WrNdefEnd; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 785 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 786 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 787 | status = phFriNfc_MifareStdMap_WrNdef( |
| 788 | NdefMap, PacketData, PacketDataLength, PH_FRINFC_NDEFMAP_SEEK_BEGIN); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 789 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 790 | if (status == NFCSTATUS_PENDING) { |
| 791 | status = NFCSTATUS_SUCCESS; |
| 792 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 793 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 794 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 795 | |
| 796 | Mfc_WrNdefEnd: |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 797 | #if (NXP_EXTNS == TRUE) |
| 798 | pthread_mutex_unlock(&SharedDataMutex); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 799 | #endif |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 800 | if (status != NFCSTATUS_SUCCESS) { |
| 801 | status = NFCSTATUS_FAILED; |
| 802 | } |
| 803 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 804 | } |
| 805 | /******************************************************************************* |
| 806 | ** |
| 807 | ** Function phFriNfc_NdefSmtCrd_Reset__ |
| 808 | ** |
| 809 | ** Description This function Resets the component instance to the initial |
| 810 | ** state and initializes the internal variables. |
| 811 | ** |
| 812 | ** Returns NFCSTATUS_SUCCESS |
| 813 | ** |
| 814 | *******************************************************************************/ |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 815 | STATIC NFCSTATUS phFriNfc_NdefSmtCrd_Reset__( |
| 816 | phFriNfc_sNdefSmtCrdFmt_t* NdefSmtCrdFmt, uint8_t* SendRecvBuffer, |
| 817 | uint16_t* SendRecvBuffLen) { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 818 | // NFCSTATUS status = NFCSTATUS_FAILED; /*commented to |
| 819 | // eliminate unused variable warning*/ |
| 820 | uint8_t index; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 821 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 822 | /* Initialize the state to Init */ |
| 823 | NdefSmtCrdFmt->State = PH_FRINFC_SMTCRDFMT_STATE_RESET_INIT; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 824 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 825 | for (index = 0; index < PH_FRINFC_SMTCRDFMT_CR; index++) { |
| 826 | /* Initialize the NdefMap Completion Routine to Null */ |
| 827 | NdefSmtCrdFmt->CompletionRoutine[index].CompletionRoutine = NULL; |
| 828 | /* Initialize the NdefMap Completion Routine context to Null */ |
| 829 | NdefSmtCrdFmt->CompletionRoutine[index].Context = NULL; |
| 830 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 831 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 832 | /* Trx Buffer registered */ |
| 833 | NdefSmtCrdFmt->SendRecvBuf = SendRecvBuffer; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 834 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 835 | /* Trx Buffer Size */ |
| 836 | NdefSmtCrdFmt->SendRecvLength = SendRecvBuffLen; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 837 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 838 | /* Register Transfer Buffer Length */ |
| 839 | NdefSmtCrdFmt->SendLength = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 840 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 841 | /* Initialize the Format status flag*/ |
| 842 | NdefSmtCrdFmt->FmtProcStatus = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 843 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 844 | /* Reset the Card Type */ |
| 845 | NdefSmtCrdFmt->CardType = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 846 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 847 | /* Reset MapCompletion Info*/ |
| 848 | NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.CompletionRoutine = NULL; |
| 849 | NdefSmtCrdFmt->SmtCrdFmtCompletionInfo.Context = NULL; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 850 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 851 | phFriNfc_MfStd_Reset(NdefSmtCrdFmt); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 852 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 853 | return NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /******************************************************************************* |
| 857 | ** |
| 858 | ** Function Mfc_FormatNdef |
| 859 | ** |
| 860 | ** Description It triggers the NDEF format of Mifare Classic Tag. |
| 861 | ** |
| 862 | ** |
| 863 | ** Returns: |
| 864 | ** NFCSTATUS_SUCCESS - if successfully initiated |
| 865 | ** NFCSTATUS_FAILED - otherwise |
| 866 | ** |
| 867 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 868 | NFCSTATUS Mfc_FormatNdef(uint8_t* secretkey, uint8_t len) { |
| 869 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 870 | uint8_t mif_std_key[6] = {0}; |
| 871 | // static uint8_t Index; |
| 872 | // /*commented to eliminate unused variable warning*/ |
| 873 | uint8_t sak = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 874 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 875 | EXTNS_SetCallBackFlag(false); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 876 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 877 | memcpy(mif_std_key, secretkey, len); |
| 878 | memcpy(current_key, secretkey, len); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 879 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 880 | if (NULL == NdefSmtCrdFmt || NULL == NdefMap || |
| 881 | NULL == NdefMap->SendRecvBuf) { |
| 882 | goto Mfc_FormatEnd; |
| 883 | } |
| 884 | NdefSmtCrdFmt->pTransceiveInfo = NdefMap->pTransceiveInfo; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 885 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 886 | gphNxpExtns_Context.CallBackMifare = phFriNfc_MfStd_Process; |
| 887 | gphNxpExtns_Context.CallBackCtxt = NdefSmtCrdFmt; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 888 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 889 | NdefInfo.NdefSendRecvLen = NDEF_SENDRCV_BUF_LEN; |
| 890 | phFriNfc_NdefSmtCrd_Reset__(NdefSmtCrdFmt, NdefMap->SendRecvBuf, |
| 891 | &(NdefInfo.NdefSendRecvLen)); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 892 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 893 | /* Register Callbacks */ |
| 894 | NdefSmtCrdFmt->CompletionRoutine[0].CompletionRoutine = |
| 895 | Mfc_FormatNdef_Completion_Routine; |
| 896 | NdefSmtCrdFmt->CompletionRoutine[1].CompletionRoutine = |
| 897 | Mfc_FormatNdef_Completion_Routine; |
| 898 | NdefSmtCrdFmt->psRemoteDevInfo = NdefMap->psRemoteDevInfo; |
| 899 | sak = NdefSmtCrdFmt->psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 900 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 901 | if ((0x08 == (sak & 0x18)) || (0x18 == (sak & 0x18)) || (0x01 == sak)) { |
| 902 | NdefSmtCrdFmt->CardType = (uint8_t)( |
| 903 | ((sak & 0x18) == 0x08) |
| 904 | ? PH_FRINFC_SMTCRDFMT_MFSTD_1K_CRD |
| 905 | : (((sak & 0x19) == 0x19) ? PH_FRINFC_SMTCRDFMT_MFSTD_2K_CRD |
| 906 | : PH_FRINFC_SMTCRDFMT_MFSTD_4K_CRD)); |
| 907 | status = phFriNfc_MfStd_Format(NdefSmtCrdFmt, mif_std_key); |
| 908 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 909 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 910 | if (NFCSTATUS_PENDING == status) { |
| 911 | status = NFCSTATUS_SUCCESS; |
| 912 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 913 | |
| 914 | Mfc_FormatEnd: |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 915 | if (status != NFCSTATUS_SUCCESS) { |
| 916 | status = NFCSTATUS_FAILED; |
| 917 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 918 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 919 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | /******************************************************************************* |
| 923 | ** |
| 924 | ** Function phNxNciExtns_MifareStd_Reconnect |
| 925 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 926 | ** Description This function sends the deactivate command to NFCC for |
| 927 | *Mifare |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 928 | ** |
| 929 | ** |
| 930 | ** Returns: |
| 931 | ** NFCSTATUS_PENDING - if successfully initiated |
| 932 | ** NFCSTATUS_FAILED - otherwise |
| 933 | ** |
| 934 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 935 | NFCSTATUS phNxNciExtns_MifareStd_Reconnect(void) { |
| 936 | tNFA_STATUS status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 937 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 938 | EXTNS_SetDeactivateFlag(true); |
| 939 | if (NFA_STATUS_OK != |
| 940 | (status = NFA_Deactivate(true))) /* deactivate to sleep state */ |
| 941 | { |
| 942 | LOG(ERROR) << StringPrintf("%s: deactivate failed, status = %d", __func__, |
| 943 | status); |
| 944 | return NFCSTATUS_FAILED; |
| 945 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 946 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 947 | return NFCSTATUS_PENDING; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /******************************************************************************* |
| 951 | ** |
| 952 | ** Function Mfc_DeactivateCbackSelect |
| 953 | ** |
| 954 | ** Description This function select the Mifare tag |
| 955 | ** |
| 956 | ** |
| 957 | ** Returns: void |
| 958 | ** |
| 959 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 960 | void Mfc_DeactivateCbackSelect(void) { |
| 961 | tNFA_STATUS status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 962 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 963 | EXTNS_SetConnectFlag(true); |
| 964 | if (NFA_STATUS_OK != |
| 965 | (status = NFA_Select(0x01, phNciNfc_e_RfProtocolsMifCProtocol, |
| 966 | phNciNfc_e_RfInterfacesTagCmd_RF))) { |
| 967 | LOG(ERROR) << StringPrintf("%s: NFA_Select failed, status = %d", __func__, |
| 968 | status); |
| 969 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 970 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 971 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /******************************************************************************* |
| 975 | ** |
| 976 | ** Function Mfc_ActivateCback |
| 977 | ** |
| 978 | ** Description This function invoke the callback when receive the response |
| 979 | ** |
| 980 | ** |
| 981 | ** Returns: void |
| 982 | ** |
| 983 | ** |
| 984 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 985 | void Mfc_ActivateCback(void) { |
| 986 | gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt, |
| 987 | NFCSTATUS_SUCCESS); |
| 988 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /******************************************************************************* |
| 992 | ** |
| 993 | ** Function Mfc_Transceive |
| 994 | ** |
| 995 | ** Description Sends raw frame to Mifare Classic Tag. |
| 996 | ** |
| 997 | ** Returns NFCSTATUS_SUCCESS - if successfully initiated |
| 998 | ** NFCSTATUS_FAILED - otherwise |
| 999 | ** |
| 1000 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1001 | NFCSTATUS Mfc_Transceive(uint8_t* p_data, uint32_t len) { |
| 1002 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 1003 | uint8_t i = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1004 | |
Ganesh Deva | 389aa82 | 2019-09-04 16:48:25 +0530 | [diff] [blame] | 1005 | if (len == 0) { |
| 1006 | android_errorWriteLog(0x534e4554, "132082342"); |
| 1007 | return status; |
| 1008 | } |
| 1009 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1010 | gphNxpExtns_Context.RawWriteCallBack = false; |
| 1011 | gphNxpExtns_Context.CallBackMifare = NULL; |
| 1012 | gphNxpExtns_Context.CallBackCtxt = NdefMap; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1013 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1014 | EXTNS_SetCallBackFlag(true); |
| 1015 | if (p_data[0] == 0x60 || p_data[0] == 0x61) { |
Ganesh Deva | 638d3d1 | 2019-09-04 16:48:38 +0530 | [diff] [blame] | 1016 | if (len < 12) { |
| 1017 | android_errorWriteLog(0x534e4554, "125900276"); |
| 1018 | return status; |
| 1019 | } |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 1020 | NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t) p_data[0]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1021 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1022 | NdefMap->SendRecvBuf[i++] = p_data[1]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1023 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1024 | NdefMap->SendRecvBuf[i++] = p_data[6]; /* TODO, handle 7 byte UID */ |
| 1025 | NdefMap->SendRecvBuf[i++] = p_data[7]; |
| 1026 | NdefMap->SendRecvBuf[i++] = p_data[8]; |
| 1027 | NdefMap->SendRecvBuf[i++] = p_data[9]; |
| 1028 | NdefMap->SendRecvBuf[i++] = p_data[10]; |
| 1029 | NdefMap->SendRecvBuf[i++] = p_data[11]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1030 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1031 | status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd, |
| 1032 | NdefMap->SendRecvBuf, NdefMap->SendLength, |
| 1033 | NdefMap->SendRecvLength); |
| 1034 | } else if (p_data[0] == 0xA0) { |
| 1035 | EXTNS_SetCallBackFlag(false); |
| 1036 | NdefMap->Cmd.MfCmd = phNfc_eMifareWrite16; |
| 1037 | gphNxpExtns_Context.RawWriteCallBack = true; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1038 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1039 | memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1); |
| 1040 | NdefMap->SendLength = len - 1; |
| 1041 | status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd, |
| 1042 | NdefMap->SendRecvBuf, NdefMap->SendLength, |
| 1043 | NdefMap->SendRecvLength); |
| 1044 | } else if ((p_data[0] == phNfc_eMifareInc) || |
| 1045 | (p_data[0] == phNfc_eMifareDec)) { |
| 1046 | EXTNS_SetCallBackFlag(false); |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 1047 | NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t) p_data[0]; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1048 | gphNxpExtns_Context.RawWriteCallBack = true; |
| 1049 | |
| 1050 | memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1); |
| 1051 | NdefMap->SendLength = len - 1; |
| 1052 | status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd, |
| 1053 | NdefMap->SendRecvBuf, NdefMap->SendLength, |
| 1054 | NdefMap->SendRecvLength); |
| 1055 | } else if (((p_data[0] == phNfc_eMifareTransfer) || |
| 1056 | (p_data[0] == phNfc_eMifareRestore)) && |
| 1057 | (len == 2)) { |
Suhas Suresh | 9139dc2 | 2018-05-09 15:48:37 +0530 | [diff] [blame] | 1058 | NdefMap->Cmd.MfCmd = (phNfc_eMifareCmdList_t) p_data[0]; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1059 | if (p_data[0] == phNfc_eMifareRestore) { |
| 1060 | EXTNS_SetCallBackFlag(false); |
| 1061 | gphNxpExtns_Context.RawWriteCallBack = true; |
| 1062 | memcpy(NdefMap->SendRecvBuf, &p_data[1], len - 1); |
| 1063 | NdefMap->SendLength = len - 1; |
| 1064 | } else { |
| 1065 | memcpy(NdefMap->SendRecvBuf, p_data, len); |
| 1066 | NdefMap->SendLength = len; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1067 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1068 | status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd, |
| 1069 | NdefMap->SendRecvBuf, NdefMap->SendLength, |
| 1070 | NdefMap->SendRecvLength); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1071 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1072 | } else { |
| 1073 | NdefMap->Cmd.MfCmd = phNfc_eMifareRaw; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1074 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1075 | memcpy(NdefMap->SendRecvBuf, p_data, len); |
| 1076 | NdefMap->SendLength = len; |
| 1077 | status = phFriNfc_ExtnsTransceive(NdefMap->pTransceiveInfo, NdefMap->Cmd, |
| 1078 | NdefMap->SendRecvBuf, NdefMap->SendLength, |
| 1079 | NdefMap->SendRecvLength); |
| 1080 | } |
| 1081 | if (NFCSTATUS_PENDING == status) { |
| 1082 | status = NFCSTATUS_SUCCESS; |
| 1083 | } else { |
| 1084 | LOG(ERROR) << StringPrintf("ERROR: Mfc_Transceive = 0x%x", status); |
| 1085 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1086 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1087 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | /******************************************************************************* |
| 1091 | ** |
| 1092 | ** Function nativeNfcExtns_doTransceive |
| 1093 | ** |
| 1094 | ** Description Sends raw frame to BCM stack. |
| 1095 | ** |
| 1096 | ** Returns NFCSTATUS_PENDING - if successfully initiated |
| 1097 | ** NFCSTATUS_FAILED - otherwise |
| 1098 | ** |
| 1099 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1100 | STATIC NFCSTATUS nativeNfcExtns_doTransceive(uint8_t* buff, uint16_t buffSz) { |
| 1101 | NFCSTATUS wStatus = NFCSTATUS_PENDING; |
| 1102 | tNFA_STATUS status = |
| 1103 | NFA_SendRawFrame(buff, buffSz, NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1104 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1105 | if (status != NFA_STATUS_OK) { |
| 1106 | LOG(ERROR) << StringPrintf("%s: fail send; error=%d", __func__, status); |
| 1107 | wStatus = NFCSTATUS_FAILED; |
| 1108 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1109 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1110 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1111 | } |
| 1112 | |
| 1113 | /******************************************************************************* |
| 1114 | ** |
| 1115 | ** Function phNciNfc_RecvMfResp |
| 1116 | ** |
| 1117 | ** Description This function shall be invoked as part of ReaderMgmt data |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1118 | ** exchange sequence handler on receiving response/data from |
| 1119 | *NFCC |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1120 | ** |
| 1121 | ** Returns NFCSTATUS_SUCCESS - Data Reception is successful |
| 1122 | ** NFCSTATUS_FAILED - Data Reception failed |
| 1123 | ** |
| 1124 | *******************************************************************************/ |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 1125 | STATIC NFCSTATUS phNciNfc_RecvMfResp(phNciNfc_Buff_t* RspBuffInfo, |
| 1126 | NFCSTATUS wStatus) { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1127 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
| 1128 | uint16_t wPldDataSize = 0; |
| 1129 | phNciNfc_ExtnRespId_t RecvdExtnRspId = phNciNfc_e_InvalidRsp; |
| 1130 | if (NULL == RspBuffInfo) { |
| 1131 | status = NFCSTATUS_FAILED; |
| 1132 | } else { |
Alisher Alikhodjaev | 0d7103d | 2022-03-18 17:13:05 -0700 | [diff] [blame] | 1133 | if (((PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE) > |
| 1134 | RspBuffInfo->wLen) || |
| 1135 | (PH_NCINFC_STATUS_OK != wStatus) || (NULL == (RspBuffInfo->pBuff))) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1136 | status = NFCSTATUS_FAILED; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1137 | } else { |
| 1138 | RecvdExtnRspId = (phNciNfc_ExtnRespId_t)RspBuffInfo->pBuff[0]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1139 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1140 | switch (RecvdExtnRspId) { |
| 1141 | case phNciNfc_e_MfXchgDataRsp: { |
| 1142 | NFCSTATUS writeResponse = NFCSTATUS_SUCCESS; |
| 1143 | /* check the status byte */ |
| 1144 | if ((NFC_GetNCIVersion() == NCI_VERSION_2_0) && |
| 1145 | (((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_WR_TLV) || |
| 1146 | ((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_WRITE) || |
| 1147 | ((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_WR_NDEF_LEN) || |
| 1148 | ((NdefMap->State) == PH_FRINFC_NDEFMAP_STATE_INIT))) { |
| 1149 | uint8_t rspAck = RspBuffInfo->pBuff[RspBuffInfo->wLen - 2]; |
| 1150 | uint8_t rspAckMask = ((RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]) & |
| 1151 | MAX_NUM_VALID_BITS_FOR_ACK); |
| 1152 | NCI_CALCULATE_ACK(rspAck, rspAckMask); |
| 1153 | writeResponse = |
| 1154 | (rspAck == T2T_RSP_ACK) ? NFCSTATUS_SUCCESS : NFC_STATUS_FAILED; |
| 1155 | } else { |
| 1156 | writeResponse = RspBuffInfo->pBuff[RspBuffInfo->wLen - 1]; |
| 1157 | } |
Nikhil Chhabra | 9ac2629 | 2018-01-10 11:57:21 +0530 | [diff] [blame] | 1158 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1159 | if (writeResponse == PH_NCINFC_STATUS_OK) { |
| 1160 | status = NFCSTATUS_SUCCESS; |
| 1161 | uint16_t wRecvDataSz = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1162 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1163 | /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */ |
| 1164 | wPldDataSize = ((RspBuffInfo->wLen) - |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1165 | (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE)); |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1166 | wRecvDataSz = NCI_MAX_DATA_LEN; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1167 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1168 | /* wPldDataSize = wPldDataSize-1; ==> ignoring the last status byte |
| 1169 | * appended with data */ |
| 1170 | if ((wPldDataSize) <= wRecvDataSz) { |
| 1171 | /* Extract the data part from pBuff[2] & fill it to be sent to |
| 1172 | * upper layer */ |
| 1173 | memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[1]), |
| 1174 | (wPldDataSize)); |
| 1175 | /* update the number of bytes received from lower layer,excluding |
| 1176 | * the status byte */ |
| 1177 | *(NdefMap->SendRecvLength) = wPldDataSize; |
| 1178 | } else { |
| 1179 | // TODO:- Map some status for remaining extra data received to be |
| 1180 | // sent back to caller?? |
| 1181 | status = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1182 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1183 | } else { |
| 1184 | status = NFCSTATUS_FAILED; |
| 1185 | } |
| 1186 | } break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1187 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1188 | case phNciNfc_e_MfcAuthRsp: { |
| 1189 | /* check the status byte */ |
| 1190 | if (PH_NCINFC_STATUS_OK == RspBuffInfo->pBuff[1]) { |
| 1191 | if (gAuthCmdBuf.auth_sent == true) { |
| 1192 | MfcPresenceCheckResult(NFCSTATUS_SUCCESS); |
| 1193 | return NFCSTATUS_SUCCESS; |
| 1194 | } |
| 1195 | gAuthCmdBuf.auth_status = true; |
| 1196 | status = NFCSTATUS_SUCCESS; |
| 1197 | |
| 1198 | /* DataLen = TotalRecvdLen - (sizeof(RspId) + sizeof(Status)) */ |
| 1199 | wPldDataSize = ((RspBuffInfo->wLen) - |
| 1200 | (PHNCINFC_EXTNID_SIZE + PHNCINFC_EXTNSTATUS_SIZE)); |
| 1201 | |
| 1202 | /* Extract the data part from pBuff[2] & fill it to be sent to upper |
| 1203 | * layer */ |
| 1204 | memcpy(NdefMap->SendRecvBuf, &(RspBuffInfo->pBuff[2]), |
| 1205 | wPldDataSize); |
| 1206 | /* update the number of bytes received from lower layer,excluding |
| 1207 | * the status byte */ |
| 1208 | *(NdefMap->SendRecvLength) = wPldDataSize; |
| 1209 | } else { |
| 1210 | if (gAuthCmdBuf.auth_sent == true) { |
| 1211 | gAuthCmdBuf.auth_status = false; |
| 1212 | MfcPresenceCheckResult(NFCSTATUS_FAILED); |
| 1213 | return NFCSTATUS_SUCCESS; |
| 1214 | } else { |
| 1215 | /* Reset the stored auth command buffer */ |
| 1216 | memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN); |
| 1217 | gAuthCmdBuf.pauth_cmd->length = 0; |
| 1218 | gAuthCmdBuf.auth_status = false; |
| 1219 | } |
| 1220 | status = NFCSTATUS_FAILED; |
| 1221 | } |
| 1222 | } break; |
| 1223 | |
| 1224 | default: { status = NFCSTATUS_FAILED; } break; |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /******************************************************************************* |
| 1233 | ** |
| 1234 | ** Function phLibNfc_SendWrt16CmdPayload |
| 1235 | ** |
| 1236 | ** Description This function map the raw write cmd |
| 1237 | ** |
| 1238 | ** Returns NFCSTATUS_SUCCESS - Command framing done |
| 1239 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1240 | ** |
| 1241 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1242 | STATIC NFCSTATUS |
| 1243 | phLibNfc_SendWrt16CmdPayload(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1244 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf) { |
| 1245 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1246 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1247 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1248 | (0 != (pTransceiveInfo->sSendData.length))) { |
| 1249 | memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer, |
| 1250 | (pTransceiveInfo->sSendData.length)); |
| 1251 | pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length; |
| 1252 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1253 | } else { |
| 1254 | wStatus = NFCSTATUS_INVALID_PARAMETER; |
| 1255 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1256 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1257 | if (gphNxpExtns_Context.RawWriteCallBack == true) { |
| 1258 | EXTNS_SetCallBackFlag(true); |
| 1259 | gphNxpExtns_Context.RawWriteCallBack = false; |
| 1260 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1261 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1262 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | /******************************************************************************* |
| 1266 | ** |
| 1267 | ** Function phLibNfc_SendIncDecCmdPayload |
| 1268 | ** |
| 1269 | ** Description This function prepares the Increment/Decrement Value to be |
| 1270 | ** sent. This is called after sending the Increment/Decrement |
| 1271 | ** command is already sent and successfull |
| 1272 | ** |
| 1273 | ** Returns NFCSTATUS_SUCCESS - Payload framing done |
| 1274 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1275 | ** |
| 1276 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1277 | STATIC NFCSTATUS |
| 1278 | phLibNfc_SendIncDecCmdPayload(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1279 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf) { |
| 1280 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1281 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1282 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1283 | (0 != (pTransceiveInfo->sSendData.length))) { |
| 1284 | memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer, |
| 1285 | (pTransceiveInfo->sSendData.length)); |
| 1286 | pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length; |
| 1287 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1288 | } else { |
| 1289 | wStatus = NFCSTATUS_INVALID_PARAMETER; |
| 1290 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1291 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1292 | if (gphNxpExtns_Context.RawWriteCallBack == true) { |
| 1293 | EXTNS_SetCallBackFlag(true); |
| 1294 | gphNxpExtns_Context.RawWriteCallBack = false; |
| 1295 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1296 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1297 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1298 | } |
| 1299 | |
| 1300 | /******************************************************************************* |
| 1301 | ** |
| 1302 | ** Function Mfc_RecvPacket |
| 1303 | ** |
| 1304 | ** Description Decodes Mifare Classic Tag Response |
| 1305 | ** This is called from NFA_SendRaw Callback |
| 1306 | ** |
| 1307 | ** Returns: |
| 1308 | ** NFCSTATUS_SUCCESS - if successfully initiated |
| 1309 | ** NFCSTATUS_FAILED - otherwise |
| 1310 | ** |
| 1311 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1312 | NFCSTATUS Mfc_RecvPacket(uint8_t* buff, uint8_t buffSz) { |
| 1313 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
| 1314 | phNciNfc_Buff_t RspBuff; |
| 1315 | uint8_t* pcmd_buff; |
| 1316 | uint16_t buffSize; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1317 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1318 | RspBuff.pBuff = buff; |
| 1319 | RspBuff.wLen = buffSz; |
| 1320 | status = phNciNfc_RecvMfResp(&RspBuff, status); |
| 1321 | if (true == gAuthCmdBuf.auth_sent) { |
| 1322 | DLOG_IF(INFO, nfc_debug_enabled) |
| 1323 | << StringPrintf("%s Mfc Check Presence in progress", __func__); |
| 1324 | gAuthCmdBuf.auth_sent = false; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1325 | return status; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1326 | } |
| 1327 | if (true == gphNxpExtns_Context.writecmdFlag && |
| 1328 | (NFCSTATUS_SUCCESS == status)) { |
| 1329 | pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE); |
| 1330 | if (NULL == pcmd_buff) { |
| 1331 | return NFCSTATUS_FAILED; |
| 1332 | } |
| 1333 | buffSize = MAX_BUFF_SIZE; |
| 1334 | gphNxpExtns_Context.writecmdFlag = false; |
| 1335 | phLibNfc_SendWrt16CmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo); |
| 1336 | status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize); |
| 1337 | if (NFCSTATUS_PENDING != status) { |
| 1338 | LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status); |
| 1339 | } else { |
| 1340 | status = NFCSTATUS_SUCCESS; |
| 1341 | } |
| 1342 | if (pcmd_buff != NULL) { |
| 1343 | free(pcmd_buff); |
| 1344 | pcmd_buff = NULL; |
| 1345 | } |
| 1346 | } else if (true == gphNxpExtns_Context.incrdecflag && |
| 1347 | (NFCSTATUS_SUCCESS == status)) { |
| 1348 | pcmd_buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE); |
| 1349 | if (NULL == pcmd_buff) { |
| 1350 | return NFCSTATUS_FAILED; |
| 1351 | } |
| 1352 | buffSize = MAX_BUFF_SIZE; |
| 1353 | gphNxpExtns_Context.incrdecflag = false; |
| 1354 | phLibNfc_SendIncDecCmdPayload(NdefMap->pTransceiveInfo, &tNciTranscvInfo); |
| 1355 | status = phNciNfc_SendMfReq(tNciTranscvInfo, pcmd_buff, &buffSize); |
| 1356 | if (NFCSTATUS_PENDING != status) { |
| 1357 | LOG(ERROR) << StringPrintf("ERROR : Mfc_RecvPacket: 0x%x", status); |
| 1358 | } else { |
| 1359 | status = NFCSTATUS_SUCCESS; |
| 1360 | } |
| 1361 | gphNxpExtns_Context.incrdecstatusflag = true; |
| 1362 | if (pcmd_buff != NULL) { |
| 1363 | free(pcmd_buff); |
| 1364 | pcmd_buff = NULL; |
| 1365 | } |
| 1366 | |
| 1367 | } else { |
| 1368 | if (gphNxpExtns_Context.CallBackMifare != NULL) { |
| 1369 | if ((gphNxpExtns_Context.incrdecstatusflag == true) && status == 0xB2) { |
| 1370 | gphNxpExtns_Context.incrdecstatusflag = false; |
| 1371 | status = NFCSTATUS_SUCCESS; |
| 1372 | } |
| 1373 | gphNxpExtns_Context.CallBackMifare(gphNxpExtns_Context.CallBackCtxt, |
| 1374 | status); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | /******************************************************************************* |
| 1382 | ** |
| 1383 | ** Function phNciNfc_MfCreateXchgDataHdr |
| 1384 | ** |
| 1385 | ** Description This function builds the payload header for mifare XchgData |
| 1386 | ** request to be sent to NFCC. |
| 1387 | ** |
| 1388 | ** Returns NFCSTATUS_PENDING - Command framing done |
| 1389 | ** NFCSTATUS_FAILED - Otherwise |
| 1390 | ** |
| 1391 | *******************************************************************************/ |
| 1392 | STATIC |
| 1393 | NFCSTATUS |
| 1394 | phNciNfc_MfCreateXchgDataHdr(phNciNfc_TransceiveInfo_t tTranscvInfo, |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1395 | uint8_t* buff, uint16_t* buffSz) |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1396 | |
| 1397 | { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1398 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
| 1399 | uint8_t i = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1400 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1401 | buff[i++] = phNciNfc_e_MfRawDataXchgHdr; |
| 1402 | memcpy(&buff[i], tTranscvInfo.tSendData.pBuff, tTranscvInfo.tSendData.wLen); |
| 1403 | *buffSz = i + tTranscvInfo.tSendData.wLen; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1404 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1405 | status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1406 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1407 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1408 | } |
| 1409 | |
| 1410 | /******************************************************************************* |
| 1411 | ** |
| 1412 | ** Function phNciNfc_MfCreateAuthCmdHdr |
| 1413 | ** |
| 1414 | ** Description This function builds the payload header for mifare |
| 1415 | ** classic Authenticate command to be sent to NFCC. |
| 1416 | ** |
| 1417 | ** Returns NFCSTATUS_PENDING - Command framing done |
| 1418 | ** NFCSTATUS_FAILED - Otherwise |
| 1419 | ** |
| 1420 | *******************************************************************************/ |
| 1421 | STATIC |
| 1422 | NFCSTATUS |
| 1423 | phNciNfc_MfCreateAuthCmdHdr(phNciNfc_TransceiveInfo_t tTranscvInfo, |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1424 | uint8_t bBlockAddr, uint8_t* buff, |
| 1425 | uint16_t* buffSz) { |
| 1426 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
| 1427 | // pphNciNfc_RemoteDevInformation_t pActivDev = NULL; |
| 1428 | // /*commented to eliminate unused variable warning*/ |
| 1429 | uint8_t bKey = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1430 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1431 | /*No need to check range of block address*/ |
| 1432 | /*To check for Authenticate A or Authenticate B type command*/ |
| 1433 | if (PHNCINFC_AUTHENTICATION_KEYB == tTranscvInfo.tSendData.pBuff[0]) { |
| 1434 | bKey = bKey | PHNCINFC_ENABLE_KEY_B; |
| 1435 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1436 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1437 | /*TO Do last 4 bits of Key to be set based of firmware implementation*/ |
| 1438 | /*this value is hardcoded but based on firmware implementation change this |
| 1439 | * value*/ |
| 1440 | bKey = (bKey | PHNCINFC_AUTHENTICATION_KEY); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1441 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1442 | bKey |= tTranscvInfo.tSendData.pBuff[2]; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1443 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1444 | /*For authentication extension no need to copy tSendData buffer of |
| 1445 | * tTranscvInfo */ |
| 1446 | tTranscvInfo.tSendData.wLen = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1447 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1448 | buff[0] = phNciNfc_e_MfcAuthReq; |
| 1449 | buff[1] = bBlockAddr; |
| 1450 | buff[2] = bKey; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1451 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1452 | *buffSz = 0x03; |
| 1453 | if (bKey & PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY) { |
| 1454 | memcpy(&buff[3], &tTranscvInfo.tSendData.pBuff[3], PHLIBNFC_MFC_AUTHKEYLEN); |
| 1455 | *buffSz += PHLIBNFC_MFC_AUTHKEYLEN; |
| 1456 | } |
| 1457 | /* Store the auth command buffer to use further for presence check */ |
| 1458 | if (gAuthCmdBuf.pauth_cmd != NULL) { |
| 1459 | memset(gAuthCmdBuf.pauth_cmd->buffer, 0, NCI_MAX_DATA_LEN); |
| 1460 | gAuthCmdBuf.pauth_cmd->length = *buffSz; |
| 1461 | memcpy(gAuthCmdBuf.pauth_cmd->buffer, buff, *buffSz); |
| 1462 | } |
| 1463 | status = nativeNfcExtns_doTransceive(buff, (uint16_t)*buffSz); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1464 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1465 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | /******************************************************************************* |
| 1469 | ** |
| 1470 | ** Function phNciNfc_SendMfReq |
| 1471 | ** |
| 1472 | ** Description This function shall be invoked as part of ReaderMgmt data |
| 1473 | ** exchange sequence handler. |
| 1474 | ** It shall send the request packet to NFCC. |
| 1475 | ** |
| 1476 | ** Returns NFCSTATUS_PENDING - Send request is Pending |
| 1477 | ** NFCSTATUS_FAILED - otherwise |
| 1478 | ** |
| 1479 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1480 | STATIC NFCSTATUS phNciNfc_SendMfReq(phNciNfc_TransceiveInfo_t tTranscvInfo, |
| 1481 | uint8_t* buff, uint16_t* buffSz) { |
| 1482 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1483 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1484 | switch (tTranscvInfo.uCmd.T2TCmd) { |
| 1485 | case phNciNfc_eT2TRaw: { |
| 1486 | status = phNciNfc_MfCreateXchgDataHdr(tTranscvInfo, buff, buffSz); |
| 1487 | } break; |
| 1488 | case phNciNfc_eT2TAuth: { |
| 1489 | status = phNciNfc_MfCreateAuthCmdHdr(tTranscvInfo, (tTranscvInfo.bAddr), |
| 1490 | buff, buffSz); |
| 1491 | } break; |
| 1492 | default: { |
| 1493 | status = NFCSTATUS_FAILED; |
| 1494 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1495 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1496 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1497 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1498 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | /******************************************************************************* |
| 1502 | ** |
| 1503 | ** Function phLibNfc_CalSectorAddress |
| 1504 | ** |
| 1505 | ** Description This function update the sector address for Mifare classic |
| 1506 | ** |
| 1507 | ** Returns none |
| 1508 | ** |
| 1509 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1510 | STATIC void phLibNfc_CalSectorAddress(uint8_t* Sector_Address) { |
| 1511 | uint8_t BlockNumber = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1512 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1513 | if (NULL != Sector_Address) { |
| 1514 | BlockNumber = *Sector_Address; |
| 1515 | if (BlockNumber >= PHLIBNFC_MIFARESTD4K_BLK128) { |
| 1516 | *Sector_Address = (uint8_t)(PHLIBNFC_MIFARESTD_SECTOR_NO32 + |
| 1517 | ((BlockNumber - PHLIBNFC_MIFARESTD4K_BLK128) / |
| 1518 | PHLIBNFC_MIFARESTD_BLOCK_BYTES)); |
| 1519 | } else { |
| 1520 | *Sector_Address = BlockNumber / PHLIBNFC_NO_OF_BLKPERSECTOR; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1521 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1522 | } else { |
| 1523 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1524 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1525 | return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | /******************************************************************************* |
| 1529 | ** |
| 1530 | ** Function phLibNfc_GetKeyNumberMFC |
| 1531 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1532 | ** Description This function find key number based on authentication |
| 1533 | *command |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1534 | ** |
| 1535 | ** Returns NFCSTATUS_SUCCESS - If found the key number |
| 1536 | ** NFCSTATUS_FAILED - otherwise |
| 1537 | ** |
| 1538 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1539 | STATIC NFCSTATUS phLibNfc_GetKeyNumberMFC(uint8_t* buffer, uint8_t* bKey) { |
| 1540 | int32_t sdwStat = 0X00; |
| 1541 | NFCSTATUS wStatus = NFCSTATUS_INVALID_PARAMETER; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1542 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1543 | uint8_t bIndex = 0x00; |
| 1544 | uint8_t bNoOfKeys = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1545 | |
| 1546 | #if PHLIBNFC_NXPETENSION_CONFIGURE_MFKEYS |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1547 | uint8_t aMfc_keys[NXP_NUMBER_OF_MFC_KEYS][NXP_MFC_KEY_SIZE] = NXP_MFC_KEYS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1548 | #else |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1549 | uint8_t aMfc_keys[1][1] = {{0x00}}; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1550 | #endif |
| 1551 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1552 | if (NULL != bKey && NULL != buffer) { |
| 1553 | bNoOfKeys = sizeof(aMfc_keys) / NXP_MFC_KEY_SIZE; |
| 1554 | /* Traverse through the keys stored to determine whether keys is preloaded |
| 1555 | * key */ |
| 1556 | for (bIndex = 0; bIndex < bNoOfKeys; bIndex++) { |
| 1557 | /* Check passed key is NDEF key */ |
| 1558 | sdwStat = memcmp(&buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], aMfc_keys[bIndex], |
| 1559 | PHLIBNFC_MFC_AUTHKEYLEN); |
| 1560 | if (!sdwStat) { |
| 1561 | LOG(ERROR) << StringPrintf( |
| 1562 | "Mifare : phLibNfc_GetKeyNumberMFC Key found"); |
| 1563 | *bKey = bIndex; |
| 1564 | wStatus = NFCSTATUS_SUCCESS; |
| 1565 | break; |
| 1566 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1567 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1568 | LOG(ERROR) << StringPrintf( |
| 1569 | "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x Key = 0x%x", |
| 1570 | wStatus, *bKey); |
| 1571 | } else { |
| 1572 | wStatus = NFCSTATUS_FAILED; |
| 1573 | LOG(ERROR) << StringPrintf( |
| 1574 | "Mifare : phLibNfc_GetKeyNumberMFC returning = 0x%x", wStatus); |
| 1575 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1576 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1577 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | /******************************************************************************* |
| 1581 | ** |
| 1582 | ** Function phLibNfc_ChkAuthCmdMFC |
| 1583 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1584 | ** Description This function Check Authentication command send is proper or |
| 1585 | *not |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1586 | ** |
| 1587 | ** Returns NFCSTATUS_SUCCESS - Authenticate command is proper |
| 1588 | ** NFCSTATUS_FAILED - otherwise |
| 1589 | ** |
| 1590 | *******************************************************************************/ |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 1591 | STATIC NFCSTATUS phLibNfc_ChkAuthCmdMFC( |
| 1592 | phNfc_sTransceiveInfo_t* pTransceiveInfo, uint8_t* bKey) { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1593 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1594 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1595 | if (NULL != pTransceiveInfo && NULL != pTransceiveInfo->sSendData.buffer && |
| 1596 | 0 != pTransceiveInfo->sSendData.length && NULL != bKey) { |
| 1597 | if ((pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentA || |
| 1598 | pTransceiveInfo->cmd.MfCmd == phNfc_eMifareAuthentB)) { |
| 1599 | wStatus = |
| 1600 | phLibNfc_GetKeyNumberMFC(pTransceiveInfo->sSendData.buffer, bKey); |
| 1601 | } else { |
| 1602 | wStatus = NFCSTATUS_FAILED; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1603 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1604 | } else { |
| 1605 | wStatus = NFCSTATUS_FAILED; |
| 1606 | } |
| 1607 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1608 | } |
| 1609 | |
| 1610 | /******************************************************************************* |
| 1611 | ** |
| 1612 | ** Function phLibNfc_MifareMap |
| 1613 | ** |
| 1614 | ** Description Mifare Mapping Utility function |
| 1615 | ** |
| 1616 | ** Returns NFCSTATUS_SUCCESS - Mapping is proper |
| 1617 | ** NFCSTATUS_INVALID_PARAMETER - otherwise |
| 1618 | ** |
| 1619 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1620 | STATIC NFCSTATUS |
| 1621 | phLibNfc_MifareMap(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1622 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf) { |
| 1623 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
| 1624 | uint8_t bBuffIdx = 0; |
| 1625 | uint8_t bSectorNumber; |
| 1626 | uint8_t bKey = 0; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1627 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1628 | switch (pTransceiveInfo->cmd.MfCmd) { |
| 1629 | case phNfc_eMifareRead16: { |
| 1630 | if ((NULL != pTransceiveInfo->sRecvData.buffer) && |
| 1631 | (0 != (pTransceiveInfo->sRecvData.length))) { |
| 1632 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareRead16; |
| 1633 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr; |
| 1634 | pMappedTranscvIf->tSendData.wLen = bBuffIdx; |
| 1635 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1636 | } else { |
| 1637 | status = NFCSTATUS_INVALID_PARAMETER; |
| 1638 | } |
| 1639 | } break; |
| 1640 | |
| 1641 | case phNfc_eMifareWrite16: { |
| 1642 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1643 | (0 != (pTransceiveInfo->sSendData.length))) { |
| 1644 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16; |
| 1645 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr; |
| 1646 | memcpy(&(pMappedTranscvIf->tSendData.pBuff[bBuffIdx]), |
| 1647 | pTransceiveInfo->sSendData.buffer, |
| 1648 | (pTransceiveInfo->sSendData.length)); |
| 1649 | pMappedTranscvIf->tSendData.wLen = |
| 1650 | bBuffIdx + pTransceiveInfo->sSendData.length; |
| 1651 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1652 | } else { |
| 1653 | status = NFCSTATUS_INVALID_PARAMETER; |
| 1654 | } |
| 1655 | } break; |
| 1656 | |
| 1657 | case phNfc_eMifareAuthentA: |
| 1658 | case phNfc_eMifareAuthentB: { |
| 1659 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1660 | (0 != (pTransceiveInfo->sSendData.length)) && |
| 1661 | (NULL != pTransceiveInfo->sRecvData.buffer) && |
| 1662 | (0 != (pTransceiveInfo->sRecvData.length))) { |
| 1663 | status = phLibNfc_ChkAuthCmdMFC(pTransceiveInfo, &bKey); |
| 1664 | if (NFCSTATUS_FAILED != status) { |
| 1665 | bSectorNumber = pTransceiveInfo->addr; |
| 1666 | phLibNfc_CalSectorAddress(&bSectorNumber); |
| 1667 | /*For creating extension command header pTransceiveInfo's MfCmd get |
| 1668 | * used*/ |
| 1669 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = |
| 1670 | pTransceiveInfo->cmd.MfCmd; |
| 1671 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bSectorNumber; |
| 1672 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TAuth; |
| 1673 | pMappedTranscvIf->bAddr = bSectorNumber; |
| 1674 | pMappedTranscvIf->bNumBlock = pTransceiveInfo->NumBlock; |
| 1675 | if (NFCSTATUS_SUCCESS == status) { |
| 1676 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey; |
| 1677 | (pMappedTranscvIf->tSendData.wLen) = (uint16_t)(bBuffIdx); |
| 1678 | |
| 1679 | } else if (NFCSTATUS_INVALID_PARAMETER == status) { |
| 1680 | bKey = bKey | PH_NCINFC_MIFARECLASSIC_EMBEDDED_KEY; |
| 1681 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = bKey; |
| 1682 | memcpy(&pMappedTranscvIf->tSendData.pBuff[bBuffIdx], |
| 1683 | &pTransceiveInfo->sSendData |
| 1684 | .buffer[PHLIBNFC_MFCUIDLEN_INAUTHCMD], |
| 1685 | PHLIBNFC_MFC_AUTHKEYLEN); |
| 1686 | |
| 1687 | (pMappedTranscvIf->tSendData.wLen) = |
| 1688 | (uint16_t)(bBuffIdx + PHLIBNFC_MFC_AUTHKEYLEN); |
| 1689 | status = NFCSTATUS_SUCCESS; |
| 1690 | } else { |
| 1691 | /* do nothing */ |
| 1692 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1693 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1694 | } else { |
| 1695 | status = NFCSTATUS_INVALID_PARAMETER; |
| 1696 | } |
| 1697 | } break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1698 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1699 | case phNfc_eMifareRaw: { |
| 1700 | } break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1701 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1702 | default: { |
| 1703 | status = NFCSTATUS_INVALID_PARAMETER; |
| 1704 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1705 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1706 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1707 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1708 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1709 | } |
| 1710 | |
| 1711 | /******************************************************************************* |
| 1712 | ** |
| 1713 | ** Function phLibNfc_MapCmds |
| 1714 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1715 | ** Description This function maps the command request from libnfc level to |
| 1716 | *nci level |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1717 | ** |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1718 | ** Returns NFCSTATUS_SUCCESS - Mapping of command is |
| 1719 | *successful |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1720 | ** NFCSTATUS_INVALID_PARAMETER - One or more of the supplied |
| 1721 | ** parameters could not be interpreted properly |
| 1722 | ** |
| 1723 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1724 | STATIC NFCSTATUS phLibNfc_MapCmds(phNciNfc_RFDevType_t RemDevType, |
| 1725 | phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1726 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf) { |
| 1727 | NFCSTATUS status = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1728 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1729 | if ((NULL == pTransceiveInfo) || (NULL == pMappedTranscvIf)) { |
| 1730 | return NFCSTATUS_FAILED; |
| 1731 | } |
| 1732 | switch (RemDevType) { |
| 1733 | case phNciNfc_eMifare1k_PICC: |
| 1734 | case phNciNfc_eMifare4k_PICC: { |
| 1735 | status = phLibNfc_MifareMap(pTransceiveInfo, pMappedTranscvIf); |
| 1736 | break; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1737 | } |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1738 | default: { break; } |
| 1739 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1740 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1741 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | /******************************************************************************* |
| 1745 | ** |
| 1746 | ** Function phLibNfc_SendAuthCmd |
| 1747 | ** |
| 1748 | ** Description This function Send authentication command to NFCC |
| 1749 | ** |
| 1750 | ** Returns NFCSTATUS_SUCCESS - Parameters are proper |
| 1751 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1752 | ** |
| 1753 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1754 | STATIC NFCSTATUS |
| 1755 | phLibNfc_SendAuthCmd(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1756 | phNciNfc_TransceiveInfo_t* tNciTranscvInfo) { |
| 1757 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1758 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1759 | wStatus = phLibNfc_MapCmds(phNciNfc_eMifare1k_PICC, pTransceiveInfo, |
| 1760 | tNciTranscvInfo); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1761 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1762 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | /******************************************************************************* |
| 1766 | ** |
| 1767 | ** Function phLibNfc_SendWrt16Cmd |
| 1768 | ** |
| 1769 | ** Description This function maps Mifarewirte16 commands |
| 1770 | ** |
| 1771 | ** Returns NFCSTATUS_SUCCESS - Parameters are mapped |
| 1772 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1773 | ** |
| 1774 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1775 | STATIC NFCSTATUS |
| 1776 | phLibNfc_SendWrt16Cmd(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1777 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf) { |
| 1778 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
| 1779 | uint8_t bBuffIdx = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1780 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1781 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1782 | (0 != (pTransceiveInfo->sSendData.length))) { |
| 1783 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = phNfc_eMifareWrite16; |
| 1784 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr; |
| 1785 | pMappedTranscvIf->tSendData.wLen = bBuffIdx; |
| 1786 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1787 | } else { |
| 1788 | wStatus = NFCSTATUS_INVALID_PARAMETER; |
| 1789 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1790 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1791 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | /******************************************************************************* |
| 1795 | ** |
| 1796 | ** Function phLibNfc_SendIncDecCmd |
| 1797 | ** |
| 1798 | ** Description This function prepares the Increment/Decrement command |
| 1799 | ** to be sent, increment/decrement value is sent separately |
| 1800 | ** |
| 1801 | ** Returns NFCSTATUS_SUCCESS - Params are mapped |
| 1802 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1803 | ** |
| 1804 | *******************************************************************************/ |
Suhas Suresh | 2de4d8f | 2018-04-26 18:40:59 +0530 | [diff] [blame] | 1805 | STATIC NFCSTATUS phLibNfc_SendIncDecCmd( |
| 1806 | phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1807 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf, uint8_t IncDecCmd) { |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1808 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
| 1809 | uint8_t bBuffIdx = 0x00; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1810 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1811 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1812 | (0 != (pTransceiveInfo->sSendData.length))) { |
| 1813 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = IncDecCmd; |
| 1814 | pMappedTranscvIf->tSendData.pBuff[bBuffIdx++] = pTransceiveInfo->addr; |
| 1815 | pMappedTranscvIf->tSendData.wLen = bBuffIdx; |
| 1816 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1817 | } else { |
| 1818 | wStatus = NFCSTATUS_INVALID_PARAMETER; |
| 1819 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1820 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1821 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | /******************************************************************************* |
| 1825 | ** |
| 1826 | ** Function phLibNfc_SendRawCmd |
| 1827 | ** |
| 1828 | ** Description This function maps Mifare raw command |
| 1829 | ** |
| 1830 | ** Returns NFCSTATUS_SUCCESS - Parameters are mapped |
| 1831 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1832 | ** |
| 1833 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1834 | STATIC NFCSTATUS |
| 1835 | phLibNfc_SendRawCmd(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1836 | pphNciNfc_TransceiveInfo_t pMappedTranscvIf) { |
| 1837 | NFCSTATUS wStatus = NFCSTATUS_SUCCESS; |
| 1838 | // uint8_t bBuffIdx = 0x00; /*commented to |
| 1839 | // eliminate unused variable warning*/ |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1840 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1841 | if ((NULL != pTransceiveInfo->sSendData.buffer) && |
| 1842 | (0 != (pTransceiveInfo->sSendData.length))) { |
| 1843 | memcpy(pMappedTranscvIf->tSendData.pBuff, pTransceiveInfo->sSendData.buffer, |
| 1844 | (pTransceiveInfo->sSendData.length)); |
| 1845 | pMappedTranscvIf->tSendData.wLen = pTransceiveInfo->sSendData.length; |
| 1846 | pMappedTranscvIf->uCmd.T2TCmd = phNciNfc_eT2TRaw; |
| 1847 | } else { |
| 1848 | wStatus = NFCSTATUS_INVALID_PARAMETER; |
| 1849 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1850 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1851 | return wStatus; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | /******************************************************************************* |
| 1855 | ** |
| 1856 | ** Function phFriNfc_ExtnsTransceive |
| 1857 | ** |
| 1858 | ** Description This function maps Mifare raw command and send it to NFCC |
| 1859 | ** |
| 1860 | ** Returns NFCSTATUS_PENDING - Operation successful |
| 1861 | ** NFCSTATUS_INVALID_PARAMETER - Otherwise |
| 1862 | ** |
| 1863 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1864 | NFCSTATUS phFriNfc_ExtnsTransceive(phNfc_sTransceiveInfo_t* pTransceiveInfo, |
| 1865 | phNfc_uCmdList_t Cmd, uint8_t* SendRecvBuf, |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1866 | uint16_t SendLength, |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1867 | uint16_t* SendRecvLength) { |
| 1868 | (void)SendRecvLength; |
| 1869 | NFCSTATUS status = NFCSTATUS_FAILED; |
| 1870 | uint8_t* buff = NULL; |
| 1871 | uint16_t buffSz = 0; |
| 1872 | uint8_t i = 0; |
| 1873 | uint32_t length = SendLength; |
| 1874 | uint8_t restore_payload[] = { |
| 1875 | 0x00, 0x00, 0x00, 0x00, |
| 1876 | }; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1877 | |
Ganesh Deva | 20b20a5 | 2019-09-04 16:48:11 +0530 | [diff] [blame] | 1878 | if (SendLength == 0) { |
| 1879 | android_errorWriteLog(0x534e4554, "132083376"); |
| 1880 | return status; |
| 1881 | } |
| 1882 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1883 | buff = (uint8_t*)malloc((uint32_t)MAX_BUFF_SIZE); |
| 1884 | if (NULL == buff) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1885 | return status; |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | pTransceiveInfo->cmd = Cmd; |
| 1889 | |
| 1890 | if ((Cmd.MfCmd == phNfc_eMifareAuthentA) || |
| 1891 | (Cmd.MfCmd == phNfc_eMifareAuthentB)) { |
| 1892 | pTransceiveInfo->addr = SendRecvBuf[i++]; |
| 1893 | pTransceiveInfo->sSendData.buffer[4] = SendRecvBuf[i++]; |
| 1894 | pTransceiveInfo->sSendData.buffer[5] = SendRecvBuf[i++]; |
| 1895 | pTransceiveInfo->sSendData.buffer[6] = SendRecvBuf[i++]; |
| 1896 | pTransceiveInfo->sSendData.buffer[7] = SendRecvBuf[i++]; |
| 1897 | pTransceiveInfo->sSendData.buffer[8] = SendRecvBuf[i++]; |
| 1898 | pTransceiveInfo->sSendData.buffer[9] = SendRecvBuf[i++]; |
| 1899 | |
| 1900 | pTransceiveInfo->cmd.MfCmd = Cmd.MfCmd; |
| 1901 | |
| 1902 | pTransceiveInfo->sSendData.length = length; |
| 1903 | pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
| 1904 | status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo); |
| 1905 | } else if (Cmd.MfCmd == phNfc_eMifareWrite16) { |
| 1906 | pTransceiveInfo->addr = SendRecvBuf[i++]; |
| 1907 | length = SendLength - i; |
| 1908 | memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length); |
| 1909 | pTransceiveInfo->sSendData.length = length; |
| 1910 | pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
| 1911 | |
| 1912 | gphNxpExtns_Context.writecmdFlag = true; |
| 1913 | |
| 1914 | status = phLibNfc_SendWrt16Cmd(pTransceiveInfo, &tNciTranscvInfo); |
| 1915 | } else if ((Cmd.MfCmd == phNfc_eMifareInc) || |
| 1916 | (Cmd.MfCmd == phNfc_eMifareDec)) { |
| 1917 | pTransceiveInfo->addr = SendRecvBuf[i++]; |
| 1918 | length = SendLength - i; |
| 1919 | memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length); |
| 1920 | pTransceiveInfo->sSendData.length = length; |
| 1921 | pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
| 1922 | |
| 1923 | gphNxpExtns_Context.incrdecflag = true; |
| 1924 | |
| 1925 | status = |
| 1926 | phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd); |
| 1927 | |
| 1928 | } else if (Cmd.MfCmd == phNfc_eMifareRestore) { |
| 1929 | pTransceiveInfo->addr = SendRecvBuf[i++]; |
| 1930 | length = SendLength - i; |
| 1931 | memcpy(pTransceiveInfo->sSendData.buffer, &restore_payload[0], |
| 1932 | sizeof(restore_payload)); |
| 1933 | pTransceiveInfo->sSendData.length = length + sizeof(restore_payload); |
| 1934 | pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
| 1935 | |
| 1936 | gphNxpExtns_Context.incrdecflag = true; |
| 1937 | |
| 1938 | status = |
| 1939 | phLibNfc_SendIncDecCmd(pTransceiveInfo, &tNciTranscvInfo, Cmd.MfCmd); |
| 1940 | |
| 1941 | } else if ((Cmd.MfCmd == phNfc_eMifareRaw) || |
| 1942 | (Cmd.MfCmd == phNfc_eMifareTransfer)) { |
| 1943 | pTransceiveInfo->cmd.MfCmd = (phNfc_eMifareCmdList_t)phNciNfc_eT2TRaw; |
| 1944 | memcpy(pTransceiveInfo->sSendData.buffer, SendRecvBuf, length); |
| 1945 | pTransceiveInfo->sSendData.length = length; |
| 1946 | pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
| 1947 | status = phLibNfc_SendRawCmd(pTransceiveInfo, &tNciTranscvInfo); |
| 1948 | } else { |
| 1949 | pTransceiveInfo->addr = SendRecvBuf[i++]; |
| 1950 | length = SendLength - i; |
| 1951 | memcpy(pTransceiveInfo->sSendData.buffer, &SendRecvBuf[i], length); |
| 1952 | pTransceiveInfo->sSendData.length = length; |
| 1953 | pTransceiveInfo->sRecvData.length = MAX_BUFF_SIZE; |
| 1954 | status = phLibNfc_MifareMap(pTransceiveInfo, &tNciTranscvInfo); |
| 1955 | } |
| 1956 | |
| 1957 | if (NFCSTATUS_SUCCESS == status) { |
| 1958 | status = phNciNfc_SendMfReq(tNciTranscvInfo, buff, &buffSz); |
| 1959 | if (NFCSTATUS_PENDING != status) { |
| 1960 | LOG(ERROR) << StringPrintf("ERROR : phNciNfc_SendMfReq()"); |
| 1961 | } |
| 1962 | } else { |
| 1963 | LOG(ERROR) << StringPrintf(" ERROR : Sending phNciNfc_SendMfReq"); |
| 1964 | } |
| 1965 | if (buff != NULL) { |
| 1966 | free(buff); |
| 1967 | buff = NULL; |
| 1968 | } |
| 1969 | |
| 1970 | return status; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1971 | } |
| 1972 | /******************************************************************************* |
| 1973 | ** |
| 1974 | ** Function Mfc_CheckNdef_timeoutcb_Routine |
| 1975 | ** |
| 1976 | ** Description Callback for Mifare check ndef |
| 1977 | ** |
| 1978 | ** Returns None |
| 1979 | ** |
| 1980 | ** |
| 1981 | *******************************************************************************/ |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1982 | STATIC void Mfc_CheckNdef_timeoutcb_Routine(union sigval value) { |
| 1983 | LOG(ERROR) << StringPrintf(" Inside Mfc_CheckNdef_timeoutcb_Routine() "); |
| 1984 | tNFA_CONN_EVT_DATA conn_evt_data; |
| 1985 | /* NDEF Detection failed for other reasons */ |
| 1986 | conn_evt_data.ndef_detect.status = NFCSTATUS_FAILED; |
| 1987 | conn_evt_data.ndef_detect.cur_size = 0; |
| 1988 | conn_evt_data.ndef_detect.max_size = 0; |
| 1989 | conn_evt_data.ndef_detect.flags = RW_NDEF_FL_UNKNOWN; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1990 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1991 | /* update local flags */ |
| 1992 | NdefInfo.is_ndef = 0; |
| 1993 | NdefInfo.NdefActualSize = conn_evt_data.ndef_detect.cur_size; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1994 | |
Suhas Suresh | 6a4cb32 | 2018-04-26 18:04:09 +0530 | [diff] [blame] | 1995 | (*gphNxpExtns_Context.p_conn_cback)(NFA_NDEF_DETECT_EVT, &conn_evt_data); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1996 | } |