Skip to content

Commit

Permalink
Separating public API from internals and adding side-by-side v3/v5 test.
Browse files Browse the repository at this point in the history
  • Loading branch information
CIPop committed Aug 16, 2023
1 parent e70d4e3 commit a812295
Show file tree
Hide file tree
Showing 34 changed files with 1,211 additions and 370 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
/doc/MQTTClient-C/
.vscode
**/html
**/latex
**/latex
**/Testing
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Ian Craggs - initial version
#*******************************************************************************/

cmake_minimum_required(VERSION 2.8)
cmake_minimum_required(VERSION 2.8.12)
project (embedded-mqtt)
set(CMAKE_BUILD_TYPE Debug)

Expand Down
1 change: 0 additions & 1 deletion MQTTClient-C/test/test1.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ START_TIME_TYPE start_clock(void)
#else
#define mqsleep sleep
#define START_TIME_TYPE struct timeval
/* TODO - unused - remove? static struct timeval start_time; */
START_TIME_TYPE start_clock(void)
{
struct timeval start_time;
Expand Down
1 change: 0 additions & 1 deletion MQTTClient/test/test1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ START_TIME_TYPE start_clock(void)
#else
#define mqsleep sleep
#define START_TIME_TYPE struct timeval
/* TODO - unused - remove? static struct timeval start_time; */
START_TIME_TYPE start_clock(void)
{
struct timeval start_time;
Expand Down
11 changes: 7 additions & 4 deletions MQTTPacket/samples/transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <signal.h>
#endif

#if defined(WIN32)
Expand Down Expand Up @@ -171,10 +172,12 @@ int* sock = &mysock;
{
#if defined(NOSIGPIPE)
int opt = 1;

if (setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
Log(TRACE_MIN, -1, "Could not set SO_NOSIGPIPE for socket %d", *sock);
#endif
#if defined(SO_NOSIGPIPE)
setsockopt(*sock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt));
#elif !defined(WIN32)
signal(SIGPIPE, SIG_IGN);
#endif // defined(SO_NOSIGPIPE)
#endif // defined(NOSIGPIPE)

if (family == AF_INET)
rc = connect(*sock, (struct sockaddr*)&address, sizeof(address));
Expand Down
8 changes: 4 additions & 4 deletions MQTTPacket/samples/v5ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void stop_init(void)

int main(int argc, char *argv[])
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer;
int rc = 0;
int mysock = 0;
unsigned char buf[200];
Expand Down Expand Up @@ -104,7 +104,7 @@ int main(int argc, char *argv[])

printf("Sent MQTTv5 connect\n");
/* wait for connack */
if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
if (MQTTV5Packet_read(buf, buflen, transport_getdata) == CONNACK)
{
unsigned char sessionPresent, connack_rc;

Expand All @@ -129,10 +129,10 @@ int main(int argc, char *argv[])
while (!toStop)
{
while(!time_to_ping());
len = MQTTSerialize_pingreq(buf, buflen);
len = MQTTV5Serialize_pingreq(buf, buflen);
transport_sendPacketBuffer(mysock, buf, len);
printf("Ping...");
if (MQTTPacket_read(buf, buflen, transport_getdata) == PINGRESP){
if (MQTTV5Packet_read(buf, buflen, transport_getdata) == PINGRESP){
printf("Pong\n");
start_ping_timer();
}
Expand Down
10 changes: 5 additions & 5 deletions MQTTPacket/samples/v5ping_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ enum states { IDLE, GETPONG };

int main(int argc, char *argv[])
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer;
int rc = 0;
int mysock = 0;
unsigned char buf[200];
int buflen = sizeof(buf);
int len = 0;
char *host = "test.mosquitto.org";
int port = 1884;
MQTTTransport mytransport;
MQTTV5Transport mytransport;
int state;
MQTTProperty connack_properties_array[5];
MQTTProperties connack_properties = MQTTProperties_initializer;
Expand Down Expand Up @@ -113,7 +113,7 @@ int main(int argc, char *argv[])
/* wait for connack */
do {
int frc;
if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == CONNACK){
if ((frc=MQTTV5Packet_readnb(buf, buflen, &mytransport)) == CONNACK){
unsigned char sessionPresent, connack_rc;

if (MQTTV5Deserialize_connack(&connack_properties, &sessionPresent, &connack_rc, buf, buflen) != 1
Expand Down Expand Up @@ -141,14 +141,14 @@ int main(int argc, char *argv[])
switch(state){
case IDLE:
if(time_to_ping()){
len = MQTTSerialize_pingreq(buf, buflen);
len = MQTTV5Serialize_pingreq(buf, buflen);
transport_sendPacketBuffer(mysock, buf, len);
printf("Ping...");
state = GETPONG;
}
break;
case GETPONG:
if((rc=MQTTPacket_readnb(buf, buflen, &mytransport)) == PINGRESP){
if((rc=MQTTV5Packet_readnb(buf, buflen, &mytransport)) == PINGRESP){
printf("Pong\n");
start_ping_timer();
state = IDLE;
Expand Down
8 changes: 4 additions & 4 deletions MQTTPacket/samples/v5pub0sub1.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void stop_init(void)

int main(int argc, char *argv[])
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
MQTTV5Packet_connectData data = MQTTV5Packet_connectData_initializer;
int rc = 0;
int mysock = 0;
unsigned char buf[200];
Expand Down Expand Up @@ -91,7 +91,7 @@ int main(int argc, char *argv[])
rc = transport_sendPacketBuffer(mysock, buf, len);

/* wait for connack */
if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
if (MQTTV5Packet_read(buf, buflen, transport_getdata) == CONNACK)
{
unsigned char sessionPresent, connack_rc;

Expand Down Expand Up @@ -123,7 +123,7 @@ int main(int argc, char *argv[])
len = MQTTV5Serialize_subscribe(buf, buflen, 0, msgid, &sub_properties, 1, &topicString, &req_qos, &sub_options);

rc = transport_sendPacketBuffer(mysock, buf, len);
if (MQTTPacket_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */
if (MQTTV5Packet_read(buf, buflen, transport_getdata) == SUBACK) /* wait for suback */
{
unsigned short submsgid;
int subcount;
Expand Down Expand Up @@ -192,7 +192,7 @@ int main(int argc, char *argv[])
{
/* transport_getdata() has a built-in 1 second timeout,
your mileage will vary */
rc = MQTTPacket_read(buf, buflen, transport_getdata);
rc = MQTTV5Packet_read(buf, buflen, transport_getdata);

unsigned char dup;
unsigned short msgid;
Expand Down
12 changes: 6 additions & 6 deletions MQTTPacket/samples/v5pub0sub1_nb.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void stop_init(void)

int main(int argc, char *argv[])
{
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
MQTTV5Packet_connectData data = MQTTPacket_connectData_initializer;
int rc = 0;
int mysock = 0;
unsigned char buf[200];
Expand All @@ -55,7 +55,7 @@ int main(int argc, char *argv[])
int len = 0;
char *host = "test.mosquitto.org";
int port = 1884;
MQTTTransport mytransport;
MQTTV5Transport mytransport;
MQTTProperty recv_properties_array[5];
MQTTProperties recv_properties = MQTTProperties_initializer;
recv_properties.array = recv_properties_array;
Expand Down Expand Up @@ -84,7 +84,7 @@ int main(int argc, char *argv[])
mytransport.state = 0;
data.clientID.cstring = "paho-emb-v5pub0sub1_nb";
data.keepAliveInterval = 20;
data.cleansession = 1;
data.cleanstart = 1;
data.username.cstring = "rw";
data.password.cstring = "readwrite";
data.MQTTVersion = 5;
Expand All @@ -95,7 +95,7 @@ int main(int argc, char *argv[])
rc = transport_sendPacketBuffer(mysock, buf, len);

/* wait for connack */
if (MQTTPacket_read(buf, buflen, transport_getdata) == CONNACK)
if (MQTTV5Packet_read(buf, buflen, transport_getdata) == CONNACK)
{
unsigned char sessionPresent, connack_rc;

Expand Down Expand Up @@ -129,7 +129,7 @@ int main(int argc, char *argv[])
rc = transport_sendPacketBuffer(mysock, buf, len);
do {
int frc;
if ((frc=MQTTPacket_readnb(buf, buflen, &mytransport)) == SUBACK) /* wait for suback */
if ((frc=MQTTV5Packet_readnb(buf, buflen, &mytransport)) == SUBACK) /* wait for suback */
{
unsigned short submsgid;
int subcount;
Expand Down Expand Up @@ -200,7 +200,7 @@ int main(int argc, char *argv[])
while (!toStop)
{
/* handle timeouts */
rc = MQTTPacket_readnb(buf, buflen, &mytransport);
rc = MQTTV5Packet_readnb(buf, buflen, &mytransport);
unsigned char dup;
unsigned short msgid;

Expand Down
2 changes: 1 addition & 1 deletion MQTTPacket/samples/v5qos0pub.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

int main(int argc, char *argv[])
{
MQTTPacket_connectData conn_data = MQTTPacket_connectData_initializer;
MQTTV5Packet_connectData conn_data = MQTTV5Packet_connectData_initializer;
int rc = 0;
char buf[200];
int buflen = sizeof(buf);
Expand Down
17 changes: 9 additions & 8 deletions MQTTPacket/src/MQTTConnectClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +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)
int MQTTV5Serialize_connectLength(MQTTV5Packet_connectData* options, MQTTProperties* connectProperties)
#else
int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
#endif
Expand Down Expand Up @@ -72,7 +72,7 @@ int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
* @return serialized length, or error if 0
*/
#if defined(MQTTV5)
int MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options,
int MQTTV5Serialize_connect(unsigned char* buf, int32_t buflen, MQTTV5Packet_connectData* options,
MQTTProperties* connectProperties)
#else
int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connectData* options)
Expand All @@ -86,7 +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)) > buflen)
if (MQTTPacket_len(len = MQTTV5Serialize_connectLength(options, connectProperties)) > buflen)
#else
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
#endif
Expand All @@ -99,7 +99,7 @@ int MQTTSerialize_connect(unsigned char* buf, int32_t buflen, MQTTPacket_connect
header.bits.type = CONNECT;
writeChar(&ptr, header.byte); /* write header */

ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */

if (options->MQTTVersion == 5 || options->MQTTVersion == 4)
writeCString(&ptr, "MQTT");
Expand Down Expand Up @@ -213,9 +213,6 @@ int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connac
* @return serialized length, or error if 0
*/
#if defined(MQTTV5)
int MQTTV5Serialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype,
unsigned char reasonCode, MQTTProperties* properties);

int MQTTV5Serialize_zero(unsigned char* buf, int32_t buflen, unsigned char packettype,
unsigned char reasonCode, MQTTProperties* properties)
#else
Expand Down Expand Up @@ -245,7 +242,7 @@ int MQTTSerialize_zero(unsigned char* buf, int32_t buflen, unsigned char packett
header.bits.type = packettype;
writeChar(&ptr, header.byte); /* write header */

ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
ptr += MQTTPacket_encode_internal(ptr, len); /* write remaining length */
#if defined(MQTTV5)
if (reasonCode >= 0 && reasonCode <= 162)
{
Expand Down Expand Up @@ -297,7 +294,11 @@ int MQTTV5Serialize_auth(unsigned char* buf, int32_t buflen,
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
* @return serialized length, or error if 0
*/
#if defined(MQTTV5)
int MQTTV5Serialize_pingreq(unsigned char* buf, int32_t buflen)
#else
int MQTTSerialize_pingreq(unsigned char* buf, int32_t buflen)
#endif
{
#if defined(MQTTV5)
return MQTTV5Serialize_zero(buf, buflen, PINGREQ, -1, NULL);
Expand Down
Loading

0 comments on commit a812295

Please sign in to comment.