You can find a more detailled description of the C API in the iceoryx_binding_c README.md
The behavior and structure is identical to the icedelivery C++ example so that we explain here only the C API differences and not the underlying mechanisms.
Like in the icedelivery C++ example we again follow the steps like:
- Create runtime instance.
- Create subscriber port.
- Subscribe to the offered service
- Receive data
- Unsubscribe.
- C API: Additionally, we have to remove the previously allocated Subscriber port!
Let's take a look at the receiving
function which comes with the
ice_c_subscriber.c
example.
-
We register our process at roudi with the name
iox-c-subscriber
iox_runtime_init("iox-c-subscriber");
-
We create a subscriber port and are subscribing to the service {"Radar", "FrontLeft", "Counter" }. Hereby the
historyRequest
tells the subscriber how many previously send samples it should receive right after the connection is established and thequeueCapacity
how many samples the subscriber can hold. These are samples which the publisher has send before the subscriber was connected. ThesubscriberStorage
is the place where the subscriber is stored in memory andsubscriber
is actually a pointer to that location.const uint64_t historyRequest = 10U; const uint64_t queueCapacity = 5U; iox_sub_storage_t subscriberStorage; iox_sub_t subscriber = iox_sub_init(&subscriberStorage, "Radar", "FrontLeft", "Object", queueCapacity, historyRequest);
-
We subscribe to the service.
iox_sub_subscribe(subscriber);
-
In this loop we receive samples as long the
killswitch
is not set totrue
by an external signal and then print the counter value to the console.while (!killswitch) { if (SubscribeState_SUBSCRIBED == iox_sub_get_subscription_state(subscriber)) { const void* chunk = NULL; while (ChunkReceiveResult_SUCCESS == iox_sub_get_chunk(subscriber, &chunk)) { const struct RadarObject* sample = (const struct RadarObject*)(chunk); printf("Got value: %.0f\n", sample->x); iox_sub_release_chunk(subscriber, chunk); } } else { printf("Not subscribed!\n"); } sleep_for(1000); }
-
After we stop receiving samples we would like to unsubscribe.
iox_sub_unsubscribe(subscriber);
-
When using the C API we have to cleanup the subscriber after its usage.
iox_sub_deinit(subscriber);
The publisher is implemented in a way like in the icedelivery C++ example.
- Create runtime instance.
- Create publisher port.
- Offer the service
- Send data
- Stop offering the service
- C API: Additionally, we have to remove the previously allocated Publisher port!
Let's take a look at the sending
function which comes with the
ice_c_publisher.c
example.
-
We register our process at roudi with the name
iox-c-subscriber
iox_runtime_init("iox-c-publisher");
-
We create a publisher with the service {"Radar", "FrontLeft", "Counter"}
const uint64_t historyRequest = 10U; iox_pub_storage_t publisherStorage; iox_pub_t publisher = iox_pub_init(&publisherStorage, "Radar", "FrontLeft", "Object", historyRequest);
-
We offer our service to the world.
iox_pub_offer(publisher);
-
Till an external signal sets
killswitch
totrue
we will send an incrementing number to all subscribers every send and print the value of this number to the console.double ct = 0.0; while (!killswitch) { void* chunk = NULL; if (AllocationResult_SUCCESS == iox_pub_allocate_chunk(publisher, &chunk, sizeof(struct RadarObject))) { struct RadarObject* sample = (struct RadarObject*)chunk; sample->x = ct; sample->y = ct; sample->z = ct; printf("Sent value: %.0f\n", ct); iox_pub_send_chunk(publisher, chunk); ++ct; sleep_for(400); } else { printf("Failed to allocate chunk!"); } }
-
We stop offering our service.
iox_pub_stop_offer(publisher);
-
And we cleanup our publisher port.
iox_pub_destroy(publisher);