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*/
267c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen#include <cutils/log.h>
27cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau#include <phOsalNfc.h>
285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfcStatus.h>
295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phLibNfc.h>
305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phNfcLlcpTypes.h>
315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_Llcp.h>
325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_LlcpTransport.h>
335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_LlcpTransport_Connectionless.h>
345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly#include <phFriNfc_LlcpTransport_Connection.h>
355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
363a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau/* local macros */
373a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau
383a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau/* Check if (a <= x < b) */
393a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau#define IS_BETWEEN(x, a, b) (((x)>=(a)) && ((x)<(b)))
403a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau
417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_RegisterName(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket,
427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                     uint8_t                            nSap,
437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                     phNfc_sData_t                      *psServiceName);
445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_DiscoverServicesEx(phFriNfc_LlcpTransport_t *psTransport);
467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic void phFriNfc_LlcpTransport_Send_CB(void            *pContext,
487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                           NFCSTATUS        status);
497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_GetFreeSap(phFriNfc_LlcpTransport_t * psTransport, phNfc_sData_t *psServiceName, uint8_t * pnSap)
51fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau{
52fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   uint8_t i;
53fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   uint8_t sap;
547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t min_sap_range, max_sap_range;
557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phFriNfc_LlcpTransport_Socket_t* pSocketTable = psTransport->pSocketTable;
567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Calculate authorized SAP range */
587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if ((psServiceName != NULL) && (psServiceName->length > 0))
597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
607b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Make sure that we will return the same SAP if service name was already used in the past */
617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      for(i=0 ; i<PHFRINFC_LLCP_SDP_ADVERTISED_NB ; i++)
627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if((psTransport->pCachedServiceNames[i].sServiceName.length > 0) &&
647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            (memcmp(psTransport->pCachedServiceNames[i].sServiceName.buffer, psServiceName->buffer, psServiceName->length) == 0))
657b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
667b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Service name matched in cached service names list */
677b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            *pnSap = psTransport->pCachedServiceNames[i].nSap;
687b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            return NFCSTATUS_SUCCESS;
697b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
707b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
717b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
727b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* SDP advertised service */
737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      min_sap_range = PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST;
747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      max_sap_range = PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST;
757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   else
777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Non-SDP advertised service */
797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      min_sap_range = PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST;
807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      max_sap_range = PHFRINFC_LLCP_SAP_NUMBER;
817b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
82fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
83fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   /* Try all possible SAPs */
847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   for(sap=min_sap_range ; sap<max_sap_range ; sap++)
85fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   {
86fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      /* Go through socket list to check if current SAP is in use */
87fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      for(i=0 ; i<PHFRINFC_LLCP_NB_SOCKET_MAX ; i++)
88fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      {
89fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         if((pSocketTable[i].eSocket_State >= phFriNfc_LlcpTransportSocket_eSocketBound) &&
90fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            (pSocketTable[i].socket_sSap == sap))
91fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         {
92fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            /* SAP is already in use */
93fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            break;
94fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         }
95fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      }
96fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
97fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      if (i >= PHFRINFC_LLCP_NB_SOCKET_MAX)
98fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      {
99fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         /* No socket is using current SAP, proceed with binding */
1007b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         *pnSap = sap;
101fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         return NFCSTATUS_SUCCESS;
102fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      }
103fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   }
104fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
105fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   /* If we reach this point, it means that no SAP is free */
106fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau   return NFCSTATUS_INSUFFICIENT_RESOURCES;
107fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau}
108fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau
1097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_EncodeSdreqTlv(phNfc_sData_t  *psTlvData,
1107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                       uint32_t       *pOffset,
1117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                       uint8_t        nTid,
1127b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                       phNfc_sData_t  *psServiceName)
1137b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
1147b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS result;
1157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t nTlvOffset = *pOffset;
1167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t nTlvStartOffset = nTlvOffset;
1177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Encode the TID */
1197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   result = phFriNfc_Llcp_EncodeTLV(psTlvData,
1207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    &nTlvOffset,
1217b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    PHFRINFC_LLCP_TLV_TYPE_SDREQ,
1227b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    1,
1237b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    &nTid);
1247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (result != NFCSTATUS_SUCCESS)
1257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
1267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      goto clean_and_return;
1277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
1287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1297b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Encode the service name itself */
1307b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   result = phFriNfc_Llcp_AppendTLV(psTlvData,
1317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    nTlvStartOffset,
1327b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    &nTlvOffset,
1337b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    psServiceName->length,
1347b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    psServiceName->buffer);
1357b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (result != NFCSTATUS_SUCCESS)
1367b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
1377b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      goto clean_and_return;
1387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
1397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogiclean_and_return:
1417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Save offset if no error occured */
1427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (result == NFCSTATUS_SUCCESS)
1437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
1447b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      *pOffset = nTlvOffset;
1457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
1467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return result;
1487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
1497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_EncodeSdresTlv(phNfc_sData_t  *psTlvData,
1517b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                       uint32_t       *pOffset,
1527b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                       uint8_t        nTid,
1537b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                       uint8_t        nSap)
1547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
1557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS result;
1567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t nTlvStartOffset = *pOffset;
1577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Encode the TID */
1597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   result = phFriNfc_Llcp_EncodeTLV(psTlvData,
1607b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    pOffset,
1617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    PHFRINFC_LLCP_TLV_TYPE_SDRES,
1627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    1,
1637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    &nTid);
1647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (result != NFCSTATUS_SUCCESS)
1657b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
1667b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      goto clean_and_return;
1677b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
1687b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1697b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Encode the service name itself */
1707b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   result = phFriNfc_Llcp_AppendTLV(psTlvData,
1717b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    nTlvStartOffset,
1727b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    pOffset,
1737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    1,
1747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                    &nSap);
1757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (result != NFCSTATUS_SUCCESS)
1767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
1777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      goto clean_and_return;
1787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
1797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogiclean_and_return:
1817b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Restore previous offset if an error occured */
1827b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (result != NFCSTATUS_SUCCESS)
1837b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
1847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      *pOffset = nTlvStartOffset;
1857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
1867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return result;
1887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
1897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic phFriNfc_LlcpTransport_Socket_t* phFriNfc_LlcpTransport_ServiceNameLoockup(phFriNfc_LlcpTransport_t *psTransport,
1917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                                                  phNfc_sData_t            *pServiceName)
1927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
1937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t                            index;
1947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                             cacheIndex;
1957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phFriNfc_Llcp_CachedServiceName_t * pCachedServiceName;
1967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phFriNfc_LlcpTransport_Socket_t *   pSocket;
1977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
1987b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Search a socket with the SN */
1997b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   for(index=0;index<PHFRINFC_LLCP_NB_SOCKET_MAX;index++)
2007b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
2017b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pSocket = &psTransport->pSocketTable[index];
2027b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Test if the CO socket is in Listen state or the CL socket is bound
2037b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         and if its SN is the good one */
2047b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if((((pSocket->eSocket_Type == phFriNfc_LlcpTransport_eConnectionOriented)
2057b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         && (pSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketRegistered))
2067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         || ((pSocket->eSocket_Type == phFriNfc_LlcpTransport_eConnectionLess)
2077b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         && (pSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketBound)))
2087b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         &&
2097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         (pServiceName->length == pSocket->sServiceName.length)
2107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         && !memcmp(pServiceName->buffer, pSocket->sServiceName.buffer, pServiceName->length))
2117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
2127b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Add new entry to cached service name/sap if not already in table */
2137b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         for(cacheIndex=0;cacheIndex<PHFRINFC_LLCP_SDP_ADVERTISED_NB;cacheIndex++)
2147b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
2157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            pCachedServiceName = &psTransport->pCachedServiceNames[cacheIndex];
2167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if (pCachedServiceName->sServiceName.buffer != NULL)
2177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
2187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               if ((pCachedServiceName->sServiceName.length == pServiceName->length) &&
2197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                   (memcmp(pCachedServiceName->sServiceName.buffer, pServiceName->buffer, pServiceName->length) == 0))
2207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               {
2217b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  /* Already registered */
2227b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  break;
2237b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               }
2247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
2257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            else
2267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
2277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Reached end of existing entries and not found the service name,
2287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                * => Add the new entry
2297b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                */
2307b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               pCachedServiceName->nSap = pSocket->socket_sSap;
2317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               pCachedServiceName->sServiceName.buffer = phOsalNfc_GetMemory(pServiceName->length);
2327b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               if (pCachedServiceName->sServiceName.buffer == NULL)
2337b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               {
2347b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  /* Unable to cache this entry, so report this service as not found */
2357b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  return NULL;
2367b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               }
2377b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               memcpy(pCachedServiceName->sServiceName.buffer, pServiceName->buffer, pServiceName->length);
2387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               pCachedServiceName->sServiceName.length = pServiceName->length;
2397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               break;
2407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
2417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
2427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         return pSocket;
2447b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
2457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
2467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return NULL;
2487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
2497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2517b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_DiscoveryAnswer(phFriNfc_LlcpTransport_t *psTransport)
2527b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
2537b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS         result = NFCSTATUS_PENDING;
2547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phNfc_sData_t     sInfoBuffer;
2557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t          nTlvOffset;
2567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t           index;
2577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t           nTid, nSap;
2587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Test if a send is pending */
2607c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   if(!testAndSetSendPending(psTransport))
2617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
2627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Set the header */
2637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->sLlcpHeader.dsap  = PHFRINFC_LLCP_SAP_SDP;
2647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->sLlcpHeader.ptype = PHFRINFC_LLCP_PTYPE_SNL;
2657b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->sLlcpHeader.ssap  = PHFRINFC_LLCP_SAP_SDP;
2667b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2677b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Prepare the info buffer */
2687b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      sInfoBuffer.buffer = psTransport->pDiscoveryBuffer;
2697b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      sInfoBuffer.length = sizeof(psTransport->pDiscoveryBuffer);
2707b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2717b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Encode as many requests as possible */
2727b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      nTlvOffset = 0;
2737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      for(index=0 ; index<psTransport->nDiscoveryResListSize ; index++)
2747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
2757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Get current TID/SAP and try to encode them in SNL frame */
2767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         nTid = psTransport->nDiscoveryResTidList[index];
2777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         nSap = psTransport->nDiscoveryResSapList[index];
2787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Encode response */
2797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         result = phFriNfc_LlcpTransport_EncodeSdresTlv(&sInfoBuffer,
2807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                        &nTlvOffset,
2817b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                        nTid,
2827b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                        nSap);
2837b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if (result != NFCSTATUS_SUCCESS)
2847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
2857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Impossible to fit the entire response */
2867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* TODO: support reponse framgentation */
2877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            break;
2887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
2897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
2907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Reset list size to be able to handle a new request */
2927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->nDiscoveryResListSize = 0;
2937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Update buffer length to match real TLV size */
2957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      sInfoBuffer.length = nTlvOffset;
2967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
2977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Send SNL frame */
2987b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      result =  phFriNfc_Llcp_Send(psTransport->pLlcp,
2997b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   &psTransport->sLlcpHeader,
3007b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   NULL,
3017b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   &sInfoBuffer,
3027b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   phFriNfc_LlcpTransport_Send_CB,
3037b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   psTransport);
3047b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
3057b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   else
3067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
3077b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Impossible to send now, this function will be called again on next opportunity */
3087b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
3097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return result;
3117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
3127b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3137b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3147b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic void Handle_Discovery_IncomingFrame(phFriNfc_LlcpTransport_t           *psTransport,
3157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                           phNfc_sData_t                      *psData)
3167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
3177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS                        result;
3187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phNfc_sData_t                    sValue;
3197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phNfc_sData_t                    sResponseData;
3207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phNfc_sData_t                    sServiceName;
3217b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t                         nInTlvOffset;
3227b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                          nType;
3237b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                          nTid;
3247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                          nSap;
3257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pphFriNfc_Cr_t                   pfSavedCb;
3267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   void                             *pfSavedContext;
3277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phFriNfc_LlcpTransport_Socket_t  *pSocket;
3287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3297b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3307b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Prepare buffer */
3317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   sResponseData.buffer = psTransport->pDiscoveryBuffer;
3327b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   sResponseData.length = sizeof(psTransport->pDiscoveryBuffer);
3337b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3347b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Parse all TLVs in frame */
3357b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   nInTlvOffset = 0;
3367b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   while(nInTlvOffset < psData->length)
3377b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
3387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      result = phFriNfc_Llcp_DecodeTLV(psData,
3397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                       &nInTlvOffset,
3407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                       &nType,
3417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                       &sValue );
3427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      switch(nType)
3437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
3447b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         case PHFRINFC_LLCP_TLV_TYPE_SDREQ:
3457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if (sValue.length < 2)
3467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Erroneous request, ignore */
3487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               break;
3497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
3507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Decode TID */
3517b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            nTid = sValue.buffer[0];
3527b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Decode service name */
3537b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            sServiceName.buffer = sValue.buffer + 1;
3547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            sServiceName.length = sValue.length - 1;
3557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Handle SDP service name */
3577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if((sServiceName.length == sizeof(PHFRINFC_LLCP_SERVICENAME_SDP)-1)
3587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               && !memcmp(sServiceName.buffer, PHFRINFC_LLCP_SERVICENAME_SDP, sServiceName.length))
3597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3607b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               nSap = PHFRINFC_LLCP_SAP_SDP;
3617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
3627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            else
3637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Match service name in socket list */
3657b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               pSocket = phFriNfc_LlcpTransport_ServiceNameLoockup(psTransport, &sServiceName);
3667b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               if (pSocket != NULL)
3677b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               {
3687b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  nSap = pSocket->socket_sSap;
3697b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               }
3707b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               else
3717b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               {
3727b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  nSap = 0;
3737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               }
3747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
3757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Encode response */
3777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if (psTransport->nDiscoveryResListSize < PHFRINFC_LLCP_SNL_RESPONSE_MAX)
3787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               psTransport->nDiscoveryResSapList[psTransport->nDiscoveryResListSize] = nSap;
3807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               psTransport->nDiscoveryResTidList[psTransport->nDiscoveryResListSize] = nTid;
3817b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               psTransport->nDiscoveryResListSize++;
3827b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
3837b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            else
3847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Remote peer is sending more than max. allowed requests (max. 256
3867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                  different TID values), drop invalid requests to avoid buffer overflow
3877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               */
3887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
3897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            break;
3907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
3917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         case PHFRINFC_LLCP_TLV_TYPE_SDRES:
3927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if (psTransport->pfDiscover_Cb == NULL)
3937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Ignore response when no requests are pending */
3957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               break;
3967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
3977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if (sValue.length != 2)
3987b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
3997b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Erroneous response, ignore it */
4007b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               break;
4017b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
4027b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Decode TID and SAP */
4037b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            nTid = sValue.buffer[0];
4047b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if (nTid >= psTransport->nDiscoveryListSize)
4057b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
4067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Unkown TID, ignore it */
4077b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               break;
4087b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
4097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            nSap = sValue.buffer[1];
4107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Save response */
4117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            psTransport->pnDiscoverySapList[nTid] = nSap;
4127b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Update response counter */
4137b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            psTransport->nDiscoveryResOffset++;
4147b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            break;
4157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         default:
4177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Ignored */
4187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            break;
4197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
4207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
4217b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4227b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* If discovery requests have been received, send response */
4237b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (psTransport->nDiscoveryResListSize > 0)
4247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
4257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      phFriNfc_LlcpTransport_DiscoveryAnswer(psTransport);
4267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
4277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* If all discovery responses have been received, trigger callback (if any) */
4297b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if ((psTransport->pfDiscover_Cb != NULL) &&
4307b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi       (psTransport->nDiscoveryResOffset >= psTransport->nDiscoveryListSize))
4317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
4327b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pfSavedCb = psTransport->pfDiscover_Cb;
4337b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pfSavedContext = psTransport->pDiscoverContext;
4347b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4357b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->pfDiscover_Cb = NULL;
4367b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->pDiscoverContext = NULL;
4377b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pfSavedCb(pfSavedContext, NFCSTATUS_SUCCESS);
4397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
4407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
4417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* TODO: comment function Transport recv CB */
4445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pellystatic void phFriNfc_LlcpTransport__Recv_CB(void            *pContext,
4455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            phNfc_sData_t   *psData,
4465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            NFCSTATUS        status)
4475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
4485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_Llcp_sPacketHeader_t   sLlcpLocalHeader;
4495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t   dsap;
4505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t   ptype;
4515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t   ssap;
4525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_LlcpTransport_t* pLlcpTransport = (phFriNfc_LlcpTransport_t*)pContext;
4545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(status != NFCSTATUS_SUCCESS)
4565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
4575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->LinkStatusError = TRUE;
4585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
4595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
4605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
4615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      phFriNfc_Llcp_Buffer2Header( psData->buffer,0x00, &sLlcpLocalHeader);
4625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      dsap  = (uint8_t)sLlcpLocalHeader.dsap;
4645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      ptype = (uint8_t)sLlcpLocalHeader.ptype;
4655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      ssap  = (uint8_t)sLlcpLocalHeader.ssap;
4665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Update the length value (without the header length) */
4685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      psData->length = psData->length - PHFRINFC_LLCP_PACKET_HEADER_SIZE;
4695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Update the buffer pointer */
4715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      psData->buffer = psData->buffer + PHFRINFC_LLCP_PACKET_HEADER_SIZE;
4725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      switch(ptype)
4745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
4755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Connectionless */
4765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_UI:
4775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
4785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Handle_Connectionless_IncommingFrame(pLlcpTransport,
4795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                 psData,
4805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                 dsap,
4815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                 ssap);
4825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }break;
4835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
4847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Service Discovery Protocol */
4857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      case PHFRINFC_LLCP_PTYPE_SNL:
4867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
4877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if ((ssap == PHFRINFC_LLCP_SAP_SDP) && (dsap == PHFRINFC_LLCP_SAP_SDP))
4887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
4897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               Handle_Discovery_IncomingFrame(pLlcpTransport,
4907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                              psData);
4917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
4927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            else
4937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
4947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               /* Ignore frame if source and destination are not the SDP service */
4957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
4967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }break;
4977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
4985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Connection oriented */
4995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* NOTE: forward reserved PTYPE to enable FRMR sending */
5005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_CONNECT:
5015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_CC:
5025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_DISC:
5035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_DM:
5045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_I:
5055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RR:
5065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RNR:
5075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_FRMR:
5085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED1:
5095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED2:
5105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      case PHFRINFC_LLCP_PTYPE_RESERVED3:
5115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
5125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            Handle_ConnectionOriented_IncommingFrame(pLlcpTransport,
5135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     psData,
5145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     dsap,
5155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     ptype,
5165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                     ssap);
5175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }break;
5185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      default:
5195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
5205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }break;
5225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
5235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /*Restart the Receive Loop */
5255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status  = phFriNfc_Llcp_Recv(pLlcpTransport->pLlcp,
5265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   phFriNfc_LlcpTransport__Recv_CB,
5275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   pLlcpTransport);
5285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
5295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
5305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
5317c4b4fadb66959c50c170182847886e83393eebfMartijn Coenenbool_t testAndSetSendPending(phFriNfc_LlcpTransport_t* transport) {
5327c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    bool_t currentValue;
5337c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    pthread_mutex_lock(&transport->mutex);
5347c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    currentValue = transport->bSendPending;
5357c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    transport->bSendPending = TRUE;
5367c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    pthread_mutex_unlock(&transport->mutex);
5377c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    return currentValue;
5387c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen}
5397c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen
5407c4b4fadb66959c50c170182847886e83393eebfMartijn Coenenvoid clearSendPending(phFriNfc_LlcpTransport_t* transport) {
5417c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    pthread_mutex_lock(&transport->mutex);
5427c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    transport->bSendPending = FALSE;
5437c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen    pthread_mutex_unlock(&transport->mutex);
5447c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen}
5455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
546955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi/* TODO: comment function Transport recv CB */
547955a8efe369ece977e4757f0ae37b3931169125bSunil Jogistatic void phFriNfc_LlcpTransport_Send_CB(void            *pContext,
548955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                           NFCSTATUS        status)
549955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi{
550955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   phFriNfc_LlcpTransport_t         *psTransport = (phFriNfc_LlcpTransport_t*)pContext;
551955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   NFCSTATUS                        result = NFCSTATUS_FAILED;
552955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   phNfc_sData_t                    sFrmrBuffer;
5537c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   phFriNfc_Llcp_LinkSend_CB_t      pfSavedCb;
554955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   void                             *pSavedContext;
555955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   phFriNfc_LlcpTransport_Socket_t  *pCurrentSocket = NULL;
556955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   uint8_t                          index;
557955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
5587c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   // Store callbacks and socket index, so they can safely be
5597c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   // overwritten by any code in the callback itself.
5607c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   pfSavedCb = psTransport->pfLinkSendCb;
5617c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   pSavedContext = psTransport->pLinkSendContext;
5627c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   psTransport->pfLinkSendCb = NULL;
5637c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   psTransport->pLinkSendContext = NULL;
5647c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   index = psTransport->socketIndex;
5657c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen
566955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* 1 - Reset the FLAG send pending*/
5677c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   clearSendPending(psTransport);
568955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
569955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* 2 - Handle pending error responses */
570955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   if(psTransport->bFrmrPending)
571955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
5727c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen      if (!testAndSetSendPending(psTransport)) {
5737c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen         /* Reset FRMR pending */
5747c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen         psTransport->bFrmrPending = FALSE;
575955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
5767c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen         /* Send Frmr */
5777c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen         sFrmrBuffer.buffer = psTransport->FrmrInfoBuffer;
5787c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen         sFrmrBuffer.length = 0x04; /* Size of FRMR Information field */
579955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
5807c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen         result =  phFriNfc_Llcp_Send(psTransport->pLlcp,
581955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   &psTransport->sLlcpHeader,
582955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   NULL,
583955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   &sFrmrBuffer,
584955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   phFriNfc_LlcpTransport_Send_CB,
585955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   psTransport);
5867c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen      }
587955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
588955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   else if(psTransport->bDmPending)
589955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
590955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Reset DM pending */
591955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->bDmPending = FALSE;
592955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
593955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Send DM pending */
594955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      result = phFriNfc_LlcpTransport_SendDisconnectMode(psTransport,
595955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                         psTransport->DmInfoBuffer[0],
596955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                         psTransport->DmInfoBuffer[1],
597955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                         psTransport->DmInfoBuffer[2]);
598955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
599955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
600955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* 3 - Call the original callback */
6017c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   if (pfSavedCb != NULL)
602955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
6037c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen      (*pfSavedCb)(pSavedContext, index, status);
604955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
605955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
6067c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen
607955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* 4 - Handle pending send operations */
608955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
6097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Check for pending discovery requests/responses */
6107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (psTransport->nDiscoveryResListSize > 0)
6117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
6127b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      phFriNfc_LlcpTransport_DiscoveryAnswer(psTransport);
6137b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
6147b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if ( (psTransport->pfDiscover_Cb != NULL) &&
6157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi        (psTransport->nDiscoveryReqOffset < psTransport->nDiscoveryListSize) )
6167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
6177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      result = phFriNfc_LlcpTransport_DiscoverServicesEx(psTransport);
6187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
6197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
620955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Init index */
621955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   index = psTransport->socketIndex;
622955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
623955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Check all sockets for pending operation */
624955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   do
625955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
626955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Modulo-increment index */
627955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      index = (index + 1) % PHFRINFC_LLCP_NB_SOCKET_MAX;
628955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
629955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      pCurrentSocket = &psTransport->pSocketTable[index];
630955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
631955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Dispatch to the corresponding transport layer */
632955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      if (pCurrentSocket->eSocket_Type == phFriNfc_LlcpTransport_eConnectionOriented)
633955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
634955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         result = phFriNfc_LlcpTransport_ConnectionOriented_HandlePendingOperations(pCurrentSocket);
635955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
636955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      else if (pCurrentSocket->eSocket_Type == phFriNfc_LlcpTransport_eConnectionLess)
637955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
638955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         result = phFriNfc_LlcpTransport_Connectionless_HandlePendingOperations(pCurrentSocket);
639955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
640955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
641955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      if (result != NFCSTATUS_FAILED)
642955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
643955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         /* Stop looping if pending operation has been found */
644955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         break;
645955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
646955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
647955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   } while(index != psTransport->socketIndex);
648955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi}
649955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
650955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
6515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/* TODO: comment function Transport reset */
6525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Reset (phFriNfc_LlcpTransport_t      *pLlcpTransport,
6535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_Llcp_t               *pLlcp)
6545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
6555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
6565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
6575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
6595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpTransport == NULL || pLlcp == NULL)
6605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
6625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
6635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
6645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
6655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Reset Transport structure */
6665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->pLlcp            = pLlcp;
6675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->LinkStatusError  = FALSE;
6685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bSendPending     = FALSE;
6695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bRecvPending     = FALSE;
6705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bDmPending       = FALSE;
6715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->bFrmrPending     = FALSE;
6725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->socketIndex      = FALSE;
6735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      pLlcpTransport->LinkStatusError  = 0;
6747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pLlcpTransport->pfDiscover_Cb    = NULL;
6755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Initialize cached service name/sap table */
6777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      memset(pLlcpTransport->pCachedServiceNames, 0x00, sizeof(phFriNfc_Llcp_CachedServiceName_t)*PHFRINFC_LLCP_SDP_ADVERTISED_NB);
6785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
6795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Reset all the socket info in the table */
6805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++)
6815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
6825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].eSocket_State                  = phFriNfc_LlcpTransportSocket_eSocketDefault;
6835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].eSocket_Type                   = phFriNfc_LlcpTransport_eDefaultType;
6845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].index                          = i;
6855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pContext                       = NULL;
6865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pListenContext                 = NULL;
6875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pAcceptContext                 = NULL;
6885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pRejectContext                 = NULL;
6895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pConnectContext                = NULL;
690837efc3194a03beaaa32b370c63a9270ed9664d8Martijn Coenen         pLlcpTransport->pSocketTable[i].pDisconnectContext             = NULL;
6915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pSendContext                   = NULL;
6925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pRecvContext                   = NULL;
6935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pSocketErrCb                   = NULL;
6945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bufferLinearLength             = 0;
6955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bufferSendMaxLength            = 0;
6965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bufferRwMaxLength              = 0;
6975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].ReceiverBusyCondition          = FALSE;
6985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].RemoteBusyConditionInfo        = FALSE;
6995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_sSap                    = PHFRINFC_LLCP_SAP_DEFAULT;
7005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_dSap                    = PHFRINFC_LLCP_SAP_DEFAULT;
7015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketRecvPending             = FALSE;
7025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketSendPending             = FALSE;
7035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketListenPending           = FALSE;
7045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketDiscPending             = FALSE;
7055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketConnectPending          = FALSE;
7065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketAcceptPending           = FALSE;
7075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketRRPending               = FALSE;
7085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].bSocketRNRPending              = FALSE;
7095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].psTransport                    = pLlcpTransport;
7105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketSend_Cb                = NULL;
7115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketRecv_Cb                = NULL;
7125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketRecvFrom_Cb            = NULL;
7135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketListen_Cb              = NULL;
7145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketConnect_Cb             = NULL;
7155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].pfSocketDisconnect_Cb          = NULL;
7165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VS                      = 0;
7175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VSA                     = 0;
7185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VR                      = 0;
7195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].socket_VRA                     = 0;
7205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].remoteRW                       = 0;
7215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].localRW                        = 0;
7225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].remoteMIU                      = 0;
7235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].localMIUX                      = 0;
7245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].index                          = 0;
7255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].indexRwRead                    = 0;
7265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         pLlcpTransport->pSocketTable[i].indexRwWrite                   = 0;
727cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau
728c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau         memset(&pLlcpTransport->pSocketTable[i].sSocketOption, 0x00, sizeof(phFriNfc_LlcpTransport_sSocketOptions_t));
729c2866714540bec65af19240e95a10d3090df0cf9Sylvain Fonteneau
730cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         if (pLlcpTransport->pSocketTable[i].sServiceName.buffer != NULL) {
731cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau            phOsalNfc_FreeMemory(pLlcpTransport->pSocketTable[i].sServiceName.buffer);
732cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         }
733cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         pLlcpTransport->pSocketTable[i].sServiceName.buffer = NULL;
734cf5cd1520ea32d2b65712a36a105f396df07345cSylvain Fonteneau         pLlcpTransport->pSocketTable[i].sServiceName.length = 0;
7355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
7365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
7375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Start The Receive Loop */
7385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status  = phFriNfc_Llcp_Recv(pLlcpTransport->pLlcp,
7395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   phFriNfc_LlcpTransport__Recv_CB,
7405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                   pLlcpTransport);
7415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
7425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
7435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
7445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
745c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau/* TODO: comment function Transport CloseAll */
746c56a3c7bc332a6656bedf342236267b636051170Sylvain FonteneauNFCSTATUS phFriNfc_LlcpTransport_CloseAll (phFriNfc_LlcpTransport_t *pLlcpTransport)
747c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau{
7487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS                           status = NFCSTATUS_SUCCESS;
7497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phFriNfc_Llcp_CachedServiceName_t * pCachedServiceName;
7507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                             i;
751c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
752c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   /* Check for NULL pointers */
753c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   if(pLlcpTransport == NULL)
754c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   {
755c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
756c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   }
757c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
758c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   /* Close all sockets */
759c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++)
760c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   {
76176270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      if(pLlcpTransport->pSocketTable[i].eSocket_Type == phFriNfc_LlcpTransport_eConnectionOriented)
76276270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      {
76376270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         switch(pLlcpTransport->pSocketTable[i].eSocket_State)
76476270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         {
76576270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketConnected:
76676270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketConnecting:
76776270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketAccepted:
76876270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketDisconnected:
76976270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketDisconnecting:
77076270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         case phFriNfc_LlcpTransportSocket_eSocketRejected:
77176270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir            phFriNfc_LlcpTransport_Close(&pLlcpTransport->pSocketTable[i]);
77276270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir            break;
773955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         default:
774955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi            /* Do nothing */
775955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi            break;
77676270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         }
77776270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      }
77876270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      else
77976270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      {
78076270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir         phFriNfc_LlcpTransport_Close(&pLlcpTransport->pSocketTable[i]);
78176270e7c32ec0c558907430e756712d5b68b417cArnaud Ferir      }
782c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   }
7837b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
7847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Reset cached service name/sap table */
7857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   for(i=0;i<PHFRINFC_LLCP_SDP_ADVERTISED_NB;i++)
7867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
7877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pCachedServiceName = &pLlcpTransport->pCachedServiceNames[i];
7887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
7897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pCachedServiceName->nSap = 0;
7907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if (pCachedServiceName->sServiceName.buffer != NULL)
7917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
7927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         phOsalNfc_FreeMemory(pCachedServiceName->sServiceName.buffer);
7937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         pCachedServiceName->sServiceName.buffer = NULL;
7947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
7957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pCachedServiceName->sServiceName.length = 0;
7967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
7977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
798c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau   return status;
799c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau}
800c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
801c56a3c7bc332a6656bedf342236267b636051170Sylvain Fonteneau
802955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi/* TODO: comment function Transport LinkSend */
803955a8efe369ece977e4757f0ae37b3931169125bSunil JogiNFCSTATUS phFriNfc_LlcpTransport_LinkSend( phFriNfc_LlcpTransport_t         *LlcpTransport,
804955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                           phFriNfc_Llcp_sPacketHeader_t    *psHeader,
805955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                           phFriNfc_Llcp_sPacketSequence_t  *psSequence,
806955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                           phNfc_sData_t                    *psInfo,
8077c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen                                           phFriNfc_Llcp_LinkSend_CB_t      pfSend_CB,
8087c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen                                           uint8_t                          socketIndex,
809955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                           void                             *pContext )
810955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi{
811d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen   NFCSTATUS status;
812955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Check if a send is already ongoing */
813955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   if (LlcpTransport->pfLinkSendCb != NULL)
814955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
815955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      return NFCSTATUS_BUSY;
816955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
817955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Save callback details */
818955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   LlcpTransport->pfLinkSendCb = pfSend_CB;
819955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   LlcpTransport->pLinkSendContext = pContext;
8207c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   LlcpTransport->socketIndex = socketIndex;
821955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
822955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Call the link-level send function */
823d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen   status = phFriNfc_Llcp_Send(LlcpTransport->pLlcp, psHeader, psSequence, psInfo, phFriNfc_LlcpTransport_Send_CB, (void*)LlcpTransport);
824d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen   if (status != NFCSTATUS_PENDING && status != NFCSTATUS_SUCCESS) {
825d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen       // Clear out callbacks
826d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen       LlcpTransport->pfLinkSendCb = NULL;
827d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen       LlcpTransport->pLinkSendContext = NULL;
828d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen   }
829d25018da04378bf0529f82ed03d9e25390216b0bMartijn Coenen   return status;
830955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi}
831955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
832955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
833955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi/* TODO: comment function Transport SendFrameReject */
834955a8efe369ece977e4757f0ae37b3931169125bSunil JogiNFCSTATUS phFriNfc_LlcpTransport_SendFrameReject(phFriNfc_LlcpTransport_t           *psTransport,
835955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            dsap,
836955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            rejectedPTYPE,
837955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            ssap,
838955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 phFriNfc_Llcp_sPacketSequence_t*   sLlcpSequence,
839955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            WFlag,
840955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            IFlag,
841955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            RFlag,
842955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            SFlag,
843955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            vs,
844955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            vsa,
845955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            vr,
846955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                 uint8_t                            vra)
847955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi{
848955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   NFCSTATUS                       status = NFCSTATUS_SUCCESS;
849955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   phNfc_sData_t                   sFrmrBuffer;
850955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   uint8_t                         flagValue;
851955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   uint8_t                         sequence = 0;
852955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   uint8_t     index;
853955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   uint8_t     socketFound = FALSE;
854955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
855955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Search a socket waiting for a FRAME */
856955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   for(index=0;index<PHFRINFC_LLCP_NB_SOCKET_MAX;index++)
857955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
858955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Test if the socket is in connected state and if its SSAP and DSAP are valid */
859955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      if(psTransport->pSocketTable[index].socket_sSap == dsap
860955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         && psTransport->pSocketTable[index].socket_dSap == ssap)
861955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
862955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         /* socket found */
863955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         socketFound = TRUE;
864955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         break;
865955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
866955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
867955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
868955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Test if a socket has been found */
869955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   if(socketFound)
870955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
871955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Set socket state to disconnected */
872955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->pSocketTable[index].eSocket_State =  phFriNfc_LlcpTransportSocket_eSocketDefault;
873955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
874955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Call ErrCB due to a FRMR*/
875955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->pSocketTable[index].pSocketErrCb( psTransport->pSocketTable[index].pContext,PHFRINFC_LLCP_ERR_FRAME_REJECTED);
876955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
877955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Close the socket */
878955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      status = phFriNfc_LlcpTransport_ConnectionOriented_Close(&psTransport->pSocketTable[index]);
879955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
880955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Set FRMR Header */
881ce7e8806d59161969589c46abbef8ed1436cea8fSunil Jogi      psTransport->sLlcpHeader.dsap   = ssap;
882955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->sLlcpHeader.ptype  = PHFRINFC_LLCP_PTYPE_FRMR;
883ce7e8806d59161969589c46abbef8ed1436cea8fSunil Jogi      psTransport->sLlcpHeader.ssap   = dsap;
884955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
885955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Set FRMR Information Field */
886955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      flagValue = (WFlag<<7) | (IFlag<<6) | (RFlag<<5) | (SFlag<<4) | rejectedPTYPE;
887955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      if (sLlcpSequence != NULL)
888955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
889955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         sequence = (uint8_t)((sLlcpSequence->ns<<4)|(sLlcpSequence->nr));
890955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
891955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
892955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->FrmrInfoBuffer[0] = flagValue;
893955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->FrmrInfoBuffer[1] = sequence;
894955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->FrmrInfoBuffer[2] = (vs<<4)|vr ;
895955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->FrmrInfoBuffer[3] = (vsa<<4)|vra ;
896955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
897955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Test if a send is pending */
8987c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen      if(testAndSetSendPending(psTransport))
899955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
900955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         psTransport->bFrmrPending = TRUE;
901955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         status = NFCSTATUS_PENDING;
902955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
903955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      else
904955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      {
905955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         sFrmrBuffer.buffer =  psTransport->FrmrInfoBuffer;
906955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         sFrmrBuffer.length =  0x04; /* Size of FRMR Information field */
907955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
908955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         /* Send FRMR frame */
909955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi         status =  phFriNfc_Llcp_Send(psTransport->pLlcp,
910955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                      &psTransport->sLlcpHeader,
911955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                      NULL,
912955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                      &sFrmrBuffer,
913955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                      phFriNfc_LlcpTransport_Send_CB,
914955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                      psTransport);
915955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      }
916955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
917955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   else
918955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
919955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* No active  socket*/
920955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* FRMR Frame not handled*/
921955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
922955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   return status;
923955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi}
924955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
925955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
926955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi/* TODO: comment function Transport SendDisconnectMode (NOTE: used only
927955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi * for requests not bound to a socket, like "service not found")
928955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi */
929955a8efe369ece977e4757f0ae37b3931169125bSunil JogiNFCSTATUS phFriNfc_LlcpTransport_SendDisconnectMode(phFriNfc_LlcpTransport_t* psTransport,
930955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                    uint8_t                   dsap,
931955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                    uint8_t                   ssap,
932955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                                    uint8_t                   dmOpCode)
933955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi{
934955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   NFCSTATUS                       status = NFCSTATUS_SUCCESS;
935955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
936955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Test if a send is pending */
9377c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   if(testAndSetSendPending(psTransport))
938955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
939955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* DM pending */
940955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->bDmPending        = TRUE;
941955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
942955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Store DM Info */
943955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->DmInfoBuffer[0] = dsap;
944955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->DmInfoBuffer[1] = ssap;
945955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->DmInfoBuffer[2] = dmOpCode;
946955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
947955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi     status = NFCSTATUS_PENDING;
948955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
949955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   else
950955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
951955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Set the header */
952955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->sDmHeader.dsap  = dsap;
953955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->sDmHeader.ptype = PHFRINFC_LLCP_PTYPE_DM;
954955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->sDmHeader.ssap  = ssap;
955955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
956955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Save Operation Code to be provided in DM frame payload */
957955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->DmInfoBuffer[2] = dmOpCode;
958955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->sDmPayload.buffer    = &psTransport->DmInfoBuffer[2];
959955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      psTransport->sDmPayload.length    = PHFRINFC_LLCP_DM_LENGTH;
960955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
961955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      /* Send DM frame */
962955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      status =  phFriNfc_Llcp_Send(psTransport->pLlcp,
963955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   &psTransport->sDmHeader,
964955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   NULL,
965955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   &psTransport->sDmPayload,
966955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   phFriNfc_LlcpTransport_Send_CB,
967955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi                                   psTransport);
968955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
969955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
970955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   return status;
971955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi}
972955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
973955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi
9745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
9755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
9765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Get the local options of a socket</b>.
9775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
9785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function returns the local options (maximum packet size and receive window size) used
9795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* for a given connection-oriented socket. This function shall not be used with connectionless
9805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* sockets.
9815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
9825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
9835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psLocalOptions        A pointer to be filled with the local options of the socket.
9845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
9855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
9865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
9875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
9885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
9895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
9905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
9915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
9925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
9935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
9945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_SocketGetLocalOptions(phFriNfc_LlcpTransport_Socket_t  *pLlcpSocket,
9955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                       phLibNfc_Llcp_sSocketOptions_t   *psLocalOptions)
9965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
9975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
9985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
9995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
10005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if (pLlcpSocket == NULL || psLocalOptions == NULL)
10015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket type */
10055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
10065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket state */
10105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketDefault)
10115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
10135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
10155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly     status = phFriNfc_LlcpTransport_ConnectionOriented_SocketGetLocalOptions(pLlcpSocket,
10175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                              psLocalOptions);
10185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
10215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
10255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
10265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Get the local options of a socket</b>.
10275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function returns the remote options (maximum packet size and receive window size) used
10295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* for a given connection-oriented socket. This function shall not be used with connectionless
10305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* sockets.
10315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
10335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psRemoteOptions       A pointer to be filled with the remote options of the socket.
10345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
10355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
10365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
10375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
10385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
10395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
10405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
10415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
10425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
10435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
10445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_SocketGetRemoteOptions(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket,
10455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                        phLibNfc_Llcp_sSocketOptions_t*    psRemoteOptions)
10465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
10475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
10485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
10505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if (pLlcpSocket == NULL || psRemoteOptions == NULL)
10515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket type */
10555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
10565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
10585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket state */
10605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketConnected)
10615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
10635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
10655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
10665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_SocketGetRemoteOptions(pLlcpSocket,
10675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                                psRemoteOptions);
10685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
10695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
10715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
10725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
10737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
10747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_DiscoverServicesEx(phFriNfc_LlcpTransport_t *psTransport)
10757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
10767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS         result = NFCSTATUS_PENDING;
10777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phNfc_sData_t     sInfoBuffer;
10787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phNfc_sData_t     *psServiceName;
10797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint32_t          nTlvOffset;
10807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
10817b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Test if a send is pending */
10827c4b4fadb66959c50c170182847886e83393eebfMartijn Coenen   if(!testAndSetSendPending(psTransport))
10837b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
10847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Set the header */
10857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->sLlcpHeader.dsap  = PHFRINFC_LLCP_SAP_SDP;
10867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->sLlcpHeader.ptype = PHFRINFC_LLCP_PTYPE_SNL;
10877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      psTransport->sLlcpHeader.ssap  = PHFRINFC_LLCP_SAP_SDP;
10887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
10897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Prepare the info buffer */
10907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      sInfoBuffer.buffer = psTransport->pDiscoveryBuffer;
10917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      sInfoBuffer.length = sizeof(psTransport->pDiscoveryBuffer);
10927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
10937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Encode as many requests as possible */
10947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      nTlvOffset = 0;
10957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      while(psTransport->nDiscoveryReqOffset < psTransport->nDiscoveryListSize)
10967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
10977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Get current service name and try to encode it in SNL frame */
10987b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         psServiceName = &psTransport->psDiscoveryServiceNameList[psTransport->nDiscoveryReqOffset];
10997b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         result = phFriNfc_LlcpTransport_EncodeSdreqTlv(&sInfoBuffer,
11007b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                        &nTlvOffset,
11017b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                        psTransport->nDiscoveryReqOffset,
11027b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                        psServiceName);
11037b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if (result != NFCSTATUS_SUCCESS)
11047b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
11057b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Impossible to fit more requests in a single frame,
11067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi             * will be continued on next opportunity
11077b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi             */
11087b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            break;
11097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
11107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Update request counter */
11127b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         psTransport->nDiscoveryReqOffset++;
11137b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
11147b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Update buffer length to match real TLV size */
11167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      sInfoBuffer.length = nTlvOffset;
11177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Send SNL frame */
11197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      result =  phFriNfc_Llcp_Send(psTransport->pLlcp,
11207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   &psTransport->sLlcpHeader,
11217b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   NULL,
11227b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   &sInfoBuffer,
11237b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   phFriNfc_LlcpTransport_Send_CB,
11247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                   psTransport);
11257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
11267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   else
11277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
11287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Impossible to send now, this function will be called again on next opportunity */
11297b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
11307b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return result;
11327b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
11337b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11347b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi/*!
11357b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \ingroup grp_fri_nfc
11367b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \brief <b>Discover remote services SAP using SDP protocol</b>.
11377b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi */
11387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil JogiNFCSTATUS phFriNfc_LlcpTransport_DiscoverServices( phFriNfc_LlcpTransport_t  *pLlcpTransport,
11397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                   phNfc_sData_t             *psServiceNameList,
11407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                   uint8_t                   *pnSapList,
11417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                   uint8_t                   nListSize,
11427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                   pphFriNfc_Cr_t            pDiscover_Cb,
11437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                   void                      *pContext )
11447b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
11457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   NFCSTATUS         result = NFCSTATUS_FAILED;
11467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Save request details */
11487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->psDiscoveryServiceNameList = psServiceNameList;
11497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->pnDiscoverySapList = pnSapList;
11507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->nDiscoveryListSize = nListSize;
11517b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->pfDiscover_Cb = pDiscover_Cb;
11527b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->pDiscoverContext = pContext;
11537b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Reset internal counters */
11557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->nDiscoveryReqOffset = 0;
11567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   pLlcpTransport->nDiscoveryResOffset = 0;
11577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Perform request */
11597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   result = phFriNfc_LlcpTransport_DiscoverServicesEx(pLlcpTransport);
11607b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return result;
11627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
11637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
11655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
11665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
11675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Create a socket on a LLCP-connected device</b>.
11685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function creates a socket for a given LLCP link. Sockets can be of two types :
11705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* connection-oriented and connectionless. If the socket is connection-oriented, the caller
11715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* must provide a working buffer to the socket in order to handle incoming data. This buffer
11725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* must be large enough to fit the receive window (RW * MIU), the remaining space being
11735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* used as a linear buffer to store incoming data as a stream. Data will be readable later
11745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* using the phLibNfc_LlcpTransport_Recv function.
11755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* The options and working buffer are not required if the socket is used as a listening socket,
11765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* since it cannot be directly used for communication.
11775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocketTable      A pointer to a table of PHFRINFC_LLCP_NB_SOCKET_DEFAULT sockets.
11795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  eType                 The socket type.
11805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psOptions             The options to be used with the socket.
11815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psWorkingBuffer       A working buffer to be used by the library.
11825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[out] pLlcpSocket           A pointer on the socket to be filled with a
11835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                    socket found on the socket table.
11845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pErr_Cb               The callback to be called each time the socket
11855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                   is in error.
11865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext              Upper layer context to be returned in the callback.
11875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
11885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
11895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
11905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
11915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_BUFFER_TOO_SMALL         The working buffer is too small for the MIU and RW
11925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            declared in the options.
11935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INSUFFICIENT_RESOURCES   No more socket handle available.
11945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
11955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* */
11965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Socket(phFriNfc_LlcpTransport_t                  *pLlcpTransport,
11975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_eSocketType_t      eType,
11985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_sSocketOptions_t   *psOptions,
11995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phNfc_sData_t                             *psWorkingBuffer,
12005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_Socket_t           **pLlcpSocket,
12015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketErrCb_t      pErr_Cb,
12025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void                                      *pContext)
12035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
12045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
12055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_Llcp_sLinkParameters_t  LlcpLinkParamInfo;
12065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t index=0;
12075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t cpt;
12085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
1210177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi   if (   ((psOptions == NULL) && (eType == phFriNfc_LlcpTransport_eConnectionOriented))
1211177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi       || ((psWorkingBuffer == NULL) && (eType == phFriNfc_LlcpTransport_eConnectionOriented))
1212177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi       || (pLlcpSocket == NULL)
1213177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi       || (pErr_Cb == NULL)
1214177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi       || (pContext == NULL)
1215177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi       || (pLlcpTransport == NULL))
12165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
12185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      return status;
12195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
12205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /*  Test the socket type*/
12215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(eType != phFriNfc_LlcpTransport_eConnectionOriented && eType != phFriNfc_LlcpTransport_eConnectionLess)
12225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
12245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      return status;
12255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
1226177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi   /* Connectionless sockets don't support options */
1227177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi   else if ((psOptions != NULL) && (eType == phFriNfc_LlcpTransport_eConnectionLess))
1228177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi   {
1229177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
1230177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi      return status;
1231177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi   }
12325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Get the local parameters of the LLCP Link */
12345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   status = phFriNfc_Llcp_GetLocalInfo(pLlcpTransport->pLlcp,&LlcpLinkParamInfo);
12355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(status != NFCSTATUS_SUCCESS)
12365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_FAILED);
1238177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi      return status;
12395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
12405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
12415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
12425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      /* Search a socket free in the Socket Table*/
12435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      do
12445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
12455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         if(pLlcpTransport->pSocketTable[index].eSocket_State == phFriNfc_LlcpTransportSocket_eSocketDefault)
12465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
12475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the socket pointer to socket of the table */
12485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            *pLlcpSocket = &pLlcpTransport->pSocketTable[index];
12495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Store the socket info in the socket pointer */
12515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].eSocket_Type     = eType;
12525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].pSocketErrCb     = pErr_Cb;
12535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Store the context of the upper layer */
12555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].pContext   = pContext;
12565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
12575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the pointers to the different working buffers */
1258177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi            if (eType == phFriNfc_LlcpTransport_eConnectionOriented)
12595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            {
1260177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Test the socket options */
1261177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                if (psOptions->rw > PHFRINFC_LLCP_RW_MAX)
1262177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                {
1263177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
1264177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    return status;
1265177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                }
1266177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1267177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Set socket options */
1268177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                memcpy(&pLlcpTransport->pSocketTable[index].sSocketOption, psOptions, sizeof(phFriNfc_LlcpTransport_sSocketOptions_t));
1269177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1270177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Set socket local params (MIUX & RW) */
1271177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].localMIUX = (pLlcpTransport->pSocketTable[index].sSocketOption.miu - PHFRINFC_LLCP_MIU_DEFAULT) & PHFRINFC_LLCP_TLV_MIUX_MASK;
1272177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].localRW   = pLlcpTransport->pSocketTable[index].sSocketOption.rw & PHFRINFC_LLCP_TLV_RW_MASK;
1273177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1274177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Set the Max length for the Send and Receive Window Buffer */
1275177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].bufferSendMaxLength   = pLlcpTransport->pSocketTable[index].sSocketOption.miu;
1276177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].bufferRwMaxLength     = pLlcpTransport->pSocketTable[index].sSocketOption.miu * ((pLlcpTransport->pSocketTable[index].sSocketOption.rw & PHFRINFC_LLCP_TLV_RW_MASK));
1277177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].bufferLinearLength    = psWorkingBuffer->length - pLlcpTransport->pSocketTable[index].bufferSendMaxLength - pLlcpTransport->pSocketTable[index].bufferRwMaxLength;
1278177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1279177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Test the connection oriented buffers length */
1280177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                if((pLlcpTransport->pSocketTable[index].bufferSendMaxLength + pLlcpTransport->pSocketTable[index].bufferRwMaxLength) > psWorkingBuffer->length
1281177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    || ((pLlcpTransport->pSocketTable[index].bufferLinearLength < PHFRINFC_LLCP_MIU_DEFAULT) && (pLlcpTransport->pSocketTable[index].bufferLinearLength != 0)))
1282177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                {
1283177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_BUFFER_TOO_SMALL);
1284177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    return status;
1285177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                }
1286177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1287177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Set the pointer and the length for the Receive Window Buffer */
1288177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                for(cpt=0;cpt<pLlcpTransport->pSocketTable[index].localRW;cpt++)
1289177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                {
1290177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    pLlcpTransport->pSocketTable[index].sSocketRwBufferTable[cpt].buffer = psWorkingBuffer->buffer + (cpt*pLlcpTransport->pSocketTable[index].sSocketOption.miu);
1291177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    pLlcpTransport->pSocketTable[index].sSocketRwBufferTable[cpt].length = 0;
1292177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                }
1293177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1294177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /* Set the pointer and the length for the Send Buffer */
1295177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].sSocketSendBuffer.buffer     = psWorkingBuffer->buffer + pLlcpTransport->pSocketTable[index].bufferRwMaxLength;
1296177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].sSocketSendBuffer.length     = pLlcpTransport->pSocketTable[index].bufferSendMaxLength;
1297177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1298177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                /** Set the pointer and the length for the Linear Buffer */
1299177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.buffer   = psWorkingBuffer->buffer + pLlcpTransport->pSocketTable[index].bufferRwMaxLength + pLlcpTransport->pSocketTable[index].bufferSendMaxLength;
1300177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.length   = pLlcpTransport->pSocketTable[index].bufferLinearLength;
1301177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1302177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                if(pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.length != 0)
1303177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                {
1304177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    /* Init Cyclic Fifo */
1305177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                    phFriNfc_Llcp_CyclicFifoInit(&pLlcpTransport->pSocketTable[index].sCyclicFifoBuffer,
1306177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                                                pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.buffer,
1307177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                                                pLlcpTransport->pSocketTable[index].sSocketLinearBuffer.length);
1308177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                }
1309177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi            }
1310177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi            /* Handle connectionless socket with buffering option */
1311177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi            else if (eType == phFriNfc_LlcpTransport_eConnectionLess)
1312177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi            {
1313177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               /* Determine how many packets can be bufferized in working buffer */
1314177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               if (psWorkingBuffer != NULL)
13155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
1316177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                  /* NOTE: the extra byte is used to store SSAP */
1317177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                  pLlcpTransport->pSocketTable[index].localRW = psWorkingBuffer->length / (pLlcpTransport->pLlcp->sLocalParams.miu + 1);
13185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
1319177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               else
13205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
1321177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                  pLlcpTransport->pSocketTable[index].localRW = 0;
13225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
13235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1324177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               if (pLlcpTransport->pSocketTable[index].localRW > PHFRINFC_LLCP_RW_MAX)
13255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
1326177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                  pLlcpTransport->pSocketTable[index].localRW = PHFRINFC_LLCP_RW_MAX;
13275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
13285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1329177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               /* Set the pointers and the lengths for buffering */
1330177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               for(cpt=0 ; cpt<pLlcpTransport->pSocketTable[index].localRW ; cpt++)
13315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               {
1332177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                  pLlcpTransport->pSocketTable[index].sSocketRwBufferTable[cpt].buffer = psWorkingBuffer->buffer + (cpt*(pLlcpTransport->pLlcp->sLocalParams.miu + 1));
1333177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi                  pLlcpTransport->pSocketTable[index].sSocketRwBufferTable[cpt].length = 0;
13345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly               }
1335177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
1336177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               /* Set other socket internals */
1337177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               pLlcpTransport->pSocketTable[index].indexRwRead      = 0;
1338177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi               pLlcpTransport->pSocketTable[index].indexRwWrite     = 0;
13395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            }
1340177497f518ad2bbb8fe6cab5cf55415244b86565Sunil Jogi
13415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Store index of the socket */
13425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].index = index;
13435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            /* Set the socket into created state */
13455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            pLlcpTransport->pSocketTable[index].eSocket_State = phFriNfc_LlcpTransportSocket_eSocketCreated;
13465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            return status;
13475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
13485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         else
13495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
13505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly            index++;
13515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
13525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }while(index<PHFRINFC_LLCP_NB_SOCKET_MAX);
13535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INSUFFICIENT_RESOURCES);
13555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
13575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
13585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
13605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
13615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Close a socket on a LLCP-connected device</b>.
13625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
13635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function closes a LLCP socket previously created using phFriNfc_LlcpTransport_Socket.
13645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* If the socket was connected, it is first disconnected, and then closed.
13655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
13665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket                    A pointer to a phFriNfc_LlcpTransport_Socket_t.
13675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
13695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
13705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
13715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
13725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
13735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Close(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket)
13745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
13755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
13765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
13785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if( pLlcpSocket == NULL)
13795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
13815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type == phFriNfc_LlcpTransport_eConnectionOriented)
13835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Close(pLlcpSocket);
13855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type ==  phFriNfc_LlcpTransport_eConnectionLess)
13875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_Connectionless_Close(pLlcpSocket);
13895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
13915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
13925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
13935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
13945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
13965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
13975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
13985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
13995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
14005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Bind a socket to a local SAP</b>.
14015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
14025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function binds the socket to a local Service Access Point.
14035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
14047b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
14057b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \param[in]  nSap                  The SAP number to bind with, or 0 for auto-bind to a free SAP.
14067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \param[in]  psServiceName         A pointer to Service Name, or NULL if no service name.
14075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
14085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
14095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
14105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
14115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
14125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
14135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_ALREADY_REGISTERED       The selected SAP is already bound to another
14145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                             socket.
14155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
14165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
14175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
14185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Bind(phFriNfc_LlcpTransport_Socket_t    *pLlcpSocket,
14197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                      uint8_t                            nSap,
14207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                      phNfc_sData_t                      *psServiceName)
14215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
14225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
14235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
14247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t min_sap_range;
14257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t max_sap_range;
14265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
14275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
14285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL)
14295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
14305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
14315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
14325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketCreated)
14335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
14345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
14355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
14365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
14375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
14387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Calculate authorized SAP range */
14397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if ((psServiceName != NULL) && (psServiceName->length > 0))
14407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
14417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* SDP advertised service */
14427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         min_sap_range = PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST;
14437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         max_sap_range = PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST;
14447b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
14457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      else
14467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
14477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Non-SDP advertised service */
14487b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         min_sap_range = PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST;
14497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         max_sap_range = PHFRINFC_LLCP_SAP_NUMBER;
14507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
14517b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
14527b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Handle dynamic SAP allocation */
14537b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if (nSap == 0)
14547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
14557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         status = phFriNfc_LlcpTransport_GetFreeSap(pLlcpSocket->psTransport, psServiceName, &nSap);
14567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if (status != NFCSTATUS_SUCCESS)
14577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
14587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            return status;
14597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
14607b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
14617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
14627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Test the SAP range */
14637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if(!IS_BETWEEN(nSap, min_sap_range, max_sap_range) &&
14647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         !IS_BETWEEN(nSap, PHFRINFC_LLCP_SAP_WKS_FIRST, PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST))
14655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
14667b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
14677b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
14687b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      else
14697b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
14707b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Test if the nSap it is used by another socket */
14717b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++)
14727b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
14737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            if(pLlcpSocket->psTransport->pSocketTable[i].socket_sSap == nSap)
14747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            {
14757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi               return status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_ALREADY_REGISTERED);
14767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            }
14777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
14787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Set service name */
14797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         status = phFriNfc_LlcpTransport_RegisterName(pLlcpSocket, nSap, psServiceName);
14807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if (status != NFCSTATUS_SUCCESS)
14815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
14827b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            return status;
14835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
14847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Set the nSap value of the socket */
14857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         pLlcpSocket->socket_sSap = nSap;
14867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Set the socket state */
14877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         pLlcpSocket->eSocket_State = phFriNfc_LlcpTransportSocket_eSocketBound;
14885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
14895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
14905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
14915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
14925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
14935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*********************************************/
14945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*           ConnectionOriented              */
14955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*********************************************/
14965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
14975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
14985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
14995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Listen for incoming connection requests on a socket</b>.
15005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
15015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function switches a socket into a listening state and registers a callback on
15025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* incoming connection requests. In this state, the socket is not able to communicate
15035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* directly. The listening state is only available for connection-oriented sockets
15045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* which are still not connected. The socket keeps listening until it is closed, and
15055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* thus can trigger several times the pListen_Cb callback.
15065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
15075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
15085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
15095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pListen_Cb         The callback to be called each time the
15105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                socket receive a connection request.
15115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
15125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
15135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
15145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
15155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
15165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
15175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state to switch
15185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            to listening state.
15195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
15205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
15215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Listen(phFriNfc_LlcpTransport_Socket_t*          pLlcpSocket,
15225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketListenCb_t   pListen_Cb,
15235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void*                                     pContext)
15245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
15255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
15265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
15275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
15287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if(pLlcpSocket == NULL || pListen_Cb == NULL|| pContext == NULL )
15295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
15305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
15315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
15325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket state */
15335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
15345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
15355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
15365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
15375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket type */
15385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
15395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
15405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
15415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
15425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if a listen is not pending with this socket */
15435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->bSocketListenPending)
15445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
15455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
15465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
15477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   else
15485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
15497b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      status = phFriNfc_LlcpTransport_ConnectionOriented_Listen(pLlcpSocket,
15507b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                                pListen_Cb,
15517b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                                pContext);
15525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
15537b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return status;
15547b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi}
15557b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
15567b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
15577b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi/**
15587b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \ingroup grp_fri_nfc
15597b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \brief <b>Register the socket service name</b>.
15607b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi*
15617b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* This function changes the service name of the corresponding socket.
15627b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi*
15637b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
15647b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \param[in]  nSap               SAP number associated to the service name.
15657b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \param[in]  psServiceName      A pointer to a Service Name.
15667b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi*
15677b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \retval NFCSTATUS_SUCCESS                  Operation successful.
15687b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
15697b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi*                                            could not be properly interpreted.
15707b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi* \retval NFCSTATUS_FAILED                   Operation failed.
15717b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi*/
15727b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogistatic NFCSTATUS phFriNfc_LlcpTransport_RegisterName(phFriNfc_LlcpTransport_Socket_t*   pLlcpSocket,
15737b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                     uint8_t                            nSap,
15747b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi                                                     phNfc_sData_t                      *psServiceName)
15757b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi{
15767b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   phFriNfc_LlcpTransport_t *       psTransport = pLlcpSocket->psTransport;
15777b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                          index;
15787b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t                          bSnMatch, bSapMatch;
15797b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
15807b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Check in cache if sap has been used for different service name */
15817b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   for(index=0 ; index<PHFRINFC_LLCP_SDP_ADVERTISED_NB ; index++)
15823a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   {
15837b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if(psTransport->pCachedServiceNames[index].sServiceName.length == 0)
15847b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
15857b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Reached end of table */
15867b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         break;
15877b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
15887b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
15897b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      bSnMatch = (memcmp(psTransport->pCachedServiceNames[index].sServiceName.buffer, psServiceName->buffer, psServiceName->length) == 0);
15907b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      bSapMatch = psTransport->pCachedServiceNames[index].nSap == nSap;
15917b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if(bSnMatch && bSapMatch)
15927b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
15937b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Request match cache */
15947b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         break;
15957b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
15967b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      else if((bSnMatch && !bSapMatch) || (!bSnMatch && bSapMatch))
15977b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
15987b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Request mismatch with cache */
15997b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         return NFCSTATUS_INVALID_PARAMETER;
16007b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
16013a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   }
16027b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
16037b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Handle service with no name */
16047b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   if (psServiceName == NULL)
16053a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   {
16067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if (pLlcpSocket->sServiceName.buffer != NULL)
16077b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
16087b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         phOsalNfc_FreeMemory(pLlcpSocket->sServiceName.buffer);
16097b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
16107b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pLlcpSocket->sServiceName.buffer = NULL;
16117b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pLlcpSocket->sServiceName.length = 0;
16123a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau   }
16135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
16145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
16157b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Check if name already in use */
16167b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      for(index=0;index<PHFRINFC_LLCP_NB_SOCKET_MAX;index++)
16177b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
16187b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         phFriNfc_LlcpTransport_Socket_t* pCurrentSocket = &pLlcpSocket->psTransport->pSocketTable[index];
16197b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
16207b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if(   (pCurrentSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
16217b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            && (pCurrentSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketRegistered))
16227b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
16237b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Only bound or listening sockets may have a service name */
16247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            continue;
16257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
16267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if(pCurrentSocket->sServiceName.length != psServiceName->length) {
16277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Service name do not match, check next */
16287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            continue;
16297b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
16307b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         if(memcmp(pCurrentSocket->sServiceName.buffer, psServiceName->buffer, psServiceName->length) == 0)
16317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         {
16327b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            /* Service name already in use */
16337b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi            return NFCSTATUS_INVALID_PARAMETER;
16347b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         }
16357b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
16367b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
16377b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      /* Store the listen socket SN */
16387b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pLlcpSocket->sServiceName.length = psServiceName->length;
16397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      pLlcpSocket->sServiceName.buffer = phOsalNfc_GetMemory(psServiceName->length);
16407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      if (pLlcpSocket->sServiceName.buffer == NULL)
16417b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      {
16427b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi          return NFCSTATUS_NOT_ENOUGH_MEMORY;
16437b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      }
16447b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      memcpy(pLlcpSocket->sServiceName.buffer, psServiceName->buffer, psServiceName->length);
16455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
16467b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi
16477b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   return NFCSTATUS_SUCCESS;
16485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
16495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
16505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
16515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
16525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Accept an incoming connection request for a socket</b>.
16535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
16545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This functions allows the client to accept an incoming connection request.
16555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* It must be used with the socket provided within the listen callback. The socket
16565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* is implicitly switched to the connected state when the function is called.
16575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
16585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
16595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psOptions             The options to be used with the socket.
16605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psWorkingBuffer       A working buffer to be used by the library.
16615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pErr_Cb               The callback to be called each time the accepted socket
16625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                   is in error.
16635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext              Upper layer context to be returned in the callback.
16645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
16655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
16665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
16675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
16685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_BUFFER_TOO_SMALL         The working buffer is too small for the MIU and RW
16695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            declared in the options.
16705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
16715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
16725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Accept(phFriNfc_LlcpTransport_Socket_t*             pLlcpSocket,
16735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phFriNfc_LlcpTransport_sSocketOptions_t*     psOptions,
16745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        phNfc_sData_t*                               psWorkingBuffer,
16755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketErrCb_t         pErr_Cb,
16765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        pphFriNfc_LlcpTransportSocketAcceptCb_t      pAccept_RspCb,
16775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                        void*                                        pContext)
16785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
16795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
16805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
16815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
16825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psOptions == NULL || psWorkingBuffer == NULL || pErr_Cb == NULL || pContext == NULL)
16835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
16845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
16855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
16865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket state */
16875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
16885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
16895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
16905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
16915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket type */
16925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
16935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
16945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
16955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
16965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the socket options */
16975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psOptions->rw > PHFRINFC_LLCP_RW_MAX)
16985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
16995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
17005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
17015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
17025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1703a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Set the Max length for the Send and Receive Window Buffer */
1704a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      pLlcpSocket->bufferSendMaxLength   = psOptions->miu;
1705a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      pLlcpSocket->bufferRwMaxLength     = psOptions->miu * ((psOptions->rw & PHFRINFC_LLCP_TLV_RW_MASK));
1706a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      pLlcpSocket->bufferLinearLength    = psWorkingBuffer->length - pLlcpSocket->bufferSendMaxLength - pLlcpSocket->bufferRwMaxLength;
17075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
1708a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Test the buffers length */
1709a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      if((pLlcpSocket->bufferSendMaxLength + pLlcpSocket->bufferRwMaxLength) > psWorkingBuffer->length
1710a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir          || ((pLlcpSocket->bufferLinearLength < PHFRINFC_LLCP_MIU_DEFAULT)  && (pLlcpSocket->bufferLinearLength != 0)))
1711a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
1712a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_BUFFER_TOO_SMALL);
1713a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
1714a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else
1715a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
1716a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         pLlcpSocket->psTransport->socketIndex = pLlcpSocket->index;
1717a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
1718a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status  = phFriNfc_LlcpTransport_ConnectionOriented_Accept(pLlcpSocket,
1719a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    psOptions,
1720a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    psWorkingBuffer,
1721a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    pErr_Cb,
1722a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    pAccept_RspCb,
1723a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                                    pContext);
1724a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
17255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
17265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
17275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
17285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
17305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
17315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Reject an incoming connection request for a socket</b>.
17325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
17335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This functions allows the client to reject an incoming connection request.
17345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* It must be used with the socket provided within the listen callback. The socket
17355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* is implicitly closed when the function is called.
17365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
17375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
17385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pReject_RspCb         The callback to be call when the Reject operation is completed
17395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext              Upper layer context to be returned in the callback.
17405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
17415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
17425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
17435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
17445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
17455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
17465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Reject( phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
17475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          pphFriNfc_LlcpTransportSocketRejectCb_t   pReject_RspCb,
17485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          void                                      *pContext)
17495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
17505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
17515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
17535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL)
17545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
17555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
17565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
17575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket state */
17585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
17595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
17605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
17615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
17625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for socket type */
17635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
17645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
17655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
17665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
17675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
17685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
17695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phLibNfc_LlcpTransport_ConnectionOriented_Reject(pLlcpSocket,
17705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                pReject_RspCb,
17715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                pContext);
17725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
17735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
17755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
17765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
17775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
17785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
17795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Try to establish connection with a socket on a remote SAP</b>.
17805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
17815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function tries to connect to a given SAP on the remote peer. If the
17825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* socket is not bound to a local SAP, it is implicitly bound to a free SAP.
17835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
17845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
17855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  nSap               The destination SAP to connect to.
17865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pConnect_RspCb     The callback to be called when the connection
17875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
17885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
17895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
17905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
17915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
17925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
17935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
17945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Connection operation is in progress,
17955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pConnect_RspCb will be called upon completion.
17965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
17975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
17985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
17995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
18005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Connect( phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
18015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          uint8_t                                    nSap,
18025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          pphFriNfc_LlcpTransportSocketConnectCb_t   pConnect_RspCb,
18035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                          void*                                      pContext)
18045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
18055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
18067b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t nLocalSap;
18075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
18085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
180999e7261d709ad32e0203dced9f4a386c5f719adfArnaud Ferir   /* Check for NULL pointers */
1810a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir   if(pLlcpSocket == NULL || pConnect_RspCb == NULL || pContext == NULL)
18115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
18125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
18135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
18145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the port number value */
18155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(nSap<02 || nSap>63)
18165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
18175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
18185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
18195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
18205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
18215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
18225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
18235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
18247b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   /* Test if the socket has a service name */
18257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   else if(pLlcpSocket->sServiceName.length != 0)
18267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   {
18277b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
18287b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   }
18295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is not in connecting or connected state*/
18305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketCreated && pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
18315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
18325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
18335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
18345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
18355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1836fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      /* Implicit bind if socket is not already bound */
18375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
18385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
18397b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Bind to a free sap */
18407b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         status = phFriNfc_LlcpTransport_GetFreeSap(pLlcpSocket->psTransport, NULL, &nLocalSap);
1841fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         if (status != NFCSTATUS_SUCCESS)
18425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
1843fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            return status;
18445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
18457b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         pLlcpSocket->socket_sSap = nLocalSap;
18465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
18475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18483a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      /* Test the SAP range for non SDP-advertised services */
18493a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      if(!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST, PHFRINFC_LLCP_SAP_NUMBER))
18503a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
18513a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
18523a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
18533a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      else
18543a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
18553a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = phFriNfc_LlcpTransport_ConnectionOriented_Connect(pLlcpSocket,
18563a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    nSap,
18573a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    NULL,
18583a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pConnect_RspCb,
18593a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pContext);
18603a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
18615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
18625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
18645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
18655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
18665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
18675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
18685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Try to establish connection with a socket on a remote service, given its URI</b>.
18695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
18705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function tries to connect to a SAP designated by an URI. If the
18715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* socket is not bound to a local SAP, it is implicitly bound to a free SAP.
18725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
18735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket           A pointer to a phFriNfc_LlcpTransport_Socket_t.
18745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psUri              The URI corresponding to the destination SAP to connect to.
18755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pConnect_RspCb     The callback to be called when the connection
18765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
18775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
18785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
18795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
18805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
18815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
18825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
18835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Connection operation is in progress,
18845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pConnect_RspCb will be called upon completion.
18855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
18865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
18875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
18885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
18895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
18905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
18915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_ConnectByUri(phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
18925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                              phNfc_sData_t*                             psUri,
18935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                              pphFriNfc_LlcpTransportSocketConnectCb_t   pConnect_RspCb,
18945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                              void*                                      pContext)
18955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
18965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
18975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   uint8_t i;
18987b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi   uint8_t nLocalSap;
18995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
19015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || pConnect_RspCb == NULL || pContext == NULL)
19025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
19035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
19065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
19075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
19085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is not in connect pending or connected state*/
19115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketConnecting || pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketConnected)
19125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
19135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the length of the SN */
19165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psUri->length > PHFRINFC_LLCP_SN_MAX_LENGTH)
19175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1918a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
19215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1922fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau      /* Implicit bind if socket is not already bound */
19235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
19245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
19257b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         /* Bind to a free sap */
19267b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         status = phFriNfc_LlcpTransport_GetFreeSap(pLlcpSocket->psTransport, NULL, &nLocalSap);
1927fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau         if (status != NFCSTATUS_SUCCESS)
19285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         {
1929fbff1ec60cb5adacbee109fb0c0e07e8132b5d39Sylvain Fonteneau            return status;
19305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         }
19317b187e70a5f0ffc021cc06a9f1a2bf2c0f8f8767Sunil Jogi         pLlcpSocket->socket_sSap = nLocalSap;
19323a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
19333a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau
19343a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      /* Test the SAP range for non SDP-advertised services */
19353a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      if(!IS_BETWEEN(pLlcpSocket->socket_sSap, PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST, PHFRINFC_LLCP_SAP_NUMBER))
19363a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
19373a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19383a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      }
19393a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      else
19403a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau      {
19413a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau         status = phFriNfc_LlcpTransport_ConnectionOriented_Connect(pLlcpSocket,
19423a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    PHFRINFC_LLCP_SAP_DEFAULT,
19433a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    psUri,
19443a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pConnect_RspCb,
19453a9d18ff42ce17d62e1968ed93358d036989b670Sylvain Fonteneau                                                                    pContext);
19465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
19475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
1948a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
19495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
19505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
19515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
19535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
19545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Disconnect a currently connected socket</b>.
19555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
19565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function initiates the disconnection of a previously connected socket.
19575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
19585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
19595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pDisconnect_RspCb  The callback to be called when the
19605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
19615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
19625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
19635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
19645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
19655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
19665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
19675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Disconnection operation is in progress,
19685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pDisconnect_RspCb will be called upon completion.
19695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
19705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
19715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
19725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
19735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
19745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
19755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Disconnect(phFriNfc_LlcpTransport_Socket_t*           pLlcpSocket,
19765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            pphLibNfc_LlcpSocketDisconnectCb_t         pDisconnect_RspCb,
19775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                            void*                                      pContext)
19785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
19795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
19805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
19815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
19825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || pDisconnect_RspCb == NULL || pContext == NULL)
19835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
19845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
19875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
19885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
1989a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is connected  state*/
19925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketConnected)
19935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
19945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly       status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
19955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
19965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
19975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
19985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phLibNfc_LlcpTransport_ConnectionOriented_Disconnect(pLlcpSocket,
19995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                    pDisconnect_RspCb,
20005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                    pContext);
20015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
20035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
20045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
20055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
20065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
20075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
20085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Send data on a socket</b>.
20095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to write data on a socket. This function
20115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* can only be called on a connection-oriented socket which is already
20125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* in a connected state.
20135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
20165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer containing the data to send.
20175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pSend_RspCb        The callback to be called when the
20185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
20195d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
20205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
20215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
20235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
20245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
20255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
20265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pSend_RspCb will be called upon completion.
20275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
20285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
20295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
20305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
20315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Send(phFriNfc_LlcpTransport_Socket_t*             pLlcpSocket,
20325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      phNfc_sData_t*                               psBuffer,
20335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      pphFriNfc_LlcpTransportSocketSendCb_t        pSend_RspCb,
20345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                      void*                                        pContext)
20355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
20365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
20375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
20385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
20395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pSend_RspCb == NULL || pContext == NULL)
20405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
20415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
20425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
20445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
20455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
20465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
20475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in connected state */
20495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketConnected)
20505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
20515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
20525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the length of the buffer */
20545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(psBuffer->length > pLlcpSocket->remoteMIU )
20555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
20565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
20575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if a send is pending */
2059407d793f3038222dac95651866b99cd064e214ebArnaud Ferir   else if(pLlcpSocket->pfSocketSend_Cb != NULL)
20605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
20615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
20625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
20645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
20655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Send(pLlcpSocket,
20665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              psBuffer,
20675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pSend_RspCb,
20685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pContext);
20695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
20705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
20715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
20725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
20735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
20745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
20755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
20765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Read data on a socket</b>.
20775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to read data from a socket. It reads at most the
20795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* size of the reception buffer, but can also return less bytes if less bytes
20805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* are available. If no data is available, the function will be pending until
20815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* more data comes, and the response will be sent by the callback. This function
20825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* can only be called on a connection-oriented socket.
20835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a phFriNfc_LlcpTransport_Socket_t.
20865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer receiving the data.
20875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pRecv_RspCb        The callback to be called when the
20885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
20895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
20905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
20915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
20925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
20935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
20945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
20955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
20965d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pRecv_RspCb will be called upon completion.
20975d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
20985d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
20995d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
21005d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
21015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_Recv( phFriNfc_LlcpTransport_Socket_t*             pLlcpSocket,
21025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                       phNfc_sData_t*                               psBuffer,
21035d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                       pphFriNfc_LlcpTransportSocketRecvCb_t        pRecv_RspCb,
21045d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                       void*                                        pContext)
21055d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
21065d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
21075d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
21085d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Check for NULL pointers */
21095d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pRecv_RspCb == NULL || pContext == NULL)
21105d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21115d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
21125d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21135d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionOriented socket */
21145d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionOriented)
21155d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21165d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
21175d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21185d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in connected state */
211904f25b3ab4ed13edb3ffc8e7354bca9f7bc60b44Sylvain Fonteneau   else if(pLlcpSocket->eSocket_State == phFriNfc_LlcpTransportSocket_eSocketDefault)
21205d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21215d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
21225d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21235d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if a receive is pending */
21245d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->bSocketRecvPending == TRUE)
21255d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21265d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
21275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21285d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
21295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = phFriNfc_LlcpTransport_ConnectionOriented_Recv(pLlcpSocket,
21315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              psBuffer,
21325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pRecv_RspCb,
21335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                              pContext);
21345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
21365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
21375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
21385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
21395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*****************************************/
21405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*           ConnectionLess              */
21415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/*****************************************/
21425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
21435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly/**
21445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_fri_nfc
21455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Send data on a socket to a given destination SAP</b>.
21465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
21475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is used to write data on a socket to a given destination SAP.
21485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function can only be called on a connectionless socket.
21495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
21505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
21515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a LlcpSocket created.
21525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  nSap               The destination SAP.
21535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer containing the data to send.
21545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pSend_RspCb        The callback to be called when the
21555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
21565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
21575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
21585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
21595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
21605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
21615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
21625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
21635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pSend_RspCb will be called upon completion.
21645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
21655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
21665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
21675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
21685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_SendTo( phFriNfc_LlcpTransport_Socket_t             *pLlcpSocket,
21695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         uint8_t                                     nSap,
21705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         phNfc_sData_t                               *psBuffer,
21715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         pphFriNfc_LlcpTransportSocketSendCb_t       pSend_RspCb,
21725d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                         void*                                       pContext)
21735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
21745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
21755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   phFriNfc_Llcp_sLinkParameters_t  LlcpRemoteLinkParamInfo;
21765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
21775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pSend_RspCb == NULL || pContext == NULL)
21785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
21805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test the port number value */
21825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(nSap<2 || nSap>63)
21835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
21855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionless socket */
21875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionLess)
21885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly       status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
21905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
21915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in an updated state */
21925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
21935d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
21945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
21955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
2196955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   /* Test if a send is pending */
2197955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   else if(pLlcpSocket->pfSocketSend_Cb != NULL)
2198955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   {
2199955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
2200955a8efe369ece977e4757f0ae37b3931169125bSunil Jogi   }
22015d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
22025d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
2203a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Get the local parameters of the LLCP Link */
2204a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = phFriNfc_Llcp_GetRemoteInfo(pLlcpSocket->psTransport->pLlcp,&LlcpRemoteLinkParamInfo);
2205a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      if(status != NFCSTATUS_SUCCESS)
2206a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
2207a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_FAILED);
2208a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
2209a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Test the length of the socket buffer for ConnectionLess mode*/
2210a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else if(psBuffer->length > LlcpRemoteLinkParamInfo.miu)
2211a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
2212a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
2213a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
2214a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      /* Test if the link is in error state */
2215a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else if(pLlcpSocket->psTransport->LinkStatusError)
2216a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
2217a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
2218a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
2219a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      else
2220a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      {
2221a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir         status = phFriNfc_LlcpTransport_Connectionless_SendTo(pLlcpSocket,
2222a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               nSap,
2223a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               psBuffer,
2224a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               pSend_RspCb,
2225a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir                                                               pContext);
2226a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      }
22275d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
2228a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
22295d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
22305d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
22315d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
22325d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly
22335d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly /**
22345d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \ingroup grp_lib_nfc
22355d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \brief <b>Read data on a socket and get the source SAP</b>.
22365d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
22375d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* This function is the same as phLibNfc_Llcp_Recv, except that the callback includes
22385d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* the source SAP. This functions can only be called on a connectionless socket.
22395d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
22405d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
22415d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pLlcpSocket        A pointer to a LlcpSocket created.
22425d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  psBuffer           The buffer receiving the data.
22435d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pRecv_RspCb        The callback to be called when the
22445d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                operation is completed.
22455d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \param[in]  pContext           Upper layer context to be returned in
22465d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                the callback.
22475d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*
22485d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SUCCESS                  Operation successful.
22495d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_PARAMETER        One or more of the supplied parameters
22505d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            could not be properly interpreted.
22515d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_PENDING                  Reception operation is in progress,
22525d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            pRecv_RspCb will be called upon completion.
22535d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_INVALID_STATE            The socket is not in a valid state, or not of
22545d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*                                            a valid type to perform the requsted operation.
22555d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_NOT_INITIALISED          Indicates stack is not yet initialized.
22565d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_SHUTDOWN                 Shutdown in progress.
22575d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly* \retval NFCSTATUS_FAILED                   Operation failed.
22585d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly*/
22595d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick PellyNFCSTATUS phFriNfc_LlcpTransport_RecvFrom( phFriNfc_LlcpTransport_Socket_t                   *pLlcpSocket,
22605d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           phNfc_sData_t*                                    psBuffer,
22615d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           pphFriNfc_LlcpTransportSocketRecvFromCb_t         pRecv_Cb,
22625d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                           void*                                             pContext)
22635d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly{
22645d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   NFCSTATUS status = NFCSTATUS_SUCCESS;
22655d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   if(pLlcpSocket == NULL || psBuffer == NULL || pRecv_Cb == NULL || pContext == NULL)
22665d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
22675d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
22685d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
22695d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is a connectionless socket */
22705d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_Type != phFriNfc_LlcpTransport_eConnectionLess)
22715d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
2272a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_PARAMETER);
22735d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
22745d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   /* Test if the socket is in an updated state */
22755d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound)
22765d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
22775d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_INVALID_STATE);
22785d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
22795d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   else
22805d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   {
22815d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      if(pLlcpSocket->bSocketRecvPending)
22825d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
22835d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         status = PHNFCSTVAL(CID_FRI_NFC_LLCP_TRANSPORT, NFCSTATUS_REJECTED);
22845d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
22855d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      else
22865d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      {
22875d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly         status = phLibNfc_LlcpTransport_Connectionless_RecvFrom(pLlcpSocket,
22885d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                 psBuffer,
22895d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                 pRecv_Cb,
22905d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly                                                                 pContext);
22915d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly      }
22925d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   }
2293a3af9c908f5c50e2405169015c86a1b667b42490Arnaud Ferir
22945d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly   return status;
22955d9927ba30ba449badb9f6df0fbeb4d6aedc6e2aNick Pelly}
2296