3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2015 Intel Corporation. All rights reserved.
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
30 #include <sys/socket.h>
35 #include "lib/bluetooth.h"
38 #include "src/shared/util.h"
39 #include "src/shared/tester.h"
40 #include "src/shared/queue.h"
41 #include "src/shared/att.h"
42 #include "src/shared/gatt-db.h"
44 #include "attrib/gattrib.h"
46 #include "profiles/input/hog-lib.h"
56 struct test_pdu *pdu_list;
65 unsigned int pdu_offset;
66 const struct test_data *data;
69 #define data(args...) ((const unsigned char[]) { args })
71 #define raw_pdu(args...) \
74 .data = g_memdup(data(args), sizeof(data(args))), \
75 .size = sizeof(data(args)), \
83 #define define_test(name, function, args...) \
85 const struct test_pdu pdus[] = { \
88 static struct test_data data; \
89 data.test_name = g_strdup(name); \
90 data.pdu_list = g_memdup(pdus, sizeof(pdus)); \
91 tester_add(name, &data, NULL, function, NULL); \
94 static void test_debug(const char *str, void *user_data)
96 const char *prefix = user_data;
98 tester_debug("%s%s", prefix, str);
101 static gboolean context_quit(gpointer user_data)
103 struct context *context = user_data;
105 if (context->process > 0)
106 g_source_remove(context->process);
108 if (context->source > 0)
109 g_source_remove(context->source);
111 bt_hog_unref(context->hog);
113 g_attrib_unref(context->attrib);
117 tester_test_passed();
122 static gboolean send_pdu(gpointer user_data)
124 struct context *context = user_data;
125 const struct test_pdu *pdu;
128 pdu = &context->data->pdu_list[context->pdu_offset++];
130 len = write(context->fd, pdu->data, pdu->size);
132 util_hexdump('<', pdu->data, len, test_debug, "hog: ");
134 g_assert_cmpint(len, ==, pdu->size);
136 context->process = 0;
138 if (!context->data->pdu_list[context->pdu_offset].valid)
139 #ifdef TIZEN_FEATURE_BLUEZ_MODIFY
140 g_idle_add(context_quit, context);
142 context_quit(context);
148 static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
151 struct context *context = user_data;
152 unsigned char buf[512];
153 const struct test_pdu *pdu;
157 pdu = &context->data->pdu_list[context->pdu_offset++];
159 if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
161 g_print("%s: cond %x\n", __func__, cond);
165 fd = g_io_channel_unix_get_fd(channel);
167 len = read(fd, buf, sizeof(buf));
171 util_hexdump('>', buf, len, test_debug, "hog: ");
173 g_assert_cmpint(len, ==, pdu->size);
175 g_assert(memcmp(buf, pdu->data, pdu->size) == 0);
177 context->process = g_idle_add(send_pdu, context);
182 static struct context *create_context(gconstpointer data)
184 struct context *context;
185 GIOChannel *channel, *att_io;
187 char name[] = "bluez-hog";
188 uint16_t vendor = 0x0002;
189 uint16_t product = 0x0001;
190 uint16_t version = 0x0001;
192 context = g_new0(struct context, 1);
193 err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
196 att_io = g_io_channel_unix_new(sv[0]);
198 g_io_channel_set_close_on_unref(att_io, TRUE);
200 context->attrib = g_attrib_new(att_io, 23, false);
201 g_assert(context->attrib);
203 g_io_channel_unref(att_io);
205 fd = open("/dev/null", O_WRONLY | O_CLOEXEC);
208 context->hog = bt_hog_new(fd, name, vendor, product, version, NULL);
209 g_assert(context->hog);
211 channel = g_io_channel_unix_new(sv[1]);
213 g_io_channel_set_close_on_unref(channel, TRUE);
214 g_io_channel_set_encoding(channel, NULL, NULL);
215 g_io_channel_set_buffered(channel, FALSE);
217 context->source = g_io_add_watch(channel,
218 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
219 test_handler, context);
220 g_assert(context->source > 0);
222 g_io_channel_unref(channel);
225 context->data = data;
230 static void test_hog(gconstpointer data)
232 struct context *context = create_context(data);
234 g_assert(bt_hog_attach(context->hog, context->attrib));
237 int main(int argc, char *argv[])
239 tester_init(&argc, &argv);
241 define_test("/TP/HGRF/RH/BV-01-I", test_hog,
242 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
243 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x04, 0x00, 0x12,
244 0x18, 0x05, 0x00, 0x08, 0x00, 0x12, 0x18),
245 raw_pdu(0x10, 0x09, 0x00, 0xff, 0xff, 0x00, 0x28),
246 raw_pdu(0x01, 0x10, 0x09, 0x00, 0x0a),
247 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),
248 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x02, 0x04, 0x00,
250 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x02, 0x28),
251 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
252 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x02, 0x28),
253 raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),
254 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x03, 0x28),
255 raw_pdu(0x09, 0x07, 0x07, 0x00, 0x02, 0x08, 0x00,
257 raw_pdu(0x0a, 0x04, 0x00),
258 raw_pdu(0x0b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
259 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
260 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
262 raw_pdu(0x0a, 0x08, 0x00),
263 raw_pdu(0x0b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
264 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
265 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
267 raw_pdu(0x0c, 0x04, 0x00, 0x16, 0x00),
268 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
269 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
270 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
272 raw_pdu(0x0c, 0x08, 0x00, 0x16, 0x00),
273 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
274 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
275 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
277 raw_pdu(0x0c, 0x04, 0x00, 0x2c, 0x00),
278 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
279 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
280 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13),
281 raw_pdu(0x0c, 0x08, 0x00, 0x2c, 0x00),
282 raw_pdu(0x0d, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
283 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
284 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13));
286 define_test("/TP/HGRF/RH/BV-08-I", test_hog,
287 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
288 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x05, 0x00, 0x12,
289 0x18, 0x06, 0x00, 0x0a, 0x00, 0x12, 0x18),
290 raw_pdu(0x10, 0x0b, 0x00, 0xff, 0xff, 0x00, 0x28),
291 raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),
292 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x03, 0x28),
293 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x0a, 0x04, 0x00,
295 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x02, 0x28),
296 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
297 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x02, 0x28),
298 raw_pdu(0x01, 0x08, 0x06, 0x00, 0x0a),
299 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x03, 0x28),
300 raw_pdu(0x09, 0x07, 0x08, 0x00, 0x0a, 0x09, 0x00,
302 raw_pdu(0x08, 0x04, 0x00, 0x05, 0x00, 0x03, 0x28),
303 raw_pdu(0x01, 0x08, 0x04, 0x00, 0x0a),
304 raw_pdu(0x08, 0x09, 0x00, 0x0a, 0x00, 0x03, 0x28),
305 raw_pdu(0x01, 0x08, 0x09, 0x00, 0x0a),
306 raw_pdu(0x0a, 0x04, 0x00),
307 raw_pdu(0x0b, 0xee, 0xee, 0xff, 0xff),
308 raw_pdu(0x04, 0x05, 0x00, 0x05, 0x00),
309 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x08, 0x29),
310 raw_pdu(0x0a, 0x09, 0x00),
311 raw_pdu(0x0b, 0xff, 0xff, 0xee, 0xee),
312 raw_pdu(0x04, 0x0a, 0x00, 0x0a, 0x00),
313 raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x08, 0x29),
314 raw_pdu(0x0a, 0x05, 0x00),
315 raw_pdu(0x0b, 0x01, 0x03),
316 raw_pdu(0x0a, 0x0a, 0x00),
317 raw_pdu(0x0b, 0x02, 0x03));
319 define_test("/TP/HGRF/RH/BV-09-I", test_hog,
320 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
321 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x04, 0x00, 0x12,
322 0x18, 0x05, 0x00, 0x08, 0x00, 0x12, 0x18),
323 raw_pdu(0x10, 0x09, 0x00, 0xff, 0xff, 0x00, 0x28),
324 raw_pdu(0x01, 0x10, 0x09, 0x00, 0x0a),
325 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x03, 0x28),
326 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x02, 0x04, 0x00,
328 raw_pdu(0x08, 0x01, 0x00, 0x04, 0x00, 0x02, 0x28),
329 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
330 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x02, 0x28),
331 raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),
332 raw_pdu(0x08, 0x05, 0x00, 0x08, 0x00, 0x03, 0x28),
333 raw_pdu(0x09, 0x07, 0x07, 0x00, 0x02, 0x08, 0x00,
335 raw_pdu(0x0a, 0x04, 0x00),
336 raw_pdu(0x0b, 0x01, 0x11, 0x00, 0x01),
337 raw_pdu(0x0a, 0x08, 0x00),
338 raw_pdu(0x0b, 0x01, 0x11, 0x00, 0x01));
340 define_test("/TP/HGRF/RH/BV-06-I", test_hog,
341 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
342 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x05, 0x00, 0x12,
343 0x18, 0x06, 0x00, 0x0a, 0x00, 0x12, 0x18),
344 raw_pdu(0x10, 0x0b, 0x00, 0xff, 0xff, 0x00, 0x28),
345 raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),
346 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x03, 0x28),
347 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x0a, 0x04, 0x00,
349 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x02, 0x28),
350 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
351 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x02, 0x28),
352 raw_pdu(0x01, 0x08, 0x06, 0x00, 0x0a),
353 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x03, 0x28),
354 raw_pdu(0x09, 0x07, 0x08, 0x00, 0x0a, 0x09, 0x00,
356 raw_pdu(0x08, 0x04, 0x00, 0x05, 0x00, 0x03, 0x28),
357 raw_pdu(0x01, 0x08, 0x05, 0x00, 0x0a),
358 raw_pdu(0x08, 0x09, 0x00, 0x0a, 0x00, 0x03, 0x28),
359 raw_pdu(0x01, 0x08, 0x09, 0x00, 0x0a),
360 raw_pdu(0x0a, 0x04, 0x00),
361 raw_pdu(0x0b, 0xee, 0xee, 0xff, 0xff),
362 raw_pdu(0x04, 0x05, 0x00, 0x05, 0x00),
363 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x08, 0x29),
364 raw_pdu(0x0a, 0x09, 0x00),
365 raw_pdu(0x0b, 0xff, 0xff, 0xee, 0xee),
366 raw_pdu(0x04, 0x0a, 0x00, 0x0a, 0x00),
367 raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x08, 0x29),
368 raw_pdu(0x0a, 0x05, 0x00),
369 raw_pdu(0x0b, 0x01, 0x02),
370 raw_pdu(0x0a, 0x0a, 0x00),
371 raw_pdu(0x0b, 0x02, 0x02));
373 define_test("/TP/HGCF/RH/BV-01-I", test_hog,
374 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
375 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x06, 0x00, 0x12,
376 0x18, 0x07, 0x00, 0x0c, 0x00, 0x12, 0x18),
377 raw_pdu(0x10, 0x0d, 0x00, 0xff, 0xff, 0x00, 0x28),
378 raw_pdu(0x01, 0x10, 0x0d, 0x00, 0x0a),
379 raw_pdu(0x08, 0x01, 0x00, 0x06, 0x00, 0x03, 0x28),
380 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x1a, 0x04, 0x00,
382 raw_pdu(0x08, 0x01, 0x00, 0x06, 0x00, 0x02, 0x28),
383 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
384 raw_pdu(0x08, 0x07, 0x00, 0x0c, 0x00, 0x02, 0x28),
385 raw_pdu(0x01, 0x08, 0x07, 0x00, 0x0a),
386 raw_pdu(0x08, 0x07, 0x00, 0x0c, 0x00, 0x03, 0x28),
387 raw_pdu(0x09, 0x07, 0x09, 0x00, 0x1a, 0x0a, 0x00,
389 raw_pdu(0x08, 0x04, 0x00, 0x06, 0x00, 0x03, 0x28),
390 raw_pdu(0x01, 0x08, 0x04, 0x00, 0x0a),
391 raw_pdu(0x08, 0x0a, 0x00, 0x0c, 0x00, 0x03, 0x28),
392 raw_pdu(0x01, 0x08, 0x0a, 0x00, 0x0a),
393 raw_pdu(0x0a, 0x04, 0x00),
394 raw_pdu(0x0b, 0xed, 0x00),
395 raw_pdu(0x04, 0x05, 0x00, 0x06, 0x00),
396 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x02, 0x29,
397 0x06, 0x00, 0x08, 0x29),
398 raw_pdu(0x0a, 0x0a, 0x00),
399 raw_pdu(0x0b, 0xed, 0x00),
400 raw_pdu(0x04, 0x0b, 0x00, 0x0c, 0x00),
401 raw_pdu(0x05, 0x01, 0x0b, 0x00, 0x02, 0x29,
402 0x0c, 0x00, 0x08, 0x29),
403 raw_pdu(0x0a, 0x06, 0x00),
404 raw_pdu(0x0b, 0x01, 0x01),
405 raw_pdu(0x0a, 0x0c, 0x00),
406 raw_pdu(0x0b, 0x02, 0x01),
407 raw_pdu(0x0a, 0x05, 0x00),
408 raw_pdu(0x0b, 0x00, 0x00),
409 raw_pdu(0x0a, 0x0b, 0x00),
410 raw_pdu(0x0b, 0x00, 0x00),
411 raw_pdu(0x12, 0x05, 0x00, 0x01, 0x00),
413 raw_pdu(0x12, 0x0b, 0x00, 0x01, 0x00),
416 define_test("/TP/HGRF/RH/BV-02-I", test_hog,
417 raw_pdu(0x10, 0x01, 0x00, 0xff, 0xff, 0x00, 0x28),
418 raw_pdu(0x11, 0x06, 0x01, 0x00, 0x05, 0x00, 0x12,
419 0x18, 0x06, 0x00, 0x0a, 0x00, 0x12, 0x18),
420 raw_pdu(0x10, 0x0b, 0x00, 0xff, 0xff, 0x00, 0x28),
421 raw_pdu(0x01, 0x10, 0x0b, 0x00, 0x0a),
422 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x03, 0x28),
423 raw_pdu(0x09, 0x07, 0x03, 0x00, 0x02, 0x04, 0x00,
425 raw_pdu(0x08, 0x01, 0x00, 0x05, 0x00, 0x02, 0x28),
426 raw_pdu(0x01, 0x08, 0x01, 0x00, 0x0a),
427 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x02, 0x28),
428 raw_pdu(0x01, 0x08, 0x06, 0x00, 0x0a),
429 raw_pdu(0x08, 0x06, 0x00, 0x0a, 0x00, 0x03, 0x28),
430 raw_pdu(0x09, 0x07, 0x08, 0x00, 0x02, 0x09, 0x00,
432 raw_pdu(0x08, 0x04, 0x00, 0x05, 0x00, 0x03, 0x28),
433 raw_pdu(0x01, 0x08, 0x04, 0x00, 0x0a),
434 raw_pdu(0x08, 0x09, 0x00, 0x0a, 0x00, 0x03, 0x28),
435 raw_pdu(0x01, 0x08, 0x09, 0x00, 0x0a),
436 raw_pdu(0x0a, 0x04, 0x00),
437 raw_pdu(0x0b, 0x01, 0x02, 0x03),
438 raw_pdu(0x04, 0x05, 0x00, 0x05, 0x00),
439 raw_pdu(0x05, 0x01, 0x05, 0x00, 0x07, 0x29),
440 raw_pdu(0x0a, 0x09, 0x00),
441 raw_pdu(0x0b, 0x01, 0x02, 0x03),
442 raw_pdu(0x04, 0x0a, 0x00, 0x0a, 0x00),
443 raw_pdu(0x05, 0x01, 0x0a, 0x00, 0x07, 0x29),
444 raw_pdu(0x0a, 0x05, 0x00),
445 raw_pdu(0x0b, 0x19, 0x2a),
446 raw_pdu(0x0a, 0x0a, 0x00),
447 raw_pdu(0x0b, 0x19, 0x2a));