kopia lustrzana https://github.com/martin-ger/esp_mqtt
Merge pull request #92 from vowstar/master
Adjust disconnect add timeout callback in MQTT module.develop
commit
a5f072bcb1
|
@ -1,14 +1,15 @@
|
||||||
language: cpp
|
language: cpp
|
||||||
before_install:
|
before_install:
|
||||||
|
- sudo apt-get -qq update
|
||||||
- sudo apt-get install -y python-serial srecord
|
- sudo apt-get install -y python-serial srecord
|
||||||
install:
|
install:
|
||||||
- wget https://github.com/GeorgeHahn/nodemcu-firmware/raw/travis/tools/esp-open-sdk.tar.gz -O tools/esp-open-sdk.tar.gz
|
- wget https://github.com/GeorgeHahn/nodemcu-firmware/raw/travis/tools/esp-open-sdk.tar.gz -O tools/esp-open-sdk.tar.gz
|
||||||
- tar -zxvf tools/esp-open-sdk.tar.gz
|
- tar -zxvf tools/esp-open-sdk.tar.gz
|
||||||
- export PATH=$PATH:$PWD/esp-open-sdk/sdk:$PWD/esp-open-sdk/xtensa-lx106-elf/bin
|
- export PATH=$PATH:$PWD/esp-open-sdk/sdk:$PWD/esp-open-sdk/xtensa-lx106-elf/bin
|
||||||
- wget http://bbs.espressif.com/download/file.php?id=189 -O tools/esp_iot_sdk_v0.9.5_15_01_23.zip
|
- wget http://bbs.espressif.com/download/file.php?id=1046 -O tools/esp_iot_sdk_v1.5.1.zip
|
||||||
- unzip tools/esp_iot_sdk_v0.9.5_15_01_23.zip
|
- unzip tools/esp_iot_sdk_v1.5.1.zip
|
||||||
script:
|
script:
|
||||||
- make all SDK_BASE="$PWD/esp_iot_sdk_v0.9.5"
|
- make all SDK_BASE="$PWD/esp_iot_sdk_v1.5.1"
|
||||||
- cd firmware/
|
- cd firmware/
|
||||||
- file_name="esp_mqtt_v${TRAVIS_TAG}.${TRAVIS_BUILD_NUMBER}.bin"
|
- file_name="esp_mqtt_v${TRAVIS_TAG}.${TRAVIS_BUILD_NUMBER}.bin"
|
||||||
- srec_cat -output ${file_name} -binary 0x00000.bin -binary -fill 0xff 0x00000 0x40000 0x40000.bin -binary -offset 0x40000
|
- srec_cat -output ${file_name} -binary 0x00000.bin -binary -fill 0xff 0x00000 0x40000 0x40000.bin -binary -offset 0x40000
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -101,7 +101,7 @@ MODULES = driver mqtt user modules
|
||||||
EXTRA_INCDIR = include $(SDK_BASE)/../include
|
EXTRA_INCDIR = include $(SDK_BASE)/../include
|
||||||
|
|
||||||
# libraries used in this project, mainly provided by the SDK
|
# libraries used in this project, mainly provided by the SDK
|
||||||
LIBS = c gcc hal phy pp net80211 lwip wpa main ssl
|
LIBS = c gcc hal phy pp net80211 lwip wpa main ssl crypto
|
||||||
|
|
||||||
# compiler flags using during compilation of source files
|
# compiler flags using during compilation of source files
|
||||||
CFLAGS = -Os -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH
|
CFLAGS = -Os -Wpointer-arith -Wundef -Werror -Wl,-EL -fno-inline-functions -nostdlib -mlongcalls -mtext-section-literals -D__ets__ -DICACHE_FLASH
|
||||||
|
|
|
@ -0,0 +1,31 @@
|
||||||
|
#ifndef __MQTT_CONFIG_H__
|
||||||
|
#define __MQTT_CONFIG_H__
|
||||||
|
|
||||||
|
#define CFG_HOLDER 0x00FF55A4 /* Change this value to load default configurations */
|
||||||
|
#define CFG_LOCATION 0x3C /* Please don't change or if you know what you doing */
|
||||||
|
#define MQTT_SSL_ENABLE
|
||||||
|
|
||||||
|
/*DEFAULT CONFIGURATIONS*/
|
||||||
|
|
||||||
|
#define MQTT_HOST "192.168.1.100" //or "mqtt.yourdomain.com"
|
||||||
|
#define MQTT_PORT 1883
|
||||||
|
#define MQTT_BUF_SIZE 1024
|
||||||
|
#define MQTT_KEEPALIVE 120 /*second*/
|
||||||
|
|
||||||
|
#define MQTT_CLIENT_ID "DVES_%08X"
|
||||||
|
#define MQTT_USER "DVES_USER"
|
||||||
|
#define MQTT_PASS "DVES_PASS"
|
||||||
|
|
||||||
|
#define STA_SSID "DVES_HOME"
|
||||||
|
#define STA_PASS "yourpassword"
|
||||||
|
#define STA_TYPE AUTH_WPA2_PSK
|
||||||
|
|
||||||
|
#define MQTT_RECONNECT_TIMEOUT 5 /*second*/
|
||||||
|
|
||||||
|
#define DEFAULT_SECURITY 0
|
||||||
|
#define QUEUE_BUFFER_SIZE 2048
|
||||||
|
|
||||||
|
#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto v0.15*/
|
||||||
|
//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/
|
||||||
|
|
||||||
|
#endif // __MQTT_CONFIG_H__
|
|
@ -1,30 +1,7 @@
|
||||||
#ifndef _USER_CONFIG_H_
|
#ifndef __USER_CONFIG_H__
|
||||||
#define _USER_CONFIG_H_
|
#define __USER_CONFIG_H__
|
||||||
|
|
||||||
#define CFG_HOLDER 0x00FF55A4 /* Change this value to load default configurations */
|
#define USE_OPTIMIZE_PRINTF
|
||||||
#define CFG_LOCATION 0x3C /* Please don't change or if you know what you doing */
|
|
||||||
#define CLIENT_SSL_ENABLE
|
|
||||||
|
|
||||||
/*DEFAULT CONFIGURATIONS*/
|
|
||||||
|
|
||||||
#define MQTT_HOST "192.168.11.122" //or "mqtt.yourdomain.com"
|
|
||||||
#define MQTT_PORT 1880
|
|
||||||
#define MQTT_BUF_SIZE 1024
|
|
||||||
#define MQTT_KEEPALIVE 120 /*second*/
|
|
||||||
|
|
||||||
#define MQTT_CLIENT_ID "DVES_%08X"
|
|
||||||
#define MQTT_USER "DVES_USER"
|
|
||||||
#define MQTT_PASS "DVES_PASS"
|
|
||||||
|
|
||||||
#define STA_SSID "DVES_HOME"
|
|
||||||
#define STA_PASS "yourpassword"
|
|
||||||
#define STA_TYPE AUTH_WPA2_PSK
|
|
||||||
|
|
||||||
#define MQTT_RECONNECT_TIMEOUT 5 /*second*/
|
|
||||||
|
|
||||||
#define DEFAULT_SECURITY 0
|
|
||||||
#define QUEUE_BUFFER_SIZE 2048
|
|
||||||
|
|
||||||
#define PROTOCOL_NAMEv31 /*MQTT version 3.1 compatible with Mosquitto v0.15*/
|
|
||||||
//PROTOCOL_NAMEv311 /*MQTT version 3.11 compatible with https://eclipse.org/paho/clients/testing/*/
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,16 @@
|
||||||
#ifndef USER_DEBUG_H_
|
#ifndef USER_DEBUG_H_
|
||||||
#define USER_DEBUG_H_
|
#define USER_DEBUG_H_
|
||||||
|
|
||||||
#ifndef INFO
|
#if defined(GLOBAL_DEBUG_ON)
|
||||||
#define INFO os_printf
|
#define MQTT_DEBUG_ON
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(MQTT_DEBUG_ON)
|
||||||
|
#define INFO( format, ... ) os_printf( format, ## __VA_ARGS__ )
|
||||||
|
#else
|
||||||
|
#define INFO( format, ... )
|
||||||
|
#endif
|
||||||
|
// #ifndef INFO
|
||||||
|
// #define INFO os_printf
|
||||||
|
// #endif
|
||||||
|
|
||||||
#endif /* USER_DEBUG_H_ */
|
#endif /* USER_DEBUG_H_ */
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
*/
|
*/
|
||||||
#ifndef USER_AT_MQTT_H_
|
#ifndef USER_AT_MQTT_H_
|
||||||
#define USER_AT_MQTT_H_
|
#define USER_AT_MQTT_H_
|
||||||
|
#include "mqtt_config.h"
|
||||||
#include "mqtt_msg.h"
|
#include "mqtt_msg.h"
|
||||||
#include "user_interface.h"
|
#include "user_interface.h"
|
||||||
|
|
||||||
|
@ -67,6 +68,7 @@ typedef enum {
|
||||||
WIFI_CONNECTING_ERROR,
|
WIFI_CONNECTING_ERROR,
|
||||||
WIFI_CONNECTED,
|
WIFI_CONNECTED,
|
||||||
DNS_RESOLVE,
|
DNS_RESOLVE,
|
||||||
|
TCP_DISCONNECTING,
|
||||||
TCP_DISCONNECTED,
|
TCP_DISCONNECTED,
|
||||||
TCP_RECONNECT_REQ,
|
TCP_RECONNECT_REQ,
|
||||||
TCP_RECONNECT,
|
TCP_RECONNECT,
|
||||||
|
@ -79,7 +81,9 @@ typedef enum {
|
||||||
MQTT_SUBSCIBE_SENDING,
|
MQTT_SUBSCIBE_SENDING,
|
||||||
MQTT_DATA,
|
MQTT_DATA,
|
||||||
MQTT_PUBLISH_RECV,
|
MQTT_PUBLISH_RECV,
|
||||||
MQTT_PUBLISHING
|
MQTT_PUBLISHING,
|
||||||
|
MQTT_DELETING,
|
||||||
|
MQTT_DELETED,
|
||||||
} tConnState;
|
} tConnState;
|
||||||
|
|
||||||
typedef void (*MqttCallback)(uint32_t *args);
|
typedef void (*MqttCallback)(uint32_t *args);
|
||||||
|
@ -96,6 +100,7 @@ typedef struct {
|
||||||
MqttCallback connectedCb;
|
MqttCallback connectedCb;
|
||||||
MqttCallback disconnectedCb;
|
MqttCallback disconnectedCb;
|
||||||
MqttCallback publishedCb;
|
MqttCallback publishedCb;
|
||||||
|
MqttCallback timeoutCb;
|
||||||
MqttDataCallback dataCb;
|
MqttDataCallback dataCb;
|
||||||
ETSTimer mqttTimer;
|
ETSTimer mqttTimer;
|
||||||
uint32_t keepAliveTick;
|
uint32_t keepAliveTick;
|
||||||
|
@ -123,12 +128,14 @@ typedef struct {
|
||||||
#define MQTT_EVENT_TYPE_EXITED 7
|
#define MQTT_EVENT_TYPE_EXITED 7
|
||||||
#define MQTT_EVENT_TYPE_PUBLISH_CONTINUATION 8
|
#define MQTT_EVENT_TYPE_PUBLISH_CONTINUATION 8
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32 port, uint8_t security);
|
void ICACHE_FLASH_ATTR MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32_t port, uint8_t security);
|
||||||
void ICACHE_FLASH_ATTR MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession);
|
void ICACHE_FLASH_ATTR MQTT_InitClient(MQTT_Client *mqttClient, uint8_t* client_id, uint8_t* client_user, uint8_t* client_pass, uint32_t keepAliveTime, uint8_t cleanSession);
|
||||||
|
void ICACHE_FLASH_ATTR MQTT_DeleteClient(MQTT_Client *mqttClient);
|
||||||
void ICACHE_FLASH_ATTR MQTT_InitLWT(MQTT_Client *mqttClient, uint8_t* will_topic, uint8_t* will_msg, uint8_t will_qos, uint8_t will_retain);
|
void ICACHE_FLASH_ATTR MQTT_InitLWT(MQTT_Client *mqttClient, uint8_t* will_topic, uint8_t* will_msg, uint8_t will_qos, uint8_t will_retain);
|
||||||
void ICACHE_FLASH_ATTR MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb);
|
void ICACHE_FLASH_ATTR MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb);
|
||||||
void ICACHE_FLASH_ATTR MQTT_OnDisconnected(MQTT_Client *mqttClient, MqttCallback disconnectedCb);
|
void ICACHE_FLASH_ATTR MQTT_OnDisconnected(MQTT_Client *mqttClient, MqttCallback disconnectedCb);
|
||||||
void ICACHE_FLASH_ATTR MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb);
|
void ICACHE_FLASH_ATTR MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb);
|
||||||
|
void ICACHE_FLASH_ATTR MQTT_OnTimeout(MQTT_Client *mqttClient, MqttCallback timeoutCb);
|
||||||
void ICACHE_FLASH_ATTR MQTT_OnData(MQTT_Client *mqttClient, MqttDataCallback dataCb);
|
void ICACHE_FLASH_ATTR MQTT_OnData(MQTT_Client *mqttClient, MqttDataCallback dataCb);
|
||||||
BOOL ICACHE_FLASH_ATTR MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos);
|
BOOL ICACHE_FLASH_ATTR MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos);
|
||||||
void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient);
|
void ICACHE_FLASH_ATTR MQTT_Connect(MQTT_Client *mqttClient);
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
#ifndef MQTT_MSG_H
|
#ifndef MQTT_MSG_H
|
||||||
#define MQTT_MSG_H
|
#define MQTT_MSG_H
|
||||||
|
#include "mqtt_config.h"
|
||||||
#include "c_types.h"
|
#include "c_types.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
210
mqtt/mqtt.c
210
mqtt/mqtt.c
|
@ -40,7 +40,7 @@
|
||||||
#include "mqtt.h"
|
#include "mqtt.h"
|
||||||
#include "queue.h"
|
#include "queue.h"
|
||||||
|
|
||||||
#define MQTT_TASK_PRIO 0
|
#define MQTT_TASK_PRIO 2
|
||||||
#define MQTT_TASK_QUEUE_SIZE 1
|
#define MQTT_TASK_QUEUE_SIZE 1
|
||||||
#define MQTT_SEND_TIMOUT 5
|
#define MQTT_SEND_TIMOUT 5
|
||||||
|
|
||||||
|
@ -79,7 +79,11 @@ mqtt_dns_found(const char *name, ip_addr_t *ipaddr, void *arg)
|
||||||
{
|
{
|
||||||
os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4);
|
os_memcpy(client->pCon->proto.tcp->remote_ip, &ipaddr->addr, 4);
|
||||||
if (client->security) {
|
if (client->security) {
|
||||||
|
#ifdef MQTT_SSL_ENABLE
|
||||||
espconn_secure_connect(client->pCon);
|
espconn_secure_connect(client->pCon);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
espconn_connect(client->pCon);
|
espconn_connect(client->pCon);
|
||||||
|
@ -109,6 +113,79 @@ deliver_publish(MQTT_Client* client, uint8_t* message, int length)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Delete tcp client and free all memory
|
||||||
|
* @param mqttClient: The mqtt client which contain TCP client
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
void ICACHE_FLASH_ATTR
|
||||||
|
mqtt_tcpclient_delete(MQTT_Client *mqttClient)
|
||||||
|
{
|
||||||
|
if (mqttClient->pCon != NULL) {
|
||||||
|
INFO("Free memory\r\n");
|
||||||
|
espconn_delete(mqttClient->pCon);
|
||||||
|
if (mqttClient->pCon->proto.tcp)
|
||||||
|
os_free(mqttClient->pCon->proto.tcp);
|
||||||
|
os_free(mqttClient->pCon);
|
||||||
|
mqttClient->pCon = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Delete MQTT client and free all memory
|
||||||
|
* @param mqttClient: The mqtt client
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
void ICACHE_FLASH_ATTR
|
||||||
|
mqtt_client_delete(MQTT_Client *mqttClient)
|
||||||
|
{
|
||||||
|
mqtt_tcpclient_delete(mqttClient);
|
||||||
|
if (mqttClient->host != NULL) {
|
||||||
|
os_free(mqttClient->host);
|
||||||
|
mqttClient->host = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mqttClient->user_data != NULL) {
|
||||||
|
os_free(mqttClient->user_data);
|
||||||
|
mqttClient->user_data = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->connect_info.client_id != NULL) {
|
||||||
|
os_free(mqttClient->connect_info.client_id);
|
||||||
|
mqttClient->connect_info.client_id = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->connect_info.username != NULL) {
|
||||||
|
os_free(mqttClient->connect_info.username);
|
||||||
|
mqttClient->connect_info.username = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->connect_info.password != NULL) {
|
||||||
|
os_free(mqttClient->connect_info.password);
|
||||||
|
mqttClient->connect_info.password = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->connect_info.will_topic != NULL) {
|
||||||
|
os_free(mqttClient->connect_info.will_topic);
|
||||||
|
mqttClient->connect_info.will_topic = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->connect_info.will_message != NULL) {
|
||||||
|
os_free(mqttClient->connect_info.will_message);
|
||||||
|
mqttClient->connect_info.will_message = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->mqtt_state.in_buffer != NULL) {
|
||||||
|
os_free(mqttClient->mqtt_state.in_buffer);
|
||||||
|
mqttClient->mqtt_state.in_buffer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mqttClient->mqtt_state.out_buffer != NULL) {
|
||||||
|
os_free(mqttClient->mqtt_state.out_buffer);
|
||||||
|
mqttClient->mqtt_state.out_buffer = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Client received callback function.
|
* @brief Client received callback function.
|
||||||
|
@ -141,7 +218,11 @@ READPACKET:
|
||||||
if (client->mqtt_state.pending_msg_type != MQTT_MSG_TYPE_CONNECT) {
|
if (client->mqtt_state.pending_msg_type != MQTT_MSG_TYPE_CONNECT) {
|
||||||
INFO("MQTT: Invalid packet\r\n");
|
INFO("MQTT: Invalid packet\r\n");
|
||||||
if (client->security) {
|
if (client->security) {
|
||||||
|
#ifdef MQTT_SSL_ENABLE
|
||||||
espconn_secure_disconnect(client->pCon);
|
espconn_secure_disconnect(client->pCon);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
espconn_disconnect(client->pCon);
|
espconn_disconnect(client->pCon);
|
||||||
|
@ -281,18 +362,28 @@ void ICACHE_FLASH_ATTR mqtt_timer(void *arg)
|
||||||
|
|
||||||
client->sendTimeout = MQTT_SEND_TIMOUT;
|
client->sendTimeout = MQTT_SEND_TIMOUT;
|
||||||
INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
|
INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
|
||||||
|
err_t result = ESPCONN_OK;
|
||||||
if (client->security) {
|
if (client->security) {
|
||||||
espconn_secure_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
#ifdef MQTT_SSL_ENABLE
|
||||||
|
result = espconn_secure_send(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
espconn_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
result = espconn_send(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
||||||
}
|
}
|
||||||
|
|
||||||
client->mqtt_state.outbound_message = NULL;
|
client->mqtt_state.outbound_message = NULL;
|
||||||
|
|
||||||
|
if(ESPCONN_OK == result) {
|
||||||
client->keepAliveTick = 0;
|
client->keepAliveTick = 0;
|
||||||
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
client->connState = TCP_RECONNECT_REQ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} else if (client->connState == TCP_RECONNECT_REQ) {
|
} else if (client->connState == TCP_RECONNECT_REQ) {
|
||||||
client->reconnectTick ++;
|
client->reconnectTick ++;
|
||||||
|
@ -300,6 +391,8 @@ void ICACHE_FLASH_ATTR mqtt_timer(void *arg)
|
||||||
client->reconnectTick = 0;
|
client->reconnectTick = 0;
|
||||||
client->connState = TCP_RECONNECT;
|
client->connState = TCP_RECONNECT;
|
||||||
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
||||||
|
if (client->timeoutCb)
|
||||||
|
client->timeoutCb((uint32_t*)client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (client->sendTimeout > 0)
|
if (client->sendTimeout > 0)
|
||||||
|
@ -313,7 +406,15 @@ mqtt_tcpclient_discon_cb(void *arg)
|
||||||
struct espconn *pespconn = (struct espconn *)arg;
|
struct espconn *pespconn = (struct espconn *)arg;
|
||||||
MQTT_Client* client = (MQTT_Client *)pespconn->reverse;
|
MQTT_Client* client = (MQTT_Client *)pespconn->reverse;
|
||||||
INFO("TCP: Disconnected callback\r\n");
|
INFO("TCP: Disconnected callback\r\n");
|
||||||
|
if(TCP_DISCONNECTING == client->connState) {
|
||||||
|
client->connState = TCP_DISCONNECTED;
|
||||||
|
}
|
||||||
|
else if(MQTT_DELETING == client->connState) {
|
||||||
|
client->connState = MQTT_DELETED;
|
||||||
|
}
|
||||||
|
else {
|
||||||
client->connState = TCP_RECONNECT_REQ;
|
client->connState = TCP_RECONNECT_REQ;
|
||||||
|
}
|
||||||
if (client->disconnectedCb)
|
if (client->disconnectedCb)
|
||||||
client->disconnectedCb((uint32_t*)client);
|
client->disconnectedCb((uint32_t*)client);
|
||||||
|
|
||||||
|
@ -347,10 +448,14 @@ mqtt_tcpclient_connect_cb(void *arg)
|
||||||
client->sendTimeout = MQTT_SEND_TIMOUT;
|
client->sendTimeout = MQTT_SEND_TIMOUT;
|
||||||
INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
|
INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
|
||||||
if (client->security) {
|
if (client->security) {
|
||||||
espconn_secure_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
#ifdef MQTT_SSL_ENABLE
|
||||||
|
espconn_secure_send(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
espconn_sent(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
espconn_send(client->pCon, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length);
|
||||||
}
|
}
|
||||||
|
|
||||||
client->mqtt_state.outbound_message = NULL;
|
client->mqtt_state.outbound_message = NULL;
|
||||||
|
@ -426,7 +531,7 @@ MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos)
|
||||||
uint16_t dataLen;
|
uint16_t dataLen;
|
||||||
|
|
||||||
client->mqtt_state.outbound_message = mqtt_msg_subscribe(&client->mqtt_state.mqtt_connection,
|
client->mqtt_state.outbound_message = mqtt_msg_subscribe(&client->mqtt_state.mqtt_connection,
|
||||||
topic, 0,
|
topic, qos,
|
||||||
&client->mqtt_state.pending_msg_id);
|
&client->mqtt_state.pending_msg_id);
|
||||||
INFO("MQTT: queue subscribe, topic\"%s\", id: %d\r\n", topic, client->mqtt_state.pending_msg_id);
|
INFO("MQTT: queue subscribe, topic\"%s\", id: %d\r\n", topic, client->mqtt_state.pending_msg_id);
|
||||||
while (QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1) {
|
while (QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1) {
|
||||||
|
@ -440,6 +545,33 @@ MQTT_Subscribe(MQTT_Client *client, char* topic, uint8_t qos)
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief MQTT ping function.
|
||||||
|
* @param client: MQTT_Client reference
|
||||||
|
* @retval TRUE if success queue
|
||||||
|
*/
|
||||||
|
BOOL ICACHE_FLASH_ATTR
|
||||||
|
MQTT_Ping(MQTT_Client *client)
|
||||||
|
{
|
||||||
|
uint8_t dataBuffer[MQTT_BUF_SIZE];
|
||||||
|
uint16_t dataLen;
|
||||||
|
client->mqtt_state.outbound_message = mqtt_msg_pingreq(&client->mqtt_state.mqtt_connection);
|
||||||
|
if(client->mqtt_state.outbound_message->length == 0){
|
||||||
|
INFO("MQTT: Queuing publish failed\r\n");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
INFO("MQTT: queuing publish, length: %d, queue size(%d/%d)\r\n", client->mqtt_state.outbound_message->length, client->msgQueue.rb.fill_cnt, client->msgQueue.rb.size);
|
||||||
|
while(QUEUE_Puts(&client->msgQueue, client->mqtt_state.outbound_message->data, client->mqtt_state.outbound_message->length) == -1){
|
||||||
|
INFO("MQTT: Queue full\r\n");
|
||||||
|
if(QUEUE_Gets(&client->msgQueue, dataBuffer, &dataLen, MQTT_BUF_SIZE) == -1) {
|
||||||
|
INFO("MQTT: Serious buffer error\r\n");
|
||||||
|
return FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)client);
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR
|
void ICACHE_FLASH_ATTR
|
||||||
MQTT_Task(os_event_t *e)
|
MQTT_Task(os_event_t *e)
|
||||||
{
|
{
|
||||||
|
@ -457,6 +589,27 @@ MQTT_Task(os_event_t *e)
|
||||||
INFO("TCP: Reconnect to: %s:%d\r\n", client->host, client->port);
|
INFO("TCP: Reconnect to: %s:%d\r\n", client->host, client->port);
|
||||||
client->connState = TCP_CONNECTING;
|
client->connState = TCP_CONNECTING;
|
||||||
break;
|
break;
|
||||||
|
case MQTT_DELETING:
|
||||||
|
case TCP_DISCONNECTING:
|
||||||
|
if (client->security) {
|
||||||
|
#ifdef MQTT_SSL_ENABLE
|
||||||
|
espconn_secure_connect(client->pCon);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
espconn_disconnect(client->pCon);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case TCP_DISCONNECTED:
|
||||||
|
INFO("MQTT: Disconnected\r\n");
|
||||||
|
mqtt_tcpclient_delete(client);
|
||||||
|
break;
|
||||||
|
case MQTT_DELETED:
|
||||||
|
INFO("MQTT: Deleted client\r\n");
|
||||||
|
mqtt_client_delete(client);
|
||||||
|
break;
|
||||||
case MQTT_DATA:
|
case MQTT_DATA:
|
||||||
if (QUEUE_IsEmpty(&client->msgQueue) || client->sendTimeout != 0) {
|
if (QUEUE_IsEmpty(&client->msgQueue) || client->sendTimeout != 0) {
|
||||||
break;
|
break;
|
||||||
|
@ -469,10 +622,14 @@ MQTT_Task(os_event_t *e)
|
||||||
client->sendTimeout = MQTT_SEND_TIMOUT;
|
client->sendTimeout = MQTT_SEND_TIMOUT;
|
||||||
INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
|
INFO("MQTT: Sending, type: %d, id: %04X\r\n", client->mqtt_state.pending_msg_type, client->mqtt_state.pending_msg_id);
|
||||||
if (client->security) {
|
if (client->security) {
|
||||||
espconn_secure_sent(client->pCon, dataBuffer, dataLen);
|
#ifdef MQTT_SSL_ENABLE
|
||||||
|
espconn_secure_send(client->pCon, dataBuffer, dataLen);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
espconn_sent(client->pCon, dataBuffer, dataLen);
|
espconn_send(client->pCon, dataBuffer, dataLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
client->mqtt_state.outbound_message = NULL;
|
client->mqtt_state.outbound_message = NULL;
|
||||||
|
@ -491,7 +648,7 @@ MQTT_Task(os_event_t *e)
|
||||||
* @retval None
|
* @retval None
|
||||||
*/
|
*/
|
||||||
void ICACHE_FLASH_ATTR
|
void ICACHE_FLASH_ATTR
|
||||||
MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32 port, uint8_t security)
|
MQTT_InitConnection(MQTT_Client *mqttClient, uint8_t* host, uint32_t port, uint8_t security)
|
||||||
{
|
{
|
||||||
uint32_t temp;
|
uint32_t temp;
|
||||||
INFO("MQTT_InitConnection\r\n");
|
INFO("MQTT_InitConnection\r\n");
|
||||||
|
@ -601,10 +758,16 @@ MQTT_Connect(MQTT_Client *mqttClient)
|
||||||
|
|
||||||
if (UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) {
|
if (UTILS_StrToIP(mqttClient->host, &mqttClient->pCon->proto.tcp->remote_ip)) {
|
||||||
INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port);
|
INFO("TCP: Connect to ip %s:%d\r\n", mqttClient->host, mqttClient->port);
|
||||||
if(mqttClient->security){
|
if (mqttClient->security)
|
||||||
|
{
|
||||||
|
#ifdef MQTT_SSL_ENABLE
|
||||||
espconn_secure_connect(mqttClient->pCon);
|
espconn_secure_connect(mqttClient->pCon);
|
||||||
|
#else
|
||||||
|
INFO("TCP: Do not support SSL\r\n");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
espconn_connect(mqttClient->pCon);
|
espconn_connect(mqttClient->pCon);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -618,16 +781,19 @@ MQTT_Connect(MQTT_Client *mqttClient)
|
||||||
void ICACHE_FLASH_ATTR
|
void ICACHE_FLASH_ATTR
|
||||||
MQTT_Disconnect(MQTT_Client *mqttClient)
|
MQTT_Disconnect(MQTT_Client *mqttClient)
|
||||||
{
|
{
|
||||||
if(mqttClient->pCon){
|
mqttClient->connState = TCP_DISCONNECTING;
|
||||||
INFO("Free memory\r\n");
|
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)mqttClient);
|
||||||
if(mqttClient->pCon->proto.tcp)
|
|
||||||
os_free(mqttClient->pCon->proto.tcp);
|
|
||||||
os_free(mqttClient->pCon);
|
|
||||||
mqttClient->pCon = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
os_timer_disarm(&mqttClient->mqttTimer);
|
os_timer_disarm(&mqttClient->mqttTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ICACHE_FLASH_ATTR
|
||||||
|
MQTT_DeleteClient(MQTT_Client *mqttClient)
|
||||||
|
{
|
||||||
|
mqttClient->connState = MQTT_DELETING;
|
||||||
|
system_os_post(MQTT_TASK_PRIO, 0, (os_param_t)mqttClient);
|
||||||
|
os_timer_disarm(&mqttClient->mqttTimer);
|
||||||
|
}
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR
|
void ICACHE_FLASH_ATTR
|
||||||
MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb)
|
MQTT_OnConnected(MQTT_Client *mqttClient, MqttCallback connectedCb)
|
||||||
{
|
{
|
||||||
|
@ -651,3 +817,9 @@ MQTT_OnPublished(MQTT_Client *mqttClient, MqttCallback publishedCb)
|
||||||
{
|
{
|
||||||
mqttClient->publishedCb = publishedCb;
|
mqttClient->publishedCb = publishedCb;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ICACHE_FLASH_ATTR
|
||||||
|
MQTT_OnTimeout(MQTT_Client *mqttClient, MqttCallback timeoutCb)
|
||||||
|
{
|
||||||
|
mqttClient->timeoutCb = timeoutCb;
|
||||||
|
}
|
|
@ -127,7 +127,7 @@ static mqtt_message_t* ICACHE_FLASH_ATTR fini_message(mqtt_connection_t* connect
|
||||||
|
|
||||||
void ICACHE_FLASH_ATTR mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length)
|
void ICACHE_FLASH_ATTR mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length)
|
||||||
{
|
{
|
||||||
memset(connection, 0, sizeof(connection));
|
memset(connection, 0, sizeof(mqtt_connection_t));
|
||||||
connection->buffer = buffer;
|
connection->buffer = buffer;
|
||||||
connection->buffer_length = buffer_length;
|
connection->buffer_length = buffer_length;
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue