Skip to content

Commit

Permalink
Fixed length arrays without length byte in packet (FOR TESTING)
Browse files Browse the repository at this point in the history
  • Loading branch information
xgibert committed May 3, 2012
1 parent 75693cc commit 4c0c073
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
1 change: 0 additions & 1 deletion conf/messages/downlink.xml
Expand Up @@ -4,7 +4,6 @@
<field name="test" type="char[2]"/><!-- XGGDEBUG:REMOVE: -->
<field name="test2" type="uint16[3]"/>
<field name="var1" type="uint64"/>
<field name="var2" type="uint16"/>
<field name="var3" type="int32"/>
</message>

Expand Down
1 change: 0 additions & 1 deletion conf/messages/uplink.xml
Expand Up @@ -4,7 +4,6 @@
<field name="var1" type="uint64"/>
<field name="test" type="char[2]"/>
<field name="test32" type="uint16[3]"/>
<field name="var2" type="uint16"/>
<field name="var3" type="int32"/>
<field name="ac_id" type="uint8"/>
</message>
Expand Down
3 changes: 1 addition & 2 deletions sw/airborne/firmwares/fixedwing/datalink.c
Expand Up @@ -104,9 +104,8 @@ void dl_parse_msg(void) {
uint64_t var1 = DL_TEST_var1(dl_buffer);
char* test = DL_TEST_test(dl_buffer);
uint16_t* test32 = DL_TEST_test32(dl_buffer);
uint16_t var2 = DL_TEST_var2(dl_buffer);
int32_t var3 = DL_TEST_var3(dl_buffer);
DOWNLINK_SEND_ACKTEST(DefaultChannel, DefaultDevice, test, test32, &var1, &var2, &var3);
DOWNLINK_SEND_ACKTEST(DefaultChannel, DefaultDevice, test, test32, &var1, &var3);
/*uint32_t var1 = 123;
char test[2] = {'x','a'};
uint16_t test32[3] = {123,321,111};
Expand Down
Binary file modified sw/ground_segment/tmtc/c_new_formats_testing
Binary file not shown.
3 changes: 1 addition & 2 deletions sw/ground_segment/tmtc/c_new_formats_testing.c
Expand Up @@ -14,9 +14,8 @@ gboolean timeout_callback(gpointer data) {
char i3 = 'a';

int i4[3] = {1234,5432,1111};
int i5 = 1234;
int i6 = -1234;
IvySendMsg("ME TEST %llu %c,%c %d,%d,%d %d %d 1", i1,i2,i3,i4[0],i4[1],i4[2],i5,i6);
IvySendMsg("ME TEST %llu %c,%c %d,%d,%d %d 1", i1,i2,i3,i4[0],i4[1],i4[2],i6);
return TRUE;
}

Expand Down
58 changes: 41 additions & 17 deletions sw/lib/ocaml/pprz.ml
Expand Up @@ -37,7 +37,7 @@ type format = string
type _type =
Scalar of string
| ArrayType of string
| FixedArrayType of string
| FixedArrayType of string * int
type value =
Int of int | Float of float | String of string | Int32 of int32 | Char of char | Int64 of int64
| Array of value array
Expand Down Expand Up @@ -130,6 +130,15 @@ let type_of_fixed_array_type = fun s ->
with
| Failure str -> failwith(sprintf "Pprz.type_of_fixed_array_type: length is not an integer")

let length_of_fixed_array_type = fun s ->
try
let type_parts = Str.full_split (Str.regexp "[][]") s in
match type_parts with
| [Str.Text ty; Str.Delim "["; Str.Text len ; Str.Delim "]"] -> begin ignore( int_of_string (len)); len end
| _ -> failwith("Pprz.type_of_fixed_array_type is not a fixed array type")
with
| Failure str -> failwith(sprintf "Pprz.type_of_fixed_array_type: length is not an integer")

let int_of_string = fun x ->
try int_of_string x with
_ -> failwith (sprintf "Pprz.int_of_string: %s" x)
Expand All @@ -144,7 +153,7 @@ let rec value = fun t v ->
| Scalar "char" -> Char v.[0]
| ArrayType t' ->
Array (Array.map (value (Scalar t')) (Array.of_list (split_array v)))
| FixedArrayType t' ->
| FixedArrayType (t',l') ->
Array (Array.map (value (Scalar t')) (Array.of_list (split_array v))) (* XGGDEBUG:FIXEDARRAY: copy paste of avobe *)
| Scalar t -> failwith (sprintf "Pprz.value: Unexpected type: %s" t)

Expand Down Expand Up @@ -172,17 +181,24 @@ let sizeof = fun f ->
match f with
Scalar t -> (List.assoc t types).size
| ArrayType t -> failwith "sizeof: Array"
| FixedArrayType t -> failwith "sizeof: Array"
| FixedArrayType (t,l) -> failwith "sizeof: Array"
let size_of_field = fun f -> sizeof f._type
let default_format = function
Scalar x | ArrayType x | FixedArrayType x ->
Scalar x | ArrayType x ->
try (List.assoc x types).format with
Not_found -> failwith (sprintf "Unknown format '%s'" x)

let default_format_for_fixed = function
FixedArrayType (x,l) ->
try (List.assoc x types).format with
Not_found -> failwith (sprintf "Unknown format '%s'" x)


let default_value = fun x ->
match x with
Scalar t -> (List.assoc t types).value
| ArrayType t -> failwith "default_value: Array"
| FixedArrayType t -> failwith "default_value: Array"
| FixedArrayType (t,l) -> failwith "default_value: Array"

let payload_size_of_message = fun message ->
List.fold_right
Expand Down Expand Up @@ -229,9 +245,14 @@ let alt_unit_coef_of_xml = function xml ->

let pipe_regexp = Str.regexp "|"
let field_of_xml = fun xml ->
let t = ExtXml.attrib xml "type" in
let t = if is_array_type t then ArrayType (type_of_array_type t) else if is_fixed_array_type t then FixedArrayType (type_of_fixed_array_type t) else Scalar t in
let f = try Xml.attrib xml "format" with _ -> default_format t in
let tt = ExtXml.attrib xml "type" in
let t = if is_array_type tt then ArrayType (type_of_array_type tt) else if is_fixed_array_type tt then FixedArrayType (type_of_fixed_array_type tt, int_of_string(length_of_fixed_array_type tt)) else Scalar tt in
let f = try Xml.attrib xml "format" with _ ->
if(is_fixed_array_type tt) then
default_format_for_fixed t
else
default_format t in

let auc = alt_unit_coef_of_xml xml in
let values = try Str.split pipe_regexp (Xml.attrib xml "values") with _ -> [] in

Expand Down Expand Up @@ -309,14 +330,16 @@ let rec value_of_bin = fun buffer index _type ->
let size = 1 + n * s in
(Array (Array.init n
(fun i -> fst (value_of_bin buffer (index+1+i*s) type_of_elt))), size)
| FixedArrayType t -> (* XGGDEBUG:FIXEDARRAY: no length byte *)
| FixedArrayType (t,l) -> (* XGGDEBUG:FIXEDARRAY: no length byte *)
(** First get the number of values *)
let n = int8_of_bytes buffer index in
(*let n = int8_of_bytes buffer index in*)
(* XGGDEBUG:FIXEDARRAY: get num of bytes *)
let n = l in
let type_of_elt = Scalar t in
let s = sizeof type_of_elt in
let size = 1 + n * s in
let size = 0 + n * s in
(Array (Array.init n
(fun i -> fst (value_of_bin buffer (index+1+i*s) type_of_elt))), size)
(fun i -> fst (value_of_bin buffer (index+0+i*s) type_of_elt))), size)
| Scalar "string" ->
let n = Char.code buffer.[index] in
(String (String.sub buffer (index+1) n), (1+n))
Expand Down Expand Up @@ -376,16 +399,16 @@ let rec sprint_value = fun buf i _type v ->
ignore (sprint_value buf (i+1+j*s) type_of_elt values.(j))
done;
1 + n * s
| FixedArrayType t, Array values -> (* XGGDEBUG:FIXEDARRAY: not putting length byte *)
| FixedArrayType (t,l), Array values -> (* XGGDEBUG:FIXEDARRAY: not putting length byte *)
(** Put the size first, then the values *)
let n = Array.length values in
ignore (sprint_value buf i (Scalar "uint8") (Int n));
(*ignore (sprint_value buf i (Scalar "uint8") (Int n));*)
let type_of_elt = Scalar t in
let s = sizeof type_of_elt in
for j = 0 to n - 1 do
ignore (sprint_value buf (i+1+j*s) type_of_elt values.(j))
ignore (sprint_value buf (i+0+j*s) type_of_elt values.(j))
done;
1 + n * s
0 + n * s
| Scalar "string", String s ->
let n = String.length s in
assert (n < 256);
Expand All @@ -397,7 +420,8 @@ let rec sprint_value = fun buf i _type v ->
1 + n
| Scalar "char", Char c ->
buf.[i] <- c; sizeof _type
| (Scalar x|ArrayType x|FixedArrayType x), _ -> failwith (sprintf "Pprz.sprint_value (%s)" x)
| (Scalar x|ArrayType x), _ -> failwith (sprintf "Pprz.sprint_value (%s)" x)
| FixedArrayType (x,l), _ -> failwith (sprintf "Pprz.sprint_value (%s)" x)



Expand Down
2 changes: 1 addition & 1 deletion sw/lib/ocaml/pprz.mli
Expand Up @@ -36,7 +36,7 @@ type format = string
type _type =
Scalar of string
| ArrayType of string
| FixedArrayType of string
| FixedArrayType of string * int
type value =
Int of int | Float of float | String of string | Int32 of int32 | Char of char | Int64 of int64
| Array of value array
Expand Down
10 changes: 6 additions & 4 deletions sw/tools/gen_messages.ml
Expand Up @@ -71,7 +71,7 @@ module Syntax = struct
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 "1+%d*%s" len (sizeof (Basic t)) (* XGGDEBUG:FIXEDARRAY: no length byte *)
| FixedArray (t, varname, len) -> sprintf "0+%d*%s" len (sizeof (Basic t)) (* XGGDEBUG:FIXEDARRAY: no length byte *)

let rec nameof = function
Basic t -> String.capitalize t
Expand Down Expand Up @@ -130,7 +130,7 @@ module Gen_onboard = struct
fprintf h "\t DownlinkPut%sArray(_trans, _dev, %s, %s); \\\n" (Syntax.nameof (Basic t)) (Syntax.length_name varname) name
| FixedArray (t, varname, len) ->
let _s = Syntax.sizeof (Basic t) in
fprintf h "\t DownlinkPut%sArray(_trans, _dev, %d, %s); \\\n" (Syntax.nameof (Basic t)) len name (*XGGDEBUG:FIXEDARRAY: PutFixedArray *)
fprintf h "\t DownlinkPut%sFixedArray(_trans, _dev, %d, %s); \\\n" (Syntax.nameof (Basic t)) len name (*XGGDEBUG:FIXEDARRAY: PutFixedArray *)

let print_parameter h = function
(Array _, s, _) -> fprintf h "%s, %s" (Syntax.length_name s) s
Expand Down Expand Up @@ -273,8 +273,10 @@ module Gen_onboard = struct
(*in*)
| FixedArray (t, _varname, len) ->
(** 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; (* XGGDEBUG:FIXEDARRAY: There's a length byte *)
(*fprintf h "#define DL_%s_%s_length(_payload) (%s)\n" msg_name field_name (typed !offset (Syntax.assoc_types "uint8"));
incr offset;*)
(* XGGDEBUG:FIXEDARRAY: There's a length byte *)
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
Expand Down

0 comments on commit 4c0c073

Please sign in to comment.