| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2011 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <string.h> |
| 25 |
#include <glib.h> |
| 26 |
#include <glib-object.h> |
| 27 |
#include <gusb.h> |
| 28 |
#include <math.h> |
| 29 |
#include <colord.h> |
| 30 |
|
| 31 |
#include "ch-math.h" |
| 32 |
#include "ch-hash.h" |
| 33 |
#include "ch-device.h" |
| 34 |
#include "ch-device-queue.h" |
| 35 |
|
| 36 |
static void |
| 37 |
ch_test_hash_func (void) |
| 38 |
{ |
| 39 |
ChSha1 sha1; |
| 40 |
gboolean ret; |
| 41 |
gchar *str; |
| 42 |
g_autoptr(GError) error = NULL; |
| 43 |
|
| 44 |
/* parse into structure */ |
| 45 |
ret = ch_sha1_parse ("f18973b4ebaeab527dc15d5dd246debfbff20324", |
| 46 |
&sha1, &error); |
| 47 |
g_assert_no_error (error); |
| 48 |
g_assert (ret); |
| 49 |
g_assert_cmpint (sha1.bytes[0], == ,241); |
| 50 |
g_assert_cmpint (sha1.bytes[1], ==, 137); |
| 51 |
|
| 52 |
/* print back to string */ |
| 53 |
str = ch_sha1_to_string (&sha1); |
| 54 |
g_assert_cmpstr (str, ==, "f18973b4ebaeab527dc15d5dd246debfbff20324"); |
| 55 |
g_free (str); |
| 56 |
} |
| 57 |
|
| 58 |
static guint device_failed_cnt = 0; |
| 59 |
static guint progress_changed_cnt = 0; |
| 60 |
|
| 61 |
static void |
| 62 |
ch_test_device_queue_device_failed_cb (ChDeviceQueue *device_queue, |
| 63 |
GUsbDevice *device, |
| 64 |
const gchar *error_message, |
| 65 |
gpointer user_data) |
| 66 |
{ |
| 67 |
device_failed_cnt++; |
| 68 |
g_debug ("device %s down, error: %s", |
| 69 |
g_usb_device_get_platform_id (device), |
| 70 |
error_message); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
ch_test_device_queue_progress_changed_cb (ChDeviceQueue *device_queue, |
| 75 |
guint percentage, |
| 76 |
gpointer user_data) |
| 77 |
{ |
| 78 |
progress_changed_cnt++; |
| 79 |
g_debug ("queue complete %u%%", |
| 80 |
percentage); |
| 81 |
} |
| 82 |
|
| 83 |
static void |
| 84 |
ch_test_device_queue_func (void) |
| 85 |
{ |
| 86 |
ChDeviceQueue *device_queue; |
| 87 |
gboolean ret; |
| 88 |
g_autoptr(GError) error = NULL; |
| 89 |
GPtrArray *devices; |
| 90 |
guint i; |
| 91 |
guint valid_devices = 0; |
| 92 |
GUsbContext *usb_ctx; |
| 93 |
GUsbDevice *device; |
| 94 |
|
| 95 |
/* try to find any ColorHug devices */ |
| 96 |
usb_ctx = g_usb_context_new (NULL); |
| 97 |
if (usb_ctx == NULL) |
| 98 |
return; |
| 99 |
|
| 100 |
devices = g_usb_context_get_devices (usb_ctx); |
| 101 |
|
| 102 |
/* watch for any failed devices */ |
| 103 |
device_queue = ch_device_queue_new (); |
| 104 |
g_signal_connect (device_queue, |
| 105 |
"device-failed", |
| 106 |
G_CALLBACK (ch_test_device_queue_device_failed_cb), |
| 107 |
NULL); |
| 108 |
g_signal_connect (device_queue, |
| 109 |
"progress-changed", |
| 110 |
G_CALLBACK (ch_test_device_queue_progress_changed_cb), |
| 111 |
NULL); |
| 112 |
for (i = 0; i < devices->len; i++) { |
| 113 |
device = g_ptr_array_index (devices, i); |
| 114 |
if (g_usb_device_get_vid (device) != CH_USB_VID) |
| 115 |
continue; |
| 116 |
if (g_usb_device_get_pid (device) != CH_USB_PID_FIRMWARE && |
| 117 |
g_usb_device_get_pid (device) != CH_USB_PID_FIRMWARE_PLUS) |
| 118 |
continue; |
| 119 |
|
| 120 |
valid_devices++; |
| 121 |
g_debug ("Found ColorHug device %s", |
| 122 |
g_usb_device_get_platform_id (device)); |
| 123 |
|
| 124 |
/* load device */ |
| 125 |
ret = ch_device_open (device, &error); |
| 126 |
g_assert_no_error (error); |
| 127 |
g_assert (ret); |
| 128 |
|
| 129 |
/* set RED to queue */ |
| 130 |
ch_device_queue_set_leds (device_queue, |
| 131 |
device, |
| 132 |
CH_STATUS_LED_RED, |
| 133 |
50, |
| 134 |
100, |
| 135 |
5); |
| 136 |
|
| 137 |
/* set GREEN to queue */ |
| 138 |
ch_device_queue_set_leds (device_queue, |
| 139 |
device, |
| 140 |
CH_STATUS_LED_GREEN, |
| 141 |
50, |
| 142 |
100, |
| 143 |
5); |
| 144 |
|
| 145 |
/* do unknown command */ |
| 146 |
ch_device_queue_add (device_queue, |
| 147 |
device, |
| 148 |
0xff, |
| 149 |
NULL, |
| 150 |
0, |
| 151 |
NULL, |
| 152 |
0); |
| 153 |
|
| 154 |
/* set ALL to queue */ |
| 155 |
ch_device_queue_set_leds (device_queue, |
| 156 |
device, |
| 157 |
CH_STATUS_LED_RED | |
| 158 |
CH_STATUS_LED_GREEN | |
| 159 |
CH_STATUS_LED_BLUE, |
| 160 |
50, |
| 161 |
100, |
| 162 |
5); |
| 163 |
} |
| 164 |
|
| 165 |
/* fix make check with no hardware attached */ |
| 166 |
if (valid_devices == 0) |
| 167 |
goto out; |
| 168 |
|
| 169 |
/* process queue */ |
| 170 |
ret = ch_device_queue_process (device_queue, |
| 171 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONFATAL_ERRORS, |
| 172 |
NULL, |
| 173 |
&error); |
| 174 |
g_assert_no_error (error); |
| 175 |
g_assert (ret); |
| 176 |
|
| 177 |
/* check we failed both devices */ |
| 178 |
g_assert_cmpint (device_failed_cnt, ==, valid_devices); |
| 179 |
|
| 180 |
/* check we got enough progress updates */ |
| 181 |
if (valid_devices > 0) |
| 182 |
g_assert_cmpint (progress_changed_cnt, ==, valid_devices * 3 + 1); |
| 183 |
|
| 184 |
/* fail on unknown command */ |
| 185 |
for (i = 0; i < devices->len; i++) { |
| 186 |
device = g_ptr_array_index (devices, i); |
| 187 |
if (g_usb_device_get_vid (device) != CH_USB_VID) |
| 188 |
continue; |
| 189 |
if (g_usb_device_get_pid (device) != CH_USB_PID_FIRMWARE) |
| 190 |
continue; |
| 191 |
ch_device_queue_add (device_queue, |
| 192 |
device, |
| 193 |
0xff, |
| 194 |
NULL, |
| 195 |
0, |
| 196 |
NULL, |
| 197 |
0); |
| 198 |
} |
| 199 |
|
| 200 |
/* process queue */ |
| 201 |
ret = ch_device_queue_process (device_queue, |
| 202 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 203 |
NULL, |
| 204 |
&error); |
| 205 |
g_assert_error (error, 1, 0); |
| 206 |
g_debug ("error was: %s", error->message); |
| 207 |
g_assert (!ret); |
| 208 |
out: |
| 209 |
g_ptr_array_unref (devices); |
| 210 |
g_object_unref (device_queue); |
| 211 |
g_object_unref (usb_ctx); |
| 212 |
} |
| 213 |
|
| 214 |
static void |
| 215 |
ch_test_math_convert_func (void) |
| 216 |
{ |
| 217 |
ChPackedFloat pf; |
| 218 |
gdouble value = 0.0f; |
| 219 |
|
| 220 |
/* test packing */ |
| 221 |
g_assert_cmpint (sizeof (ChPackedFloat), ==, 4); |
| 222 |
|
| 223 |
/* test converting to packed struct */ |
| 224 |
value = 3.1415927f; |
| 225 |
ch_double_to_packed_float (value, &pf); |
| 226 |
pf.raw = ch_packed_float_get_value (&pf); |
| 227 |
g_assert_cmpint (pf.offset, ==, 3); |
| 228 |
g_assert_cmpint (pf.fraction, <, 0x249f); |
| 229 |
g_assert_cmpint (pf.fraction, >, 0x240f); |
| 230 |
|
| 231 |
/* test converting to packed struct */ |
| 232 |
value = -3.1415927f; |
| 233 |
ch_double_to_packed_float (value, &pf); |
| 234 |
pf.raw = ch_packed_float_get_value (&pf); |
| 235 |
g_assert_cmpint (pf.offset, ==, -4); |
| 236 |
g_assert_cmpint (pf.fraction, <, (0x240f ^ 0xffff)); |
| 237 |
g_assert_cmpint (pf.fraction, >, (0x249f ^ 0xffff)); |
| 238 |
|
| 239 |
/* test converting positive to float */ |
| 240 |
pf.offset = 3; |
| 241 |
pf.fraction = 0x243c; |
| 242 |
pf.raw = ch_packed_float_get_value (&pf); |
| 243 |
ch_packed_float_to_double (&pf, &value); |
| 244 |
g_assert_cmpfloat (value, >, 3.1415); |
| 245 |
g_assert_cmpfloat (value, <, 3.1416); |
| 246 |
|
| 247 |
/* test converting negative to float */ |
| 248 |
pf.offset = -4; |
| 249 |
pf.fraction = 0x243b ^ 0xffff; |
| 250 |
ch_packed_float_to_double (&pf, &value); |
| 251 |
pf.raw = ch_packed_float_get_value (&pf); |
| 252 |
g_assert_cmpfloat (value, >, -3.1416); |
| 253 |
g_assert_cmpfloat (value, <, -3.1415); |
| 254 |
|
| 255 |
/* test converting zero */ |
| 256 |
value = 0.0f; |
| 257 |
ch_double_to_packed_float (value, &pf); |
| 258 |
pf.raw = ch_packed_float_get_value (&pf); |
| 259 |
g_assert_cmpint (pf.offset, ==, 0); |
| 260 |
g_assert_cmpint (pf.fraction, ==, 0); |
| 261 |
ch_packed_float_set_value (&pf, pf.raw); |
| 262 |
ch_packed_float_to_double (&pf, &value); |
| 263 |
g_assert_cmpfloat (value, >, -0.001f); |
| 264 |
g_assert_cmpfloat (value, <, +0.001f); |
| 265 |
|
| 266 |
/* test converting positive */ |
| 267 |
value = +1.4f; |
| 268 |
ch_double_to_packed_float (value, &pf); |
| 269 |
pf.raw = ch_packed_float_get_value (&pf); |
| 270 |
g_assert_cmpint (pf.offset, ==, 1); |
| 271 |
g_assert_cmpint (pf.fraction, <, 0x6668); |
| 272 |
g_assert_cmpint (pf.fraction, >, 0x6663); |
| 273 |
ch_packed_float_set_value (&pf, pf.raw); |
| 274 |
ch_packed_float_to_double (&pf, &value); |
| 275 |
g_assert_cmpfloat (value, <, 1.41); |
| 276 |
g_assert_cmpfloat (value, >, 1.39); |
| 277 |
|
| 278 |
/* test converting negative */ |
| 279 |
value = -1.4f; |
| 280 |
ch_double_to_packed_float (value, &pf); |
| 281 |
pf.raw = ch_packed_float_get_value (&pf); |
| 282 |
g_assert_cmpint (pf.offset, ==, -2); |
| 283 |
g_assert_cmpint (pf.fraction, <, (0x6662 ^ 0xffff)); |
| 284 |
g_assert_cmpint (pf.fraction, >, (0x6668 ^ 0xffff)); |
| 285 |
ch_packed_float_set_value (&pf, pf.raw); |
| 286 |
ch_packed_float_to_double (&pf, &value); |
| 287 |
g_assert_cmpfloat (value, <, -1.39); |
| 288 |
g_assert_cmpfloat (value, >, -1.41); |
| 289 |
|
| 290 |
/* test converting negative max */ |
| 291 |
value = -0x7fff; |
| 292 |
ch_double_to_packed_float (value, &pf); |
| 293 |
pf.raw = ch_packed_float_get_value (&pf); |
| 294 |
g_assert_cmpint (pf.offset, ==, -32767); |
| 295 |
g_assert_cmpint (pf.fraction, ==, 0); |
| 296 |
ch_packed_float_set_value (&pf, pf.raw); |
| 297 |
ch_packed_float_to_double (&pf, &value); |
| 298 |
g_assert_cmpfloat (value, >, -32768.0001); |
| 299 |
g_assert_cmpfloat (value, <, +32767.9999); |
| 300 |
} |
| 301 |
|
| 302 |
static void |
| 303 |
ch_test_math_add_func (void) |
| 304 |
{ |
| 305 |
ChPackedFloat pf; |
| 306 |
ChPackedFloat pf_tmp; |
| 307 |
ChPackedFloat pf_result; |
| 308 |
gdouble value = 0.0f; |
| 309 |
guint8 rc; |
| 310 |
|
| 311 |
/* test addition */ |
| 312 |
ch_double_to_packed_float (3.90f, &pf); |
| 313 |
ch_double_to_packed_float (1.40f, &pf_tmp); |
| 314 |
rc = ch_packed_float_add (&pf, &pf_tmp, &pf_result); |
| 315 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 316 |
ch_packed_float_to_double (&pf_result, &value); |
| 317 |
g_assert_cmpfloat (value, >, 5.299); |
| 318 |
g_assert_cmpfloat (value, <, 5.310); |
| 319 |
|
| 320 |
/* test addition with both negative */ |
| 321 |
ch_double_to_packed_float (-3.90f, &pf); |
| 322 |
ch_double_to_packed_float (-1.40f, &pf_tmp); |
| 323 |
rc = ch_packed_float_add (&pf, &pf_tmp, &pf_result); |
| 324 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 325 |
ch_packed_float_to_double (&pf_result, &value); |
| 326 |
g_assert_cmpfloat (value, >, -5.301); |
| 327 |
g_assert_cmpfloat (value, <, -5.299); |
| 328 |
|
| 329 |
/* test addition with negative */ |
| 330 |
ch_double_to_packed_float (3.20f, &pf); |
| 331 |
ch_double_to_packed_float (-1.50f, &pf_tmp); |
| 332 |
rc = ch_packed_float_add (&pf, &pf_tmp, &pf_result); |
| 333 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 334 |
ch_packed_float_to_double (&pf_result, &value); |
| 335 |
g_assert_cmpfloat (value, <, 1.701); |
| 336 |
g_assert_cmpfloat (value, >, 1.699); |
| 337 |
|
| 338 |
/* test addition with negative */ |
| 339 |
ch_double_to_packed_float (3.20f, &pf); |
| 340 |
ch_double_to_packed_float (-10.50f, &pf_tmp); |
| 341 |
rc = ch_packed_float_add (&pf, &pf_tmp, &pf_result); |
| 342 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 343 |
ch_packed_float_to_double (&pf_result, &value); |
| 344 |
g_assert_cmpfloat (value, >, -7.301); |
| 345 |
g_assert_cmpfloat (value, <, -7.299); |
| 346 |
|
| 347 |
/* test addition overflow */ |
| 348 |
ch_double_to_packed_float (0x7fff, &pf); |
| 349 |
ch_double_to_packed_float (0x7fff, &pf_tmp); |
| 350 |
rc = ch_packed_float_add (&pf, &pf_tmp, &pf_result); |
| 351 |
// g_assert_cmpint (rc, ==, CH_ERROR_OVERFLOW_ADDITION); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
static void |
| 356 |
ch_test_math_multiply_func (void) |
| 357 |
{ |
| 358 |
ChPackedFloat pf; |
| 359 |
ChPackedFloat pf_tmp; |
| 360 |
ChPackedFloat pf_result; |
| 361 |
gdouble value = 0.0f; |
| 362 |
gdouble value1; |
| 363 |
gdouble value2; |
| 364 |
guint8 rc; |
| 365 |
|
| 366 |
/* test safe multiplication */ |
| 367 |
ch_double_to_packed_float (0.25f, &pf); |
| 368 |
ch_double_to_packed_float (0.50f, &pf_tmp); |
| 369 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 370 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 371 |
ch_packed_float_to_double (&pf_result, &value); |
| 372 |
g_assert_cmpfloat (value, >, 0.1249); |
| 373 |
g_assert_cmpfloat (value, <, 0.1251); |
| 374 |
|
| 375 |
/* test multiplication we have to scale */ |
| 376 |
ch_double_to_packed_float (3.90f, &pf); |
| 377 |
ch_double_to_packed_float (1.40f, &pf_tmp); |
| 378 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 379 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 380 |
ch_packed_float_to_double (&pf_result, &value); |
| 381 |
g_assert_cmpfloat (value, >, 5.45); |
| 382 |
g_assert_cmpfloat (value, <, 5.47); |
| 383 |
|
| 384 |
/* test multiplication we have to scale a lot */ |
| 385 |
ch_double_to_packed_float (3.90f, &pf); |
| 386 |
ch_double_to_packed_float (200.0f, &pf_tmp); |
| 387 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 388 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 389 |
ch_packed_float_to_double (&pf_result, &value); |
| 390 |
g_assert_cmpfloat (value, >, 778.9); |
| 391 |
g_assert_cmpfloat (value, <, 780.1); |
| 392 |
|
| 393 |
/* test multiplication of negative */ |
| 394 |
ch_double_to_packed_float (3.90f, &pf); |
| 395 |
ch_double_to_packed_float (-1.4f, &pf_tmp); |
| 396 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 397 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 398 |
ch_packed_float_to_double (&pf_result, &value); |
| 399 |
g_assert_cmpfloat (value, <, -5.45); |
| 400 |
g_assert_cmpfloat (value, >, -5.47); |
| 401 |
|
| 402 |
/* test multiplication of double negative */ |
| 403 |
ch_double_to_packed_float (-3.90f, &pf); |
| 404 |
ch_double_to_packed_float (-1.4f, &pf_tmp); |
| 405 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 406 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 407 |
ch_packed_float_to_double (&pf_result, &value); |
| 408 |
g_assert_cmpfloat (value, >, 5.45); |
| 409 |
g_assert_cmpfloat (value, <, 5.47); |
| 410 |
|
| 411 |
/* test multiplication of very different numbers */ |
| 412 |
ch_double_to_packed_float (0.072587f, &pf); |
| 413 |
ch_double_to_packed_float (80.0f, &pf_tmp); |
| 414 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 415 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 416 |
ch_packed_float_to_double (&pf_result, &value); |
| 417 |
g_assert_cmpfloat (value, >, 5.79); |
| 418 |
g_assert_cmpfloat (value, <, 5.81); |
| 419 |
|
| 420 |
/* be evil */ |
| 421 |
for (value1 = -127; value1 < +127; value1 += 0.5f) { |
| 422 |
for (value2 = -127; value2 < +127; value2 += 0.5f) { |
| 423 |
ch_double_to_packed_float (value1, &pf); |
| 424 |
ch_double_to_packed_float (value2, &pf_tmp); |
| 425 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 426 |
g_assert_cmpint (rc, ==, CH_ERROR_NONE); |
| 427 |
ch_packed_float_to_double (&pf_result, &value); |
| 428 |
g_assert_cmpfloat (value, >, (value1 * value2) - 0.01); |
| 429 |
g_assert_cmpfloat (value, <, (value1 * value2) + 0.01); |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
/* test multiplication overflow */ |
| 434 |
ch_double_to_packed_float (0x4fff, &pf); |
| 435 |
ch_double_to_packed_float (0x4, &pf_tmp); |
| 436 |
rc = ch_packed_float_multiply (&pf, &pf_tmp, &pf_result); |
| 437 |
g_assert_cmpint (rc, ==, CH_ERROR_OVERFLOW_MULTIPLY); |
| 438 |
} |
| 439 |
|
| 440 |
static GUsbDevice * |
| 441 |
ch_client_get_default (GError **error) |
| 442 |
{ |
| 443 |
gboolean ret; |
| 444 |
GUsbContext *usb_ctx; |
| 445 |
GUsbDevice *device = NULL; |
| 446 |
|
| 447 |
g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 448 |
|
| 449 |
/* try to find the ColorHug device */ |
| 450 |
usb_ctx = g_usb_context_new (NULL); |
| 451 |
if (usb_ctx == NULL) { |
| 452 |
g_set_error(error, |
| 453 |
G_USB_DEVICE_ERROR, |
| 454 |
G_USB_DEVICE_ERROR_NO_DEVICE, |
| 455 |
"No device found; USB initialisation failed"); |
| 456 |
return NULL; |
| 457 |
} |
| 458 |
device = g_usb_context_find_by_vid_pid (usb_ctx, |
| 459 |
CH_USB_VID, |
| 460 |
CH_USB_PID_FIRMWARE_ALS_SENSOR_HID, |
| 461 |
NULL); |
| 462 |
if (device == NULL) { |
| 463 |
device = g_usb_context_find_by_vid_pid (usb_ctx, |
| 464 |
CH_USB_VID, |
| 465 |
CH_USB_PID_FIRMWARE, |
| 466 |
error); |
| 467 |
} |
| 468 |
if (device == NULL) |
| 469 |
goto out; |
| 470 |
g_debug ("Found ColorHug device %s", |
| 471 |
g_usb_device_get_platform_id (device)); |
| 472 |
ret = ch_device_open (device, error); |
| 473 |
if (!ret) |
| 474 |
goto out; |
| 475 |
out: |
| 476 |
g_object_unref (usb_ctx); |
| 477 |
return device; |
| 478 |
} |
| 479 |
|
| 480 |
static void |
| 481 |
ch_test_state_func (void) |
| 482 |
{ |
| 483 |
ChColorSelect color_select = 0; |
| 484 |
ChDeviceQueue *device_queue; |
| 485 |
ChFreqScale multiplier = 0; |
| 486 |
gboolean ret; |
| 487 |
gdouble elapsed; |
| 488 |
g_autoptr(GError) error = NULL; |
| 489 |
GTimer *timer; |
| 490 |
guint16 integral_time = 0; |
| 491 |
guint8 leds = 0; |
| 492 |
guint i; |
| 493 |
GUsbDevice *device; |
| 494 |
|
| 495 |
/* load the device */ |
| 496 |
device = ch_client_get_default (&error); |
| 497 |
if (device == NULL && g_error_matches (error, |
| 498 |
G_USB_DEVICE_ERROR, |
| 499 |
G_USB_DEVICE_ERROR_NO_DEVICE)) { |
| 500 |
g_debug ("no device, skipping tests"); |
| 501 |
return; |
| 502 |
} |
| 503 |
g_assert_no_error (error); |
| 504 |
g_assert (device != NULL); |
| 505 |
|
| 506 |
/* verify LEDs */ |
| 507 |
device_queue = ch_device_queue_new (); |
| 508 |
ch_device_queue_set_leds (device_queue, |
| 509 |
device, |
| 510 |
1, |
| 511 |
0, |
| 512 |
0x00, |
| 513 |
0x00); |
| 514 |
ret = ch_device_queue_process (device_queue, |
| 515 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 516 |
NULL, |
| 517 |
&error); |
| 518 |
g_assert_no_error (error); |
| 519 |
g_assert (ret); |
| 520 |
ch_device_queue_get_leds (device_queue, |
| 521 |
device, |
| 522 |
&leds); |
| 523 |
ret = ch_device_queue_process (device_queue, |
| 524 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 525 |
NULL, |
| 526 |
&error); |
| 527 |
g_assert_no_error (error); |
| 528 |
g_assert (ret); |
| 529 |
g_assert_cmpint (leds, ==, 1); |
| 530 |
|
| 531 |
/* verify color select */ |
| 532 |
if (ch_device_get_mode (device) == CH_DEVICE_MODE_FIRMWARE) { |
| 533 |
ch_device_queue_set_color_select (device_queue, |
| 534 |
device, |
| 535 |
CH_COLOR_SELECT_BLUE); |
| 536 |
ret = ch_device_queue_process (device_queue, |
| 537 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 538 |
NULL, |
| 539 |
&error); |
| 540 |
g_assert_no_error (error); |
| 541 |
g_assert (ret); |
| 542 |
ch_device_queue_get_color_select (device_queue, |
| 543 |
device, |
| 544 |
&color_select); |
| 545 |
ret = ch_device_queue_process (device_queue, |
| 546 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 547 |
NULL, |
| 548 |
&error); |
| 549 |
g_assert_no_error (error); |
| 550 |
g_assert (ret); |
| 551 |
g_assert_cmpint (color_select, ==, CH_COLOR_SELECT_BLUE); |
| 552 |
|
| 553 |
/* verify multiplier */ |
| 554 |
ch_device_queue_set_multiplier (device_queue, |
| 555 |
device, |
| 556 |
CH_FREQ_SCALE_2); |
| 557 |
ret = ch_device_queue_process (device_queue, |
| 558 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 559 |
NULL, |
| 560 |
&error); |
| 561 |
g_assert_no_error (error); |
| 562 |
g_assert (ret); |
| 563 |
ch_device_queue_get_multiplier (device_queue, |
| 564 |
device, |
| 565 |
&multiplier); |
| 566 |
ret = ch_device_queue_process (device_queue, |
| 567 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 568 |
NULL, |
| 569 |
&error); |
| 570 |
g_assert_no_error (error); |
| 571 |
g_assert (ret); |
| 572 |
g_assert_cmpint (multiplier, ==, CH_FREQ_SCALE_2); |
| 573 |
} |
| 574 |
|
| 575 |
/* verify integral */ |
| 576 |
ch_device_queue_set_integral_time (device_queue, |
| 577 |
device, |
| 578 |
100); |
| 579 |
ret = ch_device_queue_process (device_queue, |
| 580 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 581 |
NULL, |
| 582 |
&error); |
| 583 |
g_assert_no_error (error); |
| 584 |
g_assert (ret); |
| 585 |
ch_device_queue_get_integral_time (device_queue, |
| 586 |
device, |
| 587 |
&integral_time); |
| 588 |
ret = ch_device_queue_process (device_queue, |
| 589 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 590 |
NULL, |
| 591 |
&error); |
| 592 |
g_assert_no_error (error); |
| 593 |
g_assert (ret); |
| 594 |
g_assert_cmpint (integral_time, ==, 100); |
| 595 |
|
| 596 |
/* verify sram access time */ |
| 597 |
if (ch_device_get_mode (device) == CH_DEVICE_MODE_FIRMWARE_PLUS) { |
| 598 |
guint8 data[3500*2]; |
| 599 |
for (i = 0; i < sizeof(data); i++) |
| 600 |
data[i] = i; |
| 601 |
|
| 602 |
/* test writing */ |
| 603 |
timer = g_timer_new (); |
| 604 |
ch_device_queue_write_sram (device_queue, |
| 605 |
device, |
| 606 |
0x0000, |
| 607 |
data, |
| 608 |
sizeof(data)); |
| 609 |
ret = ch_device_queue_process (device_queue, |
| 610 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 611 |
NULL, |
| 612 |
&error); |
| 613 |
g_assert_no_error (error); |
| 614 |
g_assert (ret); |
| 615 |
elapsed = g_timer_elapsed (timer, NULL); |
| 616 |
g_debug ("%" G_GSIZE_FORMAT " writes in %.1fms", |
| 617 |
sizeof(data) / 60, elapsed * 1000); |
| 618 |
g_assert_cmpfloat (elapsed, <, 0.75); |
| 619 |
|
| 620 |
/* test reading */ |
| 621 |
g_timer_reset (timer); |
| 622 |
ch_device_queue_read_sram (device_queue, |
| 623 |
device, |
| 624 |
0x0000, |
| 625 |
data, |
| 626 |
sizeof(data)); |
| 627 |
ret = ch_device_queue_process (device_queue, |
| 628 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 629 |
NULL, |
| 630 |
&error); |
| 631 |
g_assert_no_error (error); |
| 632 |
g_assert (ret); |
| 633 |
elapsed = g_timer_elapsed (timer, NULL); |
| 634 |
g_debug ("%" G_GSIZE_FORMAT " reads in %.1fms", |
| 635 |
sizeof(data) / 60, elapsed * 1000); |
| 636 |
g_assert_cmpfloat (elapsed, <, 0.75); |
| 637 |
g_timer_destroy (timer); |
| 638 |
} |
| 639 |
|
| 640 |
/* close */ |
| 641 |
ret = ch_device_close (device, &error); |
| 642 |
g_assert_no_error (error); |
| 643 |
g_assert (ret); |
| 644 |
|
| 645 |
g_object_unref (device_queue); |
| 646 |
} |
| 647 |
|
| 648 |
static void |
| 649 |
ch_test_eeprom_func (void) |
| 650 |
{ |
| 651 |
gboolean ret; |
| 652 |
g_autoptr(GError) error = NULL; |
| 653 |
guint16 major = 0; |
| 654 |
guint16 micro = 0; |
| 655 |
guint16 minor = 0; |
| 656 |
guint8 types = 0; |
| 657 |
CdColorRGB value; |
| 658 |
gdouble post_scale = 0; |
| 659 |
gdouble post_scale_tmp = 0; |
| 660 |
gdouble pre_scale = 0; |
| 661 |
gdouble pre_scale_tmp = 0; |
| 662 |
guint32 serial_number = 0; |
| 663 |
CdMat3x3 calibration; |
| 664 |
CdMat3x3 calibration_tmp; |
| 665 |
gchar desc[24]; |
| 666 |
GUsbDevice *device; |
| 667 |
ChDeviceQueue *device_queue; |
| 668 |
|
| 669 |
/* load the device */ |
| 670 |
device = ch_client_get_default (&error); |
| 671 |
if (device != NULL && g_usb_device_get_pid (device) != CH_USB_PID_FIRMWARE) { |
| 672 |
ret = ch_device_close (device, &error); |
| 673 |
g_assert_no_error (error); |
| 674 |
g_assert (ret); |
| 675 |
g_debug ("not capable device, skipping tests"); |
| 676 |
return; |
| 677 |
} |
| 678 |
if (device == NULL && g_error_matches (error, |
| 679 |
G_USB_DEVICE_ERROR, |
| 680 |
G_USB_DEVICE_ERROR_NO_DEVICE)) { |
| 681 |
g_debug ("no device, skipping tests"); |
| 682 |
return; |
| 683 |
} |
| 684 |
g_assert_no_error (error); |
| 685 |
g_assert (device != NULL); |
| 686 |
|
| 687 |
/* only run the destructive tests on a device that is blank */ |
| 688 |
device_queue = ch_device_queue_new (); |
| 689 |
ch_device_queue_get_serial_number (device_queue, |
| 690 |
device, |
| 691 |
&serial_number); |
| 692 |
ret = ch_device_queue_process (device_queue, |
| 693 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 694 |
NULL, |
| 695 |
&error); |
| 696 |
g_assert_no_error (error); |
| 697 |
g_assert (ret); |
| 698 |
if (serial_number != 0) { |
| 699 |
g_debug ("not resetting device as bad serial, skipping tests"); |
| 700 |
return; |
| 701 |
} |
| 702 |
|
| 703 |
/* write eeprom with wrong code */ |
| 704 |
ch_device_queue_write_eeprom (device_queue, |
| 705 |
device, |
| 706 |
"hello dave"); |
| 707 |
ret = ch_device_queue_process (device_queue, |
| 708 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 709 |
NULL, |
| 710 |
&error); |
| 711 |
g_assert_error (error, 1, 0); |
| 712 |
g_assert (!ret); |
| 713 |
g_clear_error (&error); |
| 714 |
|
| 715 |
/* verify serial number */ |
| 716 |
ch_device_queue_set_serial_number (device_queue, |
| 717 |
device, |
| 718 |
12345678); |
| 719 |
ret = ch_device_queue_process (device_queue, |
| 720 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 721 |
NULL, |
| 722 |
&error); |
| 723 |
g_assert_no_error (error); |
| 724 |
g_assert (ret); |
| 725 |
ch_device_queue_get_serial_number (device_queue, |
| 726 |
device, |
| 727 |
&serial_number); |
| 728 |
ret = ch_device_queue_process (device_queue, |
| 729 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 730 |
NULL, |
| 731 |
&error); |
| 732 |
g_assert_no_error (error); |
| 733 |
g_assert (ret); |
| 734 |
g_assert_cmpint (serial_number, ==, 12345678); |
| 735 |
|
| 736 |
/* verify firmware */ |
| 737 |
ch_device_queue_get_firmware_ver (device_queue, |
| 738 |
device, |
| 739 |
&major, |
| 740 |
&minor, |
| 741 |
µ); |
| 742 |
ret = ch_device_queue_process (device_queue, |
| 743 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 744 |
NULL, |
| 745 |
&error); |
| 746 |
g_assert_no_error (error); |
| 747 |
g_assert (ret); |
| 748 |
g_assert_cmpint (major, ==, 1); |
| 749 |
g_assert_cmpint (minor, ==, 0); |
| 750 |
g_assert_cmpint (micro, >, 0); |
| 751 |
|
| 752 |
/* verify dark offsets */ |
| 753 |
value.R = 0.12; |
| 754 |
value.G = 0.34; |
| 755 |
value.B = 0.56; |
| 756 |
ch_device_queue_set_dark_offsets (device_queue, |
| 757 |
device, |
| 758 |
&value); |
| 759 |
ret = ch_device_queue_process (device_queue, |
| 760 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 761 |
NULL, |
| 762 |
&error); |
| 763 |
g_assert_no_error (error); |
| 764 |
g_assert (ret); |
| 765 |
ch_device_queue_get_dark_offsets (device_queue, |
| 766 |
device, |
| 767 |
&value); |
| 768 |
ret = ch_device_queue_process (device_queue, |
| 769 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 770 |
NULL, |
| 771 |
&error); |
| 772 |
g_assert_no_error (error); |
| 773 |
g_assert (ret); |
| 774 |
g_assert_cmpint (value.R, ==, 0.12); |
| 775 |
g_assert_cmpint (value.G, ==, 0.34); |
| 776 |
g_assert_cmpint (value.B, ==, 0.56); |
| 777 |
|
| 778 |
/* verify calibration */ |
| 779 |
calibration.m00 = 1.0f; |
| 780 |
calibration.m01 = 2.0f; |
| 781 |
calibration.m02 = 3.0f; |
| 782 |
calibration.m10 = 4.0f; |
| 783 |
calibration.m11 = 5.0f; |
| 784 |
calibration.m12 = 6.0f; |
| 785 |
calibration.m20 = 7.0f; |
| 786 |
calibration.m21 = 8.0f; |
| 787 |
calibration.m22 = 9.0f; |
| 788 |
ch_device_queue_set_calibration (device_queue, |
| 789 |
device, |
| 790 |
60, |
| 791 |
&calibration, |
| 792 |
CH_CALIBRATION_TYPE_CRT, |
| 793 |
"test0"); |
| 794 |
ch_device_queue_set_calibration (device_queue, |
| 795 |
device, |
| 796 |
61, |
| 797 |
&calibration, |
| 798 |
CH_CALIBRATION_TYPE_PROJECTOR, |
| 799 |
"test1"); |
| 800 |
ret = ch_device_queue_process (device_queue, |
| 801 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 802 |
NULL, |
| 803 |
&error); |
| 804 |
g_assert_no_error (error); |
| 805 |
g_assert (ret); |
| 806 |
ch_device_queue_set_calibration (device_queue, |
| 807 |
device, |
| 808 |
60, |
| 809 |
&calibration, |
| 810 |
CH_CALIBRATION_TYPE_CRT, |
| 811 |
"test0"); |
| 812 |
ret = ch_device_queue_process (device_queue, |
| 813 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 814 |
NULL, |
| 815 |
&error); |
| 816 |
g_assert_no_error (error); |
| 817 |
g_assert (ret); |
| 818 |
|
| 819 |
/* read back data */ |
| 820 |
ch_device_queue_get_calibration (device_queue, |
| 821 |
device, |
| 822 |
60, |
| 823 |
&calibration_tmp, |
| 824 |
&types, |
| 825 |
desc); |
| 826 |
ret = ch_device_queue_process (device_queue, |
| 827 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 828 |
NULL, |
| 829 |
&error); |
| 830 |
g_assert_no_error (error); |
| 831 |
g_assert (ret); |
| 832 |
g_assert (memcmp (&calibration_tmp, |
| 833 |
&calibration, |
| 834 |
sizeof (gfloat) * 9) == 0); |
| 835 |
g_assert_cmpint (types, ==, CH_CALIBRATION_TYPE_CRT); |
| 836 |
g_assert_cmpstr (desc, ==, "test0"); |
| 837 |
ch_device_queue_get_calibration (device_queue, |
| 838 |
device, |
| 839 |
61, |
| 840 |
&calibration_tmp, |
| 841 |
&types, |
| 842 |
desc); |
| 843 |
ret = ch_device_queue_process (device_queue, |
| 844 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 845 |
NULL, |
| 846 |
&error); |
| 847 |
g_assert_no_error (error); |
| 848 |
g_assert (ret); |
| 849 |
g_assert (memcmp (&calibration_tmp, |
| 850 |
&calibration, |
| 851 |
sizeof (gfloat) * 9) == 0); |
| 852 |
g_assert_cmpint (types, ==, CH_CALIBRATION_TYPE_PROJECTOR); |
| 853 |
g_assert_cmpstr (desc, ==, "test1"); |
| 854 |
|
| 855 |
/* verify post scale */ |
| 856 |
post_scale = 127.8f; |
| 857 |
ch_device_queue_set_post_scale (device_queue, |
| 858 |
device, |
| 859 |
post_scale); |
| 860 |
ret = ch_device_queue_process (device_queue, |
| 861 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 862 |
NULL, |
| 863 |
&error); |
| 864 |
g_assert_no_error (error); |
| 865 |
g_assert (ret); |
| 866 |
|
| 867 |
ch_device_queue_get_post_scale (device_queue, |
| 868 |
device, |
| 869 |
&post_scale_tmp); |
| 870 |
ret = ch_device_queue_process (device_queue, |
| 871 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 872 |
NULL, |
| 873 |
&error); |
| 874 |
g_assert_no_error (error); |
| 875 |
g_assert (ret); |
| 876 |
g_assert_cmpfloat (fabs (post_scale - post_scale_tmp), <, 0.0001); |
| 877 |
|
| 878 |
/* verify pre scale */ |
| 879 |
pre_scale = 1.23f; |
| 880 |
ch_device_queue_set_pre_scale (device_queue, |
| 881 |
device, |
| 882 |
pre_scale); |
| 883 |
ret = ch_device_queue_process (device_queue, |
| 884 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 885 |
NULL, |
| 886 |
&error); |
| 887 |
g_assert_no_error (error); |
| 888 |
g_assert (ret); |
| 889 |
|
| 890 |
ch_device_queue_get_pre_scale (device_queue, |
| 891 |
device, |
| 892 |
&pre_scale_tmp); |
| 893 |
ret = ch_device_queue_process (device_queue, |
| 894 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 895 |
NULL, |
| 896 |
&error); |
| 897 |
g_assert_no_error (error); |
| 898 |
g_assert (ret); |
| 899 |
g_assert_cmpfloat (fabs (pre_scale - pre_scale_tmp), <, 0.0001); |
| 900 |
|
| 901 |
#if 0 |
| 902 |
/* write eeprom */ |
| 903 |
ch_device_queue_write_eeprom (device_queue, |
| 904 |
device, |
| 905 |
CH_WRITE_EEPROM_MAGIC); |
| 906 |
ret = ch_device_queue_process (device_queue, |
| 907 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 908 |
NULL, |
| 909 |
&error); |
| 910 |
g_assert_no_error (error); |
| 911 |
g_assert (ret); |
| 912 |
#endif |
| 913 |
|
| 914 |
ret = ch_device_close (device, &error); |
| 915 |
g_assert_no_error (error); |
| 916 |
g_assert (ret); |
| 917 |
g_object_unref (device); |
| 918 |
g_object_unref (device_queue); |
| 919 |
} |
| 920 |
|
| 921 |
static void |
| 922 |
ch_test_reading_func (void) |
| 923 |
{ |
| 924 |
gboolean ret; |
| 925 |
g_autoptr(GError) error = NULL; |
| 926 |
guint32 take_reading = 0; |
| 927 |
GUsbDevice *device; |
| 928 |
ChDeviceQueue *device_queue; |
| 929 |
|
| 930 |
/* load the device */ |
| 931 |
device = ch_client_get_default (&error); |
| 932 |
if (device == NULL && g_error_matches (error, |
| 933 |
G_USB_DEVICE_ERROR, |
| 934 |
G_USB_DEVICE_ERROR_NO_DEVICE)) { |
| 935 |
g_debug ("no device, skipping tests"); |
| 936 |
return; |
| 937 |
} |
| 938 |
g_assert_no_error (error); |
| 939 |
g_assert (device != NULL); |
| 940 |
|
| 941 |
/* set color select */ |
| 942 |
device_queue = ch_device_queue_new (); |
| 943 |
if (ch_device_get_mode (device) == CH_DEVICE_MODE_FIRMWARE) { |
| 944 |
ch_device_queue_set_color_select (device_queue, |
| 945 |
device, |
| 946 |
CH_COLOR_SELECT_WHITE); |
| 947 |
ret = ch_device_queue_process (device_queue, |
| 948 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 949 |
NULL, |
| 950 |
&error); |
| 951 |
g_assert_no_error (error); |
| 952 |
g_assert (ret); |
| 953 |
|
| 954 |
/* set multiplier */ |
| 955 |
ch_device_queue_set_multiplier (device_queue, |
| 956 |
device, |
| 957 |
CH_FREQ_SCALE_100); |
| 958 |
ret = ch_device_queue_process (device_queue, |
| 959 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 960 |
NULL, |
| 961 |
&error); |
| 962 |
g_assert_no_error (error); |
| 963 |
g_assert (ret); |
| 964 |
} |
| 965 |
|
| 966 |
/* set integral and take a reading from the hardware */ |
| 967 |
ch_device_queue_set_integral_time (device_queue, |
| 968 |
device, |
| 969 |
0xffff); |
| 970 |
ch_device_queue_take_reading_raw (device_queue, |
| 971 |
device, |
| 972 |
&take_reading); |
| 973 |
ret = ch_device_queue_process (device_queue, |
| 974 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 975 |
NULL, |
| 976 |
&error); |
| 977 |
g_assert_no_error (error); |
| 978 |
g_assert (ret); |
| 979 |
g_assert_cmpint (take_reading, >, 0); |
| 980 |
|
| 981 |
/* close */ |
| 982 |
ret = ch_device_close (device, &error); |
| 983 |
g_assert_no_error (error); |
| 984 |
g_assert (ret); |
| 985 |
|
| 986 |
g_object_unref (device); |
| 987 |
g_object_unref (device_queue); |
| 988 |
} |
| 989 |
|
| 990 |
static void |
| 991 |
ch_test_reading_xyz_func (void) |
| 992 |
{ |
| 993 |
gboolean ret; |
| 994 |
CdMat3x3 calibration; |
| 995 |
CdColorXYZ reading1; |
| 996 |
CdColorXYZ reading2; |
| 997 |
gdouble scaling_factor_actual; |
| 998 |
g_autoptr(GError) error = NULL; |
| 999 |
guint16 calibration_map[6]; |
| 1000 |
guint16 post_scale; |
| 1001 |
guint i; |
| 1002 |
GUsbDevice *device; |
| 1003 |
CdColorRGB value; |
| 1004 |
ChDeviceQueue *device_queue; |
| 1005 |
|
| 1006 |
/* load the device */ |
| 1007 |
device = ch_client_get_default (&error); |
| 1008 |
if (device != NULL && g_usb_device_get_pid (device) != CH_USB_PID_FIRMWARE) { |
| 1009 |
ret = ch_device_close (device, &error); |
| 1010 |
g_assert_no_error (error); |
| 1011 |
g_assert (ret); |
| 1012 |
g_debug ("not capable device, skipping tests"); |
| 1013 |
return; |
| 1014 |
} |
| 1015 |
if (device == NULL && g_error_matches (error, |
| 1016 |
G_USB_DEVICE_ERROR, |
| 1017 |
G_USB_DEVICE_ERROR_NO_DEVICE)) { |
| 1018 |
g_debug ("no device, skipping tests"); |
| 1019 |
return; |
| 1020 |
} |
| 1021 |
g_assert_no_error (error); |
| 1022 |
g_assert (device != NULL); |
| 1023 |
|
| 1024 |
/* set unity calibration */ |
| 1025 |
device_queue = ch_device_queue_new (); |
| 1026 |
cd_mat33_set_identity (&calibration); |
| 1027 |
ch_device_queue_set_calibration (device_queue, |
| 1028 |
device, |
| 1029 |
60, |
| 1030 |
&calibration, |
| 1031 |
CH_CALIBRATION_TYPE_ALL, |
| 1032 |
"test0"); |
| 1033 |
ret = ch_device_queue_process (device_queue, |
| 1034 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 1035 |
NULL, |
| 1036 |
&error); |
| 1037 |
g_assert_no_error (error); |
| 1038 |
g_assert (ret); |
| 1039 |
|
| 1040 |
/* set everything to use the unity values */ |
| 1041 |
for (i = 0; i < 6; i++) |
| 1042 |
calibration_map[i] = 60; |
| 1043 |
ch_device_queue_set_calibration_map (device_queue, |
| 1044 |
device, |
| 1045 |
calibration_map); |
| 1046 |
ret = ch_device_queue_process (device_queue, |
| 1047 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 1048 |
NULL, |
| 1049 |
&error); |
| 1050 |
g_assert_no_error (error); |
| 1051 |
g_assert (ret); |
| 1052 |
|
| 1053 |
/* set dark offsets and scale */ |
| 1054 |
value.R = 0.0f; |
| 1055 |
value.G = 0.0f; |
| 1056 |
value.B = 0.0f; |
| 1057 |
ch_device_queue_set_dark_offsets (device_queue, |
| 1058 |
device, |
| 1059 |
&value); |
| 1060 |
ch_device_queue_set_pre_scale (device_queue, |
| 1061 |
device, |
| 1062 |
5.0f); |
| 1063 |
ch_device_queue_set_post_scale (device_queue, |
| 1064 |
device, |
| 1065 |
1.0f); |
| 1066 |
|
| 1067 |
/* take a reading from the hardware */ |
| 1068 |
ch_device_queue_take_readings_xyz (device_queue, |
| 1069 |
device, |
| 1070 |
0, |
| 1071 |
&reading1); |
| 1072 |
ret = ch_device_queue_process (device_queue, |
| 1073 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 1074 |
NULL, |
| 1075 |
&error); |
| 1076 |
g_assert_no_error (error); |
| 1077 |
g_assert (ret); |
| 1078 |
g_assert_cmpfloat (reading1.X, >, 0.0f); |
| 1079 |
g_assert_cmpfloat (reading1.Y, >, 0.0f); |
| 1080 |
g_assert_cmpfloat (reading1.Z, >, 0.0f); |
| 1081 |
|
| 1082 |
/* set post scale much higher */ |
| 1083 |
for (post_scale = 1; post_scale < 2000; post_scale *= 2) { |
| 1084 |
g_debug ("Setting post-scale %" G_GUINT16_FORMAT, post_scale); |
| 1085 |
ch_device_queue_set_post_scale (device_queue, |
| 1086 |
device, |
| 1087 |
post_scale); |
| 1088 |
ch_device_queue_take_readings_xyz (device_queue, |
| 1089 |
device, |
| 1090 |
0, |
| 1091 |
&reading2); |
| 1092 |
ret = ch_device_queue_process (device_queue, |
| 1093 |
CH_DEVICE_QUEUE_PROCESS_FLAGS_NONE, |
| 1094 |
NULL, |
| 1095 |
&error); |
| 1096 |
g_assert_no_error (error); |
| 1097 |
g_assert (ret); |
| 1098 |
|
| 1099 |
/* X */ |
| 1100 |
scaling_factor_actual = reading2.X / reading1.X; |
| 1101 |
g_debug ("scale %i: %f, scale 1: %f so effective %f", |
| 1102 |
post_scale, reading2.X, reading1.X, |
| 1103 |
scaling_factor_actual); |
| 1104 |
g_assert_cmpfloat (scaling_factor_actual, >, 0.9); |
| 1105 |
g_assert_cmpfloat (scaling_factor_actual, <, 1.1); |
| 1106 |
reading1.X = reading2.X * 2; |
| 1107 |
|
| 1108 |
/* Y */ |
| 1109 |
scaling_factor_actual = reading2.Y / reading1.Y; |
| 1110 |
g_debug ("scale %i: %f, scale 1: %f so effective %f", |
| 1111 |
post_scale, reading2.Y, reading1.Y, |
| 1112 |
scaling_factor_actual); |
| 1113 |
g_assert_cmpfloat (scaling_factor_actual, >, 0.9); |
| 1114 |
g_assert_cmpfloat (scaling_factor_actual, <, 1.1); |
| 1115 |
reading1.Y = reading2.Y * 2; |
| 1116 |
|
| 1117 |
/* Z */ |
| 1118 |
scaling_factor_actual = reading2.Z / reading1.Z; |
| 1119 |
g_debug ("scale %i: %f, scale 1: %f so effective %f", |
| 1120 |
post_scale, reading2.Z, reading1.Z, |
| 1121 |
scaling_factor_actual); |
| 1122 |
g_assert_cmpfloat (scaling_factor_actual, >, 0.9); |
| 1123 |
g_assert_cmpfloat (scaling_factor_actual, <, 1.1); |
| 1124 |
reading1.Z = reading2.Z * 2; |
| 1125 |
} |
| 1126 |
|
| 1127 |
/* close */ |
| 1128 |
ret = ch_device_close (device, &error); |
| 1129 |
g_assert_no_error (error); |
| 1130 |
g_assert (ret); |
| 1131 |
|
| 1132 |
g_object_unref (device); |
| 1133 |
g_object_unref (device_queue); |
| 1134 |
} |
| 1135 |
|
| 1136 |
/** |
| 1137 |
* ch_test_incomplete_request_func: |
| 1138 |
* |
| 1139 |
* This tests what happens when we do request,request,read on the device |
| 1140 |
* rather than just request,read. With new firmware versions we should |
| 1141 |
* return a %CH_ERROR_INCOMPLETE_REQUEST error value and the original |
| 1142 |
* command ID rather than just the device re-enumerating on the USB bus. |
| 1143 |
*/ |
| 1144 |
static void |
| 1145 |
ch_test_incomplete_request_func (void) |
| 1146 |
{ |
| 1147 |
gboolean ret; |
| 1148 |
g_autoptr(GError) error = NULL; |
| 1149 |
guint8 buffer[CH_USB_HID_EP_SIZE]; |
| 1150 |
GUsbDevice *device = NULL; |
| 1151 |
|
| 1152 |
|
| 1153 |
/* load the device */ |
| 1154 |
device = ch_client_get_default (&error); |
| 1155 |
if (device != NULL && g_usb_device_get_pid (device) != CH_USB_PID_FIRMWARE) { |
| 1156 |
ret = ch_device_close (device, &error); |
| 1157 |
g_assert_no_error (error); |
| 1158 |
g_assert (ret); |
| 1159 |
g_debug ("not capable device, skipping tests"); |
| 1160 |
return; |
| 1161 |
} |
| 1162 |
if (device == NULL && g_error_matches (error, |
| 1163 |
G_USB_DEVICE_ERROR, |
| 1164 |
G_USB_DEVICE_ERROR_NO_DEVICE)) { |
| 1165 |
g_debug ("no device, skipping tests"); |
| 1166 |
return; |
| 1167 |
} |
| 1168 |
g_assert_no_error (error); |
| 1169 |
g_assert (device != NULL); |
| 1170 |
|
| 1171 |
/* sending first tx packet */ |
| 1172 |
memset (buffer, 0x00, CH_USB_HID_EP_SIZE); |
| 1173 |
buffer[0] = CH_CMD_GET_FIRMWARE_VERSION; |
| 1174 |
ret = g_usb_device_interrupt_transfer (device, |
| 1175 |
CH_USB_HID_EP_OUT, |
| 1176 |
buffer, |
| 1177 |
CH_USB_HID_EP_SIZE, |
| 1178 |
NULL, |
| 1179 |
CH_DEVICE_USB_TIMEOUT, |
| 1180 |
NULL, |
| 1181 |
&error); |
| 1182 |
if (!ret) { |
| 1183 |
g_warning ("Error: %s", error->message); |
| 1184 |
goto out; |
| 1185 |
} |
| 1186 |
|
| 1187 |
/* oops, the calling program crashed */ |
| 1188 |
g_usleep (G_USEC_PER_SEC); |
| 1189 |
|
| 1190 |
/* sending second tx packet */ |
| 1191 |
memset (buffer, 0x00, CH_USB_HID_EP_SIZE); |
| 1192 |
buffer[0] = CH_CMD_GET_CALIBRATION; |
| 1193 |
ret = g_usb_device_interrupt_transfer (device, |
| 1194 |
CH_USB_HID_EP_OUT, |
| 1195 |
buffer, |
| 1196 |
CH_USB_HID_EP_SIZE, |
| 1197 |
NULL, |
| 1198 |
CH_DEVICE_USB_TIMEOUT, |
| 1199 |
NULL, |
| 1200 |
&error); |
| 1201 |
if (!ret) { |
| 1202 |
g_warning ("Error: %s", error->message); |
| 1203 |
goto out; |
| 1204 |
} |
| 1205 |
|
| 1206 |
/* get rx packet */ |
| 1207 |
memset (buffer, 0x00, CH_USB_HID_EP_SIZE); |
| 1208 |
ret = g_usb_device_interrupt_transfer (device, |
| 1209 |
CH_USB_HID_EP_IN, |
| 1210 |
buffer, |
| 1211 |
CH_USB_HID_EP_SIZE, |
| 1212 |
NULL, |
| 1213 |
CH_DEVICE_USB_TIMEOUT, |
| 1214 |
NULL, |
| 1215 |
&error); |
| 1216 |
if (!ret) { |
| 1217 |
g_warning ("Error: %s", error->message); |
| 1218 |
goto out; |
| 1219 |
} |
| 1220 |
g_assert_cmpint (buffer[0], ==, CH_ERROR_INCOMPLETE_REQUEST); |
| 1221 |
g_assert_cmpint (buffer[1], ==, CH_CMD_GET_FIRMWARE_VERSION); |
| 1222 |
out: |
| 1223 |
if (device != NULL) { |
| 1224 |
ret = ch_device_close (device, &error); |
| 1225 |
g_assert_no_error (error); |
| 1226 |
g_assert (ret); |
| 1227 |
g_object_unref (device); |
| 1228 |
} |
| 1229 |
} |
| 1230 |
|
| 1231 |
static void |
| 1232 |
ch_test_firmware_func (void) |
| 1233 |
{ |
| 1234 |
ChDeviceMode device_mode; |
| 1235 |
guint8 firmware_data[1024]; |
| 1236 |
guint i; |
| 1237 |
|
| 1238 |
/* set to something predictable */ |
| 1239 |
for (i = 0; i < sizeof(firmware_data); i++) |
| 1240 |
firmware_data[i] = i % 255; |
| 1241 |
|
| 1242 |
/* test with no firmware ID */ |
| 1243 |
device_mode = ch_device_mode_from_firmware (firmware_data, 1024); |
| 1244 |
g_assert_cmpint (device_mode, ==, CH_DEVICE_MODE_UNKNOWN); |
| 1245 |
|
| 1246 |
/* test with V1 signature */ |
| 1247 |
memcpy (firmware_data + 64, CH_FIRMWARE_ID_TOKEN1, 8); |
| 1248 |
device_mode = ch_device_mode_from_firmware (firmware_data, 1024); |
| 1249 |
g_assert_cmpint (device_mode, ==, CH_DEVICE_MODE_FIRMWARE); |
| 1250 |
|
| 1251 |
/* test with V2 signature */ |
| 1252 |
memcpy (firmware_data + 64, CH_FIRMWARE_ID_TOKEN2, 8); |
| 1253 |
device_mode = ch_device_mode_from_firmware (firmware_data, 1024); |
| 1254 |
g_assert_cmpint (device_mode, ==, CH_DEVICE_MODE_FIRMWARE2); |
| 1255 |
} |
| 1256 |
|
| 1257 |
int |
| 1258 |
main (int argc, char **argv) |
| 1259 |
{ |
| 1260 |
g_test_init (&argc, &argv, NULL); |
| 1261 |
|
| 1262 |
/* only critical and error are fatal */ |
| 1263 |
g_log_set_fatal_mask (NULL, G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL); |
| 1264 |
|
| 1265 |
/* tests go here */ |
| 1266 |
g_test_add_func ("/ColorHug/hash", ch_test_hash_func); |
| 1267 |
g_test_add_func ("/ColorHug/device-queue", ch_test_device_queue_func); |
| 1268 |
g_test_add_func ("/ColorHug/math-convert", ch_test_math_convert_func); |
| 1269 |
g_test_add_func ("/ColorHug/math-add", ch_test_math_add_func); |
| 1270 |
g_test_add_func ("/ColorHug/math-multiply", ch_test_math_multiply_func); |
| 1271 |
g_test_add_func ("/ColorHug/state", ch_test_state_func); |
| 1272 |
g_test_add_func ("/ColorHug/eeprom", ch_test_eeprom_func); |
| 1273 |
g_test_add_func ("/ColorHug/reading", ch_test_reading_func); |
| 1274 |
g_test_add_func ("/ColorHug/reading-xyz", ch_test_reading_xyz_func); |
| 1275 |
g_test_add_func ("/ColorHug/device-incomplete-request", ch_test_incomplete_request_func); |
| 1276 |
g_test_add_func ("/ColorHug/firmware", ch_test_firmware_func); |
| 1277 |
|
| 1278 |
return g_test_run (); |
| 1279 |
} |
| 1280 |
|