xref: /freebsd/lib/libbluetooth/dev.c (revision ad30f8e79bd1007cc2476e491bd21b4f5e389e0a)
1 /*
2  * dev.c
3  */
4 
5 /*-
6  * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32 
33 #include <bluetooth.h>
34 #include <stdio.h>
35 #include <string.h>
36 
37 struct bt_devaddr_match_arg
38 {
39 	char		devname[HCI_DEVNAME_SIZE];
40 	bdaddr_t const	*bdaddr;
41 };
42 
43 static bt_devenum_cb_t	bt_devaddr_match;
44 
45 int
46 bt_devaddr(char const *devname, bdaddr_t *addr)
47 {
48 	struct bt_devinfo	di;
49 
50 	strlcpy(di.devname, devname, sizeof(di.devname));
51 
52 	if (bt_devinfo(&di) < 0)
53 		return (0);
54 
55 	if (addr != NULL)
56 		bdaddr_copy(addr, &di.bdaddr);
57 
58 	return (1);
59 }
60 
61 int
62 bt_devname(char *devname, bdaddr_t const *addr)
63 {
64 	struct bt_devaddr_match_arg	arg;
65 
66 	memset(&arg, 0, sizeof(arg));
67 	arg.bdaddr = addr;
68 
69 	if (bt_devenum(&bt_devaddr_match, &arg) < 0)
70 		return (0);
71 
72 	if (arg.devname[0] == '\0') {
73 		errno = ENXIO;
74 		return (0);
75 	}
76 
77 	if (devname != NULL)
78 		strlcpy(devname, arg.devname, HCI_DEVNAME_SIZE);
79 
80 	return (1);
81 }
82 
83 static int
84 bt_devaddr_match(int s, struct bt_devinfo const *di, void *arg)
85 {
86 	struct bt_devaddr_match_arg	*m = (struct bt_devaddr_match_arg *)arg;
87 
88 	if (!bdaddr_same(&di->bdaddr, m->bdaddr))
89 		return (0);
90 
91 	strlcpy(m->devname, di->devname, sizeof(m->devname));
92 
93 	return (1);
94 }
95 
96