binary_serialize_data_value.cpp
Go to the documentation of this file.
1 
11 #include "common.h"
12 
18 #include <opc/ua/protocol/types.h>
20 
21 #include <algorithm>
22 #include <stdexcept>
23 
24 //-------------------------------------------------------
25 // Serialization
26 //-------------------------------------------------------
27 
29 {
30  using namespace OpcUa;
31  using namespace OpcUa::Binary;
32 
33  DataValue data;
34 
35  GetStream() << data << flush;
36 
37  const std::vector<char> expectedData =
38  {
39  0
40  };
41 
42  ASSERT_EQ(expectedData.size(), RawSize(data));
43  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
44 }
45 
47 {
48 
49  using namespace OpcUa;
50  using namespace OpcUa::Binary;
51 
52  DataValue data;
53  data.Encoding = DATA_VALUE;
54  data.Value = true;
55 
56  GetStream() << data << flush;
57 
58  char encodingMask = static_cast<uint8_t>(VariantType::BOOLEAN);
59  const std::vector<char> expectedData =
60  {
61  1,
62  encodingMask, 1
63  };
64 
65  ASSERT_EQ(expectedData.size(), RawSize(data));
66  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
67 }
68 
70 {
71 
72  using namespace OpcUa;
73  using namespace OpcUa::Binary;
74 
75  char encodingMask =
76  DATA_VALUE |
82 
83  DataValue data;
84  data.Encoding = encodingMask;
86  data.Status = static_cast<StatusCode>(1);
87  data.SourceTimestamp.Value = 2;
88  data.SourcePicoseconds = 3;
89  data.ServerTimestamp.Value = 4;
90  data.ServerPicoseconds = 5;
91 
92  GetStream() << data << flush;
93 
94 
95  char variantMask = static_cast<uint8_t>(VariantType::QUALIFIED_NAME);
96  const std::vector<char> expectedData =
97  {
98  encodingMask,
99  variantMask,
100  1, 0, // QualifiedName::NamespaceIndex
101  4, 0, 0, 0, // Qualified::Name.size()
102  'R', 'o', 'o', 't', // QualifiedName::Name::c_str()
103  1, 0, 0, 0,
104  2, 0, 0, 0, 0, 0, 0, 0,
105  3, 0,
106  4, 0, 0, 0, 0, 0, 0, 0,
107  5, 0
108  };
109 
110  ASSERT_EQ(expectedData.size(), RawSize(data));
111  ASSERT_EQ(expectedData, GetChannel().SerializedData) << PrintData(GetChannel().SerializedData) << std::endl << PrintData(expectedData);
112 }
113 
114 
115 //-------------------------------------------------------
116 // Deserialization
117 //-------------------------------------------------------
118 
120 {
121  using namespace OpcUa;
122  using namespace OpcUa::Binary;
123 
124  const std::vector<char> expectedData =
125  {
126  0
127  };
128 
129  GetChannel().SetData(expectedData);
130 
131  DataValue data;
132  GetStream() >> data;
133 
134  ASSERT_EQ(data.Encoding, 0);
135 }
136 
138 {
139  using namespace OpcUa;
140  using namespace OpcUa::Binary;
141 
142  char encodingMask = static_cast<uint8_t>(VariantType::BOOLEAN);
143  const std::vector<char> expectedData =
144  {
145  1,
146  encodingMask, 1
147  };
148 
149  GetChannel().SetData(expectedData);
150 
151  DataValue data;
152  GetStream() >> data;
153 
155  ASSERT_FALSE(data.Value.IsNul());
156 }
157 
159 {
160  using namespace OpcUa;
161  using namespace OpcUa::Binary;
162 
163  char encodingMask =
164  DATA_VALUE |
170 
171 
172  char variantMask = static_cast<uint8_t>(VariantType::BOOLEAN);
173  const std::vector<char> expectedData =
174  {
175  encodingMask,
176  variantMask, 1,
177  1, 0, 0, 0,
178  2, 0, 0, 0, 0, 0, 0, 0,
179  3, 0,
180  4, 0, 0, 0, 0, 0, 0, 0,
181  5, 0
182  };
183 
184  GetChannel().SetData(expectedData);
185 
186  DataValue data;
187  GetStream() >> data;
188 
189  ASSERT_EQ(data.Encoding, encodingMask);
190  ASSERT_FALSE(data.Value.IsNul());
191  ASSERT_EQ(data.Status, static_cast<StatusCode>(1));
192  ASSERT_EQ(data.SourceTimestamp, 2);
193  ASSERT_EQ(data.SourcePicoseconds, 3);
194  ASSERT_EQ(data.ServerTimestamp, 4);
195  ASSERT_EQ(data.ServerPicoseconds, 5);
196 }
197 
198 
199 TEST(DataValue, ConstructivbeFromDataValue)
200 {
201  using namespace OpcUa;
203  DataValue data;
204  data = node;
207 
208  DataValue newValue(data);
209  ASSERT_TRUE(newValue.Encoding && DATA_VALUE);
210  ASSERT_EQ(newValue.Value.Type(), VariantType::NODE_Id);
211  ASSERT_NO_THROW(newValue.Value.As<NodeId>());
212 }
213 
214 TEST(DataValue, ComparableByValue)
215 {
216  using namespace OpcUa;
217  DataValue data1(uint32_t(1));
218  DataValue data2(uint32_t(1));
219 
220  ASSERT_EQ(data1, data2);
221 }
std::string PrintData(const std::vector< char > &vec)
Test of opc ua binary handshake. GNU LGPL.
const uint8_t DATA_VALUE_STATUS_CODE
Definition: data_value.h:19
bool IsNul() const
uint16_t ServerPicoseconds
Definition: data_value.h:34
const uint8_t DATA_VALUE_Server_TIMESTAMP
Definition: data_value.h:21
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
const uint8_t DATA_VALUE_SOURCE_PICOSECONDS
Definition: data_value.h:22
VariantType Type() const
const uint8_t DATA_VALUE_SOURCE_TIMESTAMP
Definition: data_value.h:20
#define ASSERT_NO_THROW(statement)
OPC UA Address space part. GNU LGPL.
const uint8_t DATA_VALUE_Server_PICOSECONDS
Definition: data_value.h:23
#define ASSERT_TRUE(condition)
const char Root[]
Definition: strings.h:137
#define ASSERT_EQ(val1, val2)
TEST(DataValue, ConstructivbeFromDataValue)
TEST_F(OpcUaBinarySerialization, DataValue_NULL)
Test of opc ua binary attributes. GNU LGPL.
int64_t Value
Definition: datetime.h:49
DateTime ServerTimestamp
Definition: data_value.h:33
DateTime SourceTimestamp
Definition: data_value.h:31
const uint8_t DATA_VALUE
Definition: data_value.h:18
uint8_t Encoding
Definition: data_value.h:28
StatusCode Status
Definition: data_value.h:30
#define ASSERT_FALSE(condition)
uint16_t SourcePicoseconds
Definition: data_value.h:32
std::size_t RawSize(const T &obj)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:06:03