l2control.c revision 114879
174011Sjhb/* 268685Sjhb * l2control.c 368685Sjhb * 468685Sjhb * Copyright (c) 2001-2002 Maksim Yevmenkin <m_evmenkin@yahoo.com> 568685Sjhb * All rights reserved. 668685Sjhb * 768685Sjhb * Redistribution and use in source and binary forms, with or without 868685Sjhb * modification, are permitted provided that the following conditions 968685Sjhb * are met: 1068685Sjhb * 1. Redistributions of source code must retain the above copyright 1168685Sjhb * notice, this list of conditions and the following disclaimer. 1268685Sjhb * 2. Redistributions in binary form must reproduce the above copyright 1368685Sjhb * notice, this list of conditions and the following disclaimer in the 1468685Sjhb * documentation and/or other materials provided with the distribution. 1568685Sjhb * 1668685Sjhb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1768685Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1868685Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1968685Sjhb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2068685Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2168685Sjhb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2268685Sjhb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2368685Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2468685Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2568685Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26284888Skib * SUCH DAMAGE. 27206622Suqs * 2868685Sjhb * $Id: l2control.c,v 1.3 2003/04/27 19:45:34 max Exp $ 2968685Sjhb * $FreeBSD: head/usr.sbin/bluetooth/l2control/l2control.c 114879 2003-05-10 21:50:37Z julian $ 3068685Sjhb */ 3168685Sjhb 3268685Sjhb#include <sys/types.h> 33150628Sjhb#include <sys/socket.h> 3468685Sjhb#include <bitstring.h> 3568685Sjhb#include <assert.h> 3668685Sjhb#include <err.h> 3768685Sjhb#include <errno.h> 3868685Sjhb#include <ng_hci.h> 3968685Sjhb#include <ng_l2cap.h> 4068685Sjhb#include <ng_btsocket.h> 4184306Sru#include <stdio.h> 4284306Sru#include <stdlib.h> 4368685Sjhb#include <string.h> 4489462Sru#include <unistd.h> 4568685Sjhb#include "l2control.h" 4689462Sru 4768685Sjhb/* Prototypes */ 4889462Srustatic int do_l2cap_command (bdaddr_p, int, char **); 4989462Srustatic struct l2cap_command * find_l2cap_command (char const *, 5089462Sru struct l2cap_command *); 5189462Srustatic void print_l2cap_command (struct l2cap_command *); 5268685Sjhbstatic void usage (void); 5389462Sru 54150628Sjhb/* Main */ 55150628Sjhbint 5689462Srumain(int argc, char *argv[]) 5789462Sru{ 5889462Sru int n; 5968685Sjhb bdaddr_t bdaddr; 6089462Sru 6168685Sjhb memset(&bdaddr, 0, sizeof(bdaddr)); 6289462Sru 6368685Sjhb /* Process command line arguments */ 6489462Sru while ((n = getopt(argc, argv, "a:h")) != -1) { 65254617Sjkim switch (n) { 66254617Sjkim case 'a': { 67254617Sjkim int a0, a1, a2, a3, a4, a5; 68254617Sjkim 6968685Sjhb if (sscanf(optarg, "%x:%x:%x:%x:%x:%x", 7068685Sjhb &a5, &a4, &a3, &a2, &a1, &a0) != 6) { 7168685Sjhb usage(); 7268685Sjhb break; 7368685Sjhb } 7468685Sjhb 7589192Sru bdaddr.b[0] = (a0 & 0xff); 76115440Shmp bdaddr.b[1] = (a1 & 0xff); 7768685Sjhb bdaddr.b[2] = (a2 & 0xff); 7868685Sjhb bdaddr.b[3] = (a3 & 0xff); 7989192Sru bdaddr.b[4] = (a4 & 0xff); 8089192Sru bdaddr.b[5] = (a5 & 0xff); 8189192Sru } break; 8268685Sjhb 8389192Sru case 'h': 8468685Sjhb default: 8589192Sru usage(); 8668685Sjhb break; 8789192Sru } 8868685Sjhb } 8989192Sru 9068685Sjhb argc -= optind; 9168685Sjhb argv += optind; 9268685Sjhb 9368685Sjhb if (*argv == NULL) 9468685Sjhb usage(); 9587999Sjake 9689192Sru return (do_l2cap_command(&bdaddr, argc, argv)); 9789192Sru} /* main */ 9889192Sru 9989192Sru/* Execute commands */ 10089192Srustatic int 10187999Sjakedo_l2cap_command(bdaddr_p bdaddr, int argc, char **argv) 10289192Sru{ 10387999Sjake char *cmd = argv[0]; 10489192Sru struct l2cap_command *c = NULL; 10587999Sjake struct sockaddr_l2cap sa; 10689192Sru int s, e, help; 10787999Sjake 10887999Sjake help = 0; 10987999Sjake if (strcasecmp(cmd, "help") == 0) { 11087999Sjake argc --; 11187999Sjake argv ++; 11268685Sjhb 11389192Sru if (argc <= 0) { 11468685Sjhb fprintf(stdout, "Supported commands:\n"); 11568685Sjhb print_l2cap_command(l2cap_commands); 11668685Sjhb fprintf(stdout, "\nFor more information use " \ 11789192Sru "'help command'\n"); 11868685Sjhb 11968685Sjhb return (OK); 12068685Sjhb } 12168685Sjhb 12268685Sjhb help = 1; 12389192Sru cmd = argv[0]; 12468685Sjhb } 12568685Sjhb 126284888Skib c = find_l2cap_command(cmd, l2cap_commands); 127284888Skib if (c == NULL) { 128284888Skib fprintf(stdout, "Unknown command: \"%s\"\n", cmd); 12968685Sjhb return (ERROR); 13068685Sjhb } 13168685Sjhb 13268685Sjhb if (!help) { 13368685Sjhb if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0) 13489192Sru usage(); 13568685Sjhb 136115440Shmp memset(&sa, 0, sizeof(sa)); 13768685Sjhb sa.l2cap_len = sizeof(sa); 13868685Sjhb sa.l2cap_family = AF_BLUETOOTH; 13968685Sjhb memcpy(&sa.l2cap_bdaddr, bdaddr, sizeof(sa.l2cap_bdaddr)); 14068685Sjhb 14168685Sjhb s = socket(PF_BLUETOOTH, SOCK_RAW, BLUETOOTH_PROTO_L2CAP); 142284888Skib if (s < 0) 143284888Skib err(1, "Could not create socket"); 144284888Skib 14568685Sjhb if (bind(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) 14668685Sjhb err(2, 14768685Sjhb"Could not bind socket, bdaddr=%x:%x:%x:%x:%x:%x", 14868685Sjhb sa.l2cap_bdaddr.b[5], sa.l2cap_bdaddr.b[4], 14968685Sjhb sa.l2cap_bdaddr.b[3], sa.l2cap_bdaddr.b[2], 15089192Sru sa.l2cap_bdaddr.b[1], sa.l2cap_bdaddr.b[0]); 15168685Sjhb 152115440Shmp e = 0x0ffff; 15368685Sjhb if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, &e, sizeof(e)) < 0) 15468685Sjhb err(3, "Coult not setsockopt(RCVBUF, %d)", e); 15568685Sjhb 15668685Sjhb e = (c->handler)(s, -- argc, ++ argv); 15768685Sjhb 15868685Sjhb close(s); 15968685Sjhb } else 16068685Sjhb e = USAGE; 16168685Sjhb 16268685Sjhb switch (e) { 16368685Sjhb case OK: 16468685Sjhb case FAILED: 16568685Sjhb break; 16668685Sjhb 16768685Sjhb case ERROR: 16879687Sschweikh fprintf(stdout, "Could not execute command \"%s\". %s\n", 16968685Sjhb cmd, strerror(errno)); 17068685Sjhb break; 17168685Sjhb 17268685Sjhb case USAGE: 17368685Sjhb fprintf(stdout, "Usage: %s\n%s\n", c->command, c->description); 17468685Sjhb break; 17568685Sjhb 17668685Sjhb default: assert(0); break; 17768685Sjhb } 17868685Sjhb 17968685Sjhb return (e); 18068685Sjhb} /* do_l2cap_command */ 18168685Sjhb 18268685Sjhb/* Try to find command in specified category */ 18389192Srustatic struct l2cap_command * 18489192Srufind_l2cap_command(char const *command, struct l2cap_command *category) 18568685Sjhb{ 18668685Sjhb struct l2cap_command *c = NULL; 18789192Sru 18889192Sru for (c = category; c->command != NULL; c++) { 18968685Sjhb char *c_end = strchr(c->command, ' '); 19068685Sjhb 19189192Sru if (c_end != NULL) { 19289192Sru int len = c_end - c->command; 19368685Sjhb 19468685Sjhb if (strncasecmp(command, c->command, len) == 0) 195254617Sjkim return (c); 19668685Sjhb } else if (strcasecmp(command, c->command) == 0) 197254617Sjkim return (c); 19868685Sjhb } 19968685Sjhb 20068685Sjhb return (NULL); 20168685Sjhb} /* find_l2cap_command */ 20268685Sjhb 20389192Sru/* Try to find command in specified category */ 20489192Srustatic void 20589192Sruprint_l2cap_command(struct l2cap_command *category) 20689192Sru{ 20789192Sru struct l2cap_command *c = NULL; 20889192Sru 20968685Sjhb for (c = category; c->command != NULL; c++) 210150628Sjhb fprintf(stdout, "\t%s\n", c->command); 211150628Sjhb} /* print_l2cap_command */ 212150628Sjhb 213150628Sjhb/* Usage */ 214254617Sjkimstatic void 215150628Sjhbusage(void) 216150628Sjhb{ 217150628Sjhb fprintf(stdout, "Usage: l2control -a BD_ADDR [-h] cmd [p1] [..]]\n"); 218150628Sjhb exit(255); 219150628Sjhb} /* usage */ 220150628Sjhb 221177276Spjd