2015-01-23 04:48:05 +01:00
|
|
|
// Module for mqtt
|
2017-04-04 22:22:04 +02:00
|
|
|
//
|
2015-12-16 06:04:58 +01:00
|
|
|
#include "module.h"
|
2015-01-23 04:48:05 +01:00
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "platform.h"
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
#include <string.h>
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stddef.h>
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2019-07-23 06:22:38 +02:00
|
|
|
#include <stdint.h>
|
2015-01-23 04:48:05 +01:00
|
|
|
#include "mem.h"
|
2015-10-01 07:17:16 +02:00
|
|
|
#include "lwip/ip_addr.h"
|
2015-01-23 04:48:05 +01:00
|
|
|
#include "espconn.h"
|
|
|
|
|
2019-07-23 17:47:18 +02:00
|
|
|
#include "mqtt/mqtt_msg.h"
|
|
|
|
#include "mqtt/msg_queue.h"
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2016-01-20 09:37:03 +01:00
|
|
|
#include "user_interface.h"
|
|
|
|
|
2018-11-13 23:43:24 +01:00
|
|
|
#define MQTT_BUF_SIZE 1460
|
2015-01-23 04:48:05 +01:00
|
|
|
#define MQTT_DEFAULT_KEEPALIVE 60
|
|
|
|
#define MQTT_MAX_CLIENT_LEN 64
|
|
|
|
#define MQTT_MAX_USER_LEN 64
|
|
|
|
#define MQTT_MAX_PASS_LEN 64
|
2021-01-05 12:07:09 +01:00
|
|
|
#define MQTT_SEND_TIMEOUT 5 /* seconds */
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
MQTT_INIT,
|
|
|
|
MQTT_CONNECT_SENDING,
|
2021-01-05 12:07:09 +01:00
|
|
|
MQTT_CONNECT_SENT,
|
2015-01-23 04:48:05 +01:00
|
|
|
MQTT_DATA
|
|
|
|
} tConnState;
|
|
|
|
|
|
|
|
typedef struct mqtt_event_data_t
|
|
|
|
{
|
|
|
|
uint8_t type;
|
|
|
|
const char* topic;
|
|
|
|
const char* data;
|
|
|
|
uint16_t topic_length;
|
|
|
|
uint16_t data_length;
|
|
|
|
uint16_t data_offset;
|
|
|
|
} mqtt_event_data_t;
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
typedef enum {
|
|
|
|
MQTT_RECV_NORMAL,
|
|
|
|
MQTT_RECV_BUFFERING_SHORT,
|
|
|
|
MQTT_RECV_BUFFERING,
|
|
|
|
MQTT_RECV_SKIPPING,
|
|
|
|
} tReceiveState;
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
typedef struct mqtt_state_t
|
|
|
|
{
|
2015-03-29 18:24:09 +02:00
|
|
|
msg_queue_t* pending_msg_q;
|
2020-03-14 23:51:03 +01:00
|
|
|
uint16_t next_message_id;
|
2018-11-30 22:12:46 +01:00
|
|
|
|
|
|
|
uint8_t * recv_buffer; // heap buffer for multi-packet rx
|
|
|
|
uint8_t * recv_buffer_wp; // write pointer in multi-packet rx
|
|
|
|
union {
|
|
|
|
uint16_t recv_buffer_size; // size of recv_buffer
|
|
|
|
uint32_t recv_buffer_skip; // number of bytes left to skip, in skipping state
|
|
|
|
};
|
|
|
|
tReceiveState recv_buffer_state;
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
} mqtt_state_t;
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
typedef struct lmqtt_userdata
|
|
|
|
{
|
2020-04-07 14:06:27 +02:00
|
|
|
struct espconn pesp_conn;
|
2015-01-23 04:48:05 +01:00
|
|
|
int self_ref;
|
|
|
|
int cb_connect_ref;
|
2016-03-06 22:33:57 +01:00
|
|
|
int cb_connect_fail_ref;
|
2015-01-23 04:48:05 +01:00
|
|
|
int cb_disconnect_ref;
|
|
|
|
int cb_message_ref;
|
2018-11-30 22:12:46 +01:00
|
|
|
int cb_overflow_ref;
|
2015-01-23 04:48:05 +01:00
|
|
|
int cb_suback_ref;
|
2016-03-20 00:24:18 +01:00
|
|
|
int cb_unsuback_ref;
|
2015-01-23 04:48:05 +01:00
|
|
|
int cb_puback_ref;
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
/* Configuration options */
|
|
|
|
struct {
|
|
|
|
int client_id_ref;
|
|
|
|
int username_ref;
|
|
|
|
int password_ref;
|
|
|
|
int will_topic_ref;
|
|
|
|
int will_message_ref;
|
|
|
|
int keepalive;
|
|
|
|
uint16_t max_message_length;
|
|
|
|
struct {
|
|
|
|
unsigned will_qos : 2;
|
|
|
|
bool will_retain : 1;
|
|
|
|
bool clean_session : 1;
|
|
|
|
bool secure : 1;
|
|
|
|
} flags;
|
|
|
|
} conf;
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
mqtt_state_t mqtt_state;
|
2015-03-29 18:24:09 +02:00
|
|
|
bool connected; // indicate socket connected, not mqtt prot connected.
|
2016-11-08 21:00:17 +01:00
|
|
|
bool keepalive_sent;
|
2021-01-05 12:07:09 +01:00
|
|
|
bool sending; // data sent to network stack, awaiting local acknowledge
|
2015-01-23 04:48:05 +01:00
|
|
|
ETSTimer mqttTimer;
|
|
|
|
tConnState connState;
|
|
|
|
}lmqtt_userdata;
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
// How large MQTT messages to accept by default
|
|
|
|
#define DEFAULT_MAX_MESSAGE_LENGTH 1024
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
static sint8 mqtt_socket_do_connect(struct lmqtt_userdata *);
|
2015-04-02 18:51:02 +02:00
|
|
|
static void mqtt_socket_reconnected(void *arg, sint8_t err);
|
|
|
|
static void mqtt_socket_connected(void *arg);
|
2016-03-07 02:11:16 +01:00
|
|
|
static void mqtt_connack_fail(lmqtt_userdata * mud, int reason_code);
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2020-03-14 23:51:03 +01:00
|
|
|
static uint16_t mqtt_next_message_id(lmqtt_userdata * mud)
|
|
|
|
{
|
|
|
|
mud->mqtt_state.next_message_id++;
|
|
|
|
if (mud->mqtt_state.next_message_id == 0)
|
|
|
|
mud->mqtt_state.next_message_id++;
|
|
|
|
|
|
|
|
return mud->mqtt_state.next_message_id;
|
|
|
|
}
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
static void mqtt_socket_cb_lua_noarg(lua_State *L, lmqtt_userdata *mud, int cb)
|
|
|
|
{
|
|
|
|
if (cb == LUA_NOREF)
|
|
|
|
return;
|
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, cb);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
static void mqtt_socket_disconnected(void *arg) // tcp only
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_disconnected.\n");
|
2020-04-07 14:06:27 +02:00
|
|
|
lmqtt_userdata *mud = arg;
|
2015-01-23 04:48:05 +01:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
while (mud->mqtt_state.pending_msg_q) {
|
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
if(mud->mqtt_state.recv_buffer) {
|
2019-07-21 23:58:21 +02:00
|
|
|
free(mud->mqtt_state.recv_buffer);
|
2018-11-30 22:12:46 +01:00
|
|
|
mud->mqtt_state.recv_buffer = NULL;
|
|
|
|
}
|
|
|
|
mud->mqtt_state.recv_buffer_size = 0;
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_NORMAL;
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
if(mud->pesp_conn.proto.tcp)
|
|
|
|
free(mud->pesp_conn.proto.tcp);
|
|
|
|
mud->pesp_conn.proto.tcp = NULL;
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
int selfref = mud->self_ref;
|
|
|
|
mud->self_ref = LUA_NOREF;
|
|
|
|
|
|
|
|
lua_State *L = lua_getstate();
|
2019-07-16 11:30:41 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->connected){ // call back only called when socket is from connection to disconnection.
|
|
|
|
mud->connected = false;
|
|
|
|
if((mud->cb_disconnect_ref != LUA_NOREF) && (selfref != LUA_NOREF)) {
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, selfref);
|
|
|
|
lua_call(L, 1, 0);
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
// unref this, and the mqtt.socket userdata will delete it self
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, selfref);
|
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_disconnected.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
static void mqtt_socket_do_disconnect(struct lmqtt_userdata *mud)
|
|
|
|
{
|
|
|
|
#ifdef CLIENT_SSL_ENABLE
|
2021-01-05 12:07:09 +01:00
|
|
|
if (mud->conf.flags.secure) {
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_secure_disconnect(&mud->pesp_conn);
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
espconn_disconnect(&mud->pesp_conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
static void mqtt_socket_reconnected(void *arg, sint8_t err)
|
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("enter mqtt_socket_reconnected (err=%d)\n", err);
|
2020-04-07 14:06:27 +02:00
|
|
|
lmqtt_userdata *mud = arg;
|
2015-03-30 18:36:44 +02:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
2019-07-16 11:30:41 +02:00
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_SERVER_NOT_FOUND);
|
2017-04-04 22:22:04 +02:00
|
|
|
|
2019-07-16 11:30:41 +02:00
|
|
|
mqtt_socket_disconnected(arg);
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_reconnected.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
static void deliver_publish(lmqtt_userdata * mud, uint8_t* message, uint16_t length, uint8_t is_overflow)
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2018-11-30 22:12:46 +01:00
|
|
|
NODE_DBG("enter deliver_publish (len=%d, overflow=%d).\n", length, is_overflow);
|
2015-04-02 18:51:02 +02:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
2015-01-23 04:48:05 +01:00
|
|
|
mqtt_event_data_t event_data;
|
|
|
|
|
|
|
|
event_data.topic_length = length;
|
|
|
|
event_data.topic = mqtt_get_publish_topic(message, &event_data.topic_length);
|
|
|
|
|
|
|
|
event_data.data_length = length;
|
|
|
|
event_data.data = mqtt_get_publish_data(message, &event_data.data_length);
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
int cb_ref = !is_overflow ? mud->cb_message_ref : mud->cb_overflow_ref;
|
|
|
|
|
|
|
|
if(cb_ref == LUA_NOREF)
|
2015-01-23 04:48:05 +01:00
|
|
|
return;
|
|
|
|
if(mud->self_ref == LUA_NOREF)
|
|
|
|
return;
|
2016-03-07 04:54:28 +01:00
|
|
|
lua_State *L = lua_getstate();
|
2015-04-04 20:22:51 +02:00
|
|
|
if(event_data.topic && (event_data.topic_length > 0)){
|
2018-11-30 22:12:46 +01:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, cb_ref);
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref);
|
2016-03-07 04:54:28 +01:00
|
|
|
lua_pushlstring(L, event_data.topic, event_data.topic_length);
|
2015-04-04 20:22:51 +02:00
|
|
|
} else {
|
|
|
|
NODE_DBG("get wrong packet.\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(event_data.data && (event_data.data_length > 0)){
|
2016-03-07 04:54:28 +01:00
|
|
|
lua_pushlstring(L, event_data.data, event_data.data_length);
|
|
|
|
lua_call(L, 3, 0);
|
2015-01-23 04:48:05 +01:00
|
|
|
} else {
|
2016-03-07 04:54:28 +01:00
|
|
|
lua_call(L, 2, 0);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave deliver_publish.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2016-03-18 01:22:12 +01:00
|
|
|
|
2016-03-07 02:11:16 +01:00
|
|
|
static void mqtt_connack_fail(lmqtt_userdata * mud, int reason_code)
|
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("enter mqtt_connack_fail\n");
|
|
|
|
|
2016-03-18 01:22:12 +01:00
|
|
|
if(mud->cb_connect_fail_ref == LUA_NOREF || mud->self_ref == LUA_NOREF)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2016-11-08 21:00:17 +01:00
|
|
|
|
|
|
|
lua_State *L = lua_getstate();
|
|
|
|
|
2016-03-18 01:22:12 +01:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->self_ref);
|
2016-03-18 01:22:12 +01:00
|
|
|
lua_pushinteger(L, reason_code);
|
|
|
|
lua_call(L, 2, 0);
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
NODE_DBG("leave mqtt_connack_fail\n");
|
2016-03-18 01:22:12 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
static sint8 mqtt_send_if_possible(struct lmqtt_userdata *mud)
|
2016-03-07 04:54:28 +01:00
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
/* Waiting for the local network stack to get back to us? Can't send. */
|
|
|
|
if (mud->sending)
|
|
|
|
return ESPCONN_OK;
|
|
|
|
|
2016-03-07 04:54:28 +01:00
|
|
|
sint8 espconn_status = ESPCONN_OK;
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
msg_queue_t *pending_msg = msg_peek(&(mud->mqtt_state.pending_msg_q));
|
|
|
|
if (pending_msg && !pending_msg->sent) {
|
|
|
|
pending_msg->sent = 1;
|
|
|
|
NODE_DBG("Sent: %d\n", pending_msg->msg.length);
|
2016-03-07 04:54:28 +01:00
|
|
|
#ifdef CLIENT_SSL_ENABLE
|
2021-01-05 12:07:09 +01:00
|
|
|
if( mud->conf.flags.secure )
|
|
|
|
{
|
|
|
|
espconn_status = espconn_secure_send(&mud->pesp_conn, pending_msg->msg.data, pending_msg->msg.length );
|
|
|
|
}
|
|
|
|
else
|
2016-03-07 04:54:28 +01:00
|
|
|
#endif
|
2021-01-05 12:07:09 +01:00
|
|
|
{
|
|
|
|
espconn_status = espconn_send(&mud->pesp_conn, pending_msg->msg.data, pending_msg->msg.length );
|
2016-03-07 04:54:28 +01:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->sending = true;
|
|
|
|
|
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
|
|
|
os_timer_arm(&mud->mqttTimer, MQTT_SEND_TIMEOUT * 1000, 0);
|
2016-03-07 04:54:28 +01:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
|
2016-03-16 13:10:18 +01:00
|
|
|
NODE_DBG("send_if_poss, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2016-03-07 04:54:28 +01:00
|
|
|
return espconn_status;
|
2016-03-07 02:11:16 +01:00
|
|
|
}
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
static void mqtt_socket_received(void *arg, char *pdata, unsigned short len)
|
|
|
|
{
|
2018-11-30 22:12:46 +01:00
|
|
|
NODE_DBG("enter mqtt_socket_received (rxlen=%u).\n", len);
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
uint8_t msg_type;
|
|
|
|
uint8_t msg_qos;
|
|
|
|
uint16_t msg_id;
|
2015-04-04 20:22:51 +02:00
|
|
|
uint8_t *in_buffer = (uint8_t *)pdata;
|
2018-11-30 22:12:46 +01:00
|
|
|
uint16_t in_buffer_length = len;
|
|
|
|
uint8_t *continuation_buffer = NULL;
|
|
|
|
uint8_t *temp_pdata = NULL;
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
lmqtt_userdata *mud = arg;
|
2015-01-23 04:48:05 +01:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
switch(mud->mqtt_state.recv_buffer_state) {
|
|
|
|
case MQTT_RECV_NORMAL:
|
|
|
|
// No previous buffer.
|
|
|
|
break;
|
|
|
|
case MQTT_RECV_BUFFERING_SHORT:
|
|
|
|
// Last buffer had so few byte that we could not determine message length.
|
|
|
|
// Store in a local heap buffer and operate on this, as if was the regular pdata buffer.
|
|
|
|
// Avoids having to repeat message size/overflow logic.
|
2019-07-21 23:58:21 +02:00
|
|
|
temp_pdata = calloc(1,mud->mqtt_state.recv_buffer_size + len);
|
2018-11-30 22:12:46 +01:00
|
|
|
if(temp_pdata == NULL) {
|
|
|
|
NODE_DBG("MQTT[buffering-short]: Failed to allocate %u bytes, disconnecting...\n", mud->mqtt_state.recv_buffer_size + len);
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_socket_do_disconnect(mud);
|
2018-11-30 22:12:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DBG("MQTT[buffering-short]: Continuing with %u + %u bytes\n", mud->mqtt_state.recv_buffer_size, len);
|
|
|
|
memcpy(temp_pdata, mud->mqtt_state.recv_buffer, mud->mqtt_state.recv_buffer_size);
|
|
|
|
memcpy(temp_pdata + mud->mqtt_state.recv_buffer_size, pdata, len);
|
2019-07-21 23:58:21 +02:00
|
|
|
free(mud->mqtt_state.recv_buffer);
|
2018-11-30 22:12:46 +01:00
|
|
|
mud->mqtt_state.recv_buffer = NULL;
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_NORMAL;
|
|
|
|
|
|
|
|
in_buffer = temp_pdata;
|
|
|
|
in_buffer_length = mud->mqtt_state.recv_buffer_size + len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MQTT_RECV_BUFFERING: {
|
|
|
|
// safe cast: we never allow longer buffer.
|
|
|
|
uint16_t current_length = (uint16_t) (mud->mqtt_state.recv_buffer_wp - mud->mqtt_state.recv_buffer);
|
|
|
|
|
|
|
|
NODE_DBG("MQTT[buffering]: appending %u bytes to previous recv buffer (%u out of wanted %u)\n",
|
|
|
|
in_buffer_length,
|
|
|
|
current_length,
|
|
|
|
mud->mqtt_state.recv_buffer_size);
|
|
|
|
|
|
|
|
// Copy from rx buffer to heap buffer. Smallest of [remainder of pending message] and [all of buffer]
|
|
|
|
uint16_t copy_length = LWIP_MIN(mud->mqtt_state.recv_buffer_size - current_length, in_buffer_length);
|
|
|
|
memcpy(mud->mqtt_state.recv_buffer_wp, pdata, copy_length);
|
|
|
|
mud->mqtt_state.recv_buffer_wp += copy_length;
|
|
|
|
|
|
|
|
in_buffer_length = (uint16_t) (mud->mqtt_state.recv_buffer_wp - mud->mqtt_state.recv_buffer);
|
|
|
|
if (in_buffer_length < mud->mqtt_state.recv_buffer_size) {
|
|
|
|
NODE_DBG("MQTT[buffering]: need %u more bytes, waiting for next rx.\n",
|
|
|
|
mud->mqtt_state.recv_buffer_size - in_buffer_length
|
|
|
|
);
|
|
|
|
goto RX_PACKET_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DBG("MQTT[buffering]: Full message received (%u). remainding bytes=%u\n",
|
|
|
|
mud->mqtt_state.recv_buffer_size,
|
|
|
|
len - copy_length);
|
|
|
|
|
|
|
|
// Point continuation_buffer to any additional data in pdata.
|
|
|
|
// May become 0 bytes, but used to trigger free!
|
|
|
|
continuation_buffer = pdata + copy_length;
|
|
|
|
len -= copy_length; // borrow len instead of having another variable..
|
|
|
|
|
|
|
|
in_buffer = mud->mqtt_state.recv_buffer;
|
|
|
|
// in_buffer_length was set above
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_NORMAL;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case MQTT_RECV_SKIPPING:
|
|
|
|
// Last rx had a message which was too large to process, skip it.
|
|
|
|
if(mud->mqtt_state.recv_buffer_skip > in_buffer_length) {
|
|
|
|
NODE_DBG("MQTT[skipping]: skip=%u. Skipping full RX buffer (%u).\n",
|
|
|
|
mud->mqtt_state.recv_buffer_skip,
|
|
|
|
in_buffer_length
|
|
|
|
);
|
|
|
|
mud->mqtt_state.recv_buffer_skip -= in_buffer_length;
|
|
|
|
goto RX_PACKET_FINISHED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DBG("MQTT[skipping]: skip=%u. Skipping partial RX buffer, continuing at %u\n",
|
|
|
|
mud->mqtt_state.recv_buffer_skip,
|
|
|
|
in_buffer_length
|
|
|
|
);
|
|
|
|
|
|
|
|
in_buffer += mud->mqtt_state.recv_buffer_skip;
|
|
|
|
in_buffer_length -= mud->mqtt_state.recv_buffer_skip;
|
|
|
|
|
|
|
|
mud->mqtt_state.recv_buffer_skip = 0;
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
READPACKET:
|
2018-11-30 22:12:46 +01:00
|
|
|
if(in_buffer_length <= 0)
|
|
|
|
goto RX_PACKET_FINISHED;
|
|
|
|
|
|
|
|
// MQTT publish message can in theory be 256Mb, while we do not support it we need to be
|
|
|
|
// able to do math on it.
|
|
|
|
int32_t message_length;
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
// temp buffer for control messages
|
2015-04-02 18:51:02 +02:00
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
2020-03-14 23:51:03 +01:00
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
2015-04-02 18:51:02 +02:00
|
|
|
mqtt_message_t *temp_msg = NULL;
|
2016-03-18 01:22:12 +01:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
switch(mud->connState){
|
|
|
|
case MQTT_CONNECT_SENDING:
|
2015-03-29 18:24:09 +02:00
|
|
|
case MQTT_CONNECT_SENT:
|
2021-01-05 12:07:09 +01:00
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
|
|
|
os_timer_arm(&mud->mqttTimer, mud->conf.keepalive * 1000, 0);
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2015-04-04 20:22:51 +02:00
|
|
|
if(mqtt_get_type(in_buffer) != MQTT_MSG_TYPE_CONNACK){
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: Invalid packet\r\n");
|
|
|
|
mud->connState = MQTT_INIT;
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_socket_do_disconnect(mud);
|
2016-03-07 02:11:16 +01:00
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_NOT_A_CONNACK_MSG);
|
2016-03-06 22:33:57 +01:00
|
|
|
break;
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2016-03-06 22:33:57 +01:00
|
|
|
} else if (mqtt_get_connect_ret_code(in_buffer) != MQTT_CONNACK_ACCEPTED) {
|
|
|
|
NODE_DBG("MQTT: CONNACK REFUSED (CODE: %d)\n", mqtt_get_connect_ret_code(in_buffer));
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2016-03-06 22:33:57 +01:00
|
|
|
mud->connState = MQTT_INIT;
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_socket_do_disconnect(mud);
|
2016-03-07 02:11:16 +01:00
|
|
|
mqtt_connack_fail(mud, mqtt_get_connect_ret_code(in_buffer));
|
2016-03-06 22:33:57 +01:00
|
|
|
break;
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
} else {
|
|
|
|
mud->connState = MQTT_DATA;
|
|
|
|
NODE_DBG("MQTT: Connected\r\n");
|
2018-02-09 08:13:24 +01:00
|
|
|
mud->keepalive_sent = 0;
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
mqtt_socket_cb_lua_noarg(lua_getstate(), mud, mud->cb_connect_ref);
|
2015-04-02 18:51:02 +02:00
|
|
|
break;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MQTT_DATA:
|
2018-11-30 22:12:46 +01:00
|
|
|
message_length = mqtt_get_total_length(in_buffer, in_buffer_length);
|
2015-04-04 20:22:51 +02:00
|
|
|
msg_type = mqtt_get_type(in_buffer);
|
|
|
|
msg_qos = mqtt_get_qos(in_buffer);
|
2018-11-30 22:12:46 +01:00
|
|
|
msg_id = mqtt_get_id(in_buffer, in_buffer_length);
|
|
|
|
|
|
|
|
NODE_DBG("MQTT_DATA: msg length: %u, buffer length: %u\r\n",
|
|
|
|
message_length,
|
|
|
|
in_buffer_length);
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if (message_length > mud->conf.max_message_length) {
|
2018-11-30 22:12:46 +01:00
|
|
|
// The pending message length is larger than we was configured to allow
|
|
|
|
if(msg_qos > 0 && msg_id == 0) {
|
|
|
|
NODE_DBG("MQTT: msg too long, but not enough data to get msg_id: total=%u, deliver=%u\r\n", message_length, in_buffer_length);
|
|
|
|
// qos requested, but too short buffer to get a packet ID.
|
|
|
|
// Trigger the "short buffer" mode
|
|
|
|
message_length = -1;
|
|
|
|
// Drop through to partial message handling below.
|
|
|
|
} else {
|
|
|
|
NODE_DBG("MQTT: msg too long: total=%u, deliver=%u\r\n", message_length, in_buffer_length);
|
|
|
|
if (msg_type == MQTT_MSG_TYPE_PUBLISH) {
|
|
|
|
// In practice we should never get any other types..
|
|
|
|
deliver_publish(mud, in_buffer, in_buffer_length, 1);
|
|
|
|
|
|
|
|
// If qos specified, we should ACK it.
|
|
|
|
// In theory it might be wrong to ack it before we received all TCP packets, but this avoids
|
|
|
|
// buffering and special code to handle this corner-case. Server will most likely have
|
|
|
|
// written all to OS socket anyway, and not be aware that we "should" not have received it all yet.
|
|
|
|
if(msg_qos == 1){
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_puback(&msgb, msg_id);
|
2018-11-30 22:12:46 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
|
|
|
msg_id, MQTT_MSG_TYPE_PUBACK, (int)mqtt_get_qos(temp_msg->data) );
|
|
|
|
}
|
|
|
|
else if(msg_qos == 2){
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_pubrec(&msgb, msg_id);
|
2018-11-30 22:12:46 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
|
|
|
msg_id, MQTT_MSG_TYPE_PUBREC, (int)mqtt_get_qos(temp_msg->data) );
|
|
|
|
}
|
|
|
|
if(msg_qos == 1 || msg_qos == 2){
|
|
|
|
NODE_DBG("MQTT: Queue response QoS: %d\r\n", msg_qos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message_length > in_buffer_length) {
|
|
|
|
// Ignore bytes in subsequent packet(s) too.
|
|
|
|
NODE_DBG("MQTT: skipping into next rx\n");
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_SKIPPING;
|
|
|
|
mud->mqtt_state.recv_buffer_skip = (uint32_t) message_length - in_buffer_length;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
NODE_DBG("MQTT: Skipping message\n");
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_NORMAL;
|
|
|
|
goto RX_MESSAGE_PROCESSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message_length == -1 || message_length > in_buffer_length) {
|
|
|
|
// Partial message in buffer, need to store on heap until next RX. Allocate size for full message directly,
|
|
|
|
// instead of potential reallocs, to avoid fragmentation.
|
|
|
|
// If message_length is indicated as -1, we do not have enough data to determine the length properly.
|
|
|
|
// Just put what we have on heap, and place in state BUFFERING_SHORT.
|
|
|
|
NODE_DBG("MQTT: Partial message received (%u of %d). Buffering\r\n",
|
|
|
|
in_buffer_length,
|
|
|
|
message_length);
|
|
|
|
|
|
|
|
// although message_length is 32bit, it should never go above 16bit since
|
|
|
|
// max_message_length is 16bit.
|
|
|
|
uint16_t alloc_size = message_length > 0 ? (uint16_t)message_length : in_buffer_length;
|
|
|
|
|
2019-07-21 23:58:21 +02:00
|
|
|
mud->mqtt_state.recv_buffer = calloc(1,alloc_size);
|
2018-11-30 22:12:46 +01:00
|
|
|
if (mud->mqtt_state.recv_buffer == NULL) {
|
|
|
|
NODE_DBG("MQTT: Failed to allocate %u bytes, disconnecting...\n", alloc_size);
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_socket_do_disconnect(mud);
|
2018-11-30 22:12:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(mud->mqtt_state.recv_buffer, in_buffer, in_buffer_length);
|
|
|
|
mud->mqtt_state.recv_buffer_wp = mud->mqtt_state.recv_buffer + in_buffer_length;
|
|
|
|
mud->mqtt_state.recv_buffer_state = message_length > 0 ? MQTT_RECV_BUFFERING : MQTT_RECV_BUFFERING_SHORT;
|
|
|
|
mud->mqtt_state.recv_buffer_size = alloc_size;
|
|
|
|
|
|
|
|
NODE_DBG("MQTT: Wait for next recv\n");
|
|
|
|
break;
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2015-03-31 17:38:28 +02:00
|
|
|
msg_queue_t *pending_msg = msg_peek(&(mud->mqtt_state.pending_msg_q));
|
2018-11-30 22:12:46 +01:00
|
|
|
NODE_DBG("MQTT_DATA: type: %d, qos: %d, msg_id: %d, pending_id: %d, msg length: %u, buffer length: %u\r\n",
|
|
|
|
msg_type,
|
|
|
|
msg_qos,
|
|
|
|
msg_id,
|
|
|
|
(pending_msg)?pending_msg->msg_id:0,
|
|
|
|
message_length,
|
|
|
|
in_buffer_length);
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
switch(msg_type)
|
|
|
|
{
|
|
|
|
case MQTT_MSG_TYPE_SUBACK:
|
2015-03-29 18:24:09 +02:00
|
|
|
if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_SUBSCRIBE && pending_msg->msg_id == msg_id){
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: Subscribe successful\r\n");
|
2015-03-29 18:24:09 +02:00
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
mqtt_socket_cb_lua_noarg(lua_getstate(), mud, mud->cb_suback_ref);
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_UNSUBACK:
|
2015-03-29 18:24:09 +02:00
|
|
|
if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_UNSUBSCRIBE && pending_msg->msg_id == msg_id){
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: UnSubscribe successful\r\n");
|
2015-03-29 18:24:09 +02:00
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2016-03-20 00:24:18 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
mqtt_socket_cb_lua_noarg(lua_getstate(), mud, mud->cb_unsuback_ref);
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PUBLISH:
|
2015-03-29 18:24:09 +02:00
|
|
|
if(msg_qos == 1){
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_puback(&msgb, msg_id);
|
2016-03-07 04:54:28 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
2015-04-02 18:51:02 +02:00
|
|
|
msg_id, MQTT_MSG_TYPE_PUBACK, (int)mqtt_get_qos(temp_msg->data) );
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
|
|
|
else if(msg_qos == 2){
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_pubrec(&msgb, msg_id);
|
2016-03-07 04:54:28 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
2015-04-02 18:51:02 +02:00
|
|
|
msg_id, MQTT_MSG_TYPE_PUBREC, (int)mqtt_get_qos(temp_msg->data) );
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
|
|
|
if(msg_qos == 1 || msg_qos == 2){
|
|
|
|
NODE_DBG("MQTT: Queue response QoS: %d\r\n", msg_qos);
|
|
|
|
}
|
2018-11-30 22:12:46 +01:00
|
|
|
deliver_publish(mud, in_buffer, (uint16_t)message_length, 0);
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PUBACK:
|
2015-03-29 18:24:09 +02:00
|
|
|
if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg->msg_id == msg_id){
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: Publish with QoS = 1 successful\r\n");
|
2015-03-29 18:24:09 +02:00
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
mqtt_socket_cb_lua_noarg(lua_getstate(), mud, mud->cb_puback_ref);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PUBREC:
|
2015-03-31 17:38:28 +02:00
|
|
|
if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBLISH && pending_msg->msg_id == msg_id){
|
2015-10-13 07:49:10 +02:00
|
|
|
NODE_DBG("MQTT: Publish with QoS = 2 Received PUBREC\r\n");
|
2016-03-06 22:33:57 +01:00
|
|
|
// Note: actually, should not destroy the msg until PUBCOMP is received.
|
2015-03-31 17:38:28 +02:00
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_pubrel(&msgb, msg_id);
|
2016-03-07 04:54:28 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
2015-04-02 18:51:02 +02:00
|
|
|
msg_id, MQTT_MSG_TYPE_PUBREL, (int)mqtt_get_qos(temp_msg->data) );
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: Response PUBREL\r\n");
|
2015-03-31 17:38:28 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PUBREL:
|
2015-03-31 17:38:28 +02:00
|
|
|
if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBREC && pending_msg->msg_id == msg_id){
|
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_pubcomp(&msgb, msg_id);
|
2016-03-07 04:54:28 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
2015-04-02 18:51:02 +02:00
|
|
|
msg_id, MQTT_MSG_TYPE_PUBCOMP, (int)mqtt_get_qos(temp_msg->data) );
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: Response PUBCOMP\r\n");
|
2015-03-31 17:38:28 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PUBCOMP:
|
2015-03-31 17:38:28 +02:00
|
|
|
if(pending_msg && pending_msg->msg_type == MQTT_MSG_TYPE_PUBREL && pending_msg->msg_id == msg_id){
|
2015-01-23 04:48:05 +01:00
|
|
|
NODE_DBG("MQTT: Publish with QoS = 2 successful\r\n");
|
2015-03-29 18:24:09 +02:00
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
mqtt_socket_cb_lua_noarg(lua_getstate(), mud, mud->cb_puback_ref);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PINGREQ:
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_pingresp(&msgb);
|
2016-03-07 04:54:28 +01:00
|
|
|
msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
2015-04-02 18:51:02 +02:00
|
|
|
msg_id, MQTT_MSG_TYPE_PINGRESP, (int)mqtt_get_qos(temp_msg->data) );
|
2015-03-29 18:24:09 +02:00
|
|
|
NODE_DBG("MQTT: Response PINGRESP\r\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PINGRESP:
|
|
|
|
// Ignore
|
2016-11-08 21:00:17 +01:00
|
|
|
mud->keepalive_sent = 0;
|
2015-03-29 18:24:09 +02:00
|
|
|
NODE_DBG("MQTT: PINGRESP received\r\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
RX_MESSAGE_PROCESSED:
|
|
|
|
if(continuation_buffer != NULL) {
|
|
|
|
NODE_DBG("MQTT[buffering]: buffered message finished. Continuing with rest of rx buffer (%u)\n",
|
|
|
|
len);
|
2019-07-21 23:58:21 +02:00
|
|
|
free(mud->mqtt_state.recv_buffer);
|
2018-11-30 22:12:46 +01:00
|
|
|
mud->mqtt_state.recv_buffer = NULL;
|
|
|
|
|
|
|
|
in_buffer = continuation_buffer;
|
|
|
|
in_buffer_length = len;
|
|
|
|
continuation_buffer = NULL;
|
|
|
|
}else{
|
|
|
|
// Message have been fully processed (or ignored). Move pointer ahead
|
|
|
|
// and continue with next message, if any.
|
|
|
|
in_buffer_length -= message_length;
|
|
|
|
in_buffer += message_length;
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
if(in_buffer_length > 0)
|
|
|
|
{
|
|
|
|
NODE_DBG("Get another published message\r\n");
|
|
|
|
goto READPACKET;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2018-11-30 22:12:46 +01:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
RX_PACKET_FINISHED:
|
|
|
|
if(temp_pdata != NULL) {
|
2019-07-21 23:58:21 +02:00
|
|
|
free(temp_pdata);
|
2018-11-30 22:12:46 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_send_if_possible(mud);
|
2018-11-30 22:12:46 +01:00
|
|
|
NODE_DBG("leave mqtt_socket_received\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mqtt_socket_sent(void *arg)
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_sent.\n");
|
2020-04-07 14:06:27 +02:00
|
|
|
lmqtt_userdata *mud = arg;
|
2015-01-23 04:48:05 +01:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
|
|
|
if(!mud->connected)
|
2015-03-29 18:24:09 +02:00
|
|
|
return;
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
mud->sending = false;
|
|
|
|
|
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
2015-04-02 18:51:02 +02:00
|
|
|
|
2015-03-29 18:24:09 +02:00
|
|
|
if(mud->connState == MQTT_CONNECT_SENDING){
|
|
|
|
mud->connState = MQTT_CONNECT_SENT;
|
2021-01-05 12:07:09 +01:00
|
|
|
os_timer_arm(&mud->mqttTimer, MQTT_SEND_TIMEOUT * 1000, 0);
|
2015-04-02 18:51:02 +02:00
|
|
|
// MQTT_CONNECT not queued.
|
|
|
|
return;
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
/* Ready for timeout */
|
|
|
|
os_timer_arm(&mud->mqttTimer, mud->conf.keepalive * 1000, 0);
|
|
|
|
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("sent1, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2021-01-05 12:07:09 +01:00
|
|
|
|
2015-03-31 17:38:28 +02:00
|
|
|
msg_queue_t *node = msg_peek(&(mud->mqtt_state.pending_msg_q));
|
2021-01-05 12:07:09 +01:00
|
|
|
if (node) {
|
|
|
|
switch (node->msg_type) {
|
|
|
|
case MQTT_MSG_TYPE_PUBLISH:
|
|
|
|
// qos = 0, publish and forget. Run the callback now because we
|
|
|
|
// won't get a puback from the server and it's not clear when else
|
|
|
|
// we should tell the user the message drained from the egress queue
|
|
|
|
if (node->publish_qos == 0) {
|
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
|
|
|
mqtt_socket_cb_lua_noarg(lua_getstate(), mud, mud->cb_puback_ref);
|
|
|
|
mqtt_send_if_possible(mud);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_PUBACK:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case MQTT_MSG_TYPE_PUBCOMP:
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case MQTT_MSG_TYPE_PINGREQ:
|
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
|
|
|
mqtt_send_if_possible(mud);
|
|
|
|
break;
|
|
|
|
case MQTT_MSG_TYPE_DISCONNECT:
|
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
|
|
|
mqtt_socket_do_disconnect(mud);
|
|
|
|
break;
|
2016-02-02 04:29:32 +01:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("sent2, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_sent.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mqtt_socket_connected(void *arg)
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_connected.\n");
|
2020-04-07 14:06:27 +02:00
|
|
|
lmqtt_userdata *mud = arg;
|
2015-01-23 04:48:05 +01:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
2020-04-07 14:06:27 +02:00
|
|
|
struct espconn *pesp_conn = &mud->pesp_conn;
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->connected = true;
|
|
|
|
espconn_regist_recvcb(pesp_conn, mqtt_socket_received);
|
|
|
|
espconn_regist_sentcb(pesp_conn, mqtt_socket_sent);
|
|
|
|
espconn_regist_disconcb(pesp_conn, mqtt_socket_disconnected);
|
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
2020-03-14 23:51:03 +01:00
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
/* Build the mqtt_connect_info on the stack and assemble the message */
|
|
|
|
lua_State *L = lua_getstate();
|
|
|
|
int ltop = lua_gettop(L);
|
|
|
|
struct mqtt_connect_info mci;
|
|
|
|
mci.clean_session = mud->conf.flags.clean_session;
|
|
|
|
mci.will_retain = mud->conf.flags.will_retain;
|
|
|
|
mci.will_qos = mud->conf.flags.will_qos;
|
|
|
|
mci.keepalive = mud->conf.keepalive;
|
|
|
|
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->conf.client_id_ref);
|
|
|
|
mci.client_id = lua_tostring(L, -1);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->conf.username_ref);
|
|
|
|
mci.username = lua_tostring(L, -1);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->conf.password_ref);
|
|
|
|
mci.password = lua_tostring(L, -1);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->conf.will_topic_ref);
|
|
|
|
mci.will_topic = lua_tostring(L, -1);
|
|
|
|
lua_rawgeti(L, LUA_REGISTRYINDEX, mud->conf.will_message_ref);
|
|
|
|
mci.will_message = lua_tostring(L, -1);
|
|
|
|
|
|
|
|
mqtt_message_t* temp_msg = mqtt_msg_connect(&msgb, &mci);
|
2015-04-02 18:51:02 +02:00
|
|
|
NODE_DBG("Send MQTT connection infomation, data len: %d, d[0]=%d \r\n", temp_msg->length, temp_msg->data[0]);
|
2020-03-14 23:51:03 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_settop(L, ltop); /* Done with strings, restore the stack */
|
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
// not queue this message. should send right now. or should enqueue this before head.
|
2015-10-18 23:04:40 +02:00
|
|
|
#ifdef CLIENT_SSL_ENABLE
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->conf.flags.secure)
|
2015-10-18 23:04:40 +02:00
|
|
|
{
|
2016-01-27 23:54:19 +01:00
|
|
|
espconn_secure_send(pesp_conn, temp_msg->data, temp_msg->length);
|
2015-10-18 23:04:40 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
else
|
2015-10-18 23:04:40 +02:00
|
|
|
#endif
|
|
|
|
{
|
2016-01-27 23:54:19 +01:00
|
|
|
espconn_send(pesp_conn, temp_msg->data, temp_msg->length);
|
2015-10-18 23:04:40 +02:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->sending = true;
|
|
|
|
|
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
|
|
|
os_timer_arm(&mud->mqttTimer, MQTT_SEND_TIMEOUT * 1000, 0);
|
2015-04-02 18:51:02 +02:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->connState = MQTT_CONNECT_SENDING;
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
NODE_DBG("leave mqtt_socket_connected, heap = %u.\n", system_get_free_heap_size());
|
2015-01-23 04:48:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void mqtt_socket_timer(void *arg)
|
|
|
|
{
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_timer.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
lmqtt_userdata *mud = (lmqtt_userdata*) arg;
|
|
|
|
|
2015-03-29 18:24:09 +02:00
|
|
|
if(mud == NULL)
|
|
|
|
return;
|
2020-04-07 14:06:27 +02:00
|
|
|
|
2020-06-30 06:35:12 +02:00
|
|
|
if(mud->pesp_conn.proto.tcp == NULL){
|
2020-04-07 14:06:27 +02:00
|
|
|
NODE_DBG("MQTT not connected\n");
|
2015-04-02 18:51:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("timer, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->connState == MQTT_CONNECT_SENDING){ // MQTT_CONNECT send time out.
|
2015-04-02 18:51:02 +02:00
|
|
|
NODE_DBG("sSend MQTT_CONNECT failed.\n");
|
2015-03-30 18:36:44 +02:00
|
|
|
mud->connState = MQTT_INIT;
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_socket_do_disconnect(mud);
|
2016-03-07 02:11:16 +01:00
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_TIMEOUT_SENDING);
|
2016-03-06 22:33:57 +01:00
|
|
|
} else if(mud->connState == MQTT_CONNECT_SENT) { // wait for CONACK time out.
|
|
|
|
NODE_DBG("MQTT_CONNECT timeout.\n");
|
2016-05-10 04:04:23 +02:00
|
|
|
mud->connState = MQTT_INIT;
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_socket_do_disconnect(mud);
|
2016-03-07 02:11:16 +01:00
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_TIMEOUT_RECEIVING);
|
2015-03-30 18:36:44 +02:00
|
|
|
} else if(mud->connState == MQTT_DATA){
|
2021-01-05 12:07:09 +01:00
|
|
|
if(!msg_peek(&(mud->mqtt_state.pending_msg_q))) {
|
2015-03-30 18:36:44 +02:00
|
|
|
// no queued event.
|
2021-01-05 12:07:09 +01:00
|
|
|
if (mud->keepalive_sent) {
|
|
|
|
// Oh dear -- keepalive timer expired and still no ack of previous message
|
|
|
|
mqtt_socket_do_disconnect(mud);
|
|
|
|
} else {
|
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
|
|
|
|
|
|
|
NODE_DBG("\r\nMQTT: Send keepalive packet\r\n");
|
|
|
|
mqtt_message_t* temp_msg = mqtt_msg_pingreq(&msgb);
|
|
|
|
msg_queue_t *node = msg_enqueue( &(mud->mqtt_state.pending_msg_q), temp_msg,
|
|
|
|
0, MQTT_MSG_TYPE_PINGREQ, (int)mqtt_get_qos(temp_msg->data) );
|
|
|
|
mud->keepalive_sent = 1;
|
|
|
|
mqtt_send_if_possible(mud);
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_timer.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
// Lua: mqtt.Client(clientid, keepalive, user, pass, clean_session, max_message_length)
|
2015-01-23 04:48:05 +01:00
|
|
|
static int mqtt_socket_client( lua_State* L )
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_client.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
lmqtt_userdata *mud;
|
|
|
|
int stack = 1;
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
|
|
|
// create a object
|
|
|
|
mud = (lmqtt_userdata *)lua_newuserdata(L, sizeof(lmqtt_userdata));
|
2019-07-21 23:58:21 +02:00
|
|
|
memset(mud, 0, sizeof(*mud));
|
2015-01-23 04:48:05 +01:00
|
|
|
// pre-initialize it, in case of errors
|
|
|
|
mud->self_ref = LUA_NOREF;
|
|
|
|
mud->cb_connect_ref = LUA_NOREF;
|
2016-03-06 22:33:57 +01:00
|
|
|
mud->cb_connect_fail_ref = LUA_NOREF;
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->cb_disconnect_ref = LUA_NOREF;
|
|
|
|
|
|
|
|
mud->cb_message_ref = LUA_NOREF;
|
2018-11-30 22:12:46 +01:00
|
|
|
mud->cb_overflow_ref = LUA_NOREF;
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->cb_suback_ref = LUA_NOREF;
|
2016-05-21 14:25:16 +02:00
|
|
|
mud->cb_unsuback_ref = LUA_NOREF;
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->cb_puback_ref = LUA_NOREF;
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.client_id_ref = LUA_NOREF;
|
|
|
|
mud->conf.username_ref = LUA_NOREF;
|
|
|
|
mud->conf.password_ref = LUA_NOREF;
|
|
|
|
mud->conf.will_topic_ref = LUA_NOREF;
|
|
|
|
mud->conf.will_message_ref = LUA_NOREF;
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->connState = MQTT_INIT;
|
|
|
|
|
|
|
|
// set its metatable
|
|
|
|
luaL_getmetatable(L, "mqtt.socket");
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
|
|
|
|
if( lua_isstring(L,stack) ) // deal with the clientid string
|
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_pushvalue(L, stack);
|
2015-01-23 04:48:05 +01:00
|
|
|
stack++;
|
2021-01-05 12:07:09 +01:00
|
|
|
} else {
|
|
|
|
char tempid[20] = {0};
|
|
|
|
sprintf(tempid, "%s%x", "NodeMCU_", system_get_chip_id() );
|
|
|
|
lua_pushstring(L, tempid);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.client_id_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
if(lua_isnumber( L, stack ))
|
2015-10-13 07:49:10 +02:00
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.keepalive = luaL_checkinteger( L, stack);
|
2015-01-23 04:48:05 +01:00
|
|
|
stack++;
|
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->conf.keepalive == 0) {
|
|
|
|
mud->conf.keepalive = MQTT_DEFAULT_KEEPALIVE;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if(lua_isstring( L, stack )){
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_pushvalue(L, stack);
|
|
|
|
mud->conf.username_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
2015-01-23 04:48:05 +01:00
|
|
|
stack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(lua_isstring( L, stack )){
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_pushvalue(L, stack);
|
|
|
|
mud->conf.password_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
2015-01-23 04:48:05 +01:00
|
|
|
stack++;
|
|
|
|
}
|
2015-10-13 07:49:10 +02:00
|
|
|
|
2015-09-05 20:54:13 +02:00
|
|
|
if(lua_isnumber( L, stack ))
|
2015-10-13 07:49:10 +02:00
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.flags.clean_session = !!luaL_checkinteger(L, stack);
|
2015-09-05 20:54:13 +02:00
|
|
|
stack++;
|
|
|
|
}
|
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
if(lua_isnumber( L, stack ))
|
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.max_message_length = luaL_checkinteger( L, stack);
|
2018-11-30 22:12:46 +01:00
|
|
|
stack++;
|
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->conf.max_message_length == 0) {
|
|
|
|
mud->conf.max_message_length = DEFAULT_MAX_MESSAGE_LENGTH;
|
2018-11-30 22:12:46 +01:00
|
|
|
}
|
|
|
|
|
2015-03-29 18:24:09 +02:00
|
|
|
mud->mqtt_state.pending_msg_q = NULL;
|
2018-11-30 22:12:46 +01:00
|
|
|
mud->mqtt_state.recv_buffer = NULL;
|
|
|
|
mud->mqtt_state.recv_buffer_size = 0;
|
|
|
|
mud->mqtt_state.recv_buffer_state = MQTT_RECV_NORMAL;
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_client.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: mqtt.delete( socket )
|
|
|
|
// call close() first
|
|
|
|
// socket: unref everything
|
|
|
|
static int mqtt_delete( lua_State* L )
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_delete.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
lmqtt_userdata *mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
|
|
|
|
|
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
2015-03-29 18:24:09 +02:00
|
|
|
mud->connected = false;
|
|
|
|
|
|
|
|
// ---- alloc-ed in mqtt_socket_connect()
|
2020-04-07 14:06:27 +02:00
|
|
|
if(mud->pesp_conn.proto.tcp)
|
|
|
|
free(mud->pesp_conn.proto.tcp);
|
|
|
|
mud->pesp_conn.proto.tcp = NULL;
|
|
|
|
|
2016-08-14 10:32:11 +02:00
|
|
|
while(mud->mqtt_state.pending_msg_q) {
|
|
|
|
msg_destroy(msg_dequeue(&(mud->mqtt_state.pending_msg_q)));
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2018-11-30 22:12:46 +01:00
|
|
|
//--------- alloc-ed in mqtt_socket_received()
|
|
|
|
if(mud->mqtt_state.recv_buffer) {
|
2019-07-21 23:58:21 +02:00
|
|
|
free(mud->mqtt_state.recv_buffer);
|
2018-11-30 22:12:46 +01:00
|
|
|
mud->mqtt_state.recv_buffer = NULL;
|
|
|
|
}
|
|
|
|
// ----
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
// free (unref) callback ref
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
|
|
|
|
mud->cb_connect_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
|
|
|
|
mud->cb_connect_fail_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
|
|
|
|
mud->cb_disconnect_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_message_ref);
|
|
|
|
mud->cb_message_ref = LUA_NOREF;
|
2018-11-30 22:12:46 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_overflow_ref);
|
|
|
|
mud->cb_overflow_ref = LUA_NOREF;
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_suback_ref);
|
|
|
|
mud->cb_suback_ref = LUA_NOREF;
|
2016-05-21 14:25:16 +02:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref);
|
|
|
|
mud->cb_unsuback_ref = LUA_NOREF;
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
|
|
|
|
mud->cb_puback_ref = LUA_NOREF;
|
2020-04-05 02:25:57 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->conf.client_id_ref);
|
|
|
|
mud->conf.client_id_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->conf.username_ref);
|
|
|
|
mud->conf.username_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->conf.password_ref);
|
|
|
|
mud->conf.password_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->conf.will_topic_ref);
|
|
|
|
mud->conf.will_topic_ref = LUA_NOREF;
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->conf.will_message_ref);
|
|
|
|
mud->conf.will_message_ref = LUA_NOREF;
|
|
|
|
|
2020-04-05 02:25:57 +02:00
|
|
|
int selfref = mud->self_ref;
|
2016-03-26 04:35:07 +01:00
|
|
|
mud->self_ref = LUA_NOREF;
|
2020-04-05 02:25:57 +02:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->self_ref);
|
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_delete.\n");
|
2015-10-13 07:49:10 +02:00
|
|
|
return 0;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
static sint8 mqtt_socket_do_connect(struct lmqtt_userdata *mud)
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2016-03-06 22:33:57 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("enter mqtt_socket_do_connect.\n");
|
2016-01-27 23:54:19 +01:00
|
|
|
sint8 espconn_status;
|
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
mud->connState = MQTT_INIT;
|
2015-10-18 23:04:40 +02:00
|
|
|
#ifdef CLIENT_SSL_ENABLE
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->conf.flags.secure)
|
2015-10-18 23:04:40 +02:00
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("mqtt_socket_do_connect using espconn_secure\n");
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_status = espconn_secure_connect(&mud->pesp_conn);
|
2015-10-18 23:04:40 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
else
|
2015-10-18 23:04:40 +02:00
|
|
|
#endif
|
|
|
|
{
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_status = espconn_connect(&mud->pesp_conn);
|
2015-10-18 23:04:40 +02:00
|
|
|
}
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("leave mqtt_socket_do_connect espconn_status=%d\n", espconn_status);
|
2016-01-27 23:54:19 +01:00
|
|
|
|
|
|
|
return espconn_status;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
static void socket_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2020-04-07 14:06:27 +02:00
|
|
|
lmqtt_userdata *mud = arg;
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if((ipaddr == NULL) || (ipaddr->addr == 0))
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("socket_dns_found failure\n");
|
2020-04-07 14:06:27 +02:00
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_DNS);
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
// although not connected, but fire disconnect callback to release every thing.
|
|
|
|
mqtt_socket_disconnected(arg);
|
2021-01-05 12:07:09 +01:00
|
|
|
return;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2020-04-07 14:06:27 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
memcpy(&mud->pesp_conn.proto.tcp->remote_ip, &(ipaddr->addr), 4);
|
|
|
|
NODE_DBG("socket_dns_found success: ");
|
|
|
|
NODE_DBG(IPSTR, IP2STR(&(ipaddr->addr)));
|
|
|
|
NODE_DBG("\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2021-12-06 13:38:48 +01:00
|
|
|
if(mqtt_socket_do_connect(mud) != ESPCONN_OK) {
|
|
|
|
NODE_DBG("socket_dns_found, got DNS but connect failed\n");
|
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_DNS);
|
|
|
|
mqtt_socket_disconnected(arg);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-07 14:06:27 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 21:41:14 +02:00
|
|
|
#include "pm/swtimer.h"
|
2019-07-16 11:30:41 +02:00
|
|
|
// Lua: mqtt:connect( host, port, secure, function(client), function(client, connect_return_code) )
|
2015-01-23 04:48:05 +01:00
|
|
|
static int mqtt_socket_connect( lua_State* L )
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_connect.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
lmqtt_userdata *mud = NULL;
|
|
|
|
unsigned port = 1883;
|
|
|
|
size_t il;
|
|
|
|
ip_addr_t ipaddr;
|
|
|
|
const char *domain;
|
|
|
|
int stack = 1;
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
|
|
|
|
mud = (lmqtt_userdata *)luaL_checkudata(L, stack, "mqtt.socket");
|
|
|
|
stack++;
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
struct espconn *pesp_conn = &mud->pesp_conn;
|
2015-02-07 16:45:01 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if(mud->connected){
|
|
|
|
return luaL_error(L, "already connected");
|
2016-01-27 23:54:19 +01:00
|
|
|
}
|
2020-04-07 14:06:27 +02:00
|
|
|
|
2015-03-29 18:24:09 +02:00
|
|
|
mud->connected = false;
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->sending = false;
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
if( (stack<=top) && lua_isstring(L,stack) ) // deal with the domain string
|
|
|
|
{
|
|
|
|
domain = luaL_checklstring( L, stack, &il );
|
2021-01-05 12:07:09 +01:00
|
|
|
if (!domain)
|
|
|
|
return luaL_error(L, "invalid domain");
|
2015-01-23 04:48:05 +01:00
|
|
|
stack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (stack<=top) && lua_isnumber(L, stack) )
|
|
|
|
{
|
|
|
|
port = lua_tointeger(L, stack);
|
|
|
|
stack++;
|
|
|
|
}
|
|
|
|
|
2019-07-16 11:30:41 +02:00
|
|
|
if ( (stack<=top) && (lua_isnumber(L, stack) || lua_isboolean(L, stack)) )
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2019-07-16 11:30:41 +02:00
|
|
|
if (lua_isnumber(L, stack)) {
|
|
|
|
platform_print_deprecation_note("mqtt.connect secure parameter as integer","in the future");
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.flags.secure = !!lua_tointeger(L, stack);
|
2019-07-16 11:30:41 +02:00
|
|
|
} else {
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.flags.secure = lua_toboolean(L, stack);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2019-07-16 11:30:41 +02:00
|
|
|
stack++;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
|
|
|
|
#ifndef CLIENT_SSL_ENABLE
|
|
|
|
if ( mud->conf.flags.secure )
|
2015-10-18 23:04:40 +02:00
|
|
|
{
|
|
|
|
return luaL_error(L, "ssl not available");
|
|
|
|
}
|
|
|
|
#endif
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
// call back function when a connection is obtained, tcp only
|
2020-04-27 02:13:38 +02:00
|
|
|
if ((stack<=top) && (lua_isfunction(L, stack))){
|
2015-01-23 04:48:05 +01:00
|
|
|
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
2016-05-10 04:04:23 +02:00
|
|
|
|
|
|
|
stack++;
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2016-05-10 04:04:23 +02:00
|
|
|
// call back function when a connection fails
|
2020-04-27 02:13:38 +02:00
|
|
|
if ((stack<=top) && (lua_isfunction(L, stack))){
|
2016-03-06 22:33:57 +01:00
|
|
|
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
|
2016-03-06 22:33:57 +01:00
|
|
|
mud->cb_connect_fail_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if (!pesp_conn->proto.tcp)
|
|
|
|
pesp_conn->proto.tcp = (esp_tcp *)calloc(1,sizeof(esp_tcp));
|
|
|
|
|
|
|
|
if(!pesp_conn->proto.tcp) {
|
|
|
|
return luaL_error(L, "not enough memory");
|
|
|
|
}
|
|
|
|
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->self_ref);
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_pushvalue(L, 1); // copy userdata and persist it in the registry
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->self_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
pesp_conn->type = ESPCONN_TCP;
|
|
|
|
pesp_conn->state = ESPCONN_NONE;
|
|
|
|
|
|
|
|
pesp_conn->proto.tcp->remote_port = port;
|
|
|
|
if (pesp_conn->proto.tcp->local_port == 0)
|
|
|
|
pesp_conn->proto.tcp->local_port = espconn_port();
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_regist_connectcb(pesp_conn, mqtt_socket_connected);
|
|
|
|
espconn_regist_reconcb(pesp_conn, mqtt_socket_reconnected);
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
os_timer_disarm(&mud->mqttTimer);
|
|
|
|
os_timer_setfn(&mud->mqttTimer, (os_timer_func_t *)mqtt_socket_timer, mud);
|
2018-04-13 21:41:14 +02:00
|
|
|
SWTIMER_REG_CB(mqtt_socket_timer, SWTIMER_RESUME);
|
|
|
|
//I assume that mqtt_socket_timer connects to the mqtt server, but I'm not really sure what impact light_sleep will have on it.
|
|
|
|
//My guess: If in doubt, resume the timer
|
2015-03-30 18:36:44 +02:00
|
|
|
// timer started in socket_connect()
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
ip_addr_t host_ip;
|
|
|
|
switch (dns_gethostbyname(domain, &host_ip, socket_dns_found, mud))
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
case ERR_OK:
|
|
|
|
socket_dns_found(domain, &host_ip, mud); // ip is returned in host_ip.
|
|
|
|
break;
|
|
|
|
case ERR_INPROGRESS:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Something has gone wrong; bail out?
|
|
|
|
mqtt_connack_fail(mud, MQTT_CONN_FAIL_DNS);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_connect.\n");
|
2016-01-27 23:54:19 +01:00
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
return 0;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: mqtt:close()
|
|
|
|
// client disconnect and unref itself
|
|
|
|
static int mqtt_socket_close( lua_State* L )
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_close.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
lmqtt_userdata *mud = NULL;
|
|
|
|
|
|
|
|
mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
sint8 espconn_status = 0;
|
2016-08-14 10:32:11 +02:00
|
|
|
if (mud->connected) {
|
2020-03-14 23:51:03 +01:00
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
|
|
|
|
2016-08-14 10:32:11 +02:00
|
|
|
// Send disconnect message
|
2020-03-14 23:51:03 +01:00
|
|
|
mqtt_message_t* temp_msg = mqtt_msg_disconnect(&msgb);
|
2016-08-14 10:32:11 +02:00
|
|
|
NODE_DBG("Send MQTT disconnect infomation, data len: %d, d[0]=%d \r\n", temp_msg->length, temp_msg->data[0]);
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
if (!msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg, 0,
|
|
|
|
MQTT_MSG_TYPE_DISCONNECT, 0))
|
|
|
|
goto err;
|
2016-11-08 21:00:17 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
mqtt_send_if_possible(mud);
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2016-08-14 10:32:11 +02:00
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_close.\n");
|
2021-01-05 12:07:09 +01:00
|
|
|
return 0;
|
2016-01-27 23:54:19 +01:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
err:
|
|
|
|
/* OOM while trying to build the disconnect message. Fail the connection */
|
|
|
|
mqtt_socket_do_disconnect(mud);
|
|
|
|
|
|
|
|
NODE_DBG("leave mqtt_socket_close on error path\n");
|
|
|
|
|
|
|
|
return 0;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lua: mqtt:on( "method", function() )
|
|
|
|
static int mqtt_socket_on( lua_State* L )
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_on.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
lmqtt_userdata *mud;
|
|
|
|
size_t sl;
|
|
|
|
|
|
|
|
mud = (lmqtt_userdata *)luaL_checkudata(L, 1, "mqtt.socket");
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
luaL_checktype(L, 3, LUA_TFUNCTION);
|
2015-01-23 04:48:05 +01:00
|
|
|
lua_pushvalue(L, 3); // copy argument (func) to the top of stack
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
static const char * const cbnames[] = {
|
|
|
|
"connect", "connfail", "offline",
|
|
|
|
"message", "overflow",
|
|
|
|
"puback", "suback", "unsuback",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
switch (luaL_checkoption(L, 2, NULL, cbnames)) {
|
|
|
|
case 0:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_ref);
|
|
|
|
mud->cb_connect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_connect_fail_ref);
|
|
|
|
mud->cb_connect_fail_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_disconnect_ref);
|
|
|
|
mud->cb_disconnect_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_message_ref);
|
|
|
|
mud->cb_message_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_overflow_ref);
|
|
|
|
mud->cb_overflow_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
|
|
|
|
mud->cb_puback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_suback_ref);
|
|
|
|
mud->cb_suback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref);
|
|
|
|
mud->cb_unsuback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
break;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
2021-01-05 12:07:09 +01:00
|
|
|
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_on.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:24:18 +01:00
|
|
|
// Lua: bool = mqtt:unsubscribe(topic, function())
|
|
|
|
static int mqtt_socket_unsubscribe( lua_State* L ) {
|
|
|
|
NODE_DBG("enter mqtt_socket_unsubscribe.\n");
|
|
|
|
|
|
|
|
uint8_t stack = 1;
|
2020-03-14 23:51:03 +01:00
|
|
|
uint16_t msg_id;
|
2016-03-20 00:24:18 +01:00
|
|
|
const char *topic;
|
|
|
|
size_t il;
|
|
|
|
lmqtt_userdata *mud;
|
|
|
|
|
|
|
|
mud = (lmqtt_userdata *) luaL_checkudata( L, stack, "mqtt.socket" );
|
|
|
|
stack++;
|
|
|
|
|
|
|
|
if(!mud->connected){
|
|
|
|
luaL_error( L, "not connected" );
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
2020-03-14 23:51:03 +01:00
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
2016-03-20 00:24:18 +01:00
|
|
|
mqtt_message_t *temp_msg = NULL;
|
|
|
|
|
|
|
|
if( lua_istable( L, stack ) ) {
|
|
|
|
NODE_DBG("unsubscribe table\n");
|
|
|
|
lua_pushnil( L ); /* first key */
|
|
|
|
|
|
|
|
int topic_count = 0;
|
|
|
|
uint8_t overflow = 0;
|
|
|
|
|
|
|
|
while( lua_next( L, stack ) != 0 ) {
|
|
|
|
topic = luaL_checkstring( L, -2 );
|
|
|
|
|
|
|
|
if (topic_count == 0) {
|
2020-03-14 23:51:03 +01:00
|
|
|
msg_id = mqtt_next_message_id(mud);
|
|
|
|
temp_msg = mqtt_msg_unsubscribe_init( &msgb, msg_id );
|
2016-03-20 00:24:18 +01:00
|
|
|
}
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_unsubscribe_topic( &msgb, topic );
|
2016-03-20 00:24:18 +01:00
|
|
|
topic_count++;
|
|
|
|
|
|
|
|
NODE_DBG("topic: %s - length: %d\n", topic, temp_msg->length);
|
|
|
|
|
|
|
|
if (temp_msg->length == 0) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
overflow = 1;
|
|
|
|
break; // too long message for the outbuffer.
|
|
|
|
}
|
|
|
|
|
|
|
|
lua_pop( L, 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (topic_count == 0){
|
|
|
|
return luaL_error( L, "no topics found" );
|
|
|
|
}
|
|
|
|
if (overflow != 0){
|
|
|
|
return luaL_error( L, "buffer overflow, can't enqueue all unsubscriptions" );
|
|
|
|
}
|
|
|
|
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_unsubscribe_fini( &msgb );
|
2016-03-20 00:24:18 +01:00
|
|
|
if (temp_msg->length == 0) {
|
|
|
|
return luaL_error( L, "buffer overflow, can't enqueue all unsubscriptions" );
|
|
|
|
}
|
|
|
|
|
|
|
|
stack++;
|
|
|
|
} else {
|
|
|
|
NODE_DBG("unsubscribe string\n");
|
|
|
|
topic = luaL_checklstring( L, stack, &il );
|
|
|
|
stack++;
|
|
|
|
if( topic == NULL ){
|
|
|
|
return luaL_error( L, "need topic name" );
|
|
|
|
}
|
2020-03-14 23:51:03 +01:00
|
|
|
msg_id = mqtt_next_message_id(mud);
|
|
|
|
temp_msg = mqtt_msg_unsubscribe( &msgb, topic, msg_id );
|
2016-03-20 00:24:18 +01:00
|
|
|
}
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, stack)) { // TODO: this will overwrite the previous one.
|
|
|
|
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref( L, LUA_REGISTRYINDEX, mud->cb_unsuback_ref );
|
2016-03-20 00:24:18 +01:00
|
|
|
mud->cb_unsuback_ref = luaL_ref( L, LUA_REGISTRYINDEX );
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_queue_t *node = msg_enqueue( &(mud->mqtt_state.pending_msg_q), temp_msg,
|
|
|
|
msg_id, MQTT_MSG_TYPE_UNSUBSCRIBE, (int)mqtt_get_qos(temp_msg->data) );
|
|
|
|
|
|
|
|
NODE_DBG("topic: %s - id: %d - qos: %d, length: %d\n", topic, node->msg_id, node->publish_qos, node->msg.length);
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("msg_size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2016-03-20 00:24:18 +01:00
|
|
|
|
|
|
|
sint8 espconn_status = ESPCONN_IF;
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_status = mqtt_send_if_possible(mud);
|
2016-03-20 00:24:18 +01:00
|
|
|
|
|
|
|
if(!node || espconn_status != ESPCONN_OK){
|
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
} else {
|
|
|
|
lua_pushboolean(L, 1); // enqueued succeed.
|
|
|
|
}
|
|
|
|
NODE_DBG("unsubscribe, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
|
|
|
NODE_DBG("leave mqtt_socket_unsubscribe.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-29 18:24:09 +02:00
|
|
|
// Lua: bool = mqtt:subscribe(topic, qos, function())
|
2015-02-02 10:58:54 +01:00
|
|
|
static int mqtt_socket_subscribe( lua_State* L ) {
|
2016-01-27 23:54:19 +01:00
|
|
|
NODE_DBG("enter mqtt_socket_subscribe.\n");
|
2015-02-02 10:58:54 +01:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
uint8_t stack = 1, qos = 0;
|
2020-03-14 23:51:03 +01:00
|
|
|
uint16_t msg_id;
|
2016-01-27 23:54:19 +01:00
|
|
|
const char *topic;
|
|
|
|
size_t il;
|
|
|
|
lmqtt_userdata *mud;
|
2015-02-02 10:58:54 +01:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
mud = (lmqtt_userdata *) luaL_checkudata( L, stack, "mqtt.socket" );
|
|
|
|
stack++;
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
if(!mud->connected){
|
|
|
|
luaL_error( L, "not connected" );
|
2015-03-29 18:24:09 +02:00
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-02-02 10:58:54 +01:00
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
2020-03-14 23:51:03 +01:00
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
2015-04-02 18:51:02 +02:00
|
|
|
mqtt_message_t *temp_msg = NULL;
|
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
if( lua_istable( L, stack ) ) {
|
|
|
|
NODE_DBG("subscribe table\n");
|
|
|
|
lua_pushnil( L ); /* first key */
|
2015-02-02 10:58:54 +01:00
|
|
|
|
2016-03-10 04:45:20 +01:00
|
|
|
int topic_count = 0;
|
2016-01-27 23:54:19 +01:00
|
|
|
uint8_t overflow = 0;
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
while( lua_next( L, stack ) != 0 ) {
|
|
|
|
topic = luaL_checkstring( L, -2 );
|
|
|
|
qos = luaL_checkinteger( L, -1 );
|
2015-02-02 10:58:54 +01:00
|
|
|
|
2016-03-10 04:45:20 +01:00
|
|
|
if (topic_count == 0) {
|
2020-03-14 23:51:03 +01:00
|
|
|
msg_id = mqtt_next_message_id(mud);
|
|
|
|
temp_msg = mqtt_msg_subscribe_init( &msgb, msg_id );
|
2016-03-10 04:45:20 +01:00
|
|
|
}
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_subscribe_topic( &msgb, topic, qos );
|
2016-03-10 04:45:20 +01:00
|
|
|
topic_count++;
|
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
NODE_DBG("topic: %s - qos: %d, length: %d\n", topic, qos, temp_msg->length);
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2016-03-10 04:45:20 +01:00
|
|
|
if (temp_msg->length == 0) {
|
2015-03-29 18:24:09 +02:00
|
|
|
lua_pop(L, 1);
|
2016-01-27 23:54:19 +01:00
|
|
|
overflow = 1;
|
2015-03-29 18:24:09 +02:00
|
|
|
break; // too long message for the outbuffer.
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
lua_pop( L, 1 );
|
|
|
|
}
|
2015-02-02 10:58:54 +01:00
|
|
|
|
2016-03-10 04:45:20 +01:00
|
|
|
if (topic_count == 0){
|
|
|
|
return luaL_error( L, "no topics found" );
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2016-01-27 23:54:19 +01:00
|
|
|
if (overflow != 0){
|
2016-03-10 04:45:20 +01:00
|
|
|
return luaL_error( L, "buffer overflow, can't enqueue all subscriptions" );
|
|
|
|
}
|
|
|
|
|
2020-03-14 23:51:03 +01:00
|
|
|
temp_msg = mqtt_msg_subscribe_fini( &msgb );
|
2016-03-10 04:45:20 +01:00
|
|
|
if (temp_msg->length == 0) {
|
|
|
|
return luaL_error( L, "buffer overflow, can't enqueue all subscriptions" );
|
2016-01-27 23:54:19 +01:00
|
|
|
}
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
stack++;
|
|
|
|
} else {
|
|
|
|
NODE_DBG("subscribe string\n");
|
|
|
|
topic = luaL_checklstring( L, stack, &il );
|
|
|
|
stack++;
|
|
|
|
if( topic == NULL ){
|
2016-03-10 04:45:20 +01:00
|
|
|
return luaL_error( L, "need topic name" );
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2016-01-27 23:54:19 +01:00
|
|
|
qos = luaL_checkinteger( L, stack );
|
2020-03-14 23:51:03 +01:00
|
|
|
msg_id = mqtt_next_message_id(mud);
|
|
|
|
temp_msg = mqtt_msg_subscribe( &msgb, topic, qos, msg_id );
|
2016-01-27 23:54:19 +01:00
|
|
|
stack++;
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, stack)) { // TODO: this will overwrite the previous one.
|
2015-03-29 18:24:09 +02:00
|
|
|
lua_pushvalue( L, stack ); // copy argument (func) to the top of stack
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref( L, LUA_REGISTRYINDEX, mud->cb_suback_ref );
|
2015-03-29 18:24:09 +02:00
|
|
|
mud->cb_suback_ref = luaL_ref( L, LUA_REGISTRYINDEX );
|
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2015-10-13 07:49:10 +02:00
|
|
|
msg_queue_t *node = msg_enqueue( &(mud->mqtt_state.pending_msg_q), temp_msg,
|
2016-01-27 23:54:19 +01:00
|
|
|
msg_id, MQTT_MSG_TYPE_SUBSCRIBE, (int)mqtt_get_qos(temp_msg->data) );
|
2015-03-29 18:24:09 +02:00
|
|
|
|
|
|
|
NODE_DBG("topic: %s - id: %d - qos: %d, length: %d\n", topic, node->msg_id, node->publish_qos, node->msg.length);
|
2021-01-05 12:07:09 +01:00
|
|
|
NODE_DBG("msg_size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
sint8 espconn_status = ESPCONN_IF;
|
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_status = mqtt_send_if_possible(mud);
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
if(!node || espconn_status != ESPCONN_OK){
|
2015-03-29 18:24:09 +02:00
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
} else {
|
|
|
|
lua_pushboolean(L, 1); // enqueued succeed.
|
|
|
|
}
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("subscribe, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_subscribe.\n");
|
2016-01-27 23:54:19 +01:00
|
|
|
return 1;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2015-03-29 18:24:09 +02:00
|
|
|
// Lua: bool = mqtt:publish( topic, payload, qos, retain, function() )
|
2015-01-23 04:48:05 +01:00
|
|
|
static int mqtt_socket_publish( lua_State* L )
|
|
|
|
{
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("enter mqtt_socket_publish.\n");
|
2015-01-23 04:48:05 +01:00
|
|
|
lmqtt_userdata *mud;
|
|
|
|
size_t l;
|
|
|
|
uint8_t stack = 1;
|
2015-03-29 18:24:09 +02:00
|
|
|
uint16_t msg_id = 0;
|
2016-01-27 23:54:19 +01:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
mud = (lmqtt_userdata *)luaL_checkudata(L, stack, "mqtt.socket");
|
|
|
|
stack++;
|
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
if(!mud->connected){
|
2016-03-10 04:45:20 +01:00
|
|
|
return luaL_error( L, "not connected" );
|
2015-04-02 18:51:02 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
const char *topic = luaL_checklstring( L, stack, &l );
|
|
|
|
stack ++;
|
2015-03-29 18:24:09 +02:00
|
|
|
if (topic == NULL){
|
2016-03-10 04:45:20 +01:00
|
|
|
return luaL_error( L, "need topic" );
|
2015-03-29 18:24:09 +02:00
|
|
|
}
|
2015-01-23 04:48:05 +01:00
|
|
|
|
|
|
|
const char *payload = luaL_checklstring( L, stack, &l );
|
|
|
|
stack ++;
|
|
|
|
uint8_t qos = luaL_checkinteger( L, stack);
|
|
|
|
stack ++;
|
|
|
|
uint8_t retain = luaL_checkinteger( L, stack);
|
|
|
|
stack ++;
|
|
|
|
|
2020-03-14 23:51:03 +01:00
|
|
|
if (qos != 0) {
|
|
|
|
msg_id = mqtt_next_message_id(mud);
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:51:02 +02:00
|
|
|
uint8_t temp_buffer[MQTT_BUF_SIZE];
|
2020-03-14 23:51:03 +01:00
|
|
|
mqtt_message_buffer_t msgb;
|
|
|
|
mqtt_msg_init(&msgb, temp_buffer, MQTT_BUF_SIZE);
|
|
|
|
mqtt_message_t *temp_msg = mqtt_msg_publish(&msgb,
|
2015-01-23 04:48:05 +01:00
|
|
|
topic, payload, l,
|
|
|
|
qos, retain,
|
2020-03-14 23:51:03 +01:00
|
|
|
msg_id);
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
if (lua_isfunction(L, stack)){
|
2015-01-23 04:48:05 +01:00
|
|
|
lua_pushvalue(L, stack); // copy argument (func) to the top of stack
|
2016-03-26 04:35:07 +01:00
|
|
|
luaL_unref(L, LUA_REGISTRYINDEX, mud->cb_puback_ref);
|
2015-01-23 04:48:05 +01:00
|
|
|
mud->cb_puback_ref = luaL_ref(L, LUA_REGISTRYINDEX);
|
|
|
|
}
|
|
|
|
|
2015-10-13 07:49:10 +02:00
|
|
|
msg_queue_t *node = msg_enqueue(&(mud->mqtt_state.pending_msg_q), temp_msg,
|
2015-03-29 18:24:09 +02:00
|
|
|
msg_id, MQTT_MSG_TYPE_PUBLISH, (int)qos );
|
|
|
|
|
2016-02-02 04:29:32 +01:00
|
|
|
sint8 espconn_status = ESPCONN_OK;
|
2016-01-27 23:54:19 +01:00
|
|
|
|
2020-04-07 14:06:27 +02:00
|
|
|
espconn_status = mqtt_send_if_possible(mud);
|
2015-03-29 18:24:09 +02:00
|
|
|
|
2016-01-27 23:54:19 +01:00
|
|
|
if(!node || espconn_status != ESPCONN_OK){
|
2015-03-29 18:24:09 +02:00
|
|
|
lua_pushboolean(L, 0);
|
|
|
|
} else {
|
|
|
|
lua_pushboolean(L, 1); // enqueued succeed.
|
|
|
|
}
|
2015-04-02 18:51:02 +02:00
|
|
|
|
2015-03-31 17:38:28 +02:00
|
|
|
NODE_DBG("publish, queue size: %d\n", msg_size(&(mud->mqtt_state.pending_msg_q)));
|
2015-03-30 18:36:44 +02:00
|
|
|
NODE_DBG("leave mqtt_socket_publish.\n");
|
2015-03-29 18:24:09 +02:00
|
|
|
return 1;
|
2015-01-23 04:48:05 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
// Lua: mqtt:lwt( topic, message, [qos, [retain]])
|
2015-03-30 18:36:44 +02:00
|
|
|
static int mqtt_socket_lwt( lua_State* L )
|
|
|
|
{
|
|
|
|
NODE_DBG("enter mqtt_socket_lwt.\n");
|
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
lmqtt_userdata *mud = luaL_checkudata( L, 1, "mqtt.socket" );
|
|
|
|
luaL_argcheck( L, lua_isstring(L, 2), 2, "need lwt topic");
|
|
|
|
luaL_argcheck( L, lua_isstring(L, 3), 3, "need lwt message");
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_pushvalue(L, 2);
|
|
|
|
luaL_reref(L, LUA_REGISTRYINDEX, &mud->conf.will_topic_ref);
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
lua_pushvalue(L, 3);
|
|
|
|
luaL_reref(L, LUA_REGISTRYINDEX, &mud->conf.will_message_ref);
|
2015-03-30 18:36:44 +02:00
|
|
|
|
2021-01-05 12:07:09 +01:00
|
|
|
int stack = 4;
|
2015-03-30 18:36:44 +02:00
|
|
|
if ( lua_isnumber(L, stack) )
|
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.flags.will_qos = lua_tointeger(L, stack) & 0x3;
|
2015-03-30 18:36:44 +02:00
|
|
|
stack++;
|
|
|
|
}
|
|
|
|
if ( lua_isnumber(L, stack) )
|
|
|
|
{
|
2021-01-05 12:07:09 +01:00
|
|
|
mud->conf.flags.will_retain = !!lua_tointeger(L, stack);
|
2015-03-30 18:36:44 +02:00
|
|
|
stack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
NODE_DBG("leave mqtt_socket_lwt.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
// Module function map
|
2020-04-27 02:13:38 +02:00
|
|
|
|
|
|
|
LROT_BEGIN(mqtt_socket, NULL, LROT_MASK_GC_INDEX)
|
|
|
|
LROT_FUNCENTRY( __gc, mqtt_delete )
|
|
|
|
LROT_TABENTRY( __index, mqtt_socket )
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( connect, mqtt_socket_connect )
|
|
|
|
LROT_FUNCENTRY( close, mqtt_socket_close )
|
|
|
|
LROT_FUNCENTRY( publish, mqtt_socket_publish )
|
|
|
|
LROT_FUNCENTRY( subscribe, mqtt_socket_subscribe )
|
|
|
|
LROT_FUNCENTRY( unsubscribe, mqtt_socket_unsubscribe )
|
|
|
|
LROT_FUNCENTRY( lwt, mqtt_socket_lwt )
|
|
|
|
LROT_FUNCENTRY( on, mqtt_socket_on )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(mqtt_socket, NULL, LROT_MASK_GC_INDEX)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_BEGIN(mqtt, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_FUNCENTRY( Client, mqtt_socket_client )
|
|
|
|
LROT_NUMENTRY( CONN_FAIL_SERVER_NOT_FOUND, MQTT_CONN_FAIL_SERVER_NOT_FOUND )
|
|
|
|
LROT_NUMENTRY( CONN_FAIL_NOT_A_CONNACK_MSG, MQTT_CONN_FAIL_NOT_A_CONNACK_MSG )
|
|
|
|
LROT_NUMENTRY( CONN_FAIL_DNS, MQTT_CONN_FAIL_DNS )
|
|
|
|
LROT_NUMENTRY( CONN_FAIL_TIMEOUT_RECEIVING, MQTT_CONN_FAIL_TIMEOUT_RECEIVING )
|
|
|
|
LROT_NUMENTRY( CONN_FAIL_TIMEOUT_SENDING, MQTT_CONN_FAIL_TIMEOUT_SENDING )
|
|
|
|
LROT_NUMENTRY( CONNACK_ACCEPTED, MQTT_CONNACK_ACCEPTED )
|
|
|
|
LROT_NUMENTRY( CONNACK_REFUSED_PROTOCOL_VER, MQTT_CONNACK_REFUSED_PROTOCOL_VER )
|
|
|
|
LROT_NUMENTRY( CONNACK_REFUSED_ID_REJECTED, MQTT_CONNACK_REFUSED_ID_REJECTED )
|
|
|
|
LROT_NUMENTRY( CONNACK_REFUSED_SERVER_UNAVAILABLE, MQTT_CONNACK_REFUSED_SERVER_UNAVAILABLE )
|
|
|
|
LROT_NUMENTRY( CONNACK_REFUSED_BAD_USER_OR_PASS, MQTT_CONNACK_REFUSED_BAD_USER_OR_PASS )
|
|
|
|
LROT_NUMENTRY( CONNACK_REFUSED_NOT_AUTHORIZED, MQTT_CONNACK_REFUSED_NOT_AUTHORIZED )
|
2020-04-27 02:13:38 +02:00
|
|
|
LROT_END(mqtt, NULL, 0)
|
2019-05-08 13:08:20 +02:00
|
|
|
|
2015-01-23 04:48:05 +01:00
|
|
|
|
2015-12-16 06:04:58 +01:00
|
|
|
int luaopen_mqtt( lua_State *L )
|
2015-01-23 04:48:05 +01:00
|
|
|
{
|
2019-05-08 13:08:20 +02:00
|
|
|
luaL_rometatable(L, "mqtt.socket", LROT_TABLEREF(mqtt_socket));
|
2015-01-23 04:48:05 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-12-16 06:04:58 +01:00
|
|
|
|
2019-05-08 13:08:20 +02:00
|
|
|
NODEMCU_MODULE(MQTT, "mqtt", mqtt, luaopen_mqtt);
|