15d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*
25d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Copyright (C) 2010 NXP Semiconductors
35d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
45d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Licensed under the Apache License, Version 2.0 (the "License");
55d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * you may not use this file except in compliance with the License.
65d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * You may obtain a copy of the License at
75d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
85d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *      http://www.apache.org/licenses/LICENSE-2.0
95d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Unless required by applicable law or agreed to in writing, software
115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * distributed under the License is distributed on an "AS IS" BASIS,
125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * See the License for the specific language governing permissions and
145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * limitations under the License.
155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \file  phFriNfc_LlcpTransport.c
195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * \brief
205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly * Project: NFC-FRI
225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly *
235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly */
245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*include files*/
26cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau#include <phOsalNfc.h>
275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfcStatus.h>
285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc.h>
295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phNfcLlcpTypes.h>
305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_Llcp.h>
315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_LlcpTransport.h>
325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_LlcpTransport_Connectionless.h>
335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_LlcpTransport_Connection.h>
345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
353a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau/* local macros */
363a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau
373a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau/* Check if (a <= x < b) */
383a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau#define IS_BETWEEN(x, a, b) (((x)>=(a)) && ((x)<(b)))
393a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau
405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
41fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneaustatic NFCSTATUS phFriNfc_LlcpTransport_AutoBind(phFriNfc_LlcpTransport_Socket_t *pSocket)
42fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau{
43fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   uint8_t i;
44fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   uint8_t sap;
45fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   phFriNfc_LlcpTransport_Socket_t* pSocketTable = pSocket->psTransport->pSocketTable;
46fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
47fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   /* Try all possible SAPs */
48fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   for(sap=PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST ; sap<PHFRINFC_LLCP_SAP_NUMBER ; sap++)
49fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   {
50fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      /* Go through socket list to check if current SAP is in use */
51fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      for(i=0 ; i<PHFRINFC_LLCP_NB_SOCKET_MAX ; i++)
52fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      {
53fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         if((pSocketTable[i].eSocket_State >= phFriNfc_LlcpTransportSocket_eSocketBound) &&
54fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            (pSocketTable[i].socket_sSap == sap))
55fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         {
56fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            /* SAP is already in use */
57fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            break;
58fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         }
59fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      }
60fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
61fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      if (i >= PHFRINFC_LLCP_NB_SOCKET_MAX)
62fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      {
63fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         /* No socket is using current SAP, proceed with binding */
64fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         pSocket->socket_sSap = sap;
65fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         pSocket->eSocket_State = phFriNfc_LlcpTransportSocket_eSocketBound;
66fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         return NFCSTATUS_SUCCESS;
67fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      }
68fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   }
69fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
70fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   /* If we reach this point, it means that no SAP is free */
71fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   return NFCSTATUS_INSUFFICIENT_RESOURCES;
72fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau}
73fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* TODO: comment function Transport recv CB */
755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_LlcpTransport__Recv_CB(void            *pContext,
765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            phNfc_sData_t   *psData,
775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NFCSTATUS        status)
785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_Llcp_sPacketHeader_t   sLlcpLocalHeader;
805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t   dsap;
815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t   ptype;
825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t   ssap;
835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_LlcpTransport_t* pLlcpTransport = (phFriNfc_LlcpTransport_t*)pContext;
855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(status != NFCSTATUS_SUCCESS)
875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->LinkStatusError = TRUE;
895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      phFriNfc_Llcp_Buffer2Header( psData->buffer,0x00, &sLlcpLocalHeader);
935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      dsap  = (uint8_t)sLlcpLocalHeader.dsap;
955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      ptype = (uint8_t)sLlcpLocalHeader.ptype;
965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      ssap  = (uint8_t)sLlcpLocalHeader.ssap;
975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Update the length value (without the header length) */
995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      psData->length = psData->length - PHFRINFC_LLCP_PACKET_HEADER_SIZE;
1005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Update the buffer pointer */
1025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      psData->buffer = psData->buffer + PHFRINFC_LLCP_PACKET_HEADER_SIZE;
1035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      switch(ptype)
1055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
1065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Connectionless */
1075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_UI:
1085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
1095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Handle_Connectionless_IncommingFrame(pLlcpTransport,
1105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                 psData,
1115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                 dsap,
1125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                 ssap);
1135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }break;
1145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Connection oriented */
1165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* NOTE: forward reserved PTYPE to enable FRMR sending */
1175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_CONNECT:
1185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_CC:
1195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_DISC:
1205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_DM:
1215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_I:
1225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RR:
1235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RNR:
1245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_FRMR:
1255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED1:
1265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED2:
1275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED3:
1285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED4:
1295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
1305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Handle_ConnectionOriented_IncommingFrame(pLlcpTransport,
1315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     psData,
1325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     dsap,
1335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     ptype,
1345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     ssap);
1355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }break;
1365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      default:
1375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
1385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }break;
1405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
1415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /*Restart the Receive Loop */
1435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status  = phFriNfc_Llcp_Recv(pLlcpTransport->pLlcp,
1445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   phFriNfc_LlcpTransport__Recv_CB,
1455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   pLlcpTransport);
1465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
1475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
1485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* TODO: comment function Transport reset */
1515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Reset (phFriNfc_LlcpTransport_t      *pLlcpTransport,
1525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_Llcp_t               *pLlcp)
1535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
1545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
1555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
1565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
1585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpTransport == NULL || pLlcp == NULL)
1595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
1615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
1625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
1635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Reset Transport structure */
1655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->pLlcp            = pLlcp;
1665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->LinkStatusError  = FALSE;
1675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bSendPending     = FALSE;
1685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bRecvPending     = FALSE;
1695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bDmPending       = FALSE;
1705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bFrmrPending     = FALSE;
1715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->socketIndex      = FALSE;
1725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->LinkStatusError  = 0;
1735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Reset all the socket info in the table */
1765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++)
1775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
1785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].eSocket_State                  = phFriNfc_LlcpTransportSocket_eSocketDefault;
1795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].eSocket_Type                   = phFriNfc_LlcpTransport_eDefaultType;
1805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].index                          = i;
1815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pContext                       = NULL;
1825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pListenContext                 = NULL;
1835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pAcceptContext                 = NULL;
1845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pRejectContext                 = NULL;
1855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pConnectContext                = NULL;
1865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pDisonnectContext              = NULL;
1875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pSendContext                   = NULL;
1885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pRecvContext                   = NULL;
1895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pSocketErrCb                   = NULL;
1905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bufferLinearLength             = 0;
1915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bufferSendMaxLength            = 0;
1925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bufferRwMaxLength              = 0;
1935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].ReceiverBusyCondition          = FALSE;
1945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].RemoteBusyConditionInfo        = FALSE;
1955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_sSap                    = PHFRINFC_LLCP_SAP_DEFAULT;
1965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_dSap                    = PHFRINFC_LLCP_SAP_DEFAULT;
1975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketRecvPending             = FALSE;
1985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketSendPending             = FALSE;
1995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketListenPending           = FALSE;
2005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketDiscPending             = FALSE;
2015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketConnectPending          = FALSE;
2025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketAcceptPending           = FALSE;
2035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketRRPending               = FALSE;
2045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketRNRPending              = FALSE;
2055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].psTransport                    = pLlcpTransport;
2065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketSend_Cb                = NULL;
2075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketRecv_Cb                = NULL;
2085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketRecvFrom_Cb            = NULL;
2095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketListen_Cb              = NULL;
2105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketConnect_Cb             = NULL;
2115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketDisconnect_Cb          = NULL;
2125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VS                      = 0;
2135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VSA                     = 0;
2145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VR                      = 0;
2155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VRA                     = 0;
2165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].remoteRW                       = 0;
2175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].localRW                        = 0;
2185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].remoteMIU                      = 0;
2195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].localMIUX                      = 0;
2205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].index                          = 0;
2215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].indexRwRead                    = 0;
2225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].indexRwWrite                   = 0;
223cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau
224c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau         memset(&pLlcpTransport->pSocketTable[i].sSocketOption, 0x00, sizeof(phFriNfc_LlcpTransport_sSocketOptions_t));
225c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau
226cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         if (pLlcpTransport->pSocketTable[i].sServiceName.buffer != NULL) {
227cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau            phOsalNfc_FreeMemory(pLlcpTransport->pSocketTable[i].sServiceName.buffer);
228cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         }
229cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         pLlcpTransport->pSocketTable[i].sServiceName.buffer = NULL;
230cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         pLlcpTransport->pSocketTable[i].sServiceName.length = 0;
2315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
2325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
2335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Start The Receive Loop */
2345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status  = phFriNfc_Llcp_Recv(pLlcpTransport->pLlcp,
2355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   phFriNfc_LlcpTransport__Recv_CB,
2365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   pLlcpTransport);
2375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
2385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
2395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
2405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
241c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau/* TODO: comment function Transport CloseAll */
242c56a3c7bc332a6656bedf342236267b636051170Sylvain FonteneauNFCSTATUS phFriNfc_LlcpTransport_CloseAll (phFriNfc_LlcpTransport_t *pLlcpTransport)
243c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau{
244c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   NFCSTATUS status = NFCSTATUS_SUCCESS;
245c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   uint8_t i;
246c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
247c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   /* Check for NULL pointers */
248c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   if(pLlcpTransport == NULL)
249c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   {
250c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
251c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   }
252c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
253c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   /* Close all sockets */
254c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++)
255c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   {
25676270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      if(pLlcpTransport->pSocketTable[i].eSocket_Type == phFriNfc_LlcpTransport_eConnectionOriented)
25776270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      {
25876270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         switch(pLlcpTransport->pSocketTable[i].eSocket_State)
25976270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         {
26076270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketConnected:
26176270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketConnecting:
26276270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketAccepted:
26376270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketDisconnected:
26476270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketDisconnecting:
26576270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketRejected:
26676270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir            phFriNfc_LlcpTransport_Close(&pLlcpTransport->pSocketTable[i]);
26776270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir            break;
26834ff48f6cd6595a899e05fbd56f4c84891840d3fNick Pelly         default: break;
26976270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         }
27076270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      }
27176270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      else
27276270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      {
27376270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         phFriNfc_LlcpTransport_Close(&pLlcpTransport->pSocketTable[i]);
27476270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      }
275c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   }
276c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   return status;
277c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau}
278c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
279c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
2805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
2815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
2825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Get the local options of a socket</b>.
2835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
2845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function returns the local options (maximum packet size and receive window size) used
2855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* for a given connection-oriented socket. This function shall not be used with connectionless
2865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* sockets.
2875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
2885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
2895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psLocalOptions        A pointer to be filled with the local options of the socket.
2905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
2915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
2925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
2935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
2945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
2955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
2965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
2975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
2985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
2995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
3005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_SocketGetLocalOptions(phFriNfc_LlcpTransport_Socket_t  *pLlcpSocket,
3015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                       phLibNfc_Llcp_sSocketOptions_t   *psLocalOptions)
3025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
3035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
3045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
3065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if (pLlcpSocket == NULL || psLocalOptions == NULL)
3075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
3095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket type */
3115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
3125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
3145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket state */
3165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketDefault)
3175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
3195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
3215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     status = phFriNfc_LlcpTransport_ConnectionOriented_SocketGetLocalOptions(pLlcpSocket,
3235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                              psLocalOptions);
3245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
3275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
3315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
3325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Get the local options of a socket</b>.
3335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
3345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function returns the remote options (maximum packet size and receive window size) used
3355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* for a given connection-oriented socket. This function shall not be used with connectionless
3365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* sockets.
3375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
3385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
3395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psRemoteOptions       A pointer to be filled with the remote options of the socket.
3405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
3415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
3425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
3435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
3445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
3455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
3465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
3475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
3485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
3495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
3505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_SocketGetRemoteOptions(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket,
3515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                        phLibNfc_Llcp_sSocketOptions_t*    psRemoteOptions)
3525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
3535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
3545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
3565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if (pLlcpSocket == NULL || psRemoteOptions == NULL)
3575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
3595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket type */
3615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
3625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
3645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket state */
3665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketConnected)
3675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
3695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
3715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
3725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_SocketGetRemoteOptions(pLlcpSocket,
3735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                                psRemoteOptions);
3745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
3755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
3775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
3785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
3795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
3805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
3815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Create a socket on a LLCP-connected device</b>.
3825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
3835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function creates a socket for a given LLCP link. Sockets can be of two types :
3845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* connection-oriented and connectionless. If the socket is connection-oriented, the caller
3855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* must provide a working buffer to the socket in order to handle incoming data. This buffer
3865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* must be large enough to fit the receive window (RW * MIU), the remaining space being
3875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* used as a linear buffer to store incoming data as a stream. Data will be readable later
3885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* using the phLibNfc_LlcpTransport_Recv function.
3895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* The options and working buffer are not required if the socket is used as a listening socket,
3905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* since it cannot be directly used for communication.
3915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
3925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocketTable      A pointer to a table of PHFRINFC_LLCP_NB_SOCKET_DEFAULT sockets.
3935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  eType                 The socket type.
3945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psOptions             The options to be used with the socket.
3955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psWorkingBuffer       A working buffer to be used by the library.
3965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer on the socket to be filled with a
3975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    socket found on the socket table.
3985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pErr_Cb               The callback to be called each time the socket
3995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                   is in error.
4005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext              Upper layer context to be returned in the callback.
4015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
4025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
4035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
4045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
4055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_BUFFER_TOO_SMALL         The working buffer is too small for the MIU and RW
4065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            declared in the options.
4075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INSUFFICIENT_RESOURCES   No more socket handle available.
4085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
4095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* */
4105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Socket(phFriNfc_LlcpTransport_t                  *pLlcpTransport,
4115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_eSocketType_t      eType,
4125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_sSocketOptions_t   *psOptions,
4135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phNfc_sData_t                             *psWorkingBuffer,
4145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_Socket_t           **pLlcpSocket,
4155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketErrCb_t      pErr_Cb,
4165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void                                      *pContext)
4175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
4185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
4195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_Llcp_sLinkParameters_t  LlcpLinkParamInfo;
4205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t index=0;
4215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t cpt;
4225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
4245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if (((NULL == psOptions) && (eType != phFriNfc_LlcpTransport_eConnectionLess)) || ((psWorkingBuffer == NULL) && (eType != phFriNfc_LlcpTransport_eConnectionLess)) || pLlcpSocket == NULL || pErr_Cb == NULL || pContext == NULL || pLlcpTransport == NULL)
4255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
4265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
4275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      return status;
4285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
4295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket type*/
4305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(eType != phFriNfc_LlcpTransport_eConnectionOriented && eType != phFriNfc_LlcpTransport_eConnectionLess)
4315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
4325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
4335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      return status;
4345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
4355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Get the local parameters of the LLCP Link */
4375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   status = phFriNfc_Llcp_GetLocalInfo(pLlcpTransport->pLlcp,&LlcpLinkParamInfo);
4385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(status != NFCSTATUS_SUCCESS)
4395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
4405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_FAILED);
4415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
4425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
4435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
4445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Search a socket free in the Socket Table*/
4455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      do
4465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
4475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         if(pLlcpTransport->pSocketTable[index].eSocket_State == phFriNfc_LlcpTransportSocket_eSocketDefault)
4485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
4495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the socket pointer to socket of the table */
4505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            *pLlcpSocket = &pLlcpTransport->pSocketTable[index];
4515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Store the socket info in the socket pointer */
4535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].eSocket_Type     = eType;
4545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].pSocketErrCb     = pErr_Cb;
4555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Store the context of the upper layer */
4575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].pContext   = pContext;
4585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the pointers to the different working buffers */
4605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            if(pLlcpTransport->pSocketTable[index].eSocket_Type != phFriNfc_LlcpTransport_eConnectionLess)
4615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
4625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Test the socket options */
4635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               if((psOptions->rw > PHFRINFC_LLCP_RW_MAX) && (eType == phFriNfc_LlcpTransport_eConnectionOriented))
4645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
4655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
4665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
4675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Set socket options */
468c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau               memcpy(&pLlcpTransport->pSocketTable[index].sSocketOption, psOptions, sizeof(phFriNfc_LlcpTransport_sSocketOptions_t));
4695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Set socket local params (MIUX & RW) */
471c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau               pLlcpTransport->pSocketTable[index].localMIUX = (pLlcpTransport->pSocketTable[index].sSocketOption.miu - PHFRINFC_LLCP_MIU_DEFAULT) & PHFRINFC_LLCP_TLV_MIUX_MASK;
472c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau               pLlcpTransport->pSocketTable[index].localRW   = pLlcpTransport->pSocketTable[index].sSocketOption.rw & PHFRINFC_LLCP_TLV_RW_MASK;
4735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Set the Max length for the Send and Receive Window Buffer */
475c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau               pLlcpTransport->pSocketTable[index].bufferSendMaxLength   = pLlcpTransport->pSocketTable[index].sSocketOption.miu;
476c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau               pLlcpTransport->pSocketTable[index].bufferRwMaxLength     = pLlcpTransport->pSocketTable[index].sSocketOption.miu * ((pLlcpTransport->pSocketTable[index].sSocketOption.rw & PHFRINFC_LLCP_TLV_RW_MASK));
4775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               pLlcpTransport->pSocketTable[index].bufferLinearLength    = psWorkingBuffer->length - pLlcpTransport->pSocketTable[index].bufferSendMaxLength - pLlcpTransport->pSocketTable[index].bufferRwMaxLength;
4785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Test the connection oriented buffers length */
4805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               if((pLlcpTransport->pSocketTable[index].bufferSendMaxLength + pLlcpTransport->pSocketTable[index].bufferRwMaxLength) > psWorkingBuffer->length
4815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                   || ((pLlcpTransport->pSocketTable[index].bufferLinearLength < PHFRINFC_LLCP_MIU_DEFAULT) && (pLlcpTransport->pSocketTable[index].bufferLinearLength != 0)))
4825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
4835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_BUFFER_TOO_SMALL);
4845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  return status;
4855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
4865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Set the pointer and the length for the Receive Window Buffer */
4885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               for(cpt=0;cpt<pLlcpTransport->pSocketTable[index].localRW;cpt++)
4895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
490c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau                  pLlcpTransport->pSocketTable[index].sSocketRwBufferTable[cpt].buffer = psWorkingBuffer->buffer + (cpt*pLlcpTransport->pSocketTable[index].sSocketOption.miu);
4915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  pLlcpTransport->pSocketTable[index].sSocketRwBufferTable[cpt].length = 0;
4925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
4935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /* Set the pointer and the length for the Send Buffer */
4955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               pLlcpTransport->pSocketTable[index].sSocketSendBuffer.buffer     = psWorkingBuffer->buffer + pLlcpTransport->pSocketTable[index].bufferRwMaxLength;
4965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               pLlcpTransport->pSocketTable[index].sSocketSendBuffer.length     = pLlcpTransport->pSocketTable[index].bufferSendMaxLength;
4975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               /** Set the pointer and the length for the Linear Buffer */
4995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.buffer   = psWorkingBuffer->buffer + pLlcpTransport->pSocketTable[index].bufferRwMaxLength + pLlcpTransport->pSocketTable[index].bufferSendMaxLength;
5005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.length   = pLlcpTransport->pSocketTable[index].bufferLinearLength;
5015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               if(pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.length != 0)
5035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
5045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  /* Init Cyclic Fifo */
5055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                  phFriNfc_Llcp_CyclicFifoInit(&pLlcpTransport->pSocketTable[index].sCyclicFifoBuffer,
5065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                               pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.buffer,
5075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                               pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.length);
5085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
5095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
5105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Store index of the socket */
5115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].index = index;
5125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the socket into created state */
5145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].eSocket_State = phFriNfc_LlcpTransportSocket_eSocketCreated;
5155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            return status;
5165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
5175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         else
5185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
5195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
5205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
5215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }while(index<PHFRINFC_LLCP_NB_SOCKET_MAX);
5225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INSUFFICIENT_RESOURCES);
5245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
5265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
5295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
5305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Close a socket on a LLCP-connected device</b>.
5315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
5325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function closes a LLCP socket previously created using phFriNfc_LlcpTransport_Socket.
5335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* If the socket was connected, it is first disconnected, and then closed.
5345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
5355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket                    A pointer to a phFriNfc_LlcpTransport_Socket_t.
5365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
5385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
5395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
5405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
5415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
5425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Close(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket)
5435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
5455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
5475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if( pLlcpSocket == NULL)
5485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
5495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
5505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type == phFriNfc_LlcpTransport_eConnectionOriented)
5525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
5535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Close(pLlcpSocket);
5545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type ==  phFriNfc_LlcpTransport_eConnectionLess)
5565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
5575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_Connectionless_Close(pLlcpSocket);
5585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
5605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
5615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
5625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
5655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
5685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
5695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Bind a socket to a local SAP</b>.
5705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
5715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function binds the socket to a local Service Access Point.
5725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
5735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
5745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pConfigInfo           A port number for a specific socket
5755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
5765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
5775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
5785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
5795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
5805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
5815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_ALREADY_REGISTERED       The selected SAP is already bound to another
5825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                             socket.
5835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
5845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
5855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Bind(phFriNfc_LlcpTransport_Socket_t    *pLlcpSocket,
5875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      uint8_t                            nSap)
5885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
5895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
5905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
5915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
5935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL)
5945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
5955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
5965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketCreated)
5985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
5995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
6005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(nSap<2 || nSap>63)
6025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
6065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Test if the nSap it is useb by another socket */
6085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++)
6095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
6105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         if((pLlcpSocket->psTransport->pSocketTable[i].socket_sSap == nSap)
6115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            && (pLlcpSocket->psTransport->pSocketTable[i].eSocket_Type == pLlcpSocket->eSocket_Type))
6125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
6135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            return status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_ALREADY_REGISTERED);
6145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
6155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
6165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Set the nSap value of the socket */
6175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpSocket->socket_sSap = nSap;
6185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Set the socket state */
6195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpSocket->eSocket_State = phFriNfc_LlcpTransportSocket_eSocketBound;
6205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
6225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
6235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*********************************************/
6255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*           ConnectionOriented              */
6265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*********************************************/
6275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
6295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
6305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Listen for incoming connection requests on a socket</b>.
6315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
6325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function switches a socket into a listening state and registers a callback on
6335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* incoming connection requests. In this state, the socket is not able to communicate
6345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* directly. The listening state is only available for connection-oriented sockets
6355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* which are still not connected. The socket keeps listening until it is closed, and
6365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* thus can trigger several times the pListen_Cb callback.
6375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
6385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
6395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
6405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psServiceName      A pointer to Service Name
6415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pListen_Cb         The callback to be called each time the
6425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                socket receive a connection request.
6435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
6445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
6455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
6465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
6475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
6485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
6495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state to switch
6505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            to listening state.
6515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
6525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
6535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Listen(phFriNfc_LlcpTransport_Socket_t*          pLlcpSocket,
6545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phNfc_sData_t                             *psServiceName,
6555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketListenCb_t   pListen_Cb,
6565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void*                                     pContext)
6575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
6585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
6595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
6615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || pListen_Cb == NULL|| pContext == NULL || psServiceName == NULL)
6625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket state */
6665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
6675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
6695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket type */
6715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
6725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if a listen is not pending with this socket */
6765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->bSocketListenPending)
6775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the length of the SN */
6815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psServiceName->length > PHFRINFC_LLCP_SN_MAX_LENGTH)
6825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
683a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6853a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   /* Test the SAP range for SDP-advertised services */
6863a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   else if((psServiceName->length > 0) &&
6873a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau           (!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST, PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST)) &&
6883a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau           (!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_WKS_FIRST, PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST)))
6893a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   {
6903a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6913a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   }
6923a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   /* Test the SAP range for non SDP-advertised services */
6933a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   else if((psServiceName->length == 0) &&
6943a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau           (!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST, PHFRINFC_LLCP_SAP_NUMBER)) &&
6953a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau           (!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_WKS_FIRST, PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST)))
6963a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   {
6973a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6983a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   }
6995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
7005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
7015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Listen(pLlcpSocket,
7025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                psServiceName,
7035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                pListen_Cb,
7045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                pContext);
7055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
7075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
7105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
7115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Accept an incoming connection request for a socket</b>.
7125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
7135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This functions allows the client to accept an incoming connection request.
7145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* It must be used with the socket provided within the listen callback. The socket
7155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* is implicitly switched to the connected state when the function is called.
7165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
7175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
7185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psOptions             The options to be used with the socket.
7195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psWorkingBuffer       A working buffer to be used by the library.
7205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pErr_Cb               The callback to be called each time the accepted socket
7215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                   is in error.
7225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext              Upper layer context to be returned in the callback.
7235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
7245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
7255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
7265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
7275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_BUFFER_TOO_SMALL         The working buffer is too small for the MIU and RW
7285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            declared in the options.
7295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
7305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
7315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Accept(phFriNfc_LlcpTransport_Socket_t*             pLlcpSocket,
7325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_sSocketOptions_t*     psOptions,
7335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phNfc_sData_t*                               psWorkingBuffer,
7345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketErrCb_t         pErr_Cb,
7355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketAcceptCb_t      pAccept_RspCb,
7365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void*                                        pContext)
7375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
7385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
7395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
7415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psOptions == NULL || psWorkingBuffer == NULL || pErr_Cb == NULL || pContext == NULL)
7425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
7435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
7445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket state */
7465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
7475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
7485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
7495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket type */
7515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
7525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
7535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
7545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the socket options */
7565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psOptions->rw > PHFRINFC_LLCP_RW_MAX)
7575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
7585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
7595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
7615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
762a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Set the Max length for the Send and Receive Window Buffer */
763a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      pLlcpSocket->bufferSendMaxLength   = psOptions->miu;
764a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      pLlcpSocket->bufferRwMaxLength     = psOptions->miu * ((psOptions->rw & PHFRINFC_LLCP_TLV_RW_MASK));
765a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      pLlcpSocket->bufferLinearLength    = psWorkingBuffer->length - pLlcpSocket->bufferSendMaxLength - pLlcpSocket->bufferRwMaxLength;
7665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
767a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Test the buffers length */
768a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      if((pLlcpSocket->bufferSendMaxLength + pLlcpSocket->bufferRwMaxLength) > psWorkingBuffer->length
769a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir          || ((pLlcpSocket->bufferLinearLength < PHFRINFC_LLCP_MIU_DEFAULT)  && (pLlcpSocket->bufferLinearLength != 0)))
770a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
771a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_BUFFER_TOO_SMALL);
772a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
773a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else
774a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
775a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         pLlcpSocket->psTransport->socketIndex = pLlcpSocket->index;
776a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
777a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status  = phFriNfc_LlcpTransport_ConnectionOriented_Accept(pLlcpSocket,
778a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    psOptions,
779a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    psWorkingBuffer,
780a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    pErr_Cb,
781a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    pAccept_RspCb,
782a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    pContext);
783a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
7845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
7865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
7895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
7905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Reject an incoming connection request for a socket</b>.
7915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
7925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This functions allows the client to reject an incoming connection request.
7935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* It must be used with the socket provided within the listen callback. The socket
7945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* is implicitly closed when the function is called.
7955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
7965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
7975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pReject_RspCb         The callback to be call when the Reject operation is completed
7985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext              Upper layer context to be returned in the callback.
7995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
8005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
8015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
8025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
8035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
8045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
8055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Reject( phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
8065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          pphFriNfc_LlcpTransportSocketRejectCb_t   pReject_RspCb,
8075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          void                                      *pContext)
8085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
8105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
8125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL)
8135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
8155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket state */
8175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
8185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
8205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket type */
8225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
8235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
8255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
8275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phLibNfc_LlcpTransport_ConnectionOriented_Reject(pLlcpSocket,
8295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                pReject_RspCb,
8305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                pContext);
8315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
8345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
8355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
8375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
8385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Try to establish connection with a socket on a remote SAP</b>.
8395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
8405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function tries to connect to a given SAP on the remote peer. If the
8415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* socket is not bound to a local SAP, it is implicitly bound to a free SAP.
8425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
8435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
8445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  nSap               The destination SAP to connect to.
8455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pConnect_RspCb     The callback to be called when the connection
8465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
8475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
8485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
8495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
8505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
8515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
8525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
8535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Connection operation is in progress,
8545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pConnect_RspCb will be called upon completion.
8555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
8565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
8575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
8585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
8595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Connect( phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
8605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          uint8_t                                    nSap,
8615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          pphFriNfc_LlcpTransportSocketConnectCb_t   pConnect_RspCb,
8625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          void*                                      pContext)
8635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
8645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
8655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
8665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
86799e7261d709ad32e0203dced9f4a386c5f719adfArnaud Ferir   /* Check for NULL pointers */
868a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir   if(pLlcpSocket == NULL || pConnect_RspCb == NULL || pContext == NULL)
8695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
8715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the port number value */
8735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(nSap<02 || nSap>63)
8745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
8765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
8785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
8795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
8815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
8835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is not in connecting or connected state*/
8845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketCreated && pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
8855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
8865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
8875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
8885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
8895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
890fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      /* Implicit bind if socket is not already bound */
8915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
8925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
893fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         status = phFriNfc_LlcpTransport_AutoBind(pLlcpSocket);
894fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         if (status != NFCSTATUS_SUCCESS)
8955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
896fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            return status;
8975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
8985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
8995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9003a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      /* Test the SAP range for non SDP-advertised services */
9013a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      if(!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST, PHFRINFC_LLCP_SAP_NUMBER))
9023a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
9033a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
9043a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
9053a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      else
9063a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
9073a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = phFriNfc_LlcpTransport_ConnectionOriented_Connect(pLlcpSocket,
9083a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    nSap,
9093a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    NULL,
9103a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pConnect_RspCb,
9113a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pContext);
9123a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
9135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
9145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
9165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
9175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
9195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
9205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Try to establish connection with a socket on a remote service, given its URI</b>.
9215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
9225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function tries to connect to a SAP designated by an URI. If the
9235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* socket is not bound to a local SAP, it is implicitly bound to a free SAP.
9245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
9255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
9265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psUri              The URI corresponding to the destination SAP to connect to.
9275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pConnect_RspCb     The callback to be called when the connection
9285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
9295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
9305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
9315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
9325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
9335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
9345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
9355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Connection operation is in progress,
9365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pConnect_RspCb will be called upon completion.
9375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
9385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
9395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
9405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
9415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
9425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
9435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_ConnectByUri(phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
9445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                              phNfc_sData_t*                             psUri,
9455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                              pphFriNfc_LlcpTransportSocketConnectCb_t   pConnect_RspCb,
9465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                              void*                                      pContext)
9475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
9485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
9495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
9505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
9525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || pConnect_RspCb == NULL || pContext == NULL)
9535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
9545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
9555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
9565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
9575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
9585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
9595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
9605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
9615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is not in connect pending or connected state*/
9625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketConnecting || pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketConnected)
9635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
9645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
9655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
9665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the length of the SN */
9675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psUri->length > PHFRINFC_LLCP_SN_MAX_LENGTH)
9685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
969a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
9705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
9715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
9725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
973fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      /* Implicit bind if socket is not already bound */
9745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
9755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
976fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         status = phFriNfc_LlcpTransport_AutoBind(pLlcpSocket);
977fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         if (status != NFCSTATUS_SUCCESS)
9785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
979fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            return status;
9805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
9813a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
9823a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau
9833a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      /* Test the SAP range for non SDP-advertised services */
9843a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      if(!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST, PHFRINFC_LLCP_SAP_NUMBER))
9853a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
9863a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
9873a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
9883a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      else
9893a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
9903a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = phFriNfc_LlcpTransport_ConnectionOriented_Connect(pLlcpSocket,
9913a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    PHFRINFC_LLCP_SAP_DEFAULT,
9923a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    psUri,
9933a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pConnect_RspCb,
9943a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pContext);
9955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
9965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
997a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
9985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
9995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
10025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
10035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Disconnect a currently connected socket</b>.
10045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function initiates the disconnection of a previously connected socket.
10065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
10085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pDisconnect_RspCb  The callback to be called when the
10095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
10105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
10115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
10125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
10145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
10155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
10165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Disconnection operation is in progress,
10175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pDisconnect_RspCb will be called upon completion.
10185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
10195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
10205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
10215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
10225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
10235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
10245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Disconnect(phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
10255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            pphLibNfc_LlcpSocketDisconnectCb_t         pDisconnect_RspCb,
10265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            void*                                      pContext)
10275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
10285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
10295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
10315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || pDisconnect_RspCb == NULL || pContext == NULL)
10325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
10365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
10375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1038a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is connected  state*/
10415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketConnected)
10425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly       status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
10465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phLibNfc_LlcpTransport_ConnectionOriented_Disconnect(pLlcpSocket,
10485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                    pDisconnect_RspCb,
10495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                    pContext);
10505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
10535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
10565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
10575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Send data on a socket</b>.
10585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to write data on a socket. This function
10605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* can only be called on a connection-oriented socket which is already
10615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* in a connected state.
10625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
10655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer containing the data to send.
10665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pSend_RspCb        The callback to be called when the
10675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
10685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
10695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
10705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
10725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
10735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
10745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
10755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pSend_RspCb will be called upon completion.
10765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
10775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
10785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
10795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
10805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Send(phFriNfc_LlcpTransport_Socket_t*             pLlcpSocket,
10815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      phNfc_sData_t*                               psBuffer,
10825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      pphFriNfc_LlcpTransportSocketSendCb_t        pSend_RspCb,
10835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      void*                                        pContext)
10845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
10855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
10865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
10885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pSend_RspCb == NULL || pContext == NULL)
10895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
10935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
10945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in connected state */
10985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketConnected)
10995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
11015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the length of the buffer */
11035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psBuffer->length > pLlcpSocket->remoteMIU )
11045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
11065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if a send is pending */
1108407d793f3038222dac95651866b99cd064e214ebArnaud Ferir   else if(pLlcpSocket->pfSocketSend_Cb != NULL)
11095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
11115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
11135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Send(pLlcpSocket,
11155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              psBuffer,
11165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pSend_RspCb,
11175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pContext);
11185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
11215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
11225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
11245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
11255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Read data on a socket</b>.
11265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to read data from a socket. It reads at most the
11285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* size of the reception buffer, but can also return less bytes if less bytes
11295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* are available. If no data is available, the function will be pending until
11305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* more data comes, and the response will be sent by the callback. This function
11315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* can only be called on a connection-oriented socket.
11325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
11355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer receiving the data.
11365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pRecv_RspCb        The callback to be called when the
11375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
11385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
11395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
11405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
11425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
11435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
11445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
11455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pRecv_RspCb will be called upon completion.
11465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
11475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
11485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
11495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
11505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Recv( phFriNfc_LlcpTransport_Socket_t*             pLlcpSocket,
11515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                       phNfc_sData_t*                               psBuffer,
11525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                       pphFriNfc_LlcpTransportSocketRecvCb_t        pRecv_RspCb,
11535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                       void*                                        pContext)
11545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
11555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
11565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
11585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pRecv_RspCb == NULL || pContext == NULL)
11595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
11615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
11635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
11645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
11665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in connected state */
116804f25b3ab4ed13edb3ffc8e7354bca9f7bc60b44Sylvain Fonteneau   else if(pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketDefault)
11695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
11715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if a receive is pending */
11735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->bSocketRecvPending == TRUE)
11745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
11765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
11785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
11795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Recv(pLlcpSocket,
11805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              psBuffer,
11815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pRecv_RspCb,
11825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pContext);
11835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
11845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
11865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
11875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*****************************************/
11895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*           ConnectionLess              */
11905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*****************************************/
11915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
11925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
11935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
11945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Send data on a socket to a given destination SAP</b>.
11955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to write data on a socket to a given destination SAP.
11975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function can only be called on a connectionless socket.
11985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
12005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a LlcpSocket created.
12015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  nSap               The destination SAP.
12025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer containing the data to send.
12035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pSend_RspCb        The callback to be called when the
12045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
12055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
12065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
12075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
12085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
12095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
12105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
12115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
12125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pSend_RspCb will be called upon completion.
12135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
12145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
12155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
12165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
12175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_SendTo( phFriNfc_LlcpTransport_Socket_t             *pLlcpSocket,
12185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         uint8_t                                     nSap,
12195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         phNfc_sData_t                               *psBuffer,
12205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         pphFriNfc_LlcpTransportSocketSendCb_t       pSend_RspCb,
12215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         void*                                       pContext)
12225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
12235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
12245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_Llcp_sLinkParameters_t  LlcpRemoteLinkParamInfo;
12255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pSend_RspCb == NULL || pContext == NULL)
12275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
12295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
12305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the port number value */
12315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(nSap<2 || nSap>63)
12325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
12345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
12355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionless socket */
12365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionLess)
12375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly       status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
12395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
12405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in an updated state */
12415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
12425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
12445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
12455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
12465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1247a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Get the local parameters of the LLCP Link */
1248a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = phFriNfc_Llcp_GetRemoteInfo(pLlcpSocket->psTransport->pLlcp,&LlcpRemoteLinkParamInfo);
1249a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      if(status != NFCSTATUS_SUCCESS)
1250a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
1251a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_FAILED);
1252a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
1253a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Test the length of the socket buffer for ConnectionLess mode*/
1254a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else if(psBuffer->length > LlcpRemoteLinkParamInfo.miu)
1255a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
1256a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
1257a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
1258a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Test if the link is in error state */
1259a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else if(pLlcpSocket->psTransport->LinkStatusError)
1260a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
1261a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
1262a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
1263a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else
1264a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
1265a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = phFriNfc_LlcpTransport_Connectionless_SendTo(pLlcpSocket,
1266a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               nSap,
1267a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               psBuffer,
1268a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               pSend_RspCb,
1269a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               pContext);
1270a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
12715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
1272a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
12735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
12745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
12755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
12785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
12795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Read data on a socket and get the source SAP</b>.
12805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
12815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is the same as phLibNfc_Llcp_Recv, except that the callback includes
12825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the source SAP. This functions can only be called on a connectionless socket.
12835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
12845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
12855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a LlcpSocket created.
12865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer receiving the data.
12875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pRecv_RspCb        The callback to be called when the
12885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
12895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
12905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
12915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
12925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
12935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
12945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
12955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
12965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pRecv_RspCb will be called upon completion.
12975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
12985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
12995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
13005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
13015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
13025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
13035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_RecvFrom( phFriNfc_LlcpTransport_Socket_t                   *pLlcpSocket,
13045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           phNfc_sData_t*                                    psBuffer,
13055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           pphFriNfc_LlcpTransportSocketRecvFromCb_t         pRecv_Cb,
13065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           void*                                             pContext)
13075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
13085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
13095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pRecv_Cb == NULL || pContext == NULL)
13105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
13125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionless socket */
13145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionLess)
13155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1316a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
13175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in an updated state */
13195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
13205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
13225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
13245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if(pLlcpSocket->bSocketRecvPending)
13265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
13275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
13285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
13295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      else
13305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
13315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         status = phLibNfc_LlcpTransport_Connectionless_RecvFrom(pLlcpSocket,
13325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                 psBuffer,
13335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                 pRecv_Cb,
13345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                 pContext);
13355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
13365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
1337a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
13385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
13395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
1340