Skip to content

Commit

Permalink
Merge pull request #2 from 256dpi/big-endian
Browse files Browse the repository at this point in the history
Support for Big Endian architectures
  • Loading branch information
256dpi authored Oct 19, 2017
2 parents 759bf9c + 4bfcbaf commit f51a85e
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 120 deletions.
4 changes: 2 additions & 2 deletions include/lwmqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,11 @@ void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_
*
* @param client - The client object.
* @param keep_alive_timer - The reference to the keep alive timer.
* @param network_timer - The reference to the network timer.
* @param command_timer - The reference to the command timer.
* @param set - The set callback.
* @param get - The get callback.
*/
void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *network_timer, lwmqtt_timer_set_t set,
void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *command_timer, lwmqtt_timer_set_t set,
lwmqtt_timer_get_t get);

/**
Expand Down
26 changes: 13 additions & 13 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ void lwmqtt_set_network(lwmqtt_client_t *client, void *ref, lwmqtt_network_read_
client->network_write = write;
}

void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *network_timer, lwmqtt_timer_set_t set,
void lwmqtt_set_timers(lwmqtt_client_t *client, void *keep_alive_timer, void *command_timer, lwmqtt_timer_set_t set,
lwmqtt_timer_get_t get) {
client->keep_alive_timer = keep_alive_timer;
client->command_timer = network_timer;
client->command_timer = command_timer;
client->timer_set = set;
client->timer_get = get;

Expand Down Expand Up @@ -253,7 +253,7 @@ static lwmqtt_err_t lwmqtt_cycle(lwmqtt_client_t *client, size_t *read, lwmqtt_p
// decode pubrec packet
bool dup;
uint16_t packet_id;
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, packet_type, &dup, &packet_id);
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_PUBREC_PACKET, &dup, &packet_id);
if (err != LWMQTT_SUCCESS) {
return err;
}
Expand All @@ -279,7 +279,7 @@ static lwmqtt_err_t lwmqtt_cycle(lwmqtt_client_t *client, size_t *read, lwmqtt_p
// decode pubrec packet
bool dup;
uint16_t packet_id;
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, packet_type, &dup, &packet_id);
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_PUBREL_PACKET, &dup, &packet_id);
if (err != LWMQTT_SUCCESS) {
return err;
}
Expand Down Expand Up @@ -343,7 +343,7 @@ static lwmqtt_err_t lwmqtt_cycle_until(lwmqtt_client_t *client, lwmqtt_packet_ty
}

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

// cycle until timeout has been reached
Expand All @@ -358,7 +358,7 @@ 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
// set command timer
client->timer_set(client->command_timer, timeout);

// save keep alive interval (take 75% to be a little earlier than actually needed)
Expand Down Expand Up @@ -409,7 +409,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
// set command timer
client->timer_set(client->command_timer, timeout);

// encode subscribe packet
Expand Down Expand Up @@ -460,7 +460,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
// set command timer
client->timer_set(client->command_timer, timeout);

// encode unsubscribe packet
Expand Down Expand Up @@ -489,7 +489,7 @@ lwmqtt_err_t lwmqtt_unsubscribe(lwmqtt_client_t *client, int count, lwmqtt_strin
// decode unsuback packet
bool dup;
uint16_t packet_id;
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, &packet_type, &dup, &packet_id);
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, LWMQTT_UNSUBACK_PACKET, &dup, &packet_id);
if (err != LWMQTT_SUCCESS) {
return err;
}
Expand All @@ -503,7 +503,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
// set command timer
client->timer_set(client->command_timer, timeout);

// add packet id if at least qos 1
Expand Down Expand Up @@ -550,7 +550,7 @@ lwmqtt_err_t lwmqtt_publish(lwmqtt_client_t *client, lwmqtt_string_t topic, lwmq

// decode ack packet
bool dup;
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, &packet_type, &dup, &packet_id);
err = lwmqtt_decode_ack(client->read_buf, client->read_buf_size, ack_type, &dup, &packet_id);
if (err != LWMQTT_SUCCESS) {
return err;
}
Expand All @@ -559,7 +559,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
// set command timer
client->timer_set(client->command_timer, timeout);

// encode disconnect packet
Expand All @@ -579,7 +579,7 @@ 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
// set command timer
client->timer_set(client->command_timer, timeout);

// return immediately if keep alive interval is zero
Expand Down
8 changes: 8 additions & 0 deletions src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

#include "helpers.h"

uint8_t lwmqtt_read_bits(uint8_t byte, uint8_t pos, uint8_t num) {
return (byte & (uint8_t)((~((~0) << num)) << pos)) >> pos;
}

void lwmqtt_write_bits(uint8_t *byte, uint8_t value, uint8_t pos, uint8_t num) {
*byte = (*byte & ~(uint8_t)((~((~0) << num)) << pos)) | (value << pos);
}

lwmqtt_err_t lwmqtt_read_data(uint8_t **buf, const uint8_t *buf_end, uint8_t **data, size_t len) {
// check zero length
if (len == 0) {
Expand Down
20 changes: 20 additions & 0 deletions src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@

#include <lwmqtt.h>

/**
* Reads bits from a byte.
*
* @param byte - The byte to read from.
* @param pos - The position of the first bit.
* @param num - The number of bits to read.
* @return The read bits as a byte.
*/
uint8_t lwmqtt_read_bits(uint8_t byte, uint8_t pos, uint8_t num);

/**
* Write bits to a byte.
*
* @param byte - The byte to write bits to.
* @param value - The bits to write as a byte.
* @param pos - The position of the first bit.
* @param num - The number of bits to write.
*/
void lwmqtt_write_bits(uint8_t *byte, uint8_t value, uint8_t pos, uint8_t num);

/**
* Reads arbitrary data from the specified buffer. The pointer is incremented by bytes read.
*
Expand Down
Loading

0 comments on commit f51a85e

Please sign in to comment.