From 2067e6fe923c2797c327aa0caeedb599ed432805 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sat, 24 Aug 2019 17:33:42 +0900 Subject: [PATCH 01/43] Validate MsgId of PUBLISH MsgId of QoS0 or QoS-1 should be 0. Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp index 3162f04..98d917a 100644 --- a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp @@ -94,6 +94,12 @@ MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* return nullptr; } + if ( ( qos == 0 || qos == 3 ) && msgId > 0 ) + { + WRITELOG("%s Invalid MsgId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); + return nullptr; + } + if( !topic && msgId && qos > 0 && qos < 3 ) { /* Reply PubAck with INVALID_TOPIC_ID to the client */ From 84b90f9a0cb9b109f6feabf47661028ceb796714 Mon Sep 17 00:00:00 2001 From: Martin Kirsche Date: Sat, 14 Sep 2019 14:38:49 +0200 Subject: [PATCH 02/43] Fix documentation of MQTTSNPacket_decode Added a documentation for the missing parameters buf and buflen and removed the non-existing parameter getcharfn. Signed-off-by: Martin Kirsche --- MQTTSNPacket/src/MQTTSNPacket.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MQTTSNPacket/src/MQTTSNPacket.c b/MQTTSNPacket/src/MQTTSNPacket.c index a788b78..46f8520 100644 --- a/MQTTSNPacket/src/MQTTSNPacket.c +++ b/MQTTSNPacket/src/MQTTSNPacket.c @@ -83,7 +83,8 @@ int MQTTSNPacket_encode(unsigned char* buf, int length) /** * Obtains the MQTT-SN packet length from received data - * @param getcharfn pointer to function to read the next character from the data source + * @param buf the buffer that contains the MQTT-SN packet + * @param buflen the length in bytes of the supplied buffer * @param value the decoded length returned * @return the number of bytes read from the socket */ From 271b635d3f292f5032141caf98ede45b0aa3e085 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Thu, 3 Oct 2019 10:39:44 +0900 Subject: [PATCH 03/43] Fix Typo Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClient.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClient.h | 2 +- MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClient.cpp b/MQTTSNGateway/src/MQTTSNGWClient.cpp index 11c5520..746f1af 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClient.cpp @@ -572,7 +572,7 @@ void Client::resetPingRequest(void) _holdPingRequest = false; } -bool Client::isHoldPringReqest(void) +bool Client::isHoldPingReqest(void) { return _holdPingRequest; } diff --git a/MQTTSNGateway/src/MQTTSNGWClient.h b/MQTTSNGateway/src/MQTTSNGWClient.h index 5529ed0..5071710 100644 --- a/MQTTSNGateway/src/MQTTSNGWClient.h +++ b/MQTTSNGateway/src/MQTTSNGWClient.h @@ -252,7 +252,7 @@ public: void holdPingRequest(void); void resetPingRequest(void); - bool isHoldPringReqest(void); + bool isHoldPingReqest(void); Client* getNextClient(void); diff --git a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp index 98d917a..5ef8d5b 100644 --- a/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPublishHandler.cpp @@ -239,7 +239,7 @@ void MQTTSNPublishHandler::handleRegAck( Client* client, MQTTSNPacket* packet) ev->setClientSendEvent(client, regAck); _gateway->getClientSendQue()->post(ev); } - if (client->isHoldPringReqest() && client->getWaitREGACKPacketList()->getCount() == 0 ) + if (client->isHoldPingReqest() && client->getWaitREGACKPacketList()->getCount() == 0 ) { /* send PINGREQ to the broker */ client->resetPingRequest(); From 7aa44d94d1125074935a523303fe6f46eef70e14 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Thu, 3 Oct 2019 15:13:29 +0900 Subject: [PATCH 04/43] Bugfix of #165 Signed-off-by: tomoaki --- .../src/MQTTSNAggregateConnectionHandler.cpp | 18 ++++++++---------- .../src/MQTTSNGWConnectionHandler.cpp | 18 +++++++----------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp b/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp index dded6df..4e84630 100644 --- a/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp @@ -170,19 +170,17 @@ void MQTTSNAggregateConnectionHandler::handlePingreq(Client* client, MQTTSNPacke sendStoredPublish(client); client->holdPingRequest(); } - else - { - /* create and send PINGRESP to the PacketHandler */ - client->resetPingRequest(); - MQTTGWPacket* pingresp = new MQTTGWPacket(); + /* create and send PINGRESP to the PacketHandler */ + client->resetPingRequest(); - pingresp->setHeader(PINGRESP); + MQTTGWPacket* pingresp = new MQTTGWPacket(); - Event* evt = new Event(); - evt->setBrokerRecvEvent(client, pingresp); - _gateway->getPacketEventQue()->post(evt); - } + pingresp->setHeader(PINGRESP); + + Event* evt = new Event(); + evt->setBrokerRecvEvent(client, pingresp); + _gateway->getPacketEventQue()->post(evt); } void MQTTSNAggregateConnectionHandler::sendStoredPublish(Client* client) diff --git a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp index 33b3ef8..c73b517 100644 --- a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp @@ -275,18 +275,14 @@ void MQTTSNConnectionHandler::handlePingreq(Client* client, MQTTSNPacket* packet if ( ( client->isSleep() || client->isAwake() ) && client->getClientSleepPacket() ) { sendStoredPublish(client); - client->holdPingRequest(); - } - else - { - /* send PINGREQ to the broker */ - client->resetPingRequest(); - MQTTGWPacket* pingreq = new MQTTGWPacket(); - pingreq->setHeader(PINGREQ); - Event* evt = new Event(); - evt->setBrokerSendEvent(client, pingreq); - _gateway->getBrokerSendQue()->post(evt); } + + /* send PINGREQ to the broker */ + MQTTGWPacket* pingreq = new MQTTGWPacket(); + pingreq->setHeader(PINGREQ); + Event* evt = new Event(); + evt->setBrokerSendEvent(client, pingreq); + _gateway->getBrokerSendQue()->post(evt); } void MQTTSNConnectionHandler::sendStoredPublish(Client* client) From 65583a688701d3745ec2f8200c4d2041aefd27f5 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 4 Oct 2019 21:12:00 +0900 Subject: [PATCH 05/43] Add Invalid TopicId check. Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index 5452765..524b3b6 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -416,10 +416,16 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicName, uint16_t topicId, bool aggregate) { + if ( topicId == 0 ) + { + WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n", topicName.c_str()); + return nullptr; + } + if ( strcmp(clientId->cstring, common_topic) == 0 ) { theGateway->getTopics()->add((const char*)topicName.c_str(), topicId); - return 0; + return nullptr; } else { @@ -427,7 +433,7 @@ Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicN if ( _authorize && client == nullptr ) { - return 0; + return nullptr; } /* anonimous clients */ From d6effc80747025041500a8a039efcb66163e4da3 Mon Sep 17 00:00:00 2001 From: Jochen Wilhelmy Date: Fri, 17 Jan 2020 17:40:56 +0100 Subject: [PATCH 06/43] Support for MacOS Split semaphore classes into Semaphore and NamedSemaphore Semaphore is implemented with Grand Central Dispatch NamedSemaphore uses the spin lock approach like in boost for sem_timedwait sem_getvalue is not supported on MacOS and therefore was removed Fixed bug (*gatewayaddress_len > 0) in MQTTSNSearchClient.c Signed-off-by: Jochen Wilhelmy --- MQTTSNGateway/Makefile | 9 +- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWProcess.h | 2 +- MQTTSNGateway/src/linux/Network.cpp | 4 + MQTTSNGateway/src/linux/Threading.cpp | 158 ++++++++++-------- MQTTSNGateway/src/linux/Threading.h | 28 +++- .../src/linux/udp6/SensorNetwork.cpp | 18 +- MQTTSNPacket/src/MQTTSNSearchClient.c | 2 +- 8 files changed, 135 insertions(+), 88 deletions(-) diff --git a/MQTTSNGateway/Makefile b/MQTTSNGateway/Makefile index 1fd2233..731d88e 100644 --- a/MQTTSNGateway/Makefile +++ b/MQTTSNGateway/Makefile @@ -77,6 +77,7 @@ $(SUBDIR)/MQTTSNUnsubscribeServer.c CXX := g++ CPPFLAGS += +# include directories, for MacOS/homebrew add -I/usr/local/opt/openssl/include/ INCLUDE := INCLUDES += $(INCLUDE) -I$(SRCDIR) \ -I$(SRCDIR)/$(OS) \ @@ -84,12 +85,16 @@ INCLUDES += $(INCLUDE) -I$(SRCDIR) \ -I$(SUBDIR) \ -I$(SRCDIR)/$(TEST) +# preprocessor defines DEFS := + +# library search paths, for MacOS/homebrew add -L/usr/local/opt/openssl/lib/ LIB := LIBS += $(LIB) -L/usr/local/lib -LDFLAGS := + +LDFLAGS := CXXFLAGS := -Wall -O3 -std=c++11 -LDADD := -lpthread -lssl -lcrypto -lrt +LDADD := -lpthread -lssl -lcrypto OUTDIR := Build PROG := $(OUTDIR)/$(PROGNAME) diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.cpp b/MQTTSNGateway/src/MQTTSNGWProcess.cpp index 88ba292..ac9c915 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -103,7 +103,7 @@ void Process::initialize(int argc, char** argv) } } } - _rbsem = new Semaphore(MQTTSNGW_RB_SEMAPHOR_NAME, 0); + _rbsem = new NamedSemaphore(MQTTSNGW_RB_SEMAPHOR_NAME, 0); _rb = new RingBuffer(_configDir.c_str()); if (getParam("ShearedMemory", param) == 0) diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.h b/MQTTSNGateway/src/MQTTSNGWProcess.h index 1fb02a9..3d94f2e 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.h +++ b/MQTTSNGateway/src/MQTTSNGWProcess.h @@ -66,7 +66,7 @@ private: string _configDir; string _configFile; RingBuffer* _rb; - Semaphore* _rbsem; + NamedSemaphore* _rbsem; Mutex _mt; int _log; char _rbdata[PROCESS_LOG_BUFFER_SIZE + 1]; diff --git a/MQTTSNGateway/src/linux/Network.cpp b/MQTTSNGateway/src/linux/Network.cpp index 4f6f5a9..8880b90 100644 --- a/MQTTSNGateway/src/linux/Network.cpp +++ b/MQTTSNGateway/src/linux/Network.cpp @@ -145,7 +145,11 @@ bool TCPStack::accept(TCPStack& new_socket) int TCPStack::send(const uint8_t* buf, int length) { +#ifdef __APPLE__ + return ::send(_sockfd, buf, length, SO_NOSIGPIPE); +#else return ::send(_sockfd, buf, length, MSG_NOSIGNAL); +#endif } int TCPStack::recv(uint8_t* buf, int len) diff --git a/MQTTSNGateway/src/linux/Threading.cpp b/MQTTSNGateway/src/linux/Threading.cpp index b0b3ac4..da8cf34 100644 --- a/MQTTSNGateway/src/linux/Threading.cpp +++ b/MQTTSNGateway/src/linux/Threading.cpp @@ -21,27 +21,40 @@ #include #include #include -#include #include #include #include #include +#include using namespace std; using namespace MQTTSNGW; -#if defined(OSX) -int sem_timedwait(sem_type sem, const struct timespec *timeout) +#ifdef __APPLE__ + +int sem_timedwait(sem_t *sem, const struct timespec *abs_timeout) { - int rc = -1; - int64_t tout = timeout->tv_sec * 1000L + tv_nsec * 1000000L - rc = (int)dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, tout)); - if (rc != 0) + while (true) { - rc = ETIMEDOUT; + // try to lock the semaphore + int result = sem_trywait(sem); + if (result != -1 || errno != EAGAIN) + return result; + + // spin lock + sched_yield(); + + // check if timeout reached + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + if (ts.tv_sec > abs_timeout->tv_sec + || (ts.tv_sec == abs_timeout->tv_sec && abs_timeout->tv_nsec >= ts.tv_nsec)) + { + return ETIMEDOUT; + } } - return rc; } + #endif /*===================================== @@ -69,7 +82,7 @@ Mutex::Mutex(const char* fileName) throw Exception( -1, "Mutex can't create a shared memory."); } _pmutex = (pthread_mutex_t*) shmat(_shmid, NULL, 0); - if (_pmutex < 0) + if (_pmutex == (void*) -1) { throw Exception( -1, "Mutex can't attach shared memory."); } @@ -153,21 +166,61 @@ void Mutex::unlock(void) Class Semaphore =====================================*/ -Semaphore::Semaphore() -{ - sem_init(&_sem, 0, 0); - _name = 0; - _psem = 0; -} - Semaphore::Semaphore(unsigned int val) { +#ifdef __APPLE__ + _sem = dispatch_semaphore_create(val); +#else sem_init(&_sem, 0, val); - _name = 0; - _psem = 0; +#endif } -Semaphore::Semaphore(const char* name, unsigned int val) +Semaphore::~Semaphore() +{ +#ifdef __APPLE__ + dispatch_release(_sem); +#else + sem_destroy(&_sem); +#endif +} + +void Semaphore::post(void) +{ +#ifdef __APPLE__ + dispatch_semaphore_signal(_sem); +#else + sem_post(&_sem); +#endif +} + +void Semaphore::wait(void) +{ +#ifdef __APPLE__ + dispatch_semaphore_wait(_sem, DISPATCH_TIME_FOREVER); +#else + sem_wait(&_sem); +#endif +} + +void Semaphore::timedwait(uint16_t millsec) +{ +#ifdef __APPLE__ + dispatch_semaphore_wait(_sem, dispatch_time(DISPATCH_TIME_NOW, int64_t(millsec) * 1000000)); +#else + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + int nsec = ts.tv_nsec + (millsec % 1000) * 1000000; + ts.tv_nsec = nsec % 1000000000; + ts.tv_sec += millsec / 1000 + nsec / 1000000000; + sem_timedwait(&_sem, &ts); +#endif +} + +/*===================================== + Class NamedSemaphore + =====================================*/ + +NamedSemaphore::NamedSemaphore(const char* name, unsigned int val) { _psem = sem_open(name, O_CREAT, 0666, val); if (_psem == SEM_FAILED) @@ -181,67 +234,30 @@ Semaphore::Semaphore(const char* name, unsigned int val) } } -Semaphore::~Semaphore() +NamedSemaphore::~NamedSemaphore() { - if (_name) - { - sem_close(_psem); - sem_unlink(_name); - free(_name); - } - else - { - sem_destroy(&_sem); - } + sem_close(_psem); + sem_unlink(_name); + free(_name); } -void Semaphore::post(void) +void NamedSemaphore::post(void) { - int val = 0; - if (_psem) - { - sem_getvalue(_psem, &val); - if (val <= 0) - { - sem_post(_psem); - } - } - else - { - sem_getvalue(&_sem, &val); - if (val <= 0) - { - sem_post(&_sem); - } - } + sem_post(_psem); } -void Semaphore::wait(void) +void NamedSemaphore::wait(void) { - if (_psem) - { - sem_wait(_psem); - } - else - { - sem_wait(&_sem); - } + sem_wait(_psem); } -void Semaphore::timedwait(uint16_t millsec) +void NamedSemaphore::timedwait(uint16_t millsec) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += millsec / 1000; ts.tv_nsec = (millsec % 1000) * 1000000; - if (_psem) - { - sem_timedwait(_psem, &ts); - } - else - { - sem_timedwait(&_sem, &ts); - } + sem_timedwait(_psem, &ts); } /*========================================= @@ -274,7 +290,7 @@ RingBuffer::RingBuffer(const char* keyDirectory) if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE, IPC_CREAT | IPC_EXCL | 0666)) >= 0) { - if ((_shmaddr = (uint16_t*) shmat(_shmid, NULL, 0)) > 0) + if ((_shmaddr = (uint16_t*) shmat(_shmid, NULL, 0)) != (void*) -1) { _length = (uint16_t*) _shmaddr; _start = (uint16_t*) _length + sizeof(uint16_t*); @@ -290,9 +306,9 @@ RingBuffer::RingBuffer(const char* keyDirectory) throw Exception(-1, "RingBuffer can't attach shared memory."); } } - else if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE, IPC_CREAT | 0666)) >= 0) + else if ((_shmid = shmget(key, PROCESS_LOG_BUFFER_SIZE, IPC_CREAT | 0666)) != -1) { - if ((_shmaddr = (uint16_t*) shmat(_shmid, NULL, 0)) > 0) + if ((_shmaddr = (uint16_t*) shmat(_shmid, NULL, 0)) != (void*) -1) { _length = (uint16_t*) _shmaddr; _start = (uint16_t*) _length + sizeof(uint16_t*); @@ -330,7 +346,7 @@ RingBuffer::~RingBuffer() } } - if (_pmx > 0) + if (_pmx != NULL) { delete _pmx; } diff --git a/MQTTSNGateway/src/linux/Threading.h b/MQTTSNGateway/src/linux/Threading.h index df58f1c..795ee5a 100644 --- a/MQTTSNGateway/src/linux/Threading.h +++ b/MQTTSNGateway/src/linux/Threading.h @@ -19,6 +19,9 @@ #include #include +#ifdef __APPLE__ +#include +#endif #include "MQTTSNGWDefines.h" namespace MQTTSNGW @@ -52,17 +55,34 @@ private: class Semaphore { public: - Semaphore(); - Semaphore(unsigned int val); - Semaphore(const char* name, unsigned int val); + Semaphore(unsigned int val = 0); ~Semaphore(); void post(void); void wait(void); void timedwait(uint16_t millsec); +private: +#ifdef __APPLE__ + dispatch_semaphore_t _sem; +#else + sem_t _sem; +#endif +}; + +/*===================================== + Class NamedSemaphore + ====================================*/ +class NamedSemaphore +{ +public: + NamedSemaphore(const char* name, unsigned int val); + ~NamedSemaphore(); + void post(void); + void wait(void); + void timedwait(uint16_t millsec); + private: sem_t* _psem; - sem_t _sem; char* _name; }; diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp index 5d6b7b8..11affaa 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp @@ -121,11 +121,8 @@ char* SensorNetAddress::getAddress(void) bool SensorNetAddress::isMatch(SensorNetAddress* addr) { - return ((this->_portNo == addr->_portNo) && \ - (this->_IpAddr.sin6_addr.__in6_u.__u6_addr32[0] == addr->_IpAddr.sin6_addr.__in6_u.__u6_addr32[0]) && \ - (this->_IpAddr.sin6_addr.__in6_u.__u6_addr32[1] == addr->_IpAddr.sin6_addr.__in6_u.__u6_addr32[1]) && \ - (this->_IpAddr.sin6_addr.__in6_u.__u6_addr32[2] == addr->_IpAddr.sin6_addr.__in6_u.__u6_addr32[2]) && \ - (this->_IpAddr.sin6_addr.__in6_u.__u6_addr32[3] == addr->_IpAddr.sin6_addr.__in6_u.__u6_addr32[3])); + return (this->_portNo == addr->_portNo) && \ + (memcmp(this->_IpAddr.sin6_addr.s6_addr, addr->_IpAddr.sin6_addr.s6_addr, sizeof(this->_IpAddr.sin6_addr.s6_addr)) == 0); } SensorNetAddress& SensorNetAddress::operator =(SensorNetAddress& addr) @@ -319,8 +316,13 @@ int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadc //if given, set a given device name to bind to if(strlen(interfaceName) > 0) { +#ifdef __APPLE__ + int idx = if_nametoindex(interfaceName); + setsockopt(_sockfdUnicast, IPPROTO_IP, IP_BOUND_IF, &idx, sizeof(idx)); +#else //socket option: bind to a given interface name setsockopt(_sockfdUnicast, SOL_SOCKET, SO_BINDTODEVICE, interfaceName, strlen(interfaceName)); +#endif } //socket option: reuse address @@ -370,7 +372,7 @@ int UDPPort6::unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* add strcpy(destStr, addr->getAddress()); strcat(destStr,"%"); strcat(destStr,_interfaceName); - if(IN6_IS_ADDR_LINKLOCAL(addr->getAddress())) + if(IN6_IS_ADDR_LINKLOCAL(&addr->getIpAddress()->sin6_addr)) { getaddrinfo(destStr, portStr.c_str(), &hints, &res); } @@ -411,8 +413,8 @@ int UDPPort6::broadcast(const uint8_t* buf, uint32_t length) strcpy(destStr, _grpAddr.getAddress()); strcat(destStr,"%"); strcat(destStr,_interfaceName); - if(IN6_IS_ADDR_MC_NODELOCAL(_grpAddr.getAddress()) || - IN6_IS_ADDR_MC_LINKLOCAL(_grpAddr.getAddress())) + if(IN6_IS_ADDR_MC_NODELOCAL(&_grpAddr.getIpAddress()->sin6_addr) || + IN6_IS_ADDR_MC_LINKLOCAL(&_grpAddr.getIpAddress()->sin6_addr)) { err = getaddrinfo(destStr, std::to_string(_uniPortNo).c_str(), &hint, &info ); } diff --git a/MQTTSNPacket/src/MQTTSNSearchClient.c b/MQTTSNPacket/src/MQTTSNSearchClient.c index c55e134..af93c19 100644 --- a/MQTTSNPacket/src/MQTTSNSearchClient.c +++ b/MQTTSNPacket/src/MQTTSNSearchClient.c @@ -116,7 +116,7 @@ int MQTTSNDeserialize_gwinfo(unsigned char* gatewayid, unsigned short* gatewayad *gatewayid = readChar(&curdata); *gatewayaddress_len = enddata - curdata; - *gatewayaddress = (gatewayaddress_len > 0) ? curdata : NULL; + *gatewayaddress = (*gatewayaddress_len > 0) ? curdata : NULL; rc = 1; exit: From 1715d5d47c2b7313c22e1da19bb17321087a8f2e Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 7 Feb 2020 18:21:48 +0900 Subject: [PATCH 07/43] BugFix Adapters PINGREQ and PINGRESP Add Error message when the Aggregate GW receives PUBLISH from the broker. Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 5 +++-- MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 9 +++++---- MQTTSNGateway/src/MQTTSNGWAdapter.cpp | 15 ++++++++++----- MQTTSNGateway/src/MQTTSNGWAdapter.h | 1 + MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp | 10 ++++++++-- MQTTSNGateway/src/MQTTSNGWAggregater.cpp | 6 ++++-- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 5 ++++- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 4 ++-- MQTTSNGateway/src/MQTTSNGWDefines.h | 9 +++++---- MQTTSNGateway/src/MQTTSNGWForwarder.cpp | 1 + MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp | 6 +++--- MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp | 1 - 13 files changed, 47 insertions(+), 27 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index d549065..4f2b3c4 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -1,7 +1,8 @@ # MQTT-SN Transparent / Aggrigating Gateway **MQTT-SN** requires a MQTT-SN Gateway which acts as a protocol converter to convert **MQTT-SN messages to MQTT messages**. MQTT-SN client over SensorNetwork can not communicate directly with MQTT broker(TCP/IP). -This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. +This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. +The Transparent Gateway can not receive PUBLISH message from the broker at this version. ### **step1. Build the gateway** ```` @@ -37,7 +38,7 @@ $ ./MQTT-SNGateway [-f Config file name] # config file of MQTT-SN Gateway # -BrokerName=iot.eclipse.org +BrokerName=mqtt.eclipse.org BrokerPortNo=1883 BrokerSecurePortNo=8883 diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index ce5625d..13a1b35 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -272,8 +272,8 @@ void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* Publish pub; packet->getPUBLISH(&pub); - WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), - RIGHTARROW, client->getClientId(), "is sleeping. a message was saved."); + // Start of temporary code + WRITELOG("%s MQTTGWPublishHandler::handleAggregatePublish Aggregater can't handle a PUBLISH from the broker at the current version.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER); if (pub.header.bits.qos == 1) { @@ -283,11 +283,12 @@ void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* { replyACK(client, &pub, PUBREC); } - + // End of temporary code string* topicName = new string(pub.topic, pub.topiclen); - Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); + Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); // topic deletes topicName when the topic is deleted + AggregateTopicElement* list = _gateway->getAdapterManager()->createClientList(&topic); if ( list != nullptr ) { diff --git a/MQTTSNGateway/src/MQTTSNGWAdapter.cpp b/MQTTSNGateway/src/MQTTSNGWAdapter.cpp index 73cbc29..a38ba67 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapter.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapter.cpp @@ -126,6 +126,11 @@ bool Adapter::isSecure(SensorNetAddress* addr) } } +bool Adapter::isSecure(void) +{ + return _isSecure; +} + void Adapter::setClient(Client* client, bool secure) { if ( secure ) @@ -241,10 +246,10 @@ void Proxy::checkConnection(Client* client) if ( client->isDisconnect() || ( client->isConnecting() && _responseTimer.isTimeup()) ) { client->connectSended(); - _responseTimer.start(QOSM1_PROXY_RESPONSE_DURATION * 1000UL); + _responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL); MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer; options.clientID.cstring = client->getClientId(); - options.duration = QOSM1_PROXY_KEEPALIVE_DURATION; + options.duration = PROXY_KEEPALIVE_DURATION; MQTTSNPacket* packet = new MQTTSNPacket(); packet->setCONNECT(&options); @@ -260,10 +265,10 @@ void Proxy::checkConnection(Client* client) Event* ev = new Event(); ev->setClientRecvEvent(client, packet); _gateway->getPacketEventQue()->post(ev); - _responseTimer.start(QOSM1_PROXY_RESPONSE_DURATION * 1000UL); + _responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL); _isWaitingResp = true; - if ( ++_retryCnt > QOSM1_PROXY_MAX_RETRY_CNT ) + if ( ++_retryCnt > PROXY_MAX_RETRY_CNT ) { client->disconnected(); } @@ -274,7 +279,7 @@ void Proxy::checkConnection(Client* client) void Proxy::resetPingTimer(void) { - _keepAliveTimer.start(QOSM1_PROXY_KEEPALIVE_DURATION * 1000UL); + _keepAliveTimer.start(PROXY_KEEPALIVE_DURATION * 1000UL); } void Proxy::recv(MQTTSNPacket* packet, Client* client) diff --git a/MQTTSNGateway/src/MQTTSNGWAdapter.h b/MQTTSNGateway/src/MQTTSNGWAdapter.h index 8ea4b45..f126bf1 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapter.h +++ b/MQTTSNGateway/src/MQTTSNGWAdapter.h @@ -56,6 +56,7 @@ public: void send(MQTTSNPacket* packet, Client* client); bool isActive(void); bool isSecure(SensorNetAddress* addr); + bool isSecure(void); void savePacket(Client* client, MQTTSNPacket* packet); private: diff --git a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp index 935d58d..512be6d 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp @@ -101,12 +101,18 @@ Client* AdapterManager::getClient(Client& client) _qosm1Proxy->resetPingTimer(secure); } else if ( client.isAggregated() ) - { newClient = _aggregater->getAdapterClient(&client); _aggregater->resetPingTimer(secure); } - + else if ( client.isQoSm1Proxy() ) + { + _qosm1Proxy->resetPingTimer(secure); + } + else if ( client.isAggregater() ) + { + _aggregater->resetPingTimer(secure); + } return newClient; } diff --git a/MQTTSNGateway/src/MQTTSNGWAggregater.cpp b/MQTTSNGateway/src/MQTTSNGWAggregater.cpp index b74ec86..32f4924 100644 --- a/MQTTSNGateway/src/MQTTSNGWAggregater.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAggregater.cpp @@ -44,10 +44,11 @@ void Aggregater::initialize(void) { if (!strcasecmp(param, "YES") ) { - /* Create Aggregated Clients */ + /* Create Aggregated Clients from clients.conf */ _gateway->getClientList()->setClientList(AGGREGATER_TYPE); - string name = _gateway->getGWParams()->gatewayName; + /* Create Aggregater Client */ + string name = string(_gateway->getGWParams()->gatewayName) + "_Aggregater"; setup(name.c_str(), Atype_Aggregater); _isActive = true; } @@ -64,6 +65,7 @@ bool Aggregater::isActive(void) uint16_t Aggregater::msgId(void) { + // Only SecureClient generates msgId to avoid duplication of msgId. Client does not generate it. return Adapter::getSecureClient()->getNextPacketId(); } diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index b886c9a..0821d23 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -127,7 +127,10 @@ void BrokerSendTask::run() { WRITELOG("%s BrokerSendTask: %s can't send a packet to the broker. errno=%d %s %s\n", ERRMSG_HEADER, client->getClientId(), rc == -1 ? errno : 0, strerror(errno), ERRMSG_FOOTER); - client->getNetwork()->close(); + if ( errno != EBADF ) + { + client->getNetwork()->close(); + } /* Disconnect the client */ packet = new MQTTGWPacket(); diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index 524b3b6..776c23e 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -118,7 +118,7 @@ void ClientList::setPredefinedTopics(bool aggrecate) * File format is: * Lines bigning with # are comment line. * ClientId, SensorNetAddress, "unstableLine", "secureConnection" - * in case of UDP, SensorNetAddress format is portNo@IPAddress. + * in case of UDP, SensorNetAddress format is IPAddress:portNo. * if the SensorNetwork is not stable, write unstableLine. * if BrokerConnection is SSL, write secureConnection. * if the client send PUBLISH QoS-1, QoS-1 is required. @@ -380,7 +380,7 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, } else { - MQTTSNString dummyId MQTTSNString_initializer;; + MQTTSNString dummyId MQTTSNString_initializer; dummyId.cstring = strdup(""); client->setClientId(dummyId); free(dummyId.cstring); diff --git a/MQTTSNGateway/src/MQTTSNGWDefines.h b/MQTTSNGateway/src/MQTTSNGWDefines.h index 0cc22b6..c42544e 100644 --- a/MQTTSNGateway/src/MQTTSNGWDefines.h +++ b/MQTTSNGateway/src/MQTTSNGWDefines.h @@ -42,13 +42,14 @@ namespace MQTTSNGW #define MAX_INFLIGHTMESSAGES (10) // Number of inflight messages #define MAX_MESSAGEID_TABLE_SIZE (500) // Number of MessageIdTable size #define MAX_SAVED_PUBLISH (20) // Max number of PUBLISH message for Asleep state -#define MAX_TOPIC_PAR_CLIENT (50) // Max Topic count for a client. it should be less than 256 +#define MAX_TOPIC_PAR_CLIENT (50) // Max Topic count for a client. it should be less than 256 #define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size (5+2+TopicLen+PayloadLen + Foward Encapsulation) #define SIZE_OF_LOG_PACKET (500) // Length of the packet log in bytes -#define QOSM1_PROXY_KEEPALIVE_DURATION 900 // Secs -#define QOSM1_PROXY_RESPONSE_DURATION 10 // Secs -#define QOSM1_PROXY_MAX_RETRY_CNT 3 +#define PROXY_KEEPALIVE_DURATION (900) // Secs +#define PROXY_RESPONSE_DURATION (10) // Secs +#define PROXY_MAX_RETRY_CNT (3) + /*================================= * Data Type ==================================*/ diff --git a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp index 5fa2a49..8449c85 100644 --- a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp +++ b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp @@ -54,6 +54,7 @@ void ForwarderList::initialize(Gateway* gw) { if (!strcasecmp(param, "YES") ) { + /* Create Fowarders from clients.conf */ gw->getClientList()->setClientList(FORWARDER_TYPE); } } diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index 47f1eae..a10749f 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -148,7 +148,7 @@ void PacketHandleTask::run() if ( adpMgr->isAggregatedClient(client) ) { - aggregatePacketHandler(client, snPacket); + aggregatePacketHandler(client, snPacket); // client is converted to Aggregater by BrokerSendTask } else { diff --git a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp index 50ff56c..7876fc0 100644 --- a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp +++ b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp @@ -51,11 +51,11 @@ void QoSm1Proxy::initialize(void) { if (strcasecmp(param, "YES") == 0 ) { - /* Create QoS-1 Clients */ + /* Create QoS-1 Clients from clients.conf */ _gateway->getClientList()->setClientList(QOSM1PROXY_TYPE); - /* initialize Adapter */ - string name = string(_gateway->getGWParams()->gatewayName) + "QoS-1"; + /* Create a client for QoS-1 proxy */ + string name = string(_gateway->getGWParams()->gatewayName) + "_QoS-1"; setup(name.c_str(), Atype_QoSm1Proxy); _isActive = true; } diff --git a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp index dceb864..45b2964 100644 --- a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp @@ -223,7 +223,6 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack WRITELOG("%s MQTTSNSubscribeHandler can't create MessageIdTableElement %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); return; } -WRITELOG("msgId=%d\n",msgId); subscribe->setMsgId(msgId); Event* ev = new Event(); ev->setBrokerSendEvent(client, subscribe); From 7246ea2c8a46df20f80a3818375a69e91a709af4 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 10 Feb 2020 17:23:41 +0900 Subject: [PATCH 08/43] a --- MQTTSNGateway/src/MQTTSNGWDefines.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWDefines.h b/MQTTSNGateway/src/MQTTSNGWDefines.h index c42544e..d73827d 100644 --- a/MQTTSNGateway/src/MQTTSNGWDefines.h +++ b/MQTTSNGateway/src/MQTTSNGWDefines.h @@ -46,8 +46,8 @@ namespace MQTTSNGW #define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size (5+2+TopicLen+PayloadLen + Foward Encapsulation) #define SIZE_OF_LOG_PACKET (500) // Length of the packet log in bytes -#define PROXY_KEEPALIVE_DURATION (900) // Secs -#define PROXY_RESPONSE_DURATION (10) // Secs +#define PROXY_KEEPALIVE_DURATION (900) // Seconds +#define PROXY_RESPONSE_DURATION (10) // Seconds #define PROXY_MAX_RETRY_CNT (3) /*================================= From f1fefd47cea62d250359aeceb1d61f86a9a485fa Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 7 Feb 2020 18:21:48 +0900 Subject: [PATCH 09/43] BugFix Adapters PINGREQ and PINGRESP Add Error message when the Aggregate GW receives PUBLISH from the broker. Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 5 +++-- MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 9 +++++---- MQTTSNGateway/src/MQTTSNGWAdapter.cpp | 15 ++++++++++----- MQTTSNGateway/src/MQTTSNGWAdapter.h | 1 + MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp | 10 ++++++++-- MQTTSNGateway/src/MQTTSNGWAggregater.cpp | 6 ++++-- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 5 ++++- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 4 ++-- MQTTSNGateway/src/MQTTSNGWDefines.h | 9 +++++---- MQTTSNGateway/src/MQTTSNGWForwarder.cpp | 1 + MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp | 6 +++--- MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp | 1 - 13 files changed, 47 insertions(+), 27 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index d549065..4f2b3c4 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -1,7 +1,8 @@ # MQTT-SN Transparent / Aggrigating Gateway **MQTT-SN** requires a MQTT-SN Gateway which acts as a protocol converter to convert **MQTT-SN messages to MQTT messages**. MQTT-SN client over SensorNetwork can not communicate directly with MQTT broker(TCP/IP). -This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. +This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. +The Transparent Gateway can not receive PUBLISH message from the broker at this version. ### **step1. Build the gateway** ```` @@ -37,7 +38,7 @@ $ ./MQTT-SNGateway [-f Config file name] # config file of MQTT-SN Gateway # -BrokerName=iot.eclipse.org +BrokerName=mqtt.eclipse.org BrokerPortNo=1883 BrokerSecurePortNo=8883 diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index ce5625d..13a1b35 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -272,8 +272,8 @@ void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* Publish pub; packet->getPUBLISH(&pub); - WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), - RIGHTARROW, client->getClientId(), "is sleeping. a message was saved."); + // Start of temporary code + WRITELOG("%s MQTTGWPublishHandler::handleAggregatePublish Aggregater can't handle a PUBLISH from the broker at the current version.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER); if (pub.header.bits.qos == 1) { @@ -283,11 +283,12 @@ void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* { replyACK(client, &pub, PUBREC); } - + // End of temporary code string* topicName = new string(pub.topic, pub.topiclen); - Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); + Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); // topic deletes topicName when the topic is deleted + AggregateTopicElement* list = _gateway->getAdapterManager()->createClientList(&topic); if ( list != nullptr ) { diff --git a/MQTTSNGateway/src/MQTTSNGWAdapter.cpp b/MQTTSNGateway/src/MQTTSNGWAdapter.cpp index 73cbc29..a38ba67 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapter.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapter.cpp @@ -126,6 +126,11 @@ bool Adapter::isSecure(SensorNetAddress* addr) } } +bool Adapter::isSecure(void) +{ + return _isSecure; +} + void Adapter::setClient(Client* client, bool secure) { if ( secure ) @@ -241,10 +246,10 @@ void Proxy::checkConnection(Client* client) if ( client->isDisconnect() || ( client->isConnecting() && _responseTimer.isTimeup()) ) { client->connectSended(); - _responseTimer.start(QOSM1_PROXY_RESPONSE_DURATION * 1000UL); + _responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL); MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer; options.clientID.cstring = client->getClientId(); - options.duration = QOSM1_PROXY_KEEPALIVE_DURATION; + options.duration = PROXY_KEEPALIVE_DURATION; MQTTSNPacket* packet = new MQTTSNPacket(); packet->setCONNECT(&options); @@ -260,10 +265,10 @@ void Proxy::checkConnection(Client* client) Event* ev = new Event(); ev->setClientRecvEvent(client, packet); _gateway->getPacketEventQue()->post(ev); - _responseTimer.start(QOSM1_PROXY_RESPONSE_DURATION * 1000UL); + _responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL); _isWaitingResp = true; - if ( ++_retryCnt > QOSM1_PROXY_MAX_RETRY_CNT ) + if ( ++_retryCnt > PROXY_MAX_RETRY_CNT ) { client->disconnected(); } @@ -274,7 +279,7 @@ void Proxy::checkConnection(Client* client) void Proxy::resetPingTimer(void) { - _keepAliveTimer.start(QOSM1_PROXY_KEEPALIVE_DURATION * 1000UL); + _keepAliveTimer.start(PROXY_KEEPALIVE_DURATION * 1000UL); } void Proxy::recv(MQTTSNPacket* packet, Client* client) diff --git a/MQTTSNGateway/src/MQTTSNGWAdapter.h b/MQTTSNGateway/src/MQTTSNGWAdapter.h index 8ea4b45..f126bf1 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapter.h +++ b/MQTTSNGateway/src/MQTTSNGWAdapter.h @@ -56,6 +56,7 @@ public: void send(MQTTSNPacket* packet, Client* client); bool isActive(void); bool isSecure(SensorNetAddress* addr); + bool isSecure(void); void savePacket(Client* client, MQTTSNPacket* packet); private: diff --git a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp index 935d58d..512be6d 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp @@ -101,12 +101,18 @@ Client* AdapterManager::getClient(Client& client) _qosm1Proxy->resetPingTimer(secure); } else if ( client.isAggregated() ) - { newClient = _aggregater->getAdapterClient(&client); _aggregater->resetPingTimer(secure); } - + else if ( client.isQoSm1Proxy() ) + { + _qosm1Proxy->resetPingTimer(secure); + } + else if ( client.isAggregater() ) + { + _aggregater->resetPingTimer(secure); + } return newClient; } diff --git a/MQTTSNGateway/src/MQTTSNGWAggregater.cpp b/MQTTSNGateway/src/MQTTSNGWAggregater.cpp index b74ec86..32f4924 100644 --- a/MQTTSNGateway/src/MQTTSNGWAggregater.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAggregater.cpp @@ -44,10 +44,11 @@ void Aggregater::initialize(void) { if (!strcasecmp(param, "YES") ) { - /* Create Aggregated Clients */ + /* Create Aggregated Clients from clients.conf */ _gateway->getClientList()->setClientList(AGGREGATER_TYPE); - string name = _gateway->getGWParams()->gatewayName; + /* Create Aggregater Client */ + string name = string(_gateway->getGWParams()->gatewayName) + "_Aggregater"; setup(name.c_str(), Atype_Aggregater); _isActive = true; } @@ -64,6 +65,7 @@ bool Aggregater::isActive(void) uint16_t Aggregater::msgId(void) { + // Only SecureClient generates msgId to avoid duplication of msgId. Client does not generate it. return Adapter::getSecureClient()->getNextPacketId(); } diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index b886c9a..0821d23 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -127,7 +127,10 @@ void BrokerSendTask::run() { WRITELOG("%s BrokerSendTask: %s can't send a packet to the broker. errno=%d %s %s\n", ERRMSG_HEADER, client->getClientId(), rc == -1 ? errno : 0, strerror(errno), ERRMSG_FOOTER); - client->getNetwork()->close(); + if ( errno != EBADF ) + { + client->getNetwork()->close(); + } /* Disconnect the client */ packet = new MQTTGWPacket(); diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index 524b3b6..776c23e 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -118,7 +118,7 @@ void ClientList::setPredefinedTopics(bool aggrecate) * File format is: * Lines bigning with # are comment line. * ClientId, SensorNetAddress, "unstableLine", "secureConnection" - * in case of UDP, SensorNetAddress format is portNo@IPAddress. + * in case of UDP, SensorNetAddress format is IPAddress:portNo. * if the SensorNetwork is not stable, write unstableLine. * if BrokerConnection is SSL, write secureConnection. * if the client send PUBLISH QoS-1, QoS-1 is required. @@ -380,7 +380,7 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, } else { - MQTTSNString dummyId MQTTSNString_initializer;; + MQTTSNString dummyId MQTTSNString_initializer; dummyId.cstring = strdup(""); client->setClientId(dummyId); free(dummyId.cstring); diff --git a/MQTTSNGateway/src/MQTTSNGWDefines.h b/MQTTSNGateway/src/MQTTSNGWDefines.h index 0cc22b6..d73827d 100644 --- a/MQTTSNGateway/src/MQTTSNGWDefines.h +++ b/MQTTSNGateway/src/MQTTSNGWDefines.h @@ -42,13 +42,14 @@ namespace MQTTSNGW #define MAX_INFLIGHTMESSAGES (10) // Number of inflight messages #define MAX_MESSAGEID_TABLE_SIZE (500) // Number of MessageIdTable size #define MAX_SAVED_PUBLISH (20) // Max number of PUBLISH message for Asleep state -#define MAX_TOPIC_PAR_CLIENT (50) // Max Topic count for a client. it should be less than 256 +#define MAX_TOPIC_PAR_CLIENT (50) // Max Topic count for a client. it should be less than 256 #define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size (5+2+TopicLen+PayloadLen + Foward Encapsulation) #define SIZE_OF_LOG_PACKET (500) // Length of the packet log in bytes -#define QOSM1_PROXY_KEEPALIVE_DURATION 900 // Secs -#define QOSM1_PROXY_RESPONSE_DURATION 10 // Secs -#define QOSM1_PROXY_MAX_RETRY_CNT 3 +#define PROXY_KEEPALIVE_DURATION (900) // Seconds +#define PROXY_RESPONSE_DURATION (10) // Seconds +#define PROXY_MAX_RETRY_CNT (3) + /*================================= * Data Type ==================================*/ diff --git a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp index 5fa2a49..8449c85 100644 --- a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp +++ b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp @@ -54,6 +54,7 @@ void ForwarderList::initialize(Gateway* gw) { if (!strcasecmp(param, "YES") ) { + /* Create Fowarders from clients.conf */ gw->getClientList()->setClientList(FORWARDER_TYPE); } } diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index 47f1eae..a10749f 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -148,7 +148,7 @@ void PacketHandleTask::run() if ( adpMgr->isAggregatedClient(client) ) { - aggregatePacketHandler(client, snPacket); + aggregatePacketHandler(client, snPacket); // client is converted to Aggregater by BrokerSendTask } else { diff --git a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp index 50ff56c..7876fc0 100644 --- a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp +++ b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp @@ -51,11 +51,11 @@ void QoSm1Proxy::initialize(void) { if (strcasecmp(param, "YES") == 0 ) { - /* Create QoS-1 Clients */ + /* Create QoS-1 Clients from clients.conf */ _gateway->getClientList()->setClientList(QOSM1PROXY_TYPE); - /* initialize Adapter */ - string name = string(_gateway->getGWParams()->gatewayName) + "QoS-1"; + /* Create a client for QoS-1 proxy */ + string name = string(_gateway->getGWParams()->gatewayName) + "_QoS-1"; setup(name.c_str(), Atype_QoSm1Proxy); _isActive = true; } diff --git a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp index dceb864..45b2964 100644 --- a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp @@ -223,7 +223,6 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack WRITELOG("%s MQTTSNSubscribeHandler can't create MessageIdTableElement %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); return; } -WRITELOG("msgId=%d\n",msgId); subscribe->setMsgId(msgId); Event* ev = new Event(); ev->setBrokerSendEvent(client, subscribe); From 768123b76fe2225e91f36976138bf6ab3a64e815 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 11 Feb 2020 16:21:46 +0900 Subject: [PATCH 10/43] Bugfix of #171 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp index 0f180c9..87aace9 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp @@ -161,7 +161,7 @@ void BrokerRecvTask::run(void) delete packet; - if ( (rc == -1 || rc == -2) && client->isActive() ) + if ( (rc == -1 || rc == -2) && ( client->isActive() || client->isSleep() || client->isAwake() )) { /* disconnect the client */ packet = new MQTTGWPacket(); diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index a10749f..bbe16ff 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -359,6 +359,9 @@ void PacketHandleTask::transparentPacketHandler(Client*client, MQTTGWPacket* pac case UNSUBACK: _mqttSubscribe->handleUnsuback(client, packet); break; + case DISCONNECT: + client->disconnected(); // Just change Client's status to "Disconnected" + break; default: break; } From b8129d678109685a3585a23e1728ba2b367f0946 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 11 Feb 2020 18:30:33 +0900 Subject: [PATCH 11/43] Add a new function to set TTL/Hops of SensorNetwork(UDP and UDP6) #175 Parameters to set them in the gateway.conf, MulticastTTL=1 for UDP GatewayUDP6Hops=1 for UDP6 Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 8 ++++++++ MQTTSNGateway/gateway.conf | 2 ++ MQTTSNGateway/src/MQTTSNGWVersion.h | 2 +- MQTTSNGateway/src/linux/udp/SensorNetwork.cpp | 20 ++++++++++++++++--- MQTTSNGateway/src/linux/udp/SensorNetwork.h | 4 ++-- .../src/linux/udp6/SensorNetwork.cpp | 18 +++++++++++++++-- MQTTSNGateway/src/linux/udp6/SensorNetwork.h | 4 ++-- 7 files changed, 48 insertions(+), 10 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 4f2b3c4..819ed0b 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -73,6 +73,14 @@ KeepAlive=900 GatewayPortNo=10000 MulticastIP=225.1.1.1 MulticastPortNo=1883 +MulticastTTL=1 + +# UDP6 +GatewayUDP6Bind=FFFF:FFFE::1 +GatewayUDP6Port=10000 +GatewayUDP6Broadcast=FF02::1 +GatewayUDP6If=wpan0 +GatewayUDP6Hops=1 # XBee Baudrate=38400 diff --git a/MQTTSNGateway/gateway.conf b/MQTTSNGateway/gateway.conf index d01903a..73f110e 100644 --- a/MQTTSNGateway/gateway.conf +++ b/MQTTSNGateway/gateway.conf @@ -49,12 +49,14 @@ KeepAlive=900 GatewayPortNo=10000 MulticastIP=225.1.1.1 MulticastPortNo=1883 +MulticastTTL=1 # UDP6 GatewayUDP6Bind=FFFF:FFFE::1 GatewayUDP6Port=10000 GatewayUDP6Broadcast=FF02::1 GatewayUDP6If=wpan0 +GatewayUDP6Hops=1 # XBee Baudrate=38400 diff --git a/MQTTSNGateway/src/MQTTSNGWVersion.h b/MQTTSNGateway/src/MQTTSNGWVersion.h index ff9d6e1..9d68241 100644 --- a/MQTTSNGateway/src/MQTTSNGWVersion.h +++ b/MQTTSNGateway/src/MQTTSNGWVersion.h @@ -17,6 +17,6 @@ #ifndef MQTTSNGWVERSION_H_IN_ #define MQTTSNGWVERSION_H_IN_ -#define PAHO_GATEWAY_VERSION "1.3.1" +#define PAHO_GATEWAY_VERSION "1.3.2" #endif /* MQTTSNGWVERSION_H_IN_ */ diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp index 6f08481..5ca191d 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp @@ -185,7 +185,7 @@ int SensorNetwork::initialize(void) uint16_t multicastPortNo = 0; uint16_t unicastPortNo = 0; string ip; - + unsigned int ttl = 1; /* * theProcess->getParam( ) copies * a text specified by "Key" into param[] from the Gateway.conf @@ -216,9 +216,15 @@ int SensorNetwork::initialize(void) _description += " Gateway Port "; _description += param; } + if (theProcess->getParam("MulticastTTL", param) == 0) + { + ttl = atoi(param); + _description += " TTL: "; + _description += param; + } /* Prepare UDP sockets */ - return UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo); + return UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo, ttl); } const char* SensorNetwork::getDescription(void) @@ -261,7 +267,7 @@ void UDPPort::close(void) } } -int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo) +int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, unsigned int ttl) { char loopch = 0; const int reuse = 1; @@ -275,6 +281,7 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN uint32_t ip = inet_addr(ipAddress); _grpAddr.setAddress(ip, htons(multiPortNo)); _clientAddr.setAddress(ip, htons(uniPortNo)); + _ttl = ttl; /*------ Create unicast socket --------*/ _sockfdUnicast = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); @@ -342,6 +349,13 @@ int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortN return -1; } + if (setsockopt(_sockfdMulticast, IPPROTO_IP, IP_MULTICAST_TTL, &ttl,sizeof(ttl)) < 0) + { + D_NWSTACK("error Multicast IP_ADD_MEMBERSHIP in UDPPort::open\n"); + close(); + return -1; + } + if (setsockopt(_sockfdUnicast, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { D_NWSTACK("error Unicast IP_ADD_MEMBERSHIP in UDPPort::open\n"); diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.h b/MQTTSNGateway/src/linux/udp/SensorNetwork.h index 07967f8..735a3c5 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.h +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.h @@ -60,7 +60,7 @@ public: UDPPort(); virtual ~UDPPort(); - int open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo); + int open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo, unsigned int hops); void close(void); int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr); int broadcast(const uint8_t* buf, uint32_t length); @@ -76,7 +76,7 @@ private: SensorNetAddress _grpAddr; SensorNetAddress _clientAddr; bool _disconReq; - + unsigned int _ttl; }; /*=========================================== diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp index 11affaa..8137a1c 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp @@ -175,6 +175,7 @@ int SensorNetwork::initialize(void) string ip; string broadcast; string interface; + unsigned int hops = 1; if (theProcess->getParam("GatewayUDP6Bind", param) == 0) { @@ -200,8 +201,14 @@ int SensorNetwork::initialize(void) _description += " Interface: "; _description += param; } + if (theProcess->getParam("GatewayUDP6Hops", param) == 0) + { + hops = atoi(param); + _description += " Hops: "; + _description += param; + } - return UDPPort6::open(ip.c_str(), unicastPortNo, broadcast.c_str(), interface.c_str()); + return UDPPort6::open(ip.c_str(), unicastPortNo, broadcast.c_str(), interface.c_str(), hops); } const char* SensorNetwork::getDescription(void) @@ -244,7 +251,7 @@ void UDPPort6::close(void) } } -int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName) +int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName, unsigned int hops) { struct addrinfo hints, *res; int errnu; @@ -291,8 +298,15 @@ int UDPPort6::open(const char* ipAddress, uint16_t uniPortNo, const char* broadc WRITELOG("UDP6::open - limit IPv6: %s",strerror(errnu)); return errnu; } + errnu = setsockopt(_sockfdMulticast, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &hops,sizeof(hops)); + if(errnu <0) + { + WRITELOG("UDP6::open - limit HOPS: %s",strerror(errnu)); + return errnu; + } _uniPortNo = uniPortNo; + _hops = hops; freeaddrinfo(res); //init the structs for getaddrinfo diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.h b/MQTTSNGateway/src/linux/udp6/SensorNetwork.h index 62c297f..1dbbe5c 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.h +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.h @@ -66,7 +66,7 @@ public: UDPPort6(); virtual ~UDPPort6(); - int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName); + int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName, unsigned int ttl); void close(void); int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr); int broadcast(const uint8_t* buf, uint32_t length); @@ -84,7 +84,7 @@ private: SensorNetAddress _clientAddr; uint16_t _uniPortNo; bool _disconReq; - + unsigned int _ttl; }; /*=========================================== From 9d6b9d46ef720a620d04e25fdaa6c547422bb11b Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 11 Feb 2020 19:08:50 +0900 Subject: [PATCH 12/43] Add a new function to set TTL/Hops of SensorNetwork(UDP and UDP6) #175 Add Parameters to the gateway.conf to set those values. MulticastTTL=1 for UDP GatewayUDP6Hops=1 for UDP6 Signed-off-by: tomoaki --- MQTTSNGateway/src/linux/udp6/SensorNetwork.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.h b/MQTTSNGateway/src/linux/udp6/SensorNetwork.h index 1dbbe5c..0e3ab7e 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.h +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.h @@ -66,7 +66,7 @@ public: UDPPort6(); virtual ~UDPPort6(); - int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName, unsigned int ttl); + int open(const char* ipAddress, uint16_t uniPortNo, const char* broadcastAddr, const char* interfaceName, unsigned int hops); void close(void); int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr); int broadcast(const uint8_t* buf, uint32_t length); @@ -84,7 +84,7 @@ private: SensorNetAddress _clientAddr; uint16_t _uniPortNo; bool _disconReq; - unsigned int _ttl; + unsigned int _hops; }; /*=========================================== From 731a39255932c4e149ff702a5cb6e4c82c590861 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 11 Feb 2020 20:19:49 +0900 Subject: [PATCH 13/43] Add info to README #179 Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 819ed0b..59be27d 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -6,7 +6,7 @@ The Transparent Gateway can not receive PUBLISH message from the broker at this ### **step1. Build the gateway** ```` -$ git clone -b experiment https://github.com/eclipse/paho.mqtt-sn.embedded-c +$ git clone -b develop https://github.com/eclipse/paho.mqtt-sn.embedded-c $ cd paho.mqtt-sn.embedded-c/MQTTSNGateway $ make [SENSORNET={udp6|xbee}] $ make install @@ -28,7 +28,15 @@ $ make install INSTALL_DIR=/path/to/your_directory CONFIG_DIR=/path/to/your_dire $ cd ../ $ ./MQTT-SNGateway [-f Config file name] ```` - +If you get the error message as follows: +```` +what(): RingBuffer can't create a shared memory. +Aborted (core dumped) +```` +You have to start using sudo command only once for the first time. +```` +$ sudo ./MQTT-SNGateway [-f Config file name] +```` ### **How to Change the configuration of the gateway** **../gateway.conf** Contents are follows: From 1b08591fad433b47bc0b87c4d9455a19187e3c18 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sat, 15 Feb 2020 18:24:51 +0900 Subject: [PATCH 14/43] BugFix of #181 --- MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp index 512be6d..25c3754 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp @@ -127,8 +127,8 @@ int AdapterManager::unicastToClient(Client* client, MQTTSNPacket* packet, Client MQTTSNGWEncapsulatedPacket encap(packet); WirelessNodeId* wnId = fwd->getWirelessNodeId(client); encap.setWirelessNodeId(wnId); - WRITELOG(FORMAT_Y_W_G, currentDateTime(), encap.getName(), RIGHTARROW, fwd->getId(), encap.print(pbuf)); task->log(client, packet); + WRITELOG(FORMAT_Y_W_G, currentDateTime(), encap.getName(), RIGHTARROW, fwd->getId(), encap.print(pbuf)); rc = encap.unicast(_gateway->getSensorNetwork(),fwd->getSensorNetAddr()); } else diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index 5957cae..6e1ea6a 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -145,10 +145,12 @@ void ClientRecvTask::run() } } } + else + { + client = _gateway->getClientList()->getClient(senderAddr); + } } - client = _gateway->getClientList()->getClient(senderAddr); - if ( client ) { /* write log and post Event */ @@ -217,11 +219,11 @@ void ClientRecvTask::run() log(client, packet, 0); if ( packet->getType() == MQTTSN_ENCAPSULATED ) { - WRITELOG("%s Forwarder(%s) is not declared by ClientList file. message has been discarded.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER); + WRITELOG("%s MQTTSNGWClientRecvTask Forwarder(%s) is not declared by ClientList file. message has been discarded.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER); } else { - WRITELOG("%s Client(%s) is not connecting. message has been discarded.%s\n", ERRMSG_HEADER, senderAddr->sprint(buf), ERRMSG_FOOTER); + WRITELOG("%s MQTTSNGWClientRecvTask Client(%s) is not connecting. message has been discarded.%s\n", ERRMSG_HEADER, senderAddr->sprint(buf), ERRMSG_FOOTER); } delete packet; } From c37d9334d4b45f653f98b9c75ec1b31ac85df470 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 18 Feb 2020 12:56:00 +0900 Subject: [PATCH 15/43] update README --- MQTTSNGateway/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 59be27d..9b05649 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -2,7 +2,7 @@ **MQTT-SN** requires a MQTT-SN Gateway which acts as a protocol converter to convert **MQTT-SN messages to MQTT messages**. MQTT-SN client over SensorNetwork can not communicate directly with MQTT broker(TCP/IP). This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. -The Transparent Gateway can not receive PUBLISH message from the broker at this version. +The Aggregating Gateway can not receive PUBLISH message from the broker at this version. ### **step1. Build the gateway** ```` From ae462989ecaf7d18694aca1e1a8227cda344031f Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 21 Feb 2020 11:29:37 +0900 Subject: [PATCH 16/43] BugFix ClientList file name is not shown Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 42 ++++++------------------ MQTTSNGateway/src/MQTTSNGateway.cpp | 20 +++++++++++ 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index 776c23e..80bdfa6 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -67,25 +67,13 @@ void ClientList::setClientList(int type) { char param[MQTTSNGW_PARAM_MAX]; string fileName; - GatewayParams* params = theGateway->getGWParams(); - if (theGateway->getParam("ClientsList", param) == 0) - { - fileName = string(param); - } - else - { - fileName = params->configDir + string(CLIENT_LIST); - } + theGateway->getParam("ClientsList", param); + fileName = string(param); if (!createList(fileName.c_str(), type)) { throw Exception("ClientList::initialize(): No client list defined by the configuration."); } - - if ( params->clientListName == nullptr ) - { - params->clientListName = strdup(fileName.c_str()); - } } void ClientList::setPredefinedTopics(bool aggrecate) @@ -93,21 +81,11 @@ void ClientList::setPredefinedTopics(bool aggrecate) char param[MQTTSNGW_PARAM_MAX]; string fileName; - GatewayParams* params = theGateway->getGWParams(); - if (theGateway->getParam("PredefinedTopicList", param) == 0) - { - fileName = string(param); - } - else - { - fileName = params->configDir + string(PREDEFINEDTOPIC_FILE); - } + theGateway->getParam("PredefinedTopicList", param); + fileName = string(param); - if ( readPredefinedList(fileName.c_str(), aggrecate) ) - { - params->predefinedTopicFileName = strdup(fileName.c_str()); - } + readPredefinedList(fileName.c_str(), aggrecate); } /** @@ -125,11 +103,11 @@ void ClientList::setPredefinedTopics(bool aggrecate) * * Ex: * #Client List - * ClientId1,11200@192.168.10.10 - * ClientID2,35000@192.168.50.200,unstableLine - * ClientID3,40000@192.168.200.50,secureConnection - * ClientID4,41000@192.168.200.51,unstableLine,secureConnection - * ClientID5,41000@192.168.200.51,unstableLine,secureConnection,QoS-1 + * ClientId1,192.168.10.10:11200 + * ClientID2,192.168.50.200:35000,unstableLine + * ClientID3,192.168.200.50:40000,secureConnection + * ClientID4,192.168.200.51:41000,unstableLine,secureConnection + * ClientID5,192.168.200.51:41000,unstableLine,secureConnection,QoS-1 */ bool ClientList::createList(const char* fileName, int type) diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index c5894ce..487e745 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -215,6 +215,26 @@ void Gateway::initialize(int argc, char** argv) } } + if (getParam("ClientsList", param) == 0) + { + _params.clientListName = strdup(param); + } + + if (getParam("PredefinedTopicList", param) == 0) + { + _params.predefinedTopicFileName = strdup(param); + } + + if ( _params.clientListName == nullptr ) + { + _params.clientListName = strdup(( _params.configDir + string(CLIENT_LIST) ).c_str()); + } + + if ( _params.predefinedTopicFileName == nullptr ) + { + _params.predefinedTopicFileName = strdup(( _params.configDir + string(PREDEFINEDTOPIC_FILE) ).c_str()); + } + /* ClientList and Adapters Initialize */ _adapterManager->initialize(); From 1b6e6faab5fc6f3162cf1d07b2d658cc1af93e9e Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 21 Feb 2020 14:06:58 +0900 Subject: [PATCH 17/43] Update README #180 Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 2 +- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 9b05649..4765264 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -25,7 +25,7 @@ $ make install INSTALL_DIR=/path/to/your_directory CONFIG_DIR=/path/to/your_dire ### **step2. Execute the Gateway.** ```` -$ cd ../ +$ cd ../../ $ ./MQTT-SNGateway [-f Config file name] ```` If you get the error message as follows: diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index 6e1ea6a..f42d102 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -145,10 +145,7 @@ void ClientRecvTask::run() } } } - else - { - client = _gateway->getClientList()->getClient(senderAddr); - } + client = _gateway->getClientList()->getClient(senderAddr); } if ( client ) From f57d391b16bb3a100ce394bdce12a66198dffc90 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 21 Feb 2020 18:49:58 +0900 Subject: [PATCH 18/43] Change outage logs to be nicely. Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGateway.cpp | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp index 87aace9..99b0ce5 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerRecvTask.cpp @@ -64,7 +64,7 @@ void BrokerRecvTask::run(void) _light->blueLight(false); if (CHK_SIGINT) { - WRITELOG("%s BrokerRecvTask stopped.\n", currentDateTime()); + WRITELOG("\n%s BrokerRecvTask stopped.", currentDateTime()); return; } timeout.tv_sec = 0; diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index 0821d23..385ad87 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -70,7 +70,7 @@ void BrokerSendTask::run() if ( ev->getEventType() == EtStop ) { - WRITELOG("%s BrokerSendTask stopped.\n", currentDateTime()); + WRITELOG("\n%s BrokerSendTask stopped.", currentDateTime()); delete ev; return; } diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index f42d102..d319f47 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -77,7 +77,7 @@ void ClientRecvTask::run() if (CHK_SIGINT) { - WRITELOG("%s ClientRecvTask stopped.\n", currentDateTime()); + WRITELOG("\n%s ClientRecvTask stopped.", currentDateTime()); delete packet; return; } diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index eaa6e68..0a5e06e 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -51,7 +51,7 @@ void ClientSendTask::run() if (ev->getEventType() == EtStop) { - WRITELOG("%s ClientSendTask stopped.\n", currentDateTime()); + WRITELOG("\n%s ClientSendTask stopped.", currentDateTime()); delete ev; break; } diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index bbe16ff..a4c8718 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -113,7 +113,7 @@ void PacketHandleTask::run() if (ev->getEventType() == EtStop) { - WRITELOG("%s PacketHandleTask stopped.\n", currentDateTime()); + WRITELOG("\n%s PacketHandleTask stopped.", currentDateTime()); delete ev; return; } diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index 487e745..1d074fb 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -294,7 +294,7 @@ void Gateway::run(void) /* wait until all Task stop */ MultiTaskProcess::waitStop(); - WRITELOG("\n%s MQTT-SN Gateway stoped\n\n", currentDateTime()); + WRITELOG("\n\n%s MQTT-SN Gateway stopped.\n\n", currentDateTime()); _lightIndicator.allLightOff(); } From 6a8cdba186de0850118c2c4200c2b5be272d1393 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sun, 23 Feb 2020 17:35:06 +0900 Subject: [PATCH 19/43] Bugfix can't read PredefinedTopicList file Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 15 ++------------- MQTTSNGateway/src/MQTTSNGateway.cpp | 14 ++++++++++++++ MQTTSNGateway/src/MQTTSNGateway.h | 5 ++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index 80bdfa6..dfe32a2 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -65,12 +65,8 @@ void ClientList::initialize(bool aggregate) void ClientList::setClientList(int type) { - char param[MQTTSNGW_PARAM_MAX]; - string fileName; - theGateway->getParam("ClientsList", param); - fileName = string(param); - if (!createList(fileName.c_str(), type)) + if (!createList(theGateway->getClientListFileName(), type)) { throw Exception("ClientList::initialize(): No client list defined by the configuration."); } @@ -78,14 +74,7 @@ void ClientList::setClientList(int type) void ClientList::setPredefinedTopics(bool aggrecate) { - char param[MQTTSNGW_PARAM_MAX]; - - string fileName; - - theGateway->getParam("PredefinedTopicList", param); - fileName = string(param); - - readPredefinedList(fileName.c_str(), aggrecate); + readPredefinedList(theGateway->getPredefinedTopicFileName(), aggrecate); } /** diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index 1d074fb..bb86d14 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -86,6 +86,10 @@ Gateway::~Gateway() { free(_params.clientListName); } + if ( _params.predefinedTopicFileName ) + { + free( _params.predefinedTopicFileName); + } if ( _params.configName ) { free(_params.configName); @@ -116,6 +120,16 @@ int Gateway::getParam(const char* parameter, char* value) return MultiTaskProcess::getParam(parameter, value); } +char* Gateway::getClientListFileName(void) +{ + return _params.clientListName; +} + +char* Gateway::getPredefinedTopicFileName(void) +{ + return _params.predefinedTopicFileName; +} + void Gateway::initialize(int argc, char** argv) { char param[MQTTSNGW_PARAM_MAX]; diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index d9b4857..0053219 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -160,8 +160,8 @@ public: char* rootCApath {nullptr}; char* rootCAfile {nullptr}; char* certKey {nullptr}; - char* privateKey {nullptr}; char* predefinedTopicFileName {nullptr}; + char* privateKey {nullptr}; char* qosMinusClientListName {nullptr}; bool clientAuthentication {false}; }; @@ -190,6 +190,9 @@ public: GatewayParams* getGWParams(void); AdapterManager* getAdapterManager(void); int getParam(const char* parameter, char* value); + char* getClientListFileName(void); + char* getPredefinedTopicFileName(void); + bool hasSecureConnection(void); Topics* getTopics(void); From 53be14f76e92d44c6d574f0cc4ffbf5e78cbfdc9 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 25 Feb 2020 15:50:56 +0900 Subject: [PATCH 20/43] Bugfix of #177 Signed-off-by: tomoaki --- MQTTSNGateway/src/linux/udp/SensorNetwork.cpp | 4 ++-- MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp index 5ca191d..1b57ff5 100644 --- a/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp/SensorNetwork.cpp @@ -392,8 +392,8 @@ int UDPPort::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr) fd_set recvfds; int maxSock = 0; - timeout.tv_sec = 0; - timeout.tv_usec = 1000000; // 1 sec + timeout.tv_sec = 1; + timeout.tv_usec = 0; // 1 sec FD_ZERO(&recvfds); FD_SET(_sockfdUnicast, &recvfds); FD_SET(_sockfdMulticast, &recvfds); diff --git a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp index 8137a1c..7558d45 100644 --- a/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp +++ b/MQTTSNGateway/src/linux/udp6/SensorNetwork.cpp @@ -455,14 +455,13 @@ int UDPPort6::broadcast(const uint8_t* buf, uint32_t length) return 0; } -//TODO: test if this is working properly (GW works, but this function is not completely tested) int UDPPort6::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr) { struct timeval timeout; fd_set recvfds; - timeout.tv_sec = 0; - timeout.tv_usec = 1000000; // 1 sec + timeout.tv_sec = 1; + timeout.tv_usec = 0; // 1 sec FD_ZERO(&recvfds); FD_SET(_sockfdUnicast, &recvfds); @@ -477,7 +476,6 @@ int UDPPort6::recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr) return rc; } -//TODO: test if this is working properly (GW works, but this function is not completely tested) int UDPPort6::recvfrom(int sockfd, uint8_t* buf, uint16_t len, uint8_t flags, SensorNetAddress* addr) { sockaddr_in6 sender; From 258d534009ba6707ae9f255da9d63bf6d8f735e7 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Thu, 5 Mar 2020 17:39:54 +0900 Subject: [PATCH 21/43] Update Aggregatting gateway can now receive PUBLISH from a broker Signed-off-by: tomoaki --- MQTTSNGateway/README.md | 1 - MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 62 ++---- .../src/MQTTSNAggregateConnectionHandler.cpp | 11 +- MQTTSNGateway/src/MQTTSNGWAdapter.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp | 53 +++-- MQTTSNGateway/src/MQTTSNGWAdapterManager.h | 8 +- .../src/MQTTSNGWAggregateTopicTable.cpp | 183 ++++++++++++++++-- .../src/MQTTSNGWAggregateTopicTable.h | 20 +- MQTTSNGateway/src/MQTTSNGWAggregater.cpp | 51 ++--- MQTTSNGateway/src/MQTTSNGWAggregater.h | 14 +- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientList.cpp | 19 +- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 19 +- .../src/MQTTSNGWConnectionHandler.cpp | 1 - MQTTSNGateway/src/MQTTSNGWForwarder.cpp | 13 +- MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp | 22 +-- MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.h | 2 +- .../src/MQTTSNGWSubscribeHandler.cpp | 23 ++- MQTTSNGateway/src/MQTTSNGWTopic.cpp | 19 ++ MQTTSNGateway/src/MQTTSNGWTopic.h | 5 + MQTTSNGateway/src/MQTTSNGateway.cpp | 43 ++-- MQTTSNGateway/src/MQTTSNGateway.h | 4 + 22 files changed, 397 insertions(+), 180 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 4765264..38823c5 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -2,7 +2,6 @@ **MQTT-SN** requires a MQTT-SN Gateway which acts as a protocol converter to convert **MQTT-SN messages to MQTT messages**. MQTT-SN client over SensorNetwork can not communicate directly with MQTT broker(TCP/IP). This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. -The Aggregating Gateway can not receive PUBLISH message from the broker at this version. ### **step1. Build the gateway** ```` diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index 13a1b35..653d831 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -272,53 +272,31 @@ void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* Publish pub; packet->getPUBLISH(&pub); - // Start of temporary code - WRITELOG("%s MQTTGWPublishHandler::handleAggregatePublish Aggregater can't handle a PUBLISH from the broker at the current version.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER); - if (pub.header.bits.qos == 1) + string* topicName = new string(pub.topic, pub.topiclen); // topic deletes topicName when the topic is deleted + Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); + + // ToDo: need to refacter + ClientTopicElement* elm = _gateway->getAdapterManager()->getAggregater()->getClientElement(&topic); + + while ( elm != nullptr ) { - replyACK(client, &pub, PUBACK); - } - else if ( pub.header.bits.qos == 2) - { - replyACK(client, &pub, PUBREC); - } - // End of temporary code + Client* devClient = elm->getClient(); + MQTTGWPacket* msg = new MQTTGWPacket(); + *msg = *packet; - - string* topicName = new string(pub.topic, pub.topiclen); - Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); // topic deletes topicName when the topic is deleted - - AggregateTopicElement* list = _gateway->getAdapterManager()->createClientList(&topic); - if ( list != nullptr ) - { - ClientTopicElement* p = list->getFirstElement(); - - while ( p ) + if ( msg->getType() == 0 ) { - Client* devClient = p->getClient(); - if ( devClient != nullptr ) - { - MQTTGWPacket* msg = new MQTTGWPacket(); - *msg = *packet; - if ( msg->getType() == 0 ) - { - WRITELOG("%s MQTTGWPublishHandler::handleAggregatePublish can't allocate memories for Packet.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER); - delete msg; - break; - } - Event* ev = new Event(); - ev->setBrokerRecvEvent(devClient, msg); - _gateway->getPacketEventQue()->post(ev); - } - else - { - break; - } - - p = list->getNextElement(p); + WRITELOG("%s MQTTGWPublishHandler::handleAggregatePublish can't allocate memories for Packet.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER); + delete msg; + break; } - delete list; + + Event* ev = new Event(); + ev->setBrokerRecvEvent(devClient, msg); + _gateway->getPacketEventQue()->post(ev); + + elm = elm->getNextClientElement(); } } diff --git a/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp b/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp index 4e84630..90672bf 100644 --- a/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.cpp @@ -81,7 +81,15 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke /* renew the TopicList */ if (topics) { - _gateway->getAdapterManager()->removeAggregateTopicList(topics, client); + Topic* tp = topics->getFirstTopic(); + while( tp != nullptr ) + { + if ( tp->getType() == MQTTSN_TOPIC_TYPE_NORMAL ) + { + _gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(tp, client); + } + tp = topics->getNextTopic(tp); + } topics->eraseNormal(); } client->setSessionStatus(true); @@ -189,7 +197,6 @@ void MQTTSNAggregateConnectionHandler::sendStoredPublish(Client* client) while ( ( msg = client->getClientSleepPacket() ) != nullptr ) { - // ToDo: This version can't re-send PUBLISH when PUBACK is not returned. client->deleteFirstClientSleepPacket(); // pop the que to delete element. Event* ev = new Event(); diff --git a/MQTTSNGateway/src/MQTTSNGWAdapter.cpp b/MQTTSNGateway/src/MQTTSNGWAdapter.cpp index a38ba67..2fa37b3 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapter.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapter.cpp @@ -217,7 +217,7 @@ Client* Adapter::getAdapterClient(Client* client) { if ( client->isSecureNetwork() ) { - return _client; + return _clientSecure; } else { diff --git a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp index 25c3754..0d5fa55 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAdapterManager.cpp @@ -40,13 +40,23 @@ AdapterManager::AdapterManager(Gateway* gw) } -void AdapterManager::initialize(void) +void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder, bool qosM1) { - _aggregater->initialize(); - _forwarders->initialize(_gateway); - _qosm1Proxy->initialize(); -} + if ( aggregate ) + { + _aggregater->initialize(gwName); + } + if ( qosM1 ) + { + _qosm1Proxy->initialize(gwName); + } + + if ( forwarder ) + { + _forwarders->initialize(_gateway); + } +} AdapterManager::~AdapterManager(void) { @@ -91,25 +101,26 @@ bool AdapterManager::isAggregatedClient(Client* client) } } -Client* AdapterManager::getClient(Client& client) +Client* AdapterManager::getClient(Client* client) { - bool secure = client.isSecureNetwork(); - Client* newClient = &client; - if ( client.isQoSm1() ) + bool secure = client->isSecureNetwork(); + Client* newClient = client; + + if ( client->isQoSm1() ) { - newClient = _qosm1Proxy->getAdapterClient(&client); + newClient = _qosm1Proxy->getAdapterClient(client); _qosm1Proxy->resetPingTimer(secure); } - else if ( client.isAggregated() ) + else if ( client->isAggregated() ) { - newClient = _aggregater->getAdapterClient(&client); + newClient = _aggregater->getAdapterClient(client); _aggregater->resetPingTimer(secure); } - else if ( client.isQoSm1Proxy() ) + else if ( client->isQoSm1Proxy() ) { _qosm1Proxy->resetPingTimer(secure); } - else if ( client.isAggregater() ) + else if ( client->isAggregater() ) { _aggregater->resetPingTimer(secure); } @@ -173,22 +184,26 @@ bool AdapterManager::isAggregaterActive(void) return _aggregater->isActive(); } -AggregateTopicElement* AdapterManager::createClientList(Topic* topic) +/* +AggregateTopicElement* AdapterManager::findTopic(Topic* topic) { - return _aggregater->createClientList(topic); + return _aggregater->findTopic(topic); } -int AdapterManager::addAggregateTopic(Topic* topic, Client* client) +AggregateTopicElement* AdapterManager::addAggregateTopic(Topic* topic, Client* client) { return _aggregater->addAggregateTopic(topic, client); } + void AdapterManager::removeAggregateTopic(Topic* topic, Client* client) { - _aggregater->removeAggregateTopic(topic, client); + //_aggregater->removeAggregateTopic(topic, client); } void AdapterManager::removeAggregateTopicList(Topics* topics, Client* client) { - _aggregater->removeAggregateTopicList(topics, client); + } +*/ + diff --git a/MQTTSNGateway/src/MQTTSNGWAdapterManager.h b/MQTTSNGateway/src/MQTTSNGWAdapterManager.h index 510d02f..58e5a03 100644 --- a/MQTTSNGateway/src/MQTTSNGWAdapterManager.h +++ b/MQTTSNGateway/src/MQTTSNGWAdapterManager.h @@ -40,21 +40,17 @@ class AdapterManager public: AdapterManager(Gateway* gw); ~AdapterManager(void); - void initialize(void); + void initialize(char* gwName, bool aggregater, bool fowarder, bool qosM1); ForwarderList* getForwarderList(void); QoSm1Proxy* getQoSm1Proxy(void); Aggregater* getAggregater(void); void checkConnection(void); bool isAggregatedClient(Client* client); - Client* getClient(Client& client); + Client* getClient(Client* client); Client* convertClient(uint16_t msgId, uint16_t* clientMsgId); int unicastToClient(Client* client, MQTTSNPacket* packet, ClientSendTask* task); bool isAggregaterActive(void); - AggregateTopicElement* createClientList(Topic* topic); - int addAggregateTopic(Topic* topic, Client* client); - void removeAggregateTopic(Topic* topic, Client* client); - void removeAggregateTopicList(Topics* topics, Client* client); private: Gateway* _gateway {nullptr}; diff --git a/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.cpp b/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.cpp index b53aea1..24dd23f 100644 --- a/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.cpp @@ -34,6 +34,11 @@ Client* ClientTopicElement::getClient(void) return _client; } +ClientTopicElement* ClientTopicElement::getNextClientElement(void) +{ + return _next; +} + /*===================================== Class AggregateTopicElement =====================================*/ @@ -44,6 +49,7 @@ AggregateTopicElement::AggregateTopicElement(void) AggregateTopicElement::AggregateTopicElement(Topic* topic, Client* client) { + _topic = topic; ClientTopicElement* elm = new ClientTopicElement(client); if ( elm != nullptr ) { @@ -76,7 +82,9 @@ ClientTopicElement* AggregateTopicElement::add(Client* client) { return nullptr; } + _mutex.lock(); + if ( _head == nullptr ) { _head = elm; @@ -95,7 +103,7 @@ ClientTopicElement* AggregateTopicElement::add(Client* client) else { delete elm; - elm = nullptr; + elm = p; } } _mutex.unlock(); @@ -105,7 +113,7 @@ ClientTopicElement* AggregateTopicElement::add(Client* client) ClientTopicElement* AggregateTopicElement::find(Client* client) { ClientTopicElement* p = _head; - while ( p ) + while ( p != nullptr ) { if ( p->_client == client) { @@ -116,16 +124,48 @@ ClientTopicElement* AggregateTopicElement::find(Client* client) return p; } -ClientTopicElement* AggregateTopicElement::getFirstElement(void) +ClientTopicElement* AggregateTopicElement::getFirstClientTopicElement(void) { return _head; } -ClientTopicElement* AggregateTopicElement::getNextElement(ClientTopicElement* elm) +ClientTopicElement* AggregateTopicElement::getNextClientTopicElement(ClientTopicElement* elmClient) { - return elm->_next; + return elmClient->_next; } +void AggregateTopicElement::eraseClient(Client* client) +{ + _mutex.lock(); + + ClientTopicElement* p = find(client); + if ( p != nullptr ) + { + if ( p->_prev == nullptr ) // head element + { + _head = p->_next; + if ( p->_next == nullptr ) // head & only one + { + _tail = nullptr; + } + else + { + p->_next->_prev = nullptr; // head & midle + } + } + else if ( p->_next != nullptr ) // middle + { + p->_prev->_next = p->_next; + } + else // tail + { + p->_prev->_next = nullptr; + _tail = p->_prev; + } + delete p; + } + _mutex.unlock(); +} /*===================================== Class AggregateTopicTable @@ -143,19 +183,138 @@ AggregateTopicTable::~AggregateTopicTable() AggregateTopicElement* AggregateTopicTable::add(Topic* topic, Client* client) { - //ToDo: AggregateGW - return 0; + AggregateTopicElement* elm = nullptr; + _mutex.lock(); + elm = getAggregateTopicElement(topic); + if ( elm != nullptr ) + { + if ( elm->find(client) == nullptr ) + { + elm->add(client); + } + } + else + { + Topic* newTopic = topic->duplicate(); + elm = new AggregateTopicElement(newTopic, client); + if ( _head == nullptr ) + { + _head = elm; + _tail = elm; + } + else + { + elm->_prev = _tail; + _tail->_next = elm; + _tail = elm; + } + } + _mutex.unlock(); + return elm; } -void AggregateTopicTable::remove(Topic* topic, Client* client) +void AggregateTopicTable::erase(Topic* topic, Client* client) { - //ToDo: AggregateGW + AggregateTopicElement* elm = nullptr; + + _mutex.lock(); + elm = getAggregateTopicElement(topic); + + if ( elm != nullptr ) + { + elm->eraseClient(client); + } + if ( elm->_head == nullptr ) + { + erase(elm); + } + _mutex.unlock(); + return; } -AggregateTopicElement* AggregateTopicTable::getClientList(Topic* client) +void AggregateTopicTable::erase(AggregateTopicElement* elmTopic) { - // ToDo: AggregateGW - return 0; + if ( elmTopic != nullptr ) + { + if ( elmTopic->_prev == nullptr ) // head element + { + _head = elmTopic->_next; + if ( elmTopic->_next == nullptr ) // head & only one + { + _tail = nullptr; + } + else + { + elmTopic->_next->_prev = nullptr; // head & midle + } + } + else if ( elmTopic->_next != nullptr ) // middle + { + elmTopic->_prev->_next = elmTopic->_next; + } + else // tail + { + elmTopic->_prev->_next = nullptr; + _tail = elmTopic->_prev; + } + delete elmTopic; + } } +AggregateTopicElement* AggregateTopicTable::getAggregateTopicElement(Topic* topic) +{ + AggregateTopicElement* elm = _head; + while( elm != nullptr ) + { + if ( elm->_topic->isMatch(topic->_topicName) ) + { + break; + } + elm = elm->_next; + } + return elm; +} + +ClientTopicElement* AggregateTopicTable::getClientElement(Topic* topic) +{ + AggregateTopicElement* elm = getAggregateTopicElement(topic); + if ( elm != nullptr ) + { + return elm->_head; + } + else + { + return nullptr; + } +} + +void AggregateTopicTable::print(void) +{ + AggregateTopicElement* elm = _head; + + printf("Beginning of AggregateTopicTable\n"); + while( elm != nullptr ) + { + printf("%s\n", elm->_topic->getTopicName()->c_str()); + + ClientTopicElement* clElm = elm->getFirstClientTopicElement(); + Client* client = clElm->getClient(); + + while ( client != nullptr ) + { + printf(" %s\n", client->getClientId()); + clElm = clElm->getNextClientElement(); + if ( clElm != nullptr ) + { + client = clElm->getClient(); + } + else + { + client = nullptr; + } + } + elm = elm->_next; + } + printf("End of AggregateTopicTable\n"); +} diff --git a/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.h b/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.h index 624743f..87d9e82 100644 --- a/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.h +++ b/MQTTSNGateway/src/MQTTSNGWAggregateTopicTable.h @@ -39,10 +39,16 @@ public: ~AggregateTopicTable(); AggregateTopicElement* add(Topic* topic, Client* client); - AggregateTopicElement* getClientList(Topic* client); - void remove(Topic* topic, Client* client); + AggregateTopicElement* getAggregateTopicElement(Topic* topic); + ClientTopicElement* getClientElement(Topic* topic); + void erase(Topic* topic, Client* client); void clear(void); + + void print(void); + private: + void erase(AggregateTopicElement* elmTopic); + Mutex _mutex; AggregateTopicElement* _head {nullptr}; AggregateTopicElement* _tail {nullptr}; int _cnt {0}; @@ -61,14 +67,16 @@ public: ~AggregateTopicElement(void); ClientTopicElement* add(Client* client); - ClientTopicElement* getFirstElement(void); - ClientTopicElement* getNextElement(ClientTopicElement* elm); - void erase(ClientTopicElement* elm); + ClientTopicElement* getFirstClientTopicElement(void); + ClientTopicElement* getNextClientTopicElement(ClientTopicElement* elmClient); + void eraseClient(Client* client); ClientTopicElement* find(Client* client); private: Mutex _mutex; Topic* _topic {nullptr}; + AggregateTopicElement* _next {nullptr}; + AggregateTopicElement* _prev {nullptr}; ClientTopicElement* _head {nullptr}; ClientTopicElement* _tail {nullptr}; }; @@ -83,6 +91,8 @@ class ClientTopicElement public: ClientTopicElement(Client* client); ~ClientTopicElement(void); + + ClientTopicElement* getNextClientElement(void); Client* getClient(void); private: diff --git a/MQTTSNGateway/src/MQTTSNGWAggregater.cpp b/MQTTSNGateway/src/MQTTSNGWAggregater.cpp index 32f4924..93718db 100644 --- a/MQTTSNGateway/src/MQTTSNGWAggregater.cpp +++ b/MQTTSNGateway/src/MQTTSNGWAggregater.cpp @@ -36,23 +36,12 @@ Aggregater::~Aggregater(void) } -void Aggregater::initialize(void) +void Aggregater::initialize(char* gwName) { - char param[MQTTSNGW_PARAM_MAX]; - - if (_gateway->getParam("AggregatingGateway", param) == 0 ) - { - if (!strcasecmp(param, "YES") ) - { - /* Create Aggregated Clients from clients.conf */ - _gateway->getClientList()->setClientList(AGGREGATER_TYPE); - - /* Create Aggregater Client */ - string name = string(_gateway->getGWParams()->gatewayName) + "_Aggregater"; - setup(name.c_str(), Atype_Aggregater); - _isActive = true; - } - } + /* Create Aggregater Client */ + string name = string(gwName) + string("_Aggregater"); + setup(name.c_str(), Atype_Aggregater); + _isActive = true; //testMessageIdTable(); @@ -95,26 +84,38 @@ uint16_t Aggregater::getMsgId(Client* client, uint16_t clientMsgId) return _msgIdTable.getMsgId(client, clientMsgId); } +AggregateTopicElement* Aggregater::addAggregateTopic(Topic* topic, Client* client) +{ + return _topicTable.add(topic, client); +} + + void Aggregater::removeAggregateTopic(Topic* topic, Client* client) { - // ToDo: AggregateGW this method called when the client disconnect and erase it`s Topics. this method call */ + _topicTable.erase(topic, client); } -void Aggregater::removeAggregateTopicList(Topics* topics, Client* client) +AggregateTopicElement* Aggregater::findTopic(Topic* topic) { - // ToDo: AggregateGW this method called when the client disconnect and erase it`s Topics. this method call */ + return _topicTable.getAggregateTopicElement(topic); } -int Aggregater::addAggregateTopic(Topic* topic, Client* client) +ClientTopicElement* Aggregater::getClientElement(Topic* topic) { - // ToDo: AggregateGW */ - return 0; + AggregateTopicElement* elm = findTopic(topic); + if ( elm != nullptr ) + { + return elm->getFirstClientTopicElement(); + } + else + { + return nullptr; + } } -AggregateTopicElement* Aggregater::createClientList(Topic* topic) +void Aggregater::printAggregateTopicTable(void) { - // ToDo: AggregateGW */ - return 0; + _topicTable.print(); } bool Aggregater::testMessageIdTable(void) diff --git a/MQTTSNGateway/src/MQTTSNGWAggregater.h b/MQTTSNGateway/src/MQTTSNGWAggregater.h index 9baaa15..959ff87 100644 --- a/MQTTSNGateway/src/MQTTSNGWAggregater.h +++ b/MQTTSNGateway/src/MQTTSNGWAggregater.h @@ -20,6 +20,7 @@ #include "MQTTSNGWAdapter.h" #include "MQTTSNGWMessageIdTable.h" #include "MQTTSNGWAggregateTopicTable.h" + namespace MQTTSNGW { class Gateway; @@ -40,7 +41,7 @@ public: Aggregater(Gateway* gw); ~Aggregater(void); - void initialize(void); + void initialize(char* gwName); const char* getClientId(SensorNetAddress* addr); Client* getClient(SensorNetAddress* addr); @@ -48,13 +49,18 @@ public: uint16_t addMessageIdTable(Client* client, uint16_t msgId); uint16_t getMsgId(Client* client, uint16_t clientMsgId); + ClientTopicElement* getClientElement(Topic* topic); + ClientTopicElement* getNextClientElement(ClientTopicElement* clientElement); + Client* getClient(ClientTopicElement* clientElement); + + AggregateTopicElement* findTopic(Topic* topic); + AggregateTopicElement* addAggregateTopic(Topic* topic, Client* client); - AggregateTopicElement* createClientList(Topic* topic); - int addAggregateTopic(Topic* topic, Client* client); void removeAggregateTopic(Topic* topic, Client* client); - void removeAggregateTopicList(Topics* topics, Client* client); + void removeAggregateAllTopic(Client* client); bool isActive(void); + void printAggregateTopicTable(void); bool testMessageIdTable(void); private: diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index 385ad87..4f58359 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -81,7 +81,7 @@ void BrokerSendTask::run() packet = ev->getMQTTGWPacket(); /* Check Client is managed by Adapters */ - client = adpMgr->getClient(*client); + client = adpMgr->getClient(client); if ( packet->getType() == CONNECT && client->getNetwork()->isValid() ) { diff --git a/MQTTSNGateway/src/MQTTSNGWClientList.cpp b/MQTTSNGateway/src/MQTTSNGWClientList.cpp index dfe32a2..f164996 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientList.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientList.cpp @@ -61,20 +61,28 @@ void ClientList::initialize(bool aggregate) setClientList(type); _authorize = true; } + + if ( theGateway->getGWParams()->predefinedTopic ) + { + setPredefinedTopics(aggregate); + } } void ClientList::setClientList(int type) { - - if (!createList(theGateway->getClientListFileName(), type)) + if (!createList(theGateway->getGWParams()->clientListName, type)) { - throw Exception("ClientList::initialize(): No client list defined by the configuration."); + throw Exception("ClientList::setClientList No client list defined by config file."); } } void ClientList::setPredefinedTopics(bool aggrecate) { - readPredefinedList(theGateway->getPredefinedTopicFileName(), aggrecate); + if ( !readPredefinedList(theGateway->getGWParams()->predefinedTopicFileName, aggrecate) ) + { + throw Exception("ClientList::setPredefinedTopics No predefindTopi list defined by config file."); + + } } /** @@ -108,7 +116,7 @@ bool ClientList::createList(const char* fileName, int type) bool stable; bool qos_1; bool forwarder; - bool rc = true; + bool rc = false; SensorNetAddress netAddr; MQTTSNString clientId = MQTTSNString_initializer; @@ -161,6 +169,7 @@ bool ClientList::createList(const char* fileName, int type) free(clientId.cstring); } fclose(fp); + rc = true; } return rc; } diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index d319f47..8bc6e17 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -60,7 +60,7 @@ void ClientRecvTask::run() Event* ev = nullptr; AdapterManager* adpMgr = _gateway->getAdapterManager(); QoSm1Proxy* qosm1Proxy = adpMgr->getQoSm1Proxy(); - bool isAggrActive = adpMgr->isAggregaterActive(); + int clientType = adpMgr->isAggregaterActive() ? AGGREGATER_TYPE : TRANSPEARENT_TYPE; ClientList* clientList = _gateway->getClientList(); EventQue* packetEventQue = _gateway->getPacketEventQue(); @@ -133,11 +133,12 @@ void ClientRecvTask::run() { const char* clientName = qosm1Proxy->getClientId(senderAddr); - if ( clientName ) + if ( clientName != nullptr ) { + client = qosm1Proxy->getClient(); + if ( !packet->isQoSMinusPUBLISH() ) { - client = qosm1Proxy->getClient(); log(clientName, packet); WRITELOG("%s %s %s can send only PUBLISH with QoS-1.%s\n", ERRMSG_HEADER, clientName, senderAddr->sprint(buf), ERRMSG_FOOTER); delete packet; @@ -145,10 +146,14 @@ void ClientRecvTask::run() } } } - client = _gateway->getClientList()->getClient(senderAddr); + + if ( client == nullptr ) + { + client = _gateway->getClientList()->getClient(senderAddr); + } } - if ( client ) + if ( client != nullptr ) { /* write log and post Event */ log(client, packet, 0); @@ -178,7 +183,7 @@ void ClientRecvTask::run() if ( client == nullptr ) { /* create a new client */ - client = clientList->createClient(0, &data.clientID, isAggrActive); + client = clientList->createClient(0, &data.clientID, clientType); } /* Add to af forwarded client list of forwarder. */ fwd->addClient(client, &nodeId); @@ -193,7 +198,7 @@ void ClientRecvTask::run() else { /* create a new client */ - client = clientList->createClient(senderAddr, &data.clientID, isAggrActive); + client = clientList->createClient(senderAddr, &data.clientID, clientType); } } diff --git a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp index c73b517..f47b1cc 100644 --- a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp @@ -291,7 +291,6 @@ void MQTTSNConnectionHandler::sendStoredPublish(Client* client) while ( ( msg = client->getClientSleepPacket() ) != nullptr ) { - // ToDo: This version can't re-send PUBLISH when PUBACK is not returned. client->deleteFirstClientSleepPacket(); // pop the que to delete element. Event* ev = new Event(); diff --git a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp index 8449c85..9cba49e 100644 --- a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp +++ b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp @@ -47,17 +47,8 @@ ForwarderList::~ForwarderList() void ForwarderList::initialize(Gateway* gw) { - char param[MQTTSNGW_PARAM_MAX]; - string fileName; - - if (gw->getParam("Forwarder", param) == 0 ) - { - if (!strcasecmp(param, "YES") ) - { - /* Create Fowarders from clients.conf */ - gw->getClientList()->setClientList(FORWARDER_TYPE); - } - } + /* Create Fowarders from clients.conf */ + gw->getClientList()->setClientList(FORWARDER_TYPE); } diff --git a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp index 7876fc0..349996f 100644 --- a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp +++ b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.cpp @@ -38,28 +38,20 @@ QoSm1Proxy::~QoSm1Proxy(void) } -void QoSm1Proxy::initialize(void) +void QoSm1Proxy::initialize(char* gwName) { - char param[MQTTSNGW_PARAM_MAX]; - if ( _gateway->hasSecureConnection() ) { _isSecure = true; } - if (_gateway->getParam("QoS-1", param) == 0 ) - { - if (strcasecmp(param, "YES") == 0 ) - { - /* Create QoS-1 Clients from clients.conf */ - _gateway->getClientList()->setClientList(QOSM1PROXY_TYPE); + /* Create QoS-1 Clients from clients.conf */ + _gateway->getClientList()->setClientList(QOSM1PROXY_TYPE); - /* Create a client for QoS-1 proxy */ - string name = string(_gateway->getGWParams()->gatewayName) + "_QoS-1"; - setup(name.c_str(), Atype_QoSm1Proxy); - _isActive = true; - } - } + /* Create a client for QoS-1 proxy */ + string name = string(gwName) + string("_QoS-1"); + setup(name.c_str(), Atype_QoSm1Proxy); + _isActive = true; } diff --git a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.h b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.h index d3dfcf5..82e5f2a 100644 --- a/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.h +++ b/MQTTSNGateway/src/MQTTSNGWQoSm1Proxy.h @@ -35,7 +35,7 @@ public: QoSm1Proxy(Gateway* gw); ~QoSm1Proxy(void); - void initialize(void); + void initialize(char* GWnAME); bool isActive(void); private: diff --git a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp index 45b2964..71694ab 100644 --- a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp @@ -203,11 +203,6 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack if ( subscribe != nullptr ) { - UTF8String str = subscribe->getTopic(); - string* topicName = new string(str.data, str.len); - Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); - _gateway->getAdapterManager()->addAggregateTopic(&topic, client); - int msgId = 0; if ( packet->isDuplicate() ) { @@ -223,6 +218,13 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack WRITELOG("%s MQTTSNSubscribeHandler can't create MessageIdTableElement %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); return; } + + UTF8String str = subscribe->getTopic(); + string* topicName = new string(str.data, str.len); // topicName is delete by topic + Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); + + _gateway->getAdapterManager()->getAggregater()->addAggregateTopic(&topic, client); + subscribe->setMsgId(msgId); Event* ev = new Event(); ev->setBrokerSendEvent(client, subscribe); @@ -235,11 +237,6 @@ void MQTTSNSubscribeHandler::handleAggregateUnsubscribe(Client* client, MQTTSNPa MQTTGWPacket* unsubscribe = handleUnsubscribe(client, packet); if ( unsubscribe != nullptr ) { - UTF8String str = unsubscribe->getTopic(); - string* topicName = new string(str.data, str.len); - Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); - _gateway->getAdapterManager()->removeAggregateTopic(&topic, client); - int msgId = 0; if ( packet->isDuplicate() ) { @@ -255,6 +252,12 @@ void MQTTSNSubscribeHandler::handleAggregateUnsubscribe(Client* client, MQTTSNPa WRITELOG("%s MQTTSNUnsubscribeHandler can't create MessageIdTableElement %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER); return; } + + UTF8String str = unsubscribe->getTopic(); + string* topicName = new string(str.data, str.len); // topicName is delete by topic + Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); + _gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(&topic, client); + unsubscribe->setMsgId(msgId); Event* ev = new Event(); ev->setBrokerSendEvent(client, unsubscribe); diff --git a/MQTTSNGateway/src/MQTTSNGWTopic.cpp b/MQTTSNGateway/src/MQTTSNGWTopic.cpp index 5be2278..8f56a4c 100644 --- a/MQTTSNGateway/src/MQTTSNGWTopic.cpp +++ b/MQTTSNGateway/src/MQTTSNGWTopic.cpp @@ -63,6 +63,15 @@ MQTTSN_topicTypes Topic::getType(void) return _type; } +Topic* Topic::duplicate(void) +{ + Topic* newTopic = new Topic(); + newTopic->_type = _type; + newTopic->_topicId = _topicId; + newTopic->_topicName = new string(_topicName->c_str()); + return newTopic; +} + bool Topic::isMatch(string* topicName) { string::size_type tlen = _topicName->size(); @@ -354,6 +363,16 @@ void Topics::eraseNormal(void) } } +Topic* Topics::getFirstTopic(void) +{ + return _first; +} + +Topic* Topics::getNextTopic(Topic* topic) +{ + return topic->_next; +} + void Topics::print(void) { Topic* topic = _first; diff --git a/MQTTSNGateway/src/MQTTSNGWTopic.h b/MQTTSNGateway/src/MQTTSNGWTopic.h index 5c3cc77..4d7b0c3 100644 --- a/MQTTSNGateway/src/MQTTSNGWTopic.h +++ b/MQTTSNGateway/src/MQTTSNGWTopic.h @@ -31,6 +31,7 @@ namespace MQTTSNGW class Topic { friend class Topics; + friend class AggregateTopicTable; public: Topic(); Topic(string* topic, MQTTSN_topicTypes type); @@ -39,7 +40,9 @@ public: uint16_t getTopicId(void); MQTTSN_topicTypes getType(void); bool isMatch(string* topicName); + Topic* duplicate(void); void print(void); + private: MQTTSN_topicTypes _type; uint16_t _topicId; @@ -59,6 +62,8 @@ public: Topic* add(const char* topicName, uint16_t id = 0); Topic* getTopicByName(const MQTTSN_topicid* topic); Topic* getTopicById(const MQTTSN_topicid* topicid); + Topic* getFirstTopic(void); + Topic* getNextTopic(Topic* topic); Topic* match(const MQTTSN_topicid* topicid); void eraseNormal(void); uint16_t getNextTopicId(); diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index bb86d14..cac3ac1 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -234,29 +234,48 @@ void Gateway::initialize(int argc, char** argv) _params.clientListName = strdup(param); } - if (getParam("PredefinedTopicList", param) == 0) + if (getParam("PredefinedTopic", param) == 0) { - _params.predefinedTopicFileName = strdup(param); + if ( !strcasecmp(param, "YES") ) + { + _params.predefinedTopic = true; + if (getParam("PredefinedTopicList", param) == 0) + { + _params.predefinedTopicFileName = strdup(param); + } + } } - if ( _params.clientListName == nullptr ) + if (getParam("AggregatingGateway", param) == 0) { - _params.clientListName = strdup(( _params.configDir + string(CLIENT_LIST) ).c_str()); + if ( !strcasecmp(param, "YES") ) + { + _params.aggregatingGw = true; + } } - if ( _params.predefinedTopicFileName == nullptr ) + if (getParam("Forwarder", param) == 0) { - _params.predefinedTopicFileName = strdup(( _params.configDir + string(PREDEFINEDTOPIC_FILE) ).c_str()); + if ( !strcasecmp(param, "YES") ) + { + _params.forwarder = true; + } } - /* ClientList and Adapters Initialize */ - _adapterManager->initialize(); + if (getParam("QoS-1", param) == 0) + { + if ( !strcasecmp(param, "YES") ) + { + _params.qosMinus1 = true; + } + } - bool aggregate = _adapterManager->isAggregaterActive(); - _clientList->initialize(aggregate); - /* Setup predefined topics */ - _clientList->setPredefinedTopics(aggregate); + /* Initialize adapters */ + _adapterManager->initialize( _params.gatewayName, _params.aggregatingGw, _params.forwarder, _params.qosMinus1); + + /* Setup ClientList and Predefined topics */ + _clientList->initialize(_params.aggregatingGw); } void Gateway::run(void) diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index 0053219..b6bed6a 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -164,6 +164,10 @@ public: char* privateKey {nullptr}; char* qosMinusClientListName {nullptr}; bool clientAuthentication {false}; + bool predefinedTopic {false}; + bool aggregatingGw {false}; + bool qosMinus1 {false}; + bool forwarder {false}; }; From 1ef0add867cd3cfbcb8ac9eb40ced718f07b719d Mon Sep 17 00:00:00 2001 From: tomoaki Date: Fri, 20 Mar 2020 12:40:41 +0900 Subject: [PATCH 22/43] a --- MQTTSNGateway/src/MQTTGWPublishHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp index 653d831..e3abc79 100644 --- a/MQTTSNGateway/src/MQTTGWPublishHandler.cpp +++ b/MQTTSNGateway/src/MQTTGWPublishHandler.cpp @@ -276,7 +276,7 @@ void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* string* topicName = new string(pub.topic, pub.topiclen); // topic deletes topicName when the topic is deleted Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL); - // ToDo: need to refacter + // ToDo: need to refactor ClientTopicElement* elm = _gateway->getAdapterManager()->getAggregater()->getClientElement(&topic); while ( elm != nullptr ) From c6d7daba11249a32bb66c49b8532f6a2330bce38 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 24 Mar 2020 14:35:42 +0900 Subject: [PATCH 23/43] BugFix of PR #189 Signed-off-by: tomoaki --- MQTTSNGateway/GatewayTester/src/LPublishManager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQTTSNGateway/GatewayTester/src/LPublishManager.cpp b/MQTTSNGateway/GatewayTester/src/LPublishManager.cpp index 918cddb..78a84db 100644 --- a/MQTTSNGateway/GatewayTester/src/LPublishManager.cpp +++ b/MQTTSNGateway/GatewayTester/src/LPublishManager.cpp @@ -119,7 +119,7 @@ void LPublishManager::sendPublish(PubElement* elm) uint8_t msg[MQTTSN_MAX_MSG_LENGTH + 1]; uint8_t org = 0; - if (elm->payloadlen > 128) + if (elm->payloadlen > 248) { msg[0] = 0x01; setUint16(msg + 1, elm->payloadlen + 9); From 61d9a283f536118c5c6aa42f0879d66a3d83cfbe Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 24 Mar 2020 14:57:56 +0900 Subject: [PATCH 24/43] Change prompt of the gateway Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGateway.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index b6bed6a..8d9068b 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -26,7 +26,7 @@ namespace MQTTSNGW /*================================= * Starting prompt ==================================*/ -#define PAHO_COPYRIGHT0 " * MQTT-SN Transparent Gateway" +#define PAHO_COPYRIGHT0 " * MQTT-SN Gateway" #define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse" #define PAHO_COPYRIGHT2 " * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)" #define PAHO_COPYRIGHT3 " * Author : Tomoaki YAMAGUCHI" From 687a78ee2c5fa7c42212552d0b359a99a3b36cf6 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 6 Apr 2020 19:02:02 +0900 Subject: [PATCH 25/43] update for #188 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 4 ++-- MQTTSNGateway/src/MQTTSNGWForwarder.cpp | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index 8bc6e17..35feac0 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -178,14 +178,14 @@ void ClientRecvTask::run() client = clientList->getClient(&data.clientID); - if ( fwd ) + if ( fwd != nullptr ) { if ( client == nullptr ) { /* create a new client */ client = clientList->createClient(0, &data.clientID, clientType); } - /* Add to af forwarded client list of forwarder. */ + /* Add to a forwarded client list of forwarder. */ fwd->addClient(client, &nodeId); } else diff --git a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp index 9cba49e..71b2e83 100644 --- a/MQTTSNGateway/src/MQTTSNGWForwarder.cpp +++ b/MQTTSNGateway/src/MQTTSNGWForwarder.cpp @@ -143,6 +143,7 @@ void Forwarder::addClient(Client* client, WirelessNodeId* id) if ( p->_client == client ) { client->setForwarder(this); + p->setWirelessNodeId(id); return; } prev = p; From 480d084e69fd851e2a56f1ef5bcef7cb68774902 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 7 Apr 2020 14:21:53 +0900 Subject: [PATCH 26/43] refacter Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index 35feac0..7ca2929 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -204,7 +204,7 @@ void ClientRecvTask::run() log(client, packet, &data.clientID); - if (!client) + if ( client == nullptr ) { WRITELOG("%s Client(%s) was rejected. CONNECT message has been discarded.%s\n", ERRMSG_HEADER, senderAddr->sprint(buf), ERRMSG_FOOTER); delete packet; From 52ddaf268631347ef52c59a087e3ac147150f38a Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 7 Apr 2020 15:25:55 +0900 Subject: [PATCH 27/43] Bugfix of #191 WILLTOPICUPD is not handled. Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp index a4c8718..f195d76 100644 --- a/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWPacketHandleTask.cpp @@ -198,6 +198,9 @@ void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTSNPacket* packe case MQTTSN_DISCONNECT: _mqttsnAggrConnection->handleDisconnect(client, packet); break; + case MQTTSN_WILLTOPICUPD: + _mqttsnConnection->handleWilltopicupd(client, packet); + break; case MQTTSN_WILLMSGUPD: _mqttsnConnection->handleWillmsgupd(client, packet); break; @@ -289,6 +292,9 @@ void PacketHandleTask::transparentPacketHandler(Client*client, MQTTSNPacket* pac case MQTTSN_DISCONNECT: _mqttsnConnection->handleDisconnect(client, packet); break; + case MQTTSN_WILLTOPICUPD: + _mqttsnConnection->handleWilltopicupd(client, packet); + break; case MQTTSN_WILLMSGUPD: _mqttsnConnection->handleWillmsgupd(client, packet); break; From 9adc2fed13be9db36382d28f03ef4b02a4d60b31 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 14 Apr 2020 10:06:03 +0900 Subject: [PATCH 28/43] Bugfix of #192 Signed-off-by: tomoaki --- MQTTSNPacket/src/MQTTSNPacket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MQTTSNPacket/src/MQTTSNPacket.c b/MQTTSNPacket/src/MQTTSNPacket.c index 46f8520..86c1e90 100644 --- a/MQTTSNPacket/src/MQTTSNPacket.c +++ b/MQTTSNPacket/src/MQTTSNPacket.c @@ -53,7 +53,7 @@ const char* MQTTSNPacket_name(int code) */ int MQTTSNPacket_len(int length) { - return (length > 255) ? length + 3 : length + 1; + return (length >= 255) ? length + 3 : length + 1; } /** @@ -67,7 +67,7 @@ int MQTTSNPacket_encode(unsigned char* buf, int length) int rc = 0; FUNC_ENTRY; - if (length > 255) + if (length >= 255) { writeChar(&buf, 0x01); writeInt(&buf, length); From f8a60d811aa3545eb1617fdf806b6bd39351d21d Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sat, 23 May 2020 10:41:17 +0900 Subject: [PATCH 29/43] BugFix of #205 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index 7ca2929..c44d95b 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -192,8 +192,11 @@ void ClientRecvTask::run() { if ( client ) { - /* Client exists. Set SensorNet Address of it. */ - client->setClientAddress(senderAddr); + /* Client exists. Set SensorNet Address of it, if Client is not secure one. */ + if ( !client->isSecureNetwork() ) + { + client->setClientAddress(senderAddr); + } } else { From b73d743d0098b7497120a266c37700b6cb4c2fe4 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Thu, 28 May 2020 11:57:30 +0900 Subject: [PATCH 30/43] BugFix of #205 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp index c44d95b..03f3415 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientRecvTask.cpp @@ -192,8 +192,8 @@ void ClientRecvTask::run() { if ( client ) { - /* Client exists. Set SensorNet Address of it, if Client is not secure one. */ - if ( !client->isSecureNetwork() ) + /* Authentication is not required */ + if ( _gateway->getGWParams()->clientAuthentication == false) { client->setClientAddress(senderAddr); } From 48169d48fada21ffee869b9be8c8c4561fc74ab0 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Sat, 6 Jun 2020 12:23:33 +0900 Subject: [PATCH 31/43] Fix typo #207 --- MQTTSNGateway/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index 38823c5..c206204 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -1,7 +1,7 @@ -# MQTT-SN Transparent / Aggrigating Gateway +# MQTT-SN Transparent / Aggregating Gateway **MQTT-SN** requires a MQTT-SN Gateway which acts as a protocol converter to convert **MQTT-SN messages to MQTT messages**. MQTT-SN client over SensorNetwork can not communicate directly with MQTT broker(TCP/IP). -This Gateway can run as a transparent or aggrigating Gateway by specifying the gateway.conf. +This Gateway can run as a transparent or aggregating Gateway by specifying the gateway.conf. ### **step1. Build the gateway** ```` From d91de457f304ea2e6b1c7cca7f57d9a4b77803e6 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 17:00:09 +0900 Subject: [PATCH 32/43] BugFix of #209 Signed-off-by: tomoaki --- .../src/MQTTSNGWConnectionHandler.cpp | 29 ++++++++++++------- MQTTSNGateway/src/linux/Network.cpp | 13 ++++++--- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp index f47b1cc..76367ce 100644 --- a/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWConnectionHandler.cpp @@ -1,5 +1,5 @@ /************************************************************************************** - * Copyright (c) 2016, Tomoaki Yamaguchi + * Copyright (c) 2016, 2020 Tomoaki Yamaguchi and others * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -104,10 +104,14 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet connectData->keepAliveTimer = data.duration; connectData->flags.bits.will = data.willFlag; - if ((const char*) _gateway->getGWParams()->loginId != nullptr && (const char*) _gateway->getGWParams()->password != 0) + if ((const char*) _gateway->getGWParams()->loginId != nullptr) + { + connectData->flags.bits.username = 1; + } + + if ((const char*) _gateway->getGWParams()->password != 0) { connectData->flags.bits.password = 1; - connectData->flags.bits.username = 1; } client->setSessionStatus(false); @@ -275,14 +279,18 @@ void MQTTSNConnectionHandler::handlePingreq(Client* client, MQTTSNPacket* packet if ( ( client->isSleep() || client->isAwake() ) && client->getClientSleepPacket() ) { sendStoredPublish(client); + client->holdPingRequest(); + } + else + { + /* send PINGREQ to the broker */ + client->resetPingRequest(); + MQTTGWPacket* pingreq = new MQTTGWPacket(); + pingreq->setHeader(PINGREQ); + Event* evt = new Event(); + evt->setBrokerSendEvent(client, pingreq); + _gateway->getBrokerSendQue()->post(evt); } - - /* send PINGREQ to the broker */ - MQTTGWPacket* pingreq = new MQTTGWPacket(); - pingreq->setHeader(PINGREQ); - Event* evt = new Event(); - evt->setBrokerSendEvent(client, pingreq); - _gateway->getBrokerSendQue()->post(evt); } void MQTTSNConnectionHandler::sendStoredPublish(Client* client) @@ -291,6 +299,7 @@ void MQTTSNConnectionHandler::sendStoredPublish(Client* client) while ( ( msg = client->getClientSleepPacket() ) != nullptr ) { + // ToDo: This version can't re-send PUBLISH when PUBACK is not returned. client->deleteFirstClientSleepPacket(); // pop the que to delete element. Event* ev = new Event(); diff --git a/MQTTSNGateway/src/linux/Network.cpp b/MQTTSNGateway/src/linux/Network.cpp index 8880b90..5c5e88d 100644 --- a/MQTTSNGateway/src/linux/Network.cpp +++ b/MQTTSNGateway/src/linux/Network.cpp @@ -145,11 +145,7 @@ bool TCPStack::accept(TCPStack& new_socket) int TCPStack::send(const uint8_t* buf, int length) { -#ifdef __APPLE__ - return ::send(_sockfd, buf, length, SO_NOSIGPIPE); -#else return ::send(_sockfd, buf, length, MSG_NOSIGNAL); -#endif } int TCPStack::recv(uint8_t* buf, int len) @@ -545,6 +541,15 @@ loop: case SSL_ERROR_WANT_WRITE: readBlockedOnWrite = true; break; + case SSL_ERROR_SYSCALL: + SSL_free(_ssl); + _ssl = 0; + _numOfInstance--; + //TCPStack::close(); + _busy = false; + _mutex.unlock(); + return -1; + break; default: ERR_error_string_n(ERR_get_error(), errmsg, sizeof(errmsg)); WRITELOG("Network::recv() %s\n", errmsg); From aa199cdf9b90afc0206e76799a56aef33305881d Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 17:24:20 +0900 Subject: [PATCH 33/43] Bugfix of short topic Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp index 71694ab..a08bf49 100644 --- a/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp +++ b/MQTTSNGateway/src/MQTTSNGWSubscribeHandler.cpp @@ -100,7 +100,8 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack topicstr[0] = topicFilter.data.short_name[0]; topicstr[1] = topicFilter.data.short_name[1]; topicstr[2] = 0; - topicId = 0; + topicId = topicFilter.data.short_name[0] << 8; + topicId |= topicFilter.data.short_name[1]; subscribe = new MQTTGWPacket(); subscribe->setSUBSCRIBE(topicstr, (uint8_t)qos, (uint16_t)msgId); } From 1f9241b260e8a611c09594b20a6494230d84818b Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 17:30:11 +0900 Subject: [PATCH 34/43] Bugfix of DISCONNECT and ClientSend error message Close the Connection after sending DISCONNECT. Separate multicast error message. Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 7 ++- MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 55 ++++++++++++-------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index 4f58359..d8211e6 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -41,7 +41,7 @@ BrokerSendTask::BrokerSendTask(Gateway* gateway) BrokerSendTask::~BrokerSendTask() { - + WRITELOG("BrokerSendTask is deleted normally.\r\n"); } /** @@ -121,6 +121,11 @@ void BrokerSendTask::run() { client->connectSended(); } + else if ( packet->getType() == DISCONNECT ) + { + client->getNetwork()->close(); + client->disconnected(); + } log(client, packet); } else diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index 0a5e06e..cd45b1d 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -35,7 +35,7 @@ ClientSendTask::ClientSendTask(Gateway* gateway) ClientSendTask::~ClientSendTask() { - + WRITELOG("ClientSendTask is deleted normally.\r\n"); } void ClientSendTask::run() @@ -49,35 +49,44 @@ void ClientSendTask::run() { Event* ev = _gateway->getClientSendQue()->wait(); - if (ev->getEventType() == EtStop) + if (ev->getEventType() == EtStop || _gateway->IsStopping() ) { WRITELOG("\n%s ClientSendTask stopped.", currentDateTime()); delete ev; break; } - if (ev->getEventType() == EtClientSend) - { - client = ev->getClient(); - packet = ev->getMQTTSNPacket(); - rc = adpMgr->unicastToClient(client, packet, this); - } - else if (ev->getEventType() == EtBroadcast) - { - packet = ev->getMQTTSNPacket(); - log(client, packet); - rc = packet->broadcast(_sensorNetwork); - } - else if (ev->getEventType() == EtSensornetSend) - { - packet = ev->getMQTTSNPacket(); - log(client, packet); - rc = packet->unicast(_sensorNetwork, ev->getSensorNetAddress()); - } - if ( rc < 0 ) + if (ev->getEventType() == EtBroadcast) { - WRITELOG("%s ClientSendTask can't send a packet to the client %s. Error=%d%s\n", - ERRMSG_HEADER, (client ? (const char*)client->getClientId() : UNKNOWNCL ), errno, ERRMSG_FOOTER); + packet = ev->getMQTTSNPacket(); + log(client, packet); + + if ( packet->broadcast(_sensorNetwork) < 0 ) + { + WRITELOG("%s ClientSendTask can't multicast a packet Error=%d%s\n", + ERRMSG_HEADER, errno, ERRMSG_FOOTER); + } + } + else + { + if (ev->getEventType() == EtClientSend) + { + client = ev->getClient(); + packet = ev->getMQTTSNPacket(); + rc = adpMgr->unicastToClient(client, packet, this); + } + else if (ev->getEventType() == EtSensornetSend) + { + packet = ev->getMQTTSNPacket(); + log(client, packet); + rc = packet->unicast(_sensorNetwork, ev->getSensorNetAddress()); + } + + if ( rc < 0 ) + { + WRITELOG("%s ClientSendTask can't send a packet to the client %s. Error=%d%s\n", + ERRMSG_HEADER, (client ? (const char*)client->getClientId() : UNKNOWNCL ), errno, ERRMSG_FOOTER); + } } delete ev; } From 21f2f842197de1f8667516fc7e3b6899d7316a38 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 17:35:16 +0900 Subject: [PATCH 35/43] Typo fixed #212 Add Gateway terminate status Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWProcess.h | 2 +- MQTTSNGateway/src/MQTTSNGateway.cpp | 10 ++++++++++ MQTTSNGateway/src/MQTTSNGateway.h | 2 ++ MQTTSNGateway/src/linux/Threading.cpp | 2 +- 5 files changed, 15 insertions(+), 3 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.cpp b/MQTTSNGateway/src/MQTTSNGWProcess.cpp index ac9c915..2e4adfa 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -307,7 +307,7 @@ void MultiTaskProcess::waitStop(void) } } -void MultiTaskProcess::threadStoped(void) +void MultiTaskProcess::threadStopped(void) { _mutex.lock(); _stopCount++; diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.h b/MQTTSNGateway/src/MQTTSNGWProcess.h index 3d94f2e..42ff2a2 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.h +++ b/MQTTSNGateway/src/MQTTSNGWProcess.h @@ -84,7 +84,7 @@ public: int getParam(const char* parameter, char* value); void run(void); void waitStop(void); - void threadStoped(void); + void threadStopped(void); void attach(Thread* thread); private: diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index cac3ac1..1393dfb 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -38,6 +38,7 @@ Gateway::Gateway(void) _clientList = new ClientList(); _adapterManager = new AdapterManager(this); _topics = new Topics(); + _stopFlg = false; } Gateway::~Gateway() @@ -113,6 +114,7 @@ Gateway::~Gateway() { delete _topics; } + WRITELOG("Gateway is deleted normally.\r\n"); } int Gateway::getParam(const char* parameter, char* value) @@ -309,10 +311,13 @@ void Gateway::run(void) WRITELOG(" CertKey: %s\n", _params.certKey); WRITELOG(" PrivateKey: %s\n\n\n", _params.privateKey); + _stopFlg = false; /* Run Tasks until CTRL+C entred */ MultiTaskProcess::run(); + _stopFlg = true; + /* stop Tasks */ Event* ev = new Event(); ev->setStop(); @@ -331,6 +336,11 @@ void Gateway::run(void) _lightIndicator.allLightOff(); } +bool Gateway::IsStopping(void) +{ + return _stopFlg; +} + EventQue* Gateway::getPacketEventQue() { return &_packetEventQue; diff --git a/MQTTSNGateway/src/MQTTSNGateway.h b/MQTTSNGateway/src/MQTTSNGateway.h index 8d9068b..86c7952 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.h +++ b/MQTTSNGateway/src/MQTTSNGateway.h @@ -199,6 +199,7 @@ public: bool hasSecureConnection(void); Topics* getTopics(void); + bool IsStopping(void); private: GatewayParams _params; @@ -210,6 +211,7 @@ private: SensorNetwork _sensorNetwork; AdapterManager* _adapterManager {nullptr}; Topics* _topics; + bool _stopFlg; }; } diff --git a/MQTTSNGateway/src/linux/Threading.cpp b/MQTTSNGateway/src/linux/Threading.cpp index da8cf34..7c452e7 100644 --- a/MQTTSNGateway/src/linux/Threading.cpp +++ b/MQTTSNGateway/src/linux/Threading.cpp @@ -541,7 +541,7 @@ int Thread::start(void) void Thread::stopProcess(void) { - theMultiTaskProcess->threadStoped(); + theMultiTaskProcess->threadStopped(); } void Thread::stop(void) From 45ea265344e31d46e4d57f0993976a57ecf31a01 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 17:58:32 +0900 Subject: [PATCH 36/43] Add LoRaLink sensor network Signed-off-by: tomoaki --- .cproject | 4 +- .../src/linux/loralink/SensorNetwork.cpp | 559 ++++++++++++++++++ .../src/linux/loralink/SensorNetwork.h | 187 ++++++ 3 files changed, 748 insertions(+), 2 deletions(-) create mode 100644 MQTTSNGateway/src/linux/loralink/SensorNetwork.cpp create mode 100644 MQTTSNGateway/src/linux/loralink/SensorNetwork.h diff --git a/.cproject b/.cproject index 62eba26..ade7cf0 100644 --- a/.cproject +++ b/.cproject @@ -133,7 +133,7 @@ - + @@ -277,7 +277,7 @@ - + diff --git a/MQTTSNGateway/src/linux/loralink/SensorNetwork.cpp b/MQTTSNGateway/src/linux/loralink/SensorNetwork.cpp new file mode 100644 index 0000000..854854c --- /dev/null +++ b/MQTTSNGateway/src/linux/loralink/SensorNetwork.cpp @@ -0,0 +1,559 @@ +/************************************************************************************** + * 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. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "SensorNetwork.h" +#include "MQTTSNGWProcess.h" + +using namespace std; +using namespace MQTTSNGW; + +#define LORA_PHY_MAXPAYLOAD 256 +#define LORALINK_MAX_API_LEN ( LORA_PHY_MAXPAYLOAD + 5 ) // PayloadType[1] + Rssi[2] + Snr[2] + +#define LORALINK_ACK 0x10 +#define LORALINK_NO_FREE_CH 0x20 +#define LORALINK_TX_TIMEOUT 0x40 + +#define LORALINK_TIMEOUT_ACK 10000 // 10 secs + +/*=========================================== + Class SensorNetAddreess + ============================================*/ +SensorNetAddress::SensorNetAddress() +{ + _devAddr = 0; +} + +SensorNetAddress::~SensorNetAddress() +{ + +} + +void SensorNetAddress::setAddress( uint8_t devAddr) +{ + _devAddr = devAddr; +} + + +int SensorNetAddress::setAddress(string* address) +{ + _devAddr = atoi(address->c_str()); + + if ( _devAddr == 0 ) + { + return -1; + } + return 0; +} + +void SensorNetAddress::setBroadcastAddress(void) +{ + _devAddr = BROADCAST_DEVADDR; +} + +bool SensorNetAddress::isMatch(SensorNetAddress* addr) +{ + return _devAddr == addr->_devAddr; +} + +SensorNetAddress& SensorNetAddress::operator =(SensorNetAddress& addr) +{ + _devAddr = addr._devAddr; + return *this; +} + +char* SensorNetAddress::sprint(char* buf) +{ + sprintf( buf, "%d", _devAddr); + return buf; +} + +/*=========================================== + Class SensorNetwork + ============================================*/ +SensorNetwork::SensorNetwork() +{ + +} + +SensorNetwork::~SensorNetwork() +{ + +} + +int SensorNetwork::unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendToAddr) +{ + return LoRaLink::unicast(payload, payloadLength, sendToAddr); +} + +int SensorNetwork::broadcast(const uint8_t* payload, uint16_t payloadLength) +{ + return LoRaLink::broadcast(payload, payloadLength); +} + +int SensorNetwork::read(uint8_t* buf, uint16_t bufLen) +{ + return LoRaLink::recv(buf, bufLen, &_clientAddr); +} + +int SensorNetwork::initialize(void) +{ + char param[MQTTSNGW_PARAM_MAX]; + uint32_t baudrate = 115200; + + if (theProcess->getParam("BaudrateLoRaLink", param) == 0) + { + baudrate = (uint32_t)atoi(param); + } + _description += "LoRaLink, Baudrate "; + sprintf(param ,"%d", baudrate); + _description += param; + + theProcess->getParam("DeviceRxLoRaLink", param); + _description += ", SerialRx "; + _description += param; + if ( LoRaLink::open(LORALINK_MODEM_RX, param, baudrate) < 0 ) + { + return -1; + } + + theProcess->getParam("DeviceTxLoRaLink", param); + _description += ", SerialTx "; + _description += param; + return LoRaLink::open(LORALINK_MODEM_TX, param, baudrate); +} + +const char* SensorNetwork::getDescription(void) +{ + return _description.c_str(); +} + +SensorNetAddress* SensorNetwork::getSenderAddress(void) +{ + return &_clientAddr; +} + +/*=========================================== + Class LoRaLink + ============================================*/ +LoRaLink::LoRaLink(){ + _serialPortRx = new SerialPort(); + _serialPortTx = new SerialPort(); + _respCd = 0; +} + +LoRaLink::~LoRaLink(){ + if ( _serialPortRx ) + { + delete _serialPortRx; + } + if ( _serialPortTx ) + { + delete _serialPortTx; + } +} + +int LoRaLink::open(LoRaLinkModemType_t type, char* device, int baudrate) +{ + int rate = B9600; + + switch (baudrate) + { + case 9600: + rate = B9600; + break; + case 19200: + rate = B19200; + break; + case 38400: + rate = B38400; + break; + case 57600: + rate = B57600; + break; + case 115200: + rate = B115200; + break; + default: + return -1; + } + + int rc = 0; + if ( type == LORALINK_MODEM_RX ) + { + if ( (rc = _serialPortRx->open(device, rate, false, 1, O_RDWR | O_NOCTTY)) < 0 ) + return rc; + } + else + { + rc = _serialPortTx->open(device, rate, false, 1, O_RDWR | O_NOCTTY); + } + return rc; +} + +int LoRaLink::broadcast(const uint8_t* payload, uint16_t payloadLen){ + SensorNetAddress addr; + addr.setBroadcastAddress(); + return send(MQTT_SN, payload, (uint8_t) payloadLen, &addr); +} + +int LoRaLink:: unicast(const uint8_t* payload, uint16_t payloadLen, SensorNetAddress* addr){ + return send(MQTT_SN, payload, (uint8_t) payloadLen, addr); +} + +int LoRaLink::recv(uint8_t* buf, uint16_t bufLen, SensorNetAddress* clientAddr) +{ + while ( true ) + { + if ( ( readApiFrame( &_loRaLinkApi, &_loRaLinkPara) == true ) && (_loRaLinkPara.Available == true) && ( _loRaLinkPara.Error == false ) ) + { + clientAddr->_devAddr = _loRaLinkApi.SourceAddr; + + bufLen = _loRaLinkApi.PayloadLen; + + memcpy( buf, _loRaLinkApi.Payload, bufLen ); + + switch ( (int) _loRaLinkApi.PayloadType ) + { + case API_RSP_ACK: + _respCd = LORALINK_ACK; + break; + + case API_RSP_NFC: + _respCd = LORALINK_NO_FREE_CH; + break; + + case API_RSP_TOT: + _respCd = LORALINK_TX_TIMEOUT; + break; + + + case MQTT_SN: + memcpy( buf, _loRaLinkApi.Payload, bufLen ); + return bufLen; + + default: + return 0; + } + _sem.post(); + return bufLen; + + } + else + { + return 0; + } + } +} + + + +bool LoRaLink::readApiFrame(LoRaLinkFrame_t* api, LoRaLinkReadParameters_t* para) +{ + uint8_t byte = 0; + uint16_t val = 0; + + while ( recv(&byte) == 0 ) + { + if ( byte == START_BYTE ) + { + para->apipos= 1; + para->Error = true; + para->Available = false; + continue; + } + + if ( para->apipos > 0 && byte == ESCAPE ) + { + if( recv(&byte ) == 0 ) + { + byte ^= PAD; // decode + } + else + { + para->Escape = true; + continue; + } + } + + if( para->Escape == true ) + { + byte ^= PAD; + para->Escape = false; + } + + switch ( para->apipos ) + { + case 0: + break; + + case 1: + val = (uint16_t)byte; + api->PayloadLen = val << 8; + break; + + case 2: + api->PayloadLen += byte; + break; + + case 3: + api->SourceAddr = byte; + para->checksum = byte; + break; + + case 4: + val = (uint16_t)byte; + api->Rssi = val << 8; + para->checksum += byte; + break; + + case 5: + api->Rssi += byte; + para->checksum += byte; + break; + + case 6: + val = (uint16_t)byte; + api->Snr = val << 8; + para->checksum += byte; + break; + + case 7: + api->Snr += byte; + para->checksum += byte; + break; + + case 8: + api->PayloadType = byte; + para->checksum += byte; + break; + + default: + if ( para->apipos >= api->PayloadLen + 2 ) // FRM_DEL + CRC = 2 + { + para->Error = ( (0xff - para->checksum) != byte ); + para->Available = true; + api->PayloadLen -= 7; // 7 = SrcAddr[1] + Rssi[2] + Snr[2] + PlType[1] + Crc[1] + para->apipos = 0; + para->checksum = 0; + return true; + } + else + { + para->checksum += byte; + api->Payload[para->apipos - 9] = byte; + } + break; + } + + para->apipos++; + } + return false; +} + +int LoRaLink::send(LoRaLinkPayloadType_t type, const uint8_t* payload, uint16_t pLen, SensorNetAddress* addr) +{ + D_NWSTACK("\r\n===> Send: "); + uint8_t buf[2] = { 0 }; + uint8_t chks = 0; + uint16_t len = pLen + 3; // 3 = DestAddr[1] + PayloadType[1] + Crc[1] + _respCd = 0; + + _serialPortTx->send(START_BYTE); + + buf[0] = (len >> 8) & 0xff; + buf[1] = len & 0xff; + send(buf[0]); + send(buf[1]); + + send( addr->_devAddr ); + chks = addr->_devAddr; + + + + send(type); + chks += type; + + D_NWSTACK("\r\n Payload: "); + + for ( uint8_t i = 0; i < pLen; i++ ){ + send(payload[i]); // Payload + chks += payload[i]; + } + + chks = 0xff - chks; + D_NWSTACK(" checksum "); + send(chks); + D_NWSTACK("\r\n"); + + /* wait ACK */ + _sem.timedwait(LORALINK_TIMEOUT_ACK); + + if ( _respCd == LORALINK_NO_FREE_CH ) + { + D_NWSTACK(" Channel isn't free\r\n"); + return -1; + } + else if ( _respCd != LORALINK_ACK ) + { + D_NWSTACK(" Not Acknowleged\r\n"); + return -1; + } + return (int)pLen; +} + +void LoRaLink::send(uint8_t c) +{ + if( (c == START_BYTE || c == ESCAPE || c == XON || c == XOFF)){ + _serialPortTx->send(ESCAPE); + _serialPortTx->send(c ^ PAD); + }else{ + _serialPortTx->send(c); + } +} + +int LoRaLink::recv(uint8_t* buf) +{ + struct timeval timeout; + int maxfd; + int fd; + fd_set rfds; + + timeout.tv_sec = 1; + timeout.tv_usec = 0; // 500ms + FD_ZERO(&rfds); + FD_SET(_serialPortRx->_fd, &rfds); + FD_SET(_serialPortTx->_fd, &rfds); + if ( _serialPortRx->_fd > _serialPortTx->_fd ) + { + maxfd = _serialPortRx->_fd; + } + else + { + maxfd = _serialPortTx->_fd; + } + + if ( select(maxfd + 1, &rfds, 0, 0, &timeout) > 0 ) + { + if ( FD_ISSET(_serialPortRx->_fd, &rfds) ) + { + fd = _serialPortRx->_fd; + } + else + { + fd = _serialPortTx->_fd; + } + + if ( read(fd, buf, 1) == 1 ) + { + /* + if ( *buf == ESCAPE ) + { + D_NWSTACK( " %02x",buf[0] ); + if ( read(fd, buf, 1) == 1 ) + { + *buf = PAD ^ *buf; + } + else + { + return -1; + } + + } + */ + D_NWSTACK( " %02x",buf[0] ); + return 0; + } + } + return -1; +} + +/*========================================= + Class SerialPort + =========================================*/ +SerialPort::SerialPort() +{ + _tio.c_iflag = IGNBRK | IGNPAR; + _tio.c_cflag = CS8 | CLOCAL | CREAD; + _tio.c_cc[VINTR] = 0; + _tio.c_cc[VTIME] = 10; // 1 sec. + _tio.c_cc[VMIN] = 1; + _fd = 0; +} + +SerialPort::~SerialPort() +{ + if (_fd) + { + ::close(_fd); + } +} + +int SerialPort::open(char* devName, unsigned int baudrate, bool parity, + unsigned int stopbit, unsigned int flg) +{ + _fd = ::open(devName, flg); + if (_fd < 0) + { + return _fd; + } + + if (parity) + { + _tio.c_cflag = _tio.c_cflag | PARENB; + } + if (stopbit == 2) + { + _tio.c_cflag = _tio.c_cflag | CSTOPB; + } + + if (cfsetspeed(&_tio, baudrate) < 0) + { + return errno; + } + return tcsetattr(_fd, TCSANOW, &_tio); +} + +bool SerialPort::send(unsigned char b) +{ + if (write(_fd, &b, 1) <= 0) + { + return false; + } + else + { + D_NWSTACK( " %02x", b); + return true; + } +} + + + +void SerialPort::flush(void) +{ + tcsetattr(_fd, TCSAFLUSH, &_tio); +} + diff --git a/MQTTSNGateway/src/linux/loralink/SensorNetwork.h b/MQTTSNGateway/src/linux/loralink/SensorNetwork.h new file mode 100644 index 0000000..0565ff1 --- /dev/null +++ b/MQTTSNGateway/src/linux/loralink/SensorNetwork.h @@ -0,0 +1,187 @@ +/************************************************************************************** + * 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. + * + * Contributors: + * Tomoaki Yamaguchi - initial API and implementation + **************************************************************************************/ +#ifndef SENSORNETWORKX_H_ +#define SENSORNETWORKX_H_ + +#include "MQTTSNGWDefines.h" +#include "MQTTSNGWProcess.h" +#include +#include + +using namespace std; + +namespace MQTTSNGW +{ +//#define DEBUG_NWSTACK + +#ifdef DEBUG_NWSTACK + #define D_NWSTACK(...) printf(__VA_ARGS__); fflush(stdout) +#else + #define D_NWSTACK(...) +#endif + + +#define XMIT_STATUS_TIME_OVER 5000 + +#define START_BYTE 0x7e +#define ESCAPE 0x7d +#define XON 0x11 +#define XOFF 0x13 +#define PAD 0x20 + +#define BROADCAST_DEVADDR 0xFF + +#define LORA_PHY_MAXPAYLOAD 256 + +/*! + * LoRaLink Modem Type + */ +typedef enum +{ + LORALINK_MODEM_TX, + LORALINK_MODEM_RX, +}LoRaLinkModemType_t; + +/*! + * LoRaLink Serialized API + */ +typedef struct +{ + uint16_t PanId; + uint8_t DestinationAddr; + uint8_t SourceAddr; + uint16_t Rssi; + uint16_t Snr; + uint8_t PayloadType; + uint8_t Payload[LORA_PHY_MAXPAYLOAD]; + uint16_t PayloadLen; +}LoRaLinkFrame_t; + +typedef struct +{ + bool Available; + bool Error; + bool Escape; + uint16_t apipos; + uint8_t checksum; +} LoRaLinkReadParameters_t; + +typedef enum +{ + MQTT_SN = 0x40, + API_RSP_ACK = 0x80, + API_RSP_NFC, + API_RSP_TOT, + API_REQ_UTC, + API_RSP_UTC, + API_CHG_TASK_PARAM, + API_REQ_RESET, + +}LoRaLinkPayloadType_t; + +/*=========================================== + Class SerialPort + ============================================*/ +class SerialPort +{ + friend class LoRaLink; +public: + SerialPort(); + ~SerialPort(); + int open(char* devName, unsigned int baudrate, bool parity, unsigned int stopbit, unsigned int flg); + bool send(unsigned char b); + void flush(); + +private: + int _fd; // file descriptor + struct termios _tio; +}; + +/*=========================================== + Class SensorNetAddreess + ============================================*/ +class SensorNetAddress +{ + friend class LoRaLink; +public: + SensorNetAddress(); + ~SensorNetAddress(); + void setAddress( uint8_t devAddr); + int setAddress(string* data); + void setBroadcastAddress(void); + bool isMatch(SensorNetAddress* addr); + SensorNetAddress& operator =(SensorNetAddress& addr); + char* sprint(char*); +private: + uint8_t _devAddr; +// uint8_t _destAddr; +}; + +/*======================================== + Class LoRaLink + =======================================*/ +class LoRaLink +{ +public: + LoRaLink(); + ~LoRaLink(); + + int open(LoRaLinkModemType_t type, char* device, int boudrate ); + void close(void); + int unicast(const uint8_t* buf, uint16_t length, SensorNetAddress* sendToAddr); + int broadcast(const uint8_t* buf, uint16_t length); + int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr); + void setApiMode(uint8_t mode); + +private: + bool readApiFrame(LoRaLinkFrame_t* api, LoRaLinkReadParameters_t* para); + int recv(uint8_t* buf); + int send(LoRaLinkPayloadType_t type, const uint8_t* payload, uint16_t pLen, SensorNetAddress* addr); + void send(uint8_t b); + + Semaphore _sem; + Mutex _meutex; + uint8_t _respCd; + SerialPort* _serialPortRx; + SerialPort* _serialPortTx; + LoRaLinkFrame_t _loRaLinkApi; + LoRaLinkReadParameters_t _loRaLinkPara; +}; + +/*=========================================== + Class SensorNetwork + ============================================*/ +class SensorNetwork: public LoRaLink +{ +public: + SensorNetwork(); + ~SensorNetwork(); + + int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto); + int broadcast(const uint8_t* payload, uint16_t payloadLength); + int read(uint8_t* buf, uint16_t bufLen); + int initialize(void); + const char* getDescription(void); + SensorNetAddress* getSenderAddress(void); + +private: + SensorNetAddress _clientAddr; // Sender's address. not gateway's one. + string _description; +}; + +} + +#endif /* SENSORNETWORKX_H_ */ From 8ceb7ee67e3c01793f85e0fc15a00f4ea71821dd Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 18:33:01 +0900 Subject: [PATCH 37/43] Bugfix of compile error and update README update Makefile for Mac Signed-off-by: tomoaki --- MQTTSNGateway/Makefile | 18 +++++++++++++----- MQTTSNGateway/README.md | 9 +++++++-- MQTTSNGateway/src/tests/TestTopics.cpp | 6 +++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/MQTTSNGateway/Makefile b/MQTTSNGateway/Makefile index 731d88e..6b9c630 100644 --- a/MQTTSNGateway/Makefile +++ b/MQTTSNGateway/Makefile @@ -18,6 +18,18 @@ OS := linux SENSORNET := udp TEST := tests +# include directories, for MacOS/homebrew add -I/usr/local/opt/openssl/include/ +# library search paths, for MacOS/homebrew add -L/usr/local/opt/openssl/lib/ +ifeq ($(shell) uname),Darwin) + CXX := clang++ + INCLUDE := -I/usr/local/opt/openssl/include/ + LIB := -L/usr/local/opt/openssl/lib/ +else + CXX := g++ + INCLUDE := + LIB := +endif + INSTALL_DIR=../../ CONFIG_DIR=../../ @@ -74,11 +86,8 @@ $(SUBDIR)/MQTTSNSubscribeServer.c \ $(SUBDIR)/MQTTSNUnsubscribeClient.c \ $(SUBDIR)/MQTTSNUnsubscribeServer.c -CXX := g++ CPPFLAGS += -# include directories, for MacOS/homebrew add -I/usr/local/opt/openssl/include/ -INCLUDE := INCLUDES += $(INCLUDE) -I$(SRCDIR) \ -I$(SRCDIR)/$(OS) \ -I$(SRCDIR)/$(OS)/$(SENSORNET) \ @@ -88,8 +97,7 @@ INCLUDES += $(INCLUDE) -I$(SRCDIR) \ # preprocessor defines DEFS := -# library search paths, for MacOS/homebrew add -L/usr/local/opt/openssl/lib/ -LIB := + LIBS += $(LIB) -L/usr/local/lib LDFLAGS := diff --git a/MQTTSNGateway/README.md b/MQTTSNGateway/README.md index c206204..c7a791c 100644 --- a/MQTTSNGateway/README.md +++ b/MQTTSNGateway/README.md @@ -7,12 +7,12 @@ This Gateway can run as a transparent or aggregating Gateway by specifying the g ```` $ git clone -b develop https://github.com/eclipse/paho.mqtt-sn.embedded-c $ cd paho.mqtt-sn.embedded-c/MQTTSNGateway -$ make [SENSORNET={udp6|xbee}] +$ make [SENSORNET={udp6|xbee|loralink}] $ make install $ make clean ```` By default, a gateway for UDP is built. -In order to create a gateway for UDP6 or XBee, SENSORNET argument is required. +In order to create a gateway for UDP6, XBee or LoRaLink, SENSORNET argument is required. MQTT-SNGateway, MQTT-SNLogmonitor and *.conf files are copied into ../ directory. If you want to install the gateway into specific directories, enter a command line as follows: @@ -94,6 +94,11 @@ Baudrate=38400 SerialDevice=/dev/ttyUSB0 ApiMode=2 +#LoRaLink +BaudrateLoRaLink=115200 +DeviceRxLoRaLink=/dev/ttyLoRaLinkRx +DeviceTxLoRaLink=/dev/ttyLoRaLinkTx + # LOG ShearedMemory=NO; diff --git a/MQTTSNGateway/src/tests/TestTopics.cpp b/MQTTSNGateway/src/tests/TestTopics.cpp index 404013e..52c968b 100644 --- a/MQTTSNGateway/src/tests/TestTopics.cpp +++ b/MQTTSNGateway/src/tests/TestTopics.cpp @@ -214,7 +214,7 @@ void TestTopics::test(void) for ( int i = 0; i < 10 ; i++ ) { MQTTSN_topicid tp1; - char tp0[10]; + char tp0[20]; sprintf(tp0, "Topic/%d/%d", i, i); tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; tp1.data.long_.len = strlen(tp0); @@ -237,7 +237,7 @@ void TestTopics::test(void) for ( int i = 0; i < 10 ; i++ ) { MQTTSN_topicid tp1; - char tp0[10]; + char tp0[20]; sprintf(tp0, "Topic/%d", i); tp1.type = MQTTSN_TOPIC_TYPE_NORMAL; tp1.data.long_.len = strlen(tp0); @@ -261,7 +261,7 @@ void TestTopics::test(void) for ( int i = 0; i < 10 ; i++ ) { MQTTSN_topicid tpid1; - char tp0[10]; + char tp0[20]; sprintf(tp0, "TOPIC/%d/%d", i, i); tpid1.type = MQTTSN_TOPIC_TYPE_NORMAL; tpid1.data.long_.len = strlen(tp0); From 910dd07f3aa46e52b726d18c0122e82a06c3f718 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 18:42:10 +0900 Subject: [PATCH 38/43] Comment out debug messages and bugfix of Makefile Signed-off-by: tomoaki --- MQTTSNGateway/Makefile | 20 ++++++-------------- MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp | 2 +- MQTTSNGateway/src/MQTTSNGateway.cpp | 2 +- 4 files changed, 9 insertions(+), 17 deletions(-) diff --git a/MQTTSNGateway/Makefile b/MQTTSNGateway/Makefile index 6b9c630..7e096e9 100644 --- a/MQTTSNGateway/Makefile +++ b/MQTTSNGateway/Makefile @@ -18,18 +18,6 @@ OS := linux SENSORNET := udp TEST := tests -# include directories, for MacOS/homebrew add -I/usr/local/opt/openssl/include/ -# library search paths, for MacOS/homebrew add -L/usr/local/opt/openssl/lib/ -ifeq ($(shell) uname),Darwin) - CXX := clang++ - INCLUDE := -I/usr/local/opt/openssl/include/ - LIB := -L/usr/local/opt/openssl/lib/ -else - CXX := g++ - INCLUDE := - LIB := -endif - INSTALL_DIR=../../ CONFIG_DIR=../../ @@ -88,17 +76,21 @@ $(SUBDIR)/MQTTSNUnsubscribeServer.c CPPFLAGS += +INCLUDE := INCLUDES += $(INCLUDE) -I$(SRCDIR) \ -I$(SRCDIR)/$(OS) \ -I$(SRCDIR)/$(OS)/$(SENSORNET) \ -I$(SUBDIR) \ --I$(SRCDIR)/$(TEST) +-I$(SRCDIR)/$(TEST) \ +-I/usr/local/opt/openssl/include/ # preprocessor defines DEFS := +CXX := g++ -LIBS += $(LIB) -L/usr/local/lib +LIB := +LIBS += $(LIB) -L/usr/local/lib -L/usr/local/opt/openssl/lib/ LDFLAGS := CXXFLAGS := -Wall -O3 -std=c++11 diff --git a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp index d8211e6..ff58a8d 100644 --- a/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWBrokerSendTask.cpp @@ -41,7 +41,7 @@ BrokerSendTask::BrokerSendTask(Gateway* gateway) BrokerSendTask::~BrokerSendTask() { - WRITELOG("BrokerSendTask is deleted normally.\r\n"); +// WRITELOG("BrokerSendTask is deleted normally.\r\n"); } /** diff --git a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp index cd45b1d..b0af063 100644 --- a/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp +++ b/MQTTSNGateway/src/MQTTSNGWClientSendTask.cpp @@ -35,7 +35,7 @@ ClientSendTask::ClientSendTask(Gateway* gateway) ClientSendTask::~ClientSendTask() { - WRITELOG("ClientSendTask is deleted normally.\r\n"); +// WRITELOG("ClientSendTask is deleted normally.\r\n"); } void ClientSendTask::run() diff --git a/MQTTSNGateway/src/MQTTSNGateway.cpp b/MQTTSNGateway/src/MQTTSNGateway.cpp index 1393dfb..6ed0c04 100644 --- a/MQTTSNGateway/src/MQTTSNGateway.cpp +++ b/MQTTSNGateway/src/MQTTSNGateway.cpp @@ -114,7 +114,7 @@ Gateway::~Gateway() { delete _topics; } - WRITELOG("Gateway is deleted normally.\r\n"); +// WRITELOG("Gateway is deleted normally.\r\n"); } int Gateway::getParam(const char* parameter, char* value) From ce8c9cabadf390d21af9b7944ad5ade9bd6ebc92 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 20:15:39 +0900 Subject: [PATCH 39/43] Fix of compile error Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTGWPacket.h | 2 +- MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.h | 1 - MQTTSNGateway/src/linux/Network.cpp | 4 ++++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/MQTTSNGateway/src/MQTTGWPacket.h b/MQTTSNGateway/src/MQTTGWPacket.h index 3a4829e..fb2ad86 100644 --- a/MQTTSNGateway/src/MQTTGWPacket.h +++ b/MQTTSNGateway/src/MQTTGWPacket.h @@ -116,7 +116,7 @@ typedef struct unsigned char version; /**< MQTT version number */ } Connect; -#define MQTTPacket_Connect_Initializer {{0}, 0, nullptr, nullptr, nullptr, nullptr, 0, 0} +#define MQTTPacket_Connect_Initializer {{0}, {0}, nullptr, nullptr, nullptr, nullptr, 0, 0} #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 } #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \ MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } diff --git a/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.h b/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.h index 46bf4f0..53282b3 100644 --- a/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.h +++ b/MQTTSNGateway/src/MQTTSNAggregateConnectionHandler.h @@ -39,7 +39,6 @@ public: private: void sendStoredPublish(Client* client); - char _pbuf[MQTTSNGW_MAX_PACKET_SIZE * 3]; Gateway* _gateway; }; diff --git a/MQTTSNGateway/src/linux/Network.cpp b/MQTTSNGateway/src/linux/Network.cpp index 5c5e88d..842e121 100644 --- a/MQTTSNGateway/src/linux/Network.cpp +++ b/MQTTSNGateway/src/linux/Network.cpp @@ -145,7 +145,11 @@ bool TCPStack::accept(TCPStack& new_socket) int TCPStack::send(const uint8_t* buf, int length) { +#ifdef __APPLE__ + return ::send(_sockfd, buf, length, SO_NOSIGPIPE); +#else return ::send(_sockfd, buf, length, MSG_NOSIGNAL); +#endif } int TCPStack::recv(uint8_t* buf, int len) From 5041caf7a2023fdf28c46b73cc72eca7c7064f6b Mon Sep 17 00:00:00 2001 From: tomoaki Date: Mon, 12 Oct 2020 21:07:09 +0900 Subject: [PATCH 40/43] Avoid compile error of Mac Signed-off-by: tomoaki --- .cproject | 10 +++++----- MQTTSNGateway/GatewayTester/src/LTaskManager.cpp | 6 ++++-- MQTTSNGateway/GatewayTester/src/LTimer.h | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.cproject b/.cproject index ade7cf0..f9a90f6 100644 --- a/.cproject +++ b/.cproject @@ -133,7 +133,9 @@ - + + + @@ -273,11 +275,9 @@ - + - - - + diff --git a/MQTTSNGateway/GatewayTester/src/LTaskManager.cpp b/MQTTSNGateway/GatewayTester/src/LTaskManager.cpp index f38182d..1ec298b 100644 --- a/MQTTSNGateway/GatewayTester/src/LTaskManager.cpp +++ b/MQTTSNGateway/GatewayTester/src/LTaskManager.cpp @@ -56,12 +56,14 @@ void LTaskManager::run(void){ int i = 0; char c = 0; bool cancelFlg = false; + TestList test = {0}; + TaskList task = {0}; if ( !theClientMode ) { theClient->getGwProxy()->getMessage(); - for (i = 0; _tests[i].testTask > 0; i++) + for (i = 0; _tests[i].testTask > test.testTask; i++) { PROMPT("Execute \"%s\" ? ( y/n ) : ", _tests[i].testLabel); while (true) @@ -118,7 +120,7 @@ void LTaskManager::run(void){ while (true) { theClient->getGwProxy()->getMessage(); - for (_index = 0; _tasks[_index].callback > 0; _index++) + for (_index = 0; _tasks[_index].callback > task.callback; _index++) { if ((_tasks[_index].prevTime + _tasks[_index].interval <= time(NULL)) && _tasks[_index].count == 0) diff --git a/MQTTSNGateway/GatewayTester/src/LTimer.h b/MQTTSNGateway/GatewayTester/src/LTimer.h index 9c666cf..cb44cc0 100644 --- a/MQTTSNGateway/GatewayTester/src/LTimer.h +++ b/MQTTSNGateway/GatewayTester/src/LTimer.h @@ -17,7 +17,7 @@ #ifndef TIMER_H_ #define TIMER_H_ -#include +#include #include "LMqttsnClientApp.h" From b4a598e8959037dd0875a13daaac8d14a5b32102 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 13 Oct 2020 11:19:12 +0900 Subject: [PATCH 41/43] Update 9c214a and 438a9a1 Signed-off-by: tomoaki --- MQTTSNGateway/src/MQTTSNGWProcess.cpp | 3 +-- MQTTSNGateway/src/MQTTSNGWVersion.h | 2 +- MQTTSNGateway/src/mainGateway.cpp | 9 +++++++++ 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/MQTTSNGateway/src/MQTTSNGWProcess.cpp b/MQTTSNGateway/src/MQTTSNGWProcess.cpp index 2e4adfa..90accea 100644 --- a/MQTTSNGateway/src/MQTTSNGWProcess.cpp +++ b/MQTTSNGateway/src/MQTTSNGWProcess.cpp @@ -162,8 +162,7 @@ int Process::getParam(const char* parameter, char* value) if ((fp = fopen(configPath.c_str(), "r")) == NULL) { - WRITELOG("No config file:[%s]\n", configPath.c_str()); - return -1; + throw Exception("No config file:[" + configPath + "]\n"); } while (true) diff --git a/MQTTSNGateway/src/MQTTSNGWVersion.h b/MQTTSNGateway/src/MQTTSNGWVersion.h index 9d68241..251e5af 100644 --- a/MQTTSNGateway/src/MQTTSNGWVersion.h +++ b/MQTTSNGateway/src/MQTTSNGWVersion.h @@ -17,6 +17,6 @@ #ifndef MQTTSNGWVERSION_H_IN_ #define MQTTSNGWVERSION_H_IN_ -#define PAHO_GATEWAY_VERSION "1.3.2" +#define PAHO_GATEWAY_VERSION "1.4.0" #endif /* MQTTSNGWVERSION_H_IN_ */ diff --git a/MQTTSNGateway/src/mainGateway.cpp b/MQTTSNGateway/src/mainGateway.cpp index 6fcbbc4..e05effc 100644 --- a/MQTTSNGateway/src/mainGateway.cpp +++ b/MQTTSNGateway/src/mainGateway.cpp @@ -36,5 +36,14 @@ int main(int argc, char** argv) { gateway.initialize(argc, argv); gateway.run(); + try + { + gateway.initialize(argc, argv); + gateway.run(); + } + catch (const std::exception &ex) { + WRITELOG("\nEclipse Paho MQTT-SN Gateway exception: %s\n", ex.what()); + WRITELOG("MQTT-SNGateway [-f Config file name]\n"); + } return 0; } From b387c4c3416790a58f9e8917cc66d33d63f3f9d3 Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 13 Oct 2020 14:38:14 +0900 Subject: [PATCH 42/43] Change Include pathes to Relative path Signed-off-by: tomoaki --- .cproject | 36 +++++++++++++++---------------- .settings/language.settings.xml | 4 ++-- MQTTSNGateway/src/mainGateway.cpp | 3 ++- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/.cproject b/.cproject index f9a90f6..bfdd457 100644 --- a/.cproject +++ b/.cproject @@ -53,17 +53,17 @@ @@ -133,9 +133,7 @@ - - - + @@ -195,15 +193,15 @@ @@ -277,7 +275,7 @@ - + @@ -319,17 +317,19 @@ + + - + - + @@ -348,7 +348,5 @@ - - diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml index 7d5c34b..e5b4485 100644 --- a/.settings/language.settings.xml +++ b/.settings/language.settings.xml @@ -11,7 +11,7 @@ - + @@ -33,7 +33,7 @@ - + diff --git a/MQTTSNGateway/src/mainGateway.cpp b/MQTTSNGateway/src/mainGateway.cpp index e05effc..7d31423 100644 --- a/MQTTSNGateway/src/mainGateway.cpp +++ b/MQTTSNGateway/src/mainGateway.cpp @@ -41,7 +41,8 @@ int main(int argc, char** argv) gateway.initialize(argc, argv); gateway.run(); } - catch (const std::exception &ex) { + catch (const std::exception &ex) + { WRITELOG("\nEclipse Paho MQTT-SN Gateway exception: %s\n", ex.what()); WRITELOG("MQTT-SNGateway [-f Config file name]\n"); } From 63036f1c8fde3890fed8d029887ef247308d0d7a Mon Sep 17 00:00:00 2001 From: tomoaki Date: Tue, 13 Oct 2020 14:52:52 +0900 Subject: [PATCH 43/43] change project's properties Signed-off-by: tomoaki --- .cproject | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.cproject b/.cproject index bfdd457..bf962d5 100644 --- a/.cproject +++ b/.cproject @@ -193,15 +193,15 @@ @@ -273,9 +273,9 @@ - + - +