• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *
22  *  This is the main implementation file for the NFA P2P.
23  *
24  ******************************************************************************/
25 #include <string.h>
26 #include "nfc_api.h"
27 #include "nfa_sys.h"
28 #include "nfa_sys_int.h"
29 #include "nfa_dm_int.h"
30 #include "llcp_api.h"
31 #include "llcp_defs.h"
32 #include "nfa_p2p_api.h"
33 #include "nfa_p2p_int.h"
34 
35 /*****************************************************************************
36 **  Global Variables
37 *****************************************************************************/
38 
39 /* system manager control block definition */
40 #if NFA_DYNAMIC_MEMORY == FALSE
41 tNFA_P2P_CB nfa_p2p_cb;
42 #endif
43 
44 /*****************************************************************************
45 **  Static Functions
46 *****************************************************************************/
47 
48 /* event handler function type */
49 static BOOLEAN nfa_p2p_evt_hdlr (BT_HDR *p_msg);
50 
51 /* disable function type */
52 static void nfa_p2p_sys_disable (void);
53 static void nfa_p2p_update_active_listen (void);
54 
55 /* debug functions type */
56 #if (BT_TRACE_VERBOSE == TRUE)
57 static char *nfa_p2p_llcp_state_code (tNFA_P2P_LLCP_STATE state_code);
58 #endif
59 
60 /*****************************************************************************
61 **  Constants
62 *****************************************************************************/
63 /* timeout to restore active listen mode if no RF activation on passive mode */
64 #define NFA_P2P_RESTORE_ACTIVE_LISTEN_TIMEOUT   5000
65 
66 static const tNFA_SYS_REG nfa_p2p_sys_reg =
67 {
68     NULL,
69     nfa_p2p_evt_hdlr,
70     nfa_p2p_sys_disable,
71     NULL
72 };
73 
74 #define NFA_P2P_NUM_ACTIONS  (NFA_P2P_LAST_EVT & 0x00ff)
75 
76 /* type for action functions */
77 typedef BOOLEAN (*tNFA_P2P_ACTION) (tNFA_P2P_MSG *p_data);
78 
79 /* action function list */
80 const tNFA_P2P_ACTION nfa_p2p_action[] =
81 {
82     nfa_p2p_reg_server,                     /* NFA_P2P_API_REG_SERVER_EVT       */
83     nfa_p2p_reg_client,                     /* NFA_P2P_API_REG_CLIENT_EVT       */
84     nfa_p2p_dereg,                          /* NFA_P2P_API_DEREG_EVT            */
85     nfa_p2p_accept_connection,              /* NFA_P2P_API_ACCEPT_CONN_EVT      */
86     nfa_p2p_reject_connection,              /* NFA_P2P_API_REJECT_CONN_EVT      */
87     nfa_p2p_disconnect,                     /* NFA_P2P_API_DISCONNECT_EVT       */
88     nfa_p2p_create_data_link_connection,    /* NFA_P2P_API_CONNECT_EVT          */
89     nfa_p2p_send_ui,                        /* NFA_P2P_API_SEND_UI_EVT          */
90     nfa_p2p_send_data,                      /* NFA_P2P_API_SEND_DATA_EVT        */
91     nfa_p2p_set_local_busy,                 /* NFA_P2P_API_SET_LOCAL_BUSY_EVT   */
92     nfa_p2p_get_link_info,                  /* NFA_P2P_API_GET_LINK_INFO_EVT    */
93     nfa_p2p_get_remote_sap,                 /* NFA_P2P_API_GET_REMOTE_SAP_EVT   */
94     nfa_p2p_set_llcp_cfg,                   /* NFA_P2P_API_SET_LLCP_CFG_EVT     */
95     nfa_p2p_restart_rf_discovery            /* NFA_P2P_INT_RESTART_RF_DISC_EVT  */
96 };
97 
98 /*******************************************************************************
99 **
100 ** Function         nfa_p2p_discovery_cback
101 **
102 ** Description      Processing event from discovery callback for listening
103 **
104 **
105 ** Returns          None
106 **
107 *******************************************************************************/
nfa_p2p_discovery_cback(tNFA_DM_RF_DISC_EVT event,tNFC_DISCOVER * p_data)108 void nfa_p2p_discovery_cback (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data)
109 {
110     tNFA_CONN_EVT_DATA evt_data;
111 
112     P2P_TRACE_DEBUG1 ("nfa_p2p_discovery_cback (): event:0x%02X", event);
113 
114     switch (event)
115     {
116     case NFA_DM_RF_DISC_START_EVT:
117         if (p_data->status == NFC_STATUS_OK)
118         {
119             nfa_p2p_cb.llcp_state    = NFA_P2P_LLCP_STATE_LISTENING;
120             nfa_p2p_cb.rf_disc_state = NFA_DM_RFST_DISCOVERY;
121         }
122         break;
123 
124     case NFA_DM_RF_DISC_ACTIVATED_EVT:
125 
126         nfa_p2p_cb.rf_disc_state = NFA_DM_RFST_LISTEN_ACTIVE;
127 
128         /* notify NFC link activation */
129         memcpy (&(evt_data.activated.activate_ntf),
130                 &(p_data->activate),
131                 sizeof (tNFC_ACTIVATE_DEVT));
132         nfa_dm_conn_cback_event_notify (NFA_ACTIVATED_EVT, &evt_data);
133 
134         if ((p_data->activate.protocol        == NFC_PROTOCOL_NFC_DEP)
135           &&(p_data->activate.intf_param.type == NFC_INTERFACE_NFC_DEP))
136         {
137             nfa_p2p_activate_llcp (p_data);
138 
139             /* stop timer not to deactivate LLCP link on passive mode */
140             nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
141         }
142         break;
143 
144     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
145 
146         if (  (nfa_p2p_cb.rf_disc_state != NFA_DM_RFST_LISTEN_ACTIVE)
147             &&(nfa_p2p_cb.rf_disc_state != NFA_DM_RFST_LISTEN_SLEEP)  )
148         {
149             /* this is not for P2P listen
150             ** DM broadcasts deactivaiton event in listen sleep state.
151             */
152             break;
153         }
154 
155         /* notify deactivation */
156         if (  (p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP)
157             ||(p_data->deactivate.type == NFC_DEACTIVATE_TYPE_SLEEP_AF)  )
158         {
159             nfa_p2p_cb.rf_disc_state  = NFA_DM_RFST_LISTEN_SLEEP;
160             evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_SLEEP;
161         }
162         else
163         {
164             nfa_p2p_cb.rf_disc_state  = NFA_DM_RFST_DISCOVERY;
165             evt_data.deactivated.type = NFA_DEACTIVATE_TYPE_IDLE;
166         }
167         nfa_dm_conn_cback_event_notify (NFA_DEACTIVATED_EVT, &evt_data);
168         break;
169 
170     default:
171         P2P_TRACE_ERROR0 ("Unexpected event");
172         break;
173     }
174 }
175 
176 /*******************************************************************************
177 **
178 ** Function         nfa_p2p_update_active_listen_timeout_cback
179 **
180 ** Description      Timeout while waiting for passive mode activation
181 **
182 ** Returns          void
183 **
184 *******************************************************************************/
nfa_p2p_update_active_listen_timeout_cback(TIMER_LIST_ENT * p_tle)185 static void nfa_p2p_update_active_listen_timeout_cback (TIMER_LIST_ENT *p_tle)
186 {
187     NFA_TRACE_ERROR0 ("nfa_p2p_update_active_listen_timeout_cback()");
188 
189     /* restore active listen mode */
190     nfa_p2p_update_active_listen ();
191 }
192 
193 /*******************************************************************************
194 **
195 ** Function         nfa_p2p_update_active_listen
196 **
197 ** Description      Remove active listen mode temporarily or restore it
198 **
199 **
200 ** Returns          None
201 **
202 *******************************************************************************/
nfa_p2p_update_active_listen(void)203 static void nfa_p2p_update_active_listen (void)
204 {
205     tNFA_DM_DISC_TECH_PROTO_MASK p2p_listen_mask = 0;
206     BT_HDR *p_msg;
207 
208     P2P_TRACE_DEBUG1 ("nfa_p2p_update_active_listen (): listen_tech_mask_to_restore:0x%x",
209                        nfa_p2p_cb.listen_tech_mask_to_restore);
210 
211     /* if active listen mode was removed temporarily */
212     if (nfa_p2p_cb.listen_tech_mask_to_restore)
213     {
214         /* restore listen technologies */
215         nfa_p2p_cb.listen_tech_mask = nfa_p2p_cb.listen_tech_mask_to_restore;
216         nfa_p2p_cb.listen_tech_mask_to_restore = 0;
217         nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
218     }
219     else
220     {
221         /* start timer in case of no passive activation */
222         nfa_p2p_cb.active_listen_restore_timer.p_cback = (TIMER_CBACK *)nfa_p2p_update_active_listen_timeout_cback;
223         nfa_sys_start_timer (&nfa_p2p_cb.active_listen_restore_timer, 0, NFA_P2P_RESTORE_ACTIVE_LISTEN_TIMEOUT);
224 
225         /* save listen techonologies */
226         nfa_p2p_cb.listen_tech_mask_to_restore = nfa_p2p_cb.listen_tech_mask;
227 
228         /* remove active listen mode */
229         nfa_p2p_cb.listen_tech_mask &= ~( NFA_TECHNOLOGY_MASK_A_ACTIVE|NFA_TECHNOLOGY_MASK_F_ACTIVE);
230     }
231 
232     if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
233     {
234         nfa_dm_delete_rf_discover (nfa_p2p_cb.dm_disc_handle);
235         nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
236     }
237 
238     /* collect listen technologies with NFC-DEP protocol */
239     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A)
240         p2p_listen_mask |= NFA_DM_DISC_MASK_LA_NFC_DEP;
241 
242     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F)
243         p2p_listen_mask |= NFA_DM_DISC_MASK_LF_NFC_DEP;
244 
245     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE)
246         p2p_listen_mask |= NFA_DM_DISC_MASK_LAA_NFC_DEP;
247 
248     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE)
249         p2p_listen_mask |= NFA_DM_DISC_MASK_LFA_NFC_DEP;
250 
251     /* Configure listen technologies and protocols and register callback to NFA DM discovery */
252     nfa_p2p_cb.dm_disc_handle = nfa_dm_add_rf_discover (p2p_listen_mask,
253                                                         NFA_DM_DISC_HOST_ID_DH,
254                                                         nfa_p2p_discovery_cback);
255 
256     /* restart RF discovery to update RF technologies */
257     if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof(BT_HDR))) != NULL)
258     {
259         p_msg->event = NFA_P2P_INT_RESTART_RF_DISC_EVT;
260         nfa_sys_sendmsg (p_msg);
261     }
262 }
263 
264 /*******************************************************************************
265 **
266 ** Function         nfa_p2p_llcp_link_cback
267 **
268 ** Description      Processing event from LLCP link management callback
269 **
270 **
271 ** Returns          None
272 **
273 *******************************************************************************/
nfa_p2p_llcp_link_cback(UINT8 event,UINT8 reason)274 void nfa_p2p_llcp_link_cback (UINT8 event, UINT8 reason)
275 {
276     tNFA_LLCP_ACTIVATED     llcp_activated;
277     tNFA_LLCP_DEACTIVATED   llcp_deactivated;
278 
279     P2P_TRACE_DEBUG2 ("nfa_p2p_llcp_link_cback () event:0x%x, reason:0x%x", event, reason);
280 
281     if (event == LLCP_LINK_ACTIVATION_COMPLETE_EVT)
282     {
283         LLCP_GetLinkMIU (&nfa_p2p_cb.local_link_miu, &nfa_p2p_cb.remote_link_miu);
284         nfa_p2p_cb.llcp_state = NFA_P2P_LLCP_STATE_ACTIVATED;
285 
286         if (nfa_p2p_cb.is_initiator)
287         {
288             /* notify NFA DM to send Activate Event to applicaiton with status  */
289             nfa_dm_notify_activation_status (NFA_STATUS_OK, NULL);
290         }
291 
292         llcp_activated.is_initiator    = nfa_p2p_cb.is_initiator;
293         llcp_activated.local_link_miu  = nfa_p2p_cb.local_link_miu;
294         llcp_activated.remote_link_miu = nfa_p2p_cb.remote_link_miu;
295         llcp_activated.remote_lsc      = LLCP_GetRemoteLSC ();
296         llcp_activated.remote_wks      = LLCP_GetRemoteWKS ();
297         llcp_activated.remote_version  = LLCP_GetRemoteVersion ();
298 
299         nfa_dm_act_conn_cback_notify (NFA_LLCP_ACTIVATED_EVT, (tNFA_CONN_EVT_DATA *) &llcp_activated);
300 
301     }
302     else if (event == LLCP_LINK_ACTIVATION_FAILED_EVT)
303     {
304         nfa_p2p_cb.llcp_state = NFA_P2P_LLCP_STATE_IDLE;
305 
306         if (nfa_p2p_cb.is_initiator)
307         {
308             /* notify NFA DM to send Activate Event to applicaiton with status  */
309             nfa_dm_notify_activation_status (NFA_STATUS_FAILED, NULL);
310         }
311 
312         nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
313     }
314     else if (event == LLCP_LINK_FIRST_PACKET_RECEIVED_EVT)
315     {
316         nfa_dm_act_conn_cback_notify (NFA_LLCP_FIRST_PACKET_RECEIVED_EVT, NULL);
317     }
318     else /* LLCP_LINK_DEACTIVATED_EVT       */
319     {
320         nfa_p2p_cb.llcp_state = NFA_P2P_LLCP_STATE_IDLE;
321 
322         /* if got RF link loss without any rx LLC PDU */
323         if (reason == LLCP_LINK_RF_LINK_LOSS_NO_RX_LLC)
324         {
325             /* if it was active listen mode */
326             if (  (nfa_p2p_cb.is_active_mode)
327                 &&(!nfa_p2p_cb.is_initiator))
328             {
329                 /* if it didn't retry without active listen mode and passive mode is available */
330                 if (  (nfa_p2p_cb.listen_tech_mask_to_restore == 0x00)
331                     &&(nfa_p2p_cb.listen_tech_mask & ( NFA_TECHNOLOGY_MASK_A
332                                                       |NFA_TECHNOLOGY_MASK_F)))
333                 {
334                     P2P_TRACE_DEBUG0 ("Retry without active listen mode");
335 
336                     /* retry without active listen mode */
337                     nfa_p2p_update_active_listen ();
338                 }
339             }
340             else if (nfa_p2p_cb.listen_tech_mask_to_restore)
341             {
342                 nfa_sys_start_timer (&nfa_p2p_cb.active_listen_restore_timer, 0, NFA_P2P_RESTORE_ACTIVE_LISTEN_TIMEOUT);
343             }
344 
345             reason = LLCP_LINK_RF_LINK_LOSS_ERR;
346         }
347         else
348         {
349             if (nfa_p2p_cb.listen_tech_mask_to_restore)
350             {
351                 /* restore active listen mode */
352                 nfa_p2p_update_active_listen ();
353             }
354         }
355 
356         llcp_deactivated.reason = reason;
357         nfa_dm_act_conn_cback_notify (NFA_LLCP_DEACTIVATED_EVT, (tNFA_CONN_EVT_DATA *)&llcp_deactivated);
358 
359         if (reason != LLCP_LINK_RF_LINK_LOSS_ERR) /* if NFC link is still up */
360         {
361             if (nfa_p2p_cb.is_initiator)
362             {
363                 nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
364             }
365             else if ((nfa_p2p_cb.is_active_mode) && (reason == LLCP_LINK_TIMEOUT))
366             {
367                 /*
368                 ** target needs to trun off RF in case of receiving invalid frame from initiator
369                 */
370                 P2P_TRACE_DEBUG0 ("Got LLCP_LINK_TIMEOUT in active mode on target");
371                 nfa_dm_rf_deactivate (NFA_DEACTIVATE_TYPE_DISCOVERY);
372             }
373         }
374     }
375 }
376 
377 /*******************************************************************************
378 **
379 ** Function         nfa_p2p_activate_llcp
380 **
381 ** Description      Activate LLCP link
382 **
383 **
384 ** Returns          None
385 **
386 *******************************************************************************/
nfa_p2p_activate_llcp(tNFC_DISCOVER * p_data)387 void nfa_p2p_activate_llcp (tNFC_DISCOVER *p_data)
388 {
389     tLLCP_ACTIVATE_CONFIG config;
390 
391     P2P_TRACE_DEBUG0 ("nfa_p2p_activate_llcp ()");
392 
393     if (  (p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A)
394         ||(p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_F)
395         ||(p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A_ACTIVE)
396         ||(p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_F_ACTIVE)  )
397     {
398         config.is_initiator = TRUE;
399     }
400     else
401     {
402         config.is_initiator = FALSE;
403     }
404 
405     if (  (p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_A_ACTIVE)
406         ||(p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_POLL_F_ACTIVE)
407         ||(p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_LISTEN_A_ACTIVE)
408         ||(p_data->activate.rf_tech_param.mode == NFC_DISCOVERY_TYPE_LISTEN_F_ACTIVE)  )
409     {
410         nfa_p2p_cb.is_active_mode = TRUE;
411     }
412     else
413     {
414         nfa_p2p_cb.is_active_mode = FALSE;
415     }
416 
417     nfa_p2p_cb.is_initiator = config.is_initiator;
418 
419     config.max_payload_size = p_data->activate.intf_param.intf_param.pa_nfc.max_payload_size;
420     config.waiting_time     = p_data->activate.intf_param.intf_param.pa_nfc.waiting_time;
421     config.p_gen_bytes      = p_data->activate.intf_param.intf_param.pa_nfc.gen_bytes;
422     config.gen_bytes_len    = p_data->activate.intf_param.intf_param.pa_nfc.gen_bytes_len;
423 
424     LLCP_ActivateLink (config, nfa_p2p_llcp_link_cback);
425 }
426 
427 /*******************************************************************************
428 **
429 ** Function         nfa_p2p_deactivate_llcp
430 **
431 ** Description      Deactivate LLCP link
432 **
433 **
434 ** Returns          None
435 **
436 *******************************************************************************/
nfa_p2p_deactivate_llcp(void)437 void nfa_p2p_deactivate_llcp (void)
438 {
439     P2P_TRACE_DEBUG0 ("nfa_p2p_deactivate_llcp ()");
440 
441     LLCP_DeactivateLink ();
442 }
443 
444 /*******************************************************************************
445 **
446 ** Function         nfa_p2p_init
447 **
448 ** Description      Initialize NFA P2P
449 **
450 **
451 ** Returns          None
452 **
453 *******************************************************************************/
nfa_p2p_init(void)454 void nfa_p2p_init (void)
455 {
456     UINT8 xx;
457 
458     P2P_TRACE_DEBUG0 ("nfa_p2p_init ()");
459 
460     /* initialize control block */
461     memset (&nfa_p2p_cb, 0, sizeof (tNFA_P2P_CB));
462     nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
463     nfa_p2p_cb.trace_level    = APPL_INITIAL_TRACE_LEVEL;
464 
465     for (xx = 0; xx < LLCP_MAX_SDP_TRANSAC; xx++)
466     {
467         nfa_p2p_cb.sdp_cb[xx].local_sap = LLCP_INVALID_SAP;
468     }
469 
470     /* register message handler on NFA SYS */
471     nfa_sys_register (NFA_ID_P2P,  &nfa_p2p_sys_reg);
472 }
473 
474 
475 /*******************************************************************************
476 **
477 ** Function         nfa_p2p_sys_disable
478 **
479 ** Description      Deregister NFA P2P from NFA SYS/DM
480 **
481 **
482 ** Returns          None
483 **
484 *******************************************************************************/
nfa_p2p_sys_disable(void)485 static void nfa_p2p_sys_disable (void)
486 {
487     P2P_TRACE_DEBUG0 ("nfa_p2p_sys_disable()");
488 
489     nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
490 
491     /* deregister message handler on NFA SYS */
492     nfa_sys_deregister (NFA_ID_P2P);
493 }
494 
495 /*******************************************************************************
496 **
497 ** Function         nfa_p2p_set_config
498 **
499 ** Description      Set General bytes and WT parameters for LLCP
500 **
501 **
502 ** Returns          void
503 **
504 *******************************************************************************/
nfa_p2p_set_config(tNFA_DM_DISC_TECH_PROTO_MASK disc_mask)505 void nfa_p2p_set_config (tNFA_DM_DISC_TECH_PROTO_MASK disc_mask)
506 {
507     UINT8 wt, gen_bytes_len = LLCP_MAX_GEN_BYTES;
508     UINT8 params[LLCP_MAX_GEN_BYTES + 5], *p, length;
509 
510     P2P_TRACE_DEBUG0 ("nfa_p2p_set_config ()");
511 
512     LLCP_GetDiscoveryConfig (&wt, params + 2, &gen_bytes_len);
513     if (nfa_dm_is_p2p_paused ())
514     {
515         gen_bytes_len = 0;
516     }
517 
518     if (disc_mask & ( NFA_DM_DISC_MASK_PA_NFC_DEP
519                      |NFA_DM_DISC_MASK_PF_NFC_DEP
520                      |NFA_DM_DISC_MASK_PAA_NFC_DEP
521                      |NFA_DM_DISC_MASK_PFA_NFC_DEP) )
522     {
523         p = params;
524 
525         UINT8_TO_BE_STREAM (p, NFC_PMID_ATR_REQ_GEN_BYTES);
526         UINT8_TO_BE_STREAM (p, gen_bytes_len);
527 
528         p += gen_bytes_len;
529         length = gen_bytes_len + 2;
530 
531         nfa_dm_check_set_config (length, params, FALSE);
532     }
533 
534     if (disc_mask & ( NFA_DM_DISC_MASK_LA_NFC_DEP
535                      |NFA_DM_DISC_MASK_LF_NFC_DEP
536                      |NFA_DM_DISC_MASK_LAA_NFC_DEP
537                      |NFA_DM_DISC_MASK_LFA_NFC_DEP) )
538     {
539         p = params;
540 
541         UINT8_TO_BE_STREAM (p, NFC_PMID_ATR_RES_GEN_BYTES);
542         UINT8_TO_BE_STREAM (p, gen_bytes_len);
543 
544         p += gen_bytes_len;
545         length = gen_bytes_len + 2;
546 
547         UINT8_TO_BE_STREAM (p, NFC_PMID_WT);
548         UINT8_TO_BE_STREAM (p, NCI_PARAM_LEN_WT);
549         UINT8_TO_BE_STREAM (p, wt);
550 
551         length += 3;
552 
553         nfa_dm_check_set_config (length, params, FALSE);
554     }
555 }
556 
557 /*******************************************************************************
558 **
559 ** Function         nfa_p2p_enable_listening
560 **
561 ** Description      Configure listen technologies and protocols for LLCP
562 **                  If LLCP WKS is changed then LLCP Gen bytes will be updated.
563 **
564 ** Returns          void
565 **
566 *******************************************************************************/
nfa_p2p_enable_listening(tNFA_SYS_ID sys_id,BOOLEAN update_wks)567 void nfa_p2p_enable_listening (tNFA_SYS_ID sys_id, BOOLEAN update_wks)
568 {
569     tNFA_DM_DISC_TECH_PROTO_MASK p2p_listen_mask = 0;
570 
571     P2P_TRACE_DEBUG2 ("nfa_p2p_enable_listening () sys_id = %d, update_wks = %d",
572                        sys_id, update_wks);
573 
574     if (sys_id == NFA_ID_P2P)
575         nfa_p2p_cb.is_p2p_listening = TRUE;
576     else if (sys_id == NFA_ID_CHO)
577         nfa_p2p_cb.is_cho_listening = TRUE;
578     else if (sys_id == NFA_ID_SNEP)
579         nfa_p2p_cb.is_snep_listening = TRUE;
580 
581     if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
582     {
583         /* if need to update WKS in LLCP Gen bytes */
584         if (update_wks)
585         {
586             /* update LLCP Gen Bytes */
587             nfa_p2p_set_config (NFA_DM_DISC_MASK_PA_NFC_DEP|NFA_DM_DISC_MASK_LA_NFC_DEP);
588         }
589         return;
590     }
591 
592     /* collect listen technologies with NFC-DEP protocol */
593     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A)
594         p2p_listen_mask |= NFA_DM_DISC_MASK_LA_NFC_DEP;
595 
596     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F)
597         p2p_listen_mask |= NFA_DM_DISC_MASK_LF_NFC_DEP;
598 
599     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_A_ACTIVE)
600         p2p_listen_mask |= NFA_DM_DISC_MASK_LAA_NFC_DEP;
601 
602     if (nfa_p2p_cb.listen_tech_mask & NFA_TECHNOLOGY_MASK_F_ACTIVE)
603         p2p_listen_mask |= NFA_DM_DISC_MASK_LFA_NFC_DEP;
604 
605     if (p2p_listen_mask)
606     {
607         /* Configure listen technologies and protocols and register callback to NFA DM discovery */
608         nfa_p2p_cb.dm_disc_handle = nfa_dm_add_rf_discover (p2p_listen_mask,
609                                                             NFA_DM_DISC_HOST_ID_DH,
610                                                             nfa_p2p_discovery_cback);
611     }
612 }
613 
614 /*******************************************************************************
615 **
616 ** Function         nfa_p2p_disable_listening
617 **
618 ** Description      Remove listen technologies and protocols for LLCP and
619 **                  deregister callback from NFA DM discovery if all of P2P/CHO/SNEP
620 **                  doesn't listen LLCP any more.
621 **                  If LLCP WKS is changed then ATR_RES will be updated.
622 **
623 ** Returns          void
624 **
625 *******************************************************************************/
nfa_p2p_disable_listening(tNFA_SYS_ID sys_id,BOOLEAN update_wks)626 void nfa_p2p_disable_listening (tNFA_SYS_ID sys_id, BOOLEAN update_wks)
627 {
628 
629     P2P_TRACE_DEBUG2 ("nfa_p2p_disable_listening ()  sys_id = %d, update_wks = %d",
630                        sys_id, update_wks);
631 
632     if (sys_id == NFA_ID_P2P)
633         nfa_p2p_cb.is_p2p_listening = FALSE;
634     else if (sys_id == NFA_ID_CHO)
635         nfa_p2p_cb.is_cho_listening = FALSE;
636     else if (sys_id == NFA_ID_SNEP)
637         nfa_p2p_cb.is_snep_listening = FALSE;
638 
639     if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
640     {
641         if (  (nfa_p2p_cb.is_p2p_listening == FALSE)
642             &&(nfa_p2p_cb.is_cho_listening == FALSE)
643             &&(nfa_p2p_cb.is_snep_listening == FALSE)  )
644         {
645             nfa_p2p_cb.llcp_state    = NFA_P2P_LLCP_STATE_IDLE;
646             nfa_p2p_cb.rf_disc_state = NFA_DM_RFST_IDLE;
647 
648             nfa_dm_delete_rf_discover (nfa_p2p_cb.dm_disc_handle);
649             nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
650         }
651         else if (update_wks)
652         {
653             /* update LLCP Gen Bytes */
654             nfa_p2p_set_config (NFA_DM_DISC_MASK_PA_NFC_DEP|NFA_DM_DISC_MASK_LA_NFC_DEP);
655         }
656     }
657 }
658 
659 /*******************************************************************************
660 **
661 ** Function         nfa_p2p_update_listen_tech
662 **
663 ** Description      Update P2P listen technologies. If there is change then
664 **                  restart or stop P2P listen.
665 **
666 ** Returns          void
667 **
668 *******************************************************************************/
nfa_p2p_update_listen_tech(tNFA_TECHNOLOGY_MASK tech_mask)669 void nfa_p2p_update_listen_tech (tNFA_TECHNOLOGY_MASK tech_mask)
670 {
671     P2P_TRACE_DEBUG1 ("nfa_p2p_update_listen_tech ()  tech_mask = 0x%x", tech_mask);
672 
673     if (nfa_p2p_cb.listen_tech_mask_to_restore)
674     {
675         nfa_p2p_cb.listen_tech_mask_to_restore = 0;
676         nfa_sys_stop_timer (&nfa_p2p_cb.active_listen_restore_timer);
677     }
678 
679     if (nfa_p2p_cb.listen_tech_mask != tech_mask)
680     {
681         nfa_p2p_cb.listen_tech_mask = tech_mask;
682 
683         if (nfa_p2p_cb.dm_disc_handle != NFA_HANDLE_INVALID)
684         {
685             nfa_p2p_cb.rf_disc_state = NFA_DM_RFST_IDLE;
686 
687             nfa_dm_delete_rf_discover (nfa_p2p_cb.dm_disc_handle);
688             nfa_p2p_cb.dm_disc_handle = NFA_HANDLE_INVALID;
689         }
690 
691         /* restart discovery without updating sub-module status */
692         if (nfa_p2p_cb.is_p2p_listening)
693             nfa_p2p_enable_listening (NFA_ID_P2P, FALSE);
694         else if (nfa_p2p_cb.is_cho_listening)
695             nfa_p2p_enable_listening (NFA_ID_CHO, FALSE);
696         else if (nfa_p2p_cb.is_snep_listening)
697             nfa_p2p_enable_listening (NFA_ID_SNEP, FALSE);
698     }
699 }
700 
701 /*******************************************************************************
702 **
703 ** Function         nfa_p2p_evt_hdlr
704 **
705 ** Description      Processing event for NFA P2P
706 **
707 **
708 ** Returns          TRUE if p_msg needs to be deallocated
709 **
710 *******************************************************************************/
nfa_p2p_evt_hdlr(BT_HDR * p_hdr)711 static BOOLEAN nfa_p2p_evt_hdlr (BT_HDR *p_hdr)
712 {
713     BOOLEAN delete_msg = TRUE;
714     UINT16  event;
715 
716     tNFA_P2P_MSG *p_msg = (tNFA_P2P_MSG *)p_hdr;
717 
718 #if (BT_TRACE_VERBOSE == TRUE)
719     P2P_TRACE_DEBUG2 ("nfa_p2p_evt_hdlr (): LLCP State [%s], Event [%s]",
720                        nfa_p2p_llcp_state_code (nfa_p2p_cb.llcp_state),
721                        nfa_p2p_evt_code (p_msg->hdr.event));
722 #else
723     P2P_TRACE_DEBUG2 ("nfa_p2p_evt_hdlr (): State 0x%02x, Event 0x%02x",
724                        nfa_p2p_cb.llcp_state, p_msg->hdr.event);
725 #endif
726 
727     event = p_msg->hdr.event & 0x00ff;
728 
729     /* execute action functions */
730     if (event < NFA_P2P_NUM_ACTIONS)
731     {
732         delete_msg = (*nfa_p2p_action[event]) (p_msg);
733     }
734     else
735     {
736         P2P_TRACE_ERROR0 ("Unhandled event");
737     }
738 
739     return delete_msg;
740 }
741 
742 
743 #if (BT_TRACE_VERBOSE == TRUE)
744 /*******************************************************************************
745 **
746 ** Function         nfa_p2p_llcp_state_code
747 **
748 ** Description
749 **
750 ** Returns          string of state
751 **
752 *******************************************************************************/
nfa_p2p_llcp_state_code(tNFA_P2P_LLCP_STATE state_code)753 static char *nfa_p2p_llcp_state_code (tNFA_P2P_LLCP_STATE state_code)
754 {
755     switch (state_code)
756     {
757     case NFA_P2P_LLCP_STATE_IDLE:
758         return "Link IDLE";
759     case NFA_P2P_LLCP_STATE_LISTENING:
760         return "Link LISTENING";
761     case NFA_P2P_LLCP_STATE_ACTIVATED:
762         return "Link ACTIVATED";
763     default:
764         return "Unknown state";
765     }
766 }
767 
768 /*******************************************************************************
769 **
770 ** Function         nfa_p2p_evt_code
771 **
772 ** Description
773 **
774 ** Returns          string of event
775 **
776 *******************************************************************************/
nfa_p2p_evt_code(UINT16 evt_code)777 char *nfa_p2p_evt_code (UINT16 evt_code)
778 {
779     switch (evt_code)
780     {
781     case NFA_P2P_API_REG_SERVER_EVT:
782         return "API_REG_SERVER";
783     case NFA_P2P_API_REG_CLIENT_EVT:
784         return "API_REG_CLIENT";
785     case NFA_P2P_API_DEREG_EVT:
786         return "API_DEREG";
787     case NFA_P2P_API_ACCEPT_CONN_EVT:
788         return "API_ACCEPT_CONN";
789     case NFA_P2P_API_REJECT_CONN_EVT:
790         return "API_REJECT_CONN";
791     case NFA_P2P_API_DISCONNECT_EVT:
792         return "API_DISCONNECT";
793     case NFA_P2P_API_CONNECT_EVT:
794         return "API_CONNECT";
795     case NFA_P2P_API_SEND_UI_EVT:
796         return "API_SEND_UI";
797     case NFA_P2P_API_SEND_DATA_EVT:
798         return "API_SEND_DATA";
799     case NFA_P2P_API_SET_LOCAL_BUSY_EVT:
800         return "API_SET_LOCAL_BUSY";
801     case NFA_P2P_API_GET_LINK_INFO_EVT:
802         return "API_GET_LINK_INFO";
803     case NFA_P2P_API_GET_REMOTE_SAP_EVT:
804         return "API_GET_REMOTE_SAP";
805     case NFA_P2P_API_SET_LLCP_CFG_EVT:
806         return "API_SET_LLCP_CFG_EVT";
807     case NFA_P2P_INT_RESTART_RF_DISC_EVT:
808         return "RESTART_RF_DISC_EVT";
809     default:
810         return "Unknown event";
811     }
812 }
813 #endif  /* Debug Functions */
814