Example of CAML call from C

This commit is contained in:
Pascal Brisset
2008-12-11 14:21:32 +00:00
parent e0058a1809
commit 332be0256e
3 changed files with 47 additions and 0 deletions
+7
View File
@@ -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 $<
+30
View File
@@ -0,0 +1,30 @@
#include <stdio.h>
#include <string.h>
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/memory.h>
#include <caml/alloc.h>
#include <caml/custom.h>
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;
}
+10
View File
@@ -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