/src/Fast-DDS/include/fastdds/rtps/common/EntityId_t.hpp
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | /** |
16 | | * @file EntityId_t.hpp |
17 | | */ |
18 | | |
19 | | #ifndef FASTDDS_RTPS_COMMON__ENTITYID_T_HPP |
20 | | #define FASTDDS_RTPS_COMMON__ENTITYID_T_HPP |
21 | | |
22 | | #include <fastdds/fastdds_dll.hpp> |
23 | | #include <fastdds/rtps/common/Types.hpp> |
24 | | |
25 | | #include <cstdint> |
26 | | #include <cstring> |
27 | | #include <sstream> |
28 | | |
29 | | namespace eprosima { |
30 | | namespace fastdds { |
31 | | namespace rtps { |
32 | | |
33 | | |
34 | 25.1k | #define ENTITYID_UNKNOWN 0x00000000 |
35 | 0 | #define ENTITYID_RTPSParticipant 0x000001c1 |
36 | | #define ENTITYID_SEDP_BUILTIN_TOPIC_WRITER 0x000002c2 |
37 | | #define ENTITYID_SEDP_BUILTIN_TOPIC_READER 0x000002c7 |
38 | 0 | #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER 0x000003c2 |
39 | 0 | #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER 0x000003c7 |
40 | 0 | #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER 0x000004c2 |
41 | 0 | #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER 0x000004c7 |
42 | 0 | #define ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER 0x000100c2 |
43 | 0 | #define ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER 0x000100c7 |
44 | | #define ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER 0x000200C2 |
45 | | #define ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER 0x000200C7 |
46 | | #define ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER 0x000201C3 |
47 | | #define ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER 0x000201C4 |
48 | | |
49 | | #define ENTITYID_TL_SVC_REQ_WRITER 0x000300C3 |
50 | | #define ENTITYID_TL_SVC_REQ_READER 0x000300C4 |
51 | | #define ENTITYID_TL_SVC_REPLY_WRITER 0x000301C3 |
52 | | #define ENTITYID_TL_SVC_REPLY_READER 0x000301C4 |
53 | | |
54 | | #if HAVE_SECURITY |
55 | | #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER 0xff0003c2 |
56 | | #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER 0xff0003c7 |
57 | | #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER 0xff0004c2 |
58 | | #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER 0xff0004c7 |
59 | | #define ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER 0xff0200c2 |
60 | | #define ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER 0xff0200c7 |
61 | | #define ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER 0xff0202C3 |
62 | | #define ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER 0xff0202C4 |
63 | | #define ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER 0xff0101c2 |
64 | | #define ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER 0xff0101c7 |
65 | | #endif // if HAVE_SECURITY |
66 | | |
67 | | #define ENTITYID_DS_SERVER_VIRTUAL_WRITER 0x00030073 |
68 | | #define ENTITYID_DS_SERVER_VIRTUAL_READER 0x00030074 |
69 | | |
70 | | #ifdef FASTDDS_STATISTICS |
71 | 0 | #define ENTITYID_MONITOR_SERVICE_WRITER 0x004000D2 |
72 | | #endif // ifdef FASTDDS_STATISTICS |
73 | | |
74 | | //!@brief Structure EntityId_t, entity id part of GUID_t. |
75 | | //!@ingroup COMMON_MODULE |
76 | | struct FASTDDS_EXPORTED_API EntityId_t |
77 | | { |
78 | | static constexpr unsigned int size = 4; |
79 | | octet value[size]; |
80 | | //! Default constructor. Unknown entity. |
81 | | EntityId_t() |
82 | 25.1k | { |
83 | 25.1k | *this = ENTITYID_UNKNOWN; |
84 | 25.1k | } |
85 | | |
86 | | /** |
87 | | * Main constructor. |
88 | | * @param id Entity id |
89 | | */ |
90 | | EntityId_t( |
91 | | uint32_t id) |
92 | 12.3k | { |
93 | 12.3k | memcpy(value, &id, size); |
94 | 12.3k | #if !FASTDDS_IS_BIG_ENDIAN_TARGET |
95 | 12.3k | reverse(); |
96 | 12.3k | #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET |
97 | 12.3k | } |
98 | | |
99 | | /*! |
100 | | * @brief Copy constructor |
101 | | */ |
102 | | EntityId_t( |
103 | | const EntityId_t& id) |
104 | 32 | { |
105 | 32 | memcpy(value, id.value, size); |
106 | 32 | } |
107 | | |
108 | | /*! |
109 | | * @brief Move constructor |
110 | | */ |
111 | | EntityId_t( |
112 | | EntityId_t&& id) |
113 | 0 | { |
114 | 0 | memmove(value, id.value, size); |
115 | 0 | } |
116 | | |
117 | | EntityId_t& operator =( |
118 | | const EntityId_t& id) |
119 | 0 | { |
120 | 0 | memcpy(value, id.value, size); |
121 | 0 | return *this; |
122 | 0 | } |
123 | | |
124 | | EntityId_t& operator =( |
125 | | EntityId_t&& id) |
126 | 0 | { |
127 | 0 | memmove(value, id.value, size); |
128 | 0 | return *this; |
129 | 0 | } |
130 | | |
131 | | /** |
132 | | * Assignment operator. |
133 | | * @param id Entity id to copy |
134 | | */ |
135 | | EntityId_t& operator =( |
136 | | uint32_t id) |
137 | 25.1k | { |
138 | 25.1k | memcpy(value, &id, size); |
139 | 25.1k | #if !FASTDDS_IS_BIG_ENDIAN_TARGET |
140 | 25.1k | reverse(); |
141 | 25.1k | #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET |
142 | 25.1k | return *this; |
143 | | //return id; |
144 | 25.1k | } |
145 | | |
146 | | #if !FASTDDS_IS_BIG_ENDIAN_TARGET |
147 | | //! |
148 | | void reverse() |
149 | 37.5k | { |
150 | 37.5k | octet oaux; |
151 | 37.5k | oaux = value[3]; |
152 | 37.5k | value[3] = value[0]; |
153 | 37.5k | value[0] = oaux; |
154 | 37.5k | oaux = value[2]; |
155 | 37.5k | value[2] = value[1]; |
156 | 37.5k | value[1] = oaux; |
157 | 37.5k | } |
158 | | |
159 | | #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET |
160 | | |
161 | | /*! |
162 | | * @brief conversion to uint32_t |
163 | | * @return uint32_t representation |
164 | | */ |
165 | | uint32_t to_uint32() const |
166 | 0 | { |
167 | 0 | uint32_t res = *reinterpret_cast<const uint32_t*>(value); |
168 | |
|
169 | 0 | #if !FASTDDS_IS_BIG_ENDIAN_TARGET |
170 | 0 | res = ( res >> 24 ) | |
171 | 0 | (0x0000ff00 & ( res >> 8)) | |
172 | 0 | (0x00ff0000 & ( res << 8)) | |
173 | 0 | ( res << 24 ); |
174 | 0 | #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET |
175 | |
|
176 | 0 | return res; |
177 | 0 | } |
178 | | |
179 | | static EntityId_t unknown() |
180 | 0 | { |
181 | 0 | return EntityId_t(); |
182 | 0 | } |
183 | | |
184 | | bool is_reader() const |
185 | 0 | { |
186 | | // RTPS Standard table 9.1 |
187 | 0 | return 0x4u & to_uint32(); |
188 | 0 | } |
189 | | |
190 | | bool is_writer() const |
191 | 0 | { |
192 | | // RTPS Standard table 9.1 |
193 | 0 | return 0x2u & to_uint32() && !is_reader(); |
194 | 0 | } |
195 | | |
196 | | /** |
197 | | * Entity Id minor operator |
198 | | * @param other Second entity id to compare |
199 | | * @return True if \c other is higher than this |
200 | | */ |
201 | | bool operator <( |
202 | | const EntityId_t& other) const |
203 | 0 | { |
204 | 0 | return std::memcmp(value, other.value, size) < 0; |
205 | 0 | } |
206 | | |
207 | | /** |
208 | | * Entity Id compare static method. |
209 | | * |
210 | | * @param entity1 First entity id to compare |
211 | | * @param entity2 Second entity id to compare |
212 | | * |
213 | | * @return 0 if \c entity1 is equal to \c entity2 . |
214 | | * @return < 0 if \c entity1 is lower than \c entity2 . |
215 | | * @return > 0 if \c entity1 is higher than \c entity2 . |
216 | | */ |
217 | | static int cmp( |
218 | | const EntityId_t& entity1, |
219 | | const EntityId_t& entity2) |
220 | 11.3k | { |
221 | 11.3k | return std::memcmp(entity1.value, entity2.value, size); |
222 | 11.3k | } |
223 | | |
224 | | }; |
225 | | |
226 | | #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
227 | | |
228 | | /** |
229 | | * Entity Id comparison operator |
230 | | * @param id1 EntityId to compare |
231 | | * @param id2 ID prefix to compare |
232 | | * @return True if equal |
233 | | */ |
234 | | inline bool operator ==( |
235 | | EntityId_t& id1, |
236 | | const uint32_t id2) |
237 | 0 | { |
238 | 0 | #if !FASTDDS_IS_BIG_ENDIAN_TARGET |
239 | 0 | id1.reverse(); |
240 | 0 | #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET |
241 | 0 | const bool result = 0 == memcmp(id1.value, &id2, sizeof(id2)); |
242 | 0 | #if !FASTDDS_IS_BIG_ENDIAN_TARGET |
243 | 0 | id1.reverse(); |
244 | 0 | #endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET |
245 | 0 | return result; |
246 | 0 | } |
247 | | |
248 | | /** |
249 | | * Entity Id comparison operator |
250 | | * @param id1 First EntityId to compare |
251 | | * @param id2 Second EntityId to compare |
252 | | * @return True if equal |
253 | | */ |
254 | | inline bool operator ==( |
255 | | const EntityId_t& id1, |
256 | | const EntityId_t& id2) |
257 | 11.3k | { |
258 | 11.3k | return EntityId_t::cmp(id1, id2) == 0; |
259 | 11.3k | } |
260 | | |
261 | | /** |
262 | | * Guid prefix comparison operator |
263 | | * @param id1 First EntityId to compare |
264 | | * @param id2 Second EntityId to compare |
265 | | * @return True if not equal |
266 | | */ |
267 | | inline bool operator !=( |
268 | | const EntityId_t& id1, |
269 | | const EntityId_t& id2) |
270 | 11.3k | { |
271 | | // Use == operator as it is faster enough. |
272 | | // NOTE: this could be done comparing the entities backwards (starting in [3]) as it would probably be faster. |
273 | 11.3k | return !(operator ==(id1, id2)); |
274 | 11.3k | } |
275 | | |
276 | | #endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC |
277 | | |
278 | | inline std::ostream& operator <<( |
279 | | std::ostream& output, |
280 | | const EntityId_t& enI) |
281 | 0 | { |
282 | 0 | std::stringstream ss; |
283 | 0 | ss << std::hex; |
284 | 0 | ss << (int)enI.value[0] << "." << (int)enI.value[1] << "." << (int)enI.value[2] << "." << (int)enI.value[3]; |
285 | 0 | ss << std::dec; |
286 | 0 | return output << ss.str(); |
287 | 0 | } |
288 | | |
289 | | inline std::istream& operator >>( |
290 | | std::istream& input, |
291 | | EntityId_t& enP) |
292 | 0 | { |
293 | 0 | std::istream::sentry s(input); |
294 | |
|
295 | 0 | if (s) |
296 | 0 | { |
297 | 0 | char point; |
298 | 0 | unsigned short hex; |
299 | 0 | std::ios_base::iostate excp_mask = input.exceptions(); |
300 | |
|
301 | 0 | try |
302 | 0 | { |
303 | 0 | input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit); |
304 | 0 | input >> std::hex >> hex; |
305 | |
|
306 | 0 | if (hex > 255) |
307 | 0 | { |
308 | 0 | input.setstate(std::ios_base::failbit); |
309 | 0 | } |
310 | |
|
311 | 0 | enP.value[0] = static_cast<octet>(hex); |
312 | |
|
313 | 0 | for (int i = 1; i < 4; ++i) |
314 | 0 | { |
315 | 0 | input >> point >> hex; |
316 | 0 | if ( point != '.' || hex > 255 ) |
317 | 0 | { |
318 | 0 | input.setstate(std::ios_base::failbit); |
319 | 0 | } |
320 | 0 | enP.value[i] = static_cast<octet>(hex); |
321 | 0 | } |
322 | |
|
323 | 0 | input >> std::dec; |
324 | 0 | } |
325 | 0 | catch (std::ios_base::failure& ) |
326 | 0 | { |
327 | 0 | } |
328 | |
|
329 | 0 | input.exceptions(excp_mask); |
330 | 0 | } |
331 | |
|
332 | 0 | return input; |
333 | 0 | } |
334 | | |
335 | | const EntityId_t c_EntityId_Unknown = ENTITYID_UNKNOWN; |
336 | | const EntityId_t c_EntityId_SPDPReader = ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER; |
337 | | const EntityId_t c_EntityId_SPDPWriter = ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER; |
338 | | |
339 | | const EntityId_t c_EntityId_SEDPPubWriter = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER; |
340 | | const EntityId_t c_EntityId_SEDPPubReader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER; |
341 | | const EntityId_t c_EntityId_SEDPSubWriter = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER; |
342 | | const EntityId_t c_EntityId_SEDPSubReader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER; |
343 | | |
344 | | const EntityId_t c_EntityId_RTPSParticipant = ENTITYID_RTPSParticipant; |
345 | | |
346 | | const EntityId_t c_EntityId_WriterLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER; |
347 | | const EntityId_t c_EntityId_ReaderLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER; |
348 | | |
349 | | const EntityId_t participant_stateless_message_writer_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER; |
350 | | const EntityId_t participant_stateless_message_reader_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER; |
351 | | |
352 | | const EntityId_t c_EntityId_TypeLookup_request_writer = ENTITYID_TL_SVC_REQ_WRITER; |
353 | | const EntityId_t c_EntityId_TypeLookup_request_reader = ENTITYID_TL_SVC_REQ_READER; |
354 | | const EntityId_t c_EntityId_TypeLookup_reply_writer = ENTITYID_TL_SVC_REPLY_WRITER; |
355 | | const EntityId_t c_EntityId_TypeLookup_reply_reader = ENTITYID_TL_SVC_REPLY_READER; |
356 | | |
357 | | #if HAVE_SECURITY |
358 | | const EntityId_t sedp_builtin_publications_secure_writer = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER; |
359 | | const EntityId_t sedp_builtin_publications_secure_reader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER; |
360 | | const EntityId_t sedp_builtin_subscriptions_secure_writer = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER; |
361 | | const EntityId_t sedp_builtin_subscriptions_secure_reader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER; |
362 | | |
363 | | const EntityId_t participant_volatile_message_secure_writer_entity_id = |
364 | | ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER; |
365 | | const EntityId_t participant_volatile_message_secure_reader_entity_id = |
366 | | ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER; |
367 | | |
368 | | const EntityId_t c_EntityId_WriterLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER; |
369 | | const EntityId_t c_EntityId_ReaderLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER; |
370 | | |
371 | | const EntityId_t c_EntityId_spdp_reliable_participant_secure_reader = |
372 | | ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER; |
373 | | const EntityId_t c_EntityId_spdp_reliable_participant_secure_writer = |
374 | | ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER; |
375 | | #endif // if HAVE_SECURITY |
376 | | |
377 | | const EntityId_t ds_server_virtual_writer = ENTITYID_DS_SERVER_VIRTUAL_WRITER; |
378 | | const EntityId_t ds_server_virtual_reader = ENTITYID_DS_SERVER_VIRTUAL_READER; |
379 | | |
380 | | #ifdef FASTDDS_STATISTICS |
381 | | const EntityId_t monitor_service_status_writer = ENTITYID_MONITOR_SERVICE_WRITER; |
382 | | #endif // if FASTDDS_STATISTICS |
383 | | |
384 | | } // namespace rtps |
385 | | } // namespace fastdds |
386 | | } // namespace eprosima |
387 | | |
388 | | namespace std { |
389 | | template <> |
390 | | struct hash<eprosima::fastdds::rtps::EntityId_t> |
391 | | { |
392 | | std::size_t operator ()( |
393 | | const eprosima::fastdds::rtps::EntityId_t& k) const |
394 | 8.86k | { |
395 | 8.86k | return (static_cast<size_t>(k.value[0]) << 16) | |
396 | 8.86k | (static_cast<size_t>(k.value[1]) << 8) | |
397 | 8.86k | static_cast<size_t>(k.value[2]); |
398 | 8.86k | } |
399 | | |
400 | | }; |
401 | | |
402 | | } // namespace std |
403 | | |
404 | | |
405 | | #endif // FASTDDS_RTPS_COMMON__ENTITYID_T_HPP |