binary_messages.cpp
Go to the documentation of this file.
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 #include <opc/ua/protocol/binary/common.h>
00012 #include <opc/ua/protocol/secure_channel.h>
00013 #include <opc/ua/protocol/types.h>
00014 
00015 #include <algorithm>
00016 #include <stdexcept>
00017 #include <sstream>
00018 #include <chrono>
00019 
00020 namespace OpcUa
00021 {
00022   // TODO move all in binary namespace to the binary_common.h
00023   namespace Binary
00024   {
00025     Header::Header()
00026       : Type(MT_INVALID)
00027       , Chunk(CHT_INVALID)
00028       , Size(0)
00029     {
00030       ResetSize();
00031     }
00032 
00033     Header::Header(MessageType type, ChunkType chunk)
00034       : Type(type)
00035       , Chunk(chunk)
00036       , Size(0)
00037     {
00038       ResetSize();
00039     }
00040 
00041     std::size_t Header::AddSize(std::size_t size)
00042     {
00043       Size += size;
00044       return Size;
00045     };
00046 
00047 
00048     std::size_t Header::MessageSize() const
00049     {
00050       return Size - RawSize(*this);
00051     }
00052 
00053     void Header::ResetSize()
00054     {
00055       Size = RawSize(*this);
00056     }
00057 
00058     SecureHeader::SecureHeader()
00059       : Type(MT_INVALID)
00060       , Chunk(CHT_INVALID)
00061       , Size(0)
00062       , ChannelId(0)
00063     {
00064       ResetSize();
00065     }
00066 
00067     SecureHeader::SecureHeader(MessageType type, ChunkType chunk, uint32_t channelId)
00068       : Type(type)
00069       , Chunk(chunk)
00070       , Size(0)
00071       , ChannelId(channelId)
00072     {
00073       ResetSize();
00074     }
00075 
00076     std::size_t SecureHeader::AddSize(std::size_t size)
00077     {
00078       Size += size;
00079       return Size;
00080     };
00081 
00082 
00083     std::size_t SecureHeader::MessageSize() const
00084     {
00085       return Size - RawSize(*this);
00086     }
00087 
00088     void SecureHeader::ResetSize()
00089     {
00090       Size = RawSize(*this);
00091     }
00092 
00093 
00094     Hello::Hello()
00095       : ProtocolVersion(0)
00096       , ReceiveBufferSize(0)
00097       , SendBufferSize(0)
00098       , MaxMessageSize(0)
00099       , MaxChunkCount(0)
00100     {
00101     }
00102 
00103 
00104     Acknowledge::Acknowledge()
00105       : ProtocolVersion(0)
00106       , ReceiveBufferSize(0)
00107       , SendBufferSize(0)
00108       , MaxMessageSize(0)
00109       , MaxChunkCount(0)
00110     {
00111     }
00112 
00113     Error::Error()
00114       : Code(0)
00115     {
00116     }
00117 
00118     SequenceHeader::SequenceHeader()
00119       : SequenceNumber(0)
00120       , RequestId(0)
00121     {
00122     }
00123 
00124     SymmetricAlgorithmHeader::SymmetricAlgorithmHeader()
00125       : TokenId(0)
00126     {
00127     }
00128 
00129   } // namespace Binary
00130 
00131 
00132   DateTime DateTime::FromTimeT(time_t t, unsigned usec)
00133   {
00134     const int64_t daysBetween1601And1970 = 134774;
00135     const int64_t secsFrom1601To1970 = daysBetween1601And1970 * 24 * 3600LL;
00136     int64_t t1 = t + secsFrom1601To1970;
00137     t1 = t1 * 10000000LL;
00138     t1 += usec * 10;
00139     return DateTime(t1);
00140   }
00141 
00142   DateTime DateTime::Current()
00143   {
00144     using namespace std::chrono;
00145     const auto t = time_point<high_resolution_clock>(high_resolution_clock::now());
00146     const auto us = duration_cast<microseconds>(t.time_since_epoch());
00147     const auto n = us.count();
00148     return DateTime::FromTimeT(n / 1000000, n % 1000000);
00149   }
00150 
00151   time_t DateTime::ToTimeT(DateTime dateTime)
00152   {
00153     const int64_t daysBetween1601And1970 = 134774;
00154     const int64_t secsFrom1601To1970 = daysBetween1601And1970 * 24 * 3600LL;
00155     if (dateTime.Value < secsFrom1601To1970)
00156     {
00157       std::stringstream stream;
00158       stream << "OpcUa date time cannot be less than " << secsFrom1601To1970;
00159       throw std::invalid_argument(stream.str());
00160     }
00161     const int64_t secsFrom1970 = dateTime.Value / 10000000LL - secsFrom1601To1970;
00162     return secsFrom1970;
00163   }
00164 
00165 
00166   RequestHeader::RequestHeader()
00167   {
00168     SessionAuthenticationToken.Encoding = EV_TWO_BYTE;
00169     SessionAuthenticationToken.TwoByteData.Identifier = 0;
00170     UtcTime = DateTime::Current();
00171     RequestHandle = 0;
00172     ReturnDiagnostics = 0;
00173     AuditEntryId = "";
00174     Timeout = 0; // in miliseconds
00175     Additional.TypeId.Encoding = EV_TWO_BYTE;
00176     Additional.TypeId.TwoByteData.Identifier = 0;
00177   }
00178 
00179   OpenSecureChannelParameters::OpenSecureChannelParameters()
00180     : ClientProtocolVersion(0)
00181     , RequestType(SecurityTokenRequestType::Issue)
00182     , SecurityMode(MessageSecurityMode::None)
00183     , RequestLifeTime(300000)
00184   {
00185   }
00186 
00187   OpenSecureChannelRequest::OpenSecureChannelRequest()
00188     : TypeId(OPEN_SECURE_CHANNEL_REQUEST)
00189   {
00190   }
00191 
00192   ResponseHeader::ResponseHeader()
00193     : Timestamp(DateTime::Current())
00194     , RequestHandle(0)
00195     , ServiceResult(StatusCode::Good)
00196   {
00197   }
00198 
00199   OpenSecureChannelResponse::OpenSecureChannelResponse()
00200     : TypeId(OPEN_SECURE_CHANNEL_RESPONSE)
00201     , ServerProtocolVersion(0)
00202   {
00203   }
00204 
00205   CloseSecureChannelRequest::CloseSecureChannelRequest()
00206     : TypeId(OpcUa::CLOSE_SECURE_CHANNEL_REQUEST)
00207   {
00208   }
00209 
00210 } // namespace OpcUa
00211 


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