Skip to content

Commit 4897f26

Browse files
committed
Bluetooth: shell: Split L2CAP commands
This splits L2CAP commands under 'bt' to 'l2cap' removing 'l2cap-' prefix from the commands: l2cap - Bluetooth L2CAP shell commands Options: -h, --help :Show command help. Subcommands: connect :<psm> disconnect :[none] metrics :<value on, off> recv :[delay (in miliseconds) register :<psm> [sec_level] send :<number of packets> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent e12b772 commit 4897f26

File tree

4 files changed

+327
-278
lines changed

4 files changed

+327
-278
lines changed

subsys/bluetooth/shell/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ zephyr_library_sources_ifdef(
1010
CONFIG_BT_BREDR
1111
bredr.c
1212
)
13+
zephyr_library_sources_ifdef(
14+
CONFIG_BT_L2CAP_DYNAMIC_CHANNEL
15+
l2cap.c
16+
)
1317
zephyr_library_sources_ifdef(
1418
CONFIG_BT_RFCOMM
1519
rfcomm.c

subsys/bluetooth/shell/bt.c

Lines changed: 1 addition & 278 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <bluetooth/hci.h>
2525
#include <bluetooth/bluetooth.h>
2626
#include <bluetooth/conn.h>
27-
#include <bluetooth/l2cap.h>
2827
#include <bluetooth/rfcomm.h>
2928
#include <bluetooth/sdp.h>
3029

@@ -35,8 +34,6 @@
3534

3635
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
3736
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)
38-
#define CREDITS 10
39-
#define DATA_MTU (23 * CREDITS)
4037

4138
static u8_t selected_id = BT_ID_DEFAULT;
4239
const struct shell *ctx_shell;
@@ -48,11 +45,6 @@ struct bt_conn *default_conn;
4845
static struct bt_conn *pairing_conn;
4946
#endif /* CONFIG_BT_CONN */
5047

51-
#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
52-
NET_BUF_POOL_DEFINE(data_tx_pool, 1, DATA_MTU, BT_BUF_USER_DATA_MIN, NULL);
53-
NET_BUF_POOL_DEFINE(data_rx_pool, 1, DATA_MTU, BT_BUF_USER_DATA_MIN, NULL);
54-
#endif
55-
5648
#define NAME_LEN 30
5749

5850
static bool data_cb(struct bt_data *data, void *user_data)
@@ -335,7 +327,7 @@ static void cmd_init(const struct shell *shell, size_t argc, char *argv[])
335327
}
336328

337329
#if defined(CONFIG_BT_HCI) || defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
338-
static void hexdump(const struct shell *shell, const u8_t *data, size_t len)
330+
void hexdump(const struct shell *shell, const u8_t *data, size_t len)
339331
{
340332
int n = 0;
341333

@@ -1293,266 +1285,6 @@ static void cmd_auth_passkey(const struct shell *shell,
12931285
}
12941286
#endif /* CONFIG_BT_SMP) || CONFIG_BT_BREDR */
12951287

1296-
#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
1297-
static u32_t l2cap_rate;
1298-
static u32_t l2cap_recv_delay;
1299-
static K_FIFO_DEFINE(l2cap_recv_fifo);
1300-
struct l2ch {
1301-
struct k_delayed_work recv_work;
1302-
struct bt_l2cap_le_chan ch;
1303-
};
1304-
#define L2CH_CHAN(_chan) CONTAINER_OF(_chan, struct l2ch, ch.chan)
1305-
#define L2CH_WORK(_work) CONTAINER_OF(_work, struct l2ch, recv_work)
1306-
#define L2CAP_CHAN(_chan) _chan->ch.chan
1307-
1308-
static int l2cap_recv_metrics(struct bt_l2cap_chan *chan, struct net_buf *buf)
1309-
{
1310-
static u32_t len;
1311-
static u32_t cycle_stamp;
1312-
u32_t delta;
1313-
1314-
delta = k_cycle_get_32() - cycle_stamp;
1315-
delta = SYS_CLOCK_HW_CYCLES_TO_NS(delta);
1316-
1317-
/* if last data rx-ed was greater than 1 second in the past,
1318-
* reset the metrics.
1319-
*/
1320-
if (delta > 1000000000) {
1321-
len = 0;
1322-
l2cap_rate = 0;
1323-
cycle_stamp = k_cycle_get_32();
1324-
} else {
1325-
len += buf->len;
1326-
l2cap_rate = ((u64_t)len << 3) * 1000000000 / delta;
1327-
}
1328-
1329-
return 0;
1330-
}
1331-
1332-
static void l2cap_recv_cb(struct k_work *work)
1333-
{
1334-
struct l2ch *c = L2CH_WORK(work);
1335-
struct net_buf *buf;
1336-
1337-
while ((buf = net_buf_get(&l2cap_recv_fifo, K_NO_WAIT))) {
1338-
print(NULL, "Confirming reception\n");
1339-
bt_l2cap_chan_recv_complete(&c->ch.chan, buf);
1340-
}
1341-
}
1342-
1343-
static int l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
1344-
{
1345-
struct l2ch *l2ch = L2CH_CHAN(chan);
1346-
1347-
print(NULL, "Incoming data channel %p len %u\n", chan, buf->len);
1348-
1349-
if (buf->len) {
1350-
hexdump(ctx_shell, buf->data, buf->len);
1351-
}
1352-
1353-
if (l2cap_recv_delay) {
1354-
/* Submit work only if queue is empty */
1355-
if (k_fifo_is_empty(&l2cap_recv_fifo)) {
1356-
print(NULL, "Delaying response in %u ms...\n",
1357-
l2cap_recv_delay);
1358-
k_delayed_work_submit(&l2ch->recv_work,
1359-
l2cap_recv_delay);
1360-
}
1361-
net_buf_put(&l2cap_recv_fifo, buf);
1362-
return -EINPROGRESS;
1363-
}
1364-
1365-
return 0;
1366-
}
1367-
1368-
static void l2cap_connected(struct bt_l2cap_chan *chan)
1369-
{
1370-
struct l2ch *c = L2CH_CHAN(chan);
1371-
1372-
k_delayed_work_init(&c->recv_work, l2cap_recv_cb);
1373-
1374-
print(NULL, "Channel %p connected\n", chan);
1375-
}
1376-
1377-
static void l2cap_disconnected(struct bt_l2cap_chan *chan)
1378-
{
1379-
print(NULL, "Channel %p disconnected\n", chan);
1380-
}
1381-
1382-
static struct net_buf *l2cap_alloc_buf(struct bt_l2cap_chan *chan)
1383-
{
1384-
/* print if metrics is disabled */
1385-
if (chan->ops->recv != l2cap_recv_metrics) {
1386-
print(NULL, "Channel %p requires buffer\n", chan);
1387-
}
1388-
1389-
return net_buf_alloc(&data_rx_pool, K_FOREVER);
1390-
}
1391-
1392-
static struct bt_l2cap_chan_ops l2cap_ops = {
1393-
.alloc_buf = l2cap_alloc_buf,
1394-
.recv = l2cap_recv,
1395-
.connected = l2cap_connected,
1396-
.disconnected = l2cap_disconnected,
1397-
};
1398-
1399-
1400-
static struct l2ch l2ch_chan = {
1401-
.ch.chan.ops = &l2cap_ops,
1402-
.ch.rx.mtu = DATA_MTU,
1403-
};
1404-
1405-
static int l2cap_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan)
1406-
{
1407-
print(NULL, "Incoming conn %p\n", conn);
1408-
1409-
if (l2ch_chan.ch.chan.conn) {
1410-
print(NULL, "No channels available\n");
1411-
return -ENOMEM;
1412-
}
1413-
1414-
*chan = &l2ch_chan.ch.chan;
1415-
1416-
return 0;
1417-
}
1418-
1419-
static struct bt_l2cap_server server = {
1420-
.accept = l2cap_accept,
1421-
};
1422-
1423-
static void cmd_l2cap_register(const struct shell *shell,
1424-
size_t argc, char *argv[])
1425-
{
1426-
1427-
if (!shell_cmd_precheck(shell, argc >= 2, NULL, 0)) {
1428-
return;
1429-
}
1430-
1431-
if (server.psm) {
1432-
error(shell, "Already registered\n");
1433-
return;
1434-
}
1435-
1436-
server.psm = strtoul(argv[1], NULL, 16);
1437-
1438-
if (argc > 2) {
1439-
server.sec_level = strtoul(argv[2], NULL, 10);
1440-
}
1441-
1442-
if (bt_l2cap_server_register(&server) < 0) {
1443-
error(shell, "Unable to register psm\n");
1444-
server.psm = 0;
1445-
} else {
1446-
print(shell, "L2CAP psm %u sec_level %u registered\n",
1447-
server.psm, server.sec_level);
1448-
}
1449-
}
1450-
1451-
static void cmd_l2cap_connect(const struct shell *shell,
1452-
size_t argc, char *argv[])
1453-
{
1454-
u16_t psm;
1455-
int err;
1456-
1457-
if (!default_conn) {
1458-
error(shell, "Not connected\n");
1459-
return;
1460-
}
1461-
1462-
if (!shell_cmd_precheck(shell, argc == 2, NULL, 0)) {
1463-
return;
1464-
}
1465-
1466-
if (l2ch_chan.ch.chan.conn) {
1467-
error(shell, "Channel already in use\n");
1468-
return;
1469-
}
1470-
1471-
psm = strtoul(argv[1], NULL, 16);
1472-
1473-
err = bt_l2cap_chan_connect(default_conn, &l2ch_chan.ch.chan, psm);
1474-
if (err < 0) {
1475-
error(shell, "Unable to connect to psm %u (err %u)\n", psm,
1476-
err);
1477-
} else {
1478-
print(shell, "L2CAP connection pending\n");
1479-
}
1480-
}
1481-
1482-
static void cmd_l2cap_disconnect(const struct shell *shell,
1483-
size_t argc, char *argv[])
1484-
{
1485-
int err;
1486-
1487-
err = bt_l2cap_chan_disconnect(&l2ch_chan.ch.chan);
1488-
if (err) {
1489-
print(shell, "Unable to disconnect: %u\n", -err);
1490-
}
1491-
}
1492-
1493-
static void cmd_l2cap_send(const struct shell *shell, size_t argc, char *argv[])
1494-
{
1495-
static u8_t buf_data[DATA_MTU] = { [0 ... (DATA_MTU - 1)] = 0xff };
1496-
int ret, len, count = 1;
1497-
struct net_buf *buf;
1498-
1499-
if (argc > 1) {
1500-
count = strtoul(argv[1], NULL, 10);
1501-
}
1502-
1503-
len = min(l2ch_chan.ch.tx.mtu, DATA_MTU - BT_L2CAP_CHAN_SEND_RESERVE);
1504-
1505-
while (count--) {
1506-
buf = net_buf_alloc(&data_tx_pool, K_FOREVER);
1507-
net_buf_reserve(buf, BT_L2CAP_CHAN_SEND_RESERVE);
1508-
1509-
net_buf_add_mem(buf, buf_data, len);
1510-
ret = bt_l2cap_chan_send(&l2ch_chan.ch.chan, buf);
1511-
if (ret < 0) {
1512-
print(shell, "Unable to send: %d\n", -ret);
1513-
net_buf_unref(buf);
1514-
break;
1515-
}
1516-
}
1517-
}
1518-
1519-
static void cmd_l2cap_recv(const struct shell *shell, size_t argc, char *argv[])
1520-
{
1521-
if (argc > 1) {
1522-
l2cap_recv_delay = strtoul(argv[1], NULL, 10);
1523-
} else {
1524-
print(shell, "l2cap receive delay: %u ms\n",
1525-
l2cap_recv_delay);
1526-
}
1527-
}
1528-
1529-
static void cmd_l2cap_metrics(const struct shell *shell,
1530-
size_t argc, char *argv[])
1531-
{
1532-
const char *action;
1533-
1534-
if (argc < 2) {
1535-
print(shell, "l2cap rate: %u bps.\n", l2cap_rate);
1536-
1537-
return;
1538-
}
1539-
1540-
action = argv[1];
1541-
1542-
if (!strcmp(action, "on")) {
1543-
l2cap_ops.recv = l2cap_recv_metrics;
1544-
} else if (!strcmp(action, "off")) {
1545-
l2cap_ops.recv = l2cap_recv;
1546-
} else {
1547-
shell_help_print(shell, NULL, 0);
1548-
return;
1549-
}
1550-
1551-
print(shell, "l2cap metrics %s.\n", action);
1552-
}
1553-
1554-
#endif
1555-
15561288
#define HELP_NONE "[none]"
15571289
#define HELP_ADDR_LE "<address: XX:XX:XX:XX:XX:XX> <type: (public|random)>"
15581290

@@ -1602,15 +1334,6 @@ SHELL_CREATE_STATIC_SUBCMD_SET(bt_cmds) {
16021334
SHELL_CMD(fixed-passkey, NULL, "[passkey]", cmd_fixed_passkey),
16031335
#endif
16041336
#endif /* CONFIG_BT_SMP || CONFIG_BT_BREDR) */
1605-
#if defined(CONFIG_BT_L2CAP_DYNAMIC_CHANNEL)
1606-
SHELL_CMD(l2cap-register, NULL, "<psm> [sec_level]",
1607-
cmd_l2cap_register),
1608-
SHELL_CMD(l2cap-connect, NULL, "<psm>", cmd_l2cap_connect),
1609-
SHELL_CMD(l2cap-disconnect, NULL, HELP_NONE, cmd_l2cap_disconnect),
1610-
SHELL_CMD(l2cap-send, NULL, "<number of packets>", cmd_l2cap_send),
1611-
SHELL_CMD(l2cap-recv, NULL, "[delay (in miliseconds)", cmd_l2cap_recv),
1612-
SHELL_CMD(l2cap-metrics, NULL, "<value on, off>", cmd_l2cap_metrics),
1613-
#endif
16141337
#endif /* CONFIG_BT_CONN */
16151338
#if defined(CONFIG_BT_CTLR_ADV_EXT)
16161339
SHELL_CMD(advx, NULL, "<on off> [coded] [anon] [txp]", cmd_advx),

subsys/bluetooth/shell/bt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ extern struct bt_conn *default_conn;
2424

2525
int str2bt_addr(const char *str, bt_addr_t *addr);
2626
void conn_addr_str(struct bt_conn *conn, char *addr, size_t len);
27+
void hexdump(const struct shell *shell, const u8_t *data, size_t len);
2728

2829
#endif /* __BT_H */

0 commit comments

Comments
 (0)