4 * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $Id: l2cap.c,v 1.5 2003/05/16 19:52:37 max Exp $
32 #include <sys/ioctl.h>
33 #include <bluetooth.h>
38 #include "l2control.h"
40 #define SIZE(x) (sizeof((x))/sizeof((x)[0]))
44 bdaddrpr(bdaddr_t const *ba)
46 extern int numeric_bdaddr;
48 struct hostent *he = NULL;
50 if (memcmp(ba, NG_HCI_BDADDR_ANY, sizeof(*ba)) == 0) {
57 if (!numeric_bdaddr &&
58 (he = bt_gethostbyaddr((char *)ba, sizeof(*ba), AF_BLUETOOTH)) != NULL) {
59 strlcpy(str, he->h_name, sizeof(str));
69 /* Send read_node_flags command to the node */
71 l2cap_read_node_flags(int s, int argc, char **argv)
73 struct ng_btsocket_l2cap_raw_node_flags r;
75 memset(&r, 0, sizeof(r));
76 if (ioctl(s, SIOC_L2CAP_NODE_GET_FLAGS, &r, sizeof(r)) < 0)
79 fprintf(stdout, "Connectionless traffic flags:\n");
80 fprintf(stdout, "\tSDP: %s\n",
81 (r.flags & NG_L2CAP_CLT_SDP_DISABLED)? "disabled" : "enabled");
82 fprintf(stdout, "\tRFCOMM: %s\n",
83 (r.flags & NG_L2CAP_CLT_RFCOMM_DISABLED)? "disabled":"enabled");
84 fprintf(stdout, "\tTCP: %s\n",
85 (r.flags & NG_L2CAP_CLT_TCP_DISABLED)? "disabled" : "enabled");
88 } /* l2cap_read_node_flags */
90 /* Send read_debug_level command to the node */
92 l2cap_read_debug_level(int s, int argc, char **argv)
94 struct ng_btsocket_l2cap_raw_node_debug r;
96 memset(&r, 0, sizeof(r));
97 if (ioctl(s, SIOC_L2CAP_NODE_GET_DEBUG, &r, sizeof(r)) < 0)
100 fprintf(stdout, "Debug level: %d\n", r.debug);
103 } /* l2cap_read_debug_level */
105 /* Send write_debug_level command to the node */
107 l2cap_write_debug_level(int s, int argc, char **argv)
109 struct ng_btsocket_l2cap_raw_node_debug r;
111 memset(&r, 0, sizeof(r));
114 r.debug = atoi(argv[0]);
121 if (ioctl(s, SIOC_L2CAP_NODE_SET_DEBUG, &r, sizeof(r)) < 0)
125 } /* l2cap_write_debug_level */
127 /* Send read_connection_list command to the node */
129 l2cap_read_connection_list(int s, int argc, char **argv)
131 static char const * const state[] = {
132 /* NG_L2CAP_CON_CLOSED */ "CLOSED",
133 /* NG_L2CAP_W4_LP_CON_CFM */ "W4_LP_CON_CFM",
134 /* NG_L2CAP_CON_OPEN */ "OPEN"
136 #define con_state2str(x) ((x) >= SIZE(state)? "UNKNOWN" : state[(x)])
138 struct ng_btsocket_l2cap_raw_con_list r;
141 memset(&r, 0, sizeof(r));
142 r.num_connections = NG_L2CAP_MAX_CON_NUM;
143 r.connections = calloc(NG_L2CAP_MAX_CON_NUM,
144 sizeof(ng_l2cap_node_con_ep));
145 if (r.connections == NULL) {
150 if (ioctl(s, SIOC_L2CAP_NODE_GET_CON_LIST, &r, sizeof(r)) < 0) {
155 fprintf(stdout, "L2CAP connections:\n");
157 "Remote BD_ADDR Handle Flags Pending State\n");
158 for (n = 0; n < r.num_connections; n++) {
165 bdaddrpr(&r.connections[n].remote),
166 r.connections[n].con_handle,
167 ((r.connections[n].flags & NG_L2CAP_CON_OUTGOING)? 'O' : 'I'),
168 ((r.connections[n].flags & NG_L2CAP_CON_LP_TIMO)? 'L' : ' '),
169 ((r.connections[n].flags & NG_L2CAP_CON_AUTO_DISCON_TIMO)? 'D' : ' '),
170 ((r.connections[n].flags & NG_L2CAP_CON_TX)? 'T' : ' '),
171 ((r.connections[n].flags & NG_L2CAP_CON_RX)? 'R' : ' '),
172 r.connections[n].pending,
173 con_state2str(r.connections[n].state));
179 } /* l2cap_read_connection_list */
181 /* Send read_channel_list command to the node */
183 l2cap_read_channel_list(int s, int argc, char **argv)
185 static char const * const state[] = {
186 /* NG_L2CAP_CLOSED */ "CLOSED",
187 /* NG_L2CAP_W4_L2CAP_CON_RSP */ "W4_L2CAP_CON_RSP",
188 /* NG_L2CAP_W4_L2CA_CON_RSP */ "W4_L2CA_CON_RSP",
189 /* NG_L2CAP_CONFIG */ "CONFIG",
190 /* NG_L2CAP_OPEN */ "OPEN",
191 /* NG_L2CAP_W4_L2CAP_DISCON_RSP */ "W4_L2CAP_DISCON_RSP",
192 /* NG_L2CAP_W4_L2CA_DISCON_RSP */ "W4_L2CA_DISCON_RSP"
194 #define ch_state2str(x) ((x) >= SIZE(state)? "UNKNOWN" : state[(x)])
196 struct ng_btsocket_l2cap_raw_chan_list r;
199 memset(&r, 0, sizeof(r));
200 r.num_channels = NG_L2CAP_MAX_CHAN_NUM;
201 r.channels = calloc(NG_L2CAP_MAX_CHAN_NUM,
202 sizeof(ng_l2cap_node_chan_ep));
203 if (r.channels == NULL) {
208 if (ioctl(s, SIOC_L2CAP_NODE_GET_CHAN_LIST, &r, sizeof(r)) < 0) {
213 fprintf(stdout, "L2CAP channels:\n");
215 "Remote BD_ADDR SCID/ DCID PSM IMTU/ OMTU State\n");
216 for (n = 0; n < r.num_channels; n++) {
222 bdaddrpr(&r.channels[n].remote),
223 r.channels[n].scid, r.channels[n].dcid,
224 r.channels[n].psm, r.channels[n].imtu,
226 ch_state2str(r.channels[n].state));
232 } /* l2cap_read_channel_list */
234 /* Send read_auto_disconnect_timeout command to the node */
236 l2cap_read_auto_disconnect_timeout(int s, int argc, char **argv)
238 struct ng_btsocket_l2cap_raw_auto_discon_timo r;
240 memset(&r, 0, sizeof(r));
241 if (ioctl(s, SIOC_L2CAP_NODE_GET_AUTO_DISCON_TIMO, &r, sizeof(r)) < 0)
245 fprintf(stdout, "Auto disconnect timeout: %d sec\n", r.timeout);
247 fprintf(stdout, "Auto disconnect disabled\n");
250 } /* l2cap_read_auto_disconnect_timeout */
252 /* Send write_auto_disconnect_timeout command to the node */
254 l2cap_write_auto_disconnect_timeout(int s, int argc, char **argv)
256 struct ng_btsocket_l2cap_raw_auto_discon_timo r;
258 memset(&r, 0, sizeof(r));
261 r.timeout = atoi(argv[0]);
268 if (ioctl(s, SIOC_L2CAP_NODE_SET_AUTO_DISCON_TIMO, &r, sizeof(r)) < 0)
272 } /* l2cap_write_auto_disconnect_timeout */
274 struct l2cap_command l2cap_commands[] = {
277 "Get L2CAP node flags",
278 &l2cap_read_node_flags
282 "Get L2CAP node debug level",
283 &l2cap_read_debug_level
286 "write_debug_level <level>",
287 "Set L2CAP node debug level",
288 &l2cap_write_debug_level
291 "read_connection_list",
292 "Read list of the L2CAP connections",
293 &l2cap_read_connection_list
297 "Read list of the L2CAP channels",
298 &l2cap_read_channel_list
301 "read_auto_disconnect_timeout",
302 "Get L2CAP node auto disconnect timeout (in sec)",
303 &l2cap_read_auto_disconnect_timeout
306 "write_auto_disconnect_timeout <timeout>",
307 "Set L2CAP node auto disconnect timeout (in sec)",
308 &l2cap_write_auto_disconnect_timeout