Debian Codegraph @

spectro/vtpglut.c

raw symbols SCIP
1
2 /* Abstract Video Test Patch Generator and/or 3dLUT class implemenation */
3
4 /*
5 * Argyll Color Management System
6 *
7 * Author: Graeme W. Gill
8 * Date: 10/3/2001
9 *
10 * Copyright 2001 - 2013 Graeme W. Gill
11 * All rights reserved.
12 *
13 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
14 * see the License2.txt file for licencing details.
15 */
16
17 /*
18 TTBD:
19
20 Note that there is some support for HW 3dLUTs in X11 xrandr
21 in a patch for xf86-videeo-amdgpu drmmode_display.c
22 submitted on 3 May 2018.
23 See <https://lists.freedesktop.org/archives/amd-gfx/2018-May/022007.html>
24
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <time.h>
33 #ifndef SALONEINSTLIB
34 #include "copyright.h"
35 #include "aconfig.h"
36 #else
37 #include "sa_config.h"
38 #endif /* !SALONEINSTLIB */
39 #include "numsup.h"
40 #include "cgats.h"
41 #include "xspect.h"
42 #include "conv.h"
43
44 #include "insttypes.h"
45 #include "icoms.h"
46 #include "vtpglut.h"
47 #include "rspec.h"
48 #include "vtpgluttypes.h"
49 #include "sort.h"
50
51 icom_type dev_category(instType itype);
52
53 /* --------------------------------------------------------- */
54
55 #if defined(ENABLE_FAST_SERIAL)
56 extern devType fast_ser_dev_type(icoms *p, int tryhard,
57 inst_code (*uicallback)(void *cntx, inst_ui_purp purp), void *cntx);
58 # if defined(ENABLE_SERIAL)
59 static devType ser_vtpglut_type(icoms *p,
60 vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp), void *cntx);
61
62 /* Translate vtpglut uicallback into inst callback */
63 typedef struct {
64 void *cntx;
65 vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp);
66 } vtpglut_callback_cntx;
67
68 static inst_code ser_uicallback(void *cntx, inst_ui_purp purp) {
69 vtpglut_callback_cntx *p = (vtpglut_callback_cntx *)cntx;
70 vtpglut_ui_purp ser_purp;
71
72 if (purp == vtpglut_negcoms)
73 ser_purp = inst_negcoms;
74 else
75 return inst_internal_error;
76 return p->uicallback(p->cntx, ser_purp);
77 }
78
79 # endif /* ENABLE_SERIAL */
80 #endif /* ENABLE_FAST_SERIAL */
81
82
83 /* ------------------------------------ */
84 /* Default methods for instrument class */
85
86 /* Establish communications at the indicated baud rate. */
87 /* Timeout in to seconds, and return non-zero error code */
88 static vtpglut_code init_coms(
89 vtpglut *p) { /* Timeout */
90 return vtpglut_unsupported;
91 }
92
93 /* Initialise or re-initialise the INST */
94 /* return non-zero on an error, with inst error code */
95 static vtpglut_code init_vtpglut(
96 vtpglut *p) {
97 return vtpglut_unsupported;
98 }
99
100 /* Return the device type */
101 static devType get_dtype(vtpglut *p) {
102 if (p != NULL)
103 return p->dtype;
104 return devUnknown;
105 }
106
107 /* Return the device serial number. */
108 /* (This will be an empty string if there is no serial no) */
109 static char *get_serial_no(vtpglut *p) {
110 return "";
111 }
112
113 /* Return the device mode/capabilities */
114 static void capabilities(vtpglut *p, vtpglut_mode *cap1,
115 vtpglut_capability *cap2) {
116 if (cap1 != NULL)
117 *cap1 = vtpglut_mode_none;
118 if (cap2 != NULL)
119 *cap2 = vtpglut2_none;
120 }
121
122 /* Check the device mode */
123 static vtpglut_code check_mode(
124 vtpglut *p,
125 vtpglut_mode m) { /* Requested mode */
126 return vtpglut_unsupported;
127 }
128
129 /* Set the device mode */
130 static vtpglut_code set_mode(
131 vtpglut *p,
132 vtpglut_mode m) { /* Requested mode */
133 return vtpglut_unsupported;
134 }
135
136 /* Get a status or set or get an option (default implementation) */
137 vtpglut_code vtpglut_get_set_opt_def(
138 vtpglut *p,
139 vtpglut_opt_type m, /* Requested option type */
140 va_list args) { /* Option parameters */
141 return vtpglut_unsupported;
142 }
143
144 /* Get a status or set or get an option */
145 static vtpglut_code get_set_opt(
146 vtpglut *p,
147 vtpglut_opt_type m, /* Requested status type */
148 ...) { /* Status parameters */
149 vtpglut_code rv;
150 va_list args;
151
152 va_start(args, m);
153 rv = vtpglut_get_set_opt_def(p, m, args); /* Call the above */
154 va_end(args);
155
156 return rv;
157 }
158
159 /* Supply a user interaction callback function. */
160 static void set_uicallback(
161 vtpglut *pp,
162 vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp),
163 void *cntx) {
164 pp->uicallback = uicallback;
165 pp->uic_cntx = cntx;
166 }
167
168 /* Supply an asynchronous event callback function. */
169 static void set_event_callback(
170 vtpglut *pp,
171 void (*eventcallback)(void *cntx, vtpglut_event_type event),
172 void *cntx) {
173 pp->eventcallback = eventcallback;
174 pp->event_cntx = cntx;
175 }
176
177 /* Generic inst error codes interpretation */
178 static char *vtpglut_interp_error(vtpglut *p, vtpglut_code ec) {
179 switch(ec & vtpglut_mask) {
180 case vtpglut_ok:
181 return "No error";
182 case vtpglut_notify:
183 return "Notification";
184 case vtpglut_warning:
185 return "Warning";
186 case vtpglut_no_coms:
187 return "Internal error - communications needed but not established";
188 case vtpglut_no_init:
189 return "Internal error - initialisation needed but not done";
190 case vtpglut_unsupported:
191 return "Unsupported function";
192 case vtpglut_internal_error:
193 return "Internal software error";
194 case vtpglut_coms_fail:
195 return "Communications failure";
196 case vtpglut_unknown_model:
197 return "Not expected device model";
198 case vtpglut_protocol_error:
199 return "Communication protocol breakdown";
200 case vtpglut_user_abort:
201 return "User hit Abort Key";
202 case vtpglut_user_trig:
203 return "User hit Trigger Key";
204 case vtpglut_unexpected_reply:
205 return "Unexpected Reply";
206 case vtpglut_wrong_setup:
207 return "Wrong or conflicting setup";
208 case vtpglut_hardware_fail:
209 return "Hardware Failure";
210 case vtpglut_system_error:
211 return "Operating System Error";
212 case vtpglut_bad_parameter:
213 return "Bad Parameter Value";
214 case vtpglut_other_error:
215 return "Non-specific error";
216 }
217 return "Unknown inst error code";
218 }
219
220 /* Instrument specific error codes interpretation */
221 static char *interp_error(vtpglut *p, int ec) {
222 return "interp_error is uninplemented in base class!";
223 }
224
225 /* Return the last serial communication error code */
226 /* (This is used for deciding fallback/retry strategies) */
227 static int last_scomerr(vtpglut *p) {
228 return p->icom->lserr;
229 }
230
231 /* ---------------------------------------------- */
232
233 /* Delete things set/done by new_vtpglut() */
234 static vtpglut_code virtual_del(vtpglut *p) {
235
236 #if defined(UNIX_APPLE)
237 osx_latencycritical_end();
238 #endif
239
240 return vtpglut_ok;
241 }
242
243
244 /* Virtual constructor. */
245 /* Return NULL for unknown instrument, */
246 /* or serial instrument if nocoms == 0. */
247 extern vtpglut *new_vtpglut(
248 icompath *path, /* Device path to instrument */
249 int nocoms, /* Don't open if communications are needed to establish inst type */
250 a1log *log, /* log to use */
251 vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp), /* optional uicallback for abort */
252 void *cntx /* Context for callback */
253 ) {
254 devType itype = devUnknown; /* Actual type */
255 icoms *icom;
256 vtpglut *p = NULL;
257
258 if (path == NULL) {
259 a1logd(log, 2, "new_vtpglut: got NULL path\n");
260 return NULL;
261 }
262
263 a1logd(log, 2, "new_vtpglut: called with path '%s' type '%s'\n",path->name,inst_sname(path->itype));
264
265 if ((icom = new_icoms(path, log)) == NULL) {
266 a1logd(log, 2, "new_vtpglut: new_icoms failed to open it\n");
267 return NULL;
268 }
269
270
271
272 /* Set device type from USB port, if not specified */
273 itype = icom->itype; /* Instrument type if its known from usb/hid */
274
275 #if defined(ENABLE_FAST_SERIAL)
276 if (itype == instUnknown && !nocoms && (icom->dctype & icomt_fastserial)) {
277 vtpglut_callback_cntx wcntx;
278
279 /* Wrap vtpglut callback in inst callback */
280 wcntx.cntx = cntx;
281 wcntx.uicallback = uicallback;
282
283 itype = fast_ser_dev_type(icom, 1, ser_uicallback, &wcntx); /* Else type from serial */
284 icom->dctype = (icom->dctype & ~icomt_cat_mask) | dev_category(itype);
285 a1logd(log, 8, "new_vtpglut: fast set '%s' dctype 0x%x\n",icom->name,icom->dctype);
286 }
287 #endif /* ENABLE_FAST_SERIAL */
288
289 #if defined(ENABLE_SERIAL)
290 if (itype == instUnknown && !nocoms) {
291 itype = ser_vtpglut_type(icom, uicallback, cntx); /* Else type from serial */
292 icom->dctype = (icom->dctype & ~icomt_cat_mask) | dev_category(itype);
293 a1logd(log, 8, "new_vtpglut: set '%s' dctype 0x%x\n",icom->name,icom->dctype);
294 }
295 #endif /* ENABLE_SERIAL */
296
297
298 #ifdef ENABLE_SERIAL
299 // if (itype == devRadiance)
300 // p = (vtpglut *)new_radiance(icom, itype);
301 #endif /* ENABLE_SERIAL */
302
303 #ifdef ENABLE_USB
304 // if (itype == instXXXX)
305 // p = (vtpglut *)new_XXXX(icom, itype);
306 #endif /* ENABLE_USB */
307
308
309 /* Nothing matched */
310 if (p == NULL) {
311 a1logd(log, 2, "new_vtpglut: device type not recognised\n");
312 icom->del(icom);
313 return NULL;
314 }
315
316 p->vdel = virtual_del;
317
318 /* Add default methods if constructor did not supply them */
319 if (p->init_coms == NULL)
320 p->init_coms = init_coms;
321 if (p->init_vtpglut == NULL)
322 p->init_vtpglut = init_vtpglut;
323 if (p->get_dtype == NULL)
324 p->get_dtype = get_dtype;
325 if (p->get_serial_no == NULL)
326 p->get_serial_no = get_serial_no;
327 if (p->capabilities == NULL)
328 p->capabilities = capabilities;
329 if (p->check_mode == NULL)
330 p->check_mode = check_mode;
331 if (p->set_mode == NULL)
332 p->set_mode = set_mode;
333 if (p->get_set_opt == NULL)
334 p->get_set_opt = get_set_opt;
335 if (p->set_uicallback == NULL)
336 p->set_uicallback = set_uicallback;
337 if (p->set_event_callback == NULL)
338 p->set_event_callback = set_event_callback;
339 if (p->vtpglut_interp_error == NULL)
340 p->vtpglut_interp_error = vtpglut_interp_error;
341 if (p->interp_error == NULL)
342 p->interp_error = interp_error;
343 if (p->last_scomerr == NULL)
344 p->last_scomerr = last_scomerr;
345
346 /* Set the provided user interaction callback */
347 p->set_uicallback(p, uicallback, cntx);
348
349 #if defined(UNIX_APPLE)
350 osx_latencycritical_start();
351 #endif
352
353 return p;
354 }
355
356 /* ============================================================= */
357 /* Detect serial device */
358
359 #ifdef ENABLE_SERIAL
360 static void hex2bin(char *buf, int len);
361
362 /* Heuristicly determine the device type for */
363 /* a serial connection, and devUnknown if not serial. */
364 /* Set it in icoms and also return it. */
365 static devType ser_vtpglut_type(
366 icoms *p,
367 vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp), /* optional uicallback */
368 void *cntx /* Context for callback */
369 ) {
370 devType rv = instUnknown;
371 #define BUFSZ (128 + 10)
372 char buf[BUFSZ];
373 baud_rate brt[] = { baud_9600, baud_57600, baud_115200, baud_230400, baud_nc };
374 unsigned int etime;
375 unsigned int bi, i;
376 int se, len, bread;
377 int lumagen = 0, lgchsum = 0;
378
379 #ifdef ENABLE_USB
380 if (p->usbd != NULL || p->hidd != NULL)
381 return p->itype;
382 #endif /* ENABLE_USB */
383
384 bi = 0;
385
386 /* The tick to give up on */
387 etime = msec_time() + (long)(20.0 * 1000.0 + 0.5);
388
389 a1logd(p->log, 1, "ser_vtpglut_type: Trying different baud rates (%u msec to go)\n",etime - msec_time());
390
391 /* Until we time out, find the correct baud rate */
392 for (i = bi; msec_time() < etime; i++) {
393 lgchsum = 0;
394
395 if (brt[i] == baud_nc)
396 i = 0;
397 if ((se = p->set_ser_port(p, fc_None, brt[i], parity_none,
398 stop_1, length_8)) != ICOM_OK) {
399 a1logd(p->log, 5, "ser_vtpglut_type: set_ser_port failed with 0x%x\n",se);
400 return instUnknown; /* Give up */
401 }
402
403 a1logd(p->log, 5, "Trying %s baud\n",baud_rate_to_str(brt[i]));
404 bread = 0;
405
406 /* If Lumagen baud rate */
407 if (brt[i] == baud_9600
408 || brt[i] == baud_57600
409 || brt[i] == baud_115200
410 || brt[i] == baud_230400) {
411
412 /* Try a spectrolino/spectroscan command first, so as not to upset it, */
413 /* but do first character only and see if there is an echo */
414 p->write_read_ex(p, ";", 1, buf, BUFSZ-1, &bread, "\r", 1, 0.2, 1);
415 if (bread == 1 && buf[0] == ';')
416 goto check_lumagen; /* It may be a Lumagen, so skip it. */
417
418 /* Send the rest of the spectrolino command */
419 p->write_read_ex(p, "D024\r\n", 0, buf, BUFSZ-1, &bread, "\r", 1, 0.5, 1);
420
421
422 if (bread == 0) {
423 char *bp;
424 a1logd(p->log, 5, "ser_vtpglut_type: Spectrolino command returned nothing\n");
425
426 /* It could be a Lumagen Radiance with echo off, */
427 /* so poke it and see if it responds. */
428 /* (Unfortunately the Lumagen delimeters modes aren't */
429 /* backwards compatible, so we may have to poke it twice...) */
430
431 /* Send "X" first, to get it out of menu mode ? */
432 p->write_read_ex(p, "X", 1, buf, BUFSZ, NULL, "\n", 1, 0.1, 1); // Menu off
433
434 a1logd(p->log, 5, "ser_vtpglut_type: Checking for Lumagen Radiance\n");
435 p->write_read_ex(p, "#ZQS00\r", 0, buf, BUFSZ, &bread, "\n", 1, 0.5, 1);
436 if (bread == 0) {
437 p->write_read_ex(p, "#0ZQS008E\r", 0, buf, BUFSZ, &bread, "\n", 1, 0.5, 1);
438 lgchsum = 1;
439 }
440
441 if (bread > 0
442 && (bp = strrchr(buf, '!')) != NULL
443 && strlen(bp) >= 4
444 && strncmp(bp,"!S00",4) == 0) {
445 goto check_lumagen;
446 }
447
448 /* Nope - look for something at a different baud rate */
449 goto continue_looking;
450 }
451 buf[bread] = '\000';
452 len = strlen(buf);
453
454 /* Is this a Lumagen Radiance with echo on, it responds with ";D024..." */
455 if ((len >= 4 && strncmp(buf, ";D024", 4) == 0)
456 || (len >= 4 && strncmp(buf, "!N\n\r", 4) == 0)) {
457 char *bp;
458
459 check_lumagen:;
460
461 /* Get the Lumagen device information */
462 p->write_read_ex(p, lgchsum ? "#0ZQS018F\r" : "#ZQS01\r",
463 0, buf, BUFSZ, &bread, "\n", 1, 0.5, 1);
464
465 /* Might have echo with checksum, so lgchsum not set correctly */
466 if (!lgchsum && bread > 0 && strstr(buf, "!N") != NULL) {
467 p->write_read_ex(p, "#0ZQS018F\r", 0, buf, BUFSZ, &bread, "\n", 1, 0.5, 1);
468 if (bread >= 11 && strncmp(buf, "#0ZQS018F!Y", 11) == 0)
469 lgchsum = 1;
470 }
471
472 /* returns something like "ZQS01!S01,Radiance2020,030115,1016,001309\r\m" */
473 if ((bp = strrchr(buf, '!')) != NULL && strlen(bp) >= 13) {
474 if (strncmp(bp,"!S01,Radiance",13) == 0) {
475 a1logd(p->log, 5, "ser_vtpglut_type: Found Lumagen Radiance\n");
476 lumagen = 1;
477 break;
478 }
479 }
480 a1logd(p->log, 5, "ser_vtpglut_type: Not Lumagen Radiance\n");
481 goto continue_looking;
482 }
483 } /* Possibly Lumagen */
484
485 continue_looking:;
486
487 /* Check for user abort */
488 if (uicallback != NULL) {
489 vtpglut_code ev;
490 if ((ev = uicallback(cntx, vtpglut_negcoms)) == vtpglut_user_abort) {
491 a1logd(p->log, 5, "ser_vtpglut_type: User aborted\n");
492 return instUnknown;
493 }
494 }
495 } /* next baud */
496
497 if (rv == instUnknown
498 && lumagen == 0
499 && msec_time() >= etime) { /* We haven't established comms */
500 a1logd(p->log, 5, "ser_vtpglut_type: Failed to establish coms\n");
501 return instUnknown;
502 }
503
504 a1logd(p->log, 5, "ser_vtpglut_type: Got coms with device\n");
505
506 if (lumagen) {
507 char *bp;
508
509 /* Get the Lumagen device information */
510 if ((se = p->write_read_ex(p, "ZQS01", 0, buf, BUFSZ, NULL, "\n", 1, 2.5, 1)) != 0)
511 return instUnknown;
512
513 /* returns something like "ZQS01!S01,Radiance2020,030115,1016,001309\r\m" */
514 if ((bp = strstr(buf, "!")) != NULL && strlen(bp) >= 13) {
515 if (strncmp(bp,"!S01,Radiance",13) == 0) {
516 rv = devRadiance;
517 }
518 }
519 }
520
521 a1logd(p->log, 5, "ser_vtpglut_type: Device type is '%s'\n", inst_name(rv));
522
523 p->close_port(p); /* Or should we leave it open ?? */
524
525 p->itype = rv;
526
527 return rv;
528 }
529 #undef BUFSZ
530
531 #endif /* ENABLE_SERIAL */
532
533 #if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
534
535 /* Convert an ASCII Hex character to an integer. */
536 static int h2b(char c) {
537 if (c >= '0' && c <= '9')
538 return (c-(int)'0');
539 if (c >= 'A' && c <= 'F')
540 return (10 + c-(int)'A');
541 if (c >= 'a' && c <= 'f')
542 return (10 + c-(int)'a');
543 return 0;
544 }
545
546 /* Convert a Hex encoded buffer into binary. */
547 /* len is number of bytes out */
548 static void hex2bin(char *buf, int len) {
549 int i;
550
551 for (i = 0; i < len; i++) {
552 buf[i] = (char)((h2b(buf[2 * i + 0]) << 4)
553 | (h2b(buf[2 * i + 1]) << 0));
554 }
555 }
556
557 #endif /* ENABLE_SERIAL */
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580