summaryrefslogtreecommitdiffstats
path: root/servers/gpib_server_lin/src/gpib_conn.cpp
blob: 3fce6e2634a7a3f1d1bed142b6ff9be35f0e6103 (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
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
/*
 * Remote Laboratory GPIB Server
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * (c) 2012 Timothy Pearson
 * Raptor Engineering
 * http://www.raptorengineeringinc.com
 */

#include <stdio.h>                /* perror() */
#include <stdlib.h>               /* atoi() */
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>               /* read() */
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/signal.h>
#include <sys/types.h>

#include <tqtimer.h>
#include <tqfile.h>

#include <tdelocale.h>

#include <gpib/ib.h>

#include "gpib_functions.h"
#include "scope_functions.h"
#include "signal_functions.h"
#include "commanalyzer_functions.h"

#include "gpib_conn.h"

#define ABORT_SOCKET(s) 		s->close();						\
					s->disconnect();					\
					delete s;						\
					s = NULL;

#define NETWORK_COMM_TIMEOUT_MS 5000

/* exception handling */
struct exit_exception {
	int c;
	exit_exception(int c):c(c) { }
};

/*
  The GPIBSocket class provides a socket that is connected with a client.
  For every client that connects to the server, the server creates a new
  instance of this class.
*/
GPIBSocket::GPIBSocket(int sock, TQObject *parent, const char *name) :
	TDEKerberosServerSocket(parent, name), m_criticalSection(0), m_loopTimer(NULL), m_config(static_cast<GPIBServer*>(parent)->m_config),
	m_serverParent(static_cast<GPIBServer*>(parent)), m_commandLoopState(0)
{

	// Initialize timers
	m_kerberosInitTimer = new TQTimer();
	connect(m_kerberosInitTimer, SIGNAL(timeout()), this, SLOT(finishKerberosHandshake()));
	m_servClientTimeout = new TQTimer();

	setServiceName("remotefpga");

	line = 0;
	connect(this, SIGNAL(connectionClosed()), SLOT(connectionClosedHandler()));
	connect(this, SIGNAL(connectionClosed()), parent, SLOT(remoteConnectionClosed()));
	setSocket(sock);
}

GPIBSocket::~GPIBSocket() {
	if (m_servClientTimeout) {
		m_servClientTimeout->stop();
		delete m_servClientTimeout;
		m_servClientTimeout = NULL;
	}
	if (m_kerberosInitTimer) {
		m_kerberosInitTimer->stop();
		delete m_kerberosInitTimer;
		m_kerberosInitTimer = NULL;
	}
	if (m_loopTimer) {
		m_loopTimer->stop();
		delete m_loopTimer;
		m_loopTimer = NULL;
	}
}

void GPIBSocket::close() {
	if (state() == TQSocket::Connected) {
		TDEKerberosServerSocket::close();
		connectionClosedHandler();
		TQTimer::singleShot(0, parent(), SLOT(remoteConnectionClosed()));
	}
}

void GPIBSocket::connectionClosedHandler() {
	printf("[DEBUG] Connection from %s closed\n\r", m_remoteHost.ascii());

	if (m_criticalSection > 0) {
		throw exit_exception(-1);
	}
}

void GPIBSocket::initiateKerberosHandshake() {
	setUsingKerberos(true);
	m_kerberosInitTimer->start(100, TRUE);
}

void GPIBSocket::finishKerberosHandshake() {
	if (kerberosStatus() == TDEKerberosServerSocket::KerberosInitializing) {
		m_kerberosInitTimer->start(100, TRUE);
		return;
	}
	if (kerberosStatus() == TDEKerberosServerSocket::KerberosInUse) {
		m_config->setGroup("Security");
		TQString masterUser = m_config->readEntry("masteruser");
		TQString masterRealm = m_config->readEntry("masterrealm");
		if (masterRealm == "") {
			masterRealm = "(NULL)";
		}
		if ((m_authenticatedUserName != masterUser) || (m_authenticatedRealmName != masterRealm)) {
			printf("[DEBUG] Connection from %s closed due to authentication failure (attempted connection as user %s@%s)\n\r", m_remoteHost.ascii(), m_authenticatedUserName.ascii(), m_authenticatedRealmName.ascii());
			close();
			return;
		}

		setDataTimeout(NETWORK_COMM_TIMEOUT_MS);

		TQDataStream ds(this);
		ds.setPrintableData(true);
		ds << TQString("OK");
		writeEndOfFrame();

		enterCommandLoop();
		return;
	}
	else {
		printf("[DEBUG] Connection from %s closed due to Kerberos failure\n\r", m_remoteHost.ascii()); fflush(stdout);
		close();
		return;
	}
}

void GPIBSocket::commandLoop() {
	bool transferred_data;

	m_criticalSection++;
	try {
		transferred_data = false;
		if (state() == TQSocket::Connected) {
			if (m_commandLoopState == 0) {
				if (canReadLine()) {
					processPendingData();
				}
				if (canReadFrame()) {
					TQDataStream ds(this);
					ds.setPrintableData(true);
					TQString instrumentRequest;
					ds >> instrumentRequest;
					clearFrameTail();

					m_activeDeviceType = 0;
					if (instrumentRequest == "LIST") {
						TQStringList deviceList;
						if (m_serverParent->m_serialDevice != "") {
							deviceList.append("SERIAL PORT");
						}
						if (m_serverParent->m_scopeType != "") {
							deviceList.append("OSCILLOSCOPE");
						}
						if (m_serverParent->m_funcgenType != "") {
							deviceList.append("FUNCTION GENERATOR");
						}
						if (m_serverParent->m_commanalyzerType != "") {
							deviceList.append("COMMUNICATIONS ANALYZER");
						}
						ds << deviceList;
						writeEndOfFrame();
					}
					else if (instrumentRequest == "SERIAL PORT") {
						m_activeDeviceType = 1;
					}
					else if (instrumentRequest == "OSCILLOSCOPE") {
						m_activeDeviceType = 2;
					}
					else if (instrumentRequest == "FUNCTION GENERATOR") {
						m_activeDeviceType = 3;
					}
					else if (instrumentRequest == "COMMUNICATIONS ANALYZER") {
						m_activeDeviceType = 4;
					}

					if (m_activeDeviceType != 0) {
						ds << TQString("ACK");
						writeEndOfFrame();

						m_servClientTimeout->start(NETWORK_COMM_TIMEOUT_MS, TRUE);
						transferred_data = true;
						m_commandLoopState = 1;
					}
					else {
						ds << TQString("NCK");
						writeEndOfFrame();
					}
				}
			}
			else if (m_commandLoopState == 1) {
				if (canReadLine()) {
					processPendingData();
				}
				if (m_activeDeviceType == 1) {
					// Serial port
					if (canReadFrame()) {
						TQDataStream ds(this);
						ds.setPrintableData(true);
						TQByteArray recData;
						ds >> recData;
						clearFrameTail();
						if (recData.size() > 0) {
							if (write(m_serverParent->m_serialDeviceSocket, recData.data(), recData.size()) < 0) {
								// ERROR
							}
							transferred_data = true;
							printf("[DEBUG] Got %d bytes from the network interface\n\r", recData.size()); fflush(stdout);
						}
					}
					TQByteArray txData;
					txData.resize(10000);
					int cc = read(m_serverParent->m_serialDeviceSocket, txData.data(), txData.size());
					if (cc > 0) {
						TQDataStream ds(this);
						ds.setPrintableData(true);
						ds << txData;
						writeEndOfFrame();
						transferred_data = true;
						printf("[DEBUG] Got %d bytes from the serial port\n\r", cc); fflush(stdout);
					}
				}
				else {
					if (canReadFrame()) {
						TQDataStream ds(this);
						ds.setPrintableData(true);
						ds >> m_instrumentCommand;

						if (m_instrumentCommand != "") {
							if (m_activeDeviceType == 2) {
								// Oscilloscope
								if (m_instrumentCommand == "RESET") {				// Want to reset scope
									if (scope_reset(m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");;
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETHORIZTIMEBASE") {		// Want to change horizontal timebase
									double value;
									ds >> value;
									if (scope_set_timebase(value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if ((m_instrumentCommand == "GETCHANNELTRACE")) {		// Want channel trace
									TQ_INT32 value;
									ds >> value;
									TQ_INT32 traceLength;
									traceLength = scope_get_channel_trace(value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket);
									if (traceLength > 0) {
										long i;
										TQDoubleArray traceData;
										TQDoubleArray positionData;
										traceData.resize(traceLength);
										positionData.resize(traceLength);
										for (i=0; i<traceLength; i++) {
											traceData[i] = scope_raw_trace_data[i];
											positionData[i] = scope_raw_position_data[i];
										}
										ds << TQString("ACK");
										ds << traceData;
										ds << positionData;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETVOLTSDIV") {		// Want to change volts per division
									TQ_INT32 value1;
									ds >> value1;
									double value2;
									ds >> value2;
									if (scope_set_volts_div(value1, value2, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETVOLTSDIV") {		// Want to get volts per division
									double voltsdiv;
									TQ_INT32 value;
									ds >> value;
									if (scope_get_channel_volts_div(&voltsdiv, value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << voltsdiv;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETSECONDSSDIV") {		// Want to get seconds per division
									double secondsdiv;
									TQ_INT32 value;
									ds >> value;
									if (scope_get_channel_seconds_div(&secondsdiv, value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << secondsdiv;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETTRACESAMPLECOUNT") {	// Want to get number of samples in the trace
									unsigned long samples;
									TQ_INT32 value;
									ds >> value;
									if (scope_get_channel_sample_count(&samples, value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										TQ_INT32 safeSamples = samples;
										ds << safeSamples;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETRUNNING") {			// Want to change run status
									TQ_INT16 value;
									ds >> value;
									if (scope_set_acquisition(value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETRUNNING") {		// Want to get run status
									int running;
									if (scope_get_acquisition(&running, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										TQ_INT16 safeRunning;
										safeRunning = running;
										ds << TQString("ACK");
										ds << safeRunning;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETCHANNELACTIVE") {		// Want to change channel enable
									TQ_INT32 value1;
									ds >> value1;
									TQ_INT16 value2;
									ds >> value2;
									if (scope_set_channel_state(value1, value2, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETTRIGGERCHANNEL") {		// Want to change trigger channel
									TQ_INT16 value;
									ds >> value;
									if (scope_set_trigger_channel(value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETTRIGGERCHANNEL") {		// Want to get trigger channel
									int channel;
									if (scope_get_trigger_channel(&channel, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										TQ_INT16 safeChannel = channel;
										ds << TQString("ACK");
										ds << safeChannel;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETTRIGGERLEVEL") {		// Want to change trigger level
									double value;
									ds >> value;
									if (scope_set_trigger_level(value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETTRIGGERLEVEL") {		// Want to get trigger level
									double triggerlevel;
									if (scope_get_trigger_level(&triggerlevel, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << triggerlevel;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETCHANVERTPOS") {		// Want to change channel vertical position
									TQ_INT32 value1;
									ds >> value1;
									float value2;
									ds >> value2;
									if (scope_set_channel_position(value1, value2, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETHORIZONTALDIVCOUNT") {	// Want the number of horizontal divisions available
									TQ_INT16 divisions = scope_get_number_of_vertical_divisions(m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket);
									if (divisions >= 0) {
										ds << TQString("ACK");
										ds << divisions;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETVERTICALDIVCOUNT") {	// Want the number of vertical divisions available
									TQ_INT16 divisions = scope_get_number_of_horizontal_divisions(m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket);
									if (divisions >= 0) {
										ds << TQString("ACK");
										ds << divisions;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETNUMBEROFCHANNELS") {	// Want the number of channels available
									TQ_INT16 divisions = scope_get_number_of_channels(m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket);
									if (divisions >= 0) {
										ds << TQString("ACK");
										ds << divisions;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETCHANNELACTIVE") {		// Want to get channel activity
									int state;
									TQ_INT32 value;
									ds >> value;
									if (scope_get_channel_state(&state, value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										TQ_INT16 safeState = state;
										ds << TQString("ACK");
										ds << safeState;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETPERMITTEDVDIVS") {		// Want to get permitted volts/div settings
									double attenuation_mult;
									double* permitted_array;
									int permitted_count;
									TQ_INT32 value;
									ds >> value;
									if (scope_get_probe_attenuation_multiplier(&attenuation_mult, value, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
										if (scope_get_permitted_volts_div_settings_at_1x(&permitted_count, &permitted_array, m_serverParent->m_scopeType.ascii(), m_serverParent->m_scopeDeviceSocket) == 0) {
											long i;
											TQDoubleList permittedValues;
											for (i=0; i<permitted_count; i++) {
												permittedValues.append(permitted_array[i]/attenuation_mult);
											}
											free(permitted_array);
											ds << TQString("ACK");
											ds << permittedValues;
											writeEndOfFrame();
										}
										else {
											ds << TQString("NCK");
											writeEndOfFrame();
										}
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else {
									printf("[WARNING] Received unknown command %s from host %s\n\r", m_instrumentCommand.ascii(), m_remoteHost.ascii()); fflush(stdout);
									ds << TQString("NCK");
									writeEndOfFrame();
								}
							}
							else if (m_activeDeviceType == 3) {
								// Function generator
								char errorbuf[1000];
								if (m_instrumentCommand == "RESET") {				// Want to reset function generator
									if (signal_reset(m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETFREQUENCY") {		// Want to change frequency
									double value;
									ds >> value;
									if (signal_set_frequency(value, m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETDUTYCYCLE") {		// Want to change duty cycle
									double value;
									ds >> value;
									if (signal_set_duty_cycle(value, m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETSHAPESQUARE") {		// Want to set square wave
									if (signal_set_waveform("SQUARE", m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETSHAPESINE") {		// Want to set sine wave
									if (signal_set_waveform("SINUSOID", m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETSHAPETRIANGLE") {		// Want to set triangle wave
									if (signal_set_waveform("RAMP", m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETSHAPENOISE") {		// Want to set noise wave
									if (signal_set_waveform("NOISE", m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETPEAKPEAKVOLTAGE") {		// Want to change P-P voltage
									double value;
									ds >> value;
									if (signal_set_peak_peak_voltage(value, m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETOFFSETVOLTAGE") {		// Want to change offset voltage
									double value;
									ds >> value;
									if (signal_set_offset_voltage(value, m_serverParent->m_funcgenType.ascii(), m_serverParent->m_funcgenDeviceSocket, errorbuf) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString(errorbuf);
										writeEndOfFrame();
									}
								}
								else {
									printf("[WARNING] Received unknown command %s from host %s\n\r", m_instrumentCommand.ascii(), m_remoteHost.ascii()); fflush(stdout);
								}
							}
							else if (m_activeDeviceType == 4) {
								// Communications analyzer
								if (m_instrumentCommand == "SETMODESPECTRUMANALYZER") {		// Want to set SA mode
									if (commanalyzer_switch_to_spectrum_analyzer_mode(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if ((m_instrumentCommand == "GETSPECTRUMTRACE")) {		// Want SA trace
									if (commanalyzer_get_spectrum_analyzer_trace(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										int i;
										int tracelen = commanalyzerTraceLength(m_serverParent->m_commanalyzerType.ascii());
										TQDoubleArray traceData;
										traceData.resize(tracelen);
										for (i=0; i<tracelen; i++) {
											traceData[i] = commanalyzer_raw_trace_data[i];
										}
										ds << TQString("ACK");
										ds << traceData;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "LOCKSCREEN") {			// Want to lock screen
									if (commanalyzer_lock_screen(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										commanalyzer_set_display_brightness(0, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket);	// Don't burn in the screen
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENMODETRACKING") {		// Want to set generator to tracking mode
									if (commanalyzer_spectrum_analyzer_set_generator_mode_tracking(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENMODEFIXED") {		// Want to set generator to fixed mode
									if (commanalyzer_spectrum_analyzer_set_generator_mode_fixed(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETCENTERFREQUENCY") {		// Want to change center frequency
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_center_frequency(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETFREQUENCYSPAN") {		// Want to change frequency span
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_frequency_span(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETRFINPUTDEDICATED") {	// Want to set RF input to dedicated connector
									if (commanalyzer_spectrum_analyzer_set_rf_input_dedicated(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETRFINPUTMULTIPLEXED") {	// Want to set RF input to multiplexed connector
									if (commanalyzer_spectrum_analyzer_set_rf_input_muxed(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENOUTPUTDEDICATED") {	// Want to set generator output to dedicated connector
									if (commanalyzer_spectrum_analyzer_set_generator_output_dedicated(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENOUTPUTMULTIPLEXED") {	// Want to set generator output to multiplexed connector
									if (commanalyzer_spectrum_analyzer_set_generator_output_muxed(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETINPUTATTENUATION") {	// Want to change input attenuation
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_input_attenuation(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETVERTICALSCALE") {		// Want to change scale
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_scale(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETINPUTATTENMODEAUTO") {	// Want to set RF input attenuator mode to automatic
									if (commanalyzer_set_spectrum_analyzer_input_attenuator_mode_auto(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETINPUTATTENMODEFIXED") {	// Want to set RF input attenuator mode to fixed
									if (commanalyzer_set_spectrum_analyzer_input_attenuator_mode_fixed(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENOUTPUTPOWER") {		// Want to change generator output power
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_generator_power(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENOUTPUTFREQUENCY") {	// Want to change generator output frequency
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_generator_frequency(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENSWEEPASCENDING") {	// Want to set generator sweep to ascending
									if (commanalyzer_spectrum_analyzer_set_generator_sweep_ascending(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETGENSWEEPDESCENDING") {	// Want to set generator sweep to descending
									if (commanalyzer_spectrum_analyzer_set_generator_sweep_descending(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETTRACEAVERAGING") {		// Want to set trace averaging
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_trace_averaging(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "SETREFERENCEPOWERLEVEL") {	// Want to set reference power level
									double value;
									ds >> value;
									if (commanalyzer_set_spectrum_analyzer_reference_power_level(value, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETHORIZONTALDIVCOUNT") {	// Want the number of horizontal divisions available
									TQ_INT16 divisions = commanalyzer_get_spectrum_analyzer_number_of_horizontal_divisions(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket);
									if (divisions >= 0) {
										ds << TQString("ACK");
										ds << divisions;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETVERTICALDIVCOUNT") {	// Want the number of vertical divisions available
									TQ_INT16 divisions = commanalyzer_get_spectrum_analyzer_number_of_vertical_divisions(m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket);
									if (divisions >= 0) {
										ds << TQString("ACK");
										ds << divisions;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETTRACESAMPLECOUNT") {	// Want the number of samples in a trace
									TQ_INT16 divisions = commanalyzerTraceLength(m_serverParent->m_commanalyzerType.ascii());
									if (divisions >= 0) {
										ds << TQString("ACK");
										ds << divisions;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETREFERENCEPOWERLEVEL") {	// Want the reference power level
									double rpower;
									if (commanalyzer_get_spectrum_analyzer_reference_power_level(&rpower, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << rpower;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETVERTDIVSCALE") {		// Want the vertical division scale
									double scale;
									if (commanalyzer_get_spectrum_analyzer_scale(&scale, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << scale;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETCENTERFREQUENCY") {		// Want to get the center frequency
									double freq;
									if (commanalyzer_get_spectrum_analyzer_center_frequency(&freq, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << freq;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else if (m_instrumentCommand == "GETFREQUENCYSPAN") {		// Want to get the frequency span
									double freq;
									if (commanalyzer_get_spectrum_analyzer_span(&freq, m_serverParent->m_commanalyzerType.ascii(), m_serverParent->m_commanalyzerDeviceSocket) == 0) {
										ds << TQString("ACK");
										ds << freq;
										writeEndOfFrame();
									}
									else {
										ds << TQString("NCK");
										writeEndOfFrame();
									}
								}
								else {
									printf("[WARNING] Received unknown command %s from host %s\n\r", m_instrumentCommand.ascii(), m_remoteHost.ascii()); fflush(stdout);
								}
							}
							else {
								// Unknown
								transferred_data = true;
								m_commandLoopState = 0;
							}

							m_servClientTimeout->start(NETWORK_COMM_TIMEOUT_MS, TRUE);
							transferred_data = true;
							m_commandLoopState = 1;
						}

						clearFrameTail();
					}
				}
			}
		}
		m_criticalSection--;
		if (transferred_data) {
			if (m_loopTimer) m_loopTimer->start(0, TRUE);
		}
		else {
			if (m_loopTimer) m_loopTimer->start(100, TRUE);
		}
		return;
	}
	catch (...) {
		m_criticalSection--;
		return;
	}
}

int GPIBSocket::enterCommandLoop() {
	m_commandLoopState = 0;
	if (!m_loopTimer) {
		m_loopTimer = new TQTimer();
		connect(m_loopTimer, SIGNAL(timeout()), this, SLOT(commandLoop()));
	}
	if (m_loopTimer) m_loopTimer->start(0, TRUE);
	return 0;
}

/*
  The GPIBServer class handles new connections to the server. For every
  client that connects, it creates a new GPIBSocket -- that instance is now
  responsible for the communication with that client.
*/
GPIBServer::GPIBServer(TQObject* parent, int port, KSimpleConfig* config) :
	TQServerSocket( port, 1, parent ), m_config(config), m_numberOfConnections(0),
	m_scopeDeviceSocket(-1), m_funcgenDeviceSocket(-1), m_commanalyzerDeviceSocket(-1) {

	if ( !ok() ) {
		printf("[ERROR] Failed to bind to port %d\n\r", port);
		exit(1);
	}

	if (readConfig() != 0) {
		exit(-1);
	}

	printf("[INFO] Server started on port %d\n\r", port); fflush(stdout);
}

GPIBServer::~GPIBServer() {
	//
}

void GPIBServer::newConnection(int socket) {
	GPIBSocket *s = new GPIBSocket(socket, this);
	s->m_remoteHost = s->peerAddress().toString();
	printf("[DEBUG] New connection from %s\n\r", s->m_remoteHost.ascii());
	connect(s, SIGNAL(connectionClosed()), s, SLOT(deleteLater()));
	s->initiateKerberosHandshake();
	emit newConnect(s);
}

void GPIBServer::remoteConnectionClosed() {
	m_numberOfConnections--;
}

int GPIBServer::readConfig() {
	// Serial port
	m_config->setGroup("Serial Port");
	m_serialDevice = m_config->readEntry("device", "");
	TQString desiredBaudRate = m_config->readEntry("baudrate", "9600");
	if (desiredBaudRate == "1200") {
		m_serialBaud = B1200;
	}
	else if (desiredBaudRate == "9600") {
		m_serialBaud = B9600;
	}
	else if (desiredBaudRate == "19200") {
		m_serialBaud = B19200;
	}
	else if (desiredBaudRate == "115200") {
		m_serialBaud = B115200;
	}
	else {
		printf("[WARNING] Invalid baudrate %s specified, selecting 9600 instead\n\r", desiredBaudRate.ascii()); fflush(stdout);
		desiredBaudRate = "9600";
		m_serialBaud = B9600;
	}

	// Oscilloscope
	m_config->setGroup("Oscilloscope");
	m_scopeType = m_config->readEntry("type", "");
	m_scopeConnection = m_config->readEntry("connection", "gpib");
	m_scopeBoard = m_config->readNumEntry("board", 0);
	m_scopeDevice = m_config->readNumEntry("device", 0);

	// Function generator
	m_config->setGroup("Function Generator");
	m_funcgenType = m_config->readEntry("type", "");
	m_funcgenConnection = m_config->readEntry("connection", "gpib");
	m_funcgenBoard = m_config->readNumEntry("board", 0);
	m_funcgenDevice = m_config->readNumEntry("device", 0);

	// Communications analyzer
	m_config->setGroup("Communications Analyzer");
	m_commanalyzerType = m_config->readEntry("type", "");
	m_commanalyzerConnection = m_config->readEntry("connection", "gpib");
	m_commanalyzerBoard = m_config->readNumEntry("board", 0);
	m_commanalyzerDevice = m_config->readNumEntry("device", 0);

	if (m_serialDevice != "") {
		struct termios oldtio, newtio;

		m_serialDeviceSocket = ::open(m_serialDevice.ascii(), O_RDWR | O_NOCTTY | O_NONBLOCK | O_APPEND);
		if (m_serialDeviceSocket < 0) {
			printf("[FAIL] Unable to open serial device %s\n\r", m_serialDevice.ascii()); fflush(stdout);
			return 1;
		}
	
		tcgetattr(m_serialDeviceSocket, &oldtio);	// Save current port settings

		bzero(&newtio, sizeof(newtio));
		newtio.c_cflag = m_serialBaud | CS8 | CLOCAL | CREAD;
		newtio.c_iflag = IGNPAR;
		newtio.c_oflag = 0;
	
		// Set input mode (non-canonical, no echo,...)
		newtio.c_lflag = 0;
	
		newtio.c_cc[VTIME] = 0;	// Inter-character timer unused
		newtio.c_cc[VMIN]  = 0;	// Blocking read unused
		
		tcflush(m_serialDeviceSocket, TCIFLUSH);
		tcsetattr(m_serialDeviceSocket, TCSANOW, &newtio);

		printf("[INFO] Serial port on node %s activated at %s baud\n\r", m_serialDevice.ascii(), desiredBaudRate.ascii());
	}

	if (m_scopeType != "") {
		printf("[INFO] Oscilloscope conjectured to be on GPIB address %d:%d\n\r", m_scopeBoard, m_scopeDevice);
		m_scopeDeviceSocket = open_gpib_device(m_scopeBoard, m_scopeDevice);
		if (m_scopeDeviceSocket < 0) {
			// return -1;
		}
		else {
			time_t rawtime;
			struct tm * timeinfo;
			char datebuffer [80];
			char timebuffer [80];
			time ( &rawtime );
			timeinfo = localtime ( &rawtime );
			strftime(timebuffer,80,"TIME \"%H:%M:%S\"",timeinfo);
			strftime(datebuffer,80,"DATE \"%Y-%m-%d\"",timeinfo);
			printf("[INFO] Configuring %s oscilloscope\n\r", scopeLongDescription(m_scopeType.ascii()));
			printf("[INFO] %s\n\r", datebuffer);
			printf("[INFO] %s\n\r", timebuffer);
			if (gpib_write(m_scopeDeviceSocket, timebuffer) == 0) {
				gpib_write(m_scopeDeviceSocket, datebuffer);
				scope_perform_initial_setup(m_scopeType.ascii(), m_scopeDeviceSocket);
				printf("[INFO] Communication verified\n\r");
			}
			else {
				printf("[WARN] Communication failed!\n\r");
			}
		}
	}

	if (m_funcgenType != "") {
		printf("[INFO] Function generator conjectured to be on GPIB address %d:%d\n\r", m_funcgenBoard, m_funcgenDevice);
		m_funcgenDeviceSocket = open_gpib_device(m_funcgenBoard, m_funcgenDevice);
		if (m_funcgenDeviceSocket < 0) {
			//return 1;
		}
		else {
			printf("[INFO] Configuring %s function generator\n\r", funcgenLongDescription(m_funcgenType.ascii()));
			if (gpib_write(m_funcgenDeviceSocket, "RESET") == 0) {
				printf("[INFO] Communication verified\n\r");
			}
			else {
				printf("[WARN] Communication failed!\n\r");
			}
		}
	}

	if (m_commanalyzerType != "") {
		printf("[INFO] Communications analyzer conjectured to be on GPIB address %d:%d\n\r", m_commanalyzerBoard, m_commanalyzerDevice);
		m_commanalyzerDeviceSocket = open_gpib_device(m_commanalyzerBoard, m_commanalyzerDevice);
		if (m_commanalyzerDeviceSocket < 0) {
			//return 1;
		}
		else {
			time_t rawtime;
			struct tm * timeinfo;
			time ( &rawtime );
			timeinfo = localtime ( &rawtime );
			printf("[INFO] Configuring %s communications analyzer\n\r", commanalyzerLongDescription(m_commanalyzerType.ascii()));
			if (commanalyzer_set_time(timeinfo, m_commanalyzerType.ascii(), m_commanalyzerDeviceSocket) == 0) {
				commanalyzer_set_date(timeinfo, m_commanalyzerType.ascii(), m_commanalyzerDeviceSocket);
				printf("[INFO] Communication verified\n\r");
			}
			else {
				printf("[WARN] Communication failed!\n\r");
			}
		}
	}

	return 0;
}