dev.c revision 189462
1139969Simp/* 21556Srgrimes * dev.c 31556Srgrimes */ 41556Srgrimes 51556Srgrimes/*- 61556Srgrimes * Copyright (c) 2009 Maksim Yevmenkin <m_evmenkin@yahoo.com> 71556Srgrimes * All rights reserved. 81556Srgrimes * 91556Srgrimes * Redistribution and use in source and binary forms, with or without 101556Srgrimes * modification, are permitted provided that the following conditions 111556Srgrimes * are met: 121556Srgrimes * 1. Redistributions of source code must retain the above copyright 131556Srgrimes * notice, this list of conditions and the following disclaimer. 141556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 151556Srgrimes * notice, this list of conditions and the following disclaimer in the 161556Srgrimes * documentation and/or other materials provided with the distribution. 171556Srgrimes * 181556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 191556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 201556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 211556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 221556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 231556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 241556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 251556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 261556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 271556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281556Srgrimes * SUCH DAMAGE. 291556Srgrimes * 301556Srgrimes * $FreeBSD: head/lib/libbluetooth/dev.c 189462 2009-03-06 23:30:07Z emax $ 311556Srgrimes */ 321556Srgrimes 33114433Sobrien#include <bluetooth.h> 341556Srgrimes#include <stdio.h> 3520412Ssteve#include <string.h> 361556Srgrimes 371556Srgrimesstruct bt_devaddr_match_arg 381556Srgrimes{ 391556Srgrimes char devname[HCI_DEVNAME_SIZE]; 401556Srgrimes bdaddr_t const *bdaddr; 4136003Scharnier}; 42114433Sobrien 4335773Scharnierstatic bt_devenum_cb_t bt_devaddr_match; 4499109Sobrien 4599109Sobrienint 461556Srgrimesbt_devaddr(char const *devname, bdaddr_t *addr) 471556Srgrimes{ 481556Srgrimes struct bt_devinfo di; 498855Srgrimes 501556Srgrimes strlcpy(di.devname, devname, sizeof(di.devname)); 511556Srgrimes 5220412Ssteve if (bt_devinfo(&di) < 0) 538855Srgrimes return (0); 541556Srgrimes 551556Srgrimes if (addr != NULL) 561556Srgrimes bdaddr_copy(addr, &di.bdaddr); 571556Srgrimes 581556Srgrimes return (1); 591556Srgrimes} 601556Srgrimes 611556Srgrimesint 62113431Sbdebt_devname(char *devname, bdaddr_t const *addr) 631556Srgrimes{ 641556Srgrimes struct bt_devaddr_match_arg arg; 651556Srgrimes 661556Srgrimes memset(&arg, 0, sizeof(arg)); 671556Srgrimes arg.bdaddr = addr; 6876693Simp 69113431Sbde if (bt_devenum(&bt_devaddr_match, &arg) < 0) 7050381Smharo return (0); 7178469Sdes 721556Srgrimes if (arg.devname[0] == '\0') { 731556Srgrimes errno = ENXIO; 741556Srgrimes return (0); 751556Srgrimes } 761556Srgrimes 771556Srgrimes if (devname != NULL) 785292Sbde strlcpy(devname, arg.devname, HCI_DEVNAME_SIZE); 791556Srgrimes 801556Srgrimes return (1); 811556Srgrimes} 8291087Smarkm 831556Srgrimesstatic int 8491087Smarkmbt_devaddr_match(int s, struct bt_devinfo const *di, void *arg) 851556Srgrimes{ 86161586Sjulian struct bt_devaddr_match_arg *m = (struct bt_devaddr_match_arg *)arg; 87100538Sjohan 88113218Smdodd if (!bdaddr_same(&di->bdaddr, m->bdaddr)) 89113209Smdodd return (0); 901556Srgrimes 911556Srgrimes strlcpy(m->devname, di->devname, sizeof(m->devname)); 9299363Smarkm 93103726Swollman return (1); 94113430Sbde} 951556Srgrimes 961556Srgrimes