2002-03-09 05:10:06 +08:00
|
|
|
/*
|
|
|
|
BlueZ - Bluetooth protocol stack for Linux
|
|
|
|
Copyright (C) 2000-2001 Qualcomm Incorporated
|
|
|
|
|
|
|
|
Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License version 2 as
|
|
|
|
published by the Free Software Foundation;
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
|
|
|
|
IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
|
|
|
|
CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
|
|
|
|
COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
|
|
|
|
SOFTWARE IS DISCLAIMED.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <asm/types.h>
|
|
|
|
|
|
|
|
#include <bluetooth.h>
|
|
|
|
#include <hci.h>
|
|
|
|
#include <hci_lib.h>
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char *str; unsigned int val;
|
|
|
|
} hci_map;
|
|
|
|
|
2002-03-22 07:20:32 +08:00
|
|
|
static char *hci_bit2str(hci_map *m, unsigned int val)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
2002-04-12 00:56:10 +08:00
|
|
|
char *str = malloc(120);
|
2002-03-09 05:10:06 +08:00
|
|
|
char *ptr = str;
|
|
|
|
|
2002-03-26 03:12:16 +08:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2002-03-09 05:10:06 +08:00
|
|
|
*ptr = 0;
|
|
|
|
while (m->str) {
|
|
|
|
if ((unsigned int) m->val & val)
|
|
|
|
ptr += sprintf(ptr, "%s ", m->str);
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-03-22 07:20:32 +08:00
|
|
|
static int hci_str2bit(hci_map *map, char *str, unsigned int *val)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
char *t, *ptr;
|
|
|
|
hci_map *m;
|
|
|
|
int set;
|
|
|
|
|
2002-03-27 03:25:05 +08:00
|
|
|
if (!str || !(str = ptr = strdup(str)))
|
2002-03-09 05:10:06 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
*val = set = 0;
|
|
|
|
|
|
|
|
while ((t=strsep(&ptr, ","))) {
|
|
|
|
for (m=map; m->str; m++) {
|
|
|
|
if (!strcasecmp(m->str,t)) {
|
|
|
|
*val |= (unsigned int) m->val;
|
|
|
|
set = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2002-03-22 07:20:32 +08:00
|
|
|
static char *hci_uint2str(hci_map *m, unsigned int val)
|
|
|
|
{
|
2002-03-26 03:12:16 +08:00
|
|
|
char *str = malloc(50);
|
2002-03-22 07:20:32 +08:00
|
|
|
char *ptr = str;
|
|
|
|
|
2002-03-26 03:12:16 +08:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2002-03-22 07:20:32 +08:00
|
|
|
*ptr = 0;
|
|
|
|
while (m->str) {
|
|
|
|
if ((unsigned int) m->val == val) {
|
|
|
|
ptr += sprintf(ptr, "%s", m->str);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hci_str2uint(hci_map *map, char *str, unsigned int *val)
|
|
|
|
{
|
|
|
|
char *t, *ptr;
|
|
|
|
hci_map *m;
|
|
|
|
int set = 0;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
str = ptr = strdup(str);
|
|
|
|
|
|
|
|
while ((t=strsep(&ptr, ","))) {
|
|
|
|
for (m=map; m->str; m++) {
|
|
|
|
if (!strcasecmp(m->str,t)) {
|
|
|
|
*val = (unsigned int) m->val; set = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(str);
|
|
|
|
|
|
|
|
return set;
|
|
|
|
}
|
|
|
|
|
2002-03-09 05:10:06 +08:00
|
|
|
char *hci_dtypetostr(int type)
|
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case HCI_VHCI:
|
|
|
|
return "VHCI";
|
|
|
|
case HCI_USB:
|
|
|
|
return "USB ";
|
|
|
|
case HCI_PCCARD:
|
|
|
|
return "PCCARD";
|
|
|
|
case HCI_UART:
|
|
|
|
return "UART";
|
2002-09-23 07:59:17 +08:00
|
|
|
case HCI_RS232:
|
|
|
|
return "RS232";
|
|
|
|
case HCI_PCI:
|
|
|
|
return "PCI";
|
2002-03-09 05:10:06 +08:00
|
|
|
default:
|
|
|
|
return "UKNW";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HCI dev flags mapping */
|
|
|
|
hci_map dev_flags_map[] = {
|
|
|
|
{ "UP", HCI_UP },
|
|
|
|
{ "INIT", HCI_INIT },
|
|
|
|
{ "RUNNING", HCI_RUNNING },
|
|
|
|
{ "RAW", HCI_RAW },
|
|
|
|
{ "PSCAN", HCI_PSCAN },
|
|
|
|
{ "ISCAN", HCI_ISCAN },
|
2002-12-03 04:24:17 +08:00
|
|
|
{ "INQUIRY", HCI_INQUIRY },
|
2002-03-09 05:10:06 +08:00
|
|
|
{ "AUTH", HCI_AUTH },
|
|
|
|
{ "ENCRYPT", HCI_ENCRYPT },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
char *hci_dflagstostr(uint32_t flags)
|
|
|
|
{
|
2002-03-26 03:12:16 +08:00
|
|
|
char *str = malloc(50);
|
2002-03-09 05:10:06 +08:00
|
|
|
char *ptr = str;
|
|
|
|
hci_map *m = dev_flags_map;
|
|
|
|
|
2002-03-26 03:12:16 +08:00
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
2002-03-09 05:10:06 +08:00
|
|
|
*ptr = 0;
|
|
|
|
|
|
|
|
if (!hci_test_bit(HCI_UP, &flags))
|
|
|
|
ptr += sprintf(ptr, "DOWN ");
|
|
|
|
|
|
|
|
while (m->str) {
|
|
|
|
if (hci_test_bit(m->val, &flags))
|
|
|
|
ptr += sprintf(ptr, "%s ", m->str);
|
|
|
|
m++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HCI packet type mapping */
|
|
|
|
hci_map pkt_type_map[] = {
|
|
|
|
{ "DM1", HCI_DM1 },
|
|
|
|
{ "DM3", HCI_DM3 },
|
|
|
|
{ "DM5", HCI_DM5 },
|
|
|
|
{ "DH1", HCI_DH1 },
|
|
|
|
{ "DH3", HCI_DH3 },
|
|
|
|
{ "DH5", HCI_DH5 },
|
|
|
|
{ "HV1", HCI_HV1 },
|
|
|
|
{ "HV2", HCI_HV2 },
|
|
|
|
{ "HV3", HCI_HV3 },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
char *hci_ptypetostr(unsigned int ptype)
|
|
|
|
{
|
2002-03-22 07:20:32 +08:00
|
|
|
return hci_bit2str(pkt_type_map, ptype);
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
int hci_strtoptype(char *str, unsigned int *val)
|
|
|
|
{
|
2002-03-22 07:20:32 +08:00
|
|
|
return hci_str2bit(pkt_type_map, str, val);
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Link policy mapping */
|
|
|
|
hci_map link_policy_map[] = {
|
|
|
|
{ "NONE", 0 },
|
|
|
|
{ "RSWITCH", HCI_LP_RSWITCH },
|
|
|
|
{ "HOLD", HCI_LP_HOLD },
|
|
|
|
{ "SNIFF", HCI_LP_SNIFF },
|
|
|
|
{ "PARK", HCI_LP_PARK },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
char *hci_lptostr(unsigned int lp)
|
|
|
|
{
|
2002-03-22 07:20:32 +08:00
|
|
|
return hci_bit2str(link_policy_map, lp);
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
int hci_strtolp(char *str, unsigned int *val)
|
|
|
|
{
|
2002-03-22 07:20:32 +08:00
|
|
|
return hci_str2bit(link_policy_map, str, val);
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Link mode mapping */
|
|
|
|
hci_map link_mode_map[] = {
|
|
|
|
{ "NONE", 0 },
|
|
|
|
{ "ACCEPT", HCI_LM_ACCEPT },
|
|
|
|
{ "MASTER", HCI_LM_MASTER },
|
|
|
|
{ "AUTH", HCI_LM_AUTH },
|
|
|
|
{ "ENCRYPT", HCI_LM_ENCRYPT},
|
|
|
|
{ "TRUSTED", HCI_LM_TRUSTED},
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
char *hci_lmtostr(unsigned int lm)
|
|
|
|
{
|
2002-03-26 03:12:16 +08:00
|
|
|
char *s, *str = malloc(50);
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-03-26 03:12:16 +08:00
|
|
|
*str = 0;
|
2002-03-09 05:10:06 +08:00
|
|
|
if (!(lm & HCI_LM_MASTER))
|
|
|
|
strcpy(str, "SLAVE ");
|
|
|
|
|
2002-03-26 03:12:16 +08:00
|
|
|
s = hci_bit2str(link_mode_map, lm);
|
|
|
|
if (!s) {
|
|
|
|
free(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
strcat(str, s);
|
|
|
|
free(s);
|
2002-03-09 05:10:06 +08:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
int hci_strtolm(char *str, unsigned int *val)
|
|
|
|
{
|
2002-03-22 07:20:32 +08:00
|
|
|
return hci_str2bit(link_mode_map, str, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Version mapping */
|
|
|
|
hci_map ver_map[] = {
|
|
|
|
{ "1.0b", 0x00 },
|
|
|
|
{ "1.1", 0x01 },
|
|
|
|
{ NULL }
|
|
|
|
};
|
|
|
|
char *hci_vertostr(unsigned int ver)
|
|
|
|
{
|
|
|
|
char *str = hci_uint2str(ver_map, ver);
|
|
|
|
return *str ? str : "n/a";
|
|
|
|
}
|
|
|
|
int hci_strtover(char *str, unsigned int *ver)
|
|
|
|
{
|
|
|
|
return hci_str2uint(ver_map, str, ver);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *lmp_vertostr(unsigned int ver)
|
|
|
|
{
|
|
|
|
char *str = hci_uint2str(ver_map, ver);
|
|
|
|
return *str ? str : "n/a";
|
|
|
|
}
|
|
|
|
int lmp_strtover(char *str, unsigned int *ver)
|
|
|
|
{
|
|
|
|
return hci_str2uint(ver_map, str, ver);
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
|
2002-04-12 00:56:10 +08:00
|
|
|
/* LMP features mapping */
|
|
|
|
hci_map lmp_features_map[][9] = {
|
|
|
|
{ /* byte 0 */
|
|
|
|
{ "<3-slot packets>", LMP_3SLOT },
|
|
|
|
{ "<5-slot packets>", LMP_5SLOT },
|
|
|
|
{ "<encryption>", LMP_ENCRYPT },
|
|
|
|
{ "<slot offset>", LMP_SOFFSET },
|
|
|
|
{ "<timing accuracy>", LMP_TACCURACY},
|
|
|
|
{ "<role switch>", LMP_RSWITCH },
|
|
|
|
{ "<hold mode>", LMP_HOLD },
|
|
|
|
{ "<sniff mode>", LMP_SNIFF },
|
|
|
|
{ NULL }
|
|
|
|
},
|
|
|
|
{ /* byte 1 */
|
|
|
|
{ "<park mode>", LMP_PARK },
|
|
|
|
{ "<RSSI>", LMP_RSSI },
|
|
|
|
{ "<channel quality>", LMP_QUALITY },
|
|
|
|
{ "<SCO link>", LMP_SCO },
|
|
|
|
{ "<HV2 packets>", LMP_HV2 },
|
|
|
|
{ "<HV3 packets>", LMP_HV3 },
|
|
|
|
{ "<u-law log>", LMP_ULAW },
|
|
|
|
{ "<A-law log>", LMP_ALAW },
|
|
|
|
{ NULL }
|
|
|
|
},
|
|
|
|
{ /* byte 2 */
|
|
|
|
{ "<CVSD>", LMP_CVSD },
|
|
|
|
{ "<paging scheme>", LMP_PSCHEME },
|
|
|
|
{ "<power control>", LMP_PCONTROL },
|
|
|
|
{ "<transparent SCO>", LMP_TRSP_SCO },
|
|
|
|
{ NULL }
|
|
|
|
},
|
|
|
|
{{ NULL }}
|
|
|
|
};
|
|
|
|
|
|
|
|
char *lmp_featurestostr(uint8_t *features, char *pref, int width)
|
|
|
|
{
|
|
|
|
char *ptr, *str = malloc(400);
|
|
|
|
int i, w;
|
|
|
|
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ptr = str; *ptr = 0;
|
|
|
|
|
|
|
|
if (pref)
|
|
|
|
ptr += sprintf(ptr, "%s", pref);
|
|
|
|
|
|
|
|
for(i=0, w=0; lmp_features_map[i][0].str; i++) {
|
|
|
|
hci_map *m;
|
|
|
|
|
|
|
|
m = lmp_features_map[i];
|
|
|
|
while (m->str) {
|
2002-04-16 00:55:14 +08:00
|
|
|
if ((unsigned int) m->val & (unsigned int) features[i]) {
|
2002-04-12 00:56:10 +08:00
|
|
|
ptr += sprintf(ptr, "%s ", m->str);
|
2002-04-16 00:55:14 +08:00
|
|
|
w = (w + 1) & width;
|
|
|
|
if (!w)
|
|
|
|
ptr += sprintf(ptr, "\n%s", pref ? pref : "");
|
|
|
|
}
|
2002-04-12 00:56:10 +08:00
|
|
|
m++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2002-03-09 05:10:06 +08:00
|
|
|
/* HCI functions that do not require open device */
|
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
int hci_for_each_dev(int flag, int(*func)(int s, int dev_id, long arg), long arg)
|
|
|
|
{
|
|
|
|
struct hci_dev_list_req *dl;
|
|
|
|
struct hci_dev_req *dr;
|
|
|
|
int dev_id = -1;
|
|
|
|
int s, i;
|
|
|
|
|
|
|
|
s = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
|
|
|
|
if (s < 0)
|
|
|
|
return -1;
|
|
|
|
|
2002-11-23 05:54:27 +08:00
|
|
|
dl = malloc(HCI_MAX_DEV * sizeof(*dr) + sizeof(*dl));
|
2002-06-19 00:46:54 +08:00
|
|
|
if (!dl) {
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dl->dev_num = HCI_MAX_DEV;
|
|
|
|
dr = dl->dev_req;
|
|
|
|
|
|
|
|
if (ioctl(s, HCIGETDEVLIST, (void *)dl))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
for (i=0; i < dl->dev_num; i++, dr++) {
|
|
|
|
if (hci_test_bit(flag, &dr->dev_opt))
|
|
|
|
if (!func || func(s, dr->dev_id, arg)) {
|
|
|
|
dev_id = dr->dev_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
close(s);
|
|
|
|
free(dl);
|
|
|
|
return dev_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __other_bdaddr(int s, int dev_id, long arg)
|
|
|
|
{
|
|
|
|
struct hci_dev_info di = {dev_id: dev_id};
|
|
|
|
if (ioctl(s, HCIGETDEVINFO, (void*) &di))
|
|
|
|
return 0;
|
|
|
|
return bacmp((bdaddr_t *)arg, &di.bdaddr);
|
|
|
|
}
|
|
|
|
|
2002-06-19 02:15:13 +08:00
|
|
|
static int __same_bdaddr(int s, int dev_id, long arg)
|
|
|
|
{
|
|
|
|
struct hci_dev_info di = {dev_id: dev_id};
|
|
|
|
if (ioctl(s, HCIGETDEVINFO, (void*) &di))
|
|
|
|
return 0;
|
|
|
|
return !bacmp((bdaddr_t *)arg, &di.bdaddr);
|
|
|
|
}
|
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
int hci_get_route(bdaddr_t *bdaddr)
|
|
|
|
{
|
|
|
|
if (bdaddr)
|
|
|
|
return hci_for_each_dev(HCI_UP, __other_bdaddr, (long) bdaddr);
|
|
|
|
else
|
|
|
|
return hci_for_each_dev(HCI_UP, NULL, 0);
|
|
|
|
}
|
|
|
|
|
2003-02-11 18:22:23 +08:00
|
|
|
int hci_devid(const char *str)
|
2002-06-19 02:15:13 +08:00
|
|
|
{
|
2002-06-19 02:32:44 +08:00
|
|
|
bdaddr_t ba;
|
|
|
|
int id = -1;
|
|
|
|
|
|
|
|
if (!strncmp(str, "hci", 3) && strlen(str) >= 4) {
|
|
|
|
id = atoi(str + 3);
|
|
|
|
if (hci_devba(id, &ba) < 0)
|
|
|
|
return -1;
|
|
|
|
} else {
|
2002-06-19 02:48:36 +08:00
|
|
|
errno = ENODEV;
|
2002-06-19 02:32:44 +08:00
|
|
|
str2ba(str, &ba);
|
|
|
|
id = hci_for_each_dev(HCI_UP, __same_bdaddr, (long) &ba);
|
|
|
|
}
|
|
|
|
return id;
|
2002-06-19 02:15:13 +08:00
|
|
|
}
|
|
|
|
|
2002-03-09 05:10:06 +08:00
|
|
|
int hci_devinfo(int dev_id, struct hci_dev_info *di)
|
|
|
|
{
|
|
|
|
int s, err;
|
|
|
|
|
|
|
|
s = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
|
|
|
|
if (s < 0)
|
|
|
|
return s;
|
|
|
|
|
|
|
|
di->dev_id = dev_id;
|
|
|
|
err = ioctl(s, HCIGETDEVINFO, (void *) di);
|
|
|
|
close(s);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2002-05-18 01:12:30 +08:00
|
|
|
int hci_devba(int dev_id, bdaddr_t *ba)
|
|
|
|
{
|
|
|
|
struct hci_dev_info di;
|
|
|
|
|
|
|
|
if (hci_devinfo(dev_id, &di))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!hci_test_bit(HCI_UP, &di.flags)) {
|
|
|
|
errno = ENETDOWN;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bacpy(ba, &di.bdaddr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-11 18:22:23 +08:00
|
|
|
int hci_inquiry(int dev_id, int len, int nrsp, const uint8_t *lap, inquiry_info **ii, long flags)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
struct hci_inquiry_req *ir;
|
2002-06-19 00:46:54 +08:00
|
|
|
void *buf;
|
|
|
|
int s, err;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
if (nrsp <= 0)
|
|
|
|
nrsp = 200; // enough ?
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
if (dev_id < 0 && (dev_id = hci_get_route(NULL)) < 0) {
|
|
|
|
errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
|
|
|
|
if (s < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
buf = malloc(sizeof(*ir) + (sizeof(inquiry_info) * (nrsp)));
|
|
|
|
if (!buf) {
|
|
|
|
close(s);
|
|
|
|
return -1;
|
|
|
|
}
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
ir = buf;
|
2002-03-09 05:10:06 +08:00
|
|
|
ir->dev_id = dev_id;
|
2002-06-19 00:46:54 +08:00
|
|
|
ir->num_rsp = nrsp;
|
2002-03-09 05:10:06 +08:00
|
|
|
ir->length = len;
|
|
|
|
ir->flags = flags;
|
|
|
|
|
|
|
|
if (lap) {
|
|
|
|
memcpy(ir->lap, lap, 3);
|
|
|
|
} else {
|
|
|
|
ir->lap[0] = 0x33;
|
|
|
|
ir->lap[1] = 0x8b;
|
|
|
|
ir->lap[2] = 0x9e;
|
|
|
|
}
|
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
err = ioctl(s, HCIINQUIRY, (unsigned long) buf);
|
|
|
|
close(s);
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
if (!err) {
|
|
|
|
int size = sizeof(inquiry_info) * ir->num_rsp;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
if (!*ii)
|
|
|
|
*ii = (void *) malloc(size);
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-06-19 00:46:54 +08:00
|
|
|
if (*ii) {
|
|
|
|
memcpy((void *) *ii, buf + sizeof(*ir), size);
|
|
|
|
err = ir->num_rsp;
|
|
|
|
} else
|
|
|
|
err = -1;
|
|
|
|
}
|
2002-03-09 05:10:06 +08:00
|
|
|
free(buf);
|
2002-06-19 00:46:54 +08:00
|
|
|
return err;
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Open HCI device.
|
|
|
|
* Returns device descriptor (dd). */
|
|
|
|
int hci_open_dev(int dev_id)
|
|
|
|
{
|
|
|
|
struct sockaddr_hci a;
|
|
|
|
int dd, err;
|
|
|
|
|
|
|
|
/* Create HCI socket */
|
|
|
|
dd = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_HCI);
|
|
|
|
if (dd < 0)
|
|
|
|
return dd;
|
|
|
|
|
|
|
|
/* Bind socket to the HCI device */
|
|
|
|
a.hci_family = AF_BLUETOOTH;
|
|
|
|
a.hci_dev = dev_id;
|
|
|
|
if (bind(dd, (struct sockaddr *)&a, sizeof(a)) < 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
return dd;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
err = errno;
|
|
|
|
close(dd);
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_close_dev(int dd)
|
|
|
|
{
|
|
|
|
return close(dd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HCI functions that require open device
|
|
|
|
* dd - Device descriptor returned by hci_dev_open. */
|
|
|
|
|
|
|
|
int hci_send_cmd(int dd, uint16_t ogf, uint16_t ocf, uint8_t plen, void *param)
|
|
|
|
{
|
|
|
|
uint8_t type = HCI_COMMAND_PKT;
|
|
|
|
hci_command_hdr hc;
|
|
|
|
struct iovec iv[3];
|
|
|
|
int ivn;
|
|
|
|
|
|
|
|
hc.opcode = htobs(cmd_opcode_pack(ogf, ocf));
|
|
|
|
hc.plen= plen;
|
|
|
|
|
|
|
|
iv[0].iov_base = &type;
|
|
|
|
iv[0].iov_len = 1;
|
|
|
|
iv[1].iov_base = &hc;
|
|
|
|
iv[1].iov_len = HCI_COMMAND_HDR_SIZE;
|
|
|
|
ivn = 2;
|
|
|
|
|
|
|
|
if (plen) {
|
|
|
|
iv[2].iov_base = param;
|
|
|
|
iv[2].iov_len = plen;
|
|
|
|
ivn = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (writev(dd, iv, ivn) < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_send_req(int dd, struct hci_request *r, int to)
|
|
|
|
{
|
|
|
|
unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
|
|
|
|
uint16_t opcode = htobs(cmd_opcode_pack(r->ogf, r->ocf));
|
|
|
|
struct hci_filter nf, of;
|
|
|
|
hci_event_hdr *hdr;
|
|
|
|
int err, len, try;
|
|
|
|
|
|
|
|
len = sizeof(of);
|
|
|
|
if (getsockopt(dd, SOL_HCI, HCI_FILTER, &of, &len) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
hci_filter_clear(&nf);
|
|
|
|
hci_filter_set_ptype(HCI_EVENT_PKT, &nf);
|
|
|
|
hci_filter_set_event(EVT_CMD_STATUS, &nf);
|
|
|
|
hci_filter_set_event(EVT_CMD_COMPLETE, &nf);
|
|
|
|
hci_filter_set_event(r->event, &nf);
|
|
|
|
hci_filter_set_opcode(opcode, &nf);
|
|
|
|
if (setsockopt(dd, SOL_HCI, HCI_FILTER, &nf, sizeof(nf)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (hci_send_cmd(dd, r->ogf, r->ocf, r->clen, r->cparam) < 0)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
try = 10;
|
|
|
|
while (try--) {
|
|
|
|
evt_cmd_complete *cc;
|
|
|
|
evt_cmd_status *cs;
|
|
|
|
|
|
|
|
if (to) {
|
|
|
|
struct pollfd p;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
p.fd = dd; p.events = POLLIN;
|
|
|
|
while ((n = poll(&p, 1, to)) < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!n) {
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
to -= 10;
|
|
|
|
if (to < 0) to = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((len = read(dd, buf, sizeof(buf))) < 0) {
|
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
continue;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
hdr = (void *)(buf + 1);
|
|
|
|
ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
|
|
|
|
len -= (1 + HCI_EVENT_HDR_SIZE);
|
|
|
|
|
|
|
|
switch (hdr->evt) {
|
|
|
|
case EVT_CMD_STATUS:
|
|
|
|
cs = (void *)ptr;
|
|
|
|
|
|
|
|
if (cs->opcode != opcode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (cs->status) {
|
|
|
|
errno = EIO;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVT_CMD_COMPLETE:
|
|
|
|
cc = (void *)ptr;
|
|
|
|
|
|
|
|
if (cc->opcode != opcode)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ptr += EVT_CMD_COMPLETE_SIZE;
|
|
|
|
len -= EVT_CMD_COMPLETE_SIZE;
|
|
|
|
|
|
|
|
r->rlen = MIN(len, r->rlen);
|
|
|
|
memcpy(r->rparam, ptr, r->rlen);
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (hdr->evt != r->event)
|
|
|
|
break;
|
|
|
|
|
|
|
|
r->rlen = MIN(len, r->rlen);
|
|
|
|
memcpy(r->rparam, ptr, r->rlen);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
errno = ETIMEDOUT;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
err = errno;
|
|
|
|
setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
|
|
|
|
errno = err;
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
done:
|
|
|
|
setsockopt(dd, SOL_HCI, HCI_FILTER, &of, sizeof(of));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-11 18:22:23 +08:00
|
|
|
int hci_create_connection(int dd, const bdaddr_t *ba, uint16_t ptype, uint16_t clkoffset, uint8_t rswitch, uint16_t *handle, int to)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
evt_conn_complete rp;
|
|
|
|
create_conn_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
2003-03-21 22:13:46 +08:00
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
bacpy(&cp.bdaddr, ba);
|
|
|
|
cp.pkt_type = ptype;
|
|
|
|
cp.pscan_rep_mode = 0x01;
|
|
|
|
cp.clock_offset = clkoffset;
|
|
|
|
cp.role_switch = rswitch;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-03-09 05:10:06 +08:00
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_CREATE_CONN;
|
|
|
|
rq.event = EVT_CONN_COMPLETE;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = CREATE_CONN_CP_SIZE;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = EVT_CONN_COMPLETE_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:45:46 +08:00
|
|
|
*handle = rp.handle;
|
|
|
|
return 0;
|
2002-03-09 05:10:06 +08:00
|
|
|
}
|
|
|
|
|
2002-03-23 03:45:46 +08:00
|
|
|
int hci_disconnect(int dd, uint16_t handle, uint8_t reason, int to)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
evt_disconn_complete rp;
|
|
|
|
disconnect_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2002-03-23 03:45:46 +08:00
|
|
|
cp.handle = handle;
|
|
|
|
cp.reason = reason;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-03-09 05:10:06 +08:00
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_DISCONNECT;
|
|
|
|
rq.event = EVT_DISCONN_COMPLETE;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = DISCONNECT_CP_SIZE;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = EVT_DISCONN_COMPLETE_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-23 01:17:57 +08:00
|
|
|
int hci_local_name(int dd, int len, char *name, int to)
|
|
|
|
{
|
|
|
|
return hci_read_local_name(dd, len, name, to);
|
|
|
|
}
|
|
|
|
|
2002-08-22 18:04:18 +08:00
|
|
|
int hci_read_local_name(int dd, int len, char *name, int to)
|
2002-03-20 01:35:54 +08:00
|
|
|
{
|
|
|
|
read_local_name_rp rp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-04-11 02:20:36 +08:00
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_READ_LOCAL_NAME;
|
2002-03-20 01:35:54 +08:00
|
|
|
rq.rparam = &rp;
|
2002-04-11 02:20:36 +08:00
|
|
|
rq.rlen = READ_LOCAL_NAME_RP_SIZE;
|
2002-03-20 01:35:54 +08:00
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rp.name[247] = '\0';
|
|
|
|
strncpy(name, rp.name, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-11 18:22:23 +08:00
|
|
|
int hci_write_local_name(int dd, const char *name, int to)
|
2002-08-22 18:04:18 +08:00
|
|
|
{
|
2002-08-23 01:17:57 +08:00
|
|
|
change_local_name_cp cp;
|
|
|
|
struct hci_request rq;
|
2002-08-22 18:04:18 +08:00
|
|
|
|
2002-08-23 01:17:57 +08:00
|
|
|
strncpy(cp.name, name, sizeof(cp.name));
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_CHANGE_LOCAL_NAME;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = CHANGE_LOCAL_NAME_CP_SIZE;
|
2002-08-22 18:04:18 +08:00
|
|
|
|
2002-08-23 01:17:57 +08:00
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
2002-08-22 18:04:18 +08:00
|
|
|
}
|
|
|
|
|
2003-02-11 18:22:23 +08:00
|
|
|
int hci_remote_name(int dd, const bdaddr_t *ba, int len, char *name, int to)
|
2002-09-18 08:37:04 +08:00
|
|
|
{
|
|
|
|
return hci_read_remote_name(dd, ba, len, name, to);
|
|
|
|
}
|
|
|
|
|
2003-02-11 18:22:23 +08:00
|
|
|
int hci_read_remote_name(int dd, const bdaddr_t *ba, int len, char *name, int to)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
evt_remote_name_req_complete rn;
|
|
|
|
remote_name_req_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
bacpy(&cp.bdaddr, ba);
|
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-04-11 02:20:36 +08:00
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_REMOTE_NAME_REQ;
|
2002-03-09 05:10:06 +08:00
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = REMOTE_NAME_REQ_CP_SIZE;
|
|
|
|
rq.event = EVT_REMOTE_NAME_REQ_COMPLETE;
|
|
|
|
rq.rparam = &rn;
|
|
|
|
rq.rlen = EVT_REMOTE_NAME_REQ_COMPLETE_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rn.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rn.name[247] = '\0';
|
|
|
|
strncpy(name, rn.name, len);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:45:46 +08:00
|
|
|
int hci_read_remote_features(int dd, uint16_t handle, uint8_t *features, int to)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
evt_read_remote_features_complete rp;
|
|
|
|
read_remote_features_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2002-03-23 03:45:46 +08:00
|
|
|
cp.handle = handle;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-03-09 05:10:06 +08:00
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_READ_REMOTE_FEATURES;
|
|
|
|
rq.event = EVT_READ_REMOTE_FEATURES_COMPLETE;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = READ_REMOTE_FEATURES_CP_SIZE;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = EVT_READ_REMOTE_FEATURES_COMPLETE_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(features, rp.features, 8);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-03-23 03:45:46 +08:00
|
|
|
int hci_read_remote_version(int dd, uint16_t handle, struct hci_version *ver, int to)
|
2002-03-09 05:10:06 +08:00
|
|
|
{
|
|
|
|
evt_read_remote_version_complete rp;
|
|
|
|
read_remote_version_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
2002-03-23 03:45:46 +08:00
|
|
|
cp.handle = handle;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-03-09 05:10:06 +08:00
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_READ_REMOTE_VERSION;
|
|
|
|
rq.event = EVT_READ_REMOTE_VERSION_COMPLETE;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = READ_REMOTE_VERSION_CP_SIZE;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = EVT_READ_REMOTE_VERSION_COMPLETE_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ver->manufacturer = btohs(rp.manufacturer);
|
|
|
|
ver->lmp_ver = rp.lmp_ver;
|
|
|
|
ver->lmp_subver = btohs(rp.lmp_subver);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_read_local_version(int dd, struct hci_version *ver, int to)
|
|
|
|
{
|
|
|
|
read_local_version_rp rp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
2002-04-12 02:19:58 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
2002-04-11 02:20:36 +08:00
|
|
|
rq.ogf = OGF_INFO_PARAM;
|
|
|
|
rq.ocf = OCF_READ_LOCAL_VERSION;
|
2002-03-09 05:10:06 +08:00
|
|
|
rq.rparam = &rp;
|
2002-04-11 02:20:36 +08:00
|
|
|
rq.rlen = READ_LOCAL_VERSION_RP_SIZE;
|
2002-03-09 05:10:06 +08:00
|
|
|
|
2003-01-24 17:41:07 +08:00
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
2002-03-09 05:10:06 +08:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ver->manufacturer = btohs(rp.manufacturer);
|
2002-03-26 03:12:16 +08:00
|
|
|
ver->hci_ver = rp.hci_ver;
|
2002-03-09 05:10:06 +08:00
|
|
|
ver->hci_rev = btohs(rp.hci_rev);
|
|
|
|
ver->lmp_ver = rp.lmp_ver;
|
|
|
|
ver->lmp_subver = btohs(rp.lmp_subver);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2002-04-12 02:19:58 +08:00
|
|
|
|
2002-08-22 17:19:29 +08:00
|
|
|
int hci_read_class_of_dev(int dd, uint8_t *cls, int to)
|
2002-04-12 02:19:58 +08:00
|
|
|
{
|
|
|
|
read_class_of_dev_rp rp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_READ_CLASS_OF_DEV;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = READ_CLASS_OF_DEV_RP_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-08-22 17:19:29 +08:00
|
|
|
memcpy(cls, rp.dev_class, 3);
|
2002-04-12 02:19:58 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2002-06-19 00:46:54 +08:00
|
|
|
|
2002-08-22 17:19:29 +08:00
|
|
|
int hci_write_class_of_dev(int dd, uint32_t cls, int to)
|
|
|
|
{
|
2002-08-23 01:17:57 +08:00
|
|
|
write_class_of_dev_cp cp;
|
|
|
|
struct hci_request rq;
|
2002-08-22 17:19:29 +08:00
|
|
|
|
2002-08-23 01:17:57 +08:00
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
cp.dev_class[0] = cls & 0xff;
|
|
|
|
cp.dev_class[1] = (cls >> 8) & 0xff;
|
|
|
|
cp.dev_class[2] = (cls >> 16) & 0xff;
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_WRITE_CLASS_OF_DEV;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = WRITE_CLASS_OF_DEV_CP_SIZE;
|
2003-01-24 17:41:07 +08:00
|
|
|
return hci_send_req(dd, &rq, to);
|
2002-08-22 17:19:29 +08:00
|
|
|
}
|
|
|
|
|
2002-12-15 21:18:53 +08:00
|
|
|
int hci_read_voice_setting(int dd, uint16_t *vs, int to)
|
|
|
|
{
|
|
|
|
read_voice_setting_rp rp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_READ_VOICE_SETTING;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = READ_VOICE_SETTING_RP_SIZE;
|
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*vs = rp.voice_setting;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_write_voice_setting(int dd, uint16_t vs, int to)
|
|
|
|
{
|
|
|
|
write_voice_setting_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
cp.voice_setting = vs;
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_WRITE_VOICE_SETTING;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = WRITE_VOICE_SETTING_CP_SIZE;
|
2003-01-24 17:41:07 +08:00
|
|
|
return hci_send_req(dd, &rq, to);
|
2002-12-15 21:18:53 +08:00
|
|
|
}
|
|
|
|
|
2002-08-22 00:38:15 +08:00
|
|
|
int hci_read_current_iac_lap(int dd, uint8_t *num_iac, uint8_t *lap, int to)
|
|
|
|
{
|
2002-08-23 01:17:57 +08:00
|
|
|
read_current_iac_lap_rp rp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_READ_CURRENT_IAC_LAP;
|
|
|
|
rq.rparam = &rp;
|
|
|
|
rq.rlen = READ_CURRENT_IAC_LAP_RP_SIZE;
|
2003-01-24 17:41:07 +08:00
|
|
|
|
|
|
|
if (hci_send_req(dd, &rq, to) < 0)
|
2002-08-23 01:17:57 +08:00
|
|
|
return -1;
|
2003-01-24 17:41:07 +08:00
|
|
|
|
2002-08-23 01:17:57 +08:00
|
|
|
if (rp.status) {
|
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
2003-01-24 17:41:07 +08:00
|
|
|
|
2002-08-23 01:17:57 +08:00
|
|
|
*num_iac = rp.num_current_iac;
|
2003-01-24 17:41:07 +08:00
|
|
|
memcpy(lap, rp.lap, rp.num_current_iac * 3);
|
2002-08-23 01:17:57 +08:00
|
|
|
return 0;
|
2002-08-22 00:38:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int hci_write_current_iac_lap(int dd, uint8_t num_iac, uint8_t *lap, int to)
|
|
|
|
{
|
2002-08-23 01:17:57 +08:00
|
|
|
write_current_iac_lap_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
memset(&cp, 0, sizeof(cp));
|
|
|
|
cp.num_current_iac = num_iac;
|
2003-01-24 17:41:07 +08:00
|
|
|
memcpy(&cp.lap, lap, num_iac * 3);
|
2002-08-23 01:17:57 +08:00
|
|
|
|
|
|
|
memset(&rq, 0, sizeof(rq));
|
|
|
|
rq.ogf = OGF_HOST_CTL;
|
|
|
|
rq.ocf = OCF_WRITE_CURRENT_IAC_LAP;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = WRITE_CURRENT_IAC_LAP_CP_SIZE;
|
2003-01-24 17:41:07 +08:00
|
|
|
return hci_send_req(dd, &rq, to);
|
2002-08-22 00:38:15 +08:00
|
|
|
}
|
2003-02-11 18:22:23 +08:00
|
|
|
|
|
|
|
int hci_authenticate_link(int dd, uint16_t handle, int to)
|
|
|
|
{
|
|
|
|
auth_requested_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
cp.handle = handle;
|
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_AUTH_REQUESTED;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = AUTH_REQUESTED_CP_SIZE;
|
|
|
|
return hci_send_req(dd, &rq, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
int hci_encrypt_link(int dd, uint16_t handle, int on, int to)
|
|
|
|
{
|
|
|
|
set_conn_encrypt_cp cp;
|
|
|
|
struct hci_request rq;
|
|
|
|
|
|
|
|
cp.handle = handle;
|
|
|
|
cp.encrypt = on;
|
|
|
|
rq.ogf = OGF_LINK_CTL;
|
|
|
|
rq.ocf = OCF_SET_CONN_ENCRYPT;
|
|
|
|
rq.cparam = &cp;
|
|
|
|
rq.clen = SET_CONN_ENCRYPT_CP_SIZE;
|
|
|
|
return hci_send_req(dd, &rq, to);
|
|
|
|
}
|