-
Notifications
You must be signed in to change notification settings - Fork 2
/
mqtts-pub.c
160 lines (131 loc) · 4.31 KB
/
mqtts-pub.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
MQTT-S command-line publishing client
Copyright (C) 2013 Nicholas Humfrey
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License (http://www.gnu.org/copyleft/gpl.html)
for more details.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <signal.h>
#include "mqtts.h"
const char *client_id = NULL;
const char *topic_name = NULL;
const char *message_data = NULL;
const char *mqtts_host = "127.0.0.1";
const char *mqtts_port = "1883";
time_t keep_alive = 0;
uint16_t topic_id = 0;
uint8_t topic_id_type = MQTTS_TOPIC_TYPE_NORMAL;
int8_t qos = 0;
uint8_t retain = FALSE;
uint8_t debug = FALSE;
static void usage()
{
fprintf(stderr, "Usage: mqtts-pub [opts] -t <topic> -m <message>\n");
fprintf(stderr, "\n");
fprintf(stderr, " -d Enable debug messages.\n");
fprintf(stderr, " -h <host> MQTT-S host to connect to. Defaults to '%s'.\n", mqtts_host);
fprintf(stderr, " -i <clientid> ID to use for this client. Defaults to 'mqtts-tools-' appended with the process id.\n");
fprintf(stderr, " -m <message> Message payload to send.\n");
fprintf(stderr, " -n Send a null (zero length) message.\n");
fprintf(stderr, " -p <port> Network port to connect to. Defaults to %s.\n", mqtts_port);
fprintf(stderr, " -r Message should be retained.\n");
fprintf(stderr, " -t <topic> MQTT topic name to publish to.\n");
fprintf(stderr, " -T <topicid> Pre-defined MQTT-T topic ID to publish to.\n");
exit(-1);
}
static void parse_opts(int argc, char** argv)
{
int ch;
// Parse the options/switches
while ((ch = getopt(argc, argv, "dh:i:m:np:rt:T:?")) != -1)
switch (ch) {
case 'd':
debug = TRUE;
break;
case 'h':
mqtts_host = optarg;
break;
case 'i':
client_id = optarg;
break;
case 'm':
message_data = optarg;
break;
case 'n':
message_data = "";
break;
case 'p':
mqtts_port = optarg;
break;
case 'r':
retain = TRUE;
break;
case 't':
topic_name = optarg;
break;
case 'T':
topic_id = atoi(optarg);
break;
case '?':
default:
usage();
break;
}
// Missing Parameter?
if (!(topic_name || topic_id) || !message_data) {
usage();
}
// Both topic name and topic id?
if (topic_name && topic_id) {
fprintf(stderr, "Error: please provide either a topic id or a topic name, not both.\n");
}
}
int main(int argc, char* argv[])
{
int sock;
// Parse the command-line options
parse_opts(argc, argv);
// Enable debugging?
mqtts_set_debug(debug);
// Create a UDP socket
sock = mqtts_create_socket(mqtts_host, mqtts_port);
if (sock) {
// Connect to gateway
mqtts_send_connect(sock, client_id, keep_alive);
mqtts_recieve_connack(sock);
if (topic_id) {
// Use pre-defined topic ID
topic_id_type = MQTTS_TOPIC_TYPE_PREDEFINED;
} else if (strlen(topic_name) == 2) {
// Convert the 2 character topic name into a 2 byte topic id
topic_id = (topic_name[0] << 8) + topic_name[1];
topic_id_type = MQTTS_TOPIC_TYPE_SHORT;
} else {
// Register the topic name
mqtts_send_register(sock, topic_name);
topic_id = mqtts_recieve_regack(sock);
topic_id_type = MQTTS_TOPIC_TYPE_NORMAL;
}
// Publish to the topic
mqtts_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
// Finally, disconnect
mqtts_send_disconnect(sock);
close(sock);
}
return 0;
}