summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlavio Santes <flavio.santes@intel.com>2017-02-06 12:07:09 -0600
committerJukka Rissanen <jukka.rissanen@linux.intel.com>2017-02-15 12:20:08 +0200
commit057f31e7e9fdb6b194bfaad29a636463b42c3acd (patch)
treece3a384fd083aa12796729711a90568c2f67a967
parent84403077d26db9c23e75d3e575025334ad9b9de4 (diff)
net/mqtt: Remove length computations for some msg fields
Currently, for the following MQTT msg fields: - client_id - will_topic - user_name - topic their length is computed inside the routine that receives the MQTT msg. Although this simplifies development, also imposes one restriction: data must be null-terminated. Sometimes, data is received from other sources and not generated by the application, so the null-terminated constraint may be considered problematic for the user. This patch removes the assumption that string fields are null-terminated. Current data structures are already prepared to handle this case, so no API change is required. Change-Id: I5a147a5b21e0da49541cbe62baac363c8737cd3e Signed-off-by: Flavio Santes <flavio.santes@intel.com>
-rw-r--r--include/net/mqtt_types.h4
-rw-r--r--samples/net/mqtt_publisher/src/main.c5
-rw-r--r--subsys/net/lib/mqtt/mqtt_pkt.c4
-rw-r--r--tests/net/lib/mqtt_packet/src/mqtt_packet.c67
4 files changed, 56 insertions, 24 deletions
diff --git a/include/net/mqtt_types.h b/include/net/mqtt_types.h
index 9bf275ef4..adc988563 100644
--- a/include/net/mqtt_types.h
+++ b/include/net/mqtt_types.h
@@ -56,7 +56,7 @@ enum mqtt_qos {
struct mqtt_connect_msg {
uint8_t clean_session:1;
char *client_id;
- uint16_t client_id_len; /* only required for unpacking */
+ uint16_t client_id_len;
uint8_t will_flag:1;
enum mqtt_qos will_qos;
uint8_t will_retain:1;
@@ -66,7 +66,7 @@ struct mqtt_connect_msg {
uint16_t will_msg_len;
uint16_t keep_alive;
const char *user_name;
- uint16_t user_name_len; /*only required for unpacking */
+ uint16_t user_name_len;
uint8_t *password;
uint16_t password_len;
};
diff --git a/samples/net/mqtt_publisher/src/main.c b/samples/net/mqtt_publisher/src/main.c
index ce7289bb2..3b485b5bf 100644
--- a/samples/net/mqtt_publisher/src/main.c
+++ b/samples/net/mqtt_publisher/src/main.c
@@ -22,6 +22,8 @@
#include "config.h"
+#define CLIENTID "zephyr_publisher"
+
/**
* @brief mqtt_client_ctx Container of some structures used by the
* publisher app.
@@ -272,7 +274,8 @@ void publisher(void)
* will be set to 0 also. Please don't do that, set always to 1.
* Clean session = 0 is not yet supported.
*/
- client_ctx.connect_msg.client_id = "zephyr_publisher";
+ client_ctx.connect_msg.client_id = CLIENTID;
+ client_ctx.connect_msg.client_id_len = strlen(CLIENTID);
client_ctx.connect_msg.clean_session = 1;
client_ctx.connect_data = "CONNECTED";
diff --git a/subsys/net/lib/mqtt/mqtt_pkt.c b/subsys/net/lib/mqtt/mqtt_pkt.c
index 589ee4566..08cf82f5a 100644
--- a/subsys/net/lib/mqtt/mqtt_pkt.c
+++ b/subsys/net/lib/mqtt/mqtt_pkt.c
@@ -299,14 +299,12 @@ int mqtt_pack_connect(uint8_t *buf, uint16_t *length, uint16_t size,
/* client_id */
pkt_size = INT_SIZE;
- msg->client_id_len = mqtt_strlen(msg->client_id);
pkt_size += msg->client_id_len;
/* will flag - optional */
if (msg->will_flag) {
/* will topic */
pkt_size += INT_SIZE;
- msg->will_topic_len = mqtt_strlen(msg->will_topic);
pkt_size += msg->will_topic_len;
/* will message - binary */
pkt_size += INT_SIZE;
@@ -316,7 +314,6 @@ int mqtt_pack_connect(uint8_t *buf, uint16_t *length, uint16_t size,
/* user_name - UTF-8 - optional */
if (msg->user_name) {
pkt_size += INT_SIZE;
- msg->user_name_len = mqtt_strlen(msg->user_name);
pkt_size += msg->user_name_len;
}
@@ -850,7 +847,6 @@ int mqtt_pack_publish(uint8_t *buf, uint16_t *length, uint16_t size,
return -EINVAL;
}
- msg->topic_len = mqtt_strlen(msg->topic);
/* Packet Identifier is only included if QoS > QoS0. See MQTT 3.3.2.2
* So, payload size is:
* topic length size + topic length + packet id + msg's size
diff --git a/tests/net/lib/mqtt_packet/src/mqtt_packet.c b/tests/net/lib/mqtt_packet/src/mqtt_packet.c
index fc8e982d5..fd58ae92a 100644
--- a/tests/net/lib/mqtt_packet/src/mqtt_packet.c
+++ b/tests/net/lib/mqtt_packet/src/mqtt_packet.c
@@ -10,6 +10,15 @@
#define RC_STR(rc) (rc == TC_PASS ? PASS : FAIL)
+#define CLIENTID "zephyr"
+#define CLIENTID_LEN 6
+#define TOPIC "sensors"
+#define TOPIC_LEN 7
+#define WILL_TOPIC "quitting"
+#define WILL_TOPIC_LEN 8
+#define USERNAME "zephyr1"
+#define USERNAME_LEN 7
+
/* MQTT messages in this test are under 256 bytes */
#define BUF_SIZE 256
static uint8_t buf[BUF_SIZE];
@@ -220,7 +229,9 @@ uint8_t connect1[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x70, 0x68, 0x79, 0x72};
static struct mqtt_connect_msg msg_connect1 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 0,
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 0,
.will_qos = 0, .will_retain = 0, .will_topic = NULL,
.will_msg = NULL, .will_msg_len = 0,
.keep_alive = 0, .user_name = NULL,
@@ -240,7 +251,9 @@ uint8_t connect2[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x70, 0x68, 0x79, 0x72};
static struct mqtt_connect_msg msg_connect2 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 0,
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 0,
.will_qos = 0, .will_retain = 0, .will_topic = NULL,
.will_msg = NULL, .will_msg_len = 0,
.keep_alive = 365, .user_name = NULL,
@@ -265,8 +278,11 @@ uint8_t connect3[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x62, 0x79, 0x65};
static struct mqtt_connect_msg msg_connect3 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 1,
- .will_qos = 0, .will_retain = 0, .will_topic = "quitting",
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 1,
+ .will_qos = 0, .will_retain = 0, .will_topic = WILL_TOPIC,
+ .will_topic_len = WILL_TOPIC_LEN,
.will_msg = "bye", .will_msg_len = 3,
.keep_alive = 0, .user_name = NULL,
.password = NULL, .password_len = 0
@@ -288,8 +304,11 @@ uint8_t connect4[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x62, 0x79, 0x65};
static struct mqtt_connect_msg msg_connect4 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 1,
- .will_qos = 0, .will_retain = 1, .will_topic = "quitting",
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 1,
+ .will_qos = 0, .will_retain = 1, .will_topic = WILL_TOPIC,
+ .will_topic_len = WILL_TOPIC_LEN,
.will_msg = "bye", .will_msg_len = 3,
.keep_alive = 0, .user_name = NULL,
.password = NULL, .password_len = 0
@@ -311,8 +330,11 @@ uint8_t connect5[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x62, 0x79, 0x65};
static struct mqtt_connect_msg msg_connect5 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 1,
- .will_qos = 1, .will_retain = 0, .will_topic = "quitting",
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 1,
+ .will_qos = 1, .will_retain = 0, .will_topic = WILL_TOPIC,
+ .will_topic_len = WILL_TOPIC_LEN,
.will_msg = "bye", .will_msg_len = 3,
.keep_alive = 0, .user_name = NULL,
.password = NULL, .password_len = 0
@@ -334,8 +356,11 @@ uint8_t connect6[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x62, 0x79, 0x65};
static struct mqtt_connect_msg msg_connect6 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 1,
- .will_qos = 1, .will_retain = 1, .will_topic = "quitting",
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 1,
+ .will_qos = 1, .will_retain = 1, .will_topic = WILL_TOPIC,
+ .will_topic_len = WILL_TOPIC_LEN,
.will_msg = "bye", .will_msg_len = 3,
.keep_alive = 0, .user_name = NULL,
.password = NULL, .password_len = 0
@@ -360,10 +385,14 @@ uint8_t connect7[] = {0x10, 0x34, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
0x73, 0x73, 0x77, 0x6f, 0x72, 0x64};
static struct mqtt_connect_msg msg_connect7 = {
- .clean_session = 1, .client_id = "zephyr", .will_flag = 1,
- .will_qos = 1, .will_retain = 1, .will_topic = "quitting",
+ .clean_session = 1, .client_id = CLIENTID,
+ .client_id_len = CLIENTID_LEN,
+ .will_flag = 1,
+ .will_qos = 1, .will_retain = 1, .will_topic = WILL_TOPIC,
+ .will_topic_len = WILL_TOPIC_LEN,
.will_msg = "bye", .will_msg_len = 3,
- .keep_alive = 0, .user_name = "zephyr1",
+ .keep_alive = 0, .user_name = USERNAME,
+ .user_name_len = USERNAME_LEN,
.password = "password", .password_len = 8
};
@@ -382,7 +411,8 @@ uint8_t publish1[] = {0x30, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
0x6f, 0x72, 0x73, 0x4f, 0x4b};
static struct mqtt_publish_msg msg_publish1 = {
- .dup = 0, .qos = 0, .retain = 0, .topic = "sensors",
+ .dup = 0, .qos = 0, .retain = 0, .topic = TOPIC,
+ .topic_len = TOPIC_LEN,
.pkt_id = 0, .msg = "OK", .msg_len = 2,
};
@@ -398,7 +428,8 @@ uint8_t publish2[] = {0x31, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
0x6f, 0x72, 0x73, 0x4f, 0x4b};
static struct mqtt_publish_msg msg_publish2 = {
- .dup = 0, .qos = 0, .retain = 1, .topic = "sensors",
+ .dup = 0, .qos = 0, .retain = 1, .topic = TOPIC,
+ .topic_len = TOPIC_LEN,
.pkt_id = 0, .msg = "OK", .msg_len = 2
};
@@ -414,7 +445,8 @@ uint8_t publish3[] = {0x33, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};
static struct mqtt_publish_msg msg_publish3 = {
- .dup = 0, .qos = 1, .retain = 1, .topic = "sensors",
+ .dup = 0, .qos = 1, .retain = 1, .topic = TOPIC,
+ .topic_len = TOPIC_LEN,
.pkt_id = 1, .msg = "OK", .msg_len = 2
};
@@ -429,7 +461,8 @@ static
uint8_t publish4[] = {0x34, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};
static struct mqtt_publish_msg msg_publish4 = {
- .dup = 0, .qos = 2, .retain = 0, .topic = "sensors",
+ .dup = 0, .qos = 2, .retain = 0, .topic = TOPIC,
+ .topic_len = TOPIC_LEN,
.pkt_id = 1, .msg = "OK", .msg_len = 2
};