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