merge with master
[adaptation/devices/nfc-plugin-nxp.git] / src / phLibNfc_initiator.c
1 /*\r
2  * Copyright (C) 2010 NXP Semiconductors\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *      http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /*!\r
18  * \file phLibNfc_initiator.c\r
19 \r
20  * Project: NFC FRI 1.1\r
21  *\r
22  * $Date: Fri Apr 23 14:34:08 2010 $\r
23  * $Author: ing07385 $\r
24  * $Revision: 1.53 $\r
25  * $Aliases: NFC_FRI1.1_WK1014_SDK,NFC_FRI1.1_WK1017_PREP1,NFC_FRI1.1_WK1017_R34_1,NFC_FRI1.1_WK1017_R34_2,NFC_FRI1.1_WK1019_SDK,NFC_FRI1.1_WK1024_SDK $\r
26  *\r
27  */\r
28 \r
29 /*\r
30 ************************* Header Files ****************************************\r
31 */\r
32 \r
33 #include <phLibNfcStatus.h>\r
34 #include <phLibNfc.h>\r
35 #include <phHal4Nfc.h>\r
36 #include <phOsalNfc.h>\r
37 #include <phLibNfc_Internal.h>\r
38 #include <phLibNfc_SE.h>\r
39 #include <phLibNfc_ndef_raw.h>\r
40 #include <phLibNfc_initiator.h>\r
41 #include <phLibNfc_discovery.h>\r
42 \r
43 \r
44 /*\r
45 *************************** Macro's  ****************************************\r
46 */\r
47 \r
48 #ifndef STATIC_DISABLE\r
49 #define STATIC static\r
50 #else\r
51 #define STATIC\r
52 #endif\r
53 \r
54 /*\r
55 *************************** Global Variables **********************************\r
56 */\r
57 \r
58 #define PN544_IO_TIMEOUT_RESPONSE 0x89\r
59 \r
60 /*\r
61 *************************** Static Function Declaration ***********************\r
62 */\r
63 \r
64 /* Target discvovery notification callback */\r
65 STATIC void phLibNfc_NotificationRegister_Resp_Cb ( \r
66                                 void                             *context,\r
67                                 phHal_eNotificationType_t        type,\r
68                                 phHal4Nfc_NotificationInfo_t     info,\r
69                                 NFCSTATUS                        status\r
70                                 );\r
71 \r
72 /*Remote device connect response callback*/\r
73 STATIC void phLibNfc_RemoteDev_Connect_Cb(\r
74                            void        *pContext,                           \r
75                            phHal_sRemoteDevInformation_t *pRmtdev_info,\r
76                            NFCSTATUS    status\r
77                            );\r
78 \r
79 #ifdef RECONNECT_SUPPORT\r
80 STATIC \r
81 void \r
82 phLibNfc_config_discovery_con_failure_cb (\r
83     void                *context,\r
84     NFCSTATUS           status);\r
85 #endif /* #ifdef RECONNECT_SUPPORT */\r
86 \r
87 /*Remote device disconnect response callback*/\r
88 STATIC void phLibNfc_RemoteDev_Disconnect_cb(                        \r
89                                 void                          *context,\r
90                                 phHal_sRemoteDevInformation_t *reg_handle,\r
91                                 NFCSTATUS                      status\r
92                                 );\r
93 /*Remote device Transceive response callback*/\r
94 STATIC void phLibNfc_RemoteDev_Transceive_Cb(void *context,\r
95                                 phHal_sRemoteDevInformation_t *pRmtdev_info,\r
96                                 phNfc_sData_t *response,\r
97                                 NFCSTATUS status\r
98                                 );\r
99 /*Set P2P config paramater response callback*/\r
100 STATIC void phLibNfc_Mgt_SetP2P_ConfigParams_Cb(\r
101                                 void                             *context,\r
102                                 NFCSTATUS                        status\r
103                                 );\r
104 \r
105 \r
106 /*\r
107 *************************** Function Definitions ******************************\r
108 */\r
109 \r
110 /**\r
111 * Response to target discovery.\r
112 */\r
113 STATIC\r
114 void phLibNfc_NotificationRegister_Resp_Cb ( \r
115                                 void                            *context,\r
116                                 phHal_eNotificationType_t       type,\r
117                                 phHal4Nfc_NotificationInfo_t    info,\r
118                                 NFCSTATUS                       status\r
119                                 )\r
120 {\r
121     NFCSTATUS RetVal = NFCSTATUS_SUCCESS,\r
122               Status = NFCSTATUS_SUCCESS;\r
123     uint16_t DeviceIndx, DeviceIndx1;\r
124     uint8_t sak_byte=0;\r
125     uint8_t tag_disc_flg = 0;\r
126     phLibNfc_NtfRegister_RspCb_t pClientCb=NULL;\r
127     pClientCb =gpphLibContext->CBInfo.pClientNtfRegRespCB;\r
128         PHNFC_UNUSED_VARIABLE(context);\r
129     \r
130 \r
131     if(( type != NFC_DISCOVERY_NOTIFICATION )\r
132         &&(PHNFCSTATUS(status)!=NFCSTATUS_DESELECTED))\r
133     {\r
134         Status = NFCSTATUS_FAILED;\r
135     }\r
136     else if (PHNFCSTATUS(status) == NFCSTATUS_DESELECTED)\r
137     {\r
138         return;\r
139     }\r
140         else\r
141         {\r
142                 DeviceIndx=0;DeviceIndx1=0;\r
143                 while(DeviceIndx < info.psDiscoveryInfo->NumberOfDevices)\r
144                 {\r
145                         switch(info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx]->RemDevType)\r
146                         {\r
147                                 case  phHal_eMifare_PICC:\r
148                                 {\r
149                                         /*Mifare Tag discovered*/\r
150                                         sak_byte =  info.psDiscoveryInfo->\r
151                                                                 ppRemoteDevInfo[DeviceIndx]->RemoteDevInfo.Iso14443A_Info.Sak;\r
152                                         if((TRUE == gpphLibContext->RegNtfType.MifareUL)&& (sak_byte==0x00))\r
153                                         {\r
154                                                 /*Copy the tag related info*/\r
155                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
156                                                         info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
157                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
158                                                         (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
159                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
160                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
161                                                 DeviceIndx1++;\r
162                                                 tag_disc_flg++;\r
163                                         }\r
164                 \r
165                                         if((TRUE == gpphLibContext->RegNtfType.MifareStd)&& \r
166                                                 (((sak_byte & 0x18)==0x08)||((sak_byte & 0x18)==0x18)))\r
167                                         {\r
168                                                 /*Copy the tag related info*/\r
169                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
170                                                         info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
171                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
172                                                         (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
173                                                 gpphLibContext->Discov_handle[DeviceIndx1]= \r
174                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
175                                                 DeviceIndx1++;\r
176                                                 tag_disc_flg++;\r
177                                         }\r
178 \r
179                                 }break;\r
180                                 case  phHal_eISO14443_A_PICC:\r
181                                 {\r
182                                         /*ISO 14443-A type tag discovered*/\r
183                                         if(TRUE == gpphLibContext->RegNtfType.ISO14443_4A)\r
184                                         {\r
185                                                 /*Copy the ISO type A tag info*/\r
186                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
187                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
188                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
189                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
190                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
191                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
192                                                 DeviceIndx1++;\r
193                                                 tag_disc_flg++;\r
194                                         }\r
195                                 }break;\r
196                                 case  phHal_eISO14443_3A_PICC:\r
197                                 {\r
198                                         /*ISO 14443-A type tag discovered*/\r
199                                         if(TRUE == gpphLibContext->RegNtfType.MifareUL)\r
200                                         {\r
201                                                 /*Copy the ISO type A tag info*/\r
202                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
203                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
204                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
205                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
206                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
207                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
208                                                 DeviceIndx1++;\r
209                                                 tag_disc_flg++;\r
210                                         }\r
211                                 }break;\r
212                                 case  phHal_eISO14443_B_PICC:\r
213                                 {\r
214                                         /*ISO 14443-B type tag Discovered */\r
215                                         if(TRUE == gpphLibContext->RegNtfType.ISO14443_4B)\r
216                                         {\r
217                                                 /*Copy the Type B tag info */\r
218                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
219                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
220                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
221                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
222                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
223                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
224                                                 DeviceIndx1++;\r
225                                                 tag_disc_flg++;\r
226                                         }\r
227                                 }break;\r
228                                 case  phHal_eFelica_PICC:\r
229                                 {\r
230                                         /*Felica Type Tag Discovered */\r
231                                         if(TRUE == gpphLibContext->RegNtfType.Felica)\r
232                                         {\r
233                                                 /*Copy the Felica tag info */\r
234                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
235                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
236                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
237                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
238                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
239                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
240                                                 DeviceIndx1++;\r
241                                                 tag_disc_flg++;\r
242                                         }\r
243                                 }break;\r
244                                 case  phHal_eJewel_PICC:\r
245                                 {\r
246                                         /*Jewel Type Tag Discovered */\r
247                                         if(TRUE == gpphLibContext->RegNtfType.Jewel)\r
248                                         {\r
249                                                 /*Copy the Felica tag info */\r
250                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
251                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
252                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
253                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
254                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
255                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
256                                                 DeviceIndx1++;\r
257                                                 tag_disc_flg++;\r
258                                         }\r
259                                 }\r
260                                 break;\r
261                                 case  phHal_eISO15693_PICC:\r
262                                 {\r
263                                         /*Jewel Type Tag Discovered */\r
264                                         if(TRUE == gpphLibContext->RegNtfType.ISO15693)\r
265                                         {\r
266                                                 /*Copy the Felica tag info */\r
267                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
268                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
269                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
270                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
271                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
272                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
273                                                 DeviceIndx1++;\r
274                                                 tag_disc_flg++;\r
275                                         }\r
276                                 }\r
277                                 break;\r
278                                 case  phHal_eNfcIP1_Target:\r
279                                 {\r
280                                         if(TRUE == gpphLibContext->RegNtfType.NFC)\r
281                                         {\r
282                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
283                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
284                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
285                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx].psRemoteDevInfo;\r
286                                                 gpphLibContext->Discov_handle[DeviceIndx1] = \r
287                                                         gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
288                                                 DeviceIndx1++;\r
289                                                 tag_disc_flg++;\r
290                                         }\r
291                                 }\r
292                                 break;\r
293                                 case  phHal_eNfcIP1_Initiator:\r
294                                 {\r
295                                         if(TRUE == gpphLibContext->RegNtfType.NFC)\r
296                                         {\r
297                                                 gpphLibContext->LibNfcState.cur_state=eLibNfcHalStateConnect;\r
298                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo=\r
299                                                                 info.psDiscoveryInfo->ppRemoteDevInfo[DeviceIndx];\r
300                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev =\r
301                                                                 (uint32_t)gpphLibContext->psRemoteDevList[DeviceIndx1].psRemoteDevInfo;\r
302                                                 gpphLibContext->sNfcIp_Context.Rem_Initiator_Handle=\r
303                                                                 gpphLibContext->psRemoteDevList[DeviceIndx1].hTargetDev;\r
304                                                 DeviceIndx1++;\r
305                                                 tag_disc_flg++;\r
306                                         }\r
307                                 }\r
308                                 break;\r
309                                 default :\r
310                                 {\r
311                                         break;\r
312                                 }\r
313                         }\r
314                         DeviceIndx++;\r
315                 }\r
316         }\r
317 \r
318     if((tag_disc_flg >0 )&&(status != NFCSTATUS_FAILED))\r
319     {\r
320         gpphLibContext->dev_cnt = tag_disc_flg;\r
321         /* Check for if the discovered tags are multiple or\r
322          Multiple protocol tag */\r
323         if((gpphLibContext->dev_cnt > 1)&&(\r
324             (status ==NFCSTATUS_MULTIPLE_PROTOCOLS) ||\r
325             (status ==NFCSTATUS_MULTIPLE_TAGS)) )\r
326         {\r
327             status = status;\r
328         }\r
329         else\r
330         {\r
331             status =NFCSTATUS_SUCCESS;\r
332         }\r
333         /*Notify to upper layer the no of tag discovered and\r
334           the protocol */\r
335         if (NULL != pClientCb)\r
336         {\r
337             pClientCb(\r
338                                         (void*)gpphLibContext->CBInfo.pClientNtfRegRespCntx,\r
339                     gpphLibContext->psRemoteDevList,\r
340                     gpphLibContext->dev_cnt,\r
341                                         status\r
342                     );\r
343         }\r
344 \r
345     }\r
346     else if(PHNFCSTATUS(status)==NFCSTATUS_DESELECTED)\r
347     {\r
348         info.psDiscoveryInfo->NumberOfDevices = 0;\r
349         if (NULL != pClientCb)\r
350         {\r
351             gpphLibContext->LibNfcState.cur_state=eLibNfcHalStateRelease;\r
352             pClientCb((void*)gpphLibContext->CBInfo.pClientNtfRegRespCntx,\r
353                     NULL,\r
354                     0,\r
355                     status);\r
356         }\r
357 \r
358     }\r
359     else /*Reconfigure the discovery wheel*/\r
360     {\r
361         RetVal = phHal4Nfc_ConfigureDiscovery ( gpphLibContext->psHwReference,\r
362                                             NFC_DISCOVERY_RESUME,\r
363                                             &(gpphLibContext->sADDconfig),\r
364                                             phLibNfc_config_discovery_cb,\r
365                                             gpphLibContext);\r
366 \r
367         if((RetVal!=NFCSTATUS_SUCCESS) &&(RetVal!=NFCSTATUS_PENDING))\r
368         {\r
369             Status = NFCSTATUS_FAILED;\r
370         }\r
371 \r
372     }\r
373     if(Status == NFCSTATUS_FAILED)\r
374     {\r
375         if (NULL != pClientCb)\r
376         {\r
377             pClientCb(gpphLibContext->CBInfo.pClientNtfRegRespCntx,\r
378                 NULL,\r
379                 0,\r
380                 Status);\r
381         }\r
382     }\r
383     return;\r
384 }\r
385 \r
386 /**\r
387 * This interface registers notification handler for target discovery.\r
388 */\r
389 NFCSTATUS   \r
390 phLibNfc_RemoteDev_NtfRegister( \r
391                         phLibNfc_Registry_Info_t*       pRegistryInfo,\r
392                         phLibNfc_NtfRegister_RspCb_t    pNotificationHandler,\r
393                         void                            *pContext\r
394                         )\r
395 {\r
396     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;\r
397     \r
398 \r
399     /*Check for valid parameters*/\r
400     if((NULL == pNotificationHandler)\r
401         || (NULL == pContext) \r
402         ||(NULL== pRegistryInfo))\r
403     {\r
404         RetVal= NFCSTATUS_INVALID_PARAMETER;\r
405     }\r
406     else if((NULL == gpphLibContext) ||\r
407         (gpphLibContext->LibNfcState.cur_state\r
408                             == eLibNfcHalStateShutdown))\r
409     {\r
410         RetVal = NFCSTATUS_NOT_INITIALISED;\r
411     }\r
412     else if(gpphLibContext->LibNfcState.next_state\r
413                             == eLibNfcHalStateShutdown)\r
414     {\r
415         /*Next state is shutdown*/\r
416         RetVal= NFCSTATUS_SHUTDOWN;\r
417     }\r
418     else\r
419     {\r
420         \r
421         PHDBG_INFO("LibNfc:Registering Notification Handler");\r
422                 \r
423       \r
424         (void) memcpy(&(gpphLibContext->RegNtfType),pRegistryInfo,\r
425                         sizeof(phLibNfc_Registry_Info_t));\r
426         /* Register Discovery Notification Handler*/     \r
427        \r
428                 /*Register for NFCIP1 target type*/        \r
429                 RetVal = phHal4Nfc_RegisterNotification(\r
430                                 gpphLibContext->psHwReference,\r
431                                 eRegisterP2PDiscovery,\r
432                                 phLibNfc_NotificationRegister_Resp_Cb,\r
433                                 (void*)gpphLibContext\r
434                                 );\r
435         /*Register for Tag discovery*/        \r
436                 RetVal = phHal4Nfc_RegisterNotification(\r
437                             gpphLibContext->psHwReference,\r
438                             eRegisterTagDiscovery,\r
439                             phLibNfc_NotificationRegister_Resp_Cb,\r
440                             (void*)gpphLibContext\r
441                             );   \r
442         gpphLibContext->CBInfo.pClientNtfRegRespCB = pNotificationHandler;\r
443         gpphLibContext->CBInfo.pClientNtfRegRespCntx = pContext;\r
444         /*Register notification handler with below layer*/\r
445         \r
446     }\r
447     return RetVal;\r
448 }\r
449 /**\r
450 * This interface unregisters notification handler for target discovery.\r
451 */\r
452 NFCSTATUS phLibNfc_RemoteDev_NtfUnregister(void)\r
453 {\r
454     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;\r
455     if((NULL == gpphLibContext) ||\r
456        (gpphLibContext->LibNfcState.cur_state\r
457                             == eLibNfcHalStateShutdown))\r
458     {\r
459         /*Lib Nfc not Initialized*/\r
460         RetVal = NFCSTATUS_NOT_INITIALISED;\r
461     }\r
462     else if(gpphLibContext->LibNfcState.next_state\r
463                             == eLibNfcHalStateShutdown)\r
464     {\r
465         /*Lib Nfc Shutdown*/\r
466         RetVal= NFCSTATUS_SHUTDOWN;\r
467     }\r
468     else\r
469     {\r
470         /*Unregister notification handler with lower layer */\r
471         RetVal = phHal4Nfc_UnregisterNotification(\r
472                                     gpphLibContext->psHwReference,\r
473                                     eRegisterP2PDiscovery,\r
474                                     gpphLibContext);\r
475 \r
476                 RetVal = phHal4Nfc_UnregisterNotification(\r
477                                     gpphLibContext->psHwReference,\r
478                                     eRegisterTagDiscovery,\r
479                                     gpphLibContext);\r
480 \r
481         gpphLibContext->CBInfo.pClientNtfRegRespCB = NULL;\r
482         gpphLibContext->CBInfo.pClientNtfRegRespCntx =NULL;\r
483         PHDBG_INFO("LibNfc:Unregister Notification Handler");\r
484     }\r
485     return RetVal;\r
486 }\r
487 \r
488 #ifdef RECONNECT_SUPPORT\r
489 \r
490 NFCSTATUS \r
491 phLibNfc_RemoteDev_ReConnect (\r
492     phLibNfc_Handle                 hRemoteDevice,\r
493     pphLibNfc_ConnectCallback_t     pNotifyReConnect_RspCb,\r
494     void                            *pContext)\r
495 {\r
496 \r
497     NFCSTATUS                           ret_val = NFCSTATUS_FAILED;\r
498     phLibNfc_sRemoteDevInformation_t    *psRemoteDevInfo = NULL;\r
499 \r
500     if ((NULL == gpphLibContext) \r
501       || (eLibNfcHalStateShutdown == \r
502         gpphLibContext->LibNfcState.cur_state))\r
503     {\r
504          ret_val = NFCSTATUS_NOT_INITIALISED;        \r
505     }\r
506     else if ((NULL == pContext)\r
507         || (NULL == pNotifyReConnect_RspCb)\r
508         || (NULL == (void *)hRemoteDevice))\r
509     {\r
510         /* Check valid parameters */\r
511         ret_val = NFCSTATUS_INVALID_PARAMETER;\r
512     }   \r
513     /* Check valid lib nfc State */\r
514     else if (gpphLibContext->LibNfcState.next_state\r
515              == eLibNfcHalStateShutdown)\r
516     {\r
517         ret_val = NFCSTATUS_SHUTDOWN;\r
518     }\r
519     else if (0 == gpphLibContext->Connected_handle)\r
520     {\r
521         ret_val = NFCSTATUS_TARGET_NOT_CONNECTED;\r
522     }\r
523     else if ((gpphLibContext->Discov_handle[0] != hRemoteDevice)\r
524                 && (gpphLibContext->Discov_handle[1] != hRemoteDevice)\r
525                 && (gpphLibContext->Discov_handle[2] != hRemoteDevice)\r
526                 && (gpphLibContext->Discov_handle[3] != hRemoteDevice)\r
527                 && (gpphLibContext->Discov_handle[4] != hRemoteDevice)\r
528                 && (gpphLibContext->Discov_handle[5] != hRemoteDevice)\r
529                 && (gpphLibContext->Discov_handle[6] != hRemoteDevice)\r
530                 && (gpphLibContext->Discov_handle[7] != hRemoteDevice)\r
531                 && (gpphLibContext->Discov_handle[8] != hRemoteDevice)\r
532                 && (gpphLibContext->Discov_handle[9] != hRemoteDevice))\r
533     {\r
534         ret_val = NFCSTATUS_INVALID_HANDLE;\r
535     }\r
536     else\r
537     {\r
538         psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t *)hRemoteDevice;\r
539        \r
540         /* Call the HAL connect*/\r
541         ret_val = phHal4Nfc_Connect (gpphLibContext->psHwReference,\r
542                                psRemoteDevInfo,\r
543                                phLibNfc_RemoteDev_Connect_Cb,\r
544                                (void *)gpphLibContext);\r
545 \r
546         if (NFCSTATUS_PENDING == ret_val)\r
547         {\r
548             /* If HAL Connect is pending update the LibNFC state machine \r
549                 and store the CB pointer and Context,\r
550                 mark the General CB pending status is TRUE */\r
551             gpphLibContext->CBInfo.pClientConnectCb = pNotifyReConnect_RspCb;\r
552             gpphLibContext->CBInfo.pClientConCntx = pContext;\r
553             gpphLibContext->status.GenCb_pending_status = TRUE;\r
554                         gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;            \r
555 \r
556             gpphLibContext->Prev_Connected_handle = gpphLibContext->Connected_handle;\r
557 \r
558                         gpphLibContext->Connected_handle = hRemoteDevice;\r
559          }\r
560          else if (NFCSTATUS_INVALID_REMOTE_DEVICE == PHNFCSTATUS(ret_val))\r
561          {\r
562            /* The Handle given for connect is invalid*/\r
563             ret_val = NFCSTATUS_TARGET_NOT_CONNECTED;\r
564          }\r
565          else\r
566          {\r
567             /* Lower layer returns internal error code return NFCSTATUS_FAILED*/\r
568             ret_val = NFCSTATUS_FAILED;\r
569          }        \r
570     }\r
571 \r
572     return ret_val;\r
573 }\r
574 #endif /* #ifdef RECONNECT_SUPPORT */\r
575 \r
576 \r
577 /**\r
578 * Connect to a single Remote Device \r
579 */\r
580 NFCSTATUS phLibNfc_RemoteDev_Connect(\r
581                     phLibNfc_Handle             hRemoteDevice,\r
582                     pphLibNfc_ConnectCallback_t pNotifyConnect_RspCb,\r
583                     void                        *pContext\r
584                     )\r
585 {\r
586 \r
587     NFCSTATUS RetVal = NFCSTATUS_FAILED;\r
588     phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo;\r
589     \r
590     if((NULL == gpphLibContext) ||\r
591       (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))   \r
592     {\r
593          RetVal = NFCSTATUS_NOT_INITIALISED;        \r
594     }/* Check valid parameters*/\r
595     else if((NULL == pContext)\r
596         || (NULL == pNotifyConnect_RspCb)\r
597         || (NULL == (void*)hRemoteDevice))\r
598     {\r
599        RetVal= NFCSTATUS_INVALID_PARAMETER;\r
600     }   \r
601     /* Check valid lib nfc State*/\r
602     else if(gpphLibContext->LibNfcState.next_state\r
603                             == eLibNfcHalStateShutdown)\r
604     {\r
605         RetVal= NFCSTATUS_SHUTDOWN;\r
606     }\r
607     else if((gpphLibContext->Discov_handle[0] != hRemoteDevice)&&\r
608                 (gpphLibContext->Discov_handle[1] != hRemoteDevice)&&\r
609                 (gpphLibContext->Discov_handle[2] != hRemoteDevice)&&\r
610                 (gpphLibContext->Discov_handle[3] != hRemoteDevice)&&\r
611                 (gpphLibContext->Discov_handle[4] != hRemoteDevice)&&\r
612                 (gpphLibContext->Discov_handle[5] != hRemoteDevice)&&\r
613                 (gpphLibContext->Discov_handle[6] != hRemoteDevice)&&\r
614                 (gpphLibContext->Discov_handle[7] != hRemoteDevice)&&\r
615                 (gpphLibContext->Discov_handle[8] != hRemoteDevice)&&\r
616                 (gpphLibContext->Discov_handle[9] != hRemoteDevice))\r
617     {\r
618         RetVal= NFCSTATUS_INVALID_HANDLE;\r
619     }\r
620     else if ((hRemoteDevice != gpphLibContext->Connected_handle) \r
621         && (0 != gpphLibContext->Connected_handle))\r
622     {\r
623         RetVal = NFCSTATUS_FAILED;\r
624     }\r
625     else\r
626     {\r
627         psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice;\r
628        \r
629         /* Call the HAL connect*/\r
630         RetVal = phHal4Nfc_Connect(gpphLibContext->psHwReference,\r
631                                psRemoteDevInfo,\r
632                                phLibNfc_RemoteDev_Connect_Cb,\r
633                                (void* )gpphLibContext);\r
634         if(RetVal== NFCSTATUS_PENDING)\r
635         {\r
636             /* If HAL Connect is pending update the LibNFC state machine \r
637                 and store the CB pointer and Context,\r
638                 mark the General CB pending status is TRUE*/\r
639             gpphLibContext->CBInfo.pClientConnectCb = pNotifyConnect_RspCb;\r
640             gpphLibContext->CBInfo.pClientConCntx = pContext;\r
641             gpphLibContext->status.GenCb_pending_status=TRUE;\r
642                         gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;            \r
643             gpphLibContext->Prev_Connected_handle = gpphLibContext->Connected_handle;\r
644                         gpphLibContext->Connected_handle = hRemoteDevice;\r
645          }\r
646          else if(PHNFCSTATUS(RetVal) == NFCSTATUS_INVALID_REMOTE_DEVICE)\r
647          {\r
648            /* The Handle given for connect is invalid*/\r
649             RetVal= NFCSTATUS_TARGET_NOT_CONNECTED;\r
650          }\r
651          else\r
652          {\r
653             /* Lower layer returns internal error code return NFCSTATUS_FAILED*/\r
654             RetVal = NFCSTATUS_FAILED;\r
655          }        \r
656     }\r
657     return RetVal;\r
658 }\r
659 \r
660 #ifdef RECONNECT_SUPPORT\r
661 STATIC \r
662 void \r
663 phLibNfc_config_discovery_con_failure_cb (\r
664     void                *context,\r
665     NFCSTATUS           status)\r
666 {\r
667     if((phLibNfc_LibContext_t *)context == gpphLibContext)      \r
668     {   /*check for same context*/\r
669         pphLibNfc_ConnectCallback_t    ps_client_con_cb = \r
670                                     gpphLibContext->CBInfo.pClientConnectCb;\r
671 \r
672         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)\r
673         {\r
674             /*If shutdown called in between allow shutdown to happen*/\r
675             phLibNfc_Pending_Shutdown();\r
676             status = NFCSTATUS_SHUTDOWN;\r
677         }\r
678         else\r
679         {\r
680             gpphLibContext->status.GenCb_pending_status = FALSE;\r
681             gpphLibContext->status.DiscEnbl_status = FALSE;\r
682             status = NFCSTATUS_TARGET_LOST;\r
683 \r
684             phLibNfc_UpdateCurState (status,gpphLibContext);\r
685 #ifdef RESTART_CFG\r
686             if(gpphLibContext->status.Discovery_pending_status == TRUE)\r
687             {\r
688                 NFCSTATUS RetStatus = NFCSTATUS_FAILED;\r
689                 /* Application has called discovery before receiving this callback,\r
690                 so NO notification to the upper layer, instead lower layer\r
691                 discovery is called */\r
692                 gpphLibContext->status.Discovery_pending_status = FALSE;\r
693                 RetStatus =  phHal4Nfc_ConfigureDiscovery(\r
694                         gpphLibContext->psHwReference,\r
695                         gpphLibContext->eLibNfcCfgMode,\r
696                         &gpphLibContext->sADDconfig,\r
697                         (pphLibNfc_RspCb_t)\r
698                         phLibNfc_config_discovery_cb,\r
699                         (void *)gpphLibContext);\r
700                 if (NFCSTATUS_PENDING == RetStatus)\r
701                 {\r
702                     (void)phLibNfc_UpdateNextState(gpphLibContext,\r
703                                             eLibNfcHalStateConfigReady);\r
704                     gpphLibContext->status.GenCb_pending_status = TRUE;\r
705                     gpphLibContext->status.DiscEnbl_status = TRUE;\r
706                 }\r
707             }\r
708 \r
709 #endif /* #ifdef RESTART_CFG */\r
710         }\r
711 \r
712         if (NULL != ps_client_con_cb)\r
713         {\r
714             gpphLibContext->CBInfo.pClientConnectCb = NULL;\r
715             /* Call the upper layer callback*/      \r
716             ps_client_con_cb (gpphLibContext->CBInfo.pClientConCntx,\r
717                             0, NULL, status);\r
718         }\r
719     } /*End of if-context check*/\r
720     else\r
721     {   /*exception: wrong context pointer returned*/\r
722         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
723         status = NFCSTATUS_FAILED;\r
724     }\r
725 \r
726     \r
727 }\r
728 #endif /* #ifdef RECONNECT_SUPPORT */\r
729 /**\r
730 * Response callback for remote device connect\r
731 */\r
732 STATIC void phLibNfc_RemoteDev_Connect_Cb(\r
733                            void        *pContext,\r
734                            phHal_sRemoteDevInformation_t *pRmtdev_info,\r
735                            NFCSTATUS    status\r
736                            )\r
737 {\r
738     NFCSTATUS             Connect_status = NFCSTATUS_SUCCESS;\r
739     /*Check valid lib nfc context is returned from lower layer*/\r
740     if((phLibNfc_LibContext_t *)pContext == gpphLibContext)\r
741     {\r
742         gpphLibContext->LastTrancvSuccess = FALSE;\r
743 \r
744         /* Mark General Callback pending status as false*/\r
745         gpphLibContext->status.GenCb_pending_status = FALSE;\r
746         \r
747         /* Check the shutdown is called during the lower layer Connect in process,\r
748            If yes call shutdown call and return NFCSTATUS_SHUTDOWN */\r
749         if((eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state))\r
750         {   \r
751             phLibNfc_Pending_Shutdown();\r
752             Connect_status = NFCSTATUS_SHUTDOWN;    \r
753     \r
754         }\r
755         else if(PHNFCSTATUS(status)==NFCSTATUS_SUCCESS)\r
756         {\r
757             /* Copy the Remote device address as connected handle*/\r
758             gpphLibContext->Connected_handle =(uint32_t) pRmtdev_info;\r
759             /* Update the state to connected and return status as SUCCESS*/\r
760             gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;\r
761             Connect_status = NFCSTATUS_SUCCESS;\r
762         }\r
763         else \r
764         {  /* if(PHNFCSTATUS(status)==NFCSTATUS_INVALID_REMOTE_DEVICE) */\r
765             /* If remote device is invalid return as TARGET LOST to upper layer*/\r
766             /* If error code is other than SUCCESS return NFCSTATUS_TARGET_LOST */\r
767             Connect_status = NFCSTATUS_TARGET_LOST;\r
768             gpphLibContext->Connected_handle = gpphLibContext->Prev_Connected_handle ;\r
769         }\r
770         gpphLibContext->ndef_cntx.is_ndef = CHK_NDEF_NOT_DONE;\r
771         /* Update the Current Sate*/\r
772         phLibNfc_UpdateCurState(Connect_status,(phLibNfc_LibContext_t *)pContext);\r
773         /* Call the upper layer callback*/      \r
774         gpphLibContext->CBInfo.pClientConnectCb(\r
775                     gpphLibContext->CBInfo.pClientConCntx,\r
776                     (uint32_t)pRmtdev_info,\r
777                     (phLibNfc_sRemoteDevInformation_t*)pRmtdev_info,\r
778                     Connect_status);\r
779     }\r
780     else\r
781     {   /*exception: wrong context pointer returned*/\r
782         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
783     }\r
784     return;\r
785 }\r
786 \r
787 /**\r
788 * Allows to disconnect from already connected target.\r
789 */\r
790 NFCSTATUS phLibNfc_RemoteDev_Disconnect( phLibNfc_Handle                 hRemoteDevice,\r
791                                         phLibNfc_eReleaseType_t          ReleaseType,\r
792                                         pphLibNfc_DisconnectCallback_t   pDscntCallback,\r
793                                         void*                            pContext\r
794                                         )\r
795 {\r
796     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;\r
797     phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo=NULL;\r
798 \r
799     /*Check for valid parameter*/\r
800     if((NULL == gpphLibContext) ||\r
801         (gpphLibContext->LibNfcState.cur_state\r
802                             == eLibNfcHalStateShutdown))\r
803     {\r
804         RetVal = NFCSTATUS_NOT_INITIALISED;        \r
805     }\r
806     else if((NULL == pContext) ||\r
807         (NULL == pDscntCallback)||(hRemoteDevice == 0))\r
808     {\r
809         RetVal= NFCSTATUS_INVALID_PARAMETER;\r
810     }\r
811     /* Check for valid state,If De initialize is called then\r
812     return NFCSTATUS_SHUTDOWN */\r
813     else if(gpphLibContext->LibNfcState.next_state\r
814                             == eLibNfcHalStateShutdown)\r
815     {\r
816         RetVal= NFCSTATUS_SHUTDOWN;\r
817     }\r
818     else if(gpphLibContext->Connected_handle==0)\r
819     {\r
820         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;\r
821     }\r
822     /* The given handle is not the connected handle return NFCSTATUS_INVALID_HANDLE*/\r
823     else if(hRemoteDevice != gpphLibContext->Connected_handle )\r
824     {\r
825         RetVal=NFCSTATUS_INVALID_HANDLE;\r
826     }\r
827     else\r
828     {\r
829         if((eLibNfcHalStateRelease == gpphLibContext->LibNfcState.next_state)\r
830             ||((gpphLibContext->sSeContext.eActivatedMode == phLibNfc_SE_ActModeWired)&&\r
831                     (ReleaseType != NFC_SMARTMX_RELEASE))\r
832                     ||((gpphLibContext->sSeContext.eActivatedMode != phLibNfc_SE_ActModeWired)&&\r
833                             (ReleaseType == NFC_SMARTMX_RELEASE)))\r
834         {   /* Previous disconnect callback is pending */\r
835             RetVal = NFCSTATUS_REJECTED;            \r
836         }\r
837 #ifndef LLCP_CHANGES\r
838         else if(eLibNfcHalStateTransaction == gpphLibContext->LibNfcState.next_state)\r
839         {   /* Previous  Transaction is Pending*/\r
840             RetVal = NFCSTATUS_BUSY;            \r
841             PHDBG_INFO("LibNfc:Transaction is Pending");\r
842         }\r
843 #endif /* #ifdef LLCP_CHANGES */\r
844         else\r
845         {           \r
846             gpphLibContext->ReleaseType = ReleaseType;\r
847             psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice;\r
848             RetVal = phHal4Nfc_Disconnect(gpphLibContext->psHwReference,\r
849                                 (phHal_sRemoteDevInformation_t*)psRemoteDevInfo,\r
850                                 gpphLibContext->ReleaseType,\r
851                                 (pphHal4Nfc_DiscntCallback_t)\r
852                                 phLibNfc_RemoteDev_Disconnect_cb,\r
853                                 (void *)gpphLibContext);\r
854             if( NFCSTATUS_PENDING == PHNFCSTATUS(RetVal))\r
855             {\r
856                 /*Copy the upper layer Callback pointer and context*/\r
857                 gpphLibContext->CBInfo.pClientDisConnectCb = pDscntCallback;\r
858                 gpphLibContext->CBInfo.pClientDConCntx = pContext;\r
859                 /* Mark general callback pending status as TRUE and update the state*/\r
860                 gpphLibContext->status.GenCb_pending_status=TRUE;\r
861                                 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateRelease; \r
862                 \r
863             }\r
864             else\r
865             {\r
866                 /*If lower layer returns other than pending \r
867                 (internal error codes) return NFCSTATUS_FAILED */\r
868                 RetVal = NFCSTATUS_FAILED;\r
869             }    \r
870         }\r
871     }\r
872     return RetVal;\r
873 }\r
874 /**\r
875 * Response callback for Remote device Disconnect.\r
876 */\r
877 STATIC void phLibNfc_RemoteDev_Disconnect_cb(\r
878                                 void                          *context,\r
879                                 phHal_sRemoteDevInformation_t *reg_handle,\r
880                                 NFCSTATUS                      status\r
881                                 )\r
882 {\r
883     NFCSTATUS             DisCnct_status = NFCSTATUS_SUCCESS;\r
884     pphLibNfc_DisconnectCallback_t pUpper_NtfCb = NULL;\r
885         void  *pUpper_Context = NULL;\r
886     \r
887     /* Copy the upper layer Callback and context*/\r
888     pUpper_NtfCb = gpphLibContext->CBInfo.pClientDisConnectCb;\r
889     pUpper_Context = gpphLibContext->CBInfo.pClientDConCntx;\r
890 \r
891     /* Check valid context is returned or not */\r
892     if((phLibNfc_LibContext_t *)context != gpphLibContext)\r
893     {\r
894         /*exception: wrong context pointer returned*/\r
895         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
896     }\r
897     else\r
898     {\r
899         /* Mark the General callback pending status FALSE   */\r
900         gpphLibContext->status.GenCb_pending_status = FALSE;        \r
901         gpphLibContext->CBInfo.pClientDisConnectCb = NULL;\r
902         gpphLibContext->CBInfo.pClientDConCntx = NULL;\r
903         \r
904         gpphLibContext->ndef_cntx.is_ndef = CHK_NDEF_NOT_DONE;\r
905         gpphLibContext->LastTrancvSuccess = FALSE;\r
906         /*Reset Connected handle */\r
907         gpphLibContext->Connected_handle=0x0000;\r
908         /*Reset previous Connected handle */\r
909         gpphLibContext->Prev_Connected_handle = 0x0000;\r
910 \r
911         if(gpphLibContext->sSeContext.eActivatedMode == phLibNfc_SE_ActModeWired)\r
912         {\r
913           gpphLibContext->sSeContext.eActivatedMode = phLibNfc_SE_ActModeDefault;\r
914         }\r
915         if(NULL != gpphLibContext->psBufferedAuth)\r
916         {\r
917             if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)\r
918             {\r
919                 phOsalNfc_FreeMemory(\r
920                     gpphLibContext->psBufferedAuth->sRecvData.buffer);\r
921             }\r
922             if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)\r
923             {\r
924                 phOsalNfc_FreeMemory(\r
925                     gpphLibContext->psBufferedAuth->sSendData.buffer);\r
926             }\r
927             phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);\r
928             gpphLibContext->psBufferedAuth = NULL;\r
929         }\r
930     }   \r
931     /* Check DeInit is called or not */\r
932     if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)\r
933     {\r
934         /*call shutdown and return  status as NFCSTATUS_SHUTDOWN */ \r
935         phLibNfc_Pending_Shutdown();\r
936         DisCnct_status = NFCSTATUS_SHUTDOWN;    \r
937     }\r
938     else if(NFCSTATUS_SUCCESS == status)\r
939     {\r
940         DisCnct_status = NFCSTATUS_SUCCESS;     \r
941                 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateRelease;         \r
942     }\r
943     else\r
944     {           \r
945         DisCnct_status = NFCSTATUS_FAILED;\r
946         phLibNfc_UpdateCurState(DisCnct_status,(phLibNfc_LibContext_t *)context);\r
947     }\r
948     /* Call the upper layer Callback */\r
949     (*pUpper_NtfCb)(pUpper_Context,\r
950                     (uint32_t)reg_handle,\r
951                     DisCnct_status);\r
952     return;\r
953 }\r
954 \r
955 /**\r
956 * This interface allows to perform Read/write operation on remote device.\r
957 */\r
958 NFCSTATUS\r
959 phLibNfc_RemoteDev_Transceive(phLibNfc_Handle                   hRemoteDevice,\r
960                               phLibNfc_sTransceiveInfo_t*       psTransceiveInfo,\r
961                               pphLibNfc_TransceiveCallback_t    pTransceive_RspCb,\r
962                               void*                             pContext\r
963                               )\r
964 {\r
965     NFCSTATUS RetVal = NFCSTATUS_SUCCESS;\r
966     \r
967     /*Check for valid parameter */\r
968     \r
969     if((NULL == gpphLibContext) ||\r
970         (gpphLibContext->LibNfcState.cur_state\r
971                             == eLibNfcHalStateShutdown))\r
972     {\r
973         RetVal = NFCSTATUS_NOT_INITIALISED;\r
974     }\r
975     else if((NULL == psTransceiveInfo)\r
976         || (NULL == pTransceive_RspCb)\r
977         || (NULL == (void *)hRemoteDevice)\r
978         || (NULL == psTransceiveInfo->sRecvData.buffer)\r
979         || (NULL == psTransceiveInfo->sSendData.buffer)\r
980         || (NULL == pContext))\r
981     {\r
982         RetVal= NFCSTATUS_INVALID_PARAMETER;        \r
983     }\r
984     /* Check the state for DeInit is called or not,if yes return NFCSTATUS_SHUTDOWN*/\r
985     else if(gpphLibContext->LibNfcState.next_state\r
986                             == eLibNfcHalStateShutdown)\r
987     {       \r
988         RetVal= NFCSTATUS_SHUTDOWN;\r
989     }/* If there is no handle connected return NFCSTATUS_TARGET_NOT_CONNECTED*/\r
990     else if(gpphLibContext->Connected_handle==0)\r
991     {\r
992         RetVal=NFCSTATUS_TARGET_NOT_CONNECTED;\r
993     }/* If the given handle is not the connected handle return NFCSTATUS_INVALID_HANDLE */\r
994         else if(gpphLibContext->Connected_handle!= hRemoteDevice )\r
995     {\r
996         RetVal=NFCSTATUS_INVALID_HANDLE;\r
997     } /*If the transceive is called before finishing the previous transceive function\r
998       return NFCSTATUS_REJECTED  */\r
999     else if((eLibNfcHalStateTransaction ==\r
1000         gpphLibContext->LibNfcState.next_state)\r
1001         ||(phHal_eNfcIP1_Initiator==\r
1002         ((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType))\r
1003     {\r
1004         RetVal = NFCSTATUS_REJECTED;\r
1005     }         \r
1006 #ifdef LLCP_TRANSACT_CHANGES\r
1007     else if ((LLCP_STATE_RESET_INIT != gpphLibContext->llcp_cntx.sLlcpContext.state)\r
1008             && (LLCP_STATE_CHECKED != gpphLibContext->llcp_cntx.sLlcpContext.state))\r
1009     {\r
1010         RetVal= NFCSTATUS_BUSY;\r
1011     }\r
1012 #endif /* #ifdef LLCP_TRANSACT_CHANGES */\r
1013     else\r
1014     {\r
1015         gpphLibContext->ndef_cntx.eLast_Call = RawTrans;\r
1016         (void)memcpy((void *)(gpphLibContext->psTransInfo), \r
1017                     (void *)psTransceiveInfo, \r
1018                     sizeof(phLibNfc_sTransceiveInfo_t));\r
1019         /* Check the given Mifare command is supported or not , \r
1020                             If not return NFCSTATUS_COMMAND_NOT_SUPPORTED */\r
1021         if( (((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType == \r
1022                phHal_eMifare_PICC)&&\r
1023             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRaw ) &&\r
1024             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareAuthentA ) &&\r
1025             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareAuthentB ) &&\r
1026             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRead16 ) &&\r
1027             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRead ) &&\r
1028             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareWrite16 ) &&\r
1029             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareWrite4 ) &&\r
1030             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareDec ) &&\r
1031             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareTransfer ) &&\r
1032             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareRestore ) &&\r
1033             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareReadSector ) &&\r
1034             ( gpphLibContext->psTransInfo->cmd.MfCmd != phHal_eMifareWriteSector ))\r
1035         {\r
1036             RetVal = NFCSTATUS_COMMAND_NOT_SUPPORTED;\r
1037         }           \r
1038         if(eLibNfcHalStatePresenceChk !=\r
1039                      gpphLibContext->LibNfcState.next_state)\r
1040         {\r
1041             PHDBG_INFO("LibNfc:Transceive In Progress");\r
1042             if((((phHal_sRemoteDevInformation_t*)hRemoteDevice)->RemDevType == \r
1043                phHal_eMifare_PICC) && (((phHal_sRemoteDevInformation_t*)\r
1044                hRemoteDevice)->RemoteDevInfo.Iso14443A_Info.Sak != 0)&&\r
1045                (phHal_eMifareAuthentA == gpphLibContext->psTransInfo->cmd.MfCmd))\r
1046             {\r
1047                 if(NULL != gpphLibContext->psBufferedAuth)\r
1048                 {\r
1049                     if(NULL != gpphLibContext->psBufferedAuth->sRecvData.buffer)\r
1050                     {\r
1051                         phOsalNfc_FreeMemory(\r
1052                             gpphLibContext->psBufferedAuth->sRecvData.buffer);\r
1053                     }\r
1054                     if(NULL != gpphLibContext->psBufferedAuth->sSendData.buffer)\r
1055                     {\r
1056                         phOsalNfc_FreeMemory(\r
1057                             gpphLibContext->psBufferedAuth->sSendData.buffer);\r
1058                     }\r
1059                     phOsalNfc_FreeMemory(gpphLibContext->psBufferedAuth);\r
1060                 }\r
1061                 gpphLibContext->psBufferedAuth\r
1062                     =(phLibNfc_sTransceiveInfo_t *) \r
1063                     phOsalNfc_GetMemory(sizeof(phLibNfc_sTransceiveInfo_t));\r
1064                 gpphLibContext->psBufferedAuth->addr = psTransceiveInfo->addr;\r
1065                 gpphLibContext->psBufferedAuth->cmd = psTransceiveInfo->cmd;\r
1066                 gpphLibContext->psBufferedAuth->sSendData.length \r
1067                     = psTransceiveInfo->sSendData.length;\r
1068                 gpphLibContext->psBufferedAuth->sRecvData.length \r
1069                     = psTransceiveInfo->sRecvData.length;                \r
1070                 gpphLibContext->psBufferedAuth->sSendData.buffer\r
1071                   = (uint8_t *)\r
1072                       phOsalNfc_GetMemory(\r
1073                       gpphLibContext->psTransInfo->sSendData.length);\r
1074                 \r
1075                 (void)memcpy((void *)\r
1076                         (gpphLibContext->psBufferedAuth->sSendData.buffer), \r
1077                         (void *)psTransceiveInfo->sSendData.buffer, \r
1078                         psTransceiveInfo->sSendData.length);\r
1079                \r
1080                 gpphLibContext->psBufferedAuth->sRecvData.buffer\r
1081                   = (uint8_t *)\r
1082                       phOsalNfc_GetMemory(\r
1083                         gpphLibContext->psTransInfo->sRecvData.length);             \r
1084             }\r
1085             /*Call the lower layer Transceive function */\r
1086             RetVal = phHal4Nfc_Transceive( gpphLibContext->psHwReference,\r
1087                                         (phHal_sTransceiveInfo_t*)gpphLibContext->psTransInfo,\r
1088                                         (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice,\r
1089                                         (pphHal4Nfc_TransceiveCallback_t)\r
1090                                         phLibNfc_RemoteDev_Transceive_Cb,\r
1091                                         (void* )gpphLibContext);\r
1092             if(PHNFCSTATUS(RetVal) == NFCSTATUS_PENDING)\r
1093             {\r
1094                 /* Copy the upper layer callback pointer and context */\r
1095                 gpphLibContext->CBInfo.pClientTransceiveCb = pTransceive_RspCb;\r
1096                 gpphLibContext->CBInfo.pClientTranseCntx = pContext;\r
1097                 /* Mark the General callback pending status is TRUE */\r
1098                 gpphLibContext->status.GenCb_pending_status = TRUE;\r
1099                 /*Transceive is in Progress-Used in Release API*/\r
1100 \r
1101                 /*Update the state machine*/\r
1102                 gpphLibContext->LibNfcState.next_state = eLibNfcHalStateTransaction;\r
1103             }\r
1104         }       \r
1105         else\r
1106         {\r
1107             gpphLibContext->status.GenCb_pending_status = FALSE;\r
1108             RetVal = NFCSTATUS_FAILED;\r
1109         }       \r
1110     }\r
1111     return RetVal;\r
1112 }\r
1113 /**\r
1114 * Response for Remote device transceive.\r
1115 */\r
1116 STATIC \r
1117 void phLibNfc_RemoteDev_Transceive_Cb(void *context,\r
1118                                     phHal_sRemoteDevInformation_t *pRmtdev_info,\r
1119                                     phNfc_sData_t *response,\r
1120                                     NFCSTATUS status\r
1121                                     )\r
1122 {\r
1123     NFCSTATUS             trans_status = NFCSTATUS_SUCCESS;\r
1124     phNfc_sData_t         *trans_resp= NULL;\r
1125     void                  *pUpper_Context = NULL;\r
1126     pphLibNfc_TransceiveCallback_t pUpper_TagNtfCb =\r
1127                             gpphLibContext->CBInfo.pClientTransceiveCb;\r
1128 \r
1129     /*Check valid context is returned or not */\r
1130     if((phLibNfc_LibContext_t *)context == gpphLibContext)\r
1131     {\r
1132         trans_resp = &gpphLibContext->psTransInfo->sRecvData;\r
1133 \r
1134         pUpper_Context = gpphLibContext->CBInfo.pClientTranseCntx;\r
1135         gpphLibContext->status.GenCb_pending_status = FALSE;\r
1136 \r
1137         /*If DeInit is called during the transceive,\r
1138            call the shutdown and return NFCSTATUS_SHUTDOWN*/\r
1139         if(gpphLibContext->LibNfcState.next_state\r
1140                             == eLibNfcHalStateShutdown)\r
1141         {\r
1142             phLibNfc_Pending_Shutdown();\r
1143             trans_status = NFCSTATUS_SHUTDOWN;\r
1144         }\r
1145          /* If Disconnect is called return NFCSTATUS_ABORTED */\r
1146         else if(eLibNfcHalStateRelease == \r
1147                 gpphLibContext->LibNfcState.next_state)\r
1148         {\r
1149             trans_status = NFCSTATUS_ABORTED;\r
1150         }\r
1151         /* If the received lower layer status is not SUCCESS return NFCSTATUS_FAILED */\r
1152         else if( NFCSTATUS_SUCCESS == status)\r
1153         {\r
1154             trans_status = NFCSTATUS_SUCCESS;\r
1155         }        \r
1156         else if((PHNFCSTATUS(status) != NFCSTATUS_SUCCESS) &&\r
1157                 (phHal_eMifare_PICC == pRmtdev_info->RemDevType) && \r
1158                 (0x00 != pRmtdev_info->RemoteDevInfo.Iso14443A_Info.Sak))\r
1159         {\r
1160             gpphLibContext->LastTrancvSuccess = FALSE;\r
1161             trans_status = NFCSTATUS_FAILED;\r
1162             /* card type is mifare 1k/4k, then reconnect */\r
1163             trans_status = phHal4Nfc_Connect(gpphLibContext->psHwReference,  \r
1164                         pRmtdev_info,\r
1165                         (pphHal4Nfc_ConnectCallback_t)\r
1166                         phLibNfc_Reconnect_Mifare_Cb,\r
1167                         (void *)gpphLibContext);\r
1168         }\r
1169         else if ((PHNFCSTATUS(status) == PN544_IO_TIMEOUT_RESPONSE) ||\r
1170                  (PHNFCSTATUS(status) == NFCSTATUS_RF_TIMEOUT))\r
1171         {\r
1172             // 0x89, 0x09 HCI response values from PN544 indicate timeout\r
1173             trans_status = NFCSTATUS_TARGET_LOST;\r
1174         }\r
1175         else\r
1176         {\r
1177             // PN544 did get some reply from tag, just not valid\r
1178             trans_status = NFCSTATUS_FAILED;\r
1179         }\r
1180         /*Update the state machine */\r
1181         phLibNfc_UpdateCurState(status,gpphLibContext);\r
1182         gpphLibContext->LibNfcState.next_state = eLibNfcHalStateConnect;\r
1183         if(NFCSTATUS_PENDING != trans_status)  \r
1184         {\r
1185             /* Tranceive over */              \r
1186             PHDBG_INFO("LibNfc:TXRX Callback-Update the Transceive responce");\r
1187             if (NULL != pUpper_TagNtfCb)\r
1188             {\r
1189                 if(trans_status == NFCSTATUS_SUCCESS)\r
1190                 {\r
1191                     gpphLibContext->LastTrancvSuccess = TRUE;\r
1192                     pUpper_Context = gpphLibContext->CBInfo.pClientTranseCntx;\r
1193                     trans_resp->buffer = response->buffer;\r
1194                     trans_resp->length = response->length;                  \r
1195                             /* Notify the upper layer */\r
1196                     PHDBG_INFO("LibNfc:Transceive Complete");\r
1197                     /* Notify the Transceive Completion to upper layer */\r
1198                     gpphLibContext->CBInfo.pClientTransceiveCb(pUpper_Context,\r
1199                                 (uint32_t)pRmtdev_info,  \r
1200                                 trans_resp,\r
1201                                 trans_status);\r
1202                 }\r
1203                 else\r
1204                 {\r
1205                     gpphLibContext->LastTrancvSuccess = FALSE;\r
1206                     pUpper_Context = gpphLibContext->CBInfo.pClientTranseCntx;                  \r
1207                     trans_resp->length = 0;                     \r
1208                             /* Notify the upper layer */\r
1209                     PHDBG_INFO("LibNfc:Transceive Complete");\r
1210                     /* Notify the Transceive Completion to upper layer */\r
1211                     gpphLibContext->CBInfo.pClientTransceiveCb(pUpper_Context,\r
1212                                 (uint32_t)pRmtdev_info,  \r
1213                                 trans_resp,\r
1214                                 trans_status);\r
1215                 }\r
1216             }\r
1217        }\r
1218        \r
1219     }\r
1220     else\r
1221     {    /*exception: wrong context pointer returned*/\r
1222         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
1223     }\r
1224 \r
1225     return;\r
1226 }\r
1227 /**\r
1228 * Interface to configure P2P configurations.\r
1229 */\r
1230 NFCSTATUS \r
1231 phLibNfc_Mgt_SetP2P_ConfigParams(phLibNfc_sNfcIPCfg_t*          pConfigInfo,\r
1232                                 pphLibNfc_RspCb_t                       pConfigRspCb,\r
1233                                 void*                                           pContext\r
1234                                 )\r
1235 {\r
1236     NFCSTATUS RetVal = NFCSTATUS_FAILED;\r
1237     /* LibNfc Initialized or not */\r
1238     if((NULL == gpphLibContext)|| \r
1239         (gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown))\r
1240     {\r
1241         RetVal = NFCSTATUS_NOT_INITIALISED;\r
1242     }/* Check for valid parameters */\r
1243     else if((NULL == pConfigInfo) || (NULL == pConfigRspCb)\r
1244         || (NULL == pContext))\r
1245     {\r
1246         RetVal= NFCSTATUS_INVALID_PARAMETER;\r
1247     }\r
1248     else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)\r
1249     {\r
1250         RetVal = NFCSTATUS_SHUTDOWN;\r
1251     }    \r
1252     else if(TRUE == gpphLibContext->status.GenCb_pending_status)        \r
1253     { /*Previous callback is pending */\r
1254         RetVal = NFCSTATUS_BUSY;\r
1255     }\r
1256     else\r
1257     {\r
1258         if(eLibNfcHalStatePresenceChk !=\r
1259                 gpphLibContext->LibNfcState.next_state)\r
1260         {\r
1261             phHal_uConfig_t uConfig;  \r
1262             /* copy General bytes of Max length = 48 bytes */\r
1263             (void)memcpy((void *)&(uConfig.nfcIPConfig.generalBytes),\r
1264                     (void *)pConfigInfo->generalBytes,\r
1265                     pConfigInfo->generalBytesLength);\r
1266             /* also copy the General Bytes length*/\r
1267             uConfig.nfcIPConfig.generalBytesLength = pConfigInfo->generalBytesLength;\r
1268 \r
1269             RetVal = phHal4Nfc_ConfigParameters(                                    \r
1270                                 gpphLibContext->psHwReference,\r
1271                                 NFC_P2P_CONFIG,\r
1272                                 &uConfig,\r
1273                                 phLibNfc_Mgt_SetP2P_ConfigParams_Cb,\r
1274                                 (void *)gpphLibContext\r
1275                                 );          \r
1276         }\r
1277         else\r
1278         {\r
1279              gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb= NULL;\r
1280              RetVal = NFCSTATUS_PENDING;\r
1281         }\r
1282         if(NFCSTATUS_PENDING == RetVal)\r
1283         {\r
1284             /* save the context and callback for later use */\r
1285             gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb = pConfigRspCb;\r
1286             gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx = pContext;\r
1287             gpphLibContext->status.GenCb_pending_status=TRUE;\r
1288             /* Next state is configured */\r
1289             gpphLibContext->LibNfcState.next_state =eLibNfcHalStateConfigReady;            \r
1290         }       \r
1291         else\r
1292         {\r
1293             RetVal = NFCSTATUS_FAILED;\r
1294         }\r
1295     }\r
1296     return RetVal;\r
1297 }\r
1298 /**\r
1299 * Response callback for P2P configurations.\r
1300 */\r
1301 STATIC void phLibNfc_Mgt_SetP2P_ConfigParams_Cb(void     *context,\r
1302                                         NFCSTATUS status)\r
1303 {\r
1304         pphLibNfc_RspCb_t       pClientCb=NULL;    \r
1305     void                    *pUpperLayerContext=NULL;\r
1306      /* Check for the context returned by below layer */\r
1307     if((phLibNfc_LibContext_t *)context != gpphLibContext)\r
1308     {   /*wrong context returned*/\r
1309         phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);\r
1310     }\r
1311     else\r
1312     {\r
1313         if(eLibNfcHalStateShutdown == gpphLibContext->LibNfcState.next_state)\r
1314         {   /*shutdown called before completion of this api allow\r
1315             shutdown to happen */\r
1316             phLibNfc_Pending_Shutdown();\r
1317             status = NFCSTATUS_SHUTDOWN;    \r
1318         }\r
1319         else\r
1320         {\r
1321             gpphLibContext->status.GenCb_pending_status = FALSE;\r
1322             if(NFCSTATUS_SUCCESS != status)\r
1323             {   \r
1324                 status = NFCSTATUS_FAILED;\r
1325             }\r
1326             else\r
1327             {\r
1328                 status = NFCSTATUS_SUCCESS;\r
1329             }\r
1330         }\r
1331         /*update the current state */\r
1332         phLibNfc_UpdateCurState(status,gpphLibContext);\r
1333 \r
1334         pClientCb = gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb;\r
1335         pUpperLayerContext = gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx;\r
1336 \r
1337         gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCb = NULL;\r
1338         gpphLibContext->sNfcIp_Context.pClientNfcIpCfgCntx = NULL;\r
1339         if (NULL != pClientCb)\r
1340         {\r
1341             /* Notify to upper layer status of configure operation */\r
1342             pClientCb(pUpperLayerContext, status);\r
1343         }\r
1344     }   \r
1345     return;\r
1346 }\r
1347 \r
1348 \r
1349 \r
1350 \r
1351 \r
1352 \r
1353 \r
1354 \r
1355 \r
1356 \r