summaryrefslogtreecommitdiffstats
path: root/bt-vendor/hci.c
blob: c83d3e147d1ca1222b41ca5c5b07b8764685b616 (plain)
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
/*
 * Copyright (C) 2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>

#define LOG_TAG "gta04_bt_vendor"
#include <cutils/log.h>

#include <bt_vendor_lib.h>
#include <bt_hci_bdroid.h>

#include "gta04_bt_vendor.h"

int gta04_bt_vendor_hci_cmd_prepare(void *buffer, size_t length)
{
	HC_BT_HDR *header;

	if (buffer == NULL || length < BT_HC_HDR_SIZE)
		return -EINVAL;

	header = (HC_BT_HDR *) buffer;
	header->event = MSG_STACK_TO_HC_HCI_CMD;
	header->len = length - BT_HC_HDR_SIZE;
	header->offset = 0;
	header->layer_specific = 0;

	return 0;
}

int gta04_bt_vendor_hci_preamble(void *buffer, size_t length,
	unsigned short cmd)
{
	unsigned char *p;

	if (buffer == NULL || length < HCI_CMD_PREAMBLE_SIZE)
		return -EINVAL;

	p = (unsigned char *) buffer;

	p[0] = cmd & 0xff;
	p[1] = cmd >> 8;
	p[2] = length - HCI_CMD_PREAMBLE_SIZE;

	return 0;
}

int gta04_bt_vendor_hci_bccmd(void *buffer, size_t length,
	unsigned short pdu, unsigned short varid)
{
	static unsigned bccmd_seq = 0;
	size_t bccmd_words_count = HCI_BCCMD_WORDS_COUNT;
	size_t bccmd_length = bccmd_words_count * sizeof(unsigned short) + 1;
	unsigned char *p;

	if (buffer == NULL || length < bccmd_length)
		return -EINVAL;

	memset(buffer, 0, 11);

	bccmd_words_count = (length - 1) / 2;

	p = (unsigned char *) buffer;

	// Descriptor
	p[0] = HCI_BCCMD_DESCRIPTOR;

	// Payload
	p[1] = pdu & 0xff;
	p[2] = pdu >> 8;
	p[3] = bccmd_words_count & 0xff;
	p[4] = bccmd_words_count >> 8;
	p[5] = bccmd_seq & 0xff;
	p[6] = bccmd_seq >> 8;
	p[7] = varid & 0xff;
	p[8] = varid >> 8;
	p[9] = HCI_BCCMD_STAT_OK & 0xff;
	p[10] = HCI_BCCMD_STAT_OK >> 8;

	bccmd_seq++;

	return 0;
}

unsigned short gta04_bt_vendor_hci_bccmd_speed(speed_t speed)
{
	switch (speed) {
		case B0:
		case B50:
		case B75:
		case B110:
		case B134:
		case B150:
		case B200:
			return 0;
		case B300:
			return 1;
		case B600:
			return 3;
		case B1200:
			return 5;
		case B1800:
			return 7;
		case B2400:
			return 10;
		case B4800:
			return 20;
		case B9600:
			return 39;
		case B19200:
			return 79;
		case B38400:
			return 157;
		case B57600:
			return 236;
		case B115200:
			return 472;
		case B230400:
			return 944;
		case B460800:
			return 1887;
		case B500000:
			return 2048;
		case B576000:
			return 2359;
		case B921600:
			return 3775;
		case B1000000:
			return 4096;
		case B1152000:
			return 4719;
		case B1500000:
			return 6144;
		case B2000000:
			return 8192;
		case B2500000:
			return 10240;
		case B3000000:
			return 12288;
		case B3500000:
			return 14336;
		case B4000000:
			return 16384;
		default:
			return 12288;
	}
}

int gta04_bt_vendor_hci_h4_write(void *buffer, size_t length)
{
	void *data;
	size_t size;
	unsigned char *p;
	int rc;

	if (buffer == NULL || length < BT_HC_HDR_SIZE + HCI_CMD_PREAMBLE_SIZE)
		return -EINVAL;

	p = (unsigned char *) buffer + BT_HC_HDR_SIZE - 1;

	data = (void *) p;
	size = length - BT_HC_HDR_SIZE + 1;

	p[0] = HCI_H4_TYPE_CMD;

	rc = gta04_bt_vendor_serial_write(data, size);
	if (rc < 0) {
		ALOGE("%s: Writing to serial failed", __func__);
		return -1;
	}

	return 0;
}

int gta04_bt_vendor_hci_h4_read_event(void *buffer, size_t length,
	unsigned char event)
{
	struct timeval timeout;
	unsigned char cleanup[256];
	unsigned char byte;
	unsigned char type = 0;
	unsigned char size = 0;
	unsigned char *p = NULL;
	unsigned int count;
	unsigned int limit;
	int failures;
	int skip = 0;
	fd_set fds;
	int rc;

	if (gta04_bt_vendor == NULL || gta04_bt_vendor->serial_fd < 0)
		return -1;

	failures = 0;

	while (1) {
		FD_ZERO(&fds);
		FD_SET(gta04_bt_vendor->serial_fd, &fds);

		timeout.tv_sec = 5;
		timeout.tv_usec = 0;

		rc = select(gta04_bt_vendor->serial_fd + 1, &fds, NULL, NULL, &timeout);
		if (rc < 0) {
			ALOGE("%s: Polling failed", __func__);

			if (failures) {
				return -1;
			} else {
				failures++;
				continue;
			}
		} else if (rc == 0) {
			ALOGE("%s: Polling timed out", __func__);
			return -1;
		}

		if (failures > 10) {
			ALOGE("%s: Too many failures", __func__);
			return -1;
		}

		if (p == NULL) {
			// Read single bytes first

			rc = gta04_bt_vendor_serial_read(&byte, sizeof(byte));
			if (rc < (int) sizeof(byte)) {
				ALOGE("%s: Reading from serial failed", __func__);
				return -1;
			}

			if (type != HCI_H4_TYPE_EVENT) {
				if (byte != HCI_H4_TYPE_EVENT) {
					ALOGD("%s: Ignored response with type: %d", __func__, byte);
					gta04_bt_vendor_serial_read(&cleanup, sizeof(cleanup));
					continue;
				}

				type = byte;
			} else {
				if (byte != event) {
					ALOGD("%s: Ignored response with event: 0x%x", __func__, byte);
					gta04_bt_vendor_serial_read(&cleanup, sizeof(cleanup));
					type = 0;
					continue;
				}

				rc = gta04_bt_vendor_serial_read(&size, sizeof(size));
				if (rc <= 0) {
					ALOGE("%s: Reading from serial failed", __func__);
					failures++;
					continue;
				} else if (failures) {
					failures = 0;
				}


				if (size == 0)
					return 0;

				if (buffer == NULL || length == 0) {
					count = 0;

					while (count < size) {
						rc = gta04_bt_vendor_serial_read(&cleanup, size - count);
						if (rc <= 0) {
							ALOGE("%s: Reading from serial failed", __func__);
							failures++;
							break;
						} else if (failures) {
							failures = 0;
						}

						count += rc;
					}

					if (rc <= 0)
						continue;

					return 0;
				}

				p = (unsigned char *) buffer;
			}
		}

		if (p != NULL) {
			// Make sure to read from the start in case of failure
			p = (unsigned char *) buffer;

			if (size > length) {
				ALOGE("%s: Provided buffer length is too small for size: %d/%d bytes", __func__, length, size);

				limit = length;
			} else {
				limit = size;
			}

			count = 0;

			while (count < limit) {
				rc = gta04_bt_vendor_serial_read(p, limit - count);
				if (rc <= 0) {
					ALOGE("%s: Reading from serial failed", __func__);
					failures++;
					break;
				} else if (failures) {
					failures = 0;
				}

				p += rc;
				count += rc;
			}

			if (rc <= 0)
				continue;

			if (limit < size) {
				limit = size;

				while (count < limit) {
					rc = gta04_bt_vendor_serial_read(&cleanup, limit - count);
					if (rc <= 0) {
						ALOGE("%s: Reading from serial failed", __func__);
						failures++;
						break;
					} else if (failures) {
						failures = 0;
					}

					count += rc;
				}
			}

			if (rc <= 0)
				continue;

			return 0;
		}
	}

	return 0;
}