Debian Codegraph @

lib/colorhug/ch-device.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011-2016 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 <glib.h>
25 #include <gusb.h>
26 #include <string.h>
27 #include <lcms2.h>
28
29 #include "ch-common.h"
30 #include "ch-device.h"
31 #include "ch-math.h"
32
33 /**
34 * ch_device_error_quark:
35 *
36 * Return value: ChDevice error quark.
37 *
38 * Since: 0.1.1
39 **/
40 GQuark
41 ch_device_error_quark (void)
42 {
43 static GQuark quark = 0;
44 if (!quark)
45 quark = g_quark_from_static_string ("ch_device_error");
46 return quark;
47 }
48
49 /**
50 * ch_device_open:
51 *
52 * Since: 0.1.29
53 **/
54 gboolean
55 ch_device_open (GUsbDevice *device, GError **error)
56 {
57 return ch_device_open_full (device, NULL, error);
58 }
59
60 /**
61 * ch_device_close:
62 *
63 * Since: 1.2.11
64 **/
65 gboolean
66 ch_device_close (GUsbDevice *device, GError **error)
67 {
68 g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
69 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
70
71 /* unload device */
72 if (!g_usb_device_release_interface (device,
73 CH_USB_INTERFACE,
74 G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
75 error))
76 return FALSE;
77 if (!g_usb_device_close (device, error))
78 return FALSE;
79 return TRUE;
80 }
81
82 /**
83 * ch_device_is_colorhug:
84 *
85 * Since: 0.1.29
86 **/
87 gboolean
88 ch_device_is_colorhug (GUsbDevice *device)
89 {
90 return ch_device_get_mode (device) != CH_DEVICE_MODE_UNKNOWN;
91 }
92
93 /**
94 * ch_device_get_mode:
95 *
96 * Since: 0.1.29
97 **/
98 ChDeviceMode
99 ch_device_get_mode (GUsbDevice *device)
100 {
101 ChDeviceMode state;
102
103 /* is a legacy device */
104 if (g_usb_device_get_vid (device) == CH_USB_VID_LEGACY &&
105 g_usb_device_get_pid (device) == CH_USB_PID_LEGACY) {
106 return CH_DEVICE_MODE_LEGACY;
107 }
108
109 /* vendor doesn't match */
110 if (g_usb_device_get_vid (device) != CH_USB_VID)
111 return CH_DEVICE_MODE_UNKNOWN;
112
113 /* use the product ID to work out the state */
114 switch (g_usb_device_get_pid (device)) {
115 case CH_USB_PID_BOOTLOADER:
116 state = CH_DEVICE_MODE_BOOTLOADER;
117 break;
118 case CH_USB_PID_BOOTLOADER2:
119 state = CH_DEVICE_MODE_BOOTLOADER2;
120 break;
121 case CH_USB_PID_BOOTLOADER_PLUS:
122 state = CH_DEVICE_MODE_BOOTLOADER_PLUS;
123 break;
124 case CH_USB_PID_BOOTLOADER_ALS:
125 state = CH_DEVICE_MODE_BOOTLOADER_ALS;
126 break;
127 case CH_USB_PID_FIRMWARE:
128 state = CH_DEVICE_MODE_FIRMWARE;
129 break;
130 case CH_USB_PID_FIRMWARE2:
131 state = CH_DEVICE_MODE_FIRMWARE2;
132 break;
133 case CH_USB_PID_FIRMWARE_PLUS:
134 state = CH_DEVICE_MODE_FIRMWARE_PLUS;
135 break;
136 case CH_USB_PID_FIRMWARE_ALS:
137 case CH_USB_PID_FIRMWARE_ALS_SENSOR_HID:
138 state = CH_DEVICE_MODE_FIRMWARE_ALS;
139 break;
140 default:
141 state = CH_DEVICE_MODE_UNKNOWN;
142 break;
143 }
144 return state;
145 }
146
147 static void
148 ch_print_data_buffer (const gchar *title,
149 const guint8 *data,
150 gsize length)
151 {
152 guint i;
153
154 if (g_strcmp0 (title, "request") == 0)
155 g_print ("%c[%dm", 0x1B, 31);
156 if (g_strcmp0 (title, "reply") == 0)
157 g_print ("%c[%dm", 0x1B, 34);
158 g_print ("%s\t", title);
159
160 for (i = 0; i < length; i++)
161 g_print ("%02x [%c]\t", data[i], g_ascii_isprint (data[i]) ? data[i] : '?');
162
163 g_print ("%c[%dm\n", 0x1B, 0);
164 }
165
166 typedef struct {
167 guint8 *buffer;
168 guint8 *buffer_orig;
169 guint8 *buffer_out;
170 gsize buffer_out_len;
171 guint8 cmd;
172 guint retried_cnt;
173 guint8 report_type; /* only for Sensor HID */
174 guint report_length; /* only for Sensor HID */
175 } ChDeviceTaskData;
176
177 /**
178 * ch_device_write_command_finish:
179 * @device: a #GUsbDevice instance.
180 * @res: the #GAsyncResult
181 * @error: A #GError or %NULL
182 *
183 * Gets the result from the asynchronous function.
184 *
185 * Return value: %TRUE if the request was fulfilled.
186 *
187 * Since: 0.1.29
188 **/
189 gboolean
190 ch_device_write_command_finish (GUsbDevice *device,
191 GAsyncResult *res,
192 GError **error)
193 {
194 g_return_val_if_fail (g_task_is_valid (res, device), FALSE);
195 return g_task_propagate_boolean (G_TASK (res), error);
196 }
197
198 static void
199 ch_device_task_data_free (ChDeviceTaskData *tdata)
200 {
201 g_free (tdata->buffer);
202 g_free (tdata->buffer_orig);
203 g_free (tdata);
204 }
205
206 static void ch_device_request_cb (GObject *source_object, GAsyncResult *res, gpointer user_data);
207
208 static void
209 ch_device_reply_cb (GObject *source_object,
210 GAsyncResult *res,
211 gpointer user_data)
212 {
213 ChError error_enum;
214 GError *error = NULL;
215 gsize actual_len;
216 gchar *msg = NULL;
217 GUsbDevice *device = G_USB_DEVICE (source_object);
218 GTask *task = G_TASK (user_data);
219 ChDeviceTaskData *tdata = g_task_get_task_data (task);
220
221 /* get the result */
222 actual_len = g_usb_device_interrupt_transfer_finish (device,
223 res,
224 &error);
225 if ((gssize) actual_len < 0) {
226 g_task_return_new_error (task,
227 CH_DEVICE_ERROR,
228 CH_ERROR_INVALID_VALUE,
229 "%s", error->message);
230 g_object_unref (task);
231 return;
232 }
233
234 /* parse the reply */
235 if (g_getenv ("COLORHUG_VERBOSE") != NULL) {
236 ch_print_data_buffer ("reply",
237 tdata->buffer,
238 actual_len);
239 }
240
241 /* parse */
242 if (tdata->buffer[CH_BUFFER_OUTPUT_RETVAL] != CH_ERROR_NONE ||
243 tdata->buffer[CH_BUFFER_OUTPUT_CMD] != tdata->cmd ||
244 (actual_len != tdata->buffer_out_len + CH_BUFFER_OUTPUT_DATA &&
245 actual_len != CH_USB_HID_EP_SIZE)) {
246 error_enum = tdata->buffer[CH_BUFFER_OUTPUT_RETVAL];
247
248 /* handle incomplete previous request */
249 if (error_enum == CH_ERROR_INCOMPLETE_REQUEST &&
250 tdata->retried_cnt == 0) {
251 tdata->retried_cnt++;
252 memcpy (tdata->buffer, tdata->buffer_orig, CH_USB_HID_EP_SIZE);
253 if (g_getenv ("COLORHUG_VERBOSE") != NULL) {
254 ch_print_data_buffer ("request",
255 tdata->buffer,
256 CH_USB_HID_EP_SIZE);
257 }
258 g_usb_device_interrupt_transfer_async (device,
259 CH_USB_HID_EP_OUT,
260 tdata->buffer,
261 CH_USB_HID_EP_SIZE,
262 CH_DEVICE_USB_TIMEOUT,
263 g_task_get_cancellable (task),
264 ch_device_request_cb,
265 tdata);
266 /* we're re-using the tdata, so don't deallocate it */
267 return;
268 }
269
270 msg = g_strdup_printf ("Invalid read: retval=0x%02x [%s] "
271 "cmd=0x%02x [%s] (expected 0x%x [%s]) "
272 "len=%" G_GSIZE_FORMAT " (expected %" G_GSIZE_FORMAT " or %i)",
273 error_enum,
274 ch_strerror (error_enum),
275 tdata->buffer[CH_BUFFER_OUTPUT_CMD],
276 ch_command_to_string (tdata->buffer[CH_BUFFER_OUTPUT_CMD]),
277 tdata->cmd,
278 ch_command_to_string (tdata->cmd),
279 actual_len,
280 tdata->buffer_out_len + CH_BUFFER_OUTPUT_DATA,
281 CH_USB_HID_EP_SIZE);
282 g_task_return_new_error (task,
283 CH_DEVICE_ERROR,
284 error_enum,
285 "%s", msg);
286 g_object_unref (task);
287 return;
288 }
289
290 /* copy */
291 if (tdata->buffer_out != NULL) {
292 memcpy (tdata->buffer_out,
293 tdata->buffer + CH_BUFFER_OUTPUT_DATA,
294 tdata->buffer_out_len);
295 }
296
297 /* success */
298 g_task_return_boolean (task, TRUE);
299 g_object_unref (task);
300 }
301
302 static void
303 ch_device_request_cb (GObject *source_object,
304 GAsyncResult *res,
305 gpointer user_data)
306 {
307 GError *error = NULL;
308 gssize actual_len;
309 GUsbDevice *device = G_USB_DEVICE (source_object);
310 GTask *task = G_TASK (user_data);
311 ChDeviceTaskData *tdata = g_task_get_task_data (task);
312
313 /* get the result */
314 actual_len = g_usb_device_interrupt_transfer_finish (device,
315 res,
316 &error);
317 if (actual_len < CH_USB_HID_EP_SIZE) {
318 g_task_return_new_error (task,
319 CH_DEVICE_ERROR,
320 CH_ERROR_INVALID_VALUE,
321 "%s", error->message);
322 g_object_unref (task);
323 return;
324 }
325
326 /* request the reply */
327 g_usb_device_interrupt_transfer_async (device,
328 CH_USB_HID_EP_IN,
329 tdata->buffer,
330 CH_USB_HID_EP_SIZE,
331 CH_DEVICE_USB_TIMEOUT,
332 g_task_get_cancellable (task),
333 ch_device_reply_cb,
334 task);
335 }
336
337 static gboolean
338 ch_device_emulate_cb (gpointer user_data)
339 {
340 GTask *task = G_TASK (user_data);
341 ChDeviceTaskData *tdata = g_task_get_task_data (task);
342
343 switch (tdata->cmd) {
344 case CH_CMD_GET_SERIAL_NUMBER:
345 tdata->buffer_out[6] = 42;
346 break;
347 case CH_CMD_GET_FIRMWARE_VERSION:
348 tdata->buffer_out[0] = 0x01;
349 tdata->buffer_out[4] = 0x01;
350 break;
351 case CH_CMD_GET_HARDWARE_VERSION:
352 tdata->buffer_out[0] = 0xff;
353 break;
354 default:
355 g_debug ("Ignoring command %s",
356 ch_command_to_string (tdata->cmd));
357 break;
358 }
359
360 /* success */
361 g_task_return_boolean (task, TRUE);
362 g_object_unref (task);
363
364 return G_SOURCE_REMOVE;
365 }
366
367 #define CH_REPORT_ALS 0x00
368 #define CH_REPORT_HID_SENSOR 0x01
369 #define CH_REPORT_SENSOR_SETTINGS 0x02
370 #define CH_REPORT_SYSTEM_SETTINGS 0x03
371 #define CH_SENSOR_HID_REPORT_GET 0x01
372 #define CH_SENSOR_HID_REPORT_SET 0x09
373 #define CH_SENSOR_HID_FEATURE 0x0300
374
375 static void
376 ch_device_sensor_hid_set_cb (GObject *source_object,
377 GAsyncResult *res,
378 gpointer user_data)
379 {
380 GError *error = NULL;
381 gssize actual_len;
382 GUsbDevice *device = G_USB_DEVICE (source_object);
383 GTask *task = G_TASK (user_data);
384 ChDeviceTaskData *tdata = g_task_get_task_data (task);
385
386 /* get the result */
387 actual_len = g_usb_device_control_transfer_finish (device,
388 res,
389 &error);
390 if (actual_len != tdata->report_length) {
391 g_task_return_new_error (task,
392 CH_DEVICE_ERROR,
393 CH_ERROR_INVALID_VALUE,
394 "%s", error->message);
395 g_object_unref (task);
396 return;
397 }
398 // ch_print_data_buffer ("reply", tdata->buffer, tdata->report_length);
399
400 /* success */
401 g_task_return_boolean (task, TRUE);
402 g_object_unref (task);
403 }
404
405 static void
406 ch_device_sensor_hid_report_cb (GObject *source_object,
407 GAsyncResult *res,
408 gpointer user_data)
409 {
410 GError *error = NULL;
411 gsize actual_len;
412 GUsbDevice *device = G_USB_DEVICE (source_object);
413 GTask *task = G_TASK (user_data);
414 ChDeviceTaskData *tdata = g_task_get_task_data (task);
415
416 /* get the result */
417 actual_len = g_usb_device_interrupt_transfer_finish (device,
418 res,
419 &error);
420 if ((gssize) actual_len < 0) {
421 g_task_return_new_error (task,
422 CH_DEVICE_ERROR,
423 CH_ERROR_INVALID_VALUE,
424 "%s", error->message);
425 g_object_unref (task);
426 return;
427 }
428
429 /* copy out tdata */
430 memcpy (tdata->buffer_out, tdata->buffer + 3, 4);
431
432 /* success */
433 g_task_return_boolean (task, TRUE);
434 g_object_unref (task);
435 }
436
437 static void
438 ch_device_sensor_hid_get_cb (GObject *source_object,
439 GAsyncResult *res,
440 gpointer user_data)
441 {
442 GError *error = NULL;
443 gssize actual_len;
444 gboolean another_request_required = FALSE;
445 GUsbDevice *device = G_USB_DEVICE (source_object);
446 GTask *task = G_TASK (user_data);
447 ChDeviceTaskData *tdata = g_task_get_task_data (task);
448
449 /* get the result */
450 actual_len = g_usb_device_control_transfer_finish (device,
451 res,
452 &error);
453 if (actual_len != tdata->report_length) {
454 g_task_return_new_error (task,
455 CH_DEVICE_ERROR,
456 CH_ERROR_INVALID_VALUE,
457 "%s", error->message);
458 g_object_unref (task);
459 return;
460 }
461 // ch_print_data_buffer ("reply", tdata->buffer, tdata->report_length);
462
463 switch (tdata->cmd) {
464 case CH_CMD_TAKE_READING_RAW:
465 memcpy (tdata->buffer_out, tdata->buffer + 3, 4);
466 break;
467 case CH_CMD_GET_COLOR_SELECT:
468 memcpy (tdata->buffer_out, tdata->buffer + 1, 1);
469 break;
470 case CH_CMD_GET_INTEGRAL_TIME:
471 memcpy (tdata->buffer_out, tdata->buffer + 4, 2);
472 break;
473 case CH_CMD_GET_LEDS:
474 memcpy (tdata->buffer_out, tdata->buffer + 2, 1);
475 break;
476 case CH_CMD_GET_MULTIPLIER:
477 memcpy (tdata->buffer_out, tdata->buffer + 3, 1);
478 break;
479 case CH_CMD_GET_FIRMWARE_VERSION:
480 memcpy (tdata->buffer_out, tdata->buffer + 2, 6);
481 break;
482 case CH_CMD_GET_HARDWARE_VERSION:
483 memcpy (tdata->buffer_out, tdata->buffer + 1, 1);
484 break;
485 case CH_CMD_GET_SERIAL_NUMBER:
486 memcpy (tdata->buffer_out, tdata->buffer + 8, 4);
487 break;
488 case CH_CMD_SET_COLOR_SELECT:
489 memcpy (tdata->buffer + 1, tdata->buffer_orig + 1, 1);
490 another_request_required = TRUE;
491 break;
492 case CH_CMD_SET_INTEGRAL_TIME:
493 memcpy (tdata->buffer + 4, tdata->buffer_orig + 1, 2);
494 another_request_required = TRUE;
495 break;
496 case CH_CMD_SET_LEDS:
497 memcpy (tdata->buffer + 2, tdata->buffer_orig + 1, 1);
498 another_request_required = TRUE;
499 break;
500 case CH_CMD_SET_MULTIPLIER:
501 memcpy (tdata->buffer + 3, tdata->buffer_orig + 1, 1);
502 another_request_required = TRUE;
503 break;
504 case CH_CMD_SET_FLASH_SUCCESS:
505 memcpy (tdata->buffer + 13, tdata->buffer_orig + 1, 1);
506 another_request_required = TRUE;
507 break;
508 case CH_CMD_RESET:
509 tdata->buffer[12] = 1;
510 another_request_required = TRUE;
511 break;
512 case CH_CMD_SET_SERIAL_NUMBER:
513 memcpy (tdata->buffer + 8, tdata->buffer_orig + 1, 4);
514 another_request_required = TRUE;
515 break;
516 default:
517 g_task_return_new_error (task,
518 CH_DEVICE_ERROR,
519 CH_ERROR_UNKNOWN_CMD,
520 "No Sensor HID support for 0x%02x",
521 tdata->cmd);
522 g_object_unref (task);
523 return;
524 }
525
526 /* getting the value was enough */
527 if (!another_request_required) {
528 g_task_return_boolean (task, TRUE);
529 g_object_unref (task);
530 return;
531 }
532
533 // ch_print_data_buffer ("request", tdata->buffer, tdata->report_length);
534 g_usb_device_control_transfer_async (device,
535 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
536 G_USB_DEVICE_REQUEST_TYPE_CLASS,
537 G_USB_DEVICE_RECIPIENT_INTERFACE,
538 CH_SENSOR_HID_REPORT_SET,
539 CH_SENSOR_HID_FEATURE | tdata->report_type,
540 0x0000,
541 tdata->buffer,
542 tdata->report_length,
543 CH_DEVICE_USB_TIMEOUT,
544 g_task_get_cancellable (task),
545 ch_device_sensor_hid_set_cb,
546 task);
547 }
548
549 /**
550 * ch_device_write_command_async:
551 * @device: A #GUsbDevice
552 * @cmd: The command to use, e.g. %CH_CMD_GET_COLOR_SELECT
553 * @buffer_in: The input buffer of data, or %NULL
554 * @buffer_in_len: The input buffer length
555 * @buffer_out: The output buffer of data, or %NULL
556 * @buffer_out_len: The output buffer length
557 * @cancellable: A #GCancellable, or %NULL
558 * @callback: A #GAsyncReadyCallback that will be called when finished.
559 * @user_data: User data passed to @callback
560 *
561 * Sends a message to the device and waits for a reply.
562 *
563 * Since: 0.1.29
564 **/
565 void
566 ch_device_write_command_async (GUsbDevice *device,
567 guint8 cmd,
568 const guint8 *buffer_in,
569 gsize buffer_in_len,
570 guint8 *buffer_out,
571 gsize buffer_out_len,
572 GCancellable *cancellable,
573 GAsyncReadyCallback callback,
574 gpointer user_data)
575 {
576 ChDeviceTaskData *tdata;
577 GTask *task = NULL;
578 g_return_if_fail (G_USB_IS_DEVICE (device));
579 g_return_if_fail (cmd != 0);
580 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
581
582 task = g_task_new (device, cancellable, callback, user_data);
583
584 tdata = g_new0 (ChDeviceTaskData, 1);
585 tdata->buffer_out = buffer_out;
586 tdata->buffer_out_len = buffer_out_len;
587 tdata->buffer = g_new0 (guint8, CH_USB_HID_EP_SIZE);
588 g_task_set_task_data (task, tdata, (GDestroyNotify) ch_device_task_data_free);
589
590 /* set command */
591 tdata->cmd = cmd;
592 tdata->buffer[CH_BUFFER_INPUT_CMD] = tdata->cmd;
593 if (buffer_in != NULL) {
594 memcpy (tdata->buffer + CH_BUFFER_INPUT_DATA,
595 buffer_in,
596 buffer_in_len);
597 }
598 tdata->buffer_orig = g_memdup (tdata->buffer, CH_USB_HID_EP_SIZE);
599
600 /* request */
601 if (g_getenv ("COLORHUG_VERBOSE") != NULL) {
602 ch_print_data_buffer ("request",
603 tdata->buffer,
604 buffer_in_len + 1);
605 }
606
607 /* dummy hardware */
608 if (g_getenv ("COLORHUG_EMULATE") != NULL) {
609 g_timeout_add (20, ch_device_emulate_cb, tdata);
610 return;
611 }
612
613 /* handle ALS in sensor-hid mode differently */
614 if (g_usb_device_get_pid (device) == CH_USB_PID_FIRMWARE_ALS_SENSOR_HID) {
615
616 /* try to map the commands to Sensor HID requests */
617 switch (tdata->cmd) {
618 case CH_CMD_TAKE_READING_RAW:
619 tdata->report_type = CH_REPORT_ALS;
620 tdata->report_length = 7;
621 break;
622 case CH_CMD_GET_COLOR_SELECT:
623 case CH_CMD_GET_INTEGRAL_TIME:
624 case CH_CMD_GET_LEDS:
625 case CH_CMD_GET_MULTIPLIER:
626 case CH_CMD_SET_COLOR_SELECT:
627 case CH_CMD_SET_INTEGRAL_TIME:
628 case CH_CMD_SET_LEDS:
629 case CH_CMD_SET_MULTIPLIER:
630 tdata->report_type = CH_REPORT_SENSOR_SETTINGS;
631 tdata->report_length = 6;
632 break;
633 case CH_CMD_GET_FIRMWARE_VERSION:
634 case CH_CMD_GET_HARDWARE_VERSION:
635 case CH_CMD_GET_SERIAL_NUMBER:
636 case CH_CMD_RESET:
637 case CH_CMD_SET_FLASH_SUCCESS:
638 case CH_CMD_SET_SERIAL_NUMBER:
639 tdata->report_type = CH_REPORT_SYSTEM_SETTINGS;
640 tdata->report_length = 14;
641 break;
642 default:
643 g_task_return_new_error (task,
644 CH_DEVICE_ERROR,
645 CH_ERROR_UNKNOWN_CMD,
646 "No Sensor HID support for 0x%02x",
647 tdata->cmd);
648 g_object_unref (task);
649 return;
650 }
651
652 /* need to use this rather than feature control */
653 if (tdata->report_type == CH_REPORT_ALS) {
654 g_usb_device_interrupt_transfer_async (device,
655 CH_USB_HID_EP_IN,
656 tdata->buffer,
657 tdata->report_length,
658 CH_DEVICE_USB_TIMEOUT,
659 g_task_get_cancellable (task),
660 ch_device_sensor_hid_report_cb,
661 task);
662 return;
663 }
664
665 /* do control transfer */
666 memset(tdata->buffer, '\0', tdata->report_length);
667 // ch_print_data_buffer ("request", tdata->buffer, tdata->report_length);
668 g_usb_device_control_transfer_async (device,
669 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
670 G_USB_DEVICE_REQUEST_TYPE_CLASS,
671 G_USB_DEVICE_RECIPIENT_INTERFACE,
672 CH_SENSOR_HID_REPORT_GET,
673 CH_SENSOR_HID_FEATURE | tdata->report_type,
674 0x0000,
675 tdata->buffer,
676 tdata->report_length,
677 CH_DEVICE_USB_TIMEOUT,
678 g_task_get_cancellable (task),
679 ch_device_sensor_hid_get_cb,
680 task);
681 return;
682 }
683
684 /* do interrupt transfer */
685 g_usb_device_interrupt_transfer_async (device,
686 CH_USB_HID_EP_OUT,
687 tdata->buffer,
688 CH_USB_HID_EP_SIZE,
689 CH_DEVICE_USB_TIMEOUT,
690 g_task_get_cancellable (task),
691 ch_device_request_cb,
692 task);
693 }
694
695 /* tiny helper to help us do the async operation */
696 typedef struct {
697 GError **error;
698 GMainLoop *loop;
699 gboolean ret;
700 } ChDeviceSyncHelper;
701
702 static void
703 ch_device_write_command_finish_cb (GObject *source,
704 GAsyncResult *res,
705 gpointer user_data)
706 {
707 GUsbDevice *device = G_USB_DEVICE (source);
708 ChDeviceSyncHelper *helper = (ChDeviceSyncHelper *) user_data;
709 helper->ret = ch_device_write_command_finish (device, res, helper->error);
710 g_main_loop_quit (helper->loop);
711 }
712
713 /**
714 * ch_device_write_command:
715 * @device: A #GUsbDevice
716 * @cmd: The command to use, e.g. %CH_CMD_GET_COLOR_SELECT
717 * @buffer_in: The input buffer of data, or %NULL
718 * @buffer_in_len: The input buffer length
719 * @buffer_out: The output buffer of data, or %NULL
720 * @buffer_out_len: The output buffer length
721 * @cancellable: A #GCancellable or %NULL
722 * @error: A #GError, or %NULL
723 *
724 * Sends a message to the device and waits for a reply.
725 *
726 * Return value: %TRUE if the command was executed successfully.
727 *
728 * Since: 0.1.29
729 **/
730 gboolean
731 ch_device_write_command (GUsbDevice *device,
732 guint8 cmd,
733 const guint8 *buffer_in,
734 gsize buffer_in_len,
735 guint8 *buffer_out,
736 gsize buffer_out_len,
737 GCancellable *cancellable,
738 GError **error)
739 {
740 ChDeviceSyncHelper helper;
741
742 /* create temp object */
743 helper.ret = FALSE;
744 helper.loop = g_main_loop_new (NULL, FALSE);
745 helper.error = error;
746
747 /* run async method */
748 ch_device_write_command_async (device,
749 cmd,
750 buffer_in,
751 buffer_in_len,
752 buffer_out,
753 buffer_out_len,
754 cancellable,
755 ch_device_write_command_finish_cb,
756 &helper);
757 g_main_loop_run (helper.loop);
758
759 /* free temp object */
760 g_main_loop_unref (helper.loop);
761
762 return helper.ret;
763 }
764
765 /**
766 * ch_device_check_firmware:
767 * @data: (array length=data_len): firmware binary data
768 * @data_len: size of @data
769 *
770 * Checks the firmware is suitable for the ColorHug device that is attached.
771 *
772 * Return value: %TRUE if the command was executed successfully.
773 *
774 * Since: 1.2.3
775 **/
776 gboolean
777 ch_device_check_firmware (GUsbDevice *device,
778 const guint8 *data,
779 gsize data_len,
780 GError **error)
781 {
782 ChDeviceMode device_mode_fw;
783
784 /* this is only a heuristic */
785 device_mode_fw = ch_device_mode_from_firmware (data, data_len);
786 switch (ch_device_get_mode (device)) {
787 case CH_DEVICE_MODE_LEGACY:
788 case CH_DEVICE_MODE_BOOTLOADER:
789 case CH_DEVICE_MODE_FIRMWARE:
790 /* fw versions < 1.2.2 has no magic bytes */
791 if (device_mode_fw == CH_DEVICE_MODE_FIRMWARE2 ||
792 device_mode_fw == CH_DEVICE_MODE_FIRMWARE_ALS ||
793 device_mode_fw == CH_DEVICE_MODE_FIRMWARE_PLUS) {
794 g_set_error (error,
795 CH_DEVICE_ERROR,
796 CH_ERROR_INVALID_VALUE,
797 "This firmware is not designed for "
798 "ColorHug (identifier is '%s')",
799 ch_device_mode_to_string (device_mode_fw));
800 return FALSE;
801 }
802 break;
803 case CH_DEVICE_MODE_BOOTLOADER2:
804 case CH_DEVICE_MODE_FIRMWARE2:
805 if (device_mode_fw != CH_DEVICE_MODE_FIRMWARE2) {
806 g_set_error (error,
807 CH_DEVICE_ERROR,
808 CH_ERROR_INVALID_VALUE,
809 "This firmware is not designed for "
810 "ColorHug2 (identifier is '%s')",
811 ch_device_mode_to_string (device_mode_fw));
812 return FALSE;
813 }
814 break;
815 case CH_DEVICE_MODE_BOOTLOADER_PLUS:
816 case CH_DEVICE_MODE_FIRMWARE_PLUS:
817 if (device_mode_fw != CH_DEVICE_MODE_FIRMWARE_PLUS) {
818 g_set_error (error,
819 CH_DEVICE_ERROR,
820 CH_ERROR_INVALID_VALUE,
821 "This firmware is not designed for "
822 "ColorHug+ (identifier is '%s')",
823 ch_device_mode_to_string (device_mode_fw));
824 return FALSE;
825 }
826 break;
827 case CH_DEVICE_MODE_BOOTLOADER_ALS:
828 case CH_DEVICE_MODE_FIRMWARE_ALS:
829 if (device_mode_fw != CH_DEVICE_MODE_FIRMWARE_ALS) {
830 g_set_error (error,
831 CH_DEVICE_ERROR,
832 CH_ERROR_INVALID_VALUE,
833 "This firmware is not designed for "
834 "ColorHug ALS (identifier is '%s')",
835 ch_device_mode_to_string (device_mode_fw));
836 return FALSE;
837 }
838 break;
839 default:
840 g_assert_not_reached ();
841 break;
842 }
843 return TRUE;
844 }
845
846 /**
847 * ch_device_get_runcode_address:
848 * @device: A #GUsbDevice
849 *
850 * Returns the runcode address for the ColorHug device.
851 *
852 * Return value: the runcode address, or 0 for error
853 *
854 * Since: 1.2.9
855 **/
856 guint16
857 ch_device_get_runcode_address (GUsbDevice *device)
858 {
859 switch (ch_device_get_mode (device)) {
860 case CH_DEVICE_MODE_LEGACY:
861 case CH_DEVICE_MODE_BOOTLOADER:
862 case CH_DEVICE_MODE_FIRMWARE:
863 case CH_DEVICE_MODE_BOOTLOADER2:
864 case CH_DEVICE_MODE_FIRMWARE2:
865 case CH_DEVICE_MODE_BOOTLOADER_PLUS:
866 case CH_DEVICE_MODE_FIRMWARE_PLUS:
867 return CH_EEPROM_ADDR_RUNCODE;
868 case CH_DEVICE_MODE_FIRMWARE_ALS:
869 case CH_DEVICE_MODE_BOOTLOADER_ALS:
870 return CH_EEPROM_ADDR_RUNCODE_ALS;
871 default:
872 break;
873 }
874 return 0;
875 }
876
877 /**
878 * ch_device_get_guid:
879 * @device: A #GUsbDevice
880 *
881 * Returns the GUID for the connected ColorHug device.
882 *
883 * Return value: the GUID address, or %NULL for error
884 *
885 * Since: 1.2.9
886 **/
887 const gchar *
888 ch_device_get_guid (GUsbDevice *device)
889 {
890 ChDeviceMode mode = ch_device_get_mode (device);
891 if (mode == CH_DEVICE_MODE_LEGACY ||
892 mode == CH_DEVICE_MODE_FIRMWARE ||
893 mode == CH_DEVICE_MODE_BOOTLOADER)
894 return CH_DEVICE_GUID_COLORHUG;
895 if (mode == CH_DEVICE_MODE_FIRMWARE2 ||
896 mode == CH_DEVICE_MODE_BOOTLOADER2)
897 return CH_DEVICE_GUID_COLORHUG2;
898 if (mode == CH_DEVICE_MODE_FIRMWARE_PLUS ||
899 mode == CH_DEVICE_MODE_BOOTLOADER_PLUS)
900 return CH_DEVICE_GUID_COLORHUG_PLUS;
901 if (mode == CH_DEVICE_MODE_FIRMWARE_ALS ||
902 mode == CH_DEVICE_MODE_BOOTLOADER_ALS)
903 return CH_DEVICE_GUID_COLORHUG_ALS;
904 return NULL;
905 }
906
907 static guint8
908 ch_device_get_protocol_ver (GUsbDevice *device)
909 {
910 switch (ch_device_get_mode (device)) {
911 case CH_DEVICE_MODE_BOOTLOADER:
912 case CH_DEVICE_MODE_BOOTLOADER2:
913 case CH_DEVICE_MODE_BOOTLOADER_ALS:
914 case CH_DEVICE_MODE_FIRMWARE:
915 case CH_DEVICE_MODE_FIRMWARE2:
916 case CH_DEVICE_MODE_FIRMWARE_ALS:
917 case CH_DEVICE_MODE_LEGACY:
918 return 0x1;
919 case CH_DEVICE_MODE_FIRMWARE_PLUS:
920 return 0x2;
921 default:
922 break;
923 }
924 return 0x0;
925 }
926
927 static gdouble
928 ch_offset_float_to_double (gint32 tmp)
929 {
930 return (gdouble) tmp / (gdouble) 0xffff;
931 }
932
933 static gint32
934 ch_offset_float_from_double (gdouble tmp)
935 {
936 return tmp * 0xffff;
937 }
938
939 static gboolean
940 ch_device_check_status (GUsbDevice *device, GCancellable *cancellable, GError **error)
941 {
942 ChError status;
943 ChCmd cmd;
944
945 /* hit hardware */
946 if (!ch_device_get_error (device, &status, &cmd, cancellable, error))
947 return FALSE;
948
949 /* formulate error */
950 if (status != CH_ERROR_NONE) {
951 g_set_error (error,
952 G_USB_DEVICE_ERROR,
953 G_USB_DEVICE_ERROR_IO,
954 "Failed, %s(0x%02x) status was %s(0x%02x)",
955 ch_command_to_string (cmd), cmd,
956 ch_strerror (status), status);
957 return FALSE;
958 }
959 return TRUE;
960 }
961
962 /**
963 * ch_device_self_test:
964 * @device: A #GUsbDevice
965 * @cancellable: a #GCancellable, or %NULL
966 * @error: a #GError, or %NULL
967 *
968 * Performs a self test on the device
969 *
970 * Returns: %TRUE for success
971 *
972 * Since: 1.3.1
973 **/
974 gboolean
975 ch_device_self_test (GUsbDevice *device,
976 GCancellable *cancellable,
977 GError **error)
978 {
979 guint8 protocol_ver = ch_device_get_protocol_ver (device);
980
981 if (protocol_ver == 1) {
982 return ch_device_write_command (device,
983 CH_CMD_SELF_TEST,
984 NULL,
985 0,
986 NULL,
987 0,
988 cancellable,
989 error);
990 }
991 if (protocol_ver == 2) {
992 return g_usb_device_control_transfer (device,
993 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
994 G_USB_DEVICE_REQUEST_TYPE_CLASS,
995 G_USB_DEVICE_RECIPIENT_INTERFACE,
996 CH_CMD_SELF_TEST,
997 0, /* wValue */
998 CH_USB_INTERFACE, /* idx */
999 NULL, /* data */
1000 0, /* length */
1001 NULL, /* actual_length */
1002 CH_DEVICE_USB_TIMEOUT,
1003 cancellable,
1004 error);
1005 }
1006 g_set_error_literal (error,
1007 CH_DEVICE_ERROR,
1008 CH_ERROR_NOT_IMPLEMENTED,
1009 "Self test is not supported");
1010 return FALSE;
1011 }
1012
1013 /**
1014 * ch_device_set_serial_number:
1015 * @device: A #GUsbDevice
1016 * @value: serial number
1017 * @cancellable: a #GCancellable, or %NULL
1018 * @error: a #GError, or %NULL
1019 *
1020 * Sets the serial number on the device
1021 *
1022 * Returns: %TRUE for success
1023 *
1024 * Since: 1.3.1
1025 **/
1026 gboolean
1027 ch_device_set_serial_number (GUsbDevice *device, guint32 value,
1028 GCancellable *cancellable, GError **error)
1029 {
1030 guint32 value_le;
1031 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1032
1033 value_le = GUINT32_TO_LE (value);
1034 if (protocol_ver == 1) {
1035 return ch_device_write_command (device,
1036 CH_CMD_SET_SERIAL_NUMBER,
1037 (const guint8 *) &value_le,
1038 sizeof(value_le),
1039 NULL,
1040 0,
1041 cancellable,
1042 error);
1043 }
1044 if (protocol_ver == 2) {
1045 return g_usb_device_control_transfer (device,
1046 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1047 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1048 G_USB_DEVICE_RECIPIENT_INTERFACE,
1049 CH_CMD_SET_SERIAL_NUMBER,
1050 value_le, /* wValue */
1051 CH_USB_INTERFACE, /* idx */
1052 NULL, /* data */
1053 0, /* length */
1054 NULL, /* actual_length */
1055 CH_DEVICE_USB_TIMEOUT,
1056 cancellable,
1057 error);
1058 }
1059 g_set_error_literal (error,
1060 CH_DEVICE_ERROR,
1061 CH_ERROR_NOT_IMPLEMENTED,
1062 "Setting the serial number is not supported");
1063 return FALSE;
1064 }
1065
1066 /**
1067 * ch_device_get_serial_number:
1068 * @device: A #GUsbDevice
1069 * @value: (out): serial number
1070 * @cancellable: a #GCancellable, or %NULL
1071 * @error: a #GError, or %NULL
1072 *
1073 * Gets the serial number from the device.
1074 *
1075 * Returns: %TRUE for success
1076 *
1077 * Since: 1.3.1
1078 **/
1079 gboolean
1080 ch_device_get_serial_number (GUsbDevice *device, guint32 *value,
1081 GCancellable *cancellable, GError **error)
1082 {
1083 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1084
1085 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1086 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1087
1088 if (protocol_ver == 1) {
1089 return ch_device_write_command (device,
1090 CH_CMD_GET_SERIAL_NUMBER,
1091 NULL,
1092 0,
1093 (guint8 *) value,
1094 sizeof(guint32),
1095 cancellable,
1096 error);
1097 }
1098 if (protocol_ver == 2) {
1099 guint8 buf[2];
1100 gsize actual_length;
1101 gboolean ret;
1102 ret = g_usb_device_control_transfer (device,
1103 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1104 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1105 G_USB_DEVICE_RECIPIENT_INTERFACE,
1106 CH_CMD_GET_SERIAL_NUMBER,
1107 0x00, /* wValue */
1108 CH_USB_INTERFACE, /* idx */
1109 buf, /* data */
1110 sizeof(buf), /* length */
1111 &actual_length,
1112 CH_DEVICE_USB_TIMEOUT,
1113 cancellable,
1114 error);
1115 if (!ret)
1116 return FALSE;
1117
1118 /* return result */
1119 if (actual_length != sizeof(buf)) {
1120 g_set_error (error,
1121 G_USB_DEVICE_ERROR,
1122 G_USB_DEVICE_ERROR_IO,
1123 "Invalid size, got %" G_GSIZE_FORMAT,
1124 actual_length);
1125 return FALSE;
1126 }
1127 if (value != NULL)
1128 memcpy (value, buf, sizeof(buf));
1129 return TRUE;
1130 }
1131 g_set_error_literal (error,
1132 CH_DEVICE_ERROR,
1133 CH_ERROR_NOT_IMPLEMENTED,
1134 "Getting the serial number is not supported");
1135 return FALSE;
1136 }
1137
1138 /**
1139 * ch_device_set_leds:
1140 * @device: A #GUsbDevice
1141 * @value: serial number
1142 * @cancellable: a #GCancellable, or %NULL
1143 * @error: a #GError, or %NULL
1144 *
1145 * Sets the LEDs on the device
1146 *
1147 * Returns: %TRUE for success
1148 *
1149 * Since: 1.3.1
1150 **/
1151 gboolean
1152 ch_device_set_leds (GUsbDevice *device, ChStatusLed value,
1153 GCancellable *cancellable, GError **error)
1154 {
1155 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1156
1157 if (protocol_ver == 1) {
1158 guint8 buffer[4] = { value, 0, 0, 0 };
1159 return ch_device_write_command (device,
1160 CH_CMD_SET_LEDS,
1161 (const guint8 *) buffer,
1162 sizeof(buffer),
1163 NULL,
1164 0,
1165 cancellable,
1166 error);
1167 }
1168 if (protocol_ver == 2) {
1169 return g_usb_device_control_transfer (device,
1170 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1171 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1172 G_USB_DEVICE_RECIPIENT_INTERFACE,
1173 CH_CMD_SET_LEDS,
1174 value, /* wValue */
1175 CH_USB_INTERFACE, /* idx */
1176 NULL, /* data */
1177 0, /* length */
1178 NULL, /* actual_length */
1179 CH_DEVICE_USB_TIMEOUT,
1180 cancellable,
1181 error);
1182 }
1183 g_set_error_literal (error,
1184 CH_DEVICE_ERROR,
1185 CH_ERROR_NOT_IMPLEMENTED,
1186 "Setting the LEDs is not supported");
1187 return FALSE;
1188 }
1189
1190 /**
1191 * ch_device_get_leds:
1192 * @device: A #GUsbDevice
1193 * @value: (out): serial number
1194 * @cancellable: a #GCancellable, or %NULL
1195 * @error: a #GError, or %NULL
1196 *
1197 * Gets the LEDs from the device.
1198 *
1199 * Returns: %TRUE for success
1200 *
1201 * Since: 1.3.1
1202 **/
1203 gboolean
1204 ch_device_get_leds (GUsbDevice *device, ChStatusLed *value,
1205 GCancellable *cancellable, GError **error)
1206 {
1207 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1208
1209 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1210 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1211
1212 if (protocol_ver == 1) {
1213 return ch_device_write_command (device,
1214 CH_CMD_GET_LEDS,
1215 NULL,
1216 0,
1217 (guint8 *) value,
1218 sizeof(ChStatusLed),
1219 cancellable,
1220 error);
1221 }
1222 if (protocol_ver == 2) {
1223 guint8 buf[1];
1224 gsize actual_length;
1225 gboolean ret;
1226 ret = g_usb_device_control_transfer (device,
1227 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1228 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1229 G_USB_DEVICE_RECIPIENT_INTERFACE,
1230 CH_CMD_GET_LEDS,
1231 0x00, /* wValue */
1232 CH_USB_INTERFACE, /* idx */
1233 buf, /* data */
1234 sizeof(buf), /* length */
1235 &actual_length,
1236 CH_DEVICE_USB_TIMEOUT,
1237 cancellable,
1238 error);
1239 if (!ret)
1240 return FALSE;
1241
1242 /* return result */
1243 if (actual_length != sizeof(buf)) {
1244 g_set_error (error,
1245 G_USB_DEVICE_ERROR,
1246 G_USB_DEVICE_ERROR_IO,
1247 "Invalid size, got %" G_GSIZE_FORMAT,
1248 actual_length);
1249 return FALSE;
1250 }
1251 if (value != NULL)
1252 memcpy (value, buf, sizeof(buf));
1253 return TRUE;
1254 }
1255 g_set_error_literal (error,
1256 CH_DEVICE_ERROR,
1257 CH_ERROR_NOT_IMPLEMENTED,
1258 "Getting the LEDs is not supported");
1259 return FALSE;
1260 }
1261
1262 /**
1263 * ch_device_set_illuminants:
1264 * @device: A #GUsbDevice
1265 * @value: serial number
1266 * @cancellable: a #GCancellable, or %NULL
1267 * @error: a #GError, or %NULL
1268 *
1269 * Sets the illuminants on the device
1270 *
1271 * Returns: %TRUE for success
1272 *
1273 * Since: 1.3.4
1274 **/
1275 gboolean
1276 ch_device_set_illuminants (GUsbDevice *device, ChIlluminant value,
1277 GCancellable *cancellable, GError **error)
1278 {
1279 if (ch_device_get_protocol_ver (device) != 2) {
1280 g_set_error_literal (error,
1281 CH_DEVICE_ERROR,
1282 CH_ERROR_NOT_IMPLEMENTED,
1283 "Setting the illuminants is not supported");
1284 return FALSE;
1285 }
1286 return g_usb_device_control_transfer (device,
1287 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1288 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1289 G_USB_DEVICE_RECIPIENT_INTERFACE,
1290 CH_CMD_SET_ILLUMINANTS,
1291 value, /* wValue */
1292 CH_USB_INTERFACE, /* idx */
1293 NULL, /* data */
1294 0, /* length */
1295 NULL, /* actual_length */
1296 CH_DEVICE_USB_TIMEOUT,
1297 cancellable,
1298 error);
1299 }
1300
1301 /**
1302 * ch_device_get_illuminants:
1303 * @device: A #GUsbDevice
1304 * @value: (out): serial number
1305 * @cancellable: a #GCancellable, or %NULL
1306 * @error: a #GError, or %NULL
1307 *
1308 * Gets the illuminants from the device.
1309 *
1310 * Returns: %TRUE for success
1311 *
1312 * Since: 1.3.4
1313 **/
1314 gboolean
1315 ch_device_get_illuminants (GUsbDevice *device, ChIlluminant *value,
1316 GCancellable *cancellable, GError **error)
1317 {
1318 guint8 buf[1];
1319 gsize actual_length;
1320 gboolean ret;
1321
1322 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1323 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1324
1325 if (ch_device_get_protocol_ver (device) != 2) {
1326 g_set_error_literal (error,
1327 CH_DEVICE_ERROR,
1328 CH_ERROR_NOT_IMPLEMENTED,
1329 "Getting the illuminants is not supported");
1330 return FALSE;
1331 }
1332 ret = g_usb_device_control_transfer (device,
1333 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1334 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1335 G_USB_DEVICE_RECIPIENT_INTERFACE,
1336 CH_CMD_GET_ILLUMINANTS,
1337 0x00, /* wValue */
1338 CH_USB_INTERFACE, /* idx */
1339 buf, /* data */
1340 sizeof(buf), /* length */
1341 &actual_length,
1342 CH_DEVICE_USB_TIMEOUT,
1343 cancellable,
1344 error);
1345 if (!ret)
1346 return FALSE;
1347
1348 /* return result */
1349 if (actual_length != sizeof(buf)) {
1350 g_set_error (error,
1351 G_USB_DEVICE_ERROR,
1352 G_USB_DEVICE_ERROR_IO,
1353 "Invalid size, got %" G_GSIZE_FORMAT,
1354 actual_length);
1355 return FALSE;
1356 }
1357 if (value != NULL)
1358 memcpy (value, buf, sizeof(buf));
1359 return TRUE;
1360 }
1361
1362 /**
1363 * ch_device_set_pcb_errata:
1364 * @device: A #GUsbDevice
1365 * @value: #ChPcbErrata, e.g. %CH_PCB_ERRATA_SWAPPED_LEDS
1366 * @cancellable: a #GCancellable, or %NULL
1367 * @error: a #GError, or %NULL
1368 *
1369 * Sets any PCB errata on the device
1370 *
1371 * Returns: %TRUE for success
1372 *
1373 * Since: 1.3.1
1374 **/
1375 gboolean
1376 ch_device_set_pcb_errata (GUsbDevice *device, ChPcbErrata value,
1377 GCancellable *cancellable, GError **error)
1378 {
1379 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1380
1381 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1382 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1383
1384 if (protocol_ver == 1) {
1385 gboolean ret;
1386 ret = ch_device_write_command (device,
1387 CH_CMD_SET_PCB_ERRATA,
1388 (guint8 *) &value,
1389 sizeof(guint8),
1390 NULL,
1391 0,
1392 cancellable,
1393 error);
1394 if (!ret)
1395 return FALSE;
1396 return ch_device_write_command (device,
1397 CH_CMD_WRITE_EEPROM,
1398 (guint8 *) CH_WRITE_EEPROM_MAGIC,
1399 strlen (CH_WRITE_EEPROM_MAGIC),
1400 NULL,
1401 0,
1402 cancellable,
1403 error);
1404 }
1405 if (protocol_ver == 2) {
1406 return g_usb_device_control_transfer (device,
1407 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1408 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1409 G_USB_DEVICE_RECIPIENT_INTERFACE,
1410 CH_CMD_SET_PCB_ERRATA,
1411 value, /* wValue */
1412 CH_USB_INTERFACE, /* idx */
1413 NULL, /* data */
1414 0, /* length */
1415 NULL, /* actual_length */
1416 CH_DEVICE_USB_TIMEOUT,
1417 cancellable,
1418 error);
1419 }
1420 g_set_error_literal (error,
1421 CH_DEVICE_ERROR,
1422 CH_ERROR_NOT_IMPLEMENTED,
1423 "Setting the PCB errata is not supported");
1424 return FALSE;
1425 }
1426
1427 /**
1428 * ch_device_get_pcb_errata:
1429 * @device: A #GUsbDevice
1430 * @value: (out): #ChPcbErrata, e.g. %CH_PCB_ERRATA_SWAPPED_LEDS
1431 * @cancellable: a #GCancellable, or %NULL
1432 * @error: a #GError, or %NULL
1433 *
1434 * Gets any PCB errata from the device.
1435 *
1436 * Returns: %TRUE for success
1437 *
1438 * Since: 1.3.1
1439 **/
1440 gboolean
1441 ch_device_get_pcb_errata (GUsbDevice *device, ChPcbErrata *value,
1442 GCancellable *cancellable, GError **error)
1443 {
1444 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1445
1446 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1447 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1448
1449 if (protocol_ver == 1) {
1450 return ch_device_write_command (device,
1451 CH_CMD_GET_PCB_ERRATA,
1452 NULL,
1453 0,
1454 NULL,
1455 0,
1456 cancellable,
1457 error);
1458 }
1459 if (protocol_ver == 2) {
1460 guint8 buf[1];
1461 gsize actual_length;
1462 gboolean ret;
1463 ret = g_usb_device_control_transfer (device,
1464 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1465 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1466 G_USB_DEVICE_RECIPIENT_INTERFACE,
1467 CH_CMD_GET_PCB_ERRATA,
1468 0x00, /* wValue */
1469 CH_USB_INTERFACE, /* idx */
1470 buf, /* data */
1471 sizeof(buf), /* length */
1472 &actual_length,
1473 CH_DEVICE_USB_TIMEOUT,
1474 cancellable,
1475 error);
1476 if (!ret)
1477 return FALSE;
1478
1479 /* return result */
1480 if (actual_length != sizeof(buf)) {
1481 g_set_error (error,
1482 G_USB_DEVICE_ERROR,
1483 G_USB_DEVICE_ERROR_IO,
1484 "Invalid size, got %" G_GSIZE_FORMAT,
1485 actual_length);
1486 return FALSE;
1487 }
1488 if (value != NULL)
1489 *value = buf[0];
1490 return TRUE;
1491 }
1492 g_set_error_literal (error,
1493 CH_DEVICE_ERROR,
1494 CH_ERROR_NOT_IMPLEMENTED,
1495 "Getting the PCB errata is not supported");
1496 return FALSE;
1497 }
1498
1499 /**
1500 * ch_device_set_ccd_calibration:
1501 * @device: A #GUsbDevice
1502 * @cancellable: a #GCancellable, or %NULL
1503 * @error: a #GError, or %NULL
1504 *
1505 * Sets any PCB wavelength_cal on the device
1506 *
1507 * Returns: %TRUE for success
1508 *
1509 * Since: 1.3.1
1510 **/
1511 gboolean
1512 ch_device_set_ccd_calibration (GUsbDevice *device,
1513 gdouble start_nm,
1514 gdouble c0,
1515 gdouble c1,
1516 gdouble c2,
1517 GCancellable *cancellable,
1518 GError **error)
1519 {
1520 gboolean ret;
1521 gint32 buf[4];
1522 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1523
1524 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1525 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1526
1527 if (protocol_ver != 2) {
1528 g_set_error_literal (error,
1529 CH_DEVICE_ERROR,
1530 CH_ERROR_NOT_IMPLEMENTED,
1531 "Setting the CCD calibration is not supported");
1532 return FALSE;
1533 }
1534
1535 /* format data */
1536 buf[0] = ch_offset_float_from_double (start_nm);
1537 buf[1] = ch_offset_float_from_double (c0);
1538 buf[2] = ch_offset_float_from_double (c1) * 1000.f;
1539 buf[3] = ch_offset_float_from_double (c2) * 1000.f;
1540
1541 /* hit hardware */
1542 ret = g_usb_device_control_transfer (device,
1543 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1544 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1545 G_USB_DEVICE_RECIPIENT_INTERFACE,
1546 CH_CMD_SET_CCD_CALIBRATION,
1547 0, /* wValue */
1548 CH_USB_INTERFACE, /* idx */
1549 (guint8 *) buf, /* data */
1550 sizeof(buf), /* length */
1551 NULL, /* actual_length */
1552 CH_DEVICE_USB_TIMEOUT,
1553 cancellable,
1554 error);
1555 if (!ret)
1556 return FALSE;
1557
1558 /* check status */
1559 return ch_device_check_status (device, cancellable, error);
1560 }
1561
1562 /**
1563 * ch_device_set_crypto_key:
1564 * @device: A #GUsbDevice
1565 * @keys: a set of XTEA keys
1566 * @cancellable: a #GCancellable, or %NULL
1567 * @error: a #GError, or %NULL
1568 *
1569 * Sets the firmware signing keys on the device.
1570 *
1571 * IMPORTANT: This can only be called once until the device is unlocked.
1572 *
1573 * Returns: %TRUE for success
1574 *
1575 * Since: 1.3.1
1576 **/
1577 gboolean
1578 ch_device_set_crypto_key (GUsbDevice *device,
1579 guint32 keys[4],
1580 GCancellable *cancellable,
1581 GError **error)
1582 {
1583 gboolean ret;
1584 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1585
1586 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1587 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1588
1589 if (protocol_ver != 2) {
1590 g_set_error_literal (error,
1591 CH_DEVICE_ERROR,
1592 CH_ERROR_NOT_IMPLEMENTED,
1593 "Setting the crypto key is not supported");
1594 return FALSE;
1595 }
1596
1597 /* hit hardware */
1598 ret = g_usb_device_control_transfer (device,
1599 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1600 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1601 G_USB_DEVICE_RECIPIENT_INTERFACE,
1602 CH_CMD_SET_CRYPTO_KEY,
1603 0, /* wValue */
1604 CH_USB_INTERFACE, /* idx */
1605 (guint8 *) keys, /* data */
1606 sizeof(guint32) * 4, /* length */
1607 NULL, /* actual_length */
1608 CH_DEVICE_USB_TIMEOUT,
1609 cancellable,
1610 error);
1611 if (!ret)
1612 return FALSE;
1613
1614 /* check status */
1615 return ch_device_check_status (device, cancellable, error);
1616 }
1617
1618 /**
1619 * ch_device_get_ccd_calibration:
1620 * @device: A #GUsbDevice
1621 * @cancellable: a #GCancellable, or %NULL
1622 * @error: a #GError, or %NULL
1623 *
1624 * Gets any PCB wavelength_cal from the device.
1625 *
1626 * Returns: %TRUE for success
1627 *
1628 * Since: 1.3.1
1629 **/
1630 gboolean
1631 ch_device_get_ccd_calibration (GUsbDevice *device,
1632 gdouble *start_nm,
1633 gdouble *c0,
1634 gdouble *c1,
1635 gdouble *c2,
1636 GCancellable *cancellable,
1637 GError **error)
1638 {
1639 gboolean ret;
1640 gint32 buf[4];
1641 gsize actual_length;
1642 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1643
1644 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1645 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1646
1647 if (protocol_ver != 2) {
1648 g_set_error_literal (error,
1649 CH_DEVICE_ERROR,
1650 CH_ERROR_NOT_IMPLEMENTED,
1651 "Getting the CCD calibration is not supported");
1652 return FALSE;
1653 }
1654
1655 /* hit hardware */
1656 ret = g_usb_device_control_transfer (device,
1657 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1658 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1659 G_USB_DEVICE_RECIPIENT_INTERFACE,
1660 CH_CMD_GET_CCD_CALIBRATION,
1661 0x00, /* wValue */
1662 CH_USB_INTERFACE, /* idx */
1663 (guint8 *) buf, /* data */
1664 sizeof(buf), /* length */
1665 &actual_length,
1666 CH_DEVICE_USB_TIMEOUT,
1667 cancellable,
1668 error);
1669 if (!ret)
1670 return FALSE;
1671
1672 /* return result */
1673 if (actual_length != sizeof(buf)) {
1674 g_set_error (error,
1675 G_USB_DEVICE_ERROR,
1676 G_USB_DEVICE_ERROR_IO,
1677 "Invalid size, got %" G_GSIZE_FORMAT,
1678 actual_length);
1679 return FALSE;
1680 }
1681 if (start_nm != NULL) {
1682 *start_nm = ch_offset_float_to_double (buf[0]);
1683 *c0 = ch_offset_float_to_double (buf[1]);
1684 *c1 = ch_offset_float_to_double (buf[2]) / 1000.f;
1685 *c2 = ch_offset_float_to_double (buf[3]) / 1000.f;
1686 }
1687
1688 /* check status */
1689 return ch_device_check_status (device, cancellable, error);
1690 }
1691
1692 /**
1693 * ch_device_set_integral_time:
1694 * @device: A #GUsbDevice
1695 * @value: integration time in ms
1696 * @cancellable: a #GCancellable, or %NULL
1697 * @error: a #GError, or %NULL
1698 *
1699 * Sets the integration value for the next sample.
1700 *
1701 * Returns: %TRUE for success
1702 *
1703 * Since: 1.3.1
1704 **/
1705 gboolean
1706 ch_device_set_integral_time (GUsbDevice *device, guint16 value,
1707 GCancellable *cancellable, GError **error)
1708 {
1709 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1710
1711 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1712 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1713
1714 if (protocol_ver == 1) {
1715 return ch_device_write_command (device,
1716 CH_CMD_SET_INTEGRAL_TIME,
1717 (guint8 *) &value,
1718 sizeof(guint16),
1719 NULL,
1720 0,
1721 cancellable,
1722 error);
1723 }
1724 if (protocol_ver == 2) {
1725 gboolean ret;
1726 ret = g_usb_device_control_transfer (device,
1727 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
1728 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1729 G_USB_DEVICE_RECIPIENT_INTERFACE,
1730 CH_CMD_SET_INTEGRAL_TIME,
1731 value, /* wValue */
1732 CH_USB_INTERFACE, /* idx */
1733 NULL, /* data */
1734 0, /* length */
1735 NULL, /* actual_length */
1736 CH_DEVICE_USB_TIMEOUT,
1737 cancellable,
1738 error);
1739 return ret;
1740 }
1741 g_set_error_literal (error,
1742 CH_DEVICE_ERROR,
1743 CH_ERROR_NOT_IMPLEMENTED,
1744 "Setting the integral time is not supported");
1745 return FALSE;
1746 }
1747
1748 /**
1749 * ch_device_get_integral_time:
1750 * @device: A #GUsbDevice
1751 * @value: (out): integration time in ms
1752 * @cancellable: a #GCancellable, or %NULL
1753 * @error: a #GError, or %NULL
1754 *
1755 * Gets the integration time used for taking the next samples.
1756 *
1757 * Returns: %TRUE for success
1758 *
1759 * Since: 1.3.1
1760 **/
1761 gboolean
1762 ch_device_get_integral_time (GUsbDevice *device, guint16 *value,
1763 GCancellable *cancellable, GError **error)
1764 {
1765 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1766
1767 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1768 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1769
1770 if (protocol_ver == 1) {
1771 return ch_device_write_command (device,
1772 CH_CMD_GET_INTEGRAL_TIME,
1773 NULL,
1774 0,
1775 (guint8 *) value,
1776 sizeof(guint16),
1777 cancellable,
1778 error);
1779 }
1780 if (protocol_ver == 2) {
1781 guint8 buf[2];
1782 gsize actual_length;
1783 gboolean ret;
1784 ret = g_usb_device_control_transfer (device,
1785 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1786 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1787 G_USB_DEVICE_RECIPIENT_INTERFACE,
1788 CH_CMD_GET_INTEGRAL_TIME,
1789 0x00, /* wValue */
1790 CH_USB_INTERFACE, /* idx */
1791 buf, /* data */
1792 sizeof(buf), /* length */
1793 &actual_length,
1794 CH_DEVICE_USB_TIMEOUT,
1795 cancellable,
1796 error);
1797 if (!ret)
1798 return FALSE;
1799
1800 /* return result */
1801 if (actual_length != sizeof(buf)) {
1802 g_set_error (error,
1803 G_USB_DEVICE_ERROR,
1804 G_USB_DEVICE_ERROR_IO,
1805 "Invalid size, got %" G_GSIZE_FORMAT,
1806 actual_length);
1807 return FALSE;
1808 }
1809 if (value != NULL)
1810 memcpy (value, buf, sizeof(buf));
1811 return TRUE;
1812 }
1813 g_set_error_literal (error,
1814 CH_DEVICE_ERROR,
1815 CH_ERROR_NOT_IMPLEMENTED,
1816 "Getting the integral time is not supported");
1817 return FALSE;
1818 }
1819
1820 /**
1821 * ch_device_get_temperature:
1822 * @device: A #GUsbDevice
1823 * @value: (out): temperature in Celsius
1824 * @cancellable: a #GCancellable, or %NULL
1825 * @error: a #GError, or %NULL
1826 *
1827 * Gets the PCB board temperature from the device.
1828 *
1829 * Returns: %TRUE for success
1830 *
1831 * Since: 1.3.1
1832 **/
1833 gboolean
1834 ch_device_get_temperature (GUsbDevice *device, gdouble *value,
1835 GCancellable *cancellable, GError **error)
1836 {
1837 gboolean ret;
1838 guint8 protocol_ver = ch_device_get_protocol_ver (device);
1839
1840 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1841 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1842
1843 if (protocol_ver == 1) {
1844 ChPackedFloat pf;
1845 ret = ch_device_write_command (device,
1846 CH_CMD_GET_TEMPERATURE,
1847 NULL,
1848 0,
1849 (guint8 *) &pf,
1850 sizeof(ChPackedFloat),
1851 cancellable,
1852 error);
1853 if (!ret)
1854 return FALSE;
1855 ch_packed_float_to_double (&pf, value);
1856 return TRUE;
1857 }
1858 if (protocol_ver == 2) {
1859 gint32 buf[1];
1860 gsize actual_length;
1861 ret = g_usb_device_control_transfer (device,
1862 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1863 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1864 G_USB_DEVICE_RECIPIENT_INTERFACE,
1865 CH_CMD_GET_TEMPERATURE,
1866 0x00, /* wValue */
1867 CH_USB_INTERFACE, /* idx */
1868 (guint8 *) buf, /* data */
1869 sizeof(buf), /* length */
1870 &actual_length,
1871 CH_DEVICE_USB_TIMEOUT,
1872 cancellable,
1873 error);
1874 if (!ret)
1875 return FALSE;
1876
1877 /* return result */
1878 if (actual_length != sizeof(buf)) {
1879 g_set_error (error,
1880 G_USB_DEVICE_ERROR,
1881 G_USB_DEVICE_ERROR_IO,
1882 "Invalid size, got %" G_GSIZE_FORMAT,
1883 actual_length);
1884 return FALSE;
1885 }
1886 if (value != NULL)
1887 *value = ch_offset_float_to_double (buf[0]);
1888 return TRUE;
1889 }
1890 g_set_error_literal (error,
1891 CH_DEVICE_ERROR,
1892 CH_ERROR_NOT_IMPLEMENTED,
1893 "Getting the temperature is not supported");
1894 return FALSE;
1895 }
1896
1897 /**
1898 * ch_device_get_adc_calibration_pos:
1899 * @device: A #GUsbDevice
1900 * @value: (out): offset value
1901 * @cancellable: a #GCancellable, or %NULL
1902 * @error: a #GError, or %NULL
1903 *
1904 * Gets the ADC positive calibration value.
1905 *
1906 * Returns: %TRUE for success
1907 *
1908 * Since: 1.3.4
1909 **/
1910 gboolean
1911 ch_device_get_adc_calibration_pos (GUsbDevice *device, gdouble *value,
1912 GCancellable *cancellable, GError **error)
1913 {
1914 gboolean ret;
1915 gint32 buf[1];
1916 gsize actual_length;
1917
1918 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1919 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1920
1921 if (ch_device_get_protocol_ver (device) != 2) {
1922 g_set_error_literal (error,
1923 CH_DEVICE_ERROR,
1924 CH_ERROR_NOT_IMPLEMENTED,
1925 "Getting the ADC calibration is not supported");
1926 return FALSE;
1927 }
1928 ret = g_usb_device_control_transfer (device,
1929 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1930 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1931 G_USB_DEVICE_RECIPIENT_INTERFACE,
1932 CH_CMD_GET_ADC_CALIBRATION_POS,
1933 0x00, /* wValue */
1934 CH_USB_INTERFACE, /* idx */
1935 (guint8 *) buf, /* data */
1936 sizeof(buf), /* length */
1937 &actual_length,
1938 CH_DEVICE_USB_TIMEOUT,
1939 cancellable,
1940 error);
1941 if (!ret)
1942 return FALSE;
1943
1944 /* return result */
1945 if (actual_length != sizeof(buf)) {
1946 g_set_error (error,
1947 G_USB_DEVICE_ERROR,
1948 G_USB_DEVICE_ERROR_IO,
1949 "Invalid size, got %" G_GSIZE_FORMAT,
1950 actual_length);
1951 return FALSE;
1952 }
1953 if (value != NULL)
1954 *value = ch_offset_float_to_double (buf[0]);
1955 return TRUE;
1956 }
1957
1958 /**
1959 * ch_device_get_adc_calibration_neg:
1960 * @device: A #GUsbDevice
1961 * @value: (out): offset value
1962 * @cancellable: a #GCancellable, or %NULL
1963 * @error: a #GError, or %NULL
1964 *
1965 * Gets the ADC positive calibration value.
1966 *
1967 * Returns: %TRUE for success
1968 *
1969 * Since: 1.3.4
1970 **/
1971 gboolean
1972 ch_device_get_adc_calibration_neg (GUsbDevice *device, gdouble *value,
1973 GCancellable *cancellable, GError **error)
1974 {
1975 gboolean ret;
1976 gint32 buf[1];
1977 gsize actual_length;
1978
1979 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
1980 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1981
1982 if (ch_device_get_protocol_ver (device) != 2) {
1983 g_set_error_literal (error,
1984 CH_DEVICE_ERROR,
1985 CH_ERROR_NOT_IMPLEMENTED,
1986 "Getting the ADC calibration is not supported");
1987 return FALSE;
1988 }
1989 ret = g_usb_device_control_transfer (device,
1990 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
1991 G_USB_DEVICE_REQUEST_TYPE_CLASS,
1992 G_USB_DEVICE_RECIPIENT_INTERFACE,
1993 CH_CMD_GET_ADC_CALIBRATION_NEG,
1994 0x00, /* wValue */
1995 CH_USB_INTERFACE, /* idx */
1996 (guint8 *) buf, /* data */
1997 sizeof(buf), /* length */
1998 &actual_length,
1999 CH_DEVICE_USB_TIMEOUT,
2000 cancellable,
2001 error);
2002 if (!ret)
2003 return FALSE;
2004
2005 /* return result */
2006 if (actual_length != sizeof(buf)) {
2007 g_set_error (error,
2008 G_USB_DEVICE_ERROR,
2009 G_USB_DEVICE_ERROR_IO,
2010 "Invalid size, got %" G_GSIZE_FORMAT,
2011 actual_length);
2012 return FALSE;
2013 }
2014 if (value != NULL)
2015 *value = ch_offset_float_to_double (buf[0]);
2016 return TRUE;
2017 }
2018
2019 /**
2020 * ch_device_get_error:
2021 * @device: A #GUsbDevice
2022 * @status: (out): a #ChError, e.g. %CH_ERROR_INVALID_ADDRESS
2023 * @cmd: (out): a #ChCmd, e.g. %CH_CMD_TAKE_READING_SPECTRAL
2024 * @cancellable: a #GCancellable, or %NULL
2025 * @error: a #GError, or %NULL
2026 *
2027 * Gets the status for the last operation.
2028 *
2029 * Returns: %TRUE for success
2030 *
2031 * Since: 1.3.1
2032 **/
2033 gboolean
2034 ch_device_get_error (GUsbDevice *device, ChError *status, ChCmd *cmd,
2035 GCancellable *cancellable, GError **error)
2036 {
2037 guint8 buf[2];
2038 gsize actual_length;
2039 gboolean ret;
2040 guint8 protocol_ver = ch_device_get_protocol_ver (device);
2041
2042 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
2043 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2044
2045 if (protocol_ver != 2) {
2046 g_set_error_literal (error,
2047 CH_DEVICE_ERROR,
2048 CH_ERROR_NOT_IMPLEMENTED,
2049 "Getting the last error is not supported");
2050 return FALSE;
2051 }
2052 ret = g_usb_device_control_transfer (device,
2053 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
2054 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2055 G_USB_DEVICE_RECIPIENT_INTERFACE,
2056 CH_CMD_GET_ERROR,
2057 0x00, /* wValue */
2058 CH_USB_INTERFACE, /* idx */
2059 buf, /* data */
2060 sizeof(buf), /* length */
2061 &actual_length,
2062 CH_DEVICE_USB_TIMEOUT,
2063 cancellable,
2064 error);
2065 if (!ret)
2066 return FALSE;
2067
2068 /* return result */
2069 if (actual_length != sizeof(buf)) {
2070 g_set_error (error,
2071 G_USB_DEVICE_ERROR,
2072 G_USB_DEVICE_ERROR_IO,
2073 "Invalid size, got %" G_GSIZE_FORMAT,
2074 actual_length);
2075 return FALSE;
2076 }
2077 if (status != NULL)
2078 *status = buf[0];
2079 if (cmd != NULL)
2080 *cmd = buf[1];
2081 return TRUE;
2082 }
2083
2084 /**
2085 * ch_device_open_full:
2086 * @device: A #GUsbDevice
2087 * @cancellable: a #GCancellable, or %NULL
2088 * @error: a #GError, or %NULL
2089 *
2090 * Opens the device ready for use.
2091 *
2092 * Returns: %TRUE for success
2093 *
2094 * Since: 1.3.1
2095 **/
2096 gboolean
2097 ch_device_open_full (GUsbDevice *device, GCancellable *cancellable, GError **error)
2098 {
2099 gboolean ret;
2100 guint8 protocol_ver = ch_device_get_protocol_ver (device);
2101
2102 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
2103 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2104
2105 /* open */
2106 if (!g_usb_device_open (device, error))
2107 return FALSE;
2108
2109 /* claim interface */
2110 if (protocol_ver == 1) {
2111 if (!g_usb_device_set_configuration (device, CH_USB_CONFIG, error))
2112 return FALSE;
2113 if (!g_usb_device_claim_interface (device,
2114 CH_USB_INTERFACE,
2115 G_USB_DEVICE_CLAIM_INTERFACE_BIND_KERNEL_DRIVER,
2116 error))
2117 return FALSE;
2118 return TRUE;
2119 }
2120 if (protocol_ver == 2) {
2121 if (!g_usb_device_claim_interface (device, CH_USB_INTERFACE, 0, error))
2122 return FALSE;
2123 ret = g_usb_device_control_transfer (device,
2124 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
2125 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2126 G_USB_DEVICE_RECIPIENT_INTERFACE,
2127 CH_CMD_CLEAR_ERROR,
2128 0x00, /* wValue */
2129 CH_USB_INTERFACE, /* idx */
2130 NULL, /* data */
2131 0, /* length */
2132 NULL, /* actual_length */
2133 CH_DEVICE_USB_TIMEOUT,
2134 cancellable,
2135 error);
2136 if (!ret)
2137 return FALSE;
2138
2139 /* check status */
2140 return ch_device_check_status (device, cancellable, error);
2141 }
2142 g_set_error_literal (error,
2143 CH_DEVICE_ERROR,
2144 CH_ERROR_NOT_IMPLEMENTED,
2145 "Cannot open this hardware");
2146 return FALSE;
2147 }
2148
2149 static gboolean
2150 ch_device_fixup_error (GUsbDevice *device, GCancellable *cancellable, GError **error)
2151 {
2152 ChError status = 0xff;
2153 ChCmd cmd = 0xff;
2154
2155 /* do we match not supported */
2156 if (error == NULL)
2157 return FALSE;
2158 if (!g_error_matches (*error,
2159 G_USB_DEVICE_ERROR,
2160 G_USB_DEVICE_ERROR_NOT_SUPPORTED))
2161 return FALSE;
2162
2163 /* can we get a error enum from the device */
2164 if (!ch_device_get_error (device, &status, &cmd, cancellable, NULL))
2165 return FALSE;
2166
2167 /* add what we tried to do */
2168 g_prefix_error (error,
2169 "Failed [%s(%02x):%s(%02x)]: ",
2170 ch_command_to_string (cmd), cmd,
2171 ch_strerror (status), status);
2172 return FALSE;
2173 }
2174
2175 /**
2176 * ch_device_take_reading_spectral:
2177 * @device: A #GUsbDevice
2178 * @value: a #ChSpectrumKind, e.g. %CH_SPECTRUM_KIND_RAW
2179 * @cancellable: a #GCancellable, or %NULL
2180 * @error: a #GError, or %NULL
2181 *
2182 * Takes a reading from the device.
2183 *
2184 * Returns: %TRUE for success
2185 *
2186 * Since: 1.3.1
2187 **/
2188 gboolean
2189 ch_device_take_reading_spectral (GUsbDevice *device, ChSpectrumKind value,
2190 GCancellable *cancellable, GError **error)
2191 {
2192 guint8 protocol_ver = ch_device_get_protocol_ver (device);
2193
2194 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
2195 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2196
2197 if (protocol_ver == 2) {
2198 gboolean ret;
2199 ret = g_usb_device_control_transfer (device,
2200 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
2201 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2202 G_USB_DEVICE_RECIPIENT_INTERFACE,
2203 CH_CMD_TAKE_READING_SPECTRAL,
2204 value, /* wValue */
2205 CH_USB_INTERFACE, /* idx */
2206 NULL, /* data */
2207 0, /* length */
2208 NULL, /* actual_length */
2209 CH_DEVICE_USB_TIMEOUT,
2210 cancellable,
2211 error);
2212 if (!ret)
2213 return ch_device_fixup_error (device, cancellable, error);
2214
2215 /* check status */
2216 return ch_device_check_status (device, cancellable, error);
2217 }
2218 g_set_error_literal (error,
2219 CH_DEVICE_ERROR,
2220 CH_ERROR_NOT_IMPLEMENTED,
2221 "Taking spectral data is not supported");
2222 return FALSE;
2223 }
2224
2225 /**
2226 * ch_device_take_reading_xyz:
2227 * @device: A #GUsbDevice
2228 * @calibration_idx: A calibration index or 0 for none
2229 * @cancellable: a #GCancellable, or %NULL
2230 * @error: a #GError, or %NULL
2231 *
2232 * Takes a reading from the device and returns the XYZ value.
2233 *
2234 * Returns: a #CdColorXYZ, or %NULL for error
2235 *
2236 * Since: 1.3.1
2237 **/
2238 CdColorXYZ *
2239 ch_device_take_reading_xyz (GUsbDevice *device, guint16 calibration_idx,
2240 GCancellable *cancellable, GError **error)
2241 {
2242 CdColorXYZ *value;
2243 gboolean ret;
2244 guint8 protocol_ver = ch_device_get_protocol_ver (device);
2245
2246 g_return_val_if_fail (G_USB_DEVICE (device), NULL);
2247 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2248
2249 if (protocol_ver == 1) {
2250 ChPackedFloat pf[3];
2251 ret = ch_device_write_command (device,
2252 CH_CMD_GET_TEMPERATURE,
2253 (guint8 *) &calibration_idx,
2254 sizeof(guint16),
2255 (guint8 *) pf,
2256 sizeof(pf),
2257 cancellable,
2258 error);
2259 if (!ret)
2260 return NULL;
2261
2262 /* parse */
2263 value = cd_color_xyz_new ();
2264 ch_packed_float_to_double (&pf[0], &value->X);
2265 ch_packed_float_to_double (&pf[1], &value->Y);
2266 ch_packed_float_to_double (&pf[2], &value->Z);
2267 return value;
2268 }
2269 if (protocol_ver == 2) {
2270 gsize actual_length;
2271 gint32 buf[3];
2272 ret = g_usb_device_control_transfer (device,
2273 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
2274 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2275 G_USB_DEVICE_RECIPIENT_INTERFACE,
2276 CH_CMD_TAKE_READING_XYZ,
2277 0, /* wValue */
2278 CH_USB_INTERFACE, /* idx */
2279 (guint8 *) buf, /* data */
2280 sizeof(buf), /* length */
2281 &actual_length, /* actual_length */
2282 CH_DEVICE_USB_TIMEOUT,
2283 cancellable,
2284 error);
2285 if (!ret)
2286 return NULL;
2287
2288 /* return result */
2289 if (actual_length != sizeof(buf)) {
2290 g_set_error (error,
2291 G_USB_DEVICE_ERROR,
2292 G_USB_DEVICE_ERROR_IO,
2293 "Invalid size, got %" G_GSIZE_FORMAT,
2294 actual_length);
2295 return NULL;
2296 }
2297
2298 /* check status */
2299 if (!ch_device_check_status (device, cancellable, error))
2300 return NULL;
2301
2302 /* parse */
2303 value = cd_color_xyz_new ();
2304 value->X = ch_offset_float_to_double (buf[0]);
2305 value->Y= ch_offset_float_to_double (buf[1]);
2306 value->Z = ch_offset_float_to_double (buf[2]);
2307 return value;
2308 }
2309 g_set_error_literal (error,
2310 CH_DEVICE_ERROR,
2311 CH_ERROR_NOT_IMPLEMENTED,
2312 "Getting an XYZ value is not supported");
2313 return NULL;
2314 }
2315
2316 #define CH_SPECTRUM_STORAGE_SIZE (CH_CCD_SPECTRAL_RESOLUTION * sizeof(guint16))
2317 #define CH_PIXELS_PER_TRANSFER (CH_EP0_TRANSFER_SIZE_V2 / sizeof(guint16))
2318
2319 /**
2320 * ch_device_set_spectrum_full:
2321 * @device: A #GUsbDevice
2322 * @kind: A #ChSpectrumKind
2323 * @sp: A #CdSpectrum
2324 * @cancellable: a #GCancellable, or %NULL
2325 * @error: a #GError, or %NULL
2326 *
2327 * Sets the spectrum from the device. This sends data multiple multiple times
2328 * until the spectrum has been populated.
2329 *
2330 * Returns: %TRUE for success
2331 *
2332 * Since: 1.3.4
2333 **/
2334 gboolean
2335 ch_device_set_spectrum_full (GUsbDevice *device,
2336 ChSpectrumKind kind,
2337 CdSpectrum *sp,
2338 GCancellable *cancellable,
2339 GError **error)
2340 {
2341 gboolean ret;
2342 guint16 buf[CH_PIXELS_PER_TRANSFER];
2343 gsize actual_length;
2344 guint16 i;
2345 guint16 j;
2346 guint cnt = 0;
2347 guint8 protocol_ver = ch_device_get_protocol_ver (device);
2348 g_autoptr(CdSpectrum) sp_resampled = NULL;
2349
2350 g_return_val_if_fail (G_USB_DEVICE (device), FALSE);
2351 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
2352
2353 if (protocol_ver != 2) {
2354 g_set_error_literal (error,
2355 CH_DEVICE_ERROR,
2356 CH_ERROR_NOT_IMPLEMENTED,
2357 "Setting a spectrum is not supported");
2358 return FALSE;
2359 }
2360
2361 /* ensure spectrum has 1024 elements */
2362 if (cd_spectrum_get_size (sp) != CH_CCD_SPECTRAL_RESOLUTION) {
2363 g_debug ("resampling sample from %u points to 1024",
2364 cd_spectrum_get_size (sp));
2365 sp_resampled = cd_spectrum_resample_to_size (sp, 1024);
2366 } else {
2367 sp_resampled = cd_spectrum_dup (sp);
2368 }
2369
2370 /* i = address in SRAM */
2371 for (i = 0; i < CH_SPECTRUM_STORAGE_SIZE; i += CH_EP0_TRANSFER_SIZE_V2) {
2372
2373 /* create normalised data blob */
2374 for (j = 0; j < CH_PIXELS_PER_TRANSFER; j++) {
2375 guint16 tmp;
2376 gdouble val = cd_spectrum_get_value (sp_resampled, cnt + j);
2377 if (val < 0.f || val > 1.f) {
2378 g_set_error (error,
2379 CH_DEVICE_ERROR,
2380 CH_ERROR_NOT_IMPLEMENTED,
2381 "expected normalised data for %u, got %f",
2382 j, val);
2383 return FALSE;
2384 }
2385 tmp = val * (gdouble) 0xffff;
2386 memcpy (&buf[j], &tmp, sizeof (guint16));
2387 }
2388 cnt += j;
2389
2390 /* send to device */
2391 ret = g_usb_device_control_transfer (device,
2392 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
2393 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2394 G_USB_DEVICE_RECIPIENT_INTERFACE,
2395 CH_CMD_WRITE_SRAM,
2396 i + (kind * CH_SPECTRUM_STORAGE_SIZE),/* wValue */
2397 CH_USB_INTERFACE, /* idx */
2398 (guint8 *) buf, /* data */
2399 sizeof(buf), /* length */
2400 &actual_length, /* actual_length */
2401 CH_DEVICE_USB_TIMEOUT,
2402 cancellable,
2403 error);
2404 if (!ret)
2405 return FALSE;
2406 if (actual_length != sizeof(buf)) {
2407 g_set_error (error,
2408 G_USB_DEVICE_ERROR,
2409 G_USB_DEVICE_ERROR_IO,
2410 "Failed to get spectrum data, got %" G_GSIZE_FORMAT,
2411 actual_length);
2412 return FALSE;
2413 }
2414 }
2415
2416 /* check status */
2417 if (!ch_device_check_status (device, cancellable, error))
2418 return FALSE;
2419
2420 /* sync to eeprom */
2421 if (!ch_device_save_sram (device, cancellable, error))
2422 return FALSE;
2423
2424 /* success */
2425 return TRUE;
2426 }
2427
2428 /**
2429 * ch_device_get_spectrum_full:
2430 * @device: A #GUsbDevice
2431 * @kind: A #ChSpectrumKind
2432 * @cancellable: a #GCancellable, or %NULL
2433 * @error: a #GError, or %NULL
2434 *
2435 * Gets the spectrum from the device. This queries the device multiple times
2436 * until the spectrum has been populated.
2437 *
2438 * Returns: a #CdSpectrum, or %NULL for error
2439 *
2440 * Since: 1.3.4
2441 **/
2442 CdSpectrum *
2443 ch_device_get_spectrum_full (GUsbDevice *device,
2444 ChSpectrumKind kind,
2445 GCancellable *cancellable,
2446 GError **error)
2447 {
2448 gboolean ret;
2449 guint16 buf[CH_PIXELS_PER_TRANSFER];
2450 gsize actual_length;
2451 guint16 i;
2452 guint16 j;
2453 guint8 protocol_ver = ch_device_get_protocol_ver (device);
2454 g_autoptr(CdSpectrum) sp = NULL;
2455
2456 g_return_val_if_fail (G_USB_DEVICE (device), NULL);
2457 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2458
2459 /* populate ahead of time for each chunk */
2460 sp = cd_spectrum_new ();
2461
2462 if (protocol_ver != 2) {
2463 g_set_error_literal (error,
2464 CH_DEVICE_ERROR,
2465 CH_ERROR_NOT_IMPLEMENTED,
2466 "Getting a spectrum is not supported");
2467 return NULL;
2468 }
2469
2470 /* i = address in SRAM */
2471 for (i = 0; i < CH_SPECTRUM_STORAGE_SIZE; i += CH_EP0_TRANSFER_SIZE_V2) {
2472 ret = g_usb_device_control_transfer (device,
2473 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
2474 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2475 G_USB_DEVICE_RECIPIENT_INTERFACE,
2476 CH_CMD_READ_SRAM,
2477 i + (kind * CH_SPECTRUM_STORAGE_SIZE),/* wValue */
2478 CH_USB_INTERFACE, /* idx */
2479 (guint8 *) buf, /* data */
2480 sizeof(buf), /* length */
2481 &actual_length, /* actual_length */
2482 CH_DEVICE_USB_TIMEOUT,
2483 cancellable,
2484 error);
2485 if (!ret)
2486 return NULL;
2487 if (actual_length != sizeof(buf)) {
2488 g_set_error (error,
2489 G_USB_DEVICE_ERROR,
2490 G_USB_DEVICE_ERROR_IO,
2491 "Failed to get spectrum data, got %" G_GSIZE_FORMAT,
2492 actual_length);
2493 return NULL;
2494 }
2495
2496 /* add data */
2497 for (j = 0; j < CH_PIXELS_PER_TRANSFER; j++) {
2498 gdouble tmp = (gdouble) buf[j] / (gdouble) 0xffff;
2499 cd_spectrum_add_value (sp, tmp);
2500 }
2501 }
2502
2503 /* check status */
2504 if (!ch_device_check_status (device, cancellable, error))
2505 return NULL;
2506
2507 /* return copy */
2508 return cd_spectrum_dup (sp);
2509 }
2510
2511 /**
2512 * ch_device_get_spectrum:
2513 * @device: A #GUsbDevice
2514 * @cancellable: a #GCancellable, or %NULL
2515 * @error: a #GError, or %NULL
2516 *
2517 * Gets the spectrum from the device. This queries the device multiple times
2518 * until the spectrum has been populated.
2519 *
2520 * Returns: a #CdSpectrum, or %NULL for error
2521 *
2522 * Since: 1.3.1
2523 **/
2524 CdSpectrum *
2525 ch_device_get_spectrum (GUsbDevice *device, GCancellable *cancellable, GError **error)
2526 {
2527 return ch_device_get_spectrum_full (device, CH_SPECTRUM_KIND_RAW,
2528 cancellable, error);
2529 }
2530
2531 /**
2532 * ch_device_save_sram:
2533 * @device: A #GUsbDevice
2534 * @cancellable: a #GCancellable, or %NULL
2535 * @error: a #GError, or %NULL
2536 *
2537 * Saves the entire SRAM space into the device EEPROM.
2538 *
2539 * Returns: %TRUE for success
2540 *
2541 * Since: 1.3.4
2542 **/
2543 gboolean
2544 ch_device_save_sram (GUsbDevice *device,
2545 GCancellable *cancellable,
2546 GError **error)
2547 {
2548 gboolean ret;
2549
2550 if (ch_device_get_protocol_ver (device) != 2) {
2551 g_set_error_literal (error,
2552 CH_DEVICE_ERROR,
2553 CH_ERROR_NOT_IMPLEMENTED,
2554 "saving SRAM not supported");
2555 return FALSE;
2556 }
2557
2558 /* save SRAM */
2559 ret = g_usb_device_control_transfer (device,
2560 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
2561 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2562 G_USB_DEVICE_RECIPIENT_INTERFACE,
2563 CH_CMD_SAVE_SRAM,
2564 0, /* wValue */
2565 CH_USB_INTERFACE, /* idx */
2566 NULL, /* data */
2567 0, /* length */
2568 NULL, /* actual_length */
2569 CH_DEVICE_USB_TIMEOUT,
2570 cancellable,
2571 error);
2572 if (!ret)
2573 return FALSE;
2574
2575 /* check status */
2576 if (!ch_device_check_status (device, cancellable, error))
2577 return FALSE;
2578
2579 /* success */
2580 return TRUE;
2581 }
2582
2583 /**
2584 * ch_device_load_sram:
2585 * @device: A #GUsbDevice
2586 * @cancellable: a #GCancellable, or %NULL
2587 * @error: a #GError, or %NULL
2588 *
2589 * Loads the entire SRAM from the device EEPROM.
2590 *
2591 * Returns: %TRUE for success
2592 *
2593 * Since: 1.3.4
2594 **/
2595 gboolean
2596 ch_device_load_sram (GUsbDevice *device,
2597 GCancellable *cancellable,
2598 GError **error)
2599 {
2600 gboolean ret;
2601
2602 if (ch_device_get_protocol_ver (device) != 2) {
2603 g_set_error_literal (error,
2604 CH_DEVICE_ERROR,
2605 CH_ERROR_NOT_IMPLEMENTED,
2606 "saving SRAM not supported");
2607 return FALSE;
2608 }
2609
2610 /* save SRAM */
2611 ret = g_usb_device_control_transfer (device,
2612 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
2613 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2614 G_USB_DEVICE_RECIPIENT_INTERFACE,
2615 CH_CMD_LOAD_SRAM,
2616 0, /* wValue */
2617 CH_USB_INTERFACE, /* idx */
2618 NULL, /* data */
2619 0, /* length */
2620 NULL, /* actual_length */
2621 CH_DEVICE_USB_TIMEOUT,
2622 cancellable,
2623 error);
2624 if (!ret)
2625 return FALSE;
2626
2627 /* check status */
2628 if (!ch_device_check_status (device, cancellable, error))
2629 return FALSE;
2630
2631 /* success */
2632 return TRUE;
2633 }
2634
2635 /**
2636 * ch_device_write_sram:
2637 * @device: A #GUsbDevice
2638 * @data: A #GBytes
2639 * @cancellable: a #GCancellable, or %NULL
2640 * @error: a #GError, or %NULL
2641 *
2642 * Loads the entire SRAM from the device EEPROM.
2643 *
2644 * Returns: %TRUE for success
2645 *
2646 * Since: 1.3.4
2647 **/
2648 gboolean
2649 ch_device_write_sram (GUsbDevice *device,
2650 guint16 addr,
2651 GBytes *data,
2652 GCancellable *cancellable,
2653 GError **error)
2654 {
2655 const guint8 *dataptr;
2656 gboolean ret;
2657 gsize len;
2658
2659 if (ch_device_get_protocol_ver (device) != 2) {
2660 g_set_error_literal (error,
2661 CH_DEVICE_ERROR,
2662 CH_ERROR_NOT_IMPLEMENTED,
2663 "writing SRAM not supported");
2664 return FALSE;
2665 }
2666
2667 /* write */
2668 dataptr = g_bytes_get_data (data, &len);
2669 if (len > CH_EP0_TRANSFER_SIZE_V2) {
2670 g_set_error_literal (error,
2671 CH_DEVICE_ERROR,
2672 CH_ERROR_NOT_IMPLEMENTED,
2673 "data blob too large");
2674 return FALSE;
2675 }
2676 ret = g_usb_device_control_transfer (device,
2677 G_USB_DEVICE_DIRECTION_HOST_TO_DEVICE,
2678 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2679 G_USB_DEVICE_RECIPIENT_INTERFACE,
2680 CH_CMD_WRITE_SRAM,
2681 addr, /* wValue */
2682 CH_USB_INTERFACE, /* idx */
2683 dataptr, /* data */
2684 len, /* length */
2685 NULL, /* actual_length */
2686 CH_DEVICE_USB_TIMEOUT,
2687 cancellable,
2688 error);
2689 if (!ret)
2690 return FALSE;
2691
2692 /* check status */
2693 if (!ch_device_check_status (device, cancellable, error))
2694 return FALSE;
2695
2696 /* success */
2697 return TRUE;
2698 }
2699
2700 /**
2701 * ch_device_read_sram:
2702 * @device: A #GUsbDevice
2703 * @cancellable: a #GCancellable, or %NULL
2704 * @error: a #GError, or %NULL
2705 *
2706 * Reads a value from the SRAM.
2707 *
2708 * Returns: %TRUE for success
2709 *
2710 * Since: 1.3.4
2711 **/
2712 GBytes *
2713 ch_device_read_sram (GUsbDevice *device,
2714 guint16 addr,
2715 guint16 len,
2716 GCancellable *cancellable,
2717 GError **error)
2718 {
2719 gboolean ret;
2720 guint8 buf[CH_EP0_TRANSFER_SIZE_V2];
2721 gsize actual_length;
2722
2723 if (ch_device_get_protocol_ver (device) != 2) {
2724 g_set_error_literal (error,
2725 CH_DEVICE_ERROR,
2726 CH_ERROR_NOT_IMPLEMENTED,
2727 "reading SRAM not supported");
2728 return NULL;
2729 }
2730
2731 /* too big */
2732 if (len > CH_EP0_TRANSFER_SIZE_V2) {
2733 g_set_error_literal (error,
2734 CH_DEVICE_ERROR,
2735 CH_ERROR_NOT_IMPLEMENTED,
2736 "length too large");
2737 return NULL;
2738 }
2739
2740 /* clear the buffer */
2741 memset(buf, 0xbe, sizeof(buf));
2742
2743 /* read SRAM */
2744 ret = g_usb_device_control_transfer (device,
2745 G_USB_DEVICE_DIRECTION_DEVICE_TO_HOST,
2746 G_USB_DEVICE_REQUEST_TYPE_CLASS,
2747 G_USB_DEVICE_RECIPIENT_INTERFACE,
2748 CH_CMD_READ_SRAM,
2749 addr, /* wValue */
2750 CH_USB_INTERFACE, /* idx */
2751 buf, /* data */
2752 len, /* length */
2753 &actual_length, /* actual_length */
2754 CH_DEVICE_USB_TIMEOUT,
2755 cancellable,
2756 error);
2757 if (!ret)
2758 return NULL;
2759
2760 /* check size returned */
2761 if (actual_length != len) {
2762 g_set_error (error,
2763 G_USB_DEVICE_ERROR,
2764 G_USB_DEVICE_ERROR_IO,
2765 "failed to get SRAM data, got %" G_GSIZE_FORMAT,
2766 actual_length);
2767 return NULL;
2768 }
2769
2770 /* check status */
2771 if (!ch_device_check_status (device, cancellable, error))
2772 return NULL;
2773
2774 /* success */
2775 return g_bytes_new (buf, len);
2776 }