Skip to content

Commit

Permalink
Moving willProperties within MQTTPacket_connectData.
Browse files Browse the repository at this point in the history
  • Loading branch information
CIPop committed Aug 4, 2023
1 parent 13ded58 commit db4f1c4
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 34 deletions.
3 changes: 1 addition & 2 deletions MQTTPacket/samples/v5ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,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);

printf("Sent MQTTv5 connect\n");
Expand Down
3 changes: 1 addition & 2 deletions MQTTPacket/samples/v5ping_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,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);

printf("Sent MQTT connect\n");
Expand Down
3 changes: 1 addition & 2 deletions MQTTPacket/samples/v5pub0sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,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
3 changes: 1 addition & 2 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
4 changes: 1 addition & 3 deletions MQTTPacket/samples/v5qos0pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ int main(int argc, char *argv[])
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
15 changes: 13 additions & 2 deletions MQTTPacket/src/MQTTConnect.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#define MQTTCONNECT_H_

#include <stdint.h>
#if defined(MQTTV5)
#include "V5/MQTTProperties.h"
#endif

enum MQTTConnackReturnCodes
{
Expand Down Expand Up @@ -93,11 +96,19 @@ typedef struct
* 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'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0, NULL }
#else
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }

#endif

typedef struct
{
Expand Down
19 changes: 8 additions & 11 deletions MQTTPacket/src/MQTTConnectClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 @@ -76,11 +74,11 @@ int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
#if defined(MQTTV5)
int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options)
{
return MQTTV5Serialize_connect(buf, buflen, options, NULL, NULL);
return MQTTV5Serialize_connect(buf, buflen, options, 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 +91,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 @@ -142,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 Down
8 changes: 4 additions & 4 deletions MQTTPacket/src/MQTTConnectServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ 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, NULL, data, buf, len);
return MQTTV5Deserialize_connect(NULL, data, buf, len);
}

/**
Expand All @@ -67,8 +67,8 @@ 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* willProperties, MQTTProperties* connectProperties,
MQTTPacket_connectData* data, unsigned char* buf, int32_t len)
int32_t MQTTV5Deserialize_connect(MQTTProperties* connectProperties, MQTTPacket_connectData* data,
unsigned char* buf, int32_t len)
#else
/**
* Deserializes the supplied (wire) buffer into connect data structure
Expand Down Expand Up @@ -123,7 +123,7 @@ int32_t MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf
#if defined(MQTTV5)
if (data->MQTTVersion == 5)
{
if (!MQTTProperties_read(willProperties, &curdata, enddata))
if (!MQTTProperties_read(data->will.properties, &curdata, enddata))
goto exit;
}
#endif
Expand Down
4 changes: 4 additions & 0 deletions MQTTPacket/src/V5/MQTTProperties.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/

#ifndef MQTTPROPERTIES_H_
#define MQTTPROPERTIES_H_

enum MQTTPropertyNames {
MQTTPROPERTY_CODE_PAYLOAD_FORMAT_INDICATOR = 1,
Expand Down Expand Up @@ -102,3 +104,5 @@ DLLExport int MQTTProperties_read(MQTTProperties* properties, unsigned char** pp
* @return the `PropertyTypes` type of the property
*/
DLLExport int MQTTProperty_getType(int identifier);

#endif // MQTTPROPERTIES_H_
4 changes: 4 additions & 0 deletions MQTTPacket/src/V5/MQTTReasonCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* Contributors:
* Ian Craggs - initial API and implementation and/or initial documentation
*******************************************************************************/
#ifndef MQTTREASONCODES_H_
#define MQTTREASONCODES_H_

enum MQTTReasonCodes {
MQTTREASONCODE_SUCCESS = 0,
Expand Down Expand Up @@ -61,3 +63,5 @@ enum MQTTReasonCodes {
MQTTREASONCODE_SUBSCRIPTION_IDENTIFIERS_NOT_SUPPORTED = 161,
MQTTREASONCODE_WILDCARD_SUBSCRIPTION_NOT_SUPPORTED = 162
};

#endif // MQTTREASONCODES_H_
6 changes: 3 additions & 3 deletions MQTTPacket/src/V5/MQTTV5Connect.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
#include "MQTTConnect.h"

DLLExport int32_t MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options,
MQTTProperties* connectProperties, MQTTProperties* willProperties);
MQTTProperties* connectProperties);

DLLExport int32_t MQTTV5Deserialize_connect(MQTTProperties* willProperties,
MQTTProperties* connectProperties, MQTTPacket_connectData* data, unsigned char* buf, int32_t len);
DLLExport int32_t MQTTV5Deserialize_connect(MQTTProperties* connectProperties, MQTTPacket_connectData* data,
unsigned char* buf, int32_t len);

DLLExport int32_t MQTTV5Serialize_connack(unsigned char* buf, int32_t buflen, unsigned char connack_rc,
unsigned char sessionPresent, MQTTProperties* connackProperties);
Expand Down
4 changes: 4 additions & 0 deletions MQTTPacket/src/V5/MQTTV5Packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#ifndef MQTTV5PACKET_H_
#define MQTTV5PACKET_H_

#ifndef MQTTV5
#define MQTTV5
#endif

#include "MQTTPacket.h"

#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
Expand Down
6 changes: 4 additions & 2 deletions MQTTPacket/test/test2.c
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,13 @@ int test1(struct Options options)
data.will.qos = 1;
data.will.retained = 0;
data.will.topicName.cstring = "will topic";
data.will.properties = &willProperties;

rc = MQTTV5Serialize_connect(buf, buflen, &data, &connectProperties, &willProperties);
rc = MQTTV5Serialize_connect(buf, buflen, &data, &connectProperties);
assert("good rc from serialize connect", rc > 0, "rc was %d\n", rc);

rc = MQTTV5Deserialize_connect(&outWillProperties, &outConnectProperties, &data_after, buf, buflen);
data_after.will.properties = &outWillProperties;
rc = MQTTV5Deserialize_connect(&outConnectProperties, &data_after, buf, buflen);
assert("good rc from deserialize connect", rc == 1, "rc was %d\n", rc);

/* data after should be the same as data before */
Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/test/test3.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ int test1(struct Options options)
rc = MQTTProperties_add(&properties, &one);
assert("add properties rc should be 0", rc == 0, "rc was different %d\n", rc);

len = MQTTV5Serialize_connect((unsigned char *)buf, buflen, &data, &properties, NULL);
len = MQTTV5Serialize_connect((unsigned char *)buf, buflen, &data, &properties);
rc = transport_sendPacketBuffer(mysock, buf, len);
assert("rc and len should be the same", rc == len, "rc was different %d\n", rc);

Expand Down

0 comments on commit db4f1c4

Please sign in to comment.