Coverage Report

Created: 2022-08-24 06:19

/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 <fastrtps/fastrtps_dll.h>
23
#include <fastdds/rtps/common/Types.h>
24
25
#include <cstdint>
26
#include <cstring>
27
#include <sstream>
28
29
namespace eprosima {
30
namespace fastrtps {
31
namespace rtps {
32
33
34
54.4k
#define ENTITYID_UNKNOWN 0x00000000
35
#define ENTITYID_RTPSParticipant  0x000001c1
36
#define ENTITYID_SEDP_BUILTIN_TOPIC_WRITER  0x000002c2
37
#define ENTITYID_SEDP_BUILTIN_TOPIC_READER 0x000002c7
38
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER  0x000003c2
39
#define ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER  0x000003c7
40
#define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER 0x000004c2
41
#define ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER  0x000004c7
42
0
#define ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER  0x000100c2
43
#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
//!@brief Structure EntityId_t, entity id part of GUID_t.
71
//!@ingroup COMMON_MODULE
72
struct RTPS_DllAPI EntityId_t
73
{
74
    static constexpr unsigned int size = 4;
75
    octet value[size];
76
    //! Default constructor. Unknown entity.
77
    EntityId_t()
78
54.4k
    {
79
54.4k
        *this = ENTITYID_UNKNOWN;
80
54.4k
    }
81
82
    /**
83
     * Main constructor.
84
     * @param id Entity id
85
     */
86
    EntityId_t(
87
            uint32_t id)
88
5.32k
    {
89
5.32k
        memcpy(value, &id, size);
90
5.32k
#if !FASTDDS_IS_BIG_ENDIAN_TARGET
91
5.32k
        reverse();
92
5.32k
#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET
93
5.32k
    }
94
95
    /*!
96
     * @brief Copy constructor
97
     */
98
    EntityId_t(
99
            const EntityId_t& id)
100
0
    {
101
0
        memcpy(value, id.value, size);
102
0
    }
103
104
    /*!
105
     * @brief Move constructor
106
     */
107
    EntityId_t(
108
            EntityId_t&& id)
109
0
    {
110
0
        memmove(value, id.value, size);
111
0
    }
112
113
    EntityId_t& operator =(
114
            const EntityId_t& id)
115
0
    {
116
0
        memcpy(value, id.value, size);
117
0
        return *this;
118
0
    }
119
120
    EntityId_t& operator =(
121
            EntityId_t&& id)
122
0
    {
123
0
        memmove(value, id.value, size);
124
0
        return *this;
125
0
    }
126
127
    /**
128
     * Assignment operator.
129
     * @param id Entity id to copy
130
     */
131
    EntityId_t& operator =(
132
            uint32_t id)
133
54.4k
    {
134
54.4k
        memcpy(value, &id, size);
135
54.4k
#if !FASTDDS_IS_BIG_ENDIAN_TARGET
136
54.4k
        reverse();
137
54.4k
#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET
138
54.4k
        return *this;
139
        //return id;
140
54.4k
    }
141
142
#if !FASTDDS_IS_BIG_ENDIAN_TARGET
143
    //!
144
    void reverse()
145
59.8k
    {
146
59.8k
        octet oaux;
147
59.8k
        oaux = value[3];
148
59.8k
        value[3] = value[0];
149
59.8k
        value[0] = oaux;
150
59.8k
        oaux = value[2];
151
59.8k
        value[2] = value[1];
152
59.8k
        value[1] = oaux;
153
59.8k
    }
154
155
#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET
156
157
    /*!
158
     * @brief conversion to uint32_t
159
     * @return uint32_t representation
160
     */
161
    uint32_t to_uint32() const
162
0
    {
163
0
        uint32_t res = *reinterpret_cast<const uint32_t*>(value);
164
0
165
0
#if !FASTDDS_IS_BIG_ENDIAN_TARGET
166
0
        res = ( res >> 24 ) |
167
0
                (0x0000ff00 & ( res >> 8)) |
168
0
                (0x00ff0000 & ( res << 8)) |
169
0
                ( res << 24 );
170
0
#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET
171
0
172
0
        return res;
173
0
    }
174
175
    static EntityId_t unknown()
176
0
    {
177
0
        return EntityId_t();
178
0
    }
179
180
    bool is_reader() const
181
0
    {
182
0
        // RTPS Standard table 9.1
183
0
        return 0x4u & to_uint32();
184
0
    }
185
186
    bool is_writer() const
187
0
    {
188
0
        // RTPS Standard table 9.1
189
0
        return 0x2u & to_uint32() && !is_reader();
190
0
    }
191
192
};
193
194
#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
195
196
/**
197
 * Guid prefix comparison operator
198
 * @param id1 EntityId to compare
199
 * @param id2 ID prefix to compare
200
 * @return True if equal
201
 */
202
inline bool operator ==(
203
        EntityId_t& id1,
204
        const uint32_t id2)
205
0
{
206
0
#if !FASTDDS_IS_BIG_ENDIAN_TARGET
207
0
    id1.reverse();
208
0
#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET
209
0
    const bool result = 0 == memcmp(id1.value, &id2, sizeof(id2));
210
0
#if !FASTDDS_IS_BIG_ENDIAN_TARGET
211
0
    id1.reverse();
212
0
#endif // if !FASTDDS_IS_BIG_ENDIAN_TARGET
213
0
    return result;
214
0
}
215
216
/**
217
 * Guid prefix comparison operator
218
 * @param id1 First EntityId to compare
219
 * @param id2 Second EntityId to compare
220
 * @return True if equal
221
 */
222
inline bool operator ==(
223
        const EntityId_t& id1,
224
        const EntityId_t& id2)
225
0
{
226
0
    for (uint8_t i = 0; i < 4; ++i)
227
0
    {
228
0
        if (id1.value[i] != id2.value[i])
229
0
        {
230
0
            return false;
231
0
        }
232
0
    }
233
0
    return true;
234
0
}
235
236
/**
237
 * Guid prefix comparison operator
238
 * @param id1 First EntityId to compare
239
 * @param id2 Second EntityId to compare
240
 * @return True if not equal
241
 */
242
inline bool operator !=(
243
        const EntityId_t& id1,
244
        const EntityId_t& id2)
245
19.2k
{
246
57.7k
    for (uint8_t i = 0; i < 4; ++i)
247
55.0k
    {
248
55.0k
        if (id1.value[i] != id2.value[i])
249
16.5k
        {
250
16.5k
            return true;
251
16.5k
        }
252
55.0k
    }
253
2.71k
    return false;
254
19.2k
}
255
256
#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
257
258
inline std::ostream& operator <<(
259
        std::ostream& output,
260
        const EntityId_t& enI)
261
0
{
262
0
    output << std::hex;
263
0
    output << (int)enI.value[0] << "." << (int)enI.value[1] << "." << (int)enI.value[2] << "." << (int)enI.value[3];
264
0
    return output << std::dec;
265
0
}
266
267
inline std::istream& operator >>(
268
        std::istream& input,
269
        EntityId_t& enP)
270
0
{
271
0
    std::istream::sentry s(input);
272
0
273
0
    if (s)
274
0
    {
275
0
        char point;
276
0
        unsigned short hex;
277
0
        std::ios_base::iostate excp_mask = input.exceptions();
278
0
279
0
        try
280
0
        {
281
0
            input.exceptions(excp_mask | std::ios_base::failbit | std::ios_base::badbit);
282
0
            input >> std::hex >> hex;
283
0
284
0
            if (hex > 255)
285
0
            {
286
0
                input.setstate(std::ios_base::failbit);
287
0
            }
288
0
289
0
            enP.value[0] = static_cast<octet>(hex);
290
0
291
0
            for (int i = 1; i < 4; ++i)
292
0
            {
293
0
                input >> point >> hex;
294
0
                if ( point != '.' || hex > 255 )
295
0
                {
296
0
                    input.setstate(std::ios_base::failbit);
297
0
                }
298
0
                enP.value[i] = static_cast<octet>(hex);
299
0
            }
300
0
301
0
            input >> std::dec;
302
0
        }
303
0
        catch (std::ios_base::failure& )
304
0
        {
305
0
        }
306
0
307
0
        input.exceptions(excp_mask);
308
0
    }
309
0
310
0
    return input;
311
0
}
312
313
const EntityId_t c_EntityId_Unknown = ENTITYID_UNKNOWN;
314
const EntityId_t c_EntityId_SPDPReader = ENTITYID_SPDP_BUILTIN_RTPSParticipant_READER;
315
const EntityId_t c_EntityId_SPDPWriter = ENTITYID_SPDP_BUILTIN_RTPSParticipant_WRITER;
316
317
const EntityId_t c_EntityId_SEDPPubWriter = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_WRITER;
318
const EntityId_t c_EntityId_SEDPPubReader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_READER;
319
const EntityId_t c_EntityId_SEDPSubWriter = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_WRITER;
320
const EntityId_t c_EntityId_SEDPSubReader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_READER;
321
322
const EntityId_t c_EntityId_RTPSParticipant = ENTITYID_RTPSParticipant;
323
324
const EntityId_t c_EntityId_WriterLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_WRITER;
325
const EntityId_t c_EntityId_ReaderLiveliness = ENTITYID_P2P_BUILTIN_RTPSParticipant_MESSAGE_READER;
326
327
const EntityId_t participant_stateless_message_writer_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_WRITER;
328
const EntityId_t participant_stateless_message_reader_entity_id = ENTITYID_P2P_BUILTIN_PARTICIPANT_STATELESS_READER;
329
330
const EntityId_t c_EntityId_TypeLookup_request_writer = ENTITYID_TL_SVC_REQ_WRITER;
331
const EntityId_t c_EntityId_TypeLookup_request_reader = ENTITYID_TL_SVC_REQ_READER;
332
const EntityId_t c_EntityId_TypeLookup_reply_writer = ENTITYID_TL_SVC_REPLY_WRITER;
333
const EntityId_t c_EntityId_TypeLookup_reply_reader = ENTITYID_TL_SVC_REPLY_READER;
334
335
#if HAVE_SECURITY
336
const EntityId_t sedp_builtin_publications_secure_writer = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_WRITER;
337
const EntityId_t sedp_builtin_publications_secure_reader = ENTITYID_SEDP_BUILTIN_PUBLICATIONS_SECURE_READER;
338
const EntityId_t sedp_builtin_subscriptions_secure_writer = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_WRITER;
339
const EntityId_t sedp_builtin_subscriptions_secure_reader = ENTITYID_SEDP_BUILTIN_SUBSCRIPTIONS_SECURE_READER;
340
341
const EntityId_t participant_volatile_message_secure_writer_entity_id =
342
        ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_WRITER;
343
const EntityId_t participant_volatile_message_secure_reader_entity_id =
344
        ENTITYID_P2P_BUILTIN_PARTICIPANT_VOLATILE_MESSAGE_SECURE_READER;
345
346
const EntityId_t c_EntityId_WriterLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_WRITER;
347
const EntityId_t c_EntityId_ReaderLivelinessSecure = ENTITYID_P2P_BUILTIN_PARTICIPANT_MESSAGE_SECURE_READER;
348
#endif // if HAVE_SECURITY
349
350
const EntityId_t ds_server_virtual_writer = ENTITYID_DS_SERVER_VIRTUAL_WRITER;
351
const EntityId_t ds_server_virtual_reader = ENTITYID_DS_SERVER_VIRTUAL_READER;
352
353
} // namespace rtps
354
} // namespace fastrtps
355
} // namespace eprosima
356
357
namespace std {
358
template <>
359
struct hash<eprosima::fastrtps::rtps::EntityId_t>
360
{
361
    std::size_t operator ()(
362
            const eprosima::fastrtps::rtps::EntityId_t& k) const
363
16.5k
    {
364
16.5k
        return (static_cast<size_t>(k.value[0]) << 16) |
365
16.5k
               (static_cast<size_t>(k.value[1]) << 8) |
366
16.5k
               static_cast<size_t>(k.value[2]);
367
16.5k
    }
368
369
};
370
371
} // namespace std
372
373
374
#endif /* _FASTDDS_RTPS_COMMON_ENTITYID_T_HPP_ */