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
int start(IParamManager *manager)
void handleGetSet(const protocol::param::GetSet::Request &in, protocol::param::GetSet::Response &out)
ServiceServer< protocol::param::GetSet, GetSetCallback > get_set_srv_
protocol::param::Value Value
IParamManager * getParamManager() const
ParamServer(INode &node)
virtual void getParamNameByIndex(Index index, Name &out_name) const =0
IParamManager * manager_
StorageType< typename protocol::param::GetSet::Response::FieldTypes::name >::Type Name
virtual void assignParamValue(const Name &name, const Value &value)=0
virtual void readParamDefaultMaxMin(const Name &name, Value &out_default, NumericValue &out_max, NumericValue &out_min) const
protocol::param::NumericValue NumericValue
StorageType< typename protocol::param::GetSet::Request::FieldTypes::index >::Type Index
virtual void readParamValue(const Name &name, Value &out_value) const =0
virtual int saveAllParams()=0
void handleExecuteOpcode(const protocol::param::ExecuteOpcode::Request &in, protocol::param::ExecuteOpcode::Response &out)
ServiceServer< protocol::param::ExecuteOpcode, ExecuteOpcodeCallback > save_erase_srv_
int start(const Callback &callback)
virtual int eraseAllParams()=0


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:39