diff options
author | Sylvain Fonteneau <sylvain.fonteneau@trusted-logic.com> | 2011-06-17 07:52:29 +0200 |
---|---|---|
committer | Martijn Coenen <maco@google.com> | 2011-07-25 15:35:56 -0500 |
commit | fbff1ec60cb5adacbee109fb0c0e07e8132b5d39 (patch) | |
tree | a6adaad76b780d484996b1106d0acd7401e5e58b /src | |
parent | 3002430f96eb9c9f37806404e7dcfc944da383af (diff) | |
download | external_libnfc-nxp-fbff1ec60cb5adacbee109fb0c0e07e8132b5d39.zip external_libnfc-nxp-fbff1ec60cb5adacbee109fb0c0e07e8132b5d39.tar.gz external_libnfc-nxp-fbff1ec60cb5adacbee109fb0c0e07e8132b5d39.tar.bz2 |
Factorized and fixed code for automatic LLCP socket binding.
Fixed the SAP automatic allocation algorithm which could previously
allocate the same SAP twice in some situations (e.g.: if first socket
in socket table was bound to SAP 0x21 and second socket to 0x20, then
autobind was allocating SAP 0x21 again).
Change-Id: Ic94bbc7eaca260c69cb0ce22931e9241f459bbf5
Diffstat (limited to 'src')
-rw-r--r-- | src/phFriNfc_LlcpTransport.c | 59 |
1 files changed, 41 insertions, 18 deletions
diff --git a/src/phFriNfc_LlcpTransport.c b/src/phFriNfc_LlcpTransport.c index 9638d90..960a33a 100644 --- a/src/phFriNfc_LlcpTransport.c +++ b/src/phFriNfc_LlcpTransport.c @@ -38,6 +38,39 @@ #define IS_BETWEEN(x, a, b) (((x)>=(a)) && ((x)<(b))) +static NFCSTATUS phFriNfc_LlcpTransport_AutoBind(phFriNfc_LlcpTransport_Socket_t *pSocket) +{ + uint8_t i; + uint8_t sap; + phFriNfc_LlcpTransport_Socket_t* pSocketTable = pSocket->psTransport->pSocketTable; + + /* Try all possible SAPs */ + for(sap=PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST ; sap<PHFRINFC_LLCP_SAP_NUMBER ; sap++) + { + /* Go through socket list to check if current SAP is in use */ + for(i=0 ; i<PHFRINFC_LLCP_NB_SOCKET_MAX ; i++) + { + if((pSocketTable[i].eSocket_State >= phFriNfc_LlcpTransportSocket_eSocketBound) && + (pSocketTable[i].socket_sSap == sap)) + { + /* SAP is already in use */ + break; + } + } + + if (i >= PHFRINFC_LLCP_NB_SOCKET_MAX) + { + /* No socket is using current SAP, proceed with binding */ + pSocket->socket_sSap = sap; + pSocket->eSocket_State = phFriNfc_LlcpTransportSocket_eSocketBound; + return NFCSTATUS_SUCCESS; + } + } + + /* If we reach this point, it means that no SAP is free */ + return NFCSTATUS_INSUFFICIENT_RESOURCES; +} + /* TODO: comment function Transport recv CB */ static void phFriNfc_LlcpTransport__Recv_CB(void *pContext, phNfc_sData_t *psData, @@ -854,19 +887,14 @@ NFCSTATUS phFriNfc_LlcpTransport_Connect( phFriNfc_LlcpTransport_Socket_t* } else { + /* Implicit bind if socket is not already bound */ if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound) { - /* Bind with a sSap Free */ - pLlcpSocket->socket_sSap = 32; - - for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++) + status = phFriNfc_LlcpTransport_AutoBind(pLlcpSocket); + if (status != NFCSTATUS_SUCCESS) { - if(pLlcpSocket->socket_sSap == pLlcpSocket->psTransport->pSocketTable[i].socket_sSap) - { - pLlcpSocket->socket_sSap++; - } + return status; } - pLlcpSocket->eSocket_State = phFriNfc_LlcpTransportSocket_eSocketBound; } /* Test the SAP range for non SDP-advertised services */ @@ -942,19 +970,14 @@ NFCSTATUS phFriNfc_LlcpTransport_ConnectByUri(phFriNfc_LlcpTransport_Socket_t* } else { + /* Implicit bind if socket is not already bound */ if(pLlcpSocket->eSocket_State != phFriNfc_LlcpTransportSocket_eSocketBound) { - /* Bind with a sSap Free */ - pLlcpSocket->socket_sSap = 32; - - for(i=0;i<PHFRINFC_LLCP_NB_SOCKET_MAX;i++) + status = phFriNfc_LlcpTransport_AutoBind(pLlcpSocket); + if (status != NFCSTATUS_SUCCESS) { - if(pLlcpSocket->socket_sSap == pLlcpSocket->psTransport->pSocketTable[i].socket_sSap) - { - pLlcpSocket->socket_sSap++; - } + return status; } - pLlcpSocket->eSocket_State = phFriNfc_LlcpTransportSocket_eSocketBound; } /* Test the SAP range for non SDP-advertised services */ |