S Morita / mbed-mros2

Dependents:   mbed-os-example-mros2 example-mbed-mros2-sub-pose example-mbed-mros2-pub-twist example-mbed-mros2-mturtle-teleop

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Locator.h Source File

Locator.h

00001 /*
00002 The MIT License
00003 Copyright (c) 2019 Lehrstuhl Informatik 11 - RWTH Aachen University
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 The above copyright notice and this permission notice shall be included in
00011 all copies or substantial portions of the Software.
00012 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00013 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00014 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00015 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00016 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00018 THE SOFTWARE
00019 
00020 This file is part of embeddedRTPS.
00021 
00022 Author: i11 - Embedded Software, RWTH Aachen University
00023 */
00024 
00025 #ifndef RTPS_LOCATOR_T_H
00026 #define RTPS_LOCATOR_T_H
00027 
00028 #include "rtps/communication/UdpDriver.h"
00029 #include "rtps/utils/udpUtils.h"
00030 #include "ucdr/microcdr.h"
00031 
00032 #include <array>
00033 
00034 namespace rtps {
00035 enum class LocatorKind_t : int32_t {
00036   LOCATOR_KIND_INVALID = -1,
00037   LOCATOR_KIND_RESERVED = 0,
00038   LOCATOR_KIND_UDPv4 = 1,
00039   LOCATOR_KIND_UDPv6 = 2
00040 };
00041 
00042 const uint32_t LOCATOR_PORT_INVALID = 0;
00043 const std::array<uint8_t, 16> LOCATOR_ADDRESS_INVALID = {
00044     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
00045 
00046 struct Locator {
00047   LocatorKind_t kind = LocatorKind_t::LOCATOR_KIND_INVALID;
00048   uint32_t port = LOCATOR_PORT_INVALID;
00049   std::array<uint8_t, 16> address =
00050       LOCATOR_ADDRESS_INVALID; // TODO make private such that kind and address
00051                                // always match?
00052 
00053   static Locator createUDPv4Locator(uint8_t a, uint8_t b, uint8_t c, uint8_t d,
00054                                     uint32_t port) {
00055     Locator locator;
00056     locator.kind = LocatorKind_t::LOCATOR_KIND_UDPv4;
00057     locator.address = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, a, b, c, d};
00058     locator.port = port;
00059     return locator;
00060   }
00061 
00062   void setInvalid() { kind = LocatorKind_t::LOCATOR_KIND_INVALID; }
00063 
00064   bool isValid() const { return kind != LocatorKind_t::LOCATOR_KIND_INVALID; }
00065 
00066   bool readFromUcdrBuffer(ucdrBuffer &buffer) {
00067     if (ucdr_buffer_remaining(&buffer) < sizeof(Locator)) {
00068       return false;
00069     } else {
00070       ucdr_deserialize_array_uint8_t(&buffer, reinterpret_cast<uint8_t *>(this),
00071                                      sizeof(Locator));
00072       return true;
00073     }
00074   }
00075 
00076   bool serializeIntoUdcrBuffer(ucdrBuffer &buffer) {
00077     if (ucdr_buffer_remaining(&buffer) < sizeof(Locator)) {
00078       return false;
00079     } else {
00080       ucdr_serialize_array_uint8_t(&buffer, reinterpret_cast<uint8_t *>(this),
00081                                    sizeof(Locator));
00082     }
00083     return true;
00084   }
00085 
00086   ip4_addr_t getIp4Address() const {
00087     return transformIP4ToU32(address[12], address[13], address[14],
00088                              address[15]);
00089   }
00090 
00091   inline bool isSameSubnet() const {
00092     return UdpDriver::isSameSubnet(getIp4Address());
00093   }
00094 
00095 } __attribute__((packed));
00096 
00097 inline Locator getBuiltInUnicastLocator(ParticipantId_t participantId) {
00098   return Locator::createUDPv4Locator(
00099       Config::IP_ADDRESS[0], Config::IP_ADDRESS[1], Config::IP_ADDRESS[2],
00100       Config::IP_ADDRESS[3], getBuiltInUnicastPort(participantId));
00101 }
00102 
00103 inline Locator getBuiltInMulticastLocator() {
00104   return Locator::createUDPv4Locator(239, 255, 0, 1, getBuiltInMulticastPort());
00105 }
00106 
00107 inline Locator getUserUnicastLocator(ParticipantId_t participantId) {
00108   return Locator::createUDPv4Locator(
00109       Config::IP_ADDRESS[0], Config::IP_ADDRESS[1], Config::IP_ADDRESS[2],
00110       Config::IP_ADDRESS[3], getUserUnicastPort(participantId));
00111 }
00112 
00113 inline Locator getUserMulticastLocator() {
00114   return Locator::createUDPv4Locator(
00115       Config::IP_ADDRESS[0], Config::IP_ADDRESS[1], Config::IP_ADDRESS[2],
00116       Config::IP_ADDRESS[3], getUserMulticastPort());
00117 }
00118 
00119 inline Locator getDefaultSendMulticastLocator() {
00120   return Locator::createUDPv4Locator(239, 255, 0, 1, getBuiltInMulticastPort());
00121 }
00122 } // namespace rtps
00123 
00124 #endif // RTPS_LOCATOR_T_H