-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingyjp_connector_mqtt.c
50 lines (40 loc) · 1.35 KB
/
thingyjp_connector_mqtt.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
#define GETTEXT_PACKAGE "gtk20"
#include <glib.h>
#include <mosquittomainloop.h>
#include <mosquitto.h>
static gboolean connectedcallback(MosquittoClient* client, void* something,
gpointer userdata) {
g_message("connect callback");
int mid;
mosquitto_subscribe(mosquitto_client_getmosquittoinstance(client), &mid,
"#", 0);
return TRUE;
}
void errorcallback(struct mosquitto* mosq, void* data) {
g_message("error callback");
}
int main(int argc, char** argv) {
int ret = 0;
gchar* mqttid = NULL;
gchar* mqtthost = "mqtt.public.thingy.jp";
gint mqttport = 8883;
gchar* mqttrootcert = NULL;
gchar* mqttdevicecert = NULL;
gchar* mqttdevicekey = NULL;
GError* error = NULL;
GOptionEntry entries[] = { MQTTOPTS, { NULL } };
GOptionContext* optioncontext = g_option_context_new(NULL);
g_option_context_add_main_entries(optioncontext, entries, GETTEXT_PACKAGE);
if (!g_option_context_parse(optioncontext, &argc, &argv, &error)) {
g_print("option parsing failed: %s\n", error->message);
ret = 1;
goto err_args;
}
MosquittoClient* client = mosquitto_client_new_withclientcert(mqttid,
mqtthost, 8883, mqttrootcert, mqttdevicecert, mqttdevicekey);
g_signal_connect(client, MOSQUITTO_CLIENT_SIGNAL_CONNECTED,
(GCallback )connectedcallback, NULL);
GMainLoop* mainloop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(mainloop);
err_args: return ret;
}