-
Notifications
You must be signed in to change notification settings - Fork 1
/
MSP.cs
399 lines (343 loc) · 13.6 KB
/
MSP.cs
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
using System.Net;
using System.Runtime.InteropServices;
namespace INAV_SIM_OSD
{
public class MSP : IDisposable
{
private const int MSP_TIMEOUT = 1000; // ms
public const UInt16 MSP_FC_VARIANT = 2;
public const UInt16 MSP_DISPLAYPORT = 182;
public enum State
{
IDLE,
HEADER_START,
HEADER_M,
HEADER_X,
HEADER_V1,
PAYLOAD_V1,
CHECKSUM_V1,
HEADER_V2_OVER_V1,
PAYLOAD_V2_OVER_V1,
CHECKSUM_V2_OVER_V1,
HEADER_V2_NATIVE,
PAYLOAD_V2_NATIVE,
CHECKSUM_V2_NATIVE,
COMMAND_RECEIVED
}
public enum Version
{
MSP_V1,
MSP_V2,
MSP_V2_OVER_V1,
MSP_V2_NATIVE
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct HeaderV1
{
public byte Size;
public byte Command;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct HeaderV2
{
public byte Flags;
public UInt16 Command;
public UInt16 Size;
}
public class Frame
{
public Version Version { get; set; }
public byte CRCV1 { get; set; }
public byte CRCV2 { get; set; }
public UInt16 Command { get; set; }
public byte Flags { get; set; }
public byte[] Buffer { get; set; }
public int Offset { get; set; }
public int DataSize { get; set; }
public State State { get; set; }
public Frame()
{
Offset = 0;
Buffer = new byte[1024];
Version = Version.MSP_V1;
CRCV1 = 0;
CRCV2 = 0;
Command = 0;
Flags = 0;
DataSize = 0;
State = State.IDLE;
}
}
private const byte MSP_START = (byte)'$';
private const byte MSP_V1 = (byte)'M';
private const byte MSP_V2 = (byte)'X';
private const byte MSP_FROM_FC = (byte)'>';
private const byte MSP_TO_FC = (byte)'<';
private const byte MSP_V2_FRAMEID = 0xFF;
private IMSPConnection? _connection;
public event EventHandler? FrameReceived;
public Frame ReceivedFrame { get; private set; }
protected virtual void OnFrameReceived()
{
FrameReceived?.Invoke(this, EventArgs.Empty);
}
public MSP()
{
ReceivedFrame = new();
}
~MSP()
{
Dispose(false);
}
public void SetConnection(string connectionString)
{
_connection?.Dispose();
if (connectionString.Contains("COM"))
_connection = new SerialConnection(connectionString, 115200);
else if (Helper.TryParseIpString(connectionString, out IPAddress? iPAddress, out int port))
_connection = new TCPConnection(iPAddress, port);
}
public void Open()
{
if (_connection is null)
throw new InvalidOperationException(nameof(_connection));
if (!_connection.IsOpen())
_connection.Open();
}
public void ClosePort()
{
if (_connection is not null && _connection.IsOpen())
_connection.Close();
}
public void Close()
{
((IDisposable)this).Dispose();
}
private void SendMSP(UInt16 cmd, byte[]? payload)
{
if (_connection is null)
throw new InvalidOperationException(nameof(_connection));
if (!_connection.IsOpen())
throw new IOException("Serial Port closed");
// Only MSP v2
int bufferLength = 9;
int payloadLength = 0;
if (payload is not null || payload?.Length == 0)
{
bufferLength += payload.Length;
payloadLength = payload.Length;
}
byte[] buffer = new byte[bufferLength];
buffer[0] = MSP_START;
buffer[1] = MSP_V2;
buffer[2] = MSP_TO_FC;
buffer[3] = 0;
buffer[4] = Helper.GetLowerByte((UInt16)cmd);
buffer[5] = Helper.GetUpperByte((UInt16)cmd);
buffer[6] = Helper.GetLowerByte((UInt16)payloadLength);
buffer[7] = Helper.GetUpperByte((UInt16)payloadLength);
if (payload is not null)
Buffer.BlockCopy(payload, 0, buffer, 8, payload.Length);
int crc = 0;
for (int i = 3; i < bufferLength - 1; i++)
crc = Crc8_Dvb_S2(crc, buffer[i]);
buffer[bufferLength - 1] = (byte)crc;
_connection.Write(buffer, 0, buffer.Length);
}
public void SendReceive(CancellationToken ct, byte[]? payload = null)
{
if (_connection is null)
throw new InvalidOperationException(nameof(_connection));
DateTime start = DateTime.Now;
ReceivedFrame.State = State.IDLE;
SendMSP(MSP_FC_VARIANT, payload);
while (ReceivedFrame.State != State.COMMAND_RECEIVED && !ct.IsCancellationRequested)
{
int b = _connection.ReadByte();
if (b == -1)
return;
if (ProcessFrame((byte)b) && ReceivedFrame.State == State.COMMAND_RECEIVED)
{
TimeSpan duration = DateTime.Now - start;
if (duration > TimeSpan.FromMilliseconds(MSP_TIMEOUT))
break;
OnFrameReceived();
}
}
}
private bool ProcessFrame(byte b)
{
switch (ReceivedFrame.State)
{
case State.IDLE:
if (b == MSP_START)
{
ReceivedFrame = new();
ReceivedFrame.Version = Version.MSP_V1;
ReceivedFrame.State = State.HEADER_START;
}
else
return false;
break;
case State.HEADER_START:
ReceivedFrame.State = b switch
{
MSP_V1 => State.HEADER_M,
MSP_V2 => State.HEADER_X,
_ => State.IDLE,
};
break;
case State.HEADER_M:
if (b == MSP_FROM_FC)
{
ReceivedFrame.Offset = 0;
ReceivedFrame.CRCV1 = 0;
ReceivedFrame.CRCV2 = 0;
ReceivedFrame.State = State.HEADER_V1;
}
else
ReceivedFrame.State = State.IDLE;
break;
case State.HEADER_X:
if (b == MSP_FROM_FC)
{
ReceivedFrame.Offset = 0;
ReceivedFrame.CRCV2 = 0;
ReceivedFrame.Version = Version.MSP_V2_NATIVE; ;
ReceivedFrame.State = State.HEADER_V2_NATIVE;
}
break;
case State.HEADER_V1:
ReceivedFrame.Buffer[ReceivedFrame.Offset++] = b;
ReceivedFrame.CRCV1 ^= b;
if (ReceivedFrame.Offset == Marshal.SizeOf(typeof(HeaderV1)))
{
HeaderV1 header = Helper.BufferToStruct<HeaderV1>(ReceivedFrame.Buffer[..ReceivedFrame.Offset]);
if (header.Size > ReceivedFrame.Buffer.Length)
ReceivedFrame.State = State.IDLE;
else if (header.Command == MSP_V2_FRAMEID)
{
if (header.Size >= Marshal.SizeOf(typeof(HeaderV2)) + 1)
{
ReceivedFrame.State = State.HEADER_V2_OVER_V1;
}
else
ReceivedFrame.State = State.IDLE;
}
else
{
ReceivedFrame.DataSize = header.Size;
ReceivedFrame.Command = header.Command;
ReceivedFrame.Offset = 0;
ReceivedFrame.State = ReceivedFrame.DataSize > 0 ? State.PAYLOAD_V1 : State.CHECKSUM_V1;
}
}
break;
case State.PAYLOAD_V1:
ReceivedFrame.Buffer[ReceivedFrame.Offset++] = b;
ReceivedFrame.CRCV1 ^= b;
if (ReceivedFrame.Offset == ReceivedFrame.DataSize)
ReceivedFrame.State = State.CHECKSUM_V1;
break;
case State.CHECKSUM_V1:
if (ReceivedFrame.CRCV1 == b)
ReceivedFrame.State = State.COMMAND_RECEIVED;
else
ReceivedFrame.State = State.IDLE;
break;
case State.HEADER_V2_OVER_V1:
ReceivedFrame.Buffer[ReceivedFrame.Offset++] = b;
ReceivedFrame.CRCV1 ^= b;
ReceivedFrame.CRCV2 = (byte)Crc8_Dvb_S2(ReceivedFrame.CRCV2, b);
if (ReceivedFrame.Offset == Marshal.SizeOf(typeof(HeaderV1)) + Marshal.SizeOf(typeof(HeaderV2)))
{
HeaderV2 header = Helper.BufferToStruct<HeaderV2>(ReceivedFrame.Buffer[..ReceivedFrame.Offset]);
if (header.Size > ReceivedFrame.Buffer.Length)
ReceivedFrame.State = State.IDLE;
else
{
ReceivedFrame.Command = header.Command;
ReceivedFrame.Flags = header.Flags;
ReceivedFrame.Offset = 0;
ReceivedFrame.State = ReceivedFrame.DataSize > 0 ? State.PAYLOAD_V2_OVER_V1 : State.CHECKSUM_V2_OVER_V1;
}
}
break;
case State.PAYLOAD_V2_OVER_V1:
ReceivedFrame.CRCV2 = (byte)Crc8_Dvb_S2(ReceivedFrame.CRCV2, b);
ReceivedFrame.CRCV1 ^= b;
ReceivedFrame.Buffer[ReceivedFrame.Offset++] = b;
if (ReceivedFrame.Offset == ReceivedFrame.DataSize)
ReceivedFrame.State = State.CHECKSUM_V2_OVER_V1;
break;
case State.CHECKSUM_V2_OVER_V1:
ReceivedFrame.CRCV1 ^= b;
if (ReceivedFrame.Offset == ReceivedFrame.DataSize)
ReceivedFrame.State = State.CHECKSUM_V1;
else
ReceivedFrame.State = State.IDLE;
break;
case State.HEADER_V2_NATIVE:
ReceivedFrame.Buffer[ReceivedFrame.Offset++] = b;
ReceivedFrame.CRCV2 = (byte)Crc8_Dvb_S2(ReceivedFrame.CRCV2, b);
if (ReceivedFrame.Offset == Marshal.SizeOf(typeof(HeaderV2)))
{
HeaderV2 header = Helper.BufferToStruct<HeaderV2>(ReceivedFrame.Buffer[..ReceivedFrame.Offset]);
if (header.Size >= ReceivedFrame.Buffer.Length)
ReceivedFrame.State = State.IDLE;
else
{
ReceivedFrame.DataSize = header.Size;
ReceivedFrame.Command = header.Command;
ReceivedFrame.Flags = header.Flags;
ReceivedFrame.Offset = 0;
ReceivedFrame.State = ReceivedFrame.DataSize > 0 ? State.PAYLOAD_V2_NATIVE : State.CHECKSUM_V2_NATIVE;
}
}
break;
case State.PAYLOAD_V2_NATIVE:
ReceivedFrame.CRCV2 = (byte)Crc8_Dvb_S2(ReceivedFrame.CRCV2, b);
ReceivedFrame.Buffer[ReceivedFrame.Offset++] = b;
if (ReceivedFrame.Offset == ReceivedFrame.DataSize)
ReceivedFrame.State = State.CHECKSUM_V2_NATIVE;
break;
case State.CHECKSUM_V2_NATIVE:
if (ReceivedFrame.CRCV2 == b)
ReceivedFrame.State = State.COMMAND_RECEIVED;
else
ReceivedFrame.State = State.IDLE;
break;
}
return true;
}
public static int Crc8_Dvb_S2(int crc, int ch)
{
crc ^= ch;
for (int i = 0; i < 8; ++i)
{
if ((crc & 0x80) != 0)
{
crc = ((crc << 1) & 0xFF) ^ 0xD5;
}
else
{
crc = (crc << 1) & 0xFF;
}
}
return crc;
}
protected virtual void Dispose(bool dispose)
{
if (dispose)
{
_connection?.Close();
_connection?.Dispose();
}
}
void IDisposable.Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}