2 * Copyright (C) 2010 NXP Semiconductors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * \file phFriNfc_OvrHal.c
19 * \brief Overlapped HAL
22 * Creator: Gerald Kersch
24 * $Date: Wed May 5 10:47:27 2010 $
25 * Changed by: $Author: ing02260 $
27 * $Aliases: NFC_FRI1.1_WK1017_R34_3,NFC_FRI1.1_WK1023_R35_1 $
31 #include <phFriNfc_OvrHal.h>
32 #include <phOsalNfc.h>
33 #include <phFriNfc_NdefMap.h>
34 #include <phFriNfc_SmtCrdFmt.h>
37 #ifdef PHFRINFC_OVRHAL_MOCKUP /* */
38 //#include <phLibNfc_Gen.h>
39 #endif /* PHFRINFC_OVRHAL_MOCKUP */
44 #define MAX_MIF_PACKET_LEN 0x0FU
45 #define MIFARE_PLUS_UID_INDEX_TO_COPY 0x03U
46 #define MIFARE_PLUS_UID_LENGTH 0x07U
47 #define MIFARE_CLASSIC_UID_LENGTH 0x04U
48 #define MIFARE_UID_LEN_TO_COPY 0x04U
50 static void phFriNfc_OvrHal_CB_Send(void *context,
52 static void phFriNfc_OvrHal_CB_Receive(void *context,
53 phNfc_sData_t *pDataInfo,
55 static void phFriNfc_OvrHal_CB_Transceive(void *context,
56 phHal_sRemoteDevInformation_t *RemoteDevHandle,
57 phNfc_sData_t *pRecvdata,
60 static void phFriNfc_OvrHal_CB_ConnectDisconnect(void *context,
61 phHal_sRemoteDevInformation_t *RemoteDevHandle,
65 static void phFriNfc_OvrHal_SetComplInfo(phFriNfc_OvrHal_t *OvrHal,
66 phFriNfc_CplRt_t *CompletionInfo,
69 NFCSTATUS phFriNfc_OvrHal_Transceive(phFriNfc_OvrHal_t *OvrHal,
70 phFriNfc_CplRt_t *CompletionInfo,
71 phHal_sRemoteDevInformation_t *RemoteDevInfo,
73 phHal_sDepAdditionalInfo_t *DepAdditionalInfo,
79 NFCSTATUS status = NFCSTATUS_PENDING;
81 uint32_t length = SendLength;
83 /* To remove "warning (VS C4100) : unreferenced formal parameter" */
84 PHNFC_UNUSED_VARIABLE(DepAdditionalInfo);
86 /* Check the Input Parameters */
87 if ((NULL == OvrHal) || (NULL == CompletionInfo) || (NULL == RemoteDevInfo)
88 || (NULL == (void*)SendBuf) || (NULL == RecvBuf) || (NULL == RecvLength)
89 || ((phHal_eJewel_PICC != RemoteDevInfo->RemDevType) && (0 == SendLength)))
92 status = PHNFCSTVAL(CID_FRI_NFC_OVR_HAL, NFCSTATUS_INVALID_PARAMETER);
96 /* 16 is the maximum data, that can be sent to the mifare standard */
97 static uint8_t mif_send_buf[MAX_MIF_PACKET_LEN] = {0};
98 /* Populate the Transfer info structure */
99 OvrHal->TranceiveInfo.cmd = Cmd;
101 /* Populate the Send Buffer Info */
102 if((phHal_eMifare_PICC == RemoteDevInfo->RemDevType)
103 || (phHal_eISO14443_3A_PICC == RemoteDevInfo->RemDevType))
105 OvrHal->TranceiveInfo.addr = SendBuf[i++];
106 length = (SendLength - i);
108 if ((phHal_eMifareAuthentA == Cmd.MfCmd)
109 || (phHal_eMifareAuthentB == Cmd.MfCmd))
111 uint8_t uid_index = 0;
112 /* Authentication requires UID in the send buffer */
114 RemoteDevInfo->RemoteDevInfo.Iso14443A_Info.UidLength;
115 OvrHal->TranceiveInfo.sSendData.buffer = mif_send_buf;
119 case MIFARE_PLUS_UID_LENGTH:
121 uid_index = MIFARE_PLUS_UID_INDEX_TO_COPY;
122 uid_len = MIFARE_UID_LEN_TO_COPY;
126 case MIFARE_CLASSIC_UID_LENGTH:
134 status = PHNFCSTVAL (CID_FRI_NFC_OVR_HAL,
135 NFCSTATUS_READ_FAILED);
140 if (NFCSTATUS_PENDING == status)
142 /* copy uid to the send buffer for the authentication */
143 (void)memcpy ((void *)mif_send_buf,
144 (void *)&RemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Uid[uid_index],
147 (void)memcpy((mif_send_buf + uid_len), &(SendBuf[i]), length);
153 OvrHal->TranceiveInfo.sSendData.buffer = &SendBuf[i++];
155 OvrHal->TranceiveInfo.sSendData.length = length;
159 OvrHal->TranceiveInfo.sSendData.buffer = &SendBuf[i++];
160 OvrHal->TranceiveInfo.sSendData.length = length;
163 if (NFCSTATUS_PENDING == status)
165 /* Populate the Receive buffer */
166 OvrHal->TranceiveInfo.sRecvData.buffer = RecvBuf;
167 OvrHal->TranceiveInfo.sRecvData.length = *RecvLength;
168 OvrHal->pndef_recv_length = RecvLength;
169 phFriNfc_OvrHal_SetComplInfo(OvrHal,CompletionInfo, PH_FRINFC_OVRHAL_TRX);
171 /* Call the HAL 4.0 Transceive Function */
172 status = phHal4Nfc_Transceive (OvrHal->psHwReference,
173 &OvrHal->TranceiveInfo, RemoteDevInfo,
174 phFriNfc_OvrHal_CB_Transceive, (void *)OvrHal);
182 NFCSTATUS phFriNfc_OvrHal_Receive(phFriNfc_OvrHal_t *OvrHal,
183 phFriNfc_CplRt_t *CompletionInfo,
184 phHal_sRemoteDevInformation_t *RemoteDevInfo,
186 uint16_t *RecvLength)
188 NFCSTATUS status = NFCSTATUS_PENDING;
190 /* Check the Input Parameters */
191 if( (NULL==OvrHal) || (NULL==CompletionInfo) || (NULL==RemoteDevInfo)
192 || (NULL==RecvBuf) || (NULL==RecvLength) )
194 status = PHNFCSTVAL(CID_FRI_NFC_OVR_HAL ,NFCSTATUS_INVALID_PARAMETER);
198 /* Get the remote dev type */
199 OvrHal->TransactInfo.remotePCDType = RemoteDevInfo->RemDevType;
200 /* Save the receive buffer for use in callback */
201 OvrHal->sReceiveData.buffer = RecvBuf;
202 OvrHal->sReceiveData.length = *RecvLength;
204 OvrHal->pndef_recv_length = RecvLength;
206 /* Set the callback */
207 phFriNfc_OvrHal_SetComplInfo(OvrHal, CompletionInfo, PH_FRINFC_OVRHAL_RCV);
209 /* Call the HAL 4.0 Receive Function */
210 status = phHal4Nfc_Receive( OvrHal->psHwReference,
211 &OvrHal->TransactInfo,
212 phFriNfc_OvrHal_CB_Receive,
218 NFCSTATUS phFriNfc_OvrHal_Send(phFriNfc_OvrHal_t *OvrHal,
219 phFriNfc_CplRt_t *CompletionInfo,
220 phHal_sRemoteDevInformation_t *RemoteDevInfo,
224 NFCSTATUS status = NFCSTATUS_PENDING;
226 /* Check the Input Parameters */
227 if( (NULL==OvrHal) || (NULL==CompletionInfo) || (NULL==RemoteDevInfo) || (NULL==SendBuf) )
229 status = PHNFCSTVAL(CID_FRI_NFC_OVR_HAL ,NFCSTATUS_INVALID_PARAMETER);
233 /* Get the remote dev type */
234 OvrHal->TransactInfo.remotePCDType = RemoteDevInfo->RemDevType;
235 /* Save the receive buffer for use in callback */
236 OvrHal->sSendData.buffer = SendBuf;
237 OvrHal->sSendData.length = SendLength;
239 /* Set the callback */
240 phFriNfc_OvrHal_SetComplInfo(OvrHal, CompletionInfo, PH_FRINFC_OVRHAL_SND);
242 /* Call the HAL 4.0 Receive Function */
243 status = phHal4Nfc_Send( OvrHal->psHwReference,
244 &OvrHal->TransactInfo,
246 phFriNfc_OvrHal_CB_Send,
252 #ifndef PH_FRINFC_MAP_MIFARESTD_DISABLED
255 NFCSTATUS phFriNfc_OvrHal_Reconnect(phFriNfc_OvrHal_t *OvrHal,
256 phFriNfc_CplRt_t *CompletionInfo,
257 phHal_sRemoteDevInformation_t *RemoteDevInfo)
259 NFCSTATUS status = NFCSTATUS_PENDING;
261 /* Check the Input Parameters */
262 if((NULL == OvrHal) || (NULL == CompletionInfo) || (NULL == RemoteDevInfo))
264 status = PHNFCSTVAL(CID_FRI_NFC_OVR_HAL ,NFCSTATUS_INVALID_PARAMETER);
268 phFriNfc_OvrHal_SetComplInfo(OvrHal, CompletionInfo, PH_FRINFC_OVRHAL_DIS);
270 status = phHal4Nfc_Connect(
271 OvrHal->psHwReference,
273 phFriNfc_OvrHal_CB_ConnectDisconnect,
282 NFCSTATUS phFriNfc_OvrHal_Connect(phFriNfc_OvrHal_t *OvrHal,
283 phFriNfc_CplRt_t *CompletionInfo,
284 phHal_sRemoteDevInformation_t *RemoteDevInfo,
285 phHal_sDevInputParam_t *DevInputParam)
287 NFCSTATUS status = NFCSTATUS_PENDING;
289 /* Check the Input Parameters */
290 if((NULL == OvrHal) || (NULL == CompletionInfo) || (NULL == RemoteDevInfo) ||
291 (NULL == DevInputParam))
293 status = PHNFCSTVAL(CID_FRI_NFC_OVR_HAL ,NFCSTATUS_INVALID_PARAMETER);
297 phFriNfc_OvrHal_SetComplInfo(OvrHal, CompletionInfo, PH_FRINFC_OVRHAL_CON);
299 status = phHal4Nfc_Connect(
300 OvrHal->psHwReference,
302 phFriNfc_OvrHal_CB_ConnectDisconnect,
312 static void phFriNfc_OvrHal_CB_Transceive(void *context,
313 phHal_sRemoteDevInformation_t *RemoteDevHandle,
314 phNfc_sData_t *pRecvdata,
319 phFriNfc_OvrHal_t *OvrHal = (phFriNfc_OvrHal_t *)context;
323 if(NULL != pRecvdata && OvrHal->TranceiveInfo.sRecvData.buffer != NULL && pRecvdata->buffer != NULL)
325 /* Work-around for the NFCIP Tranceive API */
326 memcpy(OvrHal->TranceiveInfo.sRecvData.buffer, pRecvdata->buffer, pRecvdata->length);
327 if (OvrHal->pndef_recv_length != NULL)
329 *OvrHal->pndef_recv_length = (uint16_t) pRecvdata->length;
332 if(NULL != RemoteDevHandle)
334 /* Fix for Warning 4100 */
335 RemoteDevHandle=RemoteDevHandle;
338 if (NULL != OvrHal->TemporaryCompletionInfo.CompletionRoutine)
340 OvrHal->TemporaryCompletionInfo.CompletionRoutine(
341 OvrHal->TemporaryCompletionInfo.Context,
347 static void phFriNfc_OvrHal_CB_Send(void *context,
350 phFriNfc_OvrHal_t *OvrHal = (phFriNfc_OvrHal_t *)context;
354 if (NULL != OvrHal->TemporarySndCompletionInfo.CompletionRoutine)
356 OvrHal->TemporarySndCompletionInfo.CompletionRoutine(
357 OvrHal->TemporarySndCompletionInfo.Context,
363 static void phFriNfc_OvrHal_CB_Receive(void *context,
364 phNfc_sData_t *pDataInfo,
367 phFriNfc_OvrHal_t *OvrHal = (phFriNfc_OvrHal_t *)context;
371 /* Copy the received buffer */
372 if(NULL != pDataInfo && OvrHal->sReceiveData.buffer != NULL && pDataInfo->buffer != NULL)
374 memcpy(OvrHal->sReceiveData.buffer, pDataInfo->buffer, pDataInfo->length);
375 *OvrHal->pndef_recv_length = (uint16_t) pDataInfo->length;
378 if (NULL != OvrHal->TemporaryRcvCompletionInfo.CompletionRoutine)
380 OvrHal->TemporaryRcvCompletionInfo.CompletionRoutine(
381 OvrHal->TemporaryRcvCompletionInfo.Context,
387 static void phFriNfc_OvrHal_CB_ConnectDisconnect(void *context,
388 phHal_sRemoteDevInformation_t *RemoteDevHandle,
393 phFriNfc_OvrHal_t *OvrHal = (phFriNfc_OvrHal_t *)context;
397 if (RemoteDevHandle != NULL)
399 /* Fix for Warning 4100 */
400 RemoteDevHandle = RemoteDevHandle;
404 status = NFCSTATUS_FAILED;
407 OvrHal->TemporaryCompletionInfo.CompletionRoutine(
408 OvrHal->TemporaryCompletionInfo.Context, status);
413 static void phFriNfc_OvrHal_SetComplInfo(phFriNfc_OvrHal_t *OvrHal,
414 phFriNfc_CplRt_t *CompletionInfo,
418 OvrHal->Operation = Operation;
421 case PH_FRINFC_OVRHAL_RCV:
423 OvrHal->TemporaryRcvCompletionInfo.CompletionRoutine = CompletionInfo->CompletionRoutine;
424 OvrHal->TemporaryRcvCompletionInfo.Context = CompletionInfo->Context;
427 case PH_FRINFC_OVRHAL_SND:
429 OvrHal->TemporarySndCompletionInfo.CompletionRoutine = CompletionInfo->CompletionRoutine;
430 OvrHal->TemporarySndCompletionInfo.Context = CompletionInfo->Context;
435 OvrHal->TemporaryCompletionInfo.CompletionRoutine = CompletionInfo->CompletionRoutine;
436 OvrHal->TemporaryCompletionInfo.Context = CompletionInfo->Context;