summaryrefslogtreecommitdiffstats
path: root/stack/avct/avct_lcb.c
blob: bb3f447746f92e764c956d237390880fc4829363 (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
/******************************************************************************
 *
 *  Copyright (C) 2003-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 module contains the link control state machine and functions which
 *  operate on the link control block.
 *
 ******************************************************************************/

#include <string.h>
#include "data_types.h"
#include "bt_target.h"
#include "avct_api.h"
#include "avct_int.h"
#include "gki.h"

/*****************************************************************************
** state machine constants and types
*****************************************************************************/

#if BT_TRACE_VERBOSE == TRUE

/* verbose state strings for trace */
const char * const avct_lcb_st_str[] = {
    "LCB_IDLE_ST",
    "LCB_OPENING_ST",
    "LCB_OPEN_ST",
    "LCB_CLOSING_ST"
};

/* verbose event strings for trace */
const char * const avct_lcb_evt_str[] = {
    "UL_BIND_EVT",
    "UL_UNBIND_EVT",
    "UL_MSG_EVT",
    "INT_CLOSE_EVT",
    "LL_OPEN_EVT",
    "LL_CLOSE_EVT",
    "LL_MSG_EVT",
    "LL_CONG_EVT"
};

#endif

/* lcb state machine states */
enum {
    AVCT_LCB_IDLE_ST,
    AVCT_LCB_OPENING_ST,
    AVCT_LCB_OPEN_ST,
    AVCT_LCB_CLOSING_ST
};

/* state machine action enumeration list */
enum {
    AVCT_LCB_CHNL_OPEN,
    AVCT_LCB_CHNL_DISC,
    AVCT_LCB_SEND_MSG,
    AVCT_LCB_OPEN_IND,
    AVCT_LCB_OPEN_FAIL,
    AVCT_LCB_CLOSE_IND,
    AVCT_LCB_CLOSE_CFM,
    AVCT_LCB_MSG_IND,
    AVCT_LCB_CONG_IND,
    AVCT_LCB_BIND_CONN,
    AVCT_LCB_BIND_FAIL,
    AVCT_LCB_UNBIND_DISC,
    AVCT_LCB_CHK_DISC,
    AVCT_LCB_DISCARD_MSG,
    AVCT_LCB_DEALLOC,
    AVCT_LCB_FREE_MSG_IND,
    AVCT_LCB_NUM_ACTIONS
};

#define AVCT_LCB_IGNORE     AVCT_LCB_NUM_ACTIONS

/* type for action functions */
typedef void (*tAVCT_LCB_ACTION)(tAVCT_LCB *p_ccb, tAVCT_LCB_EVT *p_data);

/* action function list */
const tAVCT_LCB_ACTION avct_lcb_action[] = {
    avct_lcb_chnl_open,
    avct_lcb_chnl_disc,
    avct_lcb_send_msg,
    avct_lcb_open_ind,
    avct_lcb_open_fail,
    avct_lcb_close_ind,
    avct_lcb_close_cfm,
    avct_lcb_msg_ind,
    avct_lcb_cong_ind,
    avct_lcb_bind_conn,
    avct_lcb_bind_fail,
    avct_lcb_unbind_disc,
    avct_lcb_chk_disc,
    avct_lcb_discard_msg,
    avct_lcb_dealloc,
    avct_lcb_free_msg_ind
};

/* state table information */
#define AVCT_LCB_ACTIONS            2       /* number of actions */
#define AVCT_LCB_NEXT_STATE         2       /* position of next state */
#define AVCT_LCB_NUM_COLS           3       /* number of columns in state tables */

/* state table for idle state */
const UINT8 avct_lcb_st_idle[][AVCT_LCB_NUM_COLS] = {
/* Event                Action 1                    Action 2                    Next state */
/* UL_BIND_EVT */       {AVCT_LCB_CHNL_OPEN,        AVCT_LCB_IGNORE,            AVCT_LCB_OPENING_ST},
/* UL_UNBIND_EVT */     {AVCT_LCB_UNBIND_DISC,      AVCT_LCB_IGNORE,            AVCT_LCB_IDLE_ST},
/* UL_MSG_EVT */        {AVCT_LCB_DISCARD_MSG,      AVCT_LCB_IGNORE,            AVCT_LCB_IDLE_ST},
/* INT_CLOSE_EVT */     {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_IDLE_ST},
/* LL_OPEN_EVT */       {AVCT_LCB_OPEN_IND,         AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* LL_CLOSE_EVT */      {AVCT_LCB_CLOSE_IND,        AVCT_LCB_DEALLOC,           AVCT_LCB_IDLE_ST},
/* LL_MSG_EVT */        {AVCT_LCB_FREE_MSG_IND,     AVCT_LCB_IGNORE,            AVCT_LCB_IDLE_ST},
/* LL_CONG_EVT */       {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_IDLE_ST}
};

/* state table for opening state */
const UINT8 avct_lcb_st_opening[][AVCT_LCB_NUM_COLS] = {
/* Event                Action 1                    Action 2                    Next state */
/* UL_BIND_EVT */       {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_OPENING_ST},
/* UL_UNBIND_EVT */     {AVCT_LCB_UNBIND_DISC,      AVCT_LCB_IGNORE,            AVCT_LCB_OPENING_ST},
/* UL_MSG_EVT */        {AVCT_LCB_DISCARD_MSG,      AVCT_LCB_IGNORE,            AVCT_LCB_OPENING_ST},
/* INT_CLOSE_EVT */     {AVCT_LCB_CHNL_DISC,        AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* LL_OPEN_EVT */       {AVCT_LCB_OPEN_IND,         AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* LL_CLOSE_EVT */      {AVCT_LCB_OPEN_FAIL,        AVCT_LCB_DEALLOC,           AVCT_LCB_IDLE_ST},
/* LL_MSG_EVT */        {AVCT_LCB_FREE_MSG_IND,     AVCT_LCB_IGNORE,            AVCT_LCB_OPENING_ST},
/* LL_CONG_EVT */       {AVCT_LCB_CONG_IND,         AVCT_LCB_IGNORE,            AVCT_LCB_OPENING_ST}
};

/* state table for open state */
const UINT8 avct_lcb_st_open[][AVCT_LCB_NUM_COLS] = {
/* Event                Action 1                    Action 2                    Next state */
/* UL_BIND_EVT */       {AVCT_LCB_BIND_CONN,        AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* UL_UNBIND_EVT */     {AVCT_LCB_CHK_DISC,         AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* UL_MSG_EVT */        {AVCT_LCB_SEND_MSG,         AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* INT_CLOSE_EVT */     {AVCT_LCB_CHNL_DISC,        AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* LL_OPEN_EVT */       {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* LL_CLOSE_EVT */      {AVCT_LCB_CLOSE_IND,        AVCT_LCB_DEALLOC,           AVCT_LCB_IDLE_ST},
/* LL_MSG_EVT */        {AVCT_LCB_MSG_IND,          AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST},
/* LL_CONG_EVT */       {AVCT_LCB_CONG_IND,         AVCT_LCB_IGNORE,            AVCT_LCB_OPEN_ST}
};

/* state table for closing state */
const UINT8 avct_lcb_st_closing[][AVCT_LCB_NUM_COLS] = {
/* Event                Action 1                    Action 2                    Next state */
/* UL_BIND_EVT */       {AVCT_LCB_BIND_FAIL,        AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* UL_UNBIND_EVT */     {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* UL_MSG_EVT */        {AVCT_LCB_DISCARD_MSG,      AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* INT_CLOSE_EVT */     {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* LL_OPEN_EVT */       {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* LL_CLOSE_EVT */      {AVCT_LCB_CLOSE_CFM,        AVCT_LCB_DEALLOC,           AVCT_LCB_IDLE_ST},
/* LL_MSG_EVT */        {AVCT_LCB_FREE_MSG_IND,     AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST},
/* LL_CONG_EVT */       {AVCT_LCB_IGNORE,           AVCT_LCB_IGNORE,            AVCT_LCB_CLOSING_ST}
};

/* type for state table */
typedef const UINT8 (*tAVCT_LCB_ST_TBL)[AVCT_LCB_NUM_COLS];

/* state table */
const tAVCT_LCB_ST_TBL avct_lcb_st_tbl[] = {
    avct_lcb_st_idle,
    avct_lcb_st_opening,
    avct_lcb_st_open,
    avct_lcb_st_closing
};

/*******************************************************************************
**
** Function         avct_lcb_event
**
** Description      State machine event handling function for lcb
**
**
** Returns          Nothing.
**
*******************************************************************************/
void avct_lcb_event(tAVCT_LCB *p_lcb, UINT8 event, tAVCT_LCB_EVT *p_data)
{
    tAVCT_LCB_ST_TBL    state_table;
    UINT8               action;
    int                 i;

#if BT_TRACE_VERBOSE == TRUE
    AVCT_TRACE_EVENT3("LCB lcb=%d event=%s state=%s", p_lcb->allocated, avct_lcb_evt_str[event], avct_lcb_st_str[p_lcb->state]);
#else
    AVCT_TRACE_EVENT3("LCB lcb=%d event=%d state=%d", p_lcb->allocated, event, p_lcb->state);
#endif

    /* look up the state table for the current state */
    state_table = avct_lcb_st_tbl[p_lcb->state];

    /* set next state */
    p_lcb->state = state_table[event][AVCT_LCB_NEXT_STATE];

    /* execute action functions */
    for (i = 0; i < AVCT_LCB_ACTIONS; i++)
    {
        if ((action = state_table[event][i]) != AVCT_LCB_IGNORE)
        {
            (*avct_lcb_action[action])(p_lcb, p_data);
        }
        else
        {
            break;
        }
    }
}

/*******************************************************************************
**
** Function         avct_bcb_event
**
** Description      State machine event handling function for lcb
**
**
** Returns          Nothing.
**
*******************************************************************************/
#if (AVCT_BROWSE_INCLUDED == TRUE)
void avct_bcb_event(tAVCT_BCB *p_bcb, UINT8 event, tAVCT_LCB_EVT *p_data)
{
    tAVCT_LCB_ST_TBL    state_table;
    UINT8               action;
    int                 i;

#if BT_TRACE_VERBOSE == TRUE
    AVCT_TRACE_EVENT3("BCB lcb=%d event=%s state=%s", p_bcb->allocated, avct_lcb_evt_str[event], avct_lcb_st_str[p_bcb->state]);
#else
    AVCT_TRACE_EVENT3("BCB lcb=%d event=%d state=%d", p_bcb->allocated, event, p_bcb->state);
#endif

    /* look up the state table for the current state */
    state_table = avct_lcb_st_tbl[p_bcb->state];

    /* set next state */
    p_bcb->state = state_table[event][AVCT_LCB_NEXT_STATE];

    /* execute action functions */
    for (i = 0; i < AVCT_LCB_ACTIONS; i++)
    {
        if ((action = state_table[event][i]) != AVCT_LCB_IGNORE)
        {
            (*avct_bcb_action[action])(p_bcb, p_data);
        }
        else
        {
            break;
        }
    }
}
#endif

/*******************************************************************************
**
** Function         avct_lcb_by_bd
**
** Description      This lookup function finds the lcb for a BD address.
**
**
** Returns          pointer to the lcb, or NULL if none found.
**
*******************************************************************************/
tAVCT_LCB *avct_lcb_by_bd(BD_ADDR bd_addr)
{
    tAVCT_LCB   *p_lcb = &avct_cb.lcb[0];
    int         i;

    for (i = 0; i < AVCT_NUM_LINKS; i++, p_lcb++)
    {
        /* if allocated lcb has matching lcb */
        if (p_lcb->allocated && (!memcmp(p_lcb->peer_addr, bd_addr, BD_ADDR_LEN)))
        {
            break;
        }
    }

    if (i == AVCT_NUM_LINKS)
    {
        /* if no lcb found */
        p_lcb = NULL;

        AVCT_TRACE_DEBUG6("No lcb for addr %02x-%02x-%02x-%02x-%02x-%02x",
                          bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4], bd_addr[5]);
    }
    return p_lcb;
}

/*******************************************************************************
**
** Function         avct_lcb_alloc
**
** Description      Allocate a link control block.
**
**
** Returns          pointer to the lcb, or NULL if none could be allocated.
**
*******************************************************************************/
tAVCT_LCB *avct_lcb_alloc(BD_ADDR bd_addr)
{
    tAVCT_LCB   *p_lcb = &avct_cb.lcb[0];
    int         i;

    for (i = 0; i < AVCT_NUM_LINKS; i++, p_lcb++)
    {
        if (!p_lcb->allocated)
        {
            p_lcb->allocated = (UINT8)(i + 1);
            memcpy(p_lcb->peer_addr, bd_addr, BD_ADDR_LEN);
            AVCT_TRACE_DEBUG1("avct_lcb_alloc %d", p_lcb->allocated);
            break;
        }
    }

    if (i == AVCT_NUM_LINKS)
    {
        /* out of lcbs */
        p_lcb = NULL;
        AVCT_TRACE_WARNING0("Out of lcbs");
    }
    return p_lcb;
}

/*******************************************************************************
**
** Function         avct_lcb_dealloc
**
** Description      Deallocate a link control block.
**
**
** Returns          void.
**
*******************************************************************************/
void avct_lcb_dealloc(tAVCT_LCB *p_lcb, tAVCT_LCB_EVT *p_data)
{
    tAVCT_CCB   *p_ccb = &avct_cb.ccb[0];
    BOOLEAN     found = FALSE;
    int         i;

    AVCT_TRACE_DEBUG1("avct_lcb_dealloc %d", p_lcb->allocated);

    for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++)
    {
        /* if ccb allocated and */
        if (p_ccb->allocated)
        {
            if (p_ccb->p_lcb == p_lcb)
            {
                AVCT_TRACE_DEBUG1("avct_lcb_dealloc used by ccb: %d", i);
                found = TRUE;
                break;
            }
        }
    }

    if (!found)
    {
        AVCT_TRACE_DEBUG0("avct_lcb_dealloc now");

        /* clear reassembled msg buffer if in use */
        if (p_lcb->p_rx_msg != NULL)
        {
            GKI_freebuf(p_lcb->p_rx_msg);
        }
        memset(p_lcb, 0, sizeof(tAVCT_LCB));
    }
}

/*******************************************************************************
**
** Function         avct_lcb_by_lcid
**
** Description      Find the LCB associated with the L2CAP LCID
**
**
** Returns          pointer to the lcb, or NULL if none found.
**
*******************************************************************************/
tAVCT_LCB *avct_lcb_by_lcid(UINT16 lcid)
{
    tAVCT_LCB   *p_lcb = &avct_cb.lcb[0];
    int         i;

    for (i = 0; i < AVCT_NUM_LINKS; i++, p_lcb++)
    {
        if (p_lcb->allocated && ((p_lcb->ch_lcid == lcid) || (p_lcb->conflict_lcid == lcid)))
        {
            break;
        }
    }

    if (i == AVCT_NUM_LINKS)
    {
        /* out of lcbs */
        p_lcb = NULL;
        AVCT_TRACE_WARNING1("No lcb for lcid %x", lcid);
    }

    return p_lcb;
}

/*******************************************************************************
**
** Function         avct_lcb_has_pid
**
** Description      See if any ccbs on this lcb have a particular pid.
**
**
** Returns          Pointer to CCB if PID found, NULL otherwise.
**
*******************************************************************************/
tAVCT_CCB *avct_lcb_has_pid(tAVCT_LCB *p_lcb, UINT16 pid)
{
    tAVCT_CCB   *p_ccb = &avct_cb.ccb[0];
    int         i;

    for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++)
    {
        if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb) && (p_ccb->cc.pid == pid))
        {
            return p_ccb;
        }
    }
    return NULL;
}

/*******************************************************************************
**
** Function         avct_lcb_last_ccb
**
** Description      See if given ccb is only one on the lcb.
**
**
** Returns          TRUE if ccb is last, FALSE otherwise.
**
*******************************************************************************/
BOOLEAN avct_lcb_last_ccb(tAVCT_LCB *p_lcb, tAVCT_CCB *p_ccb_last)
{
    tAVCT_CCB   *p_ccb = &avct_cb.ccb[0];
    int         i;

    AVCT_TRACE_WARNING0("avct_lcb_last_ccb");
    for (i = 0; i < AVCT_NUM_CONN; i++, p_ccb++)
    {
        AVCT_TRACE_WARNING6("%x: aloc:%d, lcb:0x%x/0x%x, ccb:0x%x/0x%x",
            i, p_ccb->allocated, p_ccb->p_lcb, p_lcb, p_ccb, p_ccb_last);
        if (p_ccb->allocated && (p_ccb->p_lcb == p_lcb) && (p_ccb != p_ccb_last))
        {
            return FALSE;
        }
    }
    return TRUE;
}