Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added init with options #32

Merged
merged 4 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rclc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ The rclc package also provides a number of convenience functions, which make it

Convenience functions:
- rclc_support_init()
- rclc_support_init_with_options()
- rclc_support_fini()
- rclc_node_init_default()
- rclc_publisher_init_default()
Expand Down
28 changes: 28 additions & 0 deletions rclc/include/rclc/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,34 @@ rclc_support_init(
char const * const * argv,
rcl_allocator_t * allocator);

/**
* Initializes rcl and creates some support data structures.
* Initializes clock as RCL_STEADY_TIME.
* * <hr>
* Attribute | Adherence
* ------------------ | -------------
* Allocates Memory | Yes (in RCL)
* Thread-Safe | No
* Uses Atomics | No
* Lock-Free | Yes
*
* \param[inout] support a zero-initialized rclc_support_t
* \param[in] argc number of args of main
* \param[in] argv array of arguments of main
* \param[in] init_options custom initial options
* \param[in] allocator allocator for allocating memory
* \return `RCL_RET_OK` if RCL was initialized successfully
* \return `RCL_RET_INVALID_ARGUMENT` if any null pointer as argument
* \return `RCL_RET_ERROR` in case of failure
*/
rcl_ret_t
rclc_support_init_with_options(
rclc_support_t * support,
int argc,
char const * const * argv,
rcl_init_options_t * init_options,
rcl_allocator_t * allocator);


/**
* De-allocates the rclc_support_t object.
Expand Down
31 changes: 25 additions & 6 deletions rclc/src/rclc/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,38 @@ rclc_support_init(
char const * const * argv,
rcl_allocator_t * allocator)
{
RCL_CHECK_FOR_NULL_WITH_MSG(
support, "support is a null pointer", return RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_FOR_NULL_WITH_MSG(
allocator, "allocator is a null pointer", return RCL_RET_INVALID_ARGUMENT);
rcl_ret_t rc = RCL_RET_OK;

support->init_options = rcl_get_zero_initialized_init_options();
rc = rcl_init_options_init(&support->init_options, (*allocator) );
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();
rc = rcl_init_options_init(&init_options, (*allocator) );
if (rc != RCL_RET_OK) {
PRINT_RCLC_ERROR(rclc_support_init, rcl_init_options_init);
return rc;
}

rc = rclc_support_init_with_options(support,argc, argv, &init_options, allocator);

return rc;
}

rcl_ret_t
rclc_support_init_with_options(
rclc_support_t * support,
int argc,
char const * const * argv,
rcl_init_options_t * init_options,
rcl_allocator_t * allocator)
{
RCL_CHECK_FOR_NULL_WITH_MSG(
support, "support is a null pointer", return RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_FOR_NULL_WITH_MSG(
init_options, "init_options is a null pointer", return RCL_RET_INVALID_ARGUMENT);
RCL_CHECK_FOR_NULL_WITH_MSG(
allocator, "allocator is a null pointer", return RCL_RET_INVALID_ARGUMENT);
rcl_ret_t rc = RCL_RET_OK;

memcpy(&support->init_options, init_options, sizeof(rcl_init_options_t));

support->context = rcl_get_zero_initialized_context();
rc = rcl_init(argc, argv, &support->init_options, &support->context);
if (rc != RCL_RET_OK) {
Expand Down
29 changes: 29 additions & 0 deletions rclc/test/rclc/test_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,35 @@ TEST(Test, rclc_support_init) {
rcutils_reset_error();
}

TEST(Test, rclc_support_init_with_options) {
rclc_support_t support;
rcl_ret_t rc;
rcl_allocator_t allocator = rcl_get_default_allocator();
rcl_init_options_t init_options = rcl_get_zero_initialized_init_options();

rc = rcl_init_options_init(&init_options, allocator);
EXPECT_EQ(RCL_RET_OK, rc);

rc = rclc_support_init_with_options(&support, 0, nullptr, &init_options, &allocator);
EXPECT_EQ(RCL_RET_OK, rc);
// after rcl_init context should be valid
ASSERT_TRUE(rcl_context_is_valid(&support.context));
EXPECT_EQ(&allocator, support.allocator);
EXPECT_TRUE(rcl_clock_valid(&support.clock));
rc = rclc_support_fini(&support);
EXPECT_EQ(RCL_RET_OK, rc);
// test invalid arguments
rc = rclc_support_init_with_options(nullptr, 0, nullptr, &init_options, &allocator);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rc);
rcutils_reset_error();
rc = rclc_support_init_with_options(&support, 0, nullptr, &init_options, nullptr);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rc);
rcutils_reset_error();
rc = rclc_support_init_with_options(&support, 0, nullptr, nullptr, &allocator);
EXPECT_EQ(RCL_RET_INVALID_ARGUMENT, rc);
rcutils_reset_error();
}


TEST(Test, rclc_support_fini) {
rclc_support_t support;
Expand Down