Skip to content

Commit c1c5e25

Browse files
ldtsjukkar
authored andcommitted
net/mqtt: Add support for IBM BlueMix Watson topic format
Change-Id: I044180d9f76a307c1cf4423a9b1d3ee6c540cbc7 Signed-off-by: Jorge Ramirez-Ortiz <[email protected]>
1 parent 057f31e commit c1c5e25

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

samples/net/mqtt_publisher/README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ Max number of MQTT PUBLISH iterations
6767
6868
#define APP_MAX_ITERATIONS 5
6969
70+
IBM BlueMix IoT Watson topic format
71+
72+
.. code block:: c
73+
74+
#define ENABLE_BLUEMIX_TOPIC 0
75+
7076
On your Linux host computer, open a terminal window, locate the source code
7177
of this sample application (i.e. :file:`samples/net/mqtt_publisher`) and type:
7278

samples/net/mqtt_publisher/src/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,6 @@
3434

3535
#define APP_MAX_ITERATIONS 100
3636

37+
#define ENABLE_BLUEMIX_TOPIC 0
38+
3739
#endif

samples/net/mqtt_publisher/src/main.c

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <misc/printk.h>
1414
#include <string.h>
1515
#include <errno.h>
16+
#include <stdio.h>
1617

1718
#if defined(CONFIG_NET_L2_BLUETOOTH)
1819
#include <bluetooth/bluetooth.h>
@@ -24,6 +25,8 @@
2425

2526
#define CLIENTID "zephyr_publisher"
2627

28+
static bool bluemix_publisher;
29+
2730
/**
2831
* @brief mqtt_client_ctx Container of some structures used by the
2932
* publisher app.
@@ -176,28 +179,58 @@ void malformed_cb(struct mqtt_ctx *mqtt_ctx, uint16_t pkt_type)
176179
printk("[%s:%d] pkt_type: %u\n", __func__, __LINE__, pkt_type);
177180
}
178181

179-
static const char topic[] = "sensors";
182+
static
183+
char *get_payload(enum mqtt_qos qos)
184+
{
185+
static char payload[30];
186+
187+
if (bluemix_publisher) {
188+
snprintf(payload,
189+
sizeof(payload),
190+
"{d:{temperature:%d}}",
191+
(uint8_t) sys_rand32_get());
192+
} else {
193+
strncpy(payload, "DOORS:OPEN_QoSx", sizeof(payload));
194+
payload[strlen(payload) - 1] = '0' + qos;
195+
}
180196

181-
char payload[] = "DOORS:OPEN_QoSx";
197+
return payload;
198+
}
182199

183200
static
184-
void prepare_mqtt_publish_msg(struct mqtt_publish_msg *pub_msg,
185-
enum mqtt_qos qos)
201+
char *get_topic(void)
186202
{
187-
payload[strlen(payload) - 1] = '0' + qos;
203+
static char topic[50];
204+
205+
if (bluemix_publisher) {
206+
snprintf(topic, sizeof(topic),
207+
"iot-2/type/%s/id/%s/evt/%s/fmt/%s",
208+
"sensor", /* device type */
209+
"carbon", /* device id */
210+
"status", /* event type */
211+
"json"); /* event format */
212+
} else {
213+
strncpy(topic, "sensors", sizeof(topic));
214+
}
188215

216+
return topic;
217+
}
218+
219+
static
220+
void prepare_mqtt_publish_msg(struct mqtt_publish_msg *pub_msg,
221+
enum mqtt_qos qos)
222+
{
189223
/* MQTT message payload may be anything, we we use C strings */
190-
pub_msg->msg = payload;
224+
pub_msg->msg = get_payload(qos);
191225
/* Payload's length */
192226
pub_msg->msg_len = strlen(client_ctx.pub_msg.msg);
193227
/* MQTT Quality of Service */
194228
pub_msg->qos = qos;
195229
/* Message's topic */
196-
pub_msg->topic = (char *)topic;
230+
pub_msg->topic = get_topic();
197231
pub_msg->topic_len = strlen(client_ctx.pub_msg.topic);
198232
/* Packet Identifier, always use different values */
199233
pub_msg->pkt_id = sys_rand32_get();
200-
201234
}
202235

203236
#define RC_STR(rc) ((rc) == 0 ? "OK" : "ERROR")
@@ -464,5 +497,9 @@ int network_setup(struct net_context **net_ctx, const char *local_addr,
464497

465498
void main(void)
466499
{
500+
501+
#if ENABLE_BLUEMIX_TOPIC
502+
bluemix_publisher = true;
503+
#endif
467504
publisher();
468505
}

0 commit comments

Comments
 (0)