1*aae8472cSplunky.\" $NetBSD: bt_dev.3,v 1.4 2016/01/22 08:51:40 plunky Exp $ 2bd42c2e8Splunky.\" 3bd42c2e8Splunky.\" Copyright (c) 2009 The NetBSD Foundation, Inc. 4bd42c2e8Splunky.\" All rights reserved. 5bd42c2e8Splunky.\" 6bd42c2e8Splunky.\" Redistribution and use in source and binary forms, with or without 7bd42c2e8Splunky.\" modification, are permitted provided that the following conditions 8bd42c2e8Splunky.\" are met: 9bd42c2e8Splunky.\" 1. Redistributions of source code must retain the above copyright 10bd42c2e8Splunky.\" notice, this list of conditions and the following disclaimer. 11bd42c2e8Splunky.\" 2. Redistributions in binary form must reproduce the above copyright 12bd42c2e8Splunky.\" notice, this list of conditions and the following disclaimer in the 13bd42c2e8Splunky.\" documentation and/or other materials provided with the distribution. 14bd42c2e8Splunky.\" 15bd42c2e8Splunky.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16bd42c2e8Splunky.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17bd42c2e8Splunky.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18bd42c2e8Splunky.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19bd42c2e8Splunky.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20bd42c2e8Splunky.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21bd42c2e8Splunky.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22bd42c2e8Splunky.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23bd42c2e8Splunky.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24bd42c2e8Splunky.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25bd42c2e8Splunky.\" SUCH DAMAGE. 26bd42c2e8Splunky.\" 2720dc194fSplunky.Dd October 25, 2011 28bd42c2e8Splunky.Dt BT_DEV 3 29bd42c2e8Splunky.Os 30bd42c2e8Splunky.Sh NAME 31bd42c2e8Splunky.Nm bt_devaddr , 32bd42c2e8Splunky.Nm bt_devname , 33bd42c2e8Splunky.Nm bt_devenum , 34bd42c2e8Splunky.Nm bt_devinfo , 35bd42c2e8Splunky.Nm bt_devopen , 36bd42c2e8Splunky.Nm bt_devsend , 37bd42c2e8Splunky.Nm bt_devrecv , 38bd42c2e8Splunky.Nm bt_devreq , 39bd42c2e8Splunky.Nm bt_devfilter , 40bd42c2e8Splunky.Nm bt_devfilter_pkt_set , 41bd42c2e8Splunky.Nm bt_devfilter_pkt_clr , 42bd42c2e8Splunky.Nm bt_devfilter_pkt_tst , 43bd42c2e8Splunky.Nm bt_devfilter_evt_set , 44bd42c2e8Splunky.Nm bt_devfilter_evt_clr , 45bd42c2e8Splunky.Nm bt_devfilter_evt_tst , 4620dc194fSplunky.Nm bt_devinquiry 47bd42c2e8Splunky.Nd Bluetooth device access routines 48bd42c2e8Splunky.Sh LIBRARY 49bd42c2e8Splunky.Lb libbluetooth 50bd42c2e8Splunky.Sh SYNOPSIS 51bd42c2e8Splunky.In bluetooth.h 52bd42c2e8Splunky.Ft int 53bd42c2e8Splunky.Fn bt_devaddr "const char *name" "bdaddr_t *bdaddr" 54bd42c2e8Splunky.Ft int 55bd42c2e8Splunky.Fn bt_devname "char *name" "const bdaddr_t *bdaddr" 56bd42c2e8Splunky.Ft int 57bd42c2e8Splunky.Fn bt_devenum "int (*cb)(int, const struct bt_devinfo *, void *)" "void *arg" 58bd42c2e8Splunky.Ft int 59bd42c2e8Splunky.Fn bt_devinfo "const char *name" "struct bt_devinfo *info" 60bd42c2e8Splunky.Ft int 61bd42c2e8Splunky.Fn bt_devopen "const char *name" "int flags" 62bd42c2e8Splunky.Ft ssize_t 63bd42c2e8Splunky.Fn bt_devsend "int s" "uint16_t opcode" "void *param" "size_t plen" 64bd42c2e8Splunky.Ft ssize_t 65bd42c2e8Splunky.Fn bt_devrecv "int s" "void *buf" "size_t size" "time_t timeout" 66bd42c2e8Splunky.Ft int 67bd42c2e8Splunky.Fn bt_devreq "int s" "struct bt_devreq *req" "time_t timeout" 68bd42c2e8Splunky.Ft int 69bd42c2e8Splunky.Fn bt_devfilter "int s" "const struct bt_devfilter *new" "struct bt_devfilter *old" 70bd42c2e8Splunky.Ft void 71bd42c2e8Splunky.Fn bt_devfilter_pkt_set "struct bt_devfilter *filter" "uint8_t type" 72bd42c2e8Splunky.Ft void 73bd42c2e8Splunky.Fn bt_devfilter_pkt_clr "struct bt_devfilter *filter" "uint8_t type" 74bd42c2e8Splunky.Ft int 75bd42c2e8Splunky.Fn bt_devfilter_pkt_tst "const struct bt_devfilter *filter" "uint8_t type" 76bd42c2e8Splunky.Ft void 77bd42c2e8Splunky.Fn bt_devfilter_evt_set "struct bt_devfilter *filter" "uint8_t event" 78bd42c2e8Splunky.Ft void 79bd42c2e8Splunky.Fn bt_devfilter_evt_clr "struct bt_devfilter *filter" "uint8_t event" 80bd42c2e8Splunky.Ft int 81bd42c2e8Splunky.Fn bt_devfilter_evt_tst "const struct bt_devfilter *filter" "uint8_t event" 82bd42c2e8Splunky.Ft int 83bd42c2e8Splunky.Fn bt_devinquiry "const char *name" "time_t timeout" "int max_rsp" "struct bt_devinquiry **iip" 84bd42c2e8Splunky.Sh DESCRIPTION 85bd42c2e8SplunkyThese routines are designed to provide access to locally configured Bluetooth 86bd42c2e8Splunkydevices in an operating system independent manner via a socket providing access 87bd42c2e8Splunkyto Bluetooth HCI packets. 88bd42c2e8Splunky.Sh FUNCTIONS 89bd42c2e8Splunky.Bl -tag -width 4n 90bd42c2e8Splunky.It Fn bt_devaddr "name" "bdaddr" 91bd42c2e8SplunkyReturn a Bluetooth device address. 92bd42c2e8Splunky.Fn bt_devaddr 93bd42c2e8Splunkywill return 1 if the NUL-terminated 94bd42c2e8Splunky.Fa name 95bd42c2e8Splunkystring refers to a Bluetooth device present in the system, otherwise 0. 96bd42c2e8SplunkyThe name may be given as a device name 97bd42c2e8Splunky.Pq eg Qo ubt0 Qc 98bd42c2e8Splunkyor Bluetooth device address 99bd42c2e8Splunky.Pq eg Qo 00:11:22:33:44:55 Qc 100bd42c2e8Splunkyand the actual device address will be written to 101bd42c2e8Splunky.Fa bdaddr 102bd42c2e8Splunkyif not 103bd42c2e8Splunky.Dv NULL . 104bd42c2e8Splunky.It Fn bt_devname "name" "bdaddr" 105bd42c2e8SplunkyReturn a Bluetooth device name. 106bd42c2e8Splunky.Fn bt_devname 107bd42c2e8Splunkyreturns 1 if the 108bd42c2e8Splunky.Fa bdaddr 109bd42c2e8Splunkyrefers to a Bluetooth device present in the system, otherwise 0. 110bd42c2e8SplunkyThe 111bd42c2e8Splunky.Fa name 112bd42c2e8Splunkybuffer, if given, should have space for at least 113bd42c2e8Splunky.Dv HCI_DEVNAME_SIZE 114bd42c2e8Splunkybytes and the string will be NUL-terminated. 115bd42c2e8Splunky.It Fn bt_devenum "cb" "arg" 116bd42c2e8SplunkyEnumerate Bluetooth devices present in the system. 117bd42c2e8SplunkyFor each device found, the 118bd42c2e8Splunky.Fa cb 119bd42c2e8Splunkyfunction 120bd42c2e8Splunky.Pq if not Dv NULL 121bd42c2e8Splunkywill be called with the 122bd42c2e8Splunky.Fa arg 123bd42c2e8Splunkyargument provided, a fully populated 124bd42c2e8Splunky.Ft bt_devinfo 125bd42c2e8Splunkystructure and, where the device is enabled, a socket handle as returned by 126bd42c2e8Splunky.Fn bt_devopen . 127bd42c2e8SplunkyThe callback function can halt the enumeration by returning a 128bd42c2e8Splunkynon-zero value, and 129bd42c2e8Splunky.Fn bt_devenum 130bd42c2e8Splunkyreturns the number of successfully enumerated devices. 131bd42c2e8Splunky.It Fn bt_devinfo "name" "info" 132bd42c2e8SplunkyObtain information from a Bluetooth device present in the system. 133bd42c2e8SplunkyThe 134bd42c2e8Splunky.Fa info 135bd42c2e8Splunkyargument is a pointer to a 136bd42c2e8Splunky.Ft bt_devinfo 137bd42c2e8Splunkystructure into which information about device 138bd42c2e8Splunky.Fa name 139bd42c2e8Splunkyis placed. 140bd42c2e8SplunkyThe 141bd42c2e8Splunky.Ft bt_devinfo 142bd42c2e8Splunkystructure contains at least the following members: 143bd42c2e8Splunky.Bd -literal 144bd42c2e8Splunky char devname[HCI_DEVNAME_SIZE]; 145bd42c2e8Splunky int enabled; /* device is enabled */ 146bd42c2e8Splunky 147bd42c2e8Splunky /* device information */ 148bd42c2e8Splunky bdaddr_t bdaddr; 149bd42c2e8Splunky uint8_t features[HCI_FEATURES_SIZE]; 150bd42c2e8Splunky uint16_t acl_size; /* max ACL data size */ 151bd42c2e8Splunky uint16_t acl_pkts; /* total ACL packet buffers */ 152bd42c2e8Splunky uint16_t sco_size; /* max SCO data size */ 153bd42c2e8Splunky uint16_t sco_pkts; /* total SCO packet buffers */ 154bd42c2e8Splunky 155bd42c2e8Splunky /* flow control */ 156bd42c2e8Splunky uint16_t cmd_free; /* available CMD packet buffers */ 157bd42c2e8Splunky uint16_t acl_free; /* available ACL packet buffers */ 158*aae8472cSplunky uint16_t sco_free; /* available SCO packet buffers */ 159bd42c2e8Splunky 160bd42c2e8Splunky /* statistics */ 161bd42c2e8Splunky uint32_t cmd_sent; 162bd42c2e8Splunky uint32_t evnt_recv; 163bd42c2e8Splunky uint32_t acl_recv; 164bd42c2e8Splunky uint32_t acl_sent; 165bd42c2e8Splunky uint32_t sco_recv; 166bd42c2e8Splunky uint32_t sco_sent; 167bd42c2e8Splunky uint32_t bytes_recv; 168bd42c2e8Splunky uint32_t bytes_sent; 169bd42c2e8Splunky 170bd42c2e8Splunky /* device settings */ 171bd42c2e8Splunky uint16_t link_policy_info; 172bd42c2e8Splunky uint16_t packet_type_info; 173bd42c2e8Splunky uint16_t role_switch_info; 174bd42c2e8Splunky.Ed 175bd42c2e8Splunky.Lp 176bd42c2e8SplunkyBecause a Bluetooth device must be enabled in order to retrieve 177bd42c2e8Splunkyinformation, the 178bd42c2e8Splunky.Fa enabled 179bd42c2e8Splunkyflag should be tested to be non-zero before relying on further data. 180bd42c2e8Splunky.It Fn bt_devopen "name" "flags" 181bd42c2e8SplunkyReturn a Bluetooth HCI socket handle bound and connected to the 182bd42c2e8Splunkynamed Bluetooth device or, if 183bd42c2e8Splunky.Fa name 184bd42c2e8Splunkyis 185bd42c2e8Splunky.Dv NULL , 186bd42c2e8Splunkyenabled to receive packets from any device. 187bd42c2e8SplunkyThe socket should be closed using 188bd42c2e8Splunky.Xr close 2 189bd42c2e8Splunkyafter use. 190bd42c2e8SplunkyAny combination of the following 191bd42c2e8Splunky.Fa flags 192bd42c2e8Splunkymay be used to pre-set the socket options: 193bd42c2e8Splunky.Bl -tag -width ".Dv BTOPT_DIRECTION" 194bd42c2e8Splunky.It Dv BTOPT_DIRECTION 195bd42c2e8SplunkyEnable control messages on each packet indicating the direction of travel. 196bd42c2e8Splunky.It Dv BTOPT_TIMESTAMP 197bd42c2e8SplunkyEnable control messages providing packet timestamps. 198bd42c2e8Splunky.El 199bd42c2e8Splunky.Lp 200bd42c2e8SplunkyThe default filter on the socket will only allow the HCI Event packets 201bd42c2e8Splunky.Qq Command Status 202bd42c2e8Splunkyand 203bd42c2e8Splunky.Qq Command Complete 204bd42c2e8Splunkyto be received. 205bd42c2e8Splunky.It Fn bt_devsend "s" "opcode" "param" "plen" 206bd42c2e8SplunkySend an HCI command packet on the socket 207bd42c2e8Splunky.Fa s . 208bd42c2e8SplunkyThe 209bd42c2e8Splunky.Fa opcode 210bd42c2e8Splunkyshould be in host byte order and the 211bd42c2e8Splunky.Fa param 212bd42c2e8Splunkyand 213bd42c2e8Splunky.Fa plen 214bd42c2e8Splunkyarguments can be used to provide command parameter data. 215bd42c2e8Splunky.Fn bt_devsend 216bd42c2e8Splunkywill return the number of bytes successfully written. 217bd42c2e8Splunky.It Fn bt_devrecv "s" "buf" "size" "timeout" 218bd42c2e8SplunkyReceive a single HCI packet on the socket 219bd42c2e8Splunky.Fa s . 220bd42c2e8Splunky.Fn bt_devrecv 221bd42c2e8Splunkywill return the number of bytes successfully received unless the 222bd42c2e8Splunkyprovided buffer could not contain the entire packet, or if a timeout was 223bd42c2e8Splunkyrequested with a non-negative 224bd42c2e8Splunky.Fa timeout 225bd42c2e8Splunkyvalue. 226bd42c2e8Splunky.It Fn bt_devreq "s" "req" "timeout" 227bd42c2e8SplunkyMake an HCI request on the socket 228bd42c2e8Splunky.Fa s . 229bd42c2e8SplunkyThe 230bd42c2e8Splunky.Fa req 231bd42c2e8Splunkyargument is a pointer to a 232bd42c2e8Splunky.Ft bt_devreq 233bd42c2e8Splunkystructure, defined as: 234bd42c2e8Splunky.Bd -literal -offset indent 235bd42c2e8Splunkystruct bt_devreq { 236bd42c2e8Splunky uint16_t opcode; 237bd42c2e8Splunky uint8_t event; 238bd42c2e8Splunky void *cparam; 239bd42c2e8Splunky size_t clen; 240bd42c2e8Splunky void *rparam; 241bd42c2e8Splunky size_t rlen; 242bd42c2e8Splunky}; 243bd42c2e8Splunky.Ed 244bd42c2e8Splunky.Lp 245bd42c2e8Splunky.Fn bt_devreq 246bd42c2e8Splunkysends an HCI command packet with the given 247bd42c2e8Splunky.Fa opcode 248bd42c2e8Splunkyand command parameters of 249bd42c2e8Splunky.Fa clen 250bd42c2e8Splunkybytes at 251bd42c2e8Splunky.Fa cparam 252bd42c2e8Splunkythen waits up to 253bd42c2e8Splunky.Fa timeout 254bd42c2e8Splunkyseconds for the command to return a 255bd42c2e8Splunky.Qq Command Complete 256bd42c2e8Splunkyevent. 257bd42c2e8SplunkyIn the case where the command returns 258bd42c2e8Splunky.Qq Command Status 259bd42c2e8Splunkyand an additional event, and where the status indicates 260bd42c2e8Splunkythat the command is in progress, 261bd42c2e8Splunky.Fn bt_devreq 262bd42c2e8Splunkywill wait for the additional 263bd42c2e8Splunky.Fa event 264bd42c2e8Splunkyspecified in the request. 265bd42c2e8SplunkyIf required, any response will be copied into the buffer of 266bd42c2e8Splunky.Fa rlen 267bd42c2e8Splunkybytes at 268bd42c2e8Splunky.Fa rparam , 269bd42c2e8Splunkyand 270bd42c2e8Splunky.Fa rlen 271bd42c2e8Splunkywill be adjusted to indicate the number of bytes stored. 272bd42c2e8Splunky.Fn bt_devreq 273bd42c2e8Splunkytemporarily modifies the socket filter. 274bd42c2e8Splunky.It Fn bt_devfilter "s" "new" "old" 275bd42c2e8SplunkyUpdate or extract the packet filter on HCI socket 276bd42c2e8Splunky.Fa s . 277bd42c2e8SplunkyFilters can be set to indicate packet types 278bd42c2e8Splunky.Pq Commands, Events, ACL and SCO data , 279bd42c2e8Splunkyand individual event IDs. 280bd42c2e8SplunkyWhere 281bd42c2e8Splunky.Fa old 282bd42c2e8Splunkyis given, the currently set filter will be extracted first, then if 283bd42c2e8Splunky.Fa new 284bd42c2e8Splunkyis given, the filter will be updated. 285bd42c2e8Splunky.It Fn bt_devfilter_pkt_set "filter" "type" 286bd42c2e8SplunkySet packet 287bd42c2e8Splunky.Fa type 288bd42c2e8Splunkyin 289bd42c2e8Splunky.Fa filter . 290bd42c2e8Splunky.It Fn bt_devfilter_pkt_clr "filter" "type" 291bd42c2e8SplunkyClear packet 292bd42c2e8Splunky.Fa type 293bd42c2e8Splunkyfrom 294bd42c2e8Splunky.Fa filter . 295bd42c2e8Splunky.It Fn bt_devfilter_pkt_tst "filter" "type" 296bd42c2e8SplunkyTest if 297bd42c2e8Splunky.Fa filter 298bd42c2e8Splunkyhas packet 299bd42c2e8Splunky.Fa type 300bd42c2e8Splunkyset. 301bd42c2e8Splunky.It Fn bt_devfilter_evt_set "filter" "event" 302bd42c2e8SplunkySet 303bd42c2e8Splunky.Fa event 304bd42c2e8SplunkyID in 305bd42c2e8Splunky.Fa filter . 306bd42c2e8Splunky.It Fn bt_devfilter_evt_clr "filter" "event" 307bd42c2e8SplunkyClear 308bd42c2e8Splunky.Fa event 309bd42c2e8SplunkyID from 310bd42c2e8Splunky.Fa filter . 311bd42c2e8Splunky.It Fn bt_devfilter_evt_tst "filter" "event" 312bd42c2e8SplunkyTest if 313bd42c2e8Splunky.Fa filter 314bd42c2e8Splunkyhas 315bd42c2e8Splunky.Fa event 316bd42c2e8SplunkyID set. 317bd42c2e8Splunky.It Fn bt_devinquiry "name" "timeout" "max_rsp" "iip" 318bd42c2e8SplunkyPerform a Bluetooth Inquiry using the device 319bd42c2e8Splunky.Fa name , 320835184f2Swizor the first available device if 321835184f2Swiz.Dv NULL 322835184f2Swizis passed. 323bd42c2e8SplunkyThe inquiry length will be 324bd42c2e8Splunky.Fa timeout 325bd42c2e8Splunkyseconds, and the number of responses 326bd42c2e8Splunky.Pq up to a limit of Fa max_rsp 327bd42c2e8Splunkywill be returned. 328bd42c2e8SplunkyA pointer to an array of 329bd42c2e8Splunky.Ft bt_devinquiry 330bd42c2e8Splunkystructures, defined as: 331bd42c2e8Splunky.Bd -literal -offset indent 332bd42c2e8Splunkystruct bt_devinquiry { 333bd42c2e8Splunky bdaddr_t bdaddr; 334bd42c2e8Splunky uint8_t pscan_rep_mode; 335bd42c2e8Splunky uint8_t pscan_period_mode; 336bd42c2e8Splunky uint8_t dev_class[3]; 337bd42c2e8Splunky uint16_t clock_offset; 338bd42c2e8Splunky int8_t rssi; 339bd42c2e8Splunky uint8_t data[240]; 340bd42c2e8Splunky}; 341bd42c2e8Splunky.Ed 342bd42c2e8Splunky.Lp 343bd42c2e8Splunkywill be stored in the location given by 344bd42c2e8Splunky.Fa iip 345bd42c2e8Splunkyand this should be released after use with 346bd42c2e8Splunky.Xr free 3 . 347bd42c2e8Splunky.El 348bd42c2e8Splunky.Sh RETURN VALUES 349835184f2SwizThese Bluetooth device access routines return \-1 on failure, and 350bd42c2e8Splunky.Va errno 351bd42c2e8Splunkywill be set to indicate the error. 352bd42c2e8Splunky.Sh ERRORS 353bd42c2e8SplunkyIn addition to errors returned by the standard C library IO functions, 354bd42c2e8Splunkythe following errors may be indicated by device access routines. 355bd42c2e8Splunky.Bl -tag -offset indent -width ".Bq Er ETIMEDOUT" 356bd42c2e8Splunky.It Bq Er EINVAL 357bd42c2e8SplunkyA provided function argument was not valid. 358bd42c2e8Splunky.It Bq Er EIO 359bd42c2e8SplunkyA device response was not properly understood. 360bd42c2e8Splunky.It Bq Er ETIMEDOUT 361bd42c2e8SplunkyAn operation exceeded the given time limit. 362bd42c2e8Splunky.El 363bd42c2e8Splunky.Sh SEE ALSO 364bd42c2e8Splunky.Xr bluetooth 3 365bd42c2e8Splunky.Sh HISTORY 366bd42c2e8SplunkyThe Bluetooth device access API was created by 367bd42c2e8Splunky.An Maksim Yevmenkin 368bd42c2e8Splunkyand first appeared in 369bd42c2e8Splunky.Fx . 370bd42c2e8SplunkyThis implementation written for 371bd42c2e8Splunky.Nx 372bd42c2e8Splunkyby 373bd42c2e8Splunky.An Iain Hibbert . 374