#include "mqtt.h"

=== MQTT API reference

struct mg_mqtt_message {
  int cmd;
  int qos;
  int len; /* message length in the IO buffer */
  struct mg_str topic;
  struct mg_str payload;

  uint8_t connack_ret_code; /* connack */
  uint16_t message_id;      /* puback */

  /* connect */
  uint8_t protocol_version;
  uint8_t connect_flags;
  uint16_t keep_alive_timer;
  struct mg_str protocol_name;
  struct mg_str client_id;
  struct mg_str will_topic;
  struct mg_str will_message;
  struct mg_str user_name;
  struct mg_str password;
};

struct mg_mqtt_topic_expression {
  const char *topic;
  uint8_t qos;
};

struct mg_send_mqtt_handshake_opts {
  unsigned char flags; /* connection flags */
  uint16_t keep_alive;
  const char *will_topic;
  const char *will_message;
  const char *user_name;
  const char *password;
};

struct mg_mqtt_proto_data {
  uint16_t keep_alive;
  double last_control_time;
};

mg_mqtt_proto_data should be in header to allow external access to it

void mg_set_protocol_mqtt(struct mg_connection *nc);

Attaches a built-in MQTT event handler to the given connection.

The user-defined event handler will receive following extra events:

  • MG_EV_MQTT_CONNACK
  • MG_EV_MQTT_PUBLISH
  • MG_EV_MQTT_PUBACK
  • MG_EV_MQTT_PUBREC
  • MG_EV_MQTT_PUBREL
  • MG_EV_MQTT_PUBCOMP
  • MG_EV_MQTT_SUBACK

void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id);

Sends an MQTT handshake.

void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id,
                                struct mg_send_mqtt_handshake_opts);

Sends an MQTT handshake with optional parameters.

void mg_mqtt_publish(struct mg_connection *nc, const char *topic,
                     uint16_t message_id, int flags, const void *data,
                     size_t len);

Publishes a message to a given topic.

void mg_mqtt_subscribe(struct mg_connection *nc,
                       const struct mg_mqtt_topic_expression *topics,
                       size_t topics_len, uint16_t message_id);

Subscribes to a bunch of topics.

void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics,
                         size_t topics_len, uint16_t message_id);

Unsubscribes from a bunch of topics.

void mg_mqtt_disconnect(struct mg_connection *nc);

Sends a DISCONNECT command.

void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code);

Sends a CONNACK command with a given return_code.

void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id);

Sends a PUBACK command with a given message_id.

void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id);

Sends a PUBREC command with a given message_id.

void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id);

Sends a PUBREL command with a given message_id.

void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id);

Sends a PUBCOMP command with a given message_id.

void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len,
                    uint16_t message_id);

Sends a SUBACK command with a given message_id and a sequence of granted QoSs.

void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id);

Sends a UNSUBACK command with a given message_id.

void mg_mqtt_ping(struct mg_connection *nc);

Sends a PINGREQ command.

void mg_mqtt_pong(struct mg_connection *nc);

Sends a PINGRESP command.

int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg,
                                 struct mg_str *topic, uint8_t *qos, int pos);

Extracts the next topic expression from a SUBSCRIBE command payload.

The topic expression name will point to a string in the payload buffer. Returns the pos of the next topic expression or -1 when the list of topics is exhausted.

int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic);

Matches a topic against a topic expression

Returns 1 if it matches; 0 otherwise.

int mg_mqtt_vmatch_topic_expression(const char *exp, struct mg_str topic);

Same as mg_mqtt_match_topic_expression(), but takes exp as a NULL-terminated string.