/*1* dev.c2*/34/*-5* SPDX-License-Identifier: BSD-2-Clause6*7* Copyright (c) 2009 Maksim Yevmenkin <[email protected]>8* All rights reserved.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18*19* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND20* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE21* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE22* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE23* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL24* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS25* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT27* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY28* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF29* SUCH DAMAGE.30*/3132#define L2CAP_SOCKET_CHECKED33#include <bluetooth.h>34#include <stdio.h>35#include <string.h>3637struct bt_devaddr_match_arg38{39char devname[HCI_DEVNAME_SIZE];40bdaddr_t const *bdaddr;41};4243static bt_devenum_cb_t bt_devaddr_match;4445int46bt_devaddr(char const *devname, bdaddr_t *addr)47{48struct bt_devinfo di;4950strlcpy(di.devname, devname, sizeof(di.devname));5152if (bt_devinfo(&di) < 0)53return (0);5455if (addr != NULL)56bdaddr_copy(addr, &di.bdaddr);5758return (1);59}6061int62bt_devname(char *devname, bdaddr_t const *addr)63{64struct bt_devaddr_match_arg arg;6566memset(&arg, 0, sizeof(arg));67arg.bdaddr = addr;6869if (bt_devenum(&bt_devaddr_match, &arg) < 0)70return (0);7172if (arg.devname[0] == '\0') {73errno = ENXIO;74return (0);75}7677if (devname != NULL)78strlcpy(devname, arg.devname, HCI_DEVNAME_SIZE);7980return (1);81}8283static int84bt_devaddr_match(int s, struct bt_devinfo const *di, void *arg)85{86struct bt_devaddr_match_arg *m = (struct bt_devaddr_match_arg *)arg;8788if (!bdaddr_same(&di->bdaddr, m->bdaddr))89return (0);9091strlcpy(m->devname, di->devname, sizeof(m->devname));9293return (1);94}95969798