Home | History | Annotate | Download | only in rw
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2003-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  *  This is the main implementation file for the NFA_RW
     23  *
     24  ******************************************************************************/
     25 #include <string.h>
     26 #include "nfa_rw_api.h"
     27 #include "nfa_sys.h"
     28 #include "nfa_rw_int.h"
     29 #include "nfa_dm_int.h"
     30 #include "nfa_sys_int.h"
     31 
     32 /* NFA_RW control block */
     33 tNFA_RW_CB nfa_rw_cb;
     34 
     35 /*****************************************************************************
     36 ** Constants and types
     37 *****************************************************************************/
     38 static const tNFA_SYS_REG nfa_rw_sys_reg =
     39 {
     40     NULL,
     41     nfa_rw_handle_event,
     42     nfa_rw_sys_disable,
     43     NULL
     44 };
     45 
     46 /* NFA_RW actions */
     47 const tNFA_RW_ACTION nfa_rw_action_tbl[] =
     48 {
     49     nfa_rw_handle_op_req,           /* NFA_RW_OP_REQUEST_EVT            */
     50     nfa_rw_activate_ntf,            /* NFA_RW_ACTIVATE_NTF_EVT          */
     51     nfa_rw_deactivate_ntf,          /* NFA_RW_DEACTIVATE_NTF_EVT        */
     52     nfa_rw_presence_check_tick,     /* NFA_RW_PRESENCE_CHECK_TICK_EVT   */
     53 };
     54 
     55 
     56 /*****************************************************************************
     57 ** Local function prototypes
     58 *****************************************************************************/
     59 #if (BT_TRACE_VERBOSE == TRUE)
     60 static char *nfa_rw_evt_2_str (UINT16 event);
     61 #endif
     62 
     63 /*******************************************************************************
     64 **
     65 ** Function         nfa_rw_init
     66 **
     67 ** Description      Initialize NFA RW
     68 **
     69 ** Returns          None
     70 **
     71 *******************************************************************************/
     72 void nfa_rw_init (void)
     73 {
     74     NFA_TRACE_DEBUG0 ("nfa_rw_init ()");
     75 
     76     /* initialize control block */
     77     memset (&nfa_rw_cb, 0, sizeof (tNFA_RW_CB));
     78 
     79     /* register message handler on NFA SYS */
     80     nfa_sys_register (NFA_ID_RW,  &nfa_rw_sys_reg);
     81 }
     82 
     83 /*******************************************************************************
     84 **
     85 ** Function         nfa_rw_sys_disable
     86 **
     87 ** Description      Clean up rw sub-system
     88 **
     89 **
     90 ** Returns          void
     91 **
     92 *******************************************************************************/
     93 void nfa_rw_sys_disable (void)
     94 {
     95     /* Return to idle */
     96     NFC_SetStaticRfCback (NULL);
     97 
     98     /* Stop presence check timer (if started) */
     99     nfa_rw_stop_presence_check_timer ();
    100 
    101     /* Free scratch buffer if any */
    102     nfa_rw_free_ndef_rx_buf ();
    103 
    104     /* Free pending command if any */
    105     if (nfa_rw_cb.p_pending_msg)
    106     {
    107         GKI_freebuf (nfa_rw_cb.p_pending_msg);
    108         nfa_rw_cb.p_pending_msg = NULL;
    109     }
    110 
    111     nfa_sys_deregister (NFA_ID_RW);
    112 }
    113 
    114 /*******************************************************************************
    115 **
    116 ** Function         nfa_rw_proc_disc_evt
    117 **
    118 ** Description      Called by nfa_dm to handle ACTIVATED/DEACTIVATED  events
    119 **
    120 ** Returns          void
    121 **
    122 *******************************************************************************/
    123 void nfa_rw_proc_disc_evt (tNFA_DM_RF_DISC_EVT event, tNFC_DISCOVER *p_data, BOOLEAN excl_rf_not_active)
    124 {
    125     tNFA_RW_MSG msg;
    126 
    127     switch (event)
    128     {
    129     case NFA_DM_RF_DISC_ACTIVATED_EVT:
    130         msg.hdr.event                       = NFA_RW_ACTIVATE_NTF_EVT;
    131         msg.activate_ntf.p_activate_params  = &p_data->activate;
    132         msg.activate_ntf.excl_rf_not_active = excl_rf_not_active;
    133 
    134         nfa_rw_handle_event ((BT_HDR *) &msg);
    135         break;
    136 
    137     case NFA_DM_RF_DISC_DEACTIVATED_EVT:
    138         msg.hdr.event = NFA_RW_DEACTIVATE_NTF_EVT;
    139 
    140         nfa_rw_handle_event ((BT_HDR *) &msg);
    141         break;
    142 
    143     default:
    144         break;
    145     }
    146 }
    147 
    148 /*******************************************************************************
    149 **
    150 ** Function         nfa_rw_send_raw_frame
    151 **
    152 ** Description      Called by nfa_dm to send raw frame
    153 **
    154 ** Returns          tNFA_STATUS
    155 **
    156 *******************************************************************************/
    157 tNFA_STATUS nfa_rw_send_raw_frame (BT_HDR *p_data)
    158 {
    159     tNFA_RW_MSG *p_msg;
    160 
    161     if ((p_msg = (tNFA_RW_MSG *) GKI_getbuf ((UINT16) sizeof(tNFA_RW_MSG))) != NULL)
    162     {
    163         p_msg->hdr.event = NFA_RW_OP_REQUEST_EVT;
    164         p_msg->op_req.op = NFA_RW_OP_SEND_RAW_FRAME;
    165 
    166         p_msg->op_req.params.send_raw_frame.p_data = p_data;
    167 
    168         if (nfa_rw_handle_event ((BT_HDR *) p_msg))
    169             GKI_freebuf (p_msg);
    170 
    171         return (NFA_STATUS_OK);
    172     }
    173     return NFA_STATUS_FAILED;
    174 }
    175 
    176 /*******************************************************************************
    177 **
    178 ** Function         nfa_rw_handle_event
    179 **
    180 ** Description      nfa rw main event handling function.
    181 **
    182 ** Returns          TRUE if caller should free p_msg buffer
    183 **
    184 *******************************************************************************/
    185 BOOLEAN nfa_rw_handle_event(BT_HDR *p_msg)
    186 {
    187     UINT16 act_idx;
    188 
    189 #if (BT_TRACE_VERBOSE == TRUE)
    190     NFA_TRACE_EVENT3 ("nfa_rw_handle_event event: %s (0x%02x), flags: %08x", nfa_rw_evt_2_str (p_msg->event), p_msg->event, nfa_rw_cb.flags);
    191 #else
    192     NFA_TRACE_EVENT2 ("nfa_rw_handle_event event: 0x%x, flags: %08x",p_msg->event, nfa_rw_cb.flags);
    193 #endif
    194 
    195     /* Get NFA_RW sub-event */
    196     if ((act_idx = (p_msg->event & 0x00FF)) < (NFA_RW_MAX_EVT & 0xFF))
    197     {
    198         return (*nfa_rw_action_tbl[act_idx]) ( (tNFA_RW_MSG*) p_msg);
    199     }
    200     else
    201     {
    202         NFA_TRACE_ERROR1 ("nfa_rw_handle_event: unhandled event 0x%02X", p_msg->event);
    203         return TRUE;
    204     }
    205 }
    206 
    207 
    208 #if (BT_TRACE_VERBOSE == TRUE)
    209 /*******************************************************************************
    210 **
    211 ** Function         nfa_rw_evt_2_str
    212 **
    213 ** Description      convert nfa_rw evt to string
    214 **
    215 *******************************************************************************/
    216 static char *nfa_rw_evt_2_str (UINT16 event)
    217 {
    218     switch (event)
    219     {
    220     case NFA_RW_OP_REQUEST_EVT:
    221         return "NFA_RW_OP_REQUEST_EVT";
    222 
    223     case NFA_RW_ACTIVATE_NTF_EVT:
    224         return "NFA_RW_ACTIVATE_NTF_EVT";
    225 
    226     case NFA_RW_DEACTIVATE_NTF_EVT:
    227         return "NFA_RW_DEACTIVATE_NTF_EVT";
    228 
    229     case NFA_RW_PRESENCE_CHECK_TICK_EVT:
    230         return "NFA_RW_PRESENCE_CHECK_TICK_EVT";
    231 
    232     default:
    233         return "Unknown";
    234     }
    235 }
    236 #endif /* BT_TRACE_VERBOSE */
    237