22 * @Author: jiejie
33 * @Github: https://github.com/jiejieTop
44 * @Date: 2019-12-09 21:31:25
5- * @LastEditTime: 2021-01-14 10:00:12
5+ * @LastEditTime : 2022-06-12 17:39:43
66 * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
77 */
88#include "mqttclient.h"
@@ -154,6 +154,7 @@ static int mqtt_read_packet(mqtt_client_t* c, int* packet_type, platform_timer_t
154154 if (NULL == packet_type )
155155 RETURN_ERROR (MQTT_NULL_VALUE_ERROR );
156156
157+ platform_timer_init (timer );
157158 platform_timer_cutdown (timer , c -> mqtt_cmd_timeout );
158159
159160 /* 1. read the header byte. This has the packet type in it */
@@ -192,6 +193,7 @@ static int mqtt_send_packet(mqtt_client_t* c, int length, platform_timer_t* time
192193 int len = 0 ;
193194 int sent = 0 ;
194195
196+ platform_timer_init (timer );
195197 platform_timer_cutdown (timer , c -> mqtt_cmd_timeout );
196198
197199 /* send mqtt packet in a blocking manner or exit when it timer is expired */
@@ -302,7 +304,7 @@ static int mqtt_deliver_message(mqtt_client_t* c, MQTTString* topic_name, mqtt_m
302304 rc = MQTT_SUCCESS_ERROR ;
303305 }
304306
305- memset (message -> payload , 0 , strlen (( const char * ) message -> payload ));
307+ memset (message -> payload , 0 , strlen (message -> payload ));
306308 memset (topic_name -> lenstring .data , 0 , topic_name -> lenstring .len );
307309
308310 RETURN_ERROR (rc );
@@ -317,7 +319,7 @@ static ack_handlers_t *mqtt_ack_handler_create(mqtt_client_t* c, int type, uint1
317319 return NULL ;
318320
319321 mqtt_list_init (& ack_handler -> list );
320-
322+ platform_timer_init ( & ack_handler -> timer );
321323 platform_timer_cutdown (& ack_handler -> timer , c -> mqtt_cmd_timeout ); /* No response within timeout will be destroyed or resent */
322324
323325 ack_handler -> type = type ;
@@ -341,7 +343,7 @@ static void mqtt_ack_handler_destroy(ack_handlers_t* ack_handler)
341343static void mqtt_ack_handler_resend (mqtt_client_t * c , ack_handlers_t * ack_handler )
342344{
343345 platform_timer_t timer ;
344-
346+ platform_timer_init ( & timer );
345347 platform_timer_cutdown (& timer , c -> mqtt_cmd_timeout );
346348 platform_timer_cutdown (& ack_handler -> timer , c -> mqtt_cmd_timeout ); /* timeout, recutdown */
347349
@@ -351,7 +353,6 @@ static void mqtt_ack_handler_resend(mqtt_client_t* c, ack_handlers_t* ack_handle
351353 mqtt_send_packet (c , ack_handler -> payload_len , & timer ); /* resend data */
352354 platform_mutex_unlock (& c -> mqtt_write_lock );
353355 MQTT_LOG_W ("%s:%d %s()... resend %d package, packet_id is %d " , __FILE__ , __LINE__ , __FUNCTION__ , ack_handler -> type , ack_handler -> packet_id );
354-
355356}
356357
357358static int mqtt_ack_list_node_is_exist (mqtt_client_t * c , int type , uint16_t packet_id )
@@ -515,6 +516,7 @@ static void mqtt_clean_session(mqtt_client_t* c)
515516 msg_handler -> topic_filter = NULL ;
516517 platform_memory_free (msg_handler );
517518 }
519+ // MQTT_LOG_D("%s:%d %s() mqtt_msg_handler_list delete", __FILE__, __LINE__, __FUNCTION__);
518520 mqtt_list_del_init (& c -> mqtt_msg_handler_list );
519521 }
520522
@@ -568,8 +570,10 @@ static int mqtt_try_resubscribe(mqtt_client_t* c)
568570
569571 MQTT_LOG_W ("%s:%d %s()... mqtt try resubscribe ..." , __FILE__ , __LINE__ , __FUNCTION__ );
570572
571- if (mqtt_list_is_empty (& c -> mqtt_msg_handler_list ))
573+ if (mqtt_list_is_empty (& c -> mqtt_msg_handler_list )) {
574+ // MQTT_LOG_D("%s:%d %s() mqtt_msg_handler_list is empty", __FILE__, __LINE__, __FUNCTION__);
572575 RETURN_ERROR (MQTT_SUCCESS_ERROR );
576+ }
573577
574578 LIST_FOR_EACH_SAFE (curr , next , & c -> mqtt_msg_handler_list ) {
575579 msg_handler = LIST_ENTRY (curr , message_handlers_t , list );
@@ -626,7 +630,9 @@ static int mqtt_publish_ack_packet(mqtt_client_t *c, uint16_t packet_id, int pac
626630 int len = 0 ;
627631 int rc = MQTT_SUCCESS_ERROR ;
628632 platform_timer_t timer ;
629-
633+ platform_timer_init (& timer );
634+ platform_timer_cutdown (& timer , c -> mqtt_cmd_timeout );
635+
630636 platform_mutex_lock (& c -> mqtt_write_lock );
631637
632638 switch (packet_type ) {
@@ -851,7 +857,7 @@ static int mqtt_packet_handle(mqtt_client_t* c, platform_timer_t* timer)
851857 break ;
852858
853859 default :
854- break ;
860+ goto exit ;
855861 }
856862
857863 rc = mqtt_keep_alive (c );
@@ -888,7 +894,7 @@ static int mqtt_yield(mqtt_client_t* c, int timeout_ms)
888894 if (0 == timeout_ms )
889895 timeout_ms = c -> mqtt_cmd_timeout ;
890896
891-
897+ platform_timer_init ( & timer );
892898 platform_timer_cutdown (& timer , timeout_ms );
893899
894900 while (!platform_timer_is_expired (& timer )) {
@@ -966,8 +972,6 @@ static int mqtt_connect_with_results(mqtt_client_t* c)
966972 if (CLIENT_STATE_CONNECTED == mqtt_get_client_state (c ))
967973 RETURN_ERROR (MQTT_SUCCESS_ERROR );
968974
969-
970-
971975#ifndef MQTT_NETWORK_TYPE_NO_TLS
972976 rc = network_init (c -> mqtt_network , c -> mqtt_host , c -> mqtt_port , c -> mqtt_ca );
973977#else
@@ -1036,7 +1040,7 @@ static int mqtt_connect_with_results(mqtt_client_t* c)
10361040 /*creat the thread fail and disconnect the mqtt socket connect*/
10371041 network_release (c -> mqtt_network );
10381042 rc = MQTT_CONNECT_FAILED_ERROR ;
1039- MQTT_LOG_W ("%s:%d %s()... mqtt yield thread creat faile ..." , __FILE__ , __LINE__ , __FUNCTION__ );
1043+ MQTT_LOG_W ("%s:%d %s()... mqtt yield thread creat failed ..." , __FILE__ , __LINE__ , __FUNCTION__ );
10401044 }
10411045 } else {
10421046 mqtt_set_client_state (c , CLIENT_STATE_CONNECTED ); /* reconnect, mqtt thread is already exists */
@@ -1196,8 +1200,9 @@ int mqtt_keep_alive(mqtt_client_t* c)
11961200 } else {
11971201 platform_timer_t timer ;
11981202 int len = MQTTSerialize_pingreq (c -> mqtt_write_buf , c -> mqtt_write_buf_size );
1199- if (len > 0 && (rc = mqtt_send_packet (c , len , & timer )) == MQTT_SUCCESS_ERROR ) // send the ping packet
1200- c -> mqtt_ping_outstanding ++ ;
1203+ if (len > 0 )
1204+ rc = mqtt_send_packet (c , len , & timer ); // 100ask, send the ping packet
1205+ c -> mqtt_ping_outstanding ++ ;
12011206 }
12021207 }
12031208
@@ -1229,7 +1234,7 @@ int mqtt_release(mqtt_client_t* c)
12291234 if (NULL == c )
12301235 RETURN_ERROR (MQTT_NULL_VALUE_ERROR );
12311236
1232-
1237+ platform_timer_init ( & timer );
12331238 platform_timer_cutdown (& timer , c -> mqtt_cmd_timeout );
12341239
12351240 /* wait for the clean session to complete */
@@ -1276,7 +1281,7 @@ int mqtt_disconnect(mqtt_client_t* c)
12761281 platform_timer_t timer ;
12771282 int len = 0 ;
12781283
1279-
1284+ platform_timer_init ( & timer );
12801285 platform_timer_cutdown (& timer , c -> mqtt_cmd_timeout );
12811286
12821287 platform_mutex_lock (& c -> mqtt_write_lock );
@@ -1306,10 +1311,10 @@ int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, m
13061311 if (CLIENT_STATE_CONNECTED != mqtt_get_client_state (c ))
13071312 RETURN_ERROR (MQTT_NOT_CONNECT_ERROR );
13081313
1309- packet_id = mqtt_get_next_packet_id (c );
1310-
13111314 platform_mutex_lock (& c -> mqtt_write_lock );
13121315
1316+ packet_id = mqtt_get_next_packet_id (c );
1317+
13131318 /* serialize subscribe packet and send it */
13141319 len = MQTTSerialize_subscribe (c -> mqtt_write_buf , c -> mqtt_write_buf_size , 0 , packet_id , 1 , & topic , (int * )& qos );
13151320 if (len <= 0 )
@@ -1350,10 +1355,10 @@ int mqtt_unsubscribe(mqtt_client_t* c, const char* topic_filter)
13501355 if (CLIENT_STATE_CONNECTED != mqtt_get_client_state (c ))
13511356 RETURN_ERROR (MQTT_NOT_CONNECT_ERROR );
13521357
1358+ platform_mutex_lock (& c -> mqtt_write_lock );
13531359
13541360 packet_id = mqtt_get_next_packet_id (c );
13551361
1356- platform_mutex_lock (& c -> mqtt_write_lock );
13571362 /* serialize unsubscribe packet and send it */
13581363 if ((len = MQTTSerialize_unsubscribe (c -> mqtt_write_buf , c -> mqtt_write_buf_size , 0 , packet_id , 1 , & topic )) <= 0 )
13591364 goto exit ;
@@ -1385,8 +1390,10 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg
13851390 topic .cstring = (char * )topic_filter ;
13861391
13871392 if (CLIENT_STATE_CONNECTED != mqtt_get_client_state (c )) {
1393+ msg -> payloadlen = 0 ; // clear
13881394 rc = MQTT_NOT_CONNECT_ERROR ;
1389- goto exit ;
1395+ RETURN_ERROR (rc );
1396+ // goto exit; /* 100ask */
13901397 }
13911398
13921399 if ((NULL != msg -> payload ) && (0 == msg -> payloadlen ))
@@ -1417,7 +1424,7 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg
14171424 goto exit ;
14181425
14191426 if (QOS0 != msg -> qos ) {
1420- mqtt_set_publish_dup (c ,1 ); /* may resend this data, set the dup flag in advance */
1427+ mqtt_set_publish_dup (c , 1 ); /* may resend this data, set the dup flag in advance */
14211428
14221429 if (QOS1 == msg -> qos ) {
14231430 /* expect to receive PUBACK, otherwise data will be resent */
@@ -1431,7 +1438,7 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg
14311438
14321439exit :
14331440 msg -> payloadlen = 0 ; // clear
1434-
1441+
14351442 platform_mutex_unlock (& c -> mqtt_write_lock );
14361443
14371444 if ((MQTT_ACK_HANDLER_NUM_TOO_MUCH_ERROR == rc ) || (MQTT_MEM_NOT_ENOUGH_ERROR == rc )) {
0 commit comments