binary_messages.cpp
Go to the documentation of this file.
1 
13 #include <opc/ua/protocol/types.h>
14 
15 #include <algorithm>
16 #include <stdexcept>
17 #include <sstream>
18 #include <chrono>
19 
20 namespace OpcUa
21 {
22 // TODO move all in binary namespace to the binary_common.h
23 namespace Binary
24 {
26  : Type(MT_INVALID)
27  , Chunk(CHT_INVALID)
28  , Size(0)
29 {
30  ResetSize();
31 }
32 
34  : Type(type)
35  , Chunk(chunk)
36  , Size(0)
37 {
38  ResetSize();
39 }
40 
41 std::size_t Header::AddSize(std::size_t size)
42 {
43  Size += size;
44  return Size;
45 }
46 
47 
48 std::size_t Header::MessageSize() const
49 {
50  return Size - RawSize(*this);
51 }
52 
54 {
55  Size = RawSize(*this);
56 }
57 
59  : Type(MT_INVALID)
61  , Size(0)
62  , ChannelId(0)
63 {
64  ResetSize();
65 }
66 
67 SecureHeader::SecureHeader(MessageType type, ChunkType chunk, uint32_t channelId)
68  : Type(type)
69  , Chunk(chunk)
70  , Size(0)
71  , ChannelId(channelId)
72 {
73  ResetSize();
74 }
75 
76 std::size_t SecureHeader::AddSize(std::size_t size)
77 {
78  Size += size;
79  return Size;
80 }
81 
82 
83 std::size_t SecureHeader::MessageSize() const
84 {
85  return Size - RawSize(*this);
86 }
87 
89 {
90  Size = RawSize(*this);
91 }
92 
93 
95  : ProtocolVersion(0)
96  , ReceiveBufferSize(0)
97  , SendBufferSize(0)
98  , MaxMessageSize(0)
99  , MaxChunkCount(0)
100 {
101 }
102 
103 
105  : ProtocolVersion(0)
106  , ReceiveBufferSize(0)
107  , SendBufferSize(0)
108  , MaxMessageSize(0)
109  , MaxChunkCount(0)
110 {
111 }
112 
114  : Code(0)
115 {
116 }
117 
119  : SequenceNumber(0)
120  , RequestId(0)
121 {
122 }
123 
125  : TokenId(0)
126 {
127 }
128 
129 } // namespace Binary
130 
131 
132 DateTime DateTime::FromTimeT(time_t t, unsigned usec)
133 {
134  const int64_t daysBetween1601And1970 = 134774;
135  const int64_t secsFrom1601To1970 = daysBetween1601And1970 * 24 * 3600LL;
136  int64_t t1 = t + secsFrom1601To1970;
137  t1 = t1 * 10000000LL;
138  t1 += usec * 10;
139  return DateTime(t1);
140 }
141 
142 // do not use high_resolution_clock here as we need to be able to
143 // create times relative to 1601. high_resolution_clock may be an
144 // instance of steady_clock (it is on OSX) which does not have
145 // a from_time_t() method. Without from_time_t() it is not reliably
146 // possible to compute a time difference to 1970 or 1601 so we use
147 // have to use system_clock. Even for system_clock it is not _defined_
148 // which date is taken as start time - not like posix time_t which
149 // definitly starts 1970. So we have to honor a possible offset between
150 // epoch and posix start of time even when most actual implementors
151 // currently use 1970 as start of time and promise not to change that.
153 {
154  using namespace std::chrono;
155  static auto offset = system_clock::from_time_t(0).time_since_epoch();
156  const auto t = system_clock::now();
157  const auto us = duration_cast<microseconds>(t.time_since_epoch() + offset);
158  const auto n = us.count();
159  return DateTime::FromTimeT(n / 1000000, n % 1000000);
160 }
161 
162 time_t DateTime::ToTimeT(DateTime dateTime)
163 {
164  const int64_t daysBetween1601And1970 = 134774;
165  const int64_t secsFrom1601To1970 = daysBetween1601And1970 * 24 * 3600LL;
166 
167  if (dateTime.Value < secsFrom1601To1970)
168  {
169  std::stringstream stream;
170  stream << "OpcUa date time cannot be less than " << secsFrom1601To1970;
171  throw std::invalid_argument(stream.str());
172  }
173 
174  const int64_t secsFrom1970 = dateTime.Value / 10000000LL - secsFrom1601To1970;
175  return secsFrom1970;
176 }
177 
178 
180 {
182  SessionAuthenticationToken.TwoByteData.Identifier = 0;
183  UtcTime = DateTime::Current();
184  RequestHandle = 0;
185  ReturnDiagnostics = 0;
186  AuditEntryId = "";
187  Timeout = 0; // in miliseconds
188  Additional.TypeId.Encoding = EV_TWO_BYTE;
189  Additional.TypeId.TwoByteData.Identifier = 0;
190 }
191 
193  : ClientProtocolVersion(0)
194  , RequestType(SecurityTokenRequestType::Issue)
195  , SecurityMode(MessageSecurityMode::None)
196  , RequestLifeTime(300000)
197 {
198 }
199 
202 {
203 }
204 
206  : Timestamp(DateTime::Current())
207  , RequestHandle(0)
208  , ServiceResult(StatusCode::Good)
209 {
210 }
211 
214  , ServerProtocolVersion(0)
215 {
216 }
217 
220 {
221 }
222 
223 } // namespace OpcUa
224 
static DateTime FromTimeT(time_t t, unsigned usec=0)
SecurityTokenRequestType
Definition: enums.h:92
spdlog::log_clock::time_point now()
Definition: os.h:64
static time_t ToTimeT(DateTime dateTime)
std::size_t AddSize(std::size_t size)
MessageSecurityMode
Definition: enums.h:68
OPC UA Address space part. GNU LGPL.
const char * Binary(const char *input, short n)
std::size_t AddSize(std::size_t size)
int64_t Value
Definition: datetime.h:49
static DateTime Current()
std::size_t MessageSize() const
std::size_t MessageSize() const
std::size_t RawSize(const T &obj)


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