1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
|
.\" Copyright (c) 2003-2009 Maksim Yevmenkin <m_evmenkin@yahoo.com>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: bluetooth.3,v 1.5 2003/05/20 23:04:30 max Exp $
.\" $FreeBSD$
.\"
.Dd April 9, 2009
.Dt BLUETOOTH 3
.Os
.Sh NAME
.Nm bt_gethostbyname ,
.Nm bt_gethostbyaddr ,
.Nm bt_gethostent ,
.Nm bt_sethostent ,
.Nm bt_endhostent ,
.Nm bt_getprotobyname ,
.Nm bt_getprotobynumber ,
.Nm bt_getprotoent ,
.Nm bt_setprotoent ,
.Nm bt_endprotoent ,
.Nm bt_aton ,
.Nm bt_ntoa ,
.Nm bt_devaddr ,
.Nm bt_devname ,
.Nm bt_devinfo ,
.Nm bt_devenum ,
.Nm bt_devopen ,
.Nm bt_devclose ,
.Nm bt_devsend ,
.Nm bt_devrecv ,
.Nm bt_devreq ,
.Nm bt_devfilter ,
.Nm bt_devfilter_pkt_set ,
.Nm bt_devfilter_pkt_clr ,
.Nm bt_devfilter_pkt_tst ,
.Nm bt_devfilter_evt_set ,
.Nm bt_devfilter_evt_clr ,
.Nm bt_devfilter_evt_tst ,
.Nm bt_devinquiry ,
.Nm bdaddr_same ,
.Nm bdaddr_any ,
.Nm bdaddr_copy
.Nd Bluetooth routines
.Sh LIBRARY
.Lb libbluetooth
.Sh SYNOPSIS
.In bluetooth.h
.Ft struct hostent *
.Fn bt_gethostbyname "const char *name"
.Ft struct hostent *
.Fn bt_gethostbyaddr "const char *addr" "int len" "int type"
.Ft struct hostent *
.Fn bt_gethostent void
.Ft void
.Fn bt_sethostent "int stayopen"
.Ft void
.Fn bt_endhostent void
.Ft struct protoent *
.Fn bt_getprotobyname "const char *name"
.Ft struct protoent *
.Fn bt_getprotobynumber "int proto"
.Ft struct protoent *
.Fn bt_getprotoent void
.Ft void
.Fn bt_setprotoent "int stayopen"
.Ft void
.Fn bt_endprotoent void
.Ft int
.Fn bt_aton "const char *str" "bdaddr_t *ba"
.Ft const char *
.Fn bt_ntoa "const bdaddr_t *ba" "char *str"
.Ft int
.Fn bt_devaddr "const char *devname" "bdaddr_t *addr"
.Ft int
.Fn bt_devname "char *devname" "const bdaddr_t *addr"
.Ft int
.Fn (bt_devenum_cb_t) "int s" "struct bt_devinfo const *di" "void *arg"
.Ft int
.Fn bt_devinfo "struct bt_devinfo *di"
.Ft int
.Fn bt_devenum "bt_devenum_cb_t *cb" "void *arg"
.Ft int
.Fn bt_devopen "char const *devname"
.Ft int
.Fn bt_devclose "int s"
.Ft int
.Fn bt_devsend "int s" "uint16_t opcode" "void *param" "size_t plen"
.Ft ssize_t
.Fn bt_devrecv "int s" "void *buf" "size_t size" "time_t to"
.Ft int
.Fn bt_devreq "int s" "struct bt_devreq *r" "time_t to"
.Ft int
.Fn bt_devfilter "int s" "struct bt_devfilter const *new" "struct bt_devfilter *old"
.Ft void
.Fn bt_devfilter_pkt_set "struct bt_devfilter *filter" "uint8_t type"
.Ft void
.Fn bt_devfilter_pkt_clt "struct bt_devfilter *filter" "uint8_t type"
.Ft int
.Fn bt_devfilter_pkt_tst "struct bt_devfilter const *filter" "uint8_t type"
.Ft void
.Fn bt_devfilter_evt_set "struct bt_devfilter *filter" "uint8_t event"
.Ft void
.Fn bt_devfilter_evt_clt "struct bt_devfilter *filter" "uint8_t event"
.Ft int
.Fn bt_devfilter_evt_tst "struct bt_devfilter const *filter" "uint8_t event"
.Ft int
.Fn bt_devinquiry "char const *devname" "time_t length" "int num_rsp" "struct bt_devinquiry **ii"
.Ft int
.Fn bdaddr_same "const bdaddr_t *a" "const bdaddr_t *b"
.Ft int
.Fn bdaddr_any "const bdaddr_t *a"
.Ft int
.Fn bdaddr_copy "const bdaddr_t *dst" "const bdaddr_t *src"
.Sh DESCRIPTION
The
.Fn bt_gethostent ,
.Fn bt_gethostbyname
and
.Fn bt_gethostbyaddr
functions
each return a pointer to an object with the
.Vt hostent
structure describing a Bluetooth host
referenced by name or by address, respectively.
.Pp
The
.Fa name
argument passed to
.Fn bt_gethostbyname
should point to a
.Dv NUL Ns -terminated
hostname.
The
.Fa addr
argument passed to
.Fn bt_gethostbyaddr
should point to an address which is
.Fa len
bytes long,
in binary form
(i.e., not a Bluetooth BD_ADDR in human readable
.Tn ASCII
form).
The
.Fa type
argument specifies the address family of this address and must be set to
.Dv AF_BLUETOOTH .
.Pp
The structure returned contains the information obtained from a line in
.Pa /etc/bluetooth/hosts
file.
.Pp
The
.Fn bt_sethostent
function controls whether
.Pa /etc/bluetooth/hosts
file should stay open after each call to
.Fn bt_gethostbyname
or
.Fn bt_gethostbyaddr .
If the
.Fa stayopen
flag is non-zero, the file will not be closed.
.Pp
The
.Fn bt_endhostent
function closes the
.Pa /etc/bluetooth/hosts
file.
.Pp
The
.Fn bt_getprotoent ,
.Fn bt_getprotobyname
and
.Fn bt_getprotobynumber
functions each return a pointer to an object with the
.Vt protoent
structure describing a Bluetooth Protocol Service Multiplexor referenced
by name or number, respectively.
.Pp
The
.Fa name
argument passed to
.Fn bt_getprotobyname
should point to a
.Dv NUL Ns -terminated
Bluetooth Protocol Service Multiplexor name.
The
.Fa proto
argument passed to
.Fn bt_getprotobynumber
should have numeric value of the desired Bluetooth Protocol Service Multiplexor.
.Pp
The structure returned contains the information obtained from a line in
.Pa /etc/bluetooth/protocols
file.
.Pp
The
.Fn bt_setprotoent
function controls whether
.Pa /etc/bluetooth/protocols
file should stay open after each call to
.Fn bt_getprotobyname
or
.Fn bt_getprotobynumber .
If the
.Fa stayopen
flag is non-zero, the file will not be closed.
.Pp
The
.Fn bt_endprotoent
function closes the
.Pa /etc/bluetooth/protocols
file.
.Pp
The
.Fn bt_aton
routine interprets the specified character string as a Bluetooth address,
placing the address into the structure provided.
It returns 1 if the string was successfully interpreted,
or 0 if the string is invalid.
.Pp
The routine
.Fn bt_ntoa
takes a Bluetooth address and places an
.Tn ASCII
string representing the address into the buffer provided.
It is up to the caller to ensure that provided buffer has enough space.
If no buffer was provided then internal static buffer will be used.
.Pp
The
.Fn bt_devaddr
function interprets the specified
.Fa devname
string as the address or device name of a Bluetooth device on the local system,
and places the device address in the provided
.Fa bdaddr ,
if any.
The function returns 1 if the string was successfully interpreted,
or 0 if the string did not match any local device.
The
.Fn bt_devname
function takes a Bluetooth device address and copies the local device
name associated with that address into the buffer provided,
if any.
Caller must ensure that provided buffer is at least
.Dv HCI_DEVNAME_SIZE
characters in size.
The function returns 1 when the device was found,
otherwise 0.
.Pp
The
.Fn bt_devinfo
function populates provided
.Vt bt_devinfo
structure with the information about given Bluetooth device.
The caller is expected to pass Bluetooth device name in the
.Fa devname
field of the passed
.Vt bt_devinfo
structure.
The function returns 0 when successful,
otherwise -1.
The
.Vt bt_devinfo
structure is defined as follows
.Bd -literal -offset indent
struct bt_devinfo
{
char devname[HCI_DEVNAME_SIZE];
uint32_t state;
bdaddr_t bdaddr;
uint16_t _reserved0;
uint8_t features[HCI_DEVFEATURES_SIZE];
/* buffer info */
uint16_t _reserved1;
uint16_t cmd_free;
uint16_t sco_size;
uint16_t sco_pkts;
uint16_t sco_free;
uint16_t acl_size;
uint16_t acl_pkts;
uint16_t acl_free;
/* stats */
uint32_t cmd_sent;
uint32_t evnt_recv;
uint32_t acl_recv;
uint32_t acl_sent;
uint32_t sco_recv;
uint32_t sco_sent;
uint32_t bytes_recv;
uint32_t bytes_sent;
/* misc/specific */
uint16_t link_policy_info;
uint16_t packet_type_info;
uint16_t role_switch_info;
uint16_t debug;
uint8_t _padding[20];
};
.Ed
.Pp
The
.Fn bt_devenum
function enumerates Bluetooth devices present in the system.
For every device found,
the function will call provided
.Fa cb
callback function which should be of
.Vt bt_devenum_cb_t
type.
The callback function is passed a
.Dv HCI
socket
.Fa s ,
fully populated
.Vt bt_devinfo
structure
.Fa di
and
.Fa arg
argument provided to the
.Fn bt_devenum .
The callback function can stop enumeration by returning a value
that is greater than zero.
The function returns number of successfully enumerated devices,
or -1 if an error occurred.
.Pp
The
.Fn bt_devopen
function opens a Bluetooth device with the given
.Fa devname
and returns a connected and bound
.Dv HCI
socket handle.
The function returns -1 if an error has occurred.
.Pp
The
.Fn bt_devclose
closes the passed
.Dv HCI
socket handle
.Fa s ,
previously obtained with
.Xr bt_devopen 3 .
.Pp
The
.Fn bt_devsend
function sends a Bluetooth
.Dv HCI
command with the given
.Fa opcode
to the provided socket
.Fa s ,
previously obtained with
.Xr bt_devopen 3 .
The
.Fa opcode
parameter is expected to be in the host byte order.
The
.Fa param
and
.Fa plen
parameters specify command parameters.
The
.Fn bt_devsend
function does not modify the
.Dv HCI
filter on the provided socket
.Fa s .
The function returns 0 on success,
or -1 if an error occurred.
.Pp
The
.Fn bt_devrecv
function receives one Bluetooth
.Dv HCI
packet from the socket
.Fa s ,
previously obtained with
.Xr bt_devopen 3 .
The packet is placed into the provided buffer
.Fa buf
of size
.Fa size .
The
.Fa to
parameter specifies receive timeout in seconds.
Infinite timeout can be specified by passing negative value in the
.Fa to
parameter.
The
.Fn bt_devrecv
function does not modify the
.Dv HCI
filter on the provided socket
.Fa s .
The function returns total number of bytes received,
or -1 if an error occurred.
.Pp
The
.Fn bt_devreq
function makes a Bluetooth
.Dv HCI
request to the socket
.Fa s ,
previously obtained with
.Xr bt_devopen 3 .
The function will send the specified command and will wait for the specified
event,
or timeout
.Fa to
seconds to occur.
The
.Vt bt_devreq
structure is defined as follows
.Bd -literal -offset indent
struct bt_devreq
{
uint16_t opcode;
uint8_t event;
void *cparam;
size_t clen;
void *rparam;
size_t rlen;
};
.Ed
.Pp
The
.Fa opcode
field specifies the command and is expected to be in the host byte order.
The
.Fa cparam
and
.Fa clen
fields specify command parameters data and command parameters data size
respectively.
The
.Fa event
field specifies which Bluetooth
.Dv HCI
event ID the function should wait for, otherwise it should be set to zero.
The
.Dv HCI
Command Complete and Command Status events are enabled by default.
The
.Fa rparam
and
.Fa rlen
parameters specify buffer and buffer size respectively where return
parameters should be placed.
The
.Fn bt_devreq
function temporarily modifies filter on the provided
.Dv HCI
socket
.Fa s .
The function returns 0 on success, or -1 if an error occurred.
.Pp
The
.Fn bt_devfilter
controls the local
.Dv HCI
filter associated with the socket
.Fa s ,
previously obtained with
.Xr bt_devopen 3 .
Filtering can be done on packet types, i.e.
.Dv ACL ,
.Dv SCO
or
.Dv HCI ,
command and event packets, and, in addition, on
.Dv HCI
event IDs.
Before applying the
.Fa new
filter (if provided) the function will try to obtain the current filter
from the socket
.Fa s
and place it into the
.Fa old
parameter (if provided).
The function returns 0 on success, or -1 if an error occurred.
.Pp
The
.Fn bt_devfilter_pkt_set ,
.Fn bt_devfilter_pkt_clr
and
.Fn bt_devfilter_pkt_tst
functions can be used to modify and test the
.Dv HCI
filter
.Fa filter .
The
.Fa type
parameter specifies
.Dv HCI
packet type.
.Pp
The
.Fn bt_devfilter_evt_set ,
.Fn bt_devfilter_evt_clr
and
.Fn bt_devfilter_evt_tst
functions can be used to modify and test the
.Dv HCI
event filter
.Fa filter .
The
.Fa event
parameter specifies
.Dv HCI
event ID.
.Pp
The
.Fn bt_devinquiry
function performs Bluetooth inquiry.
The
.Fa devname
parameter specifies which local Bluetooth device should perform an inquiry.
If not specified, i.e.
.Dv NULL ,
then first available device will be used.
The
.Fa length
parameters specifies the total length of an inquiry in seconds.
If not specified, i.e. 0, default value will be used.
The
.Fa num_rsp
parameter specifies the number of responses that can be received before
the inquiry is halted.
If not specified, i.e. 0, default value will be used.
The
.Fa ii
parameter specifies where to place inquiry results.
On success, the function will return total number of inquiry results,
will allocate,
using
.Xr calloc 3 ,
buffer to store all the inquiry results and
will return pointer to the allocated buffer in the
.Fa ii
parameter.
It is up to the caller of the function to dispose of the buffer using
.Xr free 3
call.
The function returns -1 if an error has occurred.
The
.Vt bt_devinquiry
structure is defined as follows
.Bd -literal -offset indent
struct bt_devinquiry {
bdaddr_t bdaddr;
uint8_t pscan_rep_mode;
uint8_t pscan_period_mode;
uint8_t dev_class[3];
uint16_t clock_offset;
int8_t rssi;
uint8_t data[240];
};
.Ed
.Pp
The
.Fn bdaddr_same ,
.Fn bdaddr_any
and
.Fn bdaddr_copy
are handy shorthand Bluetooth address utility functions.
The
.Fn bdaddr_same
function will test if two provided BD_ADDRs are the same.
The
.Fn bdaddr_any
function will test if provided BD_ADDR is
.Dv ANY
BD_ADDR.
The
.Fn bdaddr_copy
function will copy provided
.Fa src
BD_ADDR into provided
.Fa dst
BD_ADDR.
.Sh FILES
.Bl -tag -width ".Pa /etc/bluetooth/hosts" -compact
.It Pa /etc/bluetooth/hosts
.It Pa /etc/bluetooth/protocols
.El
.Sh EXAMPLES
Print out the hostname associated with a specific BD_ADDR:
.Bd -literal -offset indent
const char *bdstr = "00:01:02:03:04:05";
bdaddr_t bd;
struct hostent *hp;
if (!bt_aton(bdstr, &bd))
errx(1, "can't parse BD_ADDR %s", bdstr);
if ((hp = bt_gethostbyaddr((const char *)&bd,
sizeof(bd), AF_BLUETOOTH)) == NULL)
errx(1, "no name associated with %s", bdstr);
printf("name associated with %s is %s\en", bdstr, hp->h_name);
.Ed
.Sh DIAGNOSTICS
Error return status from
.Fn bt_gethostent ,
.Fn bt_gethostbyname
and
.Fn bt_gethostbyaddr
is indicated by return of a
.Dv NULL
pointer.
The external integer
.Va h_errno
may then be checked to see whether this is a temporary failure
or an invalid or unknown host.
The routine
.Xr herror 3
can be used to print an error message describing the failure.
If its argument
.Fa string
is
.Pf non- Dv NULL ,
it is printed, followed by a colon and a space.
The error message is printed with a trailing newline.
.Pp
The variable
.Va h_errno
can have the following values:
.Bl -tag -width ".Dv HOST_NOT_FOUND"
.It Dv HOST_NOT_FOUND
No such host is known.
.It Dv NO_RECOVERY
Some unexpected server failure was encountered.
This is a non-recoverable error.
.El
.Pp
The
.Fn bt_getprotoent ,
.Fn bt_getprotobyname
and
.Fn bt_getprotobynumber
return
.Dv NULL
on EOF or error.
.Sh SEE ALSO
.Xr gethostbyaddr 3 ,
.Xr gethostbyname 3 ,
.Xr getprotobyname 3 ,
.Xr getprotobynumber 3 ,
.Xr herror 3 ,
.Xr inet_aton 3 ,
.Xr inet_ntoa 3 ,
.Xr ng_hci 4
.Sh CAVEAT
The
.Fn bt_gethostent
function reads the next line of
.Pa /etc/bluetooth/hosts ,
opening the file if necessary.
.Pp
The
.Fn bt_sethostent
function opens and/or rewinds the
.Pa /etc/bluetooth/hosts
file.
.Pp
The
.Fn bt_getprotoent
function reads the next line of
.Pa /etc/bluetooth/protocols ,
opening the file if necessary.
.Pp
The
.Fn bt_setprotoent
function opens and/or rewinds the
.Pa /etc/bluetooth/protocols
file.
.Pp
The
.Fn bt_devenum
function enumerates up to
.Dv HCI_DEVMAX
Bluetooth devices.
During enumeration the
.Fn bt_devenum
function uses the same
.Dv HCI
socket.
The function guarantees that the socket,
passed to the callback function,
will be bound and connected to the Bluetooth device being enumerated.
.Sh AUTHORS
.An Maksim Yevmenkin Aq m_evmenkin@yahoo.com
.Sh BUGS
Some of those functions use static data storage;
if the data is needed for future use, it should be
copied before any subsequent calls overwrite it.
|