1189462Semax/*
2189462Semax * dev.c
3189462Semax */
4189462Semax
5189462Semax/*-
6189462Semax * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
7189462Semax * All rights reserved.
8189462Semax *
9189462Semax * Redistribution and use in source and binary forms, with or without
10189462Semax * modification, are permitted provided that the following conditions
11189462Semax * are met:
12189462Semax * 1. Redistributions of source code must retain the above copyright
13189462Semax *    notice, this list of conditions and the following disclaimer.
14189462Semax * 2. Redistributions in binary form must reproduce the above copyright
15189462Semax *    notice, this list of conditions and the following disclaimer in the
16189462Semax *    documentation and/or other materials provided with the distribution.
17189462Semax *
18189462Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19189462Semax * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20189462Semax * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21189462Semax * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22189462Semax * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23189462Semax * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24189462Semax * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25189462Semax * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26189462Semax * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27189462Semax * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28189462Semax * SUCH DAMAGE.
29189462Semax *
30189462Semax * $FreeBSD$
31189462Semax */
32189462Semax
33189462Semax#include <bluetooth.h>
34189462Semax#include <stdio.h>
35189462Semax#include <string.h>
36189462Semax
37189462Semaxstruct bt_devaddr_match_arg
38189462Semax{
39189462Semax	char		devname[HCI_DEVNAME_SIZE];
40189462Semax	bdaddr_t const	*bdaddr;
41189462Semax};
42189462Semax
43189462Semaxstatic bt_devenum_cb_t	bt_devaddr_match;
44189462Semax
45189462Semaxint
46189462Semaxbt_devaddr(char const *devname, bdaddr_t *addr)
47189462Semax{
48189462Semax	struct bt_devinfo	di;
49189462Semax
50189462Semax	strlcpy(di.devname, devname, sizeof(di.devname));
51189462Semax
52189462Semax	if (bt_devinfo(&di) < 0)
53189462Semax		return (0);
54189462Semax
55189462Semax	if (addr != NULL)
56189462Semax		bdaddr_copy(addr, &di.bdaddr);
57189462Semax
58189462Semax	return (1);
59189462Semax}
60189462Semax
61189462Semaxint
62189462Semaxbt_devname(char *devname, bdaddr_t const *addr)
63189462Semax{
64189462Semax	struct bt_devaddr_match_arg	arg;
65189462Semax
66189462Semax	memset(&arg, 0, sizeof(arg));
67189462Semax	arg.bdaddr = addr;
68189462Semax
69189462Semax	if (bt_devenum(&bt_devaddr_match, &arg) < 0)
70189462Semax		return (0);
71189462Semax
72189462Semax	if (arg.devname[0] == '\0') {
73189462Semax		errno = ENXIO;
74189462Semax		return (0);
75189462Semax	}
76189462Semax
77189462Semax	if (devname != NULL)
78189462Semax		strlcpy(devname, arg.devname, HCI_DEVNAME_SIZE);
79189462Semax
80189462Semax	return (1);
81189462Semax}
82189462Semax
83189462Semaxstatic int
84189462Semaxbt_devaddr_match(int s, struct bt_devinfo const *di, void *arg)
85189462Semax{
86189462Semax	struct bt_devaddr_match_arg	*m = (struct bt_devaddr_match_arg *)arg;
87189462Semax
88189462Semax	if (!bdaddr_same(&di->bdaddr, m->bdaddr))
89189462Semax		return (0);
90189462Semax
91189462Semax	strlcpy(m->devname, di->devname, sizeof(m->devname));
92189462Semax
93189462Semax	return (1);
94189462Semax}
95189462Semax
96