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 #include <bluetooth.h>
40 #include "l2control.h"
43 static int do_l2cap_command (bdaddr_p, int, char **);
44 static struct l2cap_command * find_l2cap_command (char const *,
45 struct l2cap_command *);
46 static void print_l2cap_command (struct l2cap_command *);
47 static void usage (void);
51 int numeric_bdaddr = 0;
54 main(int argc, char *argv[])
59 memset(&bdaddr, 0, sizeof(bdaddr));
61 /* Process command line arguments */
62 while ((n = getopt(argc, argv, "a:nh")) != -1) {
65 if (!bt_aton(optarg, &bdaddr)) {
66 struct hostent *he = NULL;
68 if ((he = bt_gethostbyname(optarg)) == NULL)
69 errx(1, "%s: %s", optarg, hstrerror(h_errno));
71 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
92 return (do_l2cap_command(&bdaddr, argc, argv));
95 /* Execute commands */
97 do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv)
100 struct l2cap_command *c = NULL;
101 struct sockaddr_l2cap sa;
105 if (strcasecmp(cmd, "help") == 0) {
110 fprintf(stdout, "Supported commands:\n");
111 print_l2cap_command(l2cap_commands);
112 fprintf(stdout, "\nFor more information use " \
122 c = find_l2cap_command(cmd, l2cap_commands);
124 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
129 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
132 memset(&sa, 0, sizeof(sa));
133 sa.l2cap_len = sizeof(sa);
134 sa.l2cap_family = AF_BLUETOOTH;
135 memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr));
137 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP);
139 err(1, "Could not create socket");
141 if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
143 "Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL));
146 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0)
147 err(3, "Coult not setsockopt(RCVBUF, %d)", e);
149 e = (c->handler)(s, -- argc, ++ argv);
161 fprintf(stdout, "Could not execute command \"%s\". %s\n",
162 cmd, strerror(errno));
166 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description);
169 default: assert(0); break;
173 } /* do_l2cap_command */
175 /* Try to find command in specified category */
176 static struct l2cap_command *
177 find_l2cap_command(char const *command, struct l2cap_command *category)
179 struct l2cap_command *c = NULL;
181 for (c = category; c->command != NULL; c++) {
182 char *c_end = strchr(c->command, ' ');
185 int len = c_end - c->command;
187 if (strncasecmp(command, c->command, len) == 0)
189 } else if (strcasecmp(command, c->command) == 0)
194 } /* find_l2cap_command */
196 /* Print commands in specified category */
198 print_l2cap_command(struct l2cap_command *category)
200 struct l2cap_command *c = NULL;
202 for (c = category; c->command != NULL; c++)
203 fprintf(stdout, "\t%s\n", c->command);
204 } /* print_l2cap_command */
210 fprintf(stdout, "Usage: l2control -a BD_ADDR [-n] [-h] cmd [p1] [..]]\n");