l2control.c revision 114879
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 * 28114879Sjulian * $Id: l2control.c,v 1.3 2003/04/27 19:45:34 max Exp $ 29107120Sjulian * $FreeBSD: head/usr.sbin/bluetooth/l2control/l2control.c 114879 2003-05-10 21:50:37Z 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 */ 64114879Sjulian while ((n = getopt(argc, argv, "a:h")) != -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 83114879Sjulian case 'h': 84107120Sjulian default: 85107120Sjulian usage(); 86107120Sjulian break; 87107120Sjulian } 88107120Sjulian } 89107120Sjulian 90107120Sjulian argc -= optind; 91107120Sjulian argv += optind; 92107120Sjulian 93107120Sjulian if (*argv == NULL) 94107120Sjulian usage(); 95107120Sjulian 96107120Sjulian return (do_l2cap_command(&bdaddr, argc, argv)); 97107120Sjulian} /* main */ 98107120Sjulian 99107120Sjulian/* Execute commands */ 100107120Sjulianstatic int 101107120Sjuliando_l2cap_command(bdaddr_p bdaddr, int argc, char **argv) 102107120Sjulian{ 103107120Sjulian char *cmd = argv[0]; 104107120Sjulian struct l2cap_command *c = NULL; 105107120Sjulian struct sockaddr_l2cap sa; 106107120Sjulian int s, e, help; 107107120Sjulian 108107120Sjulian help = 0; 109107120Sjulian if (strcasecmp(cmd, "help") == 0) { 110107120Sjulian argc --; 111107120Sjulian argv ++; 112107120Sjulian 113107120Sjulian if (argc <= 0) { 114107120Sjulian fprintf(stdout, "Supported commands:\n"); 115107120Sjulian print_l2cap_command(l2cap_commands); 116107120Sjulian fprintf(stdout, "\nFor more information use " \ 117107120Sjulian "'help command'\n"); 118107120Sjulian 119107120Sjulian return (OK); 120107120Sjulian } 121107120Sjulian 122107120Sjulian help = 1; 123107120Sjulian cmd = argv[0]; 124107120Sjulian } 125107120Sjulian 126107120Sjulian c = find_l2cap_command(cmd, l2cap_commands); 127107120Sjulian if (c == NULL) { 128107120Sjulian fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 129107120Sjulian return (ERROR); 130107120Sjulian } 131107120Sjulian 132107120Sjulian if (!help) { 133107120Sjulian if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0) 134107120Sjulian usage(); 135107120Sjulian 136107120Sjulian memset(&sa, 0, sizeof(sa)); 137107120Sjulian sa.l2cap_len = sizeof(sa); 138107120Sjulian sa.l2cap_family = AF_BLUETOOTH; 139107120Sjulian memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr)); 140107120Sjulian 141107120Sjulian s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); 142107120Sjulian if (s < 0) 143107120Sjulian err(1, "Could not create socket"); 144107120Sjulian 145107120Sjulian if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 146107120Sjulian err(2, 147107120Sjulian"Could not bind socket, bdaddr=%x:%x:%x:%x:%x:%x", 148107120Sjulian sa.l2cap_bdaddr.b[5], sa.l2cap_bdaddr.b[4], 149107120Sjulian sa.l2cap_bdaddr.b[3], sa.l2cap_bdaddr.b[2], 150107120Sjulian sa.l2cap_bdaddr.b[1], sa.l2cap_bdaddr.b[0]); 151107120Sjulian 152107120Sjulian e = 0x0ffff; 153107120Sjulian if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0) 154107120Sjulian err(3, "Coult not setsockopt(RCVBUF, %d)", e); 155107120Sjulian 156107120Sjulian e = (c->handler)(s, -- argc, ++ argv); 157107120Sjulian 158107120Sjulian close(s); 159107120Sjulian } else 160107120Sjulian e = USAGE; 161107120Sjulian 162107120Sjulian switch (e) { 163107120Sjulian case OK: 164107120Sjulian case FAILED: 165107120Sjulian break; 166107120Sjulian 167107120Sjulian case ERROR: 168107120Sjulian fprintf(stdout, "Could not execute command \"%s\". %s\n", 169107120Sjulian cmd, strerror(errno)); 170107120Sjulian break; 171107120Sjulian 172107120Sjulian case USAGE: 173107120Sjulian fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 174107120Sjulian break; 175107120Sjulian 176107120Sjulian default: assert(0); break; 177107120Sjulian } 178107120Sjulian 179107120Sjulian return (e); 180107120Sjulian} /* do_l2cap_command */ 181107120Sjulian 182107120Sjulian/* Try to find command in specified category */ 183107120Sjulianstatic struct l2cap_command * 184107120Sjulianfind_l2cap_command(char const *command, struct l2cap_command *category) 185107120Sjulian{ 186107120Sjulian struct l2cap_command *c = NULL; 187107120Sjulian 188107120Sjulian for (c = category; c->command != NULL; c++) { 189107120Sjulian char *c_end = strchr(c->command, ' '); 190107120Sjulian 191107120Sjulian if (c_end != NULL) { 192107120Sjulian int len = c_end - c->command; 193107120Sjulian 194107120Sjulian if (strncasecmp(command, c->command, len) == 0) 195107120Sjulian return (c); 196107120Sjulian } else if (strcasecmp(command, c->command) == 0) 197107120Sjulian return (c); 198107120Sjulian } 199107120Sjulian 200107120Sjulian return (NULL); 201107120Sjulian} /* find_l2cap_command */ 202107120Sjulian 203107120Sjulian/* Try to find command in specified category */ 204107120Sjulianstatic void 205107120Sjulianprint_l2cap_command(struct l2cap_command *category) 206107120Sjulian{ 207107120Sjulian struct l2cap_command *c = NULL; 208107120Sjulian 209107120Sjulian for (c = category; c->command != NULL; c++) 210107120Sjulian fprintf(stdout, "\t%s\n", c->command); 211107120Sjulian} /* print_l2cap_command */ 212107120Sjulian 213107120Sjulian/* Usage */ 214107120Sjulianstatic void 215107120Sjulianusage(void) 216107120Sjulian{ 217114879Sjulian fprintf(stdout, "Usage: l2control -a BD_ADDR [-h] cmd [p1] [..]]\n"); 218107120Sjulian exit(255); 219107120Sjulian} /* usage */ 220107120Sjulian 221