param_server.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <map>
6 #include <gtest/gtest.h>
8 #include "helpers.hpp"
9 
11 {
12  typedef std::map<std::string, double> KeyValue;
14 
15  virtual void getParamNameByIndex(Index index, Name& out_name) const
16  {
17  Index current_idx = 0;
18  for (KeyValue::const_iterator it = kv.begin(); it != kv.end(); ++it, ++current_idx)
19  {
20  if (current_idx == index)
21  {
22  out_name = it->first.c_str();
23  break;
24  }
25  }
26  }
27 
28  virtual void assignParamValue(const Name& name, const Value& value)
29  {
30  assert(!name.empty());
31  std::cout << "ASSIGN [" << name.c_str() << "]\n" << value << "\n---" << std::endl;
32  KeyValue::iterator it = kv.find(name.c_str());
33  if (it != kv.end())
34  {
35  if (value.is(Value::Tag::boolean_value))
36  {
37  assert(value.getTag() == Value::Tag::boolean_value);
38  it->second = double(value.boolean_value);
39  }
40  else if (value.is(Value::Tag::integer_value))
41  {
42  assert(value.getTag() == Value::Tag::integer_value);
43  it->second = double(value.integer_value);
44  }
45  else if (value.is(Value::Tag::real_value))
46  {
47  assert(value.getTag() == Value::Tag::real_value);
48  it->second = double(value.real_value);
49  }
50  else if (value.is(Value::Tag::string_value))
51  {
52  assert(value.getTag() == Value::Tag::string_value);
53  it->second = std::atof(value.string_value.c_str());
54  }
55  else
56  {
57  assert(0);
58  }
59  }
60  }
61 
62  virtual void readParamValue(const Name& name, Value& out_value) const
63  {
64  assert(!name.empty());
65  KeyValue::const_iterator it = kv.find(name.c_str());
66  if (it != kv.end())
67  {
68  out_value.to<Value::Tag::real_value>() = float(it->second);
69  assert(out_value.getTag() == Value::Tag::real_value);
70  }
71  std::cout << "READ [" << name.c_str() << "]\n" << out_value << "\n---" << std::endl;
72  }
73 
74  virtual int saveAllParams()
75  {
76  std::cout << "SAVE" << std::endl;
77  return 0;
78  }
79 
80  virtual int eraseAllParams()
81  {
82  std::cout << "ERASE" << std::endl;
83  return 0;
84  }
85 };
86 
87 
88 template <typename Client, typename Message>
89 static void doCall(Client& client, const Message& request, InterlinkedTestNodesWithSysClock& nodes)
90 {
91  ASSERT_LE(0, client.call(1, request));
92  ASSERT_LE(0, nodes.spinBoth(uavcan::MonotonicDuration::fromMSec(10)));
93  ASSERT_TRUE(client.collector.result.get());
94  ASSERT_TRUE(client.collector.result->isSuccessful());
95 }
96 
97 
98 TEST(ParamServer, Basic)
99 {
101 
102  uavcan::ParamServer server(nodes.a);
103 
105 
109 
110  ASSERT_LE(0, server.start(&mgr));
111 
114 
115  /*
116  * Save/erase
117  */
118  uavcan::protocol::param::ExecuteOpcode::Request save_erase_rq;
119  save_erase_rq.opcode = uavcan::protocol::param::ExecuteOpcode::Request::OPCODE_SAVE;
120  doCall(save_erase_cln, save_erase_rq, nodes);
121  ASSERT_TRUE(save_erase_cln.collector.result.get());
122  ASSERT_TRUE(save_erase_cln.collector.result->getResponse().ok);
123 
124  save_erase_rq.opcode = uavcan::protocol::param::ExecuteOpcode::Request::OPCODE_ERASE;
125  doCall(save_erase_cln, save_erase_rq, nodes);
126  ASSERT_TRUE(save_erase_cln.collector.result->getResponse().ok);
127 
128  // Invalid opcode
129  save_erase_rq.opcode = 0xFF;
130  doCall(save_erase_cln, save_erase_rq, nodes);
131  ASSERT_FALSE(save_erase_cln.collector.result->getResponse().ok);
132 
133  /*
134  * Get/set
135  */
136  uavcan::protocol::param::GetSet::Request get_set_rq;
137  get_set_rq.name = "nonexistent_parameter";
138  doCall(get_set_cln, get_set_rq, nodes);
139  ASSERT_TRUE(get_set_cln.collector.result.get());
140  ASSERT_TRUE(get_set_cln.collector.result->getResponse().name.empty());
141 
142  // No such variable, shall return empty name/value
143  get_set_rq.index = 0;
144  get_set_rq.name.clear();
145  get_set_rq.value.to<uavcan::protocol::param::Value::Tag::integer_value>() = 0xDEADBEEF;
146  doCall(get_set_cln, get_set_rq, nodes);
147  ASSERT_TRUE(get_set_cln.collector.result->getResponse().name.empty());
148  ASSERT_TRUE(get_set_cln.collector.result->getResponse().value.is(uavcan::protocol::param::Value::Tag::empty));
149 
150  mgr.kv["foobar"] = 123.456; // New param
151 
152  // Get by name
153  get_set_rq = uavcan::protocol::param::GetSet::Request();
154  get_set_rq.name = "foobar";
155  doCall(get_set_cln, get_set_rq, nodes);
156  ASSERT_STREQ("foobar", get_set_cln.collector.result->getResponse().name.c_str());
157  ASSERT_TRUE(get_set_cln.collector.result->getResponse().value.is(uavcan::protocol::param::Value::Tag::real_value));
158  ASSERT_FLOAT_EQ(123.456F, get_set_cln.collector.result->getResponse().value.
159  to<uavcan::protocol::param::Value::Tag::real_value>());
160 
161  // Set by index
162  get_set_rq = uavcan::protocol::param::GetSet::Request();
163  get_set_rq.index = 0;
164  get_set_rq.value.to<uavcan::protocol::param::Value::Tag::string_value>() = "424242";
165  doCall(get_set_cln, get_set_rq, nodes);
166  ASSERT_STREQ("foobar", get_set_cln.collector.result->getResponse().name.c_str());
167  ASSERT_FLOAT_EQ(424242, get_set_cln.collector.result->getResponse().value.
168  to<uavcan::protocol::param::Value::Tag::real_value>());
169 
170  // Get by index
171  get_set_rq = uavcan::protocol::param::GetSet::Request();
172  get_set_rq.index = 0;
173  doCall(get_set_cln, get_set_rq, nodes);
174  ASSERT_STREQ("foobar", get_set_cln.collector.result->getResponse().name.c_str());
175  ASSERT_FLOAT_EQ(424242, get_set_cln.collector.result->getResponse().value.
176  to<uavcan::protocol::param::Value::Tag::real_value>());
177 }
ParamServerTestManager::eraseAllParams
virtual int eraseAllParams()
Definition: param_server.cpp:80
uavcan::DefaultDataTypeRegistrator
Definition: global_data_type_registry.hpp:186
TEST
TEST(ParamServer, Basic)
Definition: param_server.cpp:98
uavcan::IParamManager::Name
StorageType< typename protocol::param::GetSet::Response::FieldTypes::name >::Type Name
Definition: param_server.hpp:25
ParamServerTestManager::readParamValue
virtual void readParamValue(const Name &name, Value &out_value) const
Definition: param_server.cpp:62
uavcan::DurationBase< MonotonicDuration >::fromMSec
static MonotonicDuration fromMSec(int64_t ms)
Definition: time.hpp:41
uavcan::IParamManager::Value
protocol::param::Value Value
Definition: param_server.hpp:27
ServiceClientWithCollector
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:99
ServiceCallResultCollector::result
std::unique_ptr< Result > result
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:88
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
ParamServerTestManager::saveAllParams
virtual int saveAllParams()
Definition: param_server.cpp:74
ParamServerTestManager
Definition: param_server.cpp:10
helpers.hpp
uavcan::ParamServer
Definition: param_server.hpp:78
doCall
static void doCall(Client &client, const Message &request, InterlinkedTestNodesWithSysClock &nodes)
Definition: param_server.cpp:89
uavcan::ParamServer::start
int start(IParamManager *manager)
Definition: param_server.hpp:162
InterlinkedTestNodes
Definition: test_node.hpp:149
param_server.hpp
InterlinkedTestNodes::a
TestNode a
Definition: test_node.hpp:155
InterlinkedTestNodes::spinBoth
int spinBoth(uavcan::MonotonicDuration duration)
Definition: test_node.hpp:176
ParamServerTestManager::KeyValue
std::map< std::string, double > KeyValue
Definition: param_server.cpp:12
ParamServerTestManager::getParamNameByIndex
virtual void getParamNameByIndex(Index index, Name &out_name) const
Definition: param_server.cpp:15
ParamServerTestManager::kv
KeyValue kv
Definition: param_server.cpp:13
uavcan::GlobalDataTypeRegistry::instance
static GlobalDataTypeRegistry & instance()
Definition: uc_global_data_type_registry.cpp:128
InterlinkedTestNodes::b
TestNode b
Definition: test_node.hpp:156
ParamServerTestManager::assignParamValue
virtual void assignParamValue(const Name &name, const Value &value)
Definition: param_server.cpp:28
ServiceClientWithCollector::collector
Collector collector
Definition: libuavcan/libuavcan/test/protocol/helpers.hpp:104


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