1 /******************************************************************************
2  *
3  *  Copyright (C) 2001-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 file contains the main BNEP functions
22  *
23  ******************************************************************************/
24 
25 #include "bt_target.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 
30 #include "gki.h"
31 #include "bt_types.h"
32 #include "l2cdefs.h"
33 #include "hcidefs.h"
34 #include "hcimsgs.h"
35 
36 #include "l2c_api.h"
37 #include "l2cdefs.h"
38 
39 #include "btu.h"
40 #include "btm_api.h"
41 
42 #include "bnep_api.h"
43 #include "bnep_int.h"
44 #include "bt_utils.h"
45 
46 #include "device/include/controller.h"
47 
48 
49 /********************************************************************************/
50 /*                       G L O B A L    B N E P       D A T A                   */
51 /********************************************************************************/
52 #if BNEP_DYNAMIC_MEMORY == FALSE
53 tBNEP_CB   bnep_cb;
54 #endif
55 
56 const UINT16 bnep_frame_hdr_sizes[] = {14, 1, 2, 8, 8};
57 
58 /********************************************************************************/
59 /*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
60 /********************************************************************************/
61 static void bnep_connect_ind (BD_ADDR  bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id);
62 static void bnep_connect_cfm (UINT16 l2cap_cid, UINT16 result);
63 static void bnep_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
64 static void bnep_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
65 static void bnep_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed);
66 static void bnep_disconnect_cfm (UINT16 l2cap_cid, UINT16 result);
67 static void bnep_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg);
68 static void bnep_congestion_ind (UINT16 lcid, BOOLEAN is_congested);
69 
70 
71 /*******************************************************************************
72 **
73 ** Function         bnep_register_with_l2cap
74 **
75 ** Description      This function registers BNEP PSM with L2CAP
76 **
77 ** Returns          void
78 **
79 *******************************************************************************/
bnep_register_with_l2cap(void)80 tBNEP_RESULT bnep_register_with_l2cap (void)
81 {
82     /* Initialize the L2CAP configuration. We only care about MTU and flush */
83     memset(&bnep_cb.l2cap_my_cfg, 0, sizeof(tL2CAP_CFG_INFO));
84 
85     bnep_cb.l2cap_my_cfg.mtu_present            = TRUE;
86     bnep_cb.l2cap_my_cfg.mtu                    = BNEP_MTU_SIZE;
87     bnep_cb.l2cap_my_cfg.flush_to_present       = TRUE;
88     bnep_cb.l2cap_my_cfg.flush_to               = BNEP_FLUSH_TO;
89 
90     bnep_cb.reg_info.pL2CA_ConnectInd_Cb        = bnep_connect_ind;
91     bnep_cb.reg_info.pL2CA_ConnectCfm_Cb        = bnep_connect_cfm;
92     bnep_cb.reg_info.pL2CA_ConfigInd_Cb         = bnep_config_ind;
93     bnep_cb.reg_info.pL2CA_ConfigCfm_Cb         = bnep_config_cfm;
94     bnep_cb.reg_info.pL2CA_DisconnectInd_Cb     = bnep_disconnect_ind;
95     bnep_cb.reg_info.pL2CA_DisconnectCfm_Cb     = bnep_disconnect_cfm;
96     bnep_cb.reg_info.pL2CA_DataInd_Cb           = bnep_data_ind;
97     bnep_cb.reg_info.pL2CA_CongestionStatus_Cb  = bnep_congestion_ind;
98 
99     /* Now, register with L2CAP */
100     if (!L2CA_Register (BT_PSM_BNEP, &bnep_cb.reg_info))
101     {
102         BNEP_TRACE_ERROR ("BNEP - Registration failed");
103         return BNEP_SECURITY_FAIL;
104     }
105 
106     return BNEP_SUCCESS;
107 }
108 
109 
110 /*******************************************************************************
111 **
112 ** Function         bnep_connect_ind
113 **
114 ** Description      This function handles an inbound connection indication
115 **                  from L2CAP. This is the case where we are acting as a
116 **                  server.
117 **
118 ** Returns          void
119 **
120 *******************************************************************************/
bnep_connect_ind(BD_ADDR bd_addr,UINT16 l2cap_cid,UINT16 psm,UINT8 l2cap_id)121 static void bnep_connect_ind (BD_ADDR  bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id)
122 {
123     tBNEP_CONN    *p_bcb = bnepu_find_bcb_by_bd_addr (bd_addr);
124     UNUSED(psm);
125 
126     /* If we are not acting as server, or already have a connection, or have */
127     /* no more resources to handle the connection, reject the connection.    */
128     if (!(bnep_cb.profile_registered) || (p_bcb)
129      || ((p_bcb = bnepu_allocate_bcb(bd_addr)) == NULL))
130     {
131         L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_PSM, 0);
132         return;
133     }
134 
135     /* Transition to the next appropriate state, waiting for config setup. */
136     p_bcb->con_state = BNEP_STATE_CFG_SETUP;
137 
138     /* Save the L2CAP Channel ID. */
139     p_bcb->l2cap_cid = l2cap_cid;
140 
141     /* Send response to the L2CAP layer. */
142     L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);
143 
144     /* Send a Configuration Request. */
145     L2CA_ConfigReq (l2cap_cid, &bnep_cb.l2cap_my_cfg);
146 
147     /* Start timer waiting for config setup */
148     btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);
149 
150     BNEP_TRACE_EVENT("BNEP - Rcvd L2CAP conn ind, CID: 0x%x", p_bcb->l2cap_cid);
151 
152 }
153 
154 
155 /*******************************************************************************
156 **
157 ** Function         bnep_connect_cfm
158 **
159 ** Description      This function handles the connect confirm events
160 **                  from L2CAP. This is the case when we are acting as a
161 **                  client and have sent a connect request.
162 **
163 ** Returns          void
164 **
165 *******************************************************************************/
bnep_connect_cfm(UINT16 l2cap_cid,UINT16 result)166 static void bnep_connect_cfm (UINT16 l2cap_cid, UINT16 result)
167 {
168     tBNEP_CONN    *bcb;
169 
170     /* Find CCB based on CID */
171     if ((bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
172     {
173         BNEP_TRACE_WARNING ("BNEP - Rcvd conn cnf for unknown CID 0x%x", l2cap_cid);
174         return;
175     }
176 
177     /* If the connection response contains success status, then */
178     /* Transition to the next state and startup the timer.      */
179     if ((result == L2CAP_CONN_OK) && (bcb->con_state == BNEP_STATE_CONN_START))
180     {
181         bcb->con_state = BNEP_STATE_CFG_SETUP;
182 
183         /* Send a Configuration Request. */
184         L2CA_ConfigReq (l2cap_cid, &bnep_cb.l2cap_my_cfg);
185 
186         /* Start timer waiting for config results */
187         btu_start_timer (&bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);
188 
189         BNEP_TRACE_EVENT ("BNEP - got conn cnf, sent cfg req, CID: 0x%x", bcb->l2cap_cid);
190     }
191     else
192     {
193         BNEP_TRACE_WARNING ("BNEP - Rcvd conn cnf with error: 0x%x  CID 0x%x", result, bcb->l2cap_cid);
194 
195         /* Tell the upper layer, if he has a callback */
196         if (bnep_cb.p_conn_state_cb &&
197             bcb->con_flags & BNEP_FLAGS_IS_ORIG)
198         {
199             (*bnep_cb.p_conn_state_cb) (bcb->handle, bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
200         }
201 
202         bnepu_release_bcb (bcb);
203     }
204 }
205 
206 /*******************************************************************************
207 **
208 ** Function         bnep_config_ind
209 **
210 ** Description      This function processes the L2CAP configuration indication
211 **                  event.
212 **
213 ** Returns          void
214 **
215 *******************************************************************************/
bnep_config_ind(UINT16 l2cap_cid,tL2CAP_CFG_INFO * p_cfg)216 static void bnep_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
217 {
218     tBNEP_CONN    *p_bcb;
219     UINT16        result, mtu = 0;
220 
221     /* Find CCB based on CID */
222     if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
223     {
224         BNEP_TRACE_WARNING ("BNEP - Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
225         return;
226     }
227 
228     BNEP_TRACE_EVENT ("BNEP - Rcvd cfg ind, CID: 0x%x", l2cap_cid);
229 
230     /* Remember the remote MTU size */
231     if ((!p_cfg->mtu_present) || (p_cfg->mtu < BNEP_MIN_MTU_SIZE))
232     {
233         mtu                     = p_cfg->mtu;
234         p_cfg->flush_to_present = FALSE;
235         p_cfg->mtu_present      = TRUE;
236         p_cfg->mtu              = BNEP_MIN_MTU_SIZE;
237         p_cfg->result           = result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
238     }
239     else
240     {
241         if (p_cfg->mtu > BNEP_MTU_SIZE)
242             p_bcb->rem_mtu_size = BNEP_MTU_SIZE;
243         else
244             p_bcb->rem_mtu_size = p_cfg->mtu;
245 
246         /* For now, always accept configuration from the other side */
247         p_cfg->flush_to_present = FALSE;
248         p_cfg->mtu_present      = FALSE;
249         p_cfg->result           = result = L2CAP_CFG_OK;
250     }
251 
252     L2CA_ConfigRsp (l2cap_cid, p_cfg);
253 
254     if (result != L2CAP_CFG_OK)
255     {
256         BNEP_TRACE_EVENT ("BNEP - Rcvd cfg ind with bad MTU %d, CID: 0x%x", mtu, l2cap_cid);
257         return;
258     }
259 
260     p_bcb->con_flags |= BNEP_FLAGS_HIS_CFG_DONE;
261 
262     if (p_bcb->con_flags & BNEP_FLAGS_MY_CFG_DONE)
263     {
264         p_bcb->con_state = BNEP_STATE_SEC_CHECKING;
265 
266         /* Start timer waiting for setup or response */
267         btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_HOST_TIMEOUT);
268 
269         if (p_bcb->con_flags & BNEP_FLAGS_IS_ORIG)
270         {
271             btm_sec_mx_access_request (p_bcb->rem_bda, BT_PSM_BNEP, TRUE,
272                                        BTM_SEC_PROTO_BNEP,
273                                        bnep_get_uuid32(&(p_bcb->src_uuid)),
274                                        &bnep_sec_check_complete, p_bcb);
275         }
276     }
277 }
278 
279 
280 /*******************************************************************************
281 **
282 ** Function         bnep_config_cfm
283 **
284 ** Description      This function processes the L2CAP configuration confirmation
285 **                  event.
286 **
287 ** Returns          void
288 **
289 *******************************************************************************/
bnep_config_cfm(UINT16 l2cap_cid,tL2CAP_CFG_INFO * p_cfg)290 static void bnep_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
291 {
292     tBNEP_CONN    *p_bcb;
293 
294     BNEP_TRACE_EVENT ("BNEP - Rcvd cfg cfm, CID: 0x%x  Result: %d", l2cap_cid, p_cfg->result);
295 
296     /* Find CCB based on CID */
297     if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
298     {
299         BNEP_TRACE_WARNING ("BNEP - Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
300         return;
301     }
302 
303     /* For now, always accept configuration from the other side */
304     if (p_cfg->result == L2CAP_CFG_OK)
305     {
306         p_bcb->con_flags |= BNEP_FLAGS_MY_CFG_DONE;
307 
308         if (p_bcb->con_flags & BNEP_FLAGS_HIS_CFG_DONE)
309         {
310             p_bcb->con_state = BNEP_STATE_SEC_CHECKING;
311 
312             /* Start timer waiting for setup or response */
313             btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_HOST_TIMEOUT);
314 
315             if (p_bcb->con_flags & BNEP_FLAGS_IS_ORIG)
316             {
317                 btm_sec_mx_access_request (p_bcb->rem_bda, BT_PSM_BNEP, TRUE,
318                                            BTM_SEC_PROTO_BNEP,
319                                            bnep_get_uuid32(&(p_bcb->src_uuid)),
320                                            &bnep_sec_check_complete, p_bcb);
321             }
322         }
323     }
324     else
325     {
326         /* Tell the upper layer, if he has a callback */
327         if ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb))
328         {
329             (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED_CFG, FALSE);
330         }
331 
332         L2CA_DisconnectReq (p_bcb->l2cap_cid);
333 
334         bnepu_release_bcb (p_bcb);
335     }
336 }
337 
338 
339 /*******************************************************************************
340 **
341 ** Function         bnep_disconnect_ind
342 **
343 ** Description      This function handles a disconnect event from L2CAP. If
344 **                  requested to, we ack the disconnect before dropping the CCB
345 **
346 ** Returns          void
347 **
348 *******************************************************************************/
bnep_disconnect_ind(UINT16 l2cap_cid,BOOLEAN ack_needed)349 static void bnep_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed)
350 {
351     tBNEP_CONN    *p_bcb;
352 
353     if (ack_needed)
354         L2CA_DisconnectRsp (l2cap_cid);
355 
356     /* Find CCB based on CID */
357     if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
358     {
359         BNEP_TRACE_WARNING ("BNEP - Rcvd L2CAP disc, unknown CID: 0x%x", l2cap_cid);
360         return;
361     }
362 
363     BNEP_TRACE_EVENT ("BNEP - Rcvd L2CAP disc, CID: 0x%x", l2cap_cid);
364 
365     /* Tell the user if he has a callback */
366     if (p_bcb->con_state == BNEP_STATE_CONNECTED)
367     {
368         if (bnep_cb.p_conn_state_cb)
369             (*bnep_cb.p_conn_state_cb)(p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_DISCONNECTED, FALSE);
370     }
371     else
372     {
373         if ((bnep_cb.p_conn_state_cb) && ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) ||
374             (p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)))
375             (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
376     }
377 
378     bnepu_release_bcb (p_bcb);
379 }
380 
381 
382 
383 /*******************************************************************************
384 **
385 ** Function         bnep_disconnect_cfm
386 **
387 ** Description      This function gets the disconnect confirm event from L2CAP
388 **
389 ** Returns          void
390 **
391 *******************************************************************************/
bnep_disconnect_cfm(UINT16 l2cap_cid,UINT16 result)392 static void bnep_disconnect_cfm (UINT16 l2cap_cid, UINT16 result)
393 {
394     BNEP_TRACE_EVENT ("BNEP - Rcvd L2CAP disc cfm, CID: 0x%x, Result 0x%x", l2cap_cid, result);
395 }
396 
397 
398 
399 /*******************************************************************************
400 **
401 ** Function         bnep_congestion_ind
402 **
403 ** Description      This is a callback function called by L2CAP when
404 **                  congestion status changes
405 **
406 *******************************************************************************/
bnep_congestion_ind(UINT16 l2cap_cid,BOOLEAN is_congested)407 static void bnep_congestion_ind (UINT16 l2cap_cid, BOOLEAN is_congested)
408 {
409     tBNEP_CONN    *p_bcb;
410 
411     /* Find BCB based on CID */
412     if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
413     {
414         BNEP_TRACE_WARNING ("BNEP - Rcvd L2CAP cong, unknown CID: 0x%x", l2cap_cid);
415         return;
416     }
417 
418     if (is_congested)
419    {
420         p_bcb->con_flags |= BNEP_FLAGS_L2CAP_CONGESTED;
421        if(bnep_cb.p_tx_data_flow_cb)
422        {
423            bnep_cb.p_tx_data_flow_cb(p_bcb->handle, BNEP_TX_FLOW_OFF);
424        }
425    }
426     else
427     {
428         p_bcb->con_flags &= ~BNEP_FLAGS_L2CAP_CONGESTED;
429 
430        if(bnep_cb.p_tx_data_flow_cb)
431        {
432            bnep_cb.p_tx_data_flow_cb(p_bcb->handle, BNEP_TX_FLOW_ON);
433        }
434 
435         /* While not congested, send as many buffers as we can */
436         while (!(p_bcb->con_flags & BNEP_FLAGS_L2CAP_CONGESTED))
437         {
438             BT_HDR   *p_buf = (BT_HDR *)GKI_dequeue (&p_bcb->xmit_q);
439 
440             if (!p_buf)
441                 break;
442 
443             L2CA_DataWrite (l2cap_cid, p_buf);
444         }
445     }
446 }
447 
448 
449 
450 /*******************************************************************************
451 **
452 ** Function         bnep_data_ind
453 **
454 ** Description      This function is called when data is received from L2CAP.
455 **                  if we are the originator of the connection, we are the SDP
456 **                  client, and the received message is queued up for the client.
457 **
458 **                  If we are the destination of the connection, we are the SDP
459 **                  server, so the message is passed to the server processing
460 **                  function.
461 **
462 ** Returns          void
463 **
464 *******************************************************************************/
bnep_data_ind(UINT16 l2cap_cid,BT_HDR * p_buf)465 static void bnep_data_ind (UINT16 l2cap_cid, BT_HDR *p_buf)
466 {
467     tBNEP_CONN    *p_bcb;
468     UINT8         *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
469     UINT16        rem_len = p_buf->len;
470     UINT8         type, ctrl_type, ext_type = 0;
471     BOOLEAN       extension_present, fw_ext_present;
472     UINT16        protocol = 0;
473     UINT8         *p_src_addr, *p_dst_addr;
474 
475 
476     /* Find CCB based on CID */
477     if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
478     {
479         BNEP_TRACE_WARNING ("BNEP - Rcvd L2CAP data, unknown CID: 0x%x", l2cap_cid);
480         GKI_freebuf (p_buf);
481         return;
482     }
483 
484     /* Get the type and extension bits */
485     type = *p++;
486     extension_present = type >> 7;
487     type &= 0x7f;
488     if ((rem_len <= bnep_frame_hdr_sizes[type]) || (rem_len > BNEP_MTU_SIZE))
489     {
490         BNEP_TRACE_EVENT ("BNEP - rcvd frame, bad len: %d  type: 0x%02x", p_buf->len, type);
491         GKI_freebuf (p_buf);
492         return;
493     }
494 
495     rem_len--;
496 
497     if ((p_bcb->con_state != BNEP_STATE_CONNECTED) &&
498         (!(p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)) &&
499         (type != BNEP_FRAME_CONTROL))
500     {
501         BNEP_TRACE_WARNING ("BNEP - Ignored L2CAP data while in state: %d, CID: 0x%x",
502                             p_bcb->con_state, l2cap_cid);
503 
504         if (extension_present)
505         {
506             /*
507             ** When there is no connection if a data packet is received
508             ** with unknown control extension headers then those should be processed
509             ** according to complain/ignore law
510             */
511             UINT8       ext, length;
512             UINT16      org_len, new_len;
513             /* parse the extension headers and process unknown control headers */
514             org_len = rem_len;
515             new_len = 0;
516             do {
517 
518                 ext     = *p++;
519                 length  = *p++;
520                 p += length;
521 
522                 if ((!(ext & 0x7F)) && (*p > BNEP_FILTER_MULTI_ADDR_RESPONSE_MSG))
523                     bnep_send_command_not_understood (p_bcb, *p);
524 
525                 new_len += (length + 2);
526 
527                 if (new_len > org_len)
528                     break;
529 
530             } while (ext & 0x80);
531         }
532 
533         GKI_freebuf (p_buf);
534         return;
535     }
536 
537     if (type > BNEP_FRAME_COMPRESSED_ETHERNET_DEST_ONLY)
538     {
539         BNEP_TRACE_EVENT ("BNEP - rcvd frame, unknown type: 0x%02x", type);
540         GKI_freebuf (p_buf);
541         return;
542     }
543 
544     BNEP_TRACE_DEBUG ("BNEP - rcv frame, type: %d len: %d Ext: %d", type, p_buf->len, extension_present);
545 
546     /* Initialize addresses to 'not supplied' */
547     p_src_addr = p_dst_addr = NULL;
548 
549     switch (type)
550     {
551     case BNEP_FRAME_GENERAL_ETHERNET:
552         p_dst_addr = p;
553         p += BD_ADDR_LEN;
554         p_src_addr = p;
555         p += BD_ADDR_LEN;
556         BE_STREAM_TO_UINT16 (protocol, p);
557         rem_len -= 14;
558         break;
559 
560     case BNEP_FRAME_CONTROL:
561         ctrl_type = *p;
562         p = bnep_process_control_packet (p_bcb, p, &rem_len, FALSE);
563 
564         if (ctrl_type == BNEP_SETUP_CONNECTION_REQUEST_MSG &&
565             p_bcb->con_state != BNEP_STATE_CONNECTED &&
566             extension_present && p && rem_len)
567         {
568             p_bcb->p_pending_data = (BT_HDR *)GKI_getbuf (rem_len);
569             if (p_bcb->p_pending_data)
570             {
571                 memcpy ((UINT8 *)(p_bcb->p_pending_data + 1), p, rem_len);
572                 p_bcb->p_pending_data->len    = rem_len;
573                 p_bcb->p_pending_data->offset = 0;
574             }
575         }
576         else
577         {
578             while (extension_present && p && rem_len)
579             {
580                 ext_type = *p++;
581                 extension_present = ext_type >> 7;
582                 ext_type &= 0x7F;
583 
584                 /* if unknown extension present stop processing */
585                 if (ext_type)
586                     break;
587 
588                 p = bnep_process_control_packet (p_bcb, p, &rem_len, TRUE);
589             }
590         }
591         GKI_freebuf (p_buf);
592         return;
593 
594     case BNEP_FRAME_COMPRESSED_ETHERNET:
595         BE_STREAM_TO_UINT16 (protocol, p);
596         rem_len -= 2;
597         break;
598 
599     case BNEP_FRAME_COMPRESSED_ETHERNET_SRC_ONLY:
600         p_src_addr = p;
601         p += BD_ADDR_LEN;
602         BE_STREAM_TO_UINT16 (protocol, p);
603         rem_len -= 8;
604         break;
605 
606     case BNEP_FRAME_COMPRESSED_ETHERNET_DEST_ONLY:
607         p_dst_addr = p;
608         p += BD_ADDR_LEN;
609         BE_STREAM_TO_UINT16 (protocol, p);
610         rem_len -= 8;
611         break;
612     }
613 
614     /* Process the header extension if there is one */
615     while (extension_present && p && rem_len)
616     {
617         ext_type = *p;
618         extension_present = ext_type >> 7;
619         ext_type &= 0x7F;
620 
621         /* if unknown extension present stop processing */
622         if (ext_type)
623         {
624             BNEP_TRACE_EVENT ("Data extension type 0x%x found", ext_type);
625             break;
626         }
627 
628         p++;
629         rem_len--;
630         p = bnep_process_control_packet (p_bcb, p, &rem_len, TRUE);
631     }
632 
633     p_buf->offset += p_buf->len - rem_len;
634     p_buf->len     = rem_len;
635 
636     /* Always give the upper layer MAC addresses */
637     if (!p_src_addr)
638         p_src_addr = (UINT8 *) p_bcb->rem_bda;
639 
640     if (!p_dst_addr)
641         p_dst_addr = (UINT8 *) controller_get_interface()->get_address();
642 
643     /* check whether there are any extensions to be forwarded */
644     if (ext_type)
645         fw_ext_present = TRUE;
646     else
647         fw_ext_present = FALSE;
648 
649     if (bnep_cb.p_data_buf_cb)
650     {
651         (*bnep_cb.p_data_buf_cb)(p_bcb->handle, p_src_addr, p_dst_addr, protocol, p_buf, fw_ext_present);
652     }
653     else if (bnep_cb.p_data_ind_cb)
654     {
655         (*bnep_cb.p_data_ind_cb)(p_bcb->handle, p_src_addr, p_dst_addr, protocol, p, rem_len, fw_ext_present);
656         GKI_freebuf (p_buf);
657     }
658 }
659 
660 
661 
662 /*******************************************************************************
663 **
664 ** Function         bnep_process_timeout
665 **
666 ** Description      This function processes a timeout. If it is a startup
667 **                  timeout, we check for reading our BD address. If it
668 **                  is an L2CAP timeout, we send a disconnect req to L2CAP.
669 **
670 ** Returns          void
671 **
672 *******************************************************************************/
bnep_process_timeout(TIMER_LIST_ENT * p_tle)673 void bnep_process_timeout (TIMER_LIST_ENT  *p_tle)
674 {
675     tBNEP_CONN *p_bcb;
676 
677     if (!p_tle->param)
678     {
679         return;
680     }
681 
682     p_bcb = (tBNEP_CONN *)p_tle->param;
683 
684     BNEP_TRACE_EVENT ("BNEP - CCB timeout in state: %d  CID: 0x%x flags %x, re_transmit %d",
685                        p_bcb->con_state, p_bcb->l2cap_cid, p_bcb->con_flags, p_bcb->re_transmits);
686 
687     if (p_bcb->con_state == BNEP_STATE_CONN_SETUP)
688     {
689         BNEP_TRACE_EVENT ("BNEP - CCB timeout in state: %d  CID: 0x%x",
690                            p_bcb->con_state, p_bcb->l2cap_cid);
691 
692         if (!(p_bcb->con_flags & BNEP_FLAGS_IS_ORIG))
693         {
694             L2CA_DisconnectReq (p_bcb->l2cap_cid);
695 
696             bnepu_release_bcb (p_bcb);
697             return;
698         }
699 
700         if (p_bcb->re_transmits++ != BNEP_MAX_RETRANSMITS)
701         {
702             bnep_send_conn_req (p_bcb);
703             btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);
704         }
705         else
706         {
707             L2CA_DisconnectReq (p_bcb->l2cap_cid);
708 
709             if ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb))
710                 (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
711 
712             bnepu_release_bcb (p_bcb);
713             return;
714         }
715     }
716     else if (p_bcb->con_state != BNEP_STATE_CONNECTED)
717     {
718         BNEP_TRACE_EVENT ("BNEP - CCB timeout in state: %d  CID: 0x%x",
719                            p_bcb->con_state, p_bcb->l2cap_cid);
720 
721         L2CA_DisconnectReq (p_bcb->l2cap_cid);
722 
723         /* Tell the user if he has a callback */
724         if ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb))
725             (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
726 
727         bnepu_release_bcb (p_bcb);
728     }
729     else if (p_bcb->con_flags & BNEP_FLAGS_FILTER_RESP_PEND)
730     {
731         if (p_bcb->re_transmits++ != BNEP_MAX_RETRANSMITS)
732         {
733             bnepu_send_peer_our_filters (p_bcb);
734             btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_FILTER_SET_TIMEOUT);
735         }
736         else
737         {
738             L2CA_DisconnectReq (p_bcb->l2cap_cid);
739 
740             /* Tell the user if he has a callback */
741             if (bnep_cb.p_conn_state_cb)
742                 (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_SET_FILTER_FAIL, FALSE);
743 
744             bnepu_release_bcb (p_bcb);
745             return;
746         }
747     }
748     else if (p_bcb->con_flags & BNEP_FLAGS_MULTI_RESP_PEND)
749     {
750         if (p_bcb->re_transmits++ != BNEP_MAX_RETRANSMITS)
751         {
752             bnepu_send_peer_our_multi_filters (p_bcb);
753             btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_FILTER_SET_TIMEOUT);
754         }
755         else
756         {
757             L2CA_DisconnectReq (p_bcb->l2cap_cid);
758 
759             /* Tell the user if he has a callback */
760             if (bnep_cb.p_conn_state_cb)
761                 (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_SET_FILTER_FAIL, FALSE);
762 
763             bnepu_release_bcb (p_bcb);
764             return;
765         }
766     }
767 }
768 
769 
770 /*******************************************************************************
771 **
772 ** Function         bnep_connected
773 **
774 ** Description      This function is called when a connection is established
775 **                  (after config).
776 **
777 ** Returns          void
778 **
779 *******************************************************************************/
bnep_connected(tBNEP_CONN * p_bcb)780 void bnep_connected (tBNEP_CONN *p_bcb)
781 {
782     BOOLEAN     is_role_change;
783 
784     if (p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)
785         is_role_change = TRUE;
786     else
787         is_role_change = FALSE;
788 
789     p_bcb->con_state = BNEP_STATE_CONNECTED;
790     p_bcb->con_flags |= BNEP_FLAGS_CONN_COMPLETED;
791     p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD);
792 
793     /* Ensure timer is stopped */
794     btu_stop_timer (&p_bcb->conn_tle);
795     p_bcb->re_transmits = 0;
796 
797     /* Tell the upper layer, if he has a callback */
798     if (bnep_cb.p_conn_state_cb)
799         (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_SUCCESS, is_role_change);
800 }
801