-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim_frontpanel.c
1897 lines (1741 loc) · 61.5 KB
/
sim_frontpanel.c
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
/* sim_frontpanel.c: simulator frontpanel API
Copyright (c) 2015, Mark Pizzolato
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
MARK PIZZOLATO BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Mark Pizzolato shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Mark Pizzolato.
05-Feb-15 MP Initial implementation
01-Apr-15 MP Added register indirect, mem_examine and mem_deposit
03-Apr-15 MP Added logic to pass simulator startup messages in
panel error text if the connection to the simulator
shuts down while it is starting.
04-Apr-15 MP Added mount and dismount routines to connect and
disconnect removable media
This module provides interface between a front panel application and a simh
simulator. Facilities provide ways to gather information from and to
observe and control the state of a simulator.
*/
#ifdef __cplusplus
extern "C" {
#endif
#include "sim_frontpanel.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <signal.h>
#include <pthread.h>
#include "sim_sock.h"
#if defined(_WIN32)
#include <process.h>
#include <windows.h>
#define sleep(n) Sleep(n*1000)
#define msleep(n) Sleep(n)
#define strtoull _strtoui64
#define CLOCK_REALTIME 0
int clock_gettime(int clk_id, struct timespec *tp)
{
unsigned long long now, unixbase;
unixbase = 116444736;
unixbase *= 1000000000;
GetSystemTimeAsFileTime((FILETIME*)&now);
now -= unixbase;
tp->tv_sec = (long)(now/10000000);
tp->tv_nsec = (now%10000000)*100;
return 0;
}
#else /* NOT _WIN32 */
#include <unistd.h>
#define msleep(n) usleep(1000*n)
#include <sys/wait.h>
#if defined (__APPLE__)
#define HAVE_STRUCT_TIMESPEC 1 /* OSX defined the structure but doesn't tell us */
#endif
/* on HP-UX, CLOCK_REALTIME is enum, not preprocessor define */
#if !defined(CLOCK_REALTIME) && !defined(__hpux)
#define CLOCK_REALTIME 1
#define NEED_CLOCK_GETTIME 1
#if !defined(HAVE_STRUCT_TIMESPEC)
#define HAVE_STRUCT_TIMESPEC 1
#if !defined(_TIMESPEC_DEFINED)
#define _TIMESPEC_DEFINED
struct timespec {
long tv_sec;
long tv_nsec;
};
#endif /* _TIMESPEC_DEFINED */
#endif /* HAVE_STRUCT_TIMESPEC */
#if defined(NEED_CLOCK_GETTIME)
int clock_gettime(int clk_id, struct timespec *tp)
{
struct timeval cur;
struct timezone foo;
gettimeofday (&cur, &foo);
tp->tv_sec = cur.tv_sec;
tp->tv_nsec = cur.tv_usec*1000;
return 0;
}
#endif /* defined(NEED_CLOCK_GETTIME) */
#endif /* !defined(CLOCK_REALTIME) && !defined(__hpux) */
#endif /* NOT _WIN32 */
typedef struct {
char *name;
char *device_name;
void *addr;
size_t size;
int indirect;
size_t element_count;
} REG;
struct PANEL {
PANEL *parent; /* Device Panels can have parent panels */
char *path; /* simulator path */
char *config;
char *device_name; /* device name */
char *temp_config;
char hostport[64];
size_t device_count;
PANEL **devices;
SOCKET sock;
size_t reg_count;
REG *regs;
char *reg_query;
size_t reg_query_size;
unsigned long long array_element_data;
OperationalState State;
unsigned long long simulation_time;
pthread_mutex_t lock;
pthread_t io_thread;
int io_thread_running;
pthread_mutex_t io_lock;
pthread_mutex_t io_send_lock;
int io_reg_query_pending;
int io_waiting;
char *io_response;
size_t io_response_data;
size_t io_response_size;
pthread_cond_t io_done;
pthread_cond_t startup_cond;
PANEL_DISPLAY_PCALLBACK callback;
pthread_t callback_thread;
int callback_thread_running;
void *callback_context;
int callbacks_per_second;
int debug;
char *simulator_version;
int radix;
FILE *Debug;
#if defined(_WIN32)
HANDLE hProcess;
#else
pid_t pidProcess;
#endif
};
static const char *sim_prompt = "sim> ";
static const char *register_get_prefix = "show time";
static const char *register_get_echo = "# REGISTERS-DONE";
static const char *register_dev_echo = "# REGISTERS-FOR-DEVICE:";
static const char *register_ind_echo = "# REGISTER-INDIRECT:";
static const char *command_done_echo = "# COMMAND-DONE";
static int little_endian;
static void *_panel_reader(void *arg);
static void *_panel_callback(void *arg);
static void sim_panel_set_error (const char *fmt, ...);
#define TN_IAC 0xFFu /* -1 */ /* protocol delim */
#define TN_DONT 0xFEu /* -2 */ /* dont */
#define TN_DO 0xFDu /* -3 */ /* do */
#define TN_WONT 0xFCu /* -4 */ /* wont */
#define TN_WILL 0xFBu /* -5 */ /* will */
#define TN_BIN 0 /* bin */
#define TN_ECHO 1 /* echo */
#define TN_SGA 3 /* sga */
#define TN_CR 015 /* carriage return */
#define TN_LF 012 /* line feed */
#define TN_LINE 34 /* line mode */
static unsigned char mantra[] = {
TN_IAC, TN_WILL, TN_LINE,
TN_IAC, TN_WILL, TN_SGA,
TN_IAC, TN_WILL, TN_ECHO,
TN_IAC, TN_WILL, TN_BIN,
TN_IAC, TN_DO, TN_BIN
};
static void *
_panel_malloc (size_t size)
{
void *p = malloc (size);
if (p == NULL)
sim_panel_set_error ("Out of Memory");
return p;
}
static void _panel_debug (PANEL *p, int dbits, const char *fmt, const char *buf, int bufsize, ...)
{
if (p && p->Debug && (dbits & p->debug)) {
int i;
struct timespec time_now;
va_list arglist;
char timestamp[32];
size_t obufsize = 10240 + 8*bufsize;
char *obuf = (char *)_panel_malloc (obufsize);
clock_gettime(CLOCK_REALTIME, &time_now);
sprintf (timestamp, "%lld.%03d ", (long long)(time_now.tv_sec), (int)(time_now.tv_nsec/1000000));
va_start (arglist, bufsize);
vsnprintf (obuf, obufsize - 1, fmt, arglist);
va_end (arglist);
for (i=0; i<bufsize; ++i) {
switch ((unsigned char)buf[i]) {
case TN_CR:
sprintf (&obuf[strlen (obuf)], "_TN_CR_");
break;
case TN_LF:
sprintf (&obuf[strlen (obuf)], "_TN_LF_");
break;
case TN_IAC:
sprintf (&obuf[strlen (obuf)], "_TN_IAC_");
switch ((unsigned char)buf[i+1]) {
case TN_IAC:
sprintf (&obuf[strlen (obuf)], "_TN_IAC_"); ++i;
break;
case TN_DONT:
sprintf (&obuf[strlen (obuf)], "_TN_DONT_"); ++i;
break;
case TN_DO:
sprintf (&obuf[strlen (obuf)], "_TN_DO_"); ++i;
break;
case TN_WONT:
sprintf (&obuf[strlen (obuf)], "_TN_WONT_"); ++i;
break;
case TN_WILL:
sprintf (&obuf[strlen (obuf)], "_TN_WILL_"); ++i;
break;
default:
sprintf (&obuf[strlen (obuf)], "_0x%02X_", (unsigned char)buf[i+1]); ++i;
break;
}
switch ((unsigned char)buf[i+1]) {
case TN_BIN:
sprintf (&obuf[strlen (obuf)], "_TN_BIN_"); ++i;
break;
case TN_ECHO:
sprintf (&obuf[strlen (obuf)], "_TN_ECHO_"); ++i;
break;
case TN_SGA:
sprintf (&obuf[strlen (obuf)], "_TN_SGA_"); ++i;
break;
case TN_LINE:
sprintf (&obuf[strlen (obuf)], "_TN_LINE_"); ++i;
break;
default:
sprintf (&obuf[strlen (obuf)], "_0x%02X_", (unsigned char)buf[i+1]); ++i;
break;
}
break;
default:
if (isprint((u_char)buf[i]))
sprintf (&obuf[strlen (obuf)], "%c", buf[i]);
else {
sprintf (&obuf[strlen (obuf)], "_");
if ((buf[i] >= 1) && (buf[i] <= 26))
sprintf (&obuf[strlen (obuf)], "^%c", 'A' + buf[i] - 1);
else
sprintf (&obuf[strlen (obuf)], "\\%03o", (u_char)buf[i]);
sprintf (&obuf[strlen (obuf)], "_");
}
break;
}
}
fprintf(p->Debug, "%s%s\n", timestamp, obuf);
free (obuf);
}
}
void
sim_panel_set_debug_file (PANEL *panel, const char *debug_file)
{
if (!panel)
return;
panel->Debug = fopen(debug_file, "w");
}
void
sim_panel_set_debug_mode (PANEL *panel, int debug_bits)
{
if (panel)
panel->debug = debug_bits;
}
void
sim_panel_flush_debug (PANEL *panel)
{
if (!panel)
return;
if (panel->Debug)
fflush (panel->Debug);
}
static int
_panel_send (PANEL *p, const char *msg, int len)
{
int sent = 0;
if (p->sock == INVALID_SOCKET) {
sim_panel_set_error ("Invalid Socket for write");
p->State = Error;
return -1;
}
pthread_mutex_lock (&p->io_send_lock);
while (len) {
int bsent = sim_write_sock (p->sock, msg, len);
if (bsent < 0) {
sim_panel_set_error ("%s", sim_get_err_sock("Error writing to socket"));
p->State = Error;
pthread_mutex_unlock (&p->io_send_lock);
return bsent;
}
_panel_debug (p, DBG_XMT, "Sent %d bytes: ", msg, bsent, bsent);
len -= bsent;
msg += bsent;
sent += bsent;
}
pthread_mutex_unlock (&p->io_send_lock);
return sent;
}
static int
_panel_sendf (PANEL *p, int wait_for_completion, char **response, const char *fmt, ...);
static int
_panel_register_query_string (PANEL *panel, char **buf, size_t *buf_size)
{
size_t i, j, buf_data, buf_needed = 0;
char *dev;
pthread_mutex_lock (&panel->io_lock);
buf_needed = 2 + strlen (register_get_prefix); /* SHOW TIME */
for (i=0; i<panel->reg_count; i++) {
buf_needed += 9 + strlen (panel->regs[i].name) + (panel->regs[i].device_name ? strlen (panel->regs[i].device_name) : 0);
if (panel->regs[i].element_count > 0)
buf_needed += 4 + 6 /* 6 digit register array index */;
if (panel->regs[i].indirect)
buf_needed += 12 + strlen (register_ind_echo) + strlen (panel->regs[i].name);
}
buf_needed += 10 + strlen (register_get_echo); /* # REGISTERS-DONE */
if (buf_needed > *buf_size) {
free (*buf);
*buf = (char *)_panel_malloc (buf_needed);
if (!*buf) {
panel->State = Error;
pthread_mutex_unlock (&panel->io_lock);
return -1;
}
*buf_size = buf_needed;
}
buf_data = 0;
sprintf (*buf + buf_data, "%s\r", register_get_prefix);
buf_data += strlen (*buf + buf_data);
dev = "";
for (i=j=0; i<panel->reg_count; i++) {
char *reg_dev = panel->regs[i].device_name ? panel->regs[i].device_name : "";
if (panel->regs[i].indirect)
continue;
if (strcmp (dev, reg_dev)) {/* devices are different */
char *tbuf;
buf_needed += 4 + strlen (register_dev_echo) + strlen (reg_dev); /* # REGISTERS-for-DEVICE:XXX */
tbuf = (char *)_panel_malloc (buf_needed);
if (tbuf == NULL) {
panel->State = Error;
pthread_mutex_unlock (&panel->io_lock);
return -1;
}
strcpy (tbuf, *buf);
free (*buf);
*buf = tbuf;
sprintf (*buf + buf_data, "%s%s%s\r", (i == 0)? "" : "\r", register_dev_echo, reg_dev);
buf_data += strlen (*buf + buf_data);
dev = reg_dev;
j = 0;
*buf_size = buf_needed;
}
if (panel->regs[i].element_count == 0) {
if (j == 0)
sprintf (*buf + buf_data, "E -H %s %s", dev, panel->regs[i].name);
else
sprintf (*buf + buf_data, ",%s", panel->regs[i].name);
}
else {
if (j == 0)
sprintf (*buf + buf_data, "E -H %s %s[0:%d]", dev, panel->regs[i].name, panel->regs[i].element_count-1);
else
sprintf (*buf + buf_data, ",%s[0:%d]", panel->regs[i].name, panel->regs[i].element_count-1);
}
++j;
buf_data += strlen (*buf + buf_data);
}
if (buf_data && ((*buf)[buf_data-1] != '\r')) {
strcpy (*buf + buf_data, "\r");
buf_data += strlen (*buf + buf_data);
}
for (i=j=0; i<panel->reg_count; i++) {
char *reg_dev = panel->regs[i].device_name ? panel->regs[i].device_name : "";
if (!panel->regs[i].indirect)
continue;
sprintf (*buf + buf_data, "%s%s\rE -H %s %s,$\r", register_ind_echo, panel->regs[i].name, reg_dev, panel->regs[i].name);
buf_data += strlen (*buf + buf_data);
}
strcpy (*buf + buf_data, register_get_echo);
buf_data += strlen (*buf + buf_data);
strcpy (*buf + buf_data, "\r");
buf_data += strlen (*buf + buf_data);
*buf_size = buf_data;
pthread_mutex_unlock (&panel->io_lock);
return 0;
}
static PANEL **panels = NULL;
static int panel_count = 0;
static void
_panel_cleanup (void)
{
while (panel_count)
sim_panel_destroy (*panels);
}
static void
_panel_register_panel (PANEL *p)
{
++panel_count;
panels = (PANEL **)realloc (panels, sizeof(*panels)*panel_count);
panels[panel_count-1] = p;
if (panel_count == 1)
atexit (&_panel_cleanup);
}
static void
_panel_deregister_panel (PANEL *p)
{
int i;
for (i=0; i<panel_count; i++) {
if (panels[i] == p) {
int j;
for (j=i+1; j<panel_count; j++)
panels[j-1] = panels[j];
--panel_count;
if (panel_count == 0) {
free (panels);
panels = NULL;
}
break;
}
}
}
static PANEL *
_sim_panel_create (const char *sim_path,
const char *sim_config,
size_t device_panel_count,
PANEL *simulator_panel,
const char *device_name,
const char *debug_file)
{
PANEL *p = NULL;
FILE *fIn = NULL;
FILE *fOut = NULL;
struct stat statb;
char *buf = NULL;
int port;
size_t i, device_num;
char hostport[64];
union {int i; char c[sizeof (int)]; } end_test;
if (simulator_panel) {
for (device_num=0; device_num < simulator_panel->device_count; ++device_num)
if (simulator_panel->devices[device_num] == NULL)
break;
if (device_num == simulator_panel->device_count) {
sim_panel_set_error ("No free panel devices slots available %s simulator. All %d slots are used.", simulator_panel->path, (int)simulator_panel->device_count);
return NULL;
}
p = (PANEL *)_panel_malloc (sizeof(*p));
if (p == NULL)
goto Error_Return;
memset (p, 0, sizeof(*p));
p->device_name = (char *)_panel_malloc (1 + strlen (device_name));
if (p->device_name == NULL)
goto Error_Return;
strcpy (p->device_name, device_name);
p->parent = simulator_panel;
strcpy (p->hostport, simulator_panel->hostport);
p->sock = INVALID_SOCKET;
}
else {
end_test.i = 1; /* test endian-ness */
little_endian = (end_test.c[0] != 0);
sim_init_sock ();
for (port=1024; port < 2048; port++) {
SOCKET sock;
sprintf (hostport, "%d", port);
sock = sim_connect_sock_ex (NULL, hostport, NULL, NULL, SIM_SOCK_OPT_NODELAY | SIM_SOCK_OPT_BLOCKING);
if (sock != INVALID_SOCKET) {
int sta = 0;
while (!sta) {
msleep (10);
sta = sim_check_conn (sock, 1);
}
sim_close_sock (sock);
if (sta == -1)
break;
}
else
break;
}
if (stat (sim_config, &statb) < 0) {
sim_panel_set_error ("Can't stat simulator configuration '%s': %s", sim_config, strerror(errno));
goto Error_Return;
}
buf = (char *)_panel_malloc (statb.st_size+1);
if (buf == NULL)
goto Error_Return;
buf[statb.st_size] = '\0';
p = (PANEL *)_panel_malloc (sizeof(*p));
if (p == NULL)
goto Error_Return;
memset (p, 0, sizeof(*p));
p->sock = INVALID_SOCKET;
p->path = (char *)_panel_malloc (strlen (sim_path) + 1);
if (p->path == NULL)
goto Error_Return;
strcpy (p->path, sim_path);
p->config = (char *)_panel_malloc (strlen (sim_config) + 1);
if (p->config == NULL)
goto Error_Return;
strcpy (p->config, sim_config);
fIn = fopen (sim_config, "r");
if (fIn == NULL) {
sim_panel_set_error ("Can't open configuration file '%s': %s", sim_config, strerror(errno));
goto Error_Return;
}
p->temp_config = (char *)_panel_malloc (strlen (sim_config) + 40);
if (p->temp_config == NULL)
goto Error_Return;
sprintf (p->temp_config, "%s-Panel-%d", sim_config, getpid());
fOut = fopen (p->temp_config, "w");
if (fOut == NULL) {
sim_panel_set_error ("Can't create temporary configuration file '%s': %s", p->temp_config, strerror(errno));
goto Error_Return;
}
fprintf (fOut, "# Temporary FrontPanel generated simh configuration file\n");
fprintf (fOut, "# Original Configuration File: %s\n", p->config);
fprintf (fOut, "# Simulator Path: %s\n", sim_path);
while (fgets (buf, statb.st_size, fIn))
fputs (buf, fOut);
free (buf);
buf = NULL;
fclose (fIn);
fIn = NULL;
fprintf (fOut, "set remote notelnet\n");
if (device_panel_count)
fprintf (fOut, "set remote connections=%d\n", (int)device_panel_count+1);
fprintf (fOut, "set remote -u telnet=%s\n", hostport);
fprintf (fOut, "set remote master\n");
fprintf (fOut, "exit\n");
fclose (fOut);
fOut = NULL;
}
if (debug_file) {
sim_panel_set_debug_file (p, debug_file);
sim_panel_set_debug_mode (p, DBG_XMT|DBG_RCV);
_panel_debug (p, DBG_XMT|DBG_RCV, "Creating Simulator Process %s\n", NULL, 0, sim_path);
}
if (!simulator_panel) {
#if defined(_WIN32)
char cmd[2048];
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo;
sprintf(cmd, "%s%s%s %s%s%s", strchr (sim_path, ' ') ? "\"" : "", sim_path, strchr (sim_path, ' ') ? "\"" : "", strchr (p->temp_config, ' ') ? "\"" : "", p->temp_config, strchr (p->temp_config, ' ') ? "\"" : "");
memset (&ProcessInfo, 0, sizeof(ProcessInfo));
memset (&StartupInfo, 0, sizeof(StartupInfo));
StartupInfo.dwFlags = STARTF_USESTDHANDLES;
StartupInfo.hStdInput = INVALID_HANDLE_VALUE;
StartupInfo.hStdOutput = INVALID_HANDLE_VALUE;
StartupInfo.hStdError = INVALID_HANDLE_VALUE;
if (CreateProcessA(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &StartupInfo, &ProcessInfo)) {
CloseHandle (ProcessInfo.hThread);
p->hProcess = ProcessInfo.hProcess;
}
else { /* Creation Problem */
sim_panel_set_error ("CreateProcess Error: %d", GetLastError());
goto Error_Return;
}
#else
p->pidProcess = fork();
if (p->pidProcess == 0) {
close (0); close (1); close (2); /* make sure not to pass the open standard handles */
dup (dup (open ("/dev/null", O_RDWR))); /* open standard handles to /dev/null */
if (execlp (sim_path, sim_path, p->temp_config, NULL, NULL)) {
perror ("execl");
exit(errno);
}
}
if (p->pidProcess < 0) {
p->pidProcess = 0;
sim_panel_set_error ("fork() Error: %s", strerror(errno));
goto Error_Return;
}
#endif
strcpy (p->hostport, hostport);
}
for (i=0; i<100; i++) { /* Allow up to 10 seconds waiting for simulator to start up */
p->sock = sim_connect_sock_ex (NULL, p->hostport, NULL, NULL, SIM_SOCK_OPT_NODELAY | SIM_SOCK_OPT_BLOCKING);
if (p->sock == INVALID_SOCKET)
msleep (100);
else
break;
}
if (p->sock == INVALID_SOCKET) {
if (simulator_panel) {
sim_panel_set_error ("Can't connect to simulator Remote Console on port %s", p->hostport);
}
else {
if (stat (sim_path, &statb) < 0)
sim_panel_set_error ("Can't stat simulator '%s': %s", sim_path, strerror(errno));
else
sim_panel_set_error ("Can't connect to the %s simulator Remote Console on port %s, the simulator process may not have started or the simulator binary can't be found", sim_path, p->hostport);
}
goto Error_Return;
}
_panel_debug (p, DBG_XMT|DBG_RCV, "Connected to simulator at %s after %dms\n", NULL, 0, p->hostport, i*100);
pthread_mutex_init (&p->io_lock, NULL);
pthread_mutex_init (&p->io_send_lock, NULL);
pthread_cond_init (&p->io_done, NULL);
pthread_cond_init (&p->startup_cond, NULL);
if (sizeof(mantra) != _panel_send (p, (char *)mantra, sizeof(mantra))) {
sim_panel_set_error ("Error sending Telnet mantra (options): %s", sim_get_err_sock ("send"));
goto Error_Return;
}
if (1) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
pthread_mutex_lock (&p->io_lock);
p->io_thread_running = 0;
pthread_create (&p->io_thread, &attr, _panel_reader, (void *)p);
pthread_attr_destroy(&attr);
while (!p->io_thread_running)
pthread_cond_wait (&p->startup_cond, &p->io_lock); /* Wait for thread to stabilize */
pthread_mutex_unlock (&p->io_lock);
pthread_cond_destroy (&p->startup_cond);
}
if (simulator_panel) {
simulator_panel->devices[device_num] = p;
}
else {
if (device_panel_count) {
p->devices = (PANEL **)_panel_malloc (device_panel_count*sizeof(*p->devices));
if (p->devices == NULL)
goto Error_Return;
memset (p->devices, 0, device_panel_count*sizeof(*p->devices));
p->device_count = device_panel_count;
}
if (p->State == Error)
goto Error_Return;
/* Validate sim_frontpanel API version */
if (_panel_sendf (p, 1, &p->simulator_version, "SHOW VERSION\r"))
goto Error_Return;
if (1) {
int api_version = 0;
char *c = strstr (p->simulator_version, "FrontPanel API Version");
if ((!c) ||
(1 != sscanf (c, "FrontPanel API Version %d", &api_version)) ||
(api_version != SIM_FRONTPANEL_VERSION)) {
sim_panel_set_error ("Inconsistent sim_frontpanel API version %d in simulator. Version %d needed.-", api_version, SIM_FRONTPANEL_VERSION);
goto Error_Return;
}
}
if (1) {
char *radix = NULL;
if (_panel_sendf (p, 1, &radix, "SHOW %s RADIX\r", p->device_name ? p->device_name : "")) {
free (radix);
goto Error_Return;
}
sscanf (radix, "Radix=%d", &p->radix);
free (radix);
if ((p->radix != 16) && (p->radix != 8)) {
sim_panel_set_error ("Unsupported Radix: %d%s%s.", p->radix, p->device_name ? " on device " : "", p->device_name ? p->device_name : "");
goto Error_Return;
}
}
}
_panel_register_panel (p);
return p;
Error_Return:
if (fIn)
fclose (fIn);
if (fOut) {
fclose (fOut);
remove (p->temp_config);
}
if (buf)
free (buf);
if (1) {
const char *err = sim_panel_get_error();
char *errbuf = (char *)_panel_malloc (1 + strlen (err));
strcpy (errbuf, err); /* preserve error info while closing */
sim_panel_destroy (p);
sim_panel_set_error ("%s", errbuf);
free (errbuf);
}
return NULL;
}
PANEL *
sim_panel_start_simulator_debug (const char *sim_path,
const char *sim_config,
size_t device_panel_count,
const char *debug_file)
{
return _sim_panel_create (sim_path, sim_config, device_panel_count, NULL, NULL, debug_file);
}
PANEL *
sim_panel_start_simulator (const char *sim_path,
const char *sim_config,
size_t device_panel_count)
{
return sim_panel_start_simulator_debug (sim_path, sim_config, device_panel_count, NULL);
}
PANEL *
sim_panel_add_device_panel_debug (PANEL *simulator_panel,
const char *device_name,
const char *debug_file)
{
return _sim_panel_create (NULL, NULL, 0, simulator_panel, device_name, debug_file);
}
PANEL *
sim_panel_add_device_panel (PANEL *simulator_panel,
const char *device_name)
{
return sim_panel_add_device_panel_debug (simulator_panel, device_name, NULL);
}
int
sim_panel_destroy (PANEL *panel)
{
REG *reg;
if (panel) {
_panel_debug (panel, DBG_XMT|DBG_RCV, "Closing Panel %s\n", NULL, 0, panel->device_name? panel->device_name : panel->path);
if (panel->devices) {
size_t i;
for (i=0; i<panel->device_count; i++) {
if (panel->devices[i])
sim_panel_destroy (panel->devices[i]);
}
free (panel->devices);
panel->devices = NULL;
}
_panel_deregister_panel (panel);
free (panel->path);
free (panel->device_name);
free (panel->config);
if (panel->sock != INVALID_SOCKET) {
SOCKET sock = panel->sock;
int wait_count;
/* First, wind down the automatic register queries */
sim_panel_set_display_callback (panel, NULL, NULL, 0);
/* Next, attempt a simulator shutdown */
_panel_send (panel, "\005\rEXIT\r", 7);
/* Wait for up to 2 seconds for a graceful shutdown */
for (wait_count=0; panel->io_thread_running && (wait_count<20); ++wait_count)
msleep (100);
/* Now close the socket which should stop a pending read which hasn't completed */
panel->sock = INVALID_SOCKET;
sim_close_sock (sock);
pthread_join (panel->io_thread, NULL);
pthread_mutex_destroy (&panel->io_lock);
pthread_mutex_destroy (&panel->io_send_lock);
pthread_cond_destroy (&panel->io_done);
}
#if defined(_WIN32)
if (panel->hProcess) {
TerminateProcess (panel->hProcess, 0);
WaitForSingleObject (panel->hProcess, INFINITE);
CloseHandle (panel->hProcess);
}
#else
if (panel->pidProcess) {
int status;
if (!kill (panel->pidProcess, 0)) {
kill (panel->pidProcess, SIGTERM);
msleep (200);
if (!kill (panel->pidProcess, 0))
kill (panel->pidProcess, SIGKILL);
}
waitpid (panel->pidProcess, &status, 0);
}
#endif
if (panel->temp_config)
remove (panel->temp_config);
free (panel->temp_config);
reg = panel->regs;
while (panel->reg_count--) {
free (reg->name);
free (reg->device_name);
reg++;
}
free (panel->regs);
free (panel->reg_query);
free (panel->io_response);
free (panel->simulator_version);
if (panel->Debug)
fclose (panel->Debug);
sim_cleanup_sock ();
free (panel);
}
return 0;
}
OperationalState
sim_panel_get_state (PANEL *panel)
{
if (!panel)
return Halt;
return panel->State;
}
static int
_panel_add_register (PANEL *panel,
const char *name,
const char *device_name,
size_t size,
void *addr,
int indirect,
size_t element_count)
{
REG *regs, *reg;
char *response = NULL;
size_t i;
if (!panel || (panel->State == Error)) {
sim_panel_set_error ("Invalid Panel");
return -1;
}
regs = (REG *)_panel_malloc ((1 + panel->reg_count)*sizeof(*regs));
if (regs == NULL) {
panel->State = Error;
return -1;
}
pthread_mutex_lock (&panel->io_lock);
memcpy (regs, panel->regs, panel->reg_count*sizeof(*regs));
reg = ®s[panel->reg_count];
memset (reg, 0, sizeof(*regs));
reg->name = (char *)_panel_malloc (1 + strlen (name));
if (reg->name == NULL) {
panel->State = Error;
free (regs);
return -1;
}
strcpy (reg->name, name);
reg->indirect = indirect;
for (i=0; i<strlen (reg->name); i++) {
if (islower (reg->name[i]))
reg->name[i] = toupper (reg->name[i]);
}
if (device_name) {
reg->device_name = (char *)_panel_malloc (1 + strlen (device_name));
if (reg->device_name == NULL) {
free (reg->name);
panel->State = Error;
free (regs);
return -1;
}
strcpy (reg->device_name, device_name);
for (i=0; i<strlen (reg->device_name); i++) {
if (islower (reg->device_name[i]))
reg->device_name[i] = toupper (reg->device_name[i]);
}
}
for (i=0; i<panel->reg_count; i++) {
char *t1 = (char *)_panel_malloc (2 + strlen (regs[i].name) + (regs[i].device_name? strlen (regs[i].device_name) : 0));
char *t2 = (char *)_panel_malloc (2 + strlen (reg->name) + (reg->device_name? strlen (reg->device_name) : 0));
if ((t1 == NULL) || (t2 == NULL)) {
free (t1);
free (t2);
free (reg->name);
free (reg->device_name);
panel->State = Error;
free (regs);
return -1;
}
sprintf (t1, "%s %s", regs[i].device_name ? regs[i].device_name : "", regs[i].name);
sprintf (t2, "%s %s", reg->device_name ? reg->device_name : "", reg->name);
if ((!strcmp (t1, t2)) && (reg->indirect == regs[i].indirect)) {
sim_panel_set_error ("Duplicate Register Declaration");
free (t1);
free (t2);
free (reg->name);
free (reg->device_name);
free (regs);
return -1;
}
free (t1);
free (t2);
}
reg->addr = addr;
reg->size = size;
reg->element_count = element_count;
pthread_mutex_unlock (&panel->io_lock);
/* Validate existence of requested register/array */
if (_panel_sendf (panel, 1, &response, "EXAMINE %s %s%s\r", device_name? device_name : "", name, (element_count > 0) ? "[0]" : "")) {
free (reg->name);
free (reg->device_name);
free (regs);
return -1;
}
if (!strcmp ("Invalid argument\r\n", response)) {
sim_panel_set_error ("Invalid Register: %s %s", device_name? device_name : "", name);
free (response);
free (reg->name);
free (reg->device_name);
free (regs);
return -1;
}
free (response);
if (element_count > 0) {
if (_panel_sendf (panel, 1, &response, "EXAMINE %s %s[%d]\r", device_name? device_name : "", name, element_count-1)) {
free (reg->name);
free (reg->device_name);
free (regs);
return -1;
}
if (!strcmp ("Subscript out of range\r\n", response)) {
sim_panel_set_error ("Invalid Register Array Dimension: %s %s[%d]", device_name? device_name : "", name, element_count-1);
free (response);
free (reg->name);
free (reg->device_name);
free (regs);
return -1;
}
free (response);
}
pthread_mutex_lock (&panel->io_lock);
++panel->reg_count;
free (panel->regs);
panel->regs = regs;
pthread_mutex_unlock (&panel->io_lock);
/* Now build the register query string for the whole register list */
if (_panel_register_query_string (panel, &panel->reg_query, &panel->reg_query_size))
return -1;
return 0;
}
int
sim_panel_add_register (PANEL *panel,