summaryrefslogtreecommitdiff
path: root/spectro/icoms.c
blob: eadecc9f7233d28dbd0ce92bf9a3b113a7b2a998 (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920

/* General instrument + serial I/O support */

/*
 * Argyll Color Correction System
 *
 * Author: Graeme W. Gill
 * Date:   28/9/97
 *
 * Copyright 1997 - 2013 Graeme W. Gill
 * All rights reserved.
 *
 * This material is licenced under the GNU GENERAL PUBLIC LICENSE Version 2 or later :-
 * see the License2.txt file for licencing details.
 */

/* These routines supliement the class code in ntio.c and unixio.c */
/* with common and USB specific routines */
/* Rename to icoms.c ?? */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <signal.h>
#if defined(UNIX)
#include <termios.h>
#include <errno.h>
#include <unistd.h>
#endif
#ifndef SALONEINSTLIB
#include "copyright.h"
#include "aconfig.h"
#else
#include "sa_config.h"
#endif
#include "numsup.h"
#include "xspect.h"
#include "insttypes.h"
#include "conv.h"
#include "icoms.h"


/* ----------------------------------------------------- */

/* Fake display & instrument device */
icompath icomFakeDevice = { instFakeDisp, "Fake Display Device" };



/* Free an icompath */
static
void icompath_del_contents(icompath *p) {

	if (p->name != NULL)
		free(p->name);
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	if (p->spath != NULL)
		free(p->spath);
#endif /* ENABLE_SERIAL */
#ifdef ENABLE_USB
	usb_del_usb_idevice(p->usbd);
	hid_del_hid_idevice(p->hidd);
#endif /* ENABLE_USB */
}

/* Free an icompath */
static void icompath_del(icompath *p) {

	icompath_del_contents(p);
	free(p);
}

/* Delete the last combined path */
/* (Assume this is before icompaths_make_dslists() has been called) */ 
static void icompaths_del_last_path(
	icompaths *p
) {
	icom_dtix ix = dtix_combined;
	
	if (p->ndpaths[ix] == 0)
		return;

	icompath_del(p->dpaths[ix][p->ndpaths[ix]-1]);
	p->dpaths[ix][p->ndpaths[ix]-1] = NULL;
	p->ndpaths[ix]--;
}

/* Return the last combined path */
/* (Assume this is before icompaths_make_dslists() has been called) */ 
static icompath *icompaths_get_last_path(
	icompaths *p
) {
	icom_dtix ix = dtix_combined;

	if (p->ndpaths[ix] == 0)
		return NULL;

	return p->dpaths[ix][p->ndpaths[ix]-1];
}

/* Return the instrument path corresponding to the port number, or NULL if out of range */
static icompath *icompaths_get_path(
	icompaths *p, 
	int port		/* Enumerated port number, 1..n */
) {
	if (port == FAKE_DEVICE_PORT)
		return &icomFakeDevice;



	if (port <= 0 || port > p->ndpaths[dtix_inst])
		return NULL;

	return p->dpaths[dtix_inst][port-1];
}

/* Return the device path corresponding to the port number, or NULL if out of range */
static icompath *icompaths_get_path_sel(
	icompaths *p, 
	icom_type dctype,	/* Device type list */
	int port			/* Enumerated port number, 1..n */
) {
	/* Check it is an enumeration */
	if (dctype != dtix_combined
	 && dctype != dtix_inst
	 && dctype != dtix_3dlut
	 && dctype != dtix_vtpg
	 && dctype != dtix_printer)
		return NULL;

	if (dctype == dtix_inst)
		return icompaths_get_path(p, port);
	
	if (port <= 0 || port > p->ndpaths[dctype])
		return NULL;

	return p->dpaths[dctype][port-1];
}

/* Clear a single device paths list */
static void icompaths_clear(icompaths *p, icom_dtix ix) {

	if (p->dpaths[ix] != NULL) {
		int i;
		/* If underlying owner of icompaths */
		if (ix == dtix_combined) {
			for (i = 0; i < p->ndpaths[ix]; i++) {
				icompath_del(p->dpaths[ix][i]);
			}
		}
		free(p->dpaths[ix]);
		p->dpaths[ix] = NULL;
		p->ndpaths[ix] = 0;

		/* Clear instrument alias */
		if (ix == dtix_inst) {
			p->npaths = 0;
			p->paths = NULL;
		}
	}
}

/* Clear all device paths */
static void icompaths_clear_all(icompaths *p) {

	if (p == NULL)
		return;

	/* Free any old instrument list */
	icompaths_clear(p, dtix_inst);
	icompaths_clear(p, dtix_3dlut);
	icompaths_clear(p, dtix_vtpg);
	icompaths_clear(p, dtix_printer);
	icompaths_clear(p, dtix_combined);
}

/* Add the give path to the given list. */
/* If the path is NULL, allocat an empty */
/* one and add it to the combined list */
/* Return icom error */
static int icompaths_add_path(icompaths *p, int ix, icompath *xp) {
	if (xp == NULL)
		ix = dtix_combined;
	if (p->dpaths[ix] == NULL) {
		if ((p->dpaths[ix] = (icompath **)calloc(sizeof(icompath *), 1 + 1)) == NULL) {
			a1loge(p->log, ICOM_SYS, "icompaths: calloc failed!\n");
			return ICOM_SYS;
		}
	} else {
		icompath **npaths;
		if ((npaths = (icompath **)realloc(p->dpaths[ix],
		                     sizeof(icompath *) * (p->ndpaths[ix] + 2))) == NULL) {
			a1loge(p->log, ICOM_SYS, "icompaths: realloc failed!\n");
			return ICOM_SYS;
		}
		p->dpaths[ix] = npaths;
		p->dpaths[ix][p->ndpaths[ix]+1] = NULL;
	}
	if (xp == NULL) {
		if ((xp = calloc(sizeof(icompath), 1)) == NULL) {
			a1loge(p->log, ICOM_SYS, "icompaths: malloc failed!\n");
			return ICOM_SYS;
		}
	} 
	p->dpaths[ix][p->ndpaths[ix]] = xp;
	p->ndpaths[ix]++;
	p->dpaths[ix][p->ndpaths[ix]] = NULL;
	return ICOM_OK;
}

/* Based on each icompath's dctype, create aliase lists for everything */
/* on the combined path based on each device type. */
/* (We are only called after clearing all lists) */ 
int icompaths_make_dslists(icompaths *p) {
	int rv, i;

	for (i = 0; i < p->ndpaths[dtix_combined]; i++) {
		icompath *xp = p->dpaths[dtix_combined][i];

		if (xp == NULL)
			break;

		a1logd(g_log, 8, "icompaths_make_dslists '%s' dctype 0x%x\n",xp->name,xp->dctype);

		if (xp->dctype & icomt_instrument) {
			if ((rv = icompaths_add_path(p, dtix_inst, xp)) != ICOM_OK)
				return rv;
		}
		if (xp->dctype & icomt_3dlut) {
			if ((rv = icompaths_add_path(p, dtix_3dlut, xp)) != ICOM_OK)
				return rv;
		}
		if (xp->dctype & icomt_vtpg) {
			if ((rv = icompaths_add_path(p, dtix_vtpg, xp)) != ICOM_OK)
				return rv;
		}
		if (xp->dctype & icomt_printer) {
			if ((rv = icompaths_add_path(p, dtix_printer, xp)) != ICOM_OK)
				return rv;
		}
	}

	/* Maintain backwards compatible instrument list alias */
	p->npaths = p->ndpaths[dtix_inst];
	p->paths = p->dpaths[dtix_inst];
	
	return ICOM_OK;
}

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)

/* Add a serial path. */
/* return icom error */
static int icompaths_add_serial(icompaths *p, char *name, char *spath, icom_type dctype)  {
	icom_dtix ix = dtix_combined;
	icompath *xp;
	int rv;

	if ((rv = icompaths_add_path(p, ix, NULL)) != ICOM_OK)
		return rv;

	xp = p->dpaths[ix][p->ndpaths[ix]-1];

	a1logd(g_log, 8, "icompaths_add_serial got '%s' dctype 0x%x\n",name,dctype);

	/* Type of port, port attributes, device category */
	xp->dctype |= icomt_cat_any;		/* Assume any for now */
	xp->dctype |= icomt_serial;
	xp->dctype |= icomt_seriallike;
	xp->dctype |= dctype;

	if ((xp->name = strdup(name)) == NULL) {
		a1loge(p->log, ICOM_SYS, "icompaths: strdup failed!\n");
		return ICOM_SYS;
	}
	if ((xp->spath = strdup(spath)) == NULL) {
		a1loge(p->log, ICOM_SYS, "icompaths: strdup failed!\n");
		return ICOM_SYS;
	}

	a1logd(g_log, 8, "icompaths_add_serial returning '%s' dctype 0x%x\n",xp->name,xp->dctype);

	return ICOM_OK;
}

/* Modify a serial path to add the device type */
int icompaths_set_serial_itype(icompath *p, devType itype) {
	char pname[400], *cp;

	/* Convert device type to category */
	p->dctype = (p->dctype & ~icomt_cat_mask) | inst_category(itype);

	p->itype = itype;

	/* Strip any existing description in brackets */
	if ((cp = strchr(p->name, '(')) != NULL && cp > p->name)
		cp[-1] = '\000';
	sprintf(pname,"%s (%s)", p->name, inst_name(itype));
	cp = p->name;
	if ((p->name = strdup(pname)) == NULL) {
		p->name = cp;
		a1loge(g_log, ICOM_SYS, "icompaths_set_serial_itype: strdup path failed!\n");
		return ICOM_SYS;
	}
	free(cp);

	a1logd(g_log, 8, "icompaths_set_serial_itype '%s' returning dctype 0x%x\n",p->name,p->dctype);

	return ICOM_OK;
}

#endif /* ENABLE_SERIAL */

#ifdef ENABLE_USB

/* Set an icompath details. */
/* return icom error */
static
int icompath_set_usb(a1log *log, icompath *p, char *name, unsigned int vid, unsigned int pid,
                              int nep, struct usb_idevice *usbd, devType itype) {
	int rv;


	if ((p->name = strdup(name)) == NULL) {
		a1loge(log, ICOM_SYS, "icompath: strdup failed!\n");
		return ICOM_SYS;
	}

	a1logd(g_log, 8, "icompath_set_usb '%s' got dctype 0x%x\n",p->name,p->dctype);

	p->dctype |= icomt_usb;
	p->dctype = (p->dctype & ~icomt_cat_mask) | inst_category(itype);

	p->nep = nep;
	p->vid = vid;
	p->pid = pid;
	p->usbd = usbd;
	p->itype = itype;

	a1logd(g_log, 8, "icompath_set_usb '%s' returning dctype 0x%x\n",p->name,p->dctype);

	return ICOM_OK;
}

/* Add a usb path. usbd is taken, others are copied. */
/* return icom error */
static int icompaths_add_usb(icompaths *p, char *name, unsigned int vid, unsigned int pid,
                             int nep, struct usb_idevice *usbd, devType itype) {
	icom_dtix ix = dtix_combined;
	icompath *xp;
	int rv;

	if ((rv = icompaths_add_path(p, 0, NULL)) != ICOM_OK)
		return rv;

	xp = p->dpaths[ix][p->ndpaths[ix]-1];

	return icompath_set_usb(p->log, xp, name, vid, pid, nep, usbd, itype);
}

/* Add an hid path. hidd is taken, others are copied. */
/* return icom error */
static int icompaths_add_hid(icompaths *p, char *name, unsigned int vid, unsigned int pid,
                             int nep, struct hid_idevice *hidd, devType itype) {
	icom_dtix ix = dtix_combined;
	icompath *xp;
	int rv;

	if ((rv = icompaths_add_path(p, 0, NULL)) != ICOM_OK)
		return rv;
	
	xp = p->dpaths[ix][p->ndpaths[ix]-1];

	a1logd(g_log, 8, "icompaths_add_hid '%s' got dctype 0x%x\n",xp->name,xp->dctype);

	xp->dctype |= icomt_hid;
	xp->dctype = (xp->dctype & ~icomt_cat_mask) | inst_category(itype);
	
	if ((xp->name = strdup(name)) == NULL) {
		a1loge(p->log, ICOM_SYS, "icompaths: strdup failed!\n");
		return ICOM_SYS;
	}

	xp->nep = nep;
	xp->vid = vid;
	xp->pid = pid;
	xp->hidd = hidd;
	xp->itype = itype;

	a1logd(g_log, 8, "icompath_set_usb '%s' returning dctype 0x%x\n",xp->name,xp->dctype);

	return ICOM_OK;
}
#endif /* ENABLE_USB */

static void icompaths_del(icompaths *p) {
	if (p != NULL) {
		icompaths_clear_all(p);
	
		p->log = del_a1log(p->log);		/* Unreference it and set to NULL */
		free(p);
	}
}

/* Create and return a list of available serial ports or USB devices for this system. */
/* We look at the registry key "HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM" */
/* to determine serial ports, and use libusb to discover USB devices. */
/* return icom error */
int icompaths_refresh_paths_sel(icompaths *p, icom_type mask) {
	int rv, usbend = 0;
	int i, j;

	a1logd(p->log, 7, "icoms_refresh_paths: called with mask = %d\n",mask);

	/* Clear any existing device paths */
	p->clear(p);

#ifdef ENABLE_USB
	if (mask & icomt_hid) {
		a1logd(p->log, 6, "icoms_refresh_paths: looking for HID device\n");
		if ((rv = hid_get_paths(p)) != ICOM_OK)
			return rv;
	}
	if (mask & icomt_usb) {
		a1logd(p->log, 6, "icoms_refresh_paths: looking for USB device\n");
		if ((rv = usb_get_paths(p)) != ICOM_OK)
			return rv;
	}
	usbend = p->ndpaths[dtix_combined];
	a1logd(p->log, 6, "icoms_refresh_paths: now got %d paths\n",usbend);
#endif /* ENABLE_USB */

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)

	if (mask & (icomt_serial | icomt_fastserial | icomt_btserial)) {
		a1logd(p->log, 6, "icoms_refresh_paths: looking for serial ports\n");
		if ((rv = serial_get_paths(p, mask)) != ICOM_OK)
			return rv;

	}
#endif /* ENABLE_SERIAL || ENABLE_FAST_SERIAL */

	/* Sort the COM keys so people don't get confused... */
	/* Sort identified instruments ahead of unknown serial ports */
	a1logd(p->log, 6, "icoms_refresh_paths: we now have %d devices and are about to sort them\n",p->ndpaths[dtix_combined]);

	{
		icompath **pl = p->dpaths[dtix_combined];
		int np = p->ndpaths[dtix_combined];

		for (i = usbend; i < (np-1); i++) {
			for (j = i+1; j < np; j++) {
				if ((pl[i]->itype == instUnknown && pl[j]->itype != instUnknown)
				 || (((pl[i]->itype == instUnknown && pl[j]->itype == instUnknown)
				   || (pl[i]->itype != instUnknown && pl[j]->itype != instUnknown))
				  && strcmp(pl[i]->name, pl[j]->name) > 0)) {
					icompath *tt = pl[i];
					pl[i] = pl[j];
					pl[j] = tt;
				}
			}
		}
	}

	/* Create the device specific lists */
	if ((rv = icompaths_make_dslists(p)) != ICOM_OK) {
		a1logd(p->log, 1, "icoms_refresh_paths: icompaths_make_dslists failed with %d\n",rv);
		return rv;
	}

	a1logd(p->log, 8, "icoms_refresh_paths: returning %d paths and ICOM_OK\n",p->ndpaths[dtix_combined]);

	return ICOM_OK;
}

/* (In implementation specific) */
int serial_is_open(icoms *p);
void serial_close_port(icoms *p);

/* Close the port */
static void icoms_close_port(icoms *p) {
	if (p->is_open) {
#ifdef ENABLE_USB
		if (p->usbd) {
			usb_close_port(p);
		} else if (p->hidd) {
			hid_close_port(p);
		}
#endif
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
		if (serial_is_open(p)) {
			serial_close_port(p);
		}
#endif /* ENABLE_SERIAL */
		p->is_open = 0;
	}
}

int icompaths_refresh_paths(icompaths *p) {
	return icompaths_refresh_paths_sel(p, icomt_instrument | icomt_portattr_all);
}

/* Allocate an icom paths and set it to the list of available devices */
/* that match the icom_type mask. */ 
/* Return NULL on error */
icompaths *new_icompaths_sel(a1log *log, icom_type mask) {
	icompaths *p;
	if ((p = (icompaths *)calloc(sizeof(icompaths), 1)) == NULL) {
		a1loge(log, ICOM_SYS, "new_icompath: calloc failed!\n");
		return NULL;
	}

	p->log = new_a1log_d(log);

	p->clear         = icompaths_clear_all;
	p->refresh       = icompaths_refresh_paths;
	p->refresh_sel   = icompaths_refresh_paths_sel;
	p->get_path      = icompaths_get_path;
	p->get_path_sel  = icompaths_get_path_sel;
	p->del           = icompaths_del;

	/* ====== internal implementation ======= */
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	p->add_serial    = icompaths_add_serial;
#endif /* ENABLE_SERIAL */
#ifdef ENABLE_USB
	p->add_usb       = icompaths_add_usb;
	p->add_hid       = icompaths_add_hid;
#endif /* ENABLE_USB */
	p->del_last_path = icompaths_del_last_path;
	p->get_last_path = icompaths_get_last_path;
	/* ====================================== */

	if (icompaths_refresh_paths_sel(p, mask)) {
		a1loge(log, ICOM_SYS, "new_icompaths: icompaths_refresh_paths failed!\n");
		free(p);
		return NULL;
	}

	return p;
}

/* Allocate an icom paths and set it to the list of available instruments */
/* Return NULL on error */
icompaths *new_icompaths(a1log *log) {
	return new_icompaths_sel(log, icomt_instrument | icomt_portattr_all);
}


/* ----------------------------------------------------- */

/* Copy an icompath into an icom */
/* return icom error */
static int icom_copy_path_to_icom(icoms *p, icompath *pp) {
	int rv;

	if (p->name != NULL)
		free(p->name);
	if ((p->name = strdup(pp->name)) == NULL) {
		a1loge(p->log, ICOM_SYS, "copy_path_to_icom: malloc name failed\n");
		return ICOM_SYS;
	}
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	if (pp->spath != NULL) {
		if ((p->spath = strdup(pp->spath)) == NULL) {
			a1loge(p->log, ICOM_SYS, "copy_path_to_icom: malloc spath failed\n");
			return ICOM_SYS;
		}
	} else {
		p->spath = NULL;
	}
#endif /* ENABLE_SERIAL */
#ifdef ENABLE_USB
	p->nep = pp->nep;
	p->vid = pp->vid;
	p->pid = pp->pid;
	if ((rv = usb_copy_usb_idevice(p, pp)) != ICOM_OK)
		return rv;
	if ((rv = hid_copy_hid_idevice(p, pp)) != ICOM_OK)
		return rv;
#endif
	p->dctype = pp->dctype;
	p->itype = pp->itype;

	a1logd(g_log, 8, "icom_copy_path_to_icom '%s' returning dctype 0x%x\n",p->name,p->dctype);

	return ICOM_OK;
}

/* Return the device category */
/* (Returns bit flags) */
static icom_type icoms_cat_type(
icoms *p
) {
	return p->dctype & icomt_cat_mask;
}

/* Return the communication port type */
/* (Can use equality tests on return value) */
static icom_type icoms_port_type(
icoms *p
) {
	return p->dctype & icomt_port_mask;
}

/* Return the communication port attributes */
/* (Returns bit flags) */
static icom_type icoms_port_attr(
icoms *p
) {
	return p->dctype & icomt_attr_mask;
}

/* ----------------------------------------------------- */

/* Include the icoms implementation dependent function implementations */
#ifdef NT
# include "icoms_nt.c"
#endif
#if defined(UNIX)
# include "icoms_ux.c"
#endif

/* write and read convenience function with read flush */
/* return icom error */
static int
icoms_write_read_ex(
icoms *p,
char *wbuf,			/* Write puffer */
int nwch,			/* if > 0, number of characters to write, else nul terminated */
char *rbuf,			/* Read buffer */
int bsize,			/* Buffer size */
int *bread,			/* Bytes read (not including forced '\000') */
char *tc,			/* Terminating characers, NULL for none or char count mode */
int ntc,			/* Number of terminating characters needed, or char count needed */
double tout,		/* Timeout for write and then read (i.e. max = 2 x tout) */
int frbw			/* nz to Flush Read Before Write */
) {
	int rv = ICOM_OK;

	a1logd(p->log, 8, "icoms_write_read: called\n");

	if (p->write == NULL || p->read == NULL) {	/* Neither serial nor USB ? */
		a1loge(p->log, ICOM_NOTS, "icoms_write_read: No write and read functions set!\n");
		return ICOM_NOTS;
	}

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	/* Flush any stray chars if serial */
	if (frbw && (p->dctype & icomt_serial)) {
		char tbuf[500];
		int debug = p->log->debug;
		int bread;

		if (debug < 8)
			p->log->debug =  0;
		for (;;) {
			bread = 0;
			p->read(p, tbuf, 500, &bread, NULL, 500, 0.02);
			if (bread == 0)
				break;
		}
		p->log->debug = debug;
		rv = ICOM_OK;
	}
#endif

	/* Write the write data */
	rv = p->write(p, wbuf, nwch, tout);

	/* Return error if coms */
	if (rv != ICOM_OK) {
		a1logd(p->log, 8, "icoms_write_read: failed - returning 0x%x\n",rv);
		return rv;
	}

	/* Read response */
	rv = p->read(p, rbuf, bsize, bread, tc, ntc, tout);
	
	a1logd(p->log, 8, "icoms_write_read: returning 0x%x\n",rv);

	return rv;
}

/* write and read convenience function */
/* return icom error */
static int
icoms_write_read(
icoms *p,
char *wbuf,			/* Write puffer */
int nwch,			/* if > 0, number of characters to write, else nul terminated */
char *rbuf,			/* Read buffer */
int bsize,			/* Buffer size */
int *bread,			/* Bytes read (not including forced '\000') */
char *tc,			/* Terminating characers, NULL for none or char count mode */
int ntc,			/* Number of terminating characters needed, or char count needed */
double tout		/* Timeout for write and then read (i.e. max = 2 x tout) */
) {
	return icoms_write_read_ex(p, wbuf, nwch, rbuf, bsize, bread, tc, ntc, tout, 0);
}

/* Optional callback to client from device */ 
/* Default implementation is a NOOP */
static int icoms_interrupt(icoms *p,
	int icom_int		/* Interrupt cause */
) {
	return ICOM_OK;
}

/* Destroy ourselves */
static void
icoms_del(icoms *p) {
	a1logd(p->log, 8, "icoms_del: called\n");
	if (p->is_open) {
		a1logd(p->log, 8, "icoms_del: closing port\n");
		p->close_port(p);
	}
#ifdef ENABLE_USB
	usb_del_usb(p);
	hid_del_hid(p);
#endif
#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	if (p->spath != NULL)
		free(p->spath);
#endif
	p->log = del_a1log(p->log);
	if (p->name != NULL)
		free(p->name);
	p->log = del_a1log(p->log);		/* unref */
	free (p);
}

/* icoms Constructor */
/* Return NULL on error */
icoms *new_icoms(
	icompath *ipath,	/* instrument to open */
	a1log *log			/* log to take reference from, NULL for default */
) {
	icoms *p;

	a1logd(log, 2, "new_icoms '%s' itype '%s' dctype 0x%x\n",ipath->name,inst_sname(ipath->itype),ipath->dctype);

	if ((p = (icoms *)calloc(sizeof(icoms), 1)) == NULL) {
		a1loge(log, ICOM_SYS, "new_icoms: calloc failed!\n");
		return NULL;
	}

	if ((p->name = strdup(ipath->name)) == NULL) {
		a1loge(log, ICOM_SYS, "new_icoms: strdup failed!\n");
		return NULL;
	}
	p->itype = ipath->itype;

	/* Copy ipath info to this icoms */
	if (icom_copy_path_to_icom(p, ipath)) {
		free(p->name);
		free(p);
		return NULL;
	}

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
#ifdef NT
	p->phandle = NULL;
#endif
#ifdef UNIX
	p->fd = -1;
#endif
	p->fc = fc_nc;
	p->br = baud_nc;
	p->py = parity_nc;
	p->sb = stop_nc;
	p->wl = length_nc;
#endif	/* ENABLE_SERIAL */

	p->lserr = 0;
//	p->tc = -1;

	p->log = new_a1log_d(log);
	p->debug = p->log->debug;		/* Legacy code */
	
	p->close_port = icoms_close_port;

	p->dev_cat   = icoms_cat_type;
	p->port_type = icoms_port_type;
	p->port_attr = icoms_port_attr;

#if defined(ENABLE_SERIAL) || defined(ENABLE_FAST_SERIAL)
	p->set_ser_port_ex = icoms_set_ser_port_ex;
	p->set_ser_port = icoms_set_ser_port;
#endif	/* ENABLE_SERIAL */

	p->write = NULL;	/* Serial open or set_methods will set */
	p->read = NULL;
	p->write_read = icoms_write_read;
	p->write_read_ex = icoms_write_read_ex;
	p->interrupt = icoms_interrupt;

	p->del = icoms_del;

#ifdef ENABLE_USB
	usb_set_usb_methods(p);
	hid_set_hid_methods(p);
#endif /* ENABLE_USB */

	return p;
}

/* ---------------------------------------------------------------------------------*/
/* utilities */

/* Emit a "normal" beep */
void normal_beep() {
	/* 0msec delay, 1.0KHz for 200 msec */
	msec_beep(0, 1000, 200);
}

/* Emit a "good" beep */
void good_beep() {
	/* 0msec delay, 1.2KHz for 200 msec */
	msec_beep(0, 1200, 200);
}

/* Emit a "bad" double beep */
void bad_beep() {
	/* 0 msec delay, 800Hz for 200 msec */
	msec_beep(0, 800, 200);
	/* 350 msec delay, 800Hz for 200 msec */
	msec_beep(350, 800, 200);
}

char *baud_rate_to_str(baud_rate br) {
	switch (br) {
		case baud_nc:
			return "Not Configured";
		case baud_110:
			return "110";
		case baud_300:
			return "300";
		case baud_600:
			return "600";
		case baud_1200:
			return "1400";
		case baud_2400:
			return "2400";
		case baud_4800:
			return "4800";
		case baud_9600:
			return "9600";
		case baud_14400:
			return "14400";
		case baud_19200:
			return "19200";
		case baud_38400:
			return "38400";
		case baud_57600:
			return "57600";
		case baud_115200:
			return "115200";
		case baud_921600:
			return "921600";
	}
	return "Unknown";
}

/* Convert control chars to ^[A-Z] notation in a string */
/* Can be called 3 times without reusing the static buffer */
/* Returns a maximum of 5000 characters */
char *
icoms_fix(char *ss) {
	static unsigned char buf[3][10005];
	static int ix = 0;
	unsigned char *d;
	unsigned char *s = (unsigned char *)ss;
	if (++ix >= 3)
		ix = 0;
	if (ss == NULL) {
		strcpy((char *)buf[ix],"(null)");
		return (char *)buf[ix];
	}
	for(d = buf[ix]; (d - buf[ix]) < 10000;) {
		if (*s < ' ' && *s > '\000') {
			*d++ = '^';
			*d++ = *s++ + '@';
		} else if (*s >= 0x80) {
			*d++ = '\\';
			*d++ = '0' + ((*s >> 6) & 0x3);
			*d++ = '0' + ((*s >> 3) & 0x7);
			*d++ = '0' + ((*s++ >> 0) & 0x7);
		} else {
			*d++ = *s++;
		}
		if (s[-1] == '\000')
			break;
	}
	*d++ = '.';
	*d++ = '.';
	*d++ = '.';
	*d++ = '\000';
	return (char *)buf[ix];
}

/* Convert a limited binary buffer to a list of hex */
char *
icoms_tohex(unsigned char *s, int len) {
	static char buf[64 * 3 + 10];
	int i;
	char *d;

	buf[0] = '\000';
	for(i = 0, d = buf; i < 64 && i < len; i++, s++) {
		sprintf(d, "%s%02x", i > 0 ? " " : "", *s);
		d += strlen(d);
	}
	if (i < len)
		sprintf(d, " ...");

	return buf;
}