ronnie
2022-10-14 1504bb53e29d3d46222c0b3ea994fc494b48e153
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
/******************************************************************************
 *
 *  Copyright 2008-2016 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.
 *
 ******************************************************************************/
 
/*****************************************************************************
 *
 *  Name:           avct_l2c_br.cc
 *
 *  Description:    This AVCTP module interfaces to L2CAP
 *
 *****************************************************************************/
 
#include <string.h>
#include "avct_api.h"
#include "avct_int.h"
#include "bt_target.h"
#include "bt_utils.h"
#include "l2c_api.h"
#include "l2cdefs.h"
#include "osi/include/osi.h"
 
/* Configuration flags. */
#define AVCT_L2C_CFG_IND_DONE (1 << 0)
#define AVCT_L2C_CFG_CFM_DONE (1 << 1)
 
/* AVCTP Browsing channel FCR Option:
 * Size of the transmission window when using enhanced retransmission mode. Not
 * used in basic and streaming modes. Range: 1 - 63
 */
#define AVCT_BR_FCR_OPT_TX_WINDOW_SIZE 10
 
/* AVCTP Browsing channel FCR Option:
 * Number of transmission attempts for a single I-Frame before taking
 * Down the connection. Used In ERTM mode only. Value is Ignored in basic and
 * Streaming modes.
 * Range: 0, 1-0xFF
 *     0 - infinite retransmissions
 *     1 - single transmission
 */
#define AVCT_BR_FCR_OPT_MAX_TX_B4_DISCNT 20
 
/* AVCTP Browsing channel FCR Option: Retransmission Timeout
 * The AVRCP specification set a value in the range of 300 - 2000 ms
 * Timeout (in msecs) to detect Lost I-Frames. Only used in Enhanced
 * retransmission mode.
 * Range: Minimum 2000 (2 secs) when supporting PBF.
 */
#define AVCT_BR_FCR_OPT_RETX_TOUT 2000
 
/* AVCTP Browsing channel FCR Option: Monitor Timeout
 * The AVRCP specification set a value in the range of 300 - 2000 ms
 * Timeout (in msecs) to detect Lost S-Frames. Only used in Enhanced
 * retransmission mode.
 * Range: Minimum 12000 (12 secs) when supporting PBF.
 */
#define AVCT_BR_FCR_OPT_MONITOR_TOUT 12000
 
/* callback function declarations */
void avct_l2c_br_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
                                   uint16_t psm, uint8_t id);
void avct_l2c_br_connect_cfm_cback(uint16_t lcid, uint16_t result);
void avct_l2c_br_config_cfm_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
void avct_l2c_br_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg);
void avct_l2c_br_disconnect_ind_cback(uint16_t lcid, bool ack_needed);
void avct_l2c_br_disconnect_cfm_cback(uint16_t lcid, uint16_t result);
void avct_l2c_br_congestion_ind_cback(uint16_t lcid, bool is_congested);
void avct_l2c_br_data_ind_cback(uint16_t lcid, BT_HDR* p_buf);
 
/* L2CAP callback function structure */
const tL2CAP_APPL_INFO avct_l2c_br_appl = {
    avct_l2c_br_connect_ind_cback,
    avct_l2c_br_connect_cfm_cback,
    NULL,
    avct_l2c_br_config_ind_cback,
    avct_l2c_br_config_cfm_cback,
    avct_l2c_br_disconnect_ind_cback,
    avct_l2c_br_disconnect_cfm_cback,
    NULL,
    avct_l2c_br_data_ind_cback,
    avct_l2c_br_congestion_ind_cback,
    NULL, /* tL2CA_TX_COMPLETE_CB */
    NULL /* tL2CA_CREDITS_RECEIVED_CB */};
 
/* Browsing channel eL2CAP default options */
const tL2CAP_FCR_OPTS avct_l2c_br_fcr_opts_def = {
    L2CAP_FCR_ERTM_MODE,              /* Mandatory for Browsing channel */
    AVCT_BR_FCR_OPT_TX_WINDOW_SIZE,   /* Tx window size */
    AVCT_BR_FCR_OPT_MAX_TX_B4_DISCNT, /* Maximum transmissions before
                                         disconnecting */
    AVCT_BR_FCR_OPT_RETX_TOUT,        /* Retransmission timeout (2 secs) */
    AVCT_BR_FCR_OPT_MONITOR_TOUT,     /* Monitor timeout (12 secs) */
    L2CAP_DEFAULT_ERM_MPS             /* MPS segment size */
};
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_connect_ind_cback
 *
 * Description      This is the L2CAP connect indication callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_connect_ind_cback(const RawAddress& bd_addr, uint16_t lcid,
                                   UNUSED_ATTR uint16_t psm, uint8_t id) {
  tAVCT_LCB* p_lcb;
  uint16_t result = L2CAP_CONN_NO_RESOURCES;
  tL2CAP_CFG_INFO cfg;
  tAVCT_BCB* p_bcb;
  tL2CAP_ERTM_INFO ertm_info;
 
  memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
  cfg.mtu_present = true;
 
  p_lcb = avct_lcb_by_bd(bd_addr);
  if (p_lcb != NULL) {
    /* control channel exists */
    p_bcb = avct_bcb_by_lcb(p_lcb);
    p_bcb->peer_addr = bd_addr;
 
    if (p_bcb->allocated == 0) {
      /* browsing channel does not exist yet and the browsing channel is
       * registered
       * - accept connection */
      p_bcb->allocated = p_lcb->allocated; /* copy the index from lcb */
 
      result = L2CAP_CONN_OK;
      cfg.mtu = avct_cb.mtu_br;
 
      cfg.fcr_present = true;
      cfg.fcr = avct_l2c_br_fcr_opts_def;
    }
  }
  /* else no control channel yet, reject */
 
  /* Set the FCR options: Browsing channel mandates ERTM */
  ertm_info.preferred_mode = cfg.fcr.mode;
  ertm_info.allowed_modes = L2CAP_FCR_CHAN_OPT_ERTM;
  ertm_info.user_rx_buf_size = BT_DEFAULT_BUFFER_SIZE;
  ertm_info.user_tx_buf_size = BT_DEFAULT_BUFFER_SIZE;
  ertm_info.fcr_rx_buf_size = BT_DEFAULT_BUFFER_SIZE;
  ertm_info.fcr_tx_buf_size = BT_DEFAULT_BUFFER_SIZE;
 
  /* Send L2CAP connect rsp */
  L2CA_ErtmConnectRsp(bd_addr, id, lcid, result, 0, &ertm_info);
 
  /* if result ok, proceed with connection */
  if (result == L2CAP_CONN_OK) {
    /* store LCID */
    p_bcb->ch_lcid = lcid;
 
    /* transition to configuration state */
    p_bcb->ch_state = AVCT_CH_CFG;
 
    /* Send L2CAP config req */
    L2CA_ConfigReq(lcid, &cfg);
  }
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_connect_cfm_cback
 *
 * Description      This is the L2CAP connect confirm callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_connect_cfm_cback(uint16_t lcid, uint16_t result) {
  tAVCT_BCB* p_lcb;
  tL2CAP_CFG_INFO cfg;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if ((p_lcb == NULL) || (p_lcb->ch_state != AVCT_CH_CONN)) return;
 
  if (result != L2CAP_CONN_OK) {
    /* failure */
    tAVCT_LCB_EVT avct_lcb_evt;
    avct_lcb_evt.result = result;
    avct_bcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, &avct_lcb_evt);
    return;
  }
 
  /* result is successful */
  /* set channel state */
  p_lcb->ch_state = AVCT_CH_CFG;
 
  /* Send L2CAP config req */
  memset(&cfg, 0, sizeof(tL2CAP_CFG_INFO));
 
  cfg.mtu_present = true;
  cfg.mtu = avct_cb.mtu_br;
 
  cfg.fcr_present = true;
  cfg.fcr = avct_l2c_br_fcr_opts_def;
 
  L2CA_ConfigReq(lcid, &cfg);
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_config_cfm_cback
 *
 * Description      This is the L2CAP config confirm callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_config_cfm_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
  tAVCT_BCB* p_lcb;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if ((p_lcb == NULL) || (p_lcb->ch_state != AVCT_CH_CFG)) return;
 
  /* if result successful */
  if (p_cfg->result == L2CAP_CFG_OK) {
    /* update flags */
    p_lcb->ch_flags |= AVCT_L2C_CFG_CFM_DONE;
 
    /* if configuration complete */
    if (p_lcb->ch_flags & AVCT_L2C_CFG_IND_DONE) {
      p_lcb->ch_state = AVCT_CH_OPEN;
      avct_bcb_event(p_lcb, AVCT_LCB_LL_OPEN_EVT, NULL);
    }
  }
  /* else failure */
  else {
    /* store result value */
    p_lcb->ch_result = p_cfg->result;
 
    /* Send L2CAP disconnect req */
    L2CA_DisconnectReq(lcid);
  }
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_config_ind_cback
 *
 * Description      This is the L2CAP config indication callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_config_ind_cback(uint16_t lcid, tL2CAP_CFG_INFO* p_cfg) {
  tAVCT_BCB* p_lcb;
  uint16_t max_mtu = BT_DEFAULT_BUFFER_SIZE - L2CAP_MIN_OFFSET - BT_HDR_SIZE;
 
  /* Don't include QoS nor flush timeout in the response since we
     currently always accept these values.  Note: fcr_present is left
     untouched since l2cap negotiates this internally
  */
  p_cfg->flush_to_present = false;
  p_cfg->qos_present = false;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if (p_lcb == NULL) return;
 
  /* store the mtu in tbl */
  p_lcb->peer_mtu = L2CAP_DEFAULT_MTU;
  if (p_cfg->mtu_present) {
    p_lcb->peer_mtu = p_cfg->mtu;
  }
 
  if (p_lcb->peer_mtu > max_mtu) {
    p_lcb->peer_mtu = p_cfg->mtu = max_mtu;
 
    /* Must tell the peer what the adjusted value is */
    p_cfg->mtu_present = true;
  } else /* Don't include in the response */
    p_cfg->mtu_present = false;
 
  AVCT_TRACE_DEBUG("%s peer_mtu:%d use:%d", __func__, p_lcb->peer_mtu, max_mtu);
 
  if (p_lcb->peer_mtu >= AVCT_MIN_BROWSE_MTU)
    p_cfg->result = L2CAP_CFG_OK;
  else {
    p_cfg->result = L2CAP_CFG_UNACCEPTABLE_PARAMS;
    p_cfg->mtu_present = true;
    p_cfg->mtu = AVCT_MIN_BROWSE_MTU;
  }
 
  /* send L2CAP configure response */
  L2CA_ConfigRsp(lcid, p_cfg);
 
  if (p_cfg->result != L2CAP_CFG_OK) {
    return;
  }
 
  /* if first config ind */
  if ((p_lcb->ch_flags & AVCT_L2C_CFG_IND_DONE) == 0) {
    /* update flags */
    p_lcb->ch_flags |= AVCT_L2C_CFG_IND_DONE;
 
    /* if configuration complete */
    if (p_lcb->ch_flags & AVCT_L2C_CFG_CFM_DONE) {
      p_lcb->ch_state = AVCT_CH_OPEN;
      avct_bcb_event(p_lcb, AVCT_LCB_LL_OPEN_EVT, NULL);
    }
  }
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_disconnect_ind_cback
 *
 * Description      This is the L2CAP disconnect indication callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_disconnect_ind_cback(uint16_t lcid, bool ack_needed) {
  tAVCT_BCB* p_lcb;
  uint16_t result = AVCT_RESULT_FAIL;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if (p_lcb == NULL) return;
 
  if (ack_needed) {
    /* send L2CAP disconnect response */
    L2CA_DisconnectRsp(lcid);
  }
 
  tAVCT_LCB_EVT avct_lcb_evt;
  avct_lcb_evt.result = result;
  avct_bcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, &avct_lcb_evt);
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_disconnect_cfm_cback
 *
 * Description      This is the L2CAP disconnect confirm callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_disconnect_cfm_cback(uint16_t lcid, uint16_t result) {
  tAVCT_BCB* p_lcb;
  uint16_t res;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if (p_lcb == NULL) return;
 
  /* result value may be previously stored */
  res = (p_lcb->ch_result != 0) ? p_lcb->ch_result : result;
  p_lcb->ch_result = 0;
 
  tAVCT_LCB_EVT avct_lcb_evt;
  avct_lcb_evt.result = res;
  avct_bcb_event(p_lcb, AVCT_LCB_LL_CLOSE_EVT, &avct_lcb_evt);
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_congestion_ind_cback
 *
 * Description      This is the L2CAP congestion indication callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_congestion_ind_cback(uint16_t lcid, bool is_congested) {
  tAVCT_BCB* p_lcb;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if (p_lcb == NULL) return;
 
  tAVCT_LCB_EVT avct_lcb_evt;
  avct_lcb_evt.cong = is_congested;
  avct_bcb_event(p_lcb, AVCT_LCB_LL_CONG_EVT, &avct_lcb_evt);
}
 
/*******************************************************************************
 *
 * Function         avct_l2c_br_data_ind_cback
 *
 * Description      This is the L2CAP data indication callback function.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void avct_l2c_br_data_ind_cback(uint16_t lcid, BT_HDR* p_buf) {
  tAVCT_BCB* p_lcb;
  tAVCT_LCB_EVT evt_data;
 
  /* look up lcb for this channel */
  p_lcb = avct_bcb_by_lcid(lcid);
  if (p_lcb == NULL) {
    /* prevent buffer leak */
    osi_free(p_buf);
    return;
  }
 
  evt_data.p_buf = p_buf;
  avct_bcb_event(p_lcb, AVCT_LCB_LL_MSG_EVT, &evt_data);
}