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