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