forked from microsoft/AirSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MavLinkGenerator.cs
444 lines (400 loc) · 16.2 KB
/
MavLinkGenerator.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MavLinkComGenerator
{
class MavLinkGenerator
{
StreamWriter header;
StreamWriter impl;
MavLink definitions;
public void Generate(MavLink definitions, string outputDirectory)
{
this.definitions = definitions;
using (header = new StreamWriter(Path.Combine(outputDirectory, "MavLinkMessages.hpp")))
{
using (impl = new StreamWriter(Path.Combine(outputDirectory, "MavLinkMessages.cpp")))
{
header.WriteLine("#ifndef MavLinkCom_MavLinkMessages_hpp");
header.WriteLine("#define MavLinkCom_MavLinkMessages_hpp");
header.WriteLine("");
header.WriteLine("#include <stdint.h>");
header.WriteLine("#include <string>");
header.WriteLine("#include \"MavLinkMessageBase.hpp\"");
header.WriteLine("");
header.WriteLine("namespace mavlinkcom");
header.WriteLine("{");
header.WriteLine("");
impl.WriteLine("#include \"MavLinkMessages.hpp\"");
impl.WriteLine("using namespace mavlinkcom;");
impl.WriteLine("");
GenerateEnums();
GenerateMessages();
GenerateCommands();
header.WriteLine("}");
header.WriteLine("");
header.WriteLine("#endif");
}
}
}
private void GenerateCommands()
{
var cmds = (from e in definitions.enums where e.name == "MAV_CMD" select e).FirstOrDefault();
if (cmds == null)
{
return;
}
foreach (var cmd in cmds.entries)
{
Console.WriteLine("generating command {0}", cmd.name);
if (!string.IsNullOrWhiteSpace(cmd.description))
{
WriteComment("", cmd.description);
}
string name = CamelCase(cmd.name);
header.WriteLine("class {0} : public MavLinkCommand {{", name);
header.WriteLine("public:");
header.WriteLine(" const static uint16_t kCommandId = {0};", cmd.value);
header.WriteLine(" {0}() {{ command = kCommandId; }}", name);
UniqueList unique = new UniqueList();
foreach (var p in cmd.parameters)
{
string fieldName = NameFromDescription(p.description);
if (fieldName != "Empty" && fieldName != "Reserved")
{
if (!string.IsNullOrWhiteSpace(p.description))
{
WriteComment(" ", p.description);
}
header.WriteLine(" float {0} = 0;", unique.Add(fieldName));
}
}
unique = new UniqueList();
header.WriteLine("protected:");
header.WriteLine(" virtual void pack();");
impl.WriteLine("void {0}::pack() {{", name);
int i = 0;
foreach (var p in cmd.parameters)
{
i++;
string fieldName = NameFromDescription(p.description);
if (fieldName != "Empty" && fieldName != "Reserved")
{
impl.WriteLine(" param{0} = {1};", i, unique.Add(fieldName));
}
}
impl.WriteLine("}");
header.WriteLine(" virtual void unpack();");
impl.WriteLine("void {0}::unpack() {{", name);
unique = new UniqueList();
i = 0;
foreach (var p in cmd.parameters)
{
i++;
string fieldName = NameFromDescription(p.description);
if (fieldName != "Empty" && fieldName != "Reserved")
{
impl.WriteLine(" {1} = param{0};", i, unique.Add(fieldName));
}
}
impl.WriteLine("}");
header.WriteLine("};");
}
}
char[] nameSeparators = new char[] { '_' };
private string CamelCase(string name)
{
StringBuilder sb = new StringBuilder();
foreach (string word in name.Split(nameSeparators, StringSplitOptions.RemoveEmptyEntries))
{
string lower = word.ToLowerInvariant();
string camel = Char.ToUpperInvariant(lower[0]).ToString();
if (lower.Length > 1)
{
camel += lower.Substring(1);
}
sb.Append(camel);
}
return sb.ToString();
}
char[] terminators = new char[] { '.', '-', ':', ',', '(', '[' };
char[] whitespace = new char[] { ' ', '\t', '\n', '\r' };
private string NameFromDescription(string description)
{
StringBuilder sb = new StringBuilder();
int i = description.IndexOf("this sets");
if (i > 0)
{
description = description.Substring(i + 9);
}
i = description.IndexOfAny(terminators);
if (i > 0)
{
description = description.Substring(0, i);
}
string[] words = description.Split(whitespace, StringSplitOptions.RemoveEmptyEntries);
int count = 0;
for (int j = 0; j < words.Length; j++)
{
string w = words[j];
w = w.Replace("'", "");
w = w.Replace("/", "");
w = w.Replace("?", "");
w = CamelCase(w);
if (j > 0 && (w == "In" || w == "From" || w == "And" || w == "As" || w == "The" || w == "And" || w == "It" || w == "On"))
{
continue; // skip filler words
}
if (j == 0 && Char.IsDigit(w[0]))
{
w = "p" + w; // make it a legal C++ identifier.
}
sb.Append(w);
count++;
if (count == 3)
{
break;
}
}
return sb.ToString();
}
private void GenerateMessages()
{
foreach (var m in definitions.messages)
{
string name = CamelCase(m.name);
Console.WriteLine("generating message {0}", name);
if (!string.IsNullOrWhiteSpace(m.description))
{
WriteComment("", m.description);
}
header.WriteLine("class MavLink{0} : public MavLinkMessageBase {{", name);
header.WriteLine("public:");
header.WriteLine(" const static uint8_t kMessageId = {0};", m.id);
header.WriteLine(" MavLink{0}() {{ msgid = kMessageId; }}", name);
// find array types and split the type string "int[16]" into element type "int" and isArray=true and array_length=16.
foreach (var field in m.fields)
{
string type = field.type;
if (type.Contains('['))
{
var tuple = ParseArrayType(type);
field.type = tuple.Item1;
field.isArray = true;
field.array_length = tuple.Item2;
}
}
// mavlink packs the fields by descending size for some odd reason.
m.fields = m.fields.OrderByDescending(x => typeSize[x.type]).ToList();
int length = m.fields.Count;
for (int i = 0; i < length; i++)
{
var field = m.fields[i];
if (!string.IsNullOrWhiteSpace(field.description))
{
WriteComment(" ", field.description);
}
var type = field.type;
if (type == "uint8_t_mavlink_version")
{
type = "uint8_t";
}
if (field.isArray)
{
header.WriteLine(" {0} {1}[{2}] = {{ 0 }};", type, field.name, field.array_length);
}
else
{
header.WriteLine(" {0} {1} = 0;", type, field.name);
}
}
header.WriteLine("protected:");
header.WriteLine(" virtual int pack(char* buffer) const;");
impl.WriteLine("int MavLink{0}::pack(char* buffer) const {{", name);
int offset = 0;
for (int i = 0; i < length; i++)
{
var field = m.fields[i];
var type = field.type;
if (type == "uint8_t_mavlink_version")
{
type = "uint8_t";
}
int size = typeSize[type];
if (field.isArray)
{
// it is an array.
impl.WriteLine(" pack_{0}_array({1}, buffer, reinterpret_cast<const {0}*>(&this->{2}[0]), {3});", type, field.array_length, field.name, offset);
size *= field.array_length;
}
else
{
impl.WriteLine(" pack_{0}(buffer, reinterpret_cast<const {0}*>(&this->{1}), {2});", type, field.name, offset);
}
offset += size;
}
impl.WriteLine(" return {0};", offset);
impl.WriteLine("}");
impl.WriteLine("");
header.WriteLine(" virtual int unpack(const char* buffer);");
impl.WriteLine("int MavLink{0}::unpack(const char* buffer) {{", name);
offset = 0;
for (int i = 0; i < length; i++)
{
var field = m.fields[i];
var type = field.type;
if (type == "uint8_t_mavlink_version")
{
type = "uint8_t";
}
int size = typeSize[type];
if (field.isArray)
{
// it is an array.
impl.WriteLine(" unpack_{0}_array({1}, buffer, reinterpret_cast<{0}*>(&this->{2}[0]), {3});", type, field.array_length, field.name, offset);
size *= field.array_length;
}
else
{
impl.WriteLine(" unpack_{0}(buffer, reinterpret_cast<{0}*>(&this->{1}), {2});", type, field.name, offset);
}
offset += size;
}
impl.WriteLine(" return {0};", offset);
impl.WriteLine("}");
impl.WriteLine("");
header.WriteLine("};");
header.WriteLine("");
}
}
public Tuple<string,int> ParseArrayType(string type)
{
int i = type.IndexOf('[');
int k = type.IndexOf(']', i);
if (k > i)
{
string name = type.Substring(0, i);
i++;
string index = type.Substring(i, k - i);
int j = 0;
int.TryParse(index, out j);
return new Tuple<string, int>(name, j);
}
throw new Exception("Invalid array type specification: " + type);
}
public static Dictionary<string, int> typeSize = new Dictionary<string, int>()
{
{ "float" , 4},
{"double" , 8 },
{"char" , 1},
{"int8_t" , 1},
{"uint8_t" , 1},
{"uint8_t_mavlink_version" , 1 },
{"int16_t" , 2 },
{"uint16_t" , 2 },
{"int32_t" , 4 },
{"uint32_t" , 4 },
{"int64_t" , 8 },
{"uint64_t" , 8 }
};
private void GenerateEnums()
{
header.WriteLine("enum class MavLinkMessageIds {");
bool first = true;
foreach (var m in definitions.messages)
{
if (!first)
{
header.WriteLine(",");
}
first = false;
header.Write(" MAVLINK_MSG_ID_{0} = {1}", m.name, m.id);
}
header.WriteLine("");
header.WriteLine("};");
foreach (var e in definitions.enums)
{
Console.WriteLine("generating enum class {0}", e.name);
if (!string.IsNullOrWhiteSpace(e.description))
{
WriteComment("", e.description);
}
header.WriteLine("enum class {0} {{", e.name);
bool zeroUsed = false;
int length = e.entries.Count;
for (int i = 0; i < length; i++)
{
var field = e.entries[i];
if (!string.IsNullOrWhiteSpace(field.description))
{
WriteComment(" ", field.description);
}
header.Write(" {0}", field.name);
if (!zeroUsed || field.value != 0)
{
header.Write(" = {0}", field.value);
}
if (!zeroUsed && field.value == 0)
{
zeroUsed = true;
}
if (i + 1 < length)
{
header.Write(",");
}
header.WriteLine();
}
header.WriteLine("};");
header.WriteLine("");
}
}
private void WriteComment(string indent, string description)
{
int lineLength = 0;
foreach (string word in description.Split(new char[] { '\r', '\n', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries))
{
if (lineLength > 80)
{
header.WriteLine();
lineLength = 0;
}
if (lineLength == 0)
{
header.Write(indent + "// ");
lineLength = indent.Length + 3;
}
else
{
lineLength++;
header.Write(" ");
}
header.Write(word);
lineLength += word.Length;
}
if (lineLength > 0)
{
header.WriteLine();
}
}
class UniqueList
{
HashSet<string> existing = new HashSet<string>();
public string Add(String baseName)
{
string name = baseName;
int index = 2;
while (existing.Contains(name))
{
name = baseName + index++;
}
existing.Add(name);
return name;
}
}
}
}