diff --git a/sw/lib/ocaml/Makefile b/sw/lib/ocaml/Makefile index 48eff1ad8c..9a6df20b4d 100644 --- a/sw/lib/ocaml/Makefile +++ b/sw/lib/ocaml/Makefile @@ -26,6 +26,7 @@ INCLUDES= -I +xml-light -I +pcre -I +netstring XINCLUDES= -I +lablgl -I +lablgtk2 -I +xml-light OCAMLC=ocamlc OCAMLOPT=ocamlopt +OCAMLLIBDIR=$(shell ocamlc -where) SRC = debug.ml base64.ml serial.ml ocaml_tools.ml extXml.ml env.ml xml2h.ml latlong.ml srtm.ml http.ml gm.ml iGN.ml geometry_2d.ml cserial.o convert.o ubx.ml pprz.ml xbee.ml xmlCom.ml editAirframe.ml @@ -77,6 +78,12 @@ ml_gtkgl_hack.o : ml_gtkgl_hack.c @echo OC $< $(Q)$(OCAMLC) $(INCLUDES) -c -ccopt "$(GTKCFLAGS)" $< +camltm.o : register_example.cmo + $(OCAMLC) $(INCLUDES) -output-obj -o $@ unix.cma str.cma xml-light.cma ivy-ocaml.cma debug.cmo serial.cmo extXml.cmo env.cmo pprz.cmo tm.cmo + +caml_from_c_example : cserial.o convert.o caml_from_c_example.o camltm.o + $(CC) -o $@ $^ -L$(OCAMLLIBDIR) -lunix -lstr -livy-ocaml -lcamlrun -lm -livy -lcurses + %.cmo : %.ml @echo OC $< $(Q)$(OCAMLC) $(INCLUDES) -c $< diff --git a/sw/lib/ocaml/caml_from_c_example.c b/sw/lib/ocaml/caml_from_c_example.c new file mode 100644 index 0000000000..7d3b089325 --- /dev/null +++ b/sw/lib/ocaml/caml_from_c_example.c @@ -0,0 +1,30 @@ +#include +#include +#include +#include +#include +#include +#include + + +void print_ascii_to_binary(int n, char* msg) +{ + static value * caml_to_binary_closure = NULL; + if (caml_to_binary_closure == NULL) + caml_to_binary_closure = caml_named_value("to_binary"); + + value s = caml_callback2(*caml_to_binary_closure, Val_int(n), caml_copy_string(msg)); + + int i; + for(i = 0; i < string_length(s); i++) + printf("%02x ", Byte_u(s, i)); + printf("\n"); +} + +int main(int argc, char** argv) { + caml_startup(argv); + + print_ascii_to_binary(42, "ATTITUDE 7 22 33"); + + return 0; +} diff --git a/sw/lib/ocaml/register_example.ml b/sw/lib/ocaml/register_example.ml new file mode 100644 index 0000000000..64b99a9d98 --- /dev/null +++ b/sw/lib/ocaml/register_example.ml @@ -0,0 +1,10 @@ +module Tm_Pprz = Pprz.Messages (struct let name = "telemetry" end) + +let to_binary = fun ac_id m -> + let msg_id, vs = Tm_Pprz.values_of_string m in + let payload = Tm_Pprz.payload_of_values msg_id ac_id vs in + let buf = Pprz.Transport.packet payload in + Printf.printf "From caml: %s\n%!" (Debug.xprint buf); + buf + +let _ = Callback.register "to_binary" to_binary