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