forked from harith-a/mqttsn_secure
/
mqtt-sn-pub.c
executable file
·339 lines (271 loc) · 9.95 KB
/
mqtt-sn-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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
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 <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"
#include <sys/time.h>
const char *client_id = NULL;
const char *topic_name = NULL;
const char *message_data = "nama";
time_t keep_alive = 30;
const char *mqtt_sn_host = "127.0.0.1";
const char *mqtt_sn_port = "1884";
uint16_t topic_id = 0;
uint8_t topic_id_type = MQTT_SN_TOPIC_TYPE_NORMAL;
int8_t qos = 1;
uint8_t retain = FALSE;
uint8_t debug = TRUE;
static void usage()
{
fprintf(stderr, "Usage: mqtt-sn-pub [opts] -t <topic> -m <message>\n");
fprintf(stderr, "\n");
fprintf(stderr, " -d Enable debug messages.\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, " -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", mqtt_sn_port);
fprintf(stderr, " -q <qos> Quality of Service value (0 or -1). Defaults to %d.\n", qos);
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-SN 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:q:rt:T:?")) != -1)
switch (ch) {
case 'd':
debug = TRUE;
break;
case 'h':
mqtt_sn_host = optarg;
break;
case 'i':
client_id = optarg;
break;
case 'm':
message_data = optarg;
break;
case 'n':
message_data = "";
break;
case 'p':
mqtt_sn_port = optarg;
break;
case 'q':
qos = atoi(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();
}
//if (qos != -1 && qos != 0) {
// fprintf(stderr, "Error: only QoS level 0 or -1 is supported.\n");
// exit(-1);
//}
// 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");
exit(-1);
}
// Check topic is valid for QoS level -1
if (qos == -1 && topic_id == 0 && strlen(topic_name) != 2) {
fprintf(stderr, "Error: either a pre-defined topic id or a short topic name must be given for QoS -1.\n");
exit(-1);
}
}
int main(int argc, char* argv[])
{
int sock;
// Parse the command-line options
parse_opts(argc, argv);
int i=1;
//Variables for time taking
struct timeval start, end;
// FILE *fp;
// Create a UDP socket
sock = mqtt_sn_create_socket(mqtt_sn_host, mqtt_sn_port);
if (sock) {
// Connect to gateway
if (qos >= 0) {
mqtt_sn_send_connect(sock, client_id, keep_alive);
mqtt_sn_receive_connack(sock);
}
if (topic_id) {
// Use pre-defined topic ID
topic_id_type = MQTT_SN_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 = MQTT_SN_TOPIC_TYPE_SHORT;
} else if (qos >= 0) {
// Register the topic name
mqtt_sn_send_register(sock, topic_name);
topic_id = mqtt_sn_receive_regack(sock);
topic_id_type = MQTT_SN_TOPIC_TYPE_NORMAL;
}
// gettimeofday(&start, NULL);
while(i<=1000)
{
gettimeofday(&start, NULL);
// Publish to the topic
if (qos == 0){
mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
}
// Manage Return Packets and Retrasnmits - harith
if (qos >= 1){
mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
if (qos == 1){
struct timeval tv;
fd_set rfd;
int ret;
int resend = 0;
while (resend < 5){
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
if (ret < 0) {
printf("Select() Error!\n" );
exit(EXIT_FAILURE);
}
else if (ret > 0) {
// Receive a packet
mqtt_sn_receive_puback(sock);
resend = 6;
}
else if (ret == 0)
{
printf("republishing...\n");
mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
resend++;
}
}
}
//QOS=2
else if(qos==2){
struct timeval tv;
fd_set rfd;
int ret;
int resendpublish = 0;
int resendpubrel = 0;
while (resendpublish < 5){
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
if (ret < 0) {
printf("Select() Error!\n" );
exit(EXIT_FAILURE);
}
else if (ret > 0) {
// Receive a packet
mqtt_sn_receive_pubrec(sock);
resendpublish = 6;
mqtt_sn_send_pubrel(sock);
}
else if (ret == 0)
{
printf("republishing qos21...\n");
mqtt_sn_send_publish(sock, topic_id, topic_id_type, message_data, qos, retain);
resendpublish++;
}
}
//retransmit pubrel
while (resendpubrel < 5){
FD_ZERO(&rfd);
FD_SET(sock, &rfd);
tv.tv_sec = 1;
tv.tv_usec = 0;
ret = select(FD_SETSIZE, &rfd, NULL, NULL, &tv);
if (ret < 0) {
printf("Select() Error!\n" );
exit(EXIT_FAILURE);
}
else if (ret > 0) {
// Receive a packet
mqtt_sn_receive_pubcomp(sock);
resendpubrel = 6;
}
else if (ret == 0)
{
printf("republishing qos22...\n");
mqtt_sn_send_pubrel(sock);
resendpubrel++;
}
}
}
}
gettimeofday(&end, NULL);
// int taken = (int)((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec))/1000000;
float taken = (float)((end.tv_sec * 1000000 + end.tv_usec) - (start.tv_sec * 1000000 + start.tv_usec))/1000;
printf("Time taken is:%3.2f ms\n\n",taken);
//open file for writing
// fp = fopen( "logfile.csv", "a" ); // Open file for writing
// fprintf(fp, "Seq %d , %3.2fms, ",i,taken);
// fprintf(fp,"Time %s\r\n",ctime((const time_t *) &end.tv_sec));
// fclose(fp);
// printf("Time taken is:%3.2d s\n\n", (int)((end.tv_sec * 1000000 + end.tv_usec)
// - (start.tv_sec * 1000000 + start.tv_usec))/1000000);
sleep(.5);
i++;
// if(taken>=2){
// printf("%d messages sent.\n", i);
// exit(EXIT_SUCCESS);
// }
}
if (qos >= 0) {
mqtt_sn_send_disconnect(sock);
mqtt_sn_receive_disconnect(sock);
}
close(sock);
}
mqtt_sn_cleanup();
return 0;
}