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 <flavio.santes@intel.com>
This commit is contained in:
Flavio Santes 2016-05-30 09:53:25 -05:00
commit 2df4a0f9e2
4 changed files with 26 additions and 20 deletions

View file

@ -1,4 +1,4 @@
MQTT sample using the Paho's MQTT Packet. MQTT sample using the Paho's MQTT Packet Library.
Requirements Requirements
------------ ------------
@ -25,9 +25,20 @@ Building instructions
See http://www.eclipse.org/paho/clients/c/embedded/ for more See http://www.eclipse.org/paho/clients/c/embedded/ for more
information about Paho's MQTT Packet Library. information about Paho's MQTT Packet Library.
* Modify the src/Makefile to reflect the location of your Paho Inside samples/net/paho_mqtt_client, run the following commands:
repository. In this example, it is assumed that src/paho_mqtt_pkt
is the directory that contains the MQTTPacket source code. 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 Usage
----- -----

View file

@ -24,9 +24,9 @@
#include "mqtt.h" #include "mqtt.h"
#define RC_MSG(rc) (rc) == 0 ? "success" : "failure" #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; struct net_context *ctx;
@ -72,6 +72,6 @@ void main(void)
net_init(); net_init();
tcp_init(&ctx); 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); 0, 0, 7, 0);
} }

View file

@ -18,6 +18,7 @@
#include <errno.h> #include <errno.h>
#include "mqtt.h" #include "mqtt.h"
#include "tcp.h"
#include "MQTTPacket.h" #include "MQTTPacket.h"
#include "MQTTConnect.h" #include "MQTTConnect.h"
@ -29,9 +30,6 @@
#define BUF_SIZE 128 #define BUF_SIZE 128
static uint8_t mqtt_buffer[BUF_SIZE]; static uint8_t mqtt_buffer[BUF_SIZE];
#include "tcp.h"
int mqtt_connect(struct net_context *ctx, char *client_name) int mqtt_connect(struct net_context *ctx, char *client_name)
{ {
MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 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, rc = MQTTDeserialize_connack(&session_present, &conn_ack,
mqtt_buffer, rx_len); 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) 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) int mqtt_publish_read(struct net_context *ctx)
{ {
MQTTString received_topic; MQTTString topic;
unsigned char dup; unsigned char dup;
unsigned char retained; unsigned char retained;
unsigned short msg_id; unsigned short msg_id;
@ -173,11 +167,13 @@ int mqtt_publish_read(struct net_context *ctx)
} }
rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msg_id, rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msg_id,
&received_topic, &msg, &msg_len, &topic, &msg, &msg_len,
mqtt_buffer, rx_len); mqtt_buffer, rx_len);
rc = rc == 1 ? 0 : -EIO; rc = rc == 1 ? 0 : -EIO;
if (rc == 0) { 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; 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, rc = MQTTDeserialize_suback(&submsg_id, 1, &sub_count, &granted_qos,
mqtt_buffer, rx_len); mqtt_buffer, rx_len);
return rc != 1 ? -EINVAL : granted_qos; return rc == 1 ? granted_qos : -EINVAL;
} }

View file

@ -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_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);
#endif #endif