-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmqtt-sn-sub.c
288 lines (241 loc) · 9.98 KB
/
mqtt-sn-sub.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
MQTT-SN command-line subscribe client
Copyright (C) Nicholas Humfrey
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <unistd.h>
#include <getopt.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 <time.h>
#include "mqtt-sn.h"
const char *client_id = NULL;
// Array of pointers to topic names
char **topic_name_ar = NULL;
// Topic names array size. When too small it is incremented by ARRAY_INCREMENT.
uint16_t topic_name_ar_size = 0;
uint16_t topic_name_index = 0;
// Array of predefined topic IDs to subscribe to
uint16_t *predef_topic_id_ar = NULL;
// Predefined topic IDs array size. When too small it is incremented by ARRAY_INCREMENT.
uint16_t predef_topic_id_ar_size = 0;
uint16_t predef_topic_id_index = 0;
#define ARRAY_INCREMENT 10
const char *mqtt_sn_host = "127.0.0.1";
const char *mqtt_sn_port = MQTT_SN_DEFAULT_PORT;
uint16_t source_port = 0;
uint16_t keep_alive = MQTT_SN_DEFAULT_KEEP_ALIVE;
uint16_t sleep_duration = 0;
int8_t qos = 0;
uint8_t retain = FALSE;
uint8_t debug = 0;
uint8_t single_message = FALSE;
uint8_t clean_session = TRUE;
uint8_t verbose = 0;
uint8_t keep_running = TRUE;
static void usage()
{
fprintf(stderr, "Usage: mqtt-sn-sub [opts] -t <topic>\n");
fprintf(stderr, "\n");
fprintf(stderr, " -1 exit after receiving a single message.\n");
fprintf(stderr, " -c disable 'clean session' (store subscription and pending messages when client disconnects).\n");
fprintf(stderr, " -d Increase debug level by one. -d can occur multiple times.\n");
fprintf(stderr, " -h <host> MQTT-SN host to connect to. Defaults to '%s'.\n", mqtt_sn_host);
fprintf(stderr, " -i <clientid> ID to use for this client. Defaults to 'mqtt-sn-tools-' with process id.\n");
fprintf(stderr, " -k <keepalive> keep alive in seconds for this client. Defaults to %d.\n", keep_alive);
fprintf(stderr, " -e <sleep> sleep duration in seconds when disconnecting. Defaults to %d.\n", sleep_duration);
fprintf(stderr, " -p <port> Network port to connect to. Defaults to %s.\n", mqtt_sn_port);
fprintf(stderr, " -q <qos> QoS level to subscribe with (0 or 1). Defaults to %d.\n", qos);
fprintf(stderr, " -t <topic> MQTT-SN topic name to subscribe to. It may repeat multiple times.\n");
fprintf(stderr, " -T <topicid> Pre-defined MQTT-SN topic ID to subscribe to. It may repeat multiple times.\n");
fprintf(stderr, " --fe Enables Forwarder Encapsulation. Mqtt-sn packets are encapsulated according to MQTT-SN Protocol Specification v1.2, chapter 5.5 Forwarder Encapsulation.\n");
fprintf(stderr, " --wlnid If Forwarder Encapsulation is enabled, wireless node ID for this client. Defaults to process id.\n");
fprintf(stderr, " --cport <port> Source port for outgoing packets. Uses port in ephemeral range if not specified or set to %d.\n", source_port);
fprintf(stderr, " -v Print messages verbosely, showing the topic name.\n");
fprintf(stderr, " -V Print messages verbosely, showing current time and the topic name.\n");
exit(EXIT_FAILURE);
}
static void parse_opts(int argc, char** argv)
{
static struct option long_options[] = {
{"fe", no_argument, 0, 1000 },
{"wlnid", required_argument, 0, 1001 },
{"cport", required_argument, 0, 1002 },
{0, 0, 0, 0}
};
int ch;
/* getopt_long stores the option index here. */
int option_index = 0;
// Parse the options/switches
while ((ch = getopt_long(argc, argv, "1cdh:i:k:e:p:q:t:T:vV?", long_options, &option_index)) != -1)
switch (ch) {
case '1':
single_message = TRUE;
break;
case 'c':
clean_session = FALSE;
break;
case 'd':
debug++;
break;
case 'h':
mqtt_sn_host = optarg;
break;
case 'i':
client_id = optarg;
break;
case 'k':
keep_alive = atoi(optarg);
break;
case 'e':
sleep_duration = atoi(optarg);
break;
case 'p':
mqtt_sn_port = optarg;
break;
case 'q':
qos = atoi(optarg);
break;
case 't':
// Resize topic names array when too small
if (topic_name_index == topic_name_ar_size) {
topic_name_ar = realloc(topic_name_ar, (topic_name_ar_size + ARRAY_INCREMENT) * sizeof(char*)) ;
topic_name_ar_size += ARRAY_INCREMENT;
}
topic_name_ar[topic_name_index++] = optarg;
break;
case 'T':
// Resize predefined topic IDs array when too small
if (predef_topic_id_index == predef_topic_id_ar_size) {
predef_topic_id_ar = realloc(predef_topic_id_ar, (predef_topic_id_ar_size + ARRAY_INCREMENT) * sizeof(uint16_t));
predef_topic_id_ar_size += ARRAY_INCREMENT;
}
predef_topic_id_ar[predef_topic_id_index++] = atoi(optarg);
break;
case 1000:
mqtt_sn_enable_frwdencap();
break;
case 1001:
mqtt_sn_set_frwdencap_parameters((uint8_t*)optarg, strlen(optarg));
break;
case 1002:
source_port = atoi(optarg);
break;
case 'v':
// Prevent -v setting verbose level back down to 1 if already set to 2 by -V
verbose = (verbose == 0) ? 1 : verbose;
break;
case 'V':
verbose = 2;
break;
case '?':
default:
usage();
break;
}
// Missing Parameter?
if (!topic_name_index && !predef_topic_id_index) {
usage();
}
}
static void termination_handler (int signum)
{
switch(signum) {
case SIGHUP:
mqtt_sn_log_debug("Got hangup signal.");
break;
case SIGTERM:
mqtt_sn_log_debug("Got termination signal.");
break;
case SIGINT:
mqtt_sn_log_debug("Got interrupt signal.");
break;
}
// Signal the main thread to stop
keep_running = FALSE;
}
int main(int argc, char* argv[])
{
int sock;
mqtt_sn_disable_frwdencap();
// Parse the command-line options
parse_opts(argc, argv);
// Enable debugging?
mqtt_sn_set_debug(debug);
mqtt_sn_set_verbose(verbose);
mqtt_sn_set_timeout(keep_alive / 2);
// Setup signal handlers
signal(SIGTERM, termination_handler);
signal(SIGINT, termination_handler);
signal(SIGHUP, termination_handler);
// Create a UDP socket
sock = mqtt_sn_create_socket(mqtt_sn_host, mqtt_sn_port, source_port);
if (sock) {
// Connect to server
mqtt_sn_log_debug("Connecting...");
mqtt_sn_send_connect(sock, client_id, keep_alive, clean_session);
mqtt_sn_receive_connack(sock);
uint16_t i;
// Subscribe to the each topic name
for (i = 0; i < topic_name_index; i++) {
mqtt_sn_log_debug("Subscribing to topic name: %s ...", topic_name_ar[i]);
mqtt_sn_send_subscribe_topic_name(sock, topic_name_ar[i], qos);
// Wait for the subscription acknowledgment
uint16_t topic_id = mqtt_sn_receive_suback(sock);
if (topic_id && strlen(topic_name_ar[i]) > 2) {
mqtt_sn_register_topic(topic_id, topic_name_ar[i]);
}
}
// Subscribe to the each predefined topic ID
for (i = 0; i < predef_topic_id_index; i++) {
mqtt_sn_log_debug("Subscribing to predefined topic ID: %u ...", predef_topic_id_ar[i]);
mqtt_sn_send_subscribe_topic_id(sock, predef_topic_id_ar[i], qos);
// Wait for the subscription acknowledgment
mqtt_sn_receive_suback(sock);
}
// Keep processing packets until process is terminated
while(keep_running) {
publish_packet_t *packet = mqtt_sn_wait_for(MQTT_SN_TYPE_PUBLISH, sock);
if (packet) {
uint8_t packet_qos = packet->flags & MQTT_SN_FLAG_QOS_MASK;
if (packet_qos == MQTT_SN_FLAG_QOS_1) {
mqtt_sn_send_puback(sock, packet, MQTT_SN_ACCEPTED);
}
if (single_message) {
break;
}
}
}
// Finally, disconnect
mqtt_sn_log_debug("Disconnecting...");
mqtt_sn_send_disconnect(sock, sleep_duration);
mqtt_sn_receive_disconnect(sock);
close(sock);
}
mqtt_sn_cleanup();
free(topic_name_ar);
free(predef_topic_id_ar);
return 0;
}