From 2df4a0f9e253d983a611d5030e14319e86f18512 Mon Sep 17 00:00:00 2001 From: Flavio Santes Date: Mon, 30 May 2016 09:53:25 -0500 Subject: [PATCH] samples/net/mqtt: Update README file and code cleanup Add instructions to download Paho's MQTT Library. Simplify if statement. Change-Id: Iba500c47a56896f9c9b726751933443a5318ced7 Signed-off-by: Flavio Santes --- samples/net/paho_mqtt_client/README | 19 +++++++++++++++---- samples/net/paho_mqtt_client/src/main.c | 6 +++--- samples/net/paho_mqtt_client/src/mqtt.c | 20 ++++++++------------ samples/net/paho_mqtt_client/src/tcp.h | 1 - 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/samples/net/paho_mqtt_client/README b/samples/net/paho_mqtt_client/README index ad969f7f27f..3ec046826d7 100644 --- a/samples/net/paho_mqtt_client/README +++ b/samples/net/paho_mqtt_client/README @@ -1,4 +1,4 @@ -MQTT sample using the Paho's MQTT Packet. +MQTT sample using the Paho's MQTT Packet Library. Requirements ------------ @@ -25,9 +25,20 @@ Building instructions See http://www.eclipse.org/paho/clients/c/embedded/ for more information about Paho's MQTT Packet Library. -* Modify the src/Makefile to reflect the location of your Paho - repository. In this example, it is assumed that src/paho_mqtt_pkt - is the directory that contains the MQTTPacket source code. + Inside samples/net/paho_mqtt_client, run the following commands: + + git clone https://git.eclipse.org/r/paho/org.eclipse.paho.mqtt.embedded-c paho + + Now "paho" contains the MQTT Packet Library. + +* make pristine && make are enough to build this sample. + +* Follow the steps indicated here: + + https://www.zephyrproject.org/doc/board/galileo.html + + to load the binary into the Galileo Dev Board. + Usage ----- diff --git a/samples/net/paho_mqtt_client/src/main.c b/samples/net/paho_mqtt_client/src/main.c index ed34a6f8575..329e672bef9 100644 --- a/samples/net/paho_mqtt_client/src/main.c +++ b/samples/net/paho_mqtt_client/src/main.c @@ -24,9 +24,9 @@ #include "mqtt.h" #define RC_MSG(rc) (rc) == 0 ? "success" : "failure" -#define STACKSIZE 1024 +#define STACK_SIZE 1024 -uint8_t stack[STACKSIZE]; +uint8_t stack[STACK_SIZE]; struct net_context *ctx; @@ -72,6 +72,6 @@ void main(void) net_init(); tcp_init(&ctx); - task_fiber_start(stack, STACKSIZE, (nano_fiber_entry_t)fiber, + task_fiber_start(stack, STACK_SIZE, (nano_fiber_entry_t)fiber, 0, 0, 7, 0); } diff --git a/samples/net/paho_mqtt_client/src/mqtt.c b/samples/net/paho_mqtt_client/src/mqtt.c index 36a4bcd36a3..7aebc6dc0c6 100644 --- a/samples/net/paho_mqtt_client/src/mqtt.c +++ b/samples/net/paho_mqtt_client/src/mqtt.c @@ -18,6 +18,7 @@ #include #include "mqtt.h" +#include "tcp.h" #include "MQTTPacket.h" #include "MQTTConnect.h" @@ -29,9 +30,6 @@ #define BUF_SIZE 128 static uint8_t mqtt_buffer[BUF_SIZE]; - -#include "tcp.h" - int mqtt_connect(struct net_context *ctx, char *client_name) { MQTTPacket_connectData data = MQTTPacket_connectData_initializer; @@ -70,12 +68,8 @@ int mqtt_connect(struct net_context *ctx, char *client_name) } rc = MQTTDeserialize_connack(&session_present, &conn_ack, mqtt_buffer, rx_len); - rc = rc != 1 ? -EINVAL : 0; - if (rc != 0) { - return -EINVAL; - } - return conn_ack; + return rc == 1 ? conn_ack : -EINVAL; } int mqtt_disconnect(struct net_context *ctx) @@ -157,7 +151,7 @@ int mqtt_pingreq(struct net_context *ctx) int mqtt_publish_read(struct net_context *ctx) { - MQTTString received_topic; + MQTTString topic; unsigned char dup; unsigned char retained; unsigned short msg_id; @@ -173,11 +167,13 @@ int mqtt_publish_read(struct net_context *ctx) } rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msg_id, - &received_topic, &msg, &msg_len, + &topic, &msg, &msg_len, mqtt_buffer, rx_len); rc = rc == 1 ? 0 : -EIO; if (rc == 0) { - printf("\n\tReceived message: %.*s\n\n", msg_len, msg); + printf("\n\tReceived message: [%.*s] %.*s\n\n", + topic.lenstring.len, topic.lenstring.data, + msg_len, msg); } return rc; } @@ -214,6 +210,6 @@ int mqtt_subscribe(struct net_context *ctx, char *topic) rc = MQTTDeserialize_suback(&submsg_id, 1, &sub_count, &granted_qos, mqtt_buffer, rx_len); - return rc != 1 ? -EINVAL : granted_qos; + return rc == 1 ? granted_qos : -EINVAL; } diff --git a/samples/net/paho_mqtt_client/src/tcp.h b/samples/net/paho_mqtt_client/src/tcp.h index 04b4a4ade02..7d6fc906ea7 100644 --- a/samples/net/paho_mqtt_client/src/tcp.h +++ b/samples/net/paho_mqtt_client/src/tcp.h @@ -23,5 +23,4 @@ int tcp_init(struct net_context **ctx); 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); - #endif