From 584d85650ed44c5b205a74364591af87012e4455 Mon Sep 17 00:00:00 2001 From: Eric Blankenhorn Date: Thu, 20 Aug 2020 11:40:50 -0500 Subject: [PATCH] Fix for publish with short topic name and example --- examples/sn-client/sn-client.c | 56 ++++++++++++++++++++++++++++++++-- src/mqtt_client.c | 11 ++----- src/mqtt_packet.c | 12 +++++++- 3 files changed, 68 insertions(+), 11 deletions(-) diff --git a/examples/sn-client/sn-client.c b/examples/sn-client/sn-client.c index 4d86b71..cbc7438 100644 --- a/examples/sn-client/sn-client.c +++ b/examples/sn-client/sn-client.c @@ -38,7 +38,7 @@ static int mStopRead = 0; /* Maximum size for network read/write callbacks. */ #define MAX_BUFFER_SIZE 1024 #define TEST_MESSAGE "test" - +#define SHORT_TOPIC_NAME "s1" static int sn_message_cb(MqttClient *client, MqttMessage *msg, byte msg_new, byte msg_done) @@ -247,6 +247,53 @@ int sn_test(MQTTCtx *mqttCtx) } } + { + /* Short Topic Name Subscribe */ + SN_Subscribe subscribe; + SN_Publish publish; + + XMEMSET(&subscribe, 0, sizeof(SN_Subscribe)); + + subscribe.duplicate = 0; + subscribe.qos = MQTT_QOS_0; + subscribe.topic_type = SN_TOPIC_ID_TYPE_SHORT; + subscribe.topicNameId = SHORT_TOPIC_NAME; + subscribe.packet_id = mqtt_get_packetid(); + + PRINTF("MQTT-SN Subscribe: topic name = %s", subscribe.topicNameId); + rc = SN_Client_Subscribe(&mqttCtx->client, &subscribe); + + PRINTF("....MQTT-SN Subscribe Ack: topic id = %d, rc = %d", + subscribe.subAck.topicId, subscribe.subAck.return_code); + + /* Short Topic Name Publish */ + XMEMSET(&publish, 0, sizeof(SN_Publish)); + publish.retain = 0; + publish.qos = mqttCtx->qos; + publish.duplicate = 0; + publish.topic_type = SN_TOPIC_ID_TYPE_SHORT; + publish.topic_name = SHORT_TOPIC_NAME; + if (publish.qos > MQTT_QOS_0) { + publish.packet_id = mqtt_get_packetid(); + } + else { + publish.packet_id = 0x00; + } + + publish.buffer = (byte*)TEST_MESSAGE; + publish.total_len = (word16)XSTRLEN(TEST_MESSAGE); + + rc = SN_Client_Publish(&mqttCtx->client, &publish); + + PRINTF("MQTT-SN Publish: topic id = %d, rc = %d\r\nPayload = %s", + (word16)*publish.topic_name, + publish.return_code, + publish.buffer); + if (rc != MQTT_CODE_SUCCESS) { + goto disconn; + } + } + /* Read Loop */ PRINTF("MQTT Waiting for message..."); @@ -279,7 +326,12 @@ int sn_test(MQTTCtx *mqttCtx) mqttCtx->publishSN.duplicate = 0; mqttCtx->publishSN.topic_type = SN_TOPIC_ID_TYPE_NORMAL; mqttCtx->publishSN.topic_name = (char*)&topicID; - mqttCtx->publishSN.packet_id = mqtt_get_packetid(); + if (mqttCtx->publishSN.qos > MQTT_QOS_0) { + mqttCtx->publishSN.packet_id = mqtt_get_packetid(); + } + else { + mqttCtx->publishSN.packet_id = 0x00; + } mqttCtx->publishSN.buffer = mqttCtx->rx_buf; mqttCtx->publishSN.total_len = (word16)rc; rc = SN_Client_Publish(&mqttCtx->client, diff --git a/src/mqtt_client.c b/src/mqtt_client.c index 532f60d..885f5c5 100644 --- a/src/mqtt_client.c +++ b/src/mqtt_client.c @@ -2417,9 +2417,6 @@ wait_again: goto wait_again; } - #ifdef WOLFMQTT_V5 - case MQTT_MSG_AUTH: - #endif case MQTT_MSG_WRITE: default: { @@ -2735,9 +2732,6 @@ int SN_Client_Publish(MqttClient *client, SN_Publish *publish) break; } - #ifdef WOLFMQTT_V5 - case MQTT_MSG_AUTH: - #endif case MQTT_MSG_READ: case MQTT_MSG_READ_PAYLOAD: #ifdef WOLFMQTT_DEBUG_CLIENT @@ -2822,6 +2816,7 @@ int SN_Client_Register(MqttClient *client, SN_Register *regist) int SN_Client_Ping(MqttClient *client, SN_PingReq *ping) { int rc, len; + SN_PingReq loc_ping; /* Validate required arguments */ if (client == NULL) { @@ -2829,8 +2824,8 @@ int SN_Client_Ping(MqttClient *client, SN_PingReq *ping) } if (ping == NULL) { - /* use client global */ - ping = &client->msgSN.pingReq; + XMEMSET(&loc_ping, 0, sizeof(SN_PingReq)); + ping = &loc_ping; } if (ping->stat == MQTT_MSG_BEGIN) { diff --git a/src/mqtt_packet.c b/src/mqtt_packet.c index 9423d08..e77d214 100755 --- a/src/mqtt_packet.c +++ b/src/mqtt_packet.c @@ -2712,7 +2712,17 @@ int SN_Encode_Publish(byte *tx_buf, int tx_buf_len, SN_Publish *publish) *tx_payload++ = flags; - tx_payload += MqttEncode_Num(tx_payload, (word16)*publish->topic_name); + /* Encode topic */ + if (publish->topic_type == SN_TOPIC_ID_TYPE_SHORT) { + /* Short topic name is 2 chars */ + XMEMCPY(tx_payload, publish->topic_name, 2); + tx_payload += 2; + } + else { + /* Topic ID */ + tx_payload += MqttEncode_Num(tx_payload, (word16)*publish->topic_name); + } + tx_payload += MqttEncode_Num(tx_payload, publish->packet_id); /* Encode payload */