1/******************************************************************************
2 *
3 *  Copyright (C) 2010-2014 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  This is the public interface file for NFA P2P, Broadcom's NFC
22 *  application layer for mobile phones.
23 *
24 ******************************************************************************/
25#ifndef NFA_P2P_API_H
26#define NFA_P2P_API_H
27
28#include "llcp_api.h"
29#include "nfa_api.h"
30
31/*****************************************************************************
32**  Constants and data types
33*****************************************************************************/
34
35/* NFA P2P Reason of disconnection */
36#define NFA_P2P_DISC_REASON_REMOTE_INITIATE \
37  0x00 /* remote initiated to disconnect  */
38#define NFA_P2P_DISC_REASON_LOCAL_INITITATE \
39  0x01 /* local initiated to disconnect   */
40#define NFA_P2P_DISC_REASON_NO_SERVICE \
41  0x02 /* no service bound in remote      */
42#define NFA_P2P_DISC_REASON_REMOTE_REJECT \
43  0x03 /* remote rejected connection      */
44#define NFA_P2P_DISC_REASON_FRAME_ERROR \
45  0x04 /* sending or receiving FRMR PDU   */
46#define NFA_P2P_DISC_REASON_LLCP_DEACTIVATED \
47  0x05 /* LLCP link deactivated           */
48#define NFA_P2P_DISC_REASON_NO_RESOURCE \
49  0x06 /* Out of resource in local device */
50#define NFA_P2P_DISC_REASON_NO_INFORMATION \
51  0x80 /* Without information             */
52
53/* NFA P2P callback events */
54/* Server is registered                         */
55#define NFA_P2P_REG_SERVER_EVT 0x00
56/* Client is registered                         */
57#define NFA_P2P_REG_CLIENT_EVT 0x01
58/* LLCP Link has been activated                 */
59#define NFA_P2P_ACTIVATED_EVT 0x02
60#define NFA_P2P_DEACTIVATED_EVT \
61  0x03 /* LLCP Link has been deactivated               */
62/* Data link connection request from peer       */
63#define NFA_P2P_CONN_REQ_EVT 0x04
64/* Data link connection has been established    */
65#define NFA_P2P_CONNECTED_EVT 0x05
66/* Data link connection has been disconnected   */
67#define NFA_P2P_DISC_EVT 0x06
68/* Data received from peer                      */
69#define NFA_P2P_DATA_EVT 0x07
70/* Status indication of outgoing data           */
71#define NFA_P2P_CONGEST_EVT 0x08
72/* link MIU and Well-Known Service list         */
73#define NFA_P2P_LINK_INFO_EVT 0x09
74#define NFA_P2P_SDP_EVT 0x0A /* Remote SAP of SDP result */
75
76typedef uint8_t tNFA_P2P_EVT;
77
78/* NFA allocates a SAP for server */
79#define NFA_P2P_ANY_SAP LLCP_INVALID_SAP
80#define NFA_P2P_INVALID_SAP LLCP_INVALID_SAP
81
82/* Recommanded MIU's for connection-oriented */
83#define NFA_P2P_MIU_1 \
84  (NCI_NFC_DEP_MAX_DATA - LLCP_PDU_HEADER_SIZE - LLCP_SEQUENCE_SIZE)
85#define NFA_P2P_MIU_2 \
86  (2 * NCI_NFC_DEP_MAX_DATA - LLCP_PDU_HEADER_SIZE - LLCP_SEQUENCE_SIZE)
87#define NFA_P2P_MIU_3 \
88  (3 * NCI_NFC_DEP_MAX_DATA - LLCP_PDU_HEADER_SIZE - LLCP_SEQUENCE_SIZE)
89#define NFA_P2P_MIU_8 \
90  (8 * NCI_NFC_DEP_MAX_DATA - LLCP_PDU_HEADER_SIZE - LLCP_SEQUENCE_SIZE)
91
92#define NFA_P2P_LLINK_TYPE LLCP_LINK_TYPE_LOGICAL_DATA_LINK
93#define NFA_P2P_DLINK_TYPE LLCP_LINK_TYPE_DATA_LINK_CONNECTION
94typedef uint8_t tNFA_P2P_LINK_TYPE;
95
96/* Data for NFA_P2P_REG_SERVER_EVT */
97typedef struct {
98  tNFA_HANDLE server_handle; /* NFA_HANDLE_INVALID if failed */
99  char service_name[LLCP_MAX_SN_LEN + 1];
100  uint8_t server_sap;
101} tNFA_P2P_REG_SERVER;
102
103/* Data for NFA_P2P_REG_CLIENT_EVT */
104typedef struct {
105  tNFA_HANDLE client_handle; /* NFA_HANDLE_INVALID if failed */
106} tNFA_P2P_REG_CLIENT;
107
108/* Data for NFA_P2P_ACTIVATED_EVT */
109typedef struct {
110  tNFA_HANDLE handle;
111  uint16_t local_link_miu;
112  uint16_t remote_link_miu;
113} tNFA_P2P_ACTIVATED;
114
115/* Data for NFA_P2P_DEACTIVATED_EVT */
116typedef struct { tNFA_HANDLE handle; } tNFA_P2P_DEACTIVATED;
117
118/* Data for NFA_P2P_CONN_REQ_EVT */
119typedef struct {
120  tNFA_HANDLE server_handle;
121  tNFA_HANDLE conn_handle;
122  uint8_t remote_sap;
123  uint16_t remote_miu;
124  uint8_t remote_rw;
125} tNFA_P2P_CONN_REQ;
126
127/* Data for NFA_P2P_CONNECTED_EVT */
128typedef struct {
129  tNFA_HANDLE client_handle;
130  tNFA_HANDLE conn_handle;
131  uint8_t remote_sap;
132  uint16_t remote_miu;
133  uint8_t remote_rw;
134} tNFA_P2P_CONN;
135
136/* Data for NFA_P2P_DISC_EVT */
137typedef struct {
138  tNFA_HANDLE handle;
139  uint8_t reason;
140} tNFA_P2P_DISC;
141
142/* Data for NFA_P2P_DATA_EVT */
143typedef struct {
144  tNFA_HANDLE handle;
145  uint8_t remote_sap;
146  tNFA_P2P_LINK_TYPE link_type;
147} tNFA_P2P_DATA;
148
149/* Data for NFA_P2P_CONGEST_EVT */
150typedef struct {
151  tNFA_HANDLE handle;
152  bool is_congested;
153  tNFA_P2P_LINK_TYPE link_type;
154} tNFA_P2P_CONGEST;
155
156/* Data for NFA_P2P_LINK_INFO_EVT */
157typedef struct {
158  tNFA_HANDLE handle;
159  uint16_t wks; /* well-known service */
160  uint16_t local_link_miu;
161  uint16_t remote_link_miu;
162} tNFA_P2P_LINK_INFO;
163
164/* Data for NFA_P2P_SDP_EVT */
165typedef struct {
166  tNFA_HANDLE handle;
167  uint8_t remote_sap; /* 0x00 if failed */
168} tNFA_P2P_SDP;
169
170/* Union of all P2P callback structures */
171typedef union {
172  tNFA_P2P_REG_SERVER reg_server;   /* NFA_P2P_REG_SERVER_EVT   */
173  tNFA_P2P_REG_CLIENT reg_client;   /* NFA_P2P_REG_CLIENT_EVT   */
174  tNFA_P2P_ACTIVATED activated;     /* NFA_P2P_ACTIVATED_EVT    */
175  tNFA_P2P_DEACTIVATED deactivated; /* NFA_P2P_DEACTIVATED_EVT  */
176  tNFA_P2P_CONN_REQ conn_req;       /* NFA_P2P_CONN_REQ_EVT     */
177  tNFA_P2P_CONN connected;          /* NFA_P2P_CONNECTED_EVT    */
178  tNFA_P2P_DISC disc;               /* NFA_P2P_DISC_EVT         */
179  tNFA_P2P_DATA data;               /* NFA_P2P_DATA_EVT         */
180  tNFA_P2P_CONGEST congest;         /* NFA_P2P_CONGEST_EVT      */
181  tNFA_P2P_LINK_INFO link_info;     /* NFA_P2P_LINK_INFO_EVT    */
182  tNFA_P2P_SDP sdp;                 /* NFA_P2P_SDP_EVT          */
183} tNFA_P2P_EVT_DATA;
184
185/* NFA P2P callback */
186typedef void(tNFA_P2P_CBACK)(tNFA_P2P_EVT event, tNFA_P2P_EVT_DATA* p_data);
187
188/*****************************************************************************
189**  External Function Declarations
190*****************************************************************************/
191#ifdef __cplusplus
192extern "C" {
193#endif
194
195/*******************************************************************************
196**
197** Function         NFA_P2pRegisterServer
198**
199** Description      This function is called to listen to a SAP as server on
200**                  LLCP.
201**
202**                  NFA_P2P_REG_SERVER_EVT will be returned with status and
203**                  handle.
204**
205**                  If server_sap is set to NFA_P2P_ANY_SAP, then NFA will
206**                  allocate a SAP between LLCP_LOWER_BOUND_SDP_SAP and
207**                  LLCP_UPPER_BOUND_SDP_SAP Otherwise, server_sap must be
208**                  between (LLCP_SDP_SAP + 1) and LLCP_UPPER_BOUND_SDP_SAP
209**
210**                  link_type : NFA_P2P_LLINK_TYPE and/or NFA_P2P_DLINK_TYPE
211**
212** Note:            If RF discovery is started,
213**                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
214**                  happen before calling this function
215**
216** Returns          NFA_STATUS_OK if successfully initiated
217**                  NFA_STATUS_FAILED otherwise
218**
219*******************************************************************************/
220extern tNFA_STATUS NFA_P2pRegisterServer(uint8_t server_sap,
221                                         tNFA_P2P_LINK_TYPE link_type,
222                                         char* p_service_name,
223                                         tNFA_P2P_CBACK* p_cback);
224
225/*******************************************************************************
226**
227** Function         NFA_P2pRegisterClient
228**
229** Description      This function is called to register a client service on
230**                  LLCP.
231**
232**                  NFA_P2P_REG_CLIENT_EVT will be returned with status and
233**                  handle.
234**
235**                  link_type : NFA_P2P_LLINK_TYPE and/or NFA_P2P_DLINK_TYPE
236**
237** Returns          NFA_STATUS_OK if successfully initiated
238**                  NFA_STATUS_FAILED otherwise
239**
240*******************************************************************************/
241extern tNFA_STATUS NFA_P2pRegisterClient(tNFA_P2P_LINK_TYPE link_type,
242                                         tNFA_P2P_CBACK* p_cback);
243
244/*******************************************************************************
245**
246** Function         NFA_P2pDeregister
247**
248** Description      This function is called to stop listening to a SAP as server
249**                  or stop client service on LLCP.
250**
251** Note:            If this function is called to de-register a server and RF
252**                  discovery is started,
253**                  NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT should
254**                  happen before calling this function
255**
256** Returns          NFA_STATUS_OK if successfully initiated
257**                  NFA_STATUS_BAD_HANDLE if handle is not valid
258**                  NFA_STATUS_FAILED otherwise
259**
260*******************************************************************************/
261extern tNFA_STATUS NFA_P2pDeregister(tNFA_HANDLE handle);
262
263/*******************************************************************************
264**
265** Function         NFA_P2pAcceptConn
266**
267** Description      This function is called to accept a request of data link
268**                  connection to a listening SAP on LLCP after receiving
269**                  NFA_P2P_CONN_REQ_EVT.
270**
271** Returns          NFA_STATUS_OK if successfully initiated
272**                  NFA_STATUS_BAD_HANDLE if handle is not valid
273**                  NFA_STATUS_FAILED otherwise
274**
275*******************************************************************************/
276extern tNFA_STATUS NFA_P2pAcceptConn(tNFA_HANDLE conn_handle, uint16_t miu,
277                                     uint8_t rw);
278
279/*******************************************************************************
280**
281** Function         NFA_P2pRejectConn
282**
283** Description      This function is called to reject a request of data link
284**                  connection to a listening SAP on LLCP after receiving
285**                  NFA_P2P_CONN_REQ_EVT.
286**
287** Returns          NFA_STATUS_OK if successfully initiated
288**                  NFA_STATUS_BAD_HANDLE if handle is not valid
289**                  NFA_STATUS_FAILED otherwise
290**
291*******************************************************************************/
292extern tNFA_STATUS NFA_P2pRejectConn(tNFA_HANDLE conn_handle);
293
294/*******************************************************************************
295**
296** Function         NFA_P2pDisconnect
297**
298** Description      This function is called to disconnect an existing or
299**                  connecting data link connection.
300**
301**                  discard any pending data on data link connection if flush is
302**                  set to TRUE
303**
304**                  NFA_P2P_DISC_EVT will be returned after data link connection
305**                  is disconnected
306**
307** Returns          NFA_STATUS_OK if successfully initiated
308**                  NFA_STATUS_BAD_HANDLE if handle is not valid
309**                  NFA_STATUS_FAILED otherwise
310**
311*******************************************************************************/
312extern tNFA_STATUS NFA_P2pDisconnect(tNFA_HANDLE conn_handle, bool flush);
313
314/*******************************************************************************
315**
316** Function         NFA_P2pConnectByName
317**
318** Description      This function is called to create a connection-oriented
319**                  transport by a service name.
320**                  NFA_P2P_CONNECTED_EVT if success
321**                  NFA_P2P_DISC_EVT if failed
322**
323** Returns          NFA_STATUS_OK if successfully initiated
324**                  NFA_STATUS_BAD_HANDLE if client is not registered
325**                  NFA_STATUS_FAILED otherwise
326**
327*******************************************************************************/
328extern tNFA_STATUS NFA_P2pConnectByName(tNFA_HANDLE client_handle,
329                                        char* p_service_name, uint16_t miu,
330                                        uint8_t rw);
331
332/*******************************************************************************
333**
334** Function         NFA_P2pConnectBySap
335**
336** Description      This function is called to create a connection-oriented
337**                  transport by a SAP.
338**                  NFA_P2P_CONNECTED_EVT if success
339**                  NFA_P2P_DISC_EVT if failed
340**
341** Returns          NFA_STATUS_OK if successfully initiated
342**                  NFA_STATUS_BAD_HANDLE if client is not registered
343**                  NFA_STATUS_FAILED otherwise
344**
345*******************************************************************************/
346extern tNFA_STATUS NFA_P2pConnectBySap(tNFA_HANDLE client_handle, uint8_t dsap,
347                                       uint16_t miu, uint8_t rw);
348
349/*******************************************************************************
350**
351** Function         NFA_P2pSendUI
352**
353** Description      This function is called to send data on connectionless
354**                  transport.
355**
356** Returns          NFA_STATUS_OK if successfully initiated
357**                  NFA_STATUS_BAD_HANDLE if handle is not valid
358**                  NFA_STATUS_BAD_LENGTH if data length is more than remote
359**                  link MIU
360**                  NFA_STATUS_CONGESTED  if congested
361**                  NFA_STATUS_FAILED otherwise
362**
363*******************************************************************************/
364extern tNFA_STATUS NFA_P2pSendUI(tNFA_HANDLE handle, uint8_t dsap,
365                                 uint16_t length, uint8_t* p_data);
366
367/*******************************************************************************
368**
369** Function         NFA_P2pReadUI
370**
371** Description      This function is called to read data on connectionless
372**                  transport when receiving NFA_P2P_DATA_EVT with
373**                  NFA_P2P_LLINK_TYPE.
374**
375**                  - Remote SAP who sent UI PDU is returned.
376**                  - Information of UI PDU up to max_data_len is copied into
377**                    p_data.
378**                  - If more information of UI PDU or more UI PDU in queue then
379**                    more is returned to TRUE.
380**                  - Information of next UI PDU is not concatenated.
381**
382** Returns          NFA_STATUS_OK if successfully initiated
383**                  NFA_STATUS_BAD_HANDLE if handle is not valid
384**
385*******************************************************************************/
386extern tNFA_STATUS NFA_P2pReadUI(tNFA_HANDLE handle, uint32_t max_data_len,
387                                 uint8_t* p_remote_sap, uint32_t* p_data_len,
388                                 uint8_t* p_data, bool* p_more);
389
390/*******************************************************************************
391**
392** Function         NFA_P2pFlushUI
393**
394** Description      This function is called to flush data on connectionless
395**                  transport.
396**
397** Returns          NFA_STATUS_OK if successfully initiated
398**                  NFA_STATUS_BAD_HANDLE if handle is not valid
399**
400*******************************************************************************/
401extern tNFA_STATUS NFA_P2pFlushUI(tNFA_HANDLE handle, uint32_t* p_length);
402
403/*******************************************************************************
404**
405** Function         NFA_P2pSendData
406**
407** Description      This function is called to send data on connection-oriented
408**                  transport.
409**
410** Returns          NFA_STATUS_OK if successfully initiated
411**                  NFA_STATUS_BAD_HANDLE if handle is not valid
412**                  NFA_STATUS_BAD_LENGTH if data length is more than remote MIU
413**                  NFA_STATUS_CONGESTED  if congested
414**                  NFA_STATUS_FAILED otherwise
415**
416*******************************************************************************/
417extern tNFA_STATUS NFA_P2pSendData(tNFA_HANDLE conn_handle, uint16_t length,
418                                   uint8_t* p_data);
419
420/*******************************************************************************
421**
422** Function         NFA_P2pReadData
423**
424** Description      This function is called to read data on connection-oriented
425**                  transport when receiving NFA_P2P_DATA_EVT with
426**                  NFA_P2P_DLINK_TYPE.
427**
428**                  - Information of I PDU is copied into p_data up to
429**                    max_data_len.
430**                  - If more information of I PDU or more I PDU in queue, then
431**                    more is returned to TRUE.
432**                  - Information of next I PDU is not concatenated.
433**
434** Returns          NFA_STATUS_OK if successfully initiated
435**                  NFA_STATUS_BAD_HANDLE if handle is not valid
436**
437*******************************************************************************/
438extern tNFA_STATUS NFA_P2pReadData(tNFA_HANDLE handle, uint32_t max_data_len,
439                                   uint32_t* p_data_len, uint8_t* p_data,
440                                   bool* p_more);
441
442/*******************************************************************************
443**
444** Function         NFA_P2pFlushData
445**
446** Description      This function is called to flush data on connection-oriented
447**                  transport.
448**
449** Returns          NFA_STATUS_OK if successfully initiated
450**                  NFA_STATUS_BAD_HANDLE if handle is not valid
451**
452*******************************************************************************/
453extern tNFA_STATUS NFA_P2pFlushData(tNFA_HANDLE handle, uint32_t* p_length);
454
455/*******************************************************************************
456**
457** Function         NFA_P2pSetLocalBusy
458**
459** Description      This function is called to stop or resume incoming data on
460**                  connection-oriented transport.
461**
462** Returns          NFA_STATUS_OK if successfully initiated
463**                  NFA_STATUS_BAD_HANDLE if handle is not valid
464**                  NFA_STATUS_FAILED otherwise
465**
466*******************************************************************************/
467extern tNFA_STATUS NFA_P2pSetLocalBusy(tNFA_HANDLE conn_handle, bool is_busy);
468
469/*******************************************************************************
470**
471** Function         NFA_P2pGetLinkInfo
472**
473** Description      This function is called to get local/remote link MIU and
474**                  Well-Known Service list encoded as a 16-bit field of
475**                  connected LLCP. NFA_P2P_LINK_INFO_EVT will be returned.
476**
477** Returns          NFA_STATUS_OK if successfully initiated
478**                  NFA_STATUS_BAD_HANDLE if server or client is not registered
479**                  NFA_STATUS_FAILED otherwise
480**
481*******************************************************************************/
482extern tNFA_STATUS NFA_P2pGetLinkInfo(tNFA_HANDLE handle);
483
484/*******************************************************************************
485**
486** Function         NFA_P2pGetRemoteSap
487**
488** Description      This function is called to get SAP associated by service
489**                  name on connected remote LLCP.
490**                  NFA_P2P_SDP_EVT will be returned.
491**
492** Returns          NFA_STATUS_OK if successfully initiated
493**                  NFA_STATUS_BAD_HANDLE if server or client is not registered
494**                  NFA_STATUS_FAILED otherwise
495**
496*******************************************************************************/
497extern tNFA_STATUS NFA_P2pGetRemoteSap(tNFA_HANDLE handle,
498                                       char* p_service_name);
499
500/*******************************************************************************
501**
502** Function         NFA_P2pSetLLCPConfig
503**
504** Description      This function is called to change LLCP config parameters.
505**                  Application must call while LLCP is not activated.
506**
507**                  Parameters descriptions (default value)
508**                  - Local Link MIU (LLCP_MIU)
509**                  - Option parameter (LLCP_OPT_VALUE)
510**                  - Response Waiting Time Index (LLCP_WAITING_TIME)
511**                  - Local Link Timeout (LLCP_LTO_VALUE)
512**                  - Inactivity Timeout as initiator role
513**                    (LLCP_INIT_INACTIVITY_TIMEOUT)
514**                  - Inactivity Timeout as target role
515**                    (LLCP_TARGET_INACTIVITY_TIMEOUT)
516**                  - Delay SYMM response (LLCP_DELAY_RESP_TIME)
517**                  - Data link connection timeout
518**                    (LLCP_DATA_LINK_CONNECTION_TOUT)
519**                  - Delay timeout to send first PDU as initiator
520**                    (LLCP_DELAY_TIME_TO_SEND_FIRST_PDU)
521**
522** Returns          NFA_STATUS_OK if successfully initiated
523**                  NFA_STATUS_FAILED otherwise
524**
525*******************************************************************************/
526extern tNFA_STATUS NFA_P2pSetLLCPConfig(uint16_t link_miu, uint8_t opt,
527                                        uint8_t wt, uint16_t link_timeout,
528                                        uint16_t inact_timeout_init,
529                                        uint16_t inact_timeout_target,
530                                        uint16_t symm_delay,
531                                        uint16_t data_link_timeout,
532                                        uint16_t delay_first_pdu_timeout);
533
534/*******************************************************************************
535**
536** Function         NFA_P2pGetLLCPConfig
537**
538** Description      This function is called to read LLCP config parameters.
539**
540**                  Parameters descriptions
541**                  - Local Link MIU
542**                  - Option parameter
543**                  - Response Waiting Time Index
544**                  - Local Link Timeout
545**                  - Inactivity Timeout as initiator role
546**                  - Inactivity Timeout as target role
547**                  - Delay SYMM response
548**                  - Data link connection timeout
549**                  - Delay timeout to send first PDU as initiator
550**
551** Returns          None
552**
553*******************************************************************************/
554extern void NFA_P2pGetLLCPConfig(uint16_t* p_link_miu, uint8_t* p_opt,
555                                 uint8_t* p_wt, uint16_t* p_link_timeout,
556                                 uint16_t* p_inact_timeout_init,
557                                 uint16_t* p_inact_timeout_target,
558                                 uint16_t* p_symm_delay,
559                                 uint16_t* p_data_link_timeout,
560                                 uint16_t* p_delay_first_pdu_timeout);
561
562/*******************************************************************************
563**
564** Function         NFA_P2pSetTraceLevel
565**
566** Description      This function sets the trace level for P2P.  If called with
567**                  a value of 0xFF, it simply returns the current trace level.
568**
569** Returns          The new or current trace level
570**
571*******************************************************************************/
572extern uint8_t NFA_P2pSetTraceLevel(uint8_t new_level);
573
574#ifdef __cplusplus
575}
576#endif
577
578#endif /* NFA_P2P_API_H */
579