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:
parent
2df4a0f9e2
commit
4a320be8a2
5 changed files with 46 additions and 37 deletions
|
@ -1,4 +1,5 @@
|
||||||
CONFIG_STDOUT_CONSOLE=y
|
CONFIG_PRINTK=y
|
||||||
|
|
||||||
CONFIG_NETWORKING=y
|
CONFIG_NETWORKING=y
|
||||||
CONFIG_ETHERNET=y
|
CONFIG_ETHERNET=y
|
||||||
CONFIG_ETH_DW=y
|
CONFIG_ETH_DW=y
|
||||||
|
@ -7,7 +8,8 @@ CONFIG_NANO_TIMEOUTS=y
|
||||||
CONFIG_NETWORKING_WITH_TCP=y
|
CONFIG_NETWORKING_WITH_TCP=y
|
||||||
CONFIG_NETWORKING_WITH_IPV4=y
|
CONFIG_NETWORKING_WITH_IPV4=y
|
||||||
CONFIG_NETWORKING_IPV6_NO_ND=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
|
#CONFIG_NETWORKING_WITH_LOGGING=y
|
||||||
|
|
|
@ -15,9 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <zephyr.h>
|
#include <zephyr.h>
|
||||||
#include <stdio.h>
|
#include <misc/printk.h>
|
||||||
#include <sections.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "tcp.h"
|
#include "tcp.h"
|
||||||
|
@ -30,17 +28,21 @@ uint8_t stack[STACK_SIZE];
|
||||||
|
|
||||||
struct net_context *ctx;
|
struct net_context *ctx;
|
||||||
|
|
||||||
|
|
||||||
void fiber(void)
|
void fiber(void)
|
||||||
{
|
{
|
||||||
char *client_name = "zephyr_client";
|
char *client_name = "zephyr_client";
|
||||||
char *topic = "zephyr";
|
char *topic = "zephyr";
|
||||||
char *msg = "Hello World from 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;
|
int rc;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
rc = mqtt_connect(ctx, client_name);
|
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);
|
fiber_sleep(APP_SLEEP_TICKS);
|
||||||
} while (rc != 0);
|
} while (rc != 0);
|
||||||
|
@ -48,20 +50,24 @@ void fiber(void)
|
||||||
do {
|
do {
|
||||||
|
|
||||||
rc = mqtt_subscribe(ctx, topic);
|
rc = mqtt_subscribe(ctx, topic);
|
||||||
printf("Subscribe: %s\n", RC_MSG(rc));
|
printk("Subscribe: %s\n", RC_MSG(rc));
|
||||||
|
|
||||||
fiber_sleep(APP_SLEEP_TICKS);
|
fiber_sleep(APP_SLEEP_TICKS);
|
||||||
} while (rc != 0);
|
} while (rc != 0);
|
||||||
|
|
||||||
do {
|
do {
|
||||||
rc = mqtt_pingreq(ctx);
|
rc = mqtt_pingreq(ctx);
|
||||||
printf("Pingreq: %s\n", RC_MSG(rc));
|
printk("Pingreq: %s\n", RC_MSG(rc));
|
||||||
|
|
||||||
rc = mqtt_publish(ctx, topic, msg);
|
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);
|
rc = mqtt_publish_read(ctx, received_topic, received_msg);
|
||||||
printf("Publish read: %s\n", RC_MSG(rc));
|
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);
|
fiber_sleep(APP_SLEEP_TICKS);
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "tcp.h"
|
#include "tcp.h"
|
||||||
|
|
||||||
|
@ -149,7 +150,7 @@ int mqtt_pingreq(struct net_context *ctx)
|
||||||
return rc == 1 ? 0 : -EINVAL;
|
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;
|
MQTTString topic;
|
||||||
unsigned char dup;
|
unsigned char dup;
|
||||||
|
@ -169,15 +170,16 @@ int mqtt_publish_read(struct net_context *ctx)
|
||||||
rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msg_id,
|
rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msg_id,
|
||||||
&topic, &msg, &msg_len,
|
&topic, &msg, &msg_len,
|
||||||
mqtt_buffer, rx_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)
|
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;
|
return rc == 1 ? granted_qos : -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
int mqtt_connect(struct net_context *ctx, char *client_name);
|
int mqtt_connect(struct net_context *ctx, char *client_name);
|
||||||
int mqtt_disconnect(struct net_context *ctx);
|
int mqtt_disconnect(struct net_context *ctx);
|
||||||
int mqtt_publish(struct net_context *ctx, char *topic, char *msg);
|
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_subscribe(struct net_context *ctx, char *topic);
|
||||||
int mqtt_pingreq(struct net_context *ctx);
|
int mqtt_pingreq(struct net_context *ctx);
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,10 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <misc/printk.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "tcp.h"
|
#include "tcp.h"
|
||||||
|
|
||||||
|
@ -21,9 +25,6 @@
|
||||||
#include <net/net_core.h>
|
#include <net/net_core.h>
|
||||||
#include <net/net_socket.h>
|
#include <net/net_socket.h>
|
||||||
|
|
||||||
#include <errno.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
uip_ipaddr_t uip_hostaddr = { { CLIENT_IPADDR0, CLIENT_IPADDR1,
|
uip_ipaddr_t uip_hostaddr = { { CLIENT_IPADDR0, CLIENT_IPADDR1,
|
||||||
CLIENT_IPADDR2, CLIENT_IPADDR3 } };
|
CLIENT_IPADDR2, CLIENT_IPADDR3 } };
|
||||||
|
|
||||||
|
@ -38,17 +39,16 @@ uip_ipaddr_t uip_netmask = { { NETMASK0, NETMASK1, NETMASK2, NETMASK3 } };
|
||||||
|
|
||||||
#define INET_FAMILY AF_INET
|
#define INET_FAMILY AF_INET
|
||||||
|
|
||||||
|
|
||||||
int tcp_tx(struct net_context *ctx, uint8_t *buf, size_t size)
|
int tcp_tx(struct net_context *ctx, uint8_t *buf, size_t size)
|
||||||
{
|
{
|
||||||
int rc = 0;
|
|
||||||
uint8_t *ptr;
|
|
||||||
struct net_buf *nbuf = NULL;
|
struct net_buf *nbuf = NULL;
|
||||||
|
uint8_t *ptr;
|
||||||
|
int rc = 0;
|
||||||
|
|
||||||
nbuf = ip_buf_get_tx(ctx);
|
nbuf = ip_buf_get_tx(ctx);
|
||||||
if (nbuf == NULL) {
|
if (nbuf == NULL) {
|
||||||
printf("[%s:%d] Unable to get buffer\n", __func__, __LINE__);
|
printk("[%s:%d] Unable to get buffer\n", __func__, __LINE__);
|
||||||
return -1;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ptr = net_buf_add(nbuf, size);
|
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) {
|
switch (rc) {
|
||||||
case -EINPROGRESS:
|
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);
|
fiber_sleep(TCP_RETRY_TIMEOUT);
|
||||||
break;
|
break;
|
||||||
case -EAGAIN:
|
case -EAGAIN:
|
||||||
case -ECONNRESET:
|
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);
|
fiber_sleep(TCP_RETRY_TIMEOUT);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf("%s: sending %d bytes failed\n",
|
printk("%s: sending %d bytes failed\n",
|
||||||
__func__, size);
|
__func__, size);
|
||||||
ip_buf_unref(nbuf);
|
ip_buf_unref(nbuf);
|
||||||
return -EIO;
|
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 tcp_rx(struct net_context *ctx, uint8_t *buf, size_t *read_bytes, size_t size)
|
||||||
{
|
{
|
||||||
int rc;
|
|
||||||
struct net_buf *nbuf;
|
struct net_buf *nbuf;
|
||||||
|
int rc;
|
||||||
|
|
||||||
nbuf = net_receive(ctx, TCP_RX_TIMEOUT);
|
nbuf = net_receive(ctx, TCP_RX_TIMEOUT);
|
||||||
rc = -EIO;
|
rc = -EIO;
|
||||||
|
@ -119,7 +119,7 @@ int tcp_init(struct net_context **ctx)
|
||||||
&server, SERVER_MQTT_PORT,
|
&server, SERVER_MQTT_PORT,
|
||||||
&client, CLIENT_MQTT_PORT);
|
&client, CLIENT_MQTT_PORT);
|
||||||
if (*ctx == NULL) {
|
if (*ctx == NULL) {
|
||||||
printf("%s: Unable to get network context\n", __func__);
|
printk("%s: Unable to get network context\n", __func__);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue