-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmux6676.c
1231 lines (1152 loc) · 37.2 KB
/
mux6676.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
/*--------------------------------------------------------------------------
**
** Copyright (c) 2003-2011, Tom Hunter
**
** Name: mux6676.c
**
** Description:
** Perform emulation of CDC 6671 and 6676 data set controllers.
** The 6671 was used primarily as a mux for synchronous MODE4
** terminals, and the 6676 was used primarily for asynchronous
** TELEX/IAF terminals.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License version 3 as
** published by the Free Software Foundation.
**
** 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 version 3 for more details.
**
** You should have received a copy of the GNU General Public License
** version 3 along with this program in file "license-gpl-3.0.txt".
** If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
**
**--------------------------------------------------------------------------
*/
#define DEBUG_6671 0
#define DEBUG_6676 0
#define DEBUG_NETIO 0
#define DEBUG_PPIO 0
#define DEBUG_PPIO_VERBOSE 0
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include "const.h"
#include <fcntl.h>
#include "types.h"
#include "proto.h"
#include <sys/types.h>
#include <memory.h>
#if defined(_WIN32)
#include <winsock.h>
#else
#include <pthread.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** Function codes.
*/
#define Fc667xOutput 00001
#define Fc667xStatus 00002
#define Fc667xInput 00003
#define Fc667xEqMask 07000
#define Fc667xEqShift 9
#define St667xServiceFailure 00001
#define St667xInputRequired 00002
#define St667xChannelAReserved 00004
#define IoTurnsPerPoll 4
#define InBufSize 256
#define OutBufSize 16
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
#if DEBUG_6671 || DEBUG_6676
#define HexColumn(x) (3 * (x) + 4)
#define AsciiColumn(x) (HexColumn(16) + 2 + (x))
#define LogLineLength (AsciiColumn(16))
#endif
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
#define MaxPortGroups 16
typedef struct portGroup
{
int listenFd;
int listenPort;
int portIndex;
int portCount;
} PortGroup;
typedef struct portParam
{
struct muxParam *mux;
PortGroup *group;
u8 id;
bool active;
bool enabled;
bool carrierOn;
int connFd;
int inInIdx;
int inOutIdx;
u8 inBuffer[InBufSize];
int outInIdx;
int outOutIdx;
u8 outBuffer[OutBufSize];
} PortParam;
typedef struct muxParam
{
struct muxParam *next;
char name[20];
int type;
u8 channelNo;
u8 eqNo;
int portCount;
int ioTurns;
PortGroup portGroups[MaxPortGroups];
PortParam *ports;
} MuxParam;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static FcStatus mux667xFunc(PpWord funcCode);
static void mux667xActivate(void);
static void mux667xCheckIo(MuxParam *mp);
static void mux667xCreateThread(DevSlot *dp);
static void mux667xClose(PortParam *pp);
static void mux667xDisconnect(void);
static void mux667xInit(u8 eqNo, u8 channelNo, int muxType, char *params);
static void mux667xIo(void);
static bool mux667xInputRequired(MuxParam *mp);
#if DEBUG_6671 || DEBUG_6676
static char *mux667xFunc2String(PpWord funcCode);
static void mux667xLogBytes(FILE *log, MuxParam *mp, u8 *bytes, int len);
static void mux667xLogFlush(FILE *log, MuxParam *mp);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
u16 mux6676TelnetPort;
u16 mux6676TelnetConns;
/*
** -----------------
** Private Variables
** -----------------
*/
MuxParam *firstMux = NULL;
MuxParam *lastMux = NULL;
static char connectingMsg[] = "\r\nConnecting to host - please wait ...";
static char noPortsMsg[] = "\r\nNo free ports available - please try again later.\r\n";
#if DEBUG_6671
static FILE *mux6671Log = NULL;
#endif
#if DEBUG_6676
static FILE *mux6676Log = NULL;
#endif
#if DEBUG_6671 || DEBUG_6676
static char mux667xLogBuf[LogLineLength + 1];
static int mux667xLogBytesCol = 0;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise 6671 terminal multiplexer.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** params optional device parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void mux6671Init(u8 eqNo, u8 unitNo, u8 channelNo, char *params)
{
mux667xInit(eqNo, channelNo, DtMux6671, params);
}
/*--------------------------------------------------------------------------
** Purpose: Initialise 6676 terminal multiplexer.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** params optional device parameters
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void mux6676Init(u8 eqNo, u8 unitNo, u8 channelNo, char *params)
{
mux667xInit(eqNo, channelNo, DtMux6676, params);
}
/*--------------------------------------------------------------------------
** Purpose: Show mux status (operator interface).
**
** Parameters: Name Description.
**
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void mux6676ShowStatus()
{
char *cts;
int g;
int i;
PortGroup *gp;
MuxParam *mp;
char *mts;
char outBuf[200];
PortParam *pp;
for (mp = firstMux; mp != NULL; mp = mp->next)
{
if (mp->type == DtMux6676)
{
mts = "6676";
cts = "async";
}
else
{
mts = "6671";
cts = "mode4";
}
for (g = 0, gp = &mp->portGroups[0]; g < MaxPortGroups && gp->portCount > 0; g++, gp++)
{
if (gp->listenFd > 0)
{
sprintf(outBuf, " > %-8s C%02o E%02o ", mts, mp->channelNo, mp->eqNo);
opDisplay(outBuf);
sprintf(outBuf, FMTNETSTATUS"\n", netGetLocalTcpAddress(gp->listenFd), "", cts, "listening");
opDisplay(outBuf);
for (i = 0, pp = mp->ports + gp->portIndex; i < gp->portCount; i++, pp++)
{
if (pp->active && pp->connFd > 0)
{
sprintf(outBuf, " > %-8s P%02o ", mts, pp->id);
opDisplay(outBuf);
sprintf(outBuf, FMTNETSTATUS"\n", netGetLocalTcpAddress(pp->connFd), netGetPeerTcpAddress(pp->connFd), cts, "connected");
opDisplay(outBuf);
}
}
}
}
}
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise 667x terminal multiplexer.
**
** Parameters: Name Description.
** eqNo equipment number
** channelNo channel number the device is attached to
** muxType mux device type
** params optional device parameters
**
** Returns: Nothing.
**
** The device parameters are a comma-separated list of pairs of TCP port
** numbers and multiplexor port counts. Each pair specifies a TCP port on
** which to listen for connections and a count representing the number of
** consecutive multiplexor ports associated with the TCP port.
**
**------------------------------------------------------------------------*/
static void mux667xInit(u8 eqNo, u8 channelNo, int muxType, char *params)
{
char *cp;
DevSlot *dp;
u8 g;
PortGroup *gp;
u8 i;
int listenPort;
int maxPorts;
MuxParam *mp;
char *mts;
int n;
int numParam;
int portCount;
PortParam *pp;
dp = channelAttach(channelNo, eqNo, muxType);
dp->activate = mux667xActivate;
dp->disconnect = mux667xDisconnect;
dp->func = mux667xFunc;
dp->io = mux667xIo;
if (muxType == DtMux6676)
{
mts = "MUX6676";
maxPorts = 64;
}
else
{
mts = "MUX6671";
maxPorts = 16;
}
/*
** Only one MUX667x unit is possible per equipment.
*/
if (dp->context[0] != NULL)
{
fprintf(stderr, "(mux6676) Only one %s unit is possible per equipment\n", mts);
exit(1);
}
/*
** Allocate and initialise mux control block.
*/
mp = calloc(1, sizeof(MuxParam));
if (mp == NULL)
{
fprintf(stderr, "(mux6676) Failed to allocate %s context block\n", mts);
exit(1);
}
if (firstMux == NULL)
{
firstMux = mp;
}
else
{
lastMux->next = mp;
}
lastMux = mp;
sprintf(mp->name, "%s_CH%02o_EQ%02o", mts, channelNo, eqNo);
mp->type = muxType;
mp->channelNo = channelNo;
mp->eqNo = eqNo;
mp->ioTurns = IoTurnsPerPoll - 1;
if (params == NULL)
{
params = "";
}
cp = params;
g = 0;
while (TRUE)
{
numParam = sscanf(cp, "%d,%d", &listenPort, &portCount);
if (numParam < 1)
{
if (g > 0)
{
break;
}
else if (muxType == DtMux6676)
{
listenPort = mux6676TelnetPort;
portCount = maxPorts;
}
else
{
fprintf(stderr, "(mux6676) TCP port missing from %s definition\n", mts);
exit(1);
}
}
else if (numParam < 2)
{
portCount = maxPorts - mp->portCount;
}
if (listenPort < 0 || listenPort > 65535)
{
fprintf(stderr, "(mux6676) Invalid TCP port number in %s definition: %d\n", mts, listenPort);
exit(1);
}
if (portCount < 1)
{
fprintf(stderr, "(mux6676) Invalid port count %d in %s definition, valid range is 0 < count <= %d\n", portCount, mts, maxPorts);
exit(1);
}
gp = &mp->portGroups[g++];
gp->portIndex = mp->portCount;
gp->portCount = portCount;
gp->listenPort = listenPort;
mp->portCount += portCount;
if (mp->portCount > maxPorts)
{
fprintf(stderr, "(mux6676) Invalid total port count %d in %s definition, valid range is 0 < count <= %d\n", mp->portCount, mts, maxPorts);
exit(1);
}
if (listenPort > 0)
{
/*
** Create socket, bind to specified port, and begin listening for connections
*/
gp->listenFd = netCreateListener(listenPort);
#if defined(_WIN32)
if (gp->listenFd == INVALID_SOCKET)
#else
if (gp->listenFd == -1)
#endif
{
fprintf(stderr, "(mux6676) Can't listen for %s on port %d\n", mts, listenPort);
exit(1);
}
}
/*
** Advance to next port/count pair
*/
while (*cp != '\0' && *cp != ',') cp += 1;
if (*cp == ',') cp += 1;
while (*cp != '\0' && *cp != ',') cp += 1;
if (*cp == ',') cp += 1;
}
dp->context[0] = mp;
/*
** Initialise port control blocks.
*/
mp->ports = (PortParam *)calloc(mp->portCount, sizeof(PortParam));
if (mp->ports == NULL)
{
fprintf(stderr, "(mux6676) Failed to allocate %s context block\n", mts);
exit(1);
}
gp = &mp->portGroups[0];
for (i = 0, pp = mp->ports; i < mp->portCount; i++, pp++)
{
if (i >= gp->portIndex + gp->portCount) gp += 1;
pp->mux = mp;
pp->group = gp;
pp->enabled = mp->type == DtMux6676; // enabled by default for 6676
pp->carrierOn = mp->type == DtMux6676; // on by default for 6676
pp->active = FALSE;
pp->connFd = 0;
pp->id = i;
}
/*
** Print a friendly message.
*/
printf("(mux6676) %s initialised on channel %o equipment %o, TCP port/mux ports: ",
mts, channelNo, eqNo);
n = 0;
for (g = 0, gp = &mp->portGroups[0]; g < MaxPortGroups && gp->portCount > 0; g++, gp++)
{
if (gp->listenPort != 0)
{
if (n++ > 0) fputs(", ", stdout);
printf("%d/%d", gp->listenPort, gp->portIndex);
if (gp->portCount > 1)
{
printf("-%d", gp->portIndex + gp->portCount - 1);
}
}
}
fputc('\n', stdout);
#if DEBUG_6671
if (mux6671Log == NULL)
{
mux6671Log = fopen("mux6671log.txt", "wt");
}
#endif
#if DEBUG_6676
if (mux6676Log == NULL)
{
mux6676Log = fopen("mux6676log.txt", "wt");
}
#endif
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on 667x mux.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus mux667xFunc(PpWord funcCode)
{
u8 eqNo;
MuxParam *mp = (MuxParam *)activeDevice->context[0];
#if DEBUG_PPIO
#if DEBUG_6671
if ((mp->type == DtMux6671) && (DEBUG_PPIO_VERBOSE != 0))
{
fprintf(mux6671Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, funcCode, mux667xFunc2String(funcCode));
}
#endif
#if DEBUG_6676
if ((mp->type == DtMux6676) && (DEBUG_PPIO_VERBOSE != 0))
{
fprintf(mux6676Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, funcCode, mux667xFunc2String(funcCode));
}
#endif
#endif
eqNo = (funcCode & Fc667xEqMask) >> Fc667xEqShift;
if (eqNo != activeDevice->eqNo)
{
/*
** Equipment not configured.
*/
return (FcDeclined);
}
funcCode &= ~Fc667xEqMask;
switch (funcCode)
{
default:
#if DEBUG_PPIO
#if DEBUG_6671
if ((mp->type == DtMux6671) && (DEBUG_PPIO_VERBOSE != 0))
{
fputs("\n FUNC not implemented & declined!", mux6671Log);
}
#endif
#if DEBUG_6676
if ((mp->type == DtMux6676) && (DEBUG_PPIO_VERBOSE != 0))
{
fputs("\n FUNC not implemented & declined!", mux6676Log);
}
#endif
#endif
return (FcDeclined);
case Fc667xOutput:
case Fc667xStatus:
case Fc667xInput:
activeDevice->recordLength = 0;
break;
}
activeDevice->fcode = funcCode;
return (FcAccepted);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on 667x mux.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mux667xIo(void)
{
PpWord function;
u8 in;
MuxParam *mp = (MuxParam *)activeDevice->context[0];
PortParam *pp;
u8 portNumber;
PpWord word;
u8 x;
mux667xCheckIo(mp);
switch (activeDevice->fcode)
{
default:
break;
case Fc667xOutput:
if (activeChannel->full)
{
/*
** Output data.
*/
activeChannel->full = FALSE;
portNumber = (u8)activeDevice->recordLength++;
if (portNumber < mp->portCount)
{
word = activeChannel->data;
function = word >> 9;
#if DEBUG_PPIO
#if DEBUG_6671
if (mp->type == DtMux6671)
{
fprintf(mux6671Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s Port:%02o Data:%04o",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, activeDevice->fcode, mux667xFunc2String(activeDevice->fcode), portNumber, word);
}
#endif
#if DEBUG_6676
if (mp->type == DtMux6676)
{
fprintf(mux6676Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s Port:%02o Data:%04o",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, activeDevice->fcode, mux667xFunc2String(activeDevice->fcode), portNumber, word);
}
#endif
#endif
pp = mp->ports + portNumber;
if (pp->active)
{
/*
** Port with active TCP connection.
*/
switch (function)
{
case 2:
case 3:
if (pp->mux->type == DtMux6671)
{
pp->carrierOn = FALSE;
}
break;
case 5:
if (pp->mux->type != DtMux6671)
{
break;
}
// fall through if 6671
case 4:
if (mp->type == DtMux6676)
{
x = (word >> 1) & 0x7f; // send data with parity stripped off
}
else
{
pp->carrierOn = TRUE;
x = word & 0xff;
}
if (pp->outInIdx < OutBufSize)
{
pp->outBuffer[pp->outInIdx++] = x;
}
#if DEBUG_NETIO
#if DEBUG_6671
else if (pp->mux->type == DtMux6671)
{
fprintf(mux6671Log, "\n%010u %s output buffer overflow on port %02o",
traceSequenceNo, pp->mux->name, pp->id);
}
#endif
#if DEBUG_6676
else if (pp->mux->type == DtMux6676)
{
fprintf(mux6676Log, "\n%010u %s output buffer overflow on port %02o",
traceSequenceNo, pp->mux->name, pp->id);
}
#endif
#endif
break;
case 6:
/*
** Disconnect.
*/
mux667xClose(pp);
break;
case 7:
/*
** Enable.
*/
pp->enabled = TRUE;
break;
default:
break;
}
}
else if ((pp->mux->type == DtMux6671) && (function == 7))
{
pp->enabled = TRUE;
}
}
}
break;
case Fc667xInput:
if (!activeChannel->full)
{
activeChannel->data = 0;
activeChannel->full = TRUE;
portNumber = (u8)activeDevice->recordLength++;
if (portNumber < mp->portCount)
{
pp = mp->ports + portNumber;
if (pp->active)
{
/*
** Port with active TCP connection.
*/
activeChannel->data |= 01000;
if (pp->inOutIdx < pp->inInIdx)
{
in = pp->inBuffer[pp->inOutIdx++];
if (pp->inOutIdx >= pp->inInIdx)
{
pp->inInIdx = 0;
pp->inOutIdx = 0;
}
if (mp->type == DtMux6676)
{
activeChannel->data |= ((in & 0x7F) << 1) | 04000;
}
else
{
activeChannel->data |= in | 04000;
}
}
}
#if DEBUG_PPIO
if ((DEBUG_PPIO_VERBOSE != 0) || ((activeChannel->data & 04000) != 0))
{
#if DEBUG_6671
if (mp->type == DtMux6671)
{
fprintf(mux6671Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s Port:%02o Data:%04o",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, activeDevice->fcode, mux667xFunc2String(activeDevice->fcode), portNumber, activeChannel->data);
}
#endif
#if DEBUG_6676
if (mp->type == DtMux6676)
{
fprintf(mux6676Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s Port:%02o Data:%04o",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, activeDevice->fcode, mux667xFunc2String(activeDevice->fcode), portNumber, activeChannel->data);
}
#endif
}
#endif
}
}
break;
case Fc667xStatus:
activeChannel->data = St667xChannelAReserved;
if (mux667xInputRequired(mp))
{
activeChannel->data |= St667xInputRequired;
}
activeChannel->full = TRUE;
#if DEBUG_PPIO
#if DEBUG_6671
if (mp->type == DtMux6671)
{
fprintf(mux6671Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s Data:%04o",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, activeDevice->fcode, mux667xFunc2String(activeDevice->fcode), activeChannel->data);
}
#endif
#if DEBUG_6676
if (mp->type == DtMux6676)
{
fprintf(mux6676Log, "\n%010u %s PP:%02o CH:%02o P:%04o f:%04o T:%s Data:%04o",
traceSequenceNo, mp->name, activePpu->id, activeDevice->channel->id,
activePpu->regP, activeDevice->fcode, mux667xFunc2String(activeDevice->fcode), activeChannel->data);
}
#endif
#endif
break;
}
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mux667xActivate(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mux667xDisconnect(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Check for I/O availability.
**
** Parameters: Name Description.
** mp pointer to mux parameters.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mux667xCheckIo(MuxParam *mp)
{
PortParam *availablePort;
#if defined(_WIN32)
u_long blockEnable = 1;
#endif
struct sockaddr_in from;
#if defined(_WIN32)
int fromLen;
#else
socklen_t fromLen;
#endif
int fd;
int g;
PortGroup *gp;
int i;
int maxFd;
int n;
int optEnable = 1;
PortParam *pp;
fd_set readFds;
struct timeval timeout;
fd_set writeFds;
mp->ioTurns = (mp->ioTurns + 1) % IoTurnsPerPoll;
if (mp->ioTurns != 0)
{
return;
}
FD_ZERO(&readFds);
FD_ZERO(&writeFds);
maxFd = 0;
for (i = 0, pp = mp->ports; i < mp->portCount; i++, pp++)
{
if (pp->active)
{
if (pp->inInIdx < InBufSize)
{
FD_SET(pp->connFd, &readFds);
if (pp->connFd > maxFd)
{
maxFd = pp->connFd;
}
}
if (pp->carrierOn)
{
if (pp->outInIdx > pp->outOutIdx)
{
FD_SET(pp->connFd, &writeFds);
if (pp->connFd > maxFd)
{
maxFd = pp->connFd;
}
}
}
}
else if (pp->enabled && pp->group->listenFd != 0 && !FD_ISSET(pp->group->listenFd, &readFds))
{
FD_SET(pp->group->listenFd, &readFds);
if (pp->group->listenFd > maxFd)
{
maxFd = pp->group->listenFd;
}
}
}
if (maxFd < 1)
{
return;
}
timeout.tv_sec = 0;
timeout.tv_usec = 0;
n = select(maxFd + 1, &readFds, &writeFds, NULL, &timeout);
if (n < 1)
{
return;
}
for (i = 0, pp = mp->ports; i < mp->portCount; i++, pp++)
{
if (pp->active)
{
if (FD_ISSET(pp->connFd, &readFds))
{
n = recv(pp->connFd, &pp->inBuffer[pp->inInIdx], InBufSize - pp->inInIdx, 0);
if (n > 0)
{
#if DEBUG_NETIO
#if DEBUG_6671
if (pp->mux->type == DtMux6671)
{
fprintf(mux6671Log, "\n%010u %s received %d bytes on port %02o",
traceSequenceNo, mp->name, n, pp->id);
mux667xLogBytes(mux6671Log, mp, &pp->inBuffer[pp->inInIdx], n);
}
#endif
#if DEBUG_6676
if (pp->mux->type == DtMux6676)
{
fprintf(mux6676Log, "\n%010u %s received %d bytes on port %02o",
traceSequenceNo, mp->name, n, pp->id);
mux667xLogBytes(mux6676Log, mp, &pp->inBuffer[pp->inInIdx], n);
}
#endif
#endif
pp->inInIdx += n;
}
else
{
mux667xClose(pp);
}
}
if (FD_ISSET(pp->connFd, &writeFds) && (pp->outOutIdx < pp->outInIdx))
{
n = send(pp->connFd, &pp->outBuffer[pp->outOutIdx], pp->outInIdx - pp->outOutIdx, 0);
if (n >= 0)
{
#if DEBUG_NETIO
#if DEBUG_6671
if (pp->mux->type == DtMux6671)
{
fprintf(mux6671Log, "\n%010u %s sent %d bytes to port %02o",
traceSequenceNo, mp->name, n, pp->id);
mux667xLogBytes(mux6671Log, mp, &pp->outBuffer[pp->outOutIdx], n);
}
#endif
#if DEBUG_6676
if (pp->mux->type == DtMux6676)
{
fprintf(mux6676Log, "\n%010u %s sent %d bytes to port %02o",
traceSequenceNo, mp->name, n, pp->id);
mux667xLogBytes(mux6676Log, mp, &pp->outBuffer[pp->outOutIdx], n);
}
#endif
#endif
pp->outOutIdx += n;
if (pp->outOutIdx >= pp->outInIdx)
{
pp->outInIdx = 0;
pp->outOutIdx = 0;
}
}
}
}
}
for (g = 0, gp = &mp->portGroups[0]; g < MaxPortGroups && gp->portCount > 0; g++, gp++)
{
if (gp->listenFd != 0 && FD_ISSET(gp->listenFd, &readFds))
{
fromLen = sizeof(from);
fd = accept(gp->listenFd, (struct sockaddr *)&from, &fromLen);
if (fd < 0) break;
availablePort = NULL;
for (i = gp->portIndex; i < gp->portIndex + gp->portCount; i++)
{
pp = mp->ports + i;