nfa_snep_default.c revision e29968cf3e053557a9c2efc5a7a42d0767c51d9d
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                    if (p_data->alloc.ndef_length <= NFA_SNEP_DEFAULT_SERVER_MAX_NDEF_SIZE)
139                    {
140                        /* allocate memory, allocated buffer will be returned in NFA_SNEP_PUT_REQ_EVT */
141                        p_data->alloc.p_buff = (UINT8*) nfa_mem_co_alloc (p_data->alloc.ndef_length);
142                    }
143
144                    /* store buffer pointer in case of failure in the middle */
145                    nfa_snep_default_cb.conn[xx].p_rx_ndef = p_data->alloc.p_buff;
146                    break;
147                }
148            }
149        }
150        break;
151
152    case NFA_SNEP_PUT_REQ_EVT:
153        for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
154        {
155            if (nfa_snep_default_cb.conn[xx].conn_handle == p_data->put_req.conn_handle)
156            {
157                if (!nfa_snep_cb.is_dta_mode)
158                {
159                    nfa_dm_ndef_handle_message (NFA_STATUS_OK,
160                                                p_data->put_req.p_ndef,
161                                                p_data->put_req.ndef_length);
162                }
163#if (BT_TRACE_PROTOCOL == TRUE)
164                else
165                {
166                    DispNDEFMsg (p_data->put_req.p_ndef,
167                                 p_data->put_req.ndef_length, TRUE);
168                }
169#endif
170
171                nfa_mem_co_free (p_data->put_req.p_ndef);
172                nfa_snep_default_cb.conn[xx].p_rx_ndef = NULL;
173
174                api_put_resp.conn_handle = p_data->put_req.conn_handle;
175                api_put_resp.resp_code   = NFA_SNEP_RESP_CODE_SUCCESS;
176
177                nfa_snep_put_resp ((tNFA_SNEP_MSG *) &api_put_resp);
178                break;
179            }
180        }
181        break;
182
183    case NFA_SNEP_DISC_EVT:
184        for (xx = 0; xx < NFA_SNEP_DEFAULT_MAX_CONN; xx++)
185        {
186            if (nfa_snep_default_cb.conn[xx].conn_handle == p_data->disc.conn_handle)
187            {
188                nfa_snep_default_cb.conn[xx].conn_handle = NFA_HANDLE_INVALID;
189
190                /* if buffer is not freed */
191                if (nfa_snep_default_cb.conn[xx].p_rx_ndef)
192                {
193                    nfa_mem_co_free (nfa_snep_default_cb.conn[xx].p_rx_ndef);
194                    nfa_snep_default_cb.conn[xx].p_rx_ndef  = NULL;
195                }
196                break;
197            }
198        }
199        break;
200
201    default:
202        SNEP_TRACE_ERROR0 ("Unexpected event for default SNEP server");
203        break;
204    }
205}
206
207/*******************************************************************************
208**
209** Function         nfa_snep_start_default_server
210**
211** Description      Launching default SNEP server
212**
213**
214** Returns          TRUE to deallocate message
215**
216*******************************************************************************/
217BOOLEAN nfa_snep_start_default_server (tNFA_SNEP_MSG *p_msg)
218{
219    tNFA_SNEP_API_REG_SERVER msg;
220
221    SNEP_TRACE_DEBUG0 ("nfa_snep_start_default_server ()");
222
223    if (nfa_snep_default_cb.server_handle == NFA_HANDLE_INVALID)
224    {
225        msg.server_sap = NFA_SNEP_DEFAULT_SERVER_SAP;
226
227        BCM_STRNCPY_S (msg.service_name, sizeof (msg.service_name),
228                      "urn:nfc:sn:snep", LLCP_MAX_SN_LEN);
229        msg.service_name[LLCP_MAX_SN_LEN] = 0;
230
231        msg.p_cback = nfa_snep_default_service_cback;
232        nfa_snep_reg_server ((tNFA_SNEP_MSG *) &msg);
233    }
234
235    (*p_msg->api_start_default_server.p_cback) (NFA_SNEP_DEFAULT_SERVER_STARTED_EVT, NULL);
236
237    return TRUE;
238}
239
240/*******************************************************************************
241**
242** Function         nfa_snep_stop_default_server
243**
244** Description      Stoppping default SNEP server
245**
246**
247** Returns          TRUE to deallocate message
248**
249*******************************************************************************/
250BOOLEAN nfa_snep_stop_default_server (tNFA_SNEP_MSG *p_msg)
251{
252    tNFA_SNEP_API_DEREG msg;
253
254    SNEP_TRACE_DEBUG0 ("nfa_snep_stop_default_server ()");
255
256    if (nfa_snep_default_cb.server_handle != NFA_HANDLE_INVALID)
257    {
258        msg.reg_handle = nfa_snep_default_cb.server_handle;
259
260        nfa_snep_dereg ((tNFA_SNEP_MSG *) &msg);
261
262        nfa_snep_default_cb.server_handle = NFA_HANDLE_INVALID;
263    }
264
265    (*p_msg->api_stop_default_server.p_cback) (NFA_SNEP_DEFAULT_SERVER_STOPPED_EVT, NULL);
266
267    return TRUE;
268}
269
270