Compare commits
4 Commits
13b22b0d1c
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| c6ee8f6c5b | |||
| 4491f27c68 | |||
| 60dbba7a5f | |||
|
|
176022d490 |
@@ -45,6 +45,11 @@ if(ADCLIB_BUILD_EXAM)
|
||||
target_link_libraries(device_exam PRIVATE ${PROJECT_NAME})
|
||||
endif()
|
||||
|
||||
if(ADCLIB_BUILD_EXAM)
|
||||
add_executable(netmsg_exam examples/netmsg_exam.cpp)
|
||||
target_link_libraries(netmsg_exam PRIVATE ${PROJECT_NAME})
|
||||
endif()
|
||||
|
||||
include(GNUInstallDirs)
|
||||
install(
|
||||
TARGETS ${PROJECT_NAME}
|
||||
|
||||
31
examples/netmsg_exam.cpp
Normal file
31
examples/netmsg_exam.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <print>
|
||||
|
||||
#include <adclib/adclib_netmsg.h>
|
||||
|
||||
int main()
|
||||
{
|
||||
adc::AdcNetworkMessage msg;
|
||||
|
||||
std::println("is ACK: {}", msg.isACK());
|
||||
|
||||
msg.createSET("ATTR1", snplib::snplib_serialization_params_t{}, 10, std::vector<char>{'V', 'A', 'L'});
|
||||
|
||||
std::println("KEY: <{}>", msg.key());
|
||||
|
||||
std::println("VALUE: <{}>", msg.value());
|
||||
|
||||
std::println("is SET: {}", msg.isSET());
|
||||
|
||||
std::println("is VALID: {}", msg.isValid());
|
||||
|
||||
|
||||
std::string str{"SET ATTRQ 32.235 dwejh kd2jl"};
|
||||
|
||||
msg.fromCharRange(str);
|
||||
|
||||
std::println("KEY: <{}>", msg.key());
|
||||
|
||||
std::println("VALUE: <{}>", msg.value());
|
||||
|
||||
return 0;
|
||||
}
|
||||
179
include/adclib/adclib_netmsg.h
Normal file
179
include/adclib/adclib_netmsg.h
Normal file
@@ -0,0 +1,179 @@
|
||||
#pragma once
|
||||
|
||||
#include <snipplib/utils/snplib_keyvalue_expr.h>
|
||||
|
||||
namespace adc
|
||||
{
|
||||
|
||||
/* */
|
||||
|
||||
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_PARAM_DELIM{" "};
|
||||
static constexpr std::string_view ADCLIB_NETMSG_PARAM_PARAM_DELIM{" "};
|
||||
|
||||
/* network message valid keywords */
|
||||
//
|
||||
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_ACK{"ACK"};
|
||||
|
||||
// error message
|
||||
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_ERR{"ERR"};
|
||||
|
||||
// set device attribute value
|
||||
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_SET{"SET"};
|
||||
|
||||
// get device attribute value
|
||||
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_GET{"GET"};
|
||||
|
||||
// execute device command
|
||||
static constexpr std::string_view ADCLIB_NETMSG_KEYWORD_CMD{"CMD"};
|
||||
|
||||
|
||||
static constexpr std::array ADCLIB_NETMSG_VALID_KEYWORDS{ADCLIB_NETMSG_KEYWORD_ACK, ADCLIB_NETMSG_KEYWORD_ERR,
|
||||
ADCLIB_NETMSG_KEYWORD_SET, ADCLIB_NETMSG_KEYWORD_GET,
|
||||
ADCLIB_NETMSG_KEYWORD_CMD};
|
||||
|
||||
|
||||
template <snplib::snplib_char_range_c CharRangeT = std::vector<char>>
|
||||
class AdcNetworkMessage final : protected snplib::snplib_keyvalue_expr_t<CharRangeT>
|
||||
{
|
||||
typedef snplib::snplib_keyvalue_expr_t<CharRangeT> base_t;
|
||||
|
||||
public:
|
||||
using typename base_t::error_t;
|
||||
|
||||
using base_t::isValid;
|
||||
using base_t::key;
|
||||
using base_t::value;
|
||||
using base_t::valueSeq;
|
||||
|
||||
AdcNetworkMessage() = default;
|
||||
|
||||
bool isACK() const
|
||||
{
|
||||
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_ACK);
|
||||
}
|
||||
|
||||
bool isERR() const
|
||||
{
|
||||
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_ERR);
|
||||
}
|
||||
|
||||
bool isCMD() const
|
||||
{
|
||||
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_CMD);
|
||||
}
|
||||
|
||||
bool isSET() const
|
||||
{
|
||||
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_SET);
|
||||
}
|
||||
|
||||
bool isGET() const
|
||||
{
|
||||
return std::ranges::equal(this->_key, ADCLIB_NETMSG_KEYWORD_GET);
|
||||
}
|
||||
|
||||
error_t fromCharRange(snplib::snplib_input_char_range_c auto&& bytes)
|
||||
requires(!std::same_as<CharRangeT, std::remove_cvref_t<decltype(bytes)>>)
|
||||
{
|
||||
auto err = fromCharRange(
|
||||
std::forward<decltype(bytes)>(bytes),
|
||||
{.keyval_delim = ADCLIB_NETMSG_KEYWORD_PARAM_DELIM, .valseq_delim = ADCLIB_NETMSG_PARAM_PARAM_DELIM});
|
||||
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (!std::ranges::contains(ADCLIB_NETMSG_VALID_KEYWORDS, key())) {
|
||||
this->_isValid = false;
|
||||
// return an error here!!!
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
error_t fromCharRange(CharRangeT& bytes)
|
||||
{
|
||||
auto err = base_t::fromCharRange(bytes, {.keyval_delim = ADCLIB_NETMSG_KEYWORD_PARAM_DELIM,
|
||||
.valseq_delim = ADCLIB_NETMSG_PARAM_PARAM_DELIM});
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
if (!std::ranges::contains(ADCLIB_NETMSG_VALID_KEYWORDS, key())) {
|
||||
this->_isValid = false;
|
||||
// return an error here!!!
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
error_t createACK()
|
||||
{
|
||||
return this->setKey(ADCLIB_NETMSG_KEYWORD_ACK);
|
||||
}
|
||||
|
||||
|
||||
error_t createERR(error_t err)
|
||||
{
|
||||
return this->setKeyValue(ADCLIB_NETMSG_KEYWORD_ERR, err.value());
|
||||
}
|
||||
|
||||
template <snplib::snplib_serializable_c ATTR_NAME_T, typename... ArgTs>
|
||||
requires(sizeof...(ArgTs) > 0)
|
||||
error_t createSET(ATTR_NAME_T&& attr_name, ArgTs&&... args)
|
||||
{
|
||||
this->_isValid = false;
|
||||
|
||||
auto err = this->setKeyValue(ADCLIB_NETMSG_KEYWORD_SET, std::forward<ATTR_NAME_T>(attr_name));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
err = this->appendValue(std::forward<ArgTs>(args)...);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
this->_isValid = true;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
template <snplib::snplib_serializable_c ATTR_NAME_T, typename... ArgTs>
|
||||
error_t createGET(ATTR_NAME_T&& attr_name, ArgTs&&... args)
|
||||
{
|
||||
this->_isValid = false;
|
||||
|
||||
auto err = this->setKeyValue(ADCLIB_NETMSG_KEYWORD_GET, std::forward<ATTR_NAME_T>(attr_name));
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
this->_isValid = true;
|
||||
|
||||
if constexpr (sizeof...(ArgTs)) {
|
||||
err = this->appendValue(std::forward<ArgTs>(args)...);
|
||||
if (err) {
|
||||
this->_isValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
template <snplib::snplib_serializable_c CMD_NAME_T>
|
||||
error_t createCMD(CMD_NAME_T&& cmd_name)
|
||||
{
|
||||
this->_isValid = false;
|
||||
|
||||
auto err = this->setKeyValue(ADCLIB_NETMSG_KEYWORD_CMD, std::forward<CMD_NAME_T>(cmd_name));
|
||||
if (!err) {
|
||||
this->_isValid = true;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace adc
|
||||
Reference in New Issue
Block a user