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 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2011 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#include <glib-object.h>
#include "gusb-context.h"
#include "gusb-context-private.h"
#include "gusb-device.h"
static void
gusb_device_func (void)
{
GError *error = NULL;
GPtrArray *array;
GUsbContext *ctx;
GUsbDevice *device;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
g_assert (ctx != NULL);
g_usb_context_set_debug (ctx, G_LOG_LEVEL_ERROR);
array = g_usb_context_get_devices (ctx);
g_assert (array != NULL);
g_assert_cmpint (array->len, >, 0);
device = G_USB_DEVICE (g_ptr_array_index (array, 0));
g_assert_cmpint (g_usb_device_get_vid (device), >, 0x0000);
g_assert_cmpint (g_usb_device_get_pid (device), >, 0x0000);
g_ptr_array_unref (array);
}
static void
gusb_context_lookup_func (void)
{
GUsbContext *ctx = NULL;
GError *error = NULL;
const gchar *tmp;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
g_assert (ctx != NULL);
tmp = _g_usb_context_lookup_vendor (ctx, 0x04d8, &error);
g_assert_no_error (error);
g_assert_cmpstr (tmp, ==, "Microchip Technology, Inc.");
tmp = _g_usb_context_lookup_product (ctx, 0x04d8, 0xf8da, &error);
g_assert_no_error (error);
g_assert_cmpstr (tmp, ==, "Hughski Ltd. ColorHug");
g_object_unref (ctx);
}
static void
gusb_context_func (void)
{
GUsbContext *ctx;
GError *error = NULL;
GPtrArray *array;
guint old_number_of_devices;
guint8 bus, address;
GUsbDevice *device;
gchar *manufacturer;
gchar *product;
guint i;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
g_assert (ctx != NULL);
g_usb_context_set_debug (ctx, G_LOG_LEVEL_ERROR);
/* coldplug, and ensure we got some devices */
array = g_usb_context_get_devices (ctx);
g_assert (array != NULL);
g_assert_cmpint (array->len, >, 0);
old_number_of_devices = array->len;
/* Print a list (also exercising various bits of g_usb_device) */
g_print ("\n");
for (i = 0; i < array->len; i++) {
device = G_USB_DEVICE (g_ptr_array_index (array, i));
g_assert_cmpint (g_usb_device_get_vid (device), >, 0x0000);
g_assert_cmpint (g_usb_device_get_pid (device), >, 0x0000);
/* Needed for g_usb_device_get_string_descriptor below,
not error checked to allow running basic tests without
needing r/w rights on /dev/bus/usb nodes */
g_usb_context_set_debug (ctx, 0);
g_usb_device_open (device, NULL);
g_usb_context_set_debug (ctx, G_LOG_LEVEL_ERROR);
/* We don't error check these as not all devices have these
(and the device_open may have failed). */
manufacturer = g_usb_device_get_string_descriptor (device,
g_usb_device_get_manufacturer_index (device),
NULL);
product = g_usb_device_get_string_descriptor (device,
g_usb_device_get_product_index (device),
NULL);
g_usb_device_close (device, NULL);
g_print ("Found %04x:%04x, %s %s\n",
g_usb_device_get_vid (device),
g_usb_device_get_pid (device),
manufacturer ? manufacturer : "",
product ? product : "");
g_free (manufacturer);
g_free (product);
}
g_ptr_array_unref (array);
/* coldplug again, and ensure we did not duplicate devices */
array = g_usb_context_get_devices (ctx);
g_assert (array != NULL);
g_assert_cmpint (array->len, ==, old_number_of_devices);
device = G_USB_DEVICE (g_ptr_array_index (array, 0));
bus = g_usb_device_get_bus (device);
address = g_usb_device_get_address (device);
g_ptr_array_unref (array);
/* ensure we can search for the same device */
device = g_usb_context_find_by_bus_address (ctx,
bus,
address,
&error);
g_assert_no_error (error);
g_assert (device != NULL);
g_assert_cmpint (bus, ==, g_usb_device_get_bus (device));
g_assert_cmpint (address, ==, g_usb_device_get_address (device));
g_object_unref (device);
/* get a device that can't exist */
device = g_usb_context_find_by_vid_pid (ctx,
0xffff,
0xffff,
&error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NO_DEVICE);
g_assert (device == NULL);
g_clear_error (&error);
g_object_unref (ctx);
}
static void
gusb_device_huey_func (void)
{
GUsbContext *ctx;
GError *error = NULL;
GUsbDevice *device;
gboolean ret;
GCancellable *cancellable = NULL;
const gchar request[8] = { 0x0e, 'G', 'r', 'M', 'b', 'k', 'e', 'd' };
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
g_assert (ctx != NULL);
g_usb_context_set_debug (ctx, G_LOG_LEVEL_ERROR);
/* coldplug, and get the huey */
device = g_usb_context_find_by_vid_pid (ctx,
0x0971,
0x2005,
&error);
if (device == NULL &&
error->domain == G_USB_DEVICE_ERROR &&
error->code == G_USB_DEVICE_ERROR_NO_DEVICE) {
g_print ("No device detected!\n");
g_error_free (error);
goto out;
}
g_assert_no_error (error);
g_assert (device != NULL);
/* close not opened */
ret = g_usb_device_close (device, &error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_OPEN);
g_assert (!ret);
g_clear_error (&error);
/* open */
ret = g_usb_device_open (device, &error);
g_assert_no_error (error);
g_assert (ret);
/* open opened */
ret = g_usb_device_open (device, &error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_ALREADY_OPEN);
g_assert (!ret);
g_clear_error (&error);
/* claim interface 0 */
ret = g_usb_device_claim_interface (device, 0x00,
G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
&error);
g_assert_no_error (error);
g_assert (ret);
/* do a request (unlock) */
ret = g_usb_device_control_transfer (device,
G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
G_USB_DEVICE_REQUEST_TYPE_CLASS,
G_USB_DEVICE_RECIPIENT_INTERFACE,
0x09, /* request */
0x0200, /* value */
0, /* index */
(guint8*) request,
8,
NULL,
2000,
cancellable,
&error);
g_assert_no_error (error);
g_assert (ret);
/* do a request we know is going to fail */
ret = g_usb_device_control_transfer (device,
G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
G_USB_DEVICE_REQUEST_TYPE_CLASS,
G_USB_DEVICE_RECIPIENT_INTERFACE,
0x09, /* request */
0x0200, /* value */
0, /* index */
(guint8*) request,
8,
NULL,
2000,
cancellable,
&error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_SUPPORTED);
g_assert (!ret);
g_clear_error (&error);
/* release interface 0 */
ret = g_usb_device_release_interface (device, 0x00,
G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
&error);
g_assert_no_error (error);
g_assert (ret);
/* close */
ret = g_usb_device_close (device, &error);
g_assert_no_error (error);
g_assert (ret);
g_object_unref (device);
out:
g_object_unref (ctx);
}
typedef struct {
guint8 *buffer;
guint buffer_len;
GMainLoop *loop;
} GUsbDeviceAsyncHelper;
static void
g_usb_device_print_data (const gchar *title,
const guchar *data,
gsize length)
{
guint i;
if (g_strcmp0 (title, "request") == 0)
g_print ("%c[%dm", 0x1B, 31);
if (g_strcmp0 (title, "reply") == 0)
g_print ("%c[%dm", 0x1B, 34);
g_print ("%s\t", title);
for (i=0; i< length; i++)
g_print ("%02x [%c]\t",
data[i],
g_ascii_isprint (data[i]) ? data[i] : '?');
g_print ("%c[%dm\n", 0x1B, 0);
}
static void
g_usb_test_button_pressed_cb (GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
gboolean ret;
GError *error = NULL;
GUsbDeviceAsyncHelper *helper = (GUsbDeviceAsyncHelper *) user_data;
ret = g_usb_device_interrupt_transfer_finish (G_USB_DEVICE (source_object),
res, &error);
if (!ret) {
g_error ("%s", error->message);
g_error_free (error);
return;
}
g_usb_device_print_data ("button press",
helper->buffer,
helper->buffer_len);
g_main_loop_quit (helper->loop);
g_free (helper->buffer);
}
static void
gusb_device_munki_func (void)
{
GError *error = NULL;
GUsbContext *ctx;
GUsbDevice *device;
gboolean ret;
GCancellable *cancellable = NULL;
guint8 request[24];
GUsbDeviceAsyncHelper *helper;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
g_assert (ctx != NULL);
g_usb_context_set_debug (ctx, G_LOG_LEVEL_ERROR);
/* coldplug, and get the ColorMunki */
device = g_usb_context_find_by_vid_pid (ctx,
0x0971,
0x2007,
&error);
if (device == NULL &&
error->domain == G_USB_DEVICE_ERROR &&
error->code == G_USB_DEVICE_ERROR_NO_DEVICE) {
g_print ("No device detected!\n");
g_error_free (error);
goto out;
}
g_assert_no_error (error);
g_assert (device != NULL);
/* close not opened */
ret = g_usb_device_close (device, &error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_NOT_OPEN);
g_assert (!ret);
g_clear_error (&error);
/* open */
ret = g_usb_device_open (device, &error);
g_assert_no_error (error);
g_assert (ret);
/* open opened */
ret = g_usb_device_open (device, &error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_ALREADY_OPEN);
g_assert (!ret);
g_clear_error (&error);
/* claim interface 0 */
ret = g_usb_device_claim_interface (device, 0x00,
G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
&error);
g_assert_no_error (error);
g_assert (ret);
/* do a request (get chip id) */
ret = g_usb_device_control_transfer (device,
G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
G_USB_DEVICE_REQUEST_TYPE_VENDOR,
G_USB_DEVICE_RECIPIENT_DEVICE,
0x86, /* request */
0x0000, /* value */
0, /* index */
(guint8*) request,
24,
NULL,
2000,
cancellable,
&error);
g_assert_no_error (error);
g_assert (ret);
/* do a request we know is going to fail */
ret = g_usb_device_control_transfer (device,
G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
G_USB_DEVICE_REQUEST_TYPE_CLASS,
G_USB_DEVICE_RECIPIENT_INTERFACE,
0x00, /* request */
0x0200, /* value */
0, /* index */
(guint8*) request,
8,
NULL,
100,
cancellable,
&error);
g_assert_error (error,
G_USB_DEVICE_ERROR,
G_USB_DEVICE_ERROR_TIMED_OUT);
g_assert (!ret);
g_clear_error (&error);
/* do async read of button event */
helper = g_slice_new0 (GUsbDeviceAsyncHelper);
helper->buffer_len = 8;
helper->buffer = g_new0 (guint8, helper->buffer_len);
helper->loop = g_main_loop_new (NULL, FALSE);
g_usb_device_interrupt_transfer_async (device,
0x83,
helper->buffer,
8,
30*1000,
cancellable, /* TODO; use GCancellable */
g_usb_test_button_pressed_cb,
helper);
g_main_loop_run (helper->loop);
g_main_loop_unref (helper->loop);
/* release interface 0 */
ret = g_usb_device_release_interface (device, 0x00,
G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
&error);
g_assert_no_error (error);
g_assert (ret);
/* close */
ret = g_usb_device_close (device, &error);
g_assert_no_error (error);
g_assert (ret);
g_object_unref (device);
out:
g_object_unref (ctx);
}
static void
gusb_device_ch2_func (void)
{
GError *error = NULL;
GUsbContext *ctx;
GUsbDevice *device;
gboolean ret;
gchar *tmp = NULL;
guint8 idx;
ctx = g_usb_context_new (&error);
g_assert_no_error (error);
g_assert (ctx != NULL);
g_usb_context_set_debug (ctx, G_LOG_LEVEL_ERROR);
/* coldplug, and get the ColorHug */
device = g_usb_context_find_by_vid_pid (ctx,
0x273f,
0x1004,
&error);
if (device == NULL &&
error->domain == G_USB_DEVICE_ERROR &&
error->code == G_USB_DEVICE_ERROR_NO_DEVICE) {
g_print ("No device detected!\n");
g_error_free (error);
goto out;
}
g_assert_no_error (error);
g_assert (device != NULL);
/* open */
ret = g_usb_device_open (device, &error);
g_assert_no_error (error);
g_assert (ret);
/* get vendor data */
idx = g_usb_device_get_custom_index (device,
G_USB_DEVICE_CLASS_VENDOR_SPECIFIC,
'F', 'W', &error);
g_assert_no_error (error);
g_assert_cmpint (idx, ==, 3);
/* get the firmware version */
tmp = g_usb_device_get_string_descriptor (device, idx, &error);
g_assert_no_error (error);
g_assert_cmpstr (tmp, ==, "2.0.3");
g_free (tmp);
/* close */
ret = g_usb_device_close (device, &error);
g_assert_no_error (error);
g_assert (ret);
g_object_unref (device);
out:
g_object_unref (ctx);
}
int
main (int argc,
char **argv)
{
g_test_init (&argc, &argv, NULL);
/* only critical and error are fatal */
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL);
/* tests go here */
g_test_add_func ("/gusb/context", gusb_context_func);
g_test_add_func ("/gusb/context{lookup}", gusb_context_lookup_func);
g_test_add_func ("/gusb/device", gusb_device_func);
g_test_add_func ("/gusb/device[huey]", gusb_device_huey_func);
g_test_add_func ("/gusb/device[munki]", gusb_device_munki_func);
g_test_add_func ("/gusb/device[colorhug2]", gusb_device_ch2_func);
return g_test_run ();
}
|