Debian Codegraph @

lib/colorhug/ch-device-queue.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2012-2013 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-object.h>
25 #include <gio/gio.h>
26 #include <gusb.h>
27 #include <string.h>
28 #include <lcms2.h>
29
30 #include "ch-common.h"
31 #include "ch-device.h"
32 #include "ch-device-queue.h"
33 #include "ch-math.h"
34
35 static void ch_device_queue_finalize (GObject *object);
36
37 #define GET_PRIVATE(o) (ch_device_queue_get_instance_private (o))
38
39 /**
40 * ChDeviceQueuePrivate:
41 *
42 * Private #ChDeviceQueue data
43 **/
44 typedef struct
45 {
46 GPtrArray *data_array;
47 GHashTable *devices_in_use;
48 } ChDeviceQueuePrivate;
49
50 enum {
51 SIGNAL_DEVICE_FAILED,
52 SIGNAL_PROGRESS_CHANGED,
53 SIGNAL_LAST
54 };
55
56 G_DEFINE_TYPE_WITH_PRIVATE (ChDeviceQueue, ch_device_queue, G_TYPE_OBJECT)
57
58 typedef gboolean (*ChDeviceQueueParseFunc) (guint8 *output_buffer,
59 gsize output_buffer_size,
60 gpointer user_data,
61 GError **error);
62
63 typedef enum {
64 CH_DEVICE_QUEUE_DATA_STATE_PENDING,
65 CH_DEVICE_QUEUE_DATA_STATE_WAITING_FOR_HW,
66 CH_DEVICE_QUEUE_DATA_STATE_CANCELLED,
67 CH_DEVICE_QUEUE_DATA_STATE_COMPLETE,
68 CH_DEVICE_QUEUE_DATA_STATE_UNKNOWN
69 } ChDeviceQueueDataState;
70
71 typedef struct {
72 ChDeviceQueueDataState state;
73 GUsbDevice *device;
74 guint8 cmd;
75 guint8 *buffer_in;
76 gsize buffer_in_len;
77 guint8 *buffer_out;
78 gsize buffer_out_len;
79 GDestroyNotify buffer_out_destroy_func;
80 ChDeviceQueueParseFunc parse_func;
81 gpointer user_data;
82 GDestroyNotify user_data_destroy_func;
83 } ChDeviceQueueData;
84
85 typedef struct {
86 ChDeviceQueue *device_queue;
87 ChDeviceQueueProcessFlags process_flags;
88 GCancellable *cancellable;
89 GSimpleAsyncResult *res;
90 GPtrArray *failures;
91 } ChDeviceQueueTaskData;
92
93 static guint signals[SIGNAL_LAST] = { 0 };
94
95 static gboolean ch_device_queue_process_data (GTask *task, ChDeviceQueueData *data);
96
97 static void
98 ch_device_queue_data_free (ChDeviceQueueData *data)
99 {
100 if (data->buffer_out_destroy_func != NULL)
101 data->buffer_out_destroy_func (data->buffer_out);
102 if (data->user_data_destroy_func != NULL)
103 data->user_data_destroy_func (data->user_data);
104 g_free (data->buffer_in);
105 g_object_unref (data->device);
106 g_free (data);
107 }
108
109 static void
110 ch_device_queue_task_data_free (ChDeviceQueueTaskData *data)
111 {
112 g_object_unref (data->device_queue);
113 g_ptr_array_unref (data->failures);
114 g_free (data);
115 }
116
117 static void
118 ch_device_queue_device_force_complete (ChDeviceQueue *device_queue, GUsbDevice *device)
119 {
120 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
121 ChDeviceQueueData *data;
122 const gchar *device_id;
123 const gchar *device_id_tmp;
124 guint i;
125
126 /* go through the list of commands and cancel them all */
127 device_id = g_usb_device_get_platform_id (device);
128 for (i = 0; i < priv->data_array->len; i++) {
129 data = g_ptr_array_index (priv->data_array, i);
130 device_id_tmp = g_usb_device_get_platform_id (data->device);
131 if (g_strcmp0 (device_id_tmp, device_id) == 0)
132 data->state = CH_DEVICE_QUEUE_DATA_STATE_CANCELLED;
133 }
134 }
135
136 static void
137 ch_device_queue_update_progress (ChDeviceQueue *device_queue)
138 {
139 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
140 guint complete = 0;
141 guint i;
142 guint percentage;
143 ChDeviceQueueData *data;
144
145 /* no devices */
146 if (priv->data_array->len == 0)
147 return;
148
149 /* find out how many commands are complete */
150 for (i = 0; i < priv->data_array->len; i++) {
151 data = g_ptr_array_index (priv->data_array, i);
152 if (data->state == CH_DEVICE_QUEUE_DATA_STATE_COMPLETE ||
153 data->state == CH_DEVICE_QUEUE_DATA_STATE_CANCELLED)
154 complete++;
155 }
156
157 /* emit a signal with our progress */
158 percentage = (complete * 100) / priv->data_array->len;
159 g_signal_emit (device_queue,
160 signals[SIGNAL_PROGRESS_CHANGED], 0,
161 percentage);
162 }
163
164 static guint
165 ch_device_queue_count_in_state (ChDeviceQueue *device_queue,
166 ChDeviceQueueDataState state)
167 {
168 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
169 guint i;
170 guint cnt = 0;
171 ChDeviceQueueData *data;
172
173 /* find any data objects in a specific state */
174 for (i = 0; i < priv->data_array->len; i++) {
175 data = g_ptr_array_index (priv->data_array, i);
176 if (data->state == state)
177 cnt++;
178 }
179 return cnt;
180 }
181
182 static void
183 ch_device_queue_process_write_command_cb (GObject *source,
184 GAsyncResult *res,
185 gpointer user_data)
186 {
187 ChDeviceQueueData *data;
188 GTask *task = G_TASK (user_data);
189 ChDeviceQueueTaskData *tdata = g_task_get_task_data (task);
190 ChDeviceQueue *device_queue = CH_DEVICE_QUEUE (tdata->device_queue);
191 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
192 const gchar *device_id;
193 const gchar *tmp;
194 gboolean ret;
195 g_autoptr(GError) error = NULL;
196 guint i;
197 guint pending_commands;
198 ChError last_error_code = 0;
199 GUsbDevice *device = G_USB_DEVICE (source);
200 g_autofree gchar *error_msg = NULL;
201
202 /* mark it as not in use */
203 device_id = g_usb_device_get_platform_id (device);
204 data = g_hash_table_lookup (priv->devices_in_use, device_id);
205 g_hash_table_remove (priv->devices_in_use, device_id);
206
207 /* get data */
208 ret = ch_device_write_command_finish (device, res, &error);
209 if (ret && data->parse_func != NULL) {
210 /* do any conversion function */
211 ret = data->parse_func (data->buffer_out,
212 data->buffer_out_len,
213 data->user_data,
214 &error);
215 }
216 if (!ret) {
217 /* tell the client the device has failed */
218 g_debug ("emit device-failed: %s", error->message);
219 g_signal_emit (device_queue,
220 signals[SIGNAL_DEVICE_FAILED], 0,
221 device,
222 error->message);
223
224 /* save this so we can possibly use when we're done */
225 last_error_code = error->code;
226 g_ptr_array_add (tdata->failures,
227 g_strdup_printf ("%s: %s",
228 g_usb_device_get_platform_id (device),
229 error->message));
230
231 /* should we mark complete other commands as complete */
232 if ((tdata->process_flags & CH_DEVICE_QUEUE_PROCESS_FLAGS_CONTINUE_ERRORS) == 0) {
233 ch_device_queue_device_force_complete (device_queue, device);
234 ch_device_queue_update_progress (device_queue);
235 goto out;
236 }
237 }
238
239 /* update progress */
240 data->state = CH_DEVICE_QUEUE_DATA_STATE_COMPLETE;
241 ch_device_queue_update_progress (device_queue);
242
243 /* is there another pending command for this device */
244 for (i = 0; i < priv->data_array->len; i++) {
245 data = g_ptr_array_index (priv->data_array, i);
246 ret = ch_device_queue_process_data (task, data);
247 if (ret)
248 break;
249 }
250 out:
251 /* any more pending commands? */
252 pending_commands = ch_device_queue_count_in_state (device_queue,
253 CH_DEVICE_QUEUE_DATA_STATE_PENDING);
254 pending_commands += ch_device_queue_count_in_state (device_queue,
255 CH_DEVICE_QUEUE_DATA_STATE_WAITING_FOR_HW);
256 g_debug ("Pending commands: %u", pending_commands);
257 if (pending_commands == 0) {
258
259 /* should we return the process with an error, or just
260 * rely on the signal? */
261 if (tdata->failures->len == 1 &&
262 (tdata->process_flags & CH_DEVICE_QUEUE_PROCESS_FLAGS_NONFATAL_ERRORS) == 0) {
263 tmp = g_ptr_array_index (tdata->failures, 0);
264 g_task_return_new_error (task,
265 CH_DEVICE_ERROR,
266 last_error_code,
267 "%s", tmp);
268 } else if (tdata->failures->len > 1 &&
269 (tdata->process_flags & CH_DEVICE_QUEUE_PROCESS_FLAGS_NONFATAL_ERRORS) == 0) {
270 g_ptr_array_add (tdata->failures, NULL);
271 error_msg = g_strjoinv (", ", (gchar**) tdata->failures->pdata);
272 g_task_return_new_error (task,
273 CH_DEVICE_ERROR,
274 last_error_code,
275 "There were %u failures: %s",
276 tdata->failures->len - 1,
277 error_msg);
278 } else {
279 g_task_return_boolean (task, TRUE);
280 }
281
282 /* remove all commands from the queue, as they are done */
283 #if 0
284 /* FIXME: some sort of race uncovered in the port to GTask means that we try
285 * to remove data objects before they are marked complete; for now disable */
286
287 g_ptr_array_set_size (priv->data_array, 0);
288 #endif
289 g_object_unref (task);
290 }
291 }
292
293 /**
294 * ch_device_queue_process_data:
295 *
296 * Returns TRUE if the command was submitted
297 **/
298 static gboolean
299 ch_device_queue_process_data (GTask *task, ChDeviceQueueData *data)
300 {
301 ChDeviceQueue *device_queue = CH_DEVICE_QUEUE (g_task_get_source_object (task));
302 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
303 ChDeviceQueueData *data_tmp;
304 const gchar *device_id;
305
306 /* is this command already complete? */
307 if (data->state == CH_DEVICE_QUEUE_DATA_STATE_COMPLETE)
308 return FALSE;
309
310 /* is this device already busy? */
311 device_id = g_usb_device_get_platform_id (data->device);
312 data_tmp = g_hash_table_lookup (priv->devices_in_use, device_id);
313 if (data_tmp != NULL)
314 return FALSE;
315
316 /* write this command and wait for a response */
317 ch_device_write_command_async (data->device,
318 data->cmd,
319 data->buffer_in,
320 data->buffer_in_len,
321 data->buffer_out,
322 data->buffer_out_len,
323 g_task_get_cancellable (task),
324 ch_device_queue_process_write_command_cb,
325 task);
326 /* mark this as in use */
327 g_hash_table_insert (priv->devices_in_use, g_strdup (device_id), data);
328
329 /* remove this from the command queue -- TODO: retries? */
330 data->state = CH_DEVICE_QUEUE_DATA_STATE_WAITING_FOR_HW;
331 return TRUE;
332 }
333
334 /**
335 * ch_device_queue_process_async:
336 * @device_queue: A #ChDeviceQueue
337 * @cancellable: A #GCancellable, or %NULL
338 * @callback: A #GAsyncReadyCallback that will be called when finished.
339 * @user_data: User data passed to @callback
340 *
341 * Processes all commands in the command queue.
342 *
343 * Since: 0.1.29
344 **/
345 void
346 ch_device_queue_process_async (ChDeviceQueue *device_queue,
347 ChDeviceQueueProcessFlags process_flags,
348 GCancellable *cancellable,
349 GAsyncReadyCallback callback,
350 gpointer user_data)
351 {
352 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
353 ChDeviceQueueTaskData *tdata;
354 ChDeviceQueueData *data;
355 GTask *task = NULL;
356 guint i;
357
358 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
359 g_return_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable));
360
361 task = g_task_new (device_queue, cancellable, callback, user_data);
362 tdata = g_new0 (ChDeviceQueueTaskData, 1);
363 tdata->process_flags = process_flags;
364 tdata->device_queue = g_object_ref (device_queue);
365 tdata->failures = g_ptr_array_new_with_free_func (g_free);
366 g_task_set_task_data (task, tdata, (GDestroyNotify) ch_device_queue_task_data_free);
367
368 /* go through the list of commands and try to submit them all */
369 ch_device_queue_update_progress (device_queue);
370 for (i = 0; i < priv->data_array->len; i++) {
371 data = g_ptr_array_index (priv->data_array, i);
372 ch_device_queue_process_data (task, data);
373 }
374
375 /* is anything pending? */
376 if (g_hash_table_size (priv->devices_in_use) == 0) {
377 g_task_return_boolean (task, TRUE);
378 g_object_unref (task);
379 }
380 }
381
382 /**
383 * ch_device_queue_process_finish:
384 * @device_queue: a #ChDeviceQueue instance.
385 * @res: the #GAsyncResult
386 * @error: A #GError or %NULL
387 *
388 * Gets the result from the asynchronous function.
389 *
390 * Return value: %TRUE if the request was fulfilled.
391 *
392 * Since: 0.1.29
393 **/
394 gboolean
395 ch_device_queue_process_finish (ChDeviceQueue *device_queue,
396 GAsyncResult *res,
397 GError **error)
398 {
399 g_return_val_if_fail (g_task_is_valid (res, device_queue), FALSE);
400 return g_task_propagate_boolean (G_TASK (res), error);
401 }
402
403 /**********************************************************************/
404
405 /* tiny helper to help us do the async operation */
406 typedef struct {
407 GError **error;
408 GMainLoop *loop;
409 gboolean ret;
410 } ChDeviceQueueSyncHelper;
411
412 static void
413 ch_device_queue_process_finish_cb (GObject *source,
414 GAsyncResult *res,
415 gpointer user_data)
416 {
417 ChDeviceQueue *device_queue = CH_DEVICE_QUEUE (source);
418 ChDeviceQueueSyncHelper *helper = (ChDeviceQueueSyncHelper *) user_data;
419 helper->ret = ch_device_queue_process_finish (device_queue, res, helper->error);
420 g_main_loop_quit (helper->loop);
421 }
422
423 /**
424 * ch_device_queue_process:
425 * @device_queue: A #ChDeviceQueue
426 * @process_flags: Flags how to process the queue, e.g. %CH_DEVICE_QUEUE_PROCESS_FLAGS_CONTINUE_ERRORS
427 * @cancellable: #GCancellable or %NULL
428 * @error: A #GError, or %NULL
429 *
430 * Processes all commands in the command queue.
431 * WARNING: this function is synchronous and will block.
432 *
433 * Return value: %TRUE if the commands were executed successfully.
434 *
435 * Since: 0.1.29
436 **/
437 gboolean
438 ch_device_queue_process (ChDeviceQueue *device_queue,
439 ChDeviceQueueProcessFlags process_flags,
440 GCancellable *cancellable,
441 GError **error)
442 {
443 ChDeviceQueueSyncHelper helper;
444
445 g_return_val_if_fail (CH_IS_DEVICE_QUEUE (device_queue), FALSE);
446 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
447
448 /* create temp object */
449 helper.ret = FALSE;
450 helper.loop = g_main_loop_new (NULL, FALSE);
451 helper.error = error;
452
453 /* run async method */
454 ch_device_queue_process_async (device_queue,
455 process_flags,
456 cancellable,
457 ch_device_queue_process_finish_cb,
458 &helper);
459 g_main_loop_run (helper.loop);
460
461 /* free temp object */
462 g_main_loop_unref (helper.loop);
463
464 return helper.ret;
465 }
466
467 /**********************************************************************/
468
469 static void
470 ch_device_queue_add_internal (ChDeviceQueue *device_queue,
471 GUsbDevice *device,
472 guint8 cmd,
473 const guint8 *buffer_in,
474 gsize buffer_in_len,
475 guint8 *buffer_out,
476 gsize buffer_out_len,
477 GDestroyNotify buffer_out_destroy_func,
478 ChDeviceQueueParseFunc parse_func,
479 gpointer user_data,
480 GDestroyNotify user_data_destroy_func)
481 {
482 ChDeviceQueueData *data;
483 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
484
485 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
486 g_return_if_fail (G_USB_IS_DEVICE (device));
487
488 data = g_new0 (ChDeviceQueueData, 1);
489 data->state = CH_DEVICE_QUEUE_DATA_STATE_PENDING;
490 data->parse_func = parse_func;
491 data->user_data = user_data;
492 data->user_data_destroy_func = user_data_destroy_func;
493 data->cmd = cmd;
494 data->device = g_object_ref (device);
495 if (buffer_in != NULL)
496 data->buffer_in = g_memdup (buffer_in, buffer_in_len);
497 data->buffer_in_len = buffer_in_len;
498 data->buffer_out = buffer_out;
499 data->buffer_out_len = buffer_out_len;
500 data->buffer_out_destroy_func = buffer_out_destroy_func;
501 g_ptr_array_add (priv->data_array, data);
502 }
503
504 /**
505 * ch_device_queue_add:
506 * @device_queue: A #ChDeviceQueue
507 * @device: A #GUsbDevice
508 * @cmd: The command, e.g. %CH_CMD_TAKE_READINGS
509 * @buffer_in: The input buffer, or %NULL
510 * @buffer_in_len: The size of @buffer_in
511 * @buffer_out: The output buffer, or %NULL
512 * @buffer_out_len: The size of @buffer_out
513 *
514 * Adds a raw command to the device queue.
515 *
516 * Since: 0.1.29
517 **/
518 void
519 ch_device_queue_add (ChDeviceQueue *device_queue,
520 GUsbDevice *device,
521 guint8 cmd,
522 const guint8 *buffer_in,
523 gsize buffer_in_len,
524 guint8 *buffer_out,
525 gsize buffer_out_len)
526 {
527 ch_device_queue_add_internal (device_queue,
528 device,
529 cmd,
530 buffer_in,
531 buffer_in_len,
532 buffer_out,
533 buffer_out_len,
534 NULL,
535 NULL,
536 NULL,
537 NULL);
538 }
539
540 /**********************************************************************/
541
542
543
544 /**
545 * ch_device_queue_get_color_select:
546 * @device_queue: A #ChDeviceQueue
547 * @device: A #GUsbDevice
548 * @color_select: The color select, e.g. %CH_COLOR_SELECT_RED
549 *
550 * Gets the selected sensor color.
551 *
552 * NOTE: This command is available on hardware version: 1 & 2
553 *
554 * Since: 0.1.29
555 **/
556 void
557 ch_device_queue_get_color_select (ChDeviceQueue *device_queue,
558 GUsbDevice *device,
559 ChColorSelect *color_select)
560 {
561 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
562 g_return_if_fail (G_USB_IS_DEVICE (device));
563 g_return_if_fail (color_select != NULL);
564
565 ch_device_queue_add (device_queue,
566 device,
567 CH_CMD_GET_COLOR_SELECT,
568 NULL,
569 0,
570 (guint8 *) color_select,
571 1);
572 }
573
574 /**
575 * ch_device_queue_set_color_select:
576 * @device_queue: A #ChDeviceQueue
577 * @device: A #GUsbDevice
578 * @color_select: The color select, e.g. %CH_COLOR_SELECT_RED
579 *
580 * Sets the sensor measurement color.
581 *
582 * NOTE: This command is available on hardware version: 1 & 2
583 *
584 * Since: 0.1.29
585 **/
586 void
587 ch_device_queue_set_color_select (ChDeviceQueue *device_queue,
588 GUsbDevice *device,
589 ChColorSelect color_select)
590 {
591 guint8 csel8 = color_select;
592
593 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
594 g_return_if_fail (G_USB_IS_DEVICE (device));
595
596 ch_device_queue_add (device_queue,
597 device,
598 CH_CMD_SET_COLOR_SELECT,
599 &csel8,
600 1,
601 NULL,
602 0);
603 }
604
605 /**
606 * ch_device_queue_get_multiplier:
607 * @device_queue: A #ChDeviceQueue
608 * @device: A #GUsbDevice
609 * @multiplier: The device multiplier, e.g. %CH_FREQ_SCALE_100
610 *
611 * Gets the sensor multiplier.
612 *
613 * NOTE: This command is available on hardware version: 1 & 2
614 *
615 * Since: 0.1.29
616 **/
617 void
618 ch_device_queue_get_multiplier (ChDeviceQueue *device_queue,
619 GUsbDevice *device,
620 ChFreqScale *multiplier)
621 {
622 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
623 g_return_if_fail (G_USB_IS_DEVICE (device));
624 g_return_if_fail (multiplier != NULL);
625
626 ch_device_queue_add (device_queue,
627 device,
628 CH_CMD_GET_MULTIPLIER,
629 NULL,
630 0,
631 (guint8 *) multiplier,
632 1);
633 }
634
635 /**
636 * ch_device_queue_set_multiplier:
637 * @device_queue: A #ChDeviceQueue
638 * @device: A #GUsbDevice
639 * @multiplier: The device multiplier, e.g. %CH_FREQ_SCALE_100
640 *
641 * Sets the sensor multiplier.
642 *
643 * NOTE: This command is available on hardware version: 1 & 2
644 *
645 * Since: 0.1.29
646 **/
647 void
648 ch_device_queue_set_multiplier (ChDeviceQueue *device_queue,
649 GUsbDevice *device,
650 ChFreqScale multiplier)
651 {
652 guint8 mult8 = multiplier;
653
654 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
655 g_return_if_fail (G_USB_IS_DEVICE (device));
656
657 ch_device_queue_add (device_queue,
658 device,
659 CH_CMD_SET_MULTIPLIER,
660 &mult8,
661 1,
662 NULL,
663 0);
664 }
665
666 static gboolean
667 ch_device_queue_buffer_uint16_from_le_cb (guint8 *output_buffer,
668 gsize output_buffer_size,
669 gpointer user_data,
670 GError **error)
671 {
672 guint16 tmp;
673
674 /* check buffer size */
675 if (output_buffer_size != sizeof (guint16)) {
676 g_set_error (error, 1, 0,
677 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
678 sizeof (guint16), output_buffer_size);
679 return FALSE;
680 }
681 tmp = cd_buffer_read_uint16_le (output_buffer);
682 memcpy (output_buffer, &tmp, sizeof (tmp));
683 return TRUE;
684 }
685
686 static gboolean
687 ch_device_queue_buffer_uint32_from_le_cb (guint8 *output_buffer,
688 gsize output_buffer_size,
689 gpointer user_data,
690 GError **error)
691 {
692 guint32 tmp;
693
694 /* check buffer size */
695 if (output_buffer_size != sizeof (guint32)) {
696 g_set_error (error, 1, 0,
697 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
698 sizeof (guint32), output_buffer_size);
699 return FALSE;
700 }
701 tmp = cd_buffer_read_uint32_le (output_buffer);
702 memcpy (output_buffer, &tmp, sizeof (tmp));
703 return TRUE;
704 }
705
706 /**
707 * ch_device_queue_get_integral_time:
708 * @device_queue: A #ChDeviceQueue
709 * @device: A #GUsbDevice
710 * @integral_time: The sensor integral time in device units
711 *
712 * Gets the reading integral time.
713
714 * NOTE: This command is available on hardware version: 1 & 2
715 *
716 * Since: 0.1.29
717 **/
718 void
719 ch_device_queue_get_integral_time (ChDeviceQueue *device_queue,
720 GUsbDevice *device,
721 guint16 *integral_time)
722 {
723 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
724 g_return_if_fail (G_USB_IS_DEVICE (device));
725 g_return_if_fail (integral_time != NULL);
726
727 ch_device_queue_add_internal (device_queue,
728 device,
729 CH_CMD_GET_INTEGRAL_TIME,
730 NULL,
731 0,
732 (guint8 *) integral_time,
733 2,
734 NULL,
735 ch_device_queue_buffer_uint16_from_le_cb,
736 NULL,
737 NULL);
738 }
739
740 /**
741 * ch_device_queue_set_integral_time:
742 * @device_queue: A #ChDeviceQueue
743 * @device: A #GUsbDevice
744 * @integral_time: The sensor integral time in device units
745 *
746 * Sets the reading integral time.
747 *
748 * NOTE: This command is available on hardware version: 1 & 2
749 *
750 * Since: 0.1.29
751 **/
752 void
753 ch_device_queue_set_integral_time (ChDeviceQueue *device_queue,
754 GUsbDevice *device,
755 guint16 integral_time)
756 {
757 guint16 integral_le;
758
759 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
760 g_return_if_fail (G_USB_IS_DEVICE (device));
761 g_return_if_fail (integral_time > 0);
762
763 integral_le = GUINT16_TO_LE (integral_time);
764
765 ch_device_queue_add (device_queue,
766 device,
767 CH_CMD_SET_INTEGRAL_TIME,
768 (const guint8 *) &integral_le,
769 sizeof(guint16),
770 NULL,
771 0);
772 }
773
774 /**
775 * ch_device_queue_get_calibration_map:
776 * @device_queue: A #ChDeviceQueue
777 * @device: A #GUsbDevice
778 * @calibration_map: An array of slot positions
779 *
780 * Gets the calibration map.
781 *
782 * NOTE: This command is available on hardware version: 1 & 2
783 *
784 * Since: 0.1.29
785 **/
786 void
787 ch_device_queue_get_calibration_map (ChDeviceQueue *device_queue,
788 GUsbDevice *device,
789 guint16 *calibration_map)
790 {
791 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
792 g_return_if_fail (G_USB_IS_DEVICE (device));
793 g_return_if_fail (calibration_map != NULL);
794
795 ch_device_queue_add (device_queue,
796 device,
797 CH_CMD_GET_CALIBRATION_MAP,
798 NULL,
799 0,
800 (guint8 *) calibration_map,
801 CH_CALIBRATION_INDEX_MAX * sizeof(guint16));
802 }
803
804 /**
805 * ch_device_queue_set_calibration_map:
806 * @device_queue: A #ChDeviceQueue
807 * @device: A #GUsbDevice
808 * @calibration_map: An array of slot positions
809 *
810 * Sets the calibration map.
811 *
812 * NOTE: This command is available on hardware version: 1 & 2
813 *
814 * Since: 0.1.29
815 **/
816 void
817 ch_device_queue_set_calibration_map (ChDeviceQueue *device_queue,
818 GUsbDevice *device,
819 const guint16 *calibration_map)
820 {
821 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
822 g_return_if_fail (G_USB_IS_DEVICE (device));
823 g_return_if_fail (calibration_map != NULL);
824
825 ch_device_queue_add (device_queue,
826 device,
827 CH_CMD_SET_CALIBRATION_MAP,
828 (const guint8 *) calibration_map,
829 CH_CALIBRATION_INDEX_MAX * sizeof(guint16),
830 NULL,
831 0);
832 }
833
834 /* tiny helper */
835 typedef struct {
836 guint16 *major;
837 guint16 *minor;
838 guint16 *micro;
839 } ChDeviceQueueGetFirmwareVerHelper;
840
841 static gboolean
842 ch_device_queue_buffer_to_firmware_ver_cb (guint8 *output_buffer,
843 gsize output_buffer_size,
844 gpointer user_data,
845 GError **error)
846 {
847 ChDeviceQueueGetFirmwareVerHelper *helper = (void *) user_data;
848
849 /* check buffer size */
850 if (output_buffer_size != sizeof (guint16) * 3) {
851 g_set_error (error, 1, 0,
852 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
853 sizeof (guint16) * 3, output_buffer_size);
854 return FALSE;
855 }
856
857 *helper->major = cd_buffer_read_uint16_le (output_buffer + 0);
858 *helper->minor = cd_buffer_read_uint16_le (output_buffer + 2);
859 *helper->micro = cd_buffer_read_uint16_le (output_buffer + 4);
860 return TRUE;
861 }
862
863 /**
864 * ch_device_queue_get_firmware_ver:
865 * @device_queue: A #ChDeviceQueue
866 * @device: A #GUsbDevice
867 * @major: The firmware major version
868 * @minor: The firmware minor version
869 * @micro: The firmware micro version
870 *
871 * Gets the firmware version.
872 *
873 * NOTE: This command is available on hardware version: 1 & 2
874 *
875 * Since: 0.1.29
876 **/
877 void
878 ch_device_queue_get_firmware_ver (ChDeviceQueue *device_queue,
879 GUsbDevice *device,
880 guint16 *major,
881 guint16 *minor,
882 guint16 *micro)
883 {
884 guint8 *buffer;
885 ChDeviceQueueGetFirmwareVerHelper *helper;
886
887 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
888 g_return_if_fail (G_USB_IS_DEVICE (device));
889 g_return_if_fail (major != NULL);
890 g_return_if_fail (minor != NULL);
891 g_return_if_fail (micro != NULL);
892
893 /* create a helper structure */
894 helper = g_new0 (ChDeviceQueueGetFirmwareVerHelper, 1);
895 helper->major = major;
896 helper->minor = minor;
897 helper->micro = micro;
898
899 buffer = g_new0 (guint8, sizeof (guint16) * 3);
900 ch_device_queue_add_internal (device_queue,
901 device,
902 CH_CMD_GET_FIRMWARE_VERSION,
903 NULL,
904 0,
905 buffer,
906 sizeof (guint16) * 3,
907 g_free,
908 ch_device_queue_buffer_to_firmware_ver_cb,
909 helper,
910 g_free);
911 }
912
913 /* tiny helper */
914 typedef struct {
915 CdMat3x3 *calibration;
916 guint8 *types;
917 gchar *description;
918 } ChDeviceQueueGetCalibrationHelper;
919
920 static gboolean
921 ch_device_queue_buffer_to_get_calibration_cb (guint8 *output_buffer,
922 gsize output_buffer_size,
923 gpointer user_data,
924 GError **error)
925 {
926 ChDeviceQueueGetCalibrationHelper *helper = (void *) user_data;
927 ChPackedFloat pf_tmp;
928 gdouble *calibration_tmp;
929 guint i;
930
931 /* check buffer size */
932 if (output_buffer_size != 60) {
933 g_set_error (error, 1, 0,
934 "Wrong output buffer size, expected %i, got %" G_GSIZE_FORMAT,
935 60, output_buffer_size);
936 return FALSE;
937 }
938
939 /* convert back into floating point */
940 if (helper->calibration != NULL) {
941 calibration_tmp = cd_mat33_get_data (helper->calibration);
942 for (i = 0; i < 9; i++) {
943 memcpy (&pf_tmp, &output_buffer[i*4], sizeof (pf_tmp));
944 ch_packed_float_to_double (&pf_tmp, &calibration_tmp[i]);
945 }
946 }
947
948 /* get the supported types */
949 if (helper->types != NULL)
950 *helper->types = output_buffer[9*4];
951
952 /* get the description */
953 if (helper->description != NULL) {
954 strncpy (helper->description,
955 (const char *) output_buffer + 9*4 + 1,
956 CH_CALIBRATION_DESCRIPTION_LEN);
957 }
958 return TRUE;
959 }
960
961 /**
962 * ch_device_queue_get_calibration:
963 * @device_queue: A #ChDeviceQueue
964 * @device: A #GUsbDevice
965 * @calibration_index: The slot position
966 * @calibration: the 3x3 calibration matrix
967 * @types: The types the matrix supports
968 * @description: The description of the calibration
969 *
970 * Gets the calibration data.
971 *
972 * NOTE: This command is available on hardware version: 1 & 2
973 *
974 * Since: 0.1.29
975 **/
976 void
977 ch_device_queue_get_calibration (ChDeviceQueue *device_queue,
978 GUsbDevice *device,
979 guint16 calibration_index,
980 CdMat3x3 *calibration,
981 guint8 *types,
982 gchar *description)
983 {
984 guint8 *buffer;
985 ChDeviceQueueGetCalibrationHelper *helper;
986
987 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
988 g_return_if_fail (G_USB_IS_DEVICE (device));
989 g_return_if_fail (calibration_index < CH_CALIBRATION_MAX);
990
991 /* create a helper structure */
992 helper = g_new0 (ChDeviceQueueGetCalibrationHelper, 1);
993 helper->calibration = calibration;
994 helper->types = types;
995 helper->description = description;
996
997 buffer = g_new0 (guint8, 9*4 + 1 + CH_CALIBRATION_DESCRIPTION_LEN);
998 ch_device_queue_add_internal (device_queue,
999 device,
1000 CH_CMD_GET_CALIBRATION,
1001 (guint8 *) &calibration_index,
1002 sizeof(guint16),
1003 (guint8 *) buffer,
1004 9*4 + 1 + CH_CALIBRATION_DESCRIPTION_LEN,
1005 g_free,
1006 ch_device_queue_buffer_to_get_calibration_cb,
1007 helper,
1008 g_free);
1009 }
1010
1011 /**
1012 * ch_device_queue_set_calibration:
1013 * @device_queue: A #ChDeviceQueue
1014 * @device: A #GUsbDevice
1015 * @calibration_index: The slot position
1016 * @calibration: the 3x3 calibration matrix
1017 * @types: The types the matrix supports
1018 * @description: The description of the calibration
1019 *
1020 * Sets the calibration data.
1021 *
1022 * NOTE: This command is available on hardware version: 1 & 2
1023 *
1024 * Since: 0.1.29
1025 **/
1026 void
1027 ch_device_queue_set_calibration (ChDeviceQueue *device_queue,
1028 GUsbDevice *device,
1029 guint16 calibration_index,
1030 const CdMat3x3 *calibration,
1031 guint8 types,
1032 const gchar *description)
1033 {
1034 ChPackedFloat pf_tmp;
1035 gdouble *calibration_tmp;
1036 guint8 buffer[9*4 + 2 + 1 + CH_CALIBRATION_DESCRIPTION_LEN];
1037 guint i;
1038
1039 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1040 g_return_if_fail (G_USB_IS_DEVICE (device));
1041 g_return_if_fail (calibration_index < CH_CALIBRATION_MAX);
1042 g_return_if_fail (calibration != NULL);
1043 g_return_if_fail (description != NULL);
1044
1045 /* write index */
1046 memcpy (buffer, &calibration_index, sizeof(guint16));
1047
1048 /* convert from float to signed value */
1049 for (i = 0; i < 9; i++) {
1050 calibration_tmp = cd_mat33_get_data (calibration);
1051 ch_double_to_packed_float (calibration_tmp[i], &pf_tmp);
1052 memcpy (&buffer[i*4 + 2], &pf_tmp, sizeof (pf_tmp));
1053 }
1054
1055 /* write types */
1056 buffer[9*4 + 2] = types;
1057
1058 /* write description */
1059 strncpy ((gchar *) buffer + 9*4 + 2 + 1,
1060 description,
1061 CH_CALIBRATION_DESCRIPTION_LEN);
1062
1063 ch_device_queue_add (device_queue,
1064 device,
1065 CH_CMD_SET_CALIBRATION,
1066 (guint8 *) buffer,
1067 sizeof(buffer),
1068 NULL,
1069 0);
1070 }
1071
1072 /**
1073 * ch_device_queue_set_calibration_ccmx:
1074 *
1075 * Since: 0.1.29
1076 **/
1077 gboolean
1078 ch_device_queue_set_calibration_ccmx (ChDeviceQueue *device_queue,
1079 GUsbDevice *device,
1080 guint16 calibration_index,
1081 CdIt8 *ccmx,
1082 GError **error)
1083 {
1084 const CdMat3x3 *calibration;
1085 const gchar *description;
1086 gdouble *calibration_tmp;
1087 guint8 types = 0;
1088 guint i;
1089
1090 g_return_val_if_fail (CD_IS_IT8 (ccmx), FALSE);
1091 g_return_val_if_fail (CH_IS_DEVICE_QUEUE (device_queue), FALSE);
1092 g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE);
1093
1094 /* ensure correct kind */
1095 if (cd_it8_get_kind (ccmx) != CD_IT8_KIND_CCMX) {
1096 g_set_error (error, 1, 0, "is not a CCMX file");
1097 return FALSE;
1098 }
1099
1100 /* get the supported display types */
1101 if (cd_it8_has_option (ccmx, "TYPE_FACTORY")) {
1102 types = CH_CALIBRATION_TYPE_ALL;
1103 } else {
1104 if (cd_it8_has_option (ccmx, "TYPE_LCD"))
1105 types += CH_CALIBRATION_TYPE_LCD;
1106 if (cd_it8_has_option (ccmx, "TYPE_LED"))
1107 types += CH_CALIBRATION_TYPE_LED;
1108 if (cd_it8_has_option (ccmx, "TYPE_CRT"))
1109 types += CH_CALIBRATION_TYPE_CRT;
1110 if (cd_it8_has_option (ccmx, "TYPE_PROJECTOR"))
1111 types += CH_CALIBRATION_TYPE_PROJECTOR;
1112 }
1113
1114 /* no types set in CCMX file */
1115 if (types == 0) {
1116 g_set_error_literal (error, 1, 0, "No TYPE_x in ccmx file");
1117 return FALSE;
1118 }
1119
1120 /* get the description from the ccmx file */
1121 description = cd_it8_get_title (ccmx);
1122 if (description == NULL) {
1123 g_set_error_literal (error, 1, 0,
1124 "CCMX file does not have DISPLAY");
1125 return FALSE;
1126 }
1127
1128 /* get the values and check for sanity */
1129 calibration = cd_it8_get_matrix (ccmx);
1130 calibration_tmp = cd_mat33_get_data (calibration);
1131 for (i = 0; i < 9; i++) {
1132 if (calibration_tmp[i] < -100.0f ||
1133 calibration_tmp[i] > 100.0f) {
1134 g_set_error (error, 1, 0,
1135 "Matrix value %u out of range %f",
1136 i, calibration_tmp[i]);
1137 return FALSE;
1138 }
1139 }
1140
1141 /* set to HW */
1142 ch_device_queue_set_calibration (device_queue,
1143 device,
1144 calibration_index,
1145 calibration,
1146 types,
1147 description);
1148 return TRUE;
1149 }
1150
1151 /**
1152 * ch_device_queue_write_firmware:
1153 * @device_queue: A #ChDeviceQueue
1154 * @device: A #GUsbDevice
1155 * @data: (array length=len): Firmware binary data
1156 * @len: Size of @data
1157 *
1158 * Writes new firmware to the device.
1159 *
1160 * NOTE: This command is available on hardware version: 1 & 2
1161 *
1162 * Since: 0.1.29
1163 **/
1164 void
1165 ch_device_queue_write_firmware (ChDeviceQueue *device_queue,
1166 GUsbDevice *device,
1167 const guint8 *data,
1168 gsize len)
1169 {
1170 gsize chunk_len;
1171 guint idx;
1172 guint16 runcode_addr;
1173
1174 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1175 g_return_if_fail (G_USB_IS_DEVICE (device));
1176 g_return_if_fail (data != NULL);
1177
1178 /* erase flash */
1179 runcode_addr = ch_device_get_runcode_address (device);
1180 g_debug ("Erasing at %04x size %" G_GSIZE_FORMAT,
1181 runcode_addr, len);
1182 ch_device_queue_erase_flash (device_queue,
1183 device,
1184 runcode_addr,
1185 len);
1186
1187 /* just write in 32 byte chunks, as we're sure that the firmware
1188 * image has been prepared to end on a 64 byte chunk with
1189 * colorhug-inhx32-to-bin >= 0.1.5 */
1190 idx = 0;
1191 chunk_len = CH_FLASH_TRANSFER_BLOCK_SIZE;
1192 do {
1193 if (idx + chunk_len > len)
1194 chunk_len = len - idx;
1195 g_debug ("Writing at %04x size %" G_GSIZE_FORMAT,
1196 runcode_addr + idx,
1197 chunk_len);
1198 ch_device_queue_write_flash (device_queue,
1199 device,
1200 runcode_addr + idx,
1201 (guint8 *) data + idx,
1202 chunk_len);
1203 idx += chunk_len;
1204 } while (idx < len);
1205 }
1206
1207 /**
1208 * ch_device_queue_verify_firmware:
1209 * @device_queue: A #ChDeviceQueue
1210 * @device: A #GUsbDevice
1211 * @data: (array length=len): Firmware binary data
1212 * @len: Size of @data
1213 *
1214 * Verifies firmware on the device.
1215 *
1216 * NOTE: This command is available on hardware version: 1 & 2
1217 *
1218 * Since: 0.1.29
1219 **/
1220 void
1221 ch_device_queue_verify_firmware (ChDeviceQueue *device_queue,
1222 GUsbDevice *device,
1223 const guint8 *data,
1224 gsize len)
1225 {
1226 gsize chunk_len;
1227 guint idx;
1228 guint16 runcode_addr;
1229
1230 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1231 g_return_if_fail (G_USB_IS_DEVICE (device));
1232 g_return_if_fail (data != NULL);
1233
1234 /* read in 60 byte chunks */
1235 idx = 0;
1236 chunk_len = 60;
1237 runcode_addr = ch_device_get_runcode_address (device);
1238 do {
1239 if (idx + chunk_len > len)
1240 chunk_len = len - idx;
1241 g_debug ("Verifying at %04x size %" G_GSIZE_FORMAT,
1242 runcode_addr + idx,
1243 chunk_len);
1244 ch_device_queue_verify_flash (device_queue,
1245 device,
1246 runcode_addr + idx,
1247 data + idx,
1248 chunk_len);
1249 idx += chunk_len;
1250 } while (idx < len);
1251 }
1252
1253 /**
1254 * ch_device_queue_read_firmware:
1255 * @device_queue: A #ChDeviceQueue
1256 * @device: A #GUsbDevice
1257 * @data: (array length=len): Firmware binary data
1258 * @len: Size of @data
1259 *
1260 * Reads firmware on the device.
1261 *
1262 * NOTE: This command is available on hardware version: 1 & 2
1263 *
1264 * Since: 1.2.11
1265 **/
1266 void
1267 ch_device_queue_read_firmware (ChDeviceQueue *device_queue,
1268 GUsbDevice *device,
1269 guint8 **data,
1270 gsize *len)
1271 {
1272 gsize chunk_len = 60;
1273 guint idx = 0;
1274 guint16 runcode_addr;
1275 guint8 *data_tmp;
1276 gsize len_tmp;
1277
1278 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1279 g_return_if_fail (G_USB_IS_DEVICE (device));
1280 g_return_if_fail (data != NULL);
1281
1282 /* assume firmware is padded */
1283 len_tmp = ch_device_get_runcode_address (device);
1284 data_tmp = g_malloc0 (len_tmp);
1285
1286 /* read in 60 byte chunks */
1287 runcode_addr = ch_device_get_runcode_address (device);
1288 do {
1289 if (idx + chunk_len > len_tmp)
1290 chunk_len = len_tmp - idx;
1291 g_debug ("Reading at %04x size %" G_GSIZE_FORMAT,
1292 runcode_addr + idx,
1293 chunk_len);
1294 ch_device_queue_read_flash (device_queue,
1295 device,
1296 runcode_addr + idx,
1297 data_tmp + idx,
1298 chunk_len);
1299 idx += chunk_len;
1300 } while (idx < len_tmp);
1301
1302 /* return */
1303 *data = data_tmp;
1304 if (len != NULL)
1305 *len = len_tmp;
1306 }
1307
1308 /**
1309 * ch_device_queue_clear_calibration:
1310 * @device_queue: A #ChDeviceQueue
1311 * @device: A #GUsbDevice
1312 * @calibration_index: Slot
1313 *
1314 * Clears a calibration slot.
1315 *
1316 * NOTE: This command is available on hardware version: 1 & 2
1317 *
1318 * Since: 0.1.29
1319 **/
1320 void
1321 ch_device_queue_clear_calibration (ChDeviceQueue *device_queue,
1322 GUsbDevice *device,
1323 guint16 calibration_index)
1324 {
1325 guint8 buffer[9*4 + 2 + 1 + CH_CALIBRATION_DESCRIPTION_LEN];
1326
1327 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1328 g_return_if_fail (G_USB_IS_DEVICE (device));
1329 g_return_if_fail (calibration_index < CH_CALIBRATION_MAX);
1330
1331 /* write index */
1332 memcpy (buffer, &calibration_index, sizeof(guint16));
1333
1334 /* clear data */
1335 memset (buffer + 2, 0xff, sizeof (buffer) - 2);
1336
1337 ch_device_queue_add (device_queue,
1338 device,
1339 CH_CMD_SET_CALIBRATION,
1340 (guint8 *) buffer,
1341 sizeof(buffer),
1342 NULL,
1343 0);
1344 }
1345
1346 static gboolean
1347 ch_device_queue_buffer_to_double_cb (guint8 *output_buffer,
1348 gsize output_buffer_size,
1349 gpointer user_data,
1350 GError **error)
1351 {
1352 ChPackedFloat pf_tmp;
1353 gdouble tmp;
1354
1355 /* check buffer size */
1356 if (output_buffer_size != sizeof (ChPackedFloat)) {
1357 g_set_error (error, 1, 0,
1358 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
1359 sizeof (ChPackedFloat), output_buffer_size);
1360 return FALSE;
1361 }
1362
1363 /* convert back into floating point */
1364 memcpy (&pf_tmp, output_buffer, sizeof (ChPackedFloat));
1365 ch_packed_float_to_double (&pf_tmp, &tmp);
1366 memcpy (user_data, &tmp, sizeof (tmp));
1367 return TRUE;
1368 }
1369
1370 /**
1371 * ch_device_queue_get_pre_scale:
1372 * @device_queue: A #ChDeviceQueue
1373 * @device: A #GUsbDevice
1374 * @pre_scale: Pre-scale value
1375 *
1376 * Gets the pre scale value.
1377 *
1378 * NOTE: This command is available on hardware version: 1 & 2
1379 *
1380 * Since: 0.1.29
1381 **/
1382 void
1383 ch_device_queue_get_pre_scale (ChDeviceQueue *device_queue,
1384 GUsbDevice *device,
1385 gdouble *pre_scale)
1386 {
1387 guint8 *buffer;
1388
1389 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1390 g_return_if_fail (G_USB_IS_DEVICE (device));
1391 g_return_if_fail (pre_scale != NULL);
1392
1393 *pre_scale = 0.0f;
1394 buffer = g_new0 (guint8, sizeof (ChPackedFloat));
1395 ch_device_queue_add_internal (device_queue,
1396 device,
1397 CH_CMD_GET_PRE_SCALE,
1398 NULL,
1399 0,
1400 buffer,
1401 sizeof(ChPackedFloat),
1402 g_free,
1403 ch_device_queue_buffer_to_double_cb,
1404 pre_scale,
1405 NULL);
1406 }
1407
1408 /**
1409 * ch_device_queue_set_pre_scale:
1410 * @device_queue: A #ChDeviceQueue
1411 * @device: A #GUsbDevice
1412 * @pre_scale: Pre-scale value
1413 *
1414 * Sets the pre-scale value.
1415 *
1416 * NOTE: This command is available on hardware version: 1 & 2
1417 *
1418 * Since: 0.1.29
1419 **/
1420 void
1421 ch_device_queue_set_pre_scale (ChDeviceQueue *device_queue,
1422 GUsbDevice *device,
1423 gdouble pre_scale)
1424 {
1425 ChPackedFloat buffer;
1426
1427 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1428 g_return_if_fail (G_USB_IS_DEVICE (device));
1429
1430 /* convert from float to signed value */
1431 ch_double_to_packed_float (pre_scale, &buffer);
1432
1433 ch_device_queue_add (device_queue,
1434 device,
1435 CH_CMD_SET_PRE_SCALE,
1436 (guint8 *) &buffer,
1437 sizeof(buffer),
1438 NULL,
1439 0);
1440 }
1441
1442 /**
1443 * ch_device_queue_get_temperature:
1444 * @device_queue: A #ChDeviceQueue
1445 * @device: A #GUsbDevice
1446 * @temperature: Temperature in Celsius
1447 *
1448 * Gets the device temperature.
1449 *
1450 * NOTE: This command is available on hardware version: 2
1451 *
1452 * Since: 0.1.29
1453 **/
1454 void
1455 ch_device_queue_get_temperature (ChDeviceQueue *device_queue,
1456 GUsbDevice *device,
1457 gdouble *temperature)
1458 {
1459 guint8 *buffer;
1460
1461 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1462 g_return_if_fail (G_USB_IS_DEVICE (device));
1463 g_return_if_fail (temperature != NULL);
1464
1465 *temperature = 0.0f;
1466 buffer = g_new0 (guint8, sizeof (ChPackedFloat));
1467 ch_device_queue_add_internal (device_queue,
1468 device,
1469 CH_CMD_GET_TEMPERATURE,
1470 NULL,
1471 0,
1472 buffer,
1473 sizeof(ChPackedFloat),
1474 g_free,
1475 ch_device_queue_buffer_to_double_cb,
1476 temperature,
1477 NULL);
1478 }
1479
1480 /**
1481 * ch_device_queue_get_dac_value:
1482 * @device_queue: A #ChDeviceQueue
1483 * @device: A #GUsbDevice
1484 * @dac_value: value between 0.0 and 0.99
1485 *
1486 * Gets the DAC value.
1487 *
1488 * NOTE: This command is available on hardware version: 2
1489 *
1490 * Since: 1.1.6
1491 **/
1492 void
1493 ch_device_queue_get_dac_value (ChDeviceQueue *device_queue,
1494 GUsbDevice *device,
1495 gdouble *dac_value)
1496 {
1497 guint8 *buffer;
1498
1499 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1500 g_return_if_fail (G_USB_IS_DEVICE (device));
1501 g_return_if_fail (dac_value != NULL);
1502
1503 *dac_value = 0.0f;
1504 buffer = g_new0 (guint8, sizeof (ChPackedFloat));
1505 ch_device_queue_add_internal (device_queue,
1506 device,
1507 CH_CMD_GET_DAC_VALUE,
1508 NULL,
1509 0,
1510 buffer,
1511 sizeof(ChPackedFloat),
1512 g_free,
1513 ch_device_queue_buffer_to_double_cb,
1514 dac_value,
1515 NULL);
1516 }
1517
1518 /**
1519 * ch_device_queue_set_dac_value:
1520 * @device_queue: A #ChDeviceQueue
1521 * @device: A #GUsbDevice
1522 * @dac_value: The DAC value
1523 *
1524 * Sets the post scale value.
1525 *
1526 * NOTE: This command is available on hardware version: 1 & 2
1527 *
1528 * Since: 1.1.6
1529 **/
1530 void
1531 ch_device_queue_set_dac_value (ChDeviceQueue *device_queue,
1532 GUsbDevice *device,
1533 gdouble dac_value)
1534 {
1535 ChPackedFloat buffer;
1536
1537 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1538 g_return_if_fail (G_USB_IS_DEVICE (device));
1539
1540 /* convert from float to signed value */
1541 ch_double_to_packed_float (dac_value, &buffer);
1542
1543 ch_device_queue_add (device_queue,
1544 device,
1545 CH_CMD_SET_DAC_VALUE,
1546 (guint8 *) &buffer,
1547 sizeof(buffer),
1548 NULL,
1549 0);
1550 }
1551
1552 /**
1553 * ch_device_queue_get_adc_vref_pos:
1554 * @device_queue: A #ChDeviceQueue
1555 * @device: A #GUsbDevice
1556 * @vref: The voltage reference, ranging from 0.0 to 3.3V.
1557 *
1558 * Gets the ADC Vref+ calibration value.
1559 *
1560 * NOTE: This command is available on hardware version: 2
1561 *
1562 * Since: 0.1.31
1563 **/
1564 void
1565 ch_device_queue_get_adc_vref_pos (ChDeviceQueue *device_queue,
1566 GUsbDevice *device,
1567 gdouble *vref)
1568 {
1569 guint8 *buffer;
1570
1571 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1572 g_return_if_fail (G_USB_IS_DEVICE (device));
1573 g_return_if_fail (vref != NULL);
1574
1575 *vref = 0.0f;
1576 buffer = g_new0 (guint8, sizeof (ChPackedFloat));
1577 ch_device_queue_add_internal (device_queue,
1578 device,
1579 CH_CMD_GET_ADC_CALIBRATION_POS,
1580 NULL,
1581 0,
1582 buffer,
1583 sizeof(ChPackedFloat),
1584 g_free,
1585 ch_device_queue_buffer_to_double_cb,
1586 vref,
1587 NULL);
1588 }
1589
1590 /**
1591 * ch_device_queue_get_adc_vref_neg:
1592 * @device_queue: A #ChDeviceQueue
1593 * @device: A #GUsbDevice
1594 * @vref: The voltage reference, ranging from 0.0 to 3.3V.
1595 *
1596 * Gets the ADC Vref- calibration value.
1597 *
1598 * NOTE: This command is available on hardware version: 2
1599 *
1600 * Since: 0.1.31
1601 **/
1602 void
1603 ch_device_queue_get_adc_vref_neg (ChDeviceQueue *device_queue,
1604 GUsbDevice *device,
1605 gdouble *vref)
1606 {
1607 guint8 *buffer;
1608
1609 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1610 g_return_if_fail (G_USB_IS_DEVICE (device));
1611 g_return_if_fail (vref != NULL);
1612
1613 *vref = 0.0f;
1614 buffer = g_new0 (guint8, sizeof (ChPackedFloat));
1615 ch_device_queue_add_internal (device_queue,
1616 device,
1617 CH_CMD_GET_ADC_CALIBRATION_NEG,
1618 NULL,
1619 0,
1620 buffer,
1621 sizeof(ChPackedFloat),
1622 g_free,
1623 ch_device_queue_buffer_to_double_cb,
1624 vref,
1625 NULL);
1626 }
1627
1628 /**
1629 * ch_device_queue_take_reading_spectral:
1630 * @device_queue: A #ChDeviceQueue
1631 * @device: A #GUsbDevice
1632 * @sram_addr: The SRAM location where the data is held
1633 *
1634 * Takes a raw spectral reading.
1635 *
1636 * NOTE: This command is available on hardware version: 2
1637 *
1638 * Since: 0.1.31
1639 **/
1640 void
1641 ch_device_queue_take_reading_spectral (ChDeviceQueue *device_queue,
1642 GUsbDevice *device,
1643 guint16 *sram_addr)
1644 {
1645 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1646 g_return_if_fail (G_USB_IS_DEVICE (device));
1647 g_return_if_fail (sram_addr != NULL);
1648
1649 *sram_addr = 0x0000;
1650 ch_device_queue_add (device_queue,
1651 device,
1652 CH_CMD_TAKE_READING_SPECTRAL,
1653 NULL,
1654 0,
1655 (guint8 *) sram_addr,
1656 sizeof (guint16));
1657 }
1658
1659 /**
1660 * ch_device_queue_get_ccd_calibration:
1661 * @device_queue: A #ChDeviceQueue
1662 * @device: A #GUsbDevice
1663 * @indexes: An array of red, green, blue indexes
1664 *
1665 * Gets the CCD spectral calibration indexes. These are "pointers" to specific
1666 * spectral peaks returned by the CCD.
1667 *
1668 * NOTE: This command is available on hardware version: 2
1669 *
1670 * Since: 0.1.31
1671 **/
1672 void
1673 ch_device_queue_get_ccd_calibration (ChDeviceQueue *device_queue,
1674 GUsbDevice *device,
1675 guint16 *indexes)
1676 {
1677 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1678 g_return_if_fail (G_USB_IS_DEVICE (device));
1679 g_return_if_fail (indexes != NULL);
1680
1681 /* three uint16_t values */
1682 ch_device_queue_add (device_queue,
1683 device,
1684 CH_CMD_GET_CCD_CALIBRATION,
1685 NULL,
1686 0,
1687 (guint8 *) indexes,
1688 3 * sizeof (guint16));
1689 }
1690
1691 /**
1692 * ch_device_queue_set_ccd_calibration:
1693 * @device_queue: A #ChDeviceQueue
1694 * @device: A #GUsbDevice
1695 * @indexes: An array of red, green, blue indexes
1696 *
1697 * Sets the CCD spectral calibration indexes. These are "pointers" to specific
1698 * spectral peaks returned by the CCD.
1699 *
1700 * NOTE: This command is available on hardware version: 2
1701 *
1702 * Since: 0.1.31
1703 **/
1704 void
1705 ch_device_queue_set_ccd_calibration (ChDeviceQueue *device_queue,
1706 GUsbDevice *device,
1707 const guint16 *indexes)
1708 {
1709 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1710 g_return_if_fail (G_USB_IS_DEVICE (device));
1711 g_return_if_fail (indexes != NULL);
1712 g_return_if_fail (indexes[0] < CH_CCD_SPECTRAL_RESOLUTION);
1713 g_return_if_fail (indexes[1] < CH_CCD_SPECTRAL_RESOLUTION);
1714 g_return_if_fail (indexes[2] < CH_CCD_SPECTRAL_RESOLUTION);
1715
1716 /* three uint16_t values */
1717 ch_device_queue_add (device_queue,
1718 device,
1719 CH_CMD_SET_CCD_CALIBRATION,
1720 (const guint8 *) indexes,
1721 3 * sizeof (guint16),
1722 NULL,
1723 0);
1724 }
1725
1726 /**
1727 * ch_device_queue_get_post_scale:
1728 * @device_queue: A #ChDeviceQueue
1729 * @device: A #GUsbDevice
1730 * @post_scale: The post-scale value
1731 *
1732 * Gets the post scale value.
1733 *
1734 * NOTE: This command is available on hardware version: 1 & 2
1735 *
1736 * Since: 0.1.29
1737 **/
1738 void
1739 ch_device_queue_get_post_scale (ChDeviceQueue *device_queue,
1740 GUsbDevice *device,
1741 gdouble *post_scale)
1742 {
1743 guint8 *buffer;
1744
1745 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1746 g_return_if_fail (G_USB_IS_DEVICE (device));
1747 g_return_if_fail (post_scale != NULL);
1748
1749 *post_scale = 0.0f;
1750 buffer = g_new0 (guint8, sizeof (ChPackedFloat));
1751 ch_device_queue_add_internal (device_queue,
1752 device,
1753 CH_CMD_GET_POST_SCALE,
1754 NULL,
1755 0,
1756 buffer,
1757 sizeof(ChPackedFloat),
1758 g_free,
1759 ch_device_queue_buffer_to_double_cb,
1760 post_scale,
1761 NULL);
1762 }
1763
1764 /**
1765 * ch_device_queue_set_post_scale:
1766 * @device_queue: A #ChDeviceQueue
1767 * @device: A #GUsbDevice
1768 * @post_scale: The post-scale value
1769 *
1770 * Sets the post scale value.
1771 *
1772 * NOTE: This command is available on hardware version: 1 & 2
1773 *
1774 * Since: 0.1.29
1775 **/
1776 void
1777 ch_device_queue_set_post_scale (ChDeviceQueue *device_queue,
1778 GUsbDevice *device,
1779 gdouble post_scale)
1780 {
1781 ChPackedFloat buffer;
1782
1783 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1784 g_return_if_fail (G_USB_IS_DEVICE (device));
1785
1786 /* convert from float to signed value */
1787 ch_double_to_packed_float (post_scale, &buffer);
1788
1789 ch_device_queue_add (device_queue,
1790 device,
1791 CH_CMD_SET_POST_SCALE,
1792 (guint8 *) &buffer,
1793 sizeof(buffer),
1794 NULL,
1795 0);
1796 }
1797
1798 /**
1799 * ch_device_queue_get_serial_number:
1800 * @device_queue: A #ChDeviceQueue
1801 * @device: A #GUsbDevice
1802 * @serial_number: The device serial number
1803 *
1804 * Gets the device serial number.
1805 *
1806 * NOTE: This command is available on hardware version: 1 & 2
1807 *
1808 * Since: 0.1.29
1809 **/
1810 void
1811 ch_device_queue_get_serial_number (ChDeviceQueue *device_queue,
1812 GUsbDevice *device,
1813 guint32 *serial_number)
1814 {
1815 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1816 g_return_if_fail (G_USB_IS_DEVICE (device));
1817 g_return_if_fail (serial_number != NULL);
1818
1819 *serial_number = 0;
1820 ch_device_queue_add_internal (device_queue,
1821 device,
1822 CH_CMD_GET_SERIAL_NUMBER,
1823 NULL,
1824 0,
1825 (guint8 *) serial_number,
1826 sizeof(guint32),
1827 NULL,
1828 ch_device_queue_buffer_uint32_from_le_cb,
1829 NULL,
1830 NULL);
1831 }
1832
1833 /**
1834 * ch_device_queue_set_serial_number:
1835 * @device_queue: A #ChDeviceQueue
1836 * @device: A #GUsbDevice
1837 * @serial_number: The device serial number
1838 *
1839 * Sets the device serial number.
1840 *
1841 * NOTE: This command is available on hardware version: 1 & 2
1842 *
1843 * Since: 0.1.29
1844 **/
1845 void
1846 ch_device_queue_set_serial_number (ChDeviceQueue *device_queue,
1847 GUsbDevice *device,
1848 guint32 serial_number)
1849 {
1850 guint32 serial_le;
1851
1852 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1853 g_return_if_fail (G_USB_IS_DEVICE (device));
1854 g_return_if_fail (serial_number > 0);
1855
1856 serial_le = GUINT32_TO_LE (serial_number);
1857
1858 ch_device_queue_add (device_queue,
1859 device,
1860 CH_CMD_SET_SERIAL_NUMBER,
1861 (const guint8 *) &serial_le,
1862 sizeof(serial_le),
1863 NULL,
1864 0);
1865 }
1866
1867 /**
1868 * ch_device_queue_get_leds:
1869 * @device_queue: A #ChDeviceQueue
1870 * @device: A #GUsbDevice
1871 * @leds: The LED bitfield
1872 *
1873 * Gets the LED status.
1874 *
1875 * NOTE: This command is available on hardware version: 1 & 2
1876 *
1877 * Since: 0.1.29
1878 **/
1879 void
1880 ch_device_queue_get_leds (ChDeviceQueue *device_queue,
1881 GUsbDevice *device,
1882 guint8 *leds)
1883 {
1884 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1885 g_return_if_fail (G_USB_IS_DEVICE (device));
1886 g_return_if_fail (leds != NULL);
1887
1888 *leds = 0;
1889 ch_device_queue_add (device_queue,
1890 device,
1891 CH_CMD_GET_LEDS,
1892 NULL,
1893 0,
1894 leds,
1895 1);
1896 }
1897
1898 /**
1899 * ch_device_queue_set_leds:
1900 * @device_queue: A #ChDeviceQueue
1901 * @device: A #GUsbDevice
1902 * @leds: The LEDs bitfield
1903 * @repeat: Sets the number of times to repeat the pattern
1904 * @on_time: Set the on time
1905 * @off_time: Set the off time
1906 *
1907 * Sets the LED status.
1908 *
1909 * NOTE: This command is available on hardware version: 1 & 2
1910 *
1911 * Since: 0.1.29
1912 **/
1913 void
1914 ch_device_queue_set_leds (ChDeviceQueue *device_queue,
1915 GUsbDevice *device,
1916 guint8 leds,
1917 guint8 repeat,
1918 guint8 on_time,
1919 guint8 off_time)
1920 {
1921 guint8 buffer[4];
1922
1923 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1924 g_return_if_fail (G_USB_IS_DEVICE (device));
1925 g_return_if_fail (leds < 0x08);
1926
1927 buffer[0] = leds;
1928 buffer[1] = repeat;
1929 buffer[2] = on_time;
1930 buffer[3] = off_time;
1931 ch_device_queue_add (device_queue,
1932 device,
1933 CH_CMD_SET_LEDS,
1934 (const guint8 *) buffer,
1935 sizeof (buffer),
1936 NULL,
1937 0);
1938 }
1939
1940 /**
1941 * ch_device_queue_get_pcb_errata:
1942 * @device_queue: A #ChDeviceQueue
1943 * @device: A #GUsbDevice
1944 * @pcb_errata: The PCB errata, e.g. %CH_PCB_ERRATA_SWAPPED_LEDS
1945 *
1946 * Gets the PCB errata level.
1947 *
1948 * NOTE: This command is available on hardware version: 1 & 2
1949 *
1950 * Since: 0.1.29
1951 **/
1952 void
1953 ch_device_queue_get_pcb_errata (ChDeviceQueue *device_queue,
1954 GUsbDevice *device,
1955 guint16 *pcb_errata)
1956 {
1957 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1958 g_return_if_fail (G_USB_IS_DEVICE (device));
1959 g_return_if_fail (pcb_errata != NULL);
1960
1961 *pcb_errata = CH_PCB_ERRATA_NONE;
1962 ch_device_queue_add (device_queue,
1963 device,
1964 CH_CMD_GET_PCB_ERRATA,
1965 NULL,
1966 0,
1967 (guint8 *) pcb_errata,
1968 sizeof (guint16));
1969 }
1970
1971 /**
1972 * ch_device_queue_set_pcb_errata:
1973 * @device_queue: A #ChDeviceQueue
1974 * @device: A #GUsbDevice
1975 * @pcb_errata: The PCB errata, e.g. %CH_PCB_ERRATA_SWAPPED_LEDS
1976 *
1977 * Sets the PCB board errata.
1978 *
1979 * NOTE: This command is available on hardware version: 1 & 2
1980 *
1981 * Since: 0.1.29
1982 **/
1983 void
1984 ch_device_queue_set_pcb_errata (ChDeviceQueue *device_queue,
1985 GUsbDevice *device,
1986 guint16 pcb_errata)
1987 {
1988 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
1989 g_return_if_fail (G_USB_IS_DEVICE (device));
1990
1991 ch_device_queue_add (device_queue,
1992 device,
1993 CH_CMD_SET_PCB_ERRATA,
1994 (const guint8 *) &pcb_errata,
1995 sizeof (guint16),
1996 NULL,
1997 0);
1998 }
1999
2000 /**
2001 * ch_device_queue_get_remote_hash:
2002 * @device_queue: A #ChDeviceQueue
2003 * @device: A #GUsbDevice
2004 * @remote_hash: A #ChSha1
2005 *
2006 * Gets the remote hash stored on the device.
2007 *
2008 * NOTE: This command is available on hardware version: 1 & 2
2009 *
2010 * Since: 0.1.29
2011 **/
2012 void
2013 ch_device_queue_get_remote_hash (ChDeviceQueue *device_queue,
2014 GUsbDevice *device,
2015 ChSha1 *remote_hash)
2016 {
2017 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2018 g_return_if_fail (G_USB_IS_DEVICE (device));
2019 g_return_if_fail (remote_hash != NULL);
2020
2021 ch_device_queue_add (device_queue,
2022 device,
2023 CH_CMD_GET_REMOTE_HASH,
2024 NULL,
2025 0,
2026 (guint8 *) remote_hash,
2027 sizeof (ChSha1));
2028 }
2029
2030 /**
2031 * ch_device_queue_set_remote_hash:
2032 * @device_queue: A #ChDeviceQueue
2033 * @device: A #GUsbDevice
2034 * @remote_hash: A #ChSha1
2035 *
2036 * Sets the remote hash on the device.
2037 *
2038 * NOTE: This command is available on hardware version: 1 & 2
2039 *
2040 * Since: 0.1.29
2041 **/
2042 void
2043 ch_device_queue_set_remote_hash (ChDeviceQueue *device_queue,
2044 GUsbDevice *device,
2045 ChSha1 *remote_hash)
2046 {
2047 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2048 g_return_if_fail (G_USB_IS_DEVICE (device));
2049
2050 ch_device_queue_add (device_queue,
2051 device,
2052 CH_CMD_SET_REMOTE_HASH,
2053 (const guint8 *) remote_hash,
2054 sizeof (ChSha1),
2055 NULL,
2056 0);
2057 }
2058
2059 /**
2060 * ch_device_queue_write_eeprom:
2061 * @device_queue: A #ChDeviceQueue
2062 * @device: A #GUsbDevice
2063 * @magic: The magic sekret string
2064 *
2065 * Writes values to the firmware to be set at device startup.
2066 *
2067 * NOTE: This command is available on hardware version: 1 & 2
2068 *
2069 * Since: 0.1.29
2070 **/
2071 void
2072 ch_device_queue_write_eeprom (ChDeviceQueue *device_queue,
2073 GUsbDevice *device,
2074 const gchar *magic)
2075 {
2076 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2077 g_return_if_fail (G_USB_IS_DEVICE (device));
2078 g_return_if_fail (magic != NULL);
2079
2080 ch_device_queue_add (device_queue,
2081 device,
2082 CH_CMD_WRITE_EEPROM,
2083 (const guint8 *) magic,
2084 strlen(magic),
2085 NULL,
2086 0);
2087 }
2088
2089 static gboolean
2090 ch_device_queue_buffer_dark_offsets_cb (guint8 *output_buffer,
2091 gsize output_buffer_size,
2092 gpointer user_data,
2093 GError **error)
2094 {
2095 CdColorRGB *value = (CdColorRGB *) user_data;
2096 guint16 tmp;
2097
2098 /* check buffer size */
2099 if (output_buffer_size != sizeof (guint16) * 3) {
2100 g_set_error (error, 1, 0,
2101 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
2102 sizeof (guint16) * 3, output_buffer_size);
2103 return FALSE;
2104 }
2105
2106 /* convert back into floating point */
2107 tmp = cd_buffer_read_uint16_le (output_buffer + 0);
2108 value->R = (gdouble) tmp / (gdouble) 0xffff;
2109 tmp = cd_buffer_read_uint16_le (output_buffer + 2);
2110 value->G = (gdouble) tmp / (gdouble) 0xffff;
2111 tmp = cd_buffer_read_uint16_le (output_buffer + 4);
2112 value->B = (gdouble) tmp / (gdouble) 0xffff;
2113 return TRUE;
2114 }
2115
2116 /**
2117 * ch_device_queue_get_dark_offsets:
2118 * @device_queue: A #ChDeviceQueue
2119 * @device: A #GUsbDevice
2120 * @value: A #CdColorRGB
2121 *
2122 * Gets the device dark offsets.
2123 *
2124 * NOTE: This command is available on hardware version: 1 & 2
2125 *
2126 * Since: 0.1.29
2127 **/
2128 void
2129 ch_device_queue_get_dark_offsets (ChDeviceQueue *device_queue,
2130 GUsbDevice *device,
2131 CdColorRGB *value)
2132 {
2133 guint8 *buffer;
2134
2135 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2136 g_return_if_fail (G_USB_IS_DEVICE (device));
2137 g_return_if_fail (value != NULL);
2138
2139 buffer = g_new0 (guint8, sizeof(guint16) * 3);
2140 ch_device_queue_add_internal (device_queue,
2141 device,
2142 CH_CMD_GET_DARK_OFFSETS,
2143 NULL,
2144 0,
2145 buffer,
2146 sizeof(guint16) * 3,
2147 g_free,
2148 ch_device_queue_buffer_dark_offsets_cb,
2149 value,
2150 NULL);
2151 }
2152
2153 /**
2154 * ch_device_queue_set_dark_offsets:
2155 * @device_queue: A #ChDeviceQueue
2156 * @device: A #GUsbDevice
2157 * @value: A #CdColorRGB
2158 *
2159 * Sets the device dark offsets.
2160 *
2161 * NOTE: This command is available on hardware version: 1 & 2
2162 *
2163 * Since: 0.1.29
2164 **/
2165 void
2166 ch_device_queue_set_dark_offsets (ChDeviceQueue *device_queue,
2167 GUsbDevice *device,
2168 CdColorRGB *value)
2169 {
2170 guint16 buffer[3];
2171
2172 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2173 g_return_if_fail (G_USB_IS_DEVICE (device));
2174
2175 buffer[0] = value->R * (gdouble) 0xffff;
2176 buffer[1] = value->G * (gdouble) 0xffff;
2177 buffer[2] = value->B * (gdouble) 0xffff;
2178 ch_device_queue_add (device_queue,
2179 device,
2180 CH_CMD_SET_DARK_OFFSETS,
2181 (const guint8 *) buffer,
2182 sizeof(buffer),
2183 NULL,
2184 0);
2185 }
2186
2187 /**
2188 * ch_device_queue_take_reading_raw:
2189 * @device_queue: A #ChDeviceQueue
2190 * @device: A #GUsbDevice
2191 * @take_reading: A raw reading value
2192 *
2193 * Take a raw reading from the sensor.
2194 *
2195 * NOTE: This command is available on hardware version: 1 & 2
2196 *
2197 * Since: 0.1.29
2198 **/
2199 void
2200 ch_device_queue_take_reading_raw (ChDeviceQueue *device_queue,
2201 GUsbDevice *device,
2202 guint32 *take_reading)
2203 {
2204 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2205 g_return_if_fail (G_USB_IS_DEVICE (device));
2206 g_return_if_fail (take_reading != NULL);
2207
2208 ch_device_queue_add_internal (device_queue,
2209 device,
2210 CH_CMD_TAKE_READING_RAW,
2211 NULL,
2212 0,
2213 (guint8 *) take_reading,
2214 sizeof(guint32),
2215 NULL,
2216 ch_device_queue_buffer_uint32_from_le_cb,
2217 NULL,
2218 NULL);
2219 }
2220
2221 static gboolean
2222 ch_device_queue_buffer_triple_rgb_cb (guint8 *output_buffer,
2223 gsize output_buffer_size,
2224 gpointer user_data,
2225 GError **error)
2226 {
2227 CdColorRGB *value = (CdColorRGB *) user_data;
2228 ChPackedFloat tmp;
2229
2230 /* check buffer size */
2231 if (output_buffer_size != sizeof (ChPackedFloat) * 3) {
2232 g_set_error (error, 1, 0,
2233 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
2234 sizeof (ChPackedFloat) * 3, output_buffer_size);
2235 return FALSE;
2236 }
2237
2238 /* convert back into floating point */
2239 memcpy (&tmp, output_buffer + 0, sizeof (ChPackedFloat));
2240 ch_packed_float_to_double (&tmp, &value->R);
2241 memcpy (&tmp, output_buffer + 4, sizeof (ChPackedFloat));
2242 ch_packed_float_to_double (&tmp, &value->G);
2243 memcpy (&tmp, output_buffer + 8, sizeof (ChPackedFloat));
2244 ch_packed_float_to_double (&tmp, &value->B);
2245 return TRUE;
2246 }
2247
2248 /**
2249 * ch_device_queue_take_readings:
2250 * @device_queue: A #ChDeviceQueue
2251 * @device: A #GUsbDevice
2252 * @value: The #CdColorRGB of the raw reading
2253 *
2254 * Take a RGB triplet of readings from the sensor without applying the
2255 * calibration matrix.
2256 *
2257 * NOTE: This command is available on hardware version: 1 & 2
2258 *
2259 * Since: 0.1.29
2260 **/
2261 void
2262 ch_device_queue_take_readings (ChDeviceQueue *device_queue,
2263 GUsbDevice *device,
2264 CdColorRGB *value)
2265 {
2266 guint8 *buffer;
2267
2268 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2269 g_return_if_fail (G_USB_IS_DEVICE (device));
2270 g_return_if_fail (value != NULL);
2271
2272 buffer = g_new0 (guint8, sizeof(ChPackedFloat) * 3);
2273 ch_device_queue_add_internal (device_queue,
2274 device,
2275 CH_CMD_TAKE_READINGS,
2276 NULL,
2277 0,
2278 buffer,
2279 sizeof(ChPackedFloat) * 3,
2280 g_free,
2281 ch_device_queue_buffer_triple_rgb_cb,
2282 value,
2283 NULL);
2284 }
2285
2286 static gboolean
2287 ch_device_queue_buffer_triple_xyz_cb (guint8 *output_buffer,
2288 gsize output_buffer_size,
2289 gpointer user_data,
2290 GError **error)
2291 {
2292 CdColorXYZ *value = (CdColorXYZ *) user_data;
2293 ChPackedFloat tmp;
2294
2295 /* check buffer size */
2296 if (output_buffer_size != sizeof (ChPackedFloat) * 3) {
2297 g_set_error (error, 1, 0,
2298 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
2299 sizeof (ChPackedFloat) * 3, output_buffer_size);
2300 return FALSE;
2301 }
2302
2303 /* convert back into floating point */
2304 memcpy (&tmp, output_buffer + 0, sizeof (ChPackedFloat));
2305 ch_packed_float_to_double (&tmp, &value->X);
2306 memcpy (&tmp, output_buffer + 4, sizeof (ChPackedFloat));
2307 ch_packed_float_to_double (&tmp, &value->Y);
2308 memcpy (&tmp, output_buffer + 8, sizeof (ChPackedFloat));
2309 ch_packed_float_to_double (&tmp, &value->Z);
2310 return TRUE;
2311 }
2312
2313 /**
2314 * ch_device_queue_take_readings_xyz:
2315 * @device_queue: A #ChDeviceQueue
2316 * @device: A #GUsbDevice
2317 * @value: The #CdColorXYZ for a given calibration slot
2318 *
2319 * Take an XYZ fully cooked reading from the sensor.
2320 *
2321 * NOTE: This command is available on hardware version: 1 & 2
2322 *
2323 * Since: 0.1.29
2324 **/
2325 void
2326 ch_device_queue_take_readings_xyz (ChDeviceQueue *device_queue,
2327 GUsbDevice *device,
2328 guint16 calibration_index,
2329 CdColorXYZ *value)
2330 {
2331 guint8 *buffer;
2332
2333 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2334 g_return_if_fail (G_USB_IS_DEVICE (device));
2335 g_return_if_fail (value != NULL);
2336
2337 buffer = g_new0 (guint8, sizeof(ChPackedFloat) * 3);
2338 ch_device_queue_add_internal (device_queue,
2339 device,
2340 CH_CMD_TAKE_READING_XYZ,
2341 (guint8 *) &calibration_index,
2342 sizeof(guint16),
2343 buffer,
2344 sizeof(ChPackedFloat) * 3,
2345 g_free,
2346 ch_device_queue_buffer_triple_xyz_cb,
2347 value,
2348 NULL);
2349 }
2350
2351 /**
2352 * ch_device_queue_reset:
2353 * @device_queue: A #ChDeviceQueue
2354 * @device: A #GUsbDevice
2355 *
2356 * Resets the device back to bootloader mode.
2357 *
2358 * NOTE: This command is available on hardware version: 1 & 2
2359 *
2360 * Since: 0.1.29
2361 **/
2362 void
2363 ch_device_queue_reset (ChDeviceQueue *device_queue,
2364 GUsbDevice *device)
2365 {
2366 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2367 g_return_if_fail (G_USB_IS_DEVICE (device));
2368
2369 ch_device_queue_add (device_queue,
2370 device,
2371 CH_CMD_RESET,
2372 NULL,
2373 0,
2374 NULL,
2375 0);
2376 }
2377
2378 static guint8
2379 ch_device_queue_calculate_checksum (const guint8 *data,
2380 gsize len)
2381 {
2382 guint8 checksum = 0xff;
2383 guint i;
2384 for (i = 0; i < len; i++)
2385 checksum ^= data[i];
2386 return checksum;
2387 }
2388
2389 /**
2390 * ch_device_queue_write_flash:
2391 * @device_queue: A #ChDeviceQueue
2392 * @device: A #GUsbDevice
2393 * @address: The device EEPROM address
2394 * @data: (array length=len): Binary data
2395 * @len: The length of @data
2396 *
2397 * Write flash code to the device.
2398 *
2399 * NOTE: This command is available on hardware version: 1 & 2
2400 *
2401 * Since: 0.1.29
2402 **/
2403 void
2404 ch_device_queue_write_flash (ChDeviceQueue *device_queue,
2405 GUsbDevice *device,
2406 guint16 address,
2407 const guint8 *data,
2408 gsize len)
2409 {
2410 guint16 addr_le;
2411 guint8 buffer_tx[64];
2412
2413 /* set address, length, checksum, data */
2414 addr_le = GUINT16_TO_LE (address);
2415 memcpy (buffer_tx + 0, &addr_le, 2);
2416 buffer_tx[2] = len;
2417 buffer_tx[3] = ch_device_queue_calculate_checksum (data, len);
2418 memcpy (buffer_tx + 4, data, len);
2419
2420 ch_device_queue_add (device_queue,
2421 device,
2422 CH_CMD_WRITE_FLASH,
2423 buffer_tx,
2424 len + 4,
2425 NULL,
2426 0);
2427 }
2428
2429 /* tiny helper */
2430 typedef struct {
2431 guint16 address;
2432 guint8 *data;
2433 gsize len;
2434 } ChDeviceQueueReadFlashHelper;
2435
2436 static gboolean
2437 ch_device_queue_buffer_read_flash_cb (guint8 *output_buffer,
2438 gsize output_buffer_size,
2439 gpointer user_data,
2440 GError **error)
2441 {
2442 ChDeviceQueueReadFlashHelper *helper = (ChDeviceQueueReadFlashHelper *) user_data;
2443 guint8 expected_checksum;
2444
2445 /* check buffer size */
2446 if (output_buffer_size != helper->len + 1) {
2447 g_set_error (error, 1, 0,
2448 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
2449 helper->len + 1, output_buffer_size);
2450 return FALSE;
2451 }
2452
2453 /* verify checksum */
2454 expected_checksum = ch_device_queue_calculate_checksum (output_buffer + 1,
2455 helper->len);
2456 if (output_buffer[0] != expected_checksum) {
2457 g_set_error (error, 1, 0,
2458 "Checksum @0x%04x invalid",
2459 helper->address);
2460 return FALSE;
2461 }
2462
2463 /* copy data to final location */
2464 memcpy (helper->data, output_buffer + 1, helper->len);
2465 return TRUE;
2466 }
2467
2468 /**
2469 * ch_device_queue_read_flash:
2470 * @device_queue: A #ChDeviceQueue
2471 * @device: A #GUsbDevice
2472 * @address: The device EEPROM address
2473 * @data: (array length=len): Binary data
2474 * @len: The length of @data
2475 *
2476 * Read flash code from the device.
2477 *
2478 * NOTE: This command is available on hardware version: 1 & 2
2479 *
2480 * Since: 0.1.29
2481 **/
2482 void
2483 ch_device_queue_read_flash (ChDeviceQueue *device_queue,
2484 GUsbDevice *device,
2485 guint16 address,
2486 guint8 *data,
2487 gsize len)
2488 {
2489 ChDeviceQueueReadFlashHelper *helper;
2490 guint16 addr_le;
2491 guint8 *buffer;
2492 guint8 buffer_tx[3];
2493
2494 /* set address, length, checksum, data */
2495 addr_le = GUINT16_TO_LE (address);
2496 memcpy (buffer_tx + 0, &addr_le, 2);
2497 buffer_tx[2] = len;
2498
2499 /* create a helper structure as the checksum needs an extra
2500 * byte for the checksum */
2501 helper = g_new0 (ChDeviceQueueReadFlashHelper, 1);
2502 helper->data = data;
2503 helper->len = len;
2504 helper->address = address;
2505
2506 buffer = g_new0 (guint8, len + 1);
2507 ch_device_queue_add_internal (device_queue,
2508 device,
2509 CH_CMD_READ_FLASH,
2510 buffer_tx,
2511 sizeof(buffer_tx),
2512 buffer,
2513 len + 1,
2514 g_free,
2515 ch_device_queue_buffer_read_flash_cb,
2516 helper,
2517 g_free);
2518 }
2519
2520 static gboolean
2521 ch_device_queue_buffer_verify_flash_cb (guint8 *output_buffer,
2522 gsize output_buffer_size,
2523 gpointer user_data,
2524 GError **error)
2525 {
2526 ChDeviceQueueReadFlashHelper *helper = (ChDeviceQueueReadFlashHelper *) user_data;
2527 guint16 i;
2528 guint8 expected_checksum;
2529
2530 /* check buffer size */
2531 if (output_buffer_size != helper->len + 1) {
2532 g_set_error (error, 1, 0,
2533 "Wrong output buffer size, expected %" G_GSIZE_FORMAT ", got %" G_GSIZE_FORMAT,
2534 helper->len + 1, output_buffer_size);
2535 return FALSE;
2536 }
2537
2538 /* verify checksum */
2539 expected_checksum = ch_device_queue_calculate_checksum (output_buffer + 1,
2540 helper->len);
2541 if (output_buffer[0] != expected_checksum) {
2542 g_set_error (error, 1, 0,
2543 "Checksum @0x%04x invalid",
2544 helper->address);
2545 return FALSE;
2546 }
2547
2548 /* verify data */
2549 if (memcmp (helper->data, output_buffer + 1, helper->len) != 0) {
2550 for (i = 0; i < helper->len; i++) {
2551 if (helper->data[i] != output_buffer[i + 1])
2552 break;
2553 }
2554 g_set_error (error, 1, 0,
2555 "Failed to verify at @0x%04x",
2556 (guint) (helper->address + i));
2557 return FALSE;
2558 }
2559 return TRUE;
2560 }
2561
2562 static void
2563 ch_device_queue_verify_flash_helper_destroy (gpointer data)
2564 {
2565 ChDeviceQueueReadFlashHelper *helper = (ChDeviceQueueReadFlashHelper *) data;
2566 g_free (helper->data);
2567 g_free (helper);
2568 }
2569
2570 /**
2571 * ch_device_queue_verify_flash:
2572 * @device_queue: A #ChDeviceQueue
2573 * @device: A #GUsbDevice
2574 * @address: The device EEPROM address
2575 * @data: (array length=len): Binary data
2576 * @len: The length of @data
2577 *
2578 * Verify flash code from the device.
2579 *
2580 * NOTE: This command is available on hardware version: 1 & 2
2581 *
2582 * Since: 0.1.29
2583 **/
2584 void
2585 ch_device_queue_verify_flash (ChDeviceQueue *device_queue,
2586 GUsbDevice *device,
2587 guint16 address,
2588 const guint8 *data,
2589 gsize len)
2590 {
2591 ChDeviceQueueReadFlashHelper *helper;
2592 guint16 addr_le;
2593 guint8 *buffer;
2594 guint8 buffer_tx[3];
2595
2596 /* set address, length, checksum, data */
2597 addr_le = GUINT16_TO_LE (address);
2598 memcpy (buffer_tx + 0, &addr_le, 2);
2599 buffer_tx[2] = len;
2600
2601 /* create a helper structure as the checksum needs an extra
2602 * byte for the checksum */
2603 helper = g_new0 (ChDeviceQueueReadFlashHelper, 1);
2604 helper->data = g_memdup (data, len + 1);
2605 helper->len = len;
2606 helper->address = address;
2607
2608 buffer = g_new0 (guint8, len + 1);
2609 ch_device_queue_add_internal (device_queue,
2610 device,
2611 CH_CMD_READ_FLASH,
2612 buffer_tx,
2613 sizeof(buffer_tx),
2614 buffer,
2615 len + 1,
2616 g_free,
2617 ch_device_queue_buffer_verify_flash_cb,
2618 helper,
2619 ch_device_queue_verify_flash_helper_destroy);
2620 }
2621
2622 /**
2623 * ch_device_queue_erase_flash:
2624 * @device_queue: A #ChDeviceQueue
2625 * @device: A #GUsbDevice
2626 * @address: (array length=len): The device EEPROM address
2627 * @len: The length of @data
2628 *
2629 * Erase program code on the device.
2630 *
2631 * NOTE: This command is available on hardware version: 1 & 2
2632 *
2633 * Since: 0.1.29
2634 **/
2635 void
2636 ch_device_queue_erase_flash (ChDeviceQueue *device_queue,
2637 GUsbDevice *device,
2638 guint16 address,
2639 gsize len)
2640 {
2641 guint8 buffer_tx[4];
2642 guint16 addr_le;
2643 guint16 len_le;
2644
2645 /* set address, length, checksum, data */
2646 addr_le = GUINT16_TO_LE (address);
2647 memcpy (buffer_tx + 0, &addr_le, 2);
2648 len_le = GUINT16_TO_LE (len);
2649 memcpy (buffer_tx + 2, &len_le, 2);
2650
2651 ch_device_queue_add (device_queue,
2652 device,
2653 CH_CMD_ERASE_FLASH,
2654 buffer_tx,
2655 sizeof(buffer_tx),
2656 NULL,
2657 0);
2658 }
2659
2660 /**
2661 * ch_device_queue_set_flash_success:
2662 * @device_queue: A #ChDeviceQueue
2663 * @device: A #GUsbDevice
2664 * @value: Success value
2665 *
2666 * Sets the firmware flash success value.
2667 * Be careful using this function as misuse can result in a 'bricked'
2668 * ColorHug device.
2669 *
2670 * NOTE: This command is available on hardware version: 1 & 2
2671 *
2672 * Since: 0.1.29
2673 **/
2674 void
2675 ch_device_queue_set_flash_success (ChDeviceQueue *device_queue,
2676 GUsbDevice *device,
2677 guint8 value)
2678 {
2679 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2680 g_return_if_fail (G_USB_IS_DEVICE (device));
2681
2682 /* set flash success true */
2683 ch_device_queue_add (device_queue,
2684 device,
2685 CH_CMD_SET_FLASH_SUCCESS,
2686 (guint8 *) &value, 1,
2687 NULL, 0);
2688 }
2689
2690 /**
2691 * ch_device_queue_boot_flash:
2692 * @device_queue: A #ChDeviceQueue
2693 * @device: A #GUsbDevice
2694 *
2695 * Boots the device from bootloader to firmware mode.
2696 *
2697 * NOTE: This command is available on hardware version: 1 & 2
2698 *
2699 * Since: 0.1.29
2700 **/
2701 void
2702 ch_device_queue_boot_flash (ChDeviceQueue *device_queue,
2703 GUsbDevice *device)
2704 {
2705 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2706 g_return_if_fail (G_USB_IS_DEVICE (device));
2707
2708 /* boot into new code */
2709 ch_device_queue_add (device_queue,
2710 device,
2711 CH_CMD_BOOT_FLASH,
2712 NULL, 0,
2713 NULL, 0);
2714 }
2715
2716 /**
2717 * ch_device_queue_self_test:
2718 * @device_queue: A #ChDeviceQueue
2719 * @device: A #GUsbDevice
2720 *
2721 * Performs some self tests on the device.
2722 *
2723 * NOTE: This command is available on hardware version: 1 & 2
2724 *
2725 * Since: 0.1.29
2726 **/
2727 void
2728 ch_device_queue_self_test (ChDeviceQueue *device_queue,
2729 GUsbDevice *device)
2730 {
2731 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2732 g_return_if_fail (G_USB_IS_DEVICE (device));
2733
2734 /* do a really simple self test */
2735 ch_device_queue_add (device_queue,
2736 device,
2737 CH_CMD_SELF_TEST,
2738 NULL, 0,
2739 NULL, 0);
2740 }
2741
2742 /**
2743 * ch_device_queue_get_hardware_version:
2744 * @device_queue: A #ChDeviceQueue
2745 * @device: A #GUsbDevice
2746 * @hw_version: The hardware version
2747 *
2748 * Gets the hardware version.
2749 *
2750 * NOTE: This command is available on hardware version: 1 & 2
2751 *
2752 * Since: 0.1.29
2753 **/
2754 void
2755 ch_device_queue_get_hardware_version (ChDeviceQueue *device_queue,
2756 GUsbDevice *device,
2757 guint8 *hw_version)
2758 {
2759 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2760 g_return_if_fail (G_USB_IS_DEVICE (device));
2761 g_return_if_fail (hw_version != NULL);
2762
2763 ch_device_queue_add (device_queue,
2764 device,
2765 CH_CMD_GET_HARDWARE_VERSION,
2766 NULL,
2767 0,
2768 hw_version,
2769 1);
2770 }
2771
2772 /**
2773 * ch_device_queue_get_owner_name:
2774 * @device_queue: A #ChDeviceQueue
2775 * @device: A #GUsbDevice
2776 * @name: The owner name
2777 *
2778 * Gets the owner name.
2779 *
2780 * NOTE: This command is available on hardware version: 1 & 2
2781 *
2782 * Since: 0.1.29
2783 **/
2784 void
2785 ch_device_queue_get_owner_name (ChDeviceQueue *device_queue,
2786 GUsbDevice *device,
2787 gchar *name)
2788 {
2789 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2790 g_return_if_fail (G_USB_IS_DEVICE (device));
2791 g_return_if_fail (name != NULL);
2792
2793 ch_device_queue_add (device_queue,
2794 device,
2795 CH_CMD_GET_OWNER_NAME,
2796 NULL,
2797 0,
2798 (guint8 *) name,
2799 sizeof(gchar) * CH_OWNER_LENGTH_MAX);
2800 name[CH_OWNER_LENGTH_MAX-1] = 0;
2801 }
2802
2803 /**
2804 * ch_device_queue_set_owner_name:
2805 * @device_queue: A #ChDeviceQueue
2806 * @device: A #GUsbDevice
2807 * @name: The owner name
2808 *
2809 * Sets the owner name.
2810 *
2811 * NOTE: This command is available on hardware version: 1 & 2
2812 *
2813 * Since: 0.1.29
2814 **/
2815 void
2816 ch_device_queue_set_owner_name (ChDeviceQueue *device_queue,
2817 GUsbDevice *device,
2818 const gchar *name)
2819 {
2820 gchar buf[CH_OWNER_LENGTH_MAX];
2821
2822 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2823 g_return_if_fail (G_USB_IS_DEVICE (device));
2824 g_return_if_fail (name != NULL);
2825
2826 memset(buf, 0, CH_OWNER_LENGTH_MAX);
2827 (void)g_strlcpy(buf, name, CH_OWNER_LENGTH_MAX);
2828
2829 ch_device_queue_add (device_queue,
2830 device,
2831 CH_CMD_SET_OWNER_NAME,
2832 (const guint8 *) buf,
2833 sizeof(gchar) * CH_OWNER_LENGTH_MAX,
2834 NULL,
2835 0);
2836 }
2837
2838 /**
2839 * ch_device_queue_get_owner_email:
2840 * @device_queue: A #ChDeviceQueue
2841 * @device: A #GUsbDevice
2842 * @email: An email address
2843 *
2844 * Gets the owner email address.
2845 *
2846 * NOTE: This command is available on hardware version: 1 & 2
2847 *
2848 * Since: 0.1.29
2849 **/
2850 void
2851 ch_device_queue_get_owner_email (ChDeviceQueue *device_queue,
2852 GUsbDevice *device,
2853 gchar *email)
2854 {
2855 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2856 g_return_if_fail (G_USB_IS_DEVICE (device));
2857 g_return_if_fail (email != NULL);
2858
2859 ch_device_queue_add (device_queue,
2860 device,
2861 CH_CMD_GET_OWNER_EMAIL,
2862 NULL,
2863 0,
2864 (guint8 *) email,
2865 sizeof(gchar) * CH_OWNER_LENGTH_MAX);
2866 email[CH_OWNER_LENGTH_MAX-1] = 0;
2867 }
2868
2869 /**
2870 * ch_device_queue_set_owner_email:
2871 * @device_queue: A #ChDeviceQueue
2872 * @device: A #GUsbDevice
2873 * @email: An email address
2874 *
2875 * Sets the owner email address.
2876 *
2877 * NOTE: This command is available on hardware version: 1 & 2
2878 *
2879 * Since: 0.1.29
2880 **/
2881 void
2882 ch_device_queue_set_owner_email (ChDeviceQueue *device_queue,
2883 GUsbDevice *device,
2884 const gchar *email)
2885 {
2886 gchar buf[CH_OWNER_LENGTH_MAX];
2887
2888 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2889 g_return_if_fail (G_USB_IS_DEVICE (device));
2890 g_return_if_fail (email != NULL);
2891
2892 memset (buf, 0, CH_OWNER_LENGTH_MAX);
2893 (void)g_strlcpy (buf, email, CH_OWNER_LENGTH_MAX);
2894
2895 ch_device_queue_add (device_queue,
2896 device,
2897 CH_CMD_SET_OWNER_EMAIL,
2898 (const guint8 *) buf,
2899 sizeof(gchar) * CH_OWNER_LENGTH_MAX,
2900 NULL,
2901 0);
2902 }
2903
2904 /**
2905 * ch_device_queue_take_reading_array:
2906 * @device_queue: A #ChDeviceQueue
2907 * @device: A #GUsbDevice
2908 * @reading_array: An array of raw readings
2909 *
2910 * Get an array of raw readings in quick succession.
2911 *
2912 * NOTE: This command is available on hardware version: 1 & 2
2913 *
2914 * Since: 0.1.29
2915 **/
2916 void
2917 ch_device_queue_take_reading_array (ChDeviceQueue *device_queue,
2918 GUsbDevice *device,
2919 guint8 *reading_array)
2920 {
2921 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2922 g_return_if_fail (G_USB_IS_DEVICE (device));
2923 g_return_if_fail (reading_array != NULL);
2924
2925 ch_device_queue_add (device_queue,
2926 device,
2927 CH_CMD_TAKE_READING_ARRAY,
2928 NULL,
2929 0,
2930 reading_array,
2931 30);
2932 }
2933
2934 /**
2935 * ch_device_queue_get_measure_mode:
2936 * @device_queue: A #ChDeviceQueue
2937 * @device: A #GUsbDevice
2938 * @measure_mode: The #ChMeasureMode, e.g. %CH_MEASURE_MODE_DURATION
2939 *
2940 * Gets the measurement mode.
2941 *
2942 * NOTE: This command is available on hardware version: 1 & 2
2943 *
2944 * Since: 0.1.29
2945 **/
2946 void
2947 ch_device_queue_get_measure_mode (ChDeviceQueue *device_queue,
2948 GUsbDevice *device,
2949 ChMeasureMode *measure_mode)
2950 {
2951 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2952 g_return_if_fail (G_USB_IS_DEVICE (device));
2953 g_return_if_fail (measure_mode != NULL);
2954
2955 ch_device_queue_add (device_queue,
2956 device,
2957 CH_CMD_GET_MEASURE_MODE,
2958 NULL,
2959 0,
2960 (guint8 *) measure_mode,
2961 1);
2962 }
2963
2964 /**
2965 * ch_device_queue_set_measure_mode:
2966 * @device_queue: A #ChDeviceQueue
2967 * @device: A #GUsbDevice
2968 * @measure_mode: The #ChMeasureMode, e.g. %CH_MEASURE_MODE_DURATION
2969 *
2970 * Sets the measurement mode.
2971 *
2972 * NOTE: This command is available on hardware version: 1 & 2
2973 *
2974 * Since: 0.1.29
2975 **/
2976 void
2977 ch_device_queue_set_measure_mode (ChDeviceQueue *device_queue,
2978 GUsbDevice *device,
2979 ChMeasureMode measure_mode)
2980 {
2981 guint8 tmp = measure_mode;
2982
2983 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
2984 g_return_if_fail (G_USB_IS_DEVICE (device));
2985
2986 ch_device_queue_add (device_queue,
2987 device,
2988 CH_CMD_SET_MEASURE_MODE,
2989 &tmp,
2990 1,
2991 NULL,
2992 0);
2993 }
2994
2995 static void
2996 ch_device_queue_write_sram_internal (ChDeviceQueue *device_queue,
2997 GUsbDevice *device,
2998 guint16 address,
2999 const guint8 *data,
3000 gsize len)
3001 {
3002 guint16 addr_le;
3003 guint8 buffer_tx[CH_USB_HID_EP_SIZE];
3004
3005 /* set address, length, checksum, data */
3006 addr_le = GUINT16_TO_LE (address);
3007 memcpy (buffer_tx + 0, &addr_le, 2);
3008 buffer_tx[2] = len;
3009 memcpy (buffer_tx + 3, data, len);
3010
3011 ch_device_queue_add (device_queue,
3012 device,
3013 CH_CMD_WRITE_SRAM,
3014 buffer_tx,
3015 len + 3,
3016 NULL,
3017 0);
3018 }
3019
3020 /**
3021 * ch_device_queue_write_sram:
3022 * @device_queue: A #ChDeviceQueue
3023 * @device: A #GUsbDevice
3024 * @address: The device memory address
3025 * @data: (array length=len): The binary data
3026 * @len: Size of @data
3027 *
3028 * Writes binary data to the SRAM.
3029 *
3030 * NOTE: This command is available on hardware version: 2
3031 *
3032 * Since: 0.1.29
3033 **/
3034 void
3035 ch_device_queue_write_sram (ChDeviceQueue *device_queue,
3036 GUsbDevice *device,
3037 guint16 address,
3038 const guint8 *data,
3039 gsize len)
3040 {
3041 gsize chunk_len = 60;
3042 guint idx = 0;
3043
3044 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
3045 g_return_if_fail (G_USB_IS_DEVICE (device));
3046 g_return_if_fail (data != NULL);
3047 g_return_if_fail (len > 0);
3048
3049 /* write in 60 byte chunks */
3050 do {
3051 if (idx + chunk_len > len)
3052 chunk_len = len - idx;
3053 g_debug ("Writing SRAM at %04x size %" G_GSIZE_FORMAT,
3054 idx, chunk_len);
3055 ch_device_queue_write_sram_internal (device_queue,
3056 device,
3057 idx,
3058 data + idx,
3059 chunk_len);
3060 idx += chunk_len;
3061 } while (idx < len);
3062 }
3063
3064 static void
3065 ch_device_queue_read_sram_internal (ChDeviceQueue *device_queue,
3066 GUsbDevice *device,
3067 guint16 address,
3068 guint8 *data,
3069 gsize len)
3070 {
3071 guint16 addr_le;
3072 guint8 buffer_tx[3];
3073
3074 /* set address, length, data */
3075 addr_le = GUINT16_TO_LE (address);
3076 memcpy (buffer_tx + 0, &addr_le, 2);
3077 buffer_tx[2] = len;
3078
3079 ch_device_queue_add (device_queue,
3080 device,
3081 CH_CMD_READ_SRAM,
3082 buffer_tx,
3083 sizeof(buffer_tx),
3084 data,
3085 len);
3086 }
3087
3088 /**
3089 * ch_device_queue_read_sram:
3090 * @device_queue: A #ChDeviceQueue
3091 * @device: A #GUsbDevice
3092 * @address: The device memory address
3093 * @data: (array length=len): The binary data
3094 * @len: Size of @data
3095 *
3096 * Reads binary data from the SRAM.
3097 *
3098 * NOTE: This command is available on hardware version: 2
3099 *
3100 * Since: 0.1.29
3101 **/
3102 void
3103 ch_device_queue_read_sram (ChDeviceQueue *device_queue,
3104 GUsbDevice *device,
3105 guint16 address,
3106 guint8 *data,
3107 gsize len)
3108 {
3109 gsize chunk_len = 60;
3110 guint idx = 0;
3111
3112 g_return_if_fail (CH_IS_DEVICE_QUEUE (device_queue));
3113 g_return_if_fail (G_USB_IS_DEVICE (device));
3114 g_return_if_fail (data != NULL);
3115 g_return_if_fail (len > 0);
3116
3117 /* write in 60 byte chunks */
3118 do {
3119 if (idx + chunk_len > len)
3120 chunk_len = len - idx;
3121 g_debug ("Reading SRAM at %04x size %" G_GSIZE_FORMAT,
3122 idx, chunk_len);
3123 ch_device_queue_read_sram_internal (device_queue,
3124 device,
3125 idx,
3126 data + idx,
3127 chunk_len);
3128 idx += chunk_len;
3129 } while (idx < len);
3130 }
3131
3132 /**********************************************************************/
3133
3134 static void
3135 ch_device_queue_class_init (ChDeviceQueueClass *klass)
3136 {
3137 GObjectClass *object_class = G_OBJECT_CLASS (klass);
3138 object_class->finalize = ch_device_queue_finalize;
3139
3140 /**
3141 * ChDeviceQueueClass::device-failed:
3142 * @device_queue: the #ChDeviceQueue instance that emitted the signal
3143 * @device: the device that failed
3144 * @error_message: the error that caused the failure
3145 *
3146 * The ::device-failed signal is emitted when a device has failed.
3147 **/
3148 signals[SIGNAL_DEVICE_FAILED] =
3149 g_signal_new ("device-failed",
3150 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
3151 G_STRUCT_OFFSET (ChDeviceQueueClass, device_failed),
3152 NULL, NULL, g_cclosure_marshal_generic,
3153 G_TYPE_NONE, 2, G_TYPE_OBJECT, G_TYPE_STRING);
3154
3155 /**
3156 * ChDeviceQueueClass::progress-changed:
3157 * @device_queue: the #ChDeviceQueue instance that emitted the signal
3158 * @percentage: the percentage complete the action is
3159 * @error_message: the error that caused the failure
3160 *
3161 * The ::progress-changed signal is emitted when a the commands
3162 * are being submitted.
3163 **/
3164 signals[SIGNAL_PROGRESS_CHANGED] =
3165 g_signal_new ("progress-changed",
3166 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
3167 G_STRUCT_OFFSET (ChDeviceQueueClass, progress_changed),
3168 NULL, NULL, g_cclosure_marshal_generic,
3169 G_TYPE_NONE, 1, G_TYPE_UINT);
3170 }
3171
3172 static void
3173 ch_device_queue_init (ChDeviceQueue *device_queue)
3174 {
3175 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
3176 priv->data_array = g_ptr_array_new_with_free_func ((GDestroyNotify) ch_device_queue_data_free);
3177 priv->devices_in_use = g_hash_table_new_full (g_str_hash,
3178 g_str_equal,
3179 g_free,
3180 NULL);
3181 }
3182
3183 static void
3184 ch_device_queue_finalize (GObject *object)
3185 {
3186 ChDeviceQueue *device_queue = CH_DEVICE_QUEUE (object);
3187 ChDeviceQueuePrivate *priv = GET_PRIVATE (device_queue);
3188
3189 g_ptr_array_unref (priv->data_array);
3190 g_hash_table_unref (priv->devices_in_use);
3191
3192 G_OBJECT_CLASS (ch_device_queue_parent_class)->finalize (object);
3193 }
3194
3195 /**
3196 * ch_device_queue_new:
3197 *
3198 * Since: 0.1.29
3199 **/
3200 ChDeviceQueue *
3201 ch_device_queue_new (void)
3202 {
3203 ChDeviceQueue *device_queue;
3204 device_queue = g_object_new (CH_TYPE_DEVICE_QUEUE, NULL);
3205 return CH_DEVICE_QUEUE (device_queue);
3206 }