diff --git a/src/MQTTSNConnect.h b/src/MQTTSNConnect.h index 3a30c27..5814754 100644 --- a/src/MQTTSNConnect.h +++ b/src/MQTTSNConnect.h @@ -24,7 +24,7 @@ typedef struct /** The version number of this structure. Must be 0. */ int struct_version; - MQTTString clientID; + MQTTSNString clientID; int duration; int cleansession; int willFlag; @@ -41,32 +41,32 @@ int MQTTSNDeserialize_connack(int* connack_rc, unsigned char* buf, int buflen); int MQTTSNSerialize_disconnect(unsigned char* buf, int buflen, int duration); int MQTTSNDeserialize_disconnect(int* duration, unsigned char* buf, int buflen); -int MQTTSNSerialize_pingreq(unsigned char* buf, int buflen, MQTTString clientid); -int MQTTSNDeserialize_pingreq(MQTTString* clientID, unsigned char* buf, int len); +int MQTTSNSerialize_pingreq(unsigned char* buf, int buflen, MQTTSNString clientid); +int MQTTSNDeserialize_pingreq(MQTTSNString* clientID, unsigned char* buf, int len); int MQTTSNSerialize_pingresp(unsigned char* buf, int buflen); int MQTTSNDeserialize_pingresp(unsigned char* buf, int buflen); -int MQTTSNSerialize_willmsg(unsigned char* buf, int buflen, MQTTString willMsg); -int MQTTSNDeserialize_willmsg(MQTTString* willMsg, unsigned char* buf, int buflen); +int MQTTSNSerialize_willmsg(unsigned char* buf, int buflen, MQTTSNString willMsg); +int MQTTSNDeserialize_willmsg(MQTTSNString* willMsg, unsigned char* buf, int buflen); int MQTTSNSerialize_willmsgreq(unsigned char* buf, int buflen); int MQTTSNDeserialize_willmsgreq(unsigned char* buf, int buflen); -int MQTTSNSerialize_willmsgupd(unsigned char* buf, int buflen, MQTTString willMsg); -int MQTTSNDeserialize_willmsgupd(MQTTString* willMsg, unsigned char* buf, int buflen); +int MQTTSNSerialize_willmsgupd(unsigned char* buf, int buflen, MQTTSNString willMsg); +int MQTTSNDeserialize_willmsgupd(MQTTSNString* willMsg, unsigned char* buf, int buflen); int MQTTSNSerialize_willmsgresp(unsigned char* buf, int buflen, int resp_rc); int MQTTSNDeserialize_willmsgresp(int* resp_rc, unsigned char* buf, int buflen); -int MQTTSNSerialize_willtopic(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTString willTopic); -int MQTTSNDeserialize_willtopic(int *willQoS, int *willRetain, MQTTString* willTopic, unsigned char* buf, int buflen); +int MQTTSNSerialize_willtopic(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTSNString willTopic); +int MQTTSNDeserialize_willtopic(int *willQoS, int *willRetain, MQTTSNString* willTopic, unsigned char* buf, int buflen); int MQTTSNSerialize_willtopicreq(unsigned char* buf, int buflen); int MQTTSNDeserialize_willtopicreq(unsigned char* buf, int buflen); -int MQTTSNSerialize_willtopicupd(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTString willTopic); -int MQTTSNDeserialize_willtopicupd(int *willQoS, int *willRetain, MQTTString* willTopic, unsigned char* buf, int buflen); +int MQTTSNSerialize_willtopicupd(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTSNString willTopic); +int MQTTSNDeserialize_willtopicupd(int *willQoS, int *willRetain, MQTTSNString* willTopic, unsigned char* buf, int buflen); int MQTTSNSerialize_willtopicresp(unsigned char* buf, int buflen, int resp_rc); int MQTTSNDeserialize_willtopicresp(int* resp_rc, unsigned char* buf, int buflen); diff --git a/src/MQTTSNConnectClient.c b/src/MQTTSNConnectClient.c index f19d13d..f5f9926 100644 --- a/src/MQTTSNConnectClient.c +++ b/src/MQTTSNConnectClient.c @@ -29,7 +29,7 @@ int MQTTSNSerialize_connectLength(MQTTSNPacket_connectData* options) int len = 0; FUNC_ENTRY; - len = 5 + MQTTstrlen(options->clientID); + len = 5 + MQTTSNstrlen(options->clientID); FUNC_EXIT_RC(len); return len; } @@ -160,14 +160,14 @@ exit: * @param clientid optional string, not added to packet string == NULL * @return serialized length, or error if 0 */ -int MQTTSNSerialize_pingreq(unsigned char* buf, int buflen, MQTTString clientid) +int MQTTSNSerialize_pingreq(unsigned char* buf, int buflen, MQTTSNString clientid) { int rc = -1; unsigned char *ptr = buf; int len = 0; FUNC_ENTRY; - if ((len = MQTTSNPacket_len(MQTTstrlen(clientid) + 1)) > buflen) + if ((len = MQTTSNPacket_len(MQTTSNstrlen(clientid) + 1)) > buflen) { rc = MQTTSNPACKET_BUFFER_TOO_SHORT; goto exit; @@ -222,7 +222,7 @@ exit: * @param willTopic the topic of the will message * @return serialized length, or error if 0 */ -int MQTTSNSerialize_willtopic1(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTString willTopic, +int MQTTSNSerialize_willtopic1(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTSNString willTopic, enum MQTTSN_msgTypes packet_type) { unsigned char *ptr = buf; @@ -231,7 +231,7 @@ int MQTTSNSerialize_willtopic1(unsigned char* buf, int buflen, int willQoS, int int rc = -1; FUNC_ENTRY; - if ((len = MQTTSNPacket_len(MQTTstrlen(willTopic) + 2)) > buflen) + if ((len = MQTTSNPacket_len(MQTTSNstrlen(willTopic) + 2)) > buflen) { rc = MQTTSNPACKET_BUFFER_TOO_SHORT; goto exit; @@ -263,7 +263,7 @@ exit: * @param willTopic the topic of the will message * @return serialized length, or error if 0 */ -int MQTTSNSerialize_willtopicupd(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTString willTopic) +int MQTTSNSerialize_willtopicupd(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTSNString willTopic) { return MQTTSNSerialize_willtopic1(buf, buflen, willQoS, willRetain, willTopic, MQTTSN_WILLTOPICUPD); } @@ -278,7 +278,7 @@ int MQTTSNSerialize_willtopicupd(unsigned char* buf, int buflen, int willQoS, in * @param willTopic the topic of the will message * @return serialized length, or error if 0 */ -int MQTTSNSerialize_willtopic(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTString willTopic) +int MQTTSNSerialize_willtopic(unsigned char* buf, int buflen, int willQoS, int willRetain, MQTTSNString willTopic) { return MQTTSNSerialize_willtopic1(buf, buflen, willQoS, willRetain, willTopic, MQTTSN_WILLTOPIC); } @@ -291,14 +291,14 @@ int MQTTSNSerialize_willtopic(unsigned char* buf, int buflen, int willQoS, int w * @param willMsg the will message * @return serialized length, or error if 0 */ -int MQTTSNSerialize_willmsg1(unsigned char* buf, int buflen, MQTTString willMsg, enum MQTTSN_msgTypes packet_type) +int MQTTSNSerialize_willmsg1(unsigned char* buf, int buflen, MQTTSNString willMsg, enum MQTTSN_msgTypes packet_type) { unsigned char *ptr = buf; int len = 0; int rc = -1; FUNC_ENTRY; - if ((len = MQTTSNPacket_len(MQTTstrlen(willMsg) + 1)) > buflen) + if ((len = MQTTSNPacket_len(MQTTSNstrlen(willMsg) + 1)) > buflen) { rc = MQTTSNPACKET_BUFFER_TOO_SHORT; goto exit; @@ -322,7 +322,7 @@ int MQTTSNSerialize_willmsg1(unsigned char* buf, int buflen, MQTTString willMsg, * @param willMsg the will message * @return serialized length, or error if 0 */ -int MQTTSNSerialize_willmsg(unsigned char* buf, int buflen, MQTTString willMsg) +int MQTTSNSerialize_willmsg(unsigned char* buf, int buflen, MQTTSNString willMsg) { return MQTTSNSerialize_willmsg1(buf, buflen, willMsg, MQTTSN_WILLMSG); } @@ -335,7 +335,7 @@ int MQTTSNSerialize_willmsg(unsigned char* buf, int buflen, MQTTString willMsg) * @param willMsg the will message * @return serialized length, or error if 0 */ -int MQTTSNSerialize_willmsgupd(unsigned char* buf, int buflen, MQTTString willMsg) +int MQTTSNSerialize_willmsgupd(unsigned char* buf, int buflen, MQTTSNString willMsg) { return MQTTSNSerialize_willmsg1(buf, buflen, willMsg, MQTTSN_WILLMSGUPD); } diff --git a/src/MQTTSNConnectServer.c b/src/MQTTSNConnectServer.c index dfaa831..20db5de 100644 --- a/src/MQTTSNConnectServer.c +++ b/src/MQTTSNConnectServer.c @@ -194,7 +194,7 @@ exit: * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_pingreq(MQTTString* clientID, unsigned char* buf, int len) +int MQTTSNDeserialize_pingreq(MQTTSNString* clientID, unsigned char* buf, int len) { unsigned char* curdata = buf; unsigned char* enddata = &buf[len]; @@ -255,7 +255,7 @@ exit: * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_willtopic1(int *willQoS, int *willRetain, MQTTString* willTopic, unsigned char* buf, int len, +int MQTTSNDeserialize_willtopic1(int *willQoS, int *willRetain, MQTTSNString* willTopic, unsigned char* buf, int len, enum MQTTSN_msgTypes packet_type) { MQTTSNFlags flags; @@ -294,7 +294,7 @@ exit: * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_willtopic(int *willQoS, int *willRetain, MQTTString* willTopic, unsigned char* buf, int len) +int MQTTSNDeserialize_willtopic(int *willQoS, int *willRetain, MQTTSNString* willTopic, unsigned char* buf, int len) { return MQTTSNDeserialize_willtopic1(willQoS, willRetain, willTopic, buf, len, MQTTSN_WILLTOPIC); } @@ -306,7 +306,7 @@ int MQTTSNDeserialize_willtopic(int *willQoS, int *willRetain, MQTTString* willT * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_willtopicupd(int *willQoS, int *willRetain, MQTTString* willTopic, unsigned char* buf, int len) +int MQTTSNDeserialize_willtopicupd(int *willQoS, int *willRetain, MQTTSNString* willTopic, unsigned char* buf, int len) { return MQTTSNDeserialize_willtopic1(willQoS, willRetain, willTopic, buf, len, MQTTSN_WILLTOPICUPD); } @@ -319,7 +319,7 @@ int MQTTSNDeserialize_willtopicupd(int *willQoS, int *willRetain, MQTTString* wi * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_willmsg1(MQTTString* willMsg, unsigned char* buf, int len, enum MQTTSN_msgTypes packet_type) +int MQTTSNDeserialize_willmsg1(MQTTSNString* willMsg, unsigned char* buf, int len, enum MQTTSN_msgTypes packet_type) { unsigned char* curdata = buf; unsigned char* enddata = &buf[len]; @@ -352,7 +352,7 @@ exit: * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_willmsg(MQTTString* willMsg, unsigned char* buf, int len) +int MQTTSNDeserialize_willmsg(MQTTSNString* willMsg, unsigned char* buf, int len) { return MQTTSNDeserialize_willmsg1(willMsg, buf, len, MQTTSN_WILLMSG); } @@ -364,7 +364,7 @@ int MQTTSNDeserialize_willmsg(MQTTString* willMsg, unsigned char* buf, int len) * @param len the length in bytes of the data in the supplied buffer * @return error code. 1 is success, 0 is failure */ -int MQTTSNDeserialize_willmsgupd(MQTTString* willMsg, unsigned char* buf, int len) +int MQTTSNDeserialize_willmsgupd(MQTTSNString* willMsg, unsigned char* buf, int len) { return MQTTSNDeserialize_willmsg1(willMsg, buf, len, MQTTSN_WILLMSGUPD); } diff --git a/src/MQTTSNDeserializePublish.c b/src/MQTTSNDeserializePublish.c index c2b6053..d1da741 100644 --- a/src/MQTTSNDeserializePublish.c +++ b/src/MQTTSNDeserializePublish.c @@ -26,7 +26,7 @@ * @param qos returned integer - the MQTT QoS value * @param retained returned integer - the MQTT retained flag * @param packetid returned integer - the MQTT packet identifier - * @param topicName returned MQTTString - the MQTT topic in the publish + * @param topicName returned MQTTSNString - the MQTT topic in the publish * @param payload returned byte buffer - the MQTT publish payload * @param payloadlen returned integer - the length of the MQTT payload * @param buf the raw buffer data, of the correct length determined by the remaining length field @@ -152,12 +152,12 @@ exit: * Deserializes the supplied (wire) buffer into register data * @param topicid returned topic id * @param packetid returned integer - the MQTT packet identifier - * @param topicName returned MQTTString - the MQTT topic in the register + * @param topicName returned MQTTSNString - the MQTT topic in the register * @param buf the raw buffer data, of the correct length determined by the remaining length field * @param buflen the length in bytes of the data in the supplied buffer * @return error code. 1 is success */ -int MQTTSNDeserialize_register(unsigned short* topicid, unsigned short* packetid, MQTTString* topicname, +int MQTTSNDeserialize_register(unsigned short* topicid, unsigned short* packetid, MQTTSNString* topicname, unsigned char* buf, int buflen) { unsigned char* curdata = buf; diff --git a/src/MQTTSNPacket.c b/src/MQTTSNPacket.c index aad7d70..ef1cc64 100644 --- a/src/MQTTSNPacket.c +++ b/src/MQTTSNPacket.c @@ -184,38 +184,38 @@ int getLenStringLen(char* ptr) } -void writeMQTTSNString(unsigned char** pptr, MQTTString mqttstring) +void writeMQTTSNString(unsigned char** pptr, MQTTSNString MQTTSNString) { - if (mqttstring.lenstring.len > 0) + if (MQTTSNString.lenstring.len > 0) { - memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len); - *pptr += mqttstring.lenstring.len; + memcpy(*pptr, MQTTSNString.lenstring.data, MQTTSNString.lenstring.len); + *pptr += MQTTSNString.lenstring.len; } - else if (mqttstring.cstring) - writeCString(pptr, mqttstring.cstring); + else if (MQTTSNString.cstring) + writeCString(pptr, MQTTSNString.cstring); } /** - * @param mqttstring the MQTTString structure into which the data is to be read + * @param MQTTSNString the MQTTSNString structure into which the data is to be read * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned * @param enddata pointer to the end of the data: do not read beyond * @return 1 if successful, 0 if not */ -int readMQTTSNString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata) +int readMQTTSNString(MQTTSNString* MQTTSNString, unsigned char** pptr, unsigned char* enddata) { int rc = 0; FUNC_ENTRY; - mqttstring->lenstring.len = enddata - *pptr; - if (mqttstring->lenstring.len > 0) + MQTTSNString->lenstring.len = enddata - *pptr; + if (MQTTSNString->lenstring.len > 0) { - mqttstring->lenstring.data = (char*)*pptr; - *pptr += mqttstring->lenstring.len; + MQTTSNString->lenstring.data = (char*)*pptr; + *pptr += MQTTSNString->lenstring.len; } else - mqttstring->lenstring.data = NULL; - mqttstring->cstring = NULL; + MQTTSNString->lenstring.data = NULL; + MQTTSNString->cstring = NULL; rc = 1; FUNC_EXIT_RC(rc); return rc; @@ -223,18 +223,18 @@ int readMQTTSNString(MQTTString* mqttstring, unsigned char** pptr, unsigned char /** - * Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string - * @param mqttstring the string to return the length of + * Return the length of the MQTTSNString - C string if there is one, otherwise the length delimited string + * @param MQTTSNString the string to return the length of * @return the length of the string */ -int MQTTstrlen(MQTTString mqttstring) +int MQTTSNstrlen(MQTTSNString MQTTSNString) { int rc = 0; - if (mqttstring.cstring) - rc = strlen(mqttstring.cstring); + if (MQTTSNString.cstring) + rc = strlen(MQTTSNString.cstring); else - rc = mqttstring.lenstring.len; + rc = MQTTSNString.lenstring.len; return rc; } diff --git a/src/MQTTSNPacket.h b/src/MQTTSNPacket.h index 3449e6a..f696a92 100644 --- a/src/MQTTSNPacket.h +++ b/src/MQTTSNPacket.h @@ -108,17 +108,17 @@ typedef struct { int len; char* data; -} MQTTLenString; +} MQTTSNLenString; typedef struct { char* cstring; - MQTTLenString lenstring; -} MQTTString; + MQTTSNLenString lenstring; +} MQTTSNString; -#define MQTTString_initializer {NULL, {0, NULL}} +#define MQTTSNString_initializer {NULL, {0, NULL}} -int MQTTstrlen(MQTTString mqttstring); +int MQTTSNstrlen(MQTTSNString mqttsnstring); #include "MQTTSNConnect.h" #include "MQTTSNPublish.h" @@ -138,9 +138,9 @@ int readInt(unsigned char** pptr); char readChar(unsigned char** pptr); void writeChar(unsigned char** pptr, char c); void writeInt(unsigned char** pptr, int anInt); -int readMQTTSNString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); +int readMQTTSNString(MQTTSNString* mqttstring, unsigned char** pptr, unsigned char* enddata); void writeCString(unsigned char** pptr, char* string); -void writeMQTTSNString(unsigned char** pptr, MQTTString mqttstring); +void writeMQTTSNString(unsigned char** pptr, MQTTSNString mqttstring); int MQTTSNPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, size_t)); diff --git a/src/MQTTSNPublish.h b/src/MQTTSNPublish.h index ded7fdf..792dcd8 100644 --- a/src/MQTTSNPublish.h +++ b/src/MQTTSNPublish.h @@ -28,14 +28,13 @@ int MQTTSNDeserialize_puback(unsigned short* topicid, unsigned short* packetid, unsigned char* returncode, unsigned char* buf, int buflen); int MQTTSNSerialize_pubrec(unsigned char* buf, int buflen, unsigned short packetid); -int MQTTSNSerialize_pubrel(unsigned char* buf, int buflen, int dup, unsigned short packetid); int MQTTSNSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid); int MQTTSNDeserialize_ack(unsigned char* packettype, unsigned short* packetid, unsigned char* buf, int buflen); int MQTTSNSerialize_register(unsigned char* buf, int buflen, unsigned short topicid, unsigned short packetid, - MQTTString* topicname); -int MQTTSNDeserialize_register(unsigned short* topicid, unsigned short* packetid, MQTTString* topicname, + MQTTSNString* topicname); +int MQTTSNDeserialize_register(unsigned short* topicid, unsigned short* packetid, MQTTSNString* topicname, unsigned char* buf, int buflen); int MQTTSNSerialize_regack(unsigned char* buf, int buflen, unsigned short topicid, unsigned short packetid, diff --git a/src/MQTTSNSearchClient.c b/src/MQTTSNSearchClient.c index f9ff623..de2f4fe 100644 --- a/src/MQTTSNSearchClient.c +++ b/src/MQTTSNSearchClient.c @@ -20,6 +20,14 @@ #include +/** + * Deserializes the supplied (wire) buffer into advertise data + * @param gatewayid the returned gateway id + * @param duration the returned duration - the time interval until the next advertise will be sent + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ int MQTTSNDeserialize_advertise(unsigned char* gatewayid, unsigned short* duration, unsigned char* buf, int buflen) { unsigned char* curdata = buf; @@ -46,6 +54,14 @@ exit: } + +/** + * Serializes the supplied searchgw data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param radius the broadcast radius of this message + * @return the length of the serialized data. <= 0 indicates error + */ int MQTTSNSerialize_searchgw(unsigned char* buf, int buflen, unsigned char radius) { unsigned char *ptr = buf; @@ -71,6 +87,15 @@ exit: } +/** + * Deserializes the supplied (wire) buffer into gwinfo data + * @param gatewayid the returned gateway id + * @param gatewayaddress_len the optional returned length of the gateway address (0 if none) + * @param gatewayaddress the optional returned gateway address (set to NULL if none) + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayaddress_len, unsigned char** gatewayaddress, unsigned char* buf, int buflen) { @@ -91,7 +116,7 @@ int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayad *gatewayid = readChar(&curdata); *gatewayaddress_len = enddata - curdata; - *gatewayaddress = curdata; + *gatewayaddress = (gatewayaddress_len > 0) ? curdata : NULL; rc = 1; exit: @@ -99,7 +124,3 @@ exit: return rc; } - - - - diff --git a/src/MQTTSNSearchServer.c b/src/MQTTSNSearchServer.c index b69c244..6fcc479 100644 --- a/src/MQTTSNSearchServer.c +++ b/src/MQTTSNSearchServer.c @@ -20,6 +20,14 @@ #include +/** + * Serializes the supplied advertise data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param radius the broadcast radius of this message + * @param duration - the time interval until the next advertise will be sent + * @return the length of the serialized data. <= 0 indicates error + */ int MQTTSNSerialize_advertise(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short duration) { unsigned char *ptr = buf; @@ -45,6 +53,13 @@ exit: } +/** + * Deserializes the supplied (wire) buffer into searchgw data + * @param radius the returned broadcast radius of this message + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ int MQTTSNDeserialize_searchgw(unsigned char* radius, unsigned char* buf, int buflen) { unsigned char* curdata = buf; @@ -70,6 +85,15 @@ exit: } +/** + * Serializes the supplied gwinfo data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param gatewayid the gateway id + * @param gatewayaddress_len the optional length of the gateway address (0 if none) + * @param gatewayaddress the optional gateway address (NULL if none) + * @return the length of the serialized data. <= 0 indicates error + */ int MQTTSNSerialize_gwinfo(unsigned char* buf, int buflen, unsigned char gatewayid, unsigned short gatewayaddress_len, unsigned char* gatewayaddress) { @@ -87,8 +111,11 @@ int MQTTSNSerialize_gwinfo(unsigned char* buf, int buflen, unsigned char gateway writeChar(&ptr, MQTTSN_GWINFO); /* write message type */ writeChar(&ptr, gatewayid); - memcpy(ptr, gatewayaddress, gatewayaddress_len); - ptr += gatewayaddress_len; + if (gatewayaddress_len > 0 && gatewayaddress != NULL) + { + memcpy(ptr, gatewayaddress, gatewayaddress_len); + ptr += gatewayaddress_len; + } rc = ptr - buf; exit: diff --git a/src/MQTTSNSerializePublish.c b/src/MQTTSNSerializePublish.c index 16e2528..e495f76 100644 --- a/src/MQTTSNSerializePublish.c +++ b/src/MQTTSNSerializePublish.c @@ -225,7 +225,7 @@ int MQTTSNSerialize_registerLength(int topicnamelen) * @return the length of the serialized data. <= 0 indicates error */ int MQTTSNSerialize_register(unsigned char* buf, int buflen, unsigned short topicid, unsigned short packetid, - MQTTString* topicname) + MQTTSNString* topicname) { unsigned char *ptr = buf; int len = 0; diff --git a/src/MQTTSNSubscribeClient.c b/src/MQTTSNSubscribeClient.c index f422d7a..cbe5074 100644 --- a/src/MQTTSNSubscribeClient.c +++ b/src/MQTTSNSubscribeClient.c @@ -38,6 +38,16 @@ int MQTTSNSerialize_subscribeLength(MQTTSN_topicid* topicFilter) } +/** + * Serializes the supplied subscribe data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param dup integer - the MQTT-SN dup flag + * @param qos integer - the MQTT-SN QoS value + * @param packetid integer - the MQTT-SN packet identifier + * @param topic MQTTSN_topicid - the MQTT-SN topic in the subscribe + * @return the length of the serialized data. <= 0 indicates error + */ int MQTTSNSerialize_subscribe(unsigned char* buf, int buflen, int dup, int qos, unsigned short packetid, MQTTSN_topicid* topicFilter) { unsigned char *ptr = buf; @@ -84,6 +94,16 @@ exit: } +/** + * Deserializes the supplied (wire) buffer into suback data + * @param qos the returned qos + * @param topicid returned if "accepted" the value which will be used by the gateway in subsequent PUBLISH packets + * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE + * @param returncode returned - "accepted" or rejection reason + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ int MQTTSNDeserialize_suback(int* qos, unsigned short* topicid, unsigned short* packetid, unsigned char* returncode, unsigned char* buf, int buflen) { diff --git a/src/MQTTSNSubscribeServer.c b/src/MQTTSNSubscribeServer.c index dd7d01d..c49cdbb 100644 --- a/src/MQTTSNSubscribeServer.c +++ b/src/MQTTSNSubscribeServer.c @@ -18,6 +18,17 @@ #include "MQTTSNPacket.h" #include + +/** + * Deserializes the supplied (wire) buffer into subscribe data + * @param dup the returned MQTT-SN dup flag + * @param qos the returned qos + * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE + * @param topicFilter returned - the topic filter - normal, predefined or short + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ int MQTTSNDeserialize_subscribe(int* dup, int* qos, unsigned short* packetid, MQTTSN_topicid* topicFilter, unsigned char* buf, int buflen) { @@ -64,6 +75,16 @@ exit: } +/** + * Serializes the supplied suback data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param qos integer - the MQTT-SN QoS value + * @param topicid if "accepted" the value which will be used by the gateway in subsequent PUBLISH packets + * @param packetid integer - the MQTT-SN packet identifier + * @param returncode returned - "accepted" or rejection reason + * @return the length of the serialized data. <= 0 indicates error + */ int MQTTSNSerialize_suback(unsigned char* buf, int buflen, int qos, unsigned short topicid, unsigned short packetid, unsigned char returncode) { diff --git a/src/MQTTSNUnsubscribeClient.c b/src/MQTTSNUnsubscribeClient.c index 1eeb8d0..092ce48 100644 --- a/src/MQTTSNUnsubscribeClient.c +++ b/src/MQTTSNUnsubscribeClient.c @@ -82,6 +82,13 @@ exit: } +/** + * Deserializes the supplied (wire) buffer into unsuback data + * @param packetid returned - the same value as the one contained in the corresponding SUBSCRIBE + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ int MQTTSNDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) { unsigned char* curdata = buf; diff --git a/test/test1.c b/test/test1.c index 5f4983c..15da41b 100644 --- a/test/test1.c +++ b/test/test1.c @@ -247,7 +247,7 @@ void myassert(char* filename, int lineno, char* description, int value, char* fo #define min(a, b) ((a < b) ? a : b) -int checkMQTTStrings(MQTTString a, MQTTString b) +int checkMQTTSNStrings(MQTTSNString a, MQTTSNString b) { if (!a.lenstring.data) { @@ -295,7 +295,7 @@ int checkConnectPackets(MQTTSNPacket_connectData* before, MQTTSNPacket_connectDa before->struct_version == after->struct_version, "struct_versions were different\n", rc); assert("ClientIDs should be the same", - checkMQTTStrings(before->clientID, after->clientID), "ClientIDs were different\n", rc); + checkMQTTSNStrings(before->clientID, after->clientID), "ClientIDs were different\n", rc); assert("durations should be the same", before->duration == after->duration, "durations were different\n", rc); @@ -316,7 +316,7 @@ int test1(struct Options options) int rc = 0; unsigned char buf[100]; int buflen = sizeof(buf); - MQTTString clientid = MQTTString_initializer, clientid_after = MQTTString_initializer; + MQTTSNString clientid = MQTTSNString_initializer, clientid_after = MQTTSNString_initializer; int duration_after = -1; fprintf(xml, " 0, "rc was %d\n", rc); @@ -512,7 +512,7 @@ int test3(struct Options options) memset(buf, '\0', sizeof(buf)); int willQoS = 1, willRetain = 1, willQoS1 = 0, willRetain1 = 0; - MQTTString willTopic = MQTTString_initializer, willTopic1 = MQTTString_initializer; + MQTTSNString willTopic = MQTTSNString_initializer, willTopic1 = MQTTSNString_initializer; willTopic.cstring = "a will topic"; rc = MQTTSNSerialize_willtopic(buf, buflen, willQoS, willRetain, willTopic); assert("good rc from serialize willtopic", rc > 0, "rc was %d\n", rc); @@ -521,11 +521,11 @@ int test3(struct Options options) assert("good rc from deserialize willtopic", rc == 1, "rc was %d\n", rc); assert("willQoSs are the same", willQoS == willQoS1, "willQoS1 was %d\n", willQoS1); assert("willRetains are the same", willRetain == willRetain1, "willRetain1 was %d\n", willRetain1); - assert("willTopics are the same", checkMQTTStrings(willTopic, willTopic1), "willTopic1 was %.s\n", willTopic1.lenstring.data); + assert("willTopics are the same", checkMQTTSNStrings(willTopic, willTopic1), "willTopic1 was %.s\n", willTopic1.lenstring.data); memset(buf, '\0', sizeof(buf)); willQoS = 2; willRetain = 1; willQoS1 = 0; willRetain1 = 0; - MQTTString initTopic = MQTTString_initializer; + MQTTSNString initTopic = MQTTSNString_initializer; memcpy(&willTopic, &initTopic, sizeof(initTopic)); memcpy(&willTopic1, &initTopic, sizeof(initTopic)); willTopic.cstring = "a will topic update"; @@ -536,17 +536,17 @@ int test3(struct Options options) assert("good rc from deserialize willtopicupd", rc == 1, "rc was %d\n", rc); assert("willQoSs are the same", willQoS == willQoS1, "willQoS1 was %d\n", willQoS1); assert("willRetains are the same", willRetain == willRetain1, "willRetain1 was %d\n", willRetain1); - assert("willTopics are the same", checkMQTTStrings(willTopic, willTopic1), "willTopic1 was %.s\n", willTopic1.lenstring.data); + assert("willTopics are the same", checkMQTTSNStrings(willTopic, willTopic1), "willTopic1 was %.s\n", willTopic1.lenstring.data); memset(buf, '\0', sizeof(buf)); - MQTTString willMsg = MQTTString_initializer, willMsg1 = MQTTString_initializer; + MQTTSNString willMsg = MQTTSNString_initializer, willMsg1 = MQTTSNString_initializer; willMsg.cstring = "a will message"; rc = MQTTSNSerialize_willmsg(buf, buflen, willMsg); assert("good rc from serialize willmsg", rc > 0, "rc was %d\n", rc); rc = MQTTSNDeserialize_willmsg(&willMsg1, buf, rc); assert("good rc from deserialize willmsg", rc == 1, "rc was %d\n", rc); - assert("willMsgs are the same", checkMQTTStrings(willMsg, willMsg1), "willMsg1 was %.s\n", willMsg1.lenstring.data); + assert("willMsgs are the same", checkMQTTSNStrings(willMsg, willMsg1), "willMsg1 was %.s\n", willMsg1.lenstring.data); memset(buf, '\0', sizeof(buf)); memcpy(&willMsg, &initTopic, sizeof(initTopic)); @@ -557,7 +557,7 @@ int test3(struct Options options) rc = MQTTSNDeserialize_willmsgupd(&willMsg1, buf, rc); assert("good rc from deserialize willmsgupd", rc == 1, "rc was %d\n", rc); - assert("willMsgs are the same", checkMQTTStrings(willMsg, willMsg1), "willMsg1 was %.s\n", willMsg1.lenstring.data); + assert("willMsgs are the same", checkMQTTSNStrings(willMsg, willMsg1), "willMsg1 was %.s\n", willMsg1.lenstring.data); int resp_rc = 33, resp_rc2 = 0; rc = MQTTSNSerialize_willmsgresp(buf, buflen, resp_rc); @@ -776,7 +776,7 @@ int test9(struct Options options) int buflen = sizeof(buf); unsigned short packetid = 255, packetid2 = 0; unsigned short topicid = 233, topicid2 = 0; - MQTTString topicname = MQTTString_initializer, topicname2 = MQTTString_initializer; + MQTTSNString topicname = MQTTSNString_initializer, topicname2 = MQTTSNString_initializer; fprintf(xml, " 0, "rc was %d\n", rc); + + rc = MQTTSNDeserialize_advertise(&gatewayid2, &duration2, buf, buflen); + assert("good rc from deserialize advertise", rc == 1, "rc was %d\n", rc); + + assert("gatewayids should be the same", gatewayid == gatewayid2, "gatewayids were different %d\n", gatewayid2); + assert("return codes should be the same", duration == duration2, "return_codes were different %d\n", duration2); + +/* exit: */ + MyLog(LOGA_INFO, "TEST11: test %s. %d tests run, %d failures.", + (failures == 0) ? "passed" : "failed", tests, failures); + write_test_result(); + return failures; +} + + +int test12(struct Options options) +{ + int rc = 0; + unsigned char buf[100]; + int buflen = sizeof(buf); + unsigned char radius = 255, radius2 = 0; + + fprintf(xml, " 0, "rc was %d\n", rc); + + rc = MQTTSNDeserialize_searchgw(&radius2, buf, buflen); + assert("good rc from deserialize searchgw", rc == 1, "rc was %d\n", rc); + + assert("radiuss should be the same", radius == radius2, "radiuss were different %d\n", radius2); + +/* exit: */ + MyLog(LOGA_INFO, "TEST12: test %s. %d tests run, %d failures.", + (failures == 0) ? "passed" : "failed", tests, failures); + write_test_result(); + return failures; +} + + +int test13(struct Options options) +{ + int rc = 0; + unsigned char buf[100]; + int buflen = sizeof(buf); + unsigned char gatewayid = 255, gatewayid2 = 0; + unsigned short gatewayaddress_len = 16, gatewayaddress_len2 = 0; + unsigned char gatewayaddress[40] = "a gateway address", *gatewayaddress2 = NULL; + + fprintf(xml, " 0, "rc was %d\n", rc); + + rc = MQTTSNDeserialize_gwinfo(&gatewayid2, &gatewayaddress_len2, &gatewayaddress2, buf, buflen); + assert("good rc from deserialize gwinfo", rc == 1, "rc was %d\n", rc); + + assert("gatewayids should be the same", gatewayid == gatewayid2, "gatewayids were different %d\n", gatewayid2); + assert("gateway lengths should be the same", gatewayaddress_len == gatewayaddress_len2, "gateway lengths were different %d\n", gatewayaddress_len2); + assert("gateway addresses should be the same", memcmp(gatewayaddress, gatewayaddress2, gatewayaddress_len) == 0, + "gateway addresses were different %.10s\n", gatewayaddress2); + +/* exit: */ + MyLog(LOGA_INFO, "TEST13: test %s. %d tests run, %d failures.", + (failures == 0) ? "passed" : "failed", tests, failures); + write_test_result(); + return failures; +} + + int main(int argc, char** argv) { int rc = 0; - int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10}; + int (*tests[])() = {NULL, test1, test2, test3, test4, test5, test6, test7, test8, test9, test10, test11, test12, test13}; xml = fopen("TEST-test1.xml", "w"); fprintf(xml, "\n", (int)(ARRAY_SIZE(tests) - 1));