-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmqtts-sub.c
173 lines (141 loc) · 4.52 KB
/
mqtts-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
/*
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 *mqtts_host = "127.0.0.1";
const char *mqtts_port = "1883";
uint16_t keep_alive = 10;
uint16_t topic_id = 0;
uint8_t retain = FALSE;
uint8_t debug = FALSE;
uint8_t single_message = FALSE;
uint8_t clean_session = TRUE;
uint8_t keep_running = TRUE;
static void usage()
{
fprintf(stderr, "Usage: mqtts-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 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, " -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", mqtts_port);
fprintf(stderr, " -t <topic> MQTT topic name to subscribe to.\n");
exit(-1);
}
static void parse_opts(int argc, char** argv)
{
int ch;
// Parse the options/switches
while ((ch = getopt(argc, argv, "1cdh:i:k:p:t:?")) != -1)
switch (ch) {
case '1':
single_message = TRUE;
break;
case 'c':
clean_session = FALSE;
break;
case 'd':
debug = TRUE;
break;
case 'h':
mqtts_host = optarg;
break;
case 'i':
client_id = optarg;
break;
case 'k':
keep_alive = atoi(optarg);
break;
case 'p':
mqtts_port = optarg;
break;
case 't':
topic_name = optarg;
break;
case '?':
default:
usage();
break;
}
// Missing Parameter?
if (!topic_name) {
usage();
}
}
static void termination_handler (int signum)
{
switch(signum) {
case SIGHUP: fprintf(stderr, "Got hangup signal."); break;
case SIGTERM: fprintf(stderr, "Got termination signal."); break;
case SIGINT: fprintf(stderr, "Got interupt signal."); break;
}
// Signal the main thead to stop
keep_running = FALSE;
}
int main(int argc, char* argv[])
{
int sock, timeout;
// Parse the command-line options
parse_opts(argc, argv);
// Enable debugging?
mqtts_set_debug(debug);
// Work out timeout value for main loop
if (keep_alive) {
timeout = keep_alive / 2;
} else {
timeout = 10;
}
// Setup signal handlers
signal(SIGTERM, termination_handler);
signal(SIGINT, termination_handler);
signal(SIGHUP, termination_handler);
// 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);
// Subscribe to the topic
mqtts_send_subscribe(sock, topic_name, 0);
topic_id = mqtts_recieve_suback(sock);
// Keep processing packets until process is terminated
while(keep_running) {
publish_packet_t *packet = mqtts_loop(sock, timeout);
if (packet) {
printf("%s\n", packet->data);
// Stop if in single message mode
if (single_message)
break;
}
}
// Finally, disconnect
mqtts_send_disconnect(sock);
close(sock);
}
return 0;
}