param_server.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_PROTOCOL_PARAM_SERVER_HPP_INCLUDED
6 #define UAVCAN_PROTOCOL_PARAM_SERVER_HPP_INCLUDED
7 
8 #include <uavcan/debug.hpp>
9 #include <uavcan/protocol/param/GetSet.hpp>
10 #include <uavcan/protocol/param/ExecuteOpcode.hpp>
13 #include <cassert>
14 #include <cstdlib>
15 
16 namespace uavcan
17 {
23 {
24 public:
27  typedef protocol::param::Value Value;
28  typedef protocol::param::NumericValue NumericValue;
29 
30  virtual ~IParamManager() { }
31 
35  virtual void getParamNameByIndex(Index index, Name& out_name) const = 0;
36 
40  virtual void assignParamValue(const Name& name, const Value& value) = 0;
41 
45  virtual void readParamValue(const Name& name, Value& out_value) const = 0;
46 
52  virtual void readParamDefaultMaxMin(const Name& name, Value& out_default,
53  NumericValue& out_max, NumericValue& out_min) const
54  {
55  (void)name;
56  (void)out_default;
57  (void)out_max;
58  (void)out_min;
59  }
60 
65  virtual int saveAllParams() = 0;
66 
71  virtual int eraseAllParams() = 0;
72 };
73 
79 {
80  typedef MethodBinder<ParamServer*, void (ParamServer::*)(const protocol::param::GetSet::Request&,
81  protocol::param::GetSet::Response&)> GetSetCallback;
82 
83  typedef MethodBinder<ParamServer*,
84  void (ParamServer::*)(const protocol::param::ExecuteOpcode::Request&,
85  protocol::param::ExecuteOpcode::Response&)> ExecuteOpcodeCallback;
86 
90 
91  void handleGetSet(const protocol::param::GetSet::Request& in, protocol::param::GetSet::Response& out)
92  {
93  UAVCAN_ASSERT(manager_ != UAVCAN_NULLPTR);
94 
95  // Recover the name from index
96  if (in.name.empty())
97  {
98  manager_->getParamNameByIndex(in.index, out.name);
99  UAVCAN_TRACE("ParamServer", "GetSet: Index %i --> '%s'", int(in.index), out.name.c_str());
100  }
101  else
102  {
103  out.name = in.name;
104  }
105 
106  if (out.name.empty())
107  {
108  UAVCAN_TRACE("ParamServer", "GetSet: Can't resolve parameter name, index=%i", int(in.index));
109  return;
110  }
111 
112  // Assign if needed, read back
113  if (!in.value.is(protocol::param::Value::Tag::empty))
114  {
115  manager_->assignParamValue(out.name, in.value);
116  }
117  manager_->readParamValue(out.name, out.value);
118 
119  // Check if the value is OK, otherwise reset the name to indicate that we have no idea what is it all about
120  if (!out.value.is(protocol::param::Value::Tag::empty))
121  {
122  manager_->readParamDefaultMaxMin(out.name, out.default_value, out.max_value, out.min_value);
123  }
124  else
125  {
126  UAVCAN_TRACE("ParamServer", "GetSet: Unknown param: index=%i name='%s'", int(in.index), out.name.c_str());
127  out.name.clear();
128  }
129  }
130 
131  void handleExecuteOpcode(const protocol::param::ExecuteOpcode::Request& in,
132  protocol::param::ExecuteOpcode::Response& out)
133  {
134  UAVCAN_ASSERT(manager_ != UAVCAN_NULLPTR);
135 
136  if (in.opcode == protocol::param::ExecuteOpcode::Request::OPCODE_SAVE)
137  {
138  out.ok = manager_->saveAllParams() >= 0;
139  }
140  else if (in.opcode == protocol::param::ExecuteOpcode::Request::OPCODE_ERASE)
141  {
142  out.ok = manager_->eraseAllParams() >= 0;
143  }
144  else
145  {
146  UAVCAN_TRACE("ParamServer", "ExecuteOpcode: invalid opcode %i", int(in.opcode));
147  out.ok = false;
148  }
149  }
150 
151 public:
152  explicit ParamServer(INode& node)
153  : get_set_srv_(node)
154  , save_erase_srv_(node)
155  , manager_(UAVCAN_NULLPTR)
156  { }
157 
162  int start(IParamManager* manager)
163  {
164  if (manager == UAVCAN_NULLPTR)
165  {
166  return -ErrInvalidParam;
167  }
168  manager_ = manager;
169 
170  int res = get_set_srv_.start(GetSetCallback(this, &ParamServer::handleGetSet));
171  if (res < 0)
172  {
173  return res;
174  }
175 
176  res = save_erase_srv_.start(ExecuteOpcodeCallback(this, &ParamServer::handleExecuteOpcode));
177  if (res < 0)
178  {
179  get_set_srv_.stop();
180  }
181  return res;
182  }
183 
187  IParamManager* getParamManager() const { return manager_; }
188 };
189 
190 }
191 
192 #endif // UAVCAN_PROTOCOL_PARAM_SERVER_HPP_INCLUDED
uavcan::ParamServer::handleGetSet
void handleGetSet(const protocol::param::GetSet::Request &in, protocol::param::GetSet::Response &out)
Definition: param_server.hpp:91
UAVCAN_NULLPTR
#define UAVCAN_NULLPTR
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:51
uavcan::ParamServer::ParamServer
ParamServer(INode &node)
Definition: param_server.hpp:152
debug.hpp
uavcan::IParamManager::Name
StorageType< typename protocol::param::GetSet::Response::FieldTypes::name >::Type Name
Definition: param_server.hpp:25
UAVCAN_TRACE
#define UAVCAN_TRACE(...)
Definition: libuavcan/libuavcan/include/uavcan/debug.hpp:31
uavcan::IParamManager::Value
protocol::param::Value Value
Definition: param_server.hpp:27
uavcan::ParamServer::getParamManager
IParamManager * getParamManager() const
Definition: param_server.hpp:187
uavcan::IParamManager::Index
StorageType< typename protocol::param::GetSet::Request::FieldTypes::index >::Type Index
Definition: param_server.hpp:26
uavcan::IParamManager
Definition: param_server.hpp:22
uavcan::ParamServer::save_erase_srv_
ServiceServer< protocol::param::ExecuteOpcode, ExecuteOpcodeCallback > save_erase_srv_
Definition: param_server.hpp:88
uavcan::ServiceServer
Definition: service_server.hpp:90
uavcan::ParamServer
Definition: param_server.hpp:78
uavcan::ParamServer::start
int start(IParamManager *manager)
Definition: param_server.hpp:162
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
method_binder.hpp
uavcan::INode
Definition: abstract_node.hpp:19
uavcan::ParamServer::get_set_srv_
ServiceServer< protocol::param::GetSet, GetSetCallback > get_set_srv_
Definition: param_server.hpp:87
uavcan::ServiceServer::start
int start(const Callback &callback)
Definition: service_server.hpp:158
uavcan::IParamManager::NumericValue
protocol::param::NumericValue NumericValue
Definition: param_server.hpp:28
uavcan::IParamManager::eraseAllParams
virtual int eraseAllParams()=0
uavcan::MethodBinder
Definition: method_binder.hpp:20
uavcan::ParamServer::manager_
IParamManager * manager_
Definition: param_server.hpp:89
pyuavcan_v0.introspect.node
node
Definition: introspect.py:398
uavcan::IParamManager::~IParamManager
virtual ~IParamManager()
Definition: param_server.hpp:30
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::IParamManager::assignParamValue
virtual void assignParamValue(const Name &name, const Value &value)=0
uavcan::IParamManager::readParamDefaultMaxMin
virtual void readParamDefaultMaxMin(const Name &name, Value &out_default, NumericValue &out_max, NumericValue &out_min) const
Definition: param_server.hpp:52
uavcan::IParamManager::getParamNameByIndex
virtual void getParamNameByIndex(Index index, Name &out_name) const =0
service_server.hpp
uavcan::ServiceServer::stop
void stop()
Definition: generic_subscriber.hpp:213
uavcan::IParamManager::readParamValue
virtual void readParamValue(const Name &name, Value &out_value) const =0
UAVCAN_ASSERT
#define UAVCAN_ASSERT(x)
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:184
uavcan::StorageType::Type
T Type
Definition: type_util.hpp:53
uavcan::ParamServer::handleExecuteOpcode
void handleExecuteOpcode(const protocol::param::ExecuteOpcode::Request &in, protocol::param::ExecuteOpcode::Response &out)
Definition: param_server.hpp:131
uavcan::IParamManager::saveAllParams
virtual int saveAllParams()=0


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02