3
* BlueZ - Bluetooth protocol stack for Linux
5
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31
#include <sys/socket.h>
33
#include <bluetooth/bluetooth.h>
34
#include <bluetooth/hci.h>
35
#include <bluetooth/hci_lib.h>
39
static uint16_t seqnum = 0x0000;
43
int csr_open_hci(char *device)
45
struct hci_dev_info di;
46
struct hci_version ver;
50
dev = hci_devid(device);
52
fprintf(stderr, "Device not available\n");
57
dd = hci_open_dev(dev);
59
fprintf(stderr, "Can't open device hci%d: %s (%d)\n",
60
dev, strerror(errno), errno);
64
if (hci_devinfo(dev, &di) < 0) {
65
fprintf(stderr, "Can't get device info for hci%d: %s (%d)\n",
66
dev, strerror(errno), errno);
71
if (hci_read_local_version(dd, &ver, 1000) < 0) {
72
fprintf(stderr, "Can't read version info for hci%d: %s (%d)\n",
73
dev, strerror(errno), errno);
78
if (ver.manufacturer != 10) {
79
fprintf(stderr, "Unsupported manufacturer\n");
87
static int do_command(uint16_t command, uint16_t seqnum, uint16_t varid, uint8_t *value, uint16_t length)
89
unsigned char cp[254], rp[254];
90
struct hci_request rq;
94
size = (length < 8) ? 9 : ((length + 1) / 2) + 5;
96
cmd[0] = command & 0xff;
97
cmd[1] = command >> 8;
100
cmd[4] = seqnum & 0xff;
101
cmd[5] = seqnum >> 8;
102
cmd[6] = varid & 0xff;
107
memset(cp, 0, sizeof(cp));
109
memcpy(cp + 1, cmd, sizeof(cmd));
110
memcpy(cp + 11, value, length);
113
case CSR_VARID_COLD_RESET:
114
case CSR_VARID_WARM_RESET:
115
case CSR_VARID_COLD_HALT:
116
case CSR_VARID_WARM_HALT:
117
return hci_send_cmd(dd, OGF_VENDOR_CMD, 0x00, (size * 2) + 1, cp);
120
memset(&rq, 0, sizeof(rq));
121
rq.ogf = OGF_VENDOR_CMD;
123
rq.event = EVT_VENDOR;
125
rq.clen = (size * 2) + 1;
127
rq.rlen = sizeof(rp);
129
if (hci_send_req(dd, &rq, 2000) < 0)
137
if ((rp[9] + (rp[10] << 8)) != 0) {
142
memcpy(value, rp + 11, length);
147
int csr_read_hci(uint16_t varid, uint8_t *value, uint16_t length)
149
return do_command(0x0000, seqnum++, varid, value, length);
152
int csr_write_hci(uint16_t varid, uint8_t *value, uint16_t length)
154
return do_command(0x0002, seqnum++, varid, value, length);
157
void csr_close_hci(void)