ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
domain_id.c
1 // Copyright 2019 Open Source Robotics Foundation, Inc.
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 #include "rcl/domain_id.h"
16 
17 #include <errno.h>
18 #include <limits.h>
19 
20 #include "rcutils/env.h"
21 
22 #include "rcl/error_handling.h"
23 #include "rcl/types.h"
24 
25 const char * const RCL_DOMAIN_ID_ENV_VAR = "ROS_DOMAIN_ID";
26 
28 rcl_get_default_domain_id(size_t * domain_id)
29 {
30  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_INVALID_ARGUMENT);
31  RCUTILS_CAN_SET_MSG_AND_RETURN_WITH_ERROR_OF(RCL_RET_ERROR);
32 
33  const char * ros_domain_id = NULL;
34  const char * get_env_error_str = NULL;
35 
36  RCL_CHECK_ARGUMENT_FOR_NULL(domain_id, RCL_RET_INVALID_ARGUMENT);
37 
38  get_env_error_str = rcutils_get_env(RCL_DOMAIN_ID_ENV_VAR, &ros_domain_id);
39  if (NULL != get_env_error_str) {
40  RCL_SET_ERROR_MSG_WITH_FORMAT_STRING(
41  "Error getting env var '" RCUTILS_STRINGIFY(RCL_DOMAIN_ID_ENV_VAR) "': %s\n",
42  get_env_error_str);
43  return RCL_RET_ERROR;
44  }
45  if (ros_domain_id && strcmp(ros_domain_id, "") != 0) {
46  char * end = NULL;
47  unsigned long number = strtoul(ros_domain_id, &end, 0); // NOLINT(runtime/int)
48  if (number == 0UL && *end != '\0') {
49  RCL_SET_ERROR_MSG("ROS_DOMAIN_ID is not an integral number");
50  return RCL_RET_ERROR;
51  }
52  if ((number == ULONG_MAX && errno == ERANGE) || number > SIZE_MAX) {
53  RCL_SET_ERROR_MSG("ROS_DOMAIN_ID is out of range");
54  return RCL_RET_ERROR;
55  }
56  *domain_id = (size_t)number;
57  }
58  return RCL_RET_OK;
59 }
RCL_PUBLIC rcl_ret_t rcl_get_default_domain_id(size_t *domain_id)
Determine the default domain ID, based on the environment.
Definition: domain_id.c:28
#define RCL_RET_OK
Success return code.
Definition: types.h:27
#define RCL_RET_INVALID_ARGUMENT
Invalid argument return code.
Definition: types.h:35
#define RCL_RET_ERROR
Unspecified error return code.
Definition: types.h:29
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.
Definition: types.h:24