Skip to content

Commit

Permalink
removed mandatory client parameter for callbacks
Browse files Browse the repository at this point in the history
if needed it can be supplied as part of the custom `void *ref` parameter
  • Loading branch information
256dpi committed Oct 18, 2017
1 parent bcf1525 commit b6f1c81
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 39 deletions.
6 changes: 3 additions & 3 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int main() {
lwmqtt_set_timers(&client, &timer1, &timer2, lwmqtt_unix_timer_set, lwmqtt_unix_timer_get);
lwmqtt_set_callback(&client, NULL, message_arrived);

lwmqtt_unix_timer_set(NULL, &timer3, MESSAGE_TIMEOUT);
lwmqtt_unix_timer_set(&timer3, MESSAGE_TIMEOUT);

lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, "broker.shiftr.io", 1883);
if (err != LWMQTT_SUCCESS) {
Expand Down Expand Up @@ -75,7 +75,7 @@ int main() {
exit(1);
}

if (lwmqtt_unix_timer_get(NULL, &timer3) <= 0) {
if (lwmqtt_unix_timer_get(&timer3) <= 0) {
lwmqtt_message_t msg = {.qos = LWMQTT_QOS0, .retained = false, .payload = (uint8_t *)("world"), .payload_len = 5};

err = lwmqtt_publish(&client, lwmqtt_string("hello"), msg, COMMAND_TIMEOUT);
Expand All @@ -84,7 +84,7 @@ int main() {
exit(1);
}

lwmqtt_unix_timer_set(NULL, &timer3, MESSAGE_TIMEOUT);
lwmqtt_unix_timer_set(&timer3, MESSAGE_TIMEOUT);
}
}
}
10 changes: 4 additions & 6 deletions include/lwmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,27 +88,25 @@ 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 *client, void *ref, uint8_t *buf, size_t len,
size_t *read, uint32_t timeout);
typedef lwmqtt_err_t (*lwmqtt_network_read_t)(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout);

/**
* The callback used to write to a network object.
*
* 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 *client, void *ref, uint8_t *buf, size_t len,
size_t *sent, uint32_t timeout);
typedef lwmqtt_err_t (*lwmqtt_network_write_t)(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout);

/**
* The callback used to set a timer.
*/
typedef void (*lwmqtt_timer_set_t)(lwmqtt_client_t *client, void *ref, uint32_t timeout);
typedef void (*lwmqtt_timer_set_t)(void *ref, uint32_t timeout);

/**
* The callback used to get a timers value.
*/
typedef uint32_t (*lwmqtt_timer_get_t)(lwmqtt_client_t *client, void *ref);
typedef uint32_t (*lwmqtt_timer_get_t)(void *ref);

/**
* The callback used to forward incoming messages.
Expand Down
10 changes: 4 additions & 6 deletions include/lwmqtt/unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ 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, uint32_t timeout);
void lwmqtt_unix_timer_set(void *ref, uint32_t timeout);

/**
* Callback to read the UNIX timer object.
*/
uint32_t lwmqtt_unix_timer_get(lwmqtt_client_t *client, void *ref);
uint32_t lwmqtt_unix_timer_get(void *ref);

/**
* The UNIX network object.
Expand Down Expand Up @@ -54,13 +54,11 @@ lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_unix_network_t *network, size_t *av
/**
* Callback to read from a UNIX network connection.
*/
lwmqtt_err_t lwmqtt_unix_network_read(lwmqtt_client_t *client, void *ref, uint8_t *buf, size_t len, size_t *read,
uint32_t timeout);
lwmqtt_err_t lwmqtt_unix_network_read(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout);

/**
* Callback to write to a UNIX network connection.
*/
lwmqtt_err_t lwmqtt_unix_network_write(lwmqtt_client_t *client, void *ref, uint8_t *buf, size_t len, size_t *sent,
uint32_t timeout);
lwmqtt_err_t lwmqtt_unix_network_write(void *ref, uint8_t *buf, size_t len, size_t *sent, uint32_t timeout);

#endif // LWMQTT_UNIX_H
36 changes: 18 additions & 18 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *ne
client->timer_set = set;
client->timer_get = get;

client->timer_set(client, client->keep_alive_timer, 0);
client->timer_set(client, client->command_timer, 0);
client->timer_set(client->keep_alive_timer, 0);
client->timer_set(client->command_timer, 0);
}

void lwmqtt_set_callback(lwmqtt_client_t *client, void *ref, lwmqtt_callback_t cb) {
Expand Down Expand Up @@ -71,7 +71,7 @@ static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t off
// read while data is missing
while (read < len) {
// get remaining time
uint32_t remaining_time = client->timer_get(client, client->command_timer);
uint32_t remaining_time = client->timer_get(client->command_timer);

// check timeout
if (remaining_time <= 0) {
Expand All @@ -80,7 +80,7 @@ static lwmqtt_err_t lwmqtt_read_from_network(lwmqtt_client_t *client, size_t off

// read
size_t partial_read = 0;
lwmqtt_err_t err = client->network_read(client, client->network, client->read_buf + offset + read, len - read,
lwmqtt_err_t err = client->network_read(client->network, client->read_buf + offset + read, len - read,
&partial_read, remaining_time);
if (err != LWMQTT_SUCCESS) {
return err;
Expand All @@ -100,7 +100,7 @@ static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offs
// write while data is left
while (written < len) {
// get remaining time
uint32_t remaining_time = client->timer_get(client, client->command_timer);
uint32_t remaining_time = client->timer_get(client->command_timer);

// check timeout
if (remaining_time <= 0) {
Expand All @@ -109,8 +109,8 @@ static lwmqtt_err_t lwmqtt_write_to_network(lwmqtt_client_t *client, size_t offs

// read
size_t partial_write = 0;
lwmqtt_err_t err = client->network_write(client, client->network, client->write_buf + offset + written,
len - written, &partial_write, remaining_time);
lwmqtt_err_t err = client->network_write(client->network, client->write_buf + offset + written, len - written,
&partial_write, remaining_time);
if (err != LWMQTT_SUCCESS) {
return err;
}
Expand Down Expand Up @@ -187,7 +187,7 @@ static lwmqtt_err_t lwmqtt_send_packet_in_buffer(lwmqtt_client_t *client, size_t
}

// reset keep alive timer
client->timer_set(client, client->keep_alive_timer, client->keep_alive_interval);
client->timer_set(client->keep_alive_timer, client->keep_alive_interval);

return LWMQTT_SUCCESS;
}
Expand Down Expand Up @@ -337,14 +337,14 @@ static lwmqtt_err_t lwmqtt_cycle_until(lwmqtt_client_t *client, lwmqtt_packet_ty
if (*packet_type == needle) {
return LWMQTT_SUCCESS;
}
} while (client->timer_get(client, client->command_timer) > 0 && (available == 0 || read < available));
} while (client->timer_get(client->command_timer) > 0 && (available == 0 || read < available));

return LWMQTT_SUCCESS;
}

lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t timeout) {
// set timeout
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// cycle until timeout has been reached
lwmqtt_packet_type_t packet_type = LWMQTT_NO_PACKET;
Expand All @@ -359,13 +359,13 @@ lwmqtt_err_t lwmqtt_yield(lwmqtt_client_t *client, size_t available, uint32_t ti
lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, lwmqtt_will_t *will,
lwmqtt_return_code_t *return_code, uint32_t timeout) {
// set timer to command timeout
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// save keep alive interval (take 75% to be a little earlier than actually needed)
client->keep_alive_interval = (uint32_t)(options.keep_alive * 750);

// set keep alive timer
client->timer_set(client, client->keep_alive_timer, client->keep_alive_interval);
client->timer_set(client->keep_alive_timer, client->keep_alive_interval);

// reset pong pending flag
client->pong_pending = false;
Expand Down Expand Up @@ -410,7 +410,7 @@ lwmqtt_err_t lwmqtt_connect(lwmqtt_client_t *client, lwmqtt_options_t options, l
lwmqtt_err_t lwmqtt_subscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, lwmqtt_qos_t *qos,
uint32_t timeout) {
// set timeout
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// encode subscribe packet
size_t len;
Expand Down Expand Up @@ -461,7 +461,7 @@ lwmqtt_err_t lwmqtt_subscribe_one(lwmqtt_client_t *client, lwmqtt_string_t topic

lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_string_t *topic_filter, uint32_t timeout) {
// set timer
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// encode unsubscribe packet
size_t len;
Expand Down Expand Up @@ -504,7 +504,7 @@ lwmqtt_err_t lwmqtt_unsubscribe_one(lwmqtt_client_t *client, lwmqtt_string_t top
lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmqtt_message_t message,
uint32_t timeout) {
// set timer
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// add packet id if at least qos 1
uint16_t packet_id = 0;
Expand Down Expand Up @@ -560,7 +560,7 @@ lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmq

lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) {
// set timer
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// encode disconnect packet
size_t len;
Expand All @@ -580,15 +580,15 @@ lwmqtt_err_t lwmqtt_disconnect(lwmqtt_client_t *client, uint32_t timeout) {

lwmqtt_err_t lwmqtt_keep_alive(lwmqtt_client_t *client, uint32_t timeout) {
// set timer
client->timer_set(client, client->command_timer, timeout);
client->timer_set(client->command_timer, timeout);

// return immediately if keep alive interval is zero
if (client->keep_alive_interval == 0) {
return LWMQTT_SUCCESS;
}

// return immediately if no ping is due
if (client->timer_get(client, client->keep_alive_timer) > 0) {
if (client->timer_get(client->keep_alive_timer) > 0) {
return LWMQTT_SUCCESS;
}

Expand Down
10 changes: 4 additions & 6 deletions src/os/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <lwmqtt/unix.h>

void lwmqtt_unix_timer_set(lwmqtt_client_t *client, void *ref, uint32_t timeout) {
void lwmqtt_unix_timer_set(void *ref, uint32_t timeout) {
// cast timer reference
lwmqtt_unix_timer_t *t = (lwmqtt_unix_timer_t *)ref;

Expand All @@ -24,7 +24,7 @@ void lwmqtt_unix_timer_set(lwmqtt_client_t *client, void *ref, uint32_t timeout)
timeradd(&now, &interval, &t->end);
}

uint32_t lwmqtt_unix_timer_get(lwmqtt_client_t *client, void *ref) {
uint32_t lwmqtt_unix_timer_get(void *ref) {
// cast timer reference
lwmqtt_unix_timer_t *t = (lwmqtt_unix_timer_t *)ref;

Expand Down Expand Up @@ -121,8 +121,7 @@ lwmqtt_err_t lwmqtt_unix_network_peek(lwmqtt_unix_network_t *network, size_t *av
return LWMQTT_SUCCESS;
}

lwmqtt_err_t lwmqtt_unix_network_read(lwmqtt_client_t *client, void *ref, uint8_t *buffer, size_t len, size_t *read,
uint32_t timeout) {
lwmqtt_err_t lwmqtt_unix_network_read(void *ref, uint8_t *buffer, size_t len, size_t *read, uint32_t timeout) {
// cast network reference
lwmqtt_unix_network_t *n = (lwmqtt_unix_network_t *)ref;

Expand All @@ -145,8 +144,7 @@ lwmqtt_err_t lwmqtt_unix_network_read(lwmqtt_client_t *client, void *ref, uint8_
return LWMQTT_SUCCESS;
}

lwmqtt_err_t lwmqtt_unix_network_write(lwmqtt_client_t *client, void *ref, uint8_t *buffer, size_t len, size_t *sent,
uint32_t timeout) {
lwmqtt_err_t lwmqtt_unix_network_write(void *ref, uint8_t *buffer, size_t len, size_t *sent, uint32_t timeout) {
// cast network reference
lwmqtt_unix_network_t *n = (lwmqtt_unix_network_t *)ref;

Expand Down

0 comments on commit b6f1c81

Please sign in to comment.