summaryrefslogtreecommitdiffstats
path: root/stack/mcap/mca_main.c
blob: 2e05d5ee48ab1bc20fa562c8f57744cc565d0226 (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
/******************************************************************************
 *
 *  Copyright (C) 2009-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 is the implementation file for the MCAP Main Control Block and
 *  Utility functions.
 *
 ******************************************************************************/
#include <string.h>

#include "bt_target.h"
#include "gki.h"
#include "mca_api.h"
#include "mca_defs.h"
#include "mca_int.h"
#include "wcassert.h"
#include "l2c_api.h"

/* Main Control block for MCA */
#if MCA_DYNAMIC_MEMORY == FALSE
tMCA_CB mca_cb;
#endif

/*****************************************************************************
** constants
*****************************************************************************/

/* table of standard opcode message size */
const UINT8 mca_std_msg_len[MCA_NUM_STANDARD_OPCODE] = {
    4,          /* MCA_OP_ERROR_RSP         */
    5,          /* MCA_OP_MDL_CREATE_REQ    */
    5,          /* MCA_OP_MDL_CREATE_RSP    */
    3,          /* MCA_OP_MDL_RECONNECT_REQ */
    4,          /* MCA_OP_MDL_RECONNECT_RSP */
    3,          /* MCA_OP_MDL_ABORT_REQ     */
    4,          /* MCA_OP_MDL_ABORT_RSP     */
    3,          /* MCA_OP_MDL_DELETE_REQ    */
    4           /* MCA_OP_MDL_DELETE_RSP    */
};


/*******************************************************************************
**
** Function         mca_handle_by_cpsm
**
** Description      This function returns the handle for the given control
**                  channel PSM. 0, if not found.
**
** Returns          the MCA handle.
**
*******************************************************************************/
tMCA_HANDLE mca_handle_by_cpsm(UINT16 psm)
{
    int     i;
    tMCA_HANDLE handle = 0;
    tMCA_RCB *p_rcb = &mca_cb.rcb[0];

    for (i=0; i<MCA_NUM_REGS; i++, p_rcb++)
    {
        if (p_rcb->p_cback && p_rcb->reg.ctrl_psm == psm)
        {
            handle = i+1;
            break;
        }
    }
    return handle;
}

/*******************************************************************************
**
** Function         mca_handle_by_dpsm
**
** Description      This function returns the handle for the given data
**                  channel PSM. 0, if not found.
**
** Returns          the MCA handle.
**
*******************************************************************************/
tMCA_HANDLE mca_handle_by_dpsm(UINT16 psm)
{
    int     i;
    tMCA_HANDLE handle = 0;
    tMCA_RCB *p_rcb = &mca_cb.rcb[0];

    for (i=0; i<MCA_NUM_REGS; i++, p_rcb++)
    {
        if (p_rcb->p_cback && p_rcb->reg.data_psm == psm)
        {
            handle = i+1;
            break;
        }
    }
    return handle;
}

/*******************************************************************************
**
** Function         mca_tc_tbl_calloc
**
** Description      This function allocates a transport table for the given
**                  control channel.
**
** Returns          The tranport table.
**
*******************************************************************************/
tMCA_TC_TBL * mca_tc_tbl_calloc(tMCA_CCB *p_ccb)
{
    tMCA_TC_TBL *p_tbl = mca_cb.tc.tc_tbl;
    int             i;

    /* find next free entry in tc table */
    for (i = 0; i < MCA_NUM_TC_TBL; i++, p_tbl++)
    {
        if (p_tbl->state == MCA_TC_ST_UNUSED)
        {
            break;
        }
    }

    /* sanity check */
    WC_ASSERT(i != MCA_NUM_TC_TBL);

    /* initialize entry */
    p_tbl->peer_mtu = L2CAP_DEFAULT_MTU;
    p_tbl->cfg_flags= 0;
    p_tbl->cb_idx   = mca_ccb_to_hdl(p_ccb);
    p_tbl->tcid     = MCA_CTRL_TCID;
    p_tbl->my_mtu   = MCA_CTRL_MTU;
    p_tbl->state    = MCA_TC_ST_IDLE;
    p_tbl->lcid     = p_ccb->lcid;
    mca_cb.tc.lcid_tbl[p_ccb->lcid - L2CAP_BASE_APPL_CID] = i;
    MCA_TRACE_DEBUG1("mca_tc_tbl_calloc cb_idx: %d", p_tbl->cb_idx);

    return p_tbl;
}

/*******************************************************************************
**
** Function         mca_tc_tbl_dalloc
**
** Description      This function allocates a transport table for the given
**                  data channel.
**
** Returns          The tranport table.
**
*******************************************************************************/
tMCA_TC_TBL * mca_tc_tbl_dalloc(tMCA_DCB *p_dcb)
{
    tMCA_TC_TBL *p_tbl = mca_cb.tc.tc_tbl;
    int             i;

    /* find next free entry in tc table */
    for (i = 0; i < MCA_NUM_TC_TBL; i++, p_tbl++)
    {
        if (p_tbl->state == MCA_TC_ST_UNUSED)
        {
            break;
        }
    }

    /* sanity check */
    WC_ASSERT(i != MCA_NUM_TC_TBL);

    /* initialize entry */
    p_tbl->peer_mtu = L2CAP_DEFAULT_MTU;
    p_tbl->cfg_flags= 0;
    p_tbl->cb_idx   = mca_dcb_to_hdl(p_dcb);
    p_tbl->tcid     = p_dcb->p_cs->type + 1;
    p_tbl->my_mtu   = p_dcb->p_chnl_cfg->data_mtu;
    p_tbl->state    = MCA_TC_ST_IDLE;
    p_tbl->lcid     = p_dcb->lcid;
    mca_cb.tc.lcid_tbl[p_dcb->lcid - L2CAP_BASE_APPL_CID] = i;
    MCA_TRACE_DEBUG2("mca_tc_tbl_dalloc tcid: %d, cb_idx: %d", p_tbl->tcid, p_tbl->cb_idx);

    return p_tbl;
}

/*******************************************************************************
**
** Function         mca_tc_tbl_by_lcid
**
** Description      Find the transport channel table entry by LCID.
**
**
** Returns          The tranport table.
**
*******************************************************************************/
tMCA_TC_TBL *mca_tc_tbl_by_lcid(UINT16 lcid)
{
    UINT8 idx;

    if (lcid)
    {
        idx = mca_cb.tc.lcid_tbl[lcid - L2CAP_BASE_APPL_CID];

        if (idx < MCA_NUM_TC_TBL)
        {
            return &mca_cb.tc.tc_tbl[idx];
        }
    }
    return NULL;
}

/*******************************************************************************
**
** Function         mca_free_tc_tbl_by_lcid
**
** Description      Find the  transport table entry by LCID
**                  and free the tc_tbl
**
** Returns          void.
**
*******************************************************************************/
void mca_free_tc_tbl_by_lcid(UINT16 lcid)
{
    UINT8 idx;

    if (lcid)
    {
        idx = mca_cb.tc.lcid_tbl[lcid - L2CAP_BASE_APPL_CID];

        if (idx < MCA_NUM_TC_TBL)
        {
            mca_cb.tc.tc_tbl[idx].state = MCA_TC_ST_UNUSED;
        }
    }
}


/*******************************************************************************
**
** Function         mca_set_cfg_by_tbl
**
** Description      Set the L2CAP configuration information
**
** Returns          none.
**
*******************************************************************************/
void mca_set_cfg_by_tbl(tL2CAP_CFG_INFO *p_cfg, tMCA_TC_TBL *p_tbl)
{
    tMCA_DCB   *p_dcb;
    const tL2CAP_FCR_OPTS *p_opt;
    tMCA_FCS_OPT    fcs = MCA_FCS_NONE;

    if (p_tbl->tcid == MCA_CTRL_TCID)
    {
        p_opt = &mca_l2c_fcr_opts_def;
    }
    else
    {
        p_dcb = mca_dcb_by_hdl(p_tbl->cb_idx);
        p_opt = &p_dcb->p_chnl_cfg->fcr_opt;
        fcs   = p_dcb->p_chnl_cfg->fcs;
    }
    memset(p_cfg, 0, sizeof(tL2CAP_CFG_INFO));
    p_cfg->mtu_present = TRUE;
    p_cfg->mtu = p_tbl->my_mtu;
    p_cfg->fcr_present = TRUE;
    memcpy(&p_cfg->fcr, p_opt, sizeof (tL2CAP_FCR_OPTS));
    if (fcs & MCA_FCS_PRESNT_MASK)
    {
        p_cfg->fcs_present = TRUE;
        p_cfg->fcs = (fcs & MCA_FCS_USE_MASK);
    }
}

/*******************************************************************************
**
** Function         mca_tc_close_ind
**
** Description      This function is called by the L2CAP interface when the
**                  L2CAP channel is closed.  It looks up the CCB or DCB for
**                  the channel and sends it a close event.  The reason
**                  parameter is the same value passed by the L2CAP
**                  callback function.
**
** Returns          Nothing.
**
*******************************************************************************/
void mca_tc_close_ind(tMCA_TC_TBL *p_tbl, UINT16 reason)
{
    tMCA_CCB   *p_ccb;
    tMCA_DCB   *p_dcb;
    tMCA_CLOSE  close;

    close.param  = MCA_ACP;
    close.reason = reason;
    close.lcid   = p_tbl->lcid;

    MCA_TRACE_DEBUG3("mca_tc_close_ind tcid: %d, cb_idx:%d, old: %d",
                     p_tbl->tcid, p_tbl->cb_idx, p_tbl->state);

    /* Check if the transport channel is in use */
    if (p_tbl->state == MCA_TC_ST_UNUSED)
        return;

    /* clear mca_tc_tbl entry */
    if (p_tbl->cfg_flags&MCA_L2C_CFG_DISCN_INT)
        close.param = MCA_INT;
    p_tbl->cfg_flags = 0;
    p_tbl->peer_mtu = L2CAP_DEFAULT_MTU;

    /* if control channel, notify ccb that channel close */
    if (p_tbl->tcid == MCA_CTRL_TCID)
    {
        p_ccb = mca_ccb_by_hdl((tMCA_CL)p_tbl->cb_idx);
        mca_ccb_event(p_ccb, MCA_CCB_LL_CLOSE_EVT, (tMCA_CCB_EVT *)&close);
    }
    /* notify dcb that channel close */
    else
    {
        /* look up dcb  */
        p_dcb = mca_dcb_by_hdl(p_tbl->cb_idx);
        if (p_dcb != NULL)
        {
            mca_dcb_event(p_dcb, MCA_DCB_TC_CLOSE_EVT, (tMCA_DCB_EVT *) &close);
        }
    }
    p_tbl->state = MCA_TC_ST_UNUSED;
}

/*******************************************************************************
**
** Function         mca_tc_open_ind
**
** Description      This function is called by the L2CAP interface when
**                  the L2CAP channel is opened.  It looks up the CCB or DCB
**                  for the channel and sends it an open event.
**
** Returns          Nothing.
**
*******************************************************************************/
void mca_tc_open_ind(tMCA_TC_TBL *p_tbl)
{
    tMCA_CCB   *p_ccb;
    tMCA_DCB   *p_dcb;
    tMCA_OPEN  open;

    MCA_TRACE_DEBUG2("mca_tc_open_ind tcid: %d, cb_idx: %d", p_tbl->tcid, p_tbl->cb_idx);
    p_tbl->state = MCA_TC_ST_OPEN;

    open.peer_mtu = p_tbl->peer_mtu;
    open.lcid = p_tbl->lcid;
    /* use param to indicate the role of connection.
     * MCA_ACP, if ACP */
    open.param = MCA_INT;
    if (p_tbl->cfg_flags & MCA_L2C_CFG_CONN_ACP)
    {
        open.param = MCA_ACP;
    }

    /* if control channel, notify ccb that channel open */
    if (p_tbl->tcid == MCA_CTRL_TCID)
    {
        p_ccb = mca_ccb_by_hdl((tMCA_CL)p_tbl->cb_idx);

        mca_ccb_event(p_ccb, MCA_CCB_LL_OPEN_EVT, (tMCA_CCB_EVT *)&open);
    }
    /* must be data channel, notify dcb that channel open */
    else
    {
        /* look up dcb */
        p_dcb = mca_dcb_by_hdl(p_tbl->cb_idx);

        /* put lcid in event data */
        if (p_dcb != NULL)
        {
            mca_dcb_event(p_dcb, MCA_DCB_TC_OPEN_EVT, (tMCA_DCB_EVT *) &open);
        }
    }
}


/*******************************************************************************
**
** Function         mca_tc_cong_ind
**
** Description      This function is called by the L2CAP interface layer when
**                  L2CAP calls the congestion callback.  It looks up the CCB
**                  or DCB for the channel and sends it a congestion event.
**                  The is_congested parameter is the same value passed by
**                  the L2CAP callback function.
**
**
** Returns          Nothing.
**
*******************************************************************************/
void mca_tc_cong_ind(tMCA_TC_TBL *p_tbl, BOOLEAN is_congested)
{
    tMCA_CCB   *p_ccb;
    tMCA_DCB   *p_dcb;

    MCA_TRACE_DEBUG2("mca_tc_cong_ind tcid: %d, cb_idx: %d", p_tbl->tcid, p_tbl->cb_idx);
    /* if control channel, notify ccb of congestion */
    if (p_tbl->tcid == MCA_CTRL_TCID)
    {
        p_ccb = mca_ccb_by_hdl((tMCA_CL)p_tbl->cb_idx);
        mca_ccb_event(p_ccb, MCA_CCB_LL_CONG_EVT, (tMCA_CCB_EVT *) &is_congested);
    }
    /* notify dcb that channel open */
    else
    {
        /* look up dcb by cb_idx */
        p_dcb = mca_dcb_by_hdl(p_tbl->cb_idx);
        if (p_dcb != NULL)
        {
            mca_dcb_event(p_dcb, MCA_DCB_TC_CONG_EVT, (tMCA_DCB_EVT *) &is_congested);
        }
    }
}


/*******************************************************************************
**
** Function         mca_tc_data_ind
**
** Description      This function is called by the L2CAP interface layer when
**                  incoming data is received from L2CAP.  It looks up the CCB
**                  or DCB for the channel and routes the data accordingly.
**
** Returns          Nothing.
**
*******************************************************************************/
void mca_tc_data_ind(tMCA_TC_TBL *p_tbl, BT_HDR *p_buf)
{
    tMCA_CCB   *p_ccb;
    tMCA_DCB   *p_dcb;
    UINT8       event = MCA_CCB_MSG_RSP_EVT;
    UINT8       *p;
    UINT8       rej_rsp_code = MCA_RSP_SUCCESS;

    MCA_TRACE_DEBUG2("mca_tc_data_ind tcid: %d, cb_idx: %d", p_tbl->tcid, p_tbl->cb_idx);


    /* if control channel, handle control message */
    if (p_tbl->tcid == MCA_CTRL_TCID)
    {
        p_ccb = mca_ccb_by_hdl((tMCA_CL)p_tbl->cb_idx);
        if (p_ccb)
        {
            p = (UINT8*)(p_buf+1) + p_buf->offset;
            /* all the request opcode has bit 0 set. response code has bit 0 clear */
            if ((*p) & 0x01)
                event = MCA_CCB_MSG_REQ_EVT;

            if (*p < MCA_NUM_STANDARD_OPCODE)
            {
                if (p_buf->len != mca_std_msg_len[*p])
                {
                    MCA_TRACE_ERROR3 ("opcode: %d required len:%d, got len:%d", *p, mca_std_msg_len[*p], p_buf->len);
                    rej_rsp_code = MCA_RSP_BAD_PARAM;
                }
            }
            else if ((*p >= MCA_FIRST_SYNC_OP) && (*p <= MCA_LAST_SYNC_OP))
            {
                MCA_TRACE_ERROR2 ("unsupported SYNC opcode: %d len:%d", *p, p_buf->len);
                /* reject unsupported request */
                rej_rsp_code = MCA_RSP_NO_SUPPORT;
            }
            else
            {
                MCA_TRACE_ERROR2 ("bad opcode: %d len:%d", *p, p_buf->len);
                /* reject unsupported request */
                rej_rsp_code = MCA_RSP_BAD_OPCODE;
            }

            p_buf->layer_specific = rej_rsp_code;
            /* forward the request/response to state machine */
            mca_ccb_event(p_ccb, event, (tMCA_CCB_EVT *) p_buf);
        } /* got a valid ccb */
        else
            GKI_freebuf(p_buf);
    }
    /* else send event to dcb */
    else
    {
        p_dcb = mca_dcb_by_hdl(p_tbl->cb_idx);
        if (p_dcb != NULL)
        {
            mca_dcb_event(p_dcb, MCA_DCB_TC_DATA_EVT, (tMCA_DCB_EVT *) p_buf);
        }
        else
            GKI_freebuf(p_buf);
    }
}

/*******************************************************************************
**
** Function         mca_rcb_alloc
**
** Description      This function allocates a registration control block.
**                  If no free RCB is available, it returns NULL.
**
** Returns          tMCA_RCB *
**
*******************************************************************************/
tMCA_RCB * mca_rcb_alloc(tMCA_REG *p_reg)
{
    int     i;
    tMCA_RCB *p_rcb = NULL;

    for (i=0; i<MCA_NUM_REGS; i++)
    {
        if (mca_cb.rcb[i].p_cback == NULL)
        {
            p_rcb = &mca_cb.rcb[i];
            memcpy (&p_rcb->reg, p_reg, sizeof(tMCA_REG));
            break;
        }
    }
    return p_rcb;
}

/*******************************************************************************
**
** Function         mca_rcb_dealloc
**
** Description      This function deallocates the RCB with the given handle.
**
** Returns          void.
**
*******************************************************************************/
void mca_rcb_dealloc(tMCA_HANDLE handle)
{
    int      i;
    BOOLEAN  done = TRUE;
    tMCA_RCB *p_rcb;
    tMCA_CCB *p_ccb;

    if (handle && (handle<=MCA_NUM_REGS))
    {
        handle--;
        p_rcb = &mca_cb.rcb[handle];
        if (p_rcb->p_cback)
        {
            p_ccb = &mca_cb.ccb[handle*MCA_NUM_LINKS];
            /* check if all associated CCB are disconnected */
            for (i=0; i<MCA_NUM_LINKS; i++, p_ccb++)
            {
                if (p_ccb->p_rcb)
                {
                    done = FALSE;
                    mca_ccb_event (p_ccb, MCA_CCB_API_DISCONNECT_EVT, NULL);
                }
            }

            if (done)
            {
                memset (p_rcb, 0, sizeof(tMCA_RCB));
            }
        }
    }
}

/*******************************************************************************
**
** Function         mca_rcb_to_handle
**
** Description      This function converts a pointer to an RCB to
**                  a handle (tMCA_HANDLE).  It returns the handle.
**
** Returns          void.
**
*******************************************************************************/
tMCA_HANDLE mca_rcb_to_handle(tMCA_RCB *p_rcb)
{
    return(UINT8) (p_rcb - mca_cb.rcb + 1);
}

/*******************************************************************************
**
** Function         mca_rcb_by_handle
**
** Description      This function finds the RCB for a handle (tMCA_HANDLE).
**                  It returns a pointer to the RCB.  If no RCB matches the
**                  handle it returns NULL.
**
** Returns          tMCA_RCB *
**
*******************************************************************************/
tMCA_RCB *mca_rcb_by_handle(tMCA_HANDLE handle)
{
    tMCA_RCB *p_rcb = NULL;

    if (handle && (handle<=MCA_NUM_REGS) && mca_cb.rcb[handle-1].p_cback)
    {
        p_rcb = &mca_cb.rcb[handle-1];
    }
    return p_rcb;
}

/*******************************************************************************
**
** Function         mca_is_valid_dep_id
**
** Description      This function checks if the given dep_id is valid.
**
** Returns          TRUE, if this is a valid local dep_id
**
*******************************************************************************/
BOOLEAN mca_is_valid_dep_id(tMCA_RCB *p_rcb, tMCA_DEP dep)
{
    BOOLEAN valid = FALSE;
    if (dep < MCA_NUM_DEPS && p_rcb->dep[dep].p_data_cback)
    {
        valid = TRUE;
    }
    return valid;
}

/*******************************************************************************
**
** Function         mca_free_buf
**
** Description      free memory for specified GKI packet
**
** Returns          void
**
*******************************************************************************/
void mca_free_buf (void **p_buf)
{
    if (p_buf && *p_buf)
    {
        GKI_freebuf(*p_buf);
        *p_buf = NULL;
    }
}