-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpTemplate.cs
More file actions
324 lines (273 loc) · 7.87 KB
/
CSharpTemplate.cs
File metadata and controls
324 lines (273 loc) · 7.87 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
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
/*
* Auto-generated code, do not modify.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.IO;
namespace SpaceLeap
{
public class MavLink
{
#region Configuration
public static byte SystemId = 0;
public static byte ComponentId = 0;
#endregion
#region Enums
/*ENUMS*/
#endregion
#region Base classes
public class MessageBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public enum MessageType
{
/*MESSAGETYPEENUM*/ }
protected static Dictionary<byte, Type> _messageTypes = new Dictionary<byte, Type>();
private static byte _packageSequence = 0;
protected byte _messageId;
protected object[] _fields;
protected Type[] _fieldTypes;
protected int[] _arrayLengths;
protected int _payloadLength;
public int MessageId
{
get
{
return _messageId;
}
}
public MessageType Message
{
get
{
return (MessageType)_messageId;
}
}
private void WriteValue(BinaryWriter writer, Type type, object value)
{
if (type.Equals(typeof(float)))
writer.Write((float)value);
else if (type.Equals(typeof(double)))
writer.Write((double)value);
else if (type.Equals(typeof(byte)))
writer.Write((byte)value);
else if (type.Equals(typeof(sbyte)))
writer.Write((sbyte)value);
else if (type.Equals(typeof(Int16)))
writer.Write((Int16)value);
else if (type.Equals(typeof(UInt16)))
writer.Write((UInt16)value);
else if (type.Equals(typeof(Int32)))
writer.Write((Int32)value);
else if (type.Equals(typeof(UInt32)))
writer.Write((UInt32)value);
else if (type.Equals(typeof(Int64)))
writer.Write((Int64)value);
else if (type.Equals(typeof(UInt64)))
writer.Write((UInt64)value);
}
private void WriteField(BinaryWriter writer, int index)
{
Type type = _fieldTypes[index];
if (type.IsArray)
{
int arrayLength = _arrayLengths[index];
Array array = (Array)_fields[index];
if (array.Length > 0)
{
Type nestedType = null;
for (int i = 0; i < array.Length; ++i)
{
object item = array.GetValue(i);
if (item != null)
{
nestedType = item.GetType();
break;
}
}
if (nestedType != null)
{
for (int i = 0; i < Math.Min(array.Length, arrayLength); ++i)
{
WriteValue(writer, nestedType, array.GetValue(i));
}
// Fill the array with zeros for might missing values
object defaultValue = Convert.ChangeType(0, nestedType);
for (int i = array.Length; i < arrayLength; ++i)
{
WriteValue(writer, nestedType, defaultValue);
}
}
}
}
else
WriteValue(writer, type, _fields[index]);
}
private object ReadValue(BinaryReader reader, Type type)
{
object value = null;
if (type.Equals(typeof(float)))
value = reader.ReadSingle();
else if (type.Equals(typeof(double)))
value = reader.ReadDouble();
else if (type.Equals(typeof(byte)))
value = reader.ReadByte();
else if (type.Equals(typeof(sbyte)))
value = reader.ReadSByte();
else if (type.Equals(typeof(Int16)))
value = reader.ReadInt16();
else if (type.Equals(typeof(UInt16)))
value = reader.ReadUInt16();
else if (type.Equals(typeof(Int32)))
value = reader.ReadInt32();
else if (type.Equals(typeof(UInt32)))
value = reader.ReadUInt32();
else if (type.Equals(typeof(Int64)))
value = reader.ReadInt64();
else if (type.Equals(typeof(UInt64)))
value = reader.ReadUInt64();
return value;
}
private void ReadField(BinaryReader reader, int index)
{
Type type = _fieldTypes[index];
if (type.IsArray)
{
int arrayLength = _arrayLengths[index];
// Create an array instance
Array array = (Array)Activator.CreateInstance(type, arrayLength);
Type nestedType = array.GetValue(0).GetType();
for (int i = 0; i < arrayLength; ++i)
array.SetValue(ReadValue(reader, nestedType), i);
_fields[index] = array;
}
else
{
_fields[index] = ReadValue(reader, type);
}
}
public virtual byte[] Serialize()
{
FrameworkBitConverter converter = new FrameworkBitConverter();
converter.SetDataIsLittleEndian(BitConverter.IsLittleEndian);
byte[] data = null;
using (MemoryStream stream = new MemoryStream())
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write((byte)0xFE);
writer.Write((byte)_payloadLength);
writer.Write(_packageSequence++);
writer.Write(MavLink.SystemId);
writer.Write(MavLink.ComponentId);
writer.Write(_messageId);
for (int i = 0; i < _fields.Length; ++i)
{
WriteField(writer, i);
}
// Append two bytes to be replaced by the CRC later on
writer.Write((byte)0);
writer.Write((byte)0);
writer.Flush();
}
data = stream.ToArray();
}
MavLinkCRC crc = new MavLinkCRC();
for (int i = 1; i < data.Length - 2; ++i)
{
crc.update_checksum(data[i]);
}
crc.finish_checksum(_messageId);
data[data.Length - 2] = (byte)crc.getLSB();
data[data.Length - 1] = (byte)crc.getMSB();
return data;
}
public static MessageBase Deserialize(byte[] data)
{
using (MemoryStream stream = new MemoryStream(data))
using (BinaryReader reader = new BinaryReader(stream))
{
// Ignore initial key
reader.ReadByte();
byte payloadLength = reader.ReadByte();
byte packageSequence = reader.ReadByte();
byte systemId = reader.ReadByte();
byte componentId = reader.ReadByte();
byte messageId = reader.ReadByte();
// Create instance based on message id
MessageBase message = null;
Type messageType;
if (_messageTypes.TryGetValue(messageId, out messageType))
{
message = (MessageBase)Activator.CreateInstance(messageType);
message._fields = new object[message._fieldTypes.Length];
for (int i = 0; i < message._fieldTypes.Length; ++i)
{
message.ReadField(reader, i);
}
// TODO: Verify CRC
byte LSBCRC = reader.ReadByte();
byte MSBCRC = reader.ReadByte();
}
return message;
}
}
public override bool Equals(object obj)
{
MessageBase other = obj as MessageBase;
if (other == null)
return base.Equals(obj);
if (_fields.Length != other._fields.Length)
return false;
for (int i = 0; i < _fields.Length; ++i)
{
if (_arrayLengths[i] > 0)
{
Array left = (Array)_fields[i];
Array right = (Array)other._fields[i];
if (left.Length != right.Length)
return false;
for (int j = 0; j < left.Length; ++j)
{
if (left.GetValue(j) != right.GetValue(j))
return false;
}
}
else if (_fields[i] != other._fields[i])
return false;
}
return true;
}
public override int GetHashCode()
{
int hash = 0;
for (int i = 0; i < _fields.Length; ++i)
{
if (_arrayLengths[i] > 0)
{
Array array = (Array)_fields[i];
for (int j = 0; j < array.Length; ++j)
{
hash ^= array.GetValue(j).GetHashCode();
}
}
else
hash ^= _fields[i].GetHashCode();
}
return hash;
}
protected virtual void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
#region Messages
/*MESSAGES*/
#endregion
}
}