Increment publish packet id, as to consecutive packet must not have the same packet...
authorNathael Pajani <nathael.pajani@ed3l.fr>
Sun, 10 Mar 2019 12:46:43 +0000 (13:46 +0100)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Tue, 8 Nov 2022 16:14:23 +0000 (17:14 +0100)
mqtt_pub/main.c

index e6b73a4..d96ba07 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- *   apps/base/mqtt/main.c
+ *   apps/base/mqtt_pub/main.c
  *
  * MQTT client example using data from onboard TMP101 I2C temperature sensor
  *
@@ -273,6 +273,7 @@ int mqtt_handle_packet(char* buf, int comm_uart, int dbg_uart)
                        } else {
                                uprintf(dbg_uart, "Connection refused: %d\n", response->ret_code);
                                mqtt_comm_state = MQTT_UNCONNECTED;
+                               return -4;
                        }
                        break;
                }
@@ -281,9 +282,15 @@ int mqtt_handle_packet(char* buf, int comm_uart, int dbg_uart)
                        ret = mqtt_check_publish_response_pkt(response, MQTT_WAITING_PUBACK);
                        if (ret != 0) {
                                uprintf(dbg_uart, "Not a connack packet (ret: %d) ... protocol flow error\n", ret);
-                               return -3;
+                               return -5;
+                       }
+                       if (response->acked_pkt_id != pub_packet_id) {
+                               uprintf(dbg_uart, "Not a connack for the right packet (got %d, our is %d) ...\n",
+                                                       response->acked_pkt_id, pub_packet_id);
+                               return -6;
                        }
                        mqtt_comm_state = MQTT_IDLE;
+                       pub_packet_id++;
                        break;
                }
                case MQTT_WAITING_PUBREC:
@@ -384,6 +391,9 @@ int main(void)
                /* Check for received packet on serial line and handle them */
                if (packet_ok != NULL) {
                        int ret = mqtt_handle_packet((char*)packet_ok, UART0, UART0);
+                       if (ret != 0) {
+                               uprintf(UART0, "Packet handling returned error %d\n", ret);
+                       }
                        packet_ok = NULL;
                }
                chenillard(250);