Skip to content

Commit

Permalink
Merge pull request #244 from CIPop/v5clientapi
Browse files Browse the repository at this point in the history
MQTTV5Client API
  • Loading branch information
icraggs committed Aug 8, 2023
2 parents 13ded58 + b42df2f commit 5d7a67c
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 72 deletions.
13 changes: 9 additions & 4 deletions MQTTClient-C/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@

file(GLOB SOURCES "*.c" "linux/*.c")

add_library(
paho-embed-mqtt3cc SHARED
${SOURCES}
)
add_library(paho-embed-mqtt3cc SHARED ${SOURCES})
install(TARGETS paho-embed-mqtt3cc DESTINATION /usr/lib)
target_include_directories(paho-embed-mqtt3cc PRIVATE "linux")
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)
46 changes: 44 additions & 2 deletions MQTTClient-C/src/MQTTClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
* Ian Craggs - fix for #96 - check rem_len in readPacket
* Ian Craggs - add ability to set message handler separately #6
*******************************************************************************/
#if defined(MQTTV5)
#include "V5/MQTTV5Client.h"
#else
#include "MQTTClient.h"
#endif

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -70,7 +74,11 @@ void MQTTClientInit(MQTTClient* c, Network* network, unsigned int command_timeou
c->readbuf = readbuf;
c->readbuf_size = readbuf_size;
c->isconnected = 0;
#if defined(MQTTV5)
c->cleanstart = 0;
#else
c->cleansession = 0;
#endif
c->ping_outstanding = 0;
c->defaultMessageHandler = NULL;
c->next_packetid = 1;
Expand Down Expand Up @@ -264,11 +272,15 @@ void MQTTCloseSession(MQTTClient* c)
{
c->ping_outstanding = 0;
c->isconnected = 0;

#if defined(MQTTV5)
//TODO: Session cleanup happens only if Clean Start = 1 and Session Expiry is 0.
#else
if (c->cleansession)
MQTTCleanSession(c);
#endif
}


int cycle(MQTTClient* c, Timer* timer)
{
int32_t len = 0,
Expand Down Expand Up @@ -337,6 +349,11 @@ int cycle(MQTTClient* c, Timer* timer)
case PINGRESP:
c->ping_outstanding = 0;
break;
#if defined(MQTTV5)
case DISCONNECT:
// TODO: implement DISCONNECTv5 and callback to expose reason code and properties.
break;
#endif
}

if (keepalive(c) != MQTTCLIENT_SUCCESS) {
Expand Down Expand Up @@ -445,7 +462,11 @@ int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTC
options = &default_options; /* set default options if none were supplied */

c->keepAliveInterval = options->keepAliveInterval;
#if defined(MQTTV5)
c->cleanstart = options->cleanstart;
#else
c->cleansession = options->cleansession;
#endif
TimerCountdown(&c->last_received, c->keepAliveInterval);
if ((len = MQTTSerialize_connect(c->buf, c->buf_size, options)) <= 0)
goto exit;
Expand All @@ -455,12 +476,28 @@ int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTC
// this will be a blocking call, wait for the connack
if (waitfor(c, CONNACK, &connect_timer) == CONNACK)
{
#if defined(MQTTV5)
data->reasonCode = MQTTREASONCODE_SUCCESS;
#else
data->rc = 0;
#endif

data->sessionPresent = 0;
if (MQTTDeserialize_connack(&data->sessionPresent, &data->rc, c->readbuf, c->readbuf_size) == 1)

#if defined(MQTTV5)
if (MQTTV5Deserialize_connack(c->recvProperties, &data->sessionPresent, (unsigned char*)(&data->reasonCode),
c->readbuf, c->readbuf_size) == 1) {
rc = (int)data->reasonCode;
}
#else
if (MQTTDeserialize_connack(&data->sessionPresent, &data->rc, c->readbuf, c->readbuf_size) == 1) {
rc = data->rc;
}
#endif
else
{
rc = MQTTCLIENT_FAILURE;
}
}
else
rc = MQTTCLIENT_FAILURE;
Expand Down Expand Up @@ -559,13 +596,18 @@ int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum MQTTQo
int count = 0;
unsigned short mypacketid;
unsigned char grantedQoS = MQTTQOS_0;

#if defined(MQTTV5)
// TODO: V5 deserialization and QoS adapter.
#else
int retval = MQTTDeserialize_suback(&mypacketid, 1, &count, &grantedQoS, c->readbuf, c->readbuf_size);
data->grantedQoS = grantedQoS;
if (retval == 1)
{
if (data->grantedQoS != 0x80)
rc = MQTTSetMessageHandler(c, topicFilter, messageHandler);
}
#endif /* MQTTV5 */
}
else
rc = MQTTCLIENT_FAILURE;
Expand Down
Loading

0 comments on commit 5d7a67c

Please sign in to comment.