samples/net/mqtt: Reduce tx/rx buffer size and update mqtt_publish_read

Set CONFIG_IP_BUF_RX_SIZE and CONFIG_IP_BUF_TX_SIZE to 4.
Modify mqtt_publish_read to retrieve topic name and message as strings.
Change printf for printk.

Change-Id: I6746846cb3359096d87dfbb3134d6a3a95bf087e
Signed-off-by: Flavio Santes <flavio.santes@intel.com>
This commit is contained in:
Flavio Santes 2016-05-30 15:42:56 -05:00
parent 2df4a0f9e2
commit 4a320be8a2
5 changed files with 46 additions and 37 deletions

View file

@ -1,4 +1,5 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_PRINTK=y
CONFIG_NETWORKING=y
CONFIG_ETHERNET=y
CONFIG_ETH_DW=y
@ -7,7 +8,8 @@ CONFIG_NANO_TIMEOUTS=y
CONFIG_NETWORKING_WITH_TCP=y
CONFIG_NETWORKING_WITH_IPV4=y
CONFIG_NETWORKING_IPV6_NO_ND=y
CONFIG_IP_BUF_RX_SIZE=12
CONFIG_IP_BUF_TX_SIZE=13
CONFIG_IP_BUF_RX_SIZE=4
CONFIG_IP_BUF_TX_SIZE=4
#CONFIG_NETWORKING_WITH_LOGGING=y

View file

@ -15,9 +15,7 @@
*/
#include <zephyr.h>
#include <stdio.h>
#include <sections.h>
#include <errno.h>
#include <misc/printk.h>
#include "config.h"
#include "tcp.h"
@ -30,17 +28,21 @@ uint8_t stack[STACK_SIZE];
struct net_context *ctx;
void fiber(void)
{
char *client_name = "zephyr_client";
char *topic = "zephyr";
char *msg = "Hello World from Zephyr!";
/* it is assumed that these buffers have enough space to
* store the incoming topic and msg!
*/
char received_topic[32];
char received_msg[64];
int rc;
do {
rc = mqtt_connect(ctx, client_name);
printf("Connect: %s\n", RC_MSG(rc));
printk("Connect: %s\n", RC_MSG(rc));
fiber_sleep(APP_SLEEP_TICKS);
} while (rc != 0);
@ -48,20 +50,24 @@ void fiber(void)
do {
rc = mqtt_subscribe(ctx, topic);
printf("Subscribe: %s\n", RC_MSG(rc));
printk("Subscribe: %s\n", RC_MSG(rc));
fiber_sleep(APP_SLEEP_TICKS);
} while (rc != 0);
do {
rc = mqtt_pingreq(ctx);
printf("Pingreq: %s\n", RC_MSG(rc));
printk("Pingreq: %s\n", RC_MSG(rc));
rc = mqtt_publish(ctx, topic, msg);
printf("Publish: %s\n", RC_MSG(rc));
printk("Publish: %s\n", RC_MSG(rc));
rc = mqtt_publish_read(ctx);
printf("Publish read: %s\n", RC_MSG(rc));
rc = mqtt_publish_read(ctx, received_topic, received_msg);
if (rc == 0) {
printk("\n\tReceived topic: %s, msg: %s\n",
received_topic, received_msg);
}
printk("Publish read: %s\n", RC_MSG(rc));
fiber_sleep(APP_SLEEP_TICKS);
} while (1);

View file

@ -17,6 +17,7 @@
#include <string.h>
#include <errno.h>
#include "config.h"
#include "mqtt.h"
#include "tcp.h"
@ -149,7 +150,7 @@ int mqtt_pingreq(struct net_context *ctx)
return rc == 1 ? 0 : -EINVAL;
}
int mqtt_publish_read(struct net_context *ctx)
int mqtt_publish_read(struct net_context *ctx, char *topic_str, char *msg_str)
{
MQTTString topic;
unsigned char dup;
@ -169,15 +170,16 @@ int mqtt_publish_read(struct net_context *ctx)
rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msg_id,
&topic, &msg, &msg_len,
mqtt_buffer, rx_len);
rc = rc == 1 ? 0 : -EIO;
if (rc == 0) {
printf("\n\tReceived message: [%.*s] %.*s\n\n",
topic.lenstring.len, topic.lenstring.data,
msg_len, msg);
}
return rc;
}
if (rc == 1) {
memcpy(topic_str, topic.lenstring.data, topic.lenstring.len);
topic_str[topic.lenstring.len] = '\0';
memcpy(msg_str, msg, msg_len);
msg_str[msg_len] = '\0';
return 0;
}
return -EINVAL;
}
int mqtt_subscribe(struct net_context *ctx, char *topic)
{
@ -212,4 +214,3 @@ int mqtt_subscribe(struct net_context *ctx, char *topic)
return rc == 1 ? granted_qos : -EINVAL;
}

View file

@ -22,7 +22,7 @@
int mqtt_connect(struct net_context *ctx, char *client_name);
int mqtt_disconnect(struct net_context *ctx);
int mqtt_publish(struct net_context *ctx, char *topic, char *msg);
int mqtt_publish_read(struct net_context *ctx);
int mqtt_publish_read(struct net_context *ctx, char *topic_str, char *msg_str);
int mqtt_subscribe(struct net_context *ctx, char *topic);
int mqtt_pingreq(struct net_context *ctx);

View file

@ -14,6 +14,10 @@
* limitations under the License.
*/
#include <string.h>
#include <errno.h>
#include <misc/printk.h>
#include "config.h"
#include "tcp.h"
@ -21,9 +25,6 @@
#include <net/net_core.h>
#include <net/net_socket.h>
#include <errno.h>
#include <stdio.h>
uip_ipaddr_t uip_hostaddr = { { CLIENT_IPADDR0, CLIENT_IPADDR1,
CLIENT_IPADDR2, CLIENT_IPADDR3 } };
@ -38,17 +39,16 @@ uip_ipaddr_t uip_netmask = { { NETMASK0, NETMASK1, NETMASK2, NETMASK3 } };
#define INET_FAMILY AF_INET
int tcp_tx(struct net_context *ctx, uint8_t *buf, size_t size)
{
int rc = 0;
uint8_t *ptr;
struct net_buf *nbuf = NULL;
uint8_t *ptr;
int rc = 0;
nbuf = ip_buf_get_tx(ctx);
if (nbuf == NULL) {
printf("[%s:%d] Unable to get buffer\n", __func__, __LINE__);
return -1;
printk("[%s:%d] Unable to get buffer\n", __func__, __LINE__);
return -EINVAL;
}
ptr = net_buf_add(nbuf, size);
@ -63,16 +63,16 @@ int tcp_tx(struct net_context *ctx, uint8_t *buf, size_t size)
}
switch (rc) {
case -EINPROGRESS:
printf("%s: no connection yet, try again\n", __func__);
printk("%s: no connection yet, try again\n", __func__);
fiber_sleep(TCP_RETRY_TIMEOUT);
break;
case -EAGAIN:
case -ECONNRESET:
printf("%s: no connection, try again later\n", __func__);
printk("%s: no connection, try again later\n", __func__);
fiber_sleep(TCP_RETRY_TIMEOUT);
break;
default:
printf("%s: sending %d bytes failed\n",
printk("%s: sending %d bytes failed\n",
__func__, size);
ip_buf_unref(nbuf);
return -EIO;
@ -84,8 +84,8 @@ int tcp_tx(struct net_context *ctx, uint8_t *buf, size_t size)
int tcp_rx(struct net_context *ctx, uint8_t *buf, size_t *read_bytes, size_t size)
{
int rc;
struct net_buf *nbuf;
int rc;
nbuf = net_receive(ctx, TCP_RX_TIMEOUT);
rc = -EIO;
@ -119,7 +119,7 @@ int tcp_init(struct net_context **ctx)
&server, SERVER_MQTT_PORT,
&client, CLIENT_MQTT_PORT);
if (*ctx == NULL) {
printf("%s: Unable to get network context\n", __func__);
printk("%s: Unable to get network context\n", __func__);
return -EINVAL;
}
return 0;