nfa_dm_api.c revision 09de1b298ad4237ba50018d28086c0430da8c23e
1/******************************************************************************
2 *
3 *  Copyright (C) 2010-2013 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 *  NFA interface for device management
23 *
24 ******************************************************************************/
25#include <string.h>
26#include "nfa_api.h"
27#include "nfa_sys.h"
28#include "nfa_dm_int.h"
29#include "nfa_ce_int.h"
30#include "nfa_sys_int.h"
31#include "ndef_utils.h"
32
33/*****************************************************************************
34**  Constants
35*****************************************************************************/
36
37/*****************************************************************************
38**  APIs
39*****************************************************************************/
40/*******************************************************************************
41**
42** Function         NFA_Init
43**
44** Description      This function initializes control blocks for NFA
45**
46**                  p_hal_entry_tbl points to a table of HAL entry points
47**
48**                  NOTE: the buffer that p_hal_entry_tbl points must be
49**                  persistent until NFA is disabled.
50**
51** Returns          none
52**
53*******************************************************************************/
54void NFA_Init(tHAL_NFC_ENTRY *p_hal_entry_tbl)
55{
56    NFA_TRACE_API0 ("NFA_Init ()");
57    nfa_sys_init();
58    nfa_dm_init();
59    nfa_p2p_init();
60    nfa_cho_init();
61    nfa_snep_init(FALSE);
62    nfa_rw_init();
63    nfa_ce_init();
64    nfa_ee_init();
65    if (nfa_ee_max_ee_cfg != 0)
66    {
67        nfa_dm_cb.get_max_ee    = p_hal_entry_tbl->get_max_ee;
68        nfa_hci_init();
69    }
70
71
72    /* Initialize NFC module */
73    NFC_Init (p_hal_entry_tbl);
74}
75
76/*******************************************************************************
77**
78** Function         NFA_Enable
79**
80** Description      This function enables NFC. Prior to calling NFA_Enable,
81**                  the NFCC must be powered up, and ready to receive commands.
82**                  This function enables the tasks needed by NFC, opens the NCI
83**                  transport, resets the NFC controller, downloads patches to
84**                  the NFCC (if necessary), and initializes the NFC subsystems.
85**
86**                  This function should only be called once - typically when NFC
87**                  is enabled during boot-up, or when NFC is enabled from a
88**                  settings UI. Subsequent calls to NFA_Enable while NFA is
89**                  enabling or enabled will be ignored. When the NFC startup
90**                  procedure is completed, an NFA_DM_ENABLE_EVT is returned to the
91**                  application using the tNFA_DM_CBACK.
92**
93** Returns          NFA_STATUS_OK if successfully initiated
94**                  NFA_STATUS_FAILED otherwise
95**
96*******************************************************************************/
97tNFA_STATUS NFA_Enable (tNFA_DM_CBACK        *p_dm_cback,
98                        tNFA_CONN_CBACK      *p_conn_cback)
99{
100    tNFA_DM_API_ENABLE *p_msg;
101
102    NFA_TRACE_API0 ("NFA_Enable ()");
103
104    /* Validate parameters */
105    if ((!p_dm_cback) || (!p_conn_cback))
106    {
107        NFA_TRACE_ERROR0 ("NFA_Enable (): error null callback");
108        return (NFA_STATUS_FAILED);
109    }
110
111    if ((p_msg = (tNFA_DM_API_ENABLE *) GKI_getbuf (sizeof (tNFA_DM_API_ENABLE))) != NULL)
112    {
113        p_msg->hdr.event    = NFA_DM_API_ENABLE_EVT;
114        p_msg->p_dm_cback   = p_dm_cback;
115        p_msg->p_conn_cback = p_conn_cback;
116
117        nfa_sys_sendmsg (p_msg);
118
119        return (NFA_STATUS_OK);
120    }
121
122    return (NFA_STATUS_FAILED);
123}
124
125/*******************************************************************************
126**
127** Function         NFA_Disable
128**
129** Description      This function is called to shutdown NFC. The tasks for NFC
130**                  are terminated, and clean up routines are performed. This
131**                  function is typically called during platform shut-down, or
132**                  when NFC is disabled from a settings UI. When the NFC
133**                  shutdown procedure is completed, an NFA_DM_DISABLE_EVT is
134**                  returned to the application using the tNFA_DM_CBACK.
135**
136**                  The platform should wait until the NFC_DISABLE_REVT is
137**                  received before powering down the NFC chip and NCI transport.
138**                  This is required to so that NFA can gracefully shut down any
139**                  open connections.
140**
141** Returns          NFA_STATUS_OK if successfully initiated
142**                  NFA_STATUS_FAILED otherwise
143**
144*******************************************************************************/
145tNFA_STATUS NFA_Disable (BOOLEAN graceful)
146{
147    tNFA_DM_API_DISABLE *p_msg;
148
149    NFA_TRACE_API1 ("NFA_Disable (graceful=%i)", graceful);
150
151    if ((p_msg = (tNFA_DM_API_DISABLE *) GKI_getbuf (sizeof (tNFA_DM_API_DISABLE))) != NULL)
152    {
153        p_msg->hdr.event = NFA_DM_API_DISABLE_EVT;
154        p_msg->graceful  = graceful;
155
156        nfa_sys_sendmsg (p_msg);
157
158        return (NFA_STATUS_OK);
159    }
160
161    return (NFA_STATUS_FAILED);
162}
163
164/*******************************************************************************
165**
166** Function         NFA_SetConfig
167**
168** Description      Set the configuration parameters to NFCC. The result is
169**                  reported with an NFA_DM_SET_CONFIG_EVT in the tNFA_DM_CBACK
170**                  callback.
171**
172** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
173**                  should happen before calling this function. Most Configuration
174**                  parameters are related to RF discovery.
175**
176** Returns          NFA_STATUS_OK if successfully initiated
177**                  NFA_STATUS_BUSY if previous setting is on-going
178**                  NFA_STATUS_FAILED otherwise
179**
180*******************************************************************************/
181tNFA_STATUS NFA_SetConfig (tNFA_PMID param_id,
182                           UINT8     length,
183                           UINT8    *p_data)
184{
185    tNFA_DM_API_SET_CONFIG *p_msg;
186
187    NFA_TRACE_API1 ("NFA_SetConfig (): param_id:0x%X", param_id);
188
189    if ((p_msg = (tNFA_DM_API_SET_CONFIG *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_SET_CONFIG) + length))) != NULL)
190    {
191        p_msg->hdr.event = NFA_DM_API_SET_CONFIG_EVT;
192
193        p_msg->param_id = param_id;
194        p_msg->length   = length;
195        p_msg->p_data   = (UINT8 *) (p_msg + 1);
196
197        /* Copy parameter data */
198        memcpy (p_msg->p_data, p_data, length);
199
200        nfa_sys_sendmsg (p_msg);
201
202        return (NFA_STATUS_OK);
203    }
204
205    return (NFA_STATUS_FAILED);
206}
207
208/*******************************************************************************
209**
210** Function         NFA_GetConfig
211**
212** Description      Get the configuration parameters from NFCC. The result is
213**                  reported with an NFA_DM_GET_CONFIG_EVT in the tNFA_DM_CBACK
214**                  callback.
215**
216** Returns          NFA_STATUS_OK if successfully initiated
217**                  NFA_STATUS_FAILED otherwise
218**
219*******************************************************************************/
220tNFA_STATUS NFA_GetConfig (UINT8 num_ids,
221                           tNFA_PMID *p_param_ids)
222{
223    tNFA_DM_API_GET_CONFIG *p_msg;
224
225    NFA_TRACE_API1 ("NFA_GetConfig (): num_ids: %i", num_ids);
226
227
228    if ((p_msg = (tNFA_DM_API_GET_CONFIG *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_GET_CONFIG) + num_ids))) != NULL)
229    {
230        p_msg->hdr.event = NFA_DM_API_GET_CONFIG_EVT;
231
232        p_msg->num_ids = num_ids;
233        p_msg->p_pmids = (tNFA_PMID *) (p_msg+1);
234
235        /* Copy the param IDs */
236        memcpy (p_msg->p_pmids, p_param_ids, num_ids);
237
238        nfa_sys_sendmsg (p_msg);
239
240        return (NFA_STATUS_OK);
241    }
242
243    return (NFA_STATUS_FAILED);
244}
245
246/*******************************************************************************
247**
248** Function         NFA_RequestExclusiveRfControl
249**
250** Description      Request exclusive control of NFC.
251**                  - Previous behavior (polling/tag reading, DH card emulation)
252**                    will be suspended .
253**                  - Polling and listening will be done based on the specified
254**                    params
255**
256**                  The NFA_EXCLUSIVE_RF_CONTROL_STARTED_EVT event of
257**                  tNFA_CONN_CBACK indicates the status of the operation.
258**
259**                  NFA_ACTIVATED_EVT and NFA_DEACTIVATED_EVT indicates link
260**                  activation/deactivation.
261**
262**                  NFA_SendRawFrame is used to send data to the peer. NFA_DATA_EVT
263**                  indicates data from the peer.
264**
265**                  If a tag is activated, then the NFA_RW APIs may be used to
266**                  send commands to the tag. Incoming NDEF messages are sent to
267**                  the NDEF callback.
268**
269**                  Once exclusive RF control has started, NFA will not activate
270**                  LLCP internally. The application has exclusive control of
271**                  the link.
272**
273** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
274**                  should happen before calling this function
275**
276** Returns          NFA_STATUS_OK if successfully initiated
277**                  NFA_STATUS_FAILED otherwise
278**
279*******************************************************************************/
280tNFA_STATUS NFA_RequestExclusiveRfControl  (tNFA_TECHNOLOGY_MASK poll_mask,
281                                            tNFA_LISTEN_CFG      *p_listen_cfg,
282                                            tNFA_CONN_CBACK      *p_conn_cback,
283                                            tNFA_NDEF_CBACK      *p_ndef_cback)
284{
285    tNFA_DM_API_REQ_EXCL_RF_CTRL *p_msg;
286
287    NFA_TRACE_API1 ("NFA_RequestExclusiveRfControl () poll_mask=0x%x", poll_mask);
288
289    if (!p_conn_cback)
290    {
291        NFA_TRACE_ERROR0 ("NFA_RequestExclusiveRfControl (): error null callback");
292        return (NFA_STATUS_FAILED);
293    }
294
295    if ((p_msg = (tNFA_DM_API_REQ_EXCL_RF_CTRL *) GKI_getbuf (sizeof (tNFA_DM_API_REQ_EXCL_RF_CTRL))) != NULL)
296    {
297        p_msg->hdr.event = NFA_DM_API_REQUEST_EXCL_RF_CTRL_EVT;
298        p_msg->poll_mask    = poll_mask;
299        p_msg->p_conn_cback = p_conn_cback;
300        p_msg->p_ndef_cback = p_ndef_cback;
301
302        if (p_listen_cfg)
303            memcpy (&p_msg->listen_cfg, p_listen_cfg, sizeof (tNFA_LISTEN_CFG));
304        else
305            memset (&p_msg->listen_cfg, 0x00, sizeof (tNFA_LISTEN_CFG));
306
307        nfa_sys_sendmsg (p_msg);
308
309        return (NFA_STATUS_OK);
310    }
311
312    return (NFA_STATUS_FAILED);
313}
314
315/*******************************************************************************
316**
317** Function         NFA_ReleaseExclusiveRfControl
318**
319** Description      Release exclusive control of NFC. Once released, behavior
320**                  prior to obtaining exclusive RF control will resume.
321**
322** Returns          NFA_STATUS_OK if successfully initiated
323**                  NFA_STATUS_FAILED otherwise
324**
325*******************************************************************************/
326tNFA_STATUS NFA_ReleaseExclusiveRfControl (void)
327{
328    BT_HDR *p_msg;
329
330    NFA_TRACE_API0 ("NFA_ReleaseExclusiveRfControl ()");
331
332    if (!nfa_dm_cb.p_excl_conn_cback)
333    {
334        NFA_TRACE_ERROR0 ("NFA_ReleaseExclusiveRfControl (): Exclusive rf control is not in progress");
335        return (NFA_STATUS_FAILED);
336    }
337
338    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
339    {
340        p_msg->event = NFA_DM_API_RELEASE_EXCL_RF_CTRL_EVT;
341        nfa_sys_sendmsg (p_msg);
342        return (NFA_STATUS_OK);
343    }
344
345    return (NFA_STATUS_FAILED);
346}
347
348
349/*******************************************************************************
350**
351** Function         NFA_EnablePolling
352**
353** Description      Enable polling for technologies specified by poll_mask.
354**
355**                  The following events (notified using the connection
356**                  callback registered with NFA_Enable) are generated during
357**                  polling:
358**
359**                  - NFA_POLL_ENABLED_EVT indicates whether or not polling
360**                    successfully enabled.
361**                  - NFA_DISC_RESULT_EVT indicates there are more than one devices,
362**                    so application must select one of tags by calling NFA_Select().
363**                  - NFA_SELECT_RESULT_EVT indicates whether previous selection was
364**                    successful or not. If it was failed then application must select
365**                    again or deactivate by calling NFA_Deactivate().
366**                  - NFA_ACTIVATED_EVT is generated when an NFC link is activated.
367**                  - NFA_NDEF_DETECT_EVT is generated if tag is activated
368**                  - NFA_LLCP_ACTIVATED_EVT/NFA_LLCP_DEACTIVATED_EVT is generated
369**                    if NFC-DEP is activated
370**                  - NFA_DEACTIVATED_EVT will be returned after deactivating NFC link.
371**
372** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
373**                  should happen before calling this function
374**
375** Returns          NFA_STATUS_OK if successfully initiated
376**                  NFA_STATUS_FAILED otherwise
377**
378*******************************************************************************/
379tNFA_STATUS NFA_EnablePolling (tNFA_TECHNOLOGY_MASK poll_mask)
380{
381    tNFA_DM_API_ENABLE_POLL *p_msg;
382
383    NFA_TRACE_API1 ("NFA_EnablePolling () 0x%X", poll_mask);
384
385    if ((p_msg = (tNFA_DM_API_ENABLE_POLL *) GKI_getbuf (sizeof (tNFA_DM_API_ENABLE_POLL))) != NULL)
386    {
387        p_msg->hdr.event = NFA_DM_API_ENABLE_POLLING_EVT;
388        p_msg->poll_mask = poll_mask;
389
390        nfa_sys_sendmsg (p_msg);
391
392        return (NFA_STATUS_OK);
393    }
394
395    return (NFA_STATUS_FAILED);
396}
397
398/*******************************************************************************
399**
400** Function         NFA_DisablePolling
401**
402** Description      Disable polling
403**                  NFA_POLL_DISABLED_EVT will be returned after stopping polling.
404**
405** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
406**                  should happen before calling this function
407**
408** Returns          NFA_STATUS_OK if successfully initiated
409**                  NFA_STATUS_FAILED otherwise
410**
411*******************************************************************************/
412tNFA_STATUS NFA_DisablePolling (void)
413{
414    BT_HDR *p_msg;
415
416    NFA_TRACE_API0 ("NFA_DisablePolling ()");
417
418    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
419    {
420        p_msg->event = NFA_DM_API_DISABLE_POLLING_EVT;
421
422        nfa_sys_sendmsg (p_msg);
423
424        return (NFA_STATUS_OK);
425    }
426
427    return (NFA_STATUS_FAILED);
428}
429
430/*******************************************************************************
431**
432** Function         NFA_SetP2pListenTech
433**
434** Description      This function is called to set listen technology for NFC-DEP.
435**                  This funtion may be called before or after starting any server
436**                  on NFA P2P/CHO/SNEP.
437**                  If there is no technology for NFC-DEP, P2P listening will be
438**                  stopped.
439**
440**                  NFA_SET_P2P_LISTEN_TECH_EVT without data will be returned.
441**
442** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
443**                  should happen before calling this function
444**
445** Returns          NFA_STATUS_OK if successfully initiated
446**                  NFA_STATUS_FAILED otherwise
447**
448*******************************************************************************/
449tNFA_STATUS NFA_SetP2pListenTech (tNFA_TECHNOLOGY_MASK tech_mask)
450{
451    tNFA_DM_API_SET_P2P_LISTEN_TECH *p_msg;
452
453    NFA_TRACE_API1 ("NFA_P2pSetListenTech (): tech_mask:0x%X", tech_mask);
454
455    if ((p_msg = (tNFA_DM_API_SET_P2P_LISTEN_TECH *) GKI_getbuf (sizeof (tNFA_DM_API_SET_P2P_LISTEN_TECH))) != NULL)
456    {
457        p_msg->hdr.event = NFA_DM_API_SET_P2P_LISTEN_TECH_EVT;
458        p_msg->tech_mask = tech_mask;
459
460        nfa_sys_sendmsg (p_msg);
461
462        return (NFA_STATUS_OK);
463    }
464
465    return (NFA_STATUS_FAILED);
466}
467
468tNFA_STATUS NFA_EnableListening ()
469{
470    BT_HDR *p_msg;
471
472    NFA_TRACE_API0 ("NFA_EnableListening ()");
473
474    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
475    {
476        p_msg->event = NFA_DM_API_ENABLE_LISTENING_EVT;
477
478        nfa_sys_sendmsg (p_msg);
479
480        return (NFA_STATUS_OK);
481    }
482
483    return (NFA_STATUS_FAILED);
484}
485
486tNFA_STATUS NFA_DisableListening ()
487{
488    BT_HDR *p_msg;
489
490    NFA_TRACE_API0 ("NFA_DisableListening ()");
491
492    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
493    {
494        p_msg->event = NFA_DM_API_DISABLE_LISTENING_EVT;
495
496        nfa_sys_sendmsg (p_msg);
497
498        return (NFA_STATUS_OK);
499    }
500
501    return (NFA_STATUS_FAILED);
502}
503
504/*******************************************************************************
505**
506** Function         NFA_StartRfDiscovery
507**
508** Description      Start RF discovery
509**                  RF discovery parameters shall be set by other APIs.
510**
511**                  An NFA_RF_DISCOVERY_STARTED_EVT indicates whether starting was successful or not.
512**
513** Returns          NFA_STATUS_OK if successfully initiated
514**                  NFA_STATUS_FAILED otherwise
515**
516*******************************************************************************/
517tNFA_STATUS NFA_StartRfDiscovery (void)
518{
519    BT_HDR *p_msg;
520
521    NFA_TRACE_API0 ("NFA_StartRfDiscovery ()");
522
523    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
524    {
525        p_msg->event = NFA_DM_API_START_RF_DISCOVERY_EVT;
526
527        nfa_sys_sendmsg (p_msg);
528
529        return (NFA_STATUS_OK);
530    }
531
532    return (NFA_STATUS_FAILED);
533}
534
535/*******************************************************************************
536**
537** Function         NFA_StopRfDiscovery
538**
539** Description      Stop RF discovery
540**
541**                  An NFA_RF_DISCOVERY_STOPPED_EVT indicates whether stopping was successful or not.
542**
543** Returns          NFA_STATUS_OK if successfully initiated
544**                  NFA_STATUS_FAILED otherwise
545**
546*******************************************************************************/
547tNFA_STATUS NFA_StopRfDiscovery (void)
548{
549    BT_HDR *p_msg;
550
551    NFA_TRACE_API0 ("NFA_StopRfDiscovery ()");
552
553    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
554    {
555        p_msg->event = NFA_DM_API_STOP_RF_DISCOVERY_EVT;
556
557        nfa_sys_sendmsg (p_msg);
558
559        return (NFA_STATUS_OK);
560    }
561
562    return (NFA_STATUS_FAILED);
563}
564
565/*******************************************************************************
566**
567** Function         NFA_SetRfDiscoveryDuration
568**
569** Description      Set the duration of the single discovery period in [ms].
570**                  Allowable range: 0 ms to 0xFFFF ms.
571**
572**                  If discovery is already started, the application should
573**                  call NFA_StopRfDiscovery prior to calling
574**                  NFA_SetRfDiscoveryDuration, and then call
575**                  NFA_StartRfDiscovery afterwards to restart discovery using
576**                  the new duration.
577**
578** Note:            If RF discovery is started, NFA_StopRfDiscovery()/NFA_RF_DISCOVERY_STOPPED_EVT
579**                  should happen before calling this function
580**
581** Returns:
582**                  NFA_STATUS_OK, if command accepted
583**                  NFA_STATUS_FAILED: otherwise
584**
585*******************************************************************************/
586tNFA_STATUS NFA_SetRfDiscoveryDuration (UINT16 discovery_period_ms)
587{
588    tNFA_DM_API_SET_RF_DISC_DUR *p_msg;
589
590    NFA_TRACE_API0 ("NFA_SetRfDiscoveryDuration ()");
591
592    /* Post the API message */
593    if ((p_msg = (tNFA_DM_API_SET_RF_DISC_DUR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
594    {
595        p_msg->hdr.event = NFA_DM_API_SET_RF_DISC_DURATION_EVT;
596
597        /* Set discovery duration */
598        p_msg->rf_disc_dur_ms = discovery_period_ms;
599
600        nfa_sys_sendmsg (p_msg);
601
602        return (NFA_STATUS_OK);
603    }
604
605    return (NFA_STATUS_FAILED);
606}
607
608/*******************************************************************************
609**
610** Function         NFA_Select
611**
612** Description      Select one from detected devices during discovery
613**                  (from NFA_DISC_RESULT_EVTs). The application should wait for
614**                  the final NFA_DISC_RESULT_EVT before selecting.
615**
616**                  An NFA_SELECT_RESULT_EVT indicates whether selection was successful or not.
617**                  If failed then application must select again or deactivate by NFA_Deactivate().
618**
619** Returns          NFA_STATUS_OK if successfully initiated
620**                  NFA_STATUS_INVALID_PARAM if RF interface is not matched protocol
621**                  NFA_STATUS_FAILED otherwise
622**
623*******************************************************************************/
624tNFA_STATUS NFA_Select (UINT8             rf_disc_id,
625                        tNFA_NFC_PROTOCOL protocol,
626                        tNFA_INTF_TYPE    rf_interface)
627{
628    tNFA_DM_API_SELECT *p_msg;
629
630    NFA_TRACE_API3 ("NFA_Select (): rf_disc_id:0x%X, protocol:0x%X, rf_interface:0x%X",
631                    rf_disc_id, protocol, rf_interface);
632
633    if (  ((rf_interface == NFA_INTERFACE_ISO_DEP) && (protocol != NFA_PROTOCOL_ISO_DEP))
634        ||((rf_interface == NFA_INTERFACE_NFC_DEP) && (protocol != NFA_PROTOCOL_NFC_DEP))  )
635    {
636        NFA_TRACE_ERROR0 ("NFA_Select (): RF interface is not matched protocol");
637        return (NFA_STATUS_INVALID_PARAM);
638    }
639
640    if ((p_msg = (tNFA_DM_API_SELECT *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_SELECT)))) != NULL)
641    {
642        p_msg->hdr.event     = NFA_DM_API_SELECT_EVT;
643        p_msg->rf_disc_id    = rf_disc_id;
644        p_msg->protocol      = protocol;
645        p_msg->rf_interface  = rf_interface;
646
647        nfa_sys_sendmsg (p_msg);
648
649        return (NFA_STATUS_OK);
650    }
651
652    return (NFA_STATUS_FAILED);
653}
654
655/*******************************************************************************
656**
657** Function         NFA_UpdateRFCommParams
658**
659** Description      This function is called to update RF Communication parameters
660**                  once the Frame RF Interface has been activated.
661**
662**                  An NFA_UPDATE_RF_PARAM_RESULT_EVT indicates whether updating
663**                  was successful or not.
664**
665** Returns          NFA_STATUS_OK if successfully initiated
666**                  NFA_STATUS_FAILED otherwise
667**
668*******************************************************************************/
669tNFA_STATUS NFA_UpdateRFCommParams (tNFA_RF_COMM_PARAMS *p_params)
670{
671    tNFA_DM_API_UPDATE_RF_PARAMS *p_msg;
672
673    NFA_TRACE_API0 ("NFA_UpdateRFCommParams ()");
674
675    if ((p_msg = (tNFA_DM_API_UPDATE_RF_PARAMS *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_UPDATE_RF_PARAMS)))) != NULL)
676    {
677        p_msg->hdr.event     = NFA_DM_API_UPDATE_RF_PARAMS_EVT;
678        memcpy (&p_msg->params, p_params, sizeof (tNFA_RF_COMM_PARAMS));
679
680        nfa_sys_sendmsg (p_msg);
681
682        return (NFA_STATUS_OK);
683    }
684
685    return (NFA_STATUS_FAILED);
686}
687
688/*******************************************************************************
689**
690** Function         NFA_Deactivate
691**
692** Description
693**                  If sleep_mode=TRUE:
694**                      Deselect the activated device by deactivating into sleep mode.
695**
696**                      An NFA_DEACTIVATE_FAIL_EVT indicates that selection was not successful.
697**                      Application can select another discovered device or deactivate by NFA_Deactivate ()
698**                      after receiving NFA_DEACTIVATED_EVT.
699**
700**                      Deactivating to sleep mode is not allowed when NFCC is in wait-for-host-select
701**                      mode, or in listen-sleep states; NFA will deactivate to idle or discovery state
702**                      for these cases respectively.
703**
704**
705**                  If sleep_mode=FALSE:
706**                      Deactivate the connection (e.g. as a result of presence check failure)
707**                      NFA_DEACTIVATED_EVT will indicate that link is deactivated.
708**                      Polling/listening will resume (unless the nfcc is in wait_for-all-discoveries state)
709**
710**
711** Returns          NFA_STATUS_OK if successfully initiated
712**                  NFA_STATUS_FAILED otherwise
713**
714*******************************************************************************/
715NFC_API extern tNFA_STATUS NFA_Deactivate (BOOLEAN sleep_mode)
716{
717    tNFA_DM_API_DEACTIVATE *p_msg;
718
719    NFA_TRACE_API1 ("NFA_Deactivate (): sleep_mode:%i", sleep_mode);
720
721    if ((p_msg = (tNFA_DM_API_DEACTIVATE *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_DEACTIVATE)))) != NULL)
722    {
723        p_msg->hdr.event    = NFA_DM_API_DEACTIVATE_EVT;
724        p_msg->sleep_mode   = sleep_mode;
725
726        nfa_sys_sendmsg (p_msg);
727
728        return (NFA_STATUS_OK);
729    }
730
731    return (NFA_STATUS_FAILED);
732}
733
734/*******************************************************************************
735**
736** Function         NFA_SendRawFrame
737**
738** Description      Send a raw frame over the activated interface with the NFCC.
739**                  This function can only be called after NFC link is activated.
740**
741**                  If the activated interface is a tag and auto-presence check is
742**                  enabled then presence_check_start_delay can be used to indicate
743**                  the delay in msec after which the next auto presence check
744**                  command can be sent. NFA_DM_DEFAULT_PRESENCE_CHECK_START_DELAY
745**                  can be used as the default value for the delay.
746**
747** Returns          NFA_STATUS_OK if successfully initiated
748**                  NFA_STATUS_FAILED otherwise
749**
750*******************************************************************************/
751tNFA_STATUS NFA_SendRawFrame (UINT8  *p_raw_data,
752                              UINT16  data_len,
753                              UINT16  presence_check_start_delay)
754{
755    BT_HDR *p_msg;
756    UINT16  size;
757    UINT8  *p;
758
759    NFA_TRACE_API1 ("NFA_SendRawFrame () data_len:%d", data_len);
760
761    /* Validate parameters */
762    if ((data_len == 0) || (p_raw_data == NULL))
763        return (NFA_STATUS_INVALID_PARAM);
764
765    size = BT_HDR_SIZE + NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE + data_len;
766    if ((p_msg = (BT_HDR *) GKI_getbuf (size)) != NULL)
767    {
768        p_msg->event  = NFA_DM_API_RAW_FRAME_EVT;
769        p_msg->layer_specific = presence_check_start_delay;
770        p_msg->offset = NCI_MSG_OFFSET_SIZE + NCI_DATA_HDR_SIZE;
771        p_msg->len    = data_len;
772
773        p = (UINT8 *) (p_msg + 1) + p_msg->offset;
774        memcpy (p, p_raw_data, data_len);
775
776        nfa_sys_sendmsg (p_msg);
777
778        return (NFA_STATUS_OK);
779    }
780
781    return (NFA_STATUS_FAILED);
782}
783
784/*******************************************************************************
785** NDEF Handler APIs
786*******************************************************************************/
787
788/*******************************************************************************
789**
790** Function         NFA_RegisterNDefTypeHandler
791**
792** Description      This function allows the applications to register for
793**                  specific types of NDEF records. When NDEF records are
794**                  received, NFA will parse the record-type field, and pass
795**                  the record to the registered tNFA_NDEF_CBACK.
796**
797**                  For records types which were not registered, the record will
798**                  be sent to the default handler. A default type-handler may
799**                  be registered by calling this NFA_RegisterNDefTypeHandler
800**                  with tnf=NFA_TNF_DEFAULT. In this case, all un-registered
801**                  record types will be sent to the callback. Only one default
802**                  handler may be registered at a time.
803**
804**                  An NFA_NDEF_REGISTER_EVT will be sent to the tNFA_NDEF_CBACK
805**                  to indicate that registration was successful, and provide a
806**                  handle for this record type.
807**
808** Returns          NFA_STATUS_OK if successfully initiated
809**                  NFA_STATUS_FAILED otherwise
810**
811*******************************************************************************/
812tNFA_STATUS NFA_RegisterNDefTypeHandler (BOOLEAN         handle_whole_message,
813                                         tNFA_TNF        tnf,
814                                         UINT8           *p_type_name,
815                                         UINT8           type_name_len,
816                                         tNFA_NDEF_CBACK *p_ndef_cback)
817{
818    tNFA_DM_API_REG_NDEF_HDLR *p_msg;
819
820    NFA_TRACE_API2 ("NFA_RegisterNDefTypeHandler (): handle whole ndef message: %i, tnf=0x%02x", handle_whole_message, tnf);
821
822    /* Check for NULL callback */
823    if (!p_ndef_cback)
824    {
825        NFA_TRACE_ERROR0 ("NFA_RegisterNDefTypeHandler (): error - null callback");
826        return (NFA_STATUS_INVALID_PARAM);
827    }
828
829
830    if ((p_msg = (tNFA_DM_API_REG_NDEF_HDLR *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_REG_NDEF_HDLR) + type_name_len))) != NULL)
831    {
832        p_msg->hdr.event = NFA_DM_API_REG_NDEF_HDLR_EVT;
833
834        p_msg->flags = (handle_whole_message ? NFA_NDEF_FLAGS_HANDLE_WHOLE_MESSAGE : 0);
835        p_msg->tnf = tnf;
836        p_msg->name_len = type_name_len;
837        p_msg->p_ndef_cback = p_ndef_cback;
838        memcpy (p_msg->name, p_type_name, type_name_len);
839
840        nfa_sys_sendmsg (p_msg);
841
842        return (NFA_STATUS_OK);
843    }
844
845    return (NFA_STATUS_FAILED);
846}
847
848/*******************************************************************************
849**
850** Function         NFA_RegisterNDefUriHandler
851**
852** Description      This API is a special-case of NFA_RegisterNDefTypeHandler
853**                  with TNF=NFA_TNF_WKT, and type_name='U' (URI record); and allows
854**                  registering for specific URI types (e.g. 'tel:' or 'mailto:').
855**
856**                  An NFA_NDEF_REGISTER_EVT will be sent to the tNFA_NDEF_CBACK
857**                  to indicate that registration was successful, and provide a
858**                  handle for this registration.
859**
860**                  If uri_id=NFA_NDEF_URI_ID_ABSOLUTE, then p_abs_uri contains the
861**                  unabridged URI. For all other uri_id values, the p_abs_uri
862**                  parameter is ignored (i.e the URI prefix is implied by uri_id).
863**                  See [NFC RTD URI] for more information.
864**
865** Returns          NFA_STATUS_OK if successfully initiated
866**                  NFA_STATUS_FAILED otherwise
867**
868*******************************************************************************/
869NFC_API extern tNFA_STATUS NFA_RegisterNDefUriHandler (BOOLEAN          handle_whole_message,
870                                                       tNFA_NDEF_URI_ID uri_id,
871                                                       UINT8            *p_abs_uri,
872                                                       UINT8            uri_id_len,
873                                                       tNFA_NDEF_CBACK  *p_ndef_cback)
874{
875    tNFA_DM_API_REG_NDEF_HDLR *p_msg;
876
877    NFA_TRACE_API2 ("NFA_RegisterNDefUriHandler (): handle whole ndef message: %i, uri_id=0x%02x", handle_whole_message, uri_id);
878
879    /* Check for NULL callback */
880    if (!p_ndef_cback)
881    {
882        NFA_TRACE_ERROR0 ("NFA_RegisterNDefUriHandler (): error - null callback");
883        return (NFA_STATUS_INVALID_PARAM);
884    }
885
886
887    if ((p_msg = (tNFA_DM_API_REG_NDEF_HDLR *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_REG_NDEF_HDLR) + uri_id_len))) != NULL)
888    {
889        p_msg->hdr.event = NFA_DM_API_REG_NDEF_HDLR_EVT;
890
891        p_msg->flags = NFA_NDEF_FLAGS_WKT_URI;
892
893        if (handle_whole_message)
894        {
895            p_msg->flags |= NFA_NDEF_FLAGS_HANDLE_WHOLE_MESSAGE;
896        }
897
898        /* abs_uri is only valid fir uri_id=NFA_NDEF_URI_ID_ABSOLUTE */
899        if (uri_id != NFA_NDEF_URI_ID_ABSOLUTE)
900        {
901            uri_id_len = 0;
902        }
903
904        p_msg->tnf = NFA_TNF_WKT;
905        p_msg->uri_id = uri_id;
906        p_msg->name_len = uri_id_len;
907        p_msg->p_ndef_cback = p_ndef_cback;
908        memcpy (p_msg->name, p_abs_uri, uri_id_len);
909
910        nfa_sys_sendmsg (p_msg);
911
912        return (NFA_STATUS_OK);
913    }
914
915    return (NFA_STATUS_FAILED);
916}
917
918/*******************************************************************************
919**
920** Function         NFA_DeregisterNDefTypeHandler
921**
922** Description      Deregister NDEF record type handler.
923**
924** Returns          NFA_STATUS_OK if successfully initiated
925**                  NFA_STATUS_FAILED otherwise
926**
927*******************************************************************************/
928NFC_API extern tNFA_STATUS NFA_DeregisterNDefTypeHandler (tNFA_HANDLE ndef_type_handle)
929{
930    tNFA_DM_API_DEREG_NDEF_HDLR *p_msg;
931
932    NFA_TRACE_API1 ("NFA_DeregisterNDefHandler (): handle 0x%08x", ndef_type_handle);
933
934
935    if ((p_msg = (tNFA_DM_API_DEREG_NDEF_HDLR *) GKI_getbuf ((UINT16) (sizeof (tNFA_DM_API_DEREG_NDEF_HDLR)))) != NULL)
936    {
937        p_msg->hdr.event = NFA_DM_API_DEREG_NDEF_HDLR_EVT;
938        p_msg->ndef_type_handle = ndef_type_handle;
939
940        nfa_sys_sendmsg (p_msg);
941
942        return (NFA_STATUS_OK);
943    }
944
945    return (NFA_STATUS_FAILED);
946}
947
948/*******************************************************************************
949**
950** Function         NFA_PowerOffSleepMode
951**
952** Description      This function is called to enter or leave NFCC Power Off Sleep mode
953**                  NFA_DM_PWR_MODE_CHANGE_EVT will be sent to indicate status.
954**
955**                  start_stop : TRUE if entering Power Off Sleep mode
956**                               FALSE if leaving Power Off Sleep mode
957**
958** Returns          NFA_STATUS_OK if successfully initiated
959**                  NFA_STATUS_FAILED otherwise
960**
961*******************************************************************************/
962tNFA_STATUS NFA_PowerOffSleepMode (BOOLEAN start_stop)
963{
964    BT_HDR *p_msg;
965
966    NFA_TRACE_API1 ("NFA_PowerOffSleepState () start_stop=%d", start_stop);
967
968    if (nfa_dm_cb.flags & NFA_DM_FLAGS_SETTING_PWR_MODE)
969    {
970        NFA_TRACE_ERROR0 ("NFA_PowerOffSleepState (): NFA DM is busy to update power mode");
971        return (NFA_STATUS_FAILED);
972    }
973    else
974    {
975        nfa_dm_cb.flags |= NFA_DM_FLAGS_SETTING_PWR_MODE;
976    }
977
978    if ((p_msg = (BT_HDR *) GKI_getbuf (sizeof (BT_HDR))) != NULL)
979    {
980        p_msg->event          = NFA_DM_API_POWER_OFF_SLEEP_EVT;
981        p_msg->layer_specific = start_stop;
982
983        nfa_sys_sendmsg (p_msg);
984
985        return (NFA_STATUS_OK);
986    }
987
988    return (NFA_STATUS_FAILED);
989}
990
991/*******************************************************************************
992**
993** Function         NFA_RegVSCback
994**
995** Description      This function is called to register or de-register a callback
996**                  function to receive Proprietary NCI response and notification
997**                  events.
998**                  The maximum number of callback functions allowed is NFC_NUM_VS_CBACKS
999**
1000** Returns          tNFC_STATUS
1001**
1002*******************************************************************************/
1003tNFC_STATUS NFA_RegVSCback (BOOLEAN          is_register,
1004                            tNFA_VSC_CBACK   *p_cback)
1005{
1006    tNFA_DM_API_REG_VSC *p_msg;
1007
1008    NFA_TRACE_API1 ("NFA_RegVSCback() is_register=%d", is_register);
1009
1010    if (p_cback == NULL)
1011    {
1012        NFA_TRACE_ERROR0 ("NFA_RegVSCback() requires a valid callback function");
1013        return (NFA_STATUS_FAILED);
1014    }
1015
1016    if ((p_msg = (tNFA_DM_API_REG_VSC *) GKI_getbuf (sizeof(tNFA_DM_API_REG_VSC))) != NULL)
1017    {
1018        p_msg->hdr.event        = NFA_DM_API_REG_VSC_EVT;
1019        p_msg->is_register      = is_register;
1020        p_msg->p_cback          = p_cback;
1021
1022        nfa_sys_sendmsg (p_msg);
1023
1024        return (NFA_STATUS_OK);
1025    }
1026
1027    return (NFA_STATUS_FAILED);
1028}
1029
1030/*******************************************************************************
1031**
1032** Function         NFA_SendVsCommand
1033**
1034** Description      This function is called to send an NCI Vendor Specific
1035**                  command to NFCC.
1036**
1037**                  oid             - The opcode of the VS command.
1038**                  cmd_params_len  - The command parameter len
1039**                  p_cmd_params    - The command parameter
1040**                  p_cback         - The callback function to receive the command
1041**                                    status
1042**
1043** Returns          NFA_STATUS_OK if successfully initiated
1044**                  NFA_STATUS_FAILED otherwise
1045**
1046*******************************************************************************/
1047tNFA_STATUS NFA_SendVsCommand (UINT8            oid,
1048                               UINT8            cmd_params_len,
1049                               UINT8            *p_cmd_params,
1050                               tNFA_VSC_CBACK    *p_cback)
1051{
1052    tNFA_DM_API_SEND_VSC *p_msg;
1053    UINT16  size = sizeof(tNFA_DM_API_SEND_VSC) + cmd_params_len;
1054
1055    NFA_TRACE_API1 ("NFA_SendVsCommand() oid=0x%x", oid);
1056
1057    if ((p_msg = (tNFA_DM_API_SEND_VSC *) GKI_getbuf (size)) != NULL)
1058    {
1059        p_msg->hdr.event        = NFA_DM_API_SEND_VSC_EVT;
1060        p_msg->oid              = oid;
1061        p_msg->p_cback          = p_cback;
1062        if (cmd_params_len && p_cmd_params)
1063        {
1064            p_msg->cmd_params_len   = cmd_params_len;
1065            p_msg->p_cmd_params     = (UINT8 *)(p_msg + 1);
1066            memcpy (p_msg->p_cmd_params, p_cmd_params, cmd_params_len);
1067        }
1068        else
1069        {
1070            p_msg->cmd_params_len   = 0;
1071            p_msg->p_cmd_params     = NULL;
1072        }
1073
1074        nfa_sys_sendmsg (p_msg);
1075
1076        return (NFA_STATUS_OK);
1077    }
1078
1079    return (NFA_STATUS_FAILED);
1080}
1081
1082/*******************************************************************************
1083**
1084** Function         NFA_SetTraceLevel
1085**
1086** Description      This function sets the trace level for NFA.  If called with
1087**                  a value of 0xFF, it simply returns the current trace level.
1088**
1089** Returns          The new or current trace level
1090**
1091*******************************************************************************/
1092UINT8 NFA_SetTraceLevel (UINT8 new_level)
1093{
1094    if (new_level != 0xFF)
1095        nfa_sys_set_trace_level (new_level);
1096
1097    return (nfa_sys_cb.trace_level);
1098}
1099
1100