l2control.c revision 220840
1272343Sngie/* 2272343Sngie * l2control.c 3272343Sngie * 4272343Sngie * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5272343Sngie * All rights reserved. 6272343Sngie * 7272343Sngie * Redistribution and use in source and binary forms, with or without 8272343Sngie * modification, are permitted provided that the following conditions 9272343Sngie * are met: 10272343Sngie * 1. Redistributions of source code must retain the above copyright 11272343Sngie * notice, this list of conditions and the following disclaimer. 12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 13272343Sngie * notice, this list of conditions and the following disclaimer in the 14272343Sngie * documentation and/or other materials provided with the distribution. 15272343Sngie * 16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17272343Sngie * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18272343Sngie * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19272343Sngie * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20272343Sngie * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22272343Sngie * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23272343Sngie * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24272343Sngie * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25272343Sngie * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26272343Sngie * SUCH DAMAGE. 27272343Sngie * 28272343Sngie * $Id: l2control.c,v 1.6 2003/09/05 00:38:25 max Exp $ 29272343Sngie * $FreeBSD: head/usr.sbin/bluetooth/l2control/l2control.c 220840 2011-04-19 16:21:57Z emax $ 30272343Sngie */ 31272343Sngie 32272343Sngie#include <assert.h> 33272343Sngie#include <bluetooth.h> 34272343Sngie#include <err.h> 35272343Sngie#include <errno.h> 36272343Sngie#include <stdio.h> 37272343Sngie#include <stdlib.h> 38272343Sngie#include <string.h> 39272343Sngie#include <unistd.h> 40272343Sngie#include "l2control.h" 41272343Sngie 42272343Sngie/* Prototypes */ 43272343Sngiestatic int do_l2cap_command (bdaddr_p, int, char **); 44272343Sngiestatic struct l2cap_command * find_l2cap_command (char const *, 45272343Sngie struct l2cap_command *); 46272343Sngiestatic void print_l2cap_command (struct l2cap_command *); 47272343Sngiestatic void usage (void); 48272343Sngie 49272343Sngie/* Main */ 50272343Sngie 51272343Sngieint numeric_bdaddr = 0; 52272343Sngie 53272343Sngieint 54272343Sngiemain(int argc, char *argv[]) 55272343Sngie{ 56272343Sngie int n; 57272343Sngie bdaddr_t bdaddr; 58272343Sngie 59272343Sngie memset(&bdaddr, 0, sizeof(bdaddr)); 60272343Sngie 61272343Sngie /* Process command line arguments */ 62 while ((n = getopt(argc, argv, "a:nh")) != -1) { 63 switch (n) { 64 case 'a': 65 if (!bt_aton(optarg, &bdaddr)) { 66 struct hostent *he = NULL; 67 68 if ((he = bt_gethostbyname(optarg)) == NULL) 69 errx(1, "%s: %s", optarg, hstrerror(h_errno)); 70 71 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr)); 72 } 73 break; 74 75 case 'n': 76 numeric_bdaddr = 1; 77 break; 78 79 case 'h': 80 default: 81 usage(); 82 break; 83 } 84 } 85 86 argc -= optind; 87 argv += optind; 88 89 if (*argv == NULL) 90 usage(); 91 92 return (do_l2cap_command(&bdaddr, argc, argv)); 93} /* main */ 94 95/* Execute commands */ 96static int 97do_l2cap_command(bdaddr_p bdaddr, int argc, char **argv) 98{ 99 char *cmd = argv[0]; 100 struct l2cap_command *c = NULL; 101 struct sockaddr_l2cap sa; 102 int s, e, help; 103 104 help = 0; 105 if (strcasecmp(cmd, "help") == 0) { 106 argc --; 107 argv ++; 108 109 if (argc <= 0) { 110 fprintf(stdout, "Supported commands:\n"); 111 print_l2cap_command(l2cap_commands); 112 fprintf(stdout, "\nFor more information use " \ 113 "'help command'\n"); 114 115 return (OK); 116 } 117 118 help = 1; 119 cmd = argv[0]; 120 } 121 122 c = find_l2cap_command(cmd, l2cap_commands); 123 if (c == NULL) { 124 fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 125 return (ERROR); 126 } 127 128 if (!help) { 129 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0) 130 usage(); 131 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)); 136 137 s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); 138 if (s < 0) 139 err(1, "Could not create socket"); 140 141 if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 142 err(2, 143"Could not bind socket, bdaddr=%s", bt_ntoa(&sa.l2cap_bdaddr, NULL)); 144 145 e = 0x0ffff; 146 if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0) 147 err(3, "Coult not setsockopt(RCVBUF, %d)", e); 148 149 e = (c->handler)(s, -- argc, ++ argv); 150 151 close(s); 152 } else 153 e = USAGE; 154 155 switch (e) { 156 case OK: 157 case FAILED: 158 break; 159 160 case ERROR: 161 fprintf(stdout, "Could not execute command \"%s\". %s\n", 162 cmd, strerror(errno)); 163 break; 164 165 case USAGE: 166 fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 167 break; 168 169 default: assert(0); break; 170 } 171 172 return (e); 173} /* do_l2cap_command */ 174 175/* Try to find command in specified category */ 176static struct l2cap_command * 177find_l2cap_command(char const *command, struct l2cap_command *category) 178{ 179 struct l2cap_command *c = NULL; 180 181 for (c = category; c->command != NULL; c++) { 182 char *c_end = strchr(c->command, ' '); 183 184 if (c_end != NULL) { 185 int len = c_end - c->command; 186 187 if (strncasecmp(command, c->command, len) == 0) 188 return (c); 189 } else if (strcasecmp(command, c->command) == 0) 190 return (c); 191 } 192 193 return (NULL); 194} /* find_l2cap_command */ 195 196/* Print commands in specified category */ 197static void 198print_l2cap_command(struct l2cap_command *category) 199{ 200 struct l2cap_command *c = NULL; 201 202 for (c = category; c->command != NULL; c++) 203 fprintf(stdout, "\t%s\n", c->command); 204} /* print_l2cap_command */ 205 206/* Usage */ 207static void 208usage(void) 209{ 210 fprintf(stderr, "Usage: l2control [-hn] -a local cmd [params ..]\n"); 211 fprintf(stderr, "Where:\n"); 212 fprintf(stderr, " -a local Specify local device to connect to\n"); 213 fprintf(stderr, " -h Display this message\n"); 214 fprintf(stderr, " -n Show addresses as numbers\n"); 215 fprintf(stderr, " cmd Supported command " \ 216 "(see l2control help)\n"); 217 fprintf(stderr, " params Optional command parameters\n"); 218 exit(255); 219} /* usage */ 220 221