Home | History | Annotate | Download | only in snep
      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  *  This is the implementation file for the NFA SNEP default server.
     22  *
     23  ******************************************************************************/
     24 #include <string.h>
     25 #include "nfa_sys.h"
     26 #include "nfa_sys_int.h"
     27 #include "nfa_snep_int.h"
     28 #include "nfa_mem_co.h"
     29 #include "nfa_dm_int.h"
     30 #include "trace_api.h"
     31 
     32 /*****************************************************************************
     33 **  Global Variables
     34 *****************************************************************************/
     35 
     36 /* system manager control block definition */
     37 #if NFA_DYNAMIC_MEMORY == FALSE
     38 tNFA_SNEP_DEFAULT_CB nfa_snep_default_cb;
     39 #endif
     40 
     41 /*****************************************************************************
     42 **  Static Functions
     43 *****************************************************************************/
     44 
     45 /*****************************************************************************
     46 **  Constants
     47 *****************************************************************************/
     48 
     49 /*******************************************************************************
     50 **
     51 ** Function         nfa_snep_default_init
     52 **
     53 ** Description      Initialize NFA SNEP default server
     54 **
     55 **
     56 ** Returns          None
     57 **
     58 *******************************************************************************/
     59 void nfa_snep_default_init (void)
     60 {
     61     UINT8 xx;
     62 
     63     SNEP_TRACE_DEBUG0 ("nfa_snep_default_init ()");
     64 
     65     /* initialize control block */
     66     memset (&nfa_snep_default_cb, 0, sizeof (tNFA_SNEP_DEFAULT_CB));
     67 
     68     /* initialize non-zero value */
     69     nfa_snep_default_cb.server_handle = NFA_HANDLE_INVALID;
     70 
     71     for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
     72     {
     73         nfa_snep_default_cb.conn[xx].conn_handle = NFA_HANDLE_INVALID;
     74     }
     75 }
     76 
     77 /*******************************************************************************
     78 **
     79 ** Function         nfa_snep_default_service_cback
     80 **
     81 ** Description      Processing event to default SNEP server/client
     82 **
     83 **
     84 ** Returns          None
     85 **
     86 *******************************************************************************/
     87 void nfa_snep_default_service_cback (tNFA_SNEP_EVT event, tNFA_SNEP_EVT_DATA *p_data)
     88 {
     89     UINT8 xx;
     90     tNFA_SNEP_API_DISCONNECT api_disconnect;
     91     tNFA_SNEP_API_PUT_RESP   api_put_resp;
     92 
     93     SNEP_TRACE_DEBUG1 ("nfa_snep_default_service_cback () event:0x%X", event);
     94 
     95     switch (event)
     96     {
     97     case NFA_SNEP_REG_EVT:
     98         if (p_data->reg.status == NFA_STATUS_OK)
     99         {
    100             nfa_snep_default_cb.server_handle = p_data->reg.reg_handle;
    101         }
    102         else
    103         {
    104             SNEP_TRACE_ERROR0 ("Default SNEP server failed to register");
    105         }
    106         break;
    107 
    108     case NFA_SNEP_CONNECTED_EVT:
    109         if (p_data->connect.reg_handle == nfa_snep_default_cb.server_handle)
    110         {
    111             for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
    112             {
    113                 if (nfa_snep_default_cb.conn[xx].conn_handle == NFA_HANDLE_INVALID)
    114                 {
    115                     nfa_snep_default_cb.conn[xx].conn_handle = p_data->connect.conn_handle;
    116                     break;
    117                 }
    118             }
    119 
    120             if (xx >= NFA_SNEP_DEFAULT_MAX_CONN)
    121             {
    122                 SNEP_TRACE_ERROR1 ("Default SNEP server cannot handle more than %d connections",
    123                                   NFA_SNEP_DEFAULT_MAX_CONN);
    124 
    125                 api_disconnect.conn_handle = p_data->connect.conn_handle;
    126                 api_disconnect.flush       = TRUE;
    127                 nfa_snep_disconnect ((tNFA_SNEP_MSG *) &api_disconnect);
    128             }
    129         }
    130         break;
    131 
    132     case NFA_SNEP_ALLOC_BUFF_EVT:
    133         if (p_data->alloc.req_code == NFA_SNEP_REQ_CODE_GET)
    134         {
    135             /*
    136             ** Default server doesn't support GET
    137             ** Send NFA_SNEP_RESP_CODE_NOT_IMPLM to peer
    138             */
    139             SNEP_TRACE_WARNING0 ("Default SNEP server doesn't support GET");
    140             p_data->alloc.p_buff    = NULL;
    141             p_data->alloc.resp_code = NFA_SNEP_RESP_CODE_NOT_IMPLM;
    142         }
    143         else /* NFA_SNEP_REQ_CODE_PUT */
    144         {
    145             p_data->alloc.p_buff = NULL;
    146 
    147             for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
    148             {
    149                 if (nfa_snep_default_cb.conn[xx].conn_handle == p_data->alloc.conn_handle)
    150                 {
    151                     if (p_data->alloc.ndef_length <= NFA_SNEP_DEFAULT_SERVER_MAX_NDEF_SIZE)
    152                     {
    153                         /* allocate memory, allocated buffer will be returned in NFA_SNEP_PUT_REQ_EVT */
    154                         p_data->alloc.p_buff = (UINT8*) nfa_mem_co_alloc (p_data->alloc.ndef_length);
    155                     }
    156 
    157                     /* store buffer pointer in case of failure in the middle */
    158                     nfa_snep_default_cb.conn[xx].p_rx_ndef = p_data->alloc.p_buff;
    159                     break;
    160                 }
    161             }
    162         }
    163         break;
    164 
    165     case NFA_SNEP_PUT_REQ_EVT:
    166         for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
    167         {
    168             if (nfa_snep_default_cb.conn[xx].conn_handle == p_data->put_req.conn_handle)
    169             {
    170                 if (!nfa_snep_cb.is_dta_mode)
    171                 {
    172                     nfa_dm_ndef_handle_message (NFA_STATUS_OK,
    173                                                 p_data->put_req.p_ndef,
    174                                                 p_data->put_req.ndef_length);
    175                 }
    176 #if (BT_TRACE_PROTOCOL == TRUE)
    177                 else
    178                 {
    179                     DispNDEFMsg (p_data->put_req.p_ndef,
    180                                  p_data->put_req.ndef_length, TRUE);
    181                 }
    182 #endif
    183 
    184                 nfa_mem_co_free (p_data->put_req.p_ndef);
    185                 nfa_snep_default_cb.conn[xx].p_rx_ndef = NULL;
    186 
    187                 api_put_resp.conn_handle = p_data->put_req.conn_handle;
    188                 api_put_resp.resp_code   = NFA_SNEP_RESP_CODE_SUCCESS;
    189 
    190                 nfa_snep_put_resp ((tNFA_SNEP_MSG *) &api_put_resp);
    191                 break;
    192             }
    193         }
    194         break;
    195 
    196     case NFA_SNEP_DISC_EVT:
    197         for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
    198         {
    199             if (nfa_snep_default_cb.conn[xx].conn_handle == p_data->disc.conn_handle)
    200             {
    201                 nfa_snep_default_cb.conn[xx].conn_handle = NFA_HANDLE_INVALID;
    202 
    203                 /* if buffer is not freed */
    204                 if (nfa_snep_default_cb.conn[xx].p_rx_ndef)
    205                 {
    206                     nfa_mem_co_free (nfa_snep_default_cb.conn[xx].p_rx_ndef);
    207                     nfa_snep_default_cb.conn[xx].p_rx_ndef  = NULL;
    208                 }
    209                 break;
    210             }
    211         }
    212         break;
    213 
    214     default:
    215         SNEP_TRACE_ERROR0 ("Unexpected event for default SNEP server");
    216         break;
    217     }
    218 }
    219 
    220 /*******************************************************************************
    221 **
    222 ** Function         nfa_snep_start_default_server
    223 **
    224 ** Description      Launching default SNEP server
    225 **
    226 **
    227 ** Returns          TRUE to deallocate message
    228 **
    229 *******************************************************************************/
    230 BOOLEAN nfa_snep_start_default_server (tNFA_SNEP_MSG *p_msg)
    231 {
    232     tNFA_SNEP_API_REG_SERVER msg;
    233 
    234     SNEP_TRACE_DEBUG0 ("nfa_snep_start_default_server ()");
    235 
    236     if (nfa_snep_default_cb.server_handle == NFA_HANDLE_INVALID)
    237     {
    238         msg.server_sap = NFA_SNEP_DEFAULT_SERVER_SAP;
    239 
    240         BCM_STRNCPY_S (msg.service_name, sizeof (msg.service_name),
    241                       "urn:nfc:sn:snep", LLCP_MAX_SN_LEN);
    242         msg.service_name[LLCP_MAX_SN_LEN] = 0;
    243 
    244         msg.p_cback = nfa_snep_default_service_cback;
    245         nfa_snep_reg_server ((tNFA_SNEP_MSG *) &msg);
    246     }
    247 
    248     (*p_msg->api_start_default_server.p_cback) (NFA_SNEP_DEFAULT_SERVER_STARTED_EVT, NULL);
    249 
    250     return TRUE;
    251 }
    252 
    253 /*******************************************************************************
    254 **
    255 ** Function         nfa_snep_stop_default_server
    256 **
    257 ** Description      Stoppping default SNEP server
    258 **
    259 **
    260 ** Returns          TRUE to deallocate message
    261 **
    262 *******************************************************************************/
    263 BOOLEAN nfa_snep_stop_default_server (tNFA_SNEP_MSG *p_msg)
    264 {
    265     tNFA_SNEP_API_DEREG msg;
    266 
    267     SNEP_TRACE_DEBUG0 ("nfa_snep_stop_default_server ()");
    268 
    269     if (nfa_snep_default_cb.server_handle != NFA_HANDLE_INVALID)
    270     {
    271         msg.reg_handle = nfa_snep_default_cb.server_handle;
    272 
    273         nfa_snep_dereg ((tNFA_SNEP_MSG *) &msg);
    274 
    275         nfa_snep_default_cb.server_handle = NFA_HANDLE_INVALID;
    276     }
    277 
    278     (*p_msg->api_stop_default_server.p_cback) (NFA_SNEP_DEFAULT_SERVER_STOPPED_EVT, NULL);
    279 
    280     return TRUE;
    281 }
    282 
    283