@@ -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+
235252int cycle (MQTTClient * c , Timer * timer )
236253{
237254 int len = 0 ,
@@ -309,8 +326,8 @@ int cycle(MQTTClient* c, Timer* timer)
309326exit :
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+
384403int 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
525545exit :
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
575597exit :
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
635659exit :
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}
0 commit comments