merge mqtt branch to master and build pre_build bin
This commit is contained in:
parent
0ea9cb9c01
commit
f676825872
46
README.md
46
README.md
|
@ -9,8 +9,8 @@ Flash tool for NodeMCU [nodemcu-flasher](https://github.com/nodemcu/nodemcu-flas
|
||||||
|
|
||||||
wiki: [nodemcu wiki](https://github.com/nodemcu/nodemcu-firmware/wiki)<br />
|
wiki: [nodemcu wiki](https://github.com/nodemcu/nodemcu-firmware/wiki)<br />
|
||||||
home: [nodemcu.com](http://www.nodemcu.com)<br />
|
home: [nodemcu.com](http://www.nodemcu.com)<br />
|
||||||
bbs: [中文论坛Chinese bbs](http://bbs.nodemcu.com)<br />
|
bbs: [Chinese bbs](http://bbs.nodemcu.com)<br />
|
||||||
Tencent QQ group QQ群: 309957875<br />
|
Tencent QQ group: 309957875<br />
|
||||||
|
|
||||||
# Summary
|
# Summary
|
||||||
- Easy to access wireless router
|
- Easy to access wireless router
|
||||||
|
@ -26,6 +26,10 @@ Tencent QQ group QQ群: 309957875<br />
|
||||||
- add coap module
|
- add coap module
|
||||||
|
|
||||||
# Change log
|
# Change log
|
||||||
|
2015-01-23<br />
|
||||||
|
merge mqtt branch to master.<br />
|
||||||
|
build pre_build bin.
|
||||||
|
|
||||||
2015-01-18<br />
|
2015-01-18<br />
|
||||||
merge mqtt module to [new branch mqtt](https://github.com/nodemcu/nodemcu-firmware/tree/mqtt) from [https://github.com/tuanpmt/esp_mqtt](https://github.com/tuanpmt/esp_mqtt).<br />
|
merge mqtt module to [new branch mqtt](https://github.com/nodemcu/nodemcu-firmware/tree/mqtt) from [https://github.com/tuanpmt/esp_mqtt](https://github.com/tuanpmt/esp_mqtt).<br />
|
||||||
merge spi module from iabdalkader:spi. <br />
|
merge spi module from iabdalkader:spi. <br />
|
||||||
|
@ -40,7 +44,7 @@ fix file.read() api, take 0xFF as a regular byte, not EOF.<br />
|
||||||
pre_build/latest/nodemcu_512k_latest.bin is removed. use pre_build/latest/nodemcu_latest.bin instead.
|
pre_build/latest/nodemcu_512k_latest.bin is removed. use pre_build/latest/nodemcu_latest.bin instead.
|
||||||
|
|
||||||
[more change log](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#change_log)<br />
|
[more change log](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en#change_log)<br />
|
||||||
[更多变更日志](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn#change_log)
|
[more change_log cn](https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_cn#change_log)
|
||||||
|
|
||||||
##GPIO NEW TABLE ( Build 20141219 and later)
|
##GPIO NEW TABLE ( Build 20141219 and later)
|
||||||
|
|
||||||
|
@ -186,6 +190,42 @@ baudrate:9600
|
||||||
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
|
.."Connection: keep-alive\r\nAccept: */*\r\n\r\n")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
####Connect to MQTT Broker
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- init mqtt client with keepalive timer 120sec
|
||||||
|
m = mqtt.Client("clientid", 120, "user", "password")
|
||||||
|
|
||||||
|
-- setup Last Will and Testament (optional)
|
||||||
|
-- Broker will publish a message with qos = 0, retain = 0, data = "offline"
|
||||||
|
-- to topic "/lwt" if client don't send keepalive packet
|
||||||
|
m:lwt("/lwt", "offline", 0, 0)
|
||||||
|
|
||||||
|
m:on("connect", function(con) print ("connected") end)
|
||||||
|
m:on("offline", function(con) print ("offline") end)
|
||||||
|
|
||||||
|
-- on publish message receive event
|
||||||
|
m:on("message", function(conn, topic, data)
|
||||||
|
print(topic .. ":" )
|
||||||
|
if data ~= nil then
|
||||||
|
print(data)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- for secure: m:connect("192.168.11.118", 1880, 1)
|
||||||
|
m:connect("192.168.11.118", 1880, 0, function(conn) print("connected") end)
|
||||||
|
|
||||||
|
-- subscribe topic with qos = 0
|
||||||
|
m:subscribe("/topic",0, function(conn) print("subscribe success") end)
|
||||||
|
|
||||||
|
-- publish a message with data = hello, QoS = 0, retain = 0
|
||||||
|
m:publish("/topic","hello",0,0, function(conn) print("sent") end)
|
||||||
|
|
||||||
|
m:close();
|
||||||
|
-- you can call m:connect again
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
####Or a simple http server
|
####Or a simple http server
|
||||||
|
|
||||||
```lua
|
```lua
|
||||||
|
|
|
@ -31,6 +31,7 @@ SUBDIRS= \
|
||||||
platform \
|
platform \
|
||||||
libc \
|
libc \
|
||||||
lua \
|
lua \
|
||||||
|
mqtt \
|
||||||
smart \
|
smart \
|
||||||
wofs \
|
wofs \
|
||||||
modules \
|
modules \
|
||||||
|
@ -77,6 +78,7 @@ COMPONENTS_eagle.app.v6 = \
|
||||||
platform/libplatform.a \
|
platform/libplatform.a \
|
||||||
libc/liblibc.a \
|
libc/liblibc.a \
|
||||||
lua/liblua.a \
|
lua/liblua.a \
|
||||||
|
mqtt/mqtt.a \
|
||||||
smart/smart.a \
|
smart/smart.a \
|
||||||
wofs/wofs.a \
|
wofs/wofs.a \
|
||||||
spiffs/spiffs.a \
|
spiffs/spiffs.a \
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
#define NODE_VERSION_INTERNAL 0U
|
#define NODE_VERSION_INTERNAL 0U
|
||||||
|
|
||||||
#define NODE_VERSION "NodeMCU 0.9.5"
|
#define NODE_VERSION "NodeMCU 0.9.5"
|
||||||
#define BUILD_DATE "build 20150118"
|
#define BUILD_DATE "build 20150123"
|
||||||
|
|
||||||
// #define FLASH_512K
|
// #define FLASH_512K
|
||||||
// #define FLASH_1M
|
// #define FLASH_1M
|
||||||
|
@ -61,6 +61,7 @@
|
||||||
#define LUA_USE_MODULES_UART
|
#define LUA_USE_MODULES_UART
|
||||||
#define LUA_USE_MODULES_OW
|
#define LUA_USE_MODULES_OW
|
||||||
#define LUA_USE_MODULES_BIT
|
#define LUA_USE_MODULES_BIT
|
||||||
|
#define LUA_USE_MODULES_MQTT
|
||||||
#endif /* LUA_USE_MODULES */
|
#endif /* LUA_USE_MODULES */
|
||||||
|
|
||||||
#define LUA_NUMBER_INTEGRAL
|
#define LUA_NUMBER_INTEGRAL
|
||||||
|
|
|
@ -39,6 +39,7 @@ endif
|
||||||
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||||
INCLUDES += -I ./
|
INCLUDES += -I ./
|
||||||
INCLUDES += -I ../libc
|
INCLUDES += -I ../libc
|
||||||
|
INCLUDES += -I ../mqtt
|
||||||
INCLUDES += -I ../lua
|
INCLUDES += -I ../lua
|
||||||
INCLUDES += -I ../platform
|
INCLUDES += -I ../platform
|
||||||
INCLUDES += -I ../wofs
|
INCLUDES += -I ../wofs
|
||||||
|
|
|
@ -61,6 +61,9 @@ LUALIB_API int ( luaopen_i2c )( lua_State *L );
|
||||||
#define AUXLIB_WIFI "wifi"
|
#define AUXLIB_WIFI "wifi"
|
||||||
LUALIB_API int ( luaopen_wifi )( lua_State *L );
|
LUALIB_API int ( luaopen_wifi )( lua_State *L );
|
||||||
|
|
||||||
|
#define AUXLIB_MQTT "mqtt"
|
||||||
|
LUALIB_API int ( luaopen_mqtt )( lua_State *L );
|
||||||
|
|
||||||
#define AUXLIB_NODE "node"
|
#define AUXLIB_NODE "node"
|
||||||
LUALIB_API int ( luaopen_node )( lua_State *L );
|
LUALIB_API int ( luaopen_node )( lua_State *L );
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,14 @@
|
||||||
#define ROM_MODULES_NET
|
#define ROM_MODULES_NET
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(LUA_USE_MODULES_MQTT)
|
||||||
|
#define MODULES_MQTT "mqtt"
|
||||||
|
#define ROM_MODULES_MQTT \
|
||||||
|
_ROM(MODULES_MQTT, luaopen_mqtt, mqtt_map)
|
||||||
|
#else
|
||||||
|
#define ROM_MODULES_MQTT
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(LUA_USE_MODULES_I2C)
|
#if defined(LUA_USE_MODULES_I2C)
|
||||||
#define MODULES_I2C "i2c"
|
#define MODULES_I2C "i2c"
|
||||||
#define ROM_MODULES_I2C \
|
#define ROM_MODULES_I2C \
|
||||||
|
@ -113,6 +121,7 @@
|
||||||
ROM_MODULES_GPIO \
|
ROM_MODULES_GPIO \
|
||||||
ROM_MODULES_PWM \
|
ROM_MODULES_PWM \
|
||||||
ROM_MODULES_WIFI \
|
ROM_MODULES_WIFI \
|
||||||
|
ROM_MODULES_MQTT \
|
||||||
ROM_MODULES_I2C \
|
ROM_MODULES_I2C \
|
||||||
ROM_MODULES_SPI \
|
ROM_MODULES_SPI \
|
||||||
ROM_MODULES_TMR \
|
ROM_MODULES_TMR \
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,44 @@
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Required variables for each makefile
|
||||||
|
# Discard this section from all parent makefiles
|
||||||
|
# Expected variables (with automatic defaults):
|
||||||
|
# CSRCS (all "C" files in the dir)
|
||||||
|
# SUBDIRS (all subdirs with a Makefile)
|
||||||
|
# GEN_LIBS - list of libs to be generated ()
|
||||||
|
# GEN_IMAGES - list of images to be generated ()
|
||||||
|
# COMPONENTS_xxx - a list of libs/objs in the form
|
||||||
|
# subdir/lib to be extracted and rolled up into
|
||||||
|
# a generated lib/image xxx.a ()
|
||||||
|
#
|
||||||
|
ifndef PDIR
|
||||||
|
GEN_LIBS = mqtt.a
|
||||||
|
endif
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Configuration i.e. compile options etc.
|
||||||
|
# Target specific stuff (defines etc.) goes in here!
|
||||||
|
# Generally values applying to a tree are captured in the
|
||||||
|
# makefile at its root level - these are then overridden
|
||||||
|
# for a subtree within the makefile rooted therein
|
||||||
|
#
|
||||||
|
#DEFINES +=
|
||||||
|
|
||||||
|
#############################################################
|
||||||
|
# Recursion Magic - Don't touch this!!
|
||||||
|
#
|
||||||
|
# Each subtree potentially has an include directory
|
||||||
|
# corresponding to the common APIs applicable to modules
|
||||||
|
# rooted at that subtree. Accordingly, the INCLUDE PATH
|
||||||
|
# of a module can only contain the include directories up
|
||||||
|
# its parent path, and not its siblings
|
||||||
|
#
|
||||||
|
# Required for each makefile to inherit from the parent
|
||||||
|
#
|
||||||
|
|
||||||
|
INCLUDES := $(INCLUDES) -I $(PDIR)include
|
||||||
|
INCLUDES += -I ./
|
||||||
|
INCLUDES += -I ../libc
|
||||||
|
PDIR := ../$(PDIR)
|
||||||
|
sinclude $(PDIR)Makefile
|
||||||
|
|
|
@ -0,0 +1,457 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Stephen Robinson
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the copyright holder nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "mqtt_msg.h"
|
||||||
|
|
||||||
|
#define MQTT_MAX_FIXED_HEADER_SIZE 3
|
||||||
|
|
||||||
|
enum mqtt_connect_flag
|
||||||
|
{
|
||||||
|
MQTT_CONNECT_FLAG_USERNAME = 1 << 7,
|
||||||
|
MQTT_CONNECT_FLAG_PASSWORD = 1 << 6,
|
||||||
|
MQTT_CONNECT_FLAG_WILL_RETAIN = 1 << 5,
|
||||||
|
MQTT_CONNECT_FLAG_WILL = 1 << 2,
|
||||||
|
MQTT_CONNECT_FLAG_CLEAN_SESSION = 1 << 1
|
||||||
|
};
|
||||||
|
|
||||||
|
struct __attribute((__packed__)) mqtt_connect_variable_header
|
||||||
|
{
|
||||||
|
uint8_t lengthMsb;
|
||||||
|
uint8_t lengthLsb;
|
||||||
|
uint8_t magic[6];
|
||||||
|
uint8_t version;
|
||||||
|
uint8_t flags;
|
||||||
|
uint8_t keepaliveMsb;
|
||||||
|
uint8_t keepaliveLsb;
|
||||||
|
};
|
||||||
|
|
||||||
|
static int append_string(mqtt_connection_t* connection, const char* string, int len)
|
||||||
|
{
|
||||||
|
if(connection->message.length + len + 2 > connection->buffer_length)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
connection->buffer[connection->message.length++] = len >> 8;
|
||||||
|
connection->buffer[connection->message.length++] = len & 0xff;
|
||||||
|
memcpy(connection->buffer + connection->message.length, string, len);
|
||||||
|
connection->message.length += len;
|
||||||
|
|
||||||
|
return len + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint16_t append_message_id(mqtt_connection_t* connection, uint16_t message_id)
|
||||||
|
{
|
||||||
|
// If message_id is zero then we should assign one, otherwise
|
||||||
|
// we'll use the one supplied by the caller
|
||||||
|
while(message_id == 0)
|
||||||
|
message_id = ++connection->message_id;
|
||||||
|
|
||||||
|
if(connection->message.length + 2 > connection->buffer_length)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
connection->buffer[connection->message.length++] = message_id >> 8;
|
||||||
|
connection->buffer[connection->message.length++] = message_id & 0xff;
|
||||||
|
|
||||||
|
return message_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int init_message(mqtt_connection_t* connection)
|
||||||
|
{
|
||||||
|
connection->message.length = MQTT_MAX_FIXED_HEADER_SIZE;
|
||||||
|
return MQTT_MAX_FIXED_HEADER_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static mqtt_message_t* fail_message(mqtt_connection_t* connection)
|
||||||
|
{
|
||||||
|
connection->message.data = connection->buffer;
|
||||||
|
connection->message.length = 0;
|
||||||
|
return &connection->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
static mqtt_message_t* fini_message(mqtt_connection_t* connection, int type, int dup, int qos, int retain)
|
||||||
|
{
|
||||||
|
int remaining_length = connection->message.length - MQTT_MAX_FIXED_HEADER_SIZE;
|
||||||
|
|
||||||
|
if(remaining_length > 127)
|
||||||
|
{
|
||||||
|
connection->buffer[0] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
|
||||||
|
connection->buffer[1] = 0x80 | (remaining_length % 128);
|
||||||
|
connection->buffer[2] = remaining_length / 128;
|
||||||
|
connection->message.length = remaining_length + 3;
|
||||||
|
connection->message.data = connection->buffer;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
connection->buffer[1] = ((type & 0x0f) << 4) | ((dup & 1) << 3) | ((qos & 3) << 1) | (retain & 1);
|
||||||
|
connection->buffer[2] = remaining_length;
|
||||||
|
connection->message.length = remaining_length + 2;
|
||||||
|
connection->message.data = connection->buffer + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return &connection->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length)
|
||||||
|
{
|
||||||
|
memset(connection, 0, sizeof(connection));
|
||||||
|
connection->buffer = buffer;
|
||||||
|
connection->buffer_length = buffer_length;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mqtt_get_total_length(uint8_t* buffer, uint16_t length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int totlen = 0;
|
||||||
|
|
||||||
|
for(i = 1; i < length; ++i)
|
||||||
|
{
|
||||||
|
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
||||||
|
if((buffer[i] & 0x80) == 0)
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
totlen += i;
|
||||||
|
|
||||||
|
return totlen;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int totlen = 0;
|
||||||
|
int topiclen;
|
||||||
|
|
||||||
|
for(i = 1; i < *length; ++i)
|
||||||
|
{
|
||||||
|
totlen += (buffer[i] & 0x7f) << (7 * (i -1));
|
||||||
|
if((buffer[i] & 0x80) == 0)
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
totlen += i;
|
||||||
|
|
||||||
|
if(i + 2 >= *length)
|
||||||
|
return NULL;
|
||||||
|
topiclen = buffer[i++] << 8;
|
||||||
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
|
if(i + topiclen > *length)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
*length = topiclen;
|
||||||
|
return (const char*)(buffer + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int totlen = 0;
|
||||||
|
int topiclen;
|
||||||
|
|
||||||
|
for(i = 1; i < *length; ++i)
|
||||||
|
{
|
||||||
|
totlen += (buffer[i] & 0x7f) << (7 * (i - 1));
|
||||||
|
if((buffer[i] & 0x80) == 0)
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
totlen += i;
|
||||||
|
|
||||||
|
if(i + 2 >= *length)
|
||||||
|
return NULL;
|
||||||
|
topiclen = buffer[i++] << 8;
|
||||||
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
|
if(i + topiclen >= *length){
|
||||||
|
*length = 0;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
i += topiclen;
|
||||||
|
|
||||||
|
if(mqtt_get_qos(buffer) > 0)
|
||||||
|
{
|
||||||
|
if(i + 2 >= *length)
|
||||||
|
return NULL;
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(totlen < i)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if(totlen <= *length)
|
||||||
|
*length = totlen - i;
|
||||||
|
else
|
||||||
|
*length = *length - i;
|
||||||
|
return (const char*)(buffer + i);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length)
|
||||||
|
{
|
||||||
|
if(length < 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
switch(mqtt_get_type(buffer))
|
||||||
|
{
|
||||||
|
case MQTT_MSG_TYPE_PUBLISH:
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int topiclen;
|
||||||
|
|
||||||
|
for(i = 1; i < length; ++i)
|
||||||
|
{
|
||||||
|
if((buffer[i] & 0x80) == 0)
|
||||||
|
{
|
||||||
|
++i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i + 2 >= length)
|
||||||
|
return 0;
|
||||||
|
topiclen = buffer[i++] << 8;
|
||||||
|
topiclen |= buffer[i++];
|
||||||
|
|
||||||
|
if(i + topiclen >= length)
|
||||||
|
return 0;
|
||||||
|
i += topiclen;
|
||||||
|
|
||||||
|
if(mqtt_get_qos(buffer) > 0)
|
||||||
|
{
|
||||||
|
if(i + 2 >= length)
|
||||||
|
return 0;
|
||||||
|
//i += 2;
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (buffer[i] << 8) | buffer[i + 1];
|
||||||
|
}
|
||||||
|
case MQTT_MSG_TYPE_PUBACK:
|
||||||
|
case MQTT_MSG_TYPE_PUBREC:
|
||||||
|
case MQTT_MSG_TYPE_PUBREL:
|
||||||
|
case MQTT_MSG_TYPE_PUBCOMP:
|
||||||
|
case MQTT_MSG_TYPE_SUBACK:
|
||||||
|
case MQTT_MSG_TYPE_UNSUBACK:
|
||||||
|
case MQTT_MSG_TYPE_SUBSCRIBE:
|
||||||
|
{
|
||||||
|
// This requires the remaining length to be encoded in 1 byte,
|
||||||
|
// which it should be.
|
||||||
|
if(length >= 4 && (buffer[1] & 0x80) == 0)
|
||||||
|
return (buffer[2] << 8) | buffer[3];
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info)
|
||||||
|
{
|
||||||
|
struct mqtt_connect_variable_header* variable_header;
|
||||||
|
|
||||||
|
init_message(connection);
|
||||||
|
|
||||||
|
if(connection->message.length + sizeof(*variable_header) > connection->buffer_length)
|
||||||
|
return fail_message(connection);
|
||||||
|
variable_header = (void*)(connection->buffer + connection->message.length);
|
||||||
|
connection->message.length += sizeof(*variable_header);
|
||||||
|
|
||||||
|
variable_header->lengthMsb = 0;
|
||||||
|
variable_header->lengthLsb = 6;
|
||||||
|
memcpy(variable_header->magic, "MQIsdp", 6);
|
||||||
|
variable_header->version = 3;
|
||||||
|
variable_header->flags = 0;
|
||||||
|
variable_header->keepaliveMsb = info->keepalive >> 8;
|
||||||
|
variable_header->keepaliveLsb = info->keepalive & 0xff;
|
||||||
|
|
||||||
|
if(info->clean_session)
|
||||||
|
variable_header->flags |= MQTT_CONNECT_FLAG_CLEAN_SESSION;
|
||||||
|
|
||||||
|
if(info->client_id != NULL && info->client_id[0] != '\0')
|
||||||
|
{
|
||||||
|
if(append_string(connection, info->client_id, strlen(info->client_id)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(info->will_topic != NULL && info->will_topic[0] != '\0')
|
||||||
|
{
|
||||||
|
if(append_string(connection, info->will_topic, strlen(info->will_topic)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(append_string(connection, info->will_message, strlen(info->will_message)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
variable_header->flags |= MQTT_CONNECT_FLAG_WILL;
|
||||||
|
if(info->will_retain)
|
||||||
|
variable_header->flags |= MQTT_CONNECT_FLAG_WILL_RETAIN;
|
||||||
|
variable_header->flags |= (info->will_qos & 3) << 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(info->username != NULL && info->username[0] != '\0')
|
||||||
|
{
|
||||||
|
if(append_string(connection, info->username, strlen(info->username)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
variable_header->flags |= MQTT_CONNECT_FLAG_USERNAME;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(info->password != NULL && info->password[0] != '\0')
|
||||||
|
{
|
||||||
|
if(append_string(connection, info->password, strlen(info->password)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
variable_header->flags |= MQTT_CONNECT_FLAG_PASSWORD;
|
||||||
|
}
|
||||||
|
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_CONNECT, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
|
||||||
|
if(topic == NULL || topic[0] == '\0')
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(append_string(connection, topic, strlen(topic)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(qos > 0)
|
||||||
|
{
|
||||||
|
if((*message_id = append_message_id(connection, 0)) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
*message_id = 0;
|
||||||
|
|
||||||
|
if(connection->message.length + data_length > connection->buffer_length)
|
||||||
|
return fail_message(connection);
|
||||||
|
memcpy(connection->buffer + connection->message.length, data, data_length);
|
||||||
|
connection->message.length += data_length;
|
||||||
|
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PUBLISH, 0, qos, retain);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
if(append_message_id(connection, message_id) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PUBACK, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
if(append_message_id(connection, message_id) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PUBREC, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
if(append_message_id(connection, message_id) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PUBREL, 0, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
if(append_message_id(connection, message_id) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PUBCOMP, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
|
||||||
|
if(topic == NULL || topic[0] == '\0')
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if((*message_id = append_message_id(connection, 0)) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(append_string(connection, topic, strlen(topic)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(connection->message.length + 1 > connection->buffer_length)
|
||||||
|
return fail_message(connection);
|
||||||
|
connection->buffer[connection->message.length++] = qos;
|
||||||
|
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
|
||||||
|
if(topic == NULL || topic[0] == '\0')
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if((*message_id = append_message_id(connection, 0)) == 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
if(append_string(connection, topic, strlen(topic)) < 0)
|
||||||
|
return fail_message(connection);
|
||||||
|
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_SUBSCRIBE, 0, 1, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_pingreq(mqtt_connection_t* connection)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PINGREQ, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_pingresp(mqtt_connection_t* connection)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_PINGRESP, 0, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_disconnect(mqtt_connection_t* connection)
|
||||||
|
{
|
||||||
|
init_message(connection);
|
||||||
|
return fini_message(connection, MQTT_MSG_TYPE_DISCONNECT, 0, 0, 0);
|
||||||
|
}
|
|
@ -0,0 +1,129 @@
|
||||||
|
/*
|
||||||
|
* File: mqtt_msg.h
|
||||||
|
* Author: Minh Tuan
|
||||||
|
*
|
||||||
|
* Created on July 12, 2014, 1:05 PM
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef MQTT_MSG_H
|
||||||
|
#define MQTT_MSG_H
|
||||||
|
#include "c_types.h"
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2014, Stephen Robinson
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
* 3. Neither the name of the copyright holder nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
/* 7 6 5 4 3 2 1 0*/
|
||||||
|
/*| --- Message Type---- | DUP Flag | QoS Level | Retain |
|
||||||
|
/* Remaining Length */
|
||||||
|
|
||||||
|
|
||||||
|
enum mqtt_message_type
|
||||||
|
{
|
||||||
|
MQTT_MSG_TYPE_CONNECT = 1,
|
||||||
|
MQTT_MSG_TYPE_CONNACK = 2,
|
||||||
|
MQTT_MSG_TYPE_PUBLISH = 3,
|
||||||
|
MQTT_MSG_TYPE_PUBACK = 4,
|
||||||
|
MQTT_MSG_TYPE_PUBREC = 5,
|
||||||
|
MQTT_MSG_TYPE_PUBREL = 6,
|
||||||
|
MQTT_MSG_TYPE_PUBCOMP = 7,
|
||||||
|
MQTT_MSG_TYPE_SUBSCRIBE = 8,
|
||||||
|
MQTT_MSG_TYPE_SUBACK = 9,
|
||||||
|
MQTT_MSG_TYPE_UNSUBSCRIBE = 10,
|
||||||
|
MQTT_MSG_TYPE_UNSUBACK = 11,
|
||||||
|
MQTT_MSG_TYPE_PINGREQ = 12,
|
||||||
|
MQTT_MSG_TYPE_PINGRESP = 13,
|
||||||
|
MQTT_MSG_TYPE_DISCONNECT = 14
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct mqtt_message
|
||||||
|
{
|
||||||
|
uint8_t* data;
|
||||||
|
uint16_t length;
|
||||||
|
|
||||||
|
} mqtt_message_t;
|
||||||
|
|
||||||
|
typedef struct mqtt_connection
|
||||||
|
{
|
||||||
|
mqtt_message_t message;
|
||||||
|
|
||||||
|
uint16_t message_id;
|
||||||
|
uint8_t* buffer;
|
||||||
|
uint16_t buffer_length;
|
||||||
|
|
||||||
|
} mqtt_connection_t;
|
||||||
|
|
||||||
|
typedef struct mqtt_connect_info
|
||||||
|
{
|
||||||
|
char* client_id;
|
||||||
|
char* username;
|
||||||
|
char* password;
|
||||||
|
char* will_topic;
|
||||||
|
char* will_message;
|
||||||
|
int keepalive;
|
||||||
|
int will_qos;
|
||||||
|
int will_retain;
|
||||||
|
int clean_session;
|
||||||
|
|
||||||
|
} mqtt_connect_info_t;
|
||||||
|
|
||||||
|
|
||||||
|
static inline int mqtt_get_type(uint8_t* buffer) { return (buffer[0] & 0xf0) >> 4; }
|
||||||
|
static inline int mqtt_get_dup(uint8_t* buffer) { return (buffer[0] & 0x08) >> 3; }
|
||||||
|
static inline int mqtt_get_qos(uint8_t* buffer) { return (buffer[0] & 0x06) >> 1; }
|
||||||
|
static inline int mqtt_get_retain(uint8_t* buffer) { return (buffer[0] & 0x01); }
|
||||||
|
|
||||||
|
void mqtt_msg_init(mqtt_connection_t* connection, uint8_t* buffer, uint16_t buffer_length);
|
||||||
|
int mqtt_get_total_length(uint8_t* buffer, uint16_t length);
|
||||||
|
const char* mqtt_get_publish_topic(uint8_t* buffer, uint16_t* length);
|
||||||
|
const char* mqtt_get_publish_data(uint8_t* buffer, uint16_t* length);
|
||||||
|
uint16_t mqtt_get_id(uint8_t* buffer, uint16_t length);
|
||||||
|
|
||||||
|
mqtt_message_t* mqtt_msg_connect(mqtt_connection_t* connection, mqtt_connect_info_t* info);
|
||||||
|
mqtt_message_t* mqtt_msg_publish(mqtt_connection_t* connection, const char* topic, const char* data, int data_length, int qos, int retain, uint16_t* message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_puback(mqtt_connection_t* connection, uint16_t message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_pubrec(mqtt_connection_t* connection, uint16_t message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_pubrel(mqtt_connection_t* connection, uint16_t message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_pubcomp(mqtt_connection_t* connection, uint16_t message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_subscribe(mqtt_connection_t* connection, const char* topic, int qos, uint16_t* message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_unsubscribe(mqtt_connection_t* connection, const char* topic, uint16_t* message_id);
|
||||||
|
mqtt_message_t* mqtt_msg_pingreq(mqtt_connection_t* connection);
|
||||||
|
mqtt_message_t* mqtt_msg_pingresp(mqtt_connection_t* connection);
|
||||||
|
mqtt_message_t* mqtt_msg_disconnect(mqtt_connection_t* connection);
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* MQTT_MSG_H */
|
||||||
|
|
|
@ -313,3 +313,9 @@ uart.on("data", 0 ,function(input) if input=="q" then uart.on("data") else print
|
||||||
uart.on("data","\r",function(input) if input=="quit" then uart.on("data") else print(input) end end, 1)
|
uart.on("data","\r",function(input) if input=="quit" then uart.on("data") else print(input) end end, 1)
|
||||||
|
|
||||||
for k, v in pairs(file.list()) do print('file:'..k..' len:'..v) end
|
for k, v in pairs(file.list()) do print('file:'..k..' len:'..v) end
|
||||||
|
|
||||||
|
m=mqtt.Client()
|
||||||
|
m:connect("192.168.18.101",1883)
|
||||||
|
m:subscribe("/topic",0,function(m) print("sub done") end)
|
||||||
|
m:on("message",function(m,t,pl) print(t..":") if pl~=nil then print(pl) end end )
|
||||||
|
m:publish("/topic","hello",0,0)
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,11 @@
|
||||||
|
# Enforce Unix newlines
|
||||||
|
*.css text eol=lf
|
||||||
|
*.html text eol=lf
|
||||||
|
*.js text eol=lf
|
||||||
|
*.json text eol=lf
|
||||||
|
*.less text eol=lf
|
||||||
|
*.md text eol=lf
|
||||||
|
*.svg text eol=lf
|
||||||
|
*.yml text eol=lf
|
||||||
|
*.py text eol=lf
|
||||||
|
*.sh text eol=lf
|
Loading…
Reference in New Issue