• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 NXP Semiconductors
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  */
16 
17 #include <phNfcTypes.h>
18 #include <phLibNfc.h>
19 #include <phLibNfc_Internal.h>
20 #include <phFriNfc_Llcp.h>
21 #include <phFriNfc_LlcpTransport.h>
22 
23 /* ---------------------------- Internal macros -------------------------------- */
24 
25 #ifndef STATIC_DISABLE
26 #define STATIC static
27 #else
28 #define STATIC
29 #endif
30 
31 /* ----------------------- Internal functions headers -------------------------- */
32 
33 STATIC
34 NFCSTATUS static_CheckState();
35 
36 STATIC
37 NFCSTATUS static_CheckDevice(phLibNfc_Handle hRemoteDevice);
38 
39 STATIC
40 void phLibNfc_Llcp_CheckLlcp_Cb(void *pContext,NFCSTATUS status);
41 
42 STATIC
43 void phLibNfc_Llcp_Link_Cb(void *pContext,phLibNfc_Llcp_eLinkStatus_t status);
44 
45 /* --------------------------- Internal functions ------------------------------ */
46 
static_CheckState()47 STATIC NFCSTATUS static_CheckState()
48 {
49    /* Check if the global context is set */
50    if(gpphLibContext == NULL)
51    {
52       return NFCSTATUS_NOT_INITIALISED;
53    }
54 
55    /* Check if initialized */
56    if(gpphLibContext->LibNfcState.cur_state == eLibNfcHalStateShutdown)
57    {
58       return NFCSTATUS_NOT_INITIALISED;
59    }
60 
61    /* Check if shutting down */
62    if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
63    {
64       return NFCSTATUS_SHUTDOWN;
65    }
66 
67    return NFCSTATUS_SUCCESS;
68 }
69 
static_CheckDevice(phLibNfc_Handle hRemoteDevice)70 STATIC NFCSTATUS static_CheckDevice(phLibNfc_Handle hRemoteDevice)
71 {
72    phLibNfc_sRemoteDevInformation_t*   psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice;
73 
74    if (hRemoteDevice == NULL)
75    {
76       return NFCSTATUS_INVALID_PARAMETER;
77    }
78 
79    /* If local device is the Initiator (remote is Target),
80     * check if connection is correct
81     */
82    if (psRemoteDevInfo->RemDevType == phHal_eNfcIP1_Target)
83    {
84       /* Check if any device connected */
85       if(gpphLibContext->Connected_handle == 0)
86       {
87          return NFCSTATUS_TARGET_NOT_CONNECTED;
88       }
89 
90       /* Check if handle corresponds to connected one */
91       if(hRemoteDevice != gpphLibContext->Connected_handle)
92       {
93          return NFCSTATUS_INVALID_HANDLE;
94       }
95    }
96 
97    /* Check if previous callback is pending or if remote peer is not LLCP compliant */
98    if ((gpphLibContext->status.GenCb_pending_status == TRUE) ||
99             (gpphLibContext->llcp_cntx.bIsLlcp == FALSE))
100    {
101       return NFCSTATUS_REJECTED;
102    }
103 
104    return NFCSTATUS_SUCCESS;
105 }
106 
107 /* ---------------------------- Public functions ------------------------------- */
108 
phLibNfc_Mgt_SetLlcp_ConfigParams(phLibNfc_Llcp_sLinkParameters_t * pConfigInfo,pphLibNfc_RspCb_t pConfigRspCb,void * pContext)109 NFCSTATUS phLibNfc_Mgt_SetLlcp_ConfigParams( phLibNfc_Llcp_sLinkParameters_t* pConfigInfo,
110                                             pphLibNfc_RspCb_t                pConfigRspCb,
111                                             void*                            pContext
112                                             )
113 {
114    NFCSTATUS      result;
115    phNfc_sData_t  sGeneralBytesBuffer;
116    phLibNfc_sNfcIPCfg_t sNfcIPCfg;
117    const uint8_t  pMagicBuffer[] = { 0x46, 0x66, 0x6D };
118 
119    /* State checking */
120    result = static_CheckState();
121    if (result != NFCSTATUS_SUCCESS)
122    {
123       return result;
124    }
125 
126    /* Parameters checking */
127    if ((pConfigInfo == NULL) || (pConfigRspCb == NULL))
128    {
129       return NFCSTATUS_INVALID_PARAMETER;
130    }
131 
132    /* Save the config for later use */
133    memcpy( &gpphLibContext->llcp_cntx.sLocalParams,
134            pConfigInfo,
135            sizeof(phLibNfc_Llcp_sLinkParameters_t) );
136 
137    /* Copy magic number in NFCIP General Bytes */
138    memcpy(sNfcIPCfg.generalBytes, pMagicBuffer, sizeof(pMagicBuffer));
139    sNfcIPCfg.generalBytesLength = sizeof(pMagicBuffer);
140 
141    /* Encode link parameters in TLV to configure P2P General Bytes */
142    sGeneralBytesBuffer.buffer = sNfcIPCfg.generalBytes + sizeof(pMagicBuffer);
143    sGeneralBytesBuffer.length = sizeof(sNfcIPCfg.generalBytes) - sizeof(pMagicBuffer);
144    result = phFriNfc_Llcp_EncodeLinkParams( &sGeneralBytesBuffer,
145                                             pConfigInfo,
146                                             PHFRINFC_LLCP_VERSION);
147    if (result != NFCSTATUS_SUCCESS)
148    {
149       return PHNFCSTATUS(result);
150    }
151    sNfcIPCfg.generalBytesLength += (uint8_t)sGeneralBytesBuffer.length;
152 
153    /* Set the P2P general bytes */
154    result = phLibNfc_Mgt_SetP2P_ConfigParams(&sNfcIPCfg, pConfigRspCb, pContext);
155    if (result != NFCSTATUS_PENDING)
156    {
157       return PHNFCSTATUS(result);
158    }
159 
160    /* Resets the LLCP LLC component */
161    result = phFriNfc_Llcp_Reset( &gpphLibContext->llcp_cntx.sLlcpContext,
162                                  gpphLibContext->psOverHalCtxt,
163                                  pConfigInfo,
164                                  gpphLibContext->llcp_cntx.pRxBuffer,
165                                  sizeof(gpphLibContext->llcp_cntx.pRxBuffer),
166                                  gpphLibContext->llcp_cntx.pTxBuffer,
167                                  sizeof(gpphLibContext->llcp_cntx.pTxBuffer),
168                                  phLibNfc_Llcp_Link_Cb,
169                                  gpphLibContext);
170    if (result != NFCSTATUS_SUCCESS)
171    {
172       return PHNFCSTATUS(result);
173    }
174 
175    /* Resets the LLCP Transport component */
176    result = phFriNfc_LlcpTransport_Reset( &gpphLibContext->llcp_cntx.sLlcpTransportContext,
177                                           &gpphLibContext->llcp_cntx.sLlcpContext );
178    if (result != NFCSTATUS_SUCCESS)
179    {
180       return PHNFCSTATUS(result);
181    }
182 
183    return NFCSTATUS_PENDING;
184 }
185 
phLibNfc_Llcp_CheckLlcp(phLibNfc_Handle hRemoteDevice,pphLibNfc_ChkLlcpRspCb_t pCheckLlcp_RspCb,pphLibNfc_LlcpLinkStatusCb_t pLink_Cb,void * pContext)186 NFCSTATUS phLibNfc_Llcp_CheckLlcp( phLibNfc_Handle              hRemoteDevice,
187                                    pphLibNfc_ChkLlcpRspCb_t     pCheckLlcp_RspCb,
188                                    pphLibNfc_LlcpLinkStatusCb_t pLink_Cb,
189                                    void*                        pContext
190                                    )
191 {
192    NFCSTATUS                           result;
193    phLibNfc_sRemoteDevInformation_t*   psRemoteDevInfo = (phLibNfc_sRemoteDevInformation_t*)hRemoteDevice;
194 
195    /* State checking */
196    result = static_CheckState();
197    if (result != NFCSTATUS_SUCCESS)
198    {
199       return result;
200    }
201 
202    /* Parameters checking */
203    if ((hRemoteDevice == 0)       ||
204        (pCheckLlcp_RspCb == NULL) ||
205        (pLink_Cb == NULL))
206    {
207       return NFCSTATUS_INVALID_PARAMETER;
208    }
209 
210    /* If local device is the Initiator (remote is Target),
211     * check if connection is correct
212     */
213    if (psRemoteDevInfo->RemDevType == phHal_eNfcIP1_Target)
214    {
215       /* Check if any device connected */
216       if(gpphLibContext->Connected_handle == 0)
217       {
218          return NFCSTATUS_TARGET_NOT_CONNECTED;
219       }
220 
221       /* Check if handle corresponds to connected one */
222       if(hRemoteDevice != gpphLibContext->Connected_handle)
223       {
224          return NFCSTATUS_INVALID_HANDLE;
225       }
226    }
227 
228    /* Prepare callback */
229    gpphLibContext->CBInfo.pClientLlcpLinkCb = pLink_Cb;
230    gpphLibContext->CBInfo.pClientLlcpLinkCntx = pContext;
231 
232    // DEBUG: Reset at least the state
233    gpphLibContext->llcp_cntx.sLlcpContext.state = 0;
234 
235    /* Prepare callback */
236    gpphLibContext->CBInfo.pClientLlcpCheckRespCb = pCheckLlcp_RspCb;
237    gpphLibContext->CBInfo.pClientLlcpCheckRespCntx = pContext;
238 
239    /* Update state */
240    result = phLibNfc_UpdateNextState(gpphLibContext, eLibNfcHalStateTransaction);
241    if (result != NFCSTATUS_SUCCESS)
242    {
243       return result;
244    }
245 
246    /* Call the component function */
247    result = phFriNfc_Llcp_ChkLlcp( &gpphLibContext->llcp_cntx.sLlcpContext,
248                                    psRemoteDevInfo,
249                                    phLibNfc_Llcp_CheckLlcp_Cb,
250                                    gpphLibContext
251                                    );
252    result = PHNFCSTATUS(result);
253    if (result == NFCSTATUS_PENDING)
254    {
255       gpphLibContext->status.GenCb_pending_status = TRUE;
256    }
257    else if (result == NFCSTATUS_SUCCESS)
258    {
259       /* Nothing to do */
260    }
261    else if (result != NFCSTATUS_FAILED)
262    {
263       result = NFCSTATUS_TARGET_LOST;
264    }
265 
266    return result;
267 }
268 
269 /* LLCP link callback */
270 STATIC
phLibNfc_Llcp_Link_Cb(void * pContext,phLibNfc_Llcp_eLinkStatus_t status)271 void phLibNfc_Llcp_Link_Cb(void *pContext, phLibNfc_Llcp_eLinkStatus_t status)
272 {
273    phLibNfc_LibContext_t         *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)pContext;
274    pphLibNfc_LlcpLinkStatusCb_t  pClientCb = NULL;
275    void                          *pClientContext = NULL;
276 
277    if(pLibNfc_Ctxt != gpphLibContext)
278    {
279       /*wrong context returned from below layer*/
280       phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
281    }
282    else
283    {
284       /* Close all sockets */
285       phFriNfc_LlcpTransport_CloseAll(&gpphLibContext->llcp_cntx.sLlcpTransportContext);
286 
287       /* Copy callback details */
288       pClientCb = gpphLibContext->CBInfo.pClientLlcpLinkCb;
289       pClientContext = gpphLibContext->CBInfo.pClientLlcpLinkCntx;
290 
291       /* Trigger the callback */
292       if(pClientCb != NULL)
293       {
294          pClientCb(pClientContext, status);
295       }
296    }
297 }
298 
299 /* Response callback for phLibNfc_Ndef_CheckNdef */
300 STATIC
phLibNfc_Llcp_CheckLlcp_Cb(void * pContext,NFCSTATUS status)301 void phLibNfc_Llcp_CheckLlcp_Cb(void *pContext, NFCSTATUS status)
302 {
303    phLibNfc_LibContext_t      *pLibNfc_Ctxt = (phLibNfc_LibContext_t *)pContext;
304    NFCSTATUS                  RetStatus = NFCSTATUS_SUCCESS;
305    pphLibNfc_ChkLlcpRspCb_t   pClientCb = NULL;
306    void                       *pClientContext = NULL;
307 
308    if(pLibNfc_Ctxt != gpphLibContext)
309    {
310       /*wrong context returned from below layer*/
311       phOsalNfc_RaiseException(phOsalNfc_e_InternalErr,1);
312    }
313    else
314    {
315       if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateShutdown)
316       {
317          /*shutdown called before completion of check Ndef, allow shutdown to happen */
318          phLibNfc_Pending_Shutdown();
319          RetStatus = NFCSTATUS_SHUTDOWN;
320       }
321       else if(gpphLibContext->LibNfcState.next_state == eLibNfcHalStateRelease)
322       {
323          RetStatus = NFCSTATUS_ABORTED;
324       }
325       else
326       {
327          if(status == NFCSTATUS_SUCCESS)
328          {
329             /* Remote peer is LLCP compliant */
330             gpphLibContext->llcp_cntx.bIsLlcp = TRUE;
331          }
332          else if(PHNFCSTATUS(status)== NFCSTATUS_FAILED)
333          {
334             RetStatus = NFCSTATUS_FAILED;
335             gpphLibContext->llcp_cntx.bIsLlcp = FALSE;
336          }
337          else
338          {
339             RetStatus = NFCSTATUS_TARGET_LOST;
340          }
341       }
342 
343       /* Update the current state */
344       gpphLibContext->status.GenCb_pending_status = FALSE;
345       phLibNfc_UpdateCurState(RetStatus,gpphLibContext);
346 
347       /* Copy callback details */
348       pClientCb = gpphLibContext->CBInfo.pClientLlcpCheckRespCb;
349       pClientContext = gpphLibContext->CBInfo.pClientLlcpCheckRespCntx;
350 
351       /* Reset saved callback */
352       gpphLibContext->CBInfo.pClientCkNdefCb = NULL;
353       gpphLibContext->CBInfo.pClientCkNdefCntx = NULL;
354 
355       /* Trigger the callback */
356       if(pClientCb != NULL)
357       {
358          pClientCb(pClientContext,RetStatus);
359       }
360    }
361 }
362 
phLibNfc_Llcp_Activate(phLibNfc_Handle hRemoteDevice)363 NFCSTATUS phLibNfc_Llcp_Activate( phLibNfc_Handle hRemoteDevice )
364 {
365    NFCSTATUS result;
366 
367    /* State checking */
368    result = static_CheckState();
369    if (result != NFCSTATUS_SUCCESS)
370    {
371       return result;
372    }
373 
374    /* Parameters checking */
375    if (hRemoteDevice == 0)
376    {
377       return NFCSTATUS_INVALID_PARAMETER;
378    }
379 
380    /* Check device */
381    result = static_CheckDevice(hRemoteDevice);
382    if (result != NFCSTATUS_SUCCESS)
383    {
384       return result;
385    }
386 
387    /* Start activation */
388    result = phFriNfc_Llcp_Activate(&gpphLibContext->llcp_cntx.sLlcpContext);
389 
390    return PHNFCSTATUS(result);
391 }
392 
phLibNfc_Llcp_Deactivate(phLibNfc_Handle hRemoteDevice)393 NFCSTATUS phLibNfc_Llcp_Deactivate( phLibNfc_Handle  hRemoteDevice )
394 {
395    NFCSTATUS result;
396 
397    /* State checking */
398    result = static_CheckState();
399    if (result != NFCSTATUS_SUCCESS)
400    {
401       return result;
402    }
403 
404    /* Parameters checking */
405    if (hRemoteDevice == 0)
406    {
407       return NFCSTATUS_INVALID_PARAMETER;
408    }
409 
410    /* Check device */
411    result = static_CheckDevice(hRemoteDevice);
412    if (result != NFCSTATUS_SUCCESS)
413    {
414       return result;
415    }
416 
417    /* Start deactivation */
418    result = phFriNfc_Llcp_Deactivate(&gpphLibContext->llcp_cntx.sLlcpContext);
419 
420    return PHNFCSTATUS(result);
421 }
422 
phLibNfc_Llcp_GetLocalInfo(phLibNfc_Handle hRemoteDevice,phLibNfc_Llcp_sLinkParameters_t * pConfigInfo)423 NFCSTATUS phLibNfc_Llcp_GetLocalInfo( phLibNfc_Handle                  hRemoteDevice,
424                                       phLibNfc_Llcp_sLinkParameters_t* pConfigInfo
425                                       )
426 {
427    NFCSTATUS result;
428 
429    /* State checking */
430    result = static_CheckState();
431    if (result != NFCSTATUS_SUCCESS)
432    {
433       return result;
434    }
435 
436    /* Parameters checking */
437    if (pConfigInfo == NULL)
438    {
439       return NFCSTATUS_INVALID_PARAMETER;
440    }
441 
442    /* Get local infos */
443    result = phFriNfc_Llcp_GetLocalInfo(&gpphLibContext->llcp_cntx.sLlcpContext, pConfigInfo);
444 
445    return PHNFCSTATUS(result);
446 }
447 
phLibNfc_Llcp_GetRemoteInfo(phLibNfc_Handle hRemoteDevice,phLibNfc_Llcp_sLinkParameters_t * pConfigInfo)448 NFCSTATUS phLibNfc_Llcp_GetRemoteInfo( phLibNfc_Handle                    hRemoteDevice,
449                                        phLibNfc_Llcp_sLinkParameters_t*   pConfigInfo
450                                        )
451 {
452    NFCSTATUS result;
453 
454    /* State checking */
455    result = static_CheckState();
456    if (result != NFCSTATUS_SUCCESS)
457    {
458       return result;
459    }
460 
461    /* Parameters checking */
462    if ((hRemoteDevice == 0) ||
463        (pConfigInfo == NULL))
464    {
465       return NFCSTATUS_INVALID_PARAMETER;
466    }
467 
468    /* Check device */
469    result = static_CheckDevice(hRemoteDevice);
470    if (result != NFCSTATUS_SUCCESS)
471    {
472       return result;
473    }
474 
475    /* Get local infos */
476    result = phFriNfc_Llcp_GetRemoteInfo(&gpphLibContext->llcp_cntx.sLlcpContext, pConfigInfo);
477 
478    return PHNFCSTATUS(result);
479 }
480 
phLibNfc_Llcp_Socket(phLibNfc_Llcp_eSocketType_t eType,phLibNfc_Llcp_sSocketOptions_t * psOptions,phNfc_sData_t * psWorkingBuffer,phLibNfc_Handle * phSocket,pphLibNfc_LlcpSocketErrCb_t pErr_Cb,void * pContext)481 NFCSTATUS phLibNfc_Llcp_Socket( phLibNfc_Llcp_eSocketType_t      eType,
482                                 phLibNfc_Llcp_sSocketOptions_t*  psOptions,
483                                 phNfc_sData_t*                   psWorkingBuffer,
484                                 phLibNfc_Handle*                 phSocket,
485                                 pphLibNfc_LlcpSocketErrCb_t      pErr_Cb,
486                                 void*                            pContext
487                                 )
488 {
489    NFCSTATUS                        result;
490    phFriNfc_LlcpTransport_Socket_t  *psSocket;
491 
492    /* State checking */
493    result = static_CheckState();
494    if (result != NFCSTATUS_SUCCESS)
495    {
496       return result;
497    }
498 
499    /* Parameters checking */
500    /* NOTE: Transport Layer test psOption and psWorkingBuffer value */
501    if ((phSocket == NULL)        ||
502        (pErr_Cb == NULL))
503    {
504       return NFCSTATUS_INVALID_PARAMETER;
505    }
506 
507    /* Get local infos */
508    result = phFriNfc_LlcpTransport_Socket(&gpphLibContext->llcp_cntx.sLlcpTransportContext,
509                                           eType,
510                                           psOptions,
511                                           psWorkingBuffer,
512                                           &psSocket,
513                                           pErr_Cb,
514                                           pContext);
515 
516    /* Send back the socket handle */
517    *phSocket = (phLibNfc_Handle)psSocket;
518 
519    return PHNFCSTATUS(result);
520 }
521 
phLibNfc_Llcp_Close(phLibNfc_Handle hSocket)522 NFCSTATUS phLibNfc_Llcp_Close( phLibNfc_Handle hSocket )
523 {
524    NFCSTATUS                        result;
525    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
526 
527    /* State checking */
528    result = static_CheckState();
529    if (result != NFCSTATUS_SUCCESS)
530    {
531       return result;
532    }
533 
534    /* Parameters checking */
535    if (hSocket == 0)
536    {
537       return NFCSTATUS_INVALID_PARAMETER;
538    }
539 
540    /* Get local infos */
541    /* TODO: if connected abort and close else close only */
542    result = phFriNfc_LlcpTransport_Close(psSocket);
543 
544    return PHNFCSTATUS(result);
545 }
546 
phLibNfc_Llcp_SocketGetLocalOptions(phLibNfc_Handle hSocket,phLibNfc_Llcp_sSocketOptions_t * psLocalOptions)547 NFCSTATUS phLibNfc_Llcp_SocketGetLocalOptions( phLibNfc_Handle                  hSocket,
548                                                phLibNfc_Llcp_sSocketOptions_t*  psLocalOptions
549                                                )
550 {
551    NFCSTATUS                        result;
552    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
553 
554    /* State checking */
555    result = static_CheckState();
556    if (result != NFCSTATUS_SUCCESS)
557    {
558       return result;
559    }
560 
561    /* Parameters checking */
562    if ((hSocket == 0) ||
563        (psLocalOptions == NULL))
564    {
565       return NFCSTATUS_INVALID_PARAMETER;
566    }
567 
568    /* Get local options */
569    result = phFriNfc_LlcpTransport_SocketGetLocalOptions(psSocket, psLocalOptions);
570 
571    return PHNFCSTATUS(result);
572 }
573 
phLibNfc_Llcp_SocketGetRemoteOptions(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,phLibNfc_Llcp_sSocketOptions_t * psRemoteOptions)574 NFCSTATUS phLibNfc_Llcp_SocketGetRemoteOptions( phLibNfc_Handle                  hRemoteDevice,
575                                                 phLibNfc_Handle                  hSocket,
576                                                 phLibNfc_Llcp_sSocketOptions_t*  psRemoteOptions
577                                                 )
578 {
579    NFCSTATUS                        result;
580    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
581 
582    LLCP_PRINT("phLibNfc_Llcp_SocketGetRemoteOptions");
583 
584    /* State checking */
585    result = static_CheckState();
586    if (result != NFCSTATUS_SUCCESS)
587    {
588       return result;
589    }
590 
591    /* Parameters checking */
592    if ((hRemoteDevice == 0) ||
593        (hSocket == 0)       ||
594        (psRemoteOptions == NULL))
595    {
596       return NFCSTATUS_INVALID_PARAMETER;
597    }
598 
599    /* Check device */
600    result = static_CheckDevice(hRemoteDevice);
601    if (result != NFCSTATUS_SUCCESS)
602    {
603       return result;
604    }
605 
606    /* Get remote infos */
607    result = phFriNfc_LlcpTransport_SocketGetRemoteOptions(psSocket, psRemoteOptions);
608 
609    return PHNFCSTATUS(result);
610 }
611 
phLibNfc_Llcp_Bind(phLibNfc_Handle hSocket,uint8_t nSap)612 NFCSTATUS phLibNfc_Llcp_Bind( phLibNfc_Handle hSocket,
613                               uint8_t         nSap
614                               )
615 {
616    NFCSTATUS                        result;
617    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
618 
619    LLCP_PRINT("phLibNfc_Llcp_Bind");
620 
621    /* State checking */
622    result = static_CheckState();
623    if (result != NFCSTATUS_SUCCESS)
624    {
625       return result;
626    }
627 
628    /* Parameters checking */
629    if (hSocket == 0)
630    {
631       return NFCSTATUS_INVALID_PARAMETER;
632    }
633 
634    /* Bind the socket to the designated port */
635    result = phFriNfc_LlcpTransport_Bind(psSocket, nSap);
636 
637    return PHNFCSTATUS(result);
638 }
639 
phLibNfc_Llcp_Listen(phLibNfc_Handle hSocket,phNfc_sData_t * psServiceName,pphLibNfc_LlcpSocketListenCb_t pListen_Cb,void * pContext)640 NFCSTATUS phLibNfc_Llcp_Listen( phLibNfc_Handle                  hSocket,
641                                 phNfc_sData_t                    *psServiceName,
642                                 pphLibNfc_LlcpSocketListenCb_t   pListen_Cb,
643                                 void*                            pContext
644                                 )
645 {
646    NFCSTATUS                        result;
647    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
648 
649    LLCP_PRINT("phLibNfc_Llcp_Listen");
650 
651    /* State checking */
652    result = static_CheckState();
653    if (result != NFCSTATUS_SUCCESS)
654    {
655       return result;
656    }
657 
658    /* Parameters checking */
659    /* NOTE : psServiceName may be NULL, do not test it ! */
660    if ((hSocket == 0) ||
661        (pListen_Cb == NULL))
662    {
663       return NFCSTATUS_INVALID_PARAMETER;
664    }
665 
666    /* Start listening for incoming connections */
667    result = phFriNfc_LlcpTransport_Listen( psSocket,
668                                            psServiceName,
669                                            (pphFriNfc_LlcpTransportSocketListenCb_t)pListen_Cb,
670                                            pContext );
671 
672    return PHNFCSTATUS(result);
673 }
674 
phLibNfc_Llcp_Accept(phLibNfc_Handle hSocket,phLibNfc_Llcp_sSocketOptions_t * psOptions,phNfc_sData_t * psWorkingBuffer,pphLibNfc_LlcpSocketErrCb_t pErr_Cb,pphLibNfc_LlcpSocketAcceptCb_t pAccept_RspCb,void * pContext)675 NFCSTATUS phLibNfc_Llcp_Accept( phLibNfc_Handle                  hSocket,
676                                 phLibNfc_Llcp_sSocketOptions_t*  psOptions,
677                                 phNfc_sData_t*                   psWorkingBuffer,
678                                 pphLibNfc_LlcpSocketErrCb_t      pErr_Cb,
679                                 pphLibNfc_LlcpSocketAcceptCb_t   pAccept_RspCb,
680                                 void*                            pContext
681                                 )
682 {
683    NFCSTATUS                        result;
684    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
685 
686    LLCP_PRINT("phLibNfc_Llcp_Accept");
687 
688    /* State checking */
689    result = static_CheckState();
690    if (result != NFCSTATUS_SUCCESS)
691    {
692       return result;
693    }
694 
695    /* Parameters checking */
696    if ((hSocket == 0)            ||
697        (psOptions == NULL)       ||
698        (psWorkingBuffer == NULL) ||
699        (pErr_Cb == NULL)         ||
700        (pAccept_RspCb == NULL))
701    {
702       return NFCSTATUS_INVALID_PARAMETER;
703    }
704 
705    /* Accept incoming connection */
706    result = phFriNfc_LlcpTransport_Accept( psSocket,
707                                            psOptions,
708                                            psWorkingBuffer,
709                                            pErr_Cb,
710                                            pAccept_RspCb,
711                                            pContext );
712 
713    return PHNFCSTATUS(result);
714 }
715 
phLibNfc_Llcp_Reject(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,pphLibNfc_LlcpSocketRejectCb_t pReject_RspCb,void * pContext)716 NFCSTATUS phLibNfc_Llcp_Reject( phLibNfc_Handle                  hRemoteDevice,
717                                 phLibNfc_Handle                  hSocket,
718                                 pphLibNfc_LlcpSocketRejectCb_t   pReject_RspCb,
719                                 void*                            pContext
720                                 )
721 {
722    NFCSTATUS                        result;
723    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
724 
725    LLCP_PRINT("phLibNfc_Llcp_Reject");
726 
727    /* State checking */
728    result = static_CheckState();
729    if (result != NFCSTATUS_SUCCESS)
730    {
731       return result;
732    }
733 
734    /* Parameters checking */
735    if ((hRemoteDevice == 0)      ||
736        (hSocket == 0)            ||
737        (pReject_RspCb == NULL))
738    {
739       return NFCSTATUS_INVALID_PARAMETER;
740    }
741 
742    /* Check device */
743    result = static_CheckDevice(hRemoteDevice);
744    if (result != NFCSTATUS_SUCCESS)
745    {
746       return result;
747    }
748 
749    /* Reject incoming connection */
750    result = phFriNfc_LlcpTransport_Reject( psSocket,
751                                            pReject_RspCb,
752                                            pContext );
753 
754    return PHNFCSTATUS(result);
755 }
756 
phLibNfc_Llcp_Connect(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,uint8_t nSap,pphLibNfc_LlcpSocketConnectCb_t pConnect_RspCb,void * pContext)757 NFCSTATUS phLibNfc_Llcp_Connect( phLibNfc_Handle                 hRemoteDevice,
758                                  phLibNfc_Handle                 hSocket,
759                                  uint8_t                         nSap,
760                                  pphLibNfc_LlcpSocketConnectCb_t pConnect_RspCb,
761                                  void*                           pContext
762                                  )
763 {
764    NFCSTATUS                        result;
765    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
766 
767    LLCP_PRINT("phLibNfc_Llcp_Connect");
768 
769    /* State checking */
770    result = static_CheckState();
771    if (result != NFCSTATUS_SUCCESS)
772    {
773       return result;
774    }
775 
776    /* Parameters checking */
777    if ((hRemoteDevice == 0)      ||
778 	   (hSocket == 0)            ||
779        (pConnect_RspCb == NULL))
780    {
781       LLCP_PRINT("phLibNfc_Llcp_Connect NFCSTATUS_INVALID_PARAMETER");
782       return NFCSTATUS_INVALID_PARAMETER;
783    }
784 
785    /* Check device */
786    result = static_CheckDevice(hRemoteDevice);
787    if (result != NFCSTATUS_SUCCESS)
788    {
789       return result;
790    }
791 
792    /* Try to connect on a remote service, given its SAP */
793    result = phFriNfc_LlcpTransport_Connect( psSocket,
794                                             nSap,
795                                             pConnect_RspCb,
796                                             pContext );
797 
798    return PHNFCSTATUS(result);
799 }
800 
phLibNfc_Llcp_ConnectByUri(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,phNfc_sData_t * psUri,pphLibNfc_LlcpSocketConnectCb_t pConnect_RspCb,void * pContext)801 NFCSTATUS phLibNfc_Llcp_ConnectByUri( phLibNfc_Handle                 hRemoteDevice,
802                                       phLibNfc_Handle                 hSocket,
803                                       phNfc_sData_t*                  psUri,
804                                       pphLibNfc_LlcpSocketConnectCb_t pConnect_RspCb,
805                                       void*                           pContext
806                                       )
807 {
808    NFCSTATUS                        result;
809    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
810 
811    LLCP_PRINT("phLibNfc_Llcp_ConnectByUri");
812 
813    /* State checking */
814    result = static_CheckState();
815    if (result != NFCSTATUS_SUCCESS)
816    {
817       return result;
818    }
819 
820    /* Parameters checking */
821    if ((hRemoteDevice == 0)      ||
822        (hSocket == 0)            ||
823        (psUri   == NULL)         ||
824        (pConnect_RspCb == NULL))
825    {
826       LLCP_PRINT("phLibNfc_Llcp_ConnectByUri NFCSTATUS_INVALID_PARAMETER");
827       return NFCSTATUS_INVALID_PARAMETER;
828    }
829 
830    /* Check device */
831    result = static_CheckDevice(hRemoteDevice);
832    if (result != NFCSTATUS_SUCCESS)
833    {
834       return result;
835    }
836 
837    /* Try to connect on a remote service, using SDP */
838    result = phFriNfc_LlcpTransport_ConnectByUri( psSocket,
839                                                  psUri,
840                                                  pConnect_RspCb,
841                                                  pContext );
842 
843    return PHNFCSTATUS(result);
844 }
845 
phLibNfc_Llcp_Disconnect(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,pphLibNfc_LlcpSocketDisconnectCb_t pDisconnect_RspCb,void * pContext)846 NFCSTATUS phLibNfc_Llcp_Disconnect( phLibNfc_Handle                    hRemoteDevice,
847                                     phLibNfc_Handle                    hSocket,
848                                     pphLibNfc_LlcpSocketDisconnectCb_t pDisconnect_RspCb,
849                                     void*                              pContext
850                                     )
851 {
852    NFCSTATUS                        result;
853    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
854 
855    LLCP_PRINT("phLibNfc_Llcp_Disconnect");
856 
857    /* State checking */
858    result = static_CheckState();
859    if (result != NFCSTATUS_SUCCESS)
860    {
861       return result;
862    }
863 
864    /* Parameters checking */
865    if ((hRemoteDevice == 0) ||
866        (hSocket == 0)       ||
867        (pDisconnect_RspCb == NULL))
868    {
869       return NFCSTATUS_INVALID_PARAMETER;
870    }
871 
872    /* Check device */
873    result = static_CheckDevice(hRemoteDevice);
874    if (result != NFCSTATUS_SUCCESS)
875    {
876       return result;
877    }
878 
879    /* Disconnect a logical link */
880    result = phFriNfc_LlcpTransport_Disconnect( psSocket,
881                                                pDisconnect_RspCb,
882                                                pContext );
883 
884    return PHNFCSTATUS(result);
885 }
886 
phLibNfc_Llcp_Recv(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,phNfc_sData_t * psBuffer,pphLibNfc_LlcpSocketRecvCb_t pRecv_RspCb,void * pContext)887 NFCSTATUS phLibNfc_Llcp_Recv( phLibNfc_Handle              hRemoteDevice,
888                               phLibNfc_Handle              hSocket,
889                               phNfc_sData_t*               psBuffer,
890                               pphLibNfc_LlcpSocketRecvCb_t pRecv_RspCb,
891                               void*                        pContext
892                               )
893 {
894    NFCSTATUS                        result;
895    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
896 
897    LLCP_PRINT("phLibNfc_Llcp_Recv");
898 
899    /* State checking */
900    result = static_CheckState();
901    if (result != NFCSTATUS_SUCCESS)
902    {
903       return result;
904    }
905 
906    /* Parameters checking */
907    if ((hRemoteDevice == 0)   ||
908        (hSocket == 0)         ||
909        (psBuffer == NULL)     ||
910        (pRecv_RspCb == NULL))
911    {
912       return NFCSTATUS_INVALID_PARAMETER;
913    }
914 
915    /* Check device */
916    result = static_CheckDevice(hRemoteDevice);
917    if (result != NFCSTATUS_SUCCESS)
918    {
919       return result;
920    }
921 
922    /* Receive data from the logical link */
923    result = phFriNfc_LlcpTransport_Recv( psSocket,
924                                          psBuffer,
925                                          pRecv_RspCb,
926                                          pContext );
927 
928    return PHNFCSTATUS(result);
929 }
930 
phLibNfc_Llcp_RecvFrom(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,phNfc_sData_t * psBuffer,pphLibNfc_LlcpSocketRecvFromCb_t pRecv_Cb,void * pContext)931 NFCSTATUS phLibNfc_Llcp_RecvFrom( phLibNfc_Handle                   hRemoteDevice,
932                                   phLibNfc_Handle                   hSocket,
933                                   phNfc_sData_t*                    psBuffer,
934                                   pphLibNfc_LlcpSocketRecvFromCb_t  pRecv_Cb,
935                                   void*                             pContext
936                                   )
937 {
938    NFCSTATUS                        result;
939    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
940 
941    LLCP_PRINT("phLibNfc_Llcp_RecvFrom");
942 
943    /* State checking */
944    result = static_CheckState();
945    if (result != NFCSTATUS_SUCCESS)
946    {
947       return result;
948    }
949 
950    /* Parameters checking */
951    if ((hRemoteDevice == 0)   ||
952        (hSocket == 0)         ||
953        (psBuffer == NULL)     ||
954        (pRecv_Cb == NULL))
955    {
956       return NFCSTATUS_INVALID_PARAMETER;
957    }
958 
959    /* Check device */
960    result = static_CheckDevice(hRemoteDevice);
961    if (result != NFCSTATUS_SUCCESS)
962    {
963       return result;
964    }
965 
966    /* Receive data from the logical link */
967    result = phFriNfc_LlcpTransport_RecvFrom( psSocket,
968                                              psBuffer,
969                                              pRecv_Cb,
970                                              pContext );
971 
972    return PHNFCSTATUS(result);
973 }
974 
phLibNfc_Llcp_Send(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,phNfc_sData_t * psBuffer,pphLibNfc_LlcpSocketSendCb_t pSend_RspCb,void * pContext)975 NFCSTATUS phLibNfc_Llcp_Send( phLibNfc_Handle              hRemoteDevice,
976                               phLibNfc_Handle              hSocket,
977                               phNfc_sData_t*               psBuffer,
978                               pphLibNfc_LlcpSocketSendCb_t pSend_RspCb,
979                               void*                        pContext
980                               )
981 {
982    NFCSTATUS                        result;
983    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
984 
985    LLCP_PRINT("phLibNfc_Llcp_Send");
986 
987    /* State checking */
988    result = static_CheckState();
989    if (result != NFCSTATUS_SUCCESS)
990    {
991       return result;
992    }
993 
994    /* Parameters checking */
995    if ((hRemoteDevice == 0)   ||
996        (hSocket == 0)         ||
997        (psBuffer == NULL)     ||
998        (pSend_RspCb == NULL))
999    {
1000       return NFCSTATUS_INVALID_PARAMETER;
1001    }
1002 
1003    /* Check device */
1004    result = static_CheckDevice(hRemoteDevice);
1005    if (result != NFCSTATUS_SUCCESS)
1006    {
1007       return result;
1008    }
1009 
1010    /* Send data to the logical link */
1011    result = phFriNfc_LlcpTransport_Send( psSocket,
1012                                          psBuffer,
1013                                          pSend_RspCb,
1014                                          pContext );
1015 
1016    return PHNFCSTATUS(result);
1017 }
1018 
phLibNfc_Llcp_SendTo(phLibNfc_Handle hRemoteDevice,phLibNfc_Handle hSocket,uint8_t nSap,phNfc_sData_t * psBuffer,pphLibNfc_LlcpSocketSendCb_t pSend_RspCb,void * pContext)1019 NFCSTATUS phLibNfc_Llcp_SendTo( phLibNfc_Handle               hRemoteDevice,
1020                                 phLibNfc_Handle               hSocket,
1021                                 uint8_t                       nSap,
1022                                 phNfc_sData_t*                psBuffer,
1023                                 pphLibNfc_LlcpSocketSendCb_t  pSend_RspCb,
1024                                 void*                         pContext
1025                                 )
1026 {
1027    NFCSTATUS                        result;
1028    phFriNfc_LlcpTransport_Socket_t  *psSocket = (phFriNfc_LlcpTransport_Socket_t*)hSocket;
1029 
1030    LLCP_PRINT("phLibNfc_Llcp_SendTo");
1031 
1032    /* State checking */
1033    result = static_CheckState();
1034    if (result != NFCSTATUS_SUCCESS)
1035    {
1036       return result;
1037    }
1038 
1039    /* Parameters checking */
1040    if ((hRemoteDevice == 0)   ||
1041        (hSocket == 0)         ||
1042        (psBuffer == NULL)     ||
1043        (pSend_RspCb == NULL))
1044    {
1045       return NFCSTATUS_INVALID_PARAMETER;
1046    }
1047 
1048    /* Check device */
1049    result = static_CheckDevice(hRemoteDevice);
1050    if (result != NFCSTATUS_SUCCESS)
1051    {
1052       return result;
1053    }
1054 
1055    /* Send data to the logical link */
1056    result = phFriNfc_LlcpTransport_SendTo( psSocket,
1057                                            nSap,
1058                                            psBuffer,
1059                                            pSend_RspCb,
1060                                            pContext );
1061 
1062    return PHNFCSTATUS(result);
1063 }
1064