Change TAB to 4spaces

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2021-02-16 15:51:54 +09:00
parent d05bf8eaf4
commit 69b229daae
60 changed files with 5973 additions and 5726 deletions

View File

@@ -30,7 +30,8 @@ MQTTGWConnectionHandler::~MQTTGWConnectionHandler()
}
void MQTTGWConnectionHandler::handleConnack(Client* client, MQTTGWPacket* packet)
void MQTTGWConnectionHandler::handleConnack(Client* client,
MQTTGWPacket* packet)
{
uint8_t rc = MQTT_SERVER_UNAVAILABLE;
Connack resp;
@@ -44,29 +45,35 @@ void MQTTGWConnectionHandler::handleConnack(Client* client, MQTTGWPacket* packet
else if (resp.rc == MQTT_UNACCEPTABLE_PROTOCOL_VERSION)
{
rc = MQTTSN_RC_NOT_SUPPORTED;
WRITELOG(" ClientID : %s Requested Protocol version is not supported.\n", client->getClientId());
WRITELOG(
" ClientID : %s Requested Protocol version is not supported.\n",
client->getClientId());
}
else if (resp.rc == MQTT_IDENTIFIER_REJECTED)
{
rc = MQTTSN_RC_NOT_SUPPORTED;
WRITELOG(" ClientID : %s ClientID is collect UTF-8 but not allowed by the Server.\n",
WRITELOG(
" ClientID : %s ClientID is collect UTF-8 but not allowed by the Server.\n",
client->getClientId());
}
else if (resp.rc == MQTT_SERVER_UNAVAILABLE)
{
rc = MQTTSN_RC_REJECTED_CONGESTED;
WRITELOG(" ClientID : %s The Network Connection has been made but the MQTT service is unavailable.\n",
WRITELOG(
" ClientID : %s The Network Connection has been made but the MQTT service is unavailable.\n",
client->getClientId());
}
else if (resp.rc == MQTT_BAD_USERNAME_OR_PASSWORD)
{
rc = MQTTSN_RC_NOT_SUPPORTED;
WRITELOG(" Gateway Configuration Error: The data in the user name or password is malformed.\n");
WRITELOG(
" Gateway Configuration Error: The data in the user name or password is malformed.\n");
}
else if (resp.rc == MQTT_NOT_AUTHORIZED)
{
rc = MQTTSN_RC_NOT_SUPPORTED;
WRITELOG(" Gateway Configuration Error: The Client is not authorized to connect.\n");
WRITELOG(
" Gateway Configuration Error: The Client is not authorized to connect.\n");
}
MQTTSNPacket* snPacket = new MQTTSNPacket();
@@ -78,7 +85,8 @@ void MQTTGWConnectionHandler::handleConnack(Client* client, MQTTGWPacket* packet
_gateway->getClientSendQue()->post(ev1);
}
void MQTTGWConnectionHandler::handlePingresp(Client* client, MQTTGWPacket* packet)
void MQTTGWConnectionHandler::handlePingresp(Client* client,
MQTTGWPacket* packet)
{
MQTTSNPacket* snPacket = new MQTTSNPacket();
snPacket->setPINGRESP();
@@ -88,7 +96,8 @@ void MQTTGWConnectionHandler::handlePingresp(Client* client, MQTTGWPacket* packe
_gateway->getClientSendQue()->post(ev1);
}
void MQTTGWConnectionHandler::handleDisconnect(Client* client, MQTTGWPacket* packet)
void MQTTGWConnectionHandler::handleDisconnect(Client* client,
MQTTGWPacket* packet)
{
MQTTSNPacket* snPacket = new MQTTSNPacket();
snPacket->setDISCONNECT(0);

View File

@@ -29,8 +29,9 @@ void writeInt(unsigned char** pptr, int msgId);
* List of the predefined MQTT v3 packet names.
*/
static const char* mqtt_packet_names[] =
{ "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL", "PUBCOMP", "SUBSCRIBE", "SUBACK",
"UNSUBSCRIBE", "UNSUBACK", "PINGREQ", "PINGRESP", "DISCONNECT" };
{ "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", "PINGREQ",
"PINGRESP", "DISCONNECT" };
/**
* Encodes the message length according to the MQTT algorithm
@@ -87,8 +88,8 @@ char* readUTFlen(char** pptr, char* enddata, int* len)
*len = readInt(pptr);
if (&(*pptr)[*len] <= enddata)
{
string = (char*)calloc(*len + 1, 1);
memcpy(string, *pptr, (size_t)*len);
string = (char*) calloc(*len + 1, 1);
memcpy(string, *pptr, (size_t) *len);
string[*len] = '\0';
*pptr += *len;
}
@@ -142,9 +143,9 @@ void writeChar(unsigned char** pptr, char c)
*/
void writeInt(unsigned char** pptr, int anInt)
{
**pptr = (unsigned char)(anInt / 256);
**pptr = (unsigned char) (anInt / 256);
(*pptr)++;
**pptr = (unsigned char)(anInt % 256);
**pptr = (unsigned char) (anInt % 256);
(*pptr)++;
}
@@ -155,9 +156,9 @@ void writeInt(unsigned char** pptr, int anInt)
*/
void writeUTF(unsigned char** pptr, const char* string)
{
int len = (int)strlen(string);
int len = (int) strlen(string);
writeInt(pptr, len);
memcpy(*pptr, string, (size_t)len);
memcpy(*pptr, string, (size_t) len);
*pptr += len;
}
@@ -187,8 +188,8 @@ int MQTTGWPacket::recv(Network* network)
unsigned char c;
/* read First Byte of Packet */
int rc = network->recv((unsigned char*)&_header.byte, 1);
if ( rc <= 0)
int rc = network->recv((unsigned char*) &_header.byte, 1);
if (rc <= 0)
{
return rc;
}
@@ -207,11 +208,11 @@ int MQTTGWPacket::recv(Network* network)
multiplier *= 128;
} while ((c & 128) != 0);
if ( _remainingLength > 0 )
if (_remainingLength > 0)
{
/* allocate buffer */
_data = (unsigned char*)calloc(_remainingLength, 1);
if ( !_data )
_data = (unsigned char*) calloc(_remainingLength, 1);
if (!_data)
{
return -3;
}
@@ -219,11 +220,11 @@ int MQTTGWPacket::recv(Network* network)
/* read Payload */
int remlen = network->recv(_data, _remainingLength);
if (remlen == -1 )
if (remlen == -1)
{
return -1;
}
else if ( remlen != _remainingLength )
else if (remlen != _remainingLength)
{
return -2;
}
@@ -242,8 +243,9 @@ int MQTTGWPacket::send(Network* network)
int MQTTGWPacket::getAck(Ack* ack)
{
if (PUBACK != _header.bits.type && PUBREC != _header.bits.type && PUBREL != _header.bits.type
&& PUBCOMP != _header.bits.type && UNSUBACK != _header.bits.type)
if (PUBACK != _header.bits.type && PUBREC != _header.bits.type
&& PUBREL != _header.bits.type && PUBCOMP != _header.bits.type
&& UNSUBACK != _header.bits.type)
{
return 0;
}
@@ -303,26 +305,29 @@ int MQTTGWPacket::getPUBLISH(Publish* pub)
return 1;
}
int MQTTGWPacket::setCONNECT(Connect* connect, unsigned char* username, unsigned char* password)
int MQTTGWPacket::setCONNECT(Connect* connect, unsigned char* username,
unsigned char* password)
{
clearData();
_header = connect->header;
_remainingLength = ((connect->version == 3) ? 12 : 10) + (int)strlen(connect->clientID) + 2;
_remainingLength = ((connect->version == 3) ? 12 : 10)
+ (int) strlen(connect->clientID) + 2;
if (connect->flags.bits.will)
{
_remainingLength += (int)strlen(connect->willTopic) + 2 + (int)strlen(connect->willMsg) + 2;
_remainingLength += (int) strlen(connect->willTopic) + 2
+ (int) strlen(connect->willMsg) + 2;
}
if ( connect->flags.bits.username )
if (connect->flags.bits.username)
{
_remainingLength += (int)strlen((char*) username) + 2;
_remainingLength += (int) strlen((char*) username) + 2;
}
if (connect->flags.bits.password)
{
_remainingLength += (int)strlen((char*) password) + 2;
_remainingLength += (int) strlen((char*) password) + 2;
}
_data = (unsigned char*)calloc(_remainingLength, 1);
_data = (unsigned char*) calloc(_remainingLength, 1);
unsigned char* ptr = _data;
if (connect->version == 3)
@@ -360,14 +365,15 @@ int MQTTGWPacket::setCONNECT(Connect* connect, unsigned char* username, unsigned
return 1;
}
int MQTTGWPacket::setSUBSCRIBE(const char* topic, unsigned char qos, unsigned short msgId)
int MQTTGWPacket::setSUBSCRIBE(const char* topic, unsigned char qos,
unsigned short msgId)
{
clearData();
_header.byte = 0;
_header.bits.type = SUBSCRIBE;
_header.bits.qos = 1; // Reserved
_remainingLength = (int)strlen(topic) + 5;
_data = (unsigned char*)calloc(_remainingLength, 1);
_remainingLength = (int) strlen(topic) + 5;
_data = (unsigned char*) calloc(_remainingLength, 1);
if (_data)
{
unsigned char* ptr = _data;
@@ -386,8 +392,8 @@ int MQTTGWPacket::setUNSUBSCRIBE(const char* topic, unsigned short msgid)
_header.byte = 0;
_header.bits.type = UNSUBSCRIBE;
_header.bits.qos = 1;
_remainingLength = (int)strlen(topic) + 4;
_data = (unsigned char*)calloc(_remainingLength, 1);
_remainingLength = (int) strlen(topic) + 4;
_data = (unsigned char*) calloc(_remainingLength, 1);
if (_data)
{
unsigned char* ptr = _data;
@@ -406,14 +412,14 @@ int MQTTGWPacket::setPUBLISH(Publish* pub)
_header.byte = pub->header.byte;
_header.bits.type = PUBLISH;
_remainingLength = 4 + pub->topiclen + pub->payloadlen;
_data = (unsigned char*)calloc(_remainingLength, 1);
_data = (unsigned char*) calloc(_remainingLength, 1);
if (_data)
{
unsigned char* ptr = _data;
writeInt(&ptr, pub->topiclen);
memcpy(ptr, pub->topic, pub->topiclen);
ptr += pub->topiclen;
if ( _header.bits.qos > 0 )
if (_header.bits.qos > 0)
{
writeInt(&ptr, pub->msgId);
}
@@ -438,7 +444,7 @@ int MQTTGWPacket::setAck(unsigned char msgType, unsigned short msgid)
_header.bits.type = msgType;
_header.bits.qos = (msgType == PUBREL) ? 1 : 0;
_data = (unsigned char*)calloc(_remainingLength, 1);
_data = (unsigned char*) calloc(_remainingLength, 1);
if (_data)
{
unsigned char* data = _data;
@@ -473,7 +479,7 @@ int MQTTGWPacket::getPacketData(unsigned char* buf)
{
unsigned char* ptr = buf;
*ptr++ = _header.byte;
int len = MQTTPacket_encode((char*)ptr, _remainingLength);
int len = MQTTPacket_encode((char*) ptr, _remainingLength);
ptr += len;
memcpy(ptr, _data, _remainingLength);
return 1 + len + _remainingLength;
@@ -499,13 +505,13 @@ char* MQTTGWPacket::getMsgId(char* pbuf)
{
int type = getType();
switch ( type )
switch (type)
{
case PUBLISH:
Publish pub;
pub.msgId = 0;
getPUBLISH(&pub);
if ( _header.bits.dup )
if (_header.bits.dup)
{
sprintf(pbuf, "+%04X", pub.msgId);
}
@@ -528,7 +534,7 @@ char* MQTTGWPacket::getMsgId(char* pbuf)
sprintf(pbuf, " ");
break;
}
if ( strcmp(pbuf, " 0000") == 0 )
if (strcmp(pbuf, " 0000") == 0)
{
sprintf(pbuf, " ");
}
@@ -540,7 +546,7 @@ int MQTTGWPacket::getMsgId(void)
int type = getType();
int msgId = 0;
switch ( type )
switch (type)
{
case PUBLISH:
Publish pub;
@@ -556,7 +562,7 @@ int MQTTGWPacket::getMsgId(void)
case UNSUBSCRIBE:
case SUBACK:
case UNSUBACK:
msgId = 256 * (unsigned char)_data[0] + (unsigned char)_data[1];
msgId = 256 * (unsigned char) _data[0] + (unsigned char) _data[1];
break;
default:
break;
@@ -569,7 +575,7 @@ void MQTTGWPacket::setMsgId(int msgId)
int type = getType();
unsigned char* ptr = 0;
switch ( type )
switch (type)
{
case PUBLISH:
Publish pub;
@@ -579,8 +585,8 @@ void MQTTGWPacket::setMsgId(int msgId)
pub.msgId = msgId;
ptr = _data + pub.topiclen;
writeInt(&ptr, pub.msgId);
*ptr++ = (unsigned char)(msgId / 256);
*ptr = (unsigned char)(msgId % 256);
*ptr++ = (unsigned char) (msgId / 256);
*ptr = (unsigned char) (msgId % 256);
break;
case SUBSCRIBE:
case UNSUBSCRIBE:
@@ -591,8 +597,8 @@ void MQTTGWPacket::setMsgId(int msgId)
case SUBACK:
case UNSUBACK:
ptr = _data;
*ptr++ = (unsigned char)(msgId / 256);
*ptr = (unsigned char)(msgId % 256);
*ptr++ = (unsigned char) (msgId / 256);
*ptr = (unsigned char) (msgId % 256);
break;
default:
break;
@@ -620,7 +626,7 @@ MQTTGWPacket& MQTTGWPacket::operator =(MQTTGWPacket& packet)
clearData();
this->_header.byte = packet._header.byte;
this->_remainingLength = packet._remainingLength;
_data = (unsigned char*)calloc(_remainingLength, 1);
_data = (unsigned char*) calloc(_remainingLength, 1);
if (_data)
{
memcpy(this->_data, packet._data, _remainingLength);
@@ -634,12 +640,13 @@ MQTTGWPacket& MQTTGWPacket::operator =(MQTTGWPacket& packet)
UTF8String MQTTGWPacket::getTopic(void)
{
UTF8String str = {0, nullptr};
if ( _header.bits.type == SUBSCRIBE || _header.bits.type == UNSUBSCRIBE )
UTF8String str =
{ 0, nullptr };
if (_header.bits.type == SUBSCRIBE || _header.bits.type == UNSUBSCRIBE)
{
char* ptr = (char*)(_data + 2);
char* ptr = (char*) (_data + 2);
str.len = readInt(&ptr);
str.data = (char*)(_data + 4);
str.data = (char*) (_data + 4);
}
return str;
}

View File

@@ -31,18 +31,29 @@ typedef void* (*pf)(unsigned char, char*, size_t);
enum msgTypes
{
CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
PINGREQ, PINGRESP, DISCONNECT
CONNECT = 1,
CONNACK,
PUBLISH,
PUBACK,
PUBREC,
PUBREL,
PUBCOMP,
SUBSCRIBE,
SUBACK,
UNSUBSCRIBE,
UNSUBACK,
PINGREQ,
PINGRESP,
DISCONNECT
};
/**
* Bitfields for the MQTT header byte.
*/
typedef union
{
/*unsigned*/ char byte; /**< the whole byte */
/*unsigned*/
char byte; /**< the whole byte */
#if defined(REVERSED)
struct
{
@@ -50,25 +61,25 @@ typedef union
bool dup : 1; /**< DUP flag bit */
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
bool retain : 1; /**< retained flag bit */
} bits;
}bits;
#else
struct
{
bool retain : 1; /**< retained flag bit */
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
bool dup : 1; /**< DUP flag bit */
unsigned int type : 4; /**< message type nibble */
bool retain :1; /**< retained flag bit */
unsigned int qos :2; /**< QoS value, 0, 1 or 2 */
bool dup :1; /**< DUP flag bit */
unsigned int type :4; /**< message type nibble */
} bits;
#endif
} Header;
/**
* Data for a connect packet.
*/
enum MQTT_connackCodes{
MQTT_CONNECTION_ACCEPTED ,
enum MQTT_connackCodes
{
MQTT_CONNECTION_ACCEPTED,
MQTT_UNACCEPTABLE_PROTOCOL_VERSION,
MQTT_IDENTIFIER_REJECTED,
MQTT_SERVER_UNAVAILABLE,
@@ -92,17 +103,17 @@ typedef struct
bool will : 1; /**< will flag */
bool cleanstart : 1; /**< cleansession flag */
int : 1; /**< unused */
} bits;
}bits;
#else
struct
{
int : 1; /**< unused */
bool cleanstart : 1; /**< cleansession flag */
bool will : 1; /**< will flag */
unsigned int willQoS : 2; /**< will QoS value */
bool willRetain : 1; /**< will retain setting */
bool password : 1; /**< 3.1 password */
bool username : 1; /**< 3.1 user name */
int :1; /**< unused */
bool cleanstart :1; /**< cleansession flag */
bool will :1; /**< will flag */
unsigned int willQoS :2; /**< will QoS value */
bool willRetain :1; /**< will retain setting */
bool password :1; /**< 3.1 password */
bool username :1; /**< 3.1 user name */
} bits;
#endif
} flags; /**< connect flags byte */
@@ -121,8 +132,6 @@ typedef struct
#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}} }
/**
* Data for a willMessage.
*/
@@ -132,7 +141,7 @@ typedef struct
char* msg;
int retained;
int qos;
}willMessages;
} willMessages;
/**
* Data for a connack packet.
@@ -148,19 +157,18 @@ typedef struct
{
unsigned int reserved : 7; /**< message type nibble */
bool sessionPresent : 1; /**< was a session found on the server? */
} bits;
}bits;
#else
struct
{
bool sessionPresent : 1; /**< was a session found on the server? */
unsigned int reserved : 7; /**< message type nibble */
bool sessionPresent :1; /**< was a session found on the server? */
unsigned int reserved :7; /**< message type nibble */
} bits;
#endif
} flags; /**< connack flags byte */
char rc; /**< connack return code */
} Connack;
/**
* Data for a publish packet.
*/
@@ -214,11 +222,13 @@ public:
int getSUBACK(unsigned short* msgId, unsigned char* rc);
int getPUBLISH(Publish* pub);
int setCONNECT(Connect* conect, unsigned char* username, unsigned char* password);
int setCONNECT(Connect* conect, unsigned char* username,
unsigned char* password);
int setPUBLISH(Publish* pub);
int setAck(unsigned char msgType, unsigned short msgid);
int setHeader(unsigned char msgType);
int setSUBSCRIBE(const char* topic, unsigned char qos, unsigned short msgId);
int setSUBSCRIBE(const char* topic, unsigned char qos,
unsigned short msgId);
int setUNSUBSCRIBE(const char* topics, unsigned short msgid);
UTF8String getTopic(void);

View File

@@ -37,14 +37,15 @@ MQTTGWPublishHandler::~MQTTGWPublishHandler()
void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
{
if ( !client->isActive() && !client->isSleep() && !client->isAwake())
if (!client->isActive() && !client->isSleep() && !client->isAwake())
{
WRITELOG("%s The client is neither active nor sleep %s%s\n", ERRMSG_HEADER, client->getStatus(), ERRMSG_FOOTER);
WRITELOG("%s The client is neither active nor sleep %s%s\n",
ERRMSG_HEADER, client->getStatus(), ERRMSG_FOOTER);
return;
}
/* client is sleeping. save PUBLISH */
if ( client->isSleep() )
if (client->isSleep())
{
Publish pub;
packet->getPUBLISH(&pub);
@@ -56,16 +57,18 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
{
replyACK(client, &pub, PUBACK);
}
else if ( pub.header.bits.qos == 2)
else if (pub.header.bits.qos == 2)
{
replyACK(client, &pub, PUBREC);
}
MQTTGWPacket* msg = new MQTTGWPacket();
*msg = *packet;
if ( msg->getType() == 0 )
if (msg->getType() == 0)
{
WRITELOG("%s MQTTGWPublishHandler::handlePublish can't allocate memories for Packet.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER);
WRITELOG(
"%s MQTTGWPublishHandler::handlePublish can't allocate memories for Packet.%s\n",
ERRMSG_HEADER, ERRMSG_FOOTER);
delete msg;
return;
}
@@ -94,7 +97,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
topicId.data.long_.name = pub.topic;
Topic* tp = client->getTopics()->getTopicByName(&topicId);
if ( tp )
if (tp)
{
topicId.type = tp->getType();
topicId.data.long_.len = pub.topiclen;
@@ -113,7 +116,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
{
replyACK(client, &pub, PUBACK);
}
else if ( pub.header.bits.qos == 2 )
else if (pub.header.bits.qos == 2)
{
replyACK(client, &pub, PUBREC);
}
@@ -145,23 +148,28 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
/* send PUBLISH */
topicId.data.id = id;
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos,
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload,
pub.payloadlen);
client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId);
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup,
(int) pub.header.bits.qos,
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId,
topicId, (uint8_t*) pub.payload, pub.payloadlen);
client->getWaitREGACKPacketList()->setPacket(snPacket,
regackMsgId);
return;
}
else
{
WRITELOG("%sMQTTGWPublishHandler Can't create a Topic.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER);
WRITELOG("%sMQTTGWPublishHandler Can't create a Topic.%s\n",
ERRMSG_HEADER, ERRMSG_FOOTER);
delete snPacket;
return;
}
}
}
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos, (uint8_t) pub.header.bits.retain,
(uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload, pub.payloadlen);
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup,
(int) pub.header.bits.qos, (uint8_t) pub.header.bits.retain,
(uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload,
pub.payloadlen);
Event* ev1 = new Event();
ev1->setClientSendEvent(client, snPacket);
_gateway->getClientSendQue()->post(ev1);
@@ -171,7 +179,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
void MQTTGWPublishHandler::replyACK(Client* client, Publish* pub, int type)
{
MQTTGWPacket* pubAck = new MQTTGWPacket();
pubAck->setAck(type, (uint16_t)pub->msgId);
pubAck->setAck(type, (uint16_t) pub->msgId);
Event* ev1 = new Event();
ev1->setBrokerSendEvent(client, pubAck);
_gateway->getBrokerSendQue()->post(ev1);
@@ -181,27 +189,31 @@ void MQTTGWPublishHandler::handlePuback(Client* client, MQTTGWPacket* packet)
{
Ack ack;
packet->getAck(&ack);
TopicIdMapElement* topicId = client->getWaitedPubTopicId((uint16_t)ack.msgId);
TopicIdMapElement* topicId = client->getWaitedPubTopicId(
(uint16_t) ack.msgId);
if (topicId)
{
MQTTSNPacket* mqttsnPacket = new MQTTSNPacket();
mqttsnPacket->setPUBACK(topicId->getTopicId(), (uint16_t)ack.msgId, 0);
mqttsnPacket->setPUBACK(topicId->getTopicId(), (uint16_t) ack.msgId, 0);
client->eraseWaitedPubTopicId((uint16_t)ack.msgId);
client->eraseWaitedPubTopicId((uint16_t) ack.msgId);
Event* ev1 = new Event();
ev1->setClientSendEvent(client, mqttsnPacket);
_gateway->getClientSendQue()->post(ev1);
return;
}
WRITELOG(" PUBACK from the Broker is invalid. PacketID : %04X ClientID : %s \n", (uint16_t)ack.msgId, client->getClientId());
WRITELOG(
" PUBACK from the Broker is invalid. PacketID : %04X ClientID : %s \n",
(uint16_t) ack.msgId, client->getClientId());
}
void MQTTGWPublishHandler::handleAck(Client* client, MQTTGWPacket* packet, int type)
void MQTTGWPublishHandler::handleAck(Client* client, MQTTGWPacket* packet,
int type)
{
Ack ack;
packet->getAck(&ack);
if ( client->isActive() || client->isAwake() )
if (client->isActive() || client->isAwake())
{
MQTTSNPacket* mqttsnPacket = new MQTTSNPacket();
if (type == PUBREC)
@@ -221,12 +233,12 @@ void MQTTGWPublishHandler::handleAck(Client* client, MQTTGWPacket* packet, int t
ev1->setClientSendEvent(client, mqttsnPacket);
_gateway->getClientSendQue()->post(ev1);
}
else if ( client->isSleep() )
else if (client->isSleep())
{
if (type == PUBREL)
{
MQTTGWPacket* pubComp = new MQTTGWPacket();
pubComp->setAck(PUBCOMP, (uint16_t)ack.msgId);
pubComp->setAck(PUBCOMP, (uint16_t) ack.msgId);
Event* ev1 = new Event();
ev1->setBrokerSendEvent(client, pubComp);
_gateway->getBrokerSendQue()->post(ev1);
@@ -234,60 +246,67 @@ void MQTTGWPublishHandler::handleAck(Client* client, MQTTGWPacket* packet, int t
}
}
void MQTTGWPublishHandler::handleAggregatePuback(Client* client, MQTTGWPacket* packet)
void MQTTGWPublishHandler::handleAggregatePuback(Client* client,
MQTTGWPacket* packet)
{
uint16_t msgId = packet->getMsgId();
uint16_t clientMsgId = 0;
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId, &clientMsgId);
if ( newClient != nullptr )
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId,
&clientMsgId);
if (newClient != nullptr)
{
packet->setMsgId((int)clientMsgId);
packet->setMsgId((int) clientMsgId);
handlePuback(newClient, packet);
}
}
void MQTTGWPublishHandler::handleAggregateAck(Client* client, MQTTGWPacket* packet, int type)
void MQTTGWPublishHandler::handleAggregateAck(Client* client,
MQTTGWPacket* packet, int type)
{
uint16_t msgId = packet->getMsgId();
uint16_t clientMsgId = 0;
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId, &clientMsgId);
if ( newClient != nullptr )
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId,
&clientMsgId);
if (newClient != nullptr)
{
packet->setMsgId((int)clientMsgId);
handleAck(newClient, packet,type);
packet->setMsgId((int) clientMsgId);
handleAck(newClient, packet, type);
}
}
void MQTTGWPublishHandler::handleAggregatePubrel(Client* client, MQTTGWPacket* packet)
void MQTTGWPublishHandler::handleAggregatePubrel(Client* client,
MQTTGWPacket* packet)
{
Publish pub;
packet->getPUBLISH(&pub);
replyACK(client, &pub, PUBCOMP);
}
void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* packet)
void MQTTGWPublishHandler::handleAggregatePublish(Client* client,
MQTTGWPacket* packet)
{
Publish pub;
packet->getPUBLISH(&pub);
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 refactor
ClientTopicElement* elm = _gateway->getAdapterManager()->getAggregater()->getClientElement(&topic);
ClientTopicElement* elm =
_gateway->getAdapterManager()->getAggregater()->getClientElement(
&topic);
while ( elm != nullptr )
while (elm != nullptr)
{
Client* devClient = elm->getClient();
MQTTGWPacket* msg = new MQTTGWPacket();
*msg = *packet;
if ( msg->getType() == 0 )
if (msg->getType() == 0)
{
WRITELOG("%s MQTTGWPublishHandler::handleAggregatePublish can't allocate memories for Packet.%s\n", ERRMSG_HEADER,ERRMSG_FOOTER);
WRITELOG(
"%s MQTTGWPublishHandler::handleAggregatePublish can't allocate memories for Packet.%s\n",
ERRMSG_HEADER, ERRMSG_FOOTER);
delete msg;
break;
}

View File

@@ -45,6 +45,4 @@ private:
}
#endif /* MQTTGWPUBLISHHANDLER_H_ */

View File

@@ -61,7 +61,8 @@ void MQTTGWSubscribeHandler::handleSuback(Client* client, MQTTGWPacket* packet)
}
}
void MQTTGWSubscribeHandler::handleUnsuback(Client* client, MQTTGWPacket* packet)
void MQTTGWSubscribeHandler::handleUnsuback(Client* client,
MQTTGWPacket* packet)
{
Ack ack;
packet->getAck(&ack);
@@ -72,28 +73,33 @@ void MQTTGWSubscribeHandler::handleUnsuback(Client* client, MQTTGWPacket* packet
_gateway->getClientSendQue()->post(evt);
}
void MQTTGWSubscribeHandler::handleAggregateSuback(Client* client, MQTTGWPacket* packet)
void MQTTGWSubscribeHandler::handleAggregateSuback(Client* client,
MQTTGWPacket* packet)
{
uint16_t msgId = packet->getMsgId();
uint16_t clientMsgId = 0;
Client* newClient = _gateway->getAdapterManager()->getAggregater()->convertClient(msgId, &clientMsgId);
if ( newClient != nullptr )
Client* newClient =
_gateway->getAdapterManager()->getAggregater()->convertClient(msgId,
&clientMsgId);
if (newClient != nullptr)
{
packet->setMsgId((int)clientMsgId);
packet->setMsgId((int) clientMsgId);
handleSuback(newClient, packet);
}
}
void MQTTGWSubscribeHandler::handleAggregateUnsuback(Client* client, MQTTGWPacket* packet)
void MQTTGWSubscribeHandler::handleAggregateUnsuback(Client* client,
MQTTGWPacket* packet)
{
uint16_t msgId = packet->getMsgId();
uint16_t clientMsgId = 0;
Client* newClient = _gateway->getAdapterManager()->getAggregater()->convertClient(msgId, &clientMsgId);
if ( newClient != nullptr )
Client* newClient =
_gateway->getAdapterManager()->getAggregater()->convertClient(msgId,
&clientMsgId);
if (newClient != nullptr)
{
packet->setMsgId((int)clientMsgId);
packet->setMsgId((int) clientMsgId);
handleUnsuback(newClient, packet);
}
}

View File

@@ -26,7 +26,8 @@ using namespace MQTTSNGW;
/*=====================================
Class MQTTSNAggregateConnectionHandler
=====================================*/
MQTTSNAggregateConnectionHandler::MQTTSNAggregateConnectionHandler(Gateway* gateway)
MQTTSNAggregateConnectionHandler::MQTTSNAggregateConnectionHandler(
Gateway* gateway)
{
_gateway = gateway;
}
@@ -36,20 +37,20 @@ MQTTSNAggregateConnectionHandler::~MQTTSNAggregateConnectionHandler()
}
/*
* CONNECT
*/
void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet)
void MQTTSNAggregateConnectionHandler::handleConnect(Client* client,
MQTTSNPacket* packet)
{
MQTTSNPacket_connectData data;
if ( packet->getCONNECT(&data) == 0 )
if (packet->getCONNECT(&data) == 0)
{
return;
}
/* return CONNACK when the client is sleeping */
if ( client->isSleep() || client->isAwake() )
if (client->isSleep() || client->isAwake())
{
MQTTSNPacket* packet = new MQTTSNPacket();
packet->setCONNACK(MQTTSN_RC_ACCEPTED);
@@ -70,7 +71,6 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke
/* CONNECT was not sent yet. prepare Connect data */
client->setSessionStatus(false);
if (data.cleansession)
{
@@ -82,11 +82,12 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke
if (topics)
{
Topic* tp = topics->getFirstTopic();
while( tp != nullptr )
while (tp != nullptr)
{
if ( tp->getType() == MQTTSN_TOPIC_TYPE_NORMAL )
if (tp->getType() == MQTTSN_TOPIC_TYPE_NORMAL)
{
_gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(tp, client);
_gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(
tp, client);
}
tp = topics->getNextTopic(tp);
}
@@ -120,13 +121,13 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke
}
}
/*
* WILLMSG
*/
void MQTTSNAggregateConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet)
void MQTTSNAggregateConnectionHandler::handleWillmsg(Client* client,
MQTTSNPacket* packet)
{
if ( !client->isWaitWillMsg() )
if (!client->isWaitWillMsg())
{
DEBUGLOG(" MQTTSNConnectionHandler::handleWillmsg WaitWillMsgFlg is off.\n");
return;
@@ -135,10 +136,10 @@ void MQTTSNAggregateConnectionHandler::handleWillmsg(Client* client, MQTTSNPacke
MQTTSNString willmsg = MQTTSNString_initializer;
//Connect* connectData = client->getConnectData();
if( client->isConnectSendable() )
if (client->isConnectSendable())
{
/* save WillMsg in the client */
if ( packet->getWILLMSG(&willmsg) == 0 )
if (packet->getWILLMSG(&willmsg) == 0)
{
return;
}
@@ -159,7 +160,8 @@ void MQTTSNAggregateConnectionHandler::handleWillmsg(Client* client, MQTTSNPacke
/*
* DISCONNECT
*/
void MQTTSNAggregateConnectionHandler::handleDisconnect(Client* client, MQTTSNPacket* packet)
void MQTTSNAggregateConnectionHandler::handleDisconnect(Client* client,
MQTTSNPacket* packet)
{
MQTTSNPacket* snMsg = new MQTTSNPacket();
snMsg->setDISCONNECT(0);
@@ -171,9 +173,11 @@ void MQTTSNAggregateConnectionHandler::handleDisconnect(Client* client, MQTTSNPa
/*
* PINGREQ
*/
void MQTTSNAggregateConnectionHandler::handlePingreq(Client* client, MQTTSNPacket* packet)
void MQTTSNAggregateConnectionHandler::handlePingreq(Client* client,
MQTTSNPacket* packet)
{
if ( ( client->isSleep() || client->isAwake() ) && client->getClientSleepPacket() )
if ((client->isSleep() || client->isAwake())
&& client->getClientSleepPacket())
{
sendStoredPublish(client);
client->holdPingRequest();
@@ -195,7 +199,7 @@ void MQTTSNAggregateConnectionHandler::sendStoredPublish(Client* client)
{
MQTTGWPacket* msg = nullptr;
while ( ( msg = client->getClientSleepPacket() ) != nullptr )
while ((msg = client->getClientSleepPacket()) != nullptr)
{
client->deleteFirstClientSleepPacket(); // pop the que to delete element.

View File

@@ -24,11 +24,10 @@
#include <string.h>
using namespace MQTTSNGW;
/*=====================================
Class Adapter
=====================================*/
Adapter:: Adapter(Gateway* gw)
Adapter::Adapter(Gateway* gw)
{
_gateway = gw;
_proxy = new Proxy(gw);
@@ -37,22 +36,21 @@ Adapter:: Adapter(Gateway* gw)
Adapter::~Adapter(void)
{
if ( _proxy )
if (_proxy)
{
delete _proxy;
}
if ( _proxySecure )
if (_proxySecure)
{
delete _proxySecure;
}
}
void Adapter::setup(const char* adpterName, AdapterType adapterType)
{
_isSecure = false;
if ( _gateway->hasSecureConnection() )
if (_gateway->hasSecureConnection())
{
_isSecure = true;
}
@@ -65,24 +63,25 @@ void Adapter::setup(const char* adpterName, AdapterType adapterType)
string nameSecure = string(adpterName) + "-S";
idSecure.cstring = const_cast<char*>(nameSecure.c_str());
Client* client = _gateway->getClientList()->createClient(0, &id, true, false, TRANSPEARENT_TYPE);
Client* client = _gateway->getClientList()->createClient(0, &id, true,
false, TRANSPEARENT_TYPE);
setClient(client, false);
client->setAdapterType(adapterType);
client = _gateway->getClientList()->createClient(0, &idSecure, true, true, TRANSPEARENT_TYPE);
client = _gateway->getClientList()->createClient(0, &idSecure, true, true,
TRANSPEARENT_TYPE);
setClient(client, true);
client->setAdapterType(adapterType);
}
Client* Adapter::getClient(SensorNetAddress* addr)
{
Client* client = _gateway->getClientList()->getClient(addr);
if ( !client )
if (!client)
{
return nullptr;
}
else if ( client->isQoSm1() )
else if (client->isQoSm1())
{
return client;
}
@@ -95,11 +94,11 @@ Client* Adapter::getClient(SensorNetAddress* addr)
const char* Adapter::getClientId(SensorNetAddress* addr)
{
Client* client = getClient(addr);
if ( !client )
if (!client)
{
return nullptr;
}
else if ( client->isQoSm1() )
else if (client->isQoSm1())
{
return client->getClientId();
}
@@ -112,11 +111,11 @@ const char* Adapter::getClientId(SensorNetAddress* addr)
bool Adapter::isSecure(SensorNetAddress* addr)
{
Client* client = getClient(addr);
if ( !client )
if (!client)
{
return false;
}
else if ( client->isSecureNetwork() )
else if (client->isSecureNetwork())
{
return true;
}
@@ -133,7 +132,7 @@ bool Adapter::isSecure(void)
void Adapter::setClient(Client* client, bool secure)
{
if ( secure )
if (secure)
{
_clientSecure = client;
}
@@ -157,7 +156,7 @@ void Adapter::checkConnection(void)
{
_proxy->checkConnection(_client);
if ( _isSecure )
if (_isSecure)
{
_proxySecure->checkConnection(_clientSecure);
}
@@ -166,15 +165,17 @@ void Adapter::checkConnection(void)
void Adapter::send(MQTTSNPacket* packet, Client* client)
{
Proxy* proxy = _proxy;
if ( client->isSecureNetwork() && !_isSecure )
if (client->isSecureNetwork() && !_isSecure)
{
if ( _isSecure )
if (_isSecure)
{
proxy = _proxySecure;
}
else
{
WRITELOG("%s %s No Secure connections %s 's packet is discarded.%s\n", ERRMSG_HEADER, client->getClientId() , ERRMSG_FOOTER);
WRITELOG(
"%s %s No Secure connections %s 's packet is discarded.%s\n",
ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
return;
}
}
@@ -185,7 +186,7 @@ void Adapter::send(MQTTSNPacket* packet, Client* client)
void Adapter::resetPingTimer(bool secure)
{
if ( secure )
if (secure)
{
_proxySecure->resetPingTimer();
}
@@ -202,7 +203,7 @@ bool Adapter::isActive(void)
void Adapter::savePacket(Client* client, MQTTSNPacket* packet)
{
if ( client->isSecureNetwork())
if (client->isSecureNetwork())
{
_proxySecure->savePacket(client, packet);
}
@@ -212,10 +213,9 @@ void Adapter::savePacket(Client* client, MQTTSNPacket* packet)
}
}
Client* Adapter::getAdapterClient(Client* client)
{
if ( client->isSecureNetwork() )
if (client->isSecureNetwork())
{
return _clientSecure;
}
@@ -235,7 +235,7 @@ Proxy::Proxy(Gateway* gw)
}
Proxy::~Proxy(void)
{
if ( _suspendedPacketEventQue )
if (_suspendedPacketEventQue)
{
delete _suspendedPacketEventQue;
}
@@ -243,7 +243,8 @@ Proxy::~Proxy(void)
void Proxy::checkConnection(Client* client)
{
if ( client->isDisconnect() || ( client->isConnecting() && _responseTimer.isTimeup()) )
if (client->isDisconnect()
|| (client->isConnecting() && _responseTimer.isTimeup()))
{
client->connectSended();
_responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL);
@@ -257,7 +258,8 @@ void Proxy::checkConnection(Client* client)
ev->setClientRecvEvent(client, packet);
_gateway->getPacketEventQue()->post(ev);
}
else if ( (client->isActive() && _keepAliveTimer.isTimeup() ) || (_isWaitingResp && _responseTimer.isTimeup() ) )
else if ((client->isActive() && _keepAliveTimer.isTimeup())
|| (_isWaitingResp && _responseTimer.isTimeup()))
{
MQTTSNPacket* packet = new MQTTSNPacket();
MQTTSNString clientId = MQTTSNString_initializer;
@@ -268,7 +270,7 @@ void Proxy::checkConnection(Client* client)
_responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL);
_isWaitingResp = true;
if ( ++_retryCnt > PROXY_MAX_RETRY_CNT )
if (++_retryCnt > PROXY_MAX_RETRY_CNT)
{
client->disconnected();
}
@@ -276,7 +278,6 @@ void Proxy::checkConnection(Client* client)
}
}
void Proxy::resetPingTimer(void)
{
_keepAliveTimer.start(PROXY_KEEPALIVE_DURATION * 1000UL);
@@ -284,9 +285,9 @@ void Proxy::resetPingTimer(void)
void Proxy::recv(MQTTSNPacket* packet, Client* client)
{
if ( packet->getType() == MQTTSN_CONNACK )
if (packet->getType() == MQTTSN_CONNACK)
{
if ( packet->isAccepted() )
if (packet->isAccepted())
{
_responseTimer.stop();
_retryCnt = 0;
@@ -294,14 +295,14 @@ void Proxy::recv(MQTTSNPacket* packet, Client* client)
sendSuspendedPacket();
}
}
else if ( packet->getType() == MQTTSN_PINGRESP )
else if (packet->getType() == MQTTSN_PINGRESP)
{
_isWaitingResp = false;
_responseTimer.stop();
_retryCnt = 0;
resetPingTimer();
}
else if ( packet->getType() == MQTTSN_DISCONNECT )
else if (packet->getType() == MQTTSN_DISCONNECT)
{
// blank
}
@@ -317,7 +318,7 @@ void Proxy::savePacket(Client* client, MQTTSNPacket* packet)
void Proxy::sendSuspendedPacket(void)
{
while ( _suspendedPacketEventQue->size() )
while (_suspendedPacketEventQue->size())
{
Event* ev = _suspendedPacketEventQue->wait();
_gateway->getPacketEventQue()->post(ev);

View File

@@ -31,9 +31,10 @@ class EventQue;
class Timer;
/* When you add a new type, Client::setAdapterType() and Client::isAdapter() functions must be modified. */
typedef enum{
typedef enum
{
Atype_QoSm1Proxy, Atype_Aggregater
}AdapterType;
} AdapterType;
/*=====================================
Class Adapter
@@ -60,16 +61,15 @@ public:
void savePacket(Client* client, MQTTSNPacket* packet);
private:
Gateway* _gateway {nullptr};
Proxy* _proxy {nullptr};
Proxy* _proxySecure {nullptr};
Client* _client {nullptr};
Client* _clientSecure {nullptr};
bool _isActive {false};
bool _isSecure{false};
Gateway* _gateway { nullptr };
Proxy* _proxy { nullptr };
Proxy* _proxySecure { nullptr };
Client* _client { nullptr };
Client* _clientSecure { nullptr };
bool _isActive { false };
bool _isSecure { false };
};
/*=====================================
Class Proxy
=====================================*/
@@ -88,11 +88,12 @@ public:
private:
void sendSuspendedPacket(void);
Gateway* _gateway;
EventQue* _suspendedPacketEventQue {nullptr};
EventQue* _suspendedPacketEventQue
{ nullptr };
Timer _keepAliveTimer;
Timer _responseTimer;
bool _isWaitingResp {false};
int _retryCnt {0};
bool _isWaitingResp { false };
int _retryCnt { 0 };
};
}

View File

@@ -39,20 +39,20 @@ AdapterManager::AdapterManager(Gateway* gw)
_aggregater = new Aggregater(gw);
}
void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder, bool qosM1)
void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder,
bool qosM1)
{
if ( aggregate )
if (aggregate)
{
_aggregater->initialize(gwName);
}
if ( qosM1 )
if (qosM1)
{
_qosm1Proxy->initialize(gwName);
}
if ( forwarder )
if (forwarder)
{
_forwarders->initialize(_gateway);
}
@@ -60,15 +60,15 @@ void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder, bo
AdapterManager::~AdapterManager(void)
{
if ( _forwarders )
if (_forwarders)
{
delete _forwarders;
}
if ( _qosm1Proxy )
if (_qosm1Proxy)
{
delete _qosm1Proxy;
}
if ( _aggregater )
if (_aggregater)
{
delete _aggregater;
}
@@ -91,7 +91,8 @@ Aggregater* AdapterManager::getAggregater(void)
bool AdapterManager::isAggregatedClient(Client* client)
{
if ( !_aggregater->isActive() || client->isQoSm1() || client->isAggregater() || client->isQoSm1Proxy())
if (!_aggregater->isActive() || client->isQoSm1() || client->isAggregater()
|| client->isQoSm1Proxy())
{
return false;
}
@@ -106,56 +107,60 @@ Client* AdapterManager::getClient(Client* client)
bool secure = client->isSecureNetwork();
Client* newClient = client;
if ( client->isQoSm1() )
if (client->isQoSm1())
{
newClient = _qosm1Proxy->getAdapterClient(client);
_qosm1Proxy->resetPingTimer(secure);
}
else if ( client->isAggregated() )
else if (client->isAggregated())
{
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);
}
return newClient;
}
int AdapterManager::unicastToClient(Client* client, MQTTSNPacket* packet, ClientSendTask* task)
int AdapterManager::unicastToClient(Client* client, MQTTSNPacket* packet,
ClientSendTask* task)
{
char pbuf[SIZE_OF_LOG_PACKET * 3];
Forwarder* fwd = client->getForwarder();
int rc = 0;
if ( fwd )
if (fwd)
{
MQTTSNGWEncapsulatedPacket encap(packet);
WirelessNodeId* wnId = fwd->getWirelessNodeId(client);
encap.setWirelessNodeId(wnId);
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());
WRITELOG(FORMAT_Y_W_G, currentDateTime(), encap.getName(), RIGHTARROW,
fwd->getId(), encap.print(pbuf));
rc = encap.unicast(_gateway->getSensorNetwork(),
fwd->getSensorNetAddr());
}
else
{
task->log(client, packet);
if ( client->isQoSm1Proxy() )
if (client->isQoSm1Proxy())
{
_qosm1Proxy->send(packet, client);
}
else if ( client->isAggregater() )
else if (client->isAggregater())
{
_aggregater->send(packet, client);
}
else
{
rc = packet->unicast(_gateway->getSensorNetwork(), client->getSensorNetAddress());
rc = packet->unicast(_gateway->getSensorNetwork(),
client->getSensorNetAddress());
}
}
return rc;
@@ -163,12 +168,12 @@ int AdapterManager::unicastToClient(Client* client, MQTTSNPacket* packet, Client
void AdapterManager::checkConnection(void)
{
if ( _aggregater->isActive())
if (_aggregater->isActive())
{
_aggregater->checkConnection();
}
if ( _qosm1Proxy->isActive())
if (_qosm1Proxy->isActive())
{
_qosm1Proxy->checkConnection();
}
@@ -185,25 +190,25 @@ bool AdapterManager::isAggregaterActive(void)
}
/*
AggregateTopicElement* AdapterManager::findTopic(Topic* topic)
{
AggregateTopicElement* AdapterManager::findTopic(Topic* topic)
{
return _aggregater->findTopic(topic);
}
}
AggregateTopicElement* 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)
{
void AdapterManager::removeAggregateTopic(Topic* topic, Client* client)
{
//_aggregater->removeAggregateTopic(topic, client);
}
}
void AdapterManager::removeAggregateTopicList(Topics* topics, Client* client)
{
void AdapterManager::removeAggregateTopicList(Topics* topics, Client* client)
{
}
*/
}
*/

View File

@@ -49,18 +49,16 @@ public:
bool isAggregatedClient(Client* client);
Client* getClient(Client* client);
Client* convertClient(uint16_t msgId, uint16_t* clientMsgId);
int unicastToClient(Client* client, MQTTSNPacket* packet, ClientSendTask* task);
int unicastToClient(Client* client, MQTTSNPacket* packet,
ClientSendTask* task);
bool isAggregaterActive(void);
private:
Gateway* _gateway {nullptr};
ForwarderList* _forwarders {nullptr};
QoSm1Proxy* _qosm1Proxy {nullptr};
Aggregater* _aggregater {nullptr};
Gateway* _gateway { nullptr };
ForwarderList* _forwarders { nullptr };
QoSm1Proxy* _qosm1Proxy { nullptr };
Aggregater* _aggregater { nullptr };
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWADAPTERMANAGER_H_ */

View File

@@ -51,7 +51,7 @@ AggregateTopicElement::AggregateTopicElement(Topic* topic, Client* client)
{
_topic = topic;
ClientTopicElement* elm = new ClientTopicElement(client);
if ( elm != nullptr )
if (elm != nullptr)
{
_head = elm;
_tail = elm;
@@ -61,10 +61,10 @@ AggregateTopicElement::AggregateTopicElement(Topic* topic, Client* client)
AggregateTopicElement::~AggregateTopicElement(void)
{
_mutex.lock();
if ( _head != nullptr )
if (_head != nullptr)
{
ClientTopicElement* p = _tail;
while ( p )
while (p)
{
ClientTopicElement* pPrev = p;
delete p;
@@ -78,14 +78,14 @@ AggregateTopicElement::~AggregateTopicElement(void)
ClientTopicElement* AggregateTopicElement::add(Client* client)
{
ClientTopicElement* elm = new ClientTopicElement(client);
if ( elm == nullptr )
if (elm == nullptr)
{
return nullptr;
}
_mutex.lock();
if ( _head == nullptr )
if (_head == nullptr)
{
_head = elm;
_tail = elm;
@@ -93,7 +93,7 @@ ClientTopicElement* AggregateTopicElement::add(Client* client)
else
{
ClientTopicElement* p = find(client);
if ( p == nullptr )
if (p == nullptr)
{
p = _tail;
_tail = elm;
@@ -113,9 +113,9 @@ ClientTopicElement* AggregateTopicElement::add(Client* client)
ClientTopicElement* AggregateTopicElement::find(Client* client)
{
ClientTopicElement* p = _head;
while ( p != nullptr )
while (p != nullptr)
{
if ( p->_client == client)
if (p->_client == client)
{
break;
}
@@ -129,7 +129,8 @@ ClientTopicElement* AggregateTopicElement::getFirstClientTopicElement(void)
return _head;
}
ClientTopicElement* AggregateTopicElement::getNextClientTopicElement(ClientTopicElement* elmClient)
ClientTopicElement* AggregateTopicElement::getNextClientTopicElement(
ClientTopicElement* elmClient)
{
return elmClient->_next;
}
@@ -139,12 +140,12 @@ void AggregateTopicElement::eraseClient(Client* client)
_mutex.lock();
ClientTopicElement* p = find(client);
if ( p != nullptr )
if (p != nullptr)
{
if ( p->_prev == nullptr ) // head element
if (p->_prev == nullptr) // head element
{
_head = p->_next;
if ( p->_next == nullptr ) // head & only one
if (p->_next == nullptr) // head & only one
{
_tail = nullptr;
}
@@ -153,7 +154,7 @@ void AggregateTopicElement::eraseClient(Client* client)
p->_next->_prev = nullptr; // head & midle
}
}
else if ( p->_next != nullptr ) // middle
else if (p->_next != nullptr) // middle
{
p->_prev->_next = p->_next;
}
@@ -186,9 +187,9 @@ AggregateTopicElement* AggregateTopicTable::add(Topic* topic, Client* client)
AggregateTopicElement* elm = nullptr;
_mutex.lock();
elm = getAggregateTopicElement(topic);
if ( elm != nullptr )
if (elm != nullptr)
{
if ( elm->find(client) == nullptr )
if (elm->find(client) == nullptr)
{
elm->add(client);
}
@@ -197,7 +198,7 @@ AggregateTopicElement* AggregateTopicTable::add(Topic* topic, Client* client)
{
Topic* newTopic = topic->duplicate();
elm = new AggregateTopicElement(newTopic, client);
if ( _head == nullptr )
if (_head == nullptr)
{
_head = elm;
_tail = elm;
@@ -220,11 +221,11 @@ void AggregateTopicTable::erase(Topic* topic, Client* client)
_mutex.lock();
elm = getAggregateTopicElement(topic);
if ( elm != nullptr )
if (elm != nullptr)
{
elm->eraseClient(client);
}
if ( elm->_head == nullptr )
if (elm->_head == nullptr)
{
erase(elm);
}
@@ -234,12 +235,12 @@ void AggregateTopicTable::erase(Topic* topic, Client* client)
void AggregateTopicTable::erase(AggregateTopicElement* elmTopic)
{
if ( elmTopic != nullptr )
if (elmTopic != nullptr)
{
if ( elmTopic->_prev == nullptr ) // head element
if (elmTopic->_prev == nullptr) // head element
{
_head = elmTopic->_next;
if ( elmTopic->_next == nullptr ) // head & only one
if (elmTopic->_next == nullptr) // head & only one
{
_tail = nullptr;
}
@@ -248,7 +249,7 @@ void AggregateTopicTable::erase(AggregateTopicElement* elmTopic)
elmTopic->_next->_prev = nullptr; // head & midle
}
}
else if ( elmTopic->_next != nullptr ) // middle
else if (elmTopic->_next != nullptr) // middle
{
elmTopic->_prev->_next = elmTopic->_next;
}
@@ -261,13 +262,14 @@ void AggregateTopicTable::erase(AggregateTopicElement* elmTopic)
}
}
AggregateTopicElement* AggregateTopicTable::getAggregateTopicElement(Topic* topic)
AggregateTopicElement* AggregateTopicTable::getAggregateTopicElement(
Topic* topic)
{
AggregateTopicElement* elm = _head;
while( elm != nullptr )
while (elm != nullptr)
{
if ( elm->_topic->isMatch(topic->_topicName) )
if (elm->_topic->isMatch(topic->_topicName))
{
break;
}
@@ -279,7 +281,7 @@ AggregateTopicElement* AggregateTopicTable::getAggregateTopicElement(Topic* topi
ClientTopicElement* AggregateTopicTable::getClientElement(Topic* topic)
{
AggregateTopicElement* elm = getAggregateTopicElement(topic);
if ( elm != nullptr )
if (elm != nullptr)
{
return elm->_head;
}
@@ -294,18 +296,18 @@ void AggregateTopicTable::print(void)
AggregateTopicElement* elm = _head;
printf("Beginning of AggregateTopicTable\n");
while( elm != nullptr )
while (elm != nullptr)
{
printf("%s\n", elm->_topic->getTopicName()->c_str());
ClientTopicElement* clElm = elm->getFirstClientTopicElement();
Client* client = clElm->getClient();
while ( client != nullptr )
while (client != nullptr)
{
printf(" %s\n", client->getClientId());
clElm = clElm->getNextClientElement();
if ( clElm != nullptr )
if (clElm != nullptr)
{
client = clElm->getClient();
}

View File

@@ -49,10 +49,10 @@ public:
private:
void erase(AggregateTopicElement* elmTopic);
Mutex _mutex;
AggregateTopicElement* _head {nullptr};
AggregateTopicElement* _tail {nullptr};
int _cnt {0};
int _maxSize {MAX_MESSAGEID_TABLE_SIZE};
AggregateTopicElement* _head { nullptr };
AggregateTopicElement* _tail { nullptr };
int _cnt { 0 };
int _maxSize { MAX_MESSAGEID_TABLE_SIZE };
};
/*=====================================
@@ -68,17 +68,18 @@ public:
ClientTopicElement* add(Client* client);
ClientTopicElement* getFirstClientTopicElement(void);
ClientTopicElement* getNextClientTopicElement(ClientTopicElement* elmClient);
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};
Topic* _topic { nullptr };
AggregateTopicElement* _next { nullptr };
AggregateTopicElement* _prev { nullptr };
ClientTopicElement* _head { nullptr };
ClientTopicElement* _tail { nullptr };
};
/*=====================================
@@ -96,13 +97,11 @@ public:
Client* getClient(void);
private:
Client* _client {nullptr};
ClientTopicElement* _next {nullptr};
ClientTopicElement* _prev {nullptr};
Client* _client { nullptr };
ClientTopicElement* _next { nullptr };
ClientTopicElement* _prev { nullptr };
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWAGGREGATETOPICTABLE_H_ */

View File

@@ -26,7 +26,8 @@
using namespace MQTTSNGW;
Aggregater::Aggregater(Gateway* gw) : Adapter(gw)
Aggregater::Aggregater(Gateway* gw) :
Adapter(gw)
{
_gateway = gw;
}
@@ -63,13 +64,12 @@ Client* Aggregater::convertClient(uint16_t msgId, uint16_t* clientMsgId)
return _msgIdTable.getClientMsgId(msgId, clientMsgId);
}
uint16_t Aggregater::addMessageIdTable(Client* client, uint16_t msgId)
{
/* set Non secure client`s nextMsgId. otherwise Id is duplicated.*/
MessageIdElement* elm = _msgIdTable.add(this, client, msgId);
if ( elm == nullptr )
if (elm == nullptr)
{
return 0;
}
@@ -84,12 +84,12 @@ uint16_t Aggregater::getMsgId(Client* client, uint16_t clientMsgId)
return _msgIdTable.getMsgId(client, clientMsgId);
}
AggregateTopicElement* Aggregater::addAggregateTopic(Topic* topic, Client* client)
AggregateTopicElement* Aggregater::addAggregateTopic(Topic* topic,
Client* client)
{
return _topicTable.add(topic, client);
}
void Aggregater::removeAggregateTopic(Topic* topic, Client* client)
{
_topicTable.erase(topic, client);
@@ -103,7 +103,7 @@ AggregateTopicElement* Aggregater::findTopic(Topic* topic)
ClientTopicElement* Aggregater::getClientElement(Topic* topic)
{
AggregateTopicElement* elm = findTopic(topic);
if ( elm != nullptr )
if (elm != nullptr)
{
return elm->getFirstClientTopicElement();
}
@@ -123,26 +123,26 @@ bool Aggregater::testMessageIdTable(void)
Client* client = new Client();
uint16_t msgId = 0;
printf("msgId=%d\n", addMessageIdTable(client,1));
printf("msgId=%d\n", addMessageIdTable(client,2));
printf("msgId=%d\n", addMessageIdTable(client,3));
printf("msgId=%d\n", addMessageIdTable(client,1));
printf("msgId=%d\n", addMessageIdTable(client,2));
printf("msgId=%d\n", addMessageIdTable(client,3));
printf("msgId=%d\n", addMessageIdTable(client,4));
printf("msgId=%d\n", addMessageIdTable(client,4));
printf("msgId=%d\n", addMessageIdTable(client,4));
printf("msgId=%d\n", addMessageIdTable(client, 1));
printf("msgId=%d\n", addMessageIdTable(client, 2));
printf("msgId=%d\n", addMessageIdTable(client, 3));
printf("msgId=%d\n", addMessageIdTable(client, 1));
printf("msgId=%d\n", addMessageIdTable(client, 2));
printf("msgId=%d\n", addMessageIdTable(client, 3));
printf("msgId=%d\n", addMessageIdTable(client, 4));
printf("msgId=%d\n", addMessageIdTable(client, 4));
printf("msgId=%d\n", addMessageIdTable(client, 4));
convertClient(1,&msgId);
printf("msgId=%d\n",msgId);
convertClient(2,&msgId);
printf("msgId=%d\n",msgId);
convertClient(5,&msgId);
printf("msgId=%d\n",msgId);
convertClient(4,&msgId);
printf("msgId=%d\n",msgId);
convertClient(3,&msgId);
printf("msgId=%d\n",msgId);
convertClient(1, &msgId);
printf("msgId=%d\n", msgId);
convertClient(2, &msgId);
printf("msgId=%d\n", msgId);
convertClient(5, &msgId);
printf("msgId=%d\n", msgId);
convertClient(4, &msgId);
printf("msgId=%d\n", msgId);
convertClient(3, &msgId);
printf("msgId=%d\n", msgId);
return true;
}

View File

@@ -34,7 +34,7 @@ class Topics;
/*=====================================
Class Aggregater
=====================================*/
class Aggregater : public Adapter
class Aggregater: public Adapter
{
friend class MessageIdTable;
public:
@@ -65,18 +65,14 @@ public:
private:
uint16_t msgId(void);
Gateway* _gateway {nullptr};
Gateway* _gateway { nullptr };
MessageIdTable _msgIdTable;
AggregateTopicTable _topicTable;
bool _isActive {false};
bool _isSecure {false};
bool _isActive { false };
bool _isSecure { false };
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWAGGREGATER_H_ */

View File

@@ -30,7 +30,7 @@ char* currentDateTime(void);
BrokerRecvTask::BrokerRecvTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*)this);
_gateway->attach((Thread*) this);
_light = nullptr;
}
@@ -77,7 +77,7 @@ void BrokerRecvTask::run(void)
/* Prepare sockets list to read */
Client* client = _gateway->getClientList()->getClient(0);
while ( client )
while (client)
{
if (client->getNetwork()->isValid())
{
@@ -104,7 +104,7 @@ void BrokerRecvTask::run(void)
{
client = _gateway->getClientList()->getClient(0);
while ( client )
while (client)
{
_light->blueLight(false);
if (client->getNetwork()->isValid())
@@ -117,9 +117,9 @@ void BrokerRecvTask::run(void)
/* read sockets */
_light->blueLight(true);
rc = packet->recv(client->getNetwork());
if ( rc > 0 )
if (rc > 0)
{
if ( log(client, packet) == -1 )
if (log(client, packet) == -1)
{
delete packet;
goto nextClient;
@@ -132,7 +132,7 @@ void BrokerRecvTask::run(void)
}
else
{
if ( rc == 0 ) // Disconnected
if (rc == 0) // Disconnected
{
client->getNetwork()->close();
delete packet;
@@ -140,7 +140,7 @@ void BrokerRecvTask::run(void)
/* delete client when the client is not authorized & session is clean */
_gateway->getClientList()->erase(client);
if ( client )
if (client)
{
client = client->getNextClient();
}
@@ -148,20 +148,35 @@ void BrokerRecvTask::run(void)
}
else if (rc == -1)
{
WRITELOG("%s BrokerRecvTask can't receive a packet from the broker errno=%d %s%s\n", ERRMSG_HEADER, errno, client->getClientId(), ERRMSG_FOOTER);
WRITELOG(
"%s BrokerRecvTask can't receive a packet from the broker errno=%d %s%s\n",
ERRMSG_HEADER, errno,
client->getClientId(),
ERRMSG_FOOTER);
}
else if ( rc == -2 )
else if (rc == -2)
{
WRITELOG("%s BrokerRecvTask receive invalid length of packet from the broker. DISCONNECT %s %s\n", ERRMSG_HEADER, client->getClientId(),ERRMSG_FOOTER);
WRITELOG(
"%s BrokerRecvTask receive invalid length of packet from the broker. DISCONNECT %s %s\n",
ERRMSG_HEADER,
client->getClientId(),
ERRMSG_FOOTER);
}
else if ( rc == -3 )
else if (rc == -3)
{
WRITELOG("%s BrokerRecvTask can't get memories for the packet %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
WRITELOG(
"%s BrokerRecvTask can't get memories for the packet %s%s\n",
ERRMSG_HEADER,
client->getClientId(),
ERRMSG_FOOTER);
}
delete packet;
if ( (rc == -1 || rc == -2) && ( client->isActive() || client->isSleep() || client->isAwake() ))
if ((rc == -1 || rc == -2)
&& (client->isActive()
|| client->isSleep()
|| client->isAwake()))
{
/* disconnect the client */
packet = new MQTTGWPacket();
@@ -173,8 +188,7 @@ void BrokerRecvTask::run(void)
}
}
}
nextClient:
client = client->getNextClient();
nextClient: client = client->getNextClient();
}
}
}
@@ -186,30 +200,38 @@ void BrokerRecvTask::run(void)
*/
int BrokerRecvTask::log(Client* client, MQTTGWPacket* packet)
{
char pbuf[(SIZE_OF_LOG_PACKET + 5 )* 3];
char pbuf[(SIZE_OF_LOG_PACKET + 5) * 3];
char msgId[6];
int rc = 0;
switch (packet->getType())
{
case CONNACK:
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), LEFTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), LEFTARROWB,
client->getClientId(), packet->print(pbuf));
break;
case PUBLISH:
WRITELOG(FORMAT_W_MSGID_Y_W_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_Y_W_NL, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), LEFTARROWB, client->getClientId(),
packet->print(pbuf));
break;
case PUBACK:
case PUBREC:
case PUBREL:
case PUBCOMP:
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), LEFTARROWB, client->getClientId(),
packet->print(pbuf));
break;
case SUBACK:
case UNSUBACK:
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), LEFTARROWB, client->getClientId(),
packet->print(pbuf));
break;
case PINGRESP:
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), LEFTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), LEFTARROWB,
client->getClientId(), packet->print(pbuf));
break;
default:
WRITELOG("Type=%x\n", packet->getType());

View File

@@ -28,7 +28,7 @@ namespace MQTTSNGW
class BrokerRecvTask: public Thread
{
MAGIC_WORD_FOR_THREAD;
;
public:
BrokerRecvTask(Gateway* gateway);
~BrokerRecvTask();
@@ -44,5 +44,4 @@ private:
}
#endif /* MQTTSNGWBROKERRECVTASK_H_ */

View File

@@ -34,7 +34,7 @@ char* currentDateTime();
BrokerSendTask::BrokerSendTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*)this);
_gateway->attach((Thread*) this);
_gwparams = nullptr;
_light = nullptr;
}
@@ -68,14 +68,14 @@ void BrokerSendTask::run()
{
ev = _gateway->getBrokerSendQue()->wait();
if ( ev->getEventType() == EtStop )
if (ev->getEventType() == EtStop)
{
WRITELOG("\n%s BrokerSendTask stopped.", currentDateTime());
delete ev;
return;
}
if ( ev->getEventType() == EtBrokerSend)
if (ev->getEventType() == EtBrokerSend)
{
client = ev->getClient();
packet = ev->getMQTTGWPacket();
@@ -83,30 +83,39 @@ void BrokerSendTask::run()
/* Check Client is managed by Adapters */
client = adpMgr->getClient(client);
if ( packet->getType() == CONNECT && client->getNetwork()->isValid() )
if (packet->getType() == CONNECT && client->getNetwork()->isValid())
{
client->getNetwork()->close();
}
if ( !client->getNetwork()->isValid() )
if (!client->getNetwork()->isValid())
{
/* connect to the broker and send a packet */
if (client->isSecureNetwork())
{
rc = client->getNetwork()->connect((const char*)_gwparams->brokerName, (const char*)_gwparams->portSecure, (const char*)_gwparams->rootCApath,
(const char*)_gwparams->rootCAfile, (const char*)_gwparams->certKey, (const char*)_gwparams->privateKey);
rc = client->getNetwork()->connect(
(const char*) _gwparams->brokerName,
(const char*) _gwparams->portSecure,
(const char*) _gwparams->rootCApath,
(const char*) _gwparams->rootCAfile,
(const char*) _gwparams->certKey,
(const char*) _gwparams->privateKey);
}
else
{
rc = client->getNetwork()->connect((const char*)_gwparams->brokerName, (const char*)_gwparams->port);
rc = client->getNetwork()->connect(
(const char*) _gwparams->brokerName,
(const char*) _gwparams->port);
}
if ( !rc )
if (!rc)
{
/* disconnect the broker and the client */
WRITELOG("%s BrokerSendTask: %s can't connect to the broker. errno=%d %s %s\n",
ERRMSG_HEADER, client->getClientId(), errno, strerror(errno), ERRMSG_FOOTER);
WRITELOG(
"%s BrokerSendTask: %s can't connect to the broker. errno=%d %s %s\n",
ERRMSG_HEADER, client->getClientId(), errno,
strerror(errno), ERRMSG_FOOTER);
delete ev;
client->getNetwork()->close();
continue;
@@ -115,13 +124,13 @@ void BrokerSendTask::run()
/* send a packet */
_light->blueLight(true);
if ( (rc = packet->send(client->getNetwork())) > 0 )
if ((rc = packet->send(client->getNetwork())) > 0)
{
if ( packet->getType() == CONNECT )
if (packet->getType() == CONNECT)
{
client->connectSended();
}
else if ( packet->getType() == DISCONNECT )
else if (packet->getType() == DISCONNECT)
{
client->getNetwork()->close();
client->disconnected();
@@ -130,9 +139,11 @@ void BrokerSendTask::run()
}
else
{
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);
if ( errno != EBADF )
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);
if ( errno != EBADF)
{
client->getNetwork()->close();
}
@@ -151,22 +162,24 @@ void BrokerSendTask::run()
}
}
/**
* write message content into stdout or Ringbuffer
*/
void BrokerSendTask::log(Client* client, MQTTGWPacket* packet)
{
char pbuf[(SIZE_OF_LOG_PACKET + 5 )* 3];
char pbuf[(SIZE_OF_LOG_PACKET + 5) * 3];
char msgId[6];
switch (packet->getType())
{
case CONNECT:
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), RIGHTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(),
RIGHTARROWB, client->getClientId(), packet->print(pbuf));
break;
case PUBLISH:
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), RIGHTARROWB, client->getClientId(),
packet->print(pbuf));
break;
case SUBSCRIBE:
case UNSUBSCRIBE:
@@ -174,13 +187,17 @@ void BrokerSendTask::log(Client* client, MQTTGWPacket* packet)
case PUBREC:
case PUBREL:
case PUBCOMP:
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), RIGHTARROWB, client->getClientId(),
packet->print(pbuf));
break;
case PINGREQ:
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), RIGHTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(),
RIGHTARROWB, client->getClientId(), packet->print(pbuf));
break;
case DISCONNECT:
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), RIGHTARROWB, client->getClientId(), packet->print(pbuf));
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(),
RIGHTARROWB, client->getClientId(), packet->print(pbuf));
break;
default:
break;

View File

@@ -27,9 +27,9 @@ class Adapter;
/*=====================================
Class BrokerSendTask
=====================================*/
class BrokerSendTask : public Thread
class BrokerSendTask: public Thread
{
MAGIC_WORD_FOR_THREAD;
MAGIC_WORD_FOR_THREAD;
friend AdapterManager;
public:
BrokerSendTask(Gateway* gateway);

View File

@@ -28,11 +28,12 @@
using namespace MQTTSNGW;
char* currentDateTime(void);
/*=====================================
Class Client
=====================================*/
static const char* theClientStatus[] = { "Disconnected", "TryConnecting", "Connecting", "Active", "Asleep", "Awake", "Lost" };
static const char* theClientStatus[] =
{ "Disconnected", "TryConnecting", "Connecting", "Active", "Asleep", "Awake",
"Lost" };
Client::Client(bool secure)
{
@@ -63,22 +64,22 @@ Client::Client(bool secure)
Client::~Client()
{
if ( _topics )
if (_topics)
{
delete _topics;
}
if ( _clientId )
if (_clientId)
{
free(_clientId);
}
if ( _willTopic )
if (_willTopic)
{
free(_willTopic);
}
if ( _willMsg )
if (_willMsg)
{
free(_willMsg);
}
@@ -117,13 +118,15 @@ void Client::deleteFirstClientSleepPacket()
int Client::setClientSleepPacket(MQTTGWPacket* packet)
{
int rc = _clientSleepPacketQue.post(packet);
if ( rc )
if (rc)
{
WRITELOG("%s %s is sleeping. the packet was saved.\n", currentDateTime(), _clientId);
WRITELOG("%s %s is sleeping. the packet was saved.\n",
currentDateTime(), _clientId);
}
else
{
WRITELOG("%s %s is sleeping but discard the packet.\n", currentDateTime(), _clientId);
WRITELOG("%s %s is sleeping but discard the packet.\n",
currentDateTime(), _clientId);
}
return rc;
}
@@ -141,13 +144,15 @@ void Client::deleteFirstProxyPacket()
int Client::setProxyPacket(MQTTSNPacket* packet)
{
int rc = _proxyPacketQue.post(packet);
if ( rc )
if (rc)
{
WRITELOG("%s %s is Disconnected. the packet was saved.\n", currentDateTime(), _clientId);
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);
WRITELOG("%s %s is Disconnected and discard the packet.\n",
currentDateTime(), _clientId);
}
return rc;
}
@@ -177,11 +182,13 @@ void Client::clearWaitedSubTopicId(void)
_waitedSubTopicIdMap.clear();
}
void Client::setWaitedPubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type)
void Client::setWaitedPubTopicId(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type)
{
_waitedPubTopicIdMap.add(msgId, topicId, type);
}
void Client::setWaitedSubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type)
void Client::setWaitedSubTopicId(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type)
{
_waitedSubTopicIdMap.add(msgId, topicId, type);
}
@@ -224,7 +231,8 @@ bool Client::erasable(void)
void Client::updateStatus(MQTTSNPacket* packet)
{
if (((_status == Cstat_Disconnected) || (_status == Cstat_Lost)) && packet->getType() == MQTTSN_CONNECT)
if (((_status == Cstat_Disconnected) || (_status == Cstat_Lost))
&& packet->getType() == MQTTSN_CONNECT)
{
setKeepAlive(packet);
}
@@ -240,7 +248,7 @@ void Client::updateStatus(MQTTSNPacket* packet)
case MQTTSN_PUBCOMP:
case MQTTSN_PUBREL:
case MQTTSN_PUBREC:
if ( _clientType != Ctype_Proxy )
if (_clientType != Ctype_Proxy)
{
_keepAliveTimer.start(_keepAliveMsec * 1.5);
}
@@ -280,8 +288,7 @@ void Client::updateStatus(MQTTSNPacket* packet)
default:
break;
}
}
DEBUGLOG("Client Status = %s\n", theClientStatus[_status]);
} DEBUGLOG("Client Status = %s\n", theClientStatus[_status]);
}
void Client::updateStatus(ClientStatus stat)
@@ -319,7 +326,7 @@ void Client::tryConnect(void)
bool Client::isConnectSendable(void)
{
if ( _status == Cstat_Lost || _status == Cstat_TryConnecting )
if (_status == Cstat_Lost || _status == Cstat_TryConnecting)
{
return false;
}
@@ -332,7 +339,7 @@ bool Client::isConnectSendable(void)
uint16_t Client::getNextPacketId(void)
{
_packetId++;
if ( _packetId == 0xffff )
if (_packetId == 0xffff)
{
_packetId = 1;
}
@@ -441,49 +448,49 @@ Client* Client::getNextClient(void)
void Client::setClientId(MQTTSNString id)
{
if ( _clientId )
if (_clientId)
{
free(_clientId);
}
if ( id.cstring )
if (id.cstring)
{
_clientId = (char*)calloc(strlen(id.cstring) + 1, 1);
_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);
_clientId = (char*) calloc(MQTTSNstrlen(id) + 1, 1);
unsigned char* ptr = (unsigned char*) _clientId;
writeMQTTSNString((unsigned char**) &ptr, id);
}
}
void Client::setWillTopic(MQTTSNString willTopic)
{
if ( _willTopic )
if (_willTopic)
{
free(_willTopic);
}
_willTopic = (char*)calloc(MQTTSNstrlen(willTopic) + 1, 1);
_willTopic = (char*) calloc(MQTTSNstrlen(willTopic) + 1, 1);
/* save willTopic into (char*)_willTopic with NULL termination */
unsigned char* ptr = (unsigned char*)_willTopic;
writeMQTTSNString((unsigned char**)&ptr, willTopic);
unsigned char* ptr = (unsigned char*) _willTopic;
writeMQTTSNString((unsigned char**) &ptr, willTopic);
}
void Client::setWillMsg(MQTTSNString willMsg)
{
if ( _willMsg)
if (_willMsg)
{
free(_willMsg);
}
_willMsg = (char*)calloc(MQTTSNstrlen(willMsg) + 1, 1);
_willMsg = (char*) calloc(MQTTSNstrlen(willMsg) + 1, 1);
/* save willMsg into (char*)_willMsg with NULL termination */
unsigned char* ptr = (unsigned char*)_willMsg;
writeMQTTSNString((unsigned char**)&ptr, willMsg);
unsigned char* ptr = (unsigned char*) _willMsg;
writeMQTTSNString((unsigned char**) &ptr, willMsg);
}
char* Client::getClientId(void)
@@ -528,7 +535,7 @@ bool Client::isAggregater(void)
void Client::setAdapterType(AdapterType type)
{
switch ( type )
switch (type)
{
case Atype_QoSm1Proxy:
_clientType = Ctype_Proxy;
@@ -577,8 +584,6 @@ bool Client::isHoldPingReqest(void)
return _holdPingRequest;
}
/*=====================================
Class WaitREGACKPacket
=====================================*/
@@ -691,4 +696,3 @@ uint8_t WaitREGACKPacketList::getCount(void)
return _cnt;
}

View File

@@ -49,7 +49,6 @@ public:
_que = new Que<T>;
}
~PacketQue()
{
clear();
@@ -72,8 +71,7 @@ public:
}
}
int
post(T* packet)
int post(T* packet)
{
int rc;
_mutex.lock();
@@ -113,8 +111,6 @@ private:
Mutex _mutex;
};
/*=====================================
Class WaitREGACKPacket
=====================================*/
@@ -151,20 +147,29 @@ private:
waitREGACKPacket* _end;
};
/*=====================================
Class Client
=====================================*/
typedef enum
{
Cstat_Disconnected = 0, Cstat_TryConnecting, Cstat_Connecting, Cstat_Active, Cstat_Asleep, Cstat_Awake, Cstat_Lost
Cstat_Disconnected = 0,
Cstat_TryConnecting,
Cstat_Connecting,
Cstat_Active,
Cstat_Asleep,
Cstat_Awake,
Cstat_Lost
} ClientStatus;
typedef enum
{
Ctype_Regular = 0, Ctype_Forwarded, Ctype_QoS_1, Ctype_Aggregated, Ctype_Proxy, Ctype_Aggregater
}ClientType;
Ctype_Regular = 0,
Ctype_Forwarded,
Ctype_QoS_1,
Ctype_Aggregated,
Ctype_Proxy,
Ctype_Aggregater
} ClientType;
class Forwarder;
@@ -193,8 +198,10 @@ public:
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);
void setWaitedPubTopicId(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type);
void setWaitedSubTopicId(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type);
bool checkTimeover(void);
void updateStatus(MQTTSNPacket*);
@@ -299,7 +306,5 @@ private:
Client* _prevClient;
};
}
#endif /* MQTTSNGWCLIENT_H_ */

View File

@@ -51,10 +51,10 @@ ClientList::~ClientList()
void ClientList::initialize(bool aggregate)
{
if (theGateway->getGWParams()->clientAuthentication )
if (theGateway->getGWParams()->clientAuthentication)
{
int type = TRANSPEARENT_TYPE;
if ( aggregate )
if (aggregate)
{
type = AGGREGATER_TYPE;
}
@@ -62,7 +62,7 @@ void ClientList::initialize(bool aggregate)
_authorize = true;
}
if ( theGateway->getGWParams()->predefinedTopic )
if (theGateway->getGWParams()->predefinedTopic)
{
setPredefinedTopics(aggregate);
}
@@ -72,15 +72,18 @@ void ClientList::setClientList(int type)
{
if (!createList(theGateway->getGWParams()->clientListName, type))
{
throw Exception("ClientList::setClientList No client list defined by config file.");
throw Exception(
"ClientList::setClientList No client list defined by config file.");
}
}
void ClientList::setPredefinedTopics(bool aggrecate)
{
if ( !readPredefinedList(theGateway->getGWParams()->predefinedTopicFileName, aggrecate) )
if (!readPredefinedList(theGateway->getGWParams()->predefinedTopicFileName,
aggrecate))
{
throw Exception("ClientList::setPredefinedTopics No predefindTopi list defined by config file.");
throw Exception(
"ClientList::setPredefinedTopics No predefindTopi list defined by config file.");
}
}
@@ -148,15 +151,17 @@ bool ClientList::createList(const char* fileName, int type)
forwarder = (data.find("forwarder") != string::npos);
secure = (data.find("secureConnection") != string::npos);
stable = !(data.find("unstableLine") != string::npos);
if ( (qos_1 && type == QOSM1PROXY_TYPE) || (!qos_1 && type == AGGREGATER_TYPE) )
if ((qos_1 && type == QOSM1PROXY_TYPE)
|| (!qos_1 && type == AGGREGATER_TYPE))
{
createClient(&netAddr, &clientId, stable, secure, type);
}
else if ( forwarder && type == FORWARDER_TYPE)
else if (forwarder && type == FORWARDER_TYPE)
{
theGateway->getAdapterManager()->getForwarderList()->addForwarder(&netAddr, &clientId);
theGateway->getAdapterManager()->getForwarderList()->addForwarder(
&netAddr, &clientId);
}
else if (type == TRANSPEARENT_TYPE )
else if (type == TRANSPEARENT_TYPE)
{
createClient(&netAddr, &clientId, stable, secure, type);
}
@@ -179,7 +184,8 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
FILE* fp;
char buf[MAX_CLIENTID_LENGTH + 256];
size_t pos0, pos1;
MQTTSNString clientId = MQTTSNString_initializer;;
MQTTSNString clientId = MQTTSNString_initializer;
;
bool rc = false;
if ((fp = fopen(fileName, "r")) != 0)
@@ -201,12 +207,12 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
}
pos0 = data.find_first_of(",");
pos1 = data.find(",", pos0 + 1) ;
pos1 = data.find(",", pos0 + 1);
string id = data.substr(0, pos0);
clientId.cstring = strdup(id.c_str());
string topicName = data.substr(pos0 + 1, pos1 - pos0 -1);
string topicName = data.substr(pos0 + 1, pos1 - pos0 - 1);
uint16_t topicID = stoul(data.substr(pos1 + 1));
createPredefinedTopic( &clientId, topicName, topicID, aggregate);
createPredefinedTopic(&clientId, topicName, topicID, aggregate);
free(clientId.cstring);
}
fclose(fp);
@@ -214,7 +220,8 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
}
else
{
WRITELOG("ClientList can not open the Predefined Topic List. %s\n", fileName);
WRITELOG("ClientList can not open the Predefined Topic List. %s\n",
fileName);
return false;
}
return rc;
@@ -222,7 +229,7 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
void ClientList::erase(Client*& client)
{
if ( !_authorize && client->erasable())
if (!_authorize && client->erasable())
{
_mutex.lock();
Client* prev = client->_prevClient;
@@ -247,7 +254,7 @@ void ClientList::erase(Client*& client)
}
_clientCnt--;
Forwarder* fwd = client->getForwarder();
if ( fwd )
if (fwd)
{
fwd->eraseClient(client);
}
@@ -259,14 +266,14 @@ void ClientList::erase(Client*& client)
Client* ClientList::getClient(SensorNetAddress* addr)
{
if ( addr )
if (addr)
{
_mutex.lock();
Client* client = _firstClient;
while (client != nullptr)
{
if (client->getSensorNetAddress()->isMatch(addr) )
if (client->getSensorNetAddress()->isMatch(addr))
{
_mutex.unlock();
return client;
@@ -282,9 +289,9 @@ Client* ClientList::getClient(int index)
{
Client* client = _firstClient;
int p = 0;
while ( client != nullptr )
while (client != nullptr)
{
if ( p == index )
if (p == index)
{
return client;
}
@@ -297,21 +304,21 @@ Client* ClientList::getClient(int index)
return nullptr;
}
Client* ClientList::getClient(MQTTSNString* clientId)
{
_mutex.lock();
Client* client = _firstClient;
const char* clID =clientId->cstring;
const char* clID = clientId->cstring;
if (clID == nullptr )
if (clID == nullptr)
{
clID = clientId->lenstring.data;
}
while (client != nullptr)
{
if (strncmp((const char*)client->getClientId(), clID, MQTTSNstrlen(*clientId)) == 0 )
if (strncmp((const char*) client->getClientId(), clID,
MQTTSNstrlen(*clientId)) == 0)
{
_mutex.unlock();
return client;
@@ -322,35 +329,37 @@ Client* ClientList::getClient(MQTTSNString* clientId)
return 0;
}
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, int type)
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
int type)
{
return createClient(addr, clientId, false, false, type);
}
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine, bool secure, int type)
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
bool unstableLine, bool secure, int type)
{
Client* client = nullptr;
/* anonimous clients */
if ( _clientCnt > MAX_CLIENTS )
if (_clientCnt > MAX_CLIENTS)
{
return 0; // full of clients
}
client = getClient(addr);
if ( client )
if (client)
{
return client;
}
/* creat a new client */
client = new Client(secure);
if ( addr )
if (addr)
{
client->setClientAddress(addr);
}
client->setSensorNetType(unstableLine);
if ( MQTTSNstrlen(*clientId) )
if (MQTTSNstrlen(*clientId))
{
client->setClientId(*clientId);
}
@@ -362,11 +371,11 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
free(dummyId.cstring);
}
if ( type == AGGREGATER_TYPE )
if (type == AGGREGATER_TYPE)
{
client->setAggregated();
}
else if ( type == QOSM1PROXY_TYPE )
else if (type == QOSM1PROXY_TYPE)
{
client->setQoSm1();
}
@@ -374,7 +383,7 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
_mutex.lock();
/* add the list */
if ( _firstClient == nullptr )
if (_firstClient == nullptr)
{
_firstClient = client;
_endClient = client;
@@ -390,47 +399,49 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
return client;
}
Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicName, uint16_t topicId, bool aggregate)
Client* ClientList::createPredefinedTopic(MQTTSNString* clientId,
string topicName, uint16_t topicId, bool aggregate)
{
if ( topicId == 0 )
if (topicId == 0)
{
WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n", topicName.c_str());
WRITELOG("Invalid TopicId. Predefined Topic %s, TopicId is 0. \n",
topicName.c_str());
return nullptr;
}
if ( strcmp(clientId->cstring, common_topic) == 0 )
if (strcmp(clientId->cstring, common_topic) == 0)
{
theGateway->getTopics()->add((const char*)topicName.c_str(), topicId);
theGateway->getTopics()->add((const char*) topicName.c_str(), topicId);
return nullptr;
}
else
{
Client* client = getClient(clientId);
if ( _authorize && client == nullptr )
if (_authorize && client == nullptr)
{
return nullptr;
}
/* anonimous clients */
if ( _clientCnt > MAX_CLIENTS )
if (_clientCnt > MAX_CLIENTS)
{
return nullptr; // full of clients
}
if ( client == nullptr )
if (client == nullptr)
{
/* creat a new client */
client = new Client();
client->setClientId(*clientId);
if ( aggregate )
if (aggregate)
{
client->setAggregated();
}
_mutex.lock();
/* add the list */
if ( _firstClient == nullptr )
if (_firstClient == nullptr)
{
_firstClient = client;
_endClient = client;
@@ -446,7 +457,7 @@ Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicN
}
// create Topic & Add it
client->getTopics()->add((const char*)topicName.c_str(), topicId);
client->getTopics()->add((const char*) topicName.c_str(), topicId);
client->_hasPredefTopic = true;
return client;
}
@@ -462,4 +473,3 @@ bool ClientList::isAuthorized()
return _authorize;
}

View File

@@ -43,8 +43,10 @@ public:
void setClientList(int type);
void setPredefinedTopics(bool aggregate);
void erase(Client*&);
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId,int type);
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine, bool secure, int type);
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId,
int type);
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId,
bool unstableLine, bool secure, int type);
bool createList(const char* fileName, int type);
Client* getClient(SensorNetAddress* addr);
Client* getClient(MQTTSNString* clientId);
@@ -55,18 +57,16 @@ public:
private:
bool readPredefinedList(const char* fileName, bool _aggregate);
Gateway* _gateway {nullptr};
Client* createPredefinedTopic( MQTTSNString* clientId, string topicName, uint16_t toipcId, bool _aggregate);
Gateway* _gateway { nullptr };
Client* createPredefinedTopic(MQTTSNString* clientId, string topicName,
uint16_t toipcId, bool _aggregate);
Client* _firstClient;
Client* _endClient;
Mutex _mutex;
uint16_t _clientCnt;
bool _authorize {false};
bool _authorize { false };
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTLIST_H_ */

View File

@@ -30,7 +30,7 @@ char* currentDateTime(void);
ClientRecvTask::ClientRecvTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*)this);
_gateway->attach((Thread*) this);
_sensorNetwork = _gateway->getSensorNetwork();
}
@@ -44,7 +44,7 @@ ClientRecvTask::~ClientRecvTask()
*/
void ClientRecvTask::initialize(int argc, char** argv)
{
if ( _sensorNetwork->initialize() < 0 )
if (_sensorNetwork->initialize() < 0)
{
throw Exception(" Can't open the sensor network.\n");
}
@@ -60,7 +60,8 @@ void ClientRecvTask::run()
Event* ev = nullptr;
AdapterManager* adpMgr = _gateway->getAdapterManager();
QoSm1Proxy* qosm1Proxy = adpMgr->getQoSm1Proxy();
int clientType = adpMgr->isAggregaterActive() ? AGGREGATER_TYPE : TRANSPEARENT_TYPE;
int clientType =
adpMgr->isAggregaterActive() ? AGGREGATER_TYPE : TRANSPEARENT_TYPE;
ClientList* clientList = _gateway->getClientList();
EventQue* packetEventQue = _gateway->getPacketEventQue();
@@ -82,19 +83,20 @@ void ClientRecvTask::run()
return;
}
if (packetLen < 2 )
if (packetLen < 2)
{
delete packet;
continue;
}
if ( packet->getType() <= MQTTSN_ADVERTISE || packet->getType() == MQTTSN_GWINFO )
if (packet->getType() <= MQTTSN_ADVERTISE
|| packet->getType() == MQTTSN_GWINFO)
{
delete packet;
continue;
}
if ( packet->getType() == MQTTSN_SEARCHGW )
if (packet->getType() == MQTTSN_SEARCHGW)
{
/* write log and post Event */
log(0, packet, 0);
@@ -104,23 +106,26 @@ void ClientRecvTask::run()
continue;
}
SensorNetAddress* senderAddr =
_gateway->getSensorNetwork()->getSenderAddress();
SensorNetAddress* senderAddr = _gateway->getSensorNetwork()->getSenderAddress();
if ( packet->getType() == MQTTSN_ENCAPSULATED )
if (packet->getType() == MQTTSN_ENCAPSULATED)
{
fwd = _gateway->getAdapterManager()->getForwarderList()->getForwarder(senderAddr);
fwd =
_gateway->getAdapterManager()->getForwarderList()->getForwarder(
senderAddr);
if ( fwd != nullptr )
if (fwd != nullptr)
{
MQTTSNString fwdName = MQTTSNString_initializer;
fwdName.cstring = const_cast<char *>( fwd->getName() );
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() );
encap.desirialize(packet->getPacketData(),
packet->getPacketLength());
nodeId.setId(encap.getWirelessNodeId());
client = fwd->getClient(&nodeId);
packet = encap.getMQTTSNPacket();
}
@@ -129,36 +134,39 @@ void ClientRecvTask::run()
{
/* Check the client belonging to QoS-1Proxy ? */
if ( qosm1Proxy->isActive() )
if (qosm1Proxy->isActive())
{
const char* clientName = qosm1Proxy->getClientId(senderAddr);
if ( clientName != nullptr )
if (clientName != nullptr)
{
client = qosm1Proxy->getClient();
if ( !packet->isQoSMinusPUBLISH() )
if (!packet->isQoSMinusPUBLISH())
{
log(clientName, packet);
WRITELOG("%s %s %s can send only PUBLISH with QoS-1.%s\n", ERRMSG_HEADER, clientName, senderAddr->sprint(buf), ERRMSG_FOOTER);
WRITELOG(
"%s %s %s can send only PUBLISH with QoS-1.%s\n",
ERRMSG_HEADER, clientName,
senderAddr->sprint(buf), ERRMSG_FOOTER);
delete packet;
continue;
}
}
}
if ( client == nullptr )
if (client == nullptr)
{
client = _gateway->getClientList()->getClient(senderAddr);
}
}
if ( client != nullptr )
if (client != nullptr)
{
/* write log and post Event */
log(client, packet, 0);
ev = new Event();
ev->setClientRecvEvent(client,packet);
ev->setClientRecvEvent(client, packet);
packetEventQue->post(ev);
}
else
@@ -168,32 +176,36 @@ void ClientRecvTask::run()
{
MQTTSNPacket_connectData data;
memset(&data, 0, sizeof(MQTTSNPacket_connectData));
if ( !packet->getCONNECT(&data) )
if (!packet->getCONNECT(&data))
{
log(0, packet, &data.clientID);
WRITELOG("%s CONNECT message form %s is incorrect.%s\n", ERRMSG_HEADER, senderAddr->sprint(buf), ERRMSG_FOOTER);
WRITELOG("%s CONNECT message form %s is incorrect.%s\n",
ERRMSG_HEADER, senderAddr->sprint(buf),
ERRMSG_FOOTER);
delete packet;
continue;
}
client = clientList->getClient(&data.clientID);
if ( fwd != nullptr )
if (fwd != nullptr)
{
if ( client == nullptr )
if (client == nullptr)
{
/* create a new client */
client = clientList->createClient(0, &data.clientID, clientType);
client = clientList->createClient(0, &data.clientID,
clientType);
}
/* Add to a forwarded client list of forwarder. */
fwd->addClient(client, &nodeId);
}
else
{
if ( client )
if (client)
{
/* Authentication is not required */
if ( _gateway->getGWParams()->clientAuthentication == false)
if (_gateway->getGWParams()->clientAuthentication
== false)
{
client->setClientAddress(senderAddr);
}
@@ -201,15 +213,19 @@ void ClientRecvTask::run()
else
{
/* create a new client */
client = clientList->createClient(senderAddr, &data.clientID, clientType);
client = clientList->createClient(senderAddr,
&data.clientID, clientType);
}
}
log(client, packet, &data.clientID);
if ( client == nullptr )
if (client == nullptr)
{
WRITELOG("%s Client(%s) was rejected. CONNECT message has been discarded.%s\n", ERRMSG_HEADER, senderAddr->sprint(buf), ERRMSG_FOOTER);
WRITELOG(
"%s Client(%s) was rejected. CONNECT message has been discarded.%s\n",
ERRMSG_HEADER, senderAddr->sprint(buf),
ERRMSG_FOOTER);
delete packet;
continue;
}
@@ -222,13 +238,20 @@ void ClientRecvTask::run()
else
{
log(client, packet, 0);
if ( packet->getType() == MQTTSN_ENCAPSULATED )
if (packet->getType() == MQTTSN_ENCAPSULATED)
{
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);
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 MQTTSNGWClientRecvTask 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;
}
@@ -241,21 +264,21 @@ void ClientRecvTask::log(Client* client, MQTTSNPacket* packet, MQTTSNString* id)
const char* clientId;
char cstr[MAX_CLIENTID_LENGTH + 1];
if ( id )
if (id)
{
if ( id->cstring )
if (id->cstring)
{
strncpy(cstr, id->cstring, strlen(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 );
memset((void*) cstr, 0, id->lenstring.len + 1);
strncpy(cstr, id->lenstring.data, id->lenstring.len);
clientId = cstr;
}
}
else if ( client )
else if (client)
{
clientId = client->getClientId();
}
@@ -275,37 +298,46 @@ void ClientRecvTask::log(const char* clientId, MQTTSNPacket* packet)
switch (packet->getType())
{
case MQTTSN_SEARCHGW:
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, CLIENT, packet->print(pbuf));
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));
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));
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));
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));
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));
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));
WRITELOG(FORMAT_W_NL, currentDateTime(), packet->getName(), LEFTARROW,
clientId, packet->print(pbuf));
break;
}
}

View File

@@ -26,9 +26,9 @@ class AdapterManager;
/*=====================================
Class ClientRecvTask
=====================================*/
class ClientRecvTask:public Thread
class ClientRecvTask: public Thread
{
MAGIC_WORD_FOR_THREAD;
MAGIC_WORD_FOR_THREAD;
friend AdapterManager;
public:
ClientRecvTask(Gateway*);

View File

@@ -29,7 +29,7 @@ char* currentDateTime(void);
ClientSendTask::ClientSendTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*)this);
_gateway->attach((Thread*) this);
_sensorNetwork = _gateway->getSensorNetwork();
}
@@ -49,7 +49,7 @@ void ClientSendTask::run()
{
Event* ev = _gateway->getClientSendQue()->wait();
if (ev->getEventType() == EtStop || _gateway->IsStopping() )
if (ev->getEventType() == EtStop || _gateway->IsStopping())
{
WRITELOG("\n%s ClientSendTask stopped.", currentDateTime());
delete ev;
@@ -61,9 +61,10 @@ void ClientSendTask::run()
packet = ev->getMQTTSNPacket();
log(client, packet);
if ( packet->broadcast(_sensorNetwork) < 0 )
if (packet->broadcast(_sensorNetwork) < 0)
{
WRITELOG("%s ClientSendTask can't multicast a packet Error=%d%s\n",
WRITELOG(
"%s ClientSendTask can't multicast a packet Error=%d%s\n",
ERRMSG_HEADER, errno, ERRMSG_FOOTER);
}
}
@@ -82,10 +83,14 @@ void ClientSendTask::run()
rc = packet->unicast(_sensorNetwork, ev->getSensorNetAddress());
}
if ( rc < 0 )
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);
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;
@@ -96,13 +101,15 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
{
char pbuf[SIZE_OF_LOG_PACKET * 3 + 1];
char msgId[6];
const char* clientId = client ? (const char*)client->getClientId() : UNKNOWNCL ;
const char* clientId =
client ? (const char*) client->getClientId() : UNKNOWNCL;
switch (packet->getType())
{
case MQTTSN_ADVERTISE:
case MQTTSN_GWINFO:
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW, CLIENTS, packet->print(pbuf));
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW,
CLIENTS, packet->print(pbuf));
break;
case MQTTSN_CONNACK:
case MQTTSN_DISCONNECT:
@@ -111,11 +118,14 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
case MQTTSN_WILLTOPICRESP:
case MQTTSN_WILLMSGRESP:
case MQTTSN_PINGRESP:
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW, clientId, packet->print(pbuf));
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW,
clientId, packet->print(pbuf));
break;
case MQTTSN_REGISTER:
case MQTTSN_PUBLISH:
WRITELOG(FORMAT_W_MSGID_W_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, clientId, packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_W_G, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), RIGHTARROW, clientId,
packet->print(pbuf));
break;
case MQTTSN_REGACK:
case MQTTSN_PUBACK:
@@ -124,7 +134,9 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
case MQTTSN_PUBCOMP:
case MQTTSN_SUBACK:
case MQTTSN_UNSUBACK:
WRITELOG(FORMAT_W_MSGID_W_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, clientId, packet->print(pbuf));
WRITELOG(FORMAT_W_MSGID_W_G, currentDateTime(), packet->getName(),
packet->getMsgId(msgId), RIGHTARROW, clientId,
packet->print(pbuf));
break;
default:
break;

View File

@@ -28,7 +28,7 @@ class AdapterManager;
=====================================*/
class ClientSendTask: public Thread
{
MAGIC_WORD_FOR_THREAD;
MAGIC_WORD_FOR_THREAD;
friend AdapterManager;
public:
ClientSendTask(Gateway* gateway);

View File

@@ -42,7 +42,8 @@ MQTTSNConnectionHandler::~MQTTSNConnectionHandler()
void MQTTSNConnectionHandler::sendADVERTISE()
{
MQTTSNPacket* adv = new MQTTSNPacket();
adv->setADVERTISE(_gateway->getGWParams()->gatewayId, _gateway->getGWParams()->keepAlive);
adv->setADVERTISE(_gateway->getGWParams()->gatewayId,
_gateway->getGWParams()->keepAlive);
Event* ev1 = new Event();
ev1->setBrodcastEvent(adv); //broadcast
_gateway->getClientSendQue()->post(ev1);
@@ -66,16 +67,17 @@ void MQTTSNConnectionHandler::handleSearchgw(MQTTSNPacket* packet)
/*
* CONNECT
*/
void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handleConnect(Client* client,
MQTTSNPacket* packet)
{
MQTTSNPacket_connectData data;
if ( packet->getCONNECT(&data) == 0 )
if (packet->getCONNECT(&data) == 0)
{
return;
}
/* return CONNACK when the client is sleeping */
if ( client->isSleep() || client->isAwake() )
if (client->isSleep() || client->isAwake())
{
MQTTSNPacket* packet = new MQTTSNPacket();
packet->setCONNACK(MQTTSN_RC_ACCEPTED);
@@ -90,7 +92,7 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
//* clear ConnectData of Client */
Connect* connectData = client->getConnectData();
memset(connectData, 0, sizeof(Connect));
if ( !client->isAdapter() )
if (!client->isAdapter())
{
client->disconnected();
}
@@ -125,7 +127,8 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
/* renew the TopicList */
if (topics)
{
topics->eraseNormal();;
topics->eraseNormal();
;
}
client->setSessionStatus(true);
}
@@ -146,7 +149,9 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
/* CONNECT message was not qued in.
* create CONNECT message & send it to the broker */
MQTTGWPacket* mqMsg = new MQTTGWPacket();
mqMsg->setCONNECT(client->getConnectData(), (unsigned char*)_gateway->getGWParams()->loginId, (unsigned char*)_gateway->getGWParams()->password);
mqMsg->setCONNECT(client->getConnectData(),
(unsigned char*) _gateway->getGWParams()->loginId,
(unsigned char*) _gateway->getGWParams()->password);
Event* ev1 = new Event();
ev1->setBrokerSendEvent(client, mqMsg);
_gateway->getBrokerSendQue()->post(ev1);
@@ -156,13 +161,14 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
/*
* WILLTOPIC
*/
void MQTTSNConnectionHandler::handleWilltopic(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handleWilltopic(Client* client,
MQTTSNPacket* packet)
{
int willQos;
uint8_t willRetain;
MQTTSNString willTopic = MQTTSNString_initializer;
if ( packet->getWILLTOPIC(&willQos, &willRetain, &willTopic) == 0 )
if (packet->getWILLTOPIC(&willQos, &willRetain, &willTopic) == 0)
{
return;
}
@@ -186,9 +192,10 @@ void MQTTSNConnectionHandler::handleWilltopic(Client* client, MQTTSNPacket* pack
/*
* WILLMSG
*/
void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handleWillmsg(Client* client,
MQTTSNPacket* packet)
{
if ( !client->isWaitWillMsg() )
if (!client->isWaitWillMsg())
{
DEBUGLOG(" MQTTSNConnectionHandler::handleWillmsg WaitWillMsgFlg is off.\n");
return;
@@ -197,10 +204,10 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
MQTTSNString willmsg = MQTTSNString_initializer;
Connect* connectData = client->getConnectData();
if( client->isConnectSendable() )
if (client->isConnectSendable())
{
/* save WillMsg in the client */
if ( packet->getWILLMSG(&willmsg) == 0 )
if (packet->getWILLMSG(&willmsg) == 0)
{
return;
}
@@ -209,7 +216,9 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
/* create CONNECT message */
MQTTGWPacket* mqttPacket = new MQTTGWPacket();
connectData->willMsg = client->getWillMsg();
mqttPacket->setCONNECT(connectData, (unsigned char*)_gateway->getGWParams()->loginId, (unsigned char*)_gateway->getGWParams()->password);
mqttPacket->setCONNECT(connectData,
(unsigned char*) _gateway->getGWParams()->loginId,
(unsigned char*) _gateway->getGWParams()->password);
/* Send CONNECT to the broker */
Event* evt = new Event();
@@ -222,13 +231,14 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
/*
* DISCONNECT
*/
void MQTTSNConnectionHandler::handleDisconnect(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handleDisconnect(Client* client,
MQTTSNPacket* packet)
{
uint16_t duration = 0;
if ( packet->getDISCONNECT(&duration) != 0 )
if (packet->getDISCONNECT(&duration) != 0)
{
if ( duration == 0 )
if (duration == 0)
{
MQTTGWPacket* mqMsg = new MQTTGWPacket();
mqMsg->setHeader(DISCONNECT);
@@ -248,7 +258,8 @@ void MQTTSNConnectionHandler::handleDisconnect(Client* client, MQTTSNPacket* pac
/*
* WILLTOPICUPD
*/
void MQTTSNConnectionHandler::handleWilltopicupd(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handleWilltopicupd(Client* client,
MQTTSNPacket* packet)
{
/* send NOT_SUPPORTED responce to the client */
MQTTSNPacket* respMsg = new MQTTSNPacket();
@@ -261,7 +272,8 @@ void MQTTSNConnectionHandler::handleWilltopicupd(Client* client, MQTTSNPacket* p
/*
* WILLMSGUPD
*/
void MQTTSNConnectionHandler::handleWillmsgupd(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handleWillmsgupd(Client* client,
MQTTSNPacket* packet)
{
/* send NOT_SUPPORTED responce to the client */
MQTTSNPacket* respMsg = new MQTTSNPacket();
@@ -274,9 +286,11 @@ void MQTTSNConnectionHandler::handleWillmsgupd(Client* client, MQTTSNPacket* pac
/*
* PINGREQ
*/
void MQTTSNConnectionHandler::handlePingreq(Client* client, MQTTSNPacket* packet)
void MQTTSNConnectionHandler::handlePingreq(Client* client,
MQTTSNPacket* packet)
{
if ( ( client->isSleep() || client->isAwake() ) && client->getClientSleepPacket() )
if ((client->isSleep() || client->isAwake())
&& client->getClientSleepPacket())
{
sendStoredPublish(client);
client->holdPingRequest();
@@ -297,7 +311,7 @@ void MQTTSNConnectionHandler::sendStoredPublish(Client* client)
{
MQTTGWPacket* msg = nullptr;
while ( ( msg = client->getClientSleepPacket() ) != nullptr )
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.

View File

@@ -62,7 +62,6 @@ typedef unsigned int uint32_t;
==================================*/
//#define DEBUG // print out log for debug
//#define DEBUG_NWSTACK // print out SensorNetwork log
#ifdef DEBUG
#define DEBUGLOG(...) printf(__VA_ARGS__)
#else

View File

@@ -21,17 +21,17 @@
using namespace MQTTSNGW;
using namespace std;
WirelessNodeId::WirelessNodeId()
:
_len{0},
_nodeId{0}
WirelessNodeId::WirelessNodeId() :
_len
{ 0 }, _nodeId
{ 0 }
{
}
WirelessNodeId::~WirelessNodeId()
{
if ( _nodeId )
if (_nodeId)
{
free(_nodeId);
}
@@ -39,12 +39,12 @@ WirelessNodeId::~WirelessNodeId()
void WirelessNodeId::setId(uint8_t* id, uint8_t len)
{
if ( _nodeId )
if (_nodeId)
{
free(_nodeId);
}
uint8_t* buf = (uint8_t*)malloc(len);
if ( buf )
uint8_t* buf = (uint8_t*) malloc(len);
if (buf)
{
memcpy(buf, id, len);
_len = len;
@@ -64,7 +64,7 @@ void WirelessNodeId::setId(WirelessNodeId* id)
bool WirelessNodeId::operator ==(WirelessNodeId& id)
{
if ( _len == id._len )
if (_len == id._len)
{
return memcmp(_nodeId, id._nodeId, _len) == 0;
}
@@ -77,16 +77,18 @@ bool WirelessNodeId::operator ==(WirelessNodeId& id)
/*
* Class MQTTSNGWEncapsulatedPacket
*/
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket()
: _mqttsn{0},
_ctrl{0}
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket() :
_mqttsn
{ 0 }, _ctrl
{ 0 }
{
}
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket(MQTTSNPacket* packet)
: _mqttsn{packet},
_ctrl{0}
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket(MQTTSNPacket* packet) :
_mqttsn
{ packet }, _ctrl
{ 0 }
{
}
@@ -96,7 +98,8 @@ MQTTSNGWEncapsulatedPacket::~MQTTSNGWEncapsulatedPacket()
/* Do not delete the MQTTSNPacket. MQTTSNPacket is deleted by delete Event */
}
int MQTTSNGWEncapsulatedPacket::unicast(SensorNetwork* network, SensorNetAddress* sendTo)
int MQTTSNGWEncapsulatedPacket::unicast(SensorNetwork* network,
SensorNetAddress* sendTo)
{
uint8_t buf[MQTTSNGW_MAX_PACKET_SIZE];
int len = serialize(buf);
@@ -109,8 +112,8 @@ int MQTTSNGWEncapsulatedPacket::serialize(uint8_t* buf)
buf[0] = _id._len + 3;
buf[1] = MQTTSN_ENCAPSULATED;
buf[2] = _ctrl;
memcpy( buf + 3, _id._nodeId, _id._len);
if ( _mqttsn )
memcpy(buf + 3, _id._nodeId, _id._len);
if (_mqttsn)
{
len = _mqttsn->getPacketLength();
memcpy(buf + buf[0], _mqttsn->getPacketData(), len);
@@ -118,9 +121,10 @@ int MQTTSNGWEncapsulatedPacket::serialize(uint8_t* buf)
return buf[0] + len;
}
int MQTTSNGWEncapsulatedPacket::desirialize(unsigned char* buf, unsigned short len)
int MQTTSNGWEncapsulatedPacket::desirialize(unsigned char* buf,
unsigned short len)
{
if ( _mqttsn )
if (_mqttsn)
{
delete _mqttsn;
_mqttsn = nullptr;

View File

@@ -60,6 +60,4 @@ private:
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWENCAPSULATEDPACKET_H_ */

View File

@@ -32,10 +32,10 @@ ForwarderList::ForwarderList()
ForwarderList::~ForwarderList()
{
if ( _head )
if (_head)
{
Forwarder* p = _head;
while ( p )
while (p)
{
Forwarder* next = p->_next;
delete p;
@@ -44,20 +44,18 @@ ForwarderList::~ForwarderList()
}
}
void ForwarderList::initialize(Gateway* gw)
{
/* Create Fowarders from clients.conf */
gw->getClientList()->setClientList(FORWARDER_TYPE);
}
Forwarder* ForwarderList::getForwarder(SensorNetAddress* addr)
{
Forwarder* p = _head;
while ( p )
while (p)
{
if ( p->_sensorNetAddr.isMatch(addr) )
if (p->_sensorNetAddr.isMatch(addr))
{
break;
}
@@ -66,19 +64,20 @@ Forwarder* ForwarderList::getForwarder(SensorNetAddress* addr)
return p;
}
Forwarder* ForwarderList::addForwarder(SensorNetAddress* addr, MQTTSNString* forwarderId)
Forwarder* ForwarderList::addForwarder(SensorNetAddress* addr,
MQTTSNString* forwarderId)
{
Forwarder* fdr = new Forwarder(addr, forwarderId);
if ( _head == nullptr )
if (_head == nullptr)
{
_head = fdr;
}
else
{
Forwarder* p = _head;
while ( p )
while (p)
{
if ( p->_next == nullptr )
if (p->_next == nullptr)
{
p->_next = fdr;
break;
@@ -112,10 +111,10 @@ Forwarder::Forwarder(SensorNetAddress* addr, MQTTSNString* forwarderId)
Forwarder::~Forwarder(void)
{
if ( _headClient )
if (_headClient)
{
ForwarderElement* p = _headClient;
while ( p )
while (p)
{
ForwarderElement* next = p->_next;
delete p;
@@ -136,11 +135,11 @@ void Forwarder::addClient(Client* client, WirelessNodeId* id)
client->setForwarder(this);
if ( p != nullptr )
if (p != nullptr)
{
while ( p )
while (p)
{
if ( p->_client == client )
if (p->_client == client)
{
client->setForwarder(this);
p->setWirelessNodeId(id);
@@ -156,7 +155,7 @@ void Forwarder::addClient(Client* client, WirelessNodeId* id)
fclient->setClient(client);
fclient->setWirelessNodeId(id);
if ( prev )
if (prev)
{
prev->_next = fclient;
}
@@ -171,9 +170,9 @@ Client* Forwarder::getClient(WirelessNodeId* id)
Client* cl = nullptr;
_mutex.lock();
ForwarderElement* p = _headClient;
while ( p )
while (p)
{
if ( *(p->_wirelessNodeId) == *id )
if (*(p->_wirelessNodeId) == *id)
{
cl = p->_client;
break;
@@ -197,9 +196,9 @@ WirelessNodeId* Forwarder::getWirelessNodeId(Client* client)
WirelessNodeId* nodeId = nullptr;
_mutex.lock();
ForwarderElement* p = _headClient;
while ( p )
while (p)
{
if ( p->_client == client )
if (p->_client == client)
{
nodeId = p->_wirelessNodeId;
break;
@@ -219,11 +218,11 @@ void Forwarder::eraseClient(Client* client)
_mutex.lock();
ForwarderElement* p = _headClient;
while ( p )
while (p)
{
if ( p->_client == client )
if (p->_client == client)
{
if ( prev )
if (prev)
{
prev->_next = p->_next;
}
@@ -251,10 +250,11 @@ SensorNetAddress* Forwarder::getSensorNetAddr(void)
* Class ForwardedClient
*/
ForwarderElement::ForwarderElement()
: _client{0}
, _wirelessNodeId{0}
, _next{0}
ForwarderElement::ForwarderElement() :
_client
{ 0 }, _wirelessNodeId
{ 0 }, _next
{ 0 }
{
}
@@ -273,7 +273,7 @@ void ForwarderElement::setClient(Client* client)
void ForwarderElement::setWirelessNodeId(WirelessNodeId* id)
{
if ( _wirelessNodeId == nullptr )
if (_wirelessNodeId == nullptr)
{
_wirelessNodeId = new WirelessNodeId();
}

View File

@@ -22,7 +22,6 @@
#include "MQTTSNGWEncapsulatedPacket.h"
#include "SensorNetwork.h"
namespace MQTTSNGW
{
class Gateway;
@@ -70,8 +69,8 @@ public:
private:
string _forwarderName;
SensorNetAddress _sensorNetAddr;
ForwarderElement* _headClient{nullptr};
Forwarder* _next {nullptr};
ForwarderElement* _headClient { nullptr };
Forwarder* _next { nullptr };
Mutex _mutex;
};
@@ -94,6 +93,4 @@ private:
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWFORWARDER_H_ */

View File

@@ -35,7 +35,7 @@ void Logmonitor::run()
while (true)
{
const char* data = getLog();
if ( *data == 0 )
if (*data == 0)
{
break;
}

View File

@@ -30,10 +30,10 @@ MessageIdTable::MessageIdTable()
MessageIdTable::~MessageIdTable()
{
_mutex.lock();
if ( _head != nullptr )
if (_head != nullptr)
{
MessageIdElement* p = _tail;
while ( p )
while (p)
{
MessageIdElement* pPrev = p;
delete p;
@@ -45,20 +45,21 @@ MessageIdTable::~MessageIdTable()
_mutex.unlock();
}
MessageIdElement* MessageIdTable::add(Aggregater* aggregater, Client* client, uint16_t clientMsgId)
MessageIdElement* MessageIdTable::add(Aggregater* aggregater, Client* client,
uint16_t clientMsgId)
{
if ( _cnt > _maxSize )
if (_cnt > _maxSize)
{
return nullptr;
}
MessageIdElement* elm = new MessageIdElement(0, client, clientMsgId);
if ( elm == nullptr )
if (elm == nullptr)
{
return nullptr;
}
_mutex.lock();
if ( _head == nullptr )
if (_head == nullptr)
{
elm->_msgId = aggregater->msgId();
_head = elm;
@@ -68,7 +69,7 @@ MessageIdElement* MessageIdTable::add(Aggregater* aggregater, Client* client, ui
else
{
MessageIdElement* p = find(client, clientMsgId);
if ( p == nullptr )
if (p == nullptr)
{
elm->_msgId = aggregater->msgId();
p = _tail;
@@ -90,9 +91,9 @@ MessageIdElement* MessageIdTable::add(Aggregater* aggregater, Client* client, ui
MessageIdElement* MessageIdTable::find(uint16_t msgId)
{
MessageIdElement* p = _head;
while ( p )
while (p)
{
if ( p->_msgId == msgId)
if (p->_msgId == msgId)
{
break;
}
@@ -104,9 +105,9 @@ MessageIdElement* MessageIdTable::find(uint16_t msgId)
MessageIdElement* MessageIdTable::find(Client* client, uint16_t clientMsgId)
{
MessageIdElement* p = _head;
while ( p )
while (p)
{
if ( p->_clientMsgId == clientMsgId && p->_client == client)
if (p->_clientMsgId == clientMsgId && p->_client == client)
{
break;
}
@@ -115,14 +116,13 @@ MessageIdElement* MessageIdTable::find(Client* client, uint16_t clientMsgId)
return p;
}
Client* MessageIdTable::getClientMsgId(uint16_t msgId, uint16_t* clientMsgId)
{
Client* clt = nullptr;
*clientMsgId = 0;
_mutex.lock();
MessageIdElement* p = find(msgId);
if ( p != nullptr )
if (p != nullptr)
{
clt = p->_client;
*clientMsgId = p->_clientMsgId;
@@ -142,15 +142,15 @@ void MessageIdTable::erase(uint16_t msgId)
void MessageIdTable::clear(MessageIdElement* elm)
{
if ( elm == nullptr )
if (elm == nullptr)
{
return;
}
if ( elm->_prev == nullptr )
if (elm->_prev == nullptr)
{
_head = elm->_next;
if ( _head == nullptr)
if (_head == nullptr)
{
_tail = nullptr;
}
@@ -165,7 +165,7 @@ void MessageIdTable::clear(MessageIdElement* elm)
else
{
elm->_prev->_next = elm->_next;
if ( elm->_next == nullptr )
if (elm->_next == nullptr)
{
_tail = elm->_prev;
}
@@ -179,12 +179,11 @@ void MessageIdTable::clear(MessageIdElement* elm)
}
}
uint16_t MessageIdTable::getMsgId(Client* client, uint16_t clientMsgId)
{
uint16_t msgId = 0;
MessageIdElement* p = find(client, clientMsgId);
if ( p != nullptr )
if (p != nullptr)
{
msgId = p->_msgId;
}
@@ -194,18 +193,20 @@ uint16_t MessageIdTable::getMsgId(Client* client, uint16_t clientMsgId)
/*===============================
* Class MessageIdElement
===============================*/
MessageIdElement::MessageIdElement(void)
: _msgId{0}
, _clientMsgId {0}
, _client {nullptr}
, _next {nullptr}
, _prev {nullptr}
MessageIdElement::MessageIdElement(void) :
_msgId
{ 0 }, _clientMsgId
{ 0 }, _client
{ nullptr }, _next
{ nullptr }, _prev
{ nullptr }
{
}
MessageIdElement::MessageIdElement(uint16_t msgId, Client* client, uint16_t clientMsgId)
: MessageIdElement()
MessageIdElement::MessageIdElement(uint16_t msgId, Client* client,
uint16_t clientMsgId) :
MessageIdElement()
{
_msgId = msgId;
_client = client;

View File

@@ -37,7 +37,8 @@ public:
MessageIdTable();
~MessageIdTable();
MessageIdElement* add(Aggregater* aggregater, Client* client, uint16_t clientMsgId);
MessageIdElement* add(Aggregater* aggregater, Client* client,
uint16_t clientMsgId);
Client* getClientMsgId(uint16_t msgId, uint16_t* clientMsgId);
uint16_t getMsgId(Client* client, uint16_t clientMsgId);
void erase(uint16_t msgId);
@@ -45,10 +46,10 @@ public:
private:
MessageIdElement* find(uint16_t msgId);
MessageIdElement* find(Client* client, uint16_t clientMsgId);
MessageIdElement* _head {nullptr};
MessageIdElement* _tail {nullptr};
int _cnt {0};
int _maxSize {MAX_MESSAGEID_TABLE_SIZE};
MessageIdElement* _head { nullptr };
MessageIdElement* _tail{ nullptr };
int _cnt { 0 };
int _maxSize { MAX_MESSAGEID_TABLE_SIZE };
Mutex _mutex;
};
@@ -72,7 +73,6 @@ private:
MessageIdElement* _prev;
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWMESSAGEIDTABLE_H_ */

View File

@@ -35,7 +35,7 @@ MQTTSNPacket::MQTTSNPacket(void)
MQTTSNPacket::MQTTSNPacket(MQTTSNPacket& packet)
{
_buf = (unsigned char*)malloc(packet._bufLen);
_buf = (unsigned char*) malloc(packet._bufLen);
if (_buf)
{
_bufLen = packet._bufLen;
@@ -74,13 +74,13 @@ int MQTTSNPacket::serialize(uint8_t* buf)
int MQTTSNPacket::desirialize(unsigned char* buf, unsigned short len)
{
if ( _buf )
if (_buf)
{
free(_buf);
}
_buf = (unsigned char*)calloc(len, sizeof(unsigned char));
if ( _buf )
_buf = (unsigned char*) calloc(len, sizeof(unsigned char));
if (_buf)
{
memcpy(_buf, buf, len);
_bufLen = len;
@@ -110,7 +110,7 @@ int MQTTSNPacket::recv(SensorNetwork* network)
int MQTTSNPacket::getType(void)
{
if ( _bufLen == 0 )
if (_bufLen == 0)
{
return 0;
}
@@ -121,13 +121,13 @@ int MQTTSNPacket::getType(void)
bool MQTTSNPacket::isQoSMinusPUBLISH(void)
{
if ( _bufLen == 0 )
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 ));
return ((_buf[p] == MQTTSN_PUBLISH) && ((_buf[p + 1] & 0x60) == 0x60));
}
unsigned char* MQTTSNPacket::getPacketData(void)
@@ -158,7 +158,8 @@ int MQTTSNPacket::setGWINFO(uint8_t gatewayId)
{
unsigned char buf[3];
int buflen = sizeof(buf);
int len = MQTTSNSerialize_gwinfo(buf, buflen, (unsigned char) gatewayId, 0, 0);
int len = MQTTSNSerialize_gwinfo(buf, buflen, (unsigned char) gatewayId, 0,
0);
return desirialize(buf, len);
}
@@ -167,14 +168,14 @@ int MQTTSNPacket::setConnect(void)
unsigned char buf[40];
int buflen = sizeof(buf);
MQTTSNPacket_connectData data;
data.clientID.cstring = (char*)"client01";
data.clientID.cstring = (char*) "client01";
int len = MQTTSNSerialize_connect(buf, buflen, &data);
return desirialize(buf, len);
}
bool MQTTSNPacket::isAccepted(void)
{
return ( getType() == MQTTSN_CONNACK) && (_buf[2] == MQTTSN_RC_ACCEPTED);
return (getType() == MQTTSN_CONNACK) && (_buf[2] == MQTTSN_RC_ACCEPTED);
}
int MQTTSNPacket::setCONNACK(uint8_t returnCode)
@@ -201,40 +202,45 @@ int MQTTSNPacket::setWILLMSGREQ(void)
return desirialize(buf, len);
}
int MQTTSNPacket::setREGISTER(uint16_t topicId, uint16_t msgId, MQTTSNString* topicName)
int MQTTSNPacket::setREGISTER(uint16_t topicId, uint16_t msgId,
MQTTSNString* topicName)
{
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
int buflen = sizeof(buf);
int len = MQTTSNSerialize_register(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
topicName);
int len = MQTTSNSerialize_register(buf, buflen, (unsigned short) topicId,
(unsigned short) msgId, topicName);
return desirialize(buf, len);
}
int MQTTSNPacket::setREGACK(uint16_t topicId, uint16_t msgId, uint8_t returnCode)
int MQTTSNPacket::setREGACK(uint16_t topicId, uint16_t msgId,
uint8_t returnCode)
{
unsigned char buf[7];
int buflen = sizeof(buf);
int len = MQTTSNSerialize_regack(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
(unsigned char) returnCode);
int len = MQTTSNSerialize_regack(buf, buflen, (unsigned short) topicId,
(unsigned short) msgId, (unsigned char) returnCode);
return desirialize(buf, len);
}
int MQTTSNPacket::setPUBLISH(uint8_t dup, int qos, uint8_t retained, uint16_t msgId, MQTTSN_topicid topic,
uint8_t* payload, uint16_t payloadlen)
int MQTTSNPacket::setPUBLISH(uint8_t dup, int qos, uint8_t retained,
uint16_t msgId, MQTTSN_topicid topic, uint8_t* payload,
uint16_t payloadlen)
{
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
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);
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);
}
int MQTTSNPacket::setPUBACK(uint16_t topicId, uint16_t msgId, uint8_t returnCode)
int MQTTSNPacket::setPUBACK(uint16_t topicId, uint16_t msgId,
uint8_t returnCode)
{
unsigned char buf[7];
int buflen = sizeof(buf);
int len = MQTTSNSerialize_puback(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
(unsigned char) returnCode);
int len = MQTTSNSerialize_puback(buf, buflen, (unsigned short) topicId,
(unsigned short) msgId, (unsigned char) returnCode);
return desirialize(buf, len);
}
@@ -262,7 +268,8 @@ int MQTTSNPacket::setPUBCOMP(uint16_t msgId)
return desirialize(buf, len);
}
int MQTTSNPacket::setSUBACK(int qos, uint16_t topicId, uint16_t msgId, uint8_t returnCode)
int MQTTSNPacket::setSUBACK(int qos, uint16_t topicId, uint16_t msgId,
uint8_t returnCode)
{
unsigned char buf[8];
int buflen = sizeof(buf);
@@ -323,13 +330,14 @@ int MQTTSNPacket::setPINGREQ(MQTTSNString* clientId)
{
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
int buflen = sizeof(buf);
int len = MQTTSNSerialize_pingreq( buf, buflen, *clientId);
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);
return MQTTSNDeserialize_searchgw((unsigned char*) radius,
(unsigned char*) _buf, _bufLen);
}
int MQTTSNPacket::getCONNECT(MQTTSNPacket_connectData* data)
@@ -342,9 +350,11 @@ int MQTTSNPacket::getCONNACK(uint8_t* returnCode)
return MQTTSNSerialize_connack(_buf, _bufLen, (int) *returnCode);
}
int MQTTSNPacket::getWILLTOPIC(int* willQoS, uint8_t* willRetain, MQTTSNString* willTopic)
int MQTTSNPacket::getWILLTOPIC(int* willQoS, uint8_t* willRetain,
MQTTSNString* willTopic)
{
return MQTTSNDeserialize_willtopic((int*) willQoS, (unsigned char*) willRetain, willTopic, _buf, _bufLen);
return MQTTSNDeserialize_willtopic((int*) willQoS,
(unsigned char*) willRetain, willTopic, _buf, _bufLen);
}
int MQTTSNPacket::getWILLMSG(MQTTSNString* willmsg)
@@ -352,28 +362,34 @@ int MQTTSNPacket::getWILLMSG(MQTTSNString* willmsg)
return MQTTSNDeserialize_willmsg(willmsg, _buf, _bufLen);
}
int MQTTSNPacket::getREGISTER(uint16_t* topicId, uint16_t* msgId, MQTTSNString* topicName)
int MQTTSNPacket::getREGISTER(uint16_t* topicId, uint16_t* msgId,
MQTTSNString* topicName)
{
return MQTTSNDeserialize_register((unsigned short*) topicId, (unsigned short*) msgId, topicName,
_buf, _bufLen);
return MQTTSNDeserialize_register((unsigned short*) topicId,
(unsigned short*) msgId, topicName, _buf, _bufLen);
}
int MQTTSNPacket::getREGACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode)
int MQTTSNPacket::getREGACK(uint16_t* topicId, uint16_t* msgId,
uint8_t* returnCode)
{
return MQTTSNDeserialize_regack((unsigned short*) topicId, (unsigned short*) msgId, (unsigned char*) returnCode, _buf, _bufLen);
return MQTTSNDeserialize_regack((unsigned short*) topicId,
(unsigned short*) msgId, (unsigned char*) returnCode, _buf, _bufLen);
}
int MQTTSNPacket::getPUBLISH(uint8_t* dup, int* qos, uint8_t* retained, uint16_t* msgId, MQTTSN_topicid* topic,
uint8_t** payload, int* payloadlen)
int MQTTSNPacket::getPUBLISH(uint8_t* dup, int* qos, uint8_t* retained,
uint16_t* msgId, MQTTSN_topicid* topic, uint8_t** payload,
int* payloadlen)
{
return MQTTSNDeserialize_publish((unsigned char*) dup, qos, (unsigned char*) retained, (unsigned short*) msgId,
topic, (unsigned char**) payload, (int*) payloadlen, _buf, _bufLen);
return MQTTSNDeserialize_publish((unsigned char*) dup, qos,
(unsigned char*) retained, (unsigned short*) msgId, topic,
(unsigned char**) payload, (int*) payloadlen, _buf, _bufLen);
}
int MQTTSNPacket::getPUBACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode)
int MQTTSNPacket::getPUBACK(uint16_t* topicId, uint16_t* msgId,
uint8_t* returnCode)
{
return MQTTSNDeserialize_puback((unsigned short*) topicId, (unsigned short*) msgId, (unsigned char*) returnCode,
_buf, _bufLen);
return MQTTSNDeserialize_puback((unsigned short*) topicId,
(unsigned short*) msgId, (unsigned char*) returnCode, _buf, _bufLen);
}
int MQTTSNPacket::getACK(uint16_t* msgId)
@@ -382,19 +398,22 @@ int MQTTSNPacket::getACK(uint16_t* msgId)
return MQTTSNDeserialize_ack(&type, (unsigned short*) msgId, _buf, _bufLen);
}
int MQTTSNPacket::getSUBSCRIBE(uint8_t* dup, int* qos, uint16_t* msgId, MQTTSN_topicid* topicFilter)
int MQTTSNPacket::getSUBSCRIBE(uint8_t* dup, int* qos, uint16_t* msgId,
MQTTSN_topicid* topicFilter)
{
return MQTTSNDeserialize_subscribe((unsigned char*) dup, qos, (unsigned short*) msgId, topicFilter, _buf, _bufLen);
return MQTTSNDeserialize_subscribe((unsigned char*) dup, qos,
(unsigned short*) msgId, topicFilter, _buf, _bufLen);
}
int MQTTSNPacket::getUNSUBSCRIBE(uint16_t* msgId, MQTTSN_topicid* topicFilter)
{
return MQTTSNDeserialize_unsubscribe((unsigned short*) msgId, topicFilter, _buf, _bufLen);
return MQTTSNDeserialize_unsubscribe((unsigned short*) msgId, topicFilter,
_buf, _bufLen);
}
int MQTTSNPacket::getPINGREQ(void)
{
if (getType() == MQTTSN_PINGRESP && _bufLen > 2 )
if (getType() == MQTTSN_PINGRESP && _bufLen > 2)
{
return _bufLen - 2;
}
@@ -405,13 +424,15 @@ int MQTTSNPacket::getDISCONNECT(uint16_t* duration)
{
int dur = 0;
int rc = MQTTSNDeserialize_disconnect(&dur, _buf, _bufLen);
*duration = (uint16_t)dur;
*duration = (uint16_t) dur;
return rc;
}
int MQTTSNPacket::getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain, MQTTSNString* willTopic)
int MQTTSNPacket::getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain,
MQTTSNString* willTopic)
{
return MQTTSNDeserialize_willtopicupd((int*) willQoS, (unsigned char*) willRetain, willTopic, _buf, _bufLen);
return MQTTSNDeserialize_willtopicupd((int*) willQoS,
(unsigned char*) willRetain, willTopic, _buf, _bufLen);
}
int MQTTSNPacket::getWILLMSGUPD(MQTTSNString* willMsg)
@@ -439,11 +460,11 @@ char* MQTTSNPacket::getMsgId(char* pbuf)
int value = 0;
int p = 0;
switch ( getType() )
switch (getType())
{
case MQTTSN_PUBLISH:
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
if ( _buf[p + 1] & 0x80 )
if (_buf[p + 1] & 0x80)
{
sprintf(pbuf, "+%02X%02X", _buf[p + 4], _buf[p + 5]);
}
@@ -475,7 +496,7 @@ char* MQTTSNPacket::getMsgId(char* pbuf)
sprintf(pbuf, " ");
break;
}
if ( strcmp(pbuf, " 0000") == 0 )
if (strcmp(pbuf, " 0000") == 0)
{
sprintf(pbuf, " ");
}
@@ -489,35 +510,35 @@ int MQTTSNPacket::getMsgId(void)
int msgId = 0;
char* ptr = 0;
switch ( getType() )
switch (getType())
{
case MQTTSN_PUBLISH:
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
ptr = (char*)_buf + p + 4;
msgId = readInt((char**)&ptr);
ptr = (char*) _buf + p + 4;
msgId = readInt((char**) &ptr);
break;
case MQTTSN_PUBACK:
case MQTTSN_REGISTER:
case MQTTSN_REGACK:
ptr = (char*)_buf + 4;
msgId = readInt((char**)&ptr);
ptr = (char*) _buf + 4;
msgId = readInt((char**) &ptr);
break;
case MQTTSN_PUBREC:
case MQTTSN_PUBREL:
case MQTTSN_PUBCOMP:
case MQTTSN_UNSUBACK:
ptr = (char*)_buf + 2;
msgId = readInt((char**)&ptr);
ptr = (char*) _buf + 2;
msgId = readInt((char**) &ptr);
break;
case MQTTSN_SUBSCRIBE:
case MQTTSN_UNSUBSCRIBE:
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
ptr = (char*)_buf + p + 2;
msgId = readInt((char**)&ptr);
ptr = (char*) _buf + p + 2;
msgId = readInt((char**) &ptr);
break;
case MQTTSN_SUBACK:
ptr = (char*)_buf + 5;
msgId = readInt((char**)&ptr);
ptr = (char*) _buf + 5;
msgId = readInt((char**) &ptr);
break;
default:
break;
@@ -531,20 +552,20 @@ void MQTTSNPacket::setMsgId(uint16_t msgId)
int p = 0;
//unsigned char* ptr = 0;
switch ( getType() )
switch (getType())
{
case MQTTSN_PUBLISH:
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
_buf[p + 4] = (unsigned char)(msgId / 256);
_buf[p + 5] = (unsigned char)(msgId % 256);
_buf[p + 4] = (unsigned char) (msgId / 256);
_buf[p + 5] = (unsigned char) (msgId % 256);
//ptr = _buf + p + 4;
//writeInt(&ptr, msgId);
break;
case MQTTSN_PUBACK:
case MQTTSN_REGISTER:
case MQTTSN_REGACK:
_buf[4] = (unsigned char)(msgId / 256);
_buf[5] = (unsigned char)(msgId % 256);
_buf[4] = (unsigned char) (msgId / 256);
_buf[5] = (unsigned char) (msgId % 256);
//ptr = _buf + 4;
//writeInt(&ptr, msgId);
break;
@@ -552,25 +573,25 @@ void MQTTSNPacket::setMsgId(uint16_t msgId)
case MQTTSN_PUBREL:
case MQTTSN_PUBCOMP:
case MQTTSN_UNSUBACK:
_buf[2] = (unsigned char)(msgId / 256);
_buf[3] = (unsigned char)(msgId % 256);
_buf[2] = (unsigned char) (msgId / 256);
_buf[3] = (unsigned char) (msgId % 256);
//ptr = _buf + 2;
//writeInt(&ptr, msgId);
break;
case MQTTSN_SUBSCRIBE:
case MQTTSN_UNSUBSCRIBE:
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
_buf[p + 2] = (unsigned char)(msgId / 256);
_buf[p + 3] = (unsigned char)(msgId % 256);
_buf[p + 2] = (unsigned char) (msgId / 256);
_buf[p + 3] = (unsigned char) (msgId % 256);
//ptr = _buf + p + 2;
//writeInt(&ptr, msgId);
break;
break;
case MQTTSN_SUBACK:
_buf[5] = (unsigned char)(msgId / 256);
_buf[6] = (unsigned char)(msgId % 256);
_buf[5] = (unsigned char) (msgId / 256);
_buf[6] = (unsigned char) (msgId % 256);
//ptr = _buf + 5;
//writeInt(&ptr, msgId);
break;
break;
default:
break;
}
@@ -580,5 +601,5 @@ bool MQTTSNPacket::isDuplicate(void)
{
int value = 0;
int p = MQTTSNPacket_decode(_buf, _bufLen, &value);
return ( _buf[p + 1] & 0x80 );
return (_buf[p + 1] & 0x80);
}

View File

@@ -53,7 +53,8 @@ public:
int setPUBREC(uint16_t msgId);
int setPUBREL(uint16_t msgId);
int setPUBCOMP(uint16_t msgId);
int setSUBACK(int qos, uint16_t topicId, uint16_t msgId, uint8_t returnCode);
int setSUBACK(int qos, uint16_t topicId, uint16_t msgId,
uint8_t returnCode);
int setUNSUBACK(uint16_t msgId);
int setPINGRESP(void);
int setDISCONNECT(uint16_t duration);
@@ -66,19 +67,23 @@ public:
int getSERCHGW(uint8_t* radius);
int getCONNECT(MQTTSNPacket_connectData* option);
int getCONNACK(uint8_t* returnCode);
int getWILLTOPIC(int* willQoS, uint8_t* willRetain, MQTTSNString* willTopic);
int getWILLTOPIC(int* willQoS, uint8_t* willRetain,
MQTTSNString* willTopic);
int getWILLMSG(MQTTSNString* willmsg);
int getREGISTER(uint16_t* topicId, uint16_t* msgId, MQTTSNString* topicName);
int getREGISTER(uint16_t* topicId, uint16_t* msgId,
MQTTSNString* topicName);
int getREGACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode);
int getPUBLISH(uint8_t* dup, int* qos, uint8_t* retained, uint16_t* msgId,
MQTTSN_topicid* topic, unsigned char** payload, int* payloadlen);
int getPUBACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode);
int getACK(uint16_t* msgId);
int getSUBSCRIBE(uint8_t* dup, int* qos, uint16_t* msgId, MQTTSN_topicid* topicFilter);
int getSUBSCRIBE(uint8_t* dup, int* qos, uint16_t* msgId,
MQTTSN_topicid* topicFilter);
int getUNSUBSCRIBE(uint16_t* msgId, MQTTSN_topicid* topicFilter);
int getPINGREQ(void);
int getDISCONNECT(uint16_t* duration);
int getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain, MQTTSNString* willTopic);
int getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain,
MQTTSNString* willTopic);
int getWILLMSGUPD(MQTTSNString* willMsg);
bool isAccepted(void);

View File

@@ -45,7 +45,7 @@ char* currentDateTime(void);
PacketHandleTask::PacketHandleTask(Gateway* gateway)
{
_gateway = gateway;
_gateway->attach((Thread*)this);
_gateway->attach((Thread*) this);
_mqttConnection = new MQTTGWConnectionHandler(_gateway);
_mqttPublish = new MQTTGWPublishHandler(_gateway);
_mqttSubscribe = new MQTTGWSubscribeHandler(_gateway);
@@ -61,32 +61,32 @@ PacketHandleTask::PacketHandleTask(Gateway* gateway)
*/
PacketHandleTask::~PacketHandleTask()
{
if ( _mqttConnection )
if (_mqttConnection)
{
delete _mqttConnection;
}
if ( _mqttPublish )
if (_mqttPublish)
{
delete _mqttPublish;
}
if ( _mqttSubscribe )
if (_mqttSubscribe)
{
delete _mqttSubscribe;
}
if ( _mqttsnConnection )
if (_mqttsnConnection)
{
delete _mqttsnConnection;
}
if ( _mqttsnPublish )
if (_mqttsnPublish)
{
delete _mqttsnPublish;
}
if ( _mqttsnSubscribe )
if (_mqttsnSubscribe)
{
delete _mqttsnSubscribe;
}
if ( _mqttsnAggrConnection )
if (_mqttsnAggrConnection)
{
delete _mqttsnAggrConnection;
}
@@ -124,7 +124,8 @@ void PacketHandleTask::run()
if (_advertiseTimer.isTimeup())
{
_mqttsnConnection->sendADVERTISE();
_advertiseTimer.start(_gateway->getGWParams()->keepAlive * 1000UL);
_advertiseTimer.start(
_gateway->getGWParams()->keepAlive * 1000UL);
}
/*------ Check Adapters Connect or PINGREQ ------*/
@@ -146,7 +147,7 @@ void PacketHandleTask::run()
DEBUGLOG(" PacketHandleTask gets %s %s from the client.\n", snPacket->getName(), snPacket->getMsgId(msgId));
if ( adpMgr->isAggregatedClient(client) )
if (adpMgr->isAggregatedClient(client))
{
aggregatePacketHandler(client, snPacket); // client is converted to Aggregater by BrokerSendTask
}
@@ -155,19 +156,17 @@ void PacketHandleTask::run()
transparentPacketHandler(client, snPacket);
}
/* Reset the Timer for PINGREQ. */
client->updateStatus(snPacket);
}
/*------ Handle Messages form Broker ---------*/
else if ( ev->getEventType() == EtBrokerRecv )
else if (ev->getEventType() == EtBrokerRecv)
{
client = ev->getClient();
brPacket = ev->getMQTTGWPacket();
DEBUGLOG(" PacketHandleTask gets %s %s from the broker.\n", brPacket->getName(), brPacket->getMsgId(msgId));
if ( client->isAggregater() )
if (client->isAggregater())
{
aggregatePacketHandler(client, brPacket);
}
@@ -180,9 +179,8 @@ void PacketHandleTask::run()
}
}
void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTSNPacket* packet)
void PacketHandleTask::aggregatePacketHandler(Client*client,
MQTTSNPacket* packet)
{
switch (packet->getType())
{
@@ -239,8 +237,8 @@ void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTSNPacket* packe
}
}
void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTGWPacket* packet)
void PacketHandleTask::aggregatePacketHandler(Client*client,
MQTTGWPacket* packet)
{
switch (packet->getType())
{
@@ -276,7 +274,8 @@ void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTGWPacket* packe
}
}
void PacketHandleTask::transparentPacketHandler(Client*client, MQTTSNPacket* packet)
void PacketHandleTask::transparentPacketHandler(Client*client,
MQTTSNPacket* packet)
{
switch (packet->getType())
{
@@ -333,8 +332,8 @@ void PacketHandleTask::transparentPacketHandler(Client*client, MQTTSNPacket* pac
}
}
void PacketHandleTask::transparentPacketHandler(Client*client, MQTTGWPacket* packet)
void PacketHandleTask::transparentPacketHandler(Client*client,
MQTTGWPacket* packet)
{
switch (packet->getType())
{

View File

@@ -42,9 +42,9 @@ class Timer;
/*=====================================
Class PacketHandleTask
=====================================*/
class PacketHandleTask : public Thread
class PacketHandleTask: public Thread
{
MAGIC_WORD_FOR_THREAD;
MAGIC_WORD_FOR_THREAD;
friend class MQTTGWAggregatePublishHandler;
friend class MQTTGWAggregateSubscribeHandler;
friend class MQTTSNAggregateConnectionHandler;
@@ -60,20 +60,19 @@ private:
void transparentPacketHandler(Client*client, MQTTSNPacket* packet);
void transparentPacketHandler(Client*client, MQTTGWPacket* packet);
Gateway* _gateway {nullptr};
Gateway* _gateway
{ nullptr };
Timer _advertiseTimer;
Timer _sendUnixTimer;
MQTTGWConnectionHandler* _mqttConnection {nullptr};
MQTTGWPublishHandler* _mqttPublish {nullptr};
MQTTGWSubscribeHandler* _mqttSubscribe {nullptr};
MQTTSNConnectionHandler* _mqttsnConnection {nullptr};
MQTTSNPublishHandler* _mqttsnPublish {nullptr};
MQTTSNSubscribeHandler* _mqttsnSubscribe {nullptr};
MQTTSNAggregateConnectionHandler* _mqttsnAggrConnection {nullptr};
MQTTGWConnectionHandler* _mqttConnection { nullptr };
MQTTGWPublishHandler* _mqttPublish { nullptr };
MQTTGWSubscribeHandler* _mqttSubscribe { nullptr };
MQTTSNConnectionHandler* _mqttsnConnection { nullptr };
MQTTSNPublishHandler* _mqttsnPublish { nullptr };
MQTTSNSubscribeHandler* _mqttsnSubscribe { nullptr };
MQTTSNAggregateConnectionHandler* _mqttsnAggrConnection { nullptr };
};
}
#endif /* MQTTSNGWPACKETHANDLETASK_H_ */

View File

@@ -61,11 +61,11 @@ Process::Process()
Process::~Process()
{
if (_rb )
if (_rb)
{
delete _rb;
}
if ( _rbsem )
if (_rbsem)
{
delete _rbsem;
}
@@ -88,17 +88,18 @@ void Process::initialize(int argc, char** argv)
int opt;
while ((opt = getopt(_argc, _argv, "f:")) != -1)
{
if ( opt == 'f' )
if (opt == 'f')
{
string config = string(optarg);
size_t pos = 0;
if ( (pos = config.find_last_of("/")) == string::npos )
if ((pos = config.find_last_of("/")) == string::npos)
{
_configFile = optarg;
}
else
{
_configFile = config.substr(pos + 1, config.size() - pos - 1);;
_configFile = config.substr(pos + 1, config.size() - pos - 1);
;
_configDir = config.substr(0, pos + 1);
}
}
@@ -128,7 +129,7 @@ void Process::putLog(const char* format, ...)
va_end(arg);
if (strlen(_rbdata))
{
if ( _log > 0 )
if (_log > 0)
{
_rb->put(_rbdata);
_rbsem->post();
@@ -212,7 +213,7 @@ const char* Process::getLog()
while ((len = _rb->get(_rbdata, PROCESS_LOG_BUFFER_SIZE)) == 0)
{
_rbsem->timedwait(1000);
if ( checkSignal() == SIGINT)
if (checkSignal() == SIGINT)
{
break;
}
@@ -279,7 +280,7 @@ void MultiTaskProcess::run(void)
try
{
while(true)
while (true)
{
if (theProcess->checkSignal() == SIGINT)
{
@@ -287,12 +288,10 @@ void MultiTaskProcess::run(void)
}
sleep(1);
}
}
catch(Exception* ex)
} catch (Exception* ex)
{
ex->writeMessage();
}
catch(...)
} catch (...)
{
throw;
}
@@ -405,13 +404,14 @@ const int Exception::getExceptionNo()
void Exception::writeMessage()
{
if (getExceptionNo() == 0 )
if (getExceptionNo() == 0)
{
WRITELOG("%s %s\n", currentDateTime(), what());
}
else
{
WRITELOG("%s:%-6d %s line %-4d %s() : %s\n", currentDateTime(), getExceptionNo(),
getFileName(), getLineNo(), getFunctionName(), what());
WRITELOG("%s:%-6d %s line %-4d %s() : %s\n", currentDateTime(),
getExceptionNo(), getFileName(), getLineNo(), getFunctionName(),
what());
}
}

View File

@@ -102,8 +102,8 @@ class Exception: public exception
public:
Exception(const string& message);
Exception(const int exNo, const string& message);
Exception(const int exNo, const string& message,
const char* file, const char* func, const int line);
Exception(const int exNo, const string& message, const char* file,
const char* func, const int line);
virtual ~Exception() throw ();
const char* getFileName();
const char* getFunctionName();
@@ -120,7 +120,6 @@ private:
int _line;
};
/*=====================================
Class QueElement
====================================*/
@@ -175,10 +174,10 @@ public:
void pop(void)
{
if ( _head )
if (_head)
{
QueElement<T>* head = _head;
if ( _head == _tail )
if (_head == _tail)
{
_head = _tail = nullptr;
}
@@ -195,7 +194,7 @@ public:
T* front(void)
{
{
if ( _head )
if (_head)
{
return _head->_element;
}
@@ -208,12 +207,12 @@ public:
int post(T* t)
{
if ( t && ( _maxSize == 0 || _cnt < _maxSize ))
if (t && (_maxSize == 0 || _cnt < _maxSize))
{
QueElement<T>* elm = new QueElement<T>(t);
if ( _head )
if (_head)
{
if ( _tail == _head )
if (_tail == _head)
{
elm->_prev = _tail;
_tail = elm;
@@ -262,8 +261,9 @@ private:
#define TREE23_BI_NODE (2)
#define TREE23_TRI_NODE (3)
template <typename K, typename V>
class Tree23Elm{
template<typename K, typename V>
class Tree23Elm
{
template<typename T, typename U> friend class Tree23;
public:
Tree23Elm()
@@ -292,9 +292,9 @@ private:
V* _val;
};
template <typename K, typename V>
class Tree23Node{
template<typename K, typename V>
class Tree23Node
{
template<typename S, typename W> friend class Tree23;
public:
Tree23Node(const int type)
@@ -320,7 +320,8 @@ public:
_left = _midle = _right = NULL;
}
Tree23Node(const int type, Tree23Elm<K, V>* telm, Tree23Node<K, V>* left, Tree23Node<K, V>* right)
Tree23Node(const int type, Tree23Elm<K, V>* telm, Tree23Node<K, V>* left,
Tree23Node<K, V>* right)
{
_type = type;
_telm0 = telm;
@@ -330,7 +331,9 @@ public:
_right = right;
}
Tree23Node(const int type, Tree23Elm<K, V>* telm0, Tree23Elm<K, V>* telm1, Tree23Node<K, V>* left, Tree23Node<K, V>* midle, Tree23Node<K, V>* right)
Tree23Node(const int type, Tree23Elm<K, V>* telm0, Tree23Elm<K, V>* telm1,
Tree23Node<K, V>* left, Tree23Node<K, V>* midle,
Tree23Node<K, V>* right)
{
_type = type;
_telm0 = telm0;
@@ -354,8 +357,9 @@ private:
Tree23Node<K, V>* _right;
};
template <typename K, typename V>
class Tree23{
template<typename K, typename V>
class Tree23
{
public:
Tree23()
{
@@ -364,7 +368,7 @@ public:
~Tree23()
{
if ( _root )
if (_root)
{
delete _root;
}
@@ -372,28 +376,28 @@ public:
void add(K* key, V* val)
{
_root = add( _root, new Tree23Elm<K, V>(key, val));
_root = add(_root, new Tree23Elm<K, V>(key, val));
_root->_type = abs(_root->_type);
}
Tree23Node<K, V>* add(Tree23Node<K, V>* n, Tree23Elm<K, V>* elm)
{
if ( n == 0 )
if (n == 0)
{
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, elm);
}
int cmp0 = elm->compare(n->_telm0);
int cmp1 = 0;
switch ( n->_type )
switch (n->_type)
{
case 2:
if ( cmp0 < 0 )
if (cmp0 < 0)
{
n->_left = add(n->_left, elm);
return addLeft2(n);
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
n->_telm0 = elm;
return n;
@@ -406,22 +410,22 @@ public:
break;
case 3:
cmp1 = elm->compare(n->_telm1);
if ( cmp0 < 0 )
if (cmp0 < 0)
{
n->_left = add(n->_left, elm);
return addLeft3(n);
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
n->_telm0 = elm;
return n;
}
else if ( cmp1 < 0 )
else if (cmp1 < 0)
{
n->_midle = add(n->_midle, elm);
return addMidle3(n);
}
else if ( cmp1 == 0 )
else if (cmp1 == 0)
{
n->_telm1 = elm;
return n;
@@ -441,7 +445,7 @@ public:
void remove(K* k)
{
_root = remove(_root, k);
if ( _root != NULL && _root->_type == TREE23_DELETE_ACTIVE )
if (_root != NULL && _root->_type == TREE23_DELETE_ACTIVE)
{
_root = _root->_midle;
}
@@ -449,23 +453,23 @@ public:
Tree23Node<K, V>* remove(Tree23Node<K, V>* node, K* k)
{
if ( node == NULL )
if (node == NULL)
{
return NULL;
}
int cmp0 = k->compare(node->_telm0->_key);
int cmp1 = 0;
switch ( node->_type )
switch (node->_type)
{
case 2:
if ( cmp0 < 0 )
if (cmp0 < 0)
{
node->_left = remove( node->_left, k);
node->_left = remove(node->_left, k);
return removeLeft2(node);
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
if ( node->_left == NULL)
if (node->_left == NULL)
{
return new Tree23Node<K, V>(TREE23_DELETE_ACTIVE);
}
@@ -481,14 +485,14 @@ public:
}
case 3:
cmp1 = k->compare(node->_telm1->_key);
if ( cmp0 < 0 )
if (cmp0 < 0)
{
node->_left = remove(node->_left, k);
return removeLeft3(node);
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
if ( node->_left == NULL )
if (node->_left == NULL)
{
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1);
}
@@ -497,14 +501,14 @@ public:
node->_telm0 = maxLeft;
return removeLeft3(node);
}
else if ( cmp1 < 0 )
else if (cmp1 < 0)
{
node->_midle = remove(node->_midle, k);
return removeMidle3(node);
}
else if ( cmp1 == 0 )
else if (cmp1 == 0)
{
if ( node->_midle == NULL )
if (node->_midle == NULL)
{
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0);
}
@@ -534,8 +538,9 @@ public:
switch (node->_type)
{
case 2:
if ( cmp0 < 0 ) node = node->_left;
else if ( cmp0 == 0 )
if (cmp0 < 0)
node = node->_left;
else if (cmp0 == 0)
{
return true;
}
@@ -546,19 +551,19 @@ public:
break;
case 3:
cmp1 = key->compare(node->_telm1->_key);
if ( cmp0 < 0 )
if (cmp0 < 0)
{
node = node->_left;
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
return true;
}
else if ( cmp1 < 0 )
else if (cmp1 < 0)
{
node = node->_midle;
}
else if ( cmp1 == 0 )
else if (cmp1 == 0)
{
return true;
}
@@ -574,7 +579,6 @@ public:
return false;
}
V* getVal(K* key)
{
Tree23Node<K, V>* node = _root;
@@ -585,11 +589,11 @@ public:
switch (node->_type)
{
case 2:
if ( cmp0 < 0 )
if (cmp0 < 0)
{
node = node->_left;
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
return node->_telm0->_val;
}
@@ -600,19 +604,19 @@ public:
break;
case 3:
cmp1 = key->compare(node->_telm1->_key);
if ( cmp0 < 0 )
if (cmp0 < 0)
{
node = node->_left;
}
else if ( cmp0 == 0 )
else if (cmp0 == 0)
{
return node->_telm0->_val;
}
else if ( cmp1 < 0 )
else if (cmp1 < 0)
{
node = node->_midle;
}
else if ( cmp1 == 0 )
else if (cmp1 == 0)
{
return node->_telm1->_val;
}
@@ -632,9 +636,10 @@ private:
Tree23Node<K, V>* addLeft2(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_left;
if ( n != NULL && n->_type == TREE23_INSERT_ACTIVE )
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE)
{
return new Tree23Node<K, V>(TREE23_TRI_NODE, n->_telm0, node->_telm0, n->_left, n->_right, node->_right);
return new Tree23Node<K, V>(TREE23_TRI_NODE, n->_telm0,
node->_telm0, n->_left, n->_right, node->_right);
}
return node;
}
@@ -642,11 +647,13 @@ private:
Tree23Node<K, V>* addLeft3(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_left;
if ( n != NULL && n->_type == TREE23_INSERT_ACTIVE)
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE)
{
n->_type = TREE23_BI_NODE;
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, node->_midle, node->_right);
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm0, n, nn);
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE,
node->_telm1, node->_midle, node->_right);
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm0, n,
nn);
}
return node;
}
@@ -656,7 +663,8 @@ private:
Tree23Node<K, V>* n = node->_right;
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE)
{
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, n->_telm0, node->_left, n->_left, n->_right);
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0,
n->_telm0, node->_left, n->_left, n->_right);
}
return node;
}
@@ -664,10 +672,13 @@ private:
Tree23Node<K, V>* addRight3(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_right;
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE) {
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE)
{
n->_type = TREE23_BI_NODE;
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, node->_left, node->_midle);
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm1, nn, n);
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE,
node->_telm0, node->_left, node->_midle);
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm1, nn,
n);
}
return node;
}
@@ -675,16 +686,17 @@ private:
Tree23Node<K, V>* addMidle3(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_midle;
if ( n != NULL && n->_type == TREE23_INSERT_ACTIVE )
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE)
{
n->_left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, node->_left, n->_left);
n->_right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, n->_right, node->_right);
n->_left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
node->_left, n->_left);
n->_right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1,
n->_right, node->_right);
return n;
}
return node;
}
Tree23Node<K, V>* removeMax(Tree23Node<K, V>* node, Tree23Elm<K, V>* elm)
{
if (node->_right == NULL)
@@ -719,26 +731,29 @@ private:
return NULL;
}
Tree23Node<K, V>* removeLeft2(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_left;
if ( n != NULL && n->_type == TREE23_DELETE_ACTIVE )
if (n != NULL && n->_type == TREE23_DELETE_ACTIVE)
{
Tree23Node<K, V>* r = node->_right;
Tree23Node<K, V>* midle;
Tree23Node<K, V>* left;
Tree23Node<K, V>* right;
switch ( r->_type )
switch (r->_type)
{
case 2:
midle = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, r->_telm0, n->_midle, r->_left, r->_right);
midle = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0,
r->_telm0, n->_midle, r->_left, r->_right);
return new Tree23Node<K, V>(TREE23_DELETE_ACTIVE, midle);
case 3:
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, n->_midle, r->_left);
right = new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm1, r->_midle, r->_right);
return new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm0, left, right);
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
n->_midle, r->_left);
right = new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm1,
r->_midle, r->_right);
return new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm0, left,
right);
default:
break;
}
@@ -749,7 +764,7 @@ private:
Tree23Node<K, V>* removeRight2(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_right;
if ( n != NULL && n->_type == TREE23_DELETE_ACTIVE )
if (n != NULL && n->_type == TREE23_DELETE_ACTIVE)
{
Tree23Node<K, V>* l = node->_left;
Tree23Node<K, V>* midle;
@@ -759,12 +774,16 @@ private:
switch (l->_type)
{
case 2:
midle = new Tree23Node<K, V>(TREE23_TRI_NODE, l->_telm0, node->_telm0, l->_left, l->_right, n->_midle);
midle = new Tree23Node<K, V>(TREE23_TRI_NODE, l->_telm0,
node->_telm0, l->_left, l->_right, n->_midle);
return new Tree23Node<K, V>(-1, midle);
case 3:
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l->_right, n->_midle);
left = new Tree23Node<K, V>(TREE23_BI_NODE, l->_telm0, l->_left, l->_midle);
return new Tree23Node<K, V>(TREE23_BI_NODE, l->_telm1, left, right);
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
l->_right, n->_midle);
left = new Tree23Node<K, V>(TREE23_BI_NODE, l->_telm0, l->_left,
l->_midle);
return new Tree23Node<K, V>(TREE23_BI_NODE, l->_telm1, left,
right);
default:
break;
}
@@ -775,21 +794,27 @@ private:
Tree23Node<K, V>* removeLeft3(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_left;
if ( n != NULL && n->_type == TREE23_DELETE_ACTIVE )
if (n != NULL && n->_type == TREE23_DELETE_ACTIVE)
{
Tree23Node<K, V>* m = node->_midle;
Tree23Node<K, V>* r = node->_right;
Tree23Node<K, V>* left;
Tree23Node<K, V>* midle;
switch (m->_type) {
switch (m->_type)
{
case 2:
left = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, m->_telm0, n->_midle, m->_left, m->_right);
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, left, r);
left = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0,
m->_telm0, n->_midle, m->_left, m->_right);
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, left,
r);
case 3:
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, n->_midle, m->_left);
midle = new Tree23Node<K, V>(TREE23_BI_NODE, m->_telm1, m->_midle, m->_right);
return new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0, node->_telm1, left, midle, r);
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
n->_midle, m->_left);
midle = new Tree23Node<K, V>(TREE23_BI_NODE, m->_telm1,
m->_midle, m->_right);
return new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0,
node->_telm1, left, midle, r);
default:
break;
}
@@ -800,7 +825,7 @@ private:
Tree23Node<K, V>* removeMidle3(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_midle;
if ( n != NULL && n->_type == TREE23_DELETE_ACTIVE )
if (n != NULL && n->_type == TREE23_DELETE_ACTIVE)
{
Tree23Node<K, V>* l = node->_left;
Tree23Node<K, V>* r = node->_right;
@@ -809,12 +834,17 @@ private:
switch (r->_type)
{
case 2:
right = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm1, r->_telm0, n->_midle, r->_left, r->_right);
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l, right);
right = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm1,
r->_telm0, n->_midle, r->_left, r->_right);
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l,
right);
case 3:
midle = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, n->_midle, r->_left);
right = new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm1, r->_midle, r->_right);
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, r->_telm0, l, midle, right);
midle = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1,
n->_midle, r->_left);
right = new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm1,
r->_midle, r->_right);
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0,
r->_telm0, l, midle, right);
default:
break;
}
@@ -825,7 +855,7 @@ private:
Tree23Node<K, V>* removeRight3(Tree23Node<K, V>* node)
{
Tree23Node<K, V>* n = node->_right;
if ( n != NULL && n->_type == TREE23_DELETE_ACTIVE )
if (n != NULL && n->_type == TREE23_DELETE_ACTIVE)
{
Tree23Node<K, V>* l = node->_left;
Tree23Node<K, V>* m = node->_midle;
@@ -834,12 +864,17 @@ private:
switch (m->_type)
{
case 2:
right = new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0, node->_telm1, m->_left, m->_right, n->_midle);
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l, right);
right = new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0,
node->_telm1, m->_left, m->_right, n->_midle);
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l,
right);
case 3:
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, m->_right, n->_midle);
midle = new Tree23Node<K, V>(TREE23_BI_NODE, m->_telm0, m->_left, m->_midle);
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, m->_telm1, l, midle, right);
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1,
m->_right, n->_midle);
midle = new Tree23Node<K, V>(TREE23_BI_NODE, m->_telm0,
m->_left, m->_midle);
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0,
m->_telm1, l, midle, right);
default:
break;
}
@@ -847,14 +882,13 @@ private:
return node;
}
Tree23Node<K, V>* _root;
};
/*=====================================
Class List
=====================================*/
template <typename T>
template<typename T>
class ListElm
{
template<typename U> friend class List;
@@ -873,18 +907,23 @@ public:
{
return _elm;
}
~ListElm(){}
~ListElm()
{
}
private:
ListElm<T>* getNext(void){return _next;}
ListElm<T>* getNext(void)
{
return _next;
}
T* _elm;
ListElm<T>* _prev;
ListElm<T>* _next;
};
template <typename T>
class List{
template<typename T>
class List
{
public:
List()
{
@@ -899,11 +938,11 @@ public:
int add(T* t)
{
ListElm<T>* elm = new ListElm<T>(t);
if ( elm == nullptr )
if (elm == nullptr)
{
return 0;
}
if ( _head == nullptr )
if (_head == nullptr)
{
_head = elm;
_tail = elm;
@@ -920,13 +959,13 @@ public:
void erase(ListElm<T>* elm)
{
if ( _head == elm )
if (_head == elm)
{
_head = elm->_next;
_size--;
delete elm;
}
else if ( _tail == elm )
else if (_tail == elm)
{
_tail = elm->_prev;
elm->_prev->_next = nullptr;
@@ -944,7 +983,7 @@ public:
void clear(void)
{
ListElm<T>* p = _head;
while ( p )
while (p)
{
ListElm<T>* q = p->_next;
delete p;
@@ -970,14 +1009,12 @@ public:
return _size;
}
private:
ListElm<T>* _head;
ListElm<T>* _tail;
int _size;
};
extern Process* theProcess;
extern MultiTaskProcess* theMultiTaskProcess;

View File

@@ -35,7 +35,8 @@ MQTTSNPublishHandler::~MQTTSNPublishHandler()
}
MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client,
MQTTSNPacket* packet)
{
uint8_t dup;
int qos;
@@ -48,28 +49,30 @@ MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket*
char shortTopic[2];
if ( !_gateway->getAdapterManager()->getQoSm1Proxy()->isActive() )
if (!_gateway->getAdapterManager()->getQoSm1Proxy()->isActive())
{
if ( client->isQoSm1() )
if (client->isQoSm1())
{
_gateway->getAdapterManager()->getQoSm1Proxy()->savePacket(client, packet);
_gateway->getAdapterManager()->getQoSm1Proxy()->savePacket(client,
packet);
return nullptr;
}
}
if ( packet->getPUBLISH(&dup, &qos, &retained, &msgId, &topicid, &payload, &payloadlen) ==0 )
if (packet->getPUBLISH(&dup, &qos, &retained, &msgId, &topicid, &payload,
&payloadlen) == 0)
{
return nullptr;
}
pub.msgId = msgId;
pub.header.bits.dup = dup;
pub.header.bits.qos = ( qos == 3 ? 0 : qos );
pub.header.bits.qos = (qos == 3 ? 0 : qos);
pub.header.bits.retain = retained;
Topic* topic = nullptr;
if( topicid.type == MQTTSN_TOPIC_TYPE_SHORT )
if (topicid.type == MQTTSN_TOPIC_TYPE_SHORT)
{
shortTopic[0] = topicid.data.short_name[0];
shortTopic[1] = topicid.data.short_name[1];
@@ -79,56 +82,61 @@ MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket*
else
{
topic = client->getTopics()->getTopicById(&topicid);
if ( !topic )
if (!topic)
{
topic = _gateway->getTopics()->getTopicById(&topicid);
if ( topic )
if (topic)
{
topic = client->getTopics()->add(topic->getTopicName()->c_str(), topic->getTopicId());
topic = client->getTopics()->add(topic->getTopicName()->c_str(),
topic->getTopicId());
}
}
if( !topic && qos == 3 )
if (!topic && qos == 3)
{
WRITELOG("%s Invalid TopicId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
WRITELOG("%s Invalid TopicId.%s %s\n", ERRMSG_HEADER,
client->getClientId(), ERRMSG_FOOTER);
return nullptr;
}
if ( ( qos == 0 || qos == 3 ) && msgId > 0 )
if ((qos == 0 || qos == 3) && msgId > 0)
{
WRITELOG("%s Invalid MsgId.%s %s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
WRITELOG("%s Invalid MsgId.%s %s\n", ERRMSG_HEADER,
client->getClientId(), ERRMSG_FOOTER);
return nullptr;
}
if( !topic && msgId && qos > 0 && qos < 3 )
if (!topic && msgId && qos > 0 && qos < 3)
{
/* Reply PubAck with INVALID_TOPIC_ID to the client */
MQTTSNPacket* pubAck = new MQTTSNPacket();
pubAck->setPUBACK( topicid.data.id, msgId, MQTTSN_RC_REJECTED_INVALID_TOPIC_ID);
pubAck->setPUBACK(topicid.data.id, msgId,
MQTTSN_RC_REJECTED_INVALID_TOPIC_ID);
Event* ev1 = new Event();
ev1->setClientSendEvent(client, pubAck);
_gateway->getClientSendQue()->post(ev1);
return nullptr;
}
if ( topic )
if (topic)
{
pub.topic = (char*)topic->getTopicName()->data();
pub.topic = (char*) topic->getTopicName()->data();
pub.topiclen = topic->getTopicName()->length();
}
}
/* Save a msgId & a TopicId pare for PUBACK */
if( msgId && qos > 0 && qos < 3)
if (msgId && qos > 0 && qos < 3)
{
client->setWaitedPubTopicId(msgId, topicid.data.id, topicid.type);
}
pub.payload = (char*)payload;
pub.payload = (char*) payload;
pub.payloadlen = payloadlen;
MQTTGWPacket* publish = new MQTTGWPacket();
publish->setPUBLISH(&pub);
if ( _gateway->getAdapterManager()->isAggregaterActive() && client->isAggregated() )
if (_gateway->getAdapterManager()->isAggregaterActive()
&& client->isAggregated())
{
return publish;
}
@@ -147,16 +155,16 @@ void MQTTSNPublishHandler::handlePuback(Client* client, MQTTSNPacket* packet)
uint16_t msgId;
uint8_t rc;
if ( client->isActive() )
if (client->isActive())
{
if ( packet->getPUBACK(&topicId, &msgId, &rc) == 0 )
if (packet->getPUBACK(&topicId, &msgId, &rc) == 0)
{
return;
}
if ( rc == MQTTSN_RC_ACCEPTED)
if (rc == MQTTSN_RC_ACCEPTED)
{
if ( !_gateway->getAdapterManager()->getAggregater()->isActive() )
if (!_gateway->getAdapterManager()->getAggregater()->isActive())
{
MQTTGWPacket* pubAck = new MQTTGWPacket();
pubAck->setAck(PUBACK, msgId);
@@ -165,20 +173,21 @@ void MQTTSNPublishHandler::handlePuback(Client* client, MQTTSNPacket* packet)
_gateway->getBrokerSendQue()->post(ev1);
}
}
else if ( rc == MQTTSN_RC_REJECTED_INVALID_TOPIC_ID)
else if (rc == MQTTSN_RC_REJECTED_INVALID_TOPIC_ID)
{
WRITELOG(" PUBACK %d : Invalid Topic ID\n", msgId);
}
}
}
void MQTTSNPublishHandler::handleAck(Client* client, MQTTSNPacket* packet, uint8_t packetType)
void MQTTSNPublishHandler::handleAck(Client* client, MQTTSNPacket* packet,
uint8_t packetType)
{
uint16_t msgId;
if ( client->isActive() )
if (client->isActive())
{
if ( packet->getACK(&msgId) == 0 )
if (packet->getACK(&msgId) == 0)
{
return;
}
@@ -194,12 +203,13 @@ void MQTTSNPublishHandler::handleRegister(Client* client, MQTTSNPacket* packet)
{
uint16_t id;
uint16_t msgId;
MQTTSNString topicName = MQTTSNString_initializer;;
MQTTSNString topicName = MQTTSNString_initializer;
;
MQTTSN_topicid topicid;
if ( client->isActive() || client->isAwake())
if (client->isActive() || client->isAwake())
{
if ( packet->getREGISTER(&id, &msgId, &topicName) == 0 )
if (packet->getREGISTER(&id, &msgId, &topicName) == 0)
{
return;
}
@@ -218,28 +228,30 @@ void MQTTSNPublishHandler::handleRegister(Client* client, MQTTSNPacket* packet)
}
}
void MQTTSNPublishHandler::handleRegAck( Client* client, MQTTSNPacket* packet)
void MQTTSNPublishHandler::handleRegAck(Client* client, MQTTSNPacket* packet)
{
uint16_t id;
uint16_t msgId;
uint8_t rc;
if ( client->isActive() || client->isAwake())
if (client->isActive() || client->isAwake())
{
if ( packet->getREGACK(&id, &msgId, &rc) == 0 )
if (packet->getREGACK(&id, &msgId, &rc) == 0)
{
return;
}
MQTTSNPacket* regAck = client->getWaitREGACKPacketList()->getPacket(msgId);
MQTTSNPacket* regAck = client->getWaitREGACKPacketList()->getPacket(
msgId);
if ( regAck != nullptr )
if (regAck != nullptr)
{
client->getWaitREGACKPacketList()->erase(msgId);
Event* ev = new Event();
ev->setClientSendEvent(client, regAck);
_gateway->getClientSendQue()->post(ev);
}
if (client->isHoldPingReqest() && client->getWaitREGACKPacketList()->getCount() == 0 )
if (client->isHoldPingReqest()
&& client->getWaitREGACKPacketList()->getCount() == 0)
{
/* send PINGREQ to the broker */
client->resetPingRequest();
@@ -253,24 +265,26 @@ void MQTTSNPublishHandler::handleRegAck( Client* client, MQTTSNPacket* packet)
}
void MQTTSNPublishHandler::handleAggregatePublish(Client* client, MQTTSNPacket* packet)
void MQTTSNPublishHandler::handleAggregatePublish(Client* client,
MQTTSNPacket* packet)
{
int msgId = 0;
MQTTGWPacket* publish = handlePublish(client, packet);
if ( publish != nullptr )
if (publish != nullptr)
{
if ( publish->getMsgId() > 0 )
if (publish->getMsgId() > 0)
{
if ( packet->isDuplicate() )
if (packet->isDuplicate())
{
msgId = _gateway->getAdapterManager()->getAggregater()->getMsgId(client, packet->getMsgId());
msgId =
_gateway->getAdapterManager()->getAggregater()->getMsgId(
client, packet->getMsgId());
}
else
{
msgId = _gateway->getAdapterManager()->getAggregater()->addMessageIdTable(client, packet->getMsgId());
msgId =
_gateway->getAdapterManager()->getAggregater()->addMessageIdTable(
client, packet->getMsgId());
}
publish->setMsgId(msgId);
}
@@ -280,13 +294,14 @@ void MQTTSNPublishHandler::handleAggregatePublish(Client* client, MQTTSNPacket*
}
}
void MQTTSNPublishHandler::handleAggregateAck(Client* client, MQTTSNPacket* packet, int type)
void MQTTSNPublishHandler::handleAggregateAck(Client* client,
MQTTSNPacket* packet, int type)
{
if ( type == MQTTSN_PUBREC )
if (type == MQTTSN_PUBREC)
{
uint16_t msgId;
if ( packet->getACK(&msgId) == 0 )
if (packet->getACK(&msgId) == 0)
{
return;
}

View File

@@ -31,7 +31,7 @@ public:
void handlePuback(Client* client, MQTTSNPacket* packet);
void handleAck(Client* client, MQTTSNPacket* packet, uint8_t packetType);
void handleRegister(Client* client, MQTTSNPacket* packet);
void handleRegAck( Client* client, MQTTSNPacket* packet);
void handleRegAck(Client* client, MQTTSNPacket* packet);
void handleAggregatePublish(Client* client, MQTTSNPacket* packet);
void handleAggregateAck(Client* client, MQTTSNPacket* packet, int type);

View File

@@ -21,13 +21,13 @@
#include <string>
#include <string.h>
using namespace MQTTSNGW;
/*=====================================
Class QoSm1Proxy
=====================================*/
QoSm1Proxy:: QoSm1Proxy(Gateway* gw) : Adapter(gw)
QoSm1Proxy::QoSm1Proxy(Gateway* gw) :
Adapter(gw)
{
_gateway = gw;
}
@@ -37,10 +37,9 @@ QoSm1Proxy::~QoSm1Proxy(void)
}
void QoSm1Proxy::initialize(char* gwName)
{
if ( _gateway->hasSecureConnection() )
if (_gateway->hasSecureConnection())
{
_isSecure = true;
}
@@ -54,7 +53,6 @@ void QoSm1Proxy::initialize(char* gwName)
_isActive = true;
}
bool QoSm1Proxy::isActive(void)
{
return _isActive;

View File

@@ -29,7 +29,7 @@ class MQTTSNPacket;
/*=====================================
Class QoSm1Proxy
=====================================*/
class QoSm1Proxy : public Adapter
class QoSm1Proxy: public Adapter
{
public:
QoSm1Proxy(Gateway* gw);
@@ -41,13 +41,10 @@ public:
private:
Gateway* _gateway;
bool _isActive {false};
bool _isSecure {false};
bool _isActive { false };
bool _isSecure { false };
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWQOSM1PROXY_H_ */

View File

@@ -34,7 +34,8 @@ MQTTSNSubscribeHandler::~MQTTSNSubscribeHandler()
}
MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPacket* packet)
MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client,
MQTTSNPacket* packet)
{
uint8_t dup;
int qos;
@@ -46,26 +47,27 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
Event* ev1;
Event* evsuback;
if ( packet->getSUBSCRIBE(&dup, &qos, &msgId, &topicFilter) == 0 )
if (packet->getSUBSCRIBE(&dup, &qos, &msgId, &topicFilter) == 0)
{
return nullptr;
}
if ( msgId == 0 )
if (msgId == 0)
{
return nullptr;
}
if ( topicFilter.type == MQTTSN_TOPIC_TYPE_PREDEFINED )
if (topicFilter.type == MQTTSN_TOPIC_TYPE_PREDEFINED)
{
topic = client->getTopics()->getTopicById(&topicFilter);
if ( !topic )
if (!topic)
{
topic = _gateway->getTopics()->getTopicById(&topicFilter);
if ( topic )
if (topic)
{
topic = client->getTopics()->add(topic->getTopicName()->c_str(), topic->getTopicId());
topic = client->getTopics()->add(topic->getTopicName()->c_str(),
topic->getTopicId());
}
else
{
@@ -74,25 +76,28 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
}
topicId = topic->getTopicId();
subscribe = new MQTTGWPacket();
subscribe->setSUBSCRIBE((char*)topic->getTopicName()->c_str(), (uint8_t)qos, (uint16_t)msgId);
subscribe->setSUBSCRIBE((char*) topic->getTopicName()->c_str(),
(uint8_t) qos, (uint16_t) msgId);
}
else if (topicFilter.type == MQTTSN_TOPIC_TYPE_NORMAL)
{
topic = client->getTopics()->getTopicByName(&topicFilter);
if ( topic == nullptr )
if (topic == nullptr)
{
topic = client->getTopics()->add(&topicFilter);
if ( topic == nullptr )
if (topic == nullptr)
{
WRITELOG("%s Client(%s) can't add the Topic.%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
WRITELOG("%s Client(%s) can't add the Topic.%s\n",
ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
return nullptr;
}
}
topicId = topic->getTopicId();
subscribe = new MQTTGWPacket();
subscribe->setSUBSCRIBE((char*)topic->getTopicName()->c_str(), (uint8_t)qos, (uint16_t)msgId);
subscribe->setSUBSCRIBE((char*) topic->getTopicName()->c_str(),
(uint8_t) qos, (uint16_t) msgId);
}
else //MQTTSN_TOPIC_TYPE_SHORT
{
@@ -103,12 +108,12 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
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);
subscribe->setSUBSCRIBE(topicstr, (uint8_t) qos, (uint16_t) msgId);
}
client->setWaitedSubTopicId(msgId, topicId, topicFilter.type);
if ( !client->isAggregated() )
if (!client->isAggregated())
{
ev1 = new Event();
ev1->setBrokerSendEvent(client, subscribe);
@@ -120,33 +125,32 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
return subscribe;
}
RespExit:
MQTTSNPacket* sSuback = new MQTTSNPacket();
sSuback->setSUBACK(qos, topicFilter.data.id, msgId, MQTTSN_RC_NOT_SUPPORTED);
RespExit: MQTTSNPacket* sSuback = new MQTTSNPacket();
sSuback->setSUBACK(qos, topicFilter.data.id, msgId,
MQTTSN_RC_NOT_SUPPORTED);
evsuback = new Event();
evsuback->setClientSendEvent(client, sSuback);
_gateway->getClientSendQue()->post(evsuback);
return nullptr;
}
MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPacket* packet)
MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client,
MQTTSNPacket* packet)
{
uint16_t msgId;
MQTTSN_topicid topicFilter;
MQTTGWPacket* unsubscribe = nullptr;
if ( packet->getUNSUBSCRIBE(&msgId, &topicFilter) == 0 )
if (packet->getUNSUBSCRIBE(&msgId, &topicFilter) == 0)
{
return nullptr;
}
if ( msgId == 0 )
if (msgId == 0)
{
return nullptr;
}
if (topicFilter.type == MQTTSN_TOPIC_TYPE_SHORT)
{
char shortTopic[3];
@@ -169,7 +173,7 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPa
topic = client->getTopics()->getTopicByName(&topicFilter);
}
if ( topic == nullptr )
if (topic == nullptr)
{
MQTTSNPacket* sUnsuback = new MQTTSNPacket();
sUnsuback->setUNSUBACK(msgId);
@@ -185,7 +189,7 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPa
}
}
if ( !client->isAggregated() )
if (!client->isAggregated())
{
Event* ev1 = new Event();
ev1->setBrokerSendEvent(client, unsubscribe);
@@ -198,25 +202,31 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPa
}
}
void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPacket* packet)
void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client,
MQTTSNPacket* packet)
{
MQTTGWPacket* subscribe = handleSubscribe(client, packet);
if ( subscribe != nullptr )
if (subscribe != nullptr)
{
int msgId = 0;
if ( packet->isDuplicate() )
if (packet->isDuplicate())
{
msgId = _gateway->getAdapterManager()->getAggregater()->getMsgId(client, packet->getMsgId());
msgId = _gateway->getAdapterManager()->getAggregater()->getMsgId(
client, packet->getMsgId());
}
else
{
msgId = _gateway->getAdapterManager()->getAggregater()->addMessageIdTable(client, packet->getMsgId());
msgId =
_gateway->getAdapterManager()->getAggregater()->addMessageIdTable(
client, packet->getMsgId());
}
if ( msgId == 0 )
if (msgId == 0)
{
WRITELOG("%s MQTTSNSubscribeHandler can't create MessageIdTableElement %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
WRITELOG(
"%s MQTTSNSubscribeHandler can't create MessageIdTableElement %s%s\n",
ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
return;
}
@@ -224,7 +234,8 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack
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);
_gateway->getAdapterManager()->getAggregater()->addAggregateTopic(
&topic, client);
subscribe->setMsgId(msgId);
Event* ev = new Event();
@@ -233,31 +244,38 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack
}
}
void MQTTSNSubscribeHandler::handleAggregateUnsubscribe(Client* client, MQTTSNPacket* packet)
void MQTTSNSubscribeHandler::handleAggregateUnsubscribe(Client* client,
MQTTSNPacket* packet)
{
MQTTGWPacket* unsubscribe = handleUnsubscribe(client, packet);
if ( unsubscribe != nullptr )
if (unsubscribe != nullptr)
{
int msgId = 0;
if ( packet->isDuplicate() )
if (packet->isDuplicate())
{
msgId = _gateway->getAdapterManager()->getAggregater()->getMsgId(client, packet->getMsgId());
msgId = _gateway->getAdapterManager()->getAggregater()->getMsgId(
client, packet->getMsgId());
}
else
{
msgId = _gateway->getAdapterManager()->getAggregater()->addMessageIdTable(client, packet->getMsgId());
msgId =
_gateway->getAdapterManager()->getAggregater()->addMessageIdTable(
client, packet->getMsgId());
}
if ( msgId == 0 )
if (msgId == 0)
{
WRITELOG("%s MQTTSNUnsubscribeHandler can't create MessageIdTableElement %s%s\n", ERRMSG_HEADER, client->getClientId(), ERRMSG_FOOTER);
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);
_gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(
&topic, client);
unsubscribe->setMsgId(msgId);
Event* ev = new Event();

View File

@@ -42,5 +42,4 @@ private:
}
#endif /* MQTTSNGWSUBSCRIBEHANDLER_H_ */

View File

@@ -42,7 +42,7 @@ Topic::Topic(string* topic, MQTTSN_topicTypes type)
Topic::~Topic()
{
if ( _topicName )
if (_topicName)
{
delete _topicName;
}
@@ -83,12 +83,12 @@ bool Topic::isMatch(string* topicName)
string wildcard = "#";
string wildcards = "+";
while(true)
while (true)
{
loc = topicName->find('/', pos);
tloc = _topicName->find('/', tpos);
if ( loc != string::npos && tloc != string::npos )
if (loc != string::npos && tloc != string::npos)
{
string subtopic = topicName->substr(pos, loc - pos);
string subtopict = _topicName->substr(tpos, tloc - tpos);
@@ -98,11 +98,11 @@ bool Topic::isMatch(string* topicName)
}
else if (subtopict == wildcards)
{
if ( (tpos = tloc + 1 ) > tlen )
if ((tpos = tloc + 1) > tlen)
{
pos = loc + 1;
loc = topicName->find('/', pos);
if ( loc == string::npos )
if (loc == string::npos)
{
return true;
}
@@ -113,13 +113,13 @@ bool Topic::isMatch(string* topicName)
}
pos = loc + 1;
}
else if ( subtopic != subtopict )
else if (subtopic != subtopict)
{
return false;
}
else
{
if ( (tpos = tloc + 1) > tlen )
if ((tpos = tloc + 1) > tlen)
{
return false;
}
@@ -127,15 +127,15 @@ bool Topic::isMatch(string* topicName)
pos = loc + 1;
}
}
else if ( loc == string::npos && tloc == string::npos )
else if (loc == string::npos && tloc == string::npos)
{
string subtopic = topicName->substr(pos);
string subtopict = _topicName->substr(tpos);
if ( subtopict == wildcard || subtopict == wildcards)
if (subtopict == wildcard || subtopict == wildcards)
{
return true;
}
else if ( subtopic == subtopict )
else if (subtopic == subtopict)
{
return true;
}
@@ -144,11 +144,11 @@ bool Topic::isMatch(string* topicName)
return false;
}
}
else if ( loc == string::npos && tloc != string::npos )
else if (loc == string::npos && tloc != string::npos)
{
string subtopic = topicName->substr(pos);
string subtopict = _topicName->substr(tpos, tloc - tpos);
if ( subtopic != subtopict)
if (subtopic != subtopict)
{
return false;
}
@@ -157,7 +157,7 @@ bool Topic::isMatch(string* topicName)
return _topicName->substr(tpos) == wildcard;
}
else if ( loc != string::npos && tloc == string::npos )
else if (loc != string::npos && tloc == string::npos)
{
return _topicName->substr(tpos) == wildcard;
}
@@ -166,7 +166,8 @@ bool Topic::isMatch(string* topicName)
void Topic::print(void)
{
WRITELOG("TopicName=%s ID=%d Type=%d\n", _topicName->c_str(), _topicId, _type);
WRITELOG("TopicName=%s ID=%d Type=%d\n", _topicName->c_str(), _topicId,
_type);
}
/*=====================================
@@ -198,7 +199,7 @@ Topic* Topics::getTopicByName(const MQTTSN_topicid* topicid)
string sname = string(ch, ch + topicid->data.long_.len);
while (p)
{
if ( p->_topicName->compare(sname) == 0 )
if (p->_topicName->compare(sname) == 0)
{
return p;
}
@@ -213,7 +214,7 @@ Topic* Topics::getTopicById(const MQTTSN_topicid* topicid)
while (p)
{
if ( p->_type == topicid->type && p->_topicId == topicid->data.id )
if (p->_type == topicid->type && p->_topicId == topicid->data.id)
{
return p;
}
@@ -225,14 +226,14 @@ Topic* Topics::getTopicById(const MQTTSN_topicid* topicid)
// For MQTTSN_TOPIC_TYPE_NORMAL */
Topic* Topics::add(const MQTTSN_topicid* topicid)
{
if (topicid->type != MQTTSN_TOPIC_TYPE_NORMAL )
if (topicid->type != MQTTSN_TOPIC_TYPE_NORMAL)
{
return 0;
}
Topic* topic = getTopicByName(topicid);
if ( topic )
if (topic)
{
return topic;
}
@@ -244,18 +245,17 @@ Topic* Topics::add(const char* topicName, uint16_t id)
{
MQTTSN_topicid topicId;
if ( _cnt >= MAX_TOPIC_PAR_CLIENT )
if (_cnt >= MAX_TOPIC_PAR_CLIENT)
{
return 0;
}
topicId.data.long_.name = (char*)const_cast<char*>(topicName);
topicId.data.long_.name = (char*) const_cast<char*>(topicName);
topicId.data.long_.len = strlen(topicName);
Topic* topic = getTopicByName(&topicId);
if ( topic )
if (topic)
{
return topic;
}
@@ -270,7 +270,7 @@ Topic* Topics::add(const char* topicName, uint16_t id)
string* name = new string(topicName);
topic->_topicName = name;
if ( id == 0 )
if (id == 0)
{
topic->_type = MQTTSN_TOPIC_TYPE_NORMAL;
topic->_topicId = getNextTopicId();
@@ -283,7 +283,7 @@ Topic* Topics::add(const char* topicName, uint16_t id)
_cnt++;
if ( _first == nullptr)
if (_first == nullptr)
{
_first = topic;
}
@@ -331,7 +331,6 @@ Topic* Topics::match(const MQTTSN_topicid* topicid)
return 0;
}
void Topics::eraseNormal(void)
{
Topic* topic = _first;
@@ -340,14 +339,14 @@ void Topics::eraseNormal(void)
while (topic)
{
if ( topic->_type == MQTTSN_TOPIC_TYPE_NORMAL )
if (topic->_type == MQTTSN_TOPIC_TYPE_NORMAL)
{
next = topic->_next;
if ( _first == topic )
if (_first == topic)
{
_first = next;
}
if ( prev )
if (prev)
{
prev->_next = next;
}
@@ -376,7 +375,7 @@ Topic* Topics::getNextTopic(Topic* topic)
void Topics::print(void)
{
Topic* topic = _first;
if (topic == nullptr )
if (topic == nullptr)
{
WRITELOG("No Topic.\n");
}
@@ -398,7 +397,8 @@ uint8_t Topics::getCount(void)
/*=====================================
Class TopicIdMap
=====================================*/
TopicIdMapElement::TopicIdMapElement(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type)
TopicIdMapElement::TopicIdMapElement(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type)
{
_msgId = msgId;
_topicId = topicId;
@@ -434,7 +434,7 @@ TopicIdMap::TopicIdMap()
TopicIdMap::~TopicIdMap()
{
TopicIdMapElement* p = _first;
while ( p )
while (p)
{
TopicIdMapElement* q = p->_next;
delete p;
@@ -445,9 +445,9 @@ TopicIdMap::~TopicIdMap()
TopicIdMapElement* TopicIdMap::getElement(uint16_t msgId)
{
TopicIdMapElement* p = _first;
while ( p )
while (p)
{
if ( p->_msgId == msgId )
if (p->_msgId == msgId)
{
return p;
}
@@ -456,23 +456,25 @@ TopicIdMapElement* TopicIdMap::getElement(uint16_t msgId)
return 0;
}
TopicIdMapElement* TopicIdMap::add(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type)
TopicIdMapElement* TopicIdMap::add(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type)
{
if ( _cnt > _maxInflight * 2 || ( topicId == 0 && type != MQTTSN_TOPIC_TYPE_SHORT ) )
if (_cnt > _maxInflight * 2
|| (topicId == 0 && type != MQTTSN_TOPIC_TYPE_SHORT))
{
return 0;
}
if ( getElement(msgId) )
if (getElement(msgId))
{
erase(msgId);
}
TopicIdMapElement* elm = new TopicIdMapElement(msgId, topicId, type);
if ( elm == 0 )
if (elm == 0)
{
return 0;
}
if ( _first == nullptr )
if (_first == nullptr)
{
_first = elm;
_end = elm;
@@ -490,11 +492,11 @@ TopicIdMapElement* TopicIdMap::add(uint16_t msgId, uint16_t topicId, MQTTSN_topi
void TopicIdMap::erase(uint16_t msgId)
{
TopicIdMapElement* p = _first;
while ( p )
while (p)
{
if ( p->_msgId == msgId )
if (p->_msgId == msgId)
{
if ( p->_prev == nullptr )
if (p->_prev == nullptr)
{
_first = p->_next;
}
@@ -503,7 +505,7 @@ void TopicIdMap::erase(uint16_t msgId)
p->_prev->_next = p->_next;
}
if ( p->_next == nullptr )
if (p->_next == nullptr)
{
_end = p->_prev;
}
@@ -523,7 +525,7 @@ void TopicIdMap::erase(uint16_t msgId)
void TopicIdMap::clear(void)
{
TopicIdMapElement* p = _first;
while ( p )
while (p)
{
TopicIdMapElement* q = p->_next;
delete p;
@@ -534,5 +536,3 @@ void TopicIdMap::clear(void)
_cnt = 0;
}

View File

@@ -24,7 +24,6 @@
namespace MQTTSNGW
{
/*=====================================
Class Topic
======================================*/
@@ -101,7 +100,8 @@ public:
TopicIdMap();
~TopicIdMap();
TopicIdMapElement* getElement(uint16_t msgId);
TopicIdMapElement* add(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
TopicIdMapElement* add(uint16_t msgId, uint16_t topicId,
MQTTSN_topicTypes type);
void erase(uint16_t msgId);
void clear(void);
private:
@@ -112,9 +112,6 @@ private:
int _maxInflight;
};
}
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWTOPIC_H_ */

View File

@@ -17,6 +17,6 @@
#ifndef MQTTSNGWVERSION_H_IN_
#define MQTTSNGWVERSION_H_IN_
#define PAHO_GATEWAY_VERSION "1.4.0"
#define PAHO_GATEWAY_VERSION "1.5.0"
#endif /* MQTTSNGWVERSION_H_IN_ */

View File

@@ -43,74 +43,74 @@ Gateway::Gateway(void)
Gateway::~Gateway()
{
if ( _params.loginId )
if (_params.loginId)
{
free(_params.loginId);
}
if ( _params.password )
if (_params.password)
{
free(_params.password);
}
if ( _params.gatewayName )
if (_params.gatewayName)
{
free(_params.gatewayName);
}
if ( _params.brokerName )
if (_params.brokerName)
{
free(_params.brokerName);
}
if ( _params.port )
if (_params.port)
{
free(_params.port);
}
if ( _params.portSecure )
if (_params.portSecure)
{
free(_params.portSecure);
}
if ( _params.certKey )
if (_params.certKey)
{
free(_params.certKey);
}
if ( _params.privateKey )
if (_params.privateKey)
{
free(_params.privateKey);
}
if ( _params.rootCApath )
if (_params.rootCApath)
{
free(_params.rootCApath);
}
if ( _params.rootCAfile )
if (_params.rootCAfile)
{
free(_params.rootCAfile);
}
if ( _params.clientListName )
if (_params.clientListName)
{
free(_params.clientListName);
}
if ( _params.predefinedTopicFileName )
if (_params.predefinedTopicFileName)
{
free( _params.predefinedTopicFileName);
free(_params.predefinedTopicFileName);
}
if ( _params.configName )
if (_params.configName)
{
free(_params.configName);
}
if ( _params.qosMinusClientListName )
if (_params.qosMinusClientListName)
{
free(_params.qosMinusClientListName);
}
if ( _adapterManager )
if (_adapterManager)
{
delete _adapterManager;
}
if ( _clientList )
if (_clientList)
{
delete _clientList;
}
if ( _topics )
if (_topics)
{
delete _topics;
}
@@ -182,7 +182,7 @@ void Gateway::initialize(int argc, char** argv)
if (_params.gatewayId == 0 || _params.gatewayId > 255)
{
throw Exception( "Gateway::initialize: invalid Gateway Id");
throw Exception("Gateway::initialize: invalid Gateway Id");
}
if (getParam("GatewayName", param) == 0)
@@ -190,9 +190,9 @@ void Gateway::initialize(int argc, char** argv)
_params.gatewayName = strdup(param);
}
if (_params.gatewayName == 0 )
if (_params.gatewayName == 0)
{
throw Exception( "Gateway::initialize: Gateway Name is missing.");
throw Exception("Gateway::initialize: Gateway Name is missing.");
}
_params.mqttVersion = DEFAULT_MQTT_VERSION;
@@ -238,7 +238,7 @@ void Gateway::initialize(int argc, char** argv)
if (getParam("PredefinedTopic", param) == 0)
{
if ( !strcasecmp(param, "YES") )
if (!strcasecmp(param, "YES"))
{
_params.predefinedTopic = true;
if (getParam("PredefinedTopicList", param) == 0)
@@ -250,7 +250,7 @@ void Gateway::initialize(int argc, char** argv)
if (getParam("AggregatingGateway", param) == 0)
{
if ( !strcasecmp(param, "YES") )
if (!strcasecmp(param, "YES"))
{
_params.aggregatingGw = true;
}
@@ -258,7 +258,7 @@ void Gateway::initialize(int argc, char** argv)
if (getParam("Forwarder", param) == 0)
{
if ( !strcasecmp(param, "YES") )
if (!strcasecmp(param, "YES"))
{
_params.forwarder = true;
}
@@ -266,15 +266,15 @@ void Gateway::initialize(int argc, char** argv)
if (getParam("QoS-1", param) == 0)
{
if ( !strcasecmp(param, "YES") )
if (!strcasecmp(param, "YES"))
{
_params.qosMinus1 = true;
}
}
/* Initialize adapters */
_adapterManager->initialize( _params.gatewayName, _params.aggregatingGw, _params.forwarder, _params.qosMinus1);
_adapterManager->initialize(_params.gatewayName, _params.aggregatingGw,
_params.forwarder, _params.qosMinus1);
/* Setup ClientList and Predefined topics */
_clientList->initialize(_params.aggregatingGw);
@@ -291,21 +291,23 @@ void Gateway::run(void)
WRITELOG(" *\n%s\n", PAHO_COPYRIGHT3);
WRITELOG(" * Version: %s\n", PAHO_GATEWAY_VERSION);
WRITELOG("%s\n", PAHO_COPYRIGHT4);
WRITELOG("\n%s %s has been started.\n\n", currentDateTime(), _params.gatewayName);
WRITELOG("\n%s %s has been started.\n\n", currentDateTime(),
_params.gatewayName);
WRITELOG(" ConfigFile: %s\n", _params.configName);
if ( _params.clientListName )
if (_params.clientListName)
{
WRITELOG(" ClientList: %s\n", _params.clientListName);
}
if ( _params.predefinedTopicFileName )
if (_params.predefinedTopicFileName)
{
WRITELOG(" PreDefFile: %s\n", _params.predefinedTopicFileName);
}
WRITELOG(" SensorN/W: %s\n", _sensorNetwork.getDescription());
WRITELOG(" Broker: %s : %s, %s\n", _params.brokerName, _params.port, _params.portSecure);
WRITELOG(" Broker: %s : %s, %s\n", _params.brokerName, _params.port,
_params.portSecure);
WRITELOG(" RootCApath: %s\n", _params.rootCApath);
WRITELOG(" RootCAfile: %s\n", _params.rootCAfile);
WRITELOG(" CertKey: %s\n", _params.certKey);
@@ -388,10 +390,8 @@ Topics* Gateway::getTopics(void)
bool Gateway::hasSecureConnection(void)
{
return ( _params.certKey
&& _params.privateKey
&& _params.rootCApath
&& _params.rootCAfile );
return (_params.certKey && _params.privateKey && _params.rootCApath
&& _params.rootCAfile);
}
/*=====================================
Class EventQue
@@ -414,16 +414,16 @@ EventQue::~EventQue()
void EventQue::setMaxSize(uint16_t maxSize)
{
_que.setMaxSize((int)maxSize);
_que.setMaxSize((int) maxSize);
}
Event* EventQue::wait(void)
{
Event* ev = nullptr;
while(ev == nullptr)
while (ev == nullptr)
{
if ( _que.size() == 0 )
if (_que.size() == 0)
{
_sem.wait();
}
@@ -438,7 +438,7 @@ Event* EventQue::wait(void)
Event* EventQue::timedwait(uint16_t millsec)
{
Event* ev;
if ( _que.size() == 0 )
if (_que.size() == 0)
{
_sem.timedwait(millsec);
}
@@ -460,10 +460,10 @@ Event* EventQue::timedwait(uint16_t millsec)
void EventQue::post(Event* ev)
{
if ( ev )
if (ev)
{
_mutex.lock();
if ( _que.post(ev) )
if (_que.post(ev))
{
_sem.post();
}
@@ -483,7 +483,6 @@ int EventQue::size()
return sz;
}
/*=====================================
Class Event
=====================================*/
@@ -586,4 +585,3 @@ MQTTGWPacket* Event::getMQTTGWPacket(void)
return _mqttGWPacket;
}

View File

@@ -76,7 +76,8 @@ namespace MQTTSNGW
====================================*/
class Client;
enum EventType{
enum EventType
{
Et_NA = 0,
EtStop,
EtTimeout,
@@ -88,8 +89,8 @@ enum EventType{
EtSensornetSend
};
class Event{
class Event
{
public:
Event();
~Event();
@@ -108,14 +109,13 @@ public:
MQTTGWPacket* getMQTTGWPacket(void);
private:
EventType _eventType {Et_NA};
Client* _client {nullptr};
SensorNetAddress* _sensorNetAddr {nullptr};
MQTTSNPacket* _mqttSNPacket {nullptr};
MQTTGWPacket* _mqttGWPacket {nullptr};
EventType _eventType { Et_NA };
Client* _client { nullptr };
SensorNetAddress* _sensorNetAddr { nullptr };
MQTTSNPacket* _mqttSNPacket { nullptr };
MQTTGWPacket* _mqttGWPacket { nullptr };
};
/*=====================================
Class EventQue
====================================*/
@@ -136,8 +136,6 @@ private:
Semaphore _sem;
};
/*=====================================
Class GatewayParams
====================================*/
@@ -145,40 +143,39 @@ class GatewayParams
{
public:
string configDir;
char* configName {nullptr};
char* clientListName {nullptr};
char* loginId {nullptr};
char* password {nullptr};
uint16_t keepAlive {0};
uint8_t gatewayId {0};
uint8_t mqttVersion {0};
uint16_t maxInflightMsgs {0};
char* gatewayName {nullptr};
char* brokerName {nullptr};
char* port {nullptr};
char* portSecure {nullptr};
char* rootCApath {nullptr};
char* rootCAfile {nullptr};
char* certKey {nullptr};
char* predefinedTopicFileName {nullptr};
char* privateKey {nullptr};
char* qosMinusClientListName {nullptr};
bool clientAuthentication {false};
bool predefinedTopic {false};
bool aggregatingGw {false};
bool qosMinus1 {false};
bool forwarder {false};
char* configName { nullptr };
char* clientListName { nullptr };
char* loginId { nullptr };
char* password { nullptr };
uint16_t keepAlive { 0 };
uint8_t gatewayId { 0 };
uint8_t mqttVersion { 0 };
uint16_t maxInflightMsgs { 0 };
char* gatewayName { nullptr };
char* brokerName { nullptr };
char* port { nullptr };
char* portSecure{ nullptr };
char* rootCApath { nullptr };
char* rootCAfile { nullptr };
char* certKey { nullptr };
char* predefinedTopicFileName { nullptr };
char* privateKey { nullptr };
char* qosMinusClientListName { nullptr };
bool clientAuthentication { false };
bool predefinedTopic { false };
bool aggregatingGw { false };
bool qosMinus1 { false };
bool forwarder { false };
};
/*=====================================
Class Gateway
=====================================*/
class AdapterManager;
class ClientList;
class Gateway: public MultiTaskProcess{
class Gateway: public MultiTaskProcess
{
public:
Gateway(void);
~Gateway();
@@ -203,13 +200,13 @@ public:
private:
GatewayParams _params;
ClientList* _clientList {nullptr};
ClientList* _clientList { nullptr };
EventQue _packetEventQue;
EventQue _brokerSendQue;
EventQue _clientSendQue;
LightIndicator _lightIndicator;
SensorNetwork _sensorNetwork;
AdapterManager* _adapterManager {nullptr};
AdapterManager* _adapterManager { nullptr };
Topics* _topics;
bool _stopFlg;
};

View File

@@ -40,8 +40,7 @@ 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");

View File

@@ -19,7 +19,6 @@
using namespace MQTTSNGW;
/*
* Logmonitor process
*/