@@ -193,6 +193,36 @@ let output_sizeof_sig _loc s =
193193 value $ lid:" sizeof_" ^ s.name$ : int
194194 >>
195195
196+ let output_hexdump _loc s =
197+ let hexdump =
198+ List. fold_left (fun a f ->
199+ < :expr<
200+ $ a$ ; Buffer. add_string _buf $ str:" " ^ f.field^ " = " $ ;
201+ $ match f.ty with
202+ | UInt8 | UInt16 -> < :expr< Printf. bprintf _buf " 0x%x\n " ($ lid:getter_name s f$ v ) >>
203+ | UInt32 -> < :expr< Printf. bprintf _buf " 0x%lx\n " ($ lid:getter_name s f$ v ) >>
204+ | UInt64 -> < :expr< Printf. bprintf _buf " 0x%Lx\n " ($ lid:getter_name s f$ v ) >>
205+ | Buffer len -> < :expr< Printf. bprintf _buf " <buffer length %d>" $ int :string_of_int len$ ; Cstruct. hexdump_to_buffer _buf ($ lid:getter_name s f$ v ) >>
206+ $ >>
207+ ) < :expr< >> s.fields
208+ in
209+ < :str_item<
210+ value $ lid:" hexdump_" ^ s.name^ " _to_buffer" $ _buf v = do { $ hexdump$ };
211+ value $ lid:" hexdump_" ^ s.name$ v =
212+ let _buf = Buffer. create 128 in
213+ do {
214+ Buffer. add_string _buf $ str :s.name ^ " = {\n " $ ;
215+ $ lid :"hexdump_" ^ s.name^ "_to_buffer" $ _buf v ;
216+ print_endline (Buffer. contents _buf);
217+ print_endline " }"
218+ } >>
219+
220+ let output_hexdump_sig _loc s =
221+ < :sig_item<
222+ value $ lid:" hexdump_" ^ s.name^ " _to_buffer" $ : Buffer. t -> Cstruct. t -> unit ;
223+ value $ lid:" hexdump_" ^ s.name$ : Cstruct. t -> unit ;
224+ >>
225+
196226let output_struct _loc s =
197227 (* Generate functions of the form {get/set}_<struct>_<field> *)
198228 let expr = List. fold_left (fun a f ->
@@ -202,7 +232,7 @@ let output_struct _loc s =
202232 $ output_set _loc s f$
203233 >>
204234 ) < :str_item< $ output_sizeof _loc s$ >> s.fields
205- in expr
235+ in < :str_item < $ expr$ ; $ output_hexdump _loc s $ >>
206236
207237let output_struct_sig _loc s =
208238 (* Generate signaturs of the form {get/set}_<struct>_<field> *)
@@ -213,7 +243,7 @@ let output_struct_sig _loc s =
213243 $ output_set_sig _loc s f$ ;
214244 >>
215245 ) < :sig_item< $ output_sizeof_sig _loc s$ >> s.fields
216- in expr
246+ in < :sig_item < $ expr$ ; $ output_hexdump_sig _loc s $ >>
217247
218248let output_enum _loc name fields width =
219249 let intfn,pattfn = match ty_of_string width with
0 commit comments