Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2010 NXP Semiconductors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /**
     18  * \file  phFriNfc_Llcp.h
     19  * \brief NFC LLCP core
     20  *
     21  * Project: NFC-FRI
     22  *
     23  */
     24 
     25 #ifndef PHFRINFC_LLCP_H
     26 #define PHFRINFC_LLCP_H
     27 
     28 /*include files*/
     29 #include <phNfcTypes.h>
     30 #include <phNfcLlcpTypes.h>
     31 #include <phNfcStatus.h>
     32 #include <phFriNfc.h>
     33 
     34 #include <phFriNfc_LlcpMac.h>
     35 
     36 /**
     37  * \name NFC Forum Logical Link Control Protocol
     38  *
     39  * File: \ref phFriNfc_Llcp.h
     40  *
     41  */
     42 
     43 
     44 /** \defgroup grp_fri_nfc_llcp NFC Forum Logical Link Control Protocol Component
     45  *
     46  *  TODO
     47  *
     48  */
     49 
     50 /*=========== DEBUG MACROS ===========*/
     51 
     52 /* LLCP TRACE Macros */
     53 #if defined(LLCP_TRACE)
     54 #include <phOsalNfc.h>
     55 #include <stdio.h>
     56 extern char phOsalNfc_DbgTraceBuffer[];
     57 #define LLCP_MAX_TRACE_BUFFER   150
     58 #define LLCP_PRINT( str )  phOsalNfc_DbgString(str)
     59 #define LLCP_DEBUG(str, arg) \
     60    {                                                                    \
     61       snprintf(phOsalNfc_DbgTraceBuffer,LLCP_MAX_TRACE_BUFFER,str,arg); \
     62       phOsalNfc_DbgString(phOsalNfc_DbgTraceBuffer);                    \
     63    }
     64 #define LLCP_PRINT_BUFFER(msg,buf,len) \
     65    {                                                                             \
     66       snprintf(phOsalNfc_DbgTraceBuffer,LLCP_MAX_TRACE_BUFFER,"\n\t %s:",msg);   \
     67       phOsalNfc_DbgString(phOsalNfc_DbgTraceBuffer);                             \
     68       phOsalNfc_DbgTrace(buf,len);                                               \
     69       phOsalNfc_DbgString("\r");                                                 \
     70    }
     71 #else
     72 #define LLCP_PRINT( str )
     73 #define LLCP_DEBUG(str, arg)
     74 #define LLCP_PRINT_BUFFER(msg,buf,len)
     75 #endif
     76 
     77 
     78 /*=========== CONSTANTS ===========*/
     79 
     80 /**
     81  *  \name LLCP local protocol version.
     82  *
     83  */
     84  /*@{*/
     85 #define PHFRINFC_LLCP_VERSION_MAJOR   0x01  /**< Major number of local LLCP version.*/
     86 #define PHFRINFC_LLCP_VERSION_MINOR   0x01  /**< Minor number of local LLCP version.*/
     87 #define PHFRINFC_LLCP_VERSION         ((PHFRINFC_LLCP_VERSION_MAJOR << 4) | PHFRINFC_LLCP_VERSION_MINOR) /**< Local LLCP version.*/
     88 /*@}*/
     89 
     90 /**
     91  *  \name LLCP packet types.
     92  *
     93  */
     94  /*@{*/
     95 #define PHFRINFC_LLCP_PTYPE_SYMM       0x00 /**< Symmetry.*/
     96 #define PHFRINFC_LLCP_PTYPE_PAX        0x01 /**< PArameter Exchange.*/
     97 #define PHFRINFC_LLCP_PTYPE_AGF        0x02 /**< AGgregated Frame.*/
     98 #define PHFRINFC_LLCP_PTYPE_UI         0x03 /**< Unnumbered Information.*/
     99 #define PHFRINFC_LLCP_PTYPE_CONNECT    0x04 /**< Connect.*/
    100 #define PHFRINFC_LLCP_PTYPE_DISC       0x05 /**< Disconnect.*/
    101 #define PHFRINFC_LLCP_PTYPE_CC         0x06 /**< Connection Complete.*/
    102 #define PHFRINFC_LLCP_PTYPE_DM         0x07 /**< Disconnected Mode.*/
    103 #define PHFRINFC_LLCP_PTYPE_FRMR       0x08 /**< FRaMe Reject.*/
    104 #define PHFRINFC_LLCP_PTYPE_SNL        0x09 /**< Service Name Lookup.*/
    105 #define PHFRINFC_LLCP_PTYPE_RESERVED1  0x0A /**< Reserved.*/
    106 #define PHFRINFC_LLCP_PTYPE_RESERVED2  0x0B /**< Reserved.*/
    107 #define PHFRINFC_LLCP_PTYPE_I          0x0C /**< Information.*/
    108 #define PHFRINFC_LLCP_PTYPE_RR         0x0D /**< Receive Ready.*/
    109 #define PHFRINFC_LLCP_PTYPE_RNR        0x0E /**< Receive Not Ready.*/
    110 #define PHFRINFC_LLCP_PTYPE_RESERVED3  0x0F /**< Reserved.*/
    111 /*@}*/
    112 
    113 /**
    114  *  \name LLCP well-known SAPs.
    115  *
    116  */
    117  /*@{*/
    118 #define PHFRINFC_LLCP_SAP_LINK                     0x00 /**< Link SAP.*/
    119 #define PHFRINFC_LLCP_SAP_SDP                      0x01 /**< Service Discovery Protocol SAP.*/
    120 #define PHFRINFC_LLCP_SAP_WKS_FIRST                0x02 /**< Other Well-Known Services defined by the NFC Forum.*/
    121 #define PHFRINFC_LLCP_SAP_SDP_ADVERTISED_FIRST     0x10 /**< First SAP number from SDP-avertised SAP range.*/
    122 #define PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST   0x20 /**< First SAP number from SDP-unavertised SAP range.*/
    123 #define PHFRINFC_LLCP_SAP_NUMBER                   0x40 /**< Number of possible SAP values (also first invalid value).*/
    124 #define PHFRINFC_LLCP_SAP_DEFAULT                  0xFF /**< Default number when a socket is created or reset */
    125 #define PHFRINFC_LLCP_SDP_ADVERTISED_NB            0x10 /**< Number of SDP advertised SAP slots */
    126 /*@}*/
    127 
    128 /**
    129  *  \name LLCP well-known SAPs.
    130  *
    131  */
    132  /*@{*/
    133 #define PHFRINFC_LLCP_SERVICENAME_SDP              "urn:nfc:sn:sdp" /**< Service Discovery Protocol name.*/
    134 /*@}*/
    135 
    136 /**
    137  *  \name Length value for DM opCode
    138  *
    139  */
    140  /*@{*/
    141 #define PHFRINFC_LLCP_DM_LENGTH                    0x01 /**< Length value for DM opCode */
    142 /*@}*/
    143 
    144 
    145 /**
    146  * \internal
    147  * \name Masks used with parameters value.
    148  *
    149  */
    150 /*@{*/
    151 #define PHFRINFC_LLCP_TLV_MIUX_MASK                 0x07FF   /**< \internal Mask to apply to MIUX TLV Value.*/
    152 #define PHFRINFC_LLCP_TLV_WKS_MASK                  0x0001   /**< \internal Minimal bits to be set in WKS TLV Value.*/
    153 #define PHFRINFC_LLCP_TLV_RW_MASK                   0x0F     /**< \internal Mask to apply to RW TLV Value.*/
    154 #define PHFRINFC_LLCP_TLV_OPT_MASK                  0x03     /**< \internal Mask to apply to OPT TLV Value.*/
    155 /*@}*/
    156 
    157 /**
    158  * \internal
    159  * \name Type codes for parameters in TLV.
    160  *
    161  */
    162 /*@{*/
    163 #define PHFRINFC_LLCP_TLV_TYPE_VERSION               0x01   /**< \internal VERSION parameter Type code.*/
    164 #define PHFRINFC_LLCP_TLV_TYPE_MIUX                  0x02   /**< \internal MIUX parameter Type code.*/
    165 #define PHFRINFC_LLCP_TLV_TYPE_WKS                   0x03   /**< \internal WKS parameter Type code.*/
    166 #define PHFRINFC_LLCP_TLV_TYPE_LTO                   0x04   /**< \internal LTO parameter Type code.*/
    167 #define PHFRINFC_LLCP_TLV_TYPE_RW                    0x05   /**< \internal RW parameter Type code.*/
    168 #define PHFRINFC_LLCP_TLV_TYPE_SN                    0x06   /**< \internal SN parameter Type code.*/
    169 #define PHFRINFC_LLCP_TLV_TYPE_OPT                   0x07   /**< \internal OPT parameter Type code.*/
    170 #define PHFRINFC_LLCP_TLV_TYPE_SDREQ                 0x08   /**< \internal SDREQ parameter Type code.*/
    171 #define PHFRINFC_LLCP_TLV_TYPE_SDRES                 0x09   /**< \internal SDRES parameter Type code.*/
    172 /*@}*/
    173 
    174 /**
    175  * \internal
    176  * \name Fixed Value length for parameters in TLV.
    177  *
    178  */
    179 /*@{*/
    180 #define PHFRINFC_LLCP_TLV_LENGTH_HEADER              2   /**< \internal Fixed length of Type and Length fields in TLV.*/
    181 #define PHFRINFC_LLCP_TLV_LENGTH_VERSION             1   /**< \internal Fixed length of VERSION parameter Value.*/
    182 #define PHFRINFC_LLCP_TLV_LENGTH_MIUX                2   /**< \internal Fixed length of MIUX parameter Value.*/
    183 #define PHFRINFC_LLCP_TLV_LENGTH_WKS                 2   /**< \internal Fixed length of WKS parameter Value.*/
    184 #define PHFRINFC_LLCP_TLV_LENGTH_LTO                 1   /**< \internal Fixed length of LTO parameter Value.*/
    185 #define PHFRINFC_LLCP_TLV_LENGTH_RW                  1   /**< \internal Fixed length of RW parameter Value.*/
    186 #define PHFRINFC_LLCP_TLV_LENGTH_OPT                 1   /**< \internal Fixed length of OPT parameter Value.*/
    187 /*@}*/
    188 
    189 /**
    190  *  \name LLCP packet field sizes.
    191  *
    192  */
    193  /*@{*/
    194 #define PHFRINFC_LLCP_PACKET_HEADER_SIZE     2 /**< Size of the general packet header (DSAP+PTYPE+SSAP).*/
    195 #define PHFRINFC_LLCP_PACKET_SEQUENCE_SIZE   1 /**< Size of the sequence field, if present.*/
    196 #define PHFRINFC_LLCP_PACKET_MAX_SIZE        (PHFRINFC_LLCP_PACKET_HEADER_SIZE + \
    197                                              PHFRINFC_LLCP_PACKET_SEQUENCE_SIZE + \
    198                                              PHFRINFC_LLCP_MIU_DEFAULT + \
    199                                              PHFRINFC_LLCP_TLV_MIUX_MASK) /**< Maximum size of a packet */
    200 /*@}*/
    201 
    202 /*========== MACROS ===========*/
    203 
    204 #define CHECK_SEND_RW(socket) ( (((socket)->socket_VS - (socket)->socket_VSA)  % 16) < (socket)->remoteRW )
    205 
    206 /*========== ENUMERATES ===========*/
    207 
    208 typedef phFriNfc_LlcpMac_ePeerType_t      phFriNfc_Llcp_eRole_t;
    209 
    210 typedef phFriNfc_LlcpMac_eLinkStatus_t    phFriNfc_Llcp_eLinkStatus_t;
    211 
    212 /*========== CALLBACKS ===========*/
    213 
    214 typedef void (*phFriNfc_Llcp_Check_CB_t) (
    215    void                             *pContext,
    216    NFCSTATUS                        status
    217 );
    218 
    219 typedef void (*phFriNfc_Llcp_LinkStatus_CB_t) (
    220    void                             *pContext,
    221    phFriNfc_Llcp_eLinkStatus_t      eLinkStatus
    222 );
    223 
    224 typedef void (*phFriNfc_Llcp_Send_CB_t) (
    225    void                             *pContext,
    226    NFCSTATUS                        status
    227 );
    228 
    229 typedef void (*phFriNfc_Llcp_Recv_CB_t) (
    230    void                             *pContext,
    231    phNfc_sData_t                    *psData,
    232    NFCSTATUS                        status
    233 );
    234 
    235 /*========== STRUCTURES ===========*/
    236 
    237 typedef struct phFriNfc_Llcp_sPacketHeader
    238 {
    239    /**< The destination service access point*/
    240    unsigned   dsap : 6;
    241 
    242    /**< The packet type*/
    243    unsigned   ptype : 4;
    244 
    245    /**< The source service access point*/
    246    unsigned   ssap : 6;
    247 
    248 } phFriNfc_Llcp_sPacketHeader_t;
    249 
    250 typedef struct phFriNfc_Llcp_sPacketSequence
    251 {
    252    /**< Sequence number for sending*/
    253    unsigned   ns : 4;
    254 
    255    /**< Sequence number for reception*/
    256    unsigned   nr : 4;
    257 
    258 } phFriNfc_Llcp_sPacketSequence_t;
    259 
    260 typedef struct phFriNfc_Llcp_sSendOperation
    261 {
    262    /**< Sequence number for sending*/
    263    phFriNfc_Llcp_sPacketHeader_t    *psHeader;
    264 
    265    /**< Sequence number for sending*/
    266    phFriNfc_Llcp_sPacketSequence_t  *psSequence;
    267 
    268    /**< Sequence number for sending*/
    269    phNfc_sData_t                    *psInfo;
    270 
    271    /**< Sequence number for sending*/
    272    phFriNfc_Llcp_Send_CB_t          pfSend_CB;
    273 
    274    /**< Sequence number for sending*/
    275    void                             *pContext;
    276 
    277 } phFriNfc_Llcp_sSendOperation_t;
    278 
    279 typedef struct phFriNfc_Llcp_sRecvOperation
    280 {
    281    /**< Sequence number for sending*/
    282    uint8_t                    nSap;
    283 
    284    /**< Sequence number for sending*/
    285    phNfc_sData_t              *psBuffer;
    286 
    287    /**< Sequence number for sending*/
    288    phFriNfc_Llcp_Recv_CB_t    pfRecv_CB;
    289 
    290    /**< Sequence number for sending*/
    291    void                       *pContext;
    292 
    293 } phFriNfc_Llcp_sRecvOperation_t;
    294 
    295 typedef struct phFriNfc_Llcp
    296 {
    297    /**< The current state*/
    298    uint8_t                          state;
    299 
    300    /**< MAC mapping instance*/
    301    phFriNfc_LlcpMac_t               MAC;
    302 
    303    /**< Local LLC role*/
    304    phFriNfc_LlcpMac_ePeerType_t         eRole;
    305 
    306    /**< Local link parameters*/
    307    phFriNfc_Llcp_sLinkParameters_t  sLocalParams;
    308 
    309    /**< Remote link parameters*/
    310    phFriNfc_Llcp_sLinkParameters_t  sRemoteParams;
    311 
    312    /**< Negociated protocol version (major number on MSB, minor on LSB)*/
    313    uint8_t                          version;
    314 
    315    /**< Internal reception buffer, its size may vary during time but not exceed nRxBufferSize*/
    316    phNfc_sData_t                   sRxBuffer;
    317 
    318    /**< Actual size of reception buffer*/
    319    uint16_t                        nRxBufferLength;
    320 
    321    /**< Internal emission buffer, its size may vary during time but not exceed nTxBufferSize*/
    322    phNfc_sData_t                   sTxBuffer;
    323 
    324    /**< Actual size of emission buffer*/
    325    uint16_t                        nTxBufferLength;
    326 
    327    /**< Callback function for link status notification*/
    328    phFriNfc_Llcp_LinkStatus_CB_t    pfLink_CB;
    329 
    330    /**< Callback context for link status notification*/
    331    void                             *pLinkContext;
    332 
    333    /**< Callback function for compliance checking*/
    334    phFriNfc_Llcp_Check_CB_t         pfChk_CB;
    335 
    336    /**< Callback context for compliance checking*/
    337    void                             *pChkContext;
    338 
    339    /**< Symmetry timer*/
    340    uint32_t                         hSymmTimer;
    341 
    342    /**< Control frames buffer*/
    343    uint8_t                          pCtrlTxBuffer[10];
    344 
    345    /**< Control frames buffer size*/
    346    uint8_t                          pCtrlTxBufferLength;
    347 
    348    /**< DISC packet send pending flag*/
    349    bool_t                           bDiscPendingFlag;
    350 
    351    /**< FRMR packet send pending flag*/
    352    bool_t                           bFrmrPendingFlag;
    353 
    354    /**< Header of pending FRMR packet*/
    355    phFriNfc_Llcp_sPacketHeader_t    sFrmrHeader;
    356 
    357    /**< Info field of pending FRMR packet*/
    358    uint8_t                          pFrmrInfo[4];
    359 
    360    /**< Send callback*/
    361    phFriNfc_Llcp_Send_CB_t          pfSendCB;
    362 
    363    /**< Send callback*/
    364    void                             *pSendContext;
    365 
    366    /**< Pending send header*/
    367    phFriNfc_Llcp_sPacketHeader_t    *psSendHeader;
    368 
    369    /**< Pending send sequence*/
    370    phFriNfc_Llcp_sPacketSequence_t  *psSendSequence;
    371 
    372    /**< Pending send info*/
    373    phNfc_sData_t                    *psSendInfo;
    374 
    375    /**< Receive callback*/
    376    phFriNfc_Llcp_Recv_CB_t          pfRecvCB;
    377 
    378    /**< Receive callback*/
    379    void                             *pRecvContext;
    380 
    381 } phFriNfc_Llcp_t;
    382 
    383 /*========== UNIONS ===========*/
    384 
    385 
    386 /*========== FUNCTIONS ===========*/
    387 
    388 /*!
    389  * \brief TODO
    390  */
    391 NFCSTATUS phFriNfc_Llcp_EncodeLinkParams( phNfc_sData_t                   *psRawBuffer,
    392                                           phFriNfc_Llcp_sLinkParameters_t *psLinkParams,
    393                                           uint8_t                         nVersion );
    394 
    395 
    396 /*!
    397  * \brief TODO
    398  */
    399 NFCSTATUS phFriNfc_Llcp_Reset( phFriNfc_Llcp_t                 *Llcp,
    400                                void                            *LowerDevice,
    401                                phFriNfc_Llcp_sLinkParameters_t *psLinkParams,
    402                                void                            *pRxBuffer,
    403                                uint16_t                        nRxBufferLength,
    404                                void                            *pTxBuffer,
    405                                uint16_t                        nTxBufferLength,
    406                                phFriNfc_Llcp_LinkStatus_CB_t   pfLink_CB,
    407                                void                            *pContext );
    408 
    409 /*!
    410  * \brief TODO
    411  */
    412 NFCSTATUS phFriNfc_Llcp_ChkLlcp( phFriNfc_Llcp_t               *Llcp,
    413                                  phHal_sRemoteDevInformation_t *psRemoteDevInfo,
    414                                  phFriNfc_Llcp_Check_CB_t      pfCheck_CB,
    415                                  void                          *pContext );
    416 
    417 /*!
    418  * \brief TODO
    419  */
    420 NFCSTATUS phFriNfc_Llcp_Activate( phFriNfc_Llcp_t  *Llcp );
    421 
    422 /*!
    423  * \brief TODO
    424  */
    425 NFCSTATUS phFriNfc_Llcp_Deactivate( phFriNfc_Llcp_t *Llcp );
    426 
    427 /*!
    428  * \brief TODO
    429  */
    430 NFCSTATUS phFriNfc_Llcp_GetLocalInfo( phFriNfc_Llcp_t                   *Llcp,
    431                                       phFriNfc_Llcp_sLinkParameters_t   *pParams );
    432 
    433 /*!
    434  * \brief TODO
    435  */
    436 NFCSTATUS phFriNfc_Llcp_GetRemoteInfo( phFriNfc_Llcp_t                  *Llcp,
    437                                        phFriNfc_Llcp_sLinkParameters_t  *pParams );
    438 
    439 /*!
    440  * \brief TODO
    441  */
    442 NFCSTATUS phFriNfc_Llcp_Send( phFriNfc_Llcp_t                  *Llcp,
    443                               phFriNfc_Llcp_sPacketHeader_t    *psHeader,
    444                               phFriNfc_Llcp_sPacketSequence_t  *psSequence,
    445                               phNfc_sData_t                    *psInfo,
    446                               phFriNfc_Llcp_Send_CB_t          pfSend_CB,
    447                               void                             *pContext );
    448 
    449 /*!
    450  * \brief TODO
    451  */
    452 NFCSTATUS phFriNfc_Llcp_Recv( phFriNfc_Llcp_t            *Llcp,
    453                               phFriNfc_Llcp_Recv_CB_t    pfRecv_CB,
    454                               void                       *pContext );
    455 
    456 
    457 #endif /* PHFRINFC_LLCP_H */
    458