Skip to content

Commit 057f31e

Browse files
1010101001010101jukkar
authored andcommitted
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 <[email protected]>
1 parent 8440307 commit 057f31e

File tree

4 files changed

+56
-24
lines changed

4 files changed

+56
-24
lines changed

include/net/mqtt_types.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ enum mqtt_qos {
5656
struct mqtt_connect_msg {
5757
uint8_t clean_session:1;
5858
char *client_id;
59-
uint16_t client_id_len; /* only required for unpacking */
59+
uint16_t client_id_len;
6060
uint8_t will_flag:1;
6161
enum mqtt_qos will_qos;
6262
uint8_t will_retain:1;
@@ -66,7 +66,7 @@ struct mqtt_connect_msg {
6666
uint16_t will_msg_len;
6767
uint16_t keep_alive;
6868
const char *user_name;
69-
uint16_t user_name_len; /*only required for unpacking */
69+
uint16_t user_name_len;
7070
uint8_t *password;
7171
uint16_t password_len;
7272
};

samples/net/mqtt_publisher/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "config.h"
2424

25+
#define CLIENTID "zephyr_publisher"
26+
2527
/**
2628
* @brief mqtt_client_ctx Container of some structures used by the
2729
* publisher app.
@@ -272,7 +274,8 @@ void publisher(void)
272274
* will be set to 0 also. Please don't do that, set always to 1.
273275
* Clean session = 0 is not yet supported.
274276
*/
275-
client_ctx.connect_msg.client_id = "zephyr_publisher";
277+
client_ctx.connect_msg.client_id = CLIENTID;
278+
client_ctx.connect_msg.client_id_len = strlen(CLIENTID);
276279
client_ctx.connect_msg.clean_session = 1;
277280

278281
client_ctx.connect_data = "CONNECTED";

subsys/net/lib/mqtt/mqtt_pkt.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,12 @@ int mqtt_pack_connect(uint8_t *buf, uint16_t *length, uint16_t size,
299299

300300
/* client_id */
301301
pkt_size = INT_SIZE;
302-
msg->client_id_len = mqtt_strlen(msg->client_id);
303302
pkt_size += msg->client_id_len;
304303

305304
/* will flag - optional */
306305
if (msg->will_flag) {
307306
/* will topic */
308307
pkt_size += INT_SIZE;
309-
msg->will_topic_len = mqtt_strlen(msg->will_topic);
310308
pkt_size += msg->will_topic_len;
311309
/* will message - binary */
312310
pkt_size += INT_SIZE;
@@ -316,7 +314,6 @@ int mqtt_pack_connect(uint8_t *buf, uint16_t *length, uint16_t size,
316314
/* user_name - UTF-8 - optional */
317315
if (msg->user_name) {
318316
pkt_size += INT_SIZE;
319-
msg->user_name_len = mqtt_strlen(msg->user_name);
320317
pkt_size += msg->user_name_len;
321318
}
322319

@@ -850,7 +847,6 @@ int mqtt_pack_publish(uint8_t *buf, uint16_t *length, uint16_t size,
850847
return -EINVAL;
851848
}
852849

853-
msg->topic_len = mqtt_strlen(msg->topic);
854850
/* Packet Identifier is only included if QoS > QoS0. See MQTT 3.3.2.2
855851
* So, payload size is:
856852
* topic length size + topic length + packet id + msg's size

tests/net/lib/mqtt_packet/src/mqtt_packet.c

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010

1111
#define RC_STR(rc) (rc == TC_PASS ? PASS : FAIL)
1212

13+
#define CLIENTID "zephyr"
14+
#define CLIENTID_LEN 6
15+
#define TOPIC "sensors"
16+
#define TOPIC_LEN 7
17+
#define WILL_TOPIC "quitting"
18+
#define WILL_TOPIC_LEN 8
19+
#define USERNAME "zephyr1"
20+
#define USERNAME_LEN 7
21+
1322
/* MQTT messages in this test are under 256 bytes */
1423
#define BUF_SIZE 256
1524
static uint8_t buf[BUF_SIZE];
@@ -220,7 +229,9 @@ uint8_t connect1[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
220229
0x70, 0x68, 0x79, 0x72};
221230

222231
static struct mqtt_connect_msg msg_connect1 = {
223-
.clean_session = 1, .client_id = "zephyr", .will_flag = 0,
232+
.clean_session = 1, .client_id = CLIENTID,
233+
.client_id_len = CLIENTID_LEN,
234+
.will_flag = 0,
224235
.will_qos = 0, .will_retain = 0, .will_topic = NULL,
225236
.will_msg = NULL, .will_msg_len = 0,
226237
.keep_alive = 0, .user_name = NULL,
@@ -240,7 +251,9 @@ uint8_t connect2[] = {0x10, 0x12, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
240251
0x70, 0x68, 0x79, 0x72};
241252

242253
static struct mqtt_connect_msg msg_connect2 = {
243-
.clean_session = 1, .client_id = "zephyr", .will_flag = 0,
254+
.clean_session = 1, .client_id = CLIENTID,
255+
.client_id_len = CLIENTID_LEN,
256+
.will_flag = 0,
244257
.will_qos = 0, .will_retain = 0, .will_topic = NULL,
245258
.will_msg = NULL, .will_msg_len = 0,
246259
.keep_alive = 365, .user_name = NULL,
@@ -265,8 +278,11 @@ uint8_t connect3[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
265278
0x62, 0x79, 0x65};
266279

267280
static struct mqtt_connect_msg msg_connect3 = {
268-
.clean_session = 1, .client_id = "zephyr", .will_flag = 1,
269-
.will_qos = 0, .will_retain = 0, .will_topic = "quitting",
281+
.clean_session = 1, .client_id = CLIENTID,
282+
.client_id_len = CLIENTID_LEN,
283+
.will_flag = 1,
284+
.will_qos = 0, .will_retain = 0, .will_topic = WILL_TOPIC,
285+
.will_topic_len = WILL_TOPIC_LEN,
270286
.will_msg = "bye", .will_msg_len = 3,
271287
.keep_alive = 0, .user_name = NULL,
272288
.password = NULL, .password_len = 0
@@ -288,8 +304,11 @@ uint8_t connect4[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
288304
0x62, 0x79, 0x65};
289305

290306
static struct mqtt_connect_msg msg_connect4 = {
291-
.clean_session = 1, .client_id = "zephyr", .will_flag = 1,
292-
.will_qos = 0, .will_retain = 1, .will_topic = "quitting",
307+
.clean_session = 1, .client_id = CLIENTID,
308+
.client_id_len = CLIENTID_LEN,
309+
.will_flag = 1,
310+
.will_qos = 0, .will_retain = 1, .will_topic = WILL_TOPIC,
311+
.will_topic_len = WILL_TOPIC_LEN,
293312
.will_msg = "bye", .will_msg_len = 3,
294313
.keep_alive = 0, .user_name = NULL,
295314
.password = NULL, .password_len = 0
@@ -311,8 +330,11 @@ uint8_t connect5[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
311330
0x62, 0x79, 0x65};
312331

313332
static struct mqtt_connect_msg msg_connect5 = {
314-
.clean_session = 1, .client_id = "zephyr", .will_flag = 1,
315-
.will_qos = 1, .will_retain = 0, .will_topic = "quitting",
333+
.clean_session = 1, .client_id = CLIENTID,
334+
.client_id_len = CLIENTID_LEN,
335+
.will_flag = 1,
336+
.will_qos = 1, .will_retain = 0, .will_topic = WILL_TOPIC,
337+
.will_topic_len = WILL_TOPIC_LEN,
316338
.will_msg = "bye", .will_msg_len = 3,
317339
.keep_alive = 0, .user_name = NULL,
318340
.password = NULL, .password_len = 0
@@ -334,8 +356,11 @@ uint8_t connect6[] = {0x10, 0x21, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
334356
0x62, 0x79, 0x65};
335357

336358
static struct mqtt_connect_msg msg_connect6 = {
337-
.clean_session = 1, .client_id = "zephyr", .will_flag = 1,
338-
.will_qos = 1, .will_retain = 1, .will_topic = "quitting",
359+
.clean_session = 1, .client_id = CLIENTID,
360+
.client_id_len = CLIENTID_LEN,
361+
.will_flag = 1,
362+
.will_qos = 1, .will_retain = 1, .will_topic = WILL_TOPIC,
363+
.will_topic_len = WILL_TOPIC_LEN,
339364
.will_msg = "bye", .will_msg_len = 3,
340365
.keep_alive = 0, .user_name = NULL,
341366
.password = NULL, .password_len = 0
@@ -360,10 +385,14 @@ uint8_t connect7[] = {0x10, 0x34, 0x00, 0x04, 0x4d, 0x51, 0x54, 0x54,
360385
0x73, 0x73, 0x77, 0x6f, 0x72, 0x64};
361386

362387
static struct mqtt_connect_msg msg_connect7 = {
363-
.clean_session = 1, .client_id = "zephyr", .will_flag = 1,
364-
.will_qos = 1, .will_retain = 1, .will_topic = "quitting",
388+
.clean_session = 1, .client_id = CLIENTID,
389+
.client_id_len = CLIENTID_LEN,
390+
.will_flag = 1,
391+
.will_qos = 1, .will_retain = 1, .will_topic = WILL_TOPIC,
392+
.will_topic_len = WILL_TOPIC_LEN,
365393
.will_msg = "bye", .will_msg_len = 3,
366-
.keep_alive = 0, .user_name = "zephyr1",
394+
.keep_alive = 0, .user_name = USERNAME,
395+
.user_name_len = USERNAME_LEN,
367396
.password = "password", .password_len = 8
368397
};
369398

@@ -382,7 +411,8 @@ uint8_t publish1[] = {0x30, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
382411
0x6f, 0x72, 0x73, 0x4f, 0x4b};
383412

384413
static struct mqtt_publish_msg msg_publish1 = {
385-
.dup = 0, .qos = 0, .retain = 0, .topic = "sensors",
414+
.dup = 0, .qos = 0, .retain = 0, .topic = TOPIC,
415+
.topic_len = TOPIC_LEN,
386416
.pkt_id = 0, .msg = "OK", .msg_len = 2,
387417
};
388418

@@ -398,7 +428,8 @@ uint8_t publish2[] = {0x31, 0x0b, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
398428
0x6f, 0x72, 0x73, 0x4f, 0x4b};
399429

400430
static struct mqtt_publish_msg msg_publish2 = {
401-
.dup = 0, .qos = 0, .retain = 1, .topic = "sensors",
431+
.dup = 0, .qos = 0, .retain = 1, .topic = TOPIC,
432+
.topic_len = TOPIC_LEN,
402433
.pkt_id = 0, .msg = "OK", .msg_len = 2
403434
};
404435

@@ -414,7 +445,8 @@ uint8_t publish3[] = {0x33, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
414445
0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};
415446

416447
static struct mqtt_publish_msg msg_publish3 = {
417-
.dup = 0, .qos = 1, .retain = 1, .topic = "sensors",
448+
.dup = 0, .qos = 1, .retain = 1, .topic = TOPIC,
449+
.topic_len = TOPIC_LEN,
418450
.pkt_id = 1, .msg = "OK", .msg_len = 2
419451
};
420452

@@ -429,7 +461,8 @@ static
429461
uint8_t publish4[] = {0x34, 0x0d, 0x00, 0x07, 0x73, 0x65, 0x6e, 0x73,
430462
0x6f, 0x72, 0x73, 0x00, 0x01, 0x4f, 0x4b};
431463
static struct mqtt_publish_msg msg_publish4 = {
432-
.dup = 0, .qos = 2, .retain = 0, .topic = "sensors",
464+
.dup = 0, .qos = 2, .retain = 0, .topic = TOPIC,
465+
.topic_len = TOPIC_LEN,
433466
.pkt_id = 1, .msg = "OK", .msg_len = 2
434467
};
435468

0 commit comments

Comments
 (0)