-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
gen_messages.ml
389 lines (333 loc) · 15.8 KB
/
gen_messages.ml
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
(*
* XML preprocessing of messages.xml for downlink protocol
*
* Copyright (C) 2003-2008 ENAC, Pascal Brisset, Antoine Drouin
*
* This file is part of paparazzi.
*
* paparazzi is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* paparazzi 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*)
open Printf
type format = string
type _type =
Basic of string
| Array of string * string
| FixedArray of string * string * int
let c_type = fun format ->
match format with
"Float" -> "float"
| "Double" -> "double"
| "Int32" -> "int32_t"
| "Int16" -> "int16_t"
| "Int8" -> "int8_t"
| "Uint32" -> "uint32_t"
| "Uint16" -> "uint16_t"
| "Uint8" -> "uint8_t"
| "Char" -> "char"
| _ -> failwith (sprintf "gen_messages.c_type: unknown format '%s'" format)
let dl_type = fun format ->
match format with
"Float" -> "DL_TYPE_FLOAT"
| "Double" -> "DL_TYPE_DOUBLE"
| "Int32" -> "DL_TYPE_INT32"
| "Int16" -> "DL_TYPE_INT16"
| "Int8" -> "DL_TYPE_INT8"
| "Uint32" -> "DL_TYPE_UINT32"
| "Uint16" -> "DL_TYPE_UINT16"
| "Uint8" -> "DL_TYPE_UINT8"
| "Char" -> "DL_TYPE_CHAR"
| _ -> failwith (sprintf "gen_messages.dl_type: unknown format '%s'" format)
type field = _type * string * format option
type fields = field list
type message = {
name : string;
id : int;
period : float option;
fields : fields
}
module Syntax = struct
(** Parse a type name and returns a _type value *)
let parse_type = fun t varname ->
try
let type_parts = Str.full_split (Str.regexp "[][]") t in
match type_parts with
| [Str.Text ty] -> Basic ty
| [Str.Text ty; Str.Delim "["; Str.Delim "]"] -> Array (ty, varname)
| [Str.Text ty; Str.Delim "["; Str.Text len ; Str.Delim "]"] -> FixedArray (ty, varname, int_of_string len)
| _ -> failwith "Gen_messages: not a valid field type"
with
| Failure fail -> failwith("Gen_messages: not a valid array length")
let length_name = fun s -> "nb_"^s
let assoc_types t =
try
List.assoc t Pprz.types
with
Not_found ->
failwith (sprintf "Error: '%s' unknown type" t)
let rec sizeof = function
Basic t -> string_of_int (assoc_types t).Pprz.size
| Array (t, varname) -> sprintf "1+%s*%s" (length_name varname) (sizeof (Basic t))
| FixedArray (t, varname, len) -> sprintf "0+%d*%s" len (sizeof (Basic t))
let rec nameof = function
Basic t -> String.capitalize t
| Array _ -> failwith "nameof"
| FixedArray _ -> failwith "nameof"
(** Translates a "message" XML element into a value of the 'message' type *)
let struct_of_xml = fun xml ->
let name = ExtXml.attrib xml "name"
and id = ExtXml.int_attrib xml "id"
and period = try Some (ExtXml.float_attrib xml "period") with _ -> None
and fields =
List.map (fun field ->
let id = ExtXml.attrib field "name"
and type_name = ExtXml.attrib field "type"
and fmt = try Some (Xml.attrib field "format") with _ -> None in
let _type = parse_type type_name id in
(_type, id, fmt))
(List.filter (fun t -> compare (Xml.tag t) "field" = 0) (Xml.children xml)) in
{ id=id; name = name; period = period; fields = fields }
let check_single_ids = fun msgs ->
let tab = Array.create 256 false
and last_id = ref 0 in
List.iter (fun msg ->
if tab.(msg.id) then
failwith (sprintf "Duplicated message id: %d" msg.id);
if msg.id < !last_id then
fprintf stderr "Warning: unsorted id: %d\n%!" msg.id;
last_id := msg.id;
tab.(msg.id) <- true)
msgs
(** Translates one class of a XML message file into a list of messages *)
let read = fun filename class_ ->
let xml = Xml.parse_file filename in
try
let xml_class = ExtXml.child ~select:(fun x -> Xml.attrib x "name" = class_) xml "msg_class" in
let msgs = List.map struct_of_xml (Xml.children xml_class) in
check_single_ids msgs;
msgs
with
Not_found -> failwith (sprintf "No msg_class '%s' found" class_)
end (* module Suntax *)
(** Pretty printer of C macros for sending and parsing messages *)
module Gen_onboard = struct
let print_field = fun h (t, name, (_f: format option)) ->
match t with
Basic _ ->
fprintf h "\t trans->put_bytes(trans->impl, dev, %s, DL_FORMAT_SCALAR, %s, (void *) _%s);\n" (dl_type (Syntax.nameof t)) (Syntax.sizeof t) name
| Array (t, varname) ->
let _s = Syntax.sizeof (Basic t) in
fprintf h "\t trans->put_bytes(trans->impl, dev, DL_TYPE_ARRAY_LENGTH, DL_FORMAT_SCALAR, 1, (void *) &%s);\n" (Syntax.length_name varname);
fprintf h "\t trans->put_bytes(trans->impl, dev, %s, DL_FORMAT_ARRAY, %s * %s, (void *) _%s);\n" (dl_type (Syntax.nameof (Basic t))) (Syntax.sizeof (Basic t)) (Syntax.length_name varname) name
| FixedArray (t, varname, len) ->
let _s = Syntax.sizeof (Basic t) in
fprintf h "\t trans->put_bytes(trans->impl, dev, %s, DL_FORMAT_ARRAY, %s * %d, (void *) _%s);\n" (dl_type (Syntax.nameof (Basic t))) (Syntax.sizeof (Basic t)) len name
let print_macro_param h = function
(Array _, s, _) -> fprintf h "%s, %s" (Syntax.length_name s) s
| (FixedArray _, s, _) -> fprintf h "%s" s
| (_, s, _) -> fprintf h "%s" s
let print_macro_parameters h = function
[] -> ()
| f::fields ->
fprintf h ", ";
print_macro_param h f;
List.iter (fun f -> fprintf h ", "; print_macro_param h f) fields
let print_fun_param h = function
(Array (t, _), s, _) -> fprintf h "uint8_t %s, %s *_%s" (Syntax.length_name s) (c_type (Syntax.nameof (Basic t))) s
| (FixedArray (t, _, _), s, _) -> fprintf h "%s *_%s" (c_type (Syntax.nameof (Basic t))) s
| (t, s, _) -> fprintf h "%s *_%s" (c_type (Syntax.nameof t)) s
let print_function_parameters h = function
[] -> ()
| f::fields ->
fprintf h ", ";
print_fun_param h f;
List.iter (fun f -> fprintf h ", "; print_fun_param h f) fields
let rec size_fields = fun fields size ->
match fields with
[] -> size
| (t, _, _)::fields -> size_fields fields (size ^"+"^Syntax.sizeof t)
let size_of_message = fun m -> size_fields m.fields "0"
let estimated_size_of_message = fun m ->
try
List.fold_right
(fun (t, _, _) r -> int_of_string (Syntax.sizeof t)+r)
m.fields
0
with
Failure "int_of_string" -> 0
let print_downlink_macro = fun h {name=s; fields = fields} ->
(* Macros for backward compatibility *)
fprintf h "#define DOWNLINK_SEND_%s(_trans, _dev" s;
print_macro_parameters h fields;
fprintf h ") ";
fprintf h "pprz_msg_send_%s(&((_trans).trans_tx), &((_dev).device), AC_ID" s;
print_macro_parameters h fields;
fprintf h ")\n";
(* Print message_send functions *)
fprintf h "static inline void pprz_msg_send_%s(struct transport_tx *trans, struct link_device *dev, uint8_t ac_id" s;
print_function_parameters h fields;
fprintf h ") {\n";
let size = (size_fields fields "0") in
fprintf h "\tif (trans->check_available_space(trans->impl, dev, trans->size_of(trans->impl, %s +2 /* msg header overhead */))) {\n" size;
fprintf h "\t trans->count_bytes(trans->impl, dev, trans->size_of(trans->impl, %s +2 /* msg header overhead */));\n" size;
fprintf h "\t trans->start_message(trans->impl, dev, %s +2 /* msg header overhead */);\n" size;
fprintf h "\t trans->put_bytes(trans->impl, dev, DL_TYPE_UINT8, DL_FORMAT_SCALAR, 1, &ac_id);\n";
fprintf h "\t trans->put_named_byte(trans->impl, dev, DL_TYPE_UINT8, DL_FORMAT_SCALAR, DL_%s, \"%s\");\n" s s;
List.iter (print_field h) fields;
fprintf h "\t trans->end_message(trans->impl, dev);\n";
fprintf h "\t} else\n";
fprintf h "\t trans->overrun(trans->impl, dev);\n";
fprintf h "}\n\n"
let print_null_downlink_macro = fun h {name=s; fields = fields} ->
fprintf h "#define DOWNLINK_SEND_%s(_trans, _dev" s;
print_macro_parameters h fields;
fprintf h ") {}\n";
fprintf h "void pprz_msg_send_%s(struct transport_tx *trans, struct link_device *dev, uint8_t ac_id" s;
print_function_parameters h fields;
fprintf h ") {}\n"
(** Prints the messages ids *)
let print_enum = fun h class_ messages ->
List.iter (fun m ->
if m.id < 0 || m.id > 255 then begin
fprintf stderr "Error: message %s has id %d but should be between 0 and 255\n" m.name m.id; exit 1;
end
else fprintf h "#define DL_%s %d\n" m.name m.id
) messages;
fprintf h "#define DL_MSG_%s_NB %d\n\n" class_ (List.length messages)
(** Prints the table of the messages lengths *)
let print_lengths_array = fun h class_ messages ->
let sizes = List.map (fun m -> (m.id, size_of_message m)) messages in
let max_id = List.fold_right (fun (id, _m) x -> max x id) sizes min_int in
let n = max_id + 1 in
fprintf h "#define MSG_%s_LENGTHS {" class_;
for i = 0 to n - 1 do
fprintf h "%s," (try "(2+" ^ List.assoc i sizes^")" with Not_found -> "0")
done;
fprintf h "}\n\n";
(* Print a comment with the actual size (when not variable) *)
fprintf h "/*\n Size for non variable messages\n";
let sizes =
List.map
(fun m -> (estimated_size_of_message m, m.name))
messages in
let sizes = List.sort (fun (s1,_) (s2,_) -> compare s2 s1) sizes in
List.iter
(fun (s, id) -> fprintf h "%2d : %s\n" s id)
sizes;
fprintf h "*/\n"
(** Prints the macros required to send a message *)
let print_downlink_macros = fun h class_ messages ->
print_enum h class_ messages;
(*print_lengths_array h class_ messages;*)
List.iter (print_downlink_macro h) messages
let print_null_downlink_macros = fun h messages ->
List.iter (print_null_downlink_macro h) messages
(** Prints the macro to get access to the fields of a received message *)
let print_get_macros = fun h check_alignment message ->
let msg_name = message.name in
let offset = ref Pprz.offset_fields in
(** Prints the macro for one field, using the global [offset] ref *)
let parse_field = fun (_type, field_name, _format) ->
if !offset < 0 then
failwith "FIXME: No field allowed after an array field (print_get_macros)";
(** Converts bytes into the required type *)
let typed = fun o pprz_type -> (* o for offset *)
let size = pprz_type.Pprz.size in
if check_alignment && o mod (min size 4) <> 0 then
failwith (sprintf "Wrong alignment of field '%s' in message '%s" field_name msg_name);
match size with
1 -> sprintf "(%s)(*((uint8_t*)_payload+%d))" pprz_type.Pprz.inttype o
| 2 -> sprintf "(%s)(*((uint8_t*)_payload+%d)|*((uint8_t*)_payload+%d+1)<<8)" pprz_type.Pprz.inttype o o
| 4 when pprz_type.Pprz.inttype = "float" ->
sprintf "({ union { uint32_t u; float f; } _f; _f.u = (uint32_t)(*((uint8_t*)_payload+%d)|*((uint8_t*)_payload+%d+1)<<8|((uint32_t)*((uint8_t*)_payload+%d+2))<<16|((uint32_t)*((uint8_t*)_payload+%d+3))<<24); _f.f; })" o o o o
| 8 when pprz_type.Pprz.inttype = "double" ->
let s = ref (sprintf "*((uint8_t*)_payload+%d)" o) in
for i = 1 to 7 do
s := !s ^ sprintf "|((uint64_t)*((uint8_t*)_payload+%d+%d))<<%d" o i (8*i)
done;
sprintf "({ union { uint64_t u; double f; } _f; _f.u = (uint64_t)(%s); Swap32IfBigEndian(_f.u); _f.f; })" !s
| 4 ->
sprintf "(%s)(*((uint8_t*)_payload+%d)|*((uint8_t*)_payload+%d+1)<<8|((uint32_t)*((uint8_t*)_payload+%d+2))<<16|((uint32_t)*((uint8_t*)_payload+%d+3))<<24)" pprz_type.Pprz.inttype o o o o
| 8 ->
let s = ref (sprintf "(%s)(*((uint8_t*)_payload+%d)" pprz_type.Pprz.inttype o) in
for i = 1 to 7 do
s := !s ^ sprintf "|((uint64_t)*((uint8_t*)_payload+%d+%d))<<%d" o i (8*i)
done;
sprintf "%s)" !s
| _ -> failwith "unexpected size in Gen_messages.print_get_macros" in
(** To be an array or not to be an array: *)
match _type with
Basic t ->
let pprz_type = Syntax.assoc_types t in
fprintf h "#define DL_%s_%s(_payload) (%s)\n" msg_name field_name (typed !offset pprz_type);
offset := !offset + pprz_type.Pprz.size
| Array (t, _varname) ->
(** The macro to access to the length of the array *)
fprintf h "#define DL_%s_%s_length(_payload) (%s)\n" msg_name field_name (typed !offset (Syntax.assoc_types "uint8"));
incr offset;
(** The macro to access to the array itself *)
let pprz_type = Syntax.assoc_types t in
if check_alignment && !offset mod (min pprz_type.Pprz.size 4) <> 0 then
failwith (sprintf "Wrong alignment of field '%s' in message '%s" field_name msg_name);
fprintf h "#define DL_%s_%s(_payload) ((%s*)(_payload+%d))\n" msg_name field_name pprz_type.Pprz.inttype !offset;
offset := -1 (** Mark for no more fields *)
| FixedArray (t, _varname, len) ->
(** The macro to access to the length of the array *)
fprintf h "#define DL_%s_%s_length(_payload) (%d)\n" msg_name field_name len;
(** The macro to access to the array itself *)
let pprz_type = Syntax.assoc_types t in
if check_alignment && !offset mod (min pprz_type.Pprz.size 4) <> 0 then
failwith (sprintf "Wrong alignment of field '%s' in message '%s" field_name msg_name);
fprintf h "#define DL_%s_%s(_payload) ((%s*)(_payload+%d))\n" msg_name field_name pprz_type.Pprz.inttype !offset;
offset := !offset + (pprz_type.Pprz.size*len)
in
fprintf h "\n";
(** Do it for all the fields of the message *)
List.iter parse_field message.fields
end (* module Gen_onboard *)
(********************* Main **************************************************)
let () =
if Array.length Sys.argv <> 3 then begin
failwith (sprintf "Usage: %s <.xml file> <class_name>" Sys.argv.(0))
end;
let filename = Sys.argv.(1)
and class_name = Sys.argv.(2) in
try
let messages = Syntax.read filename class_name in
let h = stdout in
Printf.fprintf h "/* Automatically generated from %s */\n" filename;
Printf.fprintf h "/* Please DO NOT EDIT */\n";
Printf.fprintf h "/* Macros to send and receive messages of class %s */\n" class_name;
Printf.fprintf h "#ifndef _VAR_MESSAGES_%s_H_\n" class_name;
Printf.fprintf h "#define _VAR_MESSAGES_%s_H_\n" class_name;
Printf.fprintf h "#include \"subsystems/datalink/transport.h\"\n";
Printf.fprintf h "#include \"mcu_periph/link_device.h\"\n";
(** Macros for airborne downlink (sending) *)
if class_name = "telemetry" then begin (** FIXME *)
Printf.fprintf h "#ifdef DOWNLINK\n"
end;
Gen_onboard.print_downlink_macros h class_name messages;
if class_name = "telemetry" then begin
Printf.fprintf h "#else // DOWNLINK\n";
Gen_onboard.print_null_downlink_macros h messages;
Printf.fprintf h "#endif // DOWNLINK\n"
end;
(** Macros for airborne datalink (receiving) *)
let check_alignment = class_name <> "telemetry" in
List.iter (Gen_onboard.print_get_macros h check_alignment) messages;
Printf.fprintf h "#endif // _VAR_MESSAGES_%s_H_\n" class_name
with
Xml.Error (msg, pos) -> failwith (sprintf "%s:%d : %s\n" filename (Xml.line pos) (Xml.error_msg msg))