24 #include <netinet/in.h> 63 template <
typename Integer16>
64 inline int8_t LoByte(Integer16 value)
69 template <
typename Integer16>
70 inline int8_t HiByte(Integer16 value)
72 return (value & 0xFF00) >> 8;
75 template <
typename Integer32>
76 inline uint16_t LoWord(Integer32 value)
78 return value & 0xFFFF;
81 template <
typename Integer32>
82 inline uint16_t HiWord(Integer32 value)
84 return (value & 0xFFFF0000) >> 16;
87 template <
typename Integer64>
88 inline uint32_t LoDWord(Integer64 value)
90 return value & 0xFFFFFFFF;
93 template <
typename Integer64>
94 inline uint32_t HiDWord(Integer64 value)
96 return (value & 0xFFFFFFFF00000000) >> 32;
99 template <
typename Integer16>
100 inline Integer16 MakeWord(int8_t loByte, int8_t hiByte)
102 const Integer16 word = hiByte;
103 return (word << 8) | (0x00FF & loByte);
106 template<
typename Integer>
107 inline Integer MakeNumber(
char * data)
109 unsigned size =
sizeof(
Integer);
114 i = (i << 8) | (0x00FF & data[--size]);
121 const size_t MESSAGE_TYPE_SIZE = 3;
122 const char MESSAGE_TYPE_HELLO[MESSAGE_TYPE_SIZE] = {
'H',
'E',
'L'};
123 const char MESSAGE_TYPE_ACKNOWLEDGE[MESSAGE_TYPE_SIZE] = {
'A',
'C',
'K'};
124 const char MESSAGE_TYPE_ERROR[MESSAGE_TYPE_SIZE] = {
'E',
'R',
'R'};
125 const char MESSAGE_TYPE_MESSAGE[MESSAGE_TYPE_SIZE] = {
'M',
'S',
'G'};
126 const char MESSAGE_TYPE_OPEN[MESSAGE_TYPE_SIZE] = {
'O',
'P',
'N'};
127 const char MESSAGE_TYPE_CLOSE[MESSAGE_TYPE_SIZE] = {
'C',
'L',
'O'};
129 inline void ThrowReceivedNotEnoughData()
131 throw std::logic_error(
"Not enough data was received from channel.");
135 template <
typename ChannelType>
136 inline void GetData(ChannelType & channel,
char * data, std::size_t size)
138 size_t recv = channel.Read(data, size);
142 std::cout <<
"expecting " << size <<
" received: " << recv << std::endl;
143 ThrowReceivedNotEnoughData();
184 throw std::invalid_argument(
"IntegerId cannot be zero");
198 throw std::invalid_argument(
"IntegerId cannot be zero");
205 IntegerId::operator uint32_t()
const 216 void DataSerializer::Serialize<int8_t>(
const int8_t & value)
218 Buffer.push_back(value);
222 void DataSerializer::Serialize<std::vector<int8_t>>(
const std::vector<int8_t> & value)
228 void DataSerializer::Serialize<uint8_t>(
const uint8_t & value)
230 Buffer.push_back(value);
234 void DataDeserializer::Deserialize<uint8_t>(uint8_t & value)
237 GetData(In, &data, 1);
238 value =
static_cast<uint8_t
>(data);
242 void DataDeserializer::Deserialize<int8_t>(int8_t & value)
245 GetData(In, &data, 1);
250 void DataDeserializer::Deserialize<std::vector<int8_t>>(std::vector<int8_t> & value)
257 void DataSerializer::Serialize<int16_t>(
const int16_t & value)
259 Buffer.push_back(LoByte(value));
260 Buffer.push_back(HiByte(value));
264 void DataSerializer::Serialize<uint16_t>(
const uint16_t & value)
266 Buffer.push_back(LoByte(value));
267 Buffer.push_back(HiByte(value));
271 void DataDeserializer::Deserialize<uint16_t>(uint16_t & value)
274 GetData(In, data, 2);
275 value = MakeWord<uint16_t>(data[0], data[1]);
279 void DataDeserializer::Deserialize<int16_t>(int16_t & value)
282 GetData(In, data, 2);
283 value = MakeWord<int16_t>(data[0], data[1]);
287 void DataSerializer::Serialize<int32_t>(
const int32_t & value)
289 Serialize(LoWord(value));
290 Serialize(HiWord(value));
294 void DataSerializer::Serialize<uint32_t>(
const uint32_t & value)
296 Serialize(LoWord(value));
297 Serialize(HiWord(value));
301 void DataDeserializer::Deserialize<uint32_t>(uint32_t & value)
304 GetData(In, data, 4);
305 value = MakeNumber<uint32_t>(data);
309 void DataDeserializer::Deserialize<int32_t>(int32_t & value)
312 GetData(In, data, 4);
313 value = MakeNumber<int32_t>(data);
317 void DataSerializer::Serialize<int64_t>(
const int64_t & value)
319 Serialize(LoDWord(value));
320 Serialize(HiDWord(value));
324 void DataSerializer::Serialize<uint64_t>(
const uint64_t & value)
326 Serialize(LoDWord(value));
327 Serialize(HiDWord(value));
331 void DataDeserializer::Deserialize<uint64_t>(uint64_t & value)
334 GetData(In, data, 8);
335 value = MakeNumber<uint64_t>(data);
339 void DataDeserializer::Deserialize<int64_t>(int64_t & value)
342 GetData(In, data, 8);
343 value = MakeNumber<int64_t>(data);
347 void DataSerializer::Serialize<bool>(
const bool & value)
349 Serialize(static_cast<uint8_t>(value));
353 void DataSerializer::Serialize<std::vector<bool>>(
const std::vector<bool> & value)
359 void DataDeserializer::Deserialize<bool>(
bool & value)
367 void DataDeserializer::Deserialize<std::vector<bool>>(std::vector<bool> & value)
374 void DataSerializer::Serialize<float>(
const float & value)
377 const uint8_t * data =
reinterpret_cast<const uint8_t *
>(&value);
379 for (
int i = 0; i < 4; ++i)
386 void DataDeserializer::Deserialize<float>(
float & value)
389 uint8_t data[4] = {0};
391 for (
int i = 0; i < 4; ++i)
396 value = *
reinterpret_cast<const float *
>(data);
400 void DataSerializer::Serialize<double>(
const double & value)
402 const uint8_t * data =
reinterpret_cast<const uint8_t *
>(&value);
404 for (
int i = 0; i < 8; ++i)
411 void DataDeserializer::Deserialize<double>(
double & value)
413 uint8_t data[8] = {0};
415 for (
int i = 0; i < 8; ++i)
420 value = *
reinterpret_cast<const double *
>(data);
424 void DataSerializer::Serialize<OpcUa::Guid>(
const OpcUa::Guid & value)
426 *
this << value.Data1 << value.Data2 << value.Data3;
427 Buffer.insert(Buffer.end(), value.Data4, value.Data4 + 8);
431 void DataDeserializer::Deserialize<OpcUa::Guid>(
OpcUa::Guid & value)
433 *
this >> value.Data1 >> value.Data2 >> value.Data3;
435 GetData(In, data, 8);
436 std::copy(data, data + 8, value.Data4);
440 void DataSerializer::Serialize<std::string>(
const std::string & value)
444 Serialize(~uint32_t());
448 Serialize(static_cast<uint32_t>(value.size()));
449 Buffer.insert(Buffer.end(), value.begin(), value.end());
453 void DataDeserializer::Deserialize<std::string>(
std::string & value)
455 uint32_t stringSize = 0;
458 if (stringSize != ~uint32_t())
460 value.resize(stringSize);
461 GetData(In, &value[0], stringSize);
496 void DataSerializer::Serialize<std::vector<OpcUa::DateTime>>(
const std::vector<OpcUa::DateTime> & date)
502 void DataDeserializer::Deserialize<std::vector<OpcUa::DateTime>>(std::vector<OpcUa::DateTime> & date)
508 void DataSerializer::Serialize<ByteString>(
const ByteString & value)
510 if (value.Data.empty())
512 Serialize(~uint32_t());
516 Serialize(static_cast<uint32_t>(value.Data.size()));
517 Buffer.insert(Buffer.end(), value.Data.begin(), value.Data.end());
521 void DataDeserializer::Deserialize<ByteString>(
ByteString & value)
523 uint32_t stringSize = 0;
526 if (stringSize != ~uint32_t())
528 value.Data.resize(stringSize);
529 GetData(In, reinterpret_cast<char *>(&value.Data[0]), stringSize);
539 void DataSerializer::Serialize<std::vector<ByteString>>(
const std::vector<ByteString> & value)
545 void DataDeserializer::Deserialize<std::vector<ByteString>>(std::vector<ByteString> & value)
552 void DataSerializer::Serialize<std::vector<uint8_t>>(
const std::vector<uint8_t> & value)
558 void DataDeserializer::Deserialize<std::vector<uint8_t>>(std::vector<uint8_t> & value)
564 void DataSerializer::Serialize<std::vector<uint16_t>>(
const std::vector<uint16_t> & value)
570 void DataDeserializer::Deserialize<std::vector<uint16_t>>(std::vector<uint16_t> & value)
576 void DataSerializer::Serialize<std::vector<int16_t>>(
const std::vector<int16_t> & value)
582 void DataDeserializer::Deserialize<std::vector<int16_t>>(std::vector<int16_t> & value)
589 void DataSerializer::Serialize<std::vector<uint32_t>>(
const std::vector<uint32_t> & value)
595 void DataDeserializer::Deserialize<std::vector<uint32_t>>(std::vector<uint32_t> & value)
601 void DataSerializer::Serialize<std::vector<int32_t>>(
const std::vector<int32_t> & value)
607 void DataDeserializer::Deserialize<std::vector<int32_t>>(std::vector<int32_t> & value)
613 void DataSerializer::Serialize<std::vector<int64_t>>(
const std::vector<int64_t> & value)
619 void DataDeserializer::Deserialize<std::vector<int64_t>>(std::vector<int64_t> & value)
625 void DataSerializer::Serialize<std::vector<uint64_t>>(
const std::vector<uint64_t> & value)
631 void DataDeserializer::Deserialize<std::vector<uint64_t>>(std::vector<uint64_t> & value)
638 void DataSerializer::Serialize<std::vector<float>>(
const std::vector<float> & value)
644 void DataDeserializer::Deserialize<std::vector<double>>(std::vector<double> & value)
650 void DataSerializer::Serialize<std::vector<double>>(
const std::vector<double> & value)
656 void DataDeserializer::Deserialize<std::vector<float>>(std::vector<float> & value)
663 void DataSerializer::Serialize<std::vector<Guid>>(
const std::vector<Guid> & value)
669 void DataDeserializer::Deserialize<std::vector<Guid>>(std::vector<Guid> & value)
675 void DataSerializer::Serialize<std::vector<NodeId>>(
const std::vector<NodeId> & value)
681 void DataDeserializer::Deserialize<std::vector<NodeId>>(std::vector<NodeId> & value)
687 void DataSerializer::Serialize<std::vector<std::string>>(
const std::vector<std::string> & value)
693 void DataDeserializer::Deserialize<std::vector<std::string>>(std::vector<std::string> & value)
699 void DataSerializer::Serialize<std::vector<std::vector<uint8_t>>>(
const std::vector<std::vector<uint8_t>> & value)
705 void DataDeserializer::Deserialize<std::vector<std::vector<uint8_t>>>(std::vector<std::vector<uint8_t>> & value)
713 const char * typeName =
nullptr;
718 typeName = MESSAGE_TYPE_HELLO;
722 typeName = MESSAGE_TYPE_ACKNOWLEDGE;
726 typeName = MESSAGE_TYPE_ERROR;
730 typeName = MESSAGE_TYPE_OPEN;
734 typeName = MESSAGE_TYPE_CLOSE;
738 typeName = MESSAGE_TYPE_MESSAGE;
742 throw std::logic_error(
"Invalid message type.");
745 Buffer.insert(Buffer.end(), typeName, typeName + MESSAGE_TYPE_SIZE);
751 char data[MESSAGE_TYPE_SIZE] = {0};
752 GetData(In, data, MESSAGE_TYPE_SIZE);
754 if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_HELLO))
759 else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_ACKNOWLEDGE))
764 else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_ERROR))
769 else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_MESSAGE))
774 else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_OPEN))
779 else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_CLOSE))
786 std::string msg(
"Cannot deserialize Unknown message type [");
787 msg += std::to_string(data[0]) +
", " + std::to_string(data[1]) +
", " + std::to_string(data[2]);
788 msg +=
"] received.";
789 throw std::logic_error(msg);
799 Buffer.push_back(
'F');
803 Buffer.push_back(
'C');
807 Buffer.push_back(
'A');
811 throw std::logic_error(
"Invalid Chunk Type");
819 GetData(In, &data, 1);
836 throw std::logic_error(
"Invalid chunk type received.");
859 *
this <<
message.ProtocolVersion;
860 *
this <<
message.ReceiveBufferSize;
861 *
this <<
message.SendBufferSize;
862 *
this <<
message.MaxMessageSize;
863 *
this <<
message.MaxChunkCount;
870 *
this >>
message.ProtocolVersion;
871 *
this >>
message.ReceiveBufferSize;
872 *
this >>
message.SendBufferSize;
873 *
this >>
message.MaxMessageSize;
874 *
this >>
message.MaxChunkCount;
881 *
this <<
message.ProtocolVersion;
882 *
this <<
message.ReceiveBufferSize;
883 *
this <<
message.SendBufferSize;
884 *
this <<
message.MaxMessageSize;
885 *
this <<
message.MaxChunkCount;
891 *
this >>
message.ProtocolVersion;
892 *
this >>
message.ReceiveBufferSize;
893 *
this >>
message.SendBufferSize;
894 *
this >>
message.MaxMessageSize;
895 *
this >>
message.MaxChunkCount;
918 *
this <<
header.ChannelId;
927 *
this >>
header.ChannelId;
933 *
this <<
header.SecurityPolicyUri;
934 *
this <<
header.SenderCertificate;
935 *
this <<
header.ReceiverCertificateThumbPrint;
941 *
this >>
header.SecurityPolicyUri;
942 *
this >>
header.SenderCertificate;
943 *
this >>
header.ReceiverCertificateThumbPrint;
962 *
this <<
header.SequenceNumber;
963 *
this <<
header.RequestId;
969 *
this >>
header.SequenceNumber;
970 *
this >>
header.RequestId;
992 *
this <<
header.SessionAuthenticationToken;
994 *
this <<
header.RequestHandle;
995 *
this <<
header.ReturnDiagnostics;
996 *
this <<
header.AuditEntryId;
998 *
this <<
header.Additional;
1004 *
this >>
header.SessionAuthenticationToken;
1006 *
this >>
header.RequestHandle;
1007 *
this >>
header.ReturnDiagnostics;
1008 *
this >>
header.AuditEntryId;
1010 *
this >>
header.Additional;
1016 *this << static_cast<uint8_t>(value);
1030 *
this <<
info.EncodingMask;
1034 *
this <<
info.SymbolicId;
1039 *
this <<
info.NamespaceURI;
1044 *
this <<
info.LocalizedText;
1049 *
this <<
info.Locale;
1054 *
this <<
info.AdditionalInfo;
1059 *
this <<
info.InnerStatusCode;
1064 *
this << *
info.InnerDiagnostics;
1072 *
this >>
info.EncodingMask;
1076 *
this >>
info.SymbolicId;
1081 *
this >>
info.NamespaceURI;
1086 *
this >>
info.LocalizedText;
1091 *
this >>
info.Locale;
1096 *
this >>
info.AdditionalInfo;
1101 *
this >>
info.InnerStatusCode;
1108 info.InnerDiagnostics = tmp;
1127 *
this <<
header.Timestamp;
1128 *
this <<
header.RequestHandle;
1129 *
this <<
header.ServiceResult;
1130 *
this <<
header.InnerDiagnostics;
1132 *
this <<
header.Additional;
1138 *
this >>
header.Timestamp;
1139 *
this >>
header.RequestHandle;
1140 *
this >>
header.ServiceResult;
1141 *
this >>
header.InnerDiagnostics;
1143 *
this >>
header.Additional;
1150 *
this << request.TypeId;
1151 *
this << request.Header;
1152 *
this << request.Parameters.ClientProtocolVersion;
1153 *
this << (uint32_t)request.Parameters.RequestType;
1154 *
this << (uint32_t)request.Parameters.SecurityMode;
1156 *
this << request.Parameters.RequestLifeTime;
1162 *
this >> request.TypeId;
1163 *
this >> request.Header;
1165 *
this >> request.Parameters.ClientProtocolVersion;
1176 *
this >> request.Parameters.RequestLifeTime;
1183 *
this << token.SecureChannelId;
1184 *
this << token.TokenId;
1185 *
this << token.CreatedAt;
1186 *
this << token.RevisedLifetime;
1192 *
this >> token.SecureChannelId;
1193 *
this >> token.TokenId;
1194 *
this >> token.CreatedAt;
1195 *
this >> token.RevisedLifetime;
1202 *
this << response.TypeId;
1203 *
this << response.Header;
1204 *
this << response.ServerProtocolVersion;
1205 *
this << response.ChannelSecurityToken;
1212 *
this >> response.TypeId;
1213 *
this >> response.Header;
1214 *
this >> response.ServerProtocolVersion;
1215 *
this >> response.ChannelSecurityToken;
1222 Buffer.insert(Buffer.end(), raw.Data, raw.Data + raw.Size);
1228 GetData(In, raw.Data, raw.Size);
1234 *
this << request.TypeId;
1235 *
this << request.Header;
1241 *
this >> request.TypeId;
1242 *
this >> request.Header;
1248 *
this << lt.Encoding;
1264 *
this >> lt.Encoding;
1279 void DataSerializer::Serialize<std::vector<LocalizedText>>(
const std::vector<LocalizedText> & value)
1285 void DataDeserializer::Deserialize<std::vector<LocalizedText>>(std::vector<LocalizedText> & value)
1293 *
this << value.TypeId;
1294 *this << static_cast<uint8_t>(value.Encoding);
1300 *
this >> value.TypeId;
1309 *
this <<
name.NamespaceIndex;
1316 *
this >>
name.NamespaceIndex;
1325 void DataSerializer::Serialize<IntegerId>(
const IntegerId & id)
1327 *this << static_cast<uint32_t>(id);
1331 void DataDeserializer::Deserialize<IntegerId>(
IntegerId & id)
1339 void DataSerializer::Serialize<std::vector<IntegerId>>(
const std::vector<IntegerId> & targets)
1345 void DataDeserializer::Deserialize<std::vector<IntegerId>>(std::vector<IntegerId> & targets)
1355 void DataSerializer::Serialize<StatusCode>(
const StatusCode & status)
1357 *this << static_cast<uint32_t>(status);
1361 void DataDeserializer::Deserialize<StatusCode>(
StatusCode & status)
1369 void DataSerializer::Serialize<std::vector<StatusCode>>(
const std::vector<StatusCode> & value)
1375 void DataDeserializer::Deserialize<std::vector<StatusCode>>(std::vector<StatusCode> & value)
1381 void DataSerializer::Serialize<std::vector<QualifiedName>>(
const std::vector<QualifiedName> & value)
1387 void DataDeserializer::Deserialize<std::vector<QualifiedName>>(std::vector<QualifiedName> & value)
void SerializeContainer(Stream &out, const Container &c, uint32_t emptySizeValue=~uint32_t())
void DeserializeContainer(Stream &in, Container &c)
std_msgs::Header * header(M &m)
struct OpcUa::NodeId::FourByteDataType FourByteData
OPC UA Address space part. GNU LGPL.
const char * Binary(const char *input, short n)
std::vector< DiagnosticInfo > DiagnosticInfoList
IntegerId & operator=(const IntegerId &id)