mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-16 08:56:51 +01:00
QoS-1 PUBLISH is available #34
BugFix of #69 Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -131,8 +131,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
||||
/* create REGISTER */
|
||||
MQTTSNPacket* regPacket = new MQTTSNPacket();
|
||||
|
||||
MQTTSNString topicName;
|
||||
topicName.cstring = 0;
|
||||
MQTTSNString topicName = MQTTSNString_initializer;
|
||||
topicName.lenstring.len = topicId.data.long_.len;
|
||||
topicName.lenstring.data = topicId.data.long_.name;
|
||||
|
||||
|
||||
@@ -19,12 +19,13 @@
|
||||
#include "MQTTSNGWClient.h"
|
||||
#include "MQTTSNGateway.h"
|
||||
#include "SensorNetwork.h"
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
#include "Network.h"
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
|
||||
using namespace MQTTSNGW;
|
||||
char* currentDateTime(void);
|
||||
/*=====================================
|
||||
@@ -80,11 +81,7 @@ bool ClientList::authorize(const char* fileName)
|
||||
bool secure;
|
||||
bool stable;
|
||||
SensorNetAddress netAddr;
|
||||
MQTTSNString clientId;
|
||||
|
||||
clientId.cstring = 0;
|
||||
clientId.lenstring.data = 0;
|
||||
clientId.lenstring.len = 0;
|
||||
MQTTSNString clientId = MQTTSNString_initializer;
|
||||
|
||||
if ((fp = fopen(fileName, "r")) != 0)
|
||||
{
|
||||
@@ -131,13 +128,9 @@ bool ClientList::setPredefinedTopics(const char* fileName)
|
||||
FILE* fp;
|
||||
char buf[MAX_CLIENTID_LENGTH + 256];
|
||||
size_t pos0, pos1;
|
||||
MQTTSNString clientId;
|
||||
MQTTSNString clientId = MQTTSNString_initializer;;
|
||||
bool rc = false;
|
||||
|
||||
clientId.cstring = 0;
|
||||
clientId.lenstring.data = 0;
|
||||
clientId.lenstring.len = 0;
|
||||
|
||||
if ((fp = fopen(fileName, "r")) != 0)
|
||||
{
|
||||
while (fgets(buf, MAX_CLIENTID_LENGTH + 254, fp) != 0)
|
||||
@@ -300,9 +293,8 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||
}
|
||||
else
|
||||
{
|
||||
MQTTSNString dummyId;
|
||||
MQTTSNString dummyId MQTTSNString_initializer;;
|
||||
dummyId.cstring = strdup("");
|
||||
dummyId.lenstring.len = 0;
|
||||
client->setClientId(dummyId);
|
||||
free(dummyId.cstring);
|
||||
}
|
||||
@@ -367,6 +359,7 @@ Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicN
|
||||
|
||||
// create Topic & Add it
|
||||
client->getTopics()->add((const char*)topicName.c_str(), topicId);
|
||||
client->_hasPredefTopic = true;
|
||||
return client;
|
||||
}
|
||||
|
||||
@@ -413,9 +406,11 @@ Client::Client(bool secure)
|
||||
_prevClient = 0;
|
||||
_nextClient = 0;
|
||||
_clientSleepPacketQue.setMaxSize(MAX_SAVED_PUBLISH);
|
||||
_proxyPacketQue.setMaxSize(MAX_SAVED_PUBLISH);
|
||||
_hasPredefTopic = false;
|
||||
_holdPingRequest = false;
|
||||
_forwarder = 0;
|
||||
_isProxy = false;
|
||||
}
|
||||
|
||||
Client::~Client()
|
||||
@@ -485,6 +480,30 @@ int Client::setClientSleepPacket(MQTTGWPacket* packet)
|
||||
return rc;
|
||||
}
|
||||
|
||||
MQTTSNPacket* Client::getProxyPacket(void)
|
||||
{
|
||||
return _proxyPacketQue.getPacket();
|
||||
}
|
||||
|
||||
void Client::deleteFirstProxyPacket()
|
||||
{
|
||||
_proxyPacketQue.pop();
|
||||
}
|
||||
|
||||
int Client::setProxyPacket(MQTTSNPacket* packet)
|
||||
{
|
||||
int rc = _proxyPacketQue.post(packet);
|
||||
if ( rc )
|
||||
{
|
||||
WRITELOG("%s %s is Disconnected. the packet was saved.\n", currentDateTime(), _clientId);
|
||||
}
|
||||
else
|
||||
{
|
||||
WRITELOG("%s %s is Disconnected and discard the packet.\n", currentDateTime(), _clientId);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
Connect* Client::getConnectData(void)
|
||||
{
|
||||
return &_connectData;
|
||||
@@ -572,7 +591,10 @@ void Client::updateStatus(MQTTSNPacket* packet)
|
||||
case MQTTSN_PUBCOMP:
|
||||
case MQTTSN_PUBREL:
|
||||
case MQTTSN_PUBREC:
|
||||
_keepAliveTimer.start(_keepAliveMsec * 1.5);
|
||||
if ( !_isProxy )
|
||||
{
|
||||
_keepAliveTimer.start(_keepAliveMsec * 1.5);
|
||||
}
|
||||
break;
|
||||
case MQTTSN_DISCONNECT:
|
||||
uint16_t duration;
|
||||
@@ -641,9 +663,14 @@ void Client::disconnected(void)
|
||||
_waitWillMsgFlg = false;
|
||||
}
|
||||
|
||||
void Client::tryConnect(void)
|
||||
{
|
||||
_status = Cstat_TryConnecting;
|
||||
}
|
||||
|
||||
bool Client::isConnectSendable(void)
|
||||
{
|
||||
if (_status == Cstat_Lost || _status == Cstat_TryConnecting)
|
||||
if ( _status == Cstat_Lost || _status == Cstat_TryConnecting )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -703,6 +730,11 @@ void Client::setTopics(Topics* topics)
|
||||
_topics = topics;
|
||||
}
|
||||
|
||||
ClientStatus Client::getClientStatus(void)
|
||||
{
|
||||
return _status;
|
||||
}
|
||||
|
||||
void Client::setWaitWillMsgFlg(bool flg)
|
||||
{
|
||||
_waitWillMsgFlg = flg;
|
||||
@@ -733,6 +765,11 @@ bool Client::isAwake(void)
|
||||
return (_status == Cstat_Awake);
|
||||
}
|
||||
|
||||
bool Client::isConnecting(void)
|
||||
{
|
||||
return (_status == Cstat_Connecting);
|
||||
}
|
||||
|
||||
bool Client::isSecureNetwork(void)
|
||||
{
|
||||
return _secureNetwork;
|
||||
@@ -760,10 +797,18 @@ void Client::setClientId(MQTTSNString id)
|
||||
free(_clientId);
|
||||
}
|
||||
|
||||
/* save clientId into (char*)_clientId NULL terminated */
|
||||
_clientId = (char*)calloc(MQTTSNstrlen(id) + 1, 1);
|
||||
unsigned char* ptr = (unsigned char*)_clientId;
|
||||
writeMQTTSNString((unsigned char**)&ptr, id);
|
||||
if ( id.cstring )
|
||||
{
|
||||
_clientId = (char*)calloc(strlen(id.cstring) + 1, 1);
|
||||
memcpy(_clientId, id.cstring, strlen(id.cstring));
|
||||
}
|
||||
else
|
||||
{
|
||||
/* save clientId into (char*)_clientId NULL terminated */
|
||||
_clientId = (char*)calloc(MQTTSNstrlen(id) + 1, 1);
|
||||
unsigned char* ptr = (unsigned char*)_clientId;
|
||||
writeMQTTSNString((unsigned char**)&ptr, id);
|
||||
}
|
||||
}
|
||||
|
||||
void Client::setWillTopic(MQTTSNString willTopic)
|
||||
@@ -812,14 +857,14 @@ const char* Client::getStatus(void)
|
||||
return theClientStatus[_status];
|
||||
}
|
||||
|
||||
Client* Client::getOTAClient(void)
|
||||
bool Client::isProxy(void)
|
||||
{
|
||||
return _otaClient;
|
||||
return _isProxy;
|
||||
}
|
||||
|
||||
void Client::setOTAClient(Client* cl)
|
||||
void Client::setPorxy(bool isProxy)
|
||||
{
|
||||
_otaClient =cl;
|
||||
_isProxy = isProxy;;
|
||||
}
|
||||
|
||||
void Client::holdPingRequest(void)
|
||||
|
||||
@@ -253,6 +253,9 @@ public:
|
||||
TopicIdMapelement* getWaitedSubTopicId(uint16_t msgId);
|
||||
MQTTGWPacket* getClientSleepPacket(void);
|
||||
void deleteFirstClientSleepPacket(void);
|
||||
|
||||
MQTTSNPacket* getProxyPacket(void);
|
||||
void deleteFirstProxyPacket(void);
|
||||
WaitREGACKPacketList* getWaitREGACKPacketList(void);
|
||||
|
||||
void eraseWaitedPubTopicId(uint16_t msgId);
|
||||
@@ -261,6 +264,7 @@ public:
|
||||
void clearWaitedSubTopicId(void);
|
||||
|
||||
int setClientSleepPacket(MQTTGWPacket*);
|
||||
int setProxyPacket(MQTTSNPacket* packet);
|
||||
void setWaitedPubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
|
||||
void setWaitedSubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
|
||||
|
||||
@@ -271,6 +275,8 @@ public:
|
||||
void connackSended(int rc);
|
||||
void disconnected(void);
|
||||
bool isConnectSendable(void);
|
||||
void tryConnect(void);
|
||||
ClientStatus getClientStatus(void);
|
||||
|
||||
uint16_t getNextPacketId(void);
|
||||
uint8_t getNextSnMsgId(void);
|
||||
@@ -286,6 +292,9 @@ public:
|
||||
Forwarder* getForwarder(void);
|
||||
void setForwarder(Forwarder* forwader);
|
||||
|
||||
void setPorxy(bool isProxy);
|
||||
bool isProxy(void);
|
||||
|
||||
void setClientId(MQTTSNString id);
|
||||
void setWillTopic(MQTTSNString willTopic);
|
||||
void setWillMsg(MQTTSNString willmsg);
|
||||
@@ -298,6 +307,7 @@ public:
|
||||
bool erasable(void);
|
||||
|
||||
bool isDisconnect(void);
|
||||
bool isConnecting(void);
|
||||
bool isActive(void);
|
||||
bool isSleep(void);
|
||||
bool isAwake(void);
|
||||
@@ -310,11 +320,11 @@ public:
|
||||
bool isHoldPringReqest(void);
|
||||
|
||||
Client* getNextClient(void);
|
||||
Client* getOTAClient(void);
|
||||
void setOTAClient(Client* cl);
|
||||
|
||||
private:
|
||||
PacketQue<MQTTGWPacket> _clientSleepPacketQue;
|
||||
PacketQue<MQTTSNPacket> _proxyPacketQue;
|
||||
|
||||
WaitREGACKPacketList _waitREGACKList;
|
||||
|
||||
Topics* _topics;
|
||||
@@ -345,6 +355,7 @@ private:
|
||||
SensorNetAddress _sensorNetAddr;
|
||||
|
||||
Forwarder* _forwarder;
|
||||
bool _isProxy;
|
||||
|
||||
|
||||
bool _sessionStatus;
|
||||
|
||||
252
MQTTSNGateway/src/MQTTSNGWClientProxy.cpp
Normal file
252
MQTTSNGateway/src/MQTTSNGWClientProxy.cpp
Normal file
@@ -0,0 +1,252 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2018, 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 and/or initial documentation
|
||||
**************************************************************************************/
|
||||
|
||||
|
||||
#include "MQTTSNGWDefines.h"
|
||||
#include "MQTTSNGWClientProxy.h"
|
||||
#include "MQTTSNGateway.h"
|
||||
#include "SensorNetwork.h"
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
using namespace MQTTSNGW;
|
||||
|
||||
#define RESPONSE_DURATION 900 // Secs
|
||||
|
||||
/*
|
||||
* Class ClientProxyElement
|
||||
*/
|
||||
|
||||
ClientProxyElement::ClientProxyElement(void)
|
||||
: _clientId{0}
|
||||
, _next{0}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ClientProxyElement::ClientProxyElement(SensorNetAddress* addr, string* clientId)
|
||||
: _next{0}
|
||||
{
|
||||
_clientId = *clientId;
|
||||
_sensorNetAddr = *addr;
|
||||
}
|
||||
|
||||
ClientProxyElement::~ClientProxyElement(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Class ClientProxy
|
||||
*/
|
||||
|
||||
ClientProxy:: ClientProxy(void)
|
||||
: _head{0}
|
||||
{
|
||||
_gateway = 0;
|
||||
_client = 0;
|
||||
}
|
||||
|
||||
ClientProxy:: ClientProxy(Gateway* gw)
|
||||
: _head{0}
|
||||
{
|
||||
_gateway = gw;
|
||||
_client = 0;
|
||||
}
|
||||
|
||||
|
||||
ClientProxy::~ClientProxy(void)
|
||||
{
|
||||
if ( _head )
|
||||
{
|
||||
ClientProxyElement* p = _head;
|
||||
while ( p )
|
||||
{
|
||||
ClientProxyElement* next = p->_next;
|
||||
delete p;
|
||||
p = next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ClientProxy::setGateway(Gateway* gw)
|
||||
{
|
||||
_gateway = gw;
|
||||
}
|
||||
|
||||
ClientProxyElement* ClientProxy::add(SensorNetAddress* addr, string* clientId)
|
||||
{
|
||||
ClientProxyElement* elm = new ClientProxyElement(addr, clientId);
|
||||
if ( _head == 0 )
|
||||
{
|
||||
_head = elm;
|
||||
}
|
||||
else
|
||||
{
|
||||
ClientProxyElement* p = _head;
|
||||
while ( p )
|
||||
{
|
||||
if ( p->_next == 0 )
|
||||
{
|
||||
p->_next = elm;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
p = p->_next;
|
||||
}
|
||||
}
|
||||
}
|
||||
return elm;
|
||||
}
|
||||
|
||||
const char* ClientProxy::getClientId(SensorNetAddress* addr)
|
||||
{
|
||||
ClientProxyElement* p = _head;
|
||||
while ( p )
|
||||
{
|
||||
if ( p->_sensorNetAddr.isMatch(addr) )
|
||||
{
|
||||
return p->_clientId.c_str();
|
||||
break;
|
||||
}
|
||||
p = p->_next;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ClientProxy::setClient(Client* client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
Client* ClientProxy::getClient(void)
|
||||
{
|
||||
return _client;
|
||||
}
|
||||
|
||||
bool ClientProxy::setClientProxy(const char* fileName)
|
||||
{
|
||||
FILE* fp;
|
||||
char buf[MAX_CLIENTID_LENGTH + 256];
|
||||
size_t pos;
|
||||
|
||||
SensorNetAddress netAddr;
|
||||
|
||||
if ((fp = fopen(fileName, "r")) != 0)
|
||||
{
|
||||
while (fgets(buf, MAX_CLIENTID_LENGTH + 254, fp) != 0)
|
||||
{
|
||||
if (*buf == '#')
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string data = string(buf);
|
||||
while ((pos = data.find_first_of(" \t\n")) != string::npos)
|
||||
{
|
||||
data.erase(pos, 1);
|
||||
}
|
||||
if (data.empty())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
pos = data.find_first_of(",");
|
||||
string id = data.substr(0, pos);
|
||||
string addr = data.substr(pos + 1);
|
||||
|
||||
if (netAddr.setAddress(&addr) == 0)
|
||||
{
|
||||
add(&netAddr, &id);
|
||||
}
|
||||
else
|
||||
{
|
||||
WRITELOG("Invalid address %s\n", data.c_str());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
WRITELOG("Can not open the QoS_1Client List. %s\n", fileName);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void ClientProxy::checkConnection(void)
|
||||
{
|
||||
if ( _client->isDisconnect() || ( _client->isConnecting() && _responseTimer.isTimeup()) )
|
||||
{
|
||||
_client->connectSended();
|
||||
_responseTimer.start(RESPONSE_DURATION * 1000UL);
|
||||
MQTTSNPacket_connectData options = MQTTSNPacket_connectData_initializer;
|
||||
options.clientID.cstring = _client->getClientId();
|
||||
options.duration = RESPONSE_DURATION;
|
||||
|
||||
MQTTSNPacket* packet = new MQTTSNPacket();
|
||||
packet->setCONNECT(&options);
|
||||
Event* ev = new Event();
|
||||
ev->setClientRecvEvent(_client, packet);
|
||||
_gateway->getPacketEventQue()->post(ev);
|
||||
|
||||
}
|
||||
else if ( _client->isActive() && _keepAliveTimer.isTimeup() )
|
||||
{
|
||||
MQTTSNPacket* packet = new MQTTSNPacket();
|
||||
MQTTSNString clientId = MQTTSNString_initializer;
|
||||
packet->setPINGREQ(&clientId);
|
||||
Event* ev = new Event();
|
||||
ev->setClientRecvEvent(_client, packet);
|
||||
_gateway->getPacketEventQue()->post(ev);
|
||||
resetPingTimer();
|
||||
}
|
||||
}
|
||||
|
||||
void ClientProxy::resetPingTimer(void)
|
||||
{
|
||||
_keepAliveTimer.start(RESPONSE_DURATION * 1000UL);
|
||||
}
|
||||
|
||||
void ClientProxy::send(MQTTSNPacket* packet)
|
||||
{
|
||||
if ( packet->getType() == MQTTSN_CONNACK || packet->getType() == MQTTSN_PINGRESP )
|
||||
{
|
||||
resetPingTimer();
|
||||
sendStoredPublish();
|
||||
}
|
||||
else if ( packet->getType() == MQTTSN_PINGRESP )
|
||||
{
|
||||
resetPingTimer();
|
||||
}
|
||||
}
|
||||
|
||||
void ClientProxy::sendStoredPublish(void)
|
||||
{
|
||||
MQTTSNPacket* msg = 0;
|
||||
|
||||
while ( ( msg = _client->getProxyPacket() ) != 0 )
|
||||
{
|
||||
_client->deleteFirstProxyPacket(); // pop the que to delete element.
|
||||
|
||||
Event* ev = new Event();
|
||||
ev->setClientRecvEvent(_client, msg);
|
||||
_gateway->getPacketEventQue()->post(ev);
|
||||
}
|
||||
}
|
||||
77
MQTTSNGateway/src/MQTTSNGWClientProxy.h
Normal file
77
MQTTSNGateway/src/MQTTSNGWClientProxy.h
Normal file
@@ -0,0 +1,77 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2018, 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 and/or initial documentation
|
||||
**************************************************************************************/
|
||||
|
||||
#ifndef MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_
|
||||
#define MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_
|
||||
|
||||
#include "MQTTSNGateway.h"
|
||||
#include "MQTTGWPacket.h"
|
||||
#include "MQTTSNGWClient.h"
|
||||
#include "SensorNetwork.h"
|
||||
#include "MQTTSNGWProcess.h"
|
||||
|
||||
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
class Gateway;
|
||||
|
||||
class ClientProxyElement
|
||||
{
|
||||
friend class ClientProxy;
|
||||
public:
|
||||
ClientProxyElement(void);
|
||||
ClientProxyElement(SensorNetAddress* addr, string* clientId);
|
||||
~ClientProxyElement(void);
|
||||
private:
|
||||
SensorNetAddress _sensorNetAddr;
|
||||
string _clientId;
|
||||
ClientProxyElement* _next;
|
||||
};
|
||||
|
||||
class ClientProxy
|
||||
{
|
||||
public:
|
||||
ClientProxy(void);
|
||||
ClientProxy(Gateway* gw);
|
||||
~ClientProxy(void);
|
||||
bool setClientProxy(const char* fileName);
|
||||
ClientProxyElement* add(SensorNetAddress* addr, string* clientId);
|
||||
const char* getClientId(SensorNetAddress* addr);
|
||||
void setClient(Client*);
|
||||
Client* getClient(void);
|
||||
void setGateway(Gateway* gw);
|
||||
void setKeepAlive(uint16_t secs);
|
||||
|
||||
void checkConnection(void);
|
||||
void resetPingTimer(void);
|
||||
void send(MQTTSNPacket* packet);
|
||||
|
||||
private:
|
||||
void sendStoredPublish(void);
|
||||
|
||||
Gateway* _gateway;
|
||||
Client* _client;
|
||||
ClientProxyElement* _head;
|
||||
Timer _keepAliveTimer;
|
||||
Timer _responseTimer;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTPROXY_H_ */
|
||||
@@ -17,10 +17,11 @@
|
||||
#include "MQTTSNGWClientRecvTask.h"
|
||||
#include "MQTTSNGateway.h"
|
||||
#include "MQTTSNPacket.h"
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
#include "MQTTSNGWEncapsulatedPacket.h"
|
||||
#include <cstring>
|
||||
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
|
||||
using namespace MQTTSNGW;
|
||||
char* currentDateTime(void);
|
||||
/*=====================================
|
||||
@@ -91,31 +92,32 @@ void ClientRecvTask::run()
|
||||
if ( packet->getType() == MQTTSN_SEARCHGW )
|
||||
{
|
||||
/* write log and post Event */
|
||||
log(0, packet);
|
||||
log(0, packet, 0);
|
||||
ev = new Event();
|
||||
ev->setBrodcastEvent(packet);
|
||||
_gateway->getPacketEventQue()->post(ev);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if ( packet->getType() == MQTTSN_ENCAPSULATED )
|
||||
{
|
||||
fwd = _gateway->getForwarderList()->getForwarder(_sensorNetwork->getSenderAddress());
|
||||
|
||||
if ( fwd == 0 )
|
||||
{
|
||||
log(0, packet);
|
||||
WRITELOG("%s Forwarder %s is not authorized.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER);
|
||||
log(0, packet, 0);
|
||||
WRITELOG("%s Forwarder %s is not authenticated.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER);
|
||||
delete packet;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
MQTTSNString fwdName;
|
||||
fwdName.lenstring.data = const_cast<char *>( fwd->getName() );
|
||||
fwdName.lenstring.len = strlen(fwdName.lenstring.data);
|
||||
MQTTSNString fwdName = MQTTSNString_initializer;
|
||||
fwdName.cstring = const_cast<char *>( fwd->getName() );
|
||||
log(0, packet, &fwdName);
|
||||
|
||||
/* get the packet from the encapsulation message */
|
||||
MQTTSNGWEncapsulatedPacket encap;
|
||||
encap.desirialize(packet->getPacketData(), packet->getPacketLength());
|
||||
nodeId.setId( encap.getWirelessNodeId() );
|
||||
@@ -126,15 +128,35 @@ void ClientRecvTask::run()
|
||||
}
|
||||
else
|
||||
{
|
||||
/* get client from the ClientList of Gateway by sensorNetAddress. */
|
||||
client = _gateway->getClientList()->getClient(_sensorNetwork->getSenderAddress());
|
||||
const char* clientName = _gateway->getClientProxy()->getClientId(_sensorNetwork->getSenderAddress());
|
||||
|
||||
if ( clientName ) // This client is for QoS-1 PUBLISH.
|
||||
{
|
||||
if ( packet->isQoSMinusPUBLISH() )
|
||||
{
|
||||
client = _gateway->getClientProxy()->getClient(); // point to the ClientProxy
|
||||
}
|
||||
else
|
||||
{
|
||||
client = _gateway->getClientProxy()->getClient();
|
||||
log(clientName, packet);
|
||||
WRITELOG("%s %s %s can send only PUBLISH with QoS-1.%s\n", ERRMSG_HEADER, clientName, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER);
|
||||
delete packet;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* get client from the ClientList of Gateway by sensorNetAddress. */
|
||||
client = _gateway->getClientList()->getClient(_sensorNetwork->getSenderAddress());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( client )
|
||||
{
|
||||
/* write log and post Event */
|
||||
log(client, packet);
|
||||
log(client, packet, 0);
|
||||
ev = new Event();
|
||||
ev->setClientRecvEvent(client,packet);
|
||||
_gateway->getPacketEventQue()->post(ev);
|
||||
@@ -196,7 +218,7 @@ void ClientRecvTask::run()
|
||||
}
|
||||
else
|
||||
{
|
||||
log(client, packet);
|
||||
log(client, packet, 0);
|
||||
WRITELOG("%s Client(%s) is not connecting. message has been discarded.%s\n", ERRMSG_HEADER, _sensorNetwork->getSenderAddress()->sprint(buf), ERRMSG_FOOTER);
|
||||
delete packet;
|
||||
|
||||
@@ -216,16 +238,22 @@ void ClientRecvTask::run()
|
||||
|
||||
void ClientRecvTask::log(Client* client, MQTTSNPacket* packet, MQTTSNString* id)
|
||||
{
|
||||
char pbuf[SIZE_OF_LOG_PACKET * 3];
|
||||
const char* clientId;
|
||||
char cstr[MAX_CLIENTID_LENGTH + 1];
|
||||
char msgId[6];
|
||||
|
||||
if ( id )
|
||||
{
|
||||
memset((void*)cstr, 0, id->lenstring.len + 1);
|
||||
strncpy(cstr, id->lenstring.data, id->lenstring.len) ;
|
||||
clientId = cstr;
|
||||
if ( id->cstring )
|
||||
{
|
||||
strncpy(cstr, id->cstring, strlen(id->cstring) );
|
||||
clientId = cstr;
|
||||
}
|
||||
else
|
||||
{
|
||||
memset((void*)cstr, 0, id->lenstring.len + 1);
|
||||
strncpy(cstr, id->lenstring.data, id->lenstring.len );
|
||||
clientId = cstr;
|
||||
}
|
||||
}
|
||||
else if ( client )
|
||||
{
|
||||
@@ -236,40 +264,48 @@ void ClientRecvTask::log(Client* client, MQTTSNPacket* packet, MQTTSNString* id)
|
||||
clientId = UNKNOWNCL;
|
||||
}
|
||||
|
||||
switch (packet->getType())
|
||||
{
|
||||
case MQTTSN_SEARCHGW:
|
||||
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, CLIENT, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_CONNECT:
|
||||
case MQTTSN_PINGREQ:
|
||||
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_DISCONNECT:
|
||||
case MQTTSN_WILLTOPICUPD:
|
||||
case MQTTSN_WILLMSGUPD:
|
||||
case MQTTSN_WILLTOPIC:
|
||||
case MQTTSN_WILLMSG:
|
||||
WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_PUBLISH:
|
||||
case MQTTSN_REGISTER:
|
||||
case MQTTSN_SUBSCRIBE:
|
||||
case MQTTSN_UNSUBSCRIBE:
|
||||
WRITELOG(FORMAT_G_MSGID_G_G_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_REGACK:
|
||||
case MQTTSN_PUBACK:
|
||||
case MQTTSN_PUBREC:
|
||||
case MQTTSN_PUBREL:
|
||||
case MQTTSN_PUBCOMP:
|
||||
WRITELOG(FORMAT_G_MSGID_G_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_ENCAPSULATED:
|
||||
WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
default:
|
||||
WRITELOG(FORMAT_W_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
}
|
||||
log(clientId, packet);
|
||||
}
|
||||
|
||||
void ClientRecvTask::log(const char* clientId, MQTTSNPacket* packet)
|
||||
{
|
||||
char pbuf[SIZE_OF_LOG_PACKET * 3];
|
||||
char msgId[6];
|
||||
|
||||
switch (packet->getType())
|
||||
{
|
||||
case MQTTSN_SEARCHGW:
|
||||
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, CLIENT, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_CONNECT:
|
||||
case MQTTSN_PINGREQ:
|
||||
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_DISCONNECT:
|
||||
case MQTTSN_WILLTOPICUPD:
|
||||
case MQTTSN_WILLMSGUPD:
|
||||
case MQTTSN_WILLTOPIC:
|
||||
case MQTTSN_WILLMSG:
|
||||
WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_PUBLISH:
|
||||
case MQTTSN_REGISTER:
|
||||
case MQTTSN_SUBSCRIBE:
|
||||
case MQTTSN_UNSUBSCRIBE:
|
||||
WRITELOG(FORMAT_G_MSGID_G_G_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_REGACK:
|
||||
case MQTTSN_PUBACK:
|
||||
case MQTTSN_PUBREC:
|
||||
case MQTTSN_PUBREL:
|
||||
case MQTTSN_PUBCOMP:
|
||||
WRITELOG(FORMAT_G_MSGID_G_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_ENCAPSULATED:
|
||||
WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
default:
|
||||
WRITELOG(FORMAT_W_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,8 @@ public:
|
||||
void run();
|
||||
|
||||
private:
|
||||
void log(Client*, MQTTSNPacket*, MQTTSNString* id = 0);
|
||||
|
||||
void log(Client*, MQTTSNPacket*, MQTTSNString* id);
|
||||
void log(const char* clientId, MQTTSNPacket* packet);
|
||||
Gateway* _gateway;
|
||||
SensorNetwork* _sensorNetwork;
|
||||
};
|
||||
|
||||
@@ -71,6 +71,11 @@ void ClientSendTask::run()
|
||||
else
|
||||
{
|
||||
log(client, packet);
|
||||
if ( client->isProxy() )
|
||||
{
|
||||
_gateway->getClientProxy()->send(packet);
|
||||
continue;
|
||||
}
|
||||
rc = packet->unicast(_sensorNetwork, client->getSensorNetAddress());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +90,10 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
|
||||
//* clear ConnectData of Client */
|
||||
Connect* connectData = client->getConnectData();
|
||||
memset(connectData, 0, sizeof(Connect));
|
||||
client->disconnected();
|
||||
if ( !client->isProxy() )
|
||||
{
|
||||
client->disconnected();
|
||||
}
|
||||
|
||||
Topics* topics = client->getTopics();
|
||||
|
||||
@@ -153,7 +156,7 @@ void MQTTSNConnectionHandler::handleWilltopic(Client* client, MQTTSNPacket* pack
|
||||
{
|
||||
int willQos;
|
||||
uint8_t willRetain;
|
||||
MQTTSNString willTopic;
|
||||
MQTTSNString willTopic = MQTTSNString_initializer;
|
||||
|
||||
if ( packet->getWILLTOPIC(&willQos, &willRetain, &willTopic) == 0 )
|
||||
{
|
||||
@@ -187,7 +190,7 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
|
||||
return;
|
||||
}
|
||||
|
||||
MQTTSNString willmsg;
|
||||
MQTTSNString willmsg = MQTTSNString_initializer;
|
||||
Connect* connectData = client->getConnectData();
|
||||
|
||||
if( client->isConnectSendable() )
|
||||
|
||||
@@ -27,6 +27,7 @@ namespace MQTTSNGW
|
||||
#define CLIENT_LIST "clients.conf"
|
||||
#define PREDEFINEDTOPIC_FILE "predefinedTopic.conf"
|
||||
#define FORWARDER_LIST "forwarders.conf"
|
||||
#define QOS_1CLIENT_LIST "qos-1clients.conf"
|
||||
|
||||
/*==========================================================
|
||||
* Gateway default parameters
|
||||
|
||||
@@ -13,9 +13,10 @@
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation and/or initial documentation
|
||||
**************************************************************************************/
|
||||
#include <string.h>
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
using namespace MQTTSNGW;
|
||||
using namespace std;
|
||||
|
||||
@@ -155,10 +156,10 @@ Forwarder::~Forwarder(void)
|
||||
{
|
||||
if ( _headClient )
|
||||
{
|
||||
ForwardedClient* p = _headClient;
|
||||
ForwarderElement* p = _headClient;
|
||||
while ( p )
|
||||
{
|
||||
ForwardedClient* next = p->_next;
|
||||
ForwarderElement* next = p->_next;
|
||||
delete p;
|
||||
p = next;
|
||||
}
|
||||
@@ -172,8 +173,8 @@ const char* Forwarder::getId(void)
|
||||
|
||||
void Forwarder::addClient(Client* client, WirelessNodeId* id)
|
||||
{
|
||||
ForwardedClient* p = _headClient;
|
||||
ForwardedClient* prev = 0;
|
||||
ForwarderElement* p = _headClient;
|
||||
ForwarderElement* prev = 0;
|
||||
|
||||
client->setForwarder(this);
|
||||
|
||||
@@ -191,7 +192,7 @@ void Forwarder::addClient(Client* client, WirelessNodeId* id)
|
||||
}
|
||||
}
|
||||
|
||||
ForwardedClient* fclient = new ForwardedClient();
|
||||
ForwarderElement* fclient = new ForwarderElement();
|
||||
|
||||
fclient->setClient(client);
|
||||
fclient->setWirelessNodeId(id);
|
||||
@@ -210,7 +211,7 @@ Client* Forwarder::getClient(WirelessNodeId* id)
|
||||
{
|
||||
Client* cl = 0;
|
||||
_mutex.lock();
|
||||
ForwardedClient* p = _headClient;
|
||||
ForwarderElement* p = _headClient;
|
||||
while ( p )
|
||||
{
|
||||
if ( *(p->_wirelessNodeId) == *id )
|
||||
@@ -236,7 +237,7 @@ WirelessNodeId* Forwarder::getWirelessNodeId(Client* client)
|
||||
{
|
||||
WirelessNodeId* nodeId = 0;
|
||||
_mutex.lock();
|
||||
ForwardedClient* p = _headClient;
|
||||
ForwarderElement* p = _headClient;
|
||||
while ( p )
|
||||
{
|
||||
if ( p->_client == client )
|
||||
@@ -255,9 +256,9 @@ WirelessNodeId* Forwarder::getWirelessNodeId(Client* client)
|
||||
|
||||
void Forwarder::eraseClient(Client* client)
|
||||
{
|
||||
ForwardedClient* prev = 0;
|
||||
ForwarderElement* prev = 0;
|
||||
_mutex.lock();
|
||||
ForwardedClient* p = _headClient;
|
||||
ForwarderElement* p = _headClient;
|
||||
|
||||
while ( p )
|
||||
{
|
||||
@@ -291,14 +292,14 @@ SensorNetAddress* Forwarder::getSensorNetAddr(void)
|
||||
* Class ForwardedClient
|
||||
*/
|
||||
|
||||
ForwardedClient::ForwardedClient()
|
||||
ForwarderElement::ForwarderElement()
|
||||
: _client{0}
|
||||
, _wirelessNodeId{0}
|
||||
, _next{0}
|
||||
{
|
||||
}
|
||||
|
||||
ForwardedClient::~ForwardedClient()
|
||||
ForwarderElement::~ForwarderElement()
|
||||
{
|
||||
if (_wirelessNodeId)
|
||||
{
|
||||
@@ -306,12 +307,12 @@ ForwardedClient::~ForwardedClient()
|
||||
}
|
||||
}
|
||||
|
||||
void ForwardedClient::setClient(Client* client)
|
||||
void ForwarderElement::setClient(Client* client)
|
||||
{
|
||||
_client = client;
|
||||
}
|
||||
|
||||
void ForwardedClient::setWirelessNodeId(WirelessNodeId* id)
|
||||
void ForwarderElement::setWirelessNodeId(WirelessNodeId* id)
|
||||
{
|
||||
if ( _wirelessNodeId == 0 )
|
||||
{
|
||||
|
||||
@@ -28,18 +28,18 @@ namespace MQTTSNGW
|
||||
class Client;
|
||||
class WirelessNodeId;
|
||||
|
||||
class ForwardedClient
|
||||
class ForwarderElement
|
||||
{
|
||||
friend class Forwarder;
|
||||
public:
|
||||
ForwardedClient();
|
||||
~ForwardedClient();
|
||||
ForwarderElement();
|
||||
~ForwarderElement();
|
||||
void setClient(Client* client);
|
||||
void setWirelessNodeId(WirelessNodeId* id);
|
||||
private:
|
||||
Client* _client;
|
||||
WirelessNodeId* _wirelessNodeId;
|
||||
ForwardedClient* _next;
|
||||
ForwarderElement* _next;
|
||||
};
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ public:
|
||||
private:
|
||||
string _forwarderName;
|
||||
SensorNetAddress _sensorNetAddr;
|
||||
ForwardedClient* _headClient;
|
||||
ForwarderElement* _headClient;
|
||||
Forwarder* _next;
|
||||
Mutex _mutex;
|
||||
};
|
||||
|
||||
@@ -101,6 +101,17 @@ int MQTTSNPacket::getType(void)
|
||||
return _buf[p];
|
||||
}
|
||||
|
||||
bool MQTTSNPacket::isQoSMinusPUBLISH(void)
|
||||
{
|
||||
if ( _bufLen == 0 )
|
||||
{
|
||||
return false;;
|
||||
}
|
||||
int value = 0;
|
||||
int p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
||||
return ( (_buf[p] == MQTTSN_PUBLISH) && ((_buf[p + 1] & 0x60 ) == 0x60 ));
|
||||
}
|
||||
|
||||
unsigned char* MQTTSNPacket::getPacketData(void)
|
||||
{
|
||||
return _buf;
|
||||
@@ -119,7 +130,8 @@ const char* MQTTSNPacket::getName()
|
||||
int MQTTSNPacket::setADVERTISE(uint8_t gatewayid, uint16_t duration)
|
||||
{
|
||||
unsigned char buf[5];
|
||||
int len = MQTTSNSerialize_advertise(buf, 5, (unsigned char) gatewayid,
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_advertise(buf, buflen, (unsigned char) gatewayid,
|
||||
(unsigned short) duration);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
@@ -127,44 +139,50 @@ int MQTTSNPacket::setADVERTISE(uint8_t gatewayid, uint16_t duration)
|
||||
int MQTTSNPacket::setGWINFO(uint8_t gatewayId)
|
||||
{
|
||||
unsigned char buf[3];
|
||||
int len = MQTTSNSerialize_gwinfo(buf, 3, (unsigned char) gatewayId, 0, 0);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_gwinfo(buf, buflen, (unsigned char) gatewayId, 0, 0);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setConnect(void)
|
||||
{
|
||||
unsigned char buf[40];
|
||||
int buflen = sizeof(buf);
|
||||
MQTTSNPacket_connectData data;
|
||||
data.clientID.cstring = (char*)"client01";
|
||||
int len = MQTTSNSerialize_connect(buf, 40, &data);
|
||||
int len = MQTTSNSerialize_connect(buf, buflen, &data);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setCONNACK(uint8_t returnCode)
|
||||
{
|
||||
unsigned char buf[3];
|
||||
int len = MQTTSNSerialize_connack(buf, 3, (int) returnCode);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_connack(buf, buflen, (int) returnCode);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setWILLTOPICREQ(void)
|
||||
{
|
||||
unsigned char buf[2];
|
||||
int len = MQTTSNSerialize_willtopicreq(buf, 2);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_willtopicreq(buf, buflen);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setWILLMSGREQ(void)
|
||||
{
|
||||
unsigned char buf[2];
|
||||
int len = MQTTSNSerialize_willmsgreq(buf, 2);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_willmsgreq(buf, buflen);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setREGISTER(uint16_t topicId, uint16_t msgId, MQTTSNString* topicName)
|
||||
{
|
||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
int len = MQTTSNSerialize_register(buf, MQTTSNGW_MAX_PACKET_SIZE, (unsigned short) topicId, (unsigned short) msgId,
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_register(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
|
||||
topicName);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
@@ -172,7 +190,8 @@ int MQTTSNPacket::setREGISTER(uint16_t topicId, uint16_t msgId, MQTTSNString* to
|
||||
int MQTTSNPacket::setREGACK(uint16_t topicId, uint16_t msgId, uint8_t returnCode)
|
||||
{
|
||||
unsigned char buf[7];
|
||||
int len = MQTTSNSerialize_regack(buf, 7, (unsigned short) topicId, (unsigned short) msgId,
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_regack(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
|
||||
(unsigned char) returnCode);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
@@ -181,7 +200,8 @@ int MQTTSNPacket::setPUBLISH(uint8_t dup, int qos, uint8_t retained, uint16_t ms
|
||||
uint8_t* payload, uint16_t payloadlen)
|
||||
{
|
||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
int len = MQTTSNSerialize_publish(buf, MQTTSNGW_MAX_PACKET_SIZE, (unsigned char) dup, qos, (unsigned char) retained,
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_publish(buf, buflen, (unsigned char) dup, qos, (unsigned char) retained,
|
||||
(unsigned short) msgId, topic, (unsigned char*) payload, (int) payloadlen);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
@@ -189,7 +209,8 @@ int MQTTSNPacket::setPUBLISH(uint8_t dup, int qos, uint8_t retained, uint16_t ms
|
||||
int MQTTSNPacket::setPUBACK(uint16_t topicId, uint16_t msgId, uint8_t returnCode)
|
||||
{
|
||||
unsigned char buf[7];
|
||||
int len = MQTTSNSerialize_puback(buf, 7, (unsigned short) topicId, (unsigned short) msgId,
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_puback(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
|
||||
(unsigned char) returnCode);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
@@ -197,28 +218,32 @@ int MQTTSNPacket::setPUBACK(uint16_t topicId, uint16_t msgId, uint8_t returnCode
|
||||
int MQTTSNPacket::setPUBREC(uint16_t msgId)
|
||||
{
|
||||
unsigned char buf[4];
|
||||
int len = MQTTSNSerialize_pubrec(buf, 4, (unsigned short) msgId);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_pubrec(buf, buflen, (unsigned short) msgId);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setPUBREL(uint16_t msgId)
|
||||
{
|
||||
unsigned char buf[4];
|
||||
int len = MQTTSNSerialize_pubrel(buf, 4, (unsigned short) msgId);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_pubrel(buf, buflen, (unsigned short) msgId);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setPUBCOMP(uint16_t msgId)
|
||||
{
|
||||
unsigned char buf[4];
|
||||
int len = MQTTSNSerialize_pubcomp(buf, 4, (unsigned short) msgId);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_pubcomp(buf, buflen, (unsigned short) msgId);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setSUBACK(int qos, uint16_t topicId, uint16_t msgId, uint8_t returnCode)
|
||||
{
|
||||
unsigned char buf[8];
|
||||
int len = MQTTSNSerialize_suback(buf, 8, qos, (unsigned short) topicId,
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_suback(buf, buflen, qos, (unsigned short) topicId,
|
||||
(unsigned short) msgId, (unsigned char) returnCode);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
@@ -226,38 +251,59 @@ int MQTTSNPacket::setSUBACK(int qos, uint16_t topicId, uint16_t msgId, uint8_t r
|
||||
int MQTTSNPacket::setUNSUBACK(uint16_t msgId)
|
||||
{
|
||||
unsigned char buf[4];
|
||||
int len = MQTTSNSerialize_unsuback(buf, 4, (unsigned short) msgId);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_unsuback(buf, buflen, (unsigned short) msgId);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setPINGRESP(void)
|
||||
{
|
||||
unsigned char buf[32];
|
||||
int len = MQTTSNSerialize_pingresp(buf, 32);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_pingresp(buf, buflen);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setDISCONNECT(uint16_t duration)
|
||||
{
|
||||
unsigned char buf[4];
|
||||
int len = MQTTSNSerialize_disconnect(buf, 4, (int) duration);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_disconnect(buf, buflen, (int) duration);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setWILLTOPICRESP(uint8_t returnCode)
|
||||
{
|
||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
int len = MQTTSNSerialize_willtopicresp(buf, MQTTSNGW_MAX_PACKET_SIZE, (int) returnCode);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_willtopicresp(buf, buflen, (int) returnCode);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setWILLMSGRESP(uint8_t returnCode)
|
||||
{
|
||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
int len = MQTTSNSerialize_willmsgresp(buf, MQTTSNGW_MAX_PACKET_SIZE, (int) returnCode);
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_willmsgresp(buf, buflen, (int) returnCode);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setCONNECT(MQTTSNPacket_connectData* options)
|
||||
{
|
||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_connect(buf, buflen, options);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::setPINGREQ(MQTTSNString* clientId)
|
||||
{
|
||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
int buflen = sizeof(buf);
|
||||
int len = MQTTSNSerialize_pingreq( buf, buflen, *clientId);
|
||||
return desirialize(buf, len);
|
||||
}
|
||||
|
||||
int MQTTSNPacket::getSERCHGW(uint8_t* radius)
|
||||
{
|
||||
return MQTTSNDeserialize_searchgw((unsigned char*) radius, (unsigned char*) _buf, _bufLen);
|
||||
|
||||
@@ -59,6 +59,9 @@ public:
|
||||
int setWILLTOPICRESP(uint8_t returnCode);
|
||||
int setWILLMSGRESP(uint8_t returnCode);
|
||||
|
||||
int setCONNECT(MQTTSNPacket_connectData* options);
|
||||
int setPINGREQ(MQTTSNString* clientId);
|
||||
|
||||
int getSERCHGW(uint8_t* radius);
|
||||
int getCONNECT(MQTTSNPacket_connectData* option);
|
||||
int getCONNACK(uint8_t* returnCode);
|
||||
@@ -76,6 +79,8 @@ public:
|
||||
int getDISCONNECT(uint16_t* duration);
|
||||
int getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain, MQTTSNString* willTopic);
|
||||
int getWILLMSGUPD(MQTTSNString* willMsg);
|
||||
|
||||
bool isQoSMinusPUBLISH(void);
|
||||
char* getMsgId(char* buf);
|
||||
char* print(char* buf);
|
||||
|
||||
|
||||
@@ -113,6 +113,9 @@ void PacketHandleTask::run()
|
||||
_mqttsnConnection->sendADVERTISE();
|
||||
_advertiseTimer.start(_gateway->getGWParams()->keepAlive * 1000UL);
|
||||
}
|
||||
|
||||
/*------ Check ClientProxy to Connect or send PINGREQ ------*/
|
||||
_gateway->getClientProxy()->checkConnection();
|
||||
}
|
||||
|
||||
/*------ Handle SEARCHGW Message ---------*/
|
||||
|
||||
@@ -43,18 +43,26 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
uint8_t* payload;
|
||||
MQTTSN_topicid topicid;
|
||||
int payloadlen;
|
||||
Publish pub;
|
||||
Publish pub = {0, 0, 0, 0, 0, 0};
|
||||
|
||||
char shortTopic[2];
|
||||
|
||||
if ( !client->isActive() )
|
||||
{
|
||||
/* Reply DISCONNECT to the client */
|
||||
Event* ev = new Event();
|
||||
MQTTSNPacket* disconnect = new MQTTSNPacket();
|
||||
disconnect->setDISCONNECT(0);
|
||||
ev->setClientSendEvent(client, disconnect);
|
||||
_gateway->getClientSendQue()->post(ev);
|
||||
return;
|
||||
if ( client->isProxy() )
|
||||
{
|
||||
client->setProxyPacket(packet);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Reply DISCONNECT to the client */
|
||||
Event* ev = new Event();
|
||||
MQTTSNPacket* disconnect = new MQTTSNPacket();
|
||||
disconnect->setDISCONNECT(0);
|
||||
ev->setClientSendEvent(client, disconnect);
|
||||
_gateway->getClientSendQue()->post(ev);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if ( packet->getPUBLISH(&dup, &qos, &retained, &msgId, &topicid, &payload, &payloadlen) ==0 )
|
||||
@@ -63,7 +71,7 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
}
|
||||
pub.msgId = msgId;
|
||||
pub.header.bits.dup = dup;
|
||||
pub.header.bits.qos = qos;
|
||||
pub.header.bits.qos = ( qos == 3 ? 0 : qos );
|
||||
pub.header.bits.retain = retained;
|
||||
|
||||
Topic* topic = 0;
|
||||
@@ -79,7 +87,13 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
{
|
||||
topic = client->getTopics()->getTopicById(&topicid);
|
||||
|
||||
if( !topic && msgId && qos > 0 )
|
||||
if( !topic && qos == 3 )
|
||||
{
|
||||
WRITELOG("%s Invali TopicId.%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
|
||||
return;
|
||||
}
|
||||
|
||||
if( !topic && msgId && qos > 0 && qos < 3 )
|
||||
{
|
||||
/* Reply PubAck with INVALID_TOPIC_ID to the client */
|
||||
MQTTSNPacket* pubAck = new MQTTSNPacket();
|
||||
@@ -96,7 +110,7 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
}
|
||||
}
|
||||
/* Save a msgId & a TopicId pare for PUBACK */
|
||||
if( msgId && qos > 0 )
|
||||
if( msgId && qos > 0 && qos < 3)
|
||||
{
|
||||
client->setWaitedPubTopicId(msgId, topicid.data.id, topicid.type);
|
||||
}
|
||||
@@ -161,7 +175,7 @@ void MQTTSNPublishHandler::handleRegister(Client* client, MQTTSNPacket* packet)
|
||||
{
|
||||
uint16_t id;
|
||||
uint16_t msgId;
|
||||
MQTTSNString topicName;
|
||||
MQTTSNString topicName = MQTTSNString_initializer;;
|
||||
MQTTSN_topicid topicid;
|
||||
|
||||
if ( client->isActive() || client->isAwake())
|
||||
|
||||
@@ -30,6 +30,7 @@ Gateway::Gateway()
|
||||
{
|
||||
theMultiTaskProcess = this;
|
||||
theProcess = this;
|
||||
_clientProxy = new ClientProxy(this);
|
||||
_params.loginId = 0;
|
||||
_params.password = 0;
|
||||
_params.keepAlive = 0;
|
||||
@@ -48,6 +49,7 @@ Gateway::Gateway()
|
||||
_params.configName = 0;
|
||||
_params.predefinedTopicFileName = 0;
|
||||
_params.forwarderListName = 0;
|
||||
_params.qosMinusClientListName = 0;
|
||||
_packetEventQue.setMaxSize(MAX_INFLIGHTMESSAGES * MAX_CLIENTS);
|
||||
}
|
||||
|
||||
@@ -109,12 +111,21 @@ Gateway::~Gateway()
|
||||
{
|
||||
free(_params.forwarderListName);
|
||||
}
|
||||
if ( _params.qosMinusClientListName )
|
||||
{
|
||||
free(_params.qosMinusClientListName);
|
||||
}
|
||||
if ( _clientProxy )
|
||||
{
|
||||
delete _clientProxy;
|
||||
}
|
||||
}
|
||||
|
||||
void Gateway::initialize(int argc, char** argv)
|
||||
{
|
||||
char param[MQTTSNGW_PARAM_MAX];
|
||||
string fileName;
|
||||
bool secure = false;
|
||||
MultiTaskProcess::initialize(argc, argv);
|
||||
resetRingBuffer();
|
||||
|
||||
@@ -201,6 +212,7 @@ void Gateway::initialize(int argc, char** argv)
|
||||
{
|
||||
if (!strcasecmp(param, "YES"))
|
||||
{
|
||||
secure = true;
|
||||
if (getParam("ClientsList", param) == 0)
|
||||
{
|
||||
fileName = string(param);
|
||||
@@ -218,7 +230,33 @@ void Gateway::initialize(int argc, char** argv)
|
||||
}
|
||||
}
|
||||
|
||||
/* Set ClientProxy's Client */
|
||||
MQTTSNString id = MQTTSNString_initializer;
|
||||
id.cstring = const_cast<char*>(CLIENTPROXY);
|
||||
Client* client = _clientList.createClient(0, &id, true, secure);
|
||||
_clientProxy->setClient(client);
|
||||
client->setPorxy(true);
|
||||
_clientProxy->setGateway(this);
|
||||
|
||||
if (getParam("QoS-1", param) == 0 )
|
||||
{
|
||||
if (!strcasecmp(param, "YES") )
|
||||
{
|
||||
if (getParam("QoS-1ClientsList", param) == 0)
|
||||
{
|
||||
fileName = string(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = *getConfigDirName() + string(QOS_1CLIENT_LIST);
|
||||
}
|
||||
if ( !_clientProxy->setClientProxy(fileName.c_str()) )
|
||||
{
|
||||
throw Exception("Gateway::initialize: No QoS-1ClientsList file defined by the configuration..");
|
||||
}
|
||||
_params.qosMinusClientListName = strdup(fileName.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
if (getParam("PredefinedTopic", param) == 0 )
|
||||
{
|
||||
@@ -238,38 +276,31 @@ void Gateway::initialize(int argc, char** argv)
|
||||
}
|
||||
_params.predefinedTopicFileName = strdup(fileName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
_params.predefinedTopicFileName = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (getParam("Forwarder", param) == 0 )
|
||||
{
|
||||
if (!strcasecmp(param, "YES") )
|
||||
{
|
||||
if (getParam("ForwardersList", param) == 0)
|
||||
{
|
||||
fileName = string(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = *getConfigDirName() + string(FORWARDER_LIST);
|
||||
}
|
||||
if ( !_forwarderList.setFowerder(fileName.c_str()) )
|
||||
{
|
||||
throw Exception("Gateway::initialize: No ForwardersList file defined by the configuration..");
|
||||
}
|
||||
_params.forwarderListName = strdup(fileName.c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
_params.forwarderListName = 0;
|
||||
}
|
||||
}
|
||||
{
|
||||
if (!strcasecmp(param, "YES") )
|
||||
{
|
||||
if (getParam("ForwardersList", param) == 0)
|
||||
{
|
||||
fileName = string(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = *getConfigDirName() + string(FORWARDER_LIST);
|
||||
}
|
||||
if ( !_forwarderList.setFowerder(fileName.c_str()) )
|
||||
{
|
||||
throw Exception("Gateway::initialize: No ForwardersList file defined by the configuration..");
|
||||
}
|
||||
_params.forwarderListName = strdup(fileName.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
fileName = *getConfigDirName() + *getConfigFileName();
|
||||
_params.configName = strdup(fileName.c_str());
|
||||
|
||||
}
|
||||
|
||||
void Gateway::run(void)
|
||||
@@ -295,6 +326,10 @@ void Gateway::run(void)
|
||||
if ( _params.forwarderListName )
|
||||
{
|
||||
WRITELOG(" Forwarders: %s\n", _params.forwarderListName);
|
||||
}
|
||||
if ( _params.qosMinusClientListName )
|
||||
{
|
||||
WRITELOG(" QoS-1: %s\n", _params.qosMinusClientListName);
|
||||
}
|
||||
WRITELOG(" SensorN/W: %s\n", _sensorNetwork.getDescription());
|
||||
WRITELOG(" Broker: %s : %s, %s\n", _params.brokerName, _params.port, _params.portSecure);
|
||||
@@ -302,7 +337,7 @@ void Gateway::run(void)
|
||||
WRITELOG(" RootCApath: %s\n", _params.rootCApath);
|
||||
WRITELOG(" RootCAfile: %s\n", _params.rootCAfile);
|
||||
WRITELOG(" CertKey: %s\n", _params.certKey);
|
||||
WRITELOG(" PrivateKey: %s\n", _params.privateKey);
|
||||
WRITELOG(" PrivateKey: %s\n\n\n", _params.privateKey);
|
||||
|
||||
MultiTaskProcess::run();
|
||||
|
||||
@@ -364,6 +399,11 @@ GatewayParams* Gateway::getGWParams(void)
|
||||
return &_params;
|
||||
}
|
||||
|
||||
ClientProxy* Gateway::getClientProxy(void)
|
||||
{
|
||||
return _clientProxy;
|
||||
}
|
||||
|
||||
/*=====================================
|
||||
Class EventQue
|
||||
=====================================*/
|
||||
|
||||
@@ -17,10 +17,12 @@
|
||||
#define MQTTSNGATEWAY_H_
|
||||
|
||||
#include "MQTTSNGWClient.h"
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
#include "MQTTSNGWProcess.h"
|
||||
#include "MQTTSNPacket.h"
|
||||
|
||||
#include "MQTTSNGWForwarder.h"
|
||||
#include "MQTTSNGWClientProxy.h"
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
/*=================================
|
||||
@@ -43,6 +45,7 @@ namespace MQTTSNGW
|
||||
#define CLIENT "Client"
|
||||
#define CLIENTS "Clients"
|
||||
#define UNKNOWNCL "Unknown Client !"
|
||||
#define CLIENTPROXY "ClientProxy"
|
||||
|
||||
#define LEFTARROW "<---"
|
||||
#define RIGHTARROW "--->"
|
||||
@@ -156,11 +159,14 @@ typedef struct
|
||||
char* privateKey;
|
||||
char* predefinedTopicFileName;
|
||||
char* forwarderListName;
|
||||
char* qosMinusClientListName;
|
||||
}GatewayParams;
|
||||
|
||||
/*=====================================
|
||||
Class Gateway
|
||||
=====================================*/
|
||||
class ClientProxy;
|
||||
|
||||
class Gateway: public MultiTaskProcess{
|
||||
public:
|
||||
Gateway();
|
||||
@@ -176,9 +182,11 @@ public:
|
||||
SensorNetwork* getSensorNetwork(void);
|
||||
LightIndicator* getLightIndicator(void);
|
||||
GatewayParams* getGWParams(void);
|
||||
ClientProxy* getClientProxy(void);
|
||||
|
||||
private:
|
||||
ClientList _clientList;
|
||||
ClientProxy* _clientProxy;
|
||||
ForwarderList _forwarderList;
|
||||
EventQue _packetEventQue;
|
||||
EventQue _brokerSendQue;
|
||||
|
||||
Reference in New Issue
Block a user