Skip to content

Commit ab6c090

Browse files
sjanccarlescufi
authored andcommitted
tests: bluetooth: tester: Add support for L2CAP channel reconfiguration
This allows UT to reconfigure MTU of a channel and get notfied when channel configuration changed. Signed-off-by: Szymon Janc <[email protected]>
1 parent c589994 commit ab6c090

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

tests/bluetooth/tester/src/bttester.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,15 @@ struct l2cap_accept_connection_cmd {
787787
uint16_t result;
788788
} __packed;
789789

790+
#define L2CAP_RECONFIGURE 0x07
791+
struct l2cap_reconfigure_cmd {
792+
uint8_t address_type;
793+
uint8_t address[6];
794+
uint16_t mtu;
795+
uint8_t num;
796+
uint8_t chan_id[];
797+
} __packed;
798+
790799
#define L2CAP_CREDITS 0x08
791800
struct l2cap_credits_cmd {
792801
uint8_t chan_id;
@@ -836,6 +845,15 @@ struct l2cap_data_received_ev {
836845
uint8_t data[];
837846
} __packed;
838847

848+
#define L2CAP_EV_RECONFIGURED 0x84
849+
struct l2cap_reconfigured_ev {
850+
uint8_t chan_id;
851+
uint16_t mtu_remote;
852+
uint16_t mps_remote;
853+
uint16_t mtu_local;
854+
uint16_t mps_local;
855+
} __packed;
856+
839857
/* MESH Service */
840858
/* commands */
841859
#define MESH_READ_SUPPORTED_COMMANDS 0x01

tests/bluetooth/tester/src/l2cap.c

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
2020
#include "bttester.h"
2121

2222
#define CONTROLLER_INDEX 0
23+
#define DATA_MTU_INITIAL 128
2324
#define DATA_MTU 256
2425
#define DATA_BUF_SIZE BT_L2CAP_SDU_BUF_SIZE(DATA_MTU)
2526
#define CHANNELS 2
@@ -135,11 +136,29 @@ static void disconnected_cb(struct bt_l2cap_chan *l2cap_chan)
135136
CONTROLLER_INDEX, (uint8_t *) &ev, sizeof(ev));
136137
}
137138

139+
static void reconfigured_cb(struct bt_l2cap_chan *l2cap_chan)
140+
{
141+
struct l2cap_reconfigured_ev ev;
142+
struct channel *chan = CONTAINER_OF(l2cap_chan, struct channel, le);
143+
144+
(void)memset(&ev, 0, sizeof(struct l2cap_disconnected_ev));
145+
146+
ev.chan_id = chan->chan_id;
147+
ev.mtu_remote = sys_cpu_to_le16(chan->le.tx.mtu);
148+
ev.mps_remote = sys_cpu_to_le16(chan->le.tx.mps);
149+
ev.mtu_local = sys_cpu_to_le16(chan->le.rx.mtu);
150+
ev.mps_local = sys_cpu_to_le16(chan->le.rx.mps);
151+
152+
tester_send(BTP_SERVICE_ID_L2CAP, L2CAP_EV_RECONFIGURED,
153+
CONTROLLER_INDEX, (uint8_t *)&ev, sizeof(ev));
154+
}
155+
138156
static const struct bt_l2cap_chan_ops l2cap_ops = {
139157
.alloc_buf = alloc_buf_cb,
140158
.recv = recv_cb,
141159
.connected = connected_cb,
142160
.disconnected = disconnected_cb,
161+
.reconfigured = reconfigured_cb,
143162
};
144163

145164
static struct channel *get_free_channel()
@@ -178,7 +197,7 @@ static void connect(uint8_t *data, uint16_t len)
178197
uint8_t i = 0;
179198
int err;
180199

181-
if (cmd->num > CHANNELS || mtu > DATA_MTU) {
200+
if (cmd->num > CHANNELS || mtu > DATA_MTU_INITIAL) {
182201
goto fail;
183202
}
184203

@@ -255,6 +274,56 @@ static void disconnect(uint8_t *data, uint16_t len)
255274
status);
256275
}
257276

277+
static void reconfigure(uint8_t *data, uint16_t len)
278+
{
279+
const struct l2cap_reconfigure_cmd *cmd = (void *)data;
280+
uint16_t mtu = sys_le16_to_cpu(cmd->mtu);
281+
struct bt_conn *conn;
282+
uint8_t status;
283+
int err;
284+
285+
struct bt_l2cap_chan *reconf_channels[CHANNELS + 1] = {};
286+
287+
/* address is first in data */
288+
conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, (bt_addr_le_t *)cmd);
289+
if (!conn) {
290+
LOG_ERR("Unknown connection");
291+
status = BTP_STATUS_FAILED;
292+
goto rsp;
293+
}
294+
295+
if (cmd->num > CHANNELS) {
296+
status = BTP_STATUS_FAILED;
297+
goto rsp;
298+
}
299+
300+
if (mtu > DATA_MTU) {
301+
status = BTP_STATUS_FAILED;
302+
goto rsp;
303+
}
304+
305+
for (int i = 0; i < cmd->num; i++) {
306+
if (cmd->chan_id[i] > CHANNELS) {
307+
status = BTP_STATUS_FAILED;
308+
goto rsp;
309+
}
310+
311+
reconf_channels[i] = &channels[cmd->chan_id[i]].le.chan;
312+
}
313+
314+
err = bt_l2cap_ecred_chan_reconfigure(reconf_channels, mtu);
315+
if (err) {
316+
status = BTP_STATUS_FAILED;
317+
goto rsp;
318+
}
319+
320+
status = BTP_STATUS_SUCCESS;
321+
322+
rsp:
323+
tester_rsp(BTP_SERVICE_ID_L2CAP, L2CAP_RECONFIGURE, CONTROLLER_INDEX,
324+
status);
325+
}
326+
258327
#if defined(CONFIG_BT_EATT)
259328
void disconnect_eatt_chans(uint8_t *data, uint16_t len)
260329
{
@@ -372,7 +441,7 @@ static int accept(struct bt_conn *conn, struct bt_l2cap_chan **l2cap_chan)
372441
}
373442

374443
chan->le.chan.ops = &l2cap_ops;
375-
chan->le.rx.mtu = DATA_MTU;
444+
chan->le.rx.mtu = DATA_MTU_INITIAL;
376445

377446
*l2cap_chan = &chan->le.chan;
378447

@@ -460,6 +529,7 @@ static void supported_commands(uint8_t *data, uint16_t len)
460529
tester_set_bit(cmds, L2CAP_DISCONNECT);
461530
tester_set_bit(cmds, L2CAP_LISTEN);
462531
tester_set_bit(cmds, L2CAP_SEND_DATA);
532+
tester_set_bit(cmds, L2CAP_RECONFIGURE);
463533
tester_set_bit(cmds, L2CAP_CREDITS);
464534
#if defined(CONFIG_BT_EATT)
465535
tester_set_bit(cmds, L2CAP_DISCONNECT_EATT_CHANS);
@@ -487,6 +557,9 @@ void tester_handle_l2cap(uint8_t opcode, uint8_t index, uint8_t *data,
487557
case L2CAP_LISTEN:
488558
listen(data, len);
489559
return;
560+
case L2CAP_RECONFIGURE:
561+
reconfigure(data, len);
562+
return;
490563
case L2CAP_CREDITS:
491564
credits(data, len);
492565
return;

0 commit comments

Comments
 (0)