2020-12-05 19:26:32 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "net.h"
|
|
|
|
#include "str.h"
|
|
|
|
|
2020-12-16 04:29:47 +08:00
|
|
|
#define MQTT_CMD_CONNECT 1
|
|
|
|
#define MQTT_CMD_CONNACK 2
|
|
|
|
#define MQTT_CMD_PUBLISH 3
|
|
|
|
#define MQTT_CMD_PUBACK 4
|
|
|
|
#define MQTT_CMD_PUBREC 5
|
|
|
|
#define MQTT_CMD_PUBREL 6
|
|
|
|
#define MQTT_CMD_PUBCOMP 7
|
|
|
|
#define MQTT_CMD_SUBSCRIBE 8
|
|
|
|
#define MQTT_CMD_SUBACK 9
|
|
|
|
#define MQTT_CMD_UNSUBSCRIBE 10
|
|
|
|
#define MQTT_CMD_UNSUBACK 11
|
|
|
|
#define MQTT_CMD_PINGREQ 12
|
|
|
|
#define MQTT_CMD_PINGRESP 13
|
|
|
|
#define MQTT_CMD_DISCONNECT 14
|
|
|
|
|
2020-12-05 19:26:32 +08:00
|
|
|
struct mg_mqtt_opts {
|
2021-10-31 20:48:32 +08:00
|
|
|
struct mg_str user; // Username, can be empty
|
|
|
|
struct mg_str pass; // Password, can be empty
|
2021-09-15 14:43:48 +08:00
|
|
|
struct mg_str client_id; // Client ID
|
|
|
|
struct mg_str will_topic; // Will topic
|
|
|
|
struct mg_str will_message; // Will message
|
2021-12-23 19:00:18 +08:00
|
|
|
uint8_t will_qos; // Will message quality of service
|
2021-09-15 14:43:48 +08:00
|
|
|
bool will_retain; // Retain last will
|
|
|
|
bool clean; // Use clean session, 0 or 1
|
|
|
|
uint16_t keepalive; // Keep-alive timer in seconds
|
2020-12-05 19:26:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mg_mqtt_message {
|
2021-06-11 02:15:50 +08:00
|
|
|
struct mg_str topic; // Parsed topic
|
|
|
|
struct mg_str data; // Parsed message
|
|
|
|
struct mg_str dgram; // Whole MQTT datagram, including headers
|
2020-12-16 04:29:47 +08:00
|
|
|
uint16_t id; // Set for PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, PUBLISH
|
|
|
|
uint8_t cmd; // MQTT command, one of MQTT_CMD_*
|
|
|
|
uint8_t qos; // Quality of service
|
|
|
|
uint8_t ack; // Connack return code. 0 - success
|
2020-12-05 19:26:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mg_connection *mg_mqtt_connect(struct mg_mgr *, const char *url,
|
2022-04-12 17:04:55 +08:00
|
|
|
const struct mg_mqtt_opts *opts,
|
2020-12-05 19:26:32 +08:00
|
|
|
mg_event_handler_t fn, void *fn_data);
|
2020-12-16 04:29:47 +08:00
|
|
|
struct mg_connection *mg_mqtt_listen(struct mg_mgr *mgr, const char *url,
|
|
|
|
mg_event_handler_t fn, void *fn_data);
|
2022-04-12 17:04:55 +08:00
|
|
|
void mg_mqtt_login(struct mg_connection *c, const struct mg_mqtt_opts *opts);
|
2022-01-19 17:57:27 +08:00
|
|
|
void mg_mqtt_pub(struct mg_connection *c, struct mg_str topic,
|
|
|
|
struct mg_str data, int qos, bool retain);
|
|
|
|
void mg_mqtt_sub(struct mg_connection *, struct mg_str topic, int qos);
|
2020-12-16 18:14:00 +08:00
|
|
|
int mg_mqtt_parse(const uint8_t *buf, size_t len, struct mg_mqtt_message *m);
|
2020-12-16 04:29:47 +08:00
|
|
|
void mg_mqtt_send_header(struct mg_connection *, uint8_t cmd, uint8_t flags,
|
|
|
|
uint32_t len);
|
2021-05-29 01:30:42 +08:00
|
|
|
size_t mg_mqtt_next_sub(struct mg_mqtt_message *msg, struct mg_str *topic,
|
|
|
|
uint8_t *qos, size_t pos);
|
|
|
|
size_t mg_mqtt_next_unsub(struct mg_mqtt_message *msg, struct mg_str *topic,
|
|
|
|
size_t pos);
|
2021-03-07 20:21:59 +08:00
|
|
|
void mg_mqtt_ping(struct mg_connection *);
|
|
|
|
void mg_mqtt_pong(struct mg_connection *);
|
|
|
|
void mg_mqtt_disconnect(struct mg_connection *);
|