QoS-1 PUBLISH is available #34

BugFix of #69

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2018-07-29 16:11:38 +09:00
parent 9940aadd4b
commit 4932d2d0ee
32 changed files with 1274 additions and 348 deletions

View File

@@ -7,9 +7,13 @@ PUBAPPL := mainPub
PRGSUB := MQTT-SNSub
SUBAPPL := mainSub
PRGQOS := MQTT-SNPubQoS-1
QOSAPPL := mainPubQoS-1
SRCDIR := samples
SRCPUB := ClientPub
SRCSUB := ClientSub
SRCQOS := ClientPubQoS-1
SUBDIR := src
CPPSRCS := \
@@ -46,10 +50,11 @@ DEPS := $(CPPSRCS:%.cpp=$(OUTDIR)/%.d)
PROGPUB := $(OUTDIR)/$(PRGPUB)
PROGSUB := $(OUTDIR)/$(PRGSUB)
PROGQOS := $(OUTDIR)/$(PRGQOS)
.PHONY: install clean
all: $(PROG) $(PROGPUB) $(PROGSUB)
all: $(PROG) $(PROGPUB) $(PROGSUB) $(PROGQOS)
@@ -64,6 +69,9 @@ $(PROGPUB): $(OBJS) $(OUTDIR)/$(SRCDIR)/$(SRCPUB)/$(PUBAPPL).o
$(PROGSUB): $(OBJS) $(OUTDIR)/$(SRCDIR)/$(SRCSUB)/$(SUBAPPL).o
$(CXX) $(LDFLAGS) -o $(PROGSUB) $(OUTDIR)/$(SRCDIR)/$(SRCSUB)/$(SUBAPPL).o $(OBJS) $(LIBS) $(LDADD)
$(PROGQOS): $(OBJS) $(OUTDIR)/$(SRCDIR)/$(SRCQOS)/$(QOSAPPL).o
$(CXX) $(LDFLAGS) -o $(PROGQOS) $(OUTDIR)/$(SRCDIR)/$(SRCQOS)/$(QOSAPPL).o $(OBJS) $(LIBS) $(LDADD)
$(OUTDIR)/$(SUBDIR)/%.o:$(SUBDIR)/%.cpp
@if [ ! -e `dirname $@` ]; then mkdir -p `dirname $@`; fi
@@ -80,6 +88,10 @@ $(OUTDIR)/$(SRCDIR)/$(SRCPUB)/%.o:$(SRCDIR)/$(SRCPUB)%.cpp
$(OUTDIR)/$(SRCDIR)/$(SRCSUB)/%.o:$(SRCDIR)/$(SRCSUB)%.cpp
@if [ ! -e `dirname $@` ]; then mkdir -p `dirname $@`; fi
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDES) $(DEFS) -o $@ -c -MMD -MP -MF $(@:%.o=%.d) $<
$(OUTDIR)/$(SRCDIR)/$(SRCQOS)/%.o:$(SRCDIR)/$(SRCQOS)%.cpp
@if [ ! -e `dirname $@` ]; then mkdir -p `dirname $@`; fi
$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(INCLUDES) $(DEFS) -o $@ -c -MMD -MP -MF $(@:%.o=%.d) $<
clean:
rm -rf $(OUTDIR)
@@ -88,4 +100,5 @@ install:
cp -pf $(PROG) ../../../
cp -pf $(PROGPUB) ../../../
cp -pf $(PROGSUB) ../../../
cp -pf $(PROGQOS) ../../../

View File

@@ -0,0 +1,161 @@
/****************************************************************************
* Copyright (c) 2016, Tomoaki Yamaguchi
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*---------------------------------------------------------------------------
*
* MQTT-SN GATEWAY TEST CLIENT
*
* Supported functions.
*
* void PUBLISH ( const char* topicName, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
*
* void PUBLISH ( uint16_t topicId, uint8_t* payload, uint16_t len, uint8_t qos, bool retain = false );
*
* void SUBSCRIBE ( const char* topicName, TopicCallback onPublish, uint8_t qos );
*
* void SUBSCRIBE ( uint16_t topicId, TopicCallback onPublish, uint8_t qos );
*
* void UNSUBSCRIBE ( const char* topicName );
*
* void UNSUBSCRIBE ( uint16_t topicId );
*
* void DISCONNECT ( uint16_t sleepInSecs );
*
* void CONNECT ( void );
*
* void DISPLAY( format, .....); <== instead of printf()
*
*
* Contributors:
* Tomoaki Yamaguchi - initial API and implementation
***************************************************************************/
#include "LMqttsnClientApp.h"
#include "LMqttsnClient.h"
#include "LScreen.h"
using namespace std;
using namespace linuxAsyncClient;
extern LMqttsnClient* theClient;
extern LScreen* theScreen;
/*------------------------------------------------------
* UDP Configuration (theNetcon)
*------------------------------------------------------*/
UDPCONF = {
"GatewayTestPubClient", // ClientId
{225,1,1,1}, // Multicast group IP
1883, // Multicast group Port
20001, // Local PortNo
};
/*------------------------------------------------------
* Client Configuration (theMqcon)
*------------------------------------------------------*/
MQTTSNCONF = {
300, //KeepAlive [seconds]
true, //Clean session
300, //Sleep duration [seconds]
"", //WillTopic
"", //WillMessage
0, //WillQos
false //WillRetain
};
/*------------------------------------------------------
* Define Topics
*------------------------------------------------------*/
/*------------------------------------------------------
* Callback routines for Subscribed Topics
*------------------------------------------------------*/
/*------------------------------------------------------
* A Link list of Callback routines and Topics
*------------------------------------------------------*/
SUBSCRIBE_LIST = {// e.g. SUB(TopicType, topicName, TopicId, callback, QoSx),
END_OF_SUBSCRIBE_LIST
};
/*------------------------------------------------------
* Test functions
*------------------------------------------------------*/
void publishTopic1(void)
{
char payload[300];
sprintf(payload, "publish \"ty4tw/Topic1\" \n");
uint8_t qos = 3;
PUBLISH(1,(uint8_t*)payload, strlen(payload), qos);
}
void publishTopic2(void)
{
char payload[300];
sprintf(payload, "publish \"ty4tw/topic2\" \n");
uint8_t qos = 3;
PUBLISH(2,(uint8_t*)payload, strlen(payload), qos);
}
void publishTopic57(void)
{
char payload[300];
sprintf(payload, "publish \"ty4tw/topic57\" \n");
uint8_t qos = 3;
PUBLISH(5,(uint8_t*)payload, strlen(payload), qos);
}
void disconnect(void)
{
DISCONNECT(0);
}
/*------------------------------------------------------
* A List of Test functions is valid in case of
* line 23 of LMqttsnClientApp.h is commented out.
* //#define CLIENT_MODE
*------------------------------------------------------*/
TEST_LIST = {// e.g. TEST( Label, Test),
TEST("Step1:Publish topic1", publishTopic1),
TEST("Step2:Publish topic57", publishTopic57),
TEST("Step3:Publish topic2", publishTopic2),
END_OF_TEST_LIST
};
/*------------------------------------------------------
* List of tasks is invalid in case of line23 of
* LMqttsnClientApp.h is commented out.
* #define CLIENT_MODE
*------------------------------------------------------*/
TASK_LIST = {// e.g. TASK( task, executing duration in second),
TASK(publishTopic1, 4), // publishTopic1() is executed every 4 seconds
TASK(publishTopic2, 7), // publishTopic2() is executed every 7 seconds
END_OF_TASK_LIST
};
/*------------------------------------------------------
* Initialize function
*------------------------------------------------------*/
void setup(void)
{
SetQoSMinus1Mode(true);
}
/***************** END OF PROGRAM ********************/

View File

@@ -220,7 +220,7 @@ TASK_LIST = {// e.g. TASK( task, executing duration in second),
*------------------------------------------------------*/
void setup(void)
{
//SetForwarderMode();
SetForwarderMode(false);
}

View File

@@ -30,21 +30,16 @@ extern uint16_t getUint16(const uint8_t* pos);
extern LMqttsnClient* theClient;
extern LScreen* theScreen;
/*=====================================
Class GwProxy
Class GwProxy
======================================*/
static const char* packet_names[] =
{
"ADVERTISE", "SEARCHGW", "GWINFO", "RESERVED", "CONNECT", "CONNACK",
"WILLTOPICREQ", "WILLTOPIC", "WILLMSGREQ", "WILLMSG", "REGISTER", "REGACK",
"PUBLISH", "PUBACK", "PUBCOMP", "PUBREC", "PUBREL", "RESERVED",
"SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", "PINGREQ", "PINGRESP",
"DISCONNECT", "RESERVED", "WILLTOPICUPD", "WILLTOPICRESP", "WILLMSGUPD",
"WILLMSGRESP"
};
static const char* packet_names[] = { "ADVERTISE", "SEARCHGW", "GWINFO", "RESERVED", "CONNECT", "CONNACK",
"WILLTOPICREQ", "WILLTOPIC", "WILLMSGREQ", "WILLMSG", "REGISTER", "REGACK", "PUBLISH", "PUBACK", "PUBCOMP",
"PUBREC", "PUBREL", "RESERVED", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", "PINGREQ", "PINGRESP",
"DISCONNECT", "RESERVED", "WILLTOPICUPD", "WILLTOPICRESP", "WILLMSGUPD", "WILLMSGRESP" };
LGwProxy::LGwProxy(){
LGwProxy::LGwProxy()
{
_nextMsgId = 0;
_status = GW_LOST;
_gwId = 0;
@@ -61,13 +56,16 @@ LGwProxy::LGwProxy(){
_tWake = 0;
_initialized = 0;
_isForwarderMode = false;
_isQoSMinus1Mode = false;
}
LGwProxy::~LGwProxy(){
LGwProxy::~LGwProxy()
{
_topicTbl.clearTopic();
}
void LGwProxy::initialize(LUdpConfig netconf, LMqttsnConfig mqconf){
void LGwProxy::initialize(LUdpConfig netconf, LMqttsnConfig mqconf)
{
_network.initialize(netconf);
_clientId = netconf.clientId;
_willTopic = mqconf.willTopic;
@@ -79,78 +77,105 @@ void LGwProxy::initialize(LUdpConfig netconf, LMqttsnConfig mqconf){
_initialized = 1;
}
void LGwProxy::connect(){
void LGwProxy::connect()
{
char* pos;
while (_status != GW_CONNECTED){
while (_status != GW_CONNECTED)
{
pos = _msg;
if (_status == GW_SEND_WILLMSG){
*pos++ = 2 + (uint8_t)strlen(_willMsg);
*pos++ = MQTTSN_TYPE_WILLMSG;
strcpy(pos,_willMsg); // WILLMSG
_status = GW_WAIT_CONNACK;
writeGwMsg();
}else if (_status == GW_SEND_WILLTOPIC){
*pos++ = 3 + (uint8_t)strlen(_willTopic);
*pos++ = MQTTSN_TYPE_WILLTOPIC;
*pos++ = _qosWill | _retainWill;
strcpy(pos,_willTopic); // WILLTOPIC
_status = GW_WAIT_WILLMSGREQ;
writeGwMsg();
}else if (_status == GW_CONNECTING || _status == GW_DISCONNECTED || _status == GW_SLEPT ){
uint8_t clientIdLen = uint8_t(strlen(_clientId) > 23 ? 23 : strlen(_clientId));
*pos++ = 6 + clientIdLen;
*pos++ = MQTTSN_TYPE_CONNECT;
pos++;
if (_cleanSession){
_msg[2] = MQTTSN_FLAG_CLEAN;
}
*pos++ = MQTTSN_PROTOCOL_ID;
setUint16((uint8_t*)pos, _tkeepAlive);
pos += 2;
strncpy(pos, _clientId, clientIdLen);
_msg[ 6 + clientIdLen] = 0;
_status = GW_WAIT_CONNACK;
if ( _willMsg && _willTopic && _status != GW_SLEPT ){
if (strlen(_willMsg) && strlen(_willTopic)){
_msg[2] = _msg[2] | MQTTSN_FLAG_WILL; // CONNECT
_status = GW_WAIT_WILLTOPICREQ;
}
}
writeGwMsg();
_connectRetry = MQTTSN_RETRY_COUNT;
}else if (_status == GW_LOST){
if (_status == GW_LOST)
{
*pos++ = 3;
*pos++ = MQTTSN_TYPE_SEARCHGW;
*pos = 0; // SERCHGW
_status = GW_SEARCHING;
writeGwMsg();
}
else if (_status == GW_SEND_WILLMSG)
{
*pos++ = 2 + (uint8_t) strlen(_willMsg);
*pos++ = MQTTSN_TYPE_WILLMSG;
strcpy(pos, _willMsg); // WILLMSG
_status = GW_WAIT_CONNACK;
writeGwMsg();
}
else if (_status == GW_SEND_WILLTOPIC)
{
*pos++ = 3 + (uint8_t) strlen(_willTopic);
*pos++ = MQTTSN_TYPE_WILLTOPIC;
*pos++ = _qosWill | _retainWill;
strcpy(pos, _willTopic); // WILLTOPIC
_status = GW_WAIT_WILLMSGREQ;
writeGwMsg();
}
else if (_status == GW_CONNECTING || _status == GW_DISCONNECTED || _status == GW_SLEPT)
{
uint8_t clientIdLen = uint8_t(strlen(_clientId) > 23 ? 23 : strlen(_clientId));
if (_isQoSMinus1Mode)
{
_status = GW_CONNECTED;
}
else
{
*pos++ = 6 + clientIdLen;
*pos++ = MQTTSN_TYPE_CONNECT;
pos++;
if (_cleanSession)
{
_msg[2] = MQTTSN_FLAG_CLEAN;
}
*pos++ = MQTTSN_PROTOCOL_ID;
setUint16((uint8_t*) pos, _tkeepAlive);
pos += 2;
strncpy(pos, _clientId, clientIdLen);
_msg[6 + clientIdLen] = 0;
_status = GW_WAIT_CONNACK;
if (_willMsg && _willTopic && _status != GW_SLEPT)
{
if (strlen(_willMsg) && strlen(_willTopic))
{
_msg[2] = _msg[2] | MQTTSN_FLAG_WILL; // CONNECT
_status = GW_WAIT_WILLTOPICREQ;
}
}
writeGwMsg();
_connectRetry = MQTTSN_RETRY_COUNT;
}
}
getConnectResponce();
}
return;
}
int LGwProxy::getConnectResponce(void){
int LGwProxy::getConnectResponce(void)
{
int len = readMsg();
if (len == 0){
if (_sendUTC + MQTTSN_TIME_RETRY < time(NULL)){
if (len == 0)
{
if (_sendUTC + MQTTSN_TIME_RETRY < time(NULL))
{
if (_msg[1] == MQTTSN_TYPE_CONNECT)
{
_connectRetry--;
}
if (--_retryCount > 0){
writeMsg((const uint8_t*)_msg); // Not writeGwMsg() : not to reset the counter.
if (--_retryCount > 0)
{
writeMsg((const uint8_t*) _msg); // Not writeGwMsg() : not to reset the counter.
_sendUTC = time(NULL);
}else{
}
else
{
_sendUTC = 0;
if ( _status > GW_SEARCHING && _connectRetry > 0){
if (_status > GW_SEARCHING && _connectRetry > 0)
{
_status = GW_CONNECTING;
}else{
}
else
{
_status = GW_LOST;
_gwId = 0;
}
@@ -158,66 +183,87 @@ int LGwProxy::getConnectResponce(void){
}
}
return 0;
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_GWINFO && _status == GW_SEARCHING){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_GWINFO && _status == GW_SEARCHING)
{
_network.setGwAddress();
_gwId = _mqttsnMsg[1];
_status = GW_CONNECTING;
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_WILLTOPICREQ && _status == GW_WAIT_WILLTOPICREQ){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_WILLTOPICREQ && _status == GW_WAIT_WILLTOPICREQ)
{
_status = GW_SEND_WILLTOPIC;
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_WILLMSGREQ && _status == GW_WAIT_WILLMSGREQ){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_WILLMSGREQ && _status == GW_WAIT_WILLMSGREQ)
{
_status = GW_SEND_WILLMSG;
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_CONNACK && _status == GW_WAIT_CONNACK){
if (_mqttsnMsg[1] == MQTTSN_RC_ACCEPTED){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_CONNACK && _status == GW_WAIT_CONNACK)
{
if (_mqttsnMsg[1] == MQTTSN_RC_ACCEPTED)
{
_status = GW_CONNECTED;
_connectRetry = MQTTSN_RETRY_COUNT;
setPingReqTimer();
if ( _tSleep ){
if (_tSleep)
{
_tSleep = 0;
}else{
}
else
{
DISPLAY("\033[0m\033[0;32m\n\n Connected to the Broker\033[0m\033[0;37m\n\n");
if ( _cleanSession || _initialized == 1 )
if (_cleanSession || _initialized == 1)
{
_topicTbl.clearTopic();
_initialized = 0;
theClient->onConnect(); // SUBSCRIBEs are conducted
}
}
}else{
}
else
{
_status = GW_CONNECTING;
}
}
return 1;
}
void LGwProxy::reconnect(void){
void LGwProxy::reconnect(void)
{
D_MQTTLOG("...Gateway reconnect\r\n");
_status = GW_DISCONNECTED;
connect();
}
void LGwProxy::disconnect(uint16_t secs){
void LGwProxy::disconnect(uint16_t secs)
{
_tSleep = secs;
_tWake = 0;
_msg[1] = MQTTSN_TYPE_DISCONNECT;
if (secs){
if (secs)
{
_msg[0] = 4;
setUint16((uint8_t*) _msg + 2, secs);
_status = GW_SLEEPING;
}else{
}
else
{
_msg[0] = 2;
_keepAliveTimer.stop();
_status = GW_DISCONNECTING;
}
_retryCount = MQTTSN_RETRY_COUNT;
writeMsg((const uint8_t*)_msg);
writeMsg((const uint8_t*) _msg);
_sendUTC = time(NULL);
while ( _status != GW_DISCONNECTED && _status != GW_SLEPT){
if (getDisconnectResponce() < 0){
while (_status != GW_DISCONNECTED && _status != GW_SLEPT)
{
if (getDisconnectResponce() < 0)
{
_status = GW_LOST;
DISPLAY("\033[0m\033[0;31m\n\n!!!!!! DISCONNECT Error !!!!!\033[0m\033[0;37m \n\n");
return;
@@ -225,48 +271,63 @@ void LGwProxy::disconnect(uint16_t secs){
}
}
int LGwProxy::getDisconnectResponce(void){
int LGwProxy::getDisconnectResponce(void)
{
int len = readMsg();
if (len == 0){
if (_sendUTC + MQTTSN_TIME_RETRY < time(NULL)){
if (--_retryCount >= 0){
writeMsg((const uint8_t*)_msg);
if (len == 0)
{
if (_sendUTC + MQTTSN_TIME_RETRY < time(NULL))
{
if (--_retryCount >= 0)
{
writeMsg((const uint8_t*) _msg);
_sendUTC = time(NULL);
}else{
}
else
{
_status = GW_LOST;
_gwId = 0;
return -1;
}
}
return 0;
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_DISCONNECT){
if (_status == GW_SLEEPING ){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_DISCONNECT)
{
if (_status == GW_SLEEPING)
{
_status = GW_SLEPT;
uint32_t remain = _keepAliveTimer.getRemain();
theClient->setSleepMode(remain);
/* Wake up and starts from this point. */
}else{
}
else
{
_status = GW_DISCONNECTED;
}
}
return 0;
}
int LGwProxy::getMessage(void){
int LGwProxy::getMessage(void)
{
int len = readMsg();
if (len < 0){
if (len < 0)
{
return len; //error
}
#ifdef DEBUG_MQTTSN
if (len){
if (len)
{
D_MQTTLOG(" recved msgType %x\n", _mqttsnMsg[0]);
}
#endif
if (len == 0){
if (len == 0)
{
// Check PINGREQ required
checkPingReq();
@@ -282,87 +343,115 @@ int LGwProxy::getMessage(void){
// Check Timeout of SUBSCRIBEs,
theClient->getSubscribeManager()->checkTimeout();
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_PUBLISH){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_PUBLISH)
{
theClient->getPublishManager()->published(_mqttsnMsg, len);
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_PUBACK || _mqttsnMsg[0] == MQTTSN_TYPE_PUBCOMP ||
_mqttsnMsg[0] == MQTTSN_TYPE_PUBREC || _mqttsnMsg[0] == MQTTSN_TYPE_PUBREL ){
theClient->getPublishManager()->responce(_mqttsnMsg, (uint16_t)len);
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_PUBACK || _mqttsnMsg[0] == MQTTSN_TYPE_PUBCOMP
|| _mqttsnMsg[0] == MQTTSN_TYPE_PUBREC || _mqttsnMsg[0] == MQTTSN_TYPE_PUBREL)
{
theClient->getPublishManager()->responce(_mqttsnMsg, (uint16_t) len);
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_SUBACK || _mqttsnMsg[0] == MQTTSN_TYPE_UNSUBACK){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_SUBACK || _mqttsnMsg[0] == MQTTSN_TYPE_UNSUBACK)
{
theClient->getSubscribeManager()->responce(_mqttsnMsg);
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_REGISTER){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_REGISTER)
{
_regMgr.responceRegister(_mqttsnMsg, len);
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_REGACK){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_REGACK)
{
_regMgr.responceRegAck(getUint16(_mqttsnMsg + 3), getUint16(_mqttsnMsg + 1));
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_PINGRESP){
if (_pingStatus == GW_WAIT_PINGRESP){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_PINGRESP)
{
if (_pingStatus == GW_WAIT_PINGRESP)
{
_pingStatus = 0;
setPingReqTimer();
if ( _tSleep > 0 ){
if (_tSleep > 0)
{
_tWake += _tkeepAlive;
if ( _tWake < _tSleep ){
if (_tWake < _tSleep)
{
theClient->setSleepMode(_tkeepAlive * 1000UL);
}else{
}
else
{
DISPLAY("\033[0m\033[0;32m\n\n Get back to ACTIVE.\033[0m\033[0;37m\n\n");
_tWake = 0;
connect();
}
}
}
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_DISCONNECT){
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_DISCONNECT)
{
_status = GW_LOST;
_gwAliveTimer.stop();
_keepAliveTimer.stop();
}else if (_mqttsnMsg[0] == MQTTSN_TYPE_ADVERTISE){
if (getUint16((const uint8_t*)(_mqttsnMsg + 2)) < 61){
_tAdv = getUint16((const uint8_t*)(_mqttsnMsg + 2)) * 1500;
}else{
_tAdv = getUint16((const uint8_t*)(_mqttsnMsg + 2)) * 1100;
}
else if (_mqttsnMsg[0] == MQTTSN_TYPE_ADVERTISE)
{
if (getUint16((const uint8_t*) (_mqttsnMsg + 2)) < 61)
{
_tAdv = getUint16((const uint8_t*) (_mqttsnMsg + 2)) * 1500;
}
else
{
_tAdv = getUint16((const uint8_t*) (_mqttsnMsg + 2)) * 1100;
}
_gwAliveTimer.start(_tAdv);
}
return 0;
}
uint16_t LGwProxy::registerTopic(char* topicName, uint16_t topicId){
uint16_t LGwProxy::registerTopic(char* topicName, uint16_t topicId)
{
uint16_t id = topicId;
if (id == 0){
if (id == 0)
{
id = _topicTbl.getTopicId(topicName);
_regMgr.registerTopic(topicName);
}
return id;
}
int LGwProxy::writeMsg(const uint8_t* msg){
int LGwProxy::writeMsg(const uint8_t* msg)
{
uint16_t len;
uint8_t pos;
uint8_t rc = 0;
uint8_t rc = 0;
if (msg[0] == 0x01){
if (msg[0] == 0x01)
{
len = getUint16(msg + 1);
pos = 2;
}else{
}
else
{
len = msg[0];
pos = 1;
}
if (msg[0] == 3 && msg[1] == MQTTSN_TYPE_SEARCHGW){
rc = _network.broadcast(msg,len);
}else
if (msg[0] == 3 && msg[1] == MQTTSN_TYPE_SEARCHGW)
{
if ( _isForwarderMode )
rc = _network.broadcast(msg, len);
}
else
{
if (_isForwarderMode)
{
// create a forwarder encapsulation message WirelessNodeId is a 4bytes fake data
uint8_t* buf = (uint8_t*)malloc(len + 7);
uint8_t* buf = (uint8_t*) malloc(len + 7);
buf[0] = 7;
buf[1] = MQTTSN_TYPE_ENCAPSULATED;
buf[2] = 1;
@@ -371,16 +460,19 @@ int LGwProxy::writeMsg(const uint8_t* msg){
buf[5] = 'I';
buf[6] = 'd';
memcpy(buf + 7, msg, len);
if ( buf)
rc = _network.unicast(buf, len + 7);
if (buf)
rc = _network.unicast(buf, len + 7);
free(buf);
DISPLAY(" Encapsulated\n ");
}else{
rc = _network.unicast(msg,len);
}
else
{
rc = _network.unicast(msg, len);
}
if (rc > 0){
if ( msg[pos] >= MQTTSN_TYPE_ADVERTISE && msg[pos] <= MQTTSN_TYPE_WILLMSGRESP )
if (rc > 0)
{
if (msg[pos] >= MQTTSN_TYPE_ADVERTISE && msg[pos] <= MQTTSN_TYPE_WILLMSGRESP)
{
DISPLAY(" send %s\n", packet_names[msg[pos]]);
}
@@ -389,105 +481,134 @@ int LGwProxy::writeMsg(const uint8_t* msg){
return rc;
}
void LGwProxy::writeGwMsg(void){
void LGwProxy::writeGwMsg(void)
{
_retryCount = MQTTSN_RETRY_COUNT;
writeMsg((const uint8_t*)_msg);
writeMsg((const uint8_t*) _msg);
_sendUTC = time(NULL);
}
int LGwProxy::readMsg(void){
int LGwProxy::readMsg(void)
{
int len = 0;
uint8_t* msg = _network.getMessage(&len);
_mqttsnMsg = msg;
if (len == 0){
if (len == 0)
{
return 0;
}
if (_mqttsnMsg[0] == 0x01){
int msgLen = (int) getUint16((const uint8_t*)_mqttsnMsg + 1);
if (len != msgLen){
if (_mqttsnMsg[0] == 0x01)
{
int msgLen = (int) getUint16((const uint8_t*) _mqttsnMsg + 1);
if (len != msgLen)
{
_mqttsnMsg += 3;
len = msgLen - 3;
}
}else{
}
else
{
_mqttsnMsg += 1;
len -= 1;
}
if ( *_mqttsnMsg == MQTTSN_TYPE_ENCAPSULATED )
if (*_mqttsnMsg == MQTTSN_TYPE_ENCAPSULATED)
{
int lenEncap = len + 1;
if (msg[lenEncap] == 0x01){
int msgLen = (int) getUint16((const uint8_t*)(msg + lenEncap + 1));
if (msg[lenEncap] == 0x01)
{
int msgLen = (int) getUint16((const uint8_t*) (msg + lenEncap + 1));
msg += (lenEncap + 3);
len = msgLen - 3;
}else{
}
else
{
msg += (lenEncap + 1);
len = *(msg - 1);
}
_mqttsnMsg = msg;
DISPLAY(" recv encapslated message\n" );
DISPLAY(" recv encapslated message\n");
}
if ( *_mqttsnMsg >= MQTTSN_TYPE_ADVERTISE && *_mqttsnMsg <= MQTTSN_TYPE_WILLMSGRESP )
if (*_mqttsnMsg >= MQTTSN_TYPE_ADVERTISE && *_mqttsnMsg <= MQTTSN_TYPE_WILLMSGRESP)
{
DISPLAY(" recv %s\n", packet_names[*_mqttsnMsg]);
}
return len;
}
void LGwProxy::setWillTopic(const char* willTopic, uint8_t qos, bool retain){
void LGwProxy::setWillTopic(const char* willTopic, uint8_t qos, bool retain)
{
_willTopic = willTopic;
_retainWill = _qosWill = 0;
if (qos == 1){
if (qos == 1)
{
_qosWill = MQTTSN_FLAG_QOS_1;
}else if (qos == 2){
}
else if (qos == 2)
{
_qosWill = MQTTSN_FLAG_QOS_2;
}
if (retain){
if (retain)
{
_retainWill = MQTTSN_FLAG_RETAIN;
}
}
void LGwProxy::setWillMsg(const char* willMsg){
void LGwProxy::setWillMsg(const char* willMsg)
{
_willMsg = willMsg;
}
void LGwProxy::setCleanSession(bool flg){
if (flg){
void LGwProxy::setCleanSession(bool flg)
{
if (flg)
{
_cleanSession = MQTTSN_FLAG_CLEAN;
}else{
}
else
{
_cleanSession = 0;
}
}
uint16_t LGwProxy::getNextMsgId(void){
uint16_t LGwProxy::getNextMsgId(void)
{
_nextMsgId++;
if (_nextMsgId == 0){
if (_nextMsgId == 0)
{
_nextMsgId = 1;
}
return _nextMsgId;
}
void LGwProxy::checkPingReq(void){
void LGwProxy::checkPingReq(void)
{
uint8_t msg[2];
msg[0] = 0x02;
msg[1] = MQTTSN_TYPE_PINGREQ;
if ( (_status == GW_CONNECTED || _status == GW_SLEPT) && isPingReqRequired() && _pingStatus != GW_WAIT_PINGRESP){
if ((_status == GW_CONNECTED || _status == GW_SLEPT) && isPingReqRequired() && _pingStatus != GW_WAIT_PINGRESP)
{
_pingStatus = GW_WAIT_PINGRESP;
_pingRetryCount = MQTTSN_RETRY_COUNT;
writeMsg((const uint8_t*)msg);
writeMsg((const uint8_t*) msg);
_pingSendUTC = time(NULL);
}else if (_pingStatus == GW_WAIT_PINGRESP){
if (_pingSendUTC + MQTTSN_TIME_RETRY < time(NULL)){
if (--_pingRetryCount > 0){
writeMsg((const uint8_t*)msg);
}
else if (_pingStatus == GW_WAIT_PINGRESP)
{
if (_pingSendUTC + MQTTSN_TIME_RETRY < time(NULL))
{
if (--_pingRetryCount > 0)
{
writeMsg((const uint8_t*) msg);
_pingSendUTC = time(NULL);
}else{
}
else
{
_status = GW_LOST;
_gwId = 0;
_pingStatus = 0;
@@ -498,8 +619,10 @@ void LGwProxy::checkPingReq(void){
}
}
void LGwProxy::checkAdvertise(void){
if ( _gwAliveTimer.isTimeUp()){
void LGwProxy::checkAdvertise(void)
{
if (_gwAliveTimer.isTimeUp())
{
_status = GW_LOST;
_gwId = 0;
_pingStatus = 0;
@@ -509,27 +632,37 @@ void LGwProxy::checkAdvertise(void){
}
}
LTopicTable* LGwProxy::getTopicTable(void){
LTopicTable* LGwProxy::getTopicTable(void)
{
return &_topicTbl;
}
LRegisterManager* LGwProxy::getRegisterManager(void){
LRegisterManager* LGwProxy::getRegisterManager(void)
{
return &_regMgr;
}
bool LGwProxy::isPingReqRequired(void){
bool LGwProxy::isPingReqRequired(void)
{
return _keepAliveTimer.isTimeUp(_tkeepAlive * 1000UL);
}
void LGwProxy::setPingReqTimer(void){
void LGwProxy::setPingReqTimer(void)
{
_keepAliveTimer.start(_tkeepAlive * 1000UL);
}
const char* LGwProxy::getClientId(void) {
const char* LGwProxy::getClientId(void)
{
return _clientId;
}
void LGwProxy::setForwarderMode(void)
void LGwProxy::setForwarderMode(bool valid)
{
_isForwarderMode = true;
_isForwarderMode = valid;
}
void LGwProxy::setQoSMinus1Mode(bool valid)
{
_isQoSMinus1Mode = valid;
}

View File

@@ -65,7 +65,8 @@ public:
void setCleanSession(bool);
void setKeepAliveDuration(uint16_t duration);
void setAdvertiseDuration(uint16_t duration);
void setForwarderMode(void);
void setForwarderMode(bool valid);
void setQoSMinus1Mode(bool valid);
void reconnect(void);
int writeMsg(const uint8_t* msg);
void setPingReqTimer(void);
@@ -109,6 +110,7 @@ private:
uint16_t _tSleep;
uint16_t _tWake;
bool _isForwarderMode;
bool _isQoSMinus1Mode;
char _msg[MQTTSN_MAX_MSG_LENGTH + 1];
};

View File

@@ -27,7 +27,6 @@
======================================*/
//#define DEBUG_NW
//#define DEBUG_MQTTSN
//#define DEBUG_OTA
/****************************************
MQTT-SN Parameters
@@ -105,7 +104,9 @@ typedef enum
#define END_OF_SUBSCRIBE_LIST {MQTTSN_TOPIC_TYPE_NORMAL,0,0,0, 0}
#define UDPCONF LUdpConfig theNetcon
#define MQTTSNCONF LMqttsnConfig theMqcon
#define SetForwarderMode theClient->getGwProxy()->setForwarderMode
#define SetForwarderMode(...) theClient->getGwProxy()->setForwarderMode(__VA_ARGS__)
#define SetQoSMinus1Mode(...) theClient->getGwProxy()->setQoSMinus1Mode(__VA_ARGS__)
#ifdef CLIENT_MODE
#define DISPLAY(...)
#define PROMPT(...)
@@ -142,6 +143,7 @@ typedef enum
#define QoS0 0
#define QoS1 1
#define QoS2 2
#define Q0Sm1 3
#define MQTTSN_TYPE_ADVERTISE 0x00
#define MQTTSN_TYPE_SEARCHGW 0x01
#define MQTTSN_TYPE_GWINFO 0x02
@@ -177,7 +179,7 @@ typedef enum
#define MQTTSN_FLAG_QOS_0 0x0
#define MQTTSN_FLAG_QOS_1 0x20
#define MQTTSN_FLAG_QOS_2 0x40
#define MQTTSN_FLAG_QOS_N1 0xc0
#define MQTTSN_FLAG_QOS_M1 0x60
#define MQTTSN_FLAG_RETAIN 0x10
#define MQTTSN_FLAG_WILL 0x08
#define MQTTSN_FLAG_CLEAN 0x04

View File

@@ -75,7 +75,7 @@ void LPublishManager::publish(const char* topicName, uint8_t* payload, uint16_t
topicType = MQTTSN_TOPIC_TYPE_NORMAL;
}
if ( qos > 0 )
if ( qos > 0 && qos < 3 )
{
msgId = theClient->getGwProxy()->getNextMsgId();
}
@@ -428,6 +428,10 @@ PubElement* LPublishManager::add(const char* topicName, uint16_t topicId, uint8_
{
elm->flag |= MQTTSN_FLAG_QOS_2;
}
else if (qos == 3)
{
elm->flag |= MQTTSN_FLAG_QOS_M1;
}
if (retain)
{
elm->flag |= MQTTSN_FLAG_RETAIN;