summaryrefslogtreecommitdiffstats
path: root/stack/bnep/bnep_main.c
blob: 12c9da03f6e007411df0dbe8f8cb572e53d83810 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/******************************************************************************
 *
 *  Copyright (C) 2001-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  this file contains the main BNEP functions
 *
 ******************************************************************************/

#include "bt_target.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "gki.h"
#include "bt_types.h"
#include "l2cdefs.h"
#include "hcidefs.h"
#include "hcimsgs.h"

#include "l2c_api.h"
#include "l2cdefs.h"

#include "btu.h"
#include "btm_api.h"

#include "bnep_api.h"
#include "bnep_int.h"


/********************************************************************************/
/*                       G L O B A L    B N E P       D A T A                   */
/********************************************************************************/
#if BNEP_DYNAMIC_MEMORY == FALSE
tBNEP_CB   bnep_cb;
#endif

const UINT16 bnep_frame_hdr_sizes[] = {14, 1, 2, 8, 8};

/********************************************************************************/
/*              L O C A L    F U N C T I O N     P R O T O T Y P E S            */
/********************************************************************************/
static void bnep_connect_ind (BD_ADDR  bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id);
static void bnep_connect_cfm (UINT16 l2cap_cid, UINT16 result);
static void bnep_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
static void bnep_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg);
static void bnep_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed);
static void bnep_disconnect_cfm (UINT16 l2cap_cid, UINT16 result);
static void bnep_data_ind (UINT16 l2cap_cid, BT_HDR *p_msg);
static void bnep_congestion_ind (UINT16 lcid, BOOLEAN is_congested);

static void bnep_read_addr_cb (void *p_bda);


/*******************************************************************************
**
** Function         bnep_register_with_l2cap
**
** Description      This function registers BNEP PSM with L2CAP
**
** Returns          void
**
*******************************************************************************/
tBNEP_RESULT bnep_register_with_l2cap (void)
{
    /* Initialize the L2CAP configuration. We only care about MTU and flush */
    memset(&bnep_cb.l2cap_my_cfg, 0, sizeof(tL2CAP_CFG_INFO));

    bnep_cb.l2cap_my_cfg.mtu_present            = TRUE;
    bnep_cb.l2cap_my_cfg.mtu                    = BNEP_MTU_SIZE;
    bnep_cb.l2cap_my_cfg.flush_to_present       = TRUE;
    bnep_cb.l2cap_my_cfg.flush_to               = BNEP_FLUSH_TO;

    bnep_cb.reg_info.pL2CA_ConnectInd_Cb        = bnep_connect_ind;
    bnep_cb.reg_info.pL2CA_ConnectCfm_Cb        = bnep_connect_cfm;
    bnep_cb.reg_info.pL2CA_ConfigInd_Cb         = bnep_config_ind;
    bnep_cb.reg_info.pL2CA_ConfigCfm_Cb         = bnep_config_cfm;
    bnep_cb.reg_info.pL2CA_DisconnectInd_Cb     = bnep_disconnect_ind;
    bnep_cb.reg_info.pL2CA_DisconnectCfm_Cb     = bnep_disconnect_cfm;
    bnep_cb.reg_info.pL2CA_DataInd_Cb           = bnep_data_ind;
    bnep_cb.reg_info.pL2CA_CongestionStatus_Cb  = bnep_congestion_ind;

    /* Now, register with L2CAP */
    if (!L2CA_Register (BT_PSM_BNEP, &bnep_cb.reg_info))
    {
        BNEP_TRACE_ERROR0 ("BNEP - Registration failed");
        return BNEP_SECURITY_FAIL;
    }

    return BNEP_SUCCESS;
}


/*******************************************************************************
**
** Function         bnep_connect_ind
**
** Description      This function handles an inbound connection indication
**                  from L2CAP. This is the case where we are acting as a
**                  server.
**
** Returns          void
**
*******************************************************************************/
static void bnep_connect_ind (BD_ADDR  bd_addr, UINT16 l2cap_cid, UINT16 psm, UINT8 l2cap_id)
{
    tBNEP_CONN    *p_bcb = bnepu_find_bcb_by_bd_addr (bd_addr);

    /* If we are not acting as server, or already have a connection, or have */
    /* no more resources to handle the connection, reject the connection.    */
    if (!(bnep_cb.profile_registered) || (p_bcb)
     || ((p_bcb = bnepu_allocate_bcb(bd_addr)) == NULL))
    {
        L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_NO_PSM, 0);
        return;
    }

    /* Transition to the next appropriate state, waiting for config setup. */
    p_bcb->con_state = BNEP_STATE_CFG_SETUP;

    /* Save the L2CAP Channel ID. */
    p_bcb->l2cap_cid = l2cap_cid;

    /* Send response to the L2CAP layer. */
    L2CA_ConnectRsp (bd_addr, l2cap_id, l2cap_cid, L2CAP_CONN_OK, L2CAP_CONN_OK);

    /* Send a Configuration Request. */
    L2CA_ConfigReq (l2cap_cid, &bnep_cb.l2cap_my_cfg);

    /* Start timer waiting for config setup */
    btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);

    BNEP_TRACE_EVENT1("BNEP - Rcvd L2CAP conn ind, CID: 0x%x", p_bcb->l2cap_cid);

}


/*******************************************************************************
**
** Function         bnep_connect_cfm
**
** Description      This function handles the connect confirm events
**                  from L2CAP. This is the case when we are acting as a
**                  client and have sent a connect request.
**
** Returns          void
**
*******************************************************************************/
static void bnep_connect_cfm (UINT16 l2cap_cid, UINT16 result)
{
    tBNEP_CONN    *bcb;

    /* Find CCB based on CID */
    if ((bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
    {
        BNEP_TRACE_WARNING1 ("BNEP - Rcvd conn cnf for unknown CID 0x%x", l2cap_cid);
        return;
    }

    /* If the connection response contains success status, then */
    /* Transition to the next state and startup the timer.      */
    if ((result == L2CAP_CONN_OK) && (bcb->con_state == BNEP_STATE_CONN_START))
    {
        bcb->con_state = BNEP_STATE_CFG_SETUP;

        /* Send a Configuration Request. */
        L2CA_ConfigReq (l2cap_cid, &bnep_cb.l2cap_my_cfg);

        /* Start timer waiting for config results */
        btu_start_timer (&bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);

        BNEP_TRACE_EVENT1 ("BNEP - got conn cnf, sent cfg req, CID: 0x%x", bcb->l2cap_cid);
    }
    else
    {
        BNEP_TRACE_WARNING2 ("BNEP - Rcvd conn cnf with error: 0x%x  CID 0x%x", result, bcb->l2cap_cid);

        /* Tell the upper layer, if he has a callback */
        if (bnep_cb.p_conn_state_cb &&
            bcb->con_flags & BNEP_FLAGS_IS_ORIG)
        {
            (*bnep_cb.p_conn_state_cb) (bcb->handle, bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
        }

        bnepu_release_bcb (bcb);
    }
}

/*******************************************************************************
**
** Function         bnep_config_ind
**
** Description      This function processes the L2CAP configuration indication
**                  event.
**
** Returns          void
**
*******************************************************************************/
static void bnep_config_ind (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
{
    tBNEP_CONN    *p_bcb;
    UINT16        result, mtu = 0;

    /* Find CCB based on CID */
    if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
    {
        BNEP_TRACE_WARNING1 ("BNEP - Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
        return;
    }

    BNEP_TRACE_EVENT1 ("BNEP - Rcvd cfg ind, CID: 0x%x", l2cap_cid);

    /* Remember the remote MTU size */
    if ((!p_cfg->mtu_present) || (p_cfg->mtu < BNEP_MIN_MTU_SIZE))
    {
        mtu                     = p_cfg->mtu;
        p_cfg->flush_to_present = FALSE;
        p_cfg->mtu_present      = TRUE;
        p_cfg->mtu              = BNEP_MIN_MTU_SIZE;
        p_cfg->result           = result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
    }
    else
    {
        if (p_cfg->mtu > BNEP_MTU_SIZE)
            p_bcb->rem_mtu_size = BNEP_MTU_SIZE;
        else
            p_bcb->rem_mtu_size = p_cfg->mtu;

        /* For now, always accept configuration from the other side */
        p_cfg->flush_to_present = FALSE;
        p_cfg->mtu_present      = FALSE;
        p_cfg->result           = result = L2CAP_CFG_OK;
    }

    L2CA_ConfigRsp (l2cap_cid, p_cfg);

    if (result != L2CAP_CFG_OK)
    {
        BNEP_TRACE_EVENT2 ("BNEP - Rcvd cfg ind with bad MTU %d, CID: 0x%x", mtu, l2cap_cid);
        return;
    }

    p_bcb->con_flags |= BNEP_FLAGS_HIS_CFG_DONE;

    if (p_bcb->con_flags & BNEP_FLAGS_MY_CFG_DONE)
    {
        p_bcb->con_state = BNEP_STATE_SEC_CHECKING;

        /* Start timer waiting for setup or response */
        btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_HOST_TIMEOUT);

        if (p_bcb->con_flags & BNEP_FLAGS_IS_ORIG)
        {
            btm_sec_mx_access_request (p_bcb->rem_bda, BT_PSM_BNEP, TRUE,
                                       BTM_SEC_PROTO_BNEP,
                                       bnep_get_uuid32(&(p_bcb->src_uuid)),
                                       &bnep_sec_check_complete, p_bcb);
        }
    }
}


/*******************************************************************************
**
** Function         bnep_config_cfm
**
** Description      This function processes the L2CAP configuration confirmation
**                  event.
**
** Returns          void
**
*******************************************************************************/
static void bnep_config_cfm (UINT16 l2cap_cid, tL2CAP_CFG_INFO *p_cfg)
{
    tBNEP_CONN    *p_bcb;

    BNEP_TRACE_EVENT2 ("BNEP - Rcvd cfg cfm, CID: 0x%x  Result: %d", l2cap_cid, p_cfg->result);

    /* Find CCB based on CID */
    if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
    {
        BNEP_TRACE_WARNING1 ("BNEP - Rcvd L2CAP cfg ind, unknown CID: 0x%x", l2cap_cid);
        return;
    }

    /* For now, always accept configuration from the other side */
    if (p_cfg->result == L2CAP_CFG_OK)
    {
        p_bcb->con_flags |= BNEP_FLAGS_MY_CFG_DONE;

        if (p_bcb->con_flags & BNEP_FLAGS_HIS_CFG_DONE)
        {
            p_bcb->con_state = BNEP_STATE_SEC_CHECKING;

            /* Start timer waiting for setup or response */
            btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_HOST_TIMEOUT);

            if (p_bcb->con_flags & BNEP_FLAGS_IS_ORIG)
            {
                btm_sec_mx_access_request (p_bcb->rem_bda, BT_PSM_BNEP, TRUE,
                                           BTM_SEC_PROTO_BNEP,
                                           bnep_get_uuid32(&(p_bcb->src_uuid)),
                                           &bnep_sec_check_complete, p_bcb);
            }
        }
    }
    else
    {
        /* Tell the upper layer, if he has a callback */
        if ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb))
        {
            (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED_CFG, FALSE);
        }

        L2CA_DisconnectReq (p_bcb->l2cap_cid);

        bnepu_release_bcb (p_bcb);
    }
}


/*******************************************************************************
**
** Function         bnep_disconnect_ind
**
** Description      This function handles a disconnect event from L2CAP. If
**                  requested to, we ack the disconnect before dropping the CCB
**
** Returns          void
**
*******************************************************************************/
static void bnep_disconnect_ind (UINT16 l2cap_cid, BOOLEAN ack_needed)
{
    tBNEP_CONN    *p_bcb;

    if (ack_needed)
        L2CA_DisconnectRsp (l2cap_cid);

    /* Find CCB based on CID */
    if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
    {
        BNEP_TRACE_WARNING1 ("BNEP - Rcvd L2CAP disc, unknown CID: 0x%x", l2cap_cid);
        return;
    }

    BNEP_TRACE_EVENT1 ("BNEP - Rcvd L2CAP disc, CID: 0x%x", l2cap_cid);

    /* Tell the user if he has a callback */
    if (p_bcb->con_state == BNEP_STATE_CONNECTED)
    {
        if (bnep_cb.p_conn_state_cb)
            (*bnep_cb.p_conn_state_cb)(p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_DISCONNECTED, FALSE);
    }
    else
    {
        if (((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb)) ||
            p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)
            (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);
    }

    bnepu_release_bcb (p_bcb);
}



/*******************************************************************************
**
** Function         bnep_disconnect_cfm
**
** Description      This function gets the disconnect confirm event from L2CAP
**
** Returns          void
**
*******************************************************************************/
static void bnep_disconnect_cfm (UINT16 l2cap_cid, UINT16 result)
{
    BNEP_TRACE_EVENT2 ("BNEP - Rcvd L2CAP disc cfm, CID: 0x%x, Result 0x%x", l2cap_cid, result);
}



/*******************************************************************************
**
** Function         bnep_congestion_ind
**
** Description      This is a callback function called by L2CAP when
**                  congestion status changes
**
*******************************************************************************/
static void bnep_congestion_ind (UINT16 l2cap_cid, BOOLEAN is_congested)
{
    tBNEP_CONN    *p_bcb;

    /* Find BCB based on CID */
    if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
    {
        BNEP_TRACE_WARNING1 ("BNEP - Rcvd L2CAP cong, unknown CID: 0x%x", l2cap_cid);
        return;
    }

    if (is_congested)
   {
        p_bcb->con_flags |= BNEP_FLAGS_L2CAP_CONGESTED;
       if(bnep_cb.p_tx_data_flow_cb)
       {
           bnep_cb.p_tx_data_flow_cb(p_bcb->handle, BNEP_TX_FLOW_OFF);
       }
   }
    else
    {
        p_bcb->con_flags &= ~BNEP_FLAGS_L2CAP_CONGESTED;

       if(bnep_cb.p_tx_data_flow_cb)
       {
           bnep_cb.p_tx_data_flow_cb(p_bcb->handle, BNEP_TX_FLOW_ON);
       }

        /* While not congested, send as many buffers as we can */
        while (!(p_bcb->con_flags & BNEP_FLAGS_L2CAP_CONGESTED))
        {
            BT_HDR   *p_buf = (BT_HDR *)GKI_dequeue (&p_bcb->xmit_q);

            if (!p_buf)
                break;

            L2CA_DataWrite (l2cap_cid, p_buf);
        }
    }
}



/*******************************************************************************
**
** Function         bnep_data_ind
**
** Description      This function is called when data is received from L2CAP.
**                  if we are the originator of the connection, we are the SDP
**                  client, and the received message is queued up for the client.
**
**                  If we are the destination of the connection, we are the SDP
**                  server, so the message is passed to the server processing
**                  function.
**
** Returns          void
**
*******************************************************************************/
static void bnep_data_ind (UINT16 l2cap_cid, BT_HDR *p_buf)
{
    tBNEP_CONN    *p_bcb;
    UINT8         *p = (UINT8 *)(p_buf + 1) + p_buf->offset;
    UINT16        rem_len = p_buf->len;
    UINT8         type, ctrl_type, ext_type = 0;
    BOOLEAN       extension_present, fw_ext_present;
    UINT16        protocol = 0;
    UINT8         *p_src_addr, *p_dst_addr;


    /* Find CCB based on CID */
    if ((p_bcb = bnepu_find_bcb_by_cid (l2cap_cid)) == NULL)
    {
        BNEP_TRACE_WARNING1 ("BNEP - Rcvd L2CAP data, unknown CID: 0x%x", l2cap_cid);
        GKI_freebuf (p_buf);
        return;
    }

    /* Get the type and extension bits */
    type = *p++;
    extension_present = type >> 7;
    type &= 0x7f;
    if ((rem_len <= bnep_frame_hdr_sizes[type]) || (rem_len > BNEP_MTU_SIZE))
    {
        BNEP_TRACE_EVENT2 ("BNEP - rcvd frame, bad len: %d  type: 0x%02x", p_buf->len, type);
        GKI_freebuf (p_buf);
        return;
    }

    rem_len--;

    if ((p_bcb->con_state != BNEP_STATE_CONNECTED) &&
        (!(p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)) &&
        (type != BNEP_FRAME_CONTROL))
    {
        BNEP_TRACE_WARNING2 ("BNEP - Ignored L2CAP data while in state: %d, CID: 0x%x",
                            p_bcb->con_state, l2cap_cid);

        if (extension_present)
        {
            /*
            ** When there is no connection if a data packet is received
            ** with unknown control extension headers then those should be processed
            ** according to complain/ignore law
            */
            UINT8       ext, length, *p_data;
            UINT16      org_len, new_len;
            /* parse the extension headers and process unknown control headers */
            org_len = rem_len;
            new_len = 0;
            p_data  = p;
            do {

                ext     = *p++;
                length  = *p++;
                p += length;

                if ((!(ext & 0x7F)) && (*p > BNEP_FILTER_MULTI_ADDR_RESPONSE_MSG))
                    bnep_send_command_not_understood (p_bcb, *p);

                new_len += (length + 2);

                if (new_len > org_len)
                    break;

            } while (ext & 0x80);
        }

        GKI_freebuf (p_buf);
        return;
    }

    if (type > BNEP_FRAME_COMPRESSED_ETHERNET_DEST_ONLY)
    {
        BNEP_TRACE_EVENT1 ("BNEP - rcvd frame, unknown type: 0x%02x", type);
        GKI_freebuf (p_buf);
        return;
    }

    BNEP_TRACE_DEBUG3 ("BNEP - rcv frame, type: %d len: %d Ext: %d", type, p_buf->len, extension_present);

    /* Initialize addresses to 'not supplied' */
    p_src_addr = p_dst_addr = NULL;

    switch (type)
    {
    case BNEP_FRAME_GENERAL_ETHERNET:
        p_dst_addr = p;
        p += BD_ADDR_LEN;
        p_src_addr = p;
        p += BD_ADDR_LEN;
        BE_STREAM_TO_UINT16 (protocol, p);
        rem_len -= 14;
        break;

    case BNEP_FRAME_CONTROL:
        ctrl_type = *p;
        p = bnep_process_control_packet (p_bcb, p, &rem_len, FALSE);

        if (ctrl_type == BNEP_SETUP_CONNECTION_REQUEST_MSG &&
            p_bcb->con_state != BNEP_STATE_CONNECTED &&
            extension_present && p && rem_len)
        {
            p_bcb->p_pending_data = (BT_HDR *)GKI_getbuf (rem_len);
            if (p_bcb->p_pending_data)
            {
                memcpy ((UINT8 *)(p_bcb->p_pending_data + 1), p, rem_len);
                p_bcb->p_pending_data->len    = rem_len;
                p_bcb->p_pending_data->offset = 0;
            }
        }
        else
        {
            while (extension_present && p && rem_len)
            {
                ext_type = *p++;
                extension_present = ext_type >> 7;
                ext_type &= 0x7F;

                /* if unknown extension present stop processing */
                if (ext_type)
                    break;

                p = bnep_process_control_packet (p_bcb, p, &rem_len, TRUE);
            }
        }
        GKI_freebuf (p_buf);
        return;

    case BNEP_FRAME_COMPRESSED_ETHERNET:
        BE_STREAM_TO_UINT16 (protocol, p);
        rem_len -= 2;
        break;

    case BNEP_FRAME_COMPRESSED_ETHERNET_SRC_ONLY:
        p_src_addr = p;
        p += BD_ADDR_LEN;
        BE_STREAM_TO_UINT16 (protocol, p);
        rem_len -= 8;
        break;

    case BNEP_FRAME_COMPRESSED_ETHERNET_DEST_ONLY:
        p_dst_addr = p;
        p += BD_ADDR_LEN;
        BE_STREAM_TO_UINT16 (protocol, p);
        rem_len -= 8;
        break;
    }

    /* Process the header extension if there is one */
    while (extension_present && p && rem_len)
    {
        ext_type = *p;
        extension_present = ext_type >> 7;
        ext_type &= 0x7F;

        /* if unknown extension present stop processing */
        if (ext_type)
        {
            BNEP_TRACE_EVENT1 ("Data extension type 0x%x found", ext_type);
            break;
        }

        p++;
        rem_len--;
        p = bnep_process_control_packet (p_bcb, p, &rem_len, TRUE);
    }

    p_buf->offset += p_buf->len - rem_len;
    p_buf->len     = rem_len;

    /* Always give the upper layer MAC addresses */
    if (!p_src_addr)
        p_src_addr = (UINT8 *) p_bcb->rem_bda;

    if (!p_dst_addr)
        p_dst_addr = (UINT8 *) bnep_cb.my_bda;

    /* check whether there are any extensions to be forwarded */
    if (ext_type)
        fw_ext_present = TRUE;
    else
        fw_ext_present = FALSE;

    if (bnep_cb.p_data_buf_cb)
    {
        (*bnep_cb.p_data_buf_cb)(p_bcb->handle, p_src_addr, p_dst_addr, protocol, p_buf, fw_ext_present);
    }
    else if (bnep_cb.p_data_ind_cb)
    {
        (*bnep_cb.p_data_ind_cb)(p_bcb->handle, p_src_addr, p_dst_addr, protocol, p, rem_len, fw_ext_present);
        GKI_freebuf (p_buf);
    }
}



/*******************************************************************************
**
** Function         bnep_process_timeout
**
** Description      This function processes a timeout. If it is a startup
**                  timeout, we check for reading our BD address. If it
**                  is an L2CAP timeout, we send a disconnect req to L2CAP.
**
** Returns          void
**
*******************************************************************************/
void bnep_process_timeout (TIMER_LIST_ENT  *p_tle)
{
    tBNEP_CONN *p_bcb;

    if (!p_tle->param)
    {
        if (!bnep_cb.got_my_bd_addr)
        {
            if (BTM_IsDeviceUp())
                BTM_ReadLocalDeviceAddr (bnep_read_addr_cb);

            btu_start_timer (&bnep_cb.bnep_tle, BTU_TTYPE_BNEP, 2);
        }
        return;
    }

    p_bcb = (tBNEP_CONN *)p_tle->param;

    BNEP_TRACE_EVENT4 ("BNEP - CCB timeout in state: %d  CID: 0x%x flags %x, re_transmit %d",
                       p_bcb->con_state, p_bcb->l2cap_cid, p_bcb->con_flags, p_bcb->re_transmits);

    if (p_bcb->con_state == BNEP_STATE_CONN_SETUP)
    {
        BNEP_TRACE_EVENT2 ("BNEP - CCB timeout in state: %d  CID: 0x%x",
                           p_bcb->con_state, p_bcb->l2cap_cid);

        if (!(p_bcb->con_flags & BNEP_FLAGS_IS_ORIG))
        {
            L2CA_DisconnectReq (p_bcb->l2cap_cid);

            bnepu_release_bcb (p_bcb);
            return;
        }

        if (p_bcb->re_transmits++ != BNEP_MAX_RETRANSMITS)
        {
            bnep_send_conn_req (p_bcb);
            btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_CONN_TIMEOUT);
        }
        else
        {
            L2CA_DisconnectReq (p_bcb->l2cap_cid);

            if ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb))
                (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);

            bnepu_release_bcb (p_bcb);
            return;
        }
    }
    else if (p_bcb->con_state != BNEP_STATE_CONNECTED)
    {
        BNEP_TRACE_EVENT2 ("BNEP - CCB timeout in state: %d  CID: 0x%x",
                           p_bcb->con_state, p_bcb->l2cap_cid);

        L2CA_DisconnectReq (p_bcb->l2cap_cid);

        /* Tell the user if he has a callback */
        if ((p_bcb->con_flags & BNEP_FLAGS_IS_ORIG) && (bnep_cb.p_conn_state_cb))
            (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_CONN_FAILED, FALSE);

        bnepu_release_bcb (p_bcb);
    }
#if (defined (BNEP_SUPPORTS_PROT_FILTERS) && BNEP_SUPPORTS_PROT_FILTERS == TRUE)
    else if (p_bcb->con_flags & BNEP_FLAGS_FILTER_RESP_PEND)
    {
        if (p_bcb->re_transmits++ != BNEP_MAX_RETRANSMITS)
        {
            bnepu_send_peer_our_filters (p_bcb);
            btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_FILTER_SET_TIMEOUT);
        }
        else
        {
            L2CA_DisconnectReq (p_bcb->l2cap_cid);

            /* Tell the user if he has a callback */
            if (bnep_cb.p_conn_state_cb)
                (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_SET_FILTER_FAIL, FALSE);

            bnepu_release_bcb (p_bcb);
            return;
        }
    }
#endif
#if (defined (BNEP_SUPPORTS_MULTI_FILTERS) && BNEP_SUPPORTS_MULTI_FILTERS == TRUE)
    else if (p_bcb->con_flags & BNEP_FLAGS_MULTI_RESP_PEND)
    {
        if (p_bcb->re_transmits++ != BNEP_MAX_RETRANSMITS)
        {
            bnepu_send_peer_our_multi_filters (p_bcb);
            btu_start_timer (&p_bcb->conn_tle, BTU_TTYPE_BNEP, BNEP_FILTER_SET_TIMEOUT);
        }
        else
        {
            L2CA_DisconnectReq (p_bcb->l2cap_cid);

            /* Tell the user if he has a callback */
            if (bnep_cb.p_conn_state_cb)
                (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_SET_FILTER_FAIL, FALSE);

            bnepu_release_bcb (p_bcb);
            return;
        }
    }
#endif
}


/*******************************************************************************
**
** Function         bnep_connected
**
** Description      This function is called when a connection is established
**                  (after config).
**
** Returns          void
**
*******************************************************************************/
void bnep_connected (tBNEP_CONN *p_bcb)
{
    BOOLEAN     is_role_change;

    if (p_bcb->con_flags & BNEP_FLAGS_CONN_COMPLETED)
        is_role_change = TRUE;
    else
        is_role_change = FALSE;

    p_bcb->con_state = BNEP_STATE_CONNECTED;
    p_bcb->con_flags |= BNEP_FLAGS_CONN_COMPLETED;
    p_bcb->con_flags &= (~BNEP_FLAGS_SETUP_RCVD);

    /* Ensure timer is stopped */
    btu_stop_timer (&p_bcb->conn_tle);
    p_bcb->re_transmits = 0;

    /* Tell the upper layer, if he has a callback */
    if (bnep_cb.p_conn_state_cb)
        (*bnep_cb.p_conn_state_cb) (p_bcb->handle, p_bcb->rem_bda, BNEP_SUCCESS, is_role_change);
}


/*******************************************************************************
**
** Function         bnep_read_addr_cb
**
** Description      This function is called by BTM when the local BD address
**                  is read. It saves the BD address, and flags it as read.
**
** Returns          void
**
*******************************************************************************/
static void bnep_read_addr_cb (void *p_bda)
{
    UINT8 *bda = (UINT8 *)p_bda;
    if (p_bda &&
            (bda[0] | bda[1] | bda[2] | bda[3] | bda[4] | bda[5]) != 0)
    {
        /* Save my BD address */
        memcpy (bnep_cb.my_bda, p_bda, BD_ADDR_LEN);

        bnep_cb.got_my_bd_addr = TRUE;
    }
    else
        /* Retry after a couple seconds */
        btu_start_timer (&bnep_cb.bnep_tle, BTU_TTYPE_BNEP, 2);
}