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