diff --git a/platform/linux.c b/platform/linux.c index f0c5e89..fe38487 100644 --- a/platform/linux.c +++ b/platform/linux.c @@ -11,6 +11,7 @@ #include #include +#include "debug.h" #include "mqtt_internal.h" #include "platform.h" @@ -24,7 +25,7 @@ struct _PlatformData { }; PlatformStatusCode platform_init(MQTTHandle *handle) { - handle->platform = calloc(1, sizeof(struct _PlatformData)); + handle->platform = (PlatformData *)calloc(1, sizeof(struct _PlatformData)); handle->platform->sock = -1; if (handle->platform) { return PlatformStatusOk; @@ -64,6 +65,7 @@ PlatformStatusCode platform_run_task(MQTTHandle *handle, int *task_handle, Platf return PlatformStatusError; } + *task_handle = free_task; return PlatformStatusOk; } diff --git a/platform/platform.h b/platform/platform.h index e47801e..34006c8 100644 --- a/platform/platform.h +++ b/platform/platform.h @@ -3,7 +3,7 @@ #include "mqtt_internal.h" -typedef void (*PlatformTask)(MQTTHandle *handle); +typedef void *(*PlatformTask)(MQTTHandle *handle); /** maximum receiver buffer size, defined by platform */ extern const size_t max_receive_buffer_size; diff --git a/src/buffer.h b/src/buffer.h index 205875d..685ed11 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -198,7 +198,7 @@ static inline size_t buffer_append_buffer(Buffer *dest, Buffer *src) { return buffer_append_data(dest, src->data, src->len); } -#if DEBUG +#if DEBUG_HEXDUMP #include "debug.h" static inline void buffer_hexdump(Buffer *buffer, int indent) { diff --git a/src/mqtt.c b/src/mqtt.c index 29807b5..e881874 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -35,7 +35,7 @@ static inline void parse_packet(MQTTHandle *handle, MQTTPacket *packet) { break; case PacketTypePublish: - dispatch_subscription(handle, packet->payload); + dispatch_subscription(handle, (PublishPayload *)packet->payload); // TODO: Handle QoS break; @@ -55,7 +55,7 @@ static inline void parse_packet(MQTTHandle *handle, MQTTPacket *packet) { } } -static void _reader(MQTTHandle *handle) { +static void *_reader(MQTTHandle *handle) { Buffer *buffer = buffer_allocate(max_receive_buffer_size); handle->reader_alive = true; @@ -64,7 +64,7 @@ static void _reader(MQTTHandle *handle) { PlatformStatusCode ret = platform_read(handle, buffer); if (ret == PlatformStatusError) { handle->reader_alive = false; - return; + return NULL; } while (1) { @@ -85,7 +85,7 @@ static void _reader(MQTTHandle *handle) { platform_disconnect(handle); handle->reader_alive = false; buffer_release(buffer); - return; + return NULL; } } else { hexdump(buffer->data, num_bytes, 2); @@ -147,7 +147,7 @@ MQTTHandle *mqtt_connect(MQTTConfig *config, MQTTEventHandler callback, void *co return NULL; } - MQTTHandle *handle = calloc(sizeof(struct _MQTTHandle), 1); + MQTTHandle *handle = (MQTTHandle *)calloc(sizeof(struct _MQTTHandle), 1); PlatformStatusCode ret = platform_init(handle); if (ret == PlatformStatusError) { free(handle); diff --git a/src/mqtt.h.in b/src/mqtt.h.in index ecb4713..88c63d5 100644 --- a/src/mqtt.h.in +++ b/src/mqtt.h.in @@ -13,11 +13,14 @@ #define LIBMQTT_VERSION_MAJOR @LIBMQTT_VERSION_MAJOR@ #define LIBMQTT_VERSION_MINOR @LIBMQTT_VERSION_MINOR@ -#define LIBMQTT_BUILD_DATE @LIBMQTT_BUILD_DATE@ #include #include +#ifndef _unused +#define _unused __attribute__((unused)) +#endif + typedef struct _MQTTHandle MQTTHandle; typedef struct { diff --git a/src/packet.c b/src/packet.c index 9f3a50c..61db7ed 100644 --- a/src/packet.c +++ b/src/packet.c @@ -7,7 +7,7 @@ */ MQTTPacket *allocate_MQTTPacket(MQTTControlPacketType type) { - MQTTPacket *packet = calloc(1, sizeof(MQTTPacket)); + MQTTPacket *packet = (MQTTPacket *)calloc(1, sizeof(MQTTPacket)); packet->packet_type = type; switch (type) { @@ -75,12 +75,12 @@ char *utf8_string_decode(Buffer *buffer) { return NULL; // buffer too small } uint16_t sz = (buffer->data[buffer->position] << 8) + buffer->data[buffer->position + 1]; - if (buffer_free_space(buffer) < sz + 2) { + if (buffer_free_space(buffer) < (size_t)(sz + 2)) { return NULL; // incomplete buffer } buffer->position += 2; - result = malloc(sz + 1); + result = (char *)malloc(sz + 1); buffer_copy_out(buffer, result, sz); result[sz] = '\0'; return result; @@ -152,11 +152,11 @@ size_t utf8_string_encode(char *string, Buffer *buffer) { #if MQTT_SERVER bool decode_connect(Buffer *buffer, ConnectPayload *payload) { // Validate this is actually a connect packet - char template[] = { 0x00, 0x04, 'M', 'Q', 'T', 'T' }; - if (memcmp(buffer->data + buffer->position, template, sizeof(template)) != 0) { + char check[] = { 0x00, 0x04, 'M', 'Q', 'T', 'T' }; + if (memcmp(buffer->data + buffer->position, check, sizeof(check)) != 0) { return false; } - buffer->position += sizeof(template); + buffer->position += sizeof(check); payload->protocol_level = buffer->data[buffer->position++]; uint8_t flags = buffer->data[buffer->position++]; @@ -172,7 +172,7 @@ bool decode_connect(Buffer *buffer, ConnectPayload *payload) { payload->will_topic = utf8_string_decode(buffer); payload->will_message = utf8_string_decode(buffer); } - payload->will_qos = (flags & 0x18) >> 3; + payload->will_qos = (MQTTQosLevel)((flags & 0x18) >> 3); payload->retain_will = (flags & 0x20) > 0; // username @@ -191,7 +191,7 @@ bool decode_connect(Buffer *buffer, ConnectPayload *payload) { bool decode_connack(Buffer *buffer, ConnAckPayload *payload) { payload->session_present = buffer->data[buffer->position++] & 0x01; - payload->status = buffer->data[buffer->position++]; + payload->status = (ConnAckStatus)buffer->data[buffer->position++]; return true; } @@ -200,7 +200,7 @@ bool decode_publish(Buffer *buffer, PublishPayload *payload, size_t sz) { uint8_t flags = buffer->data[buffer->position - 2] & 0x0f; uint16_t start_pos = buffer->position; - payload->qos = (flags & 0x06) >> 1; + payload->qos = (MQTTQosLevel)((flags & 0x06) >> 1); payload->retain = ((flags & 0x01) > 0); payload->duplicate = ((flags & 0x08) > 0); @@ -214,7 +214,7 @@ bool decode_publish(Buffer *buffer, PublishPayload *payload, size_t sz) { size_t len = sz - (buffer->position - start_pos) + 1; if (len > 1) { - payload->message = calloc(1, len); + payload->message = (char *)calloc(1, len); memcpy(payload->message, buffer->data + buffer->position, len - 1); buffer->position += len - 1; } @@ -238,7 +238,7 @@ bool decode_subscribe(Buffer *buffer, SubscribePayload *payload) { buffer->position += 2; payload->topic = utf8_string_decode(buffer); - payload->qos = buffer->data[buffer->position++] & 0x03; + payload->qos = (MQTTQosLevel)(buffer->data[buffer->position++] & 0x03); return true; } @@ -250,7 +250,7 @@ bool decode_suback(Buffer *buffer, SubAckPayload *payload) { + buffer->data[buffer->position + 1]; buffer->position += 2; - payload->status = buffer->data[buffer->position++]; + payload->status = (SubAckStatus)(buffer->data[buffer->position++]); return true; } @@ -271,7 +271,7 @@ bool decode_unsubscribe(Buffer *buffer, UnsubscribePayload *payload) { MQTTPacket *mqtt_packet_decode(Buffer *buffer) { // validate that the buffer is big enough - MQTTControlPacketType type = (buffer->data[buffer->position] & 0xf0) >> 4; + MQTTControlPacketType type = (MQTTControlPacketType)((buffer->data[buffer->position] & 0xf0) >> 4); buffer->position++; size_t packet_size = variable_length_int_decode(buffer); @@ -283,20 +283,20 @@ MQTTPacket *mqtt_packet_decode(Buffer *buffer) { bool valid = false; switch (type) { case PacketTypeConnAck: - valid = decode_connack(buffer, result->payload); + valid = decode_connack(buffer, (ConnAckPayload *)result->payload); break; case PacketTypePublish: - valid = decode_publish(buffer, result->payload, packet_size); + valid = decode_publish(buffer, (PublishPayload *)result->payload, packet_size); break; case PacketTypeSubAck: - valid = decode_suback(buffer, result->payload); + valid = decode_suback(buffer, (SubAckPayload *)result->payload); break; case PacketTypePubAck: case PacketTypePubRec: case PacketTypePubRel: case PacketTypePubComp: case PacketTypeUnsubAck: - valid = decode_packet_id(buffer, result->payload); + valid = decode_packet_id(buffer, (PacketIDPayload *)result->payload); break; case PacketTypePingResp: case PacketTypeDisconnect: @@ -308,13 +308,13 @@ MQTTPacket *mqtt_packet_decode(Buffer *buffer) { valid = true; // there is no payload break; case PacketTypeConnect: - valid = decode_connect(buffer, result->payload); + valid = decode_connect(buffer, (ConnectPayload *)result->payload); break; case PacketTypeSubscribe: - valid = decode_subscribe(buffer, result->payload); + valid = decode_subscribe(buffer, (SubscribePayload *)result->payload); break; case PacketTypeUnsubscribe: - valid = decode_unsubscribe(buffer, result->payload); + valid = decode_unsubscribe(buffer, (UnsubscribePayload *)result->payload); break; #endif /* MQTT_SERVER */ diff --git a/src/protocol.c b/src/protocol.c index d6ca975..c8ec60b 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -58,7 +58,7 @@ void handle_pubrec(MQTTHandle *handle, void *context) { #if MQTT_CLIENT bool send_connect_packet(MQTTHandle *handle) { - ConnectPayload *payload = calloc(1, sizeof(ConnectPayload)); + ConnectPayload *payload = (ConnectPayload *)calloc(1, sizeof(ConnectPayload)); payload->client_id = handle->config->client_id; payload->protocol_level = 4; @@ -94,7 +94,7 @@ void remove_pending(MQTTHandle *handle, void *context) { #if MQTT_CLIENT bool send_subscribe_packet(MQTTHandle *handle, char *topic, MQTTQosLevel qos) { - SubscribePayload *payload = calloc(1, sizeof(SubscribePayload)); + SubscribePayload *payload = (SubscribePayload *)calloc(1, sizeof(SubscribePayload)); payload->packet_id = (++handle->packet_id_counter > 0) ? handle->packet_id_counter : ++handle->packet_id_counter; payload->topic = strdup(topic); @@ -112,7 +112,7 @@ bool send_subscribe_packet(MQTTHandle *handle, char *topic, MQTTQosLevel qos) { #if MQTT_CLIENT bool send_unsubscribe_packet(MQTTHandle *handle, char *topic) { - UnsubscribePayload *payload = calloc(1, sizeof(UnsubscribePayload)); + UnsubscribePayload *payload = (UnsubscribePayload *)calloc(1, sizeof(UnsubscribePayload)); payload->packet_id = (++handle->packet_id_counter > 0) ? handle->packet_id_counter : ++handle->packet_id_counter; payload->topic = topic; @@ -129,7 +129,7 @@ bool send_unsubscribe_packet(MQTTHandle *handle, char *topic) { #endif /* MQTT_CLIENT */ bool send_publish_packet(MQTTHandle *handle, char *topic, char *message, MQTTQosLevel qos, MQTTPublishEventHandler callback) { - PublishPayload *payload = calloc(1, sizeof(PublishPayload)); + PublishPayload *payload = (PublishPayload *)calloc(1, sizeof(PublishPayload)); payload->qos = qos; payload->retain = true; @@ -155,14 +155,14 @@ bool send_publish_packet(MQTTHandle *handle, char *topic, char *message, MQTTQos free(payload); break; case MQTT_QOS_1: { - PublishCallback *ctx = malloc(sizeof(PublishCallback)); + PublishCallback *ctx = (PublishCallback *)malloc(sizeof(PublishCallback)); ctx->payload = payload; ctx->callback = callback; expect_packet(handle, PacketTypePubAck, payload->packet_id, handle_puback_pubcomp, ctx); break; } case MQTT_QOS_2: { - PublishCallback *ctx = malloc(sizeof(PublishCallback)); + PublishCallback *ctx = (PublishCallback *)malloc(sizeof(PublishCallback)); ctx->payload = payload; ctx->callback = callback; expect_packet(handle, PacketTypePubRec, payload->packet_id, handle_pubrec, ctx); diff --git a/src/state_queue.c b/src/state_queue.c index f6d40e6..391a2c2 100644 --- a/src/state_queue.c +++ b/src/state_queue.c @@ -17,7 +17,7 @@ static inline void dump_expected(MQTTHandle *handle) { #endif void expect_packet(MQTTHandle *handle, MQTTControlPacketType type, uint16_t packet_id, MQTTEventHandler callback, void *context) { - MQTTCallbackQueueItem *item = calloc(1, sizeof(MQTTCallbackQueueItem)); + MQTTCallbackQueueItem *item = (MQTTCallbackQueueItem *)calloc(1, sizeof(MQTTCallbackQueueItem)); item->type = type; item->packet_id = packet_id; diff --git a/src/subscriptions.c b/src/subscriptions.c index 21ff2d0..cabc686 100644 --- a/src/subscriptions.c +++ b/src/subscriptions.c @@ -2,7 +2,7 @@ #include "subscriptions.h" void add_subscription(MQTTHandle *handle, char *topic, MQTTQosLevel qos, MQTTPublishEventHandler callback) { - SubscriptionItem *item = calloc(1, sizeof(SubscriptionItem)); + SubscriptionItem *item = (SubscriptionItem *)calloc(1, sizeof(SubscriptionItem)); item->topic = topic; item->qos = qos; diff --git a/tests/connect_publish.c b/tests/connect_publish.c index 2695f71..a67827d 100644 --- a/tests/connect_publish.c +++ b/tests/connect_publish.c @@ -8,20 +8,20 @@ int leave = 0; #define LOG(fmt, ...) fprintf(stdout, fmt "\n", ## __VA_ARGS__) -bool err_handler(MQTTHandle *handle, MQTTConfig *config, MQTTErrorCode error) { +bool err_handler(_unused MQTTHandle *handle, _unused MQTTConfig *config, MQTTErrorCode error) { LOG("Error received: %d", error); exit(1); return true; } -void publish_handler(MQTTHandle *handle, char *topic, char *message) { +void publish_handler(_unused MQTTHandle *handle, char *topic, char *message) { LOG("Published %s -> %s", topic, message); leave++; } -void mqtt_connected(MQTTHandle *handle, void *context) { +void mqtt_connected(MQTTHandle *handle, _unused void *context) { LOG("Connected!"); MQTTStatus result; @@ -49,7 +49,7 @@ void mqtt_connected(MQTTHandle *handle, void *context) { leave = true; } -int main(int argc, char **argv) { +int main(_unused int argc, _unused char **argv) { MQTTConfig config = { 0 }; config.client_id = "libmqtt_testsuite_this_is_too_long"; diff --git a/tests/connect_subscribe.c b/tests/connect_subscribe.c index c8f8f03..86a0083 100644 --- a/tests/connect_subscribe.c +++ b/tests/connect_subscribe.c @@ -8,7 +8,7 @@ bool leave = false; #define LOG(fmt, ...) fprintf(stdout, fmt "\n", ## __VA_ARGS__) -bool err_handler(MQTTHandle *handle, MQTTConfig *config, MQTTErrorCode error) { +bool err_handler(_unused MQTTHandle *handle, _unused MQTTConfig *config, MQTTErrorCode error) { LOG("Error received: %d", error); return 1; @@ -33,7 +33,7 @@ void callback(MQTTHandle *handle, char *topic, char *payload) { leave = true; } -void mqtt_connected(MQTTHandle *handle, void *context) { +void mqtt_connected(MQTTHandle *handle, _unused void *context) { LOG("Connected!"); LOG("Trying subscribe on testsuite/mqtt/test..."); @@ -50,7 +50,7 @@ void mqtt_connected(MQTTHandle *handle, void *context) { } } -int main(int argc, char **argv) { +int main(_unused int argc, _unused char **argv) { MQTTConfig config = { 0 }; config.client_id = "libmqtt_testsuite"; diff --git a/tests/test.h b/tests/test.h index 51ae73f..9bfa45a 100644 --- a/tests/test.h +++ b/tests/test.h @@ -19,7 +19,7 @@ typedef struct { TestStatus status; char *message; Buffer *buffer; - Buffer *template; + Buffer *valid; } TestResult; typedef TestResult (*TestPointer)(void); @@ -43,18 +43,18 @@ extern DefinedTest defined_tests[]; #define TEST_OK() return (TestResult){ TestStatusOk, NULL, NULL, NULL } #define TESTRESULT(_status, _message) return (TestResult){ _status, _message, NULL, NULL } -#define TESTRESULT_BUFFER(_status, _message, _buffer, _template) return (TestResult){ _status, _message, _buffer, _template } +#define TESTRESULT_BUFFER(_status, _message, _buffer, _valid) return (TestResult){ _status, _message, _buffer, _valid } #define TESTASSERT(_assertion, _message) if (!(_assertion)) { return (TestResult){ TestStatusFailure, _message, NULL, NULL }; } -static inline TestResult TESTMEMCMP(Buffer *template, Buffer *check) { - if (template->len != check->len) { - TESTRESULT_BUFFER(TestStatusFailureHexdump, "Buffer size differs from template", check, template); +static inline TestResult TESTMEMCMP(Buffer *valid, Buffer *check) { + if (valid->len != check->len) { + TESTRESULT_BUFFER(TestStatusFailureHexdump, "Buffer size differs from valid", check, valid); } - if (memcmp(template->data, check->data, template->len) == 0) { - TESTRESULT(TestStatusOk, "Buffer matches template"); + if (memcmp(valid->data, check->data, valid->len) == 0) { + TESTRESULT(TestStatusOk, "Buffer matches valid"); } else { - TESTRESULT_BUFFER(TestStatusFailureHexdump, "Buffer and template differ", check, template); + TESTRESULT_BUFFER(TestStatusFailureHexdump, "Buffer and valid differ", check, valid); } } @@ -66,7 +66,11 @@ static TestResult not_implemented(void) { } #endif -int main(int argc, char **argv) { +#ifndef _unused +#define _unused __attribute__((unused)) +#endif + +int main(_unused int argc, _unused char **argv) { uint16_t successes = 0; uint16_t skips = 0; uint16_t failures = 0; @@ -101,9 +105,9 @@ int main(int argc, char **argv) { if (result.message) { fprintf(stderr, " -> %s\n", result.message); } - if (result.template) { - fprintf(stderr, " -> Template (%zu bytes)\n", result.template->len); - buffer_hexdump(result.template, 5); + if (result.valid) { + fprintf(stderr, " -> Template (%zu bytes)\n", result.valid->len); + buffer_hexdump(result.valid, 5); } if (result.buffer) { fprintf(stderr, " -> Buffer (%zu bytes)\n", result.buffer->len);