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