Skip to content

Commit

Permalink
Merge branch 'mqttv5' of github.com:eclipse/paho.mqtt.embedded-c into…
Browse files Browse the repository at this point in the history
… mqttv5
  • Loading branch information
icraggs committed Aug 22, 2023
2 parents 31e5466 + bac3de7 commit 09b8537
Show file tree
Hide file tree
Showing 41 changed files with 625 additions and 290 deletions.
18 changes: 10 additions & 8 deletions MQTTClient-C/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#*******************************************************************************
# Copyright (c) 2017 IBM Corp.
# Copyright (c) 2017, 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 @@ -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
15 changes: 13 additions & 2 deletions MQTTClient/src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,16 @@ int MQTT::Client<Network, Timer, a, b>::sendPacket(int32_t length, Timer& timer)
rc = FAILURE;

#if defined(MQTT_DEBUG)

char printbuf[150];
DEBUG("Rc %d from sending packet %s\r\n", rc,
#if defined(MQTTV5)
MQTTV5Format_toServerString(printbuf, sizeof(printbuf), sendbuf, length));
#else
MQTTFormat_toServerString(printbuf, sizeof(printbuf), sendbuf, length));
#endif
#endif // MQTTV5

#endif // MQTT_DEBUG
return rc;
}

Expand Down Expand Up @@ -503,7 +509,12 @@ int MQTT::Client<Network, Timer, MAX_MQTT_PACKET_SIZE, b>::readPacket(Timer& tim
{
char printbuf[50];
DEBUG("Rc %d receiving packet %s\r\n", rc,
MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len));
#if defined(MQTTV5)
MQTTV5Format_toClientString(printbuf, sizeof(printbuf), readbuf, len));
#else
MQTTFormat_toClientString(printbuf, sizeof(printbuf), readbuf, len));
#endif // MQTTV5

}
#endif
return rc;
Expand Down
3 changes: 2 additions & 1 deletion MQTTPacket/samples/v5log.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
*
*******************************************************************************/

#include "V5/MQTTV5Packet.h"
#include <stdio.h>
#include "MQTTV5Packet.h"

static const char* v5property_identifier_to_string(int identifier)
{
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/v5log.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
*******************************************************************************/

#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"

void v5property_print(MQTTProperty property);
const char* v5reasoncode_to_string(int reasoncode);
7 changes: 3 additions & 4 deletions MQTTPacket/samples/v5ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <string.h>
#include <stdlib.h>

#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"
#include "transport.h"
#include "v5log.h"

Expand Down Expand Up @@ -92,15 +92,14 @@ 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;

MQTTProperties conn_properties = MQTTProperties_initializer;
MQTTProperties will_properties = MQTTProperties_initializer;

len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties, &will_properties);
len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties);
rc = transport_sendPacketBuffer(mysock, buf, len);

printf("Sent MQTTv5 connect\n");
Expand Down
7 changes: 3 additions & 4 deletions MQTTPacket/samples/v5ping_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <string.h>
#include <stdlib.h>

#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"
#include "transport.h"
#include "v5log.h"

Expand Down Expand Up @@ -99,15 +99,14 @@ 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;

MQTTProperties conn_properties = MQTTProperties_initializer;
MQTTProperties will_properties = MQTTProperties_initializer;

len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties, &will_properties);
len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties);
rc = transport_sendPacketBuffer(mysock, buf, len);

printf("Sent MQTT connect\n");
Expand Down
9 changes: 4 additions & 5 deletions MQTTPacket/samples/v5pub0sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <string.h>
#include <stdlib.h>

#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"
#include "transport.h"
#include "v5log.h"

Expand Down Expand Up @@ -80,15 +80,14 @@ 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;

MQTTProperties conn_properties = MQTTProperties_initializer;
MQTTProperties will_properties = MQTTProperties_initializer;

len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties, &will_properties);
len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties);
rc = transport_sendPacketBuffer(mysock, buf, len);

/* wait for connack */
Expand Down Expand Up @@ -259,7 +258,7 @@ int main(int argc, char *argv[])
}

printf("disconnecting\n");
len = MQTTSerialize_disconnect(buf, buflen);
len = MQTTV5Serialize_disconnect(buf, buflen, MQTTREASONCODE_NORMAL_DISCONNECTION, NULL);
rc = transport_sendPacketBuffer(mysock, buf, len);

exit:
Expand Down
5 changes: 2 additions & 3 deletions MQTTPacket/samples/v5pub0sub1_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ int main(int argc, char *argv[])
data.MQTTVersion = 5;

MQTTProperties conn_properties = MQTTProperties_initializer;
MQTTProperties will_properties = MQTTProperties_initializer;

len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties, &will_properties);
len = MQTTV5Serialize_connect(buf, buflen, &data, &conn_properties);
rc = transport_sendPacketBuffer(mysock, buf, len);

/* wait for connack */
Expand Down Expand Up @@ -272,7 +271,7 @@ int main(int argc, char *argv[])
}

printf("disconnecting\n");
len = MQTTSerialize_disconnect(buf, buflen);
len = MQTTV5Serialize_disconnect(buf, buflen, MQTTREASONCODE_NORMAL_DISCONNECTION, NULL);
rc = transport_sendPacketBuffer(mysock, buf, len);

exit:
Expand Down
8 changes: 3 additions & 5 deletions MQTTPacket/samples/v5qos0pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include <string.h>
#include <stdlib.h>

#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"
#include "transport.h"
#include "v5log.h"

Expand Down Expand Up @@ -48,15 +48,13 @@ 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;

MQTTProperties conn_properties = MQTTProperties_initializer;
MQTTProperties will_properties = MQTTProperties_initializer;

len = MQTTV5Serialize_connect((unsigned char *)buf, buflen, &conn_data, &conn_properties, &will_properties);
len = MQTTV5Serialize_connect((unsigned char *)buf, buflen, &conn_data, &conn_properties);

MQTTProperty pub_properties_array[1];
MQTTProperties pub_properties = MQTTProperties_initializer;
Expand Down
14 changes: 7 additions & 7 deletions MQTTPacket/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#*******************************************************************************
# Copyright (c) 2017 IBM Corp.
# Copyright (c) 2017, 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,33 +15,33 @@
#*******************************************************************************/

# MQTTPacket Library
file(GLOB SOURCES "*.c")
file(GLOB SOURCES "*.c" "V3/*.c")
add_library(paho-embed-mqtt3c SHARED ${SOURCES})
install(TARGETS paho-embed-mqtt3c DESTINATION /usr/lib)
target_compile_definitions(paho-embed-mqtt3c PRIVATE MQTT_SERVER MQTT_CLIENT)

add_library(MQTTPacketClient SHARED MQTTFormat MQTTPacket
add_library(MQTTPacketClient SHARED V3/MQTTFormat MQTTPacket
MQTTSerializePublish MQTTDeserializePublish
MQTTConnectClient MQTTSubscribeClient MQTTUnsubscribeClient)
target_compile_definitions(MQTTPacketClient PRIVATE MQTT_CLIENT)

add_library(MQTTPacketServer SHARED MQTTFormat MQTTPacket
add_library(MQTTPacketServer SHARED V3/MQTTFormat MQTTPacket
MQTTSerializePublish MQTTDeserializePublish
MQTTConnectServer MQTTSubscribeServer MQTTUnsubscribeServer)
target_compile_definitions(MQTTPacketServer PRIVATE MQTT_SERVER)

file(GLOB SOURCES5 "*.c" "V5/*.c" ../samples/transport.c)
file(GLOB SOURCES5 "*.c" "V5/*.c")

add_library(paho-embed-mqtt5c SHARED ${SOURCES5})
install(TARGETS paho-embed-mqtt5c DESTINATION /usr/lib)
target_compile_definitions(paho-embed-mqtt5c PRIVATE MQTT_SERVER MQTT_CLIENT MQTTV5)

add_library(MQTTPacketClient5 SHARED MQTTFormat MQTTPacket
add_library(MQTTPacketClient5 SHARED V5/MQTTV5Format MQTTPacket
MQTTSerializePublish MQTTDeserializePublish V5/MQTTProperties V5/MQTTV5Packet
MQTTConnectClient MQTTSubscribeClient MQTTUnsubscribeClient)
target_compile_definitions(MQTTPacketClient5 PRIVATE MQTT_CLIENT MQTTV5)

add_library(MQTTPacketServer5 SHARED MQTTFormat MQTTPacket
add_library(MQTTPacketServer5 SHARED V5/MQTTV5Format MQTTPacket
MQTTSerializePublish MQTTDeserializePublish V5/MQTTProperties V5/MQTTV5Packet
MQTTConnectServer MQTTSubscribeServer MQTTUnsubscribeServer)
target_compile_definitions(MQTTPacketServer5 PRIVATE MQTT_SERVER MQTTV5)
43 changes: 12 additions & 31 deletions MQTTPacket/src/MQTTConnectClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*******************************************************************************/

#if defined(MQTTV5)
#include "V5/MQTTV5Packet.h"
#include "MQTTV5Packet.h"
#else
#include "MQTTPacket.h"
#endif
Expand All @@ -29,8 +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,
MQTTProperties* willProperties)
int MQTTSerialize_connectLength(MQTTPacket_connectData* options, MQTTProperties* connectProperties)
#else
int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
#endif
Expand All @@ -56,16 +55,15 @@ int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
{
if (connectProperties)
len += MQTTProperties_len(connectProperties);
if (options->willFlag && willProperties)
len += MQTTProperties_len(willProperties);
if (options->willFlag && options->will.properties)
len += MQTTProperties_len(options->will.properties);
}
#endif

FUNC_EXIT_RC(len);
return len;
}


/**
* Serializes the connect options into the buffer.
* @param buf the buffer into which the packet will be serialized
Expand All @@ -74,13 +72,8 @@ int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
* @return serialized length, or error if 0
*/
#if defined(MQTTV5)
int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options)
{
return MQTTV5Serialize_connect(buf, buflen, options, NULL, NULL);
}

int MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options,
MQTTProperties* connectProperties, MQTTProperties* willProperties)
MQTTProperties* connectProperties)
#else
int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options)
#endif
Expand All @@ -93,8 +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, willProperties)) > buflen)
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options, connectProperties)) > buflen)
#else
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
#endif
Expand Down Expand Up @@ -147,8 +139,8 @@ int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connect
{
#if defined(MQTTV5)
/* write will properties */
if (options->MQTTVersion == 5 && willProperties)
MQTTProperties_write(&ptr, willProperties);
if (options->MQTTVersion == 5 && options->will.properties)
MQTTProperties_write(&ptr, options->will.properties);
#endif
writeMQTTString(&ptr, options->will.topicName);
writeMQTTString(&ptr, options->will.message);
Expand All @@ -174,11 +166,6 @@ int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connect
* @return error code. 1 is success, 0 is failure
*/
#if defined(MQTTV5)
int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int32_t buflen)
{
return MQTTV5Deserialize_connack(NULL, sessionPresent, connack_rc, buf, buflen);
}

int MQTTV5Deserialize_connack(MQTTProperties* connackProperties, unsigned char* sessionPresent, unsigned char* connack_rc,
unsigned char* buf, int32_t buflen)
#else
Expand Down Expand Up @@ -229,11 +216,6 @@ int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connac
int MQTTV5Serialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype,
unsigned char reasonCode, MQTTProperties* properties);

int MQTTSerialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype)
{
return MQTTV5Serialize_zero(buf, buflen, packettype, -1, NULL);
}

int MQTTV5Serialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype,
unsigned char reasonCode, MQTTProperties* properties)
#else
Expand Down Expand Up @@ -286,11 +268,6 @@ int MQTTSerialize_zero(unsigned char* buf, int32_t buflen, unsigned char packett
* @return serialized length, or error if 0
*/
#if defined(MQTTV5)
int MQTTSerialize_disconnect(unsigned char* buf, int32_t buflen)
{
return MQTTV5Serialize_disconnect(buf, buflen, -1, NULL);
}

int MQTTV5Serialize_disconnect(unsigned char* buf, int32_t buflen,
unsigned char reasonCode, MQTTProperties* properties)
#else
Expand Down Expand Up @@ -322,5 +299,9 @@ int MQTTV5Serialize_auth(unsigned char* buf, int32_t buflen,
*/
int MQTTSerialize_pingreq(unsigned char* buf, int32_t buflen)
{
#if defined(MQTTV5)
return MQTTV5Serialize_zero(buf, buflen, PINGREQ, -1, NULL);
#else
return MQTTSerialize_zero(buf, buflen, PINGREQ);
#endif
}
Loading

0 comments on commit 09b8537

Please sign in to comment.