| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
| 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 |
|
| 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 |
| 80 |
#endif |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
static vtpglut_code init_coms( |
| 89 |
vtpglut *p) { |
| 90 |
return vtpglut_unsupported; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
static vtpglut_code init_vtpglut( |
| 96 |
vtpglut *p) { |
| 97 |
return vtpglut_unsupported; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
static devType get_dtype(vtpglut *p) { |
| 102 |
if (p != NULL) |
| 103 |
return p->dtype; |
| 104 |
return devUnknown; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static char *get_serial_no(vtpglut *p) { |
| 110 |
return ""; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 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 |
|
| 123 |
static vtpglut_code check_mode( |
| 124 |
vtpglut *p, |
| 125 |
vtpglut_mode m) { |
| 126 |
return vtpglut_unsupported; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
static vtpglut_code set_mode( |
| 131 |
vtpglut *p, |
| 132 |
vtpglut_mode m) { |
| 133 |
return vtpglut_unsupported; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
vtpglut_code vtpglut_get_set_opt_def( |
| 138 |
vtpglut *p, |
| 139 |
vtpglut_opt_type m, |
| 140 |
va_list args) { |
| 141 |
return vtpglut_unsupported; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
static vtpglut_code get_set_opt( |
| 146 |
vtpglut *p, |
| 147 |
vtpglut_opt_type m, |
| 148 |
...) { |
| 149 |
vtpglut_code rv; |
| 150 |
va_list args; |
| 151 |
|
| 152 |
va_start(args, m); |
| 153 |
rv = vtpglut_get_set_opt_def(p, m, args); |
| 154 |
va_end(args); |
| 155 |
|
| 156 |
return rv; |
| 157 |
} |
| 158 |
|
| 159 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 221 |
static char *interp_error(vtpglut *p, int ec) { |
| 222 |
return "interp_error is uninplemented in base class!"; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
static int last_scomerr(vtpglut *p) { |
| 228 |
return p->icom->lserr; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 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 |
|
| 245 |
|
| 246 |
|
| 247 |
extern vtpglut *new_vtpglut( |
| 248 |
icompath *path, |
| 249 |
int nocoms, |
| 250 |
a1log *log, |
| 251 |
vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp), |
| 252 |
void *cntx |
| 253 |
) { |
| 254 |
devType itype = devUnknown; |
| 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 |
|
| 273 |
itype = icom->itype; |
| 274 |
|
| 275 |
#if defined(ENABLE_FAST_SERIAL) |
| 276 |
if (itype == instUnknown && !nocoms && (icom->dctype & icomt_fastserial)) { |
| 277 |
vtpglut_callback_cntx wcntx; |
| 278 |
|
| 279 |
|
| 280 |
wcntx.cntx = cntx; |
| 281 |
wcntx.uicallback = uicallback; |
| 282 |
|
| 283 |
itype = fast_ser_dev_type(icom, 1, ser_uicallback, &wcntx); |
| 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 |
| 288 |
|
| 289 |
#if defined(ENABLE_SERIAL) |
| 290 |
if (itype == instUnknown && !nocoms) { |
| 291 |
itype = ser_vtpglut_type(icom, uicallback, cntx); |
| 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 |
| 296 |
|
| 297 |
|
| 298 |
#ifdef ENABLE_SERIAL |
| 299 |
|
| 300 |
|
| 301 |
#endif |
| 302 |
|
| 303 |
#ifdef ENABLE_USB |
| 304 |
|
| 305 |
|
| 306 |
#endif |
| 307 |
|
| 308 |
|
| 309 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 358 |
|
| 359 |
#ifdef ENABLE_SERIAL |
| 360 |
static void hex2bin(char *buf, int len); |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
static devType ser_vtpglut_type( |
| 366 |
icoms *p, |
| 367 |
vtpglut_code (*uicallback)(void *cntx, vtpglut_ui_purp purp), |
| 368 |
void *cntx |
| 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 |
| 383 |
|
| 384 |
bi = 0; |
| 385 |
|
| 386 |
|
| 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 |
|
| 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; |
| 401 |
} |
| 402 |
|
| 403 |
a1logd(p->log, 5, "Trying %s baud\n",baud_rate_to_str(brt[i])); |
| 404 |
bread = 0; |
| 405 |
|
| 406 |
|
| 407 |
if (brt[i] == baud_9600 |
| 408 |
|| brt[i] == baud_57600 |
| 409 |
|| brt[i] == baud_115200 |
| 410 |
|| brt[i] == baud_230400) { |
| 411 |
|
| 412 |
|
| 413 |
|
| 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; |
| 417 |
|
| 418 |
|
| 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 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
p->write_read_ex(p, "X", 1, buf, BUFSZ, NULL, "\n", 1, 0.1, 1); |
| 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 |
|
| 449 |
goto continue_looking; |
| 450 |
} |
| 451 |
buf[bread] = '\000'; |
| 452 |
len = strlen(buf); |
| 453 |
|
| 454 |
|
| 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 |
|
| 462 |
p->write_read_ex(p, lgchsum ? "#0ZQS018F\r" : "#ZQS01\r", |
| 463 |
0, buf, BUFSZ, &bread, "\n", 1, 0.5, 1); |
| 464 |
|
| 465 |
|
| 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 |
|
| 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 |
} |
| 484 |
|
| 485 |
continue_looking:; |
| 486 |
|
| 487 |
|
| 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 |
} |
| 496 |
|
| 497 |
if (rv == instUnknown |
| 498 |
&& lumagen == 0 |
| 499 |
&& msec_time() >= etime) { |
| 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 |
|
| 510 |
if ((se = p->write_read_ex(p, "ZQS01", 0, buf, BUFSZ, NULL, "\n", 1, 2.5, 1)) != 0) |
| 511 |
return instUnknown; |
| 512 |
|
| 513 |
|
| 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); |
| 524 |
|
| 525 |
p->itype = rv; |
| 526 |
|
| 527 |
return rv; |
| 528 |
} |
| 529 |
#undef BUFSZ |
| 530 |
|
| 531 |
#endif |
| 532 |
|
| 533 |
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL) |
| 534 |
|
| 535 |
|
| 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 |
|
| 547 |
|
| 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 |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|