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   0x00  /**< 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 /**< Didconnect.*/
    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_RESERVED1  0x09 /**< Reserved.*/
    105 #define PHFRINFC_LLCP_PTYPE_RESERVED2  0x0A /**< Reserved.*/
    106 #define PHFRINFC_LLCP_PTYPE_RESERVED3  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_RESERVED4  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_SDP_ADVERTISED_FIRST     0x10 /**< First SAP number from SDP-avertised SAP range.*/
    121 #define PHFRINFC_LLCP_SAP_SDP_UNADVERTISED_FIRST   0x20 /**< First SAP number from SDP-unavertised SAP range.*/
    122 #define PHFRINFC_LLCP_SAP_NUMBER                   0x40 /**< Number of possible SAP values (also first invalid value).*/
    123 #define PHFRINFC_LLCP_SAP_DEFAULT                  0xFF /**< Default number when a socket is created or reset */
    124 /*@}*/
    125 
    126 /**
    127  *  \name Length value for DM opCode
    128  *
    129  */
    130  /*@{*/
    131 #define PHFRINFC_LLCP_DM_LENGTH                    0x01 /**< Length value for DM opCode */
    132 /*@}*/
    133 
    134 
    135 /**
    136  * \internal
    137  * \name Masks used with parameters value.
    138  *
    139  */
    140 /*@{*/
    141 #define PHFRINFC_LLCP_TLV_MIUX_MASK                 0x07FF   /**< \internal Mask to apply to MIUX TLV Value.*/
    142 #define PHFRINFC_LLCP_TLV_WKS_MASK                  0x0001   /**< \internal Minimal bits to be set in WKS TLV Value.*/
    143 #define PHFRINFC_LLCP_TLV_RW_MASK                   0x0F     /**< \internal Mask to apply to RW TLV Value.*/
    144 #define PHFRINFC_LLCP_TLV_OPT_MASK                  0x03     /**< \internal Mask to apply to OPT TLV Value.*/
    145 /*@}*/
    146 
    147 /**
    148  * \internal
    149  * \name Type codes for parameters in TLV.
    150  *
    151  */
    152 /*@{*/
    153 #define PHFRINFC_LLCP_TLV_TYPE_VERSION               0x01   /**< \internal VERSION parameter Type code.*/
    154 #define PHFRINFC_LLCP_TLV_TYPE_MIUX                  0x02   /**< \internal MIUX parameter Type code.*/
    155 #define PHFRINFC_LLCP_TLV_TYPE_WKS                   0x03   /**< \internal WKS parameter Type code.*/
    156 #define PHFRINFC_LLCP_TLV_TYPE_LTO                   0x04   /**< \internal LTO parameter Type code.*/
    157 #define PHFRINFC_LLCP_TLV_TYPE_RW                    0x05   /**< \internal RW parameter Type code.*/
    158 #define PHFRINFC_LLCP_TLV_TYPE_SN                    0x06   /**< \internal SN parameter Type code.*/
    159 #define PHFRINFC_LLCP_TLV_TYPE_OPT                   0x07   /**< \internal OPT parameter Type code.*/
    160 /*@}*/
    161 
    162 /**
    163  * \internal
    164  * \name Fixed Value length for parameters in TLV.
    165  *
    166  */
    167 /*@{*/
    168 #define PHFRINFC_LLCP_TLV_LENGTH_HEADER              2   /**< \internal Fixed length of Type and Length fields in TLV.*/
    169 #define PHFRINFC_LLCP_TLV_LENGTH_VERSION             1   /**< \internal Fixed length of VERSION parameter Value.*/
    170 #define PHFRINFC_LLCP_TLV_LENGTH_MIUX                2   /**< \internal Fixed length of MIUX parameter Value.*/
    171 #define PHFRINFC_LLCP_TLV_LENGTH_WKS                 2   /**< \internal Fixed length of WKS parameter Value.*/
    172 #define PHFRINFC_LLCP_TLV_LENGTH_LTO                 1   /**< \internal Fixed length of LTO parameter Value.*/
    173 #define PHFRINFC_LLCP_TLV_LENGTH_RW                  1   /**< \internal Fixed length of RW parameter Value.*/
    174 #define PHFRINFC_LLCP_TLV_LENGTH_OPT                 1   /**< \internal Fixed length of OPT parameter Value.*/
    175 /*@}*/
    176 
    177 /**
    178  *  \name LLCP packet field sizes.
    179  *
    180  */
    181  /*@{*/
    182 #define PHFRINFC_LLCP_PACKET_HEADER_SIZE     2 /**< Size of the general packet header (DSAP+PTYPE+SSAP).*/
    183 #define PHFRINFC_LLCP_PACKET_SEQUENCE_SIZE   1 /**< Size of the sequence field, if present.*/
    184 #define PHFRINFC_LLCP_PACKET_MAX_SIZE        (PHFRINFC_LLCP_PACKET_HEADER_SIZE + \
    185                                              PHFRINFC_LLCP_PACKET_SEQUENCE_SIZE + \
    186                                              PHFRINFC_LLCP_MIU_DEFAULT + \
    187                                              PHFRINFC_LLCP_TLV_MIUX_MASK) /**< Maximum size of a packet */
    188 /*@}*/
    189 
    190 /*========== MACROS ===========*/
    191 
    192 #define CHECK_SEND_RW(socket) ( (((socket)->socket_VS - (socket)->socket_VSA)  % 16) < (socket)->remoteRW )
    193 
    194 /*========== ENUMERATES ===========*/
    195 
    196 typedef phFriNfc_LlcpMac_ePeerType_t      phFriNfc_Llcp_eRole_t;
    197 
    198 typedef phFriNfc_LlcpMac_eLinkStatus_t    phFriNfc_Llcp_eLinkStatus_t;
    199 
    200 /*========== CALLBACKS ===========*/
    201 
    202 typedef void (*phFriNfc_Llcp_Check_CB_t) (
    203    void                             *pContext,
    204    NFCSTATUS                        status
    205 );
    206 
    207 typedef void (*phFriNfc_Llcp_LinkStatus_CB_t) (
    208    void                             *pContext,
    209    phFriNfc_Llcp_eLinkStatus_t      eLinkStatus
    210 );
    211 
    212 typedef void (*phFriNfc_Llcp_Send_CB_t) (
    213    void                             *pContext,
    214    NFCSTATUS                        status
    215 );
    216 
    217 typedef void (*phFriNfc_Llcp_Recv_CB_t) (
    218    void                             *pContext,
    219    phNfc_sData_t                    *psData,
    220    NFCSTATUS                        status
    221 );
    222 
    223 /*========== STRUCTURES ===========*/
    224 
    225 typedef struct phFriNfc_Llcp_sPacketHeader
    226 {
    227    /**< The destination service access point*/
    228    unsigned   dsap : 6;
    229 
    230    /**< The packet type*/
    231    unsigned   ptype : 4;
    232 
    233    /**< The source service access point*/
    234    unsigned   ssap : 6;
    235 
    236 } phFriNfc_Llcp_sPacketHeader_t;
    237 
    238 typedef struct phFriNfc_Llcp_sPacketSequence
    239 {
    240    /**< Sequence number for sending*/
    241    unsigned   ns : 4;
    242 
    243    /**< Sequence number for reception*/
    244    unsigned   nr : 4;
    245 
    246 } phFriNfc_Llcp_sPacketSequence_t;
    247 
    248 typedef struct phFriNfc_Llcp_sSendOperation
    249 {
    250    /**< Sequence number for sending*/
    251    phFriNfc_Llcp_sPacketHeader_t    *psHeader;
    252 
    253    /**< Sequence number for sending*/
    254    phFriNfc_Llcp_sPacketSequence_t  *psSequence;
    255 
    256    /**< Sequence number for sending*/
    257    phNfc_sData_t                    *psInfo;
    258 
    259    /**< Sequence number for sending*/
    260    phFriNfc_Llcp_Send_CB_t          pfSend_CB;
    261 
    262    /**< Sequence number for sending*/
    263    void                             *pContext;
    264 
    265 } phFriNfc_Llcp_sSendOperation_t;
    266 
    267 typedef struct phFriNfc_Llcp_sRecvOperation
    268 {
    269    /**< Sequence number for sending*/
    270    uint8_t                    nSap;
    271 
    272    /**< Sequence number for sending*/
    273    phNfc_sData_t              *psBuffer;
    274 
    275    /**< Sequence number for sending*/
    276    phFriNfc_Llcp_Recv_CB_t    pfRecv_CB;
    277 
    278    /**< Sequence number for sending*/
    279    void                       *pContext;
    280 
    281 } phFriNfc_Llcp_sRecvOperation_t;
    282 
    283 typedef struct phFriNfc_Llcp
    284 {
    285    /**< The current state*/
    286    uint8_t                          state;
    287 
    288    /**< MAC mapping instance*/
    289    phFriNfc_LlcpMac_t               MAC;
    290 
    291    /**< Local LLC role*/
    292    phFriNfc_LlcpMac_eType_t         eRole;
    293 
    294    /**< Local link parameters*/
    295    phFriNfc_Llcp_sLinkParameters_t  sLocalParams;
    296 
    297    /**< Remote link parameters*/
    298    phFriNfc_Llcp_sLinkParameters_t  sRemoteParams;
    299 
    300    /**< Negociated protocol version (major number on MSB, minor on LSB)*/
    301    uint8_t                          version;
    302 
    303    /**< Internal reception buffer, its size may vary during time but not exceed nRxBufferSize*/
    304    phNfc_sData_t                   sRxBuffer;
    305 
    306    /**< Actual size of reception buffer*/
    307    uint16_t                        nRxBufferLength;
    308 
    309    /**< Internal emission buffer, its size may vary during time but not exceed nTxBufferSize*/
    310    phNfc_sData_t                   sTxBuffer;
    311 
    312    /**< Actual size of emission buffer*/
    313    uint16_t                        nTxBufferLength;
    314 
    315    /**< Callback function for link status notification*/
    316    phFriNfc_Llcp_LinkStatus_CB_t    pfLink_CB;
    317 
    318    /**< Callback context for link status notification*/
    319    void                             *pLinkContext;
    320 
    321    /**< Callback function for compliance checking*/
    322    phFriNfc_Llcp_Check_CB_t         pfChk_CB;
    323 
    324    /**< Callback context for compliance checking*/
    325    void                             *pChkContext;
    326 
    327    /**< Symmetry timer*/
    328    uint32_t                         hSymmTimer;
    329 
    330    /**< Control frames buffer*/
    331    uint8_t                          pCtrlTxBuffer[10];
    332 
    333    /**< Control frames buffer size*/
    334    uint8_t                          pCtrlTxBufferLength;
    335 
    336    /**< DISC packet send pending flag*/
    337    bool_t                           bDiscPendingFlag;
    338 
    339    /**< FRMR packet send pending flag*/
    340    bool_t                           bFrmrPendingFlag;
    341 
    342    /**< Header of pending FRMR packet*/
    343    phFriNfc_Llcp_sPacketHeader_t    sFrmrHeader;
    344 
    345    /**< Info field of pending FRMR packet*/
    346    uint8_t                          pFrmrInfo[4];
    347 
    348    /**< Send callback*/
    349    phFriNfc_Llcp_Send_CB_t          pfSendCB;
    350 
    351    /**< Send callback*/
    352    void                             *pSendContext;
    353 
    354    /**< Pending send header*/
    355    phFriNfc_Llcp_sPacketHeader_t    *psSendHeader;
    356 
    357    /**< Pending send sequence*/
    358    phFriNfc_Llcp_sPacketSequence_t  *psSendSequence;
    359 
    360    /**< Pending send info*/
    361    phNfc_sData_t                    *psSendInfo;
    362 
    363    /**< Receive callback*/
    364    phFriNfc_Llcp_Recv_CB_t          pfRecvCB;
    365 
    366    /**< Receive callback*/
    367    void                             *pRecvContext;
    368 
    369 } phFriNfc_Llcp_t;
    370 
    371 /*========== UNIONS ===========*/
    372 
    373 
    374 /*========== FUNCTIONS ===========*/
    375 
    376 /*!
    377  * \brief TODO
    378  */
    379 NFCSTATUS phFriNfc_Llcp_EncodeLinkParams( phNfc_sData_t                   *psRawBuffer,
    380                                           phFriNfc_Llcp_sLinkParameters_t *psLinkParams,
    381                                           uint8_t                         nVersion );
    382 
    383 
    384 /*!
    385  * \brief TODO
    386  */
    387 NFCSTATUS phFriNfc_Llcp_Reset( phFriNfc_Llcp_t                 *Llcp,
    388                                void                            *LowerDevice,
    389                                phFriNfc_Llcp_sLinkParameters_t *psLinkParams,
    390                                void                            *pRxBuffer,
    391                                uint16_t                        nRxBufferLength,
    392                                void                            *pTxBuffer,
    393                                uint16_t                        nTxBufferLength,
    394                                phFriNfc_Llcp_LinkStatus_CB_t   pfLink_CB,
    395                                void                            *pContext );
    396 
    397 /*!
    398  * \brief TODO
    399  */
    400 NFCSTATUS phFriNfc_Llcp_ChkLlcp( phFriNfc_Llcp_t               *Llcp,
    401                                  phHal_sRemoteDevInformation_t *psRemoteDevInfo,
    402                                  phFriNfc_Llcp_Check_CB_t      pfCheck_CB,
    403                                  void                          *pContext );
    404 
    405 /*!
    406  * \brief TODO
    407  */
    408 NFCSTATUS phFriNfc_Llcp_Activate( phFriNfc_Llcp_t  *Llcp );
    409 
    410 /*!
    411  * \brief TODO
    412  */
    413 NFCSTATUS phFriNfc_Llcp_Deactivate( phFriNfc_Llcp_t *Llcp );
    414 
    415 /*!
    416  * \brief TODO
    417  */
    418 NFCSTATUS phFriNfc_Llcp_GetLocalInfo( phFriNfc_Llcp_t                   *Llcp,
    419                                       phFriNfc_Llcp_sLinkParameters_t   *pParams );
    420 
    421 /*!
    422  * \brief TODO
    423  */
    424 NFCSTATUS phFriNfc_Llcp_GetRemoteInfo( phFriNfc_Llcp_t                  *Llcp,
    425                                        phFriNfc_Llcp_sLinkParameters_t  *pParams );
    426 
    427 /*!
    428  * \brief TODO
    429  */
    430 NFCSTATUS phFriNfc_Llcp_Send( phFriNfc_Llcp_t                  *Llcp,
    431                               phFriNfc_Llcp_sPacketHeader_t    *psHeader,
    432                               phFriNfc_Llcp_sPacketSequence_t  *psSequence,
    433                               phNfc_sData_t                    *psInfo,
    434                               phFriNfc_Llcp_Send_CB_t          pfSend_CB,
    435                               void                             *pContext );
    436 
    437 /*!
    438  * \brief TODO
    439  */
    440 NFCSTATUS phFriNfc_Llcp_Recv( phFriNfc_Llcp_t            *Llcp,
    441                               phFriNfc_Llcp_Recv_CB_t    pfRecv_CB,
    442                               void                       *pContext );
    443 
    444 
    445 #endif /* PHFRINFC_LLCP_H */
    446