Merge "Merge net branch into master"
diff --git a/include/net/mqtt_types.h b/include/net/mqtt_types.h
index 9bf275e..adc9885 100644
--- a/include/net/mqtt_types.h
+++ b/include/net/mqtt_types.h
@@ -56,7 +56,7 @@
 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 @@
 	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/include/net/net_if.h b/include/net/net_if.h
index f473156..a0af0f0 100644
--- a/include/net/net_if.h
+++ b/include/net/net_if.h
@@ -178,7 +178,7 @@
 	ATOMIC_DEFINE(flags, NET_IF_NUM_FLAGS);
 
 	/** Interface's L2 layer */
-	const struct net_l2 const *l2;
+	const struct net_l2 * const l2;
 
 	/** Interface's private L2 data pointer */
 	void *l2_data;
diff --git a/include/net/net_l2.h b/include/net/net_l2.h
index 1820639..f0f6cdc 100644
--- a/include/net/net_l2.h
+++ b/include/net/net_l2.h
@@ -58,7 +58,7 @@
 
 #define NET_L2_GET_NAME(_name) (__net_l2_##_name)
 #define NET_L2_DECLARE_PUBLIC(_name)					\
-	extern const struct net_l2 const NET_L2_GET_NAME(_name)
+	extern const struct net_l2 NET_L2_GET_NAME(_name)
 #define NET_L2_GET_CTX_TYPE(_name) _name##_CTX_TYPE
 
 extern struct net_l2 __net_l2_start[];
@@ -95,7 +95,7 @@
 extern struct net_l2 __net_l2_end[];
 
 #define NET_L2_INIT(_name, _recv_fn, _send_fn, _reserve_fn, _enable_fn)	\
-	const struct net_l2 const (NET_L2_GET_NAME(_name)) __used	\
+	const struct net_l2 (NET_L2_GET_NAME(_name)) __used		\
 	__attribute__((__section__(".net_l2.init"))) = {		\
 		.recv = (_recv_fn),					\
 		.send = (_send_fn),					\
diff --git a/samples/net/README b/samples/net/README
deleted file mode 100644
index cf6fa1b..0000000
--- a/samples/net/README
+++ /dev/null
@@ -1,89 +0,0 @@
-Description of various IP stack test applications
-=================================================
-
-echo_server
------------
-
-The echo server test implements a network server that listens
-UDP sockets. If that socket receives data, the server reverses
-the data and sends it back.
-
-The echo client can be running in the host system in which
-case you need to use SLIP to connect to qemu. This usage scenario
-is described in net-tools project README file [1]. This is the default
-if you type "make run" in echo_server test application directory.
-The network IP stack hooks right under IP stack (network level)
-and sends the IP packet to host using SLIP. The layer 2 is the
-SLIP layer in this case, no radio layer is simulated or used.
-
-The echo server qemu instance can also be running against echo
-client that is running in another qemu. For this you need two
-terminal windows. In terminal 1 go to echo_server directory
-and type "make server". This will start the echo server and setup
-qemu pipes in suitable way and it will also start monitor application
-that will store the transferred network traffic into pcap file
-for later analysis. Then in terminal 2 go to echo_client directory and
-type "make client". This will start the echo client that will
-start to send data to the server and verify that it has received
-the data back correctly. In the two qemu case we are simulating
-the whole radio network meaning that the saved pcap file will
-contain 802.15.4 network packets.
-
-
-echo_client
------------
-
-The echo client test implements a network client that will
-send UDP data to the echo server. The client verifies that
-it has received data to the sent message and that the data
-is correct.
-
-The echo server can be running in the host system in which case
-you need to use SLIP to connect to qemu. This usage scenario
-is described in net-tools project README file [1]. This is the default
-if you type "make run" in echo_client test application directory.
-The network IP stack hooks right under IP stack (network level)
-and sends the IP packet to host using SLIP. The layer 2 is the
-SLIP layer in this case, no radio layer is simulated or used.
-
-The echo client qemu instance can also be running against echo
-server that is running in another qemu. This test scenario is
-described in echo_server chapter above.
-
-
-zoap_server
------------
-
-The Zoap server application implements a trivial CoAP server
-which exposes a single resource "a/light", that only provides the
-GET method.
-
-It can be run similar to the echo_server sample: using two terminals,
-(1) for zoap_server, run 'make server' in its directory, (2) for
-zoap_client, run 'make client' in the zoap_client test directory.
-
-
-zoap_client
------------
-
-The Zoap client application does a single request against a 'a/light'
-resource, which has the first response lost (so retransmissions are
-basically verified).
-
-Please refer to the section above about how to run these applications
-together.
-
-wpanusb
--------
-
-The wpanusb application exports IEEE 802.15.4 radio over USB to be used in
-other Operating Systems like Linux. In this scenario Linux SoftMAC driver
-must be used implementing IEEE 802.15.4 stack inside Linux.
-
-wpan_serial
------------
-
-The wpan_serial application implements IEEE 802.15.4 "serial-radio" protocol.
-This is the protocol used in Contiki-based native border routers.
-
-[1] https://gerrit.zephyrproject.org/r/gitweb?p=net-tools.git;a=summary
diff --git a/samples/net/echo_client/README.rst b/samples/net/echo_client/README.rst
new file mode 100644
index 0000000..b34e0d1
--- /dev/null
+++ b/samples/net/echo_client/README.rst
@@ -0,0 +1,98 @@
+Echo Client
+###########
+
+Overview
+********
+
+The echo-client sample application for Zephyr implements a UDP/TCP client
+that will send IPv4 or IPv6 packets, wait for the data to be sent back,
+and then verify it matches the data that was sent.
+
+The source code for this sample application can be found at:
+:file:`samples/net/echo_client`.
+
+Building and Running
+********************
+
+There are multiple ways to use this application. One of the most common
+usage scenario is to run echo-client application inside QEMU. This is
+described in :ref:`networking with QEMU <networking_with_qemu>`.
+
+There are configuration files for different boards and setups in the
+echo-client directory:
+
+- :file:`prj_arduino_101_cc2520.conf`
+  Use this for Arduino 101 with external IEEE 802.15.4 cc2520 board.
+
+- :file:`prj_arduino_101.conf`
+  Use this for Arduino 101 with external enc28j60 ethernet board.
+
+- :file:`prj_bt.conf`
+  Use this for Bluetooth IPSP connectivity.
+
+- :file:`prj_cc2520.conf`
+  Use this for devices that have support for IEEE 802.15.4 cc2520 chip.
+
+- :file:`prj_frdm_k64f_cc2520.conf`
+  Use this for FRDM-K64F board with external IEEE 802.15.4 cc2520 board.
+
+- :file:`prj_frdm_k64f.conf`
+  Use this for FRDM-K64F board with built-in ethernet.
+
+- :file:`prj_frdm_k64f_mcr20a.conf`
+  Use this for FRDM-K64F board with IEEE 802.15.4 mcr20a board.
+
+- :file:`prj_qemu_802154.conf`
+  Use this when simulating IEEE 802.15.4 network using two QEMU's that
+  are connected together.
+
+- :file:`prj_qemu_cortex_m3.conf`
+  Use this for ARM QEMU.
+
+- :file:`prj_qemu_x86.conf`
+  Use this for x86 QEMU.
+
+- :file:`prj_sam_e70_xplained.conf`
+  Use this for Atmel SMART SAM E70 Xplained board with ethernet.
+
+Build echo-client sample application like this:
+
+.. code-block:: console
+
+    $ cd $ZEPHYR_BASE/samples/net/echo_client
+    $ make pristine && make CONF_FILE=<your desired conf file> \
+      BOARD=<board to use>
+
+Make can select the default configuration file based on the BOARD you've
+specified automatically so you might not always need to mention it.
+
+Running echo-server Linux Host
+==============================
+
+There is one useful testing scenario that can be used with Linux host.
+Here echo-client is run in QEMU and echo-server is run in Linux host.
+
+Run 'loop_socat' and 'loop-slip-tap' scripts from net-tools in Linux host.
+
+.. code-block:: console
+
+    $ ./loop_socat.sh
+
+In another window:
+
+.. code-block:: console
+
+    $ sudo ./loop-slip-tap.sh
+
+In third window:
+
+.. code-block:: console
+
+    $ sudo ./echo-server -i tap0
+
+Run echo-client application in QEMU:
+
+.. code-block:: console
+
+    $ cd $ZEPHYR_BASE/samples/net/echo-client
+    $ make pristine && make qemu
diff --git a/samples/net/echo_client/prj_slip.conf b/samples/net/echo_client/prj_slip.conf
deleted file mode 100644
index b264537..0000000
--- a/samples/net/echo_client/prj_slip.conf
+++ /dev/null
@@ -1,26 +0,0 @@
-CONFIG_NETWORKING=y
-CONFIG_NET_IPV6=y
-CONFIG_NET_IPV4=y
-CONFIG_NET_UDP=y
-CONFIG_NET_TCP=y
-CONFIG_TEST_RANDOM_GENERATOR=y
-CONFIG_NET_LOG=y
-CONFIG_NET_SLIP_TAP=y
-CONFIG_SYS_LOG_SHOW_COLOR=y
-CONFIG_INIT_STACKS=y
-CONFIG_PRINTK=y
-CONFIG_NET_STATISTICS=y
-CONFIG_NET_NBUF_RX_COUNT=14
-CONFIG_NET_NBUF_TX_COUNT=14
-CONFIG_NET_NBUF_DATA_COUNT=30
-CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
-CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2
-CONFIG_NET_MAX_CONTEXTS=10
-
-CONFIG_NET_SHELL=y
-
-CONFIG_NET_SAMPLES_IP_ADDRESSES=y
-CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::2"
-CONFIG_NET_SAMPLES_PEER_IPV6_ADDR="2001:db8::1"
-CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.0.2.2"
-CONFIG_NET_SAMPLES_PEER_IPV4_ADDR="192.0.2.1"
diff --git a/samples/net/echo_server/README.rst b/samples/net/echo_server/README.rst
new file mode 100644
index 0000000..4b188ed
--- /dev/null
+++ b/samples/net/echo_server/README.rst
@@ -0,0 +1,102 @@
+Echo Server
+###########
+
+Overview
+********
+
+The echo-server sample application for Zephyr implements a UDP/TCP server
+that complements the echo-client sample application: the echo-server listens
+for incoming IPv4 or IPv6 packets (sent by the echo client) and simply sends
+them back.
+
+The source code for this sample application can be found at:
+:file:`samples/net/echo_server`.
+
+Building and Running
+********************
+
+There are multiple ways to use this application. One of the most common
+usage scenario is to run echo-server application inside QEMU. This is
+described in :ref:`networking with QEMU <networking_with_qemu>`.
+
+There are configuration files for different boards and setups in the
+echo-server directory:
+
+- :file:`prj_arduino_101_cc2520.conf`
+  Use this for Arduino 101 with external IEEE 802.15.4 cc2520 board.
+
+- :file:`prj_arduino_101.conf`
+  Use this for Arduino 101 with external enc28j60 ethernet board.
+
+- :file:`prj_bt.conf`
+  Use this for Bluetooth IPSP connectivity.
+
+- :file:`prj_cc2520.conf`
+  Use this for devices that have support for IEEE 802.15.4 cc2520 chip.
+
+- :file:`prj_frdm_k64f_cc2520.conf`
+  Use this for FRDM-K64F board with external IEEE 802.15.4 cc2520 board.
+
+- :file:`prj_frdm_k64f.conf`
+  Use this for FRDM-K64F board with built-in ethernet.
+
+- :file:`prj_frdm_k64f_mcr20a.conf`
+  Use this for FRDM-K64F board with IEEE 802.15.4 mcr20a board.
+
+- :file:`prj_qemu_802154.conf`
+  Use this when simulating IEEE 802.15.4 network using two QEMU's that
+  are connected together.
+
+- :file:`prj_qemu_cortex_m3.conf`
+  Use this for ARM QEMU.
+
+- :file:`prj_qemu_x86.conf`
+  Use this for x86 QEMU.
+
+- :file:`prj_sam_e70_xplained.conf`
+  Use this for Atmel SMART SAM E70 Xplained board with ethernet.
+
+Build echo-server sample application like this:
+
+.. code-block:: console
+
+    $ cd $ZEPHYR_BASE/samples/net/echo_server
+    $ make pristine && make CONF_FILE=<your desired conf file> \
+      BOARD=<board to use>
+
+Make can select the default configuration file based on the BOARD you've
+specified automatically so you might not always need to mention it.
+
+Running echo-client in Linux Host
+=================================
+
+There is one useful testing scenario that can be used with Linux host.
+Here echo-server is run in QEMU and echo-client is run in Linux host.
+
+Run 'loop_socat' and 'loop-slip-tap' scripts from net-tools in Linux host.
+
+.. code-block:: console
+
+    $ ./loop_socat.sh
+
+In another window:
+
+.. code-block:: console
+
+    $ sudo ./loop-slip-tap.sh
+
+Run echo-server application in QEMU:
+
+.. code-block:: console
+
+    $ cd $ZEPHYR_BASE/samples/net/echo-server
+    $ make pristine && make qemu
+
+In third window:
+
+.. code-block:: console
+
+    $ sudo ./echo-client -i tap0 2001:db8::1
+
+Note that echo-server must be running in QEMU before you start the
+echo-client application in host terminal window.
diff --git a/samples/net/mqtt_publisher/README.rst b/samples/net/mqtt_publisher/README.rst
index be597f1..26b4e2c 100644
--- a/samples/net/mqtt_publisher/README.rst
+++ b/samples/net/mqtt_publisher/README.rst
@@ -67,6 +67,12 @@
 
 	#define APP_MAX_ITERATIONS	5
 
+IBM BlueMix IoT Watson topic format
+
+.. code block:: c
+
+	#define ENABLE_BLUEMIX_TOPIC	0
+
 On your Linux host computer, open a terminal window, locate the source code
 of this sample application (i.e. :file:`samples/net/mqtt_publisher`) and type:
 
diff --git a/samples/net/echo_server/prj_slip.conf b/samples/net/mqtt_publisher/prj_qemu_x86.conf
similarity index 63%
rename from samples/net/echo_server/prj_slip.conf
rename to samples/net/mqtt_publisher/prj_qemu_x86.conf
index da8fa1e..3980736 100644
--- a/samples/net/echo_server/prj_slip.conf
+++ b/samples/net/mqtt_publisher/prj_qemu_x86.conf
@@ -1,30 +1,37 @@
-
 CONFIG_NETWORKING=y
-CONFIG_NET_IPV6=y
-CONFIG_NET_IPV4=y
-CONFIG_NET_UDP=y
 CONFIG_NET_TCP=y
+CONFIG_RANDOM_GENERATOR=y
 CONFIG_TEST_RANDOM_GENERATOR=y
 CONFIG_NET_LOG=y
 CONFIG_NET_SLIP_TAP=y
-CONFIG_SYS_LOG_SHOW_COLOR=y
 CONFIG_INIT_STACKS=y
-CONFIG_PRINTK=y
-CONFIG_NET_STATISTICS=y
+
 CONFIG_NET_NBUF_RX_COUNT=16
 CONFIG_NET_NBUF_TX_COUNT=16
-CONFIG_NET_NBUF_DATA_COUNT=40
+CONFIG_NET_NBUF_DATA_COUNT=16
+
+CONFIG_NET_IPV6_RA_RDNSS=y
 CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
 CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2
-CONFIG_NET_MAX_CONTEXTS=16
 
-CONFIG_NET_SHELL=y
+CONFIG_STDOUT_CONSOLE=y
+
+# Enable IPv6 support
+CONFIG_NET_IPV6=n
+# Enable IPv4 support
+CONFIG_NET_IPV4=y
+
+# Enable the MQTT Lib
+CONFIG_MQTT_LIB=y
 
 CONFIG_NET_SAMPLES_IP_ADDRESSES=y
 CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1"
 CONFIG_NET_SAMPLES_PEER_IPV6_ADDR="2001:db8::2"
+
 CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.0.2.1"
 CONFIG_NET_SAMPLES_PEER_IPV4_ADDR="192.0.2.2"
 
-CONFIG_TELNET_CONSOLE=y
-CONFIG_SYS_LOG_TELNET_CONSOLE_LEVEL=1
+CONFIG_MAIN_STACK_SIZE=2048
+
+# For IPv6
+CONFIG_NET_NBUF_DATA_SIZE=256
diff --git a/samples/net/mqtt_publisher/src/config.h b/samples/net/mqtt_publisher/src/config.h
index e9e4d3e..71695c3 100644
--- a/samples/net/mqtt_publisher/src/config.h
+++ b/samples/net/mqtt_publisher/src/config.h
@@ -34,4 +34,6 @@
 
 #define APP_MAX_ITERATIONS	100
 
+#define ENABLE_BLUEMIX_TOPIC	0
+
 #endif
diff --git a/samples/net/mqtt_publisher/src/main.c b/samples/net/mqtt_publisher/src/main.c
index ce7289b..38dc016 100644
--- a/samples/net/mqtt_publisher/src/main.c
+++ b/samples/net/mqtt_publisher/src/main.c
@@ -13,6 +13,7 @@
 #include <misc/printk.h>
 #include <string.h>
 #include <errno.h>
+#include <stdio.h>
 
 #if defined(CONFIG_NET_L2_BLUETOOTH)
 #include <bluetooth/bluetooth.h>
@@ -22,6 +23,10 @@
 
 #include "config.h"
 
+#define CLIENTID "zephyr_publisher"
+
+static bool bluemix_publisher;
+
 /**
  * @brief mqtt_client_ctx	Container of some structures used by the
  *				publisher app.
@@ -174,28 +179,58 @@
 	printk("[%s:%d] pkt_type: %u\n", __func__, __LINE__, pkt_type);
 }
 
-static const char topic[] = "sensors";
+static
+char *get_payload(enum mqtt_qos qos)
+{
+	static char payload[30];
 
-char payload[] = "DOORS:OPEN_QoSx";
+	if (bluemix_publisher) {
+		snprintf(payload,
+			sizeof(payload),
+			"{d:{temperature:%d}}",
+			(uint8_t) sys_rand32_get());
+	} else {
+		strncpy(payload, "DOORS:OPEN_QoSx", sizeof(payload));
+		payload[strlen(payload) - 1] = '0' + qos;
+	}
+
+	return payload;
+}
+
+static
+char *get_topic(void)
+{
+	static char topic[50];
+
+	if (bluemix_publisher) {
+		snprintf(topic, sizeof(topic),
+			"iot-2/type/%s/id/%s/evt/%s/fmt/%s",
+			"sensor",	/* device type  */
+			"carbon",	/* device id    */
+			"status",	/* event type   */
+			"json");	/* event format */
+	} else {
+		strncpy(topic, "sensors", sizeof(topic));
+	}
+
+	return topic;
+}
 
 static
 void prepare_mqtt_publish_msg(struct mqtt_publish_msg *pub_msg,
-			      enum mqtt_qos qos)
+			enum mqtt_qos qos)
 {
-	payload[strlen(payload) - 1] = '0' + qos;
-
 	/* MQTT message payload may be anything, we we use C strings */
-	pub_msg->msg = payload;
+	pub_msg->msg = get_payload(qos);
 	/* Payload's length */
 	pub_msg->msg_len = strlen(client_ctx.pub_msg.msg);
 	/* MQTT Quality of Service */
 	pub_msg->qos = qos;
 	/* Message's topic */
-	pub_msg->topic = (char *)topic;
+	pub_msg->topic = get_topic();
 	pub_msg->topic_len = strlen(client_ctx.pub_msg.topic);
 	/* Packet Identifier, always use different values */
 	pub_msg->pkt_id = sys_rand32_get();
-
 }
 
 #define RC_STR(rc)	((rc) == 0 ? "OK" : "ERROR")
@@ -272,7 +307,8 @@
 	 * 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";
@@ -461,5 +497,9 @@
 
 void main(void)
 {
+
+#if ENABLE_BLUEMIX_TOPIC
+	bluemix_publisher = true;
+#endif
 	publisher();
 }
diff --git a/samples/net/mqtt_publisher/testcase.ini b/samples/net/mqtt_publisher/testcase.ini
index 4daf653..71374b6 100644
--- a/samples/net/mqtt_publisher/testcase.ini
+++ b/samples/net/mqtt_publisher/testcase.ini
@@ -1,7 +1,7 @@
 [test]
 tags = net mqtt
 build_only = true
-platform_whitelist = frdm_k64f
+platform_whitelist = frdm_k64f qemu_x86
 
 [test_bt]
 tags = net mqtt bluetooth
diff --git a/subsys/net/lib/mqtt/mqtt_pkt.c b/subsys/net/lib/mqtt/mqtt_pkt.c
index a04ee8f..08cf82f 100644
--- a/subsys/net/lib/mqtt/mqtt_pkt.c
+++ b/subsys/net/lib/mqtt/mqtt_pkt.c
@@ -112,7 +112,7 @@
  *
  * @retval 0 always
  */
-static int rlen_encode(uint8_t *buf, int len)
+static int rlen_encode(uint8_t *buf, uint32_t len)
 {
 	uint8_t encoded;
 	uint8_t i;
@@ -144,11 +144,11 @@
  * @retval 0 on success
  * @retval -ENOMEM if size < 4
  */
-static int rlen_decode(uint16_t *rlen, uint16_t *rlen_size,
+static int rlen_decode(uint32_t *rlen, uint16_t *rlen_size,
 		       uint8_t *buf, uint16_t size)
 {
-	uint16_t value = 0;
-	uint16_t mult = 1;
+	uint32_t value = 0;
+	uint32_t mult = 1;
 	uint16_t i = 0;
 	uint8_t encoded;
 
@@ -299,14 +299,12 @@
 
 	/* 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 @@
 	/* 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;
 	}
 
@@ -452,9 +449,9 @@
 {
 	uint8_t user_name_flag;
 	uint8_t password_flag;
-	uint16_t payload_len;
 	uint16_t rlen_size;
 	uint16_t val_u16;
+	uint32_t rlen;
 	uint8_t offset;
 	int rc;
 
@@ -472,12 +469,17 @@
 		return -EINVAL;
 	}
 
-	rc = rlen_decode(&payload_len, &rlen_size, buf + PACKET_TYPE_SIZE,
+	rc = rlen_decode(&rlen, &rlen_size, buf + PACKET_TYPE_SIZE,
 			 length - PACKET_TYPE_SIZE);
 	if (rc != 0) {
 		return rc;
 	}
 
+	/* header size + remaining length value + rm length size */
+	if (PACKET_TYPE_SIZE + rlen + rlen_size > length) {
+		return -EINVAL;
+	}
+
 	/* offset points to the protocol name length */
 	offset = PACKET_TYPE_SIZE + rlen_size;
 
@@ -711,7 +713,7 @@
 {
 	uint16_t rmlen_size;
 	uint16_t val_u16;
-	uint16_t rmlen;
+	uint32_t rmlen;
 	uint16_t offset;
 	uint8_t i;
 	int rc;
@@ -780,7 +782,7 @@
 	uint16_t rlen_size;
 	enum mqtt_qos qos;
 	uint16_t val_u16;
-	uint16_t rlen;
+	uint32_t rlen;
 	uint16_t offset;
 	uint8_t i;
 	int rc;
@@ -845,7 +847,6 @@
 		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
@@ -899,8 +900,8 @@
 {
 	uint16_t rmlen_size;
 	uint16_t val_u16;
-	uint16_t rmlen;
 	uint16_t offset;
+	uint32_t rmlen;
 	int rc;
 
 	if (buf[0] >> 4 != MQTT_PUBLISH) {
diff --git a/tests/net/lib/mqtt_packet/src/mqtt_packet.c b/tests/net/lib/mqtt_packet/src/mqtt_packet.c
index fc8e982..fd58ae9 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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 		      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 @@
 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
 };