2020-12-12 18:03:19 +08:00
|
|
|
// Copyright (c) 2020 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
//
|
|
|
|
// Example MQTT client. It performs the following steps:
|
|
|
|
// 1. Connects to the MQTT server specified by `s_url` variable
|
2021-11-10 20:57:02 +08:00
|
|
|
// 2. When connected, subscribes to the topic `s_sub_topic`
|
|
|
|
// 3. Publishes message `hello` to the `s_pub_topic`
|
|
|
|
// 4. Receives that message back from the subscribed topic and closes
|
|
|
|
// 5. Timer-based reconnection logic revives the connection when it is down
|
2020-12-12 18:03:19 +08:00
|
|
|
//
|
|
|
|
// To enable SSL/TLS for this client, build it like this:
|
|
|
|
// make MBEDTLS_DIR=/path/to/your/mbedtls/installation
|
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
|
|
|
static const char *s_url = "mqtt://broker.hivemq.com:1883";
|
2021-11-10 20:57:02 +08:00
|
|
|
static const char *s_sub_topic = "mg/+/test";
|
|
|
|
static const char *s_pub_topic = "mg/clnt/test";
|
2021-11-01 21:29:44 +08:00
|
|
|
static int s_qos = 1;
|
2021-11-10 20:57:02 +08:00
|
|
|
static struct mg_connection *s_conn;
|
2020-12-12 18:03:19 +08:00
|
|
|
|
2021-11-11 15:36:25 +08:00
|
|
|
// Handle interrupts, like Ctrl-C
|
|
|
|
static int s_signo;
|
|
|
|
static void signal_handler(int signo) {
|
|
|
|
s_signo = signo;
|
|
|
|
}
|
|
|
|
|
2020-12-12 18:03:19 +08:00
|
|
|
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
2021-11-02 00:20:00 +08:00
|
|
|
if (ev == MG_EV_OPEN) {
|
2021-11-10 20:57:02 +08:00
|
|
|
LOG(LL_INFO, ("CREATED"));
|
2021-11-02 00:20:00 +08:00
|
|
|
// c->is_hexdumping = 1;
|
|
|
|
} else if (ev == MG_EV_ERROR) {
|
2020-12-12 18:03:19 +08:00
|
|
|
// On error, log error message
|
2020-12-14 22:05:01 +08:00
|
|
|
LOG(LL_ERROR, ("%p %s", c->fd, (char *) ev_data));
|
2020-12-12 18:03:19 +08:00
|
|
|
} else if (ev == MG_EV_CONNECT) {
|
|
|
|
// If target URL is SSL/TLS, command client connection to use TLS
|
|
|
|
if (mg_url_is_ssl(s_url)) {
|
|
|
|
struct mg_tls_opts opts = {.ca = "ca.pem"};
|
|
|
|
mg_tls_init(c, &opts);
|
|
|
|
}
|
|
|
|
} else if (ev == MG_EV_MQTT_OPEN) {
|
|
|
|
// MQTT connect is successful
|
2021-11-10 20:57:02 +08:00
|
|
|
struct mg_str subt = mg_str(s_sub_topic);
|
|
|
|
struct mg_str pubt = mg_str(s_pub_topic), data = mg_str("hello");
|
2020-12-12 18:03:19 +08:00
|
|
|
LOG(LL_INFO, ("CONNECTED to %s", s_url));
|
2021-11-10 20:57:02 +08:00
|
|
|
mg_mqtt_sub(c, &subt, s_qos);
|
|
|
|
LOG(LL_INFO, ("SUBSCRIBED to %.*s", (int) subt.len, subt.ptr));
|
|
|
|
|
|
|
|
mg_mqtt_pub(c, &pubt, &data, s_qos, false);
|
2020-12-12 18:03:19 +08:00
|
|
|
LOG(LL_INFO, ("PUBSLISHED %.*s -> %.*s", (int) data.len, data.ptr,
|
2021-11-10 20:57:02 +08:00
|
|
|
(int) pubt.len, pubt.ptr));
|
2020-12-12 18:03:19 +08:00
|
|
|
} else if (ev == MG_EV_MQTT_MSG) {
|
|
|
|
// When we get echo response, print it
|
|
|
|
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
|
|
|
LOG(LL_INFO, ("RECEIVED %.*s <- %.*s", (int) mm->data.len, mm->data.ptr,
|
|
|
|
(int) mm->topic.len, mm->topic.ptr));
|
2021-11-10 20:57:02 +08:00
|
|
|
c->is_closing = 1;
|
|
|
|
} else if (ev == MG_EV_CLOSE) {
|
|
|
|
LOG(LL_INFO, ("CLOSED"));
|
|
|
|
s_conn = NULL; // Mark that we're closed
|
2020-12-12 18:03:19 +08:00
|
|
|
}
|
2021-11-10 20:57:02 +08:00
|
|
|
(void) fn_data;
|
|
|
|
}
|
2020-12-12 18:03:19 +08:00
|
|
|
|
2021-11-10 20:57:02 +08:00
|
|
|
// Timer function - recreate client connection if it is closed
|
|
|
|
static void timer_fn(void *arg) {
|
|
|
|
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
2021-12-23 19:00:18 +08:00
|
|
|
struct mg_mqtt_opts opts = {.clean = true,
|
|
|
|
.will_qos = s_qos,
|
2021-11-10 20:57:02 +08:00
|
|
|
.will_topic = mg_str(s_pub_topic),
|
|
|
|
.will_message = mg_str("goodbye")};
|
|
|
|
if (s_conn == NULL) s_conn = mg_mqtt_connect(mgr, s_url, &opts, fn, NULL);
|
2020-12-12 18:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
2021-11-01 21:29:44 +08:00
|
|
|
struct mg_mgr mgr;
|
2021-11-10 20:57:02 +08:00
|
|
|
struct mg_timer timer;
|
|
|
|
int topts = MG_TIMER_REPEAT | MG_TIMER_RUN_NOW;
|
2021-11-01 21:29:44 +08:00
|
|
|
|
2021-11-11 15:36:25 +08:00
|
|
|
signal(SIGINT, signal_handler); // Setup signal handlers - exist event
|
|
|
|
signal(SIGTERM, signal_handler); // manager loop on SIGINT and SIGTERM
|
|
|
|
|
2021-11-10 20:57:02 +08:00
|
|
|
mg_mgr_init(&mgr); // Init event manager
|
|
|
|
mg_timer_init(&timer, 3000, topts, timer_fn, &mgr); // Init timer
|
2021-11-11 15:36:25 +08:00
|
|
|
while (s_signo == 0) mg_mgr_poll(&mgr, 1000); // Event loop, 1s timeout
|
2021-11-10 20:57:02 +08:00
|
|
|
mg_mgr_free(&mgr); // Finished, cleanup
|
|
|
|
mg_timer_free(&timer); // Free timer resources
|
2021-11-01 21:29:44 +08:00
|
|
|
|
2020-12-12 18:03:19 +08:00
|
|
|
return 0;
|
|
|
|
}
|