Skip to content

Commit

Permalink
V3 and V5 Packet split. MQTTV5Client-C build temporarily disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
CIPop committed Aug 14, 2023
1 parent 86f22c0 commit a008747
Show file tree
Hide file tree
Showing 16 changed files with 37 additions and 64 deletions.
16 changes: 9 additions & 7 deletions MQTTClient-C/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ target_link_libraries(paho-embed-mqtt3cc paho-embed-mqtt3c)
target_compile_definitions(paho-embed-mqtt3cc PRIVATE
MQTTCLIENT_PLATFORM_HEADER=MQTTLinux.h MQTTCLIENT_QOS2=1)

file(GLOB SOURCES "*.c" "V5/*.c" "linux/*.c")
add_library(paho-embed-mqtt5cc SHARED ${SOURCES})
install(TARGETS paho-embed-mqtt5cc DESTINATION /usr/lib)
target_include_directories(paho-embed-mqtt5cc PRIVATE "linux")
target_link_libraries(paho-embed-mqtt5cc paho-embed-mqtt5c)
target_compile_definitions(paho-embed-mqtt5cc PRIVATE
MQTTCLIENT_PLATFORM_HEADER=MQTTLinux.h MQTTCLIENT_QOS2=1 MQTTV5)
# TODO: Temporarily disable MQTT v5
#
# file(GLOB SOURCES "*.c" "V5/*.c" "linux/*.c")
# add_library(paho-embed-mqtt5cc SHARED ${SOURCES})
# install(TARGETS paho-embed-mqtt5cc DESTINATION /usr/lib)
# target_include_directories(paho-embed-mqtt5cc PRIVATE "linux")
# target_link_libraries(paho-embed-mqtt5cc paho-embed-mqtt5c)
# target_compile_definitions(paho-embed-mqtt5cc PRIVATE
# MQTTCLIENT_PLATFORM_HEADER=MQTTLinux.h MQTTCLIENT_QOS2=1 MQTTV5)
2 changes: 1 addition & 1 deletion MQTTClient-C/src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#endif

#if defined(MQTTV5)
#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"
#else
#include "MQTTPacket.h"
#endif /* MQTTV5 */
Expand Down
2 changes: 1 addition & 1 deletion MQTTClient-C/src/V5/MQTTV5Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include <stdbool.h>
#include "../MQTTClient.h"
#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"


/**
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/v5ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char *argv[])

data.clientID.cstring = "paho-emb-ping";
data.keepAliveInterval = KEEPALIVE_INTERVAL;
data.cleansession = 1;
data.cleanstart = 1;
data.username.cstring = "rw";
data.password.cstring = "readwrite";
data.MQTTVersion = 5;
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/v5ping_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ int main(int argc, char *argv[])
mytransport.state = 0;
data.clientID.cstring = "paho-emb-ping_nb";
data.keepAliveInterval = KEEPALIVE_INTERVAL;
data.cleansession = 1;
data.cleanstart = 1;
data.username.cstring = "rw";
data.password.cstring = "readwrite";
data.MQTTVersion = 5;
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/v5pub0sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int main(int argc, char *argv[])

data.clientID.cstring = "paho-emb-v5pub0sub1";
data.keepAliveInterval = 20;
data.cleansession = 1;
data.cleanstart = 1;
data.username.cstring = "rw";
data.password.cstring = "readwrite";
data.MQTTVersion = 5;
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/v5qos0pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(int argc, char *argv[])

conn_data.clientID.cstring = "paho-emb-v5qos0pub";
conn_data.keepAliveInterval = 20;
conn_data.cleansession = 1;
conn_data.cleanstart = 1;
conn_data.username.cstring = "rw";
conn_data.password.cstring = "readwrite";
conn_data.MQTTVersion = 5;
Expand Down
2 changes: 2 additions & 0 deletions MQTTPacket/src/MQTTConnectCommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,6 @@ typedef union
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
#endif

DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int32_t buflen);

#endif /* MQTTCONNECT_COMMON_H_ */
5 changes: 5 additions & 0 deletions MQTTPacket/src/MQTTPacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
*******************************************************************************/

#include "StackTrace.h"

#if defined(MQTTV5)
#include "MQTTV5Packet.h"
#else
#include "MQTTPacket.h"
#endif

#include <string.h>

Expand Down
1 change: 0 additions & 1 deletion MQTTPacket/src/V3/MQTTConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ 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_ */
40 changes: 1 addition & 39 deletions MQTTPacket/src/V3/MQTTFormat.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014 IBM Corp.
* Copyright (c) 2014, 2023 IBM Corp.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -15,11 +15,7 @@
*******************************************************************************/

#include "StackTrace.h"
#if defined(MQTTV5)
#include "MQTTV5Packet.h"
#else
#include "MQTTPacket.h"
#endif

#include <string.h>

Expand All @@ -29,9 +25,6 @@ const char* MQTTPacket_names[] =
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
"PINGREQ", "PINGRESP", "DISCONNECT"
#if defined(MQTTV5)
, "AUTH"
#endif
};


Expand All @@ -40,22 +33,14 @@ const char* MQTTPacket_getName(unsigned short packetid)
return MQTTPacket_names[packetid];
}

#if defined(MQTTV5)
int MQTTV5StringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
#else
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
#endif
{
int strindex = 0;

strindex = snprintf(strbuf, strbuflen,
"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
(int)data->MQTTVersion, (int) data->clientID.lenstring.len, data->clientID.lenstring.data,
#if defined(MQTTV5)
(int)data->cleanstart,
#else
(int)data->cleansession,
#endif
data->keepAliveInterval);

if (data->willFlag)
Expand All @@ -73,29 +58,16 @@ int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData
return strindex;
}

#if defined(MQTTV5)
int MQTTV5StringFormat_connack(char* strbuf, int strbuflen, enum MQTTReasonCodes reason_code, unsigned char sessionPresent)
#else
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent)
#endif
{
int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent,
#if defined(MQTTV5)
reason_code);
#else
connack_rc);
#endif

return strindex;
}

#if defined(MQTTV5)
int MQTTV5StringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
#else
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
#endif
{
int strindex = snprintf(strbuf, strbuflen,
"PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
Expand All @@ -106,23 +78,13 @@ int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int
}


#if defined(MQTTV5)
int MQTTV5StringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned char reason, unsigned short packetid)
{
int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d reason %d", MQTTV5Packet_names[packettype], packetid, reason);
if (dup)
strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
return strindex;
}
#else
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
{
int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
if (dup)
strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
return strindex;
}
#endif

int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
MQTTString topicFilters[], unsigned char requestedQoSs[])
Expand Down
13 changes: 8 additions & 5 deletions MQTTPacket/src/V5/MQTTV5Format.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/

// TODO: Add MQTTv5 properties implementation (some of the code exists in v5log.h), application must provide memory.
#include "StackTrace.h"
#include "MQTTV5Packet.h"

Expand All @@ -40,9 +41,9 @@ int MQTTV5StringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectDa
int strindex = 0;

strindex = snprintf(strbuf, strbuflen,
"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
"CONNECT MQTT version %d, client id %.*s, clean start %d, keep alive %d",
(int)data->MQTTVersion, (int) data->clientID.lenstring.len, data->clientID.lenstring.data,
(int)data->cleansession, data->keepAliveInterval);
(int)data->cleanstart, data->keepAliveInterval);
if (data->willFlag)
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
Expand Down Expand Up @@ -244,8 +245,10 @@ char* MQTTV5Format_toServerString(char* strbuf, int strbuflen, unsigned char* bu
int maxcount = 1, count = 0;
MQTTString topicFilters[1];
unsigned char requestedQoSs[1];
if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
topicFilters, requestedQoSs, buf, buflen) == 1)
MQTTSubscribe_options sub_options[1];

if (MQTTV5Deserialize_subscribe(&dup, &packetid, NULL, maxcount, &count,
topicFilters, requestedQoSs, sub_options, buf, buflen) == 1)
strindex = MQTTV5StringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
}
break;
Expand All @@ -255,7 +258,7 @@ char* MQTTV5Format_toServerString(char* strbuf, int strbuflen, unsigned char* bu
unsigned short packetid;
int maxcount = 1, count = 0;
MQTTString topicFilters[1];
if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
if (MQTTV5Deserialize_unsubscribe(&dup, &packetid, NULL, maxcount, &count, topicFilters, buf, buflen) == 1)
strindex = MQTTV5StringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
}
break;
Expand Down
2 changes: 0 additions & 2 deletions MQTTPacket/src/V5/MQTTV5Publish.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#define DLLExport
#endif

#include "MQTTPublish.h"

DLLExport 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* props, unsigned char* payload, int32_t payloadlen);

Expand Down
2 changes: 2 additions & 0 deletions MQTTPacket/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ ELSE ()
SET(CERTDIR $ENV{TRAVIS_BUILD_DIR}/test/ssl)
ENDIF ()

# transport.h/c are reused from ../samples:
include_directories(../src ../src/V5 ../samples)

ADD_EXECUTABLE(
Expand Down Expand Up @@ -66,6 +67,7 @@ SET_TESTS_PROPERTIES(
ADD_EXECUTABLE(
test3
test3.c
../samples/transport.c
)

# Disabling deprecation warnings:
Expand Down
6 changes: 3 additions & 3 deletions MQTTPacket/test/test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,8 @@ int checkConnectPackets(MQTTPacket_connectData* before, MQTTPacket_connectData*
assert("keepAliveIntervals should be the same",
before->keepAliveInterval == after->keepAliveInterval, "keepAliveIntervals were different %d\n", after->keepAliveInterval);

assert("cleansessions should be the same",
before->cleansession == after->cleansession, "cleansessions were different\n", rc);
assert("cleanstarts should be the same",
before->cleanstart == after->cleanstart, "cleanstarts were different\n", rc);

assert("willFlags should be the same",
before->willFlag == after->willFlag, "willFlags were different\n", rc);
Expand Down Expand Up @@ -385,7 +385,7 @@ int test1(struct Options options)
data.clientID.cstring = "my clientid";

data.keepAliveInterval = 20;
data.cleansession = 1;
data.cleanstart = 1;
data.username.cstring = "testuser";
data.password.cstring = "testpassword";

Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/test/test3.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ int test1(struct Options options)

data.clientID.cstring = "mqtt5_test3_test1";
data.keepAliveInterval = 20;
data.cleansession = 1;
data.cleanstart = 1;
data.username.cstring = "testuser";
data.password.cstring = "testpassword";
data.MQTTVersion = 5;
Expand Down

0 comments on commit a008747

Please sign in to comment.