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 0x30 /**< 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 191 /*========== ENUMERATES ===========*/ 192 193 typedef phFriNfc_LlcpMac_ePeerType_t phFriNfc_Llcp_eRole_t; 194 195 typedef phFriNfc_LlcpMac_eLinkStatus_t phFriNfc_Llcp_eLinkStatus_t; 196 197 /*========== CALLBACKS ===========*/ 198 199 typedef void (*phFriNfc_Llcp_Check_CB_t) ( 200 void *pContext, 201 NFCSTATUS status 202 ); 203 204 typedef void (*phFriNfc_Llcp_LinkStatus_CB_t) ( 205 void *pContext, 206 phFriNfc_Llcp_eLinkStatus_t eLinkStatus 207 ); 208 209 typedef void (*phFriNfc_Llcp_Send_CB_t) ( 210 void *pContext, 211 NFCSTATUS status 212 ); 213 214 typedef void (*phFriNfc_Llcp_Recv_CB_t) ( 215 void *pContext, 216 phNfc_sData_t *psData, 217 NFCSTATUS status 218 ); 219 220 /*========== STRUCTURES ===========*/ 221 222 typedef struct phFriNfc_Llcp_sPacketHeader 223 { 224 /**< The destination service access point*/ 225 unsigned dsap : 6; 226 227 /**< The packet type*/ 228 unsigned ptype : 4; 229 230 /**< The source service access point*/ 231 unsigned ssap : 6; 232 233 } phFriNfc_Llcp_sPacketHeader_t; 234 235 typedef struct phFriNfc_Llcp_sPacketSequence 236 { 237 /**< Sequence number for sending*/ 238 unsigned ns : 4; 239 240 /**< Sequence number for reception*/ 241 unsigned nr : 4; 242 243 } phFriNfc_Llcp_sPacketSequence_t; 244 245 typedef struct phFriNfc_Llcp_sSendOperation 246 { 247 /**< Sequence number for sending*/ 248 phFriNfc_Llcp_sPacketHeader_t *psHeader; 249 250 /**< Sequence number for sending*/ 251 phFriNfc_Llcp_sPacketSequence_t *psSequence; 252 253 /**< Sequence number for sending*/ 254 phNfc_sData_t *psInfo; 255 256 /**< Sequence number for sending*/ 257 phFriNfc_Llcp_Send_CB_t pfSend_CB; 258 259 /**< Sequence number for sending*/ 260 void *pContext; 261 262 } phFriNfc_Llcp_sSendOperation_t; 263 264 typedef struct phFriNfc_Llcp_sRecvOperation 265 { 266 /**< Sequence number for sending*/ 267 uint8_t nSap; 268 269 /**< Sequence number for sending*/ 270 phNfc_sData_t *psBuffer; 271 272 /**< Sequence number for sending*/ 273 phFriNfc_Llcp_Recv_CB_t pfRecv_CB; 274 275 /**< Sequence number for sending*/ 276 void *pContext; 277 278 } phFriNfc_Llcp_sRecvOperation_t; 279 280 typedef struct phFriNfc_Llcp 281 { 282 /**< The current state*/ 283 uint8_t state; 284 285 /**< MAC mapping instance*/ 286 phFriNfc_LlcpMac_t MAC; 287 288 /**< Local LLC role*/ 289 phFriNfc_LlcpMac_eType_t eRole; 290 291 /**< Local link parameters*/ 292 phFriNfc_Llcp_sLinkParameters_t sLocalParams; 293 294 /**< Remote link parameters*/ 295 phFriNfc_Llcp_sLinkParameters_t sRemoteParams; 296 297 /**< Negociated protocol version (major number on MSB, minor on LSB)*/ 298 uint8_t version; 299 300 /**< Internal reception buffer, its size may vary during time but not exceed nRxBufferSize*/ 301 phNfc_sData_t sRxBuffer; 302 303 /**< Actual size of reception buffer*/ 304 uint16_t nRxBufferLength; 305 306 /**< Internal emission buffer, its size may vary during time but not exceed nTxBufferSize*/ 307 phNfc_sData_t sTxBuffer; 308 309 /**< Actual size of emission buffer*/ 310 uint16_t nTxBufferLength; 311 312 /**< Callback function for link status notification*/ 313 phFriNfc_Llcp_LinkStatus_CB_t pfLink_CB; 314 315 /**< Callback context for link status notification*/ 316 void *pLinkContext; 317 318 /**< Callback function for compliance checking*/ 319 phFriNfc_Llcp_Check_CB_t pfChk_CB; 320 321 /**< Callback context for compliance checking*/ 322 void *pChkContext; 323 324 /**< Symmetry timer*/ 325 uint32_t hSymmTimer; 326 327 /**< Control frames buffer*/ 328 uint8_t pCtrlTxBuffer[10]; 329 330 /**< Control frames buffer size*/ 331 uint8_t pCtrlTxBufferLength; 332 333 /**< DISC packet send pending flag*/ 334 bool_t bDiscPendingFlag; 335 336 /**< FRMR packet send pending flag*/ 337 bool_t bFrmrPendingFlag; 338 339 /**< Header of pending FRMR packet*/ 340 phFriNfc_Llcp_sPacketHeader_t sFrmrHeader; 341 342 /**< Info field of pending FRMR packet*/ 343 uint8_t pFrmrInfo[4]; 344 345 /**< Send callback*/ 346 phFriNfc_Llcp_Send_CB_t pfSendCB; 347 348 /**< Send callback*/ 349 void *pSendContext; 350 351 /**< Pending send header*/ 352 phFriNfc_Llcp_sPacketHeader_t *psSendHeader; 353 354 /**< Pending send sequence*/ 355 phFriNfc_Llcp_sPacketSequence_t *psSendSequence; 356 357 /**< Pending send info*/ 358 phNfc_sData_t *psSendInfo; 359 360 /**< Receive callback*/ 361 phFriNfc_Llcp_Recv_CB_t pfRecvCB; 362 363 /**< Receive callback*/ 364 void *pRecvContext; 365 366 } phFriNfc_Llcp_t; 367 368 /*========== UNIONS ===========*/ 369 370 371 /*========== FUNCTIONS ===========*/ 372 373 /*! 374 * \brief TODO 375 */ 376 NFCSTATUS phFriNfc_Llcp_EncodeLinkParams( phNfc_sData_t *psRawBuffer, 377 phFriNfc_Llcp_sLinkParameters_t *psLinkParams, 378 uint8_t nVersion ); 379 380 381 /*! 382 * \brief TODO 383 */ 384 NFCSTATUS phFriNfc_Llcp_Reset( phFriNfc_Llcp_t *Llcp, 385 void *LowerDevice, 386 phFriNfc_Llcp_sLinkParameters_t *psLinkParams, 387 void *pRxBuffer, 388 uint16_t nRxBufferLength, 389 void *pTxBuffer, 390 uint16_t nTxBufferLength, 391 phFriNfc_Llcp_LinkStatus_CB_t pfLink_CB, 392 void *pContext ); 393 394 /*! 395 * \brief TODO 396 */ 397 NFCSTATUS phFriNfc_Llcp_ChkLlcp( phFriNfc_Llcp_t *Llcp, 398 phHal_sRemoteDevInformation_t *psRemoteDevInfo, 399 phFriNfc_Llcp_Check_CB_t pfCheck_CB, 400 void *pContext ); 401 402 /*! 403 * \brief TODO 404 */ 405 NFCSTATUS phFriNfc_Llcp_Activate( phFriNfc_Llcp_t *Llcp ); 406 407 /*! 408 * \brief TODO 409 */ 410 NFCSTATUS phFriNfc_Llcp_Deactivate( phFriNfc_Llcp_t *Llcp ); 411 412 /*! 413 * \brief TODO 414 */ 415 NFCSTATUS phFriNfc_Llcp_GetLocalInfo( phFriNfc_Llcp_t *Llcp, 416 phFriNfc_Llcp_sLinkParameters_t *pParams ); 417 418 /*! 419 * \brief TODO 420 */ 421 NFCSTATUS phFriNfc_Llcp_GetRemoteInfo( phFriNfc_Llcp_t *Llcp, 422 phFriNfc_Llcp_sLinkParameters_t *pParams ); 423 424 /*! 425 * \brief TODO 426 */ 427 NFCSTATUS phFriNfc_Llcp_Send( phFriNfc_Llcp_t *Llcp, 428 phFriNfc_Llcp_sPacketHeader_t *psHeader, 429 phFriNfc_Llcp_sPacketSequence_t *psSequence, 430 phNfc_sData_t *psInfo, 431 phFriNfc_Llcp_Send_CB_t pfSend_CB, 432 void *pContext ); 433 434 /*! 435 * \brief TODO 436 */ 437 NFCSTATUS phFriNfc_Llcp_Recv( phFriNfc_Llcp_t *Llcp, 438 phFriNfc_Llcp_Recv_CB_t pfRecv_CB, 439 void *pContext ); 440 441 442 #endif /* PHFRINFC_LLCP_H */ 443