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