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
39
static uint16_t seqnum = 0x0000;
43
static struct ubcsp_packet send_packet;
44
static uint8_t send_buffer[512];
46
static struct ubcsp_packet receive_packet;
47
static uint8_t receive_buffer[512];
49
int csr_open_bcsp(char *device)
52
uint8_t delay, activity = 0x00;
56
device = "/dev/ttyS0";
58
fd = open(device, O_RDWR | O_NOCTTY);
60
fprintf(stderr, "Can't open serial port: %s (%d)\n",
61
strerror(errno), errno);
65
tcflush(fd, TCIOFLUSH);
67
if (tcgetattr(fd, &ti) < 0) {
68
fprintf(stderr, "Can't get port settings: %s (%d)\n",
69
strerror(errno), errno);
77
ti.c_cflag &= ~CRTSCTS;
79
ti.c_cflag &= ~PARODD;
82
ti.c_cflag &= ~CSTOPB;
87
cfsetospeed(&ti, B38400);
89
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
90
fprintf(stderr, "Can't change port settings: %s (%d)\n",
91
strerror(errno), errno);
96
tcflush(fd, TCIOFLUSH);
98
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK) < 0) {
99
fprintf(stderr, "Can't set non blocking mode: %s (%d)\n",
100
strerror(errno), errno);
105
memset(&send_packet, 0, sizeof(send_packet));
106
memset(&receive_packet, 0, sizeof(receive_packet));
110
send_packet.length = 512;
111
send_packet.payload = send_buffer;
113
receive_packet.length = 512;
114
receive_packet.payload = receive_buffer;
116
ubcsp_receive_packet(&receive_packet);
119
delay = ubcsp_poll(&activity);
121
if (activity & UBCSP_PACKET_RECEIVED)
127
if (timeout++ > 100) {
128
fprintf(stderr, "Initialization timed out\n");
137
void put_uart(uint8_t ch)
139
if (write(fd, &ch, 1) < 0)
140
fprintf(stderr, "UART write error\n");
143
uint8_t get_uart(uint8_t *ch)
145
int res = read(fd, ch, 1);
146
return res > 0 ? res : 0;
149
static int do_command(uint16_t command, uint16_t seqnum, uint16_t varid, uint8_t *value, uint16_t length)
151
unsigned char cp[254], rp[254];
154
uint8_t delay, activity = 0x00;
155
int timeout = 0, sent = 0;
157
size = (length < 8) ? 9 : ((length + 1) / 2) + 5;
159
cmd[0] = command & 0xff;
160
cmd[1] = command >> 8;
161
cmd[2] = size & 0xff;
163
cmd[4] = seqnum & 0xff;
164
cmd[5] = seqnum >> 8;
165
cmd[6] = varid & 0xff;
170
memset(cp, 0, sizeof(cp));
173
cp[2] = (size * 2) + 1;
175
memcpy(cp + 4, cmd, sizeof(cmd));
176
memcpy(cp + 14, value, length);
178
receive_packet.length = 512;
179
ubcsp_receive_packet(&receive_packet);
181
send_packet.channel = 5;
182
send_packet.reliable = 1;
183
send_packet.length = (size * 2) + 4;
184
memcpy(send_packet.payload, cp, (size * 2) + 4);
186
ubcsp_send_packet(&send_packet);
189
delay = ubcsp_poll(&activity);
191
if (activity & UBCSP_PACKET_SENT) {
193
case CSR_VARID_COLD_RESET:
194
case CSR_VARID_WARM_RESET:
195
case CSR_VARID_COLD_HALT:
196
case CSR_VARID_WARM_HALT:
204
if (activity & UBCSP_PACKET_RECEIVED) {
205
if (sent && receive_packet.channel == 5 &&
206
receive_packet.payload[0] == 0xff) {
207
memcpy(rp, receive_packet.payload,
208
receive_packet.length);
212
receive_packet.length = 512;
213
ubcsp_receive_packet(&receive_packet);
220
if (timeout++ > 100) {
221
fprintf(stderr, "Operation timed out\n");
227
if (rp[0] != 0xff || rp[2] != 0xc2) {
232
if ((rp[11] + (rp[12] << 8)) != 0) {
237
memcpy(value, rp + 13, length);
242
int csr_read_bcsp(uint16_t varid, uint8_t *value, uint16_t length)
244
return do_command(0x0000, seqnum++, varid, value, length);
247
int csr_write_bcsp(uint16_t varid, uint8_t *value, uint16_t length)
249
return do_command(0x0002, seqnum++, varid, value, length);
252
void csr_close_bcsp(void)