Skip to content

Commit

Permalink
iox-eclipse-iceoryx#341 adjusted c binding to new API
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Eltzschig <me@elchris.org>
  • Loading branch information
elfenpiff committed Dec 17, 2020
1 parent 8effbef commit 3f41e1e
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ struct cpp2c_Subscriber
cpp2c_Subscriber& operator=(const cpp2c_Subscriber&) = delete;
cpp2c_Subscriber& operator=(cpp2c_Subscriber&& rhs) = delete;

iox_WaitSetResult enableEvent(iox::popo::WaitSet<>& waitset,
const uint64_t eventId,
const iox::popo::EventInfo::Callback<cpp2c_Subscriber> callback,
const iox_SubscriberEvent event) noexcept;
iox::cxx::expected<iox::popo::WaitSetError>
enableEvent(iox::popo::WaitSet<>& waitset,
const iox_SubscriberEvent event,
const uint64_t eventId,
const iox::popo::EventInfo::Callback<cpp2c_Subscriber> callback) noexcept;

void disableEvent(const iox_SubscriberEvent event) noexcept;

Expand Down
21 changes: 0 additions & 21 deletions iceoryx_binding_c/include/iceoryx_binding_c/subscriber.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,4 @@ bool iox_sub_has_chunks(iox_sub_t const self);
/// @return true if there are lost chunks otherwise false
bool iox_sub_has_lost_chunks(iox_sub_t const self);

/// @brief enables an event of the subscriber by attaching it to a waitset
/// @param[in] self handle to the subscriber
/// @param[in] waitset handle to the waitset
/// @param[in] event the type of the event which should be attached to the waitset
/// @param[in] eventId the user defined trigger id
/// @param[in] callback a callback which is attached to the trigger, NULL if no
/// callback should be set
/// @return if it was attached successfully it returns WaitSetResult_SUCCESS
/// otherwise an enum which is describing the error
ENUM iox_WaitSetResult iox_sub_enable_event(iox_sub_t const self,
iox_ws_t const waitset,
const ENUM iox_SubscriberEvent event,
const uint64_t eventId,
void (*callback)(iox_sub_t));

/// @brief disables an event of the subscriber
/// @param[in] self handle to the subscriber
/// @param[in] event the type of the event which should be detached
void iox_sub_disable_event(iox_sub_t const self, const ENUM iox_SubscriberEvent event);


#endif
17 changes: 0 additions & 17 deletions iceoryx_binding_c/include/iceoryx_binding_c/user_trigger.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,6 @@ iox_user_trigger_t iox_user_trigger_init(iox_user_trigger_storage_t* self);
/// @param[in] self handle to user trigger
void iox_user_trigger_deinit(iox_user_trigger_t const self);

/// @brief enables the trigger event by attaching the user trigger to a waitset
/// @param[in] self handle to the user trigger
/// @param[in] wait_set handle to the wait set
/// @param[in] trigger_id any arbitrary number which is used as trigger id
/// @param[in] trigger_callback a callback which is assigned to the trigger, NULL if no
/// callback should be set
/// @return if it was attached successfully it returns WaitSetResult_SUCCESS
/// otherwise an enum which is describing the error
ENUM iox_WaitSetResult iox_user_trigger_enable_trigger_event(iox_user_trigger_t const self,
iox_ws_t const wait_set,
const uint64_t trigger_id,
void (*trigger_callback)(iox_user_trigger_t));

/// @brief disables the trigger event
/// @param[in] self handle to the user trigger
void iox_user_trigger_disable_trigger_event(iox_user_trigger_t const self);

/// @brief trigger a user trigger
/// @param[in] self handle to user trigger
void iox_user_trigger_trigger(iox_user_trigger_t const self);
Expand Down
39 changes: 39 additions & 0 deletions iceoryx_binding_c/include/iceoryx_binding_c/wait_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,43 @@ uint64_t iox_ws_size(iox_ws_t const self);
/// @brief returns the maximum amount of events which can be registered at the waitset
uint64_t iox_ws_capacity(iox_ws_t const self);

/// @brief attaches a subscriber event to a waitset
/// @param[in] self handle to the waitset
/// @param[in] subscriber the subscriber of the event which should be attached
/// @param[in] subscriberEvent the event which should be attached
/// @param[in] eventId an arbitrary id which will be tagged to the event
/// @param[in] callback a callback which is attached to the event
/// @return if the attaching was successfull it returns WaitSetResult_SUCCESS, otherwise
/// an enum which describes the error
ENUM iox_WaitSetResult iox_ws_attach_subscriber_event(iox_ws_t const self,
iox_sub_t const subscriber,
const ENUM iox_SubscriberEvent subscriberEvent,
const uint64_t eventId,
void (*callback)(iox_sub_t));

/// @brief attaches a user trigger event to a waitset
/// @param[in] self handle to the waitset
/// @param[in] userTrigger the user trigger of the event which should be attached
/// @param[in] eventId an arbitrary id which will be tagged to the event
/// @param[in] callback a callback which is attached to the event
/// @return if the attaching was successfull it returns WaitSetResult_SUCCESS, otherwise
/// an enum which describes the error
ENUM iox_WaitSetResult iox_ws_attach_user_trigger_event(iox_ws_t const self,
iox_user_trigger_t const userTrigger,
const uint64_t eventId,
void (*callback)(iox_user_trigger_t));

/// @brief detaches a subscriber event from a waitset
/// @param[in] self handle to the waitset
/// @param[in] subscriber the subscriber from which the event should be detached
/// @param[in] subscriberEvent the event which should be detached from the subscriber
void iox_ws_detach_subscriber_event(iox_ws_t const self,
iox_sub_t const subscriber,
const ENUM iox_SubscriberEvent subscriberEvent);

/// @brief detaches a user trigger event from a waitset
/// @param[in] self handle to the waitset
/// @param[in] usertrigger the user trigger which should be detached
void iox_ws_detach_user_trigger_event(iox_ws_t const self, iox_user_trigger_t const userTrigger);

#endif
15 changes: 0 additions & 15 deletions iceoryx_binding_c/source/c_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,3 @@ bool iox_sub_has_lost_chunks(iox_sub_t const self)
{
return SubscriberPortUser(self->m_portData).hasLostChunksSinceLastCall();
}

iox_WaitSetResult iox_sub_enable_event(iox_sub_t const self,
iox_ws_t const waitset,
const iox_SubscriberEvent event,
const uint64_t eventId,
void (*callback)(iox_sub_t))
{
return self->enableEvent(*waitset, eventId, callback, event);
}

void iox_sub_disable_event(iox_sub_t const self, const iox_SubscriberEvent event)
{
return self->disableEvent(event);
}

19 changes: 0 additions & 19 deletions iceoryx_binding_c/source/c_user_trigger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,3 @@ void iox_user_trigger_reset_trigger(iox_user_trigger_t const self)
self->resetTrigger();
}

iox_WaitSetResult iox_user_trigger_enable_trigger_event(iox_user_trigger_t const self,
iox_ws_t const wait_set,
const uint64_t trigger_id,
void (*trigger_callback)(iox_user_trigger_t))
{
auto result = self->enableEvent(*wait_set, trigger_id, trigger_callback);
if (!result.has_error())
{
return iox_WaitSetResult::WaitSetResult_SUCCESS;
}

return cpp2c::WaitSetResult(result.get_error());
}

void iox_user_trigger_disable_trigger_event(iox_user_trigger_t const self)
{
self->disableEvent();
}

34 changes: 34 additions & 0 deletions iceoryx_binding_c/source/c_wait_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
// limitations under the License.

#include "iceoryx_binding_c/internal/cpp2c_enum_translation.hpp"
#include "iceoryx_binding_c/internal/cpp2c_subscriber.hpp"
#include "iceoryx_binding_c/internal/cpp2c_waitset.hpp"
#include "iceoryx_posh/popo/user_trigger.hpp"
#include "iceoryx_posh/popo/wait_set.hpp"

using namespace iox;
Expand Down Expand Up @@ -91,3 +93,35 @@ uint64_t iox_ws_capacity(iox_ws_t const self)
{
return self->capacity();
}

iox_WaitSetResult iox_ws_attach_subscriber_event(iox_ws_t const self,
iox_sub_t const subscriber,
const iox_SubscriberEvent subscriberEvent,
const uint64_t eventId,
void (*callback)(iox_sub_t))
{
auto result = self->attachEvent(*subscriber, subscriberEvent, eventId, callback);
return (result.has_error()) ? cpp2c::WaitSetResult(result.get_error()) : iox_WaitSetResult::WaitSetResult_SUCCESS;
}

iox_WaitSetResult iox_ws_attach_user_trigger_event(iox_ws_t const self,
iox_user_trigger_t const userTrigger,
const uint64_t eventId,
void (*callback)(iox_user_trigger_t))
{
auto result = self->attachEvent(*userTrigger, eventId, callback);
return (result.has_error()) ? cpp2c::WaitSetResult(result.get_error()) : iox_WaitSetResult::WaitSetResult_SUCCESS;
}

void iox_ws_detach_subscriber_event(iox_ws_t const self,
iox_sub_t const subscriber,
const iox_SubscriberEvent subscriberEvent)
{
self->detachEvent(*subscriber, subscriberEvent);
}

void iox_ws_detach_user_trigger_event(iox_ws_t const self, iox_user_trigger_t const userTrigger)
{
self->detachEvent(*userTrigger);
}

32 changes: 15 additions & 17 deletions iceoryx_binding_c/source/cpp2c_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,24 @@ cpp2c_Subscriber::~cpp2c_Subscriber()
}
}

iox_WaitSetResult cpp2c_Subscriber::enableEvent(iox::popo::WaitSet<>& waitset,
const uint64_t eventId,
const iox::popo::EventInfo::Callback<cpp2c_Subscriber> callback,
const iox_SubscriberEvent subscriberEvent) noexcept
iox::cxx::expected<iox::popo::WaitSetError>
cpp2c_Subscriber::enableEvent(iox::popo::WaitSet<>& waitset,
const iox_SubscriberEvent subscriberEvent,
const uint64_t eventId,
const iox::popo::EventInfo::Callback<cpp2c_Subscriber> callback) noexcept
{
static_cast<void>(subscriberEvent);

auto result = std::move(
waitset
.acquireTriggerHandle(this,
{*this, &cpp2c_Subscriber::hasNewSamples},
{*this, &cpp2c_Subscriber::invalidateTrigger},
eventId,
callback)
.and_then([this](iox::popo::TriggerHandle& trigger) {
m_trigger = std::move(trigger);
iox::popo::SubscriberPortUser(m_portData).setConditionVariable(m_trigger.getConditionVariableData());
}));

return (result.has_error()) ? cpp2c::WaitSetResult(result.get_error()) : iox_WaitSetResult::WaitSetResult_SUCCESS;
return waitset
.acquireTriggerHandle(this,
{*this, &cpp2c_Subscriber::hasNewSamples},
{*this, &cpp2c_Subscriber::invalidateTrigger},
eventId,
callback)
.and_then([this](iox::popo::TriggerHandle& trigger) {
m_trigger = std::move(trigger);
iox::popo::SubscriberPortUser(m_portData).setConditionVariable(m_trigger.getConditionVariableData());
});
}

void cpp2c_Subscriber::disableEvent(const iox_SubscriberEvent subscriberEvent) noexcept
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ using namespace iox::posix;
extern "C" {
#include "iceoryx_binding_c/event_info.h"
#include "iceoryx_binding_c/subscriber.h"
#include "iceoryx_binding_c/wait_set.h"
}

#include "test.hpp"
Expand Down Expand Up @@ -98,7 +99,7 @@ UserTrigger* iox_event_info_test::m_lastTriggerCallbackArgument = nullptr;
TEST_F(iox_event_info_test, triggerStateHasCorrectId)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 123;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, nullptr);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, nullptr);
m_userTrigger.trigger();

auto triggerStateVector = m_waitSet.wait();
Expand All @@ -110,7 +111,7 @@ TEST_F(iox_event_info_test, triggerStateHasCorrectId)
TEST_F(iox_event_info_test, triggerOriginIsUserTriggerPointerWhenItsOriginatingFromThem)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 124;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, nullptr);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, nullptr);
m_userTrigger.trigger();

auto triggerStateVector = m_waitSet.wait();
Expand All @@ -121,7 +122,7 @@ TEST_F(iox_event_info_test, triggerOriginIsUserTriggerPointerWhenItsOriginatingF
TEST_F(iox_event_info_test, triggerOriginIsNotUserTriggerPointerWhenItsNotOriginatingFromThem)
{
constexpr uint64_t CHUNK_SIZE = 100;
iox_sub_enable_event(m_subscriberHandle, &m_waitSet, SubscriberEvent_HAS_SAMPLES, 587, NULL);
iox_ws_attach_subscriber_event(&m_waitSet, m_subscriberHandle, SubscriberEvent_HAS_SAMPLES, 587, NULL);
this->Subscribe(&m_portPtr);
m_chunkPusher.tryPush(m_memoryManager.getChunk(CHUNK_SIZE));

Expand All @@ -133,7 +134,7 @@ TEST_F(iox_event_info_test, triggerOriginIsNotUserTriggerPointerWhenItsNotOrigin
TEST_F(iox_event_info_test, triggerOriginIsSubscriberPointerWhenItsOriginatingFromThem)
{
constexpr uint64_t CHUNK_SIZE = 100;
iox_sub_enable_event(m_subscriberHandle, &m_waitSet, SubscriberEvent_HAS_SAMPLES, 587, NULL);
iox_ws_attach_subscriber_event(&m_waitSet, m_subscriberHandle, SubscriberEvent_HAS_SAMPLES, 587, NULL);
this->Subscribe(&m_portPtr);
m_chunkPusher.tryPush(m_memoryManager.getChunk(CHUNK_SIZE));

Expand All @@ -145,7 +146,7 @@ TEST_F(iox_event_info_test, triggerOriginIsSubscriberPointerWhenItsOriginatingFr
TEST_F(iox_event_info_test, triggerOriginIsNotSubscriberPointerWhenItsOriginatingFromThem)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 8921;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, nullptr);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, nullptr);
m_userTrigger.trigger();

auto triggerStateVector = m_waitSet.wait();
Expand All @@ -157,7 +158,7 @@ TEST_F(iox_event_info_test, triggerOriginIsNotSubscriberPointerWhenItsOriginatin
TEST_F(iox_event_info_test, getOriginReturnsPointerToUserTriggerWhenOriginatingFromThem)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 89121;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, nullptr);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, nullptr);
m_userTrigger.trigger();

auto triggerStateVector = m_waitSet.wait();
Expand All @@ -168,7 +169,7 @@ TEST_F(iox_event_info_test, getOriginReturnsPointerToUserTriggerWhenOriginatingF
TEST_F(iox_event_info_test, getOriginReturnsNullptrUserTriggerWhenNotOriginatingFromThem)
{
constexpr uint64_t CHUNK_SIZE = 100;
iox_sub_enable_event(m_subscriberHandle, &m_waitSet, SubscriberEvent_HAS_SAMPLES, 587, NULL);
iox_ws_attach_subscriber_event(&m_waitSet, m_subscriberHandle, SubscriberEvent_HAS_SAMPLES, 587, NULL);
this->Subscribe(&m_portPtr);
m_chunkPusher.tryPush(m_memoryManager.getChunk(CHUNK_SIZE));

Expand All @@ -181,7 +182,7 @@ TEST_F(iox_event_info_test, getOriginReturnsNullptrUserTriggerWhenNotOriginating
TEST_F(iox_event_info_test, getOriginReturnsPointerToSubscriberWhenOriginatingFromThem)
{
constexpr uint64_t CHUNK_SIZE = 100;
iox_sub_enable_event(m_subscriberHandle, &m_waitSet, SubscriberEvent_HAS_SAMPLES, 587, NULL);
iox_ws_attach_subscriber_event(&m_waitSet, m_subscriberHandle, SubscriberEvent_HAS_SAMPLES, 587, NULL);
this->Subscribe(&m_portPtr);
m_chunkPusher.tryPush(m_memoryManager.getChunk(CHUNK_SIZE));

Expand All @@ -193,7 +194,7 @@ TEST_F(iox_event_info_test, getOriginReturnsPointerToSubscriberWhenOriginatingFr
TEST_F(iox_event_info_test, getOriginReturnsNullptrSubscriberWhenNotOriginatingFromThem)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 891121;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, iox_event_info_test::triggerCallback);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, iox_event_info_test::triggerCallback);
m_userTrigger.trigger();

auto triggerStateVector = m_waitSet.wait();
Expand All @@ -204,7 +205,7 @@ TEST_F(iox_event_info_test, getOriginReturnsNullptrSubscriberWhenNotOriginatingF
TEST_F(iox_event_info_test, callbackCanBeCalledOnce)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 80;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, iox_event_info_test::triggerCallback);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, iox_event_info_test::triggerCallback);
m_userTrigger.trigger();

auto triggerStateVector = m_waitSet.wait();
Expand All @@ -216,7 +217,7 @@ TEST_F(iox_event_info_test, callbackCanBeCalledOnce)
TEST_F(iox_event_info_test, callbackCanBeCalledMultipleTimes)
{
constexpr uint64_t ARBITRARY_TRIGGER_ID = 180;
m_userTrigger.enableEvent(m_waitSet, ARBITRARY_TRIGGER_ID, iox_event_info_test::triggerCallback);
m_waitSet.attachEvent(m_userTrigger, ARBITRARY_TRIGGER_ID, iox_event_info_test::triggerCallback);
m_userTrigger.trigger();
auto triggerStateVector = m_waitSet.wait();

Expand Down
Loading

0 comments on commit 3f41e1e

Please sign in to comment.