| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 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 |
/** |
| 23 |
* SECTION:dtp94-enum |
| 24 |
* @short_description: Types used by dtp94 and libdtp94 |
| 25 |
* |
| 26 |
* These helper functions provide a way to marshal enumerated values to |
| 27 |
* text and back again. |
| 28 |
* |
| 29 |
* See also: #CdClient, #CdDevice |
| 30 |
*/ |
| 31 |
|
| 32 |
#include "config.h" |
| 33 |
|
| 34 |
#include <glib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <colord-private.h> |
| 37 |
|
| 38 |
#include "dtp94-device.h" |
| 39 |
#include "dtp94-enum.h" |
| 40 |
|
| 41 |
#define DTP94_MAX_READ_RETRIES 5 |
| 42 |
#define DTP94_CONTROL_MESSAGE_TIMEOUT 50000 /* ms */ |
| 43 |
|
| 44 |
/** |
| 45 |
* dtp94_device_error_quark: |
| 46 |
**/ |
| 47 |
GQuark |
| 48 |
dtp94_device_error_quark (void) |
| 49 |
{ |
| 50 |
static GQuark quark = 0; |
| 51 |
if (!quark) |
| 52 |
quark = g_quark_from_static_string ("Dtp94DeviceError"); |
| 53 |
return quark; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* dtp94_device_send_data: |
| 58 |
* |
| 59 |
* Since: 0.1.29 |
| 60 |
**/ |
| 61 |
gboolean |
| 62 |
dtp94_device_send_data (GUsbDevice *device, |
| 63 |
const guint8 *request, |
| 64 |
gsize request_len, |
| 65 |
guint8 *reply, |
| 66 |
gsize reply_len, |
| 67 |
gsize *reply_read, |
| 68 |
GError **error) |
| 69 |
{ |
| 70 |
gboolean ret; |
| 71 |
|
| 72 |
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE); |
| 73 |
g_return_val_if_fail (request != NULL, FALSE); |
| 74 |
g_return_val_if_fail (request_len != 0, FALSE); |
| 75 |
g_return_val_if_fail (reply != NULL, FALSE); |
| 76 |
g_return_val_if_fail (reply_len != 0, FALSE); |
| 77 |
g_return_val_if_fail (reply_read != NULL, FALSE); |
| 78 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 79 |
|
| 80 |
/* request data from device */ |
| 81 |
cd_buffer_debug (CD_BUFFER_KIND_REQUEST, |
| 82 |
request, request_len); |
| 83 |
ret = g_usb_device_interrupt_transfer (device, |
| 84 |
0x2, |
| 85 |
(guint8 *) request, |
| 86 |
request_len, |
| 87 |
NULL, |
| 88 |
DTP94_CONTROL_MESSAGE_TIMEOUT, |
| 89 |
NULL, |
| 90 |
error); |
| 91 |
if (!ret) |
| 92 |
return FALSE; |
| 93 |
|
| 94 |
/* get sync response */ |
| 95 |
ret = g_usb_device_interrupt_transfer (device, |
| 96 |
0x81, |
| 97 |
(guint8 *) reply, |
| 98 |
reply_len, |
| 99 |
reply_read, |
| 100 |
DTP94_CONTROL_MESSAGE_TIMEOUT, |
| 101 |
NULL, |
| 102 |
error); |
| 103 |
if (!ret) |
| 104 |
return FALSE; |
| 105 |
if (*reply_read == 0) { |
| 106 |
g_set_error_literal (error, |
| 107 |
DTP94_DEVICE_ERROR, |
| 108 |
DTP94_DEVICE_ERROR_INTERNAL, |
| 109 |
"failed to get data from device"); |
| 110 |
return FALSE; |
| 111 |
} |
| 112 |
cd_buffer_debug (CD_BUFFER_KIND_RESPONSE, |
| 113 |
reply, *reply_read); |
| 114 |
return TRUE; |
| 115 |
} |
| 116 |
|
| 117 |
static gboolean |
| 118 |
dtp94_device_send_cmd_issue (GUsbDevice *device, |
| 119 |
const gchar *command, |
| 120 |
GError **error) |
| 121 |
{ |
| 122 |
gboolean ret; |
| 123 |
gsize reply_read; |
| 124 |
guint8 buffer[128]; |
| 125 |
guint8 rc; |
| 126 |
guint command_len; |
| 127 |
|
| 128 |
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE); |
| 129 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 130 |
|
| 131 |
/* sent command raw */ |
| 132 |
command_len = strlen (command); |
| 133 |
ret = dtp94_device_send_data (device, |
| 134 |
(const guint8 *) command, |
| 135 |
command_len, |
| 136 |
buffer, |
| 137 |
sizeof (buffer), |
| 138 |
&reply_read, |
| 139 |
error); |
| 140 |
if (!ret) |
| 141 |
return FALSE; |
| 142 |
|
| 143 |
/* device busy */ |
| 144 |
rc = dtp94_rc_parse (buffer, reply_read); |
| 145 |
if (rc == DTP94_RC_BAD_COMMAND) { |
| 146 |
g_set_error_literal (error, |
| 147 |
DTP94_DEVICE_ERROR, |
| 148 |
DTP94_DEVICE_ERROR_NO_DATA, |
| 149 |
"device busy"); |
| 150 |
return FALSE; |
| 151 |
} |
| 152 |
|
| 153 |
/* no success */ |
| 154 |
if (rc != DTP94_RC_OK) { |
| 155 |
buffer[reply_read] = '\0'; |
| 156 |
g_set_error (error, |
| 157 |
DTP94_DEVICE_ERROR, |
| 158 |
DTP94_DEVICE_ERROR_INTERNAL, |
| 159 |
"unexpected response from device: %s [%s]", |
| 160 |
(const gchar *) buffer, |
| 161 |
dtp94_rc_to_string (rc)); |
| 162 |
return FALSE; |
| 163 |
} |
| 164 |
return TRUE; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* dtp94_device_send_cmd: |
| 169 |
* |
| 170 |
* Since: 0.1.29 |
| 171 |
**/ |
| 172 |
gboolean |
| 173 |
dtp94_device_send_cmd (GUsbDevice *device, |
| 174 |
const gchar *command, |
| 175 |
GError **error) |
| 176 |
{ |
| 177 |
gboolean ret = FALSE; |
| 178 |
GError *error_local = NULL; |
| 179 |
guint error_cnt = 0; |
| 180 |
|
| 181 |
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE); |
| 182 |
g_return_val_if_fail (command != NULL, FALSE); |
| 183 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 184 |
|
| 185 |
/* repeat until the device is ready */ |
| 186 |
for (error_cnt = 0; ret != TRUE; error_cnt++) { |
| 187 |
ret = dtp94_device_send_cmd_issue (device, command, &error_local); |
| 188 |
if (!ret) { |
| 189 |
if (error_cnt < DTP94_MAX_READ_RETRIES && |
| 190 |
g_error_matches (error_local, |
| 191 |
DTP94_DEVICE_ERROR, |
| 192 |
DTP94_DEVICE_ERROR_NO_DATA)) { |
| 193 |
g_debug ("ignoring %s", error_local->message); |
| 194 |
g_clear_error (&error_local); |
| 195 |
continue; |
| 196 |
} |
| 197 |
g_propagate_error (error, error_local); |
| 198 |
break; |
| 199 |
} |
| 200 |
}; |
| 201 |
return ret; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* dtp94_device_setup: |
| 206 |
* |
| 207 |
* Since: 0.1.29 |
| 208 |
**/ |
| 209 |
gboolean |
| 210 |
dtp94_device_setup (GUsbDevice *device, GError **error) |
| 211 |
{ |
| 212 |
g_return_val_if_fail (G_USB_IS_DEVICE (device), FALSE); |
| 213 |
g_return_val_if_fail (error == NULL || *error == NULL, FALSE); |
| 214 |
|
| 215 |
/* reset device */ |
| 216 |
if (!dtp94_device_send_cmd (device, "0PR\r", error)) |
| 217 |
return FALSE; |
| 218 |
|
| 219 |
/* reset device again */ |
| 220 |
if (!dtp94_device_send_cmd (device, "0PR\r", error)) |
| 221 |
return FALSE; |
| 222 |
|
| 223 |
/* set color data separator to '\t' */ |
| 224 |
if (!dtp94_device_send_cmd (device, "0207CF\r", error)) |
| 225 |
return FALSE; |
| 226 |
|
| 227 |
/* set delimiter to CR */ |
| 228 |
if (!dtp94_device_send_cmd (device, "0008CF\r", error)) |
| 229 |
return FALSE; |
| 230 |
|
| 231 |
/* set extra digit resolution */ |
| 232 |
if (!dtp94_device_send_cmd (device, "010ACF\r", error)) |
| 233 |
return FALSE; |
| 234 |
|
| 235 |
/* no black point subtraction */ |
| 236 |
if (!dtp94_device_send_cmd (device, "0019CF\r", error)) |
| 237 |
return FALSE; |
| 238 |
|
| 239 |
/* set to factory calibration */ |
| 240 |
if (!dtp94_device_send_cmd (device, "EFC\r", error)) |
| 241 |
return FALSE; |
| 242 |
|
| 243 |
/* compensate for offset drift */ |
| 244 |
if (!dtp94_device_send_cmd (device, "0117CF\r", error)) |
| 245 |
return FALSE; |
| 246 |
|
| 247 |
return TRUE; |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* dtp94_device_take_sample: |
| 252 |
* |
| 253 |
* Since: 0.1.29 |
| 254 |
**/ |
| 255 |
CdColorXYZ * |
| 256 |
dtp94_device_take_sample (GUsbDevice *device, CdSensorCap cap, GError **error) |
| 257 |
{ |
| 258 |
CdColorXYZ *result = NULL; |
| 259 |
gboolean ret = FALSE; |
| 260 |
gchar *tmp; |
| 261 |
gsize reply_read; |
| 262 |
guint8 buffer[128]; |
| 263 |
|
| 264 |
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL); |
| 265 |
g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 266 |
|
| 267 |
/* set hardware support */ |
| 268 |
switch (cap) { |
| 269 |
case CD_SENSOR_CAP_CRT: |
| 270 |
case CD_SENSOR_CAP_PLASMA: |
| 271 |
/* CRT = 01 */ |
| 272 |
ret = dtp94_device_send_cmd (device, "0116CF\r", error); |
| 273 |
break; |
| 274 |
case CD_SENSOR_CAP_LCD: |
| 275 |
/* LCD = 02 */ |
| 276 |
ret = dtp94_device_send_cmd (device, "0216CF\r", error); |
| 277 |
break; |
| 278 |
default: |
| 279 |
g_set_error (error, |
| 280 |
DTP94_DEVICE_ERROR, |
| 281 |
DTP94_DEVICE_ERROR_NO_SUPPORT, |
| 282 |
"DTP94 cannot measure in %s mode", |
| 283 |
cd_sensor_cap_to_string (cap)); |
| 284 |
break; |
| 285 |
} |
| 286 |
if (!ret) |
| 287 |
return NULL; |
| 288 |
|
| 289 |
/* get sample */ |
| 290 |
ret = dtp94_device_send_data (device, |
| 291 |
(const guint8 *) "RM\r", 3, |
| 292 |
buffer, sizeof (buffer), |
| 293 |
&reply_read, |
| 294 |
error); |
| 295 |
if (!ret) |
| 296 |
return NULL; |
| 297 |
tmp = g_strstr_len ((const gchar *) buffer, reply_read, "\r"); |
| 298 |
if (tmp == NULL || memcmp (tmp + 1, "<00>", 4) != 0) { |
| 299 |
buffer[reply_read] = '\0'; |
| 300 |
g_set_error (error, |
| 301 |
DTP94_DEVICE_ERROR, |
| 302 |
DTP94_DEVICE_ERROR_INTERNAL, |
| 303 |
"unexpected response from device: %s", |
| 304 |
(const gchar *) buffer); |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
|
| 308 |
/* format is raw ASCII with fixed formatting: |
| 309 |
* 'X 10.29 Y 10.33 Z 4.65\u000d<00>' */ |
| 310 |
tmp = (gchar *) buffer; |
| 311 |
g_strdelimit (tmp, "\t\r", '\0'); |
| 312 |
|
| 313 |
/* success */ |
| 314 |
result = cd_color_xyz_new (); |
| 315 |
cd_color_xyz_set (result, |
| 316 |
g_ascii_strtod (tmp + 1, NULL), |
| 317 |
g_ascii_strtod (tmp + 13, NULL), |
| 318 |
g_ascii_strtod (tmp + 25, NULL)); |
| 319 |
return result; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* dtp94_device_get_serial: |
| 324 |
* |
| 325 |
* Since: 0.1.29 |
| 326 |
**/ |
| 327 |
gchar * |
| 328 |
dtp94_device_get_serial (GUsbDevice *device, GError **error) |
| 329 |
{ |
| 330 |
gboolean ret; |
| 331 |
gchar *tmp; |
| 332 |
gsize reply_read; |
| 333 |
guint8 buffer[128]; |
| 334 |
|
| 335 |
g_return_val_if_fail (G_USB_IS_DEVICE (device), NULL); |
| 336 |
g_return_val_if_fail (error == NULL || *error == NULL, NULL); |
| 337 |
|
| 338 |
ret = dtp94_device_send_data (device, |
| 339 |
(const guint8 *) "SV\r", 3, |
| 340 |
buffer, sizeof (buffer), |
| 341 |
&reply_read, |
| 342 |
error); |
| 343 |
if (!ret) |
| 344 |
return NULL; |
| 345 |
tmp = g_strstr_len ((const gchar *) buffer, reply_read, "\r"); |
| 346 |
if (tmp == NULL || memcmp (tmp + 1, "<00>", 4) != 0) { |
| 347 |
buffer[reply_read] = '\0'; |
| 348 |
g_set_error (error, |
| 349 |
DTP94_DEVICE_ERROR, |
| 350 |
DTP94_DEVICE_ERROR_INTERNAL, |
| 351 |
"unexpected response from device: %s", |
| 352 |
(const gchar *) buffer); |
| 353 |
return NULL; |
| 354 |
} |
| 355 |
tmp[0] = '\0'; |
| 356 |
return g_strdup (tmp); |
| 357 |
} |