diff --git a/samples/net/paho_mqtt_client/prj_ethernet.conf b/samples/net/paho_mqtt_client/prj_ethernet.conf index 9a65653941c..809a3f9701a 100644 --- a/samples/net/paho_mqtt_client/prj_ethernet.conf +++ b/samples/net/paho_mqtt_client/prj_ethernet.conf @@ -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 diff --git a/samples/net/paho_mqtt_client/src/main.c b/samples/net/paho_mqtt_client/src/main.c index 329e672bef9..1d486eb03e0 100644 --- a/samples/net/paho_mqtt_client/src/main.c +++ b/samples/net/paho_mqtt_client/src/main.c @@ -15,9 +15,7 @@ */ #include -#include -#include -#include +#include #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); diff --git a/samples/net/paho_mqtt_client/src/mqtt.c b/samples/net/paho_mqtt_client/src/mqtt.c index 7aebc6dc0c6..faa19961505 100644 --- a/samples/net/paho_mqtt_client/src/mqtt.c +++ b/samples/net/paho_mqtt_client/src/mqtt.c @@ -17,6 +17,7 @@ #include #include +#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; } - diff --git a/samples/net/paho_mqtt_client/src/mqtt.h b/samples/net/paho_mqtt_client/src/mqtt.h index dc2a0d3a9ef..4c31712d60c 100644 --- a/samples/net/paho_mqtt_client/src/mqtt.h +++ b/samples/net/paho_mqtt_client/src/mqtt.h @@ -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); diff --git a/samples/net/paho_mqtt_client/src/tcp.c b/samples/net/paho_mqtt_client/src/tcp.c index 82125f30955..65781374d4e 100644 --- a/samples/net/paho_mqtt_client/src/tcp.c +++ b/samples/net/paho_mqtt_client/src/tcp.c @@ -14,6 +14,10 @@ * limitations under the License. */ +#include +#include +#include + #include "config.h" #include "tcp.h" @@ -21,9 +25,6 @@ #include #include -#include -#include - 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;