From 1eb86de3003f129acf76ea0b06773efb3f042edf Mon Sep 17 00:00:00 2001 From: HMS-LIG <15227320+HMS-LIG@users.noreply.github.com> Date: Fri, 25 May 2018 10:43:25 +0200 Subject: [PATCH] topic now constructed from deviceid The mqtt topic used when publishing is now automatically constructed based on deviceid. --- iot/mqtt-ciotc/mqtt_ciotc.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/iot/mqtt-ciotc/mqtt_ciotc.c b/iot/mqtt-ciotc/mqtt_ciotc.c index c11bdfd..da4870b 100644 --- a/iot/mqtt-ciotc/mqtt_ciotc.c +++ b/iot/mqtt-ciotc/mqtt_ciotc.c @@ -30,6 +30,8 @@ #define TRACE 1 /* Set to 1 to enable tracing */ +#define TOPIC_FMT_STR "/devices/%s/events" + struct { char* address; enum { clientid_maxlen = 256, clientid_size }; @@ -40,7 +42,8 @@ struct { char* region; char* registryid; char* rootpath; - char* topic; + enum { topic_maxlen = 256, topic_size }; + char topic[topic_size]; char* payload; } opts = { .address = "ssl://mqtt.googleapis.com:8883", @@ -51,7 +54,7 @@ struct { .region = "{your-region-id}", .registryid = "{your-registry-id}", .rootpath = "roots.pem", - .topic = "/devices/{your-device-id}/events", + .topic = "", // Automatically calculated from deviceid and TOPIC_FMT_STR .payload = "Hello world!" }; @@ -224,6 +227,16 @@ bool GetOpts(int argc, char** argv) { printf("%s\n", opts.clientid); } + n = snprintf(opts.topic, sizeof(opts.topic), TOPIC_FMT_STR, opts.deviceid); + if (n < 0 || (n > topic_maxlen)) { + if (n < 0) { + printf("Encoding error!\n"); + } else { + printf("Error, buffer for storing topic was too small.\n"); + } + return false; + } + return true; // Caller must free opts.clientid } return false;