Skip to content

Commit efadb74

Browse files
author
Ian Craggs
committed
Add session state tests for MQTTClient-C
1 parent 7d032f5 commit efadb74

File tree

4 files changed

+265
-215
lines changed

4 files changed

+265
-215
lines changed

MQTTClient-C/src/MQTTClient.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ void MQTTClientInit(MQTTClient* c, Network* network, unsigned int command_timeou
6565
c->readbuf = readbuf;
6666
c->readbuf_size = readbuf_size;
6767
c->isconnected = 0;
68+
c->cleansession = 0;
6869
c->ping_outstanding = 0;
6970
c->defaultMessageHandler = NULL;
7071
c->next_packetid = 1;
@@ -232,6 +233,22 @@ int keepalive(MQTTClient* c)
232233
}
233234

234235

236+
void MQTTCleanSession(MQTTClient* c)
237+
{
238+
for (int i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
239+
c->messageHandlers[i].topicFilter = NULL;
240+
}
241+
242+
243+
void MQTTCloseSession(MQTTClient* c)
244+
{
245+
c->ping_outstanding = 0;
246+
c->isconnected = 0;
247+
if (c->cleansession)
248+
MQTTCleanSession(c);
249+
}
250+
251+
235252
int cycle(MQTTClient* c, Timer* timer)
236253
{
237254
int len = 0,
@@ -309,8 +326,8 @@ int cycle(MQTTClient* c, Timer* timer)
309326
exit:
310327
if (rc == SUCCESS)
311328
rc = packet_type;
312-
else
313-
c->isconnected = 0;
329+
else if (c->isconnected)
330+
MQTTCloseSession(c);
314331
return rc;
315332
}
316333

@@ -381,6 +398,8 @@ int waitfor(MQTTClient* c, int packet_type, Timer* timer)
381398
}
382399

383400

401+
402+
384403
int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTConnackData* data)
385404
{
386405
Timer connect_timer;
@@ -401,6 +420,7 @@ int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTC
401420
options = &default_options; /* set default options if none were supplied */
402421

403422
c->keepAliveInterval = options->keepAliveInterval;
423+
c->cleansession = options->cleansession;
404424
TimerCountdown(&c->last_received, c->keepAliveInterval);
405425
if ((len = MQTTSerialize_connect(c->buf, c->buf_size, options)) <= 0)
406426
goto exit;
@@ -512,8 +532,8 @@ int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum QoS qo
512532
{
513533
int count = 0;
514534
unsigned short mypacketid;
515-
data->grantedQoS = 0;
516-
if (MQTTDeserialize_suback(&mypacketid, 1, &count, &data->grantedQoS, c->readbuf, c->readbuf_size) == 1)
535+
data->grantedQoS = QOS0;
536+
if (MQTTDeserialize_suback(&mypacketid, 1, &count, (int*)&data->grantedQoS, c->readbuf, c->readbuf_size) == 1)
517537
{
518538
if (data->grantedQoS != 0x80)
519539
rc = MQTTSetMessageHandler(c, topicFilter, messageHandler);
@@ -523,6 +543,8 @@ int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum QoS qo
523543
rc = FAILURE;
524544

525545
exit:
546+
if (rc == FAILURE)
547+
MQTTCloseSession(c);
526548
#if defined(MQTT_TASK)
527549
MutexUnlock(&c->mutex);
528550
#endif
@@ -573,6 +595,8 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
573595
rc = FAILURE;
574596

575597
exit:
598+
if (rc == FAILURE)
599+
MQTTCloseSession(c);
576600
#if defined(MQTT_TASK)
577601
MutexUnlock(&c->mutex);
578602
#endif
@@ -633,6 +657,8 @@ int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
633657
}
634658

635659
exit:
660+
if (rc == FAILURE)
661+
MQTTCloseSession(c);
636662
#if defined(MQTT_TASK)
637663
MutexUnlock(&c->mutex);
638664
#endif
@@ -652,14 +678,13 @@ int MQTTDisconnect(MQTTClient* c)
652678
TimerInit(&timer);
653679
TimerCountdownMS(&timer, c->command_timeout_ms);
654680

655-
len = MQTTSerialize_disconnect(c->buf, c->buf_size);
681+
len = MQTTSerialize_disconnect(c->buf, c->buf_size);
656682
if (len > 0)
657683
rc = sendPacket(c, len, &timer); // send the disconnect packet
658-
659-
c->isconnected = 0;
684+
MQTTCloseSession(c);
660685

661686
#if defined(MQTT_TASK)
662-
MutexUnlock(&c->mutex);
687+
MutexUnlock(&c->mutex);
663688
#endif
664689
return rc;
665690
}

MQTTClient-C/src/MQTTClient.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */
5353
#endif
5454

55-
enum QoS { QOS0, QOS1, QOS2 };
55+
enum QoS { QOS0, QOS1, QOS2, SUBFAIL=0x80 };
5656

5757
/* all failure return codes must be negative */
5858
enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
@@ -98,7 +98,7 @@ typedef struct MQTTConnackData
9898

9999
typedef struct MQTTSubackData
100100
{
101-
int grantedQoS;
101+
enum QoS grantedQoS;
102102
} MQTTSubackData;
103103

104104
typedef void (*messageHandler)(MessageData*);
@@ -114,6 +114,7 @@ typedef struct MQTTClient
114114
unsigned int keepAliveInterval;
115115
char ping_outstanding;
116116
int isconnected;
117+
int cleansession;
117118

118119
struct MessageHandlers
119120
{
@@ -212,6 +213,15 @@ DLLExport int MQTTDisconnect(MQTTClient* client);
212213
*/
213214
DLLExport int MQTTYield(MQTTClient* client, int time);
214215

216+
/** MQTT isConnected
217+
* @param client - the client object to use
218+
* @return truth value indicating whether the client is connected to the server
219+
*/
220+
DLLExport int MQTTIsConnected(MQTTClient* client)
221+
{
222+
return client->isconnected;
223+
}
224+
215225
#if defined(MQTT_TASK)
216226
/** MQTT start background thread for a client. After this, MQTTYield should not be called.
217227
* @param client - the client object to use

0 commit comments

Comments
 (0)