-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmdi.c
executable file
·1476 lines (1269 loc) · 38.2 KB
/
mdi.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-2019, Kevin Jordan, Tom Hunter
**
** Name: mdi.c
**
** Author: Kevin Jordan
**
** Description:
** Perform emulation of the Host Interface Protocol in a CDCNet MDI.
**
** 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>.
**
**--------------------------------------------------------------------------
*/
/*
** If this is set to 1, the DEBUG flag in npu_hip.c must also be set to 1.
*/
#define DEBUG 0
/*
** -------------
** Include Files
** -------------
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <time.h>
#if defined(__FreeBSD__)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#include "const.h"
#include "types.h"
#include "proto.h"
#include "npu.h"
#if defined(__APPLE__)
#include <execinfo.h>
#endif
/*
** -----------------
** Private Constants
** -----------------
*/
/*
** Direct function codes
*/
#define FcMdiMasterClear 0400
#define FcMdiReqGeneralStatus 0410
#define FcMdiWriteData 0420
#define FcMdiReadData 0430
/*
** Transparent function codes
*/
#define FcMdiReqDetailedStatus 00001
#define FcMdiReadError 00003
#define FcMdiIfcReset 00004
#define FcMdiStartReg 00005
#define FcMdiStopReg 00006
#define FcMdiReqDiagnostics 00007
#define FcMdiSetProtoVersion 00032
#define FcMdiDiagEchoTimeout 00040
#define FcMdiDiagReadError 00041
#define FcMdiNormalOperation 00042
#define FcMdiNormalFlowCtrlOn 00043
#define FcMdiNormalFlowCtrlOff 00044
#define FcMdiReqProtoVersion 00200
#define FcMdiEqMask 07000
/*
** MDI status bit masks
*/
#define MdiStatusError 04000
#define MdiStatusMemoryError 02000
#define MdiStatusDataAvailable 01000
#define MdiStatusAcceptingData 00400
#define MdiStatusBusy 00200
#define MdiStatusOperational 00100
/*
** State values when MDI is not operational
*/
#define MdiStateMdiReset 000
#define MdiStateDiagnostics 010
#define MdiStateStarting 030
#define MdiStateInputAvailable 030
#define MdiStateLoading 040
#define MdiStateMciReset 050
#define MdiStateClosed 060
#define MdiStateDown 070
/*
** Input available values when MDI is operational
*/
#define MdiIvtInputLe256 000
#define MdiIvtInputGt256 010
#define MdiPruOne 020
#define MdiPruTwo 030
#define MdiPruThree 040
#define MdiInlineDiagnostics 050
/*
** MDI global flow control flags
*/
#define MdiFlowControlOff 0
#define MdiFlowControlOn 1
/*
** MDI protocol version
*/
#define MdiProtocolVersion 4
/*
** MDI header
*/
#define MdiHdrOffDstAddr 0
#define MdiHdrOffSrcAddr 6
#define MdiHdrOffBlockLen 12
#define MdiHdrOffDstSAP 14
#define MdiHdrOffSrcSAP 15
#define MdiHdrOffControl 16
#define MdiHdrOffAlignBytes 17
#define MdiHdrLen 19
/*
** MDI I/O word state
*/
#define MdiIoStateEvenWord 0
#define MdiIoStateOddWord 1
/*
** -----------------------
** Private Macro Functions
** -----------------------
*/
#if DEBUG
#define HexColumn(x) (4 * (x) + 1 + 4)
#define AsciiColumn(x) (HexColumn(16) + 2 + (x))
#define LogLineLength (AsciiColumn(24))
#endif
/*
** -----------------------------------------
** Private Typedef and Structure Definitions
** -----------------------------------------
*/
#define MdiMaxBuffer 3000
typedef struct mdiBuffer
{
u16 offset;
u16 numBytes;
u8 blockSeqNo;
u8 data[MdiMaxBuffer];
} MdiBuffer;
typedef struct mdiParam
{
u8 wordState;
u8 headerIndex;
u8 header[MdiHdrLen];
u32 parcel;
time_t svDeadline;
NpuBuffer *uplineData;
MdiBuffer downlineData;
} MdiParam;
/*
** ---------------------------
** Private Function Prototypes
** ---------------------------
*/
static void mdiReset(void);
static FcStatus mdiHipFunc(PpWord funcCode);
static void mdiHipIo(void);
static void mdiHipActivate(void);
static void mdiHipDisconnect(void);
static PpWord mdiHipReadMdiStatus(void);
static bool mdiHipDownlineBlockImpl(NpuBuffer *bp);
static bool mdiHipUplineBlockImpl(NpuBuffer *bp);
static char *mdiHipFunc2String(PpWord funcCode);
#if DEBUG
static void mdiLogBuffer(u8 *dp);
static void mdiLogBytes(int b);
static void mdiLogFlush(void);
static void mdiLogPpWord(int b);
static char *mdiPfc2String(u8 pfc);
static char *mdiSfc2String(u8 sfc);
#endif
#if defined(__APPLE__)
static void mdiPrintStackTrace(FILE *fp);
#endif
/*
** ----------------
** Public Variables
** ----------------
*/
extern bool (*npuHipDownlineBlockFunc)(NpuBuffer *bp);
extern void (*npuHipResetFunc)(void);
extern bool (*npuHipUplineBlockFunc)(NpuBuffer *bp);
#if DEBUG
extern FILE *npuLog;
#endif
/*
** -----------------
** Private Variables
** -----------------
*/
static enum
{
StMdiStarting,
StMdiSendRegLevel,
StMdiOperational
}
mdiState = StMdiStarting;
static MdiParam *mdi;
static u8 detailedStartingResponse[] =
{
0x05, // channel protocol version
0x07, // slot number
0xE4, 0x31, // system version
0x08, 0x00, 0x25, 0x01, 0x01, 0x01, // system ID
0x02, // last I/O operation
0x84, // last transparent function
0x01, 0x10, // last PPU function
0x01, 0x18, // last but one PPU function
0x00, 0x00, // summary flags and MCI channel status
0x00, // MCI status register one
0x02, // MCI status register three
0x00, 0x40, // software status flags
0x00, 0x00, 0x08, 0x98, // maximum PDU size
0x89 // not used, padding to make whole 24-bit parcel
};
static u8 detailedOperationalResponse[] =
{
0x04, // channel protocol version
0x07, // slot number
0xE4, 0x31, // system version
0x08, 0x00, 0x25, 0x01, 0x01, 0x01, // system ID
0x01, // last I/O operation
0x84, // last transparent function
0x00, 0x84, // last PPU function
0x01, 0x18, // last but one PPU function
0x00, 0x00, // summary flags and MCI channel status
0x00, // MCI status register one
0x81, // MCI status register three
0x00, 0x40, // software status flags
0x00, 0x00, 0x08, 0x98, // maximum PDU size
0x89 // not used, padding to make whole 24-bit parcel
};
static u8 mdiRegLevelIndication[] =
{
0x00, // DN
0x00, // SN
0x00, // CN
0x84, // high prio service message
0x01, // PFC (logical link)
0x01, // SFC (logical link)
0x07, // CS, regulation level
0x00 // unused, padding
};
static u8 mdiSupervisionRequest[] =
{
0x00, // DN
0x00, // SN
0x00, // CN
0x84, // high prio service message
0x0E, // PFC (supervise)
0x0A, // SFC (initiate supervision)
0x00, // PS
0x00, // PL
0x00, // RI
0x00, 0x00, 0x00, // not used
0x03, // CCP version
0x01, // ...
0x00, // CCP level
0x00, // ...
0x00, // CCP cycle or variant
0x00, // ...
0x00, // not used
0x00, 0x00 // NCF version in NDL file (ignored)
};
#if DEBUG
static char mdiLogBuf[LogLineLength + 1];
static int mdiLogBytesCol = 0;
static int mdiLogWordCol = 0;
#endif
/*
**--------------------------------------------------------------------------
**
** Public Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Initialise MDI.
**
** Parameters: Name Description.
** eqNo equipment number
** unitNo unit number
** channelNo channel number the device is attached to
** deviceName optional device file name
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
void mdiInit(u8 eqNo, u8 unitNo, u8 channelNo, char *deviceName)
{
DevSlot *dp;
#if DEBUG
if (npuLog == NULL)
{
npuLog = fopen("mdilog.txt", "wt");
}
#endif
/*
** set HCP software type, exit if npuSw is not SwUndefined
*/
if (npuSw != SwUndefined)
{
fprintf(stderr, "(cci_hip) CCP and CCI devices are mutually exclusive\n");
exit(1);
}
npuSw = SwCCP;
/*
** Attach device to channel and initialise device control block.
*/
dp = channelAttach(channelNo, eqNo, DtMdi);
dp->activate = mdiHipActivate;
dp->disconnect = mdiHipDisconnect;
dp->func = mdiHipFunc;
dp->io = mdiHipIo;
dp->selectedUnit = unitNo;
activeDevice = dp;
/*
** Allocate and initialise MDI parameters.
*/
mdi = calloc(1, sizeof(MdiParam));
if (mdi == NULL)
{
fprintf(stderr, "Failed to allocate mdi context block\n");
exit(1);
}
dp->controllerContext = mdi;
npuHipDownlineBlockFunc = mdiHipDownlineBlockImpl;
npuHipResetFunc = mdiReset;
npuHipUplineBlockFunc = mdiHipUplineBlockImpl;
/*
** Initialize node numbers in upline canned messages
*/
mdiSupervisionRequest[BlkOffDN] = npuSvmCouplerNode;
mdiSupervisionRequest[BlkOffSN] = npuSvmNpuNode;
/*
** Initialise BIP, SVC, TIP, and LIP.
*/
npuBipInit();
npuSvmInit();
npuTipInit();
mdiState = StMdiStarting;
/*
** Print a friendly message.
*/
printf("(mdi ) MDI initialised on channel %o equipment %o\n", channelNo, eqNo);
printf(" Host ID: %s\n", npuNetHostID);
printf("(mdi ) Coupler node: %u\n", npuSvmCouplerNode);
printf(" MDI node: %u\n", npuSvmNpuNode);
}
/*--------------------------------------------------------------------------
** Purpose: Request sending of upline block.
**
** Parameters: Name Description.
** bp pointer to first upline buffer.
**
** Returns: TRUE if buffer can be accepted, FALSE otherwise.
**
**------------------------------------------------------------------------*/
bool mdiHipUplineBlockImpl(NpuBuffer *bp)
{
if (mdi->uplineData != NULL)
{
#if DEBUG
if ((bp != mdi->uplineData)
|| (bp->data[BlkOffCN] != mdi->uplineData->data[BlkOffCN]))
{
fprintf(stderr, "(mdi ) MDI upline block rejected, CN=%02X, BT=%02X, PDU size=%d\n", bp->data[BlkOffCN],
bp->data[BlkOffBTBSN] & BlkMaskBT, bp->numBytes);
mdiPrintStackTrace(stderr);
}
#endif
return (FALSE);
}
mdi->uplineData = bp;
return (TRUE);
}
/*--------------------------------------------------------------------------
** Purpose: Request reception of downline block.
**
** Parameters: Name Description.
** bp pointer to first downline buffer.
**
** Returns: TRUE if buffer can be accepted, FALSE otherwise.
**
**------------------------------------------------------------------------*/
bool mdiHipDownlineBlockImpl(NpuBuffer *bp)
{
if ((bp == NULL) || (mdi->downlineData.numBytes < 1))
{
return (FALSE);
}
bp->offset = 0;
bp->numBytes = mdi->downlineData.numBytes;
bp->blockSeqNo = mdi->downlineData.blockSeqNo;
memcpy(bp->data, mdi->downlineData.data, mdi->downlineData.numBytes);
return (TRUE);
}
/*
**--------------------------------------------------------------------------
**
** Private Functions
**
**--------------------------------------------------------------------------
*/
/*--------------------------------------------------------------------------
** Purpose: Reset MDI.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mdiReset(void)
{
/*
** Reset all subsystems - order matters!
*/
cdcnetReset();
npuNetReset();
npuTipReset();
npuSvmReset();
npuBipReset();
/*
** Reset HIP state.
*/
memset(mdi, 0, sizeof(MdiParam));
mdiState = StMdiStarting;
}
/*--------------------------------------------------------------------------
** Purpose: Execute function code on MDI.
**
** Parameters: Name Description.
** funcCode function code
**
** Returns: FcStatus
**
**------------------------------------------------------------------------*/
static FcStatus mdiHipFunc(PpWord funcCode)
{
time_t currentTime;
MdiBuffer *mbp;
NpuBuffer *nbp;
u16 numBytes;
funcCode &= ~FcMdiEqMask;
#if DEBUG
mdiLogFlush();
fprintf(npuLog, "\n%06d PP:%02o CH:%02o f:%04o T:%-25s > ",
traceSequenceNo,
activePpu->id,
activeChannel->id,
funcCode,
mdiHipFunc2String(funcCode));
#endif
switch (funcCode)
{
default:
if ((funcCode >= FcMdiReqProtoVersion) && (funcCode <= FcMdiReqProtoVersion + 0177))
{
mdiState = StMdiSendRegLevel;
}
else
{
#if DEBUG
fprintf(npuLog, " FUNC not implemented & declined!");
#endif
return (FcDeclined);
}
break;
case FcMdiReqGeneralStatus:
currentTime = getSeconds();
if (mdiState == StMdiSendRegLevel)
{
npuBipRequestUplineCanned(mdiRegLevelIndication, sizeof(mdiRegLevelIndication));
mdi->svDeadline = currentTime + (time_t)10; // allow 10 seconds for supervision
mdiState = StMdiOperational;
}
else
{
if ((mdiState == StMdiOperational) && !npuSvmIsReady() && (currentTime >= mdi->svDeadline))
{
npuLogMessage("Supervision timeout");
npuBipRequestUplineCanned(mdiSupervisionRequest, sizeof(mdiSupervisionRequest));
mdi->svDeadline = currentTime + (time_t)5;
}
/*
** Poll network status.
*/
npuNetCheckStatus();
cdcnetCheckStatus();
}
break;
case FcMdiReqDetailedStatus:
mdi->headerIndex = 0;
mdi->wordState = MdiIoStateEvenWord;
mdi->parcel = 0;
activeDevice->recordLength = sizeof(detailedOperationalResponse);
break;
case FcMdiReadData:
nbp = mdi->uplineData;
if (nbp == NULL)
{
/*
** Unexpected input request by host.
*/
activeDevice->recordLength = 0;
activeDevice->fcode = 0;
return (FcDeclined);
}
numBytes = nbp->numBytes + (MdiHdrLen - MdiHdrOffDstSAP);
nbp->offset = 0;
mdi->headerIndex = 0;
mdi->header[MdiHdrOffBlockLen] = numBytes >> 8;
mdi->header[MdiHdrOffBlockLen + 1] = numBytes & 0xff;
mdi->wordState = MdiIoStateEvenWord;
mdi->parcel = 0;
activeDevice->recordLength = MdiHdrLen + nbp->numBytes;
break;
case FcMdiWriteData:
if (mdi->downlineData.numBytes > 0)
{
/*
** Unexpected output request by host.
*/
activeDevice->recordLength = 0;
activeDevice->fcode = 0;
return (FcDeclined);
}
mdi->downlineData.offset = 0;
mdi->headerIndex = 0;
mdi->header[MdiHdrOffBlockLen] = 0;
mdi->header[MdiHdrOffBlockLen + 1] = 0;
mdi->wordState = MdiIoStateEvenWord;
mdi->parcel = 0;
mbp = &mdi->downlineData;
mbp->offset = mbp->numBytes = 0;
activeDevice->recordLength = 0;
break;
case FcMdiMasterClear:
mdiReset();
break;
/*
** The functions below are not supported and are implemented as dummies.
*/
case FcMdiReadError:
case FcMdiStartReg:
case FcMdiStopReg:
case FcMdiReqDiagnostics:
case FcMdiDiagEchoTimeout:
case FcMdiDiagReadError:
break;
case FcMdiSetProtoVersion:
case FcMdiIfcReset:
case FcMdiNormalOperation:
case FcMdiNormalFlowCtrlOn:
case FcMdiNormalFlowCtrlOff:
case FcMdiReqProtoVersion:
return (FcProcessed);
}
activeDevice->fcode = funcCode;
return (FcAccepted);
}
/*--------------------------------------------------------------------------
** Purpose: Perform I/O on MDI.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mdiHipIo(void)
{
int i;
MdiBuffer *mbp;
NpuBuffer *nbp;
int shift;
u8 *u8p;
switch (activeDevice->fcode)
{
default:
break;
case FcMdiReqGeneralStatus:
activeChannel->data = mdiHipReadMdiStatus();
activeChannel->full = TRUE;
#if DEBUG
fprintf(npuLog, " %03X", activeChannel->data);
#endif
break;
case FcMdiReqDetailedStatus:
if (activeChannel->full || (activeDevice->recordLength < 1))
{
break;
}
if (mdi->wordState == MdiIoStateEvenWord)
{
mdi->parcel = 0;
u8p = (mdiState == StMdiStarting) ? detailedStartingResponse : detailedOperationalResponse;
for (i = 0; i < 3; i++)
{
mdi->parcel <<= 8;
mdi->parcel |= u8p[mdi->headerIndex++];
}
activeChannel->data = (PpWord)(mdi->parcel >> 12);
mdi->wordState = MdiIoStateOddWord;
}
else
{
activeChannel->data = mdi->parcel & 0xfff;
mdi->wordState = MdiIoStateEvenWord;
activeDevice->recordLength = (activeDevice->recordLength > 2) ? activeDevice->recordLength - 3 : 0;
}
activeChannel->full = TRUE;
#if DEBUG
mdiLogPpWord(activeChannel->data);
if (mdi->wordState == MdiIoStateEvenWord)
{
mdiLogBytes(mdi->parcel);
}
#endif
if (activeDevice->recordLength < 1)
{
/*
** Transmission complete.
*/
activeChannel->discAfterInput = TRUE;
activeDevice->fcode = 0;
mdi->uplineData = NULL;
npuBipNotifyUplineSent();
}
break;
case FcMdiReadData:
nbp = mdi->uplineData;
if (activeChannel->full || (nbp == NULL) || (activeDevice->recordLength < 1))
{
break;
}
if (mdi->wordState == MdiIoStateEvenWord)
{
mdi->parcel = 0;
for (i = 0; i < 3; i++)
{
mdi->parcel <<= 8;
if (mdi->headerIndex < MdiHdrLen)
{
mdi->parcel |= mdi->header[mdi->headerIndex++];
}
else if (nbp->offset < nbp->numBytes)
{
mdi->parcel |= nbp->data[nbp->offset++];
}
}
activeChannel->data = (PpWord)(mdi->parcel >> 12);
mdi->wordState = MdiIoStateOddWord;
}
else
{
activeChannel->data = mdi->parcel & 0xfff;
mdi->wordState = MdiIoStateEvenWord;
activeDevice->recordLength = (activeDevice->recordLength > 2) ? activeDevice->recordLength - 3 : 0;
}
activeChannel->full = TRUE;
#if DEBUG
mdiLogPpWord(activeChannel->data);
if (mdi->wordState == MdiIoStateEvenWord)
{
mdiLogBytes(mdi->parcel);
}
#endif
if (activeDevice->recordLength < 1)
{
/*
** Transmission complete.
*/
#if DEBUG
mdiLogFlush();
mdiLogBuffer(mdi->uplineData->data);
fprintf(npuLog, " PDU size=%d\n", mdi->uplineData->numBytes);
#endif
activeChannel->discAfterInput = TRUE;
activeDevice->fcode = 0;
mdi->uplineData = NULL;
npuBipNotifyUplineSent();
}
break;
case FcMdiWriteData:
if (activeChannel->full)
{
activeChannel->full = FALSE;
if (mdi->wordState == MdiIoStateEvenWord)
{
mdi->parcel = activeChannel->data << 12;
mdi->wordState = MdiIoStateOddWord;
}
else
{
mdi->parcel |= activeChannel->data;
mdi->wordState = MdiIoStateEvenWord;
mbp = &mdi->downlineData;
for (i = 0, shift = 16; i < 3; shift -= 8, i++)
{
if (mdi->headerIndex < MdiHdrLen)
{
mdi->headerIndex += 1;
activeDevice->recordLength += 1;
}
else if (mbp->numBytes < MdiMaxBuffer)
{
mbp->data[mbp->numBytes++] = (mdi->parcel >> shift) & 0xff;
activeDevice->recordLength += 1;
}
}
}
#if DEBUG
mdiLogPpWord(activeChannel->data);
if (mdi->wordState == MdiIoStateEvenWord)
{
mdiLogBytes(mdi->parcel);
}
#endif
}
break;
}
}
/*--------------------------------------------------------------------------
** Purpose: Handle channel activation.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mdiHipActivate(void)
{
}
/*--------------------------------------------------------------------------
** Purpose: Handle disconnecting of channel.
**
** Parameters: Name Description.
**
** Returns: Nothing.
**
**------------------------------------------------------------------------*/
static void mdiHipDisconnect(void)
{
u8 blockType;
u8 byte;
int i;
MdiBuffer *mbp;
u8 prio;
int shift;
// on output, marks end of block
if (activeDevice->fcode == FcMdiWriteData)
{
mbp = &mdi->downlineData;
if (mdi->wordState == MdiIoStateOddWord)
{
for (i = 0, shift = 16; i < 2; shift -= 8, i++)
{
if (mdi->headerIndex < MdiHdrLen)
{
mdi->headerIndex += 1;
}
else if (mbp->numBytes < MdiMaxBuffer)
{
mbp->data[mbp->numBytes++] = (mdi->parcel >> shift) & 0xff;
activeDevice->recordLength += 1;
}
}
#if DEBUG
mdiLogBytes(mdi->parcel);
#endif
}
#if DEBUG
mdiLogFlush();
mdiLogBuffer(mbp->data);
#endif
if (mbp->numBytes >= 2)
{
/*
** The last two bytes transmitted by PIP provide the true message length including
** the 19-byte MDI header.
*/
mbp->numBytes = ((mbp->data[mbp->numBytes - 2] << 8) | mbp->data[mbp->numBytes - 1]) - MdiHdrLen;
#if DEBUG
fprintf(npuLog, " PDU size=%d\n", mbp->numBytes);
#endif
}
activeDevice->fcode = 0;
byte = mbp->data[BlkOffBTBSN];
blockType = byte & BlkMaskBT;
prio = (byte >> BlkShiftPRIO) & BlkMaskPRIO;
mbp->blockSeqNo = (byte >> BlkShiftBSN) & BlkMaskBSN;
if ((blockType == BtHTCMD) && (mbp->data[BlkOffCN] == 0))
{
if (mbp->data[BlkOffPfc] == 0x01) // Link regulation
{
npuSvmNotifyHostRegulation(3 | 0x04);
}
else
{
npuBipNotifyServiceMessage();
npuBipNotifyDownlineReceived();
}
}
else
{
npuBipNotifyData(prio);
npuBipNotifyDownlineReceived();
}
mbp->offset = mbp->numBytes = 0;
}
}
/*--------------------------------------------------------------------------
** Purpose: PP reads MDI status register.
**
** Parameters: Name Description.
**
** Returns: MDI status register value.
**
**------------------------------------------------------------------------*/
static PpWord mdiHipReadMdiStatus(void)
{
int bits;
NpuBuffer *bp;
PpWord mdiStatus;
int prus;
int words;
if (mdiState == StMdiStarting)
{
return (MdiStateStarting);
}
mdiStatus = MdiStatusOperational;
if (mdi->downlineData.numBytes < 1)
{
mdiStatus |= MdiStatusAcceptingData;
}
if (mdi->uplineData != NULL)
{
mdiStatus |= MdiStatusDataAvailable;
bp = mdi->uplineData;
if ((bp->numBytes > BlkOffDbc)
&& ((bp->data[BlkOffBTBSN] & BlkMaskBT) == BtHTMSG)
&& ((bp->data[BlkOffDbc] & DbcPRU) == DbcPRU))
{
bits = (bp->numBytes - (BlkOffDbc + 1)) * (((bp->data[BlkOffDbc] & Dbc8Bit) != 0) ? 8 : 6);
words = (bits / 60);
if (bits % 60)
{
words++;
}
prus = words / 64;
if (words % 64)
{
prus++;
}
if (prus < 1)
{
prus = 1;
}
if (prus < 2)
{
mdiStatus |= MdiPruOne;
}
else if (prus < 3)
{
mdiStatus |= MdiPruTwo;
}
else
{
mdiStatus |= MdiPruThree;
}
}
else if (bp->numBytes <= 256)
{
mdiStatus |= MdiIvtInputLe256;
}
else