From e08a570a138915b8d115dbcf21612deb53b7e666 Mon Sep 17 00:00:00 2001 From: Joost Wooning Date: Tue, 20 Apr 2021 17:50:40 +0200 Subject: [PATCH] create library for using locationd in python using ffi (#20711) * create library for using locationd in python using ffi * lib is not required in release * cleanup * dont build liblocationd on release * add check on buffer size --- selfdrive/locationd/SConscript | 10 +++++++--- selfdrive/locationd/liblocationd.cc | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100755 selfdrive/locationd/liblocationd.cc diff --git a/selfdrive/locationd/SConscript b/selfdrive/locationd/SConscript index b7d0e128b..4b7fba19b 100644 --- a/selfdrive/locationd/SConscript +++ b/selfdrive/locationd/SConscript @@ -10,9 +10,13 @@ if GetOption('kaitai'): env.Program("ubloxd", ["ubloxd.cc", "ublox_msg.cc", "generated/ubx.cpp", "generated/gps.cpp"], LIBS=loc_libs) -ekf_sym_cc = env.Object("#rednose/helpers/ekf_sym.cc") +ekf_sym_cc = env.SharedObject("#rednose/helpers/ekf_sym.cc") +locationd_sources = ["locationd.cc", "models/live_kf.cc", ekf_sym_cc] lenv = env.Clone() lenv["_LIBFLAGS"] += f' {libkf[0].get_labspath()}' -locationd = lenv.Program("locationd", ["locationd.cc", "models/live_kf.cc", ekf_sym_cc], - LIBS=loc_libs + transformations) +locationd = lenv.Program("locationd", locationd_sources, LIBS=loc_libs + transformations) lenv.Depends(locationd, libkf) + +if File("liblocationd.cc").exists(): + liblocationd = lenv.SharedLibrary("liblocationd", ["liblocationd.cc"] + locationd_sources, LIBS=loc_libs + transformations) + lenv.Depends(liblocationd, libkf) diff --git a/selfdrive/locationd/liblocationd.cc b/selfdrive/locationd/liblocationd.cc new file mode 100755 index 000000000..f324649ae --- /dev/null +++ b/selfdrive/locationd/liblocationd.cc @@ -0,0 +1,22 @@ +#include "locationd.h" + +extern "C" { + typedef Localizer* Localizer_t; + + Localizer *localizer_init() { + return new Localizer(); + } + + void localizer_get_message_bytes(Localizer *localizer, uint64_t logMonoTime, + bool inputsOK, bool sensorsOK, bool gpsOK, char *buff, size_t buff_size) + { + MessageBuilder msg_builder; + kj::ArrayPtr arr = localizer->get_message_bytes(msg_builder, logMonoTime, inputsOK, sensorsOK, gpsOK).asChars(); + assert(buff_size >= arr.size()); + memcpy(buff, arr.begin(), arr.size()); + } + + void localizer_handle_msg_bytes(Localizer *localizer, const char *data, size_t size) { + localizer->handle_msg_bytes(data, size); + } +}