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