binary_stream.cpp
Go to the documentation of this file.
1 
11 #include "binary_serialization.h"
12 
16 
17 #include <algorithm>
18 #include <iostream>
19 #include <stdexcept>
20 
21 #ifdef _WIN32
22 #include <WinSock2.h>
23 #else
24 #include <netinet/in.h>
25 #endif
26 
27 namespace
28 {
29 
30 /*
31  * not used
32  *
33 float float_htonl(float value){
34  std::cout << "sizez: " << sizeof(float) << " " << sizeof(uint32_t) << std::endl;
35  union v {
36  float f;
37  uint32_t i;
38  };
39 
40  union v val;
41 
42  val.f = value;
43  val.i = htonl(val.i);
44 
45  return val.f;
46 }
47 
48 float float_ntohl(float value){
49  union v {
50  float f;
51  uint32_t i;
52  };
53 
54  union v val;
55 
56  val.f = value;
57  val.i = ntohl(val.i);
58 
59  return val.f;
60 }
61  */
62 
63 template <typename Integer16>
64 inline int8_t LoByte(Integer16 value)
65 {
66  return value & 0xFF;
67 }
68 
69 template <typename Integer16>
70 inline int8_t HiByte(Integer16 value)
71 {
72  return (value & 0xFF00) >> 8;
73 }
74 
75 template <typename Integer32>
76 inline uint16_t LoWord(Integer32 value)
77 {
78  return value & 0xFFFF;
79 }
80 
81 template <typename Integer32>
82 inline uint16_t HiWord(Integer32 value)
83 {
84  return (value & 0xFFFF0000) >> 16;
85 }
86 
87 template <typename Integer64>
88 inline uint32_t LoDWord(Integer64 value)
89 {
90  return value & 0xFFFFFFFF;
91 }
92 
93 template <typename Integer64>
94 inline uint32_t HiDWord(Integer64 value)
95 {
96  return (value & 0xFFFFFFFF00000000) >> 32;
97 }
98 
99 template <typename Integer16>
100 inline Integer16 MakeWord(int8_t loByte, int8_t hiByte)
101 {
102  const Integer16 word = hiByte;
103  return (word << 8) | (0x00FF & loByte);
104 }
105 
106 template<typename Integer>
107 inline Integer MakeNumber(char * data)
108 {
109  unsigned size = sizeof(Integer);
110  Integer i = 0;
111 
112  while (size)
113  {
114  i = (i << 8) | (0x00FF & data[--size]);
115  }
116 
117  return i;
118 }
119 
120 
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'};
128 
129 inline void ThrowReceivedNotEnoughData()
130 {
131  throw std::logic_error("Not enough data was received from channel.");
132 }
133 
134 
135 template <typename ChannelType>
136 inline void GetData(ChannelType & channel, char * data, std::size_t size)
137 {
138  size_t recv = channel.Read(data, size);
139 
140  if (recv != size)
141  {
142  std::cout << "expecting " << size << " received: " << recv << std::endl;
143  ThrowReceivedNotEnoughData();
144  }
145 }
146 
147 
148 } // namespace
149 
150 
151 namespace OpcUa
152 {
154  : Encoding(ExtensionObjectEncoding::NONE)
155 {
156 }
157 
160  : Encoding(encoding)
161 {
163  TypeId.FourByteData.Identifier = objectId;
164 }
166 // IntegerId
168 
170  : Value(1)
171 {
172 }
173 
175  : Value(id.Value)
176 {
177 }
178 
179 IntegerId::IntegerId(uint32_t num)
180  : Value(num)
181 {
182  if (!Value)
183  {
184  throw std::invalid_argument("IntegerId cannot be zero");
185  }
186 }
187 
189 {
190  Value = id.Value;
191  return *this;
192 }
193 
195 {
196  if (!Value)
197  {
198  throw std::invalid_argument("IntegerId cannot be zero");
199  }
200 
201  Value = value;
202  return *this;
203 }
204 
205 IntegerId::operator uint32_t() const
206 {
207  return Value;
208 }
209 
211 
212 namespace Binary
213 {
214 
215 template<>
216 void DataSerializer::Serialize<int8_t>(const int8_t & value)
217 {
218  Buffer.push_back(value);
219 }
220 
221 template<>
222 void DataSerializer::Serialize<std::vector<int8_t>>(const std::vector<int8_t> & value)
223 {
224  SerializeContainer(*this, value);
225 }
226 
227 template<>
228 void DataSerializer::Serialize<uint8_t>(const uint8_t & value)
229 {
230  Buffer.push_back(value);
231 }
232 
233 template<>
234 void DataDeserializer::Deserialize<uint8_t>(uint8_t & value)
235 {
236  char data = 0;
237  GetData(In, &data, 1);
238  value = static_cast<uint8_t>(data);
239 }
240 
241 template<>
242 void DataDeserializer::Deserialize<int8_t>(int8_t & value)
243 {
244  char data = 0;
245  GetData(In, &data, 1);
246  value = data;
247 }
248 
249 template<>
250 void DataDeserializer::Deserialize<std::vector<int8_t>>(std::vector<int8_t> & value)
251 {
252  DeserializeContainer(*this, value);
253 }
254 
255 
256 template<>
257 void DataSerializer::Serialize<int16_t>(const int16_t & value)
258 {
259  Buffer.push_back(LoByte(value));
260  Buffer.push_back(HiByte(value));
261 }
262 
263 template<>
264 void DataSerializer::Serialize<uint16_t>(const uint16_t & value)
265 {
266  Buffer.push_back(LoByte(value));
267  Buffer.push_back(HiByte(value));
268 }
269 
270 template<>
271 void DataDeserializer::Deserialize<uint16_t>(uint16_t & value)
272 {
273  char data[2] = {0};
274  GetData(In, data, 2);
275  value = MakeWord<uint16_t>(data[0], data[1]);
276 }
277 
278 template<>
279 void DataDeserializer::Deserialize<int16_t>(int16_t & value)
280 {
281  char data[2] = {0};
282  GetData(In, data, 2);
283  value = MakeWord<int16_t>(data[0], data[1]);
284 }
285 
286 template<>
287 void DataSerializer::Serialize<int32_t>(const int32_t & value)
288 {
289  Serialize(LoWord(value));
290  Serialize(HiWord(value));
291 }
292 
293 template<>
294 void DataSerializer::Serialize<uint32_t>(const uint32_t & value)
295 {
296  Serialize(LoWord(value));
297  Serialize(HiWord(value));
298 }
299 
300 template<>
301 void DataDeserializer::Deserialize<uint32_t>(uint32_t & value)
302 {
303  char data[4] = {0};
304  GetData(In, data, 4);
305  value = MakeNumber<uint32_t>(data);
306 }
307 
308 template<>
309 void DataDeserializer::Deserialize<int32_t>(int32_t & value)
310 {
311  char data[4] = {0};
312  GetData(In, data, 4);
313  value = MakeNumber<int32_t>(data);
314 }
315 
316 template<>
317 void DataSerializer::Serialize<int64_t>(const int64_t & value)
318 {
319  Serialize(LoDWord(value));
320  Serialize(HiDWord(value));
321 }
322 
323 template<>
324 void DataSerializer::Serialize<uint64_t>(const uint64_t & value)
325 {
326  Serialize(LoDWord(value));
327  Serialize(HiDWord(value));
328 }
329 
330 template<>
331 void DataDeserializer::Deserialize<uint64_t>(uint64_t & value)
332 {
333  char data[8] = {0};
334  GetData(In, data, 8);
335  value = MakeNumber<uint64_t>(data);
336 }
337 
338 template<>
339 void DataDeserializer::Deserialize<int64_t>(int64_t & value)
340 {
341  char data[8] = {0};
342  GetData(In, data, 8);
343  value = MakeNumber<int64_t>(data);
344 }
345 
346 template<>
347 void DataSerializer::Serialize<bool>(const bool & value)
348 {
349  Serialize(static_cast<uint8_t>(value));
350 }
351 
352 template<>
353 void DataSerializer::Serialize<std::vector<bool>>(const std::vector<bool> & value)
354 {
355  SerializeContainer(*this, value);
356 }
357 
358 template<>
359 void DataDeserializer::Deserialize<bool>(bool & value)
360 {
361  uint8_t tmp = 0;
362  *this >> tmp;
363  value = (tmp != 0);
364 }
365 
366 template<>
367 void DataDeserializer::Deserialize<std::vector<bool>>(std::vector<bool> & value)
368 {
369  DeserializeContainer(*this, value);
370 }
371 
372 
373 template<>
374 void DataSerializer::Serialize<float>(const float & value)
375 {
376  //float network_value = float_htonl(value);
377  const uint8_t * data = reinterpret_cast<const uint8_t *>(&value);
378 
379  for (int i = 0; i < 4; ++i)
380  {
381  Serialize(data[i]);
382  }
383 }
384 
385 template<>
386 void DataDeserializer::Deserialize<float>(float & value)
387 {
388  //float network_value = float_htonl(value);
389  uint8_t data[4] = {0};
390 
391  for (int i = 0; i < 4; ++i)
392  {
393  *this >> data[i];
394  }
395 
396  value = *reinterpret_cast<const float *>(data); // FIXME: probably broken on ARM
397 }
398 
399 template<>
400 void DataSerializer::Serialize<double>(const double & value)
401 {
402  const uint8_t * data = reinterpret_cast<const uint8_t *>(&value);
403 
404  for (int i = 0; i < 8; ++i)
405  {
406  Serialize(data[i]);
407  }
408 }
409 
410 template<>
411 void DataDeserializer::Deserialize<double>(double & value)
412 {
413  uint8_t data[8] = {0};
414 
415  for (int i = 0; i < 8; ++i)
416  {
417  *this >> data[i];
418  }
419 
420  value = *reinterpret_cast<const double *>(data); //FIXME: probably broken on ARM
421 }
422 
423 template<>
424 void DataSerializer::Serialize<OpcUa::Guid>(const OpcUa::Guid & value)
425 {
426  *this << value.Data1 << value.Data2 << value.Data3;
427  Buffer.insert(Buffer.end(), value.Data4, value.Data4 + 8);
428 }
429 
430 template<>
431 void DataDeserializer::Deserialize<OpcUa::Guid>(OpcUa::Guid & value)
432 {
433  *this >> value.Data1 >> value.Data2 >> value.Data3;
434  char data[8] = {0};
435  GetData(In, data, 8);
436  std::copy(data, data + 8, value.Data4);
437 }
438 
439 template<>
440 void DataSerializer::Serialize<std::string>(const std::string & value)
441 {
442  if (value.empty())
443  {
444  Serialize(~uint32_t());
445  return;
446  }
447 
448  Serialize(static_cast<uint32_t>(value.size()));
449  Buffer.insert(Buffer.end(), value.begin(), value.end());
450 }
451 
452 template<>
453 void DataDeserializer::Deserialize<std::string>(std::string & value)
454 {
455  uint32_t stringSize = 0;
456  *this >> stringSize;
457 
458  if (stringSize != ~uint32_t())
459  {
460  value.resize(stringSize);
461  GetData(In, &value[0], stringSize);
462  return;
463  }
464 
465  value.clear();
466  return;
467  // TODO standard says that 0xff*4 - is the zero byte string , actually it seems that it is an empty string
468  /*
469  while(true)
470  {
471  uint8_t val = 0;
472  *this >> val;
473  if (val == 0)
474  {
475  return;
476  }
477  value.push_back(val);
478  }
479  */
480 }
481 
482 
483 template<>
484 void DataSerializer::Serialize<OpcUa::DateTime>(const OpcUa::DateTime & date)
485 {
486  *this << date.Value;
487 }
488 
489 template<>
490 void DataDeserializer::Deserialize<OpcUa::DateTime>(OpcUa::DateTime & date)
491 {
492  *this >> date.Value;
493 }
494 
495 template<>
496 void DataSerializer::Serialize<std::vector<OpcUa::DateTime>>(const std::vector<OpcUa::DateTime> & date)
497 {
498  SerializeContainer(*this, date);
499 }
500 
501 template<>
502 void DataDeserializer::Deserialize<std::vector<OpcUa::DateTime>>(std::vector<OpcUa::DateTime> & date)
503 {
504  DeserializeContainer(*this, date);
505 }
506 
507 template<>
508 void DataSerializer::Serialize<ByteString>(const ByteString & value)
509 {
510  if (value.Data.empty())
511  {
512  Serialize(~uint32_t());
513  return;
514  }
515 
516  Serialize(static_cast<uint32_t>(value.Data.size()));
517  Buffer.insert(Buffer.end(), value.Data.begin(), value.Data.end());
518 }
519 
520 template<>
521 void DataDeserializer::Deserialize<ByteString>(ByteString & value)
522 {
523  uint32_t stringSize = 0;
524  *this >> stringSize;
525 
526  if (stringSize != ~uint32_t())
527  {
528  value.Data.resize(stringSize);
529  GetData(In, reinterpret_cast<char *>(&value.Data[0]), stringSize);
530  return;
531  }
532 
533  value.Data.clear();
534  return;
535 }
536 
537 
538 template<>
539 void DataSerializer::Serialize<std::vector<ByteString>>(const std::vector<ByteString> & value)
540 {
541  SerializeContainer(*this, value);
542 }
543 
544 template<>
545 void DataDeserializer::Deserialize<std::vector<ByteString>>(std::vector<ByteString> & value)
546 {
547  DeserializeContainer(*this, value);
548 }
549 
550 
551 template<>
552 void DataSerializer::Serialize<std::vector<uint8_t>>(const std::vector<uint8_t> & value)
553 {
554  SerializeContainer(*this, value);
555 }
556 
557 template<>
558 void DataDeserializer::Deserialize<std::vector<uint8_t>>(std::vector<uint8_t> & value)
559 {
560  DeserializeContainer(*this, value);
561 }
562 
563 template<>
564 void DataSerializer::Serialize<std::vector<uint16_t>>(const std::vector<uint16_t> & value)
565 {
566  SerializeContainer(*this, value);
567 }
568 
569 template<>
570 void DataDeserializer::Deserialize<std::vector<uint16_t>>(std::vector<uint16_t> & value)
571 {
572  DeserializeContainer(*this, value);
573 }
574 
575 template<>
576 void DataSerializer::Serialize<std::vector<int16_t>>(const std::vector<int16_t> & value)
577 {
578  SerializeContainer(*this, value);
579 }
580 
581 template<>
582 void DataDeserializer::Deserialize<std::vector<int16_t>>(std::vector<int16_t> & value)
583 {
584  DeserializeContainer(*this, value);
585 }
586 
587 
588 template<>
589 void DataSerializer::Serialize<std::vector<uint32_t>>(const std::vector<uint32_t> & value)
590 {
591  SerializeContainer(*this, value);
592 }
593 
594 template<>
595 void DataDeserializer::Deserialize<std::vector<uint32_t>>(std::vector<uint32_t> & value)
596 {
597  DeserializeContainer(*this, value);
598 }
599 
600 template<>
601 void DataSerializer::Serialize<std::vector<int32_t>>(const std::vector<int32_t> & value)
602 {
603  SerializeContainer(*this, value);
604 }
605 
606 template<>
607 void DataDeserializer::Deserialize<std::vector<int32_t>>(std::vector<int32_t> & value)
608 {
609  DeserializeContainer(*this, value);
610 }
611 
612 template<>
613 void DataSerializer::Serialize<std::vector<int64_t>>(const std::vector<int64_t> & value)
614 {
615  SerializeContainer(*this, value);
616 }
617 
618 template<>
619 void DataDeserializer::Deserialize<std::vector<int64_t>>(std::vector<int64_t> & value)
620 {
621  DeserializeContainer(*this, value);
622 }
623 
624 template<>
625 void DataSerializer::Serialize<std::vector<uint64_t>>(const std::vector<uint64_t> & value)
626 {
627  SerializeContainer(*this, value);
628 }
629 
630 template<>
631 void DataDeserializer::Deserialize<std::vector<uint64_t>>(std::vector<uint64_t> & value)
632 {
633  DeserializeContainer(*this, value);
634 }
635 
636 
637 template<>
638 void DataSerializer::Serialize<std::vector<float>>(const std::vector<float> & value)
639 {
640  SerializeContainer(*this, value);
641 }
642 
643 template<>
644 void DataDeserializer::Deserialize<std::vector<double>>(std::vector<double> & value)
645 {
646  DeserializeContainer(*this, value);
647 }
648 
649 template<>
650 void DataSerializer::Serialize<std::vector<double>>(const std::vector<double> & value)
651 {
652  SerializeContainer(*this, value);
653 }
654 
655 template<>
656 void DataDeserializer::Deserialize<std::vector<float>>(std::vector<float> & value)
657 {
658  DeserializeContainer(*this, value);
659 }
660 
661 
662 template<>
663 void DataSerializer::Serialize<std::vector<Guid>>(const std::vector<Guid> & value)
664 {
665  SerializeContainer(*this, value);
666 }
667 
668 template<>
669 void DataDeserializer::Deserialize<std::vector<Guid>>(std::vector<Guid> & value)
670 {
671  DeserializeContainer(*this, value);
672 }
673 
674 template<>
675 void DataSerializer::Serialize<std::vector<NodeId>>(const std::vector<NodeId> & value)
676 {
677  SerializeContainer(*this, value);
678 }
679 
680 template<>
681 void DataDeserializer::Deserialize<std::vector<NodeId>>(std::vector<NodeId> & value)
682 {
683  DeserializeContainer(*this, value);
684 }
685 
686 template<>
687 void DataSerializer::Serialize<std::vector<std::string>>(const std::vector<std::string> & value)
688 {
689  SerializeContainer(*this, value);
690 }
691 
692 template<>
693 void DataDeserializer::Deserialize<std::vector<std::string>>(std::vector<std::string> & value)
694 {
695  DeserializeContainer(*this, value);
696 }
697 
698 template<>
699 void DataSerializer::Serialize<std::vector<std::vector<uint8_t>>>(const std::vector<std::vector<uint8_t>> & value)
700 {
701  SerializeContainer(*this, value);
702 }
703 
704 template<>
705 void DataDeserializer::Deserialize<std::vector<std::vector<uint8_t>>>(std::vector<std::vector<uint8_t>> & value)
706 {
707  DeserializeContainer(*this, value);
708 }
709 
710 template<>
711 void DataSerializer::Serialize<OpcUa::Binary::MessageType>(const OpcUa::Binary::MessageType & value)
712 {
713  const char * typeName = nullptr;
714 
715  switch (value)
716  {
717  case MT_HELLO:
718  typeName = MESSAGE_TYPE_HELLO;
719  break;
720 
721  case MT_ACKNOWLEDGE:
722  typeName = MESSAGE_TYPE_ACKNOWLEDGE;
723  break;
724 
725  case MT_ERROR:
726  typeName = MESSAGE_TYPE_ERROR;
727  break;
728 
729  case MT_SECURE_OPEN:
730  typeName = MESSAGE_TYPE_OPEN;
731  break;
732 
733  case MT_SECURE_CLOSE:
734  typeName = MESSAGE_TYPE_CLOSE;
735  break;
736 
737  case MT_SECURE_MESSAGE:
738  typeName = MESSAGE_TYPE_MESSAGE;
739  break;
740 
741  default:
742  throw std::logic_error("Invalid message type.");
743  }
744 
745  Buffer.insert(Buffer.end(), typeName, typeName + MESSAGE_TYPE_SIZE);
746 }
747 
748 template<>
749 void DataDeserializer::Deserialize<OpcUa::Binary::MessageType>(OpcUa::Binary::MessageType & value)
750 {
751  char data[MESSAGE_TYPE_SIZE] = {0};
752  GetData(In, data, MESSAGE_TYPE_SIZE);
753 
754  if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_HELLO))
755  {
756  value = OpcUa::Binary::MT_HELLO;
757  }
758 
759  else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_ACKNOWLEDGE))
760  {
762  }
763 
764  else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_ERROR))
765  {
766  value = OpcUa::Binary::MT_ERROR;
767  }
768 
769  else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_MESSAGE))
770  {
772  }
773 
774  else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_OPEN))
775  {
777  }
778 
779  else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_CLOSE))
780  {
782  }
783 
784  else
785  {
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);
790  }
791 }
792 
793 template<>
794 void DataSerializer::Serialize<OpcUa::Binary::ChunkType>(const OpcUa::Binary::ChunkType & value)
795 {
796  switch (value)
797  {
798  case CHT_SINGLE:
799  Buffer.push_back('F');
800  break;
801 
802  case CHT_INTERMEDIATE:
803  Buffer.push_back('C');
804  break;
805 
806  case CHT_FINAL:
807  Buffer.push_back('A');
808  break;
809 
810  default:
811  throw std::logic_error("Invalid Chunk Type");
812  }
813 }
814 
815 template<>
816 void DataDeserializer::Deserialize<OpcUa::Binary::ChunkType>(OpcUa::Binary::ChunkType & value)
817 {
818  char data = 0;
819  GetData(In, &data, 1);
820 
821  switch (data)
822  {
823  case 'F':
824  value = CHT_SINGLE;
825  break;
826 
827  case 'C':
828  value = CHT_INTERMEDIATE;
829  break;
830 
831  case 'A':
832  value = CHT_FINAL;
833  break;
834 
835  default:
836  throw std::logic_error("Invalid chunk type received.");
837  };
838 }
839 
840 template<>
841 void DataSerializer::Serialize<OpcUa::Binary::Header>(const OpcUa::Binary::Header & header)
842 {
843  *this << header.Type;
844  *this << header.Chunk;
845  *this << header.Size;
846 }
847 
848 template<>
849 void DataDeserializer::Deserialize<OpcUa::Binary::Header>(OpcUa::Binary::Header & header)
850 {
851  *this >> header.Type;
852  *this >> header.Chunk;
853  *this >> header.Size;
854 }
855 
856 template<>
857 void DataSerializer::Serialize<OpcUa::Binary::Hello>(const OpcUa::Binary::Hello & message)
858 {
859  *this << message.ProtocolVersion;
860  *this << message.ReceiveBufferSize;
861  *this << message.SendBufferSize;
862  *this << message.MaxMessageSize;
863  *this << message.MaxChunkCount;
864  *this << message.EndpointUrl;
865 }
866 
867 template<>
868 void DataDeserializer::Deserialize<OpcUa::Binary::Hello>(OpcUa::Binary::Hello & message)
869 {
870  *this >> message.ProtocolVersion;
871  *this >> message.ReceiveBufferSize;
872  *this >> message.SendBufferSize;
873  *this >> message.MaxMessageSize;
874  *this >> message.MaxChunkCount;
875  *this >> message.EndpointUrl;
876 }
877 
878 template<>
879 void DataSerializer::Serialize<OpcUa::Binary::Acknowledge>(const OpcUa::Binary::Acknowledge & message)
880 {
881  *this << message.ProtocolVersion;
882  *this << message.ReceiveBufferSize;
883  *this << message.SendBufferSize;
884  *this << message.MaxMessageSize;
885  *this << message.MaxChunkCount;
886 }
887 
888 template<>
889 void DataDeserializer::Deserialize<OpcUa::Binary::Acknowledge>(OpcUa::Binary::Acknowledge & message)
890 {
891  *this >> message.ProtocolVersion;
892  *this >> message.ReceiveBufferSize;
893  *this >> message.SendBufferSize;
894  *this >> message.MaxMessageSize;
895  *this >> message.MaxChunkCount;
896 }
897 
898 template<>
899 void DataSerializer::Serialize<OpcUa::Binary::Error>(const OpcUa::Binary::Error & message)
900 {
901  *this << message.Code;
902  *this << message.Reason;
903 }
904 
905 template<>
906 void DataDeserializer::Deserialize<OpcUa::Binary::Error>(OpcUa::Binary::Error & message)
907 {
908  *this >> message.Code;
909  *this >> message.Reason;
910 }
911 
912 template<>
913 void DataSerializer::Serialize<OpcUa::Binary::SecureHeader>(const OpcUa::Binary::SecureHeader & header)
914 {
915  *this << header.Type;
916  *this << header.Chunk;
917  *this << header.Size;
918  *this << header.ChannelId;
919 }
920 
921 template<>
922 void DataDeserializer::Deserialize<OpcUa::Binary::SecureHeader>(OpcUa::Binary::SecureHeader & header)
923 {
924  *this >> header.Type;
925  *this >> header.Chunk;
926  *this >> header.Size;
927  *this >> header.ChannelId;
928 }
929 
930 template<>
931 void DataSerializer::Serialize<OpcUa::Binary::AsymmetricAlgorithmHeader>(const OpcUa::Binary::AsymmetricAlgorithmHeader & header)
932 {
933  *this << header.SecurityPolicyUri;
934  *this << header.SenderCertificate;
935  *this << header.ReceiverCertificateThumbPrint;
936 }
937 
938 template<>
939 void DataDeserializer::Deserialize<OpcUa::Binary::AsymmetricAlgorithmHeader>(OpcUa::Binary::AsymmetricAlgorithmHeader & header)
940 {
941  *this >> header.SecurityPolicyUri;
942  *this >> header.SenderCertificate;
943  *this >> header.ReceiverCertificateThumbPrint;
944 }
945 
946 template<>
947 void DataSerializer::Serialize<OpcUa::Binary::SymmetricAlgorithmHeader>(const OpcUa::Binary::SymmetricAlgorithmHeader & header)
948 {
949  *this << header.TokenId;
950 }
951 
952 template<>
953 void DataDeserializer::Deserialize<OpcUa::Binary::SymmetricAlgorithmHeader>(OpcUa::Binary::SymmetricAlgorithmHeader & header)
954 {
955  *this >> header.TokenId;
956 }
957 
958 
959 template<>
960 void DataSerializer::Serialize<OpcUa::Binary::SequenceHeader>(const OpcUa::Binary::SequenceHeader & header)
961 {
962  *this << header.SequenceNumber;
963  *this << header.RequestId;
964 }
965 
966 template<>
967 void DataDeserializer::Deserialize<OpcUa::Binary::SequenceHeader>(OpcUa::Binary::SequenceHeader & header)
968 {
969  *this >> header.SequenceNumber;
970  *this >> header.RequestId;
971 }
972 
973 
974 template<>
975 void DataSerializer::Serialize<OpcUa::AdditionalHeader>(const OpcUa::AdditionalHeader & header)
976 {
977  *this << header.TypeId;
978  *this << header.Encoding;
979 }
980 
981 template<>
982 void DataDeserializer::Deserialize<OpcUa::AdditionalHeader>(OpcUa::AdditionalHeader & header)
983 {
984  *this >> header.TypeId;
985  *this >> header.Encoding;
986 }
987 
988 
989 template<>
990 void DataSerializer::Serialize<OpcUa::RequestHeader>(const OpcUa::RequestHeader & header)
991 {
992  *this << header.SessionAuthenticationToken;
993  *this << header.UtcTime;
994  *this << header.RequestHandle;
995  *this << header.ReturnDiagnostics;
996  *this << header.AuditEntryId;
997  *this << header.Timeout; // in miliseconds
998  *this << header.Additional;
999 }
1000 
1001 template<>
1002 void DataDeserializer::Deserialize<OpcUa::RequestHeader>(OpcUa::RequestHeader & header)
1003 {
1004  *this >> header.SessionAuthenticationToken;
1005  *this >> header.UtcTime;
1006  *this >> header.RequestHandle;
1007  *this >> header.ReturnDiagnostics;
1008  *this >> header.AuditEntryId;
1009  *this >> header.Timeout; // in miliseconds
1010  *this >> header.Additional;
1011 }
1012 
1013 template<>
1014 void DataSerializer::Serialize<DiagnosticInfoMask>(const DiagnosticInfoMask & value)
1015 {
1016  *this << static_cast<uint8_t>(value);
1017 }
1018 
1019 template<>
1020 void DataDeserializer::Deserialize<DiagnosticInfoMask>(DiagnosticInfoMask & value)
1021 {
1022  uint8_t tmp = 0;
1023  *this >> tmp;
1024  value = static_cast<DiagnosticInfoMask>(tmp);
1025 }
1026 
1027 template<>
1028 void DataSerializer::Serialize<OpcUa::DiagnosticInfo>(const OpcUa::DiagnosticInfo & info)
1029 {
1030  *this << info.EncodingMask;
1031 
1032  if (info.EncodingMask & DIM_SYMBOLIC_Id)
1033  {
1034  *this << info.SymbolicId;
1035  }
1036 
1037  if (info.EncodingMask & DIM_NAMESPACE)
1038  {
1039  *this << info.NamespaceURI;
1040  }
1041 
1042  if (info.EncodingMask & DIM_LOCALIZED_TEXT)
1043  {
1044  *this << info.LocalizedText;
1045  }
1046 
1047  if (info.EncodingMask & DIM_LOCALE)
1048  {
1049  *this << info.Locale;
1050  }
1051 
1052  if (info.EncodingMask & DIM_ADDITIONAL_INFO)
1053  {
1054  *this << info.AdditionalInfo;
1055  }
1056 
1057  if (info.EncodingMask & DIM_INNER_STATUS_CODE)
1058  {
1059  *this << info.InnerStatusCode;
1060  }
1061 
1062  if ((info.EncodingMask & DIM_INNER_DIAGNOSTIC_INFO) && info.InnerDiagnostics)
1063  {
1064  *this << *info.InnerDiagnostics;
1065  }
1066 }
1067 
1068 
1069 template<>
1070 void DataDeserializer::Deserialize<OpcUa::DiagnosticInfo>(OpcUa::DiagnosticInfo & info)
1071 {
1072  *this >> info.EncodingMask;
1073 
1074  if (info.EncodingMask & DIM_SYMBOLIC_Id)
1075  {
1076  *this >> info.SymbolicId;
1077  }
1078 
1079  if (info.EncodingMask & DIM_NAMESPACE)
1080  {
1081  *this >> info.NamespaceURI;
1082  }
1083 
1084  if (info.EncodingMask & DIM_LOCALIZED_TEXT)
1085  {
1086  *this >> info.LocalizedText;
1087  }
1088 
1089  if (info.EncodingMask & DIM_LOCALE)
1090  {
1091  *this >> info.Locale;
1092  }
1093 
1094  if (info.EncodingMask & DIM_ADDITIONAL_INFO)
1095  {
1096  *this >> info.AdditionalInfo;
1097  }
1098 
1099  if (info.EncodingMask & DIM_INNER_STATUS_CODE)
1100  {
1101  *this >> info.InnerStatusCode;
1102  }
1103 
1104  if (info.EncodingMask & DIM_INNER_DIAGNOSTIC_INFO)
1105  {
1106  std::shared_ptr<DiagnosticInfo> tmp(new DiagnosticInfo);
1107  *this >> *tmp;
1108  info.InnerDiagnostics = tmp;
1109  }
1110 }
1111 
1112 template<>
1113 void DataSerializer::Serialize<OpcUa::DiagnosticInfoList>(const OpcUa::DiagnosticInfoList & infos)
1114 {
1115  SerializeContainer(*this, infos, 0);
1116 }
1117 
1118 template<>
1119 void DataDeserializer::Deserialize<OpcUa::DiagnosticInfoList>(OpcUa::DiagnosticInfoList & infos)
1120 {
1121  DeserializeContainer(*this, infos);
1122 }
1123 
1124 template<>
1125 void DataSerializer::Serialize<OpcUa::ResponseHeader>(const OpcUa::ResponseHeader & header)
1126 {
1127  *this << header.Timestamp;
1128  *this << header.RequestHandle;
1129  *this << header.ServiceResult;
1130  *this << header.InnerDiagnostics;
1131  SerializeContainer(*this, header.StringTable);
1132  *this << header.Additional;
1133 }
1134 
1135 template<>
1136 void DataDeserializer::Deserialize<OpcUa::ResponseHeader>(OpcUa::ResponseHeader & header)
1137 {
1138  *this >> header.Timestamp;
1139  *this >> header.RequestHandle;
1140  *this >> header.ServiceResult;
1141  *this >> header.InnerDiagnostics;
1142  DeserializeContainer(*this, header.StringTable);
1143  *this >> header.Additional;
1144 }
1145 
1146 
1147 template<>
1148 void DataSerializer::Serialize<OpcUa::OpenSecureChannelRequest>(const OpcUa::OpenSecureChannelRequest & request)
1149 {
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;
1155  SerializeContainer(*this, request.Parameters.ClientNonce);
1156  *this << request.Parameters.RequestLifeTime;
1157 }
1158 
1159 template<>
1160 void DataDeserializer::Deserialize<OpcUa::OpenSecureChannelRequest>(OpcUa::OpenSecureChannelRequest & request)
1161 {
1162  *this >> request.TypeId;
1163  *this >> request.Header;
1164 
1165  *this >> request.Parameters.ClientProtocolVersion;
1166 
1167  uint32_t tmp = 0;
1168  *this >> tmp;
1169  request.Parameters.RequestType = static_cast<SecurityTokenRequestType>(tmp);
1170 
1171  uint32_t tmp2 = 0;
1172  *this >> tmp2;
1173  request.Parameters.SecurityMode = static_cast<MessageSecurityMode>(tmp2);
1174 
1175  DeserializeContainer(*this, request.Parameters.ClientNonce);
1176  *this >> request.Parameters.RequestLifeTime;
1177 }
1178 
1179 
1180 template<>
1181 void DataSerializer::Serialize<OpcUa::SecurityToken>(const OpcUa::SecurityToken & token)
1182 {
1183  *this << token.SecureChannelId;
1184  *this << token.TokenId;
1185  *this << token.CreatedAt;
1186  *this << token.RevisedLifetime;
1187 }
1188 
1189 template<>
1190 void DataDeserializer::Deserialize<OpcUa::SecurityToken>(OpcUa::SecurityToken & token)
1191 {
1192  *this >> token.SecureChannelId;
1193  *this >> token.TokenId;
1194  *this >> token.CreatedAt;
1195  *this >> token.RevisedLifetime;
1196 }
1197 
1198 
1199 template<>
1200 void DataSerializer::Serialize<OpcUa::OpenSecureChannelResponse>(const OpcUa::OpenSecureChannelResponse & response)
1201 {
1202  *this << response.TypeId;
1203  *this << response.Header;
1204  *this << response.ServerProtocolVersion;
1205  *this << response.ChannelSecurityToken;
1206  SerializeContainer(*this, response.ServerNonce);
1207 }
1208 
1209 template<>
1210 void DataDeserializer::Deserialize<OpcUa::OpenSecureChannelResponse>(OpcUa::OpenSecureChannelResponse & response)
1211 {
1212  *this >> response.TypeId;
1213  *this >> response.Header;
1214  *this >> response.ServerProtocolVersion;
1215  *this >> response.ChannelSecurityToken;
1216  DeserializeContainer(*this, response.ServerNonce);
1217 }
1218 
1219 template<>
1220 void DataSerializer::Serialize<OpcUa::Binary::RawMessage>(const OpcUa::Binary::RawMessage & raw)
1221 {
1222  Buffer.insert(Buffer.end(), raw.Data, raw.Data + raw.Size);
1223 }
1224 
1225 template<>
1226 void DataDeserializer::Deserialize<OpcUa::Binary::RawBuffer>(OpcUa::Binary::RawBuffer & raw)
1227 {
1228  GetData(In, raw.Data, raw.Size);
1229 }
1230 
1231 template<>
1232 void DataSerializer::Serialize<OpcUa::CloseSecureChannelRequest>(const OpcUa::CloseSecureChannelRequest & request)
1233 {
1234  *this << request.TypeId;
1235  *this << request.Header;
1236 }
1237 
1238 template<>
1239 void DataDeserializer::Deserialize<OpcUa::CloseSecureChannelRequest>(OpcUa::CloseSecureChannelRequest & request)
1240 {
1241  *this >> request.TypeId;
1242  *this >> request.Header;
1243 }
1244 
1245 template<>
1246 void DataSerializer::Serialize<OpcUa::LocalizedText>(const OpcUa::LocalizedText & lt)
1247 {
1248  *this << lt.Encoding;
1249 
1250  if (lt.Encoding & HAS_LOCALE)
1251  {
1252  *this << lt.Locale;
1253  }
1254 
1255  if (lt.Encoding & HAS_TEXT)
1256  {
1257  *this << lt.Text;
1258  }
1259 }
1260 
1261 template<>
1262 void DataDeserializer::Deserialize<OpcUa::LocalizedText>(OpcUa::LocalizedText & lt)
1263 {
1264  *this >> lt.Encoding;
1265 
1266  if (lt.Encoding & HAS_LOCALE)
1267  {
1268  *this >> lt.Locale;
1269  }
1270 
1271  if (lt.Encoding & HAS_TEXT)
1272  {
1273  *this >> lt.Text;
1274  }
1275 }
1276 
1277 
1278 template<>
1279 void DataSerializer::Serialize<std::vector<LocalizedText>>(const std::vector<LocalizedText> & value)
1280 {
1281  SerializeContainer(*this, value);
1282 }
1283 
1284 template<>
1285 void DataDeserializer::Deserialize<std::vector<LocalizedText>>(std::vector<LocalizedText> & value)
1286 {
1287  DeserializeContainer(*this, value);
1288 }
1289 
1290 template<>
1291 void DataSerializer::Serialize<ExtensionObjectHeader>(const ExtensionObjectHeader & value)
1292 {
1293  *this << value.TypeId;
1294  *this << static_cast<uint8_t>(value.Encoding);
1295 }
1296 
1297 template<>
1298 void DataDeserializer::Deserialize<ExtensionObjectHeader>(ExtensionObjectHeader & value)
1299 {
1300  *this >> value.TypeId;
1301  uint8_t tmp = 0;
1302  *this >> tmp;
1303  value.Encoding = static_cast<ExtensionObjectEncoding>(tmp);
1304 }
1305 
1306 template<>
1307 void DataSerializer::Serialize<QualifiedName>(const QualifiedName & name)
1308 {
1309  *this << name.NamespaceIndex;
1310  *this << name.Name;
1311 }
1312 
1313 template<>
1314 void DataDeserializer::Deserialize<QualifiedName>(QualifiedName & name)
1315 {
1316  *this >> name.NamespaceIndex;
1317  *this >> name.Name;
1318 }
1319 
1321 // IntegerId
1323 
1324 template<>
1325 void DataSerializer::Serialize<IntegerId>(const IntegerId & id)
1326 {
1327  *this << static_cast<uint32_t>(id);
1328 }
1329 
1330 template<>
1331 void DataDeserializer::Deserialize<IntegerId>(IntegerId & id)
1332 {
1333  uint32_t value = 0;
1334  *this >> value;
1335  id = value;
1336 }
1337 
1338 template<>
1339 void DataSerializer::Serialize<std::vector<IntegerId>>(const std::vector<IntegerId> & targets)
1340 {
1341  SerializeContainer(*this, targets);
1342 }
1343 
1344 template<>
1345 void DataDeserializer::Deserialize<std::vector<IntegerId>>(std::vector<IntegerId> & targets)
1346 {
1347  DeserializeContainer(*this, targets);
1348 }
1349 
1351 // StatusCode
1353 
1354 template<>
1355 void DataSerializer::Serialize<StatusCode>(const StatusCode & status)
1356 {
1357  *this << static_cast<uint32_t>(status);
1358 }
1359 
1360 template<>
1361 void DataDeserializer::Deserialize<StatusCode>(StatusCode & status)
1362 {
1363  uint32_t value = 0;
1364  *this >> value;
1365  status = static_cast<StatusCode>(value);
1366 }
1367 
1368 template<>
1369 void DataSerializer::Serialize<std::vector<StatusCode>>(const std::vector<StatusCode> & value)
1370 {
1371  SerializeContainer(*this, value);
1372 }
1373 
1374 template<>
1375 void DataDeserializer::Deserialize<std::vector<StatusCode>>(std::vector<StatusCode> & value)
1376 {
1377  DeserializeContainer(*this, value);
1378 }
1379 
1380 template<>
1381 void DataSerializer::Serialize<std::vector<QualifiedName>>(const std::vector<QualifiedName> & value)
1382 {
1383  SerializeContainer(*this, value);
1384 }
1385 
1386 template<>
1387 void DataDeserializer::Deserialize<std::vector<QualifiedName>>(std::vector<QualifiedName> & value)
1388 {
1389  DeserializeContainer(*this, value);
1390 }
1391 
1392 
1393 } // namespace Binary
1394 } // namespace OpcUa
1395 
const uint8_t HAS_LOCALE
Definition: types.h:129
const char Integer[]
Definition: strings.h:93
ExtensionObjectEncoding Encoding
Definition: types.h:287
ExtensionObjectEncoding
Definition: types.h:276
void SerializeContainer(Stream &out, const Container &c, uint32_t emptySizeValue=~uint32_t())
DiagnosticInfoMask
Definition: types.h:200
const uint8_t HAS_TEXT
Definition: types.h:130
void DeserializeContainer(Stream &in, Container &c)
uint32_t Value
Definition: types.h:68
std_msgs::Header * header(M &m)
SecurityTokenRequestType
Definition: enums.h:92
name
Definition: setup.py:38
message
Definition: server.py:50
ExpandedNodeId TypeId
Definition: types.h:286
MessageSecurityMode
Definition: enums.h:68
struct OpcUa::NodeId::FourByteDataType FourByteData
OPC UA Address space part. GNU LGPL.
const char * Binary(const char *input, short n)
std::vector< DiagnosticInfo > DiagnosticInfoList
Definition: types.h:253
IntegerId & operator=(const IntegerId &id)
NodeIdEncoding Encoding
Definition: nodeid.h:46


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