EntityId_t.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. * @file EntityId_t.hpp
  16. */
  17. #ifndef _FASTDDS_RTPS_COMMON_ENTITYID_T_HPP_
  18. #define _FASTDDS_RTPS_COMMON_ENTITYID_T_HPP_
  19. #include <fastrtps/fastrtps_dll.h>
  20. #include <fastdds/rtps/common/Types.h>
  21. #include <cstdint>
  22. #include <cstring>
  23. #include <sstream>
  24. namespace eprosima {
  25. namespace fastrtps {
  26. namespace rtps {
  27. #define ENTITYID_UNKNOWN 0x00000000
  28. #define ENTITYID_RTPSParticipant 0x000001c1
  29. #define ENTITYID_SEDP_BUILTIN_TOPIC_WRITER 0x000002c2
  30. #define ENTITYID_SEDP_BUILTIN_TOPIC_READER 0x000002c7
  31. #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER 0x000003c2
  32. #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER 0x000003c7
  33. #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER 0x000004c2
  34. #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER 0x000004c7
  35. #define ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER 0x000100c2
  36. #define ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER 0x000100c7
  37. #define ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER 0x000200C2
  38. #define ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER 0x000200C7
  39. #define ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER 0x000201C3
  40. #define ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER 0x000201C4
  41. #define ENTITYID_TL_SVC_REQ_WRITER 0x000300C3
  42. #define ENTITYID_TL_SVC_REQ_READER 0x000300C4
  43. #define ENTITYID_TL_SVC_REPLY_WRITER 0x000301C3
  44. #define ENTITYID_TL_SVC_REPLY_READER 0x000301C4
  45. #if HAVE_SECURITY
  46. #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER 0xff0003c2
  47. #define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER 0xff0003c7
  48. #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER 0xff0004c2
  49. #define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER 0xff0004c7
  50. #define ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER 0xff0200c2
  51. #define ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER 0xff0200c7
  52. #define ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER 0xff0202C3
  53. #define ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER 0xff0202C4
  54. #define ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_WRITER 0xff0101c2
  55. #define ENTITYID_SPDP_RELIABLE_BUILTIN_PARTICIPANT_SECURE_READER 0xff0101c7
  56. #endif
  57. //!@brief Structure EntityId_t, entity id part of GUID_t.
  58. //!@ingroup COMMON_MODULE
  59. struct RTPS_DllAPI EntityId_t
  60. {
  61. static constexpr unsigned int size = 4;
  62. octet value[size];
  63. //! Default constructor. Uknown entity.
  64. EntityId_t(){
  65. *this = ENTITYID_UNKNOWN;
  66. }
  67. /**
  68. * Main constructor.
  69. * @param id Entity id
  70. */
  71. EntityId_t(
  72. uint32_t id)
  73. {
  74. memcpy(value, &id, size);
  75. #if !__BIG_ENDIAN__
  76. reverse();
  77. #endif
  78. }
  79. /*!
  80. * @brief Copy constructor
  81. */
  82. EntityId_t(
  83. const EntityId_t& id)
  84. {
  85. memcpy(value, id.value, size);
  86. }
  87. /*!
  88. * @brief Move constructor
  89. */
  90. EntityId_t(
  91. EntityId_t&& id)
  92. {
  93. memmove(value, id.value, size);
  94. }
  95. EntityId_t& operator =(
  96. const EntityId_t& id)
  97. {
  98. memcpy(value, id.value, size);
  99. return *this;
  100. }
  101. EntityId_t& operator =(
  102. EntityId_t&& id)
  103. {
  104. memmove(value, id.value, size);
  105. return *this;
  106. }
  107. /**
  108. * Assignment operator.
  109. * @param id Entity id to copy
  110. */
  111. EntityId_t& operator =(
  112. uint32_t id)
  113. {
  114. memcpy(value, &id, size);
  115. #if !__BIG_ENDIAN__
  116. reverse();
  117. #endif
  118. return *this;
  119. //return id;
  120. }
  121. #if !__BIG_ENDIAN__
  122. //!
  123. void reverse(){
  124. octet oaux;
  125. oaux = value[3];
  126. value[3] = value[0];
  127. value[0] = oaux;
  128. oaux = value[2];
  129. value[2] = value[1];
  130. value[1] = oaux;
  131. }
  132. #endif
  133. static EntityId_t unknown()
  134. {
  135. return EntityId_t();
  136. }
  137. };
  138. #ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
  139. /**
  140. * Guid prefix comparison operator
  141. * @param id1 EntityId to compare
  142. * @param id2 ID prefix to compare
  143. * @return True if equal
  144. */
  145. inline bool operator ==(
  146. EntityId_t& id1,
  147. const uint32_t id2)
  148. {
  149. #if !__BIG_ENDIAN__
  150. id1.reverse();
  151. #endif
  152. const bool result = 0 == memcmp(id1.value, &id2, sizeof(id2));
  153. #if !__BIG_ENDIAN__
  154. id1.reverse();
  155. #endif
  156. return result;
  157. }
  158. /**
  159. * Guid prefix comparison operator
  160. * @param id1 First EntityId to compare
  161. * @param id2 Second EntityId to compare
  162. * @return True if equal
  163. */
  164. inline bool operator ==(
  165. const EntityId_t& id1,
  166. const EntityId_t& id2)
  167. {
  168. for (uint8_t i = 0; i < 4; ++i)
  169. {
  170. if (id1.value[i] != id2.value[i])
  171. {
  172. return false;
  173. }
  174. }
  175. return true;
  176. }
  177. /**
  178. * Guid prefix comparison operator
  179. * @param id1 First EntityId to compare
  180. * @param id2 Second EntityId to compare
  181. * @return True if not equal
  182. */
  183. inline bool operator !=(
  184. const EntityId_t& id1,
  185. const EntityId_t& id2)
  186. {
  187. for (uint8_t i = 0; i < 4; ++i)
  188. {
  189. if (id1.value[i] != id2.value[i])
  190. {
  191. return true;
  192. }
  193. }
  194. return false;
  195. }
  196. #endif
  197. inline std::ostream& operator <<(
  198. std::ostream& output,
  199. const EntityId_t& enI)
  200. {
  201. output << std::hex;
  202. output << (int)enI.value[0] << "." << (int)enI.value[1] << "." << (int)enI.value[2] << "." << (int)enI.value[3];
  203. return output << std::dec;
  204. }
  205. inline std::istream& operator >>(
  206. std::istream& input,
  207. EntityId_t& enP)
  208. {
  209. std::istream::sentry s(input);
  210. if (s)
  211. {
  212. char point;
  213. unsigned short hex;
  214. std::ios_base::iostate excp_mask = input.exceptions();
  215. try
  216. {
  217. input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
  218. input >> std::hex >> hex;
  219. if (hex > 255)
  220. {
  221. input.setstate(std::ios_base::failbit);
  222. }
  223. enP.value[0] = static_cast<octet>(hex);
  224. for (int i = 1; i < 4; ++i)
  225. {
  226. input >> point >> hex;
  227. if ( point != '.' || hex > 255 )
  228. {
  229. input.setstate(std::ios_base::failbit);
  230. }
  231. enP.value[i] = static_cast<octet>(hex);
  232. }
  233. input >> std::dec;
  234. }
  235. catch (std::ios_base::failure& ){}
  236. input.exceptions(excp_mask);
  237. }
  238. return input;
  239. }
  240. const EntityId_t c_EntityId_Unknown = ENTITYID_UNKNOWN;
  241. const EntityId_t c_EntityId_SPDPReader = ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER;
  242. const EntityId_t c_EntityId_SPDPWriter = ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER;
  243. const EntityId_t c_EntityId_SEDPPubWriter = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER;
  244. const EntityId_t c_EntityId_SEDPPubReader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER;
  245. const EntityId_t c_EntityId_SEDPSubWriter = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER;
  246. const EntityId_t c_EntityId_SEDPSubReader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER;
  247. const EntityId_t c_EntityId_RTPSParticipant = ENTITYID_RTPSParticipant;
  248. const EntityId_t c_EntityId_WriterLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER;
  249. const EntityId_t c_EntityId_ReaderLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER;
  250. const EntityId_t participant_stateless_message_writer_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER;
  251. const EntityId_t participant_stateless_message_reader_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER;
  252. const EntityId_t c_EntityId_TypeLookup_request_writer = ENTITYID_TL_SVC_REQ_WRITER;
  253. const EntityId_t c_EntityId_TypeLookup_request_reader = ENTITYID_TL_SVC_REQ_READER;
  254. const EntityId_t c_EntityId_TypeLookup_reply_writer = ENTITYID_TL_SVC_REPLY_WRITER;
  255. const EntityId_t c_EntityId_TypeLookup_reply_reader = ENTITYID_TL_SVC_REPLY_READER;
  256. #if HAVE_SECURITY
  257. const EntityId_t sedp_builtin_publications_secure_writer = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER;
  258. const EntityId_t sedp_builtin_publications_secure_reader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER;
  259. const EntityId_t sedp_builtin_subscriptions_secure_writer = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER;
  260. const EntityId_t sedp_builtin_subscriptions_secure_reader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER;
  261. const EntityId_t participant_volatile_message_secure_writer_entity_id =
  262. ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER;
  263. const EntityId_t participant_volatile_message_secure_reader_entity_id =
  264. ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER;
  265. const EntityId_t c_EntityId_WriterLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER;
  266. const EntityId_t c_EntityId_ReaderLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER;
  267. #endif
  268. } // namespace rtps
  269. } // namespace fastrtps
  270. } // namespace eprosima
  271. namespace std {
  272. template <>
  273. struct hash<eprosima::fastrtps::rtps::EntityId_t>
  274. {
  275. std::size_t operator()(
  276. const eprosima::fastrtps::rtps::EntityId_t& k) const
  277. {
  278. // recover the participant entity counter
  279. eprosima::fastrtps::rtps::octet value[4];
  280. #if __BIG_ENDIAN__
  281. value[3] = k.value[2];
  282. value[2] = k.value[1];
  283. value[1] = k.value[0];
  284. value[0] = 0;
  285. #else
  286. value[3] = 0;
  287. value[2] = k.value[0];
  288. value[1] = k.value[1];
  289. value[0] = k.value[2];
  290. #endif
  291. return static_cast<std::size_t>(*reinterpret_cast<const uint32_t*>(&value));
  292. }
  293. };
  294. } // namespace std
  295. #endif /* _FASTDDS_RTPS_COMMON_ENTITYID_T_HPP_ */