forked from njh/mqtt-sn-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt-sn-sub.c
228 lines (188 loc) · 6.77 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
/*
MQTT-SN command-line publishing client
Copyright (C) 2013 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;
const char *topic_name = NULL;
const char *mqtt_sn_host = "127.0.0.1";
const char *mqtt_sn_port = MQTT_SN_DEFAULT_PORT;
uint16_t topic_id = 0;
uint16_t keep_alive = 10;
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, " -p <port> Network port to connect to. Defaults to %s.\n", mqtt_sn_port);
fprintf(stderr, " -t <topic> MQTT topic name to subscribe to.\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, " -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, 'f' },
{"wlnid", required_argument, 0, 'w' },
{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:p: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 'p':
mqtt_sn_port = optarg;
break;
case 't':
topic_name = optarg;
break;
case 'f':
mqtt_sn_enable_frwdencap();
break;
case 'w' :
mqtt_sn_set_frwdencap_parameters((uint8_t*)optarg, strlen(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) {
usage();
}
}
static void termination_handler (int signum)
{
switch(signum) {
case SIGHUP:
log_debug("Got hangup signal.");
break;
case SIGTERM:
log_debug("Got termination signal.");
break;
case SIGINT:
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);
if (sock) {
// Connect to server
log_debug("Connecting...");
mqtt_sn_send_connect(sock, client_id, keep_alive, clean_session);
mqtt_sn_receive_connack(sock);
// Subscribe to the topic
if (topic_name) {
mqtt_sn_send_subscribe_topic_name(sock, topic_name, 0);
} else {
mqtt_sn_send_subscribe_topic_id(sock, topic_id, 0);
}
// Wait for the subscription acknowledgment
topic_id = mqtt_sn_receive_suback(sock);
if (topic_id && topic_name && strlen(topic_name) > 2) {
mqtt_sn_register_topic(topic_id, topic_name);
}
// 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 && single_message) {
break;
}
}
// Finally, disconnect
log_debug("Disconnecting...");
mqtt_sn_send_disconnect(sock);
mqtt_sn_receive_disconnect(sock);
close(sock);
}
mqtt_sn_cleanup();
return 0;
}