1*86d7f5d3SJohn Marino /* $NetBSD: devaddr.c,v 1.2 2006/08/28 08:24:39 plunky Exp $ */
2*86d7f5d3SJohn Marino /* $DragonFly: src/lib/libbluetooth/devaddr.c,v 1.1 2008/01/03 11:47:52 hasso Exp $ */
3*86d7f5d3SJohn Marino
4*86d7f5d3SJohn Marino /*-
5*86d7f5d3SJohn Marino * Copyright (c) 2006 Itronix Inc.
6*86d7f5d3SJohn Marino * All rights reserved.
7*86d7f5d3SJohn Marino *
8*86d7f5d3SJohn Marino * Written by Iain Hibbert for Itronix Inc.
9*86d7f5d3SJohn Marino *
10*86d7f5d3SJohn Marino * Redistribution and use in source and binary forms, with or without
11*86d7f5d3SJohn Marino * modification, are permitted provided that the following conditions
12*86d7f5d3SJohn Marino * are met:
13*86d7f5d3SJohn Marino * 1. Redistributions of source code must retain the above copyright
14*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer.
15*86d7f5d3SJohn Marino * 2. Redistributions in binary form must reproduce the above copyright
16*86d7f5d3SJohn Marino * notice, this list of conditions and the following disclaimer in the
17*86d7f5d3SJohn Marino * documentation and/or other materials provided with the distribution.
18*86d7f5d3SJohn Marino * 3. The name of Itronix Inc. may not be used to endorse
19*86d7f5d3SJohn Marino * or promote products derived from this software without specific
20*86d7f5d3SJohn Marino * prior written permission.
21*86d7f5d3SJohn Marino *
22*86d7f5d3SJohn Marino * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
23*86d7f5d3SJohn Marino * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24*86d7f5d3SJohn Marino * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25*86d7f5d3SJohn Marino * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
26*86d7f5d3SJohn Marino * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27*86d7f5d3SJohn Marino * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28*86d7f5d3SJohn Marino * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*86d7f5d3SJohn Marino * ON ANY THEORY OF LIABILITY, WHETHER IN
30*86d7f5d3SJohn Marino * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31*86d7f5d3SJohn Marino * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32*86d7f5d3SJohn Marino * POSSIBILITY OF SUCH DAMAGE.
33*86d7f5d3SJohn Marino */
34*86d7f5d3SJohn Marino
35*86d7f5d3SJohn Marino #include <sys/ioctl.h>
36*86d7f5d3SJohn Marino #include <bluetooth.h>
37*86d7f5d3SJohn Marino #include <errno.h>
38*86d7f5d3SJohn Marino #include <stdio.h>
39*86d7f5d3SJohn Marino #include <stdlib.h>
40*86d7f5d3SJohn Marino #include <string.h>
41*86d7f5d3SJohn Marino #include <unistd.h>
42*86d7f5d3SJohn Marino
43*86d7f5d3SJohn Marino int
bt_devaddr(const char * name,bdaddr_t * addr)44*86d7f5d3SJohn Marino bt_devaddr(const char *name, bdaddr_t *addr)
45*86d7f5d3SJohn Marino {
46*86d7f5d3SJohn Marino struct btreq btr;
47*86d7f5d3SJohn Marino bdaddr_t bdaddr;
48*86d7f5d3SJohn Marino int s, rv;
49*86d7f5d3SJohn Marino
50*86d7f5d3SJohn Marino if (name == NULL) {
51*86d7f5d3SJohn Marino errno = EINVAL;
52*86d7f5d3SJohn Marino return 0;
53*86d7f5d3SJohn Marino }
54*86d7f5d3SJohn Marino
55*86d7f5d3SJohn Marino if (addr == NULL)
56*86d7f5d3SJohn Marino addr = &bdaddr;
57*86d7f5d3SJohn Marino
58*86d7f5d3SJohn Marino if (bt_aton(name, addr))
59*86d7f5d3SJohn Marino return bt_devname(NULL, addr);
60*86d7f5d3SJohn Marino
61*86d7f5d3SJohn Marino memset(&btr, 0, sizeof(btr));
62*86d7f5d3SJohn Marino strlcpy(btr.btr_name, name, HCI_DEVNAME_SIZE);
63*86d7f5d3SJohn Marino
64*86d7f5d3SJohn Marino s = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
65*86d7f5d3SJohn Marino if (s == -1)
66*86d7f5d3SJohn Marino return 0;
67*86d7f5d3SJohn Marino
68*86d7f5d3SJohn Marino rv = ioctl(s, SIOCGBTINFO, &btr);
69*86d7f5d3SJohn Marino close(s);
70*86d7f5d3SJohn Marino
71*86d7f5d3SJohn Marino if (rv == -1)
72*86d7f5d3SJohn Marino return 0;
73*86d7f5d3SJohn Marino
74*86d7f5d3SJohn Marino if ((btr.btr_flags & BTF_UP) == 0) {
75*86d7f5d3SJohn Marino errno = ENXIO;
76*86d7f5d3SJohn Marino return 0;
77*86d7f5d3SJohn Marino }
78*86d7f5d3SJohn Marino
79*86d7f5d3SJohn Marino bdaddr_copy(addr, &btr.btr_bdaddr);
80*86d7f5d3SJohn Marino return 1;
81*86d7f5d3SJohn Marino }
82*86d7f5d3SJohn Marino
83*86d7f5d3SJohn Marino int
bt_devname(char * name,const bdaddr_t * addr)84*86d7f5d3SJohn Marino bt_devname(char *name, const bdaddr_t *addr)
85*86d7f5d3SJohn Marino {
86*86d7f5d3SJohn Marino struct btreq btr;
87*86d7f5d3SJohn Marino int s, rv;
88*86d7f5d3SJohn Marino
89*86d7f5d3SJohn Marino if (addr == NULL) {
90*86d7f5d3SJohn Marino errno = EINVAL;
91*86d7f5d3SJohn Marino return 0;
92*86d7f5d3SJohn Marino }
93*86d7f5d3SJohn Marino
94*86d7f5d3SJohn Marino memset(&btr, 0, sizeof(btr));
95*86d7f5d3SJohn Marino bdaddr_copy(&btr.btr_bdaddr, addr);
96*86d7f5d3SJohn Marino
97*86d7f5d3SJohn Marino s = socket(PF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
98*86d7f5d3SJohn Marino if (s == -1)
99*86d7f5d3SJohn Marino return 0;
100*86d7f5d3SJohn Marino
101*86d7f5d3SJohn Marino rv = ioctl(s, SIOCGBTINFOA, &btr);
102*86d7f5d3SJohn Marino close(s);
103*86d7f5d3SJohn Marino
104*86d7f5d3SJohn Marino if (rv == -1)
105*86d7f5d3SJohn Marino return 0;
106*86d7f5d3SJohn Marino
107*86d7f5d3SJohn Marino if ((btr.btr_flags & BTF_UP) == 0) {
108*86d7f5d3SJohn Marino errno = ENXIO;
109*86d7f5d3SJohn Marino return 0;
110*86d7f5d3SJohn Marino }
111*86d7f5d3SJohn Marino
112*86d7f5d3SJohn Marino if (name != NULL)
113*86d7f5d3SJohn Marino strlcpy(name, btr.btr_name, HCI_DEVNAME_SIZE);
114*86d7f5d3SJohn Marino
115*86d7f5d3SJohn Marino return 1;
116*86d7f5d3SJohn Marino }
117