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: l2control.c,v 1.6 2003/09/05 00:38:25 max Exp $
33 #define L2CAP_SOCKET_CHECKED
34 #include <bluetooth.h>
41 #include "l2control.h"
44 static int do_l2cap_command (bdaddr_p, int, char **);
45 static struct l2cap_command * find_l2cap_command (char const *,
46 struct l2cap_command *);
47 static void print_l2cap_command (struct l2cap_command *);
48 static void usage (void);
52 int numeric_bdaddr = 0;
55 main(int argc, char *argv[])
60 memset(&bdaddr, 0, sizeof(bdaddr));
62 /* Process command line arguments */
63 while ((n = getopt(argc, argv, "a:nh")) != -1) {
66 if (!bt_aton(optarg, &bdaddr)) {
67 struct hostent *he = NULL;
69 if ((he = bt_gethostbyname(optarg)) == NULL)
70 errx(1, "%s: %s", optarg, hstrerror(h_errno));
72 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
93 return (do_l2cap_command(&bdaddr, argc, argv));
96 /* Execute commands */
98 do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv)
101 struct l2cap_command *c = NULL;
102 struct sockaddr_l2cap sa;
106 if (strcasecmp(cmd, "help") == 0) {
111 fprintf(stdout, "Supported commands:\n");
112 print_l2cap_command(l2cap_commands);
113 fprintf(stdout, "\nFor more information use " \
123 c = find_l2cap_command(cmd, l2cap_commands);
125 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
130 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
133 memset(&sa, 0, sizeof(sa));
134 sa.l2cap_len = sizeof(sa);
135 sa.l2cap_family = AF_BLUETOOTH;
136 memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr));
138 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
140 err(1, "Could not create socket");
142 if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
144 "Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
147 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0)
148 err(3, "Coult not setsockopt(RCVBUF, %d)", e);
150 e = (c->handler)(s, -- argc, ++ argv);
162 fprintf(stdout, "Could not execute command \"%s\". %s\n",
163 cmd, strerror(errno));
167 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
170 default: assert(0); break;
174 } /* do_l2cap_command */
176 /* Try to find command in specified category */
177 static struct l2cap_command *
178 find_l2cap_command(char const *command, struct l2cap_command *category)
180 struct l2cap_command *c = NULL;
182 for (c = category; c->command != NULL; c++) {
183 char *c_end = strchr(c->command, ' ');
186 int len = c_end - c->command;
188 if (strncasecmp(command, c->command, len) == 0)
190 } else if (strcasecmp(command, c->command) == 0)
195 } /* find_l2cap_command */
197 /* Print commands in specified category */
199 print_l2cap_command(struct l2cap_command *category)
201 struct l2cap_command *c = NULL;
203 for (c = category; c->command != NULL; c++)
204 fprintf(stdout, "\t%s\n", c->command);
205 } /* print_l2cap_command */
211 fprintf(stderr, "Usage: l2control [-hn] -a local cmd [params ..]\n");
212 fprintf(stderr, "Where:\n");
213 fprintf(stderr, " -a local Specify local device to connect to\n");
214 fprintf(stderr, " -h Display this message\n");
215 fprintf(stderr, " -n Show addresses as numbers\n");
216 fprintf(stderr, " cmd Supported command " \
217 "(see l2control help)\n");
218 fprintf(stderr, " params Optional command parameters\n");