Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
wardru committed Dec 8, 2023
1 parent f935673 commit b41dcd2
Show file tree
Hide file tree
Showing 45 changed files with 1,428 additions and 1,202 deletions.
18 changes: 18 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
BasedOnStyle: LLVM
IndentWidth: 8
TabWidth: 8
UseTab: Always
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
AllowShortFunctionsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 80
AccessModifierOffset: -8
NamespaceIndentation: None
PointerAlignment: Right
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
FixNamespaceComments: false
AlignAfterOpenBracket: Align
11 changes: 11 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
Checks:
clang-diagnostic-*,
clang-analyzer-*,
cppcoreguidelines-*,
modernize-*,
-modernize-use-trailing-return-type,
-cppcoreguidelines-non-private-member-variables-in-classes
WarningsAsErrors: '*'
HeaderFilterRegex: '.*'
---
Binary file removed .conanfile.py.swp
Binary file not shown.
30 changes: 6 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,29 @@ cmake_minimum_required(VERSION 3.25)

include(cmake/version.cmake)
get_version_from_changelog()
project(PZACXX_LIBRARY VERSION ${PZACXX_VERSION_MAJOR}.${PZACXX_VERSION_MINOR}.${PZACXX_VERSION_PATCH} LANGUAGES CXX)
project(PZACXX_LIBRARY VERSION ${PZACXX_VERSION} LANGUAGES CXX)

set(LIBRARY_NAME pzacxx)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(cmake/tidy.cmake)
include(cmake/format.cmake)
include(cmake/compiler.cmake)
include(cmake/lib.cmake)

find_package(spdlog REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(PahoMqttCpp REQUIRED)
if (CMAKE_SYSTEM_NAME MATCHES "Linux")
find_package(GTest REQUIRED)
find_package(cppcheck REQUIRED)
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

include(cmake/compiler.cmake)
include(cmake/lib.cmake)
include(cmake/cppcheck.cmake)

add_subdirectory(source)
#add_subdirectory(test)

option(BUILD_EXAMPLES "Build examples" OFF)
if (BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

#if (CMAKE_SYSTEM_NAME MATCHES "Windows" AND BUILD_SHARED_LIBS)
# message(STATUS "Copying DLLs from ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin to ${CMAKE_BINARY_DIR}/bin")
# add_custom_command(TARGET ${LIBRARY_NAME} POST_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy_if_different
# ${CMAKE_BINARY_DIR}/${CMAKE_BUILD_TYPE}/bin/*.dll
# $<TARGET_FILE_DIR:${LIBRARY_NAME}>
# )
#endif()

set_target_properties(${LIBRARY_NAME} PROPERTIES
VERSION ${PROJECT_VERSION}
SOVERSION ${PROJECT_VERSION_MAJOR}
)
2 changes: 0 additions & 2 deletions cmake/compiler.cmake
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
include_guard()

add_compile_options(-Wall -Wextra)

if (CMAKE_SYSTEM_NAME MATCHES "Windows")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--allow-multiple-definition")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--allow-multiple-definition")
Expand Down
16 changes: 0 additions & 16 deletions cmake/cppcheck.cmake

This file was deleted.

19 changes: 19 additions & 0 deletions cmake/format.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
find_program(CLANG_FORMAT "clang-format")

file(GLOB_RECURSE ALL_SOURCE_FILES
${CMAKE_SOURCE_DIR}/source/*.cxx
${CMAKE_SOURCE_DIR}/source/*.hxx
${CMAKE_SOURCE_DIR}/include/*.hxx
${CMAKE_SOURCE_DIR}/examples/*.cxx
${CMAKE_SOURCE_DIR}/examples/*.hxx
)

add_custom_target(format
COMMAND ${CLANG_FORMAT} --style=file -i ${ALL_SOURCE_FILES}
COMMENT "Running clang-format on all source files"
)

add_custom_target(format-check
COMMAND ${CLANG_FORMAT} --style=file -output-replacements-xml ${ALL_SOURCE_FILES} | grep \"<replacement \" && echo \"check-format failed. Run cmake --build <build> format\" && exit 1 || exit 0
COMMENT "Checking code format"
)
4 changes: 4 additions & 0 deletions cmake/tidy.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
find_program(CLANG_TIDY_EXECUTABLE clang-tidy REQUIRED)

set(CMAKE_CXX_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE})
set(CMAKE_C_CLANG_TIDY ${CLANG_TIDY_EXECUTABLE})
3 changes: 0 additions & 3 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ def requirements(self):
self.requires("paho-mqtt-cpp/[>=1.2.0]")
self.requires("spdlog/[>=1.12.0]")
self.requires("nlohmann_json/[>=3.11.2]")
if self.settings.os == "Linux":
self.requires("gtest/[>=1.14.0]")
self.requires("cppcheck/[>=2.12.1]")

def layout(self):
self.folders.build_folder_vars = ['settings.os', 'settings.compiler', 'options.shared']
Expand Down
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ foreach(example ${examples})
add_executable(${example} ${example}.cxx)
set_target_properties(${example} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/examples)
set_source_files_properties(${example}.cxx PROPERTIES
SKIP_LINTING ON
)
target_link_libraries(${example} ${LIBRARY_NAME})
endforeach()
98 changes: 50 additions & 48 deletions examples/bps.cxx
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
#include <pza/core/core.hxx>
#include <pza/core/client.hxx>
#include <pza/interfaces/voltmeter.hxx>
#include <pza/core/core.hxx>
#include <pza/interfaces/ammeter.hxx>
#include <pza/interfaces/bps_chan_ctrl.hxx>
#include <pza/interfaces/voltmeter.hxx>

static const std::string addr = "localhost";
static const unsigned int port = 1883;

int main(void)
int main()
{
spdlog::info("pza version: {}", pza::core::get_version());
spdlog::info("pza version hash: {}", pza::core::get_version_githash());
pza::core::set_log_level(pza::core::log_level::debug);

pza::client::s_ptr cli = std::make_shared<pza::client>("localhost", 1883);

if (cli->connect() < 0)
return -1;

if (cli->register_devices() < 0)
return -1;

auto vm = cli->get_interface<pza::itf::voltmeter>("default", "Panduza_FakeBps", "channel", 0, "vm");
if (!vm) {
spdlog::error("voltmeter not found");
return -1;
}

auto ctrl = cli->get_interface<pza::itf::bps_chan_ctrl>("default", "Panduza_FakeBps", "channel", 0, "ctrl");
if (!ctrl) {
spdlog::error("ctrl not found");
return -1;
}

auto am = cli->get_interface<pza::itf::ammeter>("default", "Panduza_FakeBps", "channel", 0, "am");
if (!am) {
spdlog::error("ammeter not found");
return -1;
}

ctrl->set_enable(true);
ctrl->set_voltage(3.3);
ctrl->set_current(0.1);

vm->register_measure_callback([&]() {
spdlog::info("voltage: {}", vm->get_measure());
});

am->register_measure_callback([&]() {
spdlog::info("current: {}", am->get_measure());
});

while (1)
;
return 0;
pza::core::set_log_level(pza::core::log_level::debug);

pza::client::s_ptr cli = std::make_shared<pza::client>(addr, port);

if (cli->connect() < 0)
return -1;

if (cli->register_devices() < 0)
return -1;

auto vm = cli->get_interface<pza::itf::voltmeter>(
"default", "Panduza_FakeBps", "channel", 0, "vm");
if (!vm) {
spdlog::error("voltmeter not found");
return -1;
}

auto ctrl = cli->get_interface<pza::itf::bps_chan_ctrl>(
"default", "Panduza_FakeBps", "channel", 0, "ctrl");
if (!ctrl) {
spdlog::error("ctrl not found");
return -1;
}

auto am = cli->get_interface<pza::itf::ammeter>(
"default", "Panduza_FakeBps", "channel", 0, "am");
if (!am) {
spdlog::error("ammeter not found");
return -1;
}

ctrl->set_enable(true);
ctrl->set_voltage(3.3);
ctrl->set_current(0.1);

vm->register_measure_callback(
[&]() { spdlog::info("voltage: {}", vm->get_measure()); });

am->register_measure_callback(
[&]() { spdlog::info("current: {}", am->get_measure()); });

while (true)
;
return 0;
}
91 changes: 57 additions & 34 deletions include/pza/core/client.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,74 @@ namespace pza
class client
{
public:
using s_ptr = std::shared_ptr<client>;
using u_ptr = std::unique_ptr<client>;
using w_ptr = std::weak_ptr<client>;
using s_ptr = std::shared_ptr<client>;
using u_ptr = std::unique_ptr<client>;
using w_ptr = std::weak_ptr<client>;

explicit client(const std::string &addr, int port, std::optional<std::string> id = std::nullopt);
~client();
explicit client(const std::string &addr, int port,
std::optional<std::string> id = std::nullopt);
client(const client &) = delete;
client &operator=(const client &) = delete;
client(client &&) = delete;
client &operator=(client &&) = delete;
~client();

int connect(void);
int disconnect(void);
bool is_connected(void) const;
int connect();
int disconnect();
[[nodiscard]] bool is_connected() const;

const std::string &get_addr(void) const;
const std::string &get_id(void) const;
int get_port(void) const;
[[nodiscard]] const std::string &get_addr() const;
[[nodiscard]] const std::string &get_id() const;
[[nodiscard]] int get_port() const;

void set_connection_timeout(unsigned int timeout_ms);
unsigned int get_connection_timeout(void) const;
void set_connection_timeout(unsigned int timeout_ms);
[[nodiscard]] unsigned int get_connection_timeout() const;

device::s_ptr register_device(const std::string &group, const std::string &name, unsigned int timeout_ms = device_timeout_default);
int register_devices(unsigned int timeout_ms = device_timeout_default * 5);
device::s_ptr
register_device(const std::string &group, const std::string &name,
unsigned int timeout_ms = device_timeout_default);
int register_devices(unsigned int timeout_ms = devices_timeout_default);

device::s_ptr get_device(const std::string &group, const std::string &name) const;
std::vector<device::s_ptr> get_devices() const;
std::vector<device::s_ptr> get_devices_in_group(const std::string &group) const;
std::set<std::string> get_groups() const;
[[nodiscard]] device::s_ptr get_device(const std::string &group,
const std::string &name) const;
[[nodiscard]] std::vector<device::s_ptr> get_devices() const;
[[nodiscard]] std::vector<device::s_ptr>
get_devices_in_group(const std::string &group) const;
[[nodiscard]] std::set<std::string> get_groups() const;

itf_base::s_ptr get_interface(const std::string &group, const std::string &name, const std::string &interface_group, unsigned int idx, const std::string &interface_name) const;
itf_base::s_ptr get_interface(const std::string &group, const std::string &name, const std::string &interface_name) const;
[[nodiscard]] itf_base::s_ptr
get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
const std::string &interface_name) const;
[[nodiscard]] itf_base::s_ptr
get_interface(const std::string &group, const std::string &name,
const std::string &interface_name) const;

template<typename T>
std::shared_ptr<T> get_interface(const std::string &group, const std::string &name, const std::string &interface_group, unsigned int idx, const std::string &interface_name) const
{
return std::dynamic_pointer_cast<T>(get_interface(group, name, interface_group, idx, interface_name));
}
template <typename T>
[[nodiscard]] std::shared_ptr<T>
get_interface(const std::string &group, const std::string &name,
const std::string &interface_group, unsigned int idx,
const std::string &interface_name) const
{
return std::dynamic_pointer_cast<T>(get_interface(
group, name, interface_group, idx, interface_name));
}

template<typename T>
std::shared_ptr<T> get_interface(const std::string &group, const std::string &name, const std::string &interface_name) const
{
return std::dynamic_pointer_cast<T>(get_interface(group, name, interface_name));
}
template <typename T>
[[nodiscard]] std::shared_ptr<T>
get_interface(const std::string &group, const std::string &name,
const std::string &interface_name) const
{
return std::dynamic_pointer_cast<T>(
get_interface(group, name, interface_name));
}

private:
static constexpr unsigned int platforms_timeout_default = 500;
static constexpr unsigned int device_timeout_default = 500;
static constexpr unsigned int platforms_timeout_default = 500;
static constexpr unsigned int device_timeout_default = 500;
static constexpr unsigned int devices_timeout_default =
device_timeout_default * 5;

std::unique_ptr<client_impl> _impl;
std::unique_ptr<client_impl> _impl;
};
};
Loading

0 comments on commit b41dcd2

Please sign in to comment.