nodemcu-firmware/app/mqtt/mqtt_msg.h

154 lines
5.6 KiB
C
Raw Permalink Normal View History

2019-02-17 19:26:29 +01:00
/*
* File: mqtt_msg.h
* Author: Minh Tuan
*
* Created on July 12, 2014, 1:05 PM
*/
#ifndef MQTT_MSG_H
#define MQTT_MSG_H
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
* Copyright (c) 2014, Stephen Robinson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
/* 7 6 5 4 3 2 1 0*/
/*| --- Message Type---- | DUP Flag | QoS Level | Retain |
/* Remaining Length */
enum mqtt_message_type
{
MQTT_MSG_TYPE_CONNECT = 1,
MQTT_MSG_TYPE_CONNACK = 2,
MQTT_MSG_TYPE_PUBLISH = 3,
MQTT_MSG_TYPE_PUBACK = 4,
MQTT_MSG_TYPE_PUBREC = 5,
MQTT_MSG_TYPE_PUBREL = 6,
MQTT_MSG_TYPE_PUBCOMP = 7,
MQTT_MSG_TYPE_SUBSCRIBE = 8,
MQTT_MSG_TYPE_SUBACK = 9,
MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
MQTT_MSG_TYPE_UNSUBACK = 11,
MQTT_MSG_TYPE_PINGREQ = 12,
MQTT_MSG_TYPE_PINGRESP = 13,
MQTT_MSG_TYPE_DISCONNECT = 14
};
enum mqtt_connack_return_code
{
MQTT_CONN_FAIL_SERVER_NOT_FOUND = -5,
MQTT_CONN_FAIL_NOT_A_CONNACK_MSG = -4,
MQTT_CONN_FAIL_DNS = -3,
MQTT_CONN_FAIL_TIMEOUT_RECEIVING = -2,
MQTT_CONN_FAIL_TIMEOUT_SENDING = -1,
MQTT_CONNACK_ACCEPTED = 0,
MQTT_CONNACK_REFUSED_PROTOCOL_VER = 1,
MQTT_CONNACK_REFUSED_ID_REJECTED = 2,
MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE = 3,
MQTT_CONNACK_REFUSED_BAD_USER_OR_PASS = 4,
MQTT_CONNACK_REFUSED_NOT_AUTHORIZED = 5
};
typedef struct mqtt_message
{
uint8_t* data;
uint16_t length;
} mqtt_message_t;
typedef struct mqtt_message_buffer
{
mqtt_message_t message;
uint16_t message_id;
uint8_t* buffer;
uint16_t buffer_length;
} mqtt_message_buffer_t;
typedef struct mqtt_connect_info
{
First round of MQTT fixes (#3360) * mqtt: remove concept of connection timeout Just rely on the network stack to tell us when things have gone south. * mqtt: remove write-only mqtt_state.port field * mqtt: drop useless conditional * mqtt: decouple message sent flag from timer * mqtt: reconnect callback does not need to hang up The network stack has certainly done that for us at this point. Similarly, since we're about to call mqtt_socket_disconnected, don't bother unregistering the timer here, either. * mqtt: don't tick once per second Set the timer for the duration of the wait and cancel it on the other side. * mqtt: defer message queue destruction to _disconnect We're going to want to publish a disconnect message for real, so doing this in _close does no one any favors * mqtt: miscellaneous cleanups No functional change intended * mqtt: close() should send disconnect message for real This means waiting for _sent() to fire again before telling the network stack to disconnect. * mqtt: tidy connect and dns - Push the self-ref to after all allocations and error returns - Don't try to extract IPv4 from the domain string ourselves, let the resolver, since it can - Don't try to connect to localhost. That can't possibly work. * mqtt: common up some callback invocations * mqtt: don't retransmit messages on timeout There's no point in retransmitting messages on timeout; the network stack will be trying to do it for us anyway. * mqtt: remove unnecessary NULL udata checks * mqtt: hold strings in Lua, not C Eliminates a host of C-side allocations. While here, move the rest of the mqtt_connect_info structure out to its own thing, and pack some flags using a bitfield. * mqtt: mqtt_socket_on use lua_checkoption * mqtt: slightly augment debug messages These changes have made some debugging ever so slightly easier.
2021-01-05 12:07:09 +01:00
const char* client_id;
const char* username;
const char* password;
const char* will_topic;
const char* will_message;
int keepalive;
int will_qos;
int will_retain;
int clean_session;
} mqtt_connect_info_t;
static inline uint8_t mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
static inline uint8_t mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
static inline uint8_t mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
static inline uint8_t mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
static inline uint8_t mqtt_get_connect_ret_code(uint8_t* buffer) { return (buffer[3]); }
void mqtt_msg_init(mqtt_message_buffer_t* msgb, uint8_t* buffer, uint16_t buffer_length);
int32_t mqtt_get_total_length(uint8_t* buffer, uint16_t buffer_length);
const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* buffer_length);
const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* buffer_length);
uint16_t mqtt_get_id(uint8_t* buffer, uint16_t buffer_length);
mqtt_message_t* mqtt_msg_connect(mqtt_message_buffer_t* msgb, mqtt_connect_info_t* info);
mqtt_message_t* mqtt_msg_publish(mqtt_message_buffer_t* msgb, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t message_id);
mqtt_message_t* mqtt_msg_puback(mqtt_message_buffer_t* msgb, uint16_t message_id);
mqtt_message_t* mqtt_msg_pubrec(mqtt_message_buffer_t* msgb, uint16_t message_id);
mqtt_message_t* mqtt_msg_pubrel(mqtt_message_buffer_t* msgb, uint16_t message_id);
mqtt_message_t* mqtt_msg_pubcomp(mqtt_message_buffer_t* msgb, uint16_t message_id);
mqtt_message_t* mqtt_msg_subscribe(mqtt_message_buffer_t* msgb, const char* topic, int qos, uint16_t message_id);
mqtt_message_t* mqtt_msg_unsubscribe(mqtt_message_buffer_t* msgb, const char* topic, uint16_t message_id);
mqtt_message_t* mqtt_msg_pingreq(mqtt_message_buffer_t* msgb);
mqtt_message_t* mqtt_msg_pingresp(mqtt_message_buffer_t* msgb);
mqtt_message_t* mqtt_msg_disconnect(mqtt_message_buffer_t* msgb);
mqtt_message_t* mqtt_msg_subscribe_init(mqtt_message_buffer_t* msgb, uint16_t message_id);
mqtt_message_t* mqtt_msg_subscribe_topic(mqtt_message_buffer_t* msgb, const char* topic, int qos);
mqtt_message_t* mqtt_msg_subscribe_fini(mqtt_message_buffer_t* msgb);
mqtt_message_t* mqtt_msg_unsubscribe_init(mqtt_message_buffer_t* msgb, uint16_t message_id);
mqtt_message_t* mqtt_msg_unsubscribe_topic(mqtt_message_buffer_t* msgb, const char* topic);
mqtt_message_t* mqtt_msg_unsubscribe_fini(mqtt_message_buffer_t* msgb);
2016-03-20 00:24:18 +01:00
#ifdef __cplusplus
}
#endif
#endif /* MQTT_MSG_H */