diff --git a/.gitignore b/.gitignore index c8693868..9c07eea9 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ /doc/MQTTClient-C/ .vscode **/html -**/latex \ No newline at end of file +**/latex +**/Testing diff --git a/CMakeLists.txt b/CMakeLists.txt index 17f732f9..bd04c229 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ # Ian Craggs - initial version #*******************************************************************************/ -cmake_minimum_required(VERSION 2.8) +cmake_minimum_required(VERSION 2.8.12) project (embedded-mqtt) set(CMAKE_BUILD_TYPE Debug) diff --git a/MQTTClient-C/test/test1.c b/MQTTClient-C/test/test1.c index 99b33685..f2333c4b 100644 --- a/MQTTClient-C/test/test1.c +++ b/MQTTClient-C/test/test1.c @@ -196,7 +196,6 @@ START_TIME_TYPE start_clock(void) #else #define mqsleep sleep #define START_TIME_TYPE struct timeval -/* TODO - unused - remove? static struct timeval start_time; */ START_TIME_TYPE start_clock(void) { struct timeval start_time; diff --git a/MQTTClient-CPP/test/test1.cpp b/MQTTClient-CPP/test/test1.cpp index ce402f29..6c055fe8 100644 --- a/MQTTClient-CPP/test/test1.cpp +++ b/MQTTClient-CPP/test/test1.cpp @@ -191,7 +191,6 @@ START_TIME_TYPE start_clock(void) #else #define mqsleep sleep #define START_TIME_TYPE struct timeval -/* TODO - unused - remove? static struct timeval start_time; */ START_TIME_TYPE start_clock(void) { struct timeval start_time; diff --git a/MQTTPacket/samples/transport.c b/MQTTPacket/samples/transport.c index 98ded0eb..8bfe4c73 100644 --- a/MQTTPacket/samples/transport.c +++ b/MQTTPacket/samples/transport.c @@ -52,6 +52,7 @@ #include #include #include +#include #endif #if defined(WIN32) @@ -171,10 +172,12 @@ int* sock = &mysock; { #if defined(NOSIGPIPE) int opt = 1; - - if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0) - Log(TRACE_MIN, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock); -#endif +#if defined(SO_NOSIGPIPE) + setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)); +#elif !defined(WIN32) + signal(SIGPIPE, SIG_IGN); +#endif // defined(SO_NOSIGPIPE) +#endif // defined(NOSIGPIPE) if (family == AF_INET) rc = connect(*sock, (struct sockaddr*)&address, sizeof(address)); diff --git a/MQTTPacket/samples/v5ping.c b/MQTTPacket/samples/v5ping.c index 60de103a..69b395ff 100644 --- a/MQTTPacket/samples/v5ping.c +++ b/MQTTPacket/samples/v5ping.c @@ -63,7 +63,7 @@ void stop_init(void) int main(int argc, char *argv[]) { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; int rc = 0; int mysock = 0; unsigned char buf[200]; @@ -104,7 +104,7 @@ int main(int argc, char *argv[]) printf("Sent MQTTv5 connect\n"); /* wait for connack */ - if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) + if (MQTTV5Packet_read(buf, buflen, transport_getdata) == CONNACK) { unsigned char sessionPresent, connack_rc; @@ -129,10 +129,10 @@ int main(int argc, char *argv[]) while (!toStop) { while(!time_to_ping()); - len = MQTTSerialize_pingreq(buf, buflen); + len = MQTTV5Serialize_pingreq(buf, buflen); transport_sendPacketBuffer(mysock, buf, len); printf("Ping..."); - if (MQTTPacket_read(buf, buflen, transport_getdata) == PINGRESP){ + if (MQTTV5Packet_read(buf, buflen, transport_getdata) == PINGRESP){ printf("Pong\n"); start_ping_timer(); } diff --git a/MQTTPacket/samples/v5ping_nb.c b/MQTTPacket/samples/v5ping_nb.c index 2889fce5..8ac84e18 100644 --- a/MQTTPacket/samples/v5ping_nb.c +++ b/MQTTPacket/samples/v5ping_nb.c @@ -65,7 +65,7 @@ enum states { IDLE, GETPONG }; int main(int argc, char *argv[]) { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; int rc = 0; int mysock = 0; unsigned char buf[200]; @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) int len = 0; char *host = "test.mosquitto.org"; int port = 1884; - MQTTTransport mytransport; + MQTTV5Transport mytransport; int state; MQTTProperty connack_properties_array[5]; MQTTProperties connack_properties = MQTTProperties_initializer; @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) /* wait for connack */ do { int frc; - if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == CONNACK){ + if ((frc=MQTTV5Packet_readnb(buf, buflen, &mytransport)) == CONNACK){ unsigned char sessionPresent, connack_rc; if (MQTTV5Deserialize_connack(&connack_properties, &sessionPresent, &connack_rc, buf, buflen) != 1 @@ -141,14 +141,14 @@ int main(int argc, char *argv[]) switch(state){ case IDLE: if(time_to_ping()){ - len = MQTTSerialize_pingreq(buf, buflen); + len = MQTTV5Serialize_pingreq(buf, buflen); transport_sendPacketBuffer(mysock, buf, len); printf("Ping..."); state = GETPONG; } break; case GETPONG: - if((rc=MQTTPacket_readnb(buf, buflen, &mytransport)) == PINGRESP){ + if((rc=MQTTV5Packet_readnb(buf, buflen, &mytransport)) == PINGRESP){ printf("Pong\n"); start_ping_timer(); state = IDLE; diff --git a/MQTTPacket/samples/v5pub0sub1.c b/MQTTPacket/samples/v5pub0sub1.c index 3df9b4ea..44feeef9 100644 --- a/MQTTPacket/samples/v5pub0sub1.c +++ b/MQTTPacket/samples/v5pub0sub1.c @@ -42,7 +42,7 @@ void stop_init(void) int main(int argc, char *argv[]) { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; int rc = 0; int mysock = 0; unsigned char buf[200]; @@ -91,7 +91,7 @@ int main(int argc, char *argv[]) rc = transport_sendPacketBuffer(mysock, buf, len); /* wait for connack */ - if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) + if (MQTTV5Packet_read(buf, buflen, transport_getdata) == CONNACK) { unsigned char sessionPresent, connack_rc; @@ -123,7 +123,7 @@ int main(int argc, char *argv[]) len = MQTTV5Serialize_subscribe(buf, buflen, 0, msgid, &sub_properties, 1, &topicString, &req_qos, &sub_options); rc = transport_sendPacketBuffer(mysock, buf, len); - if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */ + if (MQTTV5Packet_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */ { unsigned short submsgid; int subcount; @@ -192,7 +192,7 @@ int main(int argc, char *argv[]) { /* transport_getdata() has a built-in 1 second timeout, your mileage will vary */ - rc = MQTTPacket_read(buf, buflen, transport_getdata); + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); unsigned char dup; unsigned short msgid; diff --git a/MQTTPacket/samples/v5pub0sub1_nb.c b/MQTTPacket/samples/v5pub0sub1_nb.c index 6528fd4c..8faa78a7 100644 --- a/MQTTPacket/samples/v5pub0sub1_nb.c +++ b/MQTTPacket/samples/v5pub0sub1_nb.c @@ -42,7 +42,7 @@ void stop_init(void) int main(int argc, char *argv[]) { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData data = MQTTPacket_connectData_initializer; int rc = 0; int mysock = 0; unsigned char buf[200]; @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) int len = 0; char *host = "test.mosquitto.org"; int port = 1884; - MQTTTransport mytransport; + MQTTV5Transport mytransport; MQTTProperty recv_properties_array[5]; MQTTProperties recv_properties = MQTTProperties_initializer; recv_properties.array = recv_properties_array; @@ -84,7 +84,7 @@ int main(int argc, char *argv[]) mytransport.state = 0; data.clientID.cstring = "paho-emb-v5pub0sub1_nb"; data.keepAliveInterval = 20; - data.cleansession = 1; + data.cleanstart = 1; data.username.cstring = "rw"; data.password.cstring = "readwrite"; data.MQTTVersion = 5; @@ -95,7 +95,7 @@ int main(int argc, char *argv[]) rc = transport_sendPacketBuffer(mysock, buf, len); /* wait for connack */ - if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK) + if (MQTTV5Packet_read(buf, buflen, transport_getdata) == CONNACK) { unsigned char sessionPresent, connack_rc; @@ -129,7 +129,7 @@ int main(int argc, char *argv[]) rc = transport_sendPacketBuffer(mysock, buf, len); do { int frc; - if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == SUBACK) /* wait for suback */ + if ((frc=MQTTV5Packet_readnb(buf, buflen, &mytransport)) == SUBACK) /* wait for suback */ { unsigned short submsgid; int subcount; @@ -200,7 +200,7 @@ int main(int argc, char *argv[]) while (!toStop) { /* handle timeouts */ - rc = MQTTPacket_readnb(buf, buflen, &mytransport); + rc = MQTTV5Packet_readnb(buf, buflen, &mytransport); unsigned char dup; unsigned short msgid; diff --git a/MQTTPacket/samples/v5qos0pub.c b/MQTTPacket/samples/v5qos0pub.c index fd0d703f..9edf6871 100644 --- a/MQTTPacket/samples/v5qos0pub.c +++ b/MQTTPacket/samples/v5qos0pub.c @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) { - MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData conn_data = MQTTV5Packet_connectData_initializer; int rc = 0; char buf[200]; int buflen = sizeof(buf); diff --git a/MQTTPacket/src/MQTTConnectClient.c b/MQTTPacket/src/MQTTConnectClient.c index c7a2b0d9..5efb4afa 100644 --- a/MQTTPacket/src/MQTTConnectClient.c +++ b/MQTTPacket/src/MQTTConnectClient.c @@ -29,7 +29,7 @@ * @return the length of buffer needed to contain the serialized version of the packet */ #if defined(MQTTV5) -int MQTTSerialize_connectLength(MQTTPacket_connectData* options, MQTTProperties* connectProperties) +int MQTTV5Serialize_connectLength(MQTTV5Packet_connectData* options, MQTTProperties* connectProperties) #else int MQTTSerialize_connectLength(MQTTPacket_connectData* options) #endif @@ -72,7 +72,7 @@ int MQTTSerialize_connectLength(MQTTPacket_connectData* options) * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options, +int MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTV5Packet_connectData* options, MQTTProperties* connectProperties) #else int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options) @@ -86,7 +86,7 @@ int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connect FUNC_ENTRY; #if defined(MQTTV5) - if (MQTTPacket_len(len = MQTTSerialize_connectLength(options, connectProperties)) > buflen) + if (MQTTPacket_len(len = MQTTV5Serialize_connectLength(options, connectProperties)) > buflen) #else if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) #endif @@ -99,7 +99,7 @@ int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connect header.bits.type = CONNECT; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */ if (options->MQTTVersion == 5 || options->MQTTVersion == 4) writeCString(&ptr, "MQTT"); @@ -213,9 +213,6 @@ int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connac * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int MQTTV5Serialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype, - unsigned char reasonCode, MQTTProperties* properties); - int MQTTV5Serialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype, unsigned char reasonCode, MQTTProperties* properties) #else @@ -245,7 +242,7 @@ int MQTTSerialize_zero(unsigned char* buf, int32_t buflen, unsigned char packett header.bits.type = packettype; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */ #if defined(MQTTV5) if (reasonCode >= 0 && reasonCode <= 162) { @@ -297,7 +294,11 @@ int MQTTV5Serialize_auth(unsigned char* buf, int32_t buflen, * @param buflen the length in bytes of the supplied buffer, to avoid overruns * @return serialized length, or error if 0 */ +#if defined(MQTTV5) +int MQTTV5Serialize_pingreq(unsigned char* buf, int32_t buflen) +#else int MQTTSerialize_pingreq(unsigned char* buf, int32_t buflen) +#endif { #if defined(MQTTV5) return MQTTV5Serialize_zero(buf, buflen, PINGREQ, -1, NULL); diff --git a/MQTTPacket/src/MQTTConnectCommon.h b/MQTTPacket/src/MQTTConnectCommon.h deleted file mode 100644 index 6c8d3e24..00000000 --- a/MQTTPacket/src/MQTTConnectCommon.h +++ /dev/null @@ -1,155 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2023 Microsoft Corporation. All rights reserved. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v10.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - *******************************************************************************/ - -#ifndef MQTTCONNECT_COMMON_H_ -#define MQTTCONNECT_COMMON_H_ - -#include -#if defined(MQTTV5) -#include "V5/MQTTProperties.h" -#else -enum MQTTConnackReturnCodes -{ - MQTTCONNACK_CONNECTION_ACCEPTED = 0, - MQTTCONNACK_UNNACCEPTABLE_PROTOCOL = 1, - MQTTCONNACK_CLIENTID_REJECTED = 2, - MQTTCONNACK_SERVER_UNAVAILABLE = 3, - MQTTCONNACK_BAD_USERNAME_OR_PASSWORD = 4, - MQTTCONNACK_NOT_AUTHORIZED = 5, -}; -#endif - - -#if !defined(DLLImport) - #define DLLImport -#endif -#if !defined(DLLExport) - #define DLLExport -#endif - - -typedef union -{ - unsigned char all; /**< all connect flags */ -#if defined(REVERSED) - struct - { - unsigned int username : 1; /**< 3.1 user name */ - unsigned int password : 1; /**< 3.1 password */ - unsigned int willRetain : 1; /**< will retain setting */ - unsigned int willQoS : 2; /**< will QoS value */ - unsigned int will : 1; /**< will flag */ - unsigned int cleansession : 1; /**< V3 clean session or V5 clean start flag */ - unsigned int : 1; /**< unused */ - } bits; -#else - struct - { - unsigned int : 1; /**< unused */ - unsigned int cleansession : 1; /**< V3 clean session or V5 cleanstart flag */ - unsigned int will : 1; /**< will flag */ - unsigned int willQoS : 2; /**< will QoS value */ - unsigned int willRetain : 1; /**< will retain setting */ - unsigned int password : 1; /**< 3.1 password */ - unsigned int username : 1; /**< 3.1 user name */ - } bits; -#endif -} MQTTConnectFlags; /**< connect flags byte */ - - - -/** - * Defines the MQTT "Last Will and Testament" (LWT) settings for - * the connect packet. - */ -typedef struct -{ - /** The eyecatcher for this structure. must be MQTW. */ - char struct_id[4]; - /** The LWT topic to which the LWT message will be published. */ - MQTTString topicName; - /** The LWT payload. */ - MQTTString message; - /** - * The retained flag for the LWT message (see MQTTAsync_message.retained). - */ - unsigned char retained; - /** - * The quality of service setting for the LWT message (see - * MQTTAsync_message.qos and @ref qos). - */ - char qos; -#if defined(MQTTV5) - /** - * LWT properties. - */ - MQTTProperties* properties; -#endif -} MQTTPacket_willOptions; - -#if defined(MQTTV5) -#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0, NULL } -#else -#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 } -#endif - -typedef struct -{ - /** The eyecatcher for this structure. must be MQTC. */ - char struct_id[4]; - /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1, 5 = 5.0 - */ - unsigned char MQTTVersion; - MQTTString clientID; - unsigned short keepAliveInterval; -#if defined(MQTTV5) - unsigned char cleanstart; -#else - unsigned char cleansession; -#endif /* MQTTV5 */ - unsigned char willFlag; - MQTTPacket_willOptions will; - MQTTString username; - MQTTString password; -} MQTTPacket_connectData; - -typedef union -{ - unsigned char all; /**< all connack flags */ -#if defined(REVERSED) - struct - { - unsigned int reserved : 7; /**< unused */ - unsigned int sessionpresent : 1; /**< session present flag */ - } bits; -#else - struct - { - unsigned int sessionpresent : 1; /**< session present flag */ - unsigned int reserved: 7; /**< unused */ - } bits; -#endif -} MQTTConnackFlags; /**< connack flags byte */ - -#if defined(MQTTV5) -#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 5, {NULL, {0, NULL}}, 60, 1, 0, \ - MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } -#else -#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 4, {NULL, {0, NULL}}, 60, 1, 0, \ - MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } -#endif - -DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int32_t buflen); - -#endif /* MQTTCONNECT_COMMON_H_ */ diff --git a/MQTTPacket/src/MQTTConnectInternal.h b/MQTTPacket/src/MQTTConnectInternal.h new file mode 100644 index 00000000..333be3f4 --- /dev/null +++ b/MQTTPacket/src/MQTTConnectInternal.h @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2023 Microsoft Corporation. All rights reserved. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + *******************************************************************************/ + +#ifndef MQTTCONNECTINTERNAL_H_ +#define MQTTCONNECTINTERNAL_H_ + +#include +#if !defined(DLLImport) + #define DLLImport +#endif +#if !defined(DLLExport) + #define DLLExport +#endif + +typedef union +{ + unsigned char all; /**< all connect flags */ +#if defined(REVERSED) + struct + { + unsigned int username : 1; /**< 3.1 user name */ + unsigned int password : 1; /**< 3.1 password */ + unsigned int willRetain : 1; /**< will retain setting */ + unsigned int willQoS : 2; /**< will QoS value */ + unsigned int will : 1; /**< will flag */ + unsigned int cleansession : 1; /**< V3 clean session or V5 clean start flag */ + unsigned int : 1; /**< unused */ + } bits; +#else + struct + { + unsigned int : 1; /**< unused */ + unsigned int cleansession : 1; /**< V3 clean session or V5 cleanstart flag */ + unsigned int will : 1; /**< will flag */ + unsigned int willQoS : 2; /**< will QoS value */ + unsigned int willRetain : 1; /**< will retain setting */ + unsigned int password : 1; /**< 3.1 password */ + unsigned int username : 1; /**< 3.1 user name */ + } bits; +#endif +} MQTTConnectFlags; /**< connect flags byte */ + +typedef union +{ + unsigned char all; /**< all connack flags */ +#if defined(REVERSED) + struct + { + unsigned int reserved : 7; /**< unused */ + unsigned int sessionpresent : 1; /**< session present flag */ + } bits; +#else + struct + { + unsigned int sessionpresent : 1; /**< session present flag */ + unsigned int reserved: 7; /**< unused */ + } bits; +#endif +} MQTTConnackFlags; /**< connack flags byte */ + +#endif /* MQTTCONNECTINTERNAL_H_ */ diff --git a/MQTTPacket/src/MQTTConnectServer.c b/MQTTPacket/src/MQTTConnectServer.c index 46abba4c..e602b5fd 100644 --- a/MQTTPacket/src/MQTTConnectServer.c +++ b/MQTTPacket/src/MQTTConnectServer.c @@ -53,11 +53,6 @@ int MQTTPacket_checkVersion(MQTTString* protocol, int version) #if defined(MQTTV5) -int32_t MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int32_t len) -{ - return MQTTV5Deserialize_connect(NULL, data, buf, len); -} - /** * Deserializes the supplied (wire) buffer into connect data structure * @param willProperties the V5 properties to be applied to the will message, if it exists @@ -67,7 +62,7 @@ int32_t MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int32_t MQTTV5Deserialize_connect(MQTTProperties* connectProperties, MQTTPacket_connectData* data, +int32_t MQTTV5Deserialize_connect(MQTTProperties* connectProperties, MQTTV5Packet_connectData* data, unsigned char* buf, int32_t len) #else /** @@ -166,11 +161,6 @@ int32_t MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int32_t MQTTSerialize_connack(unsigned char* buf, int32_t buflen, unsigned char connack_rc, unsigned char sessionPresent) -{ - return MQTTV5Serialize_connack(buf, buflen, connack_rc, sessionPresent, NULL); -} - int32_t MQTTV5Serialize_connack(unsigned char* buf, int32_t buflen, unsigned char connack_rc, unsigned char sessionPresent, MQTTProperties* connackProperties) #else @@ -200,7 +190,7 @@ int32_t MQTTSerialize_connack(unsigned char* buf, int32_t buflen, unsigned char header.bits.type = CONNACK; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */ flags.all = 0; flags.bits.sessionpresent = sessionPresent; @@ -271,8 +261,7 @@ int32_t MQTTV5Deserialize_auth(MQTTProperties* properties, unsigned char* reason { return MQTTV5Deserialize_zero(AUTH, properties, reasonCode, buf, buflen); } -#endif - +#else int32_t MQTTDeserialize_disconnect(unsigned char* buf, int32_t buflen) { unsigned char type = 0; @@ -287,3 +276,4 @@ int32_t MQTTDeserialize_disconnect(unsigned char* buf, int32_t buflen) FUNC_EXIT_RC(rc); return rc; } +#endif diff --git a/MQTTPacket/src/MQTTDeserializePublish.c b/MQTTPacket/src/MQTTDeserializePublish.c index 1e19e229..861952a5 100644 --- a/MQTTPacket/src/MQTTDeserializePublish.c +++ b/MQTTPacket/src/MQTTDeserializePublish.c @@ -39,12 +39,6 @@ * @return error code. 1 is success */ #if defined(MQTTV5) -int32_t MQTTDeserialize_publish(unsigned char* dup, unsigned char* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, - unsigned char** payload, int32_t* payloadlen, unsigned char* buf, int32_t buflen) -{ - return MQTTV5Deserialize_publish(dup, qos, retained, packetid, topicName, NULL, payload, payloadlen, buf, buflen); -} - int32_t MQTTV5Deserialize_publish(unsigned char* dup, unsigned char* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, MQTTProperties* properties, unsigned char** payload, int32_t* payloadlen, unsigned char* buf, int32_t buflen) #else @@ -101,14 +95,6 @@ int32_t MQTTDeserialize_publish(unsigned char* dup, unsigned char* qos, unsigned * @return error code. 1 is success, 0 is failure */ #if defined(MQTTV5) -int32_t MQTTV5Deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, - unsigned char* reasonCode, MQTTProperties* properties, unsigned char* buf, int32_t buflen); - -int32_t MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int32_t buflen) -{ - return MQTTV5Deserialize_ack(packettype, dup, packetid, NULL, NULL, buf, buflen); -} - int32_t MQTTV5Deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char *reasonCode, MQTTProperties* properties, unsigned char* buf, int32_t buflen) #else diff --git a/MQTTPacket/src/MQTTPacket.c b/MQTTPacket/src/MQTTPacket.c index e9715e89..891561dc 100644 --- a/MQTTPacket/src/MQTTPacket.c +++ b/MQTTPacket/src/MQTTPacket.c @@ -38,7 +38,11 @@ * @param length the length to be encoded * @return the number of bytes written to buffer */ +#if defined(MQTTV5) +int32_t MQTTV5Packet_encode(unsigned char* buf, int32_t length) +#else int32_t MQTTPacket_encode(unsigned char* buf, int32_t length) +#endif { int32_t rc = 0; @@ -266,7 +270,11 @@ int MQTTstrlen(MQTTString mqttstring) * @param bptr the C string to compare * @return boolean - equal or not */ +#if defined(MQTTV5) +int MQTTV5Packet_equals(MQTTString* a, char* bptr) +#else int MQTTPacket_equals(MQTTString* a, char* bptr) +#endif { int alen = 0, blen = 0; @@ -296,7 +304,11 @@ int MQTTPacket_equals(MQTTString* a, char* bptr) * @return integer MQTT packet type, or -1 on error * @note the whole message must fit into the caller's buffer */ +#if defined(MQTTV5) +int MQTTV5Packet_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned char*, int)) +#else int MQTTPacket_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned char*, int)) +#endif { int rc = -1; MQTTHeader header = {0}; @@ -310,7 +322,11 @@ int MQTTPacket_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned ch len = 1; /* 2. read the remaining length. This is variable in itself */ MQTTPacket_decode(getfn, &rem_len); +#if defined(MQTTV5) + len += MQTTV5Packet_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */ +#else len += MQTTPacket_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */ +#endif /* 3. read the rest of the buffer using a callback to supply the rest of the data */ if((rem_len + len) > buflen) @@ -330,7 +346,11 @@ int MQTTPacket_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned ch * @param value the decoded length returned * @return integer the number of bytes read from the socket, 0 for call again, or -1 on error */ +#if defined(MQTTV5) +static int MQTTV5Packet_decodenb(MQTTV5Transport *trp) +#else static int MQTTPacket_decodenb(MQTTTransport *trp) +#endif { unsigned char c; int rc = MQTTPACKET_READ_ERROR; @@ -368,7 +388,11 @@ static int MQTTPacket_decodenb(MQTTTransport *trp) * @return integer MQTT packet type, 0 for call again, or -1 on error * @note the whole message must fit into the caller's buffer */ +#if defined(MQTTV5) +int MQTTV5Packet_readnb(unsigned char* buf, int32_t buflen, MQTTV5Transport *trp) +#else int MQTTPacket_readnb(unsigned char* buf, int32_t buflen, MQTTTransport *trp) +#endif { int rc = -1, frc; MQTTHeader header = {0}; @@ -388,11 +412,20 @@ int MQTTPacket_readnb(unsigned char* buf, int32_t buflen, MQTTTransport *trp) /*FALLTHROUGH*/ /* read the remaining length. This is variable in itself */ case 1: + +#if defined(MQTTV5) + if((frc=MQTTV5Packet_decodenb(trp)) == MQTTPACKET_READ_ERROR) +#else if((frc=MQTTPacket_decodenb(trp)) == MQTTPACKET_READ_ERROR) +#endif goto exit; if(frc == 0) return 0; +#if defined(MQTTV5) + trp->len = 1 + MQTTV5Packet_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */ +#else trp->len = 1 + MQTTPacket_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */ +#endif if((trp->rem_len + trp->len) > buflen) goto exit; ++trp->state; diff --git a/MQTTPacket/src/MQTTPacket.h b/MQTTPacket/src/MQTTPacket.h index 5b2d7239..b49bbe05 100644 --- a/MQTTPacket/src/MQTTPacket.h +++ b/MQTTPacket/src/MQTTPacket.h @@ -24,13 +24,30 @@ extern "C" { #endif -#include "MQTTPacketCommon.h" +#include "MQTTPacketInternal.h" #include "V3/MQTTConnect.h" #include "V3/MQTTPublish.h" #include "V3/MQTTSubscribe.h" #include "V3/MQTTUnsubscribe.h" #include "V3/MQTTFormat.h" +DLLExport int32_t MQTTSerialize_ack(unsigned char* buf, int32_t buflen, unsigned char type, unsigned char dup, unsigned short packetid); +DLLExport int32_t MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int32_t buflen); + +DLLExport int MQTTPacket_equals(MQTTString* a, char* b); +DLLExport int32_t MQTTPacket_encode(unsigned char* buf, int32_t length); +DLLExport int MQTTPacket_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned char*, int)); +typedef struct { + int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ + void *sck; /* pointer to whatever the system may use to identify the transport */ + int multiplier; + int rem_len; + int32_t len; + char state; +} MQTTTransport; + +DLLExport int MQTTPacket_readnb(unsigned char* buf, int32_t buflen, MQTTTransport *trp); + #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ } #endif diff --git a/MQTTPacket/src/MQTTPacketCommon.h b/MQTTPacket/src/MQTTPacketInternal.h similarity index 73% rename from MQTTPacket/src/MQTTPacketCommon.h rename to MQTTPacket/src/MQTTPacketInternal.h index 1eb7ee8e..871e3703 100644 --- a/MQTTPacket/src/MQTTPacketCommon.h +++ b/MQTTPacket/src/MQTTPacketInternal.h @@ -12,8 +12,8 @@ * *******************************************************************************/ -#ifndef MQTTPACKETCOMMON_H_ -#define MQTTPACKETCOMMON_H_ +#ifndef MQTTPACKETINTERNAL_H_ +#define MQTTPACKETINTERNAL_H_ #include @@ -43,10 +43,7 @@ enum msgTypes { CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, - PINGREQ, PINGRESP, DISCONNECT -#if defined(MQTTV5) - , AUTH -#endif + PINGREQ, PINGRESP, DISCONNECT, AUTH }; /** @@ -90,14 +87,8 @@ typedef struct int MQTTstrlen(MQTTString mqttstring); -DLLExport int32_t MQTTSerialize_ack(unsigned char* buf, int32_t buflen, unsigned char type, unsigned char dup, unsigned short packetid); -DLLExport int32_t MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int32_t buflen); - int32_t MQTTPacket_VBIlen(int32_t rem_len); int32_t MQTTPacket_len(int32_t rem_len); -DLLExport int MQTTPacket_equals(MQTTString* a, char* b); - -DLLExport int32_t MQTTPacket_encode(unsigned char* buf, int32_t length); int32_t MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int32_t* value); int32_t MQTTPacket_decodeBuf(unsigned char* buf, int32_t* value); @@ -109,22 +100,15 @@ int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned cha void writeCString(unsigned char** pptr, const char* string); void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); -DLLExport int MQTTPacket_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned char*, int)); - -typedef struct { - int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ - void *sck; /* pointer to whatever the system may use to identify the transport */ - int multiplier; - int rem_len; - int32_t len; - char state; -}MQTTTransport; - -int MQTTPacket_readnb(unsigned char* buf, int32_t buflen, MQTTTransport *trp); +#if defined(MQTTV5) +#define MQTTPacket_encode_internal MQTTV5Packet_encode +#else +#define MQTTPacket_encode_internal MQTTPacket_encode +#endif #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ } #endif -#endif /* MQTTPACKETCOMMON_H_ */ +#endif /* MQTTPACKETINTERNAL_H_ */ diff --git a/MQTTPacket/src/MQTTSerializePublish.c b/MQTTPacket/src/MQTTSerializePublish.c index 80f48e80..0acfbab8 100644 --- a/MQTTPacket/src/MQTTSerializePublish.c +++ b/MQTTPacket/src/MQTTSerializePublish.c @@ -67,12 +67,6 @@ int32_t MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadle * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTSerialize_publish(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned char qos, unsigned char retained, unsigned short packetid, - MQTTString topicName, unsigned char* payload, int32_t payloadlen) -{ - return MQTTV5Serialize_publish(buf, buflen, dup, qos, retained, packetid, topicName, NULL, payload, payloadlen); -} - int32_t MQTTV5Serialize_publish(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned char qos, unsigned char retained, unsigned short packetid, MQTTString topicName, MQTTProperties* properties, unsigned char* payload, int payloadlen) #else @@ -102,7 +96,7 @@ int32_t MQTTSerialize_publish(unsigned char* buf, int32_t buflen, unsigned char header.bits.retain = retained; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; + ptr += MQTTPacket_encode_internal(ptr, rem_len); /* write remaining length */; writeMQTTString(&ptr, topicName); @@ -136,14 +130,6 @@ int32_t MQTTSerialize_publish(unsigned char* buf, int32_t buflen, unsigned char * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int32_t MQTTV5Serialize_ack(unsigned char* buf, int32_t buflen, unsigned char packettype, unsigned char dup, unsigned short packetid, - unsigned char reasonCode, MQTTProperties* properties); - -int32_t MQTTSerialize_ack(unsigned char* buf, int32_t buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) -{ - return MQTTV5Serialize_ack(buf, buflen, packettype, dup, packetid, -1, NULL); -} - int32_t MQTTV5Serialize_ack(unsigned char* buf, int32_t buflen, unsigned char packettype, unsigned char dup, unsigned short packetid, unsigned char reasonCode, MQTTProperties* properties) #else @@ -174,7 +160,7 @@ int32_t MQTTSerialize_ack(unsigned char* buf, int32_t buflen, unsigned char pack header.bits.qos = (packettype == PUBREL) ? 1 : 0; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */ writeInt(&ptr, packetid); #if defined(MQTTV5) @@ -201,11 +187,6 @@ int32_t MQTTSerialize_ack(unsigned char* buf, int32_t buflen, unsigned char pack * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int32_t MQTTSerialize_puback(unsigned char* buf, int32_t buflen, unsigned short packetid) -{ - return MQTTV5Serialize_puback(buf, buflen, packetid, -1, NULL); -} - int32_t MQTTV5Serialize_puback(unsigned char* buf, int32_t buflen, unsigned short packetid, unsigned char reasonCode, MQTTProperties* properties) #else @@ -228,11 +209,6 @@ int32_t MQTTSerialize_puback(unsigned char* buf, int32_t buflen, unsigned short * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int32_t MQTTSerialize_pubrec(unsigned char* buf, int32_t buflen, unsigned short packetid) -{ - return MQTTV5Serialize_pubrec(buf, buflen, packetid, -1, NULL); -} - int32_t MQTTV5Serialize_pubrec(unsigned char* buf, int32_t buflen, unsigned short packetid, unsigned char reasonCode, MQTTProperties* properties) #else @@ -256,11 +232,6 @@ int32_t MQTTSerialize_pubrec(unsigned char* buf, int32_t buflen, unsigned short * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int32_t MQTTSerialize_pubrel(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned short packetid) -{ - return MQTTV5Serialize_pubrel(buf, buflen, dup, packetid, -1, NULL); -} - int32_t MQTTV5Serialize_pubrel(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned short packetid, unsigned char reasonCode, MQTTProperties* properties) #else @@ -283,11 +254,6 @@ int32_t MQTTSerialize_pubrel(unsigned char* buf, int32_t buflen, unsigned char d * @return serialized length, or error if 0 */ #if defined(MQTTV5) -int32_t MQTTSerialize_pubcomp(unsigned char* buf, int32_t buflen, unsigned short packetid) -{ - return MQTTV5Serialize_pubcomp(buf, buflen, packetid, -1, NULL); -} - int32_t MQTTV5Serialize_pubcomp(unsigned char* buf, int32_t buflen, unsigned short packetid, unsigned char reasonCode, MQTTProperties* properties) #else diff --git a/MQTTPacket/src/MQTTSubscribeClient.c b/MQTTPacket/src/MQTTSubscribeClient.c index 371ce69a..91b2ab18 100644 --- a/MQTTPacket/src/MQTTSubscribeClient.c +++ b/MQTTPacket/src/MQTTSubscribeClient.c @@ -32,7 +32,7 @@ * @return the length of buffer needed to contain the serialized version of the packet */ #if defined(MQTTV5) -int32_t MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[], MQTTProperties* properties) +int32_t MQTTV5Serialize_subscribeLength(int count, MQTTString topicFilters[], MQTTProperties* properties) #else int32_t MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) #endif @@ -62,13 +62,6 @@ int32_t MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTSerialize_subscribe(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned short packetid, int count, - MQTTString topicFilters[], unsigned char requestedQoSs[]) -{ - /* need to pack requestedQoSs into subscribeOptions */ - return MQTTV5Serialize_subscribe(buf, buflen, dup, packetid, NULL, count, topicFilters, requestedQoSs, NULL); -} - int32_t MQTTV5Serialize_subscribe(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned short packetid, MQTTProperties* properties, int count, MQTTString topicFilters[], unsigned char requestedQoSs[], MQTTSubscribe_options options[]) #else @@ -84,7 +77,7 @@ int32_t MQTTSerialize_subscribe(unsigned char* buf, int32_t buflen, unsigned cha FUNC_ENTRY; #if defined(MQTTV5) - if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters, properties)) > buflen) + if (MQTTPacket_len(rem_len = MQTTV5Serialize_subscribeLength(count, topicFilters, properties)) > buflen) #else if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) #endif @@ -99,7 +92,7 @@ int32_t MQTTSerialize_subscribe(unsigned char* buf, int32_t buflen, unsigned cha header.bits.qos = 1; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; + ptr += MQTTPacket_encode_internal(ptr, rem_len); /* write remaining length */; writeInt(&ptr, packetid); @@ -142,12 +135,6 @@ int32_t MQTTSerialize_subscribe(unsigned char* buf, int32_t buflen, unsigned cha * @return error code. 1 is success, 0 is failure */ #if defined(MQTTV5) -int32_t MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, unsigned char grantedQoSs[], - unsigned char* buf, int32_t buflen) -{ - return MQTTV5Deserialize_suback(packetid, NULL, maxcount, count, grantedQoSs, buf, buflen); -} - int32_t MQTTV5Deserialize_suback(unsigned short* packetid, MQTTProperties* properties, int maxcount, int* count, unsigned char* reasonCodes, unsigned char* buf, int32_t buflen) { diff --git a/MQTTPacket/src/MQTTSubscribeServer.c b/MQTTPacket/src/MQTTSubscribeServer.c index 789aa5aa..bb7a5593 100644 --- a/MQTTPacket/src/MQTTSubscribeServer.c +++ b/MQTTPacket/src/MQTTSubscribeServer.c @@ -38,14 +38,6 @@ * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, - int maxcount, int* count, MQTTString topicFilters[], unsigned char requestedQoSs[], - unsigned char* buf, int32_t buflen) -{ - return MQTTV5Deserialize_subscribe(dup, packetid, NULL, - maxcount, count, topicFilters, requestedQoSs, NULL, buf, buflen); -} - int32_t MQTTV5Deserialize_subscribe(unsigned char* dup, unsigned short* packetid, MQTTProperties* properties, int maxcount, int* count, MQTTString topicFilters[], unsigned char requestedQoSs[], MQTTSubscribe_options options[], unsigned char* buf, int32_t buflen) @@ -119,11 +111,6 @@ int32_t MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTSerialize_suback(unsigned char* buf, int32_t buflen, unsigned short packetid, int count, unsigned char* grantedQoSs) -{ - return MQTTV5Serialize_suback(buf, buflen, packetid, NULL, count, grantedQoSs); -} - int32_t MQTTV5Serialize_suback(unsigned char* buf, int32_t buflen, unsigned short packetid, MQTTProperties* properties, int count, unsigned char* reasonCodes) #else @@ -151,7 +138,7 @@ int32_t MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short pack header.bits.type = SUBACK; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */ writeInt(&ptr, packetid); diff --git a/MQTTPacket/src/MQTTUnsubscribeClient.c b/MQTTPacket/src/MQTTUnsubscribeClient.c index c3e4145f..ac3b7d74 100644 --- a/MQTTPacket/src/MQTTUnsubscribeClient.c +++ b/MQTTPacket/src/MQTTUnsubscribeClient.c @@ -32,7 +32,7 @@ * @return the length of buffer needed to contain the serialized version of the packet */ #if defined(MQTTV5) -int32_t MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[], MQTTProperties* properties) +int32_t MQTTV5Serialize_unsubscribeLength(int count, MQTTString topicFilters[], MQTTProperties* properties) #else int32_t MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) #endif @@ -61,12 +61,6 @@ int32_t MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTSerialize_unsubscribe(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned short packetid, - int count, MQTTString topicFilters[]) -{ - return MQTTV5Serialize_unsubscribe(buf, buflen, dup, packetid, NULL, count, topicFilters); -} - int32_t MQTTV5Serialize_unsubscribe(unsigned char* buf, int32_t buflen, unsigned char dup, unsigned short packetid, MQTTProperties* properties, int count, MQTTString topicFilters[]) #else @@ -82,7 +76,7 @@ int32_t MQTTSerialize_unsubscribe(unsigned char* buf, int32_t buflen, unsigned c FUNC_ENTRY; #if defined(MQTTV5) - if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters, properties)) > buflen) + if (MQTTPacket_len(rem_len = MQTTV5Serialize_unsubscribeLength(count, topicFilters, properties)) > buflen) #else if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) #endif @@ -97,7 +91,7 @@ int32_t MQTTSerialize_unsubscribe(unsigned char* buf, int32_t buflen, unsigned c header.bits.qos = 1; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; + ptr += MQTTPacket_encode_internal(ptr, rem_len); /* write remaining length */; writeInt(&ptr, packetid); @@ -124,11 +118,6 @@ int32_t MQTTSerialize_unsubscribe(unsigned char* buf, int32_t buflen, unsigned c * @return error code. 1 is success, 0 is failure */ #if defined(MQTTV5) -int32_t MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int32_t buflen) -{ - return MQTTV5Deserialize_unsuback(packetid, NULL, 0, 0, NULL, buf, buflen); -} - int32_t MQTTV5Deserialize_unsuback(unsigned short* packetid, MQTTProperties* properties, int maxcount, int* count, unsigned char* reasonCodes, unsigned char* buf, int32_t buflen) #else diff --git a/MQTTPacket/src/MQTTUnsubscribeServer.c b/MQTTPacket/src/MQTTUnsubscribeServer.c index ec3c7364..afdd97d9 100644 --- a/MQTTPacket/src/MQTTUnsubscribeServer.c +++ b/MQTTPacket/src/MQTTUnsubscribeServer.c @@ -38,13 +38,7 @@ * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], - unsigned char* buf, int32_t len) -{ - return MQTTV5Deserialize_unsubscribe(dup, packetid, NULL, maxcount, count, topicFilters, buf, len); -} - -DLLExport int32_t MQTTV5Deserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, MQTTProperties* properties, +int32_t MQTTV5Deserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, MQTTProperties* properties, int maxcount, int* count, MQTTString topicFilters[], unsigned char* buf, int32_t len) #else int32_t MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], @@ -101,11 +95,6 @@ int32_t MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid * @return the length of the serialized data. <= 0 indicates error */ #if defined(MQTTV5) -int32_t MQTTSerialize_unsuback(unsigned char* buf, int32_t buflen, unsigned short packetid) -{ - return MQTTV5Serialize_unsuback(buf, buflen, packetid, NULL, 0, NULL); -} - int32_t MQTTV5Serialize_unsuback(unsigned char* buf, int32_t buflen, unsigned short packetid, MQTTProperties* properties, int count, unsigned char* reasonCodes) #else @@ -135,7 +124,7 @@ int32_t MQTTSerialize_unsuback(unsigned char* buf, int32_t buflen, unsigned shor header.bits.type = UNSUBACK; writeChar(&ptr, header.byte); /* write header */ - ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */ writeInt(&ptr, packetid); diff --git a/MQTTPacket/src/MQTTV5Packet.h b/MQTTPacket/src/MQTTV5Packet.h index 7fb5b504..6d8638dd 100644 --- a/MQTTPacket/src/MQTTV5Packet.h +++ b/MQTTPacket/src/MQTTV5Packet.h @@ -17,15 +17,11 @@ #ifndef MQTTV5PACKET_H_ #define MQTTV5PACKET_H_ -#ifndef MQTTV5 -#define MQTTV5 -#endif - #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ extern "C" { #endif -#include "MQTTPacketCommon.h" +#include "MQTTPacketInternal.h" #include "V5/MQTTReasonCodes.h" #include "V5/MQTTProperties.h" #include "V5/MQTTV5Connect.h" @@ -35,11 +31,28 @@ extern "C" { void writeInt4(unsigned char** pptr, int anInt); int readInt4(unsigned char** pptr); - void writeMQTTLenString(unsigned char** pptr, MQTTLenString lenstring); - int MQTTLenStringRead(MQTTLenString* lenstring, unsigned char** pptr, unsigned char* enddata); +DLLExport int32_t MQTTV5Serialize_ack(unsigned char* buf, int32_t buflen, unsigned char packettype, unsigned char dup, unsigned short packetid, + unsigned char reasonCode, MQTTProperties* properties); +DLLExport int32_t MQTTV5Deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, + unsigned char *reasonCode, MQTTProperties* properties, unsigned char* buf, int32_t buflen); + +DLLExport int MQTTV5Packet_equals(MQTTString* a, char* b); +DLLExport int32_t MQTTV5Packet_encode(unsigned char* buf, int32_t length); +DLLExport int MQTTV5Packet_read(unsigned char* buf, int32_t buflen, int (*getfn)(unsigned char*, int)); +typedef struct { + int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ + void *sck; /* pointer to whatever the system may use to identify the transport */ + int multiplier; + int rem_len; + int32_t len; + char state; +} MQTTV5Transport; + +DLLExport int MQTTV5Packet_readnb(unsigned char* buf, int32_t buflen, MQTTV5Transport *trp); + #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ } #endif diff --git a/MQTTPacket/src/V3/MQTTConnect.h b/MQTTPacket/src/V3/MQTTConnect.h index cf84845c..d82599ca 100644 --- a/MQTTPacket/src/V3/MQTTConnect.h +++ b/MQTTPacket/src/V3/MQTTConnect.h @@ -21,7 +21,60 @@ #define MQTTCONNECT_H_ #include -#include "MQTTConnectCommon.h" +#include "MQTTConnectInternal.h" + +enum MQTTConnackReturnCodes +{ + MQTTCONNACK_CONNECTION_ACCEPTED = 0, + MQTTCONNACK_UNNACCEPTABLE_PROTOCOL = 1, + MQTTCONNACK_CLIENTID_REJECTED = 2, + MQTTCONNACK_SERVER_UNAVAILABLE = 3, + MQTTCONNACK_BAD_USERNAME_OR_PASSWORD = 4, + MQTTCONNACK_NOT_AUTHORIZED = 5, +}; + +/** + * Defines the MQTT "Last Will and Testament" (LWT) settings for + * the connect packet. + */ +typedef struct +{ + /** The eyecatcher for this structure. must be MQTW. */ + char struct_id[4]; + /** The LWT topic to which the LWT message will be published. */ + MQTTString topicName; + /** The LWT payload. */ + MQTTString message; + /** + * The retained flag for the LWT message (see MQTTAsync_message.retained). + */ + unsigned char retained; + /** + * The quality of service setting for the LWT message (see + * MQTTAsync_message.qos and @ref qos). + */ + char qos; +} MQTTPacket_willOptions; + +#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 } + +typedef struct +{ + /** The eyecatcher for this structure. must be MQTC. */ + char struct_id[4]; + /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1 + */ + unsigned char MQTTVersion; + MQTTString clientID; + unsigned short keepAliveInterval; + unsigned char cleansession; + unsigned char willFlag; + + MQTTPacket_willOptions will; + MQTTString username; + MQTTString password; + +} MQTTPacket_connectData; #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 4, {NULL, {0, NULL}}, 60, 1, 0, \ MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } @@ -35,4 +88,6 @@ DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned ch DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int32_t buflen); DLLExport int MQTTDeserialize_disconnect(unsigned char* buf, int32_t buflen); +DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int32_t buflen); + #endif /* MQTTCONNECT_H_ */ diff --git a/MQTTPacket/src/V5/MQTTProperties.c b/MQTTPacket/src/V5/MQTTProperties.c index a1d5c629..57b27bd3 100644 --- a/MQTTPacket/src/V5/MQTTProperties.c +++ b/MQTTPacket/src/V5/MQTTProperties.c @@ -152,7 +152,7 @@ int MQTTProperty_write(unsigned char** pptr, MQTTProperty* prop) rc = 4; break; case MQTTPROPERTY_TYPE_VARIABLE_BYTE_INTEGER: - rc = MQTTPacket_encode(*pptr, prop->value.integer4); + rc = MQTTV5Packet_encode(*pptr, prop->value.integer4); break; case MQTTPROPERTY_TYPE_BINARY_DATA: case MQTTPROPERTY_TYPE_UTF_8_ENCODED_STRING: @@ -182,7 +182,7 @@ int MQTTProperties_write(unsigned char** pptr, MQTTProperties* properties) int i = 0, len = 0; /* write the entire property list length first */ - *pptr += MQTTPacket_encode(*pptr, properties->length); + *pptr += MQTTV5Packet_encode(*pptr, properties->length); len = rc = 1; for (i = 0; i < properties->count; ++i) { diff --git a/MQTTPacket/src/V5/MQTTV5Connect.h b/MQTTPacket/src/V5/MQTTV5Connect.h index fdf5b01b..9f394b66 100644 --- a/MQTTPacket/src/V5/MQTTV5Connect.h +++ b/MQTTPacket/src/V5/MQTTV5Connect.h @@ -17,12 +17,63 @@ #ifndef MQTT5CONNECT_H_ #define MQTT5CONNECT_H_ -#include "MQTTConnectCommon.h" +#include "MQTTConnectInternal.h" +#include "V5/MQTTProperties.h" -DLLExport int32_t MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options, +/** + * Defines the MQTT "Last Will and Testament" (LWT) settings for + * the connect packet. + */ +typedef struct +{ + /** The eyecatcher for this structure. must be MQTW. */ + char struct_id[4]; + /** The LWT topic to which the LWT message will be published. */ + MQTTString topicName; + /** The LWT payload. */ + MQTTString message; + /** + * The retained flag for the LWT message (see MQTTAsync_message.retained). + */ + unsigned char retained; + /** + * The quality of service setting for the LWT message (see + * MQTTAsync_message.qos and @ref qos). + */ + char qos; + /** + * LWT properties. + */ + MQTTProperties* properties; +} MQTTV5Packet_willOptions; + +#define MQTTV5Packet_willOptions_initializer { {'M', 'Q', 'T', 'W'}, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0, NULL } + +typedef struct +{ + /** The eyecatcher for this structure. must be MQTC. */ + char struct_id[4]; + /** Version of MQTT to be used. 5 = 5.0 + */ + unsigned char MQTTVersion; + MQTTString clientID; + unsigned short keepAliveInterval; + unsigned char cleanstart; + unsigned char willFlag; + + MQTTV5Packet_willOptions will; + MQTTString username; + MQTTString password; + +} MQTTV5Packet_connectData; + +#define MQTTV5Packet_connectData_initializer { {'M', 'Q', 'T', 'C'}, 5, {NULL, {0, NULL}}, 60, 1, 0, \ + MQTTV5Packet_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } + +DLLExport int32_t MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTV5Packet_connectData* options, MQTTProperties* connectProperties); -DLLExport int32_t MQTTV5Deserialize_connect(MQTTProperties* connectProperties, MQTTPacket_connectData* data, +DLLExport int32_t MQTTV5Deserialize_connect(MQTTProperties* connectProperties, MQTTV5Packet_connectData* data, unsigned char* buf, int32_t len); DLLExport int32_t MQTTV5Serialize_connack(unsigned char* buf, int32_t buflen, unsigned char connack_rc, @@ -43,4 +94,6 @@ DLLExport int32_t MQTTV5Serialize_auth(unsigned char* buf, int32_t buflen, unsig DLLExport int32_t MQTTV5Deserialize_auth(MQTTProperties* properties, unsigned char* reasonCode, unsigned char* buf, int32_t buflen); +DLLExport int MQTTV5Serialize_pingreq(unsigned char* buf, int32_t buflen); + #endif /* MQTTV5CONNECT_H_ */ diff --git a/MQTTPacket/src/V5/MQTTV5Format.c b/MQTTPacket/src/V5/MQTTV5Format.c index 447fe62f..28756c66 100644 --- a/MQTTPacket/src/V5/MQTTV5Format.c +++ b/MQTTPacket/src/V5/MQTTV5Format.c @@ -34,7 +34,7 @@ const char* MQTTV5Packet_getName(unsigned short packetid) } -int MQTTV5StringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data) +int MQTTV5StringFormat_connect(char* strbuf, int strbuflen, MQTTV5Packet_connectData* data) { int strindex = 0; @@ -207,7 +207,7 @@ char* MQTTV5Format_toServerString(char* strbuf, int strbuflen, unsigned char* bu { case CONNECT: { - MQTTPacket_connectData data; + MQTTV5Packet_connectData data; int rc; if ((rc = MQTTV5Deserialize_connect(NULL, &data, buf, buflen)) == 1) strindex = MQTTV5StringFormat_connect(strbuf, strbuflen, &data); diff --git a/MQTTPacket/src/V5/MQTTV5Publish.h b/MQTTPacket/src/V5/MQTTV5Publish.h index 84d1c697..acd9034a 100644 --- a/MQTTPacket/src/V5/MQTTV5Publish.h +++ b/MQTTPacket/src/V5/MQTTV5Publish.h @@ -42,7 +42,4 @@ DLLExport int32_t MQTTV5Serialize_pubrel(unsigned char* buf, int32_t buflen, uns DLLExport int32_t MQTTV5Serialize_pubcomp(unsigned char* buf, int32_t buflen, unsigned short packetid, unsigned char reasonCode, MQTTProperties* properties); -int32_t MQTTV5Deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, - unsigned char *reasonCode, MQTTProperties* properties, unsigned char* buf, int32_t buflen); - #endif /* MQTTV5PUBLISH_H_ */ diff --git a/MQTTPacket/test/CMakeLists.txt b/MQTTPacket/test/CMakeLists.txt index a18ecc7c..9113d962 100644 --- a/MQTTPacket/test/CMakeLists.txt +++ b/MQTTPacket/test/CMakeLists.txt @@ -87,4 +87,4 @@ ADD_TEST( SET_TESTS_PROPERTIES( test3 PROPERTIES TIMEOUT 540 -) \ No newline at end of file +) diff --git a/MQTTPacket/test/test1.c b/MQTTPacket/test/test1.c index 73e404ad..13099620 100644 --- a/MQTTPacket/test/test1.c +++ b/MQTTPacket/test/test1.c @@ -162,7 +162,6 @@ START_TIME_TYPE start_clock(void) #else #define mqsleep sleep #define START_TIME_TYPE struct timeval -/* TODO - unused - remove? static struct timeval start_time; */ START_TIME_TYPE start_clock(void) { struct timeval start_time; diff --git a/MQTTPacket/test/test2.c b/MQTTPacket/test/test2.c index b051b8db..77a95895 100644 --- a/MQTTPacket/test/test2.c +++ b/MQTTPacket/test/test2.c @@ -165,7 +165,6 @@ START_TIME_TYPE start_clock(void) #else #define mqsleep sleep #define START_TIME_TYPE struct timeval -/* TODO - unused - remove? static struct timeval start_time; */ START_TIME_TYPE start_clock(void) { struct timeval start_time; @@ -268,7 +267,7 @@ int checkMQTTStrings(MQTTString a, MQTTString b) } -int checkConnectPackets(MQTTPacket_connectData* before, MQTTPacket_connectData* after) +int checkConnectPackets(MQTTV5Packet_connectData* before, MQTTV5Packet_connectData* after) { int rc = 0; int start_failures = failures; @@ -347,8 +346,8 @@ int checkMQTTProperties(MQTTProperties* before, MQTTProperties* after) int test1(struct Options options) { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; - MQTTPacket_connectData data_after = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; + MQTTV5Packet_connectData data_after = MQTTV5Packet_connectData_initializer; int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); diff --git a/MQTTPacket/test/test3.c b/MQTTPacket/test/test3.c index b921c3a0..44afc36c 100644 --- a/MQTTPacket/test/test3.c +++ b/MQTTPacket/test/test3.c @@ -150,7 +150,6 @@ START_TIME_TYPE start_clock(void) #else #define mqsleep sleep #define START_TIME_TYPE struct timeval -/* TODO - unused - remove? static struct timeval start_time; */ START_TIME_TYPE start_clock(void) { struct timeval start_time; @@ -237,7 +236,7 @@ void myassert(char* filename, int lineno, char* description, int value, char* fo int test1(struct Options options) { - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer; int rc = 0; unsigned char buf[200]; int buflen = sizeof(buf); @@ -292,7 +291,7 @@ int test1(struct Options options) assert("rc and len should be the same", rc == len, "rc was different %d\n", rc); /* wait for connack */ - rc = MQTTPacket_read(buf, buflen, transport_getdata); + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); assert("Should receive connack", rc == CONNACK, "did not get connack %d\n", rc); if (rc == CONNACK) { @@ -318,7 +317,7 @@ int test1(struct Options options) rc = transport_sendPacketBuffer(mysock, buf, len); assert("rc and len should be the same", rc == len, "rc was different %d\n", rc); - rc = MQTTPacket_read(buf, buflen, transport_getdata); /* wait for suback */ + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); /* wait for suback */ assert("Should receive suback", rc == SUBACK, "did not get suback %d\n", rc); if (rc == SUBACK) { @@ -345,7 +344,7 @@ int test1(struct Options options) rc = transport_sendPacketBuffer(mysock, buf, len); assert("rc and len should be the same", rc == len, "rc was different %d\n", rc); - rc = MQTTPacket_read(buf, buflen, transport_getdata); /* wait for publish */ + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); /* wait for publish */ assert("Should receive publish", rc == PUBLISH, "did not get publish %d\n", rc); if (rc == PUBLISH) { @@ -376,7 +375,7 @@ int test1(struct Options options) i = 0; while (i < 2) { - rc = MQTTPacket_read(buf, buflen, transport_getdata); /* wait for publish and puback */ + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); /* wait for publish and puback */ assert("Should receive publish or puback", rc == PUBACK || rc == PUBLISH, "did not get puback or publish %d\n", rc); if (rc == PUBLISH) { @@ -430,7 +429,7 @@ int test1(struct Options options) { static unsigned short pubmsgid = 999; - rc = MQTTPacket_read(buf, buflen, transport_getdata); /* wait for publish, pubrel and pubcomp */ + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); /* wait for publish, pubrel and pubcomp */ assert("Should receive publish, pubrec, pubrel or pubcomp", rc == PUBREC || rc == PUBREL || rc == PUBLISH || rc == PUBCOMP, "did not get pubrec, pubrel, pubcomp or publish %d\n", rc); if (rc == PUBLISH) @@ -525,7 +524,7 @@ int test1(struct Options options) rc = transport_sendPacketBuffer(mysock, buf, len); assert("rc and len should be the same", rc == len, "rc was different %d\n", rc); - rc = MQTTPacket_read(buf, buflen, transport_getdata); /* wait for unsuback */ + rc = MQTTV5Packet_read(buf, buflen, transport_getdata); /* wait for unsuback */ assert("Should receive unsuback", rc == UNSUBACK, "did not get unsuback %d\n", rc); if (rc == UNSUBACK) {