4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $Id: l2cap.c,v 1.5 2003/05/16 19:52:37 max Exp $
34 #include <sys/ioctl.h>
35 #define L2CAP_SOCKET_CHECKED
36 #include <bluetooth.h>
41 #include "l2control.h"
43 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
47 bdaddrpr(bdaddr_t const *ba)
49 extern int numeric_bdaddr;
51 struct hostent *he = NULL;
53 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
60 if (!numeric_bdaddr &&
61 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
62 strlcpy(str, he->h_name, sizeof(str));
72 /* Send read_node_flags command to the node */
74 l2cap_read_node_flags(int s, int argc, char **argv)
76 struct ng_btsocket_l2cap_raw_node_flags r;
78 memset(&r, 0, sizeof(r));
79 if (ioctl(s, SIOC_L2CAP_NODE_GET_FLAGS, &r, sizeof(r)) < 0)
82 fprintf(stdout, "Connectionless traffic flags:\n");
83 fprintf(stdout, "\tSDP: %s\n",
84 (r.flags & NG_L2CAP_CLT_SDP_DISABLED)? "disabled" : "enabled");
85 fprintf(stdout, "\tRFCOMM: %s\n",
86 (r.flags & NG_L2CAP_CLT_RFCOMM_DISABLED)? "disabled":"enabled");
87 fprintf(stdout, "\tTCP: %s\n",
88 (r.flags & NG_L2CAP_CLT_TCP_DISABLED)? "disabled" : "enabled");
91 } /* l2cap_read_node_flags */
93 /* Send read_debug_level command to the node */
95 l2cap_read_debug_level(int s, int argc, char **argv)
97 struct ng_btsocket_l2cap_raw_node_debug r;
99 memset(&r, 0, sizeof(r));
100 if (ioctl(s, SIOC_L2CAP_NODE_GET_DEBUG, &r, sizeof(r)) < 0)
103 fprintf(stdout, "Debug level: %d\n", r.debug);
106 } /* l2cap_read_debug_level */
108 /* Send write_debug_level command to the node */
110 l2cap_write_debug_level(int s, int argc, char **argv)
112 struct ng_btsocket_l2cap_raw_node_debug r;
114 memset(&r, 0, sizeof(r));
117 r.debug = atoi(argv[0]);
124 if (ioctl(s, SIOC_L2CAP_NODE_SET_DEBUG, &r, sizeof(r)) < 0)
128 } /* l2cap_write_debug_level */
130 /* Send read_connection_list command to the node */
132 l2cap_read_connection_list(int s, int argc, char **argv)
134 static char const * const state[] = {
135 /* NG_L2CAP_CON_CLOSED */ "CLOSED",
136 /* NG_L2CAP_W4_LP_CON_CFM */ "W4_LP_CON_CFM",
137 /* NG_L2CAP_CON_OPEN */ "OPEN"
139 #define con_state2str(x) ((x) >= SIZE(state)? "UNKNOWN" : state[(x)])
141 struct ng_btsocket_l2cap_raw_con_list r;
144 memset(&r, 0, sizeof(r));
145 r.num_connections = NG_L2CAP_MAX_CON_NUM;
146 r.connections = calloc(NG_L2CAP_MAX_CON_NUM,
147 sizeof(ng_l2cap_node_con_ep));
148 if (r.connections == NULL) {
153 if (ioctl(s, SIOC_L2CAP_NODE_GET_CON_LIST, &r, sizeof(r)) < 0) {
158 fprintf(stdout, "L2CAP connections:\n");
160 "Remote BD_ADDR Handle Flags Pending State\n");
161 for (n = 0; n < r.num_connections; n++) {
168 bdaddrpr(&r.connections[n].remote),
169 r.connections[n].con_handle,
170 ((r.connections[n].flags & NG_L2CAP_CON_OUTGOING)? 'O' : 'I'),
171 ((r.connections[n].flags & NG_L2CAP_CON_LP_TIMO)? 'L' : ' '),
172 ((r.connections[n].flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)? 'D' : ' '),
173 ((r.connections[n].flags & NG_L2CAP_CON_TX)? 'T' : ' '),
174 ((r.connections[n].flags & NG_L2CAP_CON_RX)? 'R' : ' '),
175 r.connections[n].pending,
176 con_state2str(r.connections[n].state));
182 } /* l2cap_read_connection_list */
184 /* Send read_channel_list command to the node */
186 l2cap_read_channel_list(int s, int argc, char **argv)
188 static char const * const state[] = {
189 /* NG_L2CAP_CLOSED */ "CLOSED",
190 /* NG_L2CAP_W4_L2CAP_CON_RSP */ "W4_L2CAP_CON_RSP",
191 /* NG_L2CAP_W4_L2CA_CON_RSP */ "W4_L2CA_CON_RSP",
192 /* NG_L2CAP_CONFIG */ "CONFIG",
193 /* NG_L2CAP_OPEN */ "OPEN",
194 /* NG_L2CAP_W4_L2CAP_DISCON_RSP */ "W4_L2CAP_DISCON_RSP",
195 /* NG_L2CAP_W4_L2CA_DISCON_RSP */ "W4_L2CA_DISCON_RSP"
197 #define ch_state2str(x) ((x) >= SIZE(state)? "UNKNOWN" : state[(x)])
199 struct ng_btsocket_l2cap_raw_chan_list r;
202 memset(&r, 0, sizeof(r));
203 r.num_channels = NG_L2CAP_MAX_CHAN_NUM;
204 r.channels = calloc(NG_L2CAP_MAX_CHAN_NUM,
205 sizeof(ng_l2cap_node_chan_ep));
206 if (r.channels == NULL) {
211 if (ioctl(s, SIOC_L2CAP_NODE_GET_CHAN_LIST, &r, sizeof(r)) < 0) {
216 fprintf(stdout, "L2CAP channels:\n");
218 "Remote BD_ADDR SCID/ DCID PSM IMTU/ OMTU State\n");
219 for (n = 0; n < r.num_channels; n++) {
225 bdaddrpr(&r.channels[n].remote),
226 r.channels[n].scid, r.channels[n].dcid,
227 r.channels[n].psm, r.channels[n].imtu,
229 ch_state2str(r.channels[n].state));
235 } /* l2cap_read_channel_list */
237 /* Send read_auto_disconnect_timeout command to the node */
239 l2cap_read_auto_disconnect_timeout(int s, int argc, char **argv)
241 struct ng_btsocket_l2cap_raw_auto_discon_timo r;
243 memset(&r, 0, sizeof(r));
244 if (ioctl(s, SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO, &r, sizeof(r)) < 0)
248 fprintf(stdout, "Auto disconnect timeout: %d sec\n", r.timeout);
250 fprintf(stdout, "Auto disconnect disabled\n");
253 } /* l2cap_read_auto_disconnect_timeout */
255 /* Send write_auto_disconnect_timeout command to the node */
257 l2cap_write_auto_disconnect_timeout(int s, int argc, char **argv)
259 struct ng_btsocket_l2cap_raw_auto_discon_timo r;
261 memset(&r, 0, sizeof(r));
264 r.timeout = atoi(argv[0]);
271 if (ioctl(s, SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO, &r, sizeof(r)) < 0)
275 } /* l2cap_write_auto_disconnect_timeout */
277 struct l2cap_command l2cap_commands[] = {
280 "Get L2CAP node flags",
281 &l2cap_read_node_flags
285 "Get L2CAP node debug level",
286 &l2cap_read_debug_level
289 "write_debug_level <level>",
290 "Set L2CAP node debug level",
291 &l2cap_write_debug_level
294 "read_connection_list",
295 "Read list of the L2CAP connections",
296 &l2cap_read_connection_list
300 "Read list of the L2CAP channels",
301 &l2cap_read_channel_list
304 "read_auto_disconnect_timeout",
305 "Get L2CAP node auto disconnect timeout (in sec)",
306 &l2cap_read_auto_disconnect_timeout
309 "write_auto_disconnect_timeout <timeout>",
310 "Set L2CAP node auto disconnect timeout (in sec)",
311 &l2cap_write_auto_disconnect_timeout