Skip to content

Commit

Permalink
topic now constructed from deviceid
Browse files Browse the repository at this point in the history
The mqtt topic used when publishing is now automatically
constructed based on deviceid.
  • Loading branch information
HMS-LIG committed May 25, 2018
1 parent 1e31062 commit 1eb86de
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions iot/mqtt-ciotc/mqtt_ciotc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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",
Expand All @@ -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!"
};

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1eb86de

Please sign in to comment.