From 3e16a23768680d95090e5c1d75a19395980e0a91 Mon Sep 17 00:00:00 2001 From: John Estabrook Date: Wed, 23 Oct 2024 19:48:31 -0500 Subject: T5528: add adapter interface Expose legacy set/delete functions in a form suitable for external calls. --- src/vy_adapter.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/vy_adapter.h | 31 ++++++++++++++++ 2 files changed, 133 insertions(+) create mode 100644 src/vy_adapter.cpp create mode 100644 src/vy_adapter.h (limited to 'src') diff --git a/src/vy_adapter.cpp b/src/vy_adapter.cpp new file mode 100644 index 0000000..793728f --- /dev/null +++ b/src/vy_adapter.cpp @@ -0,0 +1,102 @@ +/* Copyright 2024 VyOS maintainers and contributors + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace cstore; + +static uint64_t uint_of_voidptr(void* p) +{ + assert (((uintptr_t) p & 1) == 0); + return (uint64_t) p | 1; +} + +static void *voidptr_of_uint(uint64_t v) +{ + return (void *)(uintptr_t)(v & ~1); +} + +static Cstore *cstore_of_handle(uint64_t handle) +{ + return (Cstore *) voidptr_of_uint(handle); +} + +uint64_t +vy_cstore_init(void) +{ + Cstore *handle = Cstore::createCstore(false); + return uint_of_voidptr(handle); +} + +void +vy_cstore_free(uint64_t handle) +{ + Cstore *h = cstore_of_handle(handle); + delete h; +} + +int +vy_in_session(uint64_t handle) +{ + Cstore *h = cstore_of_handle(handle); + return h->inSession() ? 1 : 0; +} + +int +vy_set_path(uint64_t handle, const void** path_ptr, size_t len) +{ + Cstore *cstore = cstore_of_handle(handle); + const char **path = (const char **) path_ptr; + Cpath path_comps = Cpath(path, len); + int res; + + res = cstore->validateSetPath(path_comps); + if (!res) { + return 1; + } + + res = cstore->setCfgPath(path_comps); + if (!res) { + return 2; + } + + return 0; +} + +int +vy_delete_path(uint64_t handle, const void** path_ptr, size_t len) +{ + Cstore *cstore = cstore_of_handle(handle); + const char **path = (const char **) path_ptr; + Cpath path_comps = Cpath(path, len); + int res; + + res = cstore->deleteCfgPath(path_comps); + if (!res) { + return 1; + } + + return 0; +} diff --git a/src/vy_adapter.h b/src/vy_adapter.h new file mode 100644 index 0000000..8e0f376 --- /dev/null +++ b/src/vy_adapter.h @@ -0,0 +1,31 @@ +/* Copyright 2024 VyOS maintainers and contributors + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this library. If not, see . + */ + +#ifndef _VY_ADAPTER_H_ +#define _VY_ADAPTER_H_ +#ifdef __cplusplus +extern "C" { +#endif + +uint64_t vy_cstore_init(void); +void vy_cstore_free(uint64_t); +int vy_in_session(uint64_t); +int vy_set_path(uint64_t, const void **, size_t); +int vy_delete_path(uint64_t, const void **, size_t); + +#ifdef __cplusplus +} +#endif +#endif -- cgit v1.2.3