From 440e6432b37319b9724fd3237a0b097b1ed56fdf Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Sat, 16 Mar 2019 17:34:33 +0100 Subject: [PATCH] Update app to modifications in mqtt protocol support Fix header checksum computation Use MQTT_CONTROL_PUBACK instead of MQTT_WAITING_PUBACK convert endianness of acked_pkt_id before using --- mqtt_pub/main.c | 4 ++-- mqtt_pub/mqtt_comm.c | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/mqtt_pub/main.c b/mqtt_pub/main.c index 2d2c8ba..3fd39c1 100644 --- a/mqtt_pub/main.c +++ b/mqtt_pub/main.c @@ -43,8 +43,6 @@ #define MODULE_VERSION 0x04 #define MODULE_NAME "GPIO Demo Module" -#define APP_BOARD_ADDRESS 0x0102 - #define SELECTED_FREQ FREQ_SEL_48MHz /***************************************************************************** */ @@ -186,6 +184,8 @@ int main(void) /* Configure onboard temp sensor */ temp_config(UART0); + + /* Connect to brocker and start MQTT protocol handling */ mqtt_init(UART0, UART0); while (1) { diff --git a/mqtt_pub/mqtt_comm.c b/mqtt_pub/mqtt_comm.c index cb6c4af..384ad0f 100644 --- a/mqtt_pub/mqtt_comm.c +++ b/mqtt_pub/mqtt_comm.c @@ -43,7 +43,7 @@ uint16_t app_board_addr = APP_BOARD_ADDRESS; /* This is our static connect data */ const struct mqtt_connect_pkt mqtt_conn = { .keep_alive_seconds = 600, - .clean_session_flag = MQTT_SESSION_RESUME, + .clean_session_flag = MQTT_SESSION_NEW, /* or MQTT_SESSION_RESUME */ .client_id = "test", .will_topic = { .name = "will/temp/test", .QoS = MQTT_QoS_0 }, .will_retain = 1, @@ -73,6 +73,7 @@ void add_packet_header(int pkt_len) header->seqnum = 0; header->data_len = pkt_len; header->data_cksum = cksum; + header->header_cksum = 0; /* Compute header chksum */ cksum = 0; for (i = 0; i < HEADER_SIZE; i++) { @@ -160,35 +161,36 @@ int mqtt_handle_packet(char* buf, int comm_uart, int dbg_uart) switch (mqtt_comm_state) { case MQTT_WAITING_CONNECT_ACK: { /* We must not receive anything before the connect acknowledge */ - struct mqtt_connack_response_pkt* response = (struct mqtt_connack_response_pkt*)(buf + HEADER_SIZE); - ret = mqtt_check_connack_response_pkt(response); + struct mqtt_connack_reply_pkt* reply = (struct mqtt_connack_reply_pkt*)(buf + HEADER_SIZE); + ret = mqtt_check_connack_reply_pkt(reply); if (ret != 0) { uprintf(dbg_uart, "Not a connack packet (ret: %d) ... protocol flow error\n", ret); return -3; } /* Valid packet, check return code */ - if (response->ret_code == MQTT_CONNACK_ACCEPTED) { + if (reply->ret_code == MQTT_CONNACK_ACCEPTED) { uprintf(dbg_uart, "Connection accepted\n"); mqtt_comm_state = MQTT_IDLE; } else { - uprintf(dbg_uart, "Connection refused: %d\n", response->ret_code); + uprintf(dbg_uart, "Connection refused: %d\n", reply->ret_code); mqtt_comm_state = MQTT_UNCONNECTED; return -4; } break; } case MQTT_WAITING_PUBACK: { - struct mqtt_publish_response_pkt* response = (struct mqtt_publish_response_pkt*)(buf + HEADER_SIZE); - ret = mqtt_check_publish_response_pkt(response, MQTT_WAITING_PUBACK); + struct mqtt_publish_reply_pkt* reply = (struct mqtt_publish_reply_pkt*)(buf + HEADER_SIZE); + ret = mqtt_check_publish_reply_pkt(reply, MQTT_CONTROL_PUBACK); if (ret != 0) { - uprintf(dbg_uart, "Not a connack packet (ret: %d) ... protocol flow error\n", ret); + uprintf(dbg_uart, "Not a puback packet (ret: %d) ... protocol flow error\n", ret); 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); + if (ntohs(reply->acked_pkt_id) != pub_packet_id) { + uprintf(dbg_uart, "Not a puback for the right packet (got %d, our is %d) ...\n", + ntohs(reply->acked_pkt_id), pub_packet_id); return -6; } + uprintf(dbg_uart, "Publication accepted\n"); mqtt_comm_state = MQTT_IDLE; pub_packet_id++; break; -- 2.43.0