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