From 15fa5ad163a6e56e661980cf05ae334d685b6a8e Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Sat, 9 May 2015 12:37:16 +0300 Subject: [PATCH] Param server moved to header --- .../include/uavcan/protocol/param_server.hpp | 89 ++++++++++++++++- libuavcan/src/protocol/uc_param_server.cpp | 95 ------------------- 2 files changed, 85 insertions(+), 99 deletions(-) delete mode 100644 libuavcan/src/protocol/uc_param_server.cpp diff --git a/libuavcan/include/uavcan/protocol/param_server.hpp b/libuavcan/include/uavcan/protocol/param_server.hpp index 8f7bdef5e0..e5afb04943 100644 --- a/libuavcan/include/uavcan/protocol/param_server.hpp +++ b/libuavcan/include/uavcan/protocol/param_server.hpp @@ -5,10 +5,13 @@ #ifndef UAVCAN_PROTOCOL_PARAM_SERVER_HPP_INCLUDED #define UAVCAN_PROTOCOL_PARAM_SERVER_HPP_INCLUDED +#include #include #include #include #include +#include +#include namespace uavcan { @@ -101,10 +104,65 @@ class UAVCAN_EXPORT ParamServer ServiceServer save_erase_srv_; IParamManager* manager_; - void handleGetSet(const protocol::param::GetSet::Request& request, protocol::param::GetSet::Response& response); + void handleGetSet(const protocol::param::GetSet::Request& in, protocol::param::GetSet::Response& out) + { + UAVCAN_ASSERT(manager_ != NULL); - void handleExecuteOpcode(const protocol::param::ExecuteOpcode::Request& request, - protocol::param::ExecuteOpcode::Response& response); + // Recover the name from index + if (in.name.empty()) + { + manager_->getParamNameByIndex(in.index, out.name); + UAVCAN_TRACE("ParamServer", "GetSet: Index %i --> '%s'", int(in.index), out.name.c_str()); + } + else + { + out.name = in.name; + } + + if (out.name.empty()) + { + UAVCAN_TRACE("ParamServer", "GetSet: Can't resolve parameter name, index=%i", int(in.index)); + return; + } + + // Assign if needed, read back + if (!IParamManager::isValueEmpty(in.value)) + { + manager_->assignParamValue(out.name, in.value); + } + manager_->readParamValue(out.name, out.value); + + // Check if the value is OK, otherwise reset the name to indicate that we have no idea what is it all about + if (!IParamManager::isValueEmpty(out.value)) + { + manager_->readParamDefaultMaxMin(out.name, out.default_value, out.max_value, out.min_value); + } + else + { + UAVCAN_TRACE("ParamServer", "GetSet: Unknown param: index=%i name='%s'", int(in.index), out.name.c_str()); + out.name.clear(); + } + } + + void handleExecuteOpcode(const protocol::param::ExecuteOpcode::Request& in, + protocol::param::ExecuteOpcode::Response& out) + { + UAVCAN_ASSERT(manager_ != NULL); + + if (in.opcode == protocol::param::ExecuteOpcode::Request::OPCODE_SAVE) + { + out.ok = manager_->saveAllParams() >= 0; + } + else if (in.opcode == protocol::param::ExecuteOpcode::Request::OPCODE_ERASE) + { + out.ok = manager_->eraseAllParams() >= 0; + } + else + { + UAVCAN_TRACE("ParamServer", "ExecuteOpcode: invalid opcode %i", int(in.opcode)); + out.ok = false; + } + } public: explicit ParamServer(INode& node) @@ -117,8 +175,31 @@ public: * Starts the parameter server with given param manager instance. * Returns negative error code. */ - int start(IParamManager* manager); + int start(IParamManager* manager) + { + if (manager == NULL) + { + return -ErrInvalidParam; + } + manager_ = manager; + int res = get_set_srv_.start(GetSetCallback(this, &ParamServer::handleGetSet)); + if (res < 0) + { + return res; + } + + res = save_erase_srv_.start(ExecuteOpcodeCallback(this, &ParamServer::handleExecuteOpcode)); + if (res < 0) + { + get_set_srv_.stop(); + } + return res; + } + + /** + * @ref IParamManager + */ IParamManager* getParamManager() const { return manager_; } }; diff --git a/libuavcan/src/protocol/uc_param_server.cpp b/libuavcan/src/protocol/uc_param_server.cpp deleted file mode 100644 index 908400af91..0000000000 --- a/libuavcan/src/protocol/uc_param_server.cpp +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (C) 2014 Pavel Kirienko - */ - -#include -#include -#include -#include - -namespace uavcan -{ - -void ParamServer::handleGetSet(const protocol::param::GetSet::Request& in, protocol::param::GetSet::Response& out) -{ - UAVCAN_ASSERT(manager_ != NULL); - - // Recover the name from index - if (in.name.empty()) - { - manager_->getParamNameByIndex(in.index, out.name); - UAVCAN_TRACE("ParamServer", "GetSet: Index %i --> '%s'", int(in.index), out.name.c_str()); - } - else - { - out.name = in.name; - } - - if (out.name.empty()) - { - UAVCAN_TRACE("ParamServer", "GetSet: Can't resolve parameter name, index=%i", int(in.index)); - return; - } - - // Assign if needed, read back - if (!IParamManager::isValueEmpty(in.value)) - { - manager_->assignParamValue(out.name, in.value); - } - manager_->readParamValue(out.name, out.value); - - // Check if the value is OK, otherwise reset the name to indicate that we have no idea what is it all about - if (!IParamManager::isValueEmpty(out.value)) - { - manager_->readParamDefaultMaxMin(out.name, out.default_value, out.max_value, out.min_value); - } - else - { - UAVCAN_TRACE("ParamServer", "GetSet: Unknown param: index=%i name='%s'", int(in.index), out.name.c_str()); - out.name.clear(); - } -} - -void ParamServer::handleExecuteOpcode(const protocol::param::ExecuteOpcode::Request& in, - protocol::param::ExecuteOpcode::Response& out) -{ - UAVCAN_ASSERT(manager_ != NULL); - - if (in.opcode == protocol::param::ExecuteOpcode::Request::OPCODE_SAVE) - { - out.ok = manager_->saveAllParams() >= 0; - } - else if (in.opcode == protocol::param::ExecuteOpcode::Request::OPCODE_ERASE) - { - out.ok = manager_->eraseAllParams() >= 0; - } - else - { - UAVCAN_TRACE("ParamServer", "ExecuteOpcode: invalid opcode %i", int(in.opcode)); - out.ok = false; - } -} - -int ParamServer::start(IParamManager* manager) -{ - if (manager == NULL) - { - return -ErrInvalidParam; - } - manager_ = manager; - - int res = get_set_srv_.start(GetSetCallback(this, &ParamServer::handleGetSet)); - if (res < 0) - { - return res; - } - - res = save_erase_srv_.start(ExecuteOpcodeCallback(this, &ParamServer::handleExecuteOpcode)); - if (res < 0) - { - get_set_srv_.stop(); - } - return res; -} - -}