diff --git a/rcl/CMakeLists.txt b/rcl/CMakeLists.txt index 8100686..c3168dc 100644 --- a/rcl/CMakeLists.txt +++ b/rcl/CMakeLists.txt @@ -24,11 +24,12 @@ set(${PROJECT_NAME}_sources src/rcl/node.c src/rcl/publisher.c src/rcl/rcl.c + src/rcl/rmw_implementation_identifier_check.c src/rcl/service.c src/rcl/subscription.c - src/rcl/wait.c src/rcl/time.c src/rcl/timer.c + src/rcl/wait.c ) macro(target) diff --git a/rcl/include/rcl/types.h b/rcl/include/rcl/types.h index 34877f3..1065e01 100644 --- a/rcl/include/rcl/types.h +++ b/rcl/include/rcl/types.h @@ -26,6 +26,7 @@ typedef rmw_ret_t rcl_ret_t; #define RCL_RET_NOT_INIT 101 #define RCL_RET_BAD_ALLOC 102 #define RCL_RET_INVALID_ARGUMENT 103 +#define RCL_RET_MISMATCHED_RMW_ID 104 // rcl node specific ret codes in 2XX #define RCL_RET_NODE_INVALID 200 // rcl publisher specific ret codes in 3XX diff --git a/rcl/src/rcl/rmw_implementation_identifier_check.c b/rcl/src/rcl/rmw_implementation_identifier_check.c new file mode 100644 index 0000000..de4229a --- /dev/null +++ b/rcl/src/rcl/rmw_implementation_identifier_check.c @@ -0,0 +1,77 @@ +// Copyright 2016 Open Source Robotics Foundation, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#if __cplusplus +extern "C" +{ +#endif + +#include +#include +#include + +#include + +#include "rcl/types.h" +#include "./common.h" + +// Extracted this portable method of doing a "shared library constructor" from SO: +// http://stackoverflow.com/a/2390626/671658 +// Initializer/finalizer sample for MSVC and GCC/Clang. +// 2010-2016 Joe Lowe. Released into the public domain. +#if defined(_MSC_VER) + #pragma section(".CRT$XCU", read) + #define INITIALIZER2_(f, p) \ + static void f(void); \ + __declspec(allocate(".CRT$XCU")) void(*f ## _)(void) = f; \ + __pragma(comment(linker, "/include:" p #f "_")) \ + static void f(void) + #ifdef _WIN64 + #define INITIALIZER(f) INITIALIZER2_(f, "") + #else + #define INITIALIZER(f) INITIALIZER2_(f, "_") + #endif +#else + #define INITIALIZER(f) \ + static void f(void) __attribute__((constructor)); \ + static void f(void) +#endif + +INITIALIZER(initialize) { + // If the environement variable RCL_ASSERT_RMW_ID_MATCHES is set, + // check that the result of `rmw_get_implementation_identifier` matches. + const char * expected = NULL; + rcl_ret_t ret = rcl_impl_getenv("RCL_ASSERT_RMW_ID_MATCHES", &expected); + if (ret != RCL_RET_OK) { + fprintf(stderr, + "Error getting environement variable 'RCL_ASSERT_RMW_ID_MATCHES': %s\n", + rcl_get_error_string_safe() + ); + exit(ret); + } + // If the environment variable is set, and it does not match, print a warning and exit. + if (strlen(expected) > 0 && strcmp(rmw_get_implementation_identifier(), expected) != 0) { + fprintf(stderr, + "Expected RMW implementation identifier of '%s' but instead found '%s', exiting with %d.\n", + expected, + rmw_get_implementation_identifier(), + RCL_RET_MISMATCHED_RMW_ID + ); + exit(RCL_RET_MISMATCHED_RMW_ID); + } +} + +#if __cplusplus +} +#endif