This repository was archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPresmasterSwitcherExtendedMessage.pas
More file actions
203 lines (175 loc) · 5.9 KB
/
PresmasterSwitcherExtendedMessage.pas
File metadata and controls
203 lines (175 loc) · 5.9 KB
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
unit PresmasterSwitcherExtendedMessage;
interface
uses
SysUtils,
Classes,
UMath,
UStreamUtilities,
PresmasterSwitcherMessage;
type
TPresmasterSwitcherExtendedMessage = class(TPresmasterMessage)
public
procedure ReadSpecific(const aStream: TStream); overload; override;
procedure WriteSpecific(const aStream: TStream); overload; override;
public const
LogDebugChecksumVerification = true;
public type
EChecksumIncorrect = class(Exception);
protected
// this method rewinds the specified stream to the initial position,
// so after calling it the stream Position property remains unchanged
procedure AssertChecksum(const aStream: TStream; const aDataLength: Byte);
function VerifyChecksum(const aStream: TStream; const aDataLength: Byte;
out actualSum, specifiedSum: Byte): boolean;
// no autorewind in this function
class function DebugChecksumMessage(const aActual, aSpecified: Byte): string;
class function EvaluateChecksum(const aStream: TStream; const aDataLength: Byte): Byte;
// this method is meant to be overridden in descendant classes
procedure ReadSpecific(const aStream: TStream; const aDataLength: Byte); overload; virtual;
// this method is meant to be overridden in descendant classes
procedure WriteSpecificDescendantPlug(const aStream: TStream); virtual;
procedure WriteExtendedData(const aStream, aData: TStream); overload;
procedure WriteExtendedData(const aStream: TStream); overload;
end;
// HEX 00 13
TPresmasterSwitcherVoiceoverArmCommand = class(TPresmasterSwitcherExtendedMessage)
protected
FIndex, FState: Byte;
procedure ReadSpecific(const aStream: TStream; const aDataLength: Byte); overload; override;
public const
StateOpposite = $00;
StateDisarm = $04;
public
property Index: Byte read FIndex;
property State: Byte read FState;
end;
// HEX 08 13
TPresmasterSwitcherVoiceoverArmCommandAnswer = class(TPresmasterSwitcherExtendedMessage)
public
constructor Create; overload; override;
constructor Create(const aIndex, aState: Byte); overload;
public const
StateDisarmed = $00;
StateArmed = $01;
protected
FIndex, FState: Byte;
procedure WriteSpecific(const aStream: TStream); override;
end;
implementation
procedure TPresmasterSwitcherExtendedMessage.ReadSpecific(const aStream: TStream);
var
dataLength: Byte;
mixerIndex: Byte;
begin
inherited ReadSpecific(aStream);
aStream.Read(dataLength, sizeOf(dataLength));
AssertChecksum(aStream, dataLength);
ReadSpecific(aStream, dataLength);
end;
procedure TPresmasterSwitcherExtendedMessage.WriteSpecific(const aStream: TStream);
begin
inherited WriteSpecific(aStream);
end;
procedure TPresmasterSwitcherExtendedMessage.AssertChecksum(const aStream: TStream;
const aDataLength: Byte);
var
checksumCorrect: boolean;
actual, specified: byte;
begin
checksumCorrect := VerifyChecksum(aStream, aDataLength, actual, specified);
{
if not checksumCorrect then
raise EChecksumIncorrect.Create( DebugChecksumMessage(actual, specified) );
}
end;
function TPresmasterSwitcherExtendedMessage.VerifyChecksum(const aStream: TStream;
const aDataLength: Byte; out actualSum, specifiedSum: Byte): boolean;
var
initialPosition: Int64;
begin
initialPosition := aStream.Position;
actualSum := EvaluateChecksum(aStream, aDataLength); // evaluate actual sum
aStream.ReadBuffer(specifiedSum, sizeOf(specifiedSum)); // read specified sum
result := specifiedSum = actualSum;
if LogDebugChecksumVerification then
log.Write( DebugChecksumMessage(actualSum, specifiedSum) );
aStream.Position := initialPosition;
end;
class function TPresmasterSwitcherExtendedMessage.DebugChecksumMessage(
const aActual, aSpecified: Byte): string;
begin
result :=
'Checksum: actual = '
+ IntToHex(aActual, CountOfHexadecimalDigits(aActual))
+ ' = '
+ IntToHex(aSpecified, CountOfHexadecimalDigits(aSpecified))
+ ' = specified';
end;
class function TPresmasterSwitcherExtendedMessage.EvaluateChecksum(const aStream: TStream;
const aDataLength: Byte): Byte;
var
i: Byte;
current, sum: Byte;
begin
sum := 0;
for i := 1 to aDataLength do
begin
aStream.Read(current, sizeOf(current));
sum := OverflowAdd(sum, current);
end;
sum := sum and $7F;
result := sum;
end;
procedure TPresmasterSwitcherExtendedMessage.ReadSpecific(const aStream: TStream;
const aDataLength: Byte);
begin
end;
procedure TPresmasterSwitcherExtendedMessage.WriteSpecificDescendantPlug(const aStream: TStream);
begin
end;
procedure TPresmasterSwitcherExtendedMessage.WriteExtendedData(const aStream, aData: TStream);
var
checkSum, dataSize: byte;
begin
Rewind(aData);
checkSum := EvaluateChecksum(aData, aData.Size);
dataSize := Byte(aData.Size);
Rewind(aData);
aStream.Write(dataSize, sizeOf(dataSize)); // data size
aStream.CopyFrom(aData, aData.Size); // data
aStream.Write(checkSum, sizeOf(checkSum));
end;
procedure TPresmasterSwitcherExtendedMessage.WriteExtendedData(const aStream: TStream);
var
data: TStream;
begin
data := TMemoryStream.Create;
WriteSpecificDescendantPlug(data);
WriteExtendedData(aStream, data);
FreeAndNil(data);
end;
procedure TPresmasterSwitcherVoiceoverArmCommand.ReadSpecific(const aStream: TStream;
const aDataLength: Byte);
begin
inherited ReadSpecific(aStream, aDataLength);
aStream.Read(FIndex, sizeOf(FIndex));
aStream.Read(FState, sizeOf(FState));
end;
constructor TPresmasterSwitcherVoiceoverArmCommandAnswer.Create(const aIndex, aState: Byte);
begin
Create;
FIndex := aIndex;
FState := aState;
end;
constructor TPresmasterSwitcherVoiceoverArmCommandAnswer.Create;
begin
inherited Create;
Command := AnswerVoiceoverArmCommand;
end;
procedure TPresmasterSwitcherVoiceoverArmCommandAnswer.WriteSpecific(const aStream: TStream);
begin
inherited WriteSpecific(aStream);
aStream.Write(FIndex, sizeOf(FIndex));
aStream.Write(FState, sizeOf(FState));
end;
end.