forked from TLeconte/acarsdec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttout.c
63 lines (48 loc) · 1.55 KB
/
mqttout.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MQTTAsync.h"
static MQTTAsync client;
static MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
static char *msgtopic;
int MQTTinit(char **urls, char * client_id, char *topic, char *user,char *passwd)
{
int rc;
MQTTAsync_createOptions create_opts = MQTTAsync_createOptions_initializer;
MQTTAsync_connectOptions conn_opts = MQTTAsync_connectOptions_initializer;
create_opts.maxBufferedMessages=200;
create_opts.sendWhileDisconnected=1;
create_opts.allowDisconnectedSendAtAnyTime=1;
create_opts.deleteOldestMessages=1;
MQTTAsync_createWithOptions(&client, urls[0], client_id, MQTTCLIENT_PERSISTENCE_NONE, NULL, &create_opts);
conn_opts.keepAliveInterval = 60;
conn_opts.cleansession = 1;
conn_opts.automaticReconnect = 1;
conn_opts.password = passwd;
conn_opts.username = user;
if(urls[1]) conn_opts.serverURIs = urls;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS) {
return(rc);
}
if(topic == NULL) {
msgtopic=malloc(strlen(client_id)+strlen("acarsdec")+2);
if(msgtopic==NULL)
return -1;
sprintf(msgtopic,"acarsdec/%s",client_id);
} else
msgtopic=topic;
return rc;
}
int MQTTsend(char *msgtxt)
{
pubmsg.payload = msgtxt;
pubmsg.payloadlen = strlen(msgtxt);
pubmsg.qos = 0;
pubmsg.retained = 0;
return MQTTAsync_sendMessage(client, msgtopic, &pubmsg,NULL);
}
void MQTTend()
{
MQTTAsync_disconnect(client,NULL);
MQTTAsync_destroy(&client);
}