From df4bc6d3d94c8c33c191210bd7abab0e959566d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Fri, 18 Aug 2017 00:21:06 +0200 Subject: [PATCH] cleaned and documented unix header --- example/main.c | 2 +- include/lwmqtt.h | 10 +++++----- include/lwmqtt/unix.h | 41 ++++++++++++++++++++++++++++++++++++++++- src/os/unix.c | 2 +- tests/client.cpp | 12 ++++++------ 5 files changed, 53 insertions(+), 14 deletions(-) diff --git a/example/main.c b/example/main.c index d225b8b..f1e00e6 100644 --- a/example/main.c +++ b/example/main.c @@ -51,7 +51,7 @@ int main() { while (true) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); if (err != LWMQTT_SUCCESS) { printf("failed lwmqtt_unix_network_peek: %d\n", err); exit(1); diff --git a/include/lwmqtt.h b/include/lwmqtt.h index 7981417..6c3c3bb 100644 --- a/include/lwmqtt.h +++ b/include/lwmqtt.h @@ -85,7 +85,7 @@ typedef struct lwmqtt_client_t lwmqtt_client_t; * The callbacks is expected to read up to the amount of bytes in to the passed buffer. It should block the specified * timeout and wait for more incoming data. */ -typedef lwmqtt_err_t (*lwmqtt_network_read_t)(lwmqtt_client_t *c, void *ref, void *buf, int len, int *read, +typedef lwmqtt_err_t (*lwmqtt_network_read_t)(lwmqtt_client_t *client, void *ref, void *buf, int len, int *read, int timeout); /** @@ -94,18 +94,18 @@ typedef lwmqtt_err_t (*lwmqtt_network_read_t)(lwmqtt_client_t *c, void *ref, voi * The callback is expected to write up to the amount of bytes from the passed buffer. It should wait up to the * specified timeout to write the specified data to the network. */ -typedef lwmqtt_err_t (*lwmqtt_network_write_t)(lwmqtt_client_t *c, void *ref, void *buf, int len, int *sent, +typedef lwmqtt_err_t (*lwmqtt_network_write_t)(lwmqtt_client_t *client, void *ref, void *buf, int len, int *sent, int timeout); /** * The callback used to set a timer. */ -typedef void (*lwmqtt_timer_set_t)(lwmqtt_client_t *c, void *ref, int timeout); +typedef void (*lwmqtt_timer_set_t)(lwmqtt_client_t *client, void *ref, int timeout); /** * The callback used to get a timers value. */ -typedef int (*lwmqtt_timer_get_t)(lwmqtt_client_t *c, void *ref); +typedef int (*lwmqtt_timer_get_t)(lwmqtt_client_t *client, void *ref); /** * The callback used to forward incoming messages. @@ -116,7 +116,7 @@ typedef int (*lwmqtt_timer_get_t)(lwmqtt_client_t *c, void *ref); * recommended to call any further lwmqtt methods in the callback as this might result in weird call stacks. The * callback should place the received messages in a queue and dispatch them after the caller has returned. */ -typedef void (*lwmqtt_callback_t)(lwmqtt_client_t *, void *ref, lwmqtt_string_t *, lwmqtt_message_t *); +typedef void (*lwmqtt_callback_t)(lwmqtt_client_t *client, void *ref, lwmqtt_string_t *str, lwmqtt_message_t *msg); /** * The client object. diff --git a/include/lwmqtt/unix.h b/include/lwmqtt/unix.h index df94ce0..d6cc55c 100644 --- a/include/lwmqtt/unix.h +++ b/include/lwmqtt/unix.h @@ -5,21 +5,60 @@ #include +/** + * The UNIX timer object. + */ typedef struct { struct timeval end; } lwmqtt_unix_timer_t; +/** + * Callback to set the UNIX timer object. + */ void lwmqtt_unix_timer_set(lwmqtt_client_t *client, void *ref, int timeout); +/** + * Callback to read the UNIX timer object. + */ int lwmqtt_unix_timer_get(lwmqtt_client_t *client, void *ref); +/** + * The UNIX network object. + */ typedef struct { int socket; } lwmqtt_unix_network_t; +/** + * Function to establish a UNIX network connection. + * + * @param network - The network object. + * @param host - The host. + * @param port - The port. + * @return An error value. + */ lwmqtt_err_t lwmqtt_unix_network_connect(lwmqtt_unix_network_t *network, char *host, int port); +/** + * Function to disconnect a UNIX network connection. + * + * @param network - The network object. + */ void lwmqtt_unix_network_disconnect(lwmqtt_unix_network_t *network); -lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_client_t *client, lwmqtt_unix_network_t *network, int *available); +/** + * Function to peek available bytes on a UNIX network connection. + * + * @param network - The network object. + * @param available - The available bytes. + * @return An error value. + */ +lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_unix_network_t *network, int *available); +/** + * Callback to read from a UNIX network connection. + */ lwmqtt_err_t lwmqtt_unix_network_read(lwmqtt_client_t *client, void *ref, void *buf, int len, int *read, int timeout); + +/** + * Callback to write to a UNIX network connection. + */ lwmqtt_err_t lwmqtt_unix_network_write(lwmqtt_client_t *client, void *ref, void *buf, int len, int *sent, int timeout); #endif // LWMQTT_UNIX_H diff --git a/src/os/unix.c b/src/os/unix.c index c60098d..a3eadbd 100644 --- a/src/os/unix.c +++ b/src/os/unix.c @@ -111,7 +111,7 @@ void lwmqtt_unix_network_disconnect(lwmqtt_unix_network_t *network) { } } -lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_client_t *client, lwmqtt_unix_network_t *network, int *available) { +lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_unix_network_t *network, int *available) { // get the available bytes on the socket int rc = ioctl(network->socket, FIONREAD, available); if (rc < 0) { diff --git a/tests/client.cpp b/tests/client.cpp index 5187b31..64c22f1 100644 --- a/tests/client.cpp +++ b/tests/client.cpp @@ -82,7 +82,7 @@ TEST(Client, PublishSubscribeQOS0) { while (counter < 5) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); ASSERT_EQ(err, LWMQTT_SUCCESS); if (available > 0) { @@ -141,7 +141,7 @@ TEST(Client, PublishSubscribeQOS1) { while (counter < 5) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); ASSERT_EQ(err, LWMQTT_SUCCESS); if (available > 0) { @@ -200,7 +200,7 @@ TEST(Client, PublishSubscribeQOS2) { while (counter < 5) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); ASSERT_EQ(err, LWMQTT_SUCCESS); if (available > 0) { @@ -257,7 +257,7 @@ TEST(Client, BufferOverflowProtection) { while (counter < 1) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); ASSERT_EQ(err, LWMQTT_SUCCESS); if (available > 0) { @@ -314,7 +314,7 @@ TEST(Client, BigBuffersAndPayload) { while (counter < 5) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); ASSERT_EQ(err, LWMQTT_SUCCESS); if (available > 0) { @@ -376,7 +376,7 @@ TEST(Client, MultipleSubscriptions) { while (counter < 5) { int available = 0; - err = lwmqtt_unix_network_peek(&client, &network, &available); + err = lwmqtt_unix_network_peek(&network, &available); ASSERT_EQ(err, LWMQTT_SUCCESS); if (available > 0) {