binary_stream.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #include "binary_serialization.h"
00012 
00013 #include <opc/ua/protocol/endpoints.h>
00014 #include <opc/ua/protocol/secure_channel.h>
00015 #include <opc/ua/protocol/binary/stream.h>
00016 
00017 #include <algorithm>
00018 #include <iostream>
00019 #include <stdexcept>
00020 
00021 #ifdef _WIN32
00022 #include <WinSock2.h>
00023 #else
00024 #include <netinet/in.h>
00025 #endif
00026 
00027 namespace
00028 {
00029 
00030   float float_htonl(float value){
00031     std::cout << "sizez: " << sizeof(float) << "  " << sizeof(uint32_t) << std::endl;
00032     union v {
00033         float       f;
00034         uint32_t    i;
00035     };
00036 
00037     union v val;
00038 
00039     val.f = value;
00040     val.i = htonl(val.i);
00041 
00042     return val.f;
00043   }
00044 
00045   float float_ntohl(float value){
00046     union v {
00047         float       f;
00048         uint32_t    i;
00049     };
00050 
00051     union v val;
00052 
00053     val.f = value;
00054     val.i = ntohl(val.i);
00055 
00056     return val.f;
00057   }
00058 
00059 
00060   template <typename Integer16>
00061   inline int8_t LoByte(Integer16 value)
00062   {
00063     return value & 0xFF;
00064   }
00065 
00066   template <typename Integer16>
00067   inline int8_t HiByte(Integer16 value)
00068   {
00069     return (value & 0xFF00) >> 8;
00070   }
00071 
00072   template <typename Integer32>
00073   inline uint16_t LoWord(Integer32 value)
00074   {
00075     return value & 0xFFFF;
00076   }
00077 
00078   template <typename Integer32>
00079   inline uint16_t HiWord(Integer32 value)
00080   {
00081     return (value & 0xFFFF0000) >> 16;
00082   }
00083 
00084   template <typename Integer64>
00085   inline uint32_t LoDWord(Integer64 value)
00086   {
00087     return value & 0xFFFFFFFF;
00088   }
00089 
00090   template <typename Integer64>
00091   inline uint32_t HiDWord(Integer64 value)
00092   {
00093     return (value & 0xFFFFFFFF00000000) >> 32;
00094   }
00095 
00096   template <typename Integer16>
00097   inline Integer16  MakeWord(int8_t loByte, int8_t hiByte)
00098   {
00099     const Integer16 word = hiByte;
00100     return (word << 8) | (0x00FF & loByte);
00101   }
00102 
00103   template<typename Integer>
00104   inline Integer MakeNumber(char* data)
00105   {
00106     unsigned size = sizeof(Integer);
00107     Integer i = 0;
00108     while(size)
00109     {
00110       i = (i << 8) | (0x00FF & data[--size]);
00111     }
00112     return i;
00113   }
00114 
00115 
00116   const size_t MESSAGE_TYPE_SIZE = 3;
00117   const char MESSAGE_TYPE_HELLO[MESSAGE_TYPE_SIZE]       = {'H', 'E', 'L'};
00118   const char MESSAGE_TYPE_ACKNOWLEDGE[MESSAGE_TYPE_SIZE] = {'A', 'C', 'K'};
00119   const char MESSAGE_TYPE_ERROR[MESSAGE_TYPE_SIZE]       = {'E', 'R', 'R'};
00120   const char MESSAGE_TYPE_MESSAGE[MESSAGE_TYPE_SIZE]     = {'M', 'S', 'G'};
00121   const char MESSAGE_TYPE_OPEN[MESSAGE_TYPE_SIZE]        = {'O', 'P', 'N'};
00122   const char MESSAGE_TYPE_CLOSE[MESSAGE_TYPE_SIZE]       = {'C', 'L', 'O'};
00123 
00124   inline void ThrowReceivedNotEnoughData()
00125   {
00126     throw std::logic_error("Not enough data was received from channel.");
00127   }
00128 
00129 
00130   template <typename ChannelType>
00131   inline void GetData(ChannelType& channel, char* data, std::size_t size)
00132   {
00133     size_t recv = channel.Read(data, size);
00134     if (recv != size)
00135     {
00136       std::cout << "expecting " << size << "  received: " << recv << std::endl;
00137       ThrowReceivedNotEnoughData();
00138     }
00139   }
00140 
00141 
00142 } // namespace
00143 
00144 
00145 namespace OpcUa
00146 {
00147   ExtensionObjectHeader::ExtensionObjectHeader()
00148     : Encoding(ExtensionObjectEncoding::NONE)
00149   {
00150   }
00151 
00153   ExtensionObjectHeader::ExtensionObjectHeader(ExtensionObjectId objectId, ExtensionObjectEncoding encoding)
00154     : Encoding(encoding)
00155   {
00156     TypeId.Encoding = EV_FOUR_BYTE;
00157     TypeId.FourByteData.Identifier = objectId;
00158   }
00160   // IntegerId
00162 
00163   IntegerId::IntegerId()
00164     : Value(1)
00165   {
00166   }
00167 
00168   IntegerId::IntegerId(const IntegerId& id)
00169     : Value(id.Value)
00170   {
00171   }
00172 
00173   IntegerId::IntegerId(uint32_t num)
00174     : Value(num)
00175   {
00176     if (!Value)
00177     {
00178       throw std::invalid_argument("IntegerId cannot be zero");
00179     }
00180   }
00181 
00182   IntegerId& IntegerId::operator= (const IntegerId& id)
00183   {
00184     Value = id.Value;
00185     return *this;
00186   }
00187 
00188   IntegerId& IntegerId::operator= (uint32_t value)
00189   {
00190     if (!Value)
00191     {
00192       throw std::invalid_argument("IntegerId cannot be zero");
00193     }
00194 
00195     Value = value;
00196     return *this;
00197   }
00198 
00199   IntegerId::operator uint32_t() const
00200   {
00201     return Value;
00202   }
00203 
00205 
00206   namespace Binary
00207   {
00208 
00209     template<>
00210     void DataSerializer::Serialize<int8_t>(const int8_t& value)
00211     {
00212       Buffer.push_back(value);
00213     }
00214 
00215     template<>
00216     void DataSerializer::Serialize<std::vector<int8_t>>(const std::vector<int8_t>& value)
00217     {
00218       SerializeContainer(*this, value);
00219     }
00220 
00221     template<>
00222     void DataSerializer::Serialize<uint8_t>(const uint8_t& value)
00223     {
00224       Buffer.push_back(value);
00225     }
00226 
00227     template<>
00228     void DataDeserializer::Deserialize<uint8_t>(uint8_t& value)
00229     {
00230       char data = 0;
00231       GetData(In, &data, 1);
00232       value = static_cast<uint8_t>(data);
00233     }
00234 
00235     template<>
00236     void DataDeserializer::Deserialize<int8_t>(int8_t& value)
00237     {
00238       char data = 0;
00239       GetData(In, &data, 1);
00240       value = data;
00241     }
00242 
00243     template<>
00244     void DataDeserializer::Deserialize<std::vector<int8_t>>(std::vector<int8_t>& value)
00245     {
00246       DeserializeContainer(*this, value);
00247     }
00248 
00249 
00250     template<>
00251     void DataSerializer::Serialize<int16_t>(const int16_t& value)
00252     {
00253       Buffer.push_back(LoByte(value));
00254       Buffer.push_back(HiByte(value));
00255     }
00256 
00257     template<>
00258     void DataSerializer::Serialize<uint16_t>(const uint16_t& value)
00259     {
00260       Buffer.push_back(LoByte(value));
00261       Buffer.push_back(HiByte(value));
00262     }
00263 
00264     template<>
00265     void DataDeserializer::Deserialize<uint16_t>(uint16_t& value)
00266     {
00267       char data[2] = {0};
00268       GetData(In, data, 2);
00269       value = MakeWord<uint16_t>(data[0], data[1]);
00270     }
00271 
00272     template<>
00273     void DataDeserializer::Deserialize<int16_t>(int16_t& value)
00274     {
00275       char data[2] = {0};
00276       GetData(In, data, 2);
00277       value = MakeWord<int16_t>(data[0], data[1]);
00278     }
00279 
00280     template<>
00281     void DataSerializer::Serialize<int32_t>(const int32_t& value)
00282     {
00283       Serialize(LoWord(value));
00284       Serialize(HiWord(value));
00285     }
00286 
00287     template<>
00288     void DataSerializer::Serialize<uint32_t>(const uint32_t& value)
00289     {
00290       Serialize(LoWord(value));
00291       Serialize(HiWord(value));
00292     }
00293 
00294     template<>
00295     void DataDeserializer::Deserialize<uint32_t>(uint32_t& value)
00296     {
00297       char data[4] = {0};
00298       GetData(In, data, 4);
00299       value = MakeNumber<uint32_t>(data);
00300     }
00301 
00302     template<>
00303     void DataDeserializer::Deserialize<int32_t>(int32_t& value)
00304     {
00305       char data[4] = {0};
00306       GetData(In, data, 4);
00307       value = MakeNumber<int32_t>(data);
00308     }
00309 
00310     template<>
00311     void DataSerializer::Serialize<int64_t>(const int64_t& value)
00312     {
00313       Serialize(LoDWord(value));
00314       Serialize(HiDWord(value));
00315     }
00316 
00317     template<>
00318     void DataSerializer::Serialize<uint64_t>(const uint64_t& value)
00319     {
00320       Serialize(LoDWord(value));
00321       Serialize(HiDWord(value));
00322     }
00323 
00324     template<>
00325     void DataDeserializer::Deserialize<uint64_t>(uint64_t& value)
00326     {
00327       char data[8] = {0};
00328       GetData(In, data, 8);
00329       value = MakeNumber<uint64_t>(data);
00330     }
00331 
00332     template<>
00333     void DataDeserializer::Deserialize<int64_t>(int64_t& value)
00334     {
00335       char data[8] = {0};
00336       GetData(In, data, 8);
00337       value = MakeNumber<int64_t>(data);
00338     }
00339 
00340     template<>
00341     void DataSerializer::Serialize<bool>(const bool& value)
00342     {
00343       Serialize(static_cast<uint8_t>(value));
00344     }
00345 
00346     template<>
00347     void DataSerializer::Serialize<std::vector<bool>>(const std::vector<bool>& value)
00348     {
00349       SerializeContainer(*this, value);
00350     }
00351 
00352     template<>
00353     void DataDeserializer::Deserialize<bool>(bool& value)
00354     {
00355       uint8_t tmp = 0;
00356       *this >> tmp;
00357       value = (tmp != 0);
00358     }
00359 
00360     template<>
00361     void DataDeserializer::Deserialize<std::vector<bool>>(std::vector<bool>& value)
00362     {
00363       DeserializeContainer(*this, value);
00364     }
00365 
00366 
00367     template<>
00368     void DataSerializer::Serialize<float>(const float& value)
00369     {
00370       //float network_value = float_htonl(value);
00371       const uint8_t* data = reinterpret_cast<const uint8_t*>(&value);
00372       for (int i = 0; i < 4; ++i)
00373       {
00374         Serialize(data[i]);
00375       }
00376     }
00377 
00378     template<>
00379     void DataDeserializer::Deserialize<float>(float& value)
00380     {
00381       //float network_value = float_htonl(value);
00382       uint8_t data[4] = {0};
00383       for (int i = 0; i < 4; ++i)
00384       {
00385         *this >> data[i];
00386       }
00387       value = *reinterpret_cast<const float*>(data); // FIXME: probably broken on ARM
00388      }
00389 
00390     template<>
00391     void DataSerializer::Serialize<double>(const double& value)
00392     {
00393       const uint8_t* data = reinterpret_cast<const uint8_t*>(&value);
00394       for (int i = 0; i < 8; ++i)
00395       {
00396         Serialize(data[i]);
00397       }
00398     }
00399 
00400     template<>
00401     void DataDeserializer::Deserialize<double>(double& value)
00402     {
00403       uint8_t data[8] = {0};
00404       for (int i = 0; i < 8; ++i)
00405       {
00406         *this >> data[i];
00407       }
00408       value = *reinterpret_cast<const double*>(data); //FIXME: probably broken on ARM
00409     }
00410 
00411     template<>
00412     void DataSerializer::Serialize<OpcUa::Guid>(const OpcUa::Guid& value)
00413     {
00414       *this << value.Data1 << value.Data2 << value.Data3;
00415       Buffer.insert(Buffer.end(), value.Data4, value.Data4 + 8);
00416     }
00417 
00418     template<>
00419     void DataDeserializer::Deserialize<OpcUa::Guid>(OpcUa::Guid& value)
00420     {
00421       *this >> value.Data1 >> value.Data2 >> value.Data3;
00422       char data[8] = {0};
00423       GetData(In, data, 8);
00424       std::copy(data, data + 8, value.Data4);
00425     }
00426 
00427     template<>
00428     void DataSerializer::Serialize<std::string>(const std::string& value)
00429     {
00430       if (value.empty())
00431       {
00432         Serialize(~uint32_t());
00433         return;
00434       }
00435       Serialize(static_cast<uint32_t>(value.size()));
00436       Buffer.insert(Buffer.end(), value.begin(), value.end());
00437     }
00438 
00439     template<>
00440     void DataDeserializer::Deserialize<std::string>(std::string& value)
00441     {
00442       uint32_t stringSize = 0;
00443       *this >> stringSize;
00444       if (stringSize != ~uint32_t())
00445       {
00446         value.resize(stringSize);
00447         GetData(In, &value[0], stringSize);
00448         return;
00449       }
00450 
00451       value.clear();
00452       return;
00453      // TODO standard says that 0xff*4 - is the zero byte string , actually it seems that it is an empty string
00454 /*
00455       while(true)
00456       {
00457         uint8_t val = 0;
00458         *this >> val;
00459         if (val == 0)
00460         {
00461           return;
00462         }
00463         value.push_back(val);
00464       }
00465 */
00466     }
00467 
00468 
00469     template<>
00470     void DataSerializer::Serialize<OpcUa::DateTime>(const OpcUa::DateTime& date)
00471     {
00472       *this << date.Value;
00473     }
00474 
00475     template<>
00476     void DataDeserializer::Deserialize<OpcUa::DateTime>(OpcUa::DateTime& date)
00477     {
00478       *this >> date.Value;
00479     }
00480 
00481     template<>
00482     void DataSerializer::Serialize<std::vector<OpcUa::DateTime>>(const std::vector<OpcUa::DateTime>& date)
00483     {
00484       SerializeContainer(*this, date);
00485     }
00486 
00487     template<>
00488     void DataDeserializer::Deserialize<std::vector<OpcUa::DateTime>>(std::vector<OpcUa::DateTime>& date)
00489     {
00490       DeserializeContainer(*this, date);
00491     }
00492 
00493     template<>
00494     void DataSerializer::Serialize<ByteString>(const ByteString& value)
00495     {
00496       if (value.Data.empty())
00497       {
00498         Serialize(~uint32_t());
00499         return;
00500       }
00501       Serialize(static_cast<uint32_t>(value.Data.size()));
00502       Buffer.insert(Buffer.end(), value.Data.begin(), value.Data.end());
00503     }
00504 
00505     template<>
00506     void DataDeserializer::Deserialize<ByteString>(ByteString& value)
00507     {
00508       uint32_t stringSize = 0;
00509       *this >> stringSize;
00510       if (stringSize != ~uint32_t())
00511       {
00512         value.Data.resize(stringSize);
00513         GetData(In, reinterpret_cast<char*>(&value.Data[0]), stringSize);
00514         return;
00515       }
00516 
00517       value.Data.clear();
00518       return;
00519     }
00520 
00521 
00522     template<>
00523     void DataSerializer::Serialize<std::vector<ByteString>>(const std::vector<ByteString>& value)
00524     {
00525       SerializeContainer(*this, value);
00526     }
00527 
00528     template<>
00529     void DataDeserializer::Deserialize<std::vector<ByteString>>(std::vector<ByteString>& value)
00530     {
00531       DeserializeContainer(*this, value);
00532     }
00533 
00534 
00535     template<>
00536     void DataSerializer::Serialize<std::vector<uint8_t>>(const std::vector<uint8_t>& value)
00537     {
00538       SerializeContainer(*this, value);
00539     }
00540 
00541     template<>
00542     void DataDeserializer::Deserialize<std::vector<uint8_t>>(std::vector<uint8_t>& value)
00543     {
00544       DeserializeContainer(*this, value);
00545     }
00546 
00547     template<>
00548     void DataSerializer::Serialize<std::vector<uint16_t>>(const std::vector<uint16_t>& value)
00549     {
00550       SerializeContainer(*this, value);
00551     }
00552 
00553     template<>
00554     void DataDeserializer::Deserialize<std::vector<uint16_t>>(std::vector<uint16_t>& value)
00555     {
00556       DeserializeContainer(*this, value);
00557     }
00558 
00559     template<>
00560     void DataSerializer::Serialize<std::vector<int16_t>>(const std::vector<int16_t>& value)
00561     {
00562       SerializeContainer(*this, value);
00563     }
00564 
00565     template<>
00566     void DataDeserializer::Deserialize<std::vector<int16_t>>(std::vector<int16_t>& value)
00567     {
00568       DeserializeContainer(*this, value);
00569     }
00570 
00571 
00572     template<>
00573     void DataSerializer::Serialize<std::vector<uint32_t>>(const std::vector<uint32_t>& value)
00574     {
00575       SerializeContainer(*this, value);
00576     }
00577 
00578     template<>
00579     void DataDeserializer::Deserialize<std::vector<uint32_t>>(std::vector<uint32_t>& value)
00580     {
00581       DeserializeContainer(*this, value);
00582     }
00583 
00584     template<>
00585     void DataSerializer::Serialize<std::vector<int32_t>>(const std::vector<int32_t>& value)
00586     {
00587       SerializeContainer(*this, value);
00588     }
00589 
00590     template<>
00591     void DataDeserializer::Deserialize<std::vector<int32_t>>(std::vector<int32_t>& value)
00592     {
00593       DeserializeContainer(*this, value);
00594     }
00595 
00596     template<>
00597     void DataSerializer::Serialize<std::vector<int64_t>>(const std::vector<int64_t>& value)
00598     {
00599       SerializeContainer(*this, value);
00600     }
00601 
00602     template<>
00603     void DataDeserializer::Deserialize<std::vector<int64_t>>(std::vector<int64_t>& value)
00604     {
00605       DeserializeContainer(*this, value);
00606     }
00607 
00608     template<>
00609     void DataSerializer::Serialize<std::vector<uint64_t>>(const std::vector<uint64_t>& value)
00610     {
00611       SerializeContainer(*this, value);
00612     }
00613 
00614     template<>
00615     void DataDeserializer::Deserialize<std::vector<uint64_t>>(std::vector<uint64_t>& value)
00616     {
00617       DeserializeContainer(*this, value);
00618     }
00619 
00620 
00621     template<>
00622     void DataSerializer::Serialize<std::vector<float>>(const std::vector<float>& value)
00623     {
00624       SerializeContainer(*this, value);
00625     }
00626 
00627     template<>
00628     void DataDeserializer::Deserialize<std::vector<double>>(std::vector<double>& value)
00629     {
00630       DeserializeContainer(*this, value);
00631     }
00632 
00633     template<>
00634     void DataSerializer::Serialize<std::vector<double>>(const std::vector<double>& value)
00635     {
00636       SerializeContainer(*this, value);
00637     }
00638 
00639     template<>
00640     void DataDeserializer::Deserialize<std::vector<float>>(std::vector<float>& value)
00641     {
00642       DeserializeContainer(*this, value);
00643     }
00644 
00645 
00646     template<>
00647     void DataSerializer::Serialize<std::vector<Guid>>(const std::vector<Guid>& value)
00648     {
00649       SerializeContainer(*this, value);
00650     }
00651 
00652     template<>
00653     void DataDeserializer::Deserialize<std::vector<Guid>>(std::vector<Guid>& value)
00654     {
00655       DeserializeContainer(*this, value);
00656     }
00657 
00658     template<>
00659     void DataSerializer::Serialize<std::vector<NodeId>>(const std::vector<NodeId>& value)
00660     {
00661       SerializeContainer(*this, value);
00662     }
00663 
00664     template<>
00665     void DataDeserializer::Deserialize<std::vector<NodeId>>(std::vector<NodeId>& value)
00666     {
00667       DeserializeContainer(*this, value);
00668     }
00669 
00670     template<>
00671     void DataSerializer::Serialize<std::vector<std::string>>(const std::vector<std::string>& value)
00672     {
00673       SerializeContainer(*this, value);
00674     }
00675 
00676     template<>
00677     void DataDeserializer::Deserialize<std::vector<std::string>>(std::vector<std::string>& value)
00678     {
00679       DeserializeContainer(*this, value);
00680     }
00681 
00682     template<>
00683     void DataSerializer::Serialize<std::vector<std::vector<uint8_t>>>(const std::vector<std::vector<uint8_t>>& value)
00684     {
00685       SerializeContainer(*this, value);
00686     }
00687 
00688     template<>
00689     void DataDeserializer::Deserialize<std::vector<std::vector<uint8_t>>>(std::vector<std::vector<uint8_t>>& value)
00690     {
00691       DeserializeContainer(*this, value);
00692     }
00693 
00694     template<>
00695     void DataSerializer::Serialize<OpcUa::Binary::MessageType>(const OpcUa::Binary::MessageType& value)
00696     {
00697       const char* typeName = nullptr;
00698       switch(value)
00699       {
00700         case MT_HELLO:
00701           typeName = MESSAGE_TYPE_HELLO;
00702           break;
00703         case MT_ACKNOWLEDGE:
00704           typeName = MESSAGE_TYPE_ACKNOWLEDGE;
00705           break;
00706         case MT_ERROR:
00707           typeName = MESSAGE_TYPE_ERROR;
00708           break;
00709         case MT_SECURE_OPEN:
00710           typeName = MESSAGE_TYPE_OPEN;
00711           break;
00712         case MT_SECURE_CLOSE:
00713           typeName = MESSAGE_TYPE_CLOSE;
00714           break;
00715         case MT_SECURE_MESSAGE:
00716           typeName = MESSAGE_TYPE_MESSAGE;
00717           break;
00718         default:
00719           throw std::logic_error("Invalid message type.");
00720       }
00721       Buffer.insert(Buffer.end(), typeName, typeName + MESSAGE_TYPE_SIZE);
00722     }
00723 
00724     template<>
00725     void DataDeserializer::Deserialize<OpcUa::Binary::MessageType>(OpcUa::Binary::MessageType& value)
00726     {
00727       char data[MESSAGE_TYPE_SIZE] = {0};
00728       GetData(In, data, MESSAGE_TYPE_SIZE);
00729       if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_HELLO))
00730       {
00731         value = OpcUa::Binary::MT_HELLO;
00732       }
00733       else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_ACKNOWLEDGE))
00734       {
00735         value = OpcUa::Binary::MT_ACKNOWLEDGE;
00736       }
00737       else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_ERROR))
00738       {
00739         value = OpcUa::Binary::MT_ERROR;
00740       }
00741       else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_MESSAGE))
00742       {
00743         value = OpcUa::Binary::MT_SECURE_MESSAGE;
00744       }
00745       else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_OPEN))
00746       {
00747         value = OpcUa::Binary::MT_SECURE_OPEN;
00748       }
00749       else if (std::equal(data, data + MESSAGE_TYPE_SIZE, MESSAGE_TYPE_CLOSE))
00750       {
00751         value = OpcUa::Binary::MT_SECURE_CLOSE;
00752       }
00753       else
00754       {
00755         std::string msg("Cannot deserialize Unknown message type [");
00756         msg += std::to_string(data[0]) + ", " + std::to_string(data[1]) + ", " + std::to_string(data[2]);
00757         msg += "] received.";
00758         throw std::logic_error(msg);
00759       }
00760     }
00761 
00762     template<>
00763     void DataSerializer::Serialize<OpcUa::Binary::ChunkType>(const OpcUa::Binary::ChunkType& value)
00764     {
00765       switch (value)
00766       {
00767         case CHT_SINGLE:
00768           Buffer.push_back('F');
00769           break;
00770         case CHT_INTERMEDIATE:
00771           Buffer.push_back('C');
00772           break;
00773         case CHT_FINAL:
00774           Buffer.push_back('A');
00775           break;
00776         default:
00777           throw std::logic_error("Invalid Chunk Type");
00778       }
00779     }
00780 
00781     template<>
00782     void DataDeserializer::Deserialize<OpcUa::Binary::ChunkType>(OpcUa::Binary::ChunkType& value)
00783     {
00784       char data = 0;
00785       GetData(In, &data, 1);
00786       switch (data)
00787       {
00788         case 'F':
00789           value = CHT_SINGLE;
00790           break;
00791 
00792         case 'C':
00793           value = CHT_INTERMEDIATE;
00794           break;
00795 
00796         case 'A':
00797           value = CHT_FINAL;
00798           break;
00799 
00800         default:
00801           throw std::logic_error("Invalid chunk type received.");
00802       };
00803     }
00804 
00805     template<>
00806     void DataSerializer::Serialize<OpcUa::Binary::Header>(const OpcUa::Binary::Header& header)
00807     {
00808       *this << header.Type;
00809       *this << header.Chunk;
00810       *this << header.Size;
00811     }
00812 
00813     template<>
00814     void DataDeserializer::Deserialize<OpcUa::Binary::Header>(OpcUa::Binary::Header& header)
00815     {
00816       *this >> header.Type;
00817       *this >> header.Chunk;
00818       *this >> header.Size;
00819     }
00820 
00821     template<>
00822     void DataSerializer::Serialize<OpcUa::Binary::Hello>(const OpcUa::Binary::Hello& message)
00823     {
00824       *this << message.ProtocolVersion;
00825       *this << message.ReceiveBufferSize;
00826       *this << message.SendBufferSize;
00827       *this << message.MaxMessageSize;
00828       *this << message.MaxChunkCount;
00829       *this << message.EndpointUrl;
00830     }
00831 
00832     template<>
00833     void DataDeserializer::Deserialize<OpcUa::Binary::Hello>(OpcUa::Binary::Hello& message)
00834     {
00835       *this >> message.ProtocolVersion;
00836       *this >> message.ReceiveBufferSize;
00837       *this >> message.SendBufferSize;
00838       *this >> message.MaxMessageSize;
00839       *this >> message.MaxChunkCount;
00840       *this >> message.EndpointUrl;
00841     }
00842 
00843     template<>
00844     void DataSerializer::Serialize<OpcUa::Binary::Acknowledge>(const OpcUa::Binary::Acknowledge& message)
00845     {
00846       *this << message.ProtocolVersion;
00847       *this << message.ReceiveBufferSize;
00848       *this << message.SendBufferSize;
00849       *this << message.MaxMessageSize;
00850       *this << message.MaxChunkCount;
00851     }
00852 
00853     template<>
00854     void DataDeserializer::Deserialize<OpcUa::Binary::Acknowledge>(OpcUa::Binary::Acknowledge& message)
00855     {
00856       *this >> message.ProtocolVersion;
00857       *this >> message.ReceiveBufferSize;
00858       *this >> message.SendBufferSize;
00859       *this >> message.MaxMessageSize;
00860       *this >> message.MaxChunkCount;
00861     }
00862 
00863     template<>
00864     void DataSerializer::Serialize<OpcUa::Binary::Error>(const OpcUa::Binary::Error& message)
00865     {
00866       *this << message.Code;
00867       *this << message.Reason;
00868     }
00869 
00870     template<>
00871     void DataDeserializer::Deserialize<OpcUa::Binary::Error>(OpcUa::Binary::Error& message)
00872     {
00873       *this >> message.Code;
00874       *this >> message.Reason;
00875     }
00876 
00877     template<>
00878     void DataSerializer::Serialize<OpcUa::Binary::SecureHeader>(const OpcUa::Binary::SecureHeader& header)
00879     {
00880       *this << header.Type;
00881       *this << header.Chunk;
00882       *this << header.Size;
00883       *this << header.ChannelId;
00884     }
00885 
00886     template<>
00887     void DataDeserializer::Deserialize<OpcUa::Binary::SecureHeader>(OpcUa::Binary::SecureHeader& header)
00888     {
00889       *this >> header.Type;
00890       *this >> header.Chunk;
00891       *this >> header.Size;
00892       *this >> header.ChannelId;
00893     }
00894 
00895     template<>
00896     void DataSerializer::Serialize<OpcUa::Binary::AsymmetricAlgorithmHeader>(const OpcUa::Binary::AsymmetricAlgorithmHeader& header)
00897     {
00898       *this << header.SecurityPolicyUri;
00899       *this << header.SenderCertificate;
00900       *this << header.ReceiverCertificateThumbPrint;
00901     }
00902 
00903     template<>
00904     void DataDeserializer::Deserialize<OpcUa::Binary::AsymmetricAlgorithmHeader>(OpcUa::Binary::AsymmetricAlgorithmHeader& header)
00905     {
00906       *this >> header.SecurityPolicyUri;
00907       *this >> header.SenderCertificate;
00908       *this >> header.ReceiverCertificateThumbPrint;
00909     };
00910 
00911     template<>
00912     void DataSerializer::Serialize<OpcUa::Binary::SymmetricAlgorithmHeader>(const OpcUa::Binary::SymmetricAlgorithmHeader& header)
00913     {
00914       *this << header.TokenId;
00915     }
00916 
00917     template<>
00918     void DataDeserializer::Deserialize<OpcUa::Binary::SymmetricAlgorithmHeader>(OpcUa::Binary::SymmetricAlgorithmHeader& header)
00919     {
00920       *this >> header.TokenId;
00921     };
00922 
00923 
00924     template<>
00925     void DataSerializer::Serialize<OpcUa::Binary::SequenceHeader>(const OpcUa::Binary::SequenceHeader& header)
00926     {
00927       *this << header.SequenceNumber;
00928       *this << header.RequestId;
00929     }
00930 
00931     template<>
00932     void DataDeserializer::Deserialize<OpcUa::Binary::SequenceHeader>(OpcUa::Binary::SequenceHeader& header)
00933     {
00934       *this >> header.SequenceNumber;
00935       *this >> header.RequestId;
00936     };
00937 
00938 
00939     template<>
00940     void DataSerializer::Serialize<OpcUa::AdditionalHeader>(const OpcUa::AdditionalHeader& header)
00941     {
00942       *this << header.TypeId;
00943       *this << header.Encoding;
00944     }
00945 
00946     template<>
00947     void DataDeserializer::Deserialize<OpcUa::AdditionalHeader>(OpcUa::AdditionalHeader& header)
00948     {
00949       *this >> header.TypeId;
00950       *this >> header.Encoding;
00951     };
00952 
00953 
00954     template<>
00955     void DataSerializer::Serialize<OpcUa::RequestHeader>(const OpcUa::RequestHeader& header)
00956     {
00957       *this << header.SessionAuthenticationToken;
00958       *this << header.UtcTime;
00959       *this << header.RequestHandle;
00960       *this << header.ReturnDiagnostics;
00961       *this << header.AuditEntryId;
00962       *this << header.Timeout; // in miliseconds
00963       *this << header.Additional;
00964     }
00965 
00966     template<>
00967     void DataDeserializer::Deserialize<OpcUa::RequestHeader>(OpcUa::RequestHeader& header)
00968     {
00969       *this >> header.SessionAuthenticationToken;
00970       *this >> header.UtcTime;
00971       *this >> header.RequestHandle;
00972       *this >> header.ReturnDiagnostics;
00973       *this >> header.AuditEntryId;
00974       *this >> header.Timeout; // in miliseconds
00975       *this >> header.Additional;
00976     };
00977 
00978     template<>
00979     void DataSerializer::Serialize<DiagnosticInfoMask>(const DiagnosticInfoMask& value)
00980     {
00981       *this << static_cast<uint8_t>(value);
00982     }
00983 
00984     template<>
00985     void DataDeserializer::Deserialize<DiagnosticInfoMask>(DiagnosticInfoMask& value)
00986     {
00987       uint8_t tmp = 0;
00988       *this >> tmp;
00989       value = static_cast<DiagnosticInfoMask>(tmp);
00990     }
00991 
00992     template<>
00993     void DataSerializer::Serialize<OpcUa::DiagnosticInfo>(const OpcUa::DiagnosticInfo& info)
00994     {
00995       *this << info.EncodingMask;
00996 
00997       if (info.EncodingMask & DIM_SYMBOLIC_Id)
00998       {
00999         *this << info.SymbolicId;
01000       }
01001       if (info.EncodingMask & DIM_NAMESPACE)
01002       {
01003         *this << info.NamespaceURI;
01004       }
01005       if (info.EncodingMask & DIM_LOCALIZED_TEXT)
01006       {
01007         *this << info.LocalizedText;
01008       }
01009       if (info.EncodingMask & DIM_LOCALE)
01010       {
01011         *this << info.Locale;
01012       }
01013       if (info.EncodingMask & DIM_ADDITIONAL_INFO)
01014       {
01015         *this << info.AdditionalInfo;
01016       }
01017       if (info.EncodingMask & DIM_INNER_STATUS_CODE)
01018       {
01019         *this << info.InnerStatusCode;
01020       }
01021       if ((info.EncodingMask & DIM_INNER_DIAGNOSTIC_INFO) && info.InnerDiagnostics)
01022       {
01023         *this << *info.InnerDiagnostics;
01024       }
01025     }
01026 
01027 
01028     template<>
01029     void DataDeserializer::Deserialize<OpcUa::DiagnosticInfo>(OpcUa::DiagnosticInfo& info)
01030     {
01031       *this >> info.EncodingMask;
01032 
01033       if (info.EncodingMask & DIM_SYMBOLIC_Id)
01034       {
01035         *this >> info.SymbolicId;
01036       }
01037       if (info.EncodingMask & DIM_NAMESPACE)
01038       {
01039         *this >> info.NamespaceURI;
01040       }
01041       if (info.EncodingMask & DIM_LOCALIZED_TEXT)
01042       {
01043         *this >> info.LocalizedText;
01044       }
01045       if (info.EncodingMask & DIM_LOCALE)
01046       {
01047         *this >> info.Locale;
01048       }
01049       if (info.EncodingMask & DIM_ADDITIONAL_INFO)
01050       {
01051         *this >> info.AdditionalInfo;
01052       }
01053       if (info.EncodingMask & DIM_INNER_STATUS_CODE)
01054       {
01055         *this >> info.InnerStatusCode;
01056       }
01057       if (info.EncodingMask & DIM_INNER_DIAGNOSTIC_INFO)
01058       {
01059         std::shared_ptr<DiagnosticInfo> tmp(new DiagnosticInfo);
01060         *this >> *tmp;
01061         info.InnerDiagnostics = tmp;
01062       };
01063     };
01064 
01065     template<>
01066     void DataSerializer::Serialize<OpcUa::DiagnosticInfoList>(const OpcUa::DiagnosticInfoList& infos)
01067     {
01068       SerializeContainer(*this, infos, 0);
01069     }
01070 
01071     template<>
01072     void DataDeserializer::Deserialize<OpcUa::DiagnosticInfoList>(OpcUa::DiagnosticInfoList& infos)
01073     {
01074       DeserializeContainer(*this, infos);
01075     }
01076 
01077     template<>
01078     void DataSerializer::Serialize<OpcUa::ResponseHeader>(const OpcUa::ResponseHeader& header)
01079     {
01080       *this << header.Timestamp;
01081       *this << header.RequestHandle;
01082       *this << header.ServiceResult;
01083       *this << header.InnerDiagnostics;
01084       SerializeContainer(*this, header.StringTable);
01085       *this << header.Additional;
01086     }
01087 
01088     template<>
01089     void DataDeserializer::Deserialize<OpcUa::ResponseHeader>(OpcUa::ResponseHeader& header)
01090     {
01091       *this >> header.Timestamp;
01092       *this >> header.RequestHandle;
01093       *this >> header.ServiceResult;
01094       *this >> header.InnerDiagnostics;
01095       DeserializeContainer(*this, header.StringTable);
01096       *this >> header.Additional;
01097     };
01098 
01099 
01100     template<>
01101     void DataSerializer::Serialize<OpcUa::OpenSecureChannelRequest>(const OpcUa::OpenSecureChannelRequest& request)
01102     {
01103       *this << request.TypeId;
01104       *this << request.Header;
01105       *this << request.Parameters.ClientProtocolVersion;
01106       *this << (uint32_t)request.Parameters.RequestType;
01107       *this << (uint32_t)request.Parameters.SecurityMode;
01108       SerializeContainer(*this, request.Parameters.ClientNonce);
01109       *this << request.Parameters.RequestLifeTime;
01110     }
01111 
01112     template<>
01113     void DataDeserializer::Deserialize<OpcUa::OpenSecureChannelRequest>(OpcUa::OpenSecureChannelRequest& request)
01114     {
01115       *this >> request.TypeId;
01116       *this >> request.Header;
01117 
01118       *this >> request.Parameters.ClientProtocolVersion;
01119 
01120       uint32_t tmp = 0;
01121       *this >> tmp;
01122       request.Parameters.RequestType = static_cast<SecurityTokenRequestType>(tmp);
01123 
01124       uint32_t tmp2 = 0;
01125       *this >> tmp2;
01126       request.Parameters.SecurityMode = static_cast<MessageSecurityMode>(tmp2);
01127 
01128       DeserializeContainer(*this, request.Parameters.ClientNonce);
01129       *this >> request.Parameters.RequestLifeTime;
01130     };
01131 
01132 
01133     template<>
01134     void DataSerializer::Serialize<OpcUa::SecurityToken>(const OpcUa::SecurityToken& token)
01135     {
01136       *this << token.SecureChannelId;
01137       *this << token.TokenId;
01138       *this << token.CreatedAt;
01139       *this << token.RevisedLifetime;
01140     }
01141 
01142     template<>
01143     void DataDeserializer::Deserialize<OpcUa::SecurityToken>(OpcUa::SecurityToken& token)
01144     {
01145       *this >> token.SecureChannelId;
01146       *this >> token.TokenId;
01147       *this >> token.CreatedAt;
01148       *this >> token.RevisedLifetime;
01149     };
01150 
01151 
01152     template<>
01153     void DataSerializer::Serialize<OpcUa::OpenSecureChannelResponse>(const OpcUa::OpenSecureChannelResponse& response)
01154     {
01155       *this << response.TypeId;
01156       *this << response.Header;
01157       *this << response.ServerProtocolVersion;
01158       *this << response.ChannelSecurityToken;
01159       SerializeContainer(*this, response.ServerNonce);
01160     }
01161 
01162     template<>
01163     void DataDeserializer::Deserialize<OpcUa::OpenSecureChannelResponse>(OpcUa::OpenSecureChannelResponse& response)
01164     {
01165       *this >> response.TypeId;
01166       *this >> response.Header;
01167       *this >> response.ServerProtocolVersion;
01168       *this >> response.ChannelSecurityToken;
01169       DeserializeContainer(*this, response.ServerNonce);
01170     };
01171 
01172     template<>
01173     void DataSerializer::Serialize<OpcUa::Binary::RawMessage>(const OpcUa::Binary::RawMessage& raw)
01174     {
01175       Buffer.insert(Buffer.end(), raw.Data, raw.Data + raw.Size);
01176     }
01177 
01178     template<>
01179     void DataDeserializer::Deserialize<OpcUa::Binary::RawBuffer>(OpcUa::Binary::RawBuffer& raw)
01180     {
01181       GetData(In, raw.Data, raw.Size);
01182     };
01183 
01184     template<>
01185     void DataSerializer::Serialize<OpcUa::CloseSecureChannelRequest>(const OpcUa::CloseSecureChannelRequest& request)
01186     {
01187       *this << request.TypeId;
01188       *this << request.Header;
01189     }
01190 
01191     template<>
01192     void DataDeserializer::Deserialize<OpcUa::CloseSecureChannelRequest>(OpcUa::CloseSecureChannelRequest& request)
01193     {
01194       *this >> request.TypeId;
01195       *this >> request.Header;
01196     };
01197 
01198     template<>
01199     void DataSerializer::Serialize<OpcUa::LocalizedText>(const OpcUa::LocalizedText& lt)
01200     {
01201       *this << lt.Encoding;
01202       if (lt.Encoding & HAS_LOCALE)
01203       {
01204         *this << lt.Locale;
01205       }
01206       if (lt.Encoding & HAS_TEXT)
01207       {
01208         *this << lt.Text;
01209       }
01210     }
01211 
01212     template<>
01213     void DataDeserializer::Deserialize<OpcUa::LocalizedText>(OpcUa::LocalizedText& lt)
01214     {
01215       *this >> lt.Encoding;
01216       if (lt.Encoding & HAS_LOCALE)
01217       {
01218         *this >> lt.Locale;
01219       }
01220       if (lt.Encoding & HAS_TEXT)
01221       {
01222         *this >> lt.Text;
01223       }
01224     };
01225 
01226 
01227     template<>
01228     void DataSerializer::Serialize<std::vector<LocalizedText>>(const std::vector<LocalizedText>& value)
01229     {
01230       SerializeContainer(*this, value);
01231     }
01232 
01233     template<>
01234     void DataDeserializer::Deserialize<std::vector<LocalizedText>>(std::vector<LocalizedText>& value)
01235     {
01236       DeserializeContainer(*this, value);
01237     }
01238 
01239     template<>
01240     void DataSerializer::Serialize<ExtensionObjectHeader>(const ExtensionObjectHeader& value)
01241     {
01242       *this << value.TypeId;
01243       *this << static_cast<uint8_t>(value.Encoding);
01244     }
01245 
01246     template<>
01247     void DataDeserializer::Deserialize<ExtensionObjectHeader>(ExtensionObjectHeader& value)
01248     {
01249       *this >> value.TypeId;
01250       uint8_t tmp = 0;
01251       *this >> tmp;
01252       value.Encoding = static_cast<ExtensionObjectEncoding>(tmp);
01253     }
01254 
01255     template<>
01256     void DataSerializer::Serialize<QualifiedName>(const QualifiedName& name)
01257     {
01258       *this << name.NamespaceIndex;
01259       *this << name.Name;
01260     }
01261 
01262     template<>
01263     void DataDeserializer::Deserialize<QualifiedName>(QualifiedName&  name)
01264     {
01265       *this >> name.NamespaceIndex;
01266       *this >> name.Name;
01267     }
01268 
01270     // IntegerId
01272 
01273     template<>
01274     void DataSerializer::Serialize<IntegerId>(const IntegerId& id)
01275     {
01276       *this << static_cast<uint32_t>(id);
01277     }
01278 
01279     template<>
01280     void DataDeserializer::Deserialize<IntegerId>(IntegerId&  id)
01281     {
01282       uint32_t value = 0;
01283       *this >> value;
01284       id = value;
01285     }
01286 
01287     template<>
01288     void DataSerializer::Serialize<std::vector<IntegerId>>(const std::vector<IntegerId>& targets)
01289     {
01290       SerializeContainer(*this, targets);
01291     }
01292 
01293     template<>
01294     void DataDeserializer::Deserialize<std::vector<IntegerId>>(std::vector<IntegerId>& targets)
01295     {
01296       DeserializeContainer(*this, targets);
01297     }
01298 
01300     // StatusCode
01302 
01303     template<>
01304     void DataSerializer::Serialize<StatusCode>(const StatusCode& status)
01305     {
01306       *this << static_cast<uint32_t>(status);
01307     }
01308 
01309     template<>
01310     void DataDeserializer::Deserialize<StatusCode>(StatusCode&  status)
01311     {
01312       uint32_t value = 0;
01313       *this >> value;
01314       status = static_cast<StatusCode>(value);
01315     }
01316 
01317     template<>
01318     void DataSerializer::Serialize<std::vector<StatusCode>>(const std::vector<StatusCode>& value)
01319     {
01320       SerializeContainer(*this, value);
01321     }
01322 
01323     template<>
01324     void DataDeserializer::Deserialize<std::vector<StatusCode>>(std::vector<StatusCode>& value)
01325     {
01326       DeserializeContainer(*this, value);
01327     }
01328 
01329     template<>
01330     void DataSerializer::Serialize<std::vector<QualifiedName>>(const std::vector<QualifiedName>& value)
01331     {
01332       SerializeContainer(*this, value);
01333     }
01334 
01335     template<>
01336     void DataDeserializer::Deserialize<std::vector<QualifiedName>>(std::vector<QualifiedName>& value)
01337     {
01338       DeserializeContainer(*this, value);
01339     }
01340 
01341 
01342   } // namespace Binary
01343 } // namespace OpcUa
01344 


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:40