l2control.c revision 107120
1107120Sjulian/* 2107120Sjulian * l2control.c 3107120Sjulian * 4107120Sjulian * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 5107120Sjulian * All rights reserved. 6107120Sjulian * 7107120Sjulian * Redistribution and use in source and binary forms, with or without 8107120Sjulian * modification, are permitted provided that the following conditions 9107120Sjulian * are met: 10107120Sjulian * 1. Redistributions of source code must retain the above copyright 11107120Sjulian * notice, this list of conditions and the following disclaimer. 12107120Sjulian * 2. Redistributions in binary form must reproduce the above copyright 13107120Sjulian * notice, this list of conditions and the following disclaimer in the 14107120Sjulian * documentation and/or other materials provided with the distribution. 15107120Sjulian * 16107120Sjulian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17107120Sjulian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18107120Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19107120Sjulian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20107120Sjulian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21107120Sjulian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22107120Sjulian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23107120Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24107120Sjulian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25107120Sjulian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26107120Sjulian * SUCH DAMAGE. 27107120Sjulian * 28107120Sjulian * $Id: l2control.c,v 1.5 2002/09/04 21:30:40 max Exp $ 29107120Sjulian * $FreeBSD: head/usr.sbin/bluetooth/l2control/l2control.c 107120 2002-11-20 23:01:59Z julian $ 30107120Sjulian */ 31107120Sjulian 32107120Sjulian#include <sys/types.h> 33107120Sjulian#include <sys/socket.h> 34107120Sjulian#include <bitstring.h> 35107120Sjulian#include <assert.h> 36107120Sjulian#include <err.h> 37107120Sjulian#include <errno.h> 38107120Sjulian#include <ng_hci.h> 39107120Sjulian#include <ng_l2cap.h> 40107120Sjulian#include <ng_btsocket.h> 41107120Sjulian#include <stdio.h> 42107120Sjulian#include <stdlib.h> 43107120Sjulian#include <string.h> 44107120Sjulian#include <unistd.h> 45107120Sjulian#include "l2control.h" 46107120Sjulian 47107120Sjulian/* Prototypes */ 48107120Sjulianstatic int do_l2cap_command (bdaddr_p, int, char **); 49107120Sjulianstatic struct l2cap_command * find_l2cap_command (char const *, 50107120Sjulian struct l2cap_command *); 51107120Sjulianstatic void print_l2cap_command (struct l2cap_command *); 52107120Sjulianstatic void usage (void); 53107120Sjulian 54107120Sjulian/* Main */ 55107120Sjulianint 56107120Sjulianmain(int argc, char *argv[]) 57107120Sjulian{ 58107120Sjulian int n; 59107120Sjulian bdaddr_t bdaddr; 60107120Sjulian 61107120Sjulian memset(&bdaddr, 0, sizeof(bdaddr)); 62107120Sjulian 63107120Sjulian /* Process command line arguments */ 64107120Sjulian while ((n = getopt(argc, argv, "a:")) != -1) { 65107120Sjulian switch (n) { 66107120Sjulian case 'a': { 67107120Sjulian int a0, a1, a2, a3, a4, a5; 68107120Sjulian 69107120Sjulian if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", 70107120Sjulian &a5, &a4, &a3, &a2, &a1, &a0) != 6) { 71107120Sjulian usage(); 72107120Sjulian break; 73107120Sjulian } 74107120Sjulian 75107120Sjulian bdaddr.b[0] = (a0 & 0xff); 76107120Sjulian bdaddr.b[1] = (a1 & 0xff); 77107120Sjulian bdaddr.b[2] = (a2 & 0xff); 78107120Sjulian bdaddr.b[3] = (a3 & 0xff); 79107120Sjulian bdaddr.b[4] = (a4 & 0xff); 80107120Sjulian bdaddr.b[5] = (a5 & 0xff); 81107120Sjulian } break; 82107120Sjulian 83107120Sjulian default: 84107120Sjulian usage(); 85107120Sjulian break; 86107120Sjulian } 87107120Sjulian } 88107120Sjulian 89107120Sjulian argc -= optind; 90107120Sjulian argv += optind; 91107120Sjulian 92107120Sjulian if (*argv == NULL) 93107120Sjulian usage(); 94107120Sjulian 95107120Sjulian return (do_l2cap_command(&bdaddr, argc, argv)); 96107120Sjulian} /* main */ 97107120Sjulian 98107120Sjulian/* Execute commands */ 99107120Sjulianstatic int 100107120Sjuliando_l2cap_command(bdaddr_p bdaddr, int argc, char **argv) 101107120Sjulian{ 102107120Sjulian char *cmd = argv[0]; 103107120Sjulian struct l2cap_command *c = NULL; 104107120Sjulian struct sockaddr_l2cap sa; 105107120Sjulian int s, e, help; 106107120Sjulian 107107120Sjulian help = 0; 108107120Sjulian if (strcasecmp(cmd, "help") == 0) { 109107120Sjulian argc --; 110107120Sjulian argv ++; 111107120Sjulian 112107120Sjulian if (argc <= 0) { 113107120Sjulian fprintf(stdout, "Supported commands:\n"); 114107120Sjulian print_l2cap_command(l2cap_commands); 115107120Sjulian fprintf(stdout, "\nFor more information use " \ 116107120Sjulian "'help command'\n"); 117107120Sjulian 118107120Sjulian return (OK); 119107120Sjulian } 120107120Sjulian 121107120Sjulian help = 1; 122107120Sjulian cmd = argv[0]; 123107120Sjulian } 124107120Sjulian 125107120Sjulian c = find_l2cap_command(cmd, l2cap_commands); 126107120Sjulian if (c == NULL) { 127107120Sjulian fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 128107120Sjulian return (ERROR); 129107120Sjulian } 130107120Sjulian 131107120Sjulian if (!help) { 132107120Sjulian if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0) 133107120Sjulian usage(); 134107120Sjulian 135107120Sjulian memset(&sa, 0, sizeof(sa)); 136107120Sjulian sa.l2cap_len = sizeof(sa); 137107120Sjulian sa.l2cap_family = AF_BLUETOOTH; 138107120Sjulian memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr)); 139107120Sjulian 140107120Sjulian s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); 141107120Sjulian if (s < 0) 142107120Sjulian err(1, "Could not create socket"); 143107120Sjulian 144107120Sjulian if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 145107120Sjulian err(2, 146107120Sjulian"Could not bind socket, bdaddr=%x:%x:%x:%x:%x:%x", 147107120Sjulian sa.l2cap_bdaddr.b[5], sa.l2cap_bdaddr.b[4], 148107120Sjulian sa.l2cap_bdaddr.b[3], sa.l2cap_bdaddr.b[2], 149107120Sjulian sa.l2cap_bdaddr.b[1], sa.l2cap_bdaddr.b[0]); 150107120Sjulian 151107120Sjulian if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 152107120Sjulian err(2, 153107120Sjulian"Could not connect socket, bdaddr=%x:%x:%x:%x:%x:%x", 154107120Sjulian sa.l2cap_bdaddr.b[5], sa.l2cap_bdaddr.b[4], 155107120Sjulian sa.l2cap_bdaddr.b[3], sa.l2cap_bdaddr.b[2], 156107120Sjulian sa.l2cap_bdaddr.b[1], sa.l2cap_bdaddr.b[0]); 157107120Sjulian 158107120Sjulian e = 0x0ffff; 159107120Sjulian if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0) 160107120Sjulian err(3, "Coult not setsockopt(RCVBUF, %d)", e); 161107120Sjulian 162107120Sjulian e = (c->handler)(s, -- argc, ++ argv); 163107120Sjulian 164107120Sjulian close(s); 165107120Sjulian } else 166107120Sjulian e = USAGE; 167107120Sjulian 168107120Sjulian switch (e) { 169107120Sjulian case OK: 170107120Sjulian case FAILED: 171107120Sjulian break; 172107120Sjulian 173107120Sjulian case ERROR: 174107120Sjulian fprintf(stdout, "Could not execute command \"%s\". %s\n", 175107120Sjulian cmd, strerror(errno)); 176107120Sjulian break; 177107120Sjulian 178107120Sjulian case USAGE: 179107120Sjulian fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 180107120Sjulian break; 181107120Sjulian 182107120Sjulian default: assert(0); break; 183107120Sjulian } 184107120Sjulian 185107120Sjulian return (e); 186107120Sjulian} /* do_l2cap_command */ 187107120Sjulian 188107120Sjulian/* Try to find command in specified category */ 189107120Sjulianstatic struct l2cap_command * 190107120Sjulianfind_l2cap_command(char const *command, struct l2cap_command *category) 191107120Sjulian{ 192107120Sjulian struct l2cap_command *c = NULL; 193107120Sjulian 194107120Sjulian for (c = category; c->command != NULL; c++) { 195107120Sjulian char *c_end = strchr(c->command, ' '); 196107120Sjulian 197107120Sjulian if (c_end != NULL) { 198107120Sjulian int len = c_end - c->command; 199107120Sjulian 200107120Sjulian if (strncasecmp(command, c->command, len) == 0) 201107120Sjulian return (c); 202107120Sjulian } else if (strcasecmp(command, c->command) == 0) 203107120Sjulian return (c); 204107120Sjulian } 205107120Sjulian 206107120Sjulian return (NULL); 207107120Sjulian} /* find_l2cap_command */ 208107120Sjulian 209107120Sjulian/* Try to find command in specified category */ 210107120Sjulianstatic void 211107120Sjulianprint_l2cap_command(struct l2cap_command *category) 212107120Sjulian{ 213107120Sjulian struct l2cap_command *c = NULL; 214107120Sjulian 215107120Sjulian for (c = category; c->command != NULL; c++) 216107120Sjulian fprintf(stdout, "\t%s\n", c->command); 217107120Sjulian} /* print_l2cap_command */ 218107120Sjulian 219107120Sjulian/* Usage */ 220107120Sjulianstatic void 221107120Sjulianusage(void) 222107120Sjulian{ 223107120Sjulian fprintf(stdout, "Usage: l2control -a BD_ADDR cmd [p1] [..]]\n"); 224107120Sjulian exit(255); 225107120Sjulian} /* usage */ 226107120Sjulian 227