mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-16 00:46:52 +01:00
@@ -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;
|
uint8_t rc = MQTT_SERVER_UNAVAILABLE;
|
||||||
Connack resp;
|
Connack resp;
|
||||||
@@ -44,29 +45,35 @@ void MQTTGWConnectionHandler::handleConnack(Client* client, MQTTGWPacket* packet
|
|||||||
else if (resp.rc == MQTT_UNACCEPTABLE_PROTOCOL_VERSION)
|
else if (resp.rc == MQTT_UNACCEPTABLE_PROTOCOL_VERSION)
|
||||||
{
|
{
|
||||||
rc = MQTTSN_RC_NOT_SUPPORTED;
|
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)
|
else if (resp.rc == MQTT_IDENTIFIER_REJECTED)
|
||||||
{
|
{
|
||||||
rc = MQTTSN_RC_NOT_SUPPORTED;
|
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());
|
client->getClientId());
|
||||||
}
|
}
|
||||||
else if (resp.rc == MQTT_SERVER_UNAVAILABLE)
|
else if (resp.rc == MQTT_SERVER_UNAVAILABLE)
|
||||||
{
|
{
|
||||||
rc = MQTTSN_RC_REJECTED_CONGESTED;
|
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());
|
client->getClientId());
|
||||||
}
|
}
|
||||||
else if (resp.rc == MQTT_BAD_USERNAME_OR_PASSWORD)
|
else if (resp.rc == MQTT_BAD_USERNAME_OR_PASSWORD)
|
||||||
{
|
{
|
||||||
rc = MQTTSN_RC_NOT_SUPPORTED;
|
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)
|
else if (resp.rc == MQTT_NOT_AUTHORIZED)
|
||||||
{
|
{
|
||||||
rc = MQTTSN_RC_NOT_SUPPORTED;
|
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();
|
MQTTSNPacket* snPacket = new MQTTSNPacket();
|
||||||
@@ -78,7 +85,8 @@ void MQTTGWConnectionHandler::handleConnack(Client* client, MQTTGWPacket* packet
|
|||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTTGWConnectionHandler::handlePingresp(Client* client, MQTTGWPacket* packet)
|
void MQTTGWConnectionHandler::handlePingresp(Client* client,
|
||||||
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
MQTTSNPacket* snPacket = new MQTTSNPacket();
|
MQTTSNPacket* snPacket = new MQTTSNPacket();
|
||||||
snPacket->setPINGRESP();
|
snPacket->setPINGRESP();
|
||||||
@@ -88,7 +96,8 @@ void MQTTGWConnectionHandler::handlePingresp(Client* client, MQTTGWPacket* packe
|
|||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTTGWConnectionHandler::handleDisconnect(Client* client, MQTTGWPacket* packet)
|
void MQTTGWConnectionHandler::handleDisconnect(Client* client,
|
||||||
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
MQTTSNPacket* snPacket = new MQTTSNPacket();
|
MQTTSNPacket* snPacket = new MQTTSNPacket();
|
||||||
snPacket->setDISCONNECT(0);
|
snPacket->setDISCONNECT(0);
|
||||||
|
|||||||
@@ -29,8 +29,9 @@ void writeInt(unsigned char** pptr, int msgId);
|
|||||||
* List of the predefined MQTT v3 packet names.
|
* List of the predefined MQTT v3 packet names.
|
||||||
*/
|
*/
|
||||||
static const char* mqtt_packet_names[] =
|
static const char* mqtt_packet_names[] =
|
||||||
{ "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL", "PUBCOMP", "SUBSCRIBE", "SUBACK",
|
{ "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
|
||||||
"UNSUBSCRIBE", "UNSUBACK", "PINGREQ", "PINGRESP", "DISCONNECT" };
|
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", "PINGREQ",
|
||||||
|
"PINGRESP", "DISCONNECT" };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encodes the message length according to the MQTT algorithm
|
* Encodes the message length according to the MQTT algorithm
|
||||||
@@ -87,8 +88,8 @@ char* readUTFlen(char** pptr, char* enddata, int* len)
|
|||||||
*len = readInt(pptr);
|
*len = readInt(pptr);
|
||||||
if (&(*pptr)[*len] <= enddata)
|
if (&(*pptr)[*len] <= enddata)
|
||||||
{
|
{
|
||||||
string = (char*)calloc(*len + 1, 1);
|
string = (char*) calloc(*len + 1, 1);
|
||||||
memcpy(string, *pptr, (size_t)*len);
|
memcpy(string, *pptr, (size_t) *len);
|
||||||
string[*len] = '\0';
|
string[*len] = '\0';
|
||||||
*pptr += *len;
|
*pptr += *len;
|
||||||
}
|
}
|
||||||
@@ -142,9 +143,9 @@ void writeChar(unsigned char** pptr, char c)
|
|||||||
*/
|
*/
|
||||||
void writeInt(unsigned char** pptr, int anInt)
|
void writeInt(unsigned char** pptr, int anInt)
|
||||||
{
|
{
|
||||||
**pptr = (unsigned char)(anInt / 256);
|
**pptr = (unsigned char) (anInt / 256);
|
||||||
(*pptr)++;
|
(*pptr)++;
|
||||||
**pptr = (unsigned char)(anInt % 256);
|
**pptr = (unsigned char) (anInt % 256);
|
||||||
(*pptr)++;
|
(*pptr)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -155,9 +156,9 @@ void writeInt(unsigned char** pptr, int anInt)
|
|||||||
*/
|
*/
|
||||||
void writeUTF(unsigned char** pptr, const char* string)
|
void writeUTF(unsigned char** pptr, const char* string)
|
||||||
{
|
{
|
||||||
int len = (int)strlen(string);
|
int len = (int) strlen(string);
|
||||||
writeInt(pptr, len);
|
writeInt(pptr, len);
|
||||||
memcpy(*pptr, string, (size_t)len);
|
memcpy(*pptr, string, (size_t) len);
|
||||||
*pptr += len;
|
*pptr += len;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,8 +188,8 @@ int MQTTGWPacket::recv(Network* network)
|
|||||||
unsigned char c;
|
unsigned char c;
|
||||||
|
|
||||||
/* read First Byte of Packet */
|
/* read First Byte of Packet */
|
||||||
int rc = network->recv((unsigned char*)&_header.byte, 1);
|
int rc = network->recv((unsigned char*) &_header.byte, 1);
|
||||||
if ( rc <= 0)
|
if (rc <= 0)
|
||||||
{
|
{
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
@@ -207,11 +208,11 @@ int MQTTGWPacket::recv(Network* network)
|
|||||||
multiplier *= 128;
|
multiplier *= 128;
|
||||||
} while ((c & 128) != 0);
|
} while ((c & 128) != 0);
|
||||||
|
|
||||||
if ( _remainingLength > 0 )
|
if (_remainingLength > 0)
|
||||||
{
|
{
|
||||||
/* allocate buffer */
|
/* allocate buffer */
|
||||||
_data = (unsigned char*)calloc(_remainingLength, 1);
|
_data = (unsigned char*) calloc(_remainingLength, 1);
|
||||||
if ( !_data )
|
if (!_data)
|
||||||
{
|
{
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
@@ -219,11 +220,11 @@ int MQTTGWPacket::recv(Network* network)
|
|||||||
/* read Payload */
|
/* read Payload */
|
||||||
int remlen = network->recv(_data, _remainingLength);
|
int remlen = network->recv(_data, _remainingLength);
|
||||||
|
|
||||||
if (remlen == -1 )
|
if (remlen == -1)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else if ( remlen != _remainingLength )
|
else if (remlen != _remainingLength)
|
||||||
{
|
{
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
@@ -242,8 +243,9 @@ int MQTTGWPacket::send(Network* network)
|
|||||||
|
|
||||||
int MQTTGWPacket::getAck(Ack* ack)
|
int MQTTGWPacket::getAck(Ack* ack)
|
||||||
{
|
{
|
||||||
if (PUBACK != _header.bits.type && PUBREC != _header.bits.type && PUBREL != _header.bits.type
|
if (PUBACK != _header.bits.type && PUBREC != _header.bits.type
|
||||||
&& PUBCOMP != _header.bits.type && UNSUBACK != _header.bits.type)
|
&& PUBREL != _header.bits.type && PUBCOMP != _header.bits.type
|
||||||
|
&& UNSUBACK != _header.bits.type)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -303,26 +305,29 @@ int MQTTGWPacket::getPUBLISH(Publish* pub)
|
|||||||
return 1;
|
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();
|
clearData();
|
||||||
_header = connect->header;
|
_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)
|
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)
|
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;
|
unsigned char* ptr = _data;
|
||||||
|
|
||||||
if (connect->version == 3)
|
if (connect->version == 3)
|
||||||
@@ -360,14 +365,15 @@ int MQTTGWPacket::setCONNECT(Connect* connect, unsigned char* username, unsigned
|
|||||||
return 1;
|
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();
|
clearData();
|
||||||
_header.byte = 0;
|
_header.byte = 0;
|
||||||
_header.bits.type = SUBSCRIBE;
|
_header.bits.type = SUBSCRIBE;
|
||||||
_header.bits.qos = 1; // Reserved
|
_header.bits.qos = 1; // Reserved
|
||||||
_remainingLength = (int)strlen(topic) + 5;
|
_remainingLength = (int) strlen(topic) + 5;
|
||||||
_data = (unsigned char*)calloc(_remainingLength, 1);
|
_data = (unsigned char*) calloc(_remainingLength, 1);
|
||||||
if (_data)
|
if (_data)
|
||||||
{
|
{
|
||||||
unsigned char* ptr = _data;
|
unsigned char* ptr = _data;
|
||||||
@@ -386,8 +392,8 @@ int MQTTGWPacket::setUNSUBSCRIBE(const char* topic, unsigned short msgid)
|
|||||||
_header.byte = 0;
|
_header.byte = 0;
|
||||||
_header.bits.type = UNSUBSCRIBE;
|
_header.bits.type = UNSUBSCRIBE;
|
||||||
_header.bits.qos = 1;
|
_header.bits.qos = 1;
|
||||||
_remainingLength = (int)strlen(topic) + 4;
|
_remainingLength = (int) strlen(topic) + 4;
|
||||||
_data = (unsigned char*)calloc(_remainingLength, 1);
|
_data = (unsigned char*) calloc(_remainingLength, 1);
|
||||||
if (_data)
|
if (_data)
|
||||||
{
|
{
|
||||||
unsigned char* ptr = _data;
|
unsigned char* ptr = _data;
|
||||||
@@ -406,14 +412,14 @@ int MQTTGWPacket::setPUBLISH(Publish* pub)
|
|||||||
_header.byte = pub->header.byte;
|
_header.byte = pub->header.byte;
|
||||||
_header.bits.type = PUBLISH;
|
_header.bits.type = PUBLISH;
|
||||||
_remainingLength = 4 + pub->topiclen + pub->payloadlen;
|
_remainingLength = 4 + pub->topiclen + pub->payloadlen;
|
||||||
_data = (unsigned char*)calloc(_remainingLength, 1);
|
_data = (unsigned char*) calloc(_remainingLength, 1);
|
||||||
if (_data)
|
if (_data)
|
||||||
{
|
{
|
||||||
unsigned char* ptr = _data;
|
unsigned char* ptr = _data;
|
||||||
writeInt(&ptr, pub->topiclen);
|
writeInt(&ptr, pub->topiclen);
|
||||||
memcpy(ptr, pub->topic, pub->topiclen);
|
memcpy(ptr, pub->topic, pub->topiclen);
|
||||||
ptr += pub->topiclen;
|
ptr += pub->topiclen;
|
||||||
if ( _header.bits.qos > 0 )
|
if (_header.bits.qos > 0)
|
||||||
{
|
{
|
||||||
writeInt(&ptr, pub->msgId);
|
writeInt(&ptr, pub->msgId);
|
||||||
}
|
}
|
||||||
@@ -438,7 +444,7 @@ int MQTTGWPacket::setAck(unsigned char msgType, unsigned short msgid)
|
|||||||
_header.bits.type = msgType;
|
_header.bits.type = msgType;
|
||||||
_header.bits.qos = (msgType == PUBREL) ? 1 : 0;
|
_header.bits.qos = (msgType == PUBREL) ? 1 : 0;
|
||||||
|
|
||||||
_data = (unsigned char*)calloc(_remainingLength, 1);
|
_data = (unsigned char*) calloc(_remainingLength, 1);
|
||||||
if (_data)
|
if (_data)
|
||||||
{
|
{
|
||||||
unsigned char* data = _data;
|
unsigned char* data = _data;
|
||||||
@@ -473,7 +479,7 @@ int MQTTGWPacket::getPacketData(unsigned char* buf)
|
|||||||
{
|
{
|
||||||
unsigned char* ptr = buf;
|
unsigned char* ptr = buf;
|
||||||
*ptr++ = _header.byte;
|
*ptr++ = _header.byte;
|
||||||
int len = MQTTPacket_encode((char*)ptr, _remainingLength);
|
int len = MQTTPacket_encode((char*) ptr, _remainingLength);
|
||||||
ptr += len;
|
ptr += len;
|
||||||
memcpy(ptr, _data, _remainingLength);
|
memcpy(ptr, _data, _remainingLength);
|
||||||
return 1 + len + _remainingLength;
|
return 1 + len + _remainingLength;
|
||||||
@@ -499,13 +505,13 @@ char* MQTTGWPacket::getMsgId(char* pbuf)
|
|||||||
{
|
{
|
||||||
int type = getType();
|
int type = getType();
|
||||||
|
|
||||||
switch ( type )
|
switch (type)
|
||||||
{
|
{
|
||||||
case PUBLISH:
|
case PUBLISH:
|
||||||
Publish pub;
|
Publish pub;
|
||||||
pub.msgId = 0;
|
pub.msgId = 0;
|
||||||
getPUBLISH(&pub);
|
getPUBLISH(&pub);
|
||||||
if ( _header.bits.dup )
|
if (_header.bits.dup)
|
||||||
{
|
{
|
||||||
sprintf(pbuf, "+%04X", pub.msgId);
|
sprintf(pbuf, "+%04X", pub.msgId);
|
||||||
}
|
}
|
||||||
@@ -528,7 +534,7 @@ char* MQTTGWPacket::getMsgId(char* pbuf)
|
|||||||
sprintf(pbuf, " ");
|
sprintf(pbuf, " ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ( strcmp(pbuf, " 0000") == 0 )
|
if (strcmp(pbuf, " 0000") == 0)
|
||||||
{
|
{
|
||||||
sprintf(pbuf, " ");
|
sprintf(pbuf, " ");
|
||||||
}
|
}
|
||||||
@@ -540,7 +546,7 @@ int MQTTGWPacket::getMsgId(void)
|
|||||||
int type = getType();
|
int type = getType();
|
||||||
int msgId = 0;
|
int msgId = 0;
|
||||||
|
|
||||||
switch ( type )
|
switch (type)
|
||||||
{
|
{
|
||||||
case PUBLISH:
|
case PUBLISH:
|
||||||
Publish pub;
|
Publish pub;
|
||||||
@@ -556,7 +562,7 @@ int MQTTGWPacket::getMsgId(void)
|
|||||||
case UNSUBSCRIBE:
|
case UNSUBSCRIBE:
|
||||||
case SUBACK:
|
case SUBACK:
|
||||||
case UNSUBACK:
|
case UNSUBACK:
|
||||||
msgId = 256 * (unsigned char)_data[0] + (unsigned char)_data[1];
|
msgId = 256 * (unsigned char) _data[0] + (unsigned char) _data[1];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -569,7 +575,7 @@ void MQTTGWPacket::setMsgId(int msgId)
|
|||||||
int type = getType();
|
int type = getType();
|
||||||
unsigned char* ptr = 0;
|
unsigned char* ptr = 0;
|
||||||
|
|
||||||
switch ( type )
|
switch (type)
|
||||||
{
|
{
|
||||||
case PUBLISH:
|
case PUBLISH:
|
||||||
Publish pub;
|
Publish pub;
|
||||||
@@ -579,8 +585,8 @@ void MQTTGWPacket::setMsgId(int msgId)
|
|||||||
pub.msgId = msgId;
|
pub.msgId = msgId;
|
||||||
ptr = _data + pub.topiclen;
|
ptr = _data + pub.topiclen;
|
||||||
writeInt(&ptr, pub.msgId);
|
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;
|
break;
|
||||||
case SUBSCRIBE:
|
case SUBSCRIBE:
|
||||||
case UNSUBSCRIBE:
|
case UNSUBSCRIBE:
|
||||||
@@ -591,8 +597,8 @@ void MQTTGWPacket::setMsgId(int msgId)
|
|||||||
case SUBACK:
|
case SUBACK:
|
||||||
case UNSUBACK:
|
case UNSUBACK:
|
||||||
ptr = _data;
|
ptr = _data;
|
||||||
*ptr++ = (unsigned char)(msgId / 256);
|
*ptr++ = (unsigned char) (msgId / 256);
|
||||||
*ptr = (unsigned char)(msgId % 256);
|
*ptr = (unsigned char) (msgId % 256);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -620,7 +626,7 @@ MQTTGWPacket& MQTTGWPacket::operator =(MQTTGWPacket& packet)
|
|||||||
clearData();
|
clearData();
|
||||||
this->_header.byte = packet._header.byte;
|
this->_header.byte = packet._header.byte;
|
||||||
this->_remainingLength = packet._remainingLength;
|
this->_remainingLength = packet._remainingLength;
|
||||||
_data = (unsigned char*)calloc(_remainingLength, 1);
|
_data = (unsigned char*) calloc(_remainingLength, 1);
|
||||||
if (_data)
|
if (_data)
|
||||||
{
|
{
|
||||||
memcpy(this->_data, packet._data, _remainingLength);
|
memcpy(this->_data, packet._data, _remainingLength);
|
||||||
@@ -634,12 +640,13 @@ MQTTGWPacket& MQTTGWPacket::operator =(MQTTGWPacket& packet)
|
|||||||
|
|
||||||
UTF8String MQTTGWPacket::getTopic(void)
|
UTF8String MQTTGWPacket::getTopic(void)
|
||||||
{
|
{
|
||||||
UTF8String str = {0, nullptr};
|
UTF8String str =
|
||||||
if ( _header.bits.type == SUBSCRIBE || _header.bits.type == UNSUBSCRIBE )
|
{ 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.len = readInt(&ptr);
|
||||||
str.data = (char*)(_data + 4);
|
str.data = (char*) (_data + 4);
|
||||||
}
|
}
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,18 +31,29 @@ typedef void* (*pf)(unsigned char, char*, size_t);
|
|||||||
|
|
||||||
enum msgTypes
|
enum msgTypes
|
||||||
{
|
{
|
||||||
CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
|
CONNECT = 1,
|
||||||
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
|
CONNACK,
|
||||||
PINGREQ, PINGRESP, DISCONNECT
|
PUBLISH,
|
||||||
|
PUBACK,
|
||||||
|
PUBREC,
|
||||||
|
PUBREL,
|
||||||
|
PUBCOMP,
|
||||||
|
SUBSCRIBE,
|
||||||
|
SUBACK,
|
||||||
|
UNSUBSCRIBE,
|
||||||
|
UNSUBACK,
|
||||||
|
PINGREQ,
|
||||||
|
PINGRESP,
|
||||||
|
DISCONNECT
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bitfields for the MQTT header byte.
|
* Bitfields for the MQTT header byte.
|
||||||
*/
|
*/
|
||||||
typedef union
|
typedef union
|
||||||
{
|
{
|
||||||
/*unsigned*/ char byte; /**< the whole byte */
|
/*unsigned*/
|
||||||
|
char byte; /**< the whole byte */
|
||||||
#if defined(REVERSED)
|
#if defined(REVERSED)
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
@@ -50,25 +61,25 @@ typedef union
|
|||||||
bool dup : 1; /**< DUP flag bit */
|
bool dup : 1; /**< DUP flag bit */
|
||||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||||
bool retain : 1; /**< retained flag bit */
|
bool retain : 1; /**< retained flag bit */
|
||||||
} bits;
|
}bits;
|
||||||
#else
|
#else
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
bool retain : 1; /**< retained flag bit */
|
bool retain :1; /**< retained flag bit */
|
||||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
unsigned int qos :2; /**< QoS value, 0, 1 or 2 */
|
||||||
bool dup : 1; /**< DUP flag bit */
|
bool dup :1; /**< DUP flag bit */
|
||||||
unsigned int type : 4; /**< message type nibble */
|
unsigned int type :4; /**< message type nibble */
|
||||||
} bits;
|
} bits;
|
||||||
#endif
|
#endif
|
||||||
} Header;
|
} Header;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data for a connect packet.
|
* Data for a connect packet.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
enum MQTT_connackCodes{
|
enum MQTT_connackCodes
|
||||||
MQTT_CONNECTION_ACCEPTED ,
|
{
|
||||||
|
MQTT_CONNECTION_ACCEPTED,
|
||||||
MQTT_UNACCEPTABLE_PROTOCOL_VERSION,
|
MQTT_UNACCEPTABLE_PROTOCOL_VERSION,
|
||||||
MQTT_IDENTIFIER_REJECTED,
|
MQTT_IDENTIFIER_REJECTED,
|
||||||
MQTT_SERVER_UNAVAILABLE,
|
MQTT_SERVER_UNAVAILABLE,
|
||||||
@@ -92,17 +103,17 @@ typedef struct
|
|||||||
bool will : 1; /**< will flag */
|
bool will : 1; /**< will flag */
|
||||||
bool cleanstart : 1; /**< cleansession flag */
|
bool cleanstart : 1; /**< cleansession flag */
|
||||||
int : 1; /**< unused */
|
int : 1; /**< unused */
|
||||||
} bits;
|
}bits;
|
||||||
#else
|
#else
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
int : 1; /**< unused */
|
int :1; /**< unused */
|
||||||
bool cleanstart : 1; /**< cleansession flag */
|
bool cleanstart :1; /**< cleansession flag */
|
||||||
bool will : 1; /**< will flag */
|
bool will :1; /**< will flag */
|
||||||
unsigned int willQoS : 2; /**< will QoS value */
|
unsigned int willQoS :2; /**< will QoS value */
|
||||||
bool willRetain : 1; /**< will retain setting */
|
bool willRetain :1; /**< will retain setting */
|
||||||
bool password : 1; /**< 3.1 password */
|
bool password :1; /**< 3.1 password */
|
||||||
bool username : 1; /**< 3.1 user name */
|
bool username :1; /**< 3.1 user name */
|
||||||
} bits;
|
} bits;
|
||||||
#endif
|
#endif
|
||||||
} flags; /**< connect flags byte */
|
} 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, \
|
#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}} }
|
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data for a willMessage.
|
* Data for a willMessage.
|
||||||
*/
|
*/
|
||||||
@@ -132,7 +141,7 @@ typedef struct
|
|||||||
char* msg;
|
char* msg;
|
||||||
int retained;
|
int retained;
|
||||||
int qos;
|
int qos;
|
||||||
}willMessages;
|
} willMessages;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data for a connack packet.
|
* Data for a connack packet.
|
||||||
@@ -148,19 +157,18 @@ typedef struct
|
|||||||
{
|
{
|
||||||
unsigned int reserved : 7; /**< message type nibble */
|
unsigned int reserved : 7; /**< message type nibble */
|
||||||
bool sessionPresent : 1; /**< was a session found on the server? */
|
bool sessionPresent : 1; /**< was a session found on the server? */
|
||||||
} bits;
|
}bits;
|
||||||
#else
|
#else
|
||||||
struct
|
struct
|
||||||
{
|
{
|
||||||
bool sessionPresent : 1; /**< was a session found on the server? */
|
bool sessionPresent :1; /**< was a session found on the server? */
|
||||||
unsigned int reserved : 7; /**< message type nibble */
|
unsigned int reserved :7; /**< message type nibble */
|
||||||
} bits;
|
} bits;
|
||||||
#endif
|
#endif
|
||||||
} flags; /**< connack flags byte */
|
} flags; /**< connack flags byte */
|
||||||
char rc; /**< connack return code */
|
char rc; /**< connack return code */
|
||||||
} Connack;
|
} Connack;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Data for a publish packet.
|
* Data for a publish packet.
|
||||||
*/
|
*/
|
||||||
@@ -214,11 +222,13 @@ public:
|
|||||||
int getSUBACK(unsigned short* msgId, unsigned char* rc);
|
int getSUBACK(unsigned short* msgId, unsigned char* rc);
|
||||||
int getPUBLISH(Publish* pub);
|
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 setPUBLISH(Publish* pub);
|
||||||
int setAck(unsigned char msgType, unsigned short msgid);
|
int setAck(unsigned char msgType, unsigned short msgid);
|
||||||
int setHeader(unsigned char msgType);
|
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);
|
int setUNSUBSCRIBE(const char* topics, unsigned short msgid);
|
||||||
|
|
||||||
UTF8String getTopic(void);
|
UTF8String getTopic(void);
|
||||||
|
|||||||
@@ -37,14 +37,15 @@ MQTTGWPublishHandler::~MQTTGWPublishHandler()
|
|||||||
|
|
||||||
void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* client is sleeping. save PUBLISH */
|
/* client is sleeping. save PUBLISH */
|
||||||
if ( client->isSleep() )
|
if (client->isSleep())
|
||||||
{
|
{
|
||||||
Publish pub;
|
Publish pub;
|
||||||
packet->getPUBLISH(&pub);
|
packet->getPUBLISH(&pub);
|
||||||
@@ -56,16 +57,18 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
|||||||
{
|
{
|
||||||
replyACK(client, &pub, PUBACK);
|
replyACK(client, &pub, PUBACK);
|
||||||
}
|
}
|
||||||
else if ( pub.header.bits.qos == 2)
|
else if (pub.header.bits.qos == 2)
|
||||||
{
|
{
|
||||||
replyACK(client, &pub, PUBREC);
|
replyACK(client, &pub, PUBREC);
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTGWPacket* msg = new MQTTGWPacket();
|
MQTTGWPacket* msg = new MQTTGWPacket();
|
||||||
*msg = *packet;
|
*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;
|
delete msg;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -94,7 +97,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
|||||||
topicId.data.long_.name = pub.topic;
|
topicId.data.long_.name = pub.topic;
|
||||||
Topic* tp = client->getTopics()->getTopicByName(&topicId);
|
Topic* tp = client->getTopics()->getTopicByName(&topicId);
|
||||||
|
|
||||||
if ( tp )
|
if (tp)
|
||||||
{
|
{
|
||||||
topicId.type = tp->getType();
|
topicId.type = tp->getType();
|
||||||
topicId.data.long_.len = pub.topiclen;
|
topicId.data.long_.len = pub.topiclen;
|
||||||
@@ -113,7 +116,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
|||||||
{
|
{
|
||||||
replyACK(client, &pub, PUBACK);
|
replyACK(client, &pub, PUBACK);
|
||||||
}
|
}
|
||||||
else if ( pub.header.bits.qos == 2 )
|
else if (pub.header.bits.qos == 2)
|
||||||
{
|
{
|
||||||
replyACK(client, &pub, PUBREC);
|
replyACK(client, &pub, PUBREC);
|
||||||
}
|
}
|
||||||
@@ -145,23 +148,28 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
|||||||
|
|
||||||
/* send PUBLISH */
|
/* send PUBLISH */
|
||||||
topicId.data.id = id;
|
topicId.data.id = id;
|
||||||
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos,
|
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup,
|
||||||
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload,
|
(int) pub.header.bits.qos,
|
||||||
pub.payloadlen);
|
(uint8_t) pub.header.bits.retain, (uint16_t) pub.msgId,
|
||||||
client->getWaitREGACKPacketList()->setPacket(snPacket, regackMsgId);
|
topicId, (uint8_t*) pub.payload, pub.payloadlen);
|
||||||
|
client->getWaitREGACKPacketList()->setPacket(snPacket,
|
||||||
|
regackMsgId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else
|
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;
|
delete snPacket;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup, (int) pub.header.bits.qos, (uint8_t) pub.header.bits.retain,
|
snPacket->setPUBLISH((uint8_t) pub.header.bits.dup,
|
||||||
(uint16_t) pub.msgId, topicId, (uint8_t*) pub.payload, pub.payloadlen);
|
(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();
|
Event* ev1 = new Event();
|
||||||
ev1->setClientSendEvent(client, snPacket);
|
ev1->setClientSendEvent(client, snPacket);
|
||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
@@ -171,7 +179,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
|||||||
void MQTTGWPublishHandler::replyACK(Client* client, Publish* pub, int type)
|
void MQTTGWPublishHandler::replyACK(Client* client, Publish* pub, int type)
|
||||||
{
|
{
|
||||||
MQTTGWPacket* pubAck = new MQTTGWPacket();
|
MQTTGWPacket* pubAck = new MQTTGWPacket();
|
||||||
pubAck->setAck(type, (uint16_t)pub->msgId);
|
pubAck->setAck(type, (uint16_t) pub->msgId);
|
||||||
Event* ev1 = new Event();
|
Event* ev1 = new Event();
|
||||||
ev1->setBrokerSendEvent(client, pubAck);
|
ev1->setBrokerSendEvent(client, pubAck);
|
||||||
_gateway->getBrokerSendQue()->post(ev1);
|
_gateway->getBrokerSendQue()->post(ev1);
|
||||||
@@ -181,27 +189,31 @@ void MQTTGWPublishHandler::handlePuback(Client* client, MQTTGWPacket* packet)
|
|||||||
{
|
{
|
||||||
Ack ack;
|
Ack ack;
|
||||||
packet->getAck(&ack);
|
packet->getAck(&ack);
|
||||||
TopicIdMapElement* topicId = client->getWaitedPubTopicId((uint16_t)ack.msgId);
|
TopicIdMapElement* topicId = client->getWaitedPubTopicId(
|
||||||
|
(uint16_t) ack.msgId);
|
||||||
if (topicId)
|
if (topicId)
|
||||||
{
|
{
|
||||||
MQTTSNPacket* mqttsnPacket = new MQTTSNPacket();
|
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();
|
Event* ev1 = new Event();
|
||||||
ev1->setClientSendEvent(client, mqttsnPacket);
|
ev1->setClientSendEvent(client, mqttsnPacket);
|
||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
return;
|
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;
|
Ack ack;
|
||||||
packet->getAck(&ack);
|
packet->getAck(&ack);
|
||||||
|
|
||||||
if ( client->isActive() || client->isAwake() )
|
if (client->isActive() || client->isAwake())
|
||||||
{
|
{
|
||||||
MQTTSNPacket* mqttsnPacket = new MQTTSNPacket();
|
MQTTSNPacket* mqttsnPacket = new MQTTSNPacket();
|
||||||
if (type == PUBREC)
|
if (type == PUBREC)
|
||||||
@@ -221,12 +233,12 @@ void MQTTGWPublishHandler::handleAck(Client* client, MQTTGWPacket* packet, int t
|
|||||||
ev1->setClientSendEvent(client, mqttsnPacket);
|
ev1->setClientSendEvent(client, mqttsnPacket);
|
||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
}
|
}
|
||||||
else if ( client->isSleep() )
|
else if (client->isSleep())
|
||||||
{
|
{
|
||||||
if (type == PUBREL)
|
if (type == PUBREL)
|
||||||
{
|
{
|
||||||
MQTTGWPacket* pubComp = new MQTTGWPacket();
|
MQTTGWPacket* pubComp = new MQTTGWPacket();
|
||||||
pubComp->setAck(PUBCOMP, (uint16_t)ack.msgId);
|
pubComp->setAck(PUBCOMP, (uint16_t) ack.msgId);
|
||||||
Event* ev1 = new Event();
|
Event* ev1 = new Event();
|
||||||
ev1->setBrokerSendEvent(client, pubComp);
|
ev1->setBrokerSendEvent(client, pubComp);
|
||||||
_gateway->getBrokerSendQue()->post(ev1);
|
_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 msgId = packet->getMsgId();
|
||||||
uint16_t clientMsgId = 0;
|
uint16_t clientMsgId = 0;
|
||||||
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId, &clientMsgId);
|
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId,
|
||||||
if ( newClient != nullptr )
|
&clientMsgId);
|
||||||
|
if (newClient != nullptr)
|
||||||
{
|
{
|
||||||
packet->setMsgId((int)clientMsgId);
|
packet->setMsgId((int) clientMsgId);
|
||||||
handlePuback(newClient, packet);
|
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 msgId = packet->getMsgId();
|
||||||
uint16_t clientMsgId = 0;
|
uint16_t clientMsgId = 0;
|
||||||
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId, &clientMsgId);
|
Client* newClient = _gateway->getAdapterManager()->convertClient(msgId,
|
||||||
if ( newClient != nullptr )
|
&clientMsgId);
|
||||||
|
if (newClient != nullptr)
|
||||||
{
|
{
|
||||||
packet->setMsgId((int)clientMsgId);
|
packet->setMsgId((int) clientMsgId);
|
||||||
handleAck(newClient, packet,type);
|
handleAck(newClient, packet, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTTGWPublishHandler::handleAggregatePubrel(Client* client, MQTTGWPacket* packet)
|
void MQTTGWPublishHandler::handleAggregatePubrel(Client* client,
|
||||||
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
Publish pub;
|
Publish pub;
|
||||||
packet->getPUBLISH(&pub);
|
packet->getPUBLISH(&pub);
|
||||||
replyACK(client, &pub, PUBCOMP);
|
replyACK(client, &pub, PUBCOMP);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTTGWPublishHandler::handleAggregatePublish(Client* client, MQTTGWPacket* packet)
|
void MQTTGWPublishHandler::handleAggregatePublish(Client* client,
|
||||||
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
Publish pub;
|
Publish pub;
|
||||||
packet->getPUBLISH(&pub);
|
packet->getPUBLISH(&pub);
|
||||||
|
|
||||||
|
|
||||||
string* topicName = new string(pub.topic, pub.topiclen); // topic deletes topicName when the topic is deleted
|
string* topicName = new string(pub.topic, pub.topiclen); // topic deletes topicName when the topic is deleted
|
||||||
Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL);
|
Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL);
|
||||||
|
|
||||||
// ToDo: need to refactor
|
// 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();
|
Client* devClient = elm->getClient();
|
||||||
MQTTGWPacket* msg = new MQTTGWPacket();
|
MQTTGWPacket* msg = new MQTTGWPacket();
|
||||||
*msg = *packet;
|
*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;
|
delete msg;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,6 +45,4 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTGWPUBLISHHANDLER_H_ */
|
#endif /* MQTTGWPUBLISHHANDLER_H_ */
|
||||||
|
|||||||
@@ -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;
|
Ack ack;
|
||||||
packet->getAck(&ack);
|
packet->getAck(&ack);
|
||||||
@@ -72,28 +73,33 @@ void MQTTGWSubscribeHandler::handleUnsuback(Client* client, MQTTGWPacket* packet
|
|||||||
_gateway->getClientSendQue()->post(evt);
|
_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 msgId = packet->getMsgId();
|
||||||
uint16_t clientMsgId = 0;
|
uint16_t clientMsgId = 0;
|
||||||
Client* newClient = _gateway->getAdapterManager()->getAggregater()->convertClient(msgId, &clientMsgId);
|
Client* newClient =
|
||||||
if ( newClient != nullptr )
|
_gateway->getAdapterManager()->getAggregater()->convertClient(msgId,
|
||||||
|
&clientMsgId);
|
||||||
|
if (newClient != nullptr)
|
||||||
{
|
{
|
||||||
packet->setMsgId((int)clientMsgId);
|
packet->setMsgId((int) clientMsgId);
|
||||||
handleSuback(newClient, packet);
|
handleSuback(newClient, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTTGWSubscribeHandler::handleAggregateUnsuback(Client* client, MQTTGWPacket* packet)
|
void MQTTGWSubscribeHandler::handleAggregateUnsuback(Client* client,
|
||||||
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
uint16_t msgId = packet->getMsgId();
|
uint16_t msgId = packet->getMsgId();
|
||||||
uint16_t clientMsgId = 0;
|
uint16_t clientMsgId = 0;
|
||||||
Client* newClient = _gateway->getAdapterManager()->getAggregater()->convertClient(msgId, &clientMsgId);
|
Client* newClient =
|
||||||
if ( newClient != nullptr )
|
_gateway->getAdapterManager()->getAggregater()->convertClient(msgId,
|
||||||
|
&clientMsgId);
|
||||||
|
if (newClient != nullptr)
|
||||||
{
|
{
|
||||||
packet->setMsgId((int)clientMsgId);
|
packet->setMsgId((int) clientMsgId);
|
||||||
handleUnsuback(newClient, packet);
|
handleUnsuback(newClient, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,8 @@ using namespace MQTTSNGW;
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class MQTTSNAggregateConnectionHandler
|
Class MQTTSNAggregateConnectionHandler
|
||||||
=====================================*/
|
=====================================*/
|
||||||
MQTTSNAggregateConnectionHandler::MQTTSNAggregateConnectionHandler(Gateway* gateway)
|
MQTTSNAggregateConnectionHandler::MQTTSNAggregateConnectionHandler(
|
||||||
|
Gateway* gateway)
|
||||||
{
|
{
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
}
|
}
|
||||||
@@ -36,20 +37,20 @@ MQTTSNAggregateConnectionHandler::~MQTTSNAggregateConnectionHandler()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* CONNECT
|
* CONNECT
|
||||||
*/
|
*/
|
||||||
void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet)
|
void MQTTSNAggregateConnectionHandler::handleConnect(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
MQTTSNPacket_connectData data;
|
MQTTSNPacket_connectData data;
|
||||||
if ( packet->getCONNECT(&data) == 0 )
|
if (packet->getCONNECT(&data) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return CONNACK when the client is sleeping */
|
/* return CONNACK when the client is sleeping */
|
||||||
if ( client->isSleep() || client->isAwake() )
|
if (client->isSleep() || client->isAwake())
|
||||||
{
|
{
|
||||||
MQTTSNPacket* packet = new MQTTSNPacket();
|
MQTTSNPacket* packet = new MQTTSNPacket();
|
||||||
packet->setCONNACK(MQTTSN_RC_ACCEPTED);
|
packet->setCONNACK(MQTTSN_RC_ACCEPTED);
|
||||||
@@ -70,7 +71,6 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke
|
|||||||
|
|
||||||
/* CONNECT was not sent yet. prepare Connect data */
|
/* CONNECT was not sent yet. prepare Connect data */
|
||||||
|
|
||||||
|
|
||||||
client->setSessionStatus(false);
|
client->setSessionStatus(false);
|
||||||
if (data.cleansession)
|
if (data.cleansession)
|
||||||
{
|
{
|
||||||
@@ -82,11 +82,12 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke
|
|||||||
if (topics)
|
if (topics)
|
||||||
{
|
{
|
||||||
Topic* tp = topics->getFirstTopic();
|
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);
|
tp = topics->getNextTopic(tp);
|
||||||
}
|
}
|
||||||
@@ -120,13 +121,13 @@ void MQTTSNAggregateConnectionHandler::handleConnect(Client* client, MQTTSNPacke
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* WILLMSG
|
* 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");
|
DEBUGLOG(" MQTTSNConnectionHandler::handleWillmsg WaitWillMsgFlg is off.\n");
|
||||||
return;
|
return;
|
||||||
@@ -135,10 +136,10 @@ void MQTTSNAggregateConnectionHandler::handleWillmsg(Client* client, MQTTSNPacke
|
|||||||
MQTTSNString willmsg = MQTTSNString_initializer;
|
MQTTSNString willmsg = MQTTSNString_initializer;
|
||||||
//Connect* connectData = client->getConnectData();
|
//Connect* connectData = client->getConnectData();
|
||||||
|
|
||||||
if( client->isConnectSendable() )
|
if (client->isConnectSendable())
|
||||||
{
|
{
|
||||||
/* save WillMsg in the client */
|
/* save WillMsg in the client */
|
||||||
if ( packet->getWILLMSG(&willmsg) == 0 )
|
if (packet->getWILLMSG(&willmsg) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -159,7 +160,8 @@ void MQTTSNAggregateConnectionHandler::handleWillmsg(Client* client, MQTTSNPacke
|
|||||||
/*
|
/*
|
||||||
* DISCONNECT
|
* DISCONNECT
|
||||||
*/
|
*/
|
||||||
void MQTTSNAggregateConnectionHandler::handleDisconnect(Client* client, MQTTSNPacket* packet)
|
void MQTTSNAggregateConnectionHandler::handleDisconnect(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
MQTTSNPacket* snMsg = new MQTTSNPacket();
|
MQTTSNPacket* snMsg = new MQTTSNPacket();
|
||||||
snMsg->setDISCONNECT(0);
|
snMsg->setDISCONNECT(0);
|
||||||
@@ -171,9 +173,11 @@ void MQTTSNAggregateConnectionHandler::handleDisconnect(Client* client, MQTTSNPa
|
|||||||
/*
|
/*
|
||||||
* PINGREQ
|
* 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);
|
sendStoredPublish(client);
|
||||||
client->holdPingRequest();
|
client->holdPingRequest();
|
||||||
@@ -195,7 +199,7 @@ void MQTTSNAggregateConnectionHandler::sendStoredPublish(Client* client)
|
|||||||
{
|
{
|
||||||
MQTTGWPacket* msg = nullptr;
|
MQTTGWPacket* msg = nullptr;
|
||||||
|
|
||||||
while ( ( msg = client->getClientSleepPacket() ) != nullptr )
|
while ((msg = client->getClientSleepPacket()) != nullptr)
|
||||||
{
|
{
|
||||||
client->deleteFirstClientSleepPacket(); // pop the que to delete element.
|
client->deleteFirstClientSleepPacket(); // pop the que to delete element.
|
||||||
|
|
||||||
|
|||||||
@@ -24,11 +24,10 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Adapter
|
Class Adapter
|
||||||
=====================================*/
|
=====================================*/
|
||||||
Adapter:: Adapter(Gateway* gw)
|
Adapter::Adapter(Gateway* gw)
|
||||||
{
|
{
|
||||||
_gateway = gw;
|
_gateway = gw;
|
||||||
_proxy = new Proxy(gw);
|
_proxy = new Proxy(gw);
|
||||||
@@ -37,22 +36,21 @@ Adapter:: Adapter(Gateway* gw)
|
|||||||
|
|
||||||
Adapter::~Adapter(void)
|
Adapter::~Adapter(void)
|
||||||
{
|
{
|
||||||
if ( _proxy )
|
if (_proxy)
|
||||||
{
|
{
|
||||||
delete _proxy;
|
delete _proxy;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _proxySecure )
|
if (_proxySecure)
|
||||||
{
|
{
|
||||||
delete _proxySecure;
|
delete _proxySecure;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Adapter::setup(const char* adpterName, AdapterType adapterType)
|
void Adapter::setup(const char* adpterName, AdapterType adapterType)
|
||||||
{
|
{
|
||||||
_isSecure = false;
|
_isSecure = false;
|
||||||
if ( _gateway->hasSecureConnection() )
|
if (_gateway->hasSecureConnection())
|
||||||
{
|
{
|
||||||
_isSecure = true;
|
_isSecure = true;
|
||||||
}
|
}
|
||||||
@@ -65,24 +63,25 @@ void Adapter::setup(const char* adpterName, AdapterType adapterType)
|
|||||||
string nameSecure = string(adpterName) + "-S";
|
string nameSecure = string(adpterName) + "-S";
|
||||||
idSecure.cstring = const_cast<char*>(nameSecure.c_str());
|
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);
|
setClient(client, false);
|
||||||
client->setAdapterType(adapterType);
|
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);
|
setClient(client, true);
|
||||||
client->setAdapterType(adapterType);
|
client->setAdapterType(adapterType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Client* Adapter::getClient(SensorNetAddress* addr)
|
Client* Adapter::getClient(SensorNetAddress* addr)
|
||||||
{
|
{
|
||||||
Client* client = _gateway->getClientList()->getClient(addr);
|
Client* client = _gateway->getClientList()->getClient(addr);
|
||||||
if ( !client )
|
if (!client)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
else if ( client->isQoSm1() )
|
else if (client->isQoSm1())
|
||||||
{
|
{
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@@ -95,11 +94,11 @@ Client* Adapter::getClient(SensorNetAddress* addr)
|
|||||||
const char* Adapter::getClientId(SensorNetAddress* addr)
|
const char* Adapter::getClientId(SensorNetAddress* addr)
|
||||||
{
|
{
|
||||||
Client* client = getClient(addr);
|
Client* client = getClient(addr);
|
||||||
if ( !client )
|
if (!client)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
else if ( client->isQoSm1() )
|
else if (client->isQoSm1())
|
||||||
{
|
{
|
||||||
return client->getClientId();
|
return client->getClientId();
|
||||||
}
|
}
|
||||||
@@ -112,11 +111,11 @@ const char* Adapter::getClientId(SensorNetAddress* addr)
|
|||||||
bool Adapter::isSecure(SensorNetAddress* addr)
|
bool Adapter::isSecure(SensorNetAddress* addr)
|
||||||
{
|
{
|
||||||
Client* client = getClient(addr);
|
Client* client = getClient(addr);
|
||||||
if ( !client )
|
if (!client)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else if ( client->isSecureNetwork() )
|
else if (client->isSecureNetwork())
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -133,7 +132,7 @@ bool Adapter::isSecure(void)
|
|||||||
|
|
||||||
void Adapter::setClient(Client* client, bool secure)
|
void Adapter::setClient(Client* client, bool secure)
|
||||||
{
|
{
|
||||||
if ( secure )
|
if (secure)
|
||||||
{
|
{
|
||||||
_clientSecure = client;
|
_clientSecure = client;
|
||||||
}
|
}
|
||||||
@@ -157,7 +156,7 @@ void Adapter::checkConnection(void)
|
|||||||
{
|
{
|
||||||
_proxy->checkConnection(_client);
|
_proxy->checkConnection(_client);
|
||||||
|
|
||||||
if ( _isSecure )
|
if (_isSecure)
|
||||||
{
|
{
|
||||||
_proxySecure->checkConnection(_clientSecure);
|
_proxySecure->checkConnection(_clientSecure);
|
||||||
}
|
}
|
||||||
@@ -166,15 +165,17 @@ void Adapter::checkConnection(void)
|
|||||||
void Adapter::send(MQTTSNPacket* packet, Client* client)
|
void Adapter::send(MQTTSNPacket* packet, Client* client)
|
||||||
{
|
{
|
||||||
Proxy* proxy = _proxy;
|
Proxy* proxy = _proxy;
|
||||||
if ( client->isSecureNetwork() && !_isSecure )
|
if (client->isSecureNetwork() && !_isSecure)
|
||||||
{
|
{
|
||||||
if ( _isSecure )
|
if (_isSecure)
|
||||||
{
|
{
|
||||||
proxy = _proxySecure;
|
proxy = _proxySecure;
|
||||||
}
|
}
|
||||||
else
|
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;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -185,7 +186,7 @@ void Adapter::send(MQTTSNPacket* packet, Client* client)
|
|||||||
|
|
||||||
void Adapter::resetPingTimer(bool secure)
|
void Adapter::resetPingTimer(bool secure)
|
||||||
{
|
{
|
||||||
if ( secure )
|
if (secure)
|
||||||
{
|
{
|
||||||
_proxySecure->resetPingTimer();
|
_proxySecure->resetPingTimer();
|
||||||
}
|
}
|
||||||
@@ -202,7 +203,7 @@ bool Adapter::isActive(void)
|
|||||||
|
|
||||||
void Adapter::savePacket(Client* client, MQTTSNPacket* packet)
|
void Adapter::savePacket(Client* client, MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
if ( client->isSecureNetwork())
|
if (client->isSecureNetwork())
|
||||||
{
|
{
|
||||||
_proxySecure->savePacket(client, packet);
|
_proxySecure->savePacket(client, packet);
|
||||||
}
|
}
|
||||||
@@ -212,10 +213,9 @@ void Adapter::savePacket(Client* client, MQTTSNPacket* packet)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Client* Adapter::getAdapterClient(Client* client)
|
Client* Adapter::getAdapterClient(Client* client)
|
||||||
{
|
{
|
||||||
if ( client->isSecureNetwork() )
|
if (client->isSecureNetwork())
|
||||||
{
|
{
|
||||||
return _clientSecure;
|
return _clientSecure;
|
||||||
}
|
}
|
||||||
@@ -235,7 +235,7 @@ Proxy::Proxy(Gateway* gw)
|
|||||||
}
|
}
|
||||||
Proxy::~Proxy(void)
|
Proxy::~Proxy(void)
|
||||||
{
|
{
|
||||||
if ( _suspendedPacketEventQue )
|
if (_suspendedPacketEventQue)
|
||||||
{
|
{
|
||||||
delete _suspendedPacketEventQue;
|
delete _suspendedPacketEventQue;
|
||||||
}
|
}
|
||||||
@@ -243,7 +243,8 @@ Proxy::~Proxy(void)
|
|||||||
|
|
||||||
void Proxy::checkConnection(Client* client)
|
void Proxy::checkConnection(Client* client)
|
||||||
{
|
{
|
||||||
if ( client->isDisconnect() || ( client->isConnecting() && _responseTimer.isTimeup()) )
|
if (client->isDisconnect()
|
||||||
|
|| (client->isConnecting() && _responseTimer.isTimeup()))
|
||||||
{
|
{
|
||||||
client->connectSended();
|
client->connectSended();
|
||||||
_responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL);
|
_responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL);
|
||||||
@@ -257,7 +258,8 @@ void Proxy::checkConnection(Client* client)
|
|||||||
ev->setClientRecvEvent(client, packet);
|
ev->setClientRecvEvent(client, packet);
|
||||||
_gateway->getPacketEventQue()->post(ev);
|
_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();
|
MQTTSNPacket* packet = new MQTTSNPacket();
|
||||||
MQTTSNString clientId = MQTTSNString_initializer;
|
MQTTSNString clientId = MQTTSNString_initializer;
|
||||||
@@ -268,7 +270,7 @@ void Proxy::checkConnection(Client* client)
|
|||||||
_responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL);
|
_responseTimer.start(PROXY_RESPONSE_DURATION * 1000UL);
|
||||||
_isWaitingResp = true;
|
_isWaitingResp = true;
|
||||||
|
|
||||||
if ( ++_retryCnt > PROXY_MAX_RETRY_CNT )
|
if (++_retryCnt > PROXY_MAX_RETRY_CNT)
|
||||||
{
|
{
|
||||||
client->disconnected();
|
client->disconnected();
|
||||||
}
|
}
|
||||||
@@ -276,7 +278,6 @@ void Proxy::checkConnection(Client* client)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Proxy::resetPingTimer(void)
|
void Proxy::resetPingTimer(void)
|
||||||
{
|
{
|
||||||
_keepAliveTimer.start(PROXY_KEEPALIVE_DURATION * 1000UL);
|
_keepAliveTimer.start(PROXY_KEEPALIVE_DURATION * 1000UL);
|
||||||
@@ -284,9 +285,9 @@ void Proxy::resetPingTimer(void)
|
|||||||
|
|
||||||
void Proxy::recv(MQTTSNPacket* packet, Client* client)
|
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();
|
_responseTimer.stop();
|
||||||
_retryCnt = 0;
|
_retryCnt = 0;
|
||||||
@@ -294,14 +295,14 @@ void Proxy::recv(MQTTSNPacket* packet, Client* client)
|
|||||||
sendSuspendedPacket();
|
sendSuspendedPacket();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( packet->getType() == MQTTSN_PINGRESP )
|
else if (packet->getType() == MQTTSN_PINGRESP)
|
||||||
{
|
{
|
||||||
_isWaitingResp = false;
|
_isWaitingResp = false;
|
||||||
_responseTimer.stop();
|
_responseTimer.stop();
|
||||||
_retryCnt = 0;
|
_retryCnt = 0;
|
||||||
resetPingTimer();
|
resetPingTimer();
|
||||||
}
|
}
|
||||||
else if ( packet->getType() == MQTTSN_DISCONNECT )
|
else if (packet->getType() == MQTTSN_DISCONNECT)
|
||||||
{
|
{
|
||||||
// blank
|
// blank
|
||||||
}
|
}
|
||||||
@@ -317,7 +318,7 @@ void Proxy::savePacket(Client* client, MQTTSNPacket* packet)
|
|||||||
|
|
||||||
void Proxy::sendSuspendedPacket(void)
|
void Proxy::sendSuspendedPacket(void)
|
||||||
{
|
{
|
||||||
while ( _suspendedPacketEventQue->size() )
|
while (_suspendedPacketEventQue->size())
|
||||||
{
|
{
|
||||||
Event* ev = _suspendedPacketEventQue->wait();
|
Event* ev = _suspendedPacketEventQue->wait();
|
||||||
_gateway->getPacketEventQue()->post(ev);
|
_gateway->getPacketEventQue()->post(ev);
|
||||||
|
|||||||
@@ -31,9 +31,10 @@ class EventQue;
|
|||||||
class Timer;
|
class Timer;
|
||||||
|
|
||||||
/* When you add a new type, Client::setAdapterType() and Client::isAdapter() functions must be modified. */
|
/* When you add a new type, Client::setAdapterType() and Client::isAdapter() functions must be modified. */
|
||||||
typedef enum{
|
typedef enum
|
||||||
|
{
|
||||||
Atype_QoSm1Proxy, Atype_Aggregater
|
Atype_QoSm1Proxy, Atype_Aggregater
|
||||||
}AdapterType;
|
} AdapterType;
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Adapter
|
Class Adapter
|
||||||
@@ -60,16 +61,15 @@ public:
|
|||||||
void savePacket(Client* client, MQTTSNPacket* packet);
|
void savePacket(Client* client, MQTTSNPacket* packet);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Gateway* _gateway {nullptr};
|
Gateway* _gateway { nullptr };
|
||||||
Proxy* _proxy {nullptr};
|
Proxy* _proxy { nullptr };
|
||||||
Proxy* _proxySecure {nullptr};
|
Proxy* _proxySecure { nullptr };
|
||||||
Client* _client {nullptr};
|
Client* _client { nullptr };
|
||||||
Client* _clientSecure {nullptr};
|
Client* _clientSecure { nullptr };
|
||||||
bool _isActive {false};
|
bool _isActive { false };
|
||||||
bool _isSecure{false};
|
bool _isSecure { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Proxy
|
Class Proxy
|
||||||
=====================================*/
|
=====================================*/
|
||||||
@@ -88,11 +88,12 @@ public:
|
|||||||
private:
|
private:
|
||||||
void sendSuspendedPacket(void);
|
void sendSuspendedPacket(void);
|
||||||
Gateway* _gateway;
|
Gateway* _gateway;
|
||||||
EventQue* _suspendedPacketEventQue {nullptr};
|
EventQue* _suspendedPacketEventQue
|
||||||
|
{ nullptr };
|
||||||
Timer _keepAliveTimer;
|
Timer _keepAliveTimer;
|
||||||
Timer _responseTimer;
|
Timer _responseTimer;
|
||||||
bool _isWaitingResp {false};
|
bool _isWaitingResp { false };
|
||||||
int _retryCnt {0};
|
int _retryCnt { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,20 +39,20 @@ AdapterManager::AdapterManager(Gateway* gw)
|
|||||||
_aggregater = new Aggregater(gw);
|
_aggregater = new Aggregater(gw);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder,
|
||||||
void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder, bool qosM1)
|
bool qosM1)
|
||||||
{
|
{
|
||||||
if ( aggregate )
|
if (aggregate)
|
||||||
{
|
{
|
||||||
_aggregater->initialize(gwName);
|
_aggregater->initialize(gwName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( qosM1 )
|
if (qosM1)
|
||||||
{
|
{
|
||||||
_qosm1Proxy->initialize(gwName);
|
_qosm1Proxy->initialize(gwName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( forwarder )
|
if (forwarder)
|
||||||
{
|
{
|
||||||
_forwarders->initialize(_gateway);
|
_forwarders->initialize(_gateway);
|
||||||
}
|
}
|
||||||
@@ -60,15 +60,15 @@ void AdapterManager::initialize(char* gwName, bool aggregate, bool forwarder, bo
|
|||||||
|
|
||||||
AdapterManager::~AdapterManager(void)
|
AdapterManager::~AdapterManager(void)
|
||||||
{
|
{
|
||||||
if ( _forwarders )
|
if (_forwarders)
|
||||||
{
|
{
|
||||||
delete _forwarders;
|
delete _forwarders;
|
||||||
}
|
}
|
||||||
if ( _qosm1Proxy )
|
if (_qosm1Proxy)
|
||||||
{
|
{
|
||||||
delete _qosm1Proxy;
|
delete _qosm1Proxy;
|
||||||
}
|
}
|
||||||
if ( _aggregater )
|
if (_aggregater)
|
||||||
{
|
{
|
||||||
delete _aggregater;
|
delete _aggregater;
|
||||||
}
|
}
|
||||||
@@ -91,7 +91,8 @@ Aggregater* AdapterManager::getAggregater(void)
|
|||||||
|
|
||||||
bool AdapterManager::isAggregatedClient(Client* client)
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -106,56 +107,60 @@ Client* AdapterManager::getClient(Client* client)
|
|||||||
bool secure = client->isSecureNetwork();
|
bool secure = client->isSecureNetwork();
|
||||||
Client* newClient = client;
|
Client* newClient = client;
|
||||||
|
|
||||||
if ( client->isQoSm1() )
|
if (client->isQoSm1())
|
||||||
{
|
{
|
||||||
newClient = _qosm1Proxy->getAdapterClient(client);
|
newClient = _qosm1Proxy->getAdapterClient(client);
|
||||||
_qosm1Proxy->resetPingTimer(secure);
|
_qosm1Proxy->resetPingTimer(secure);
|
||||||
}
|
}
|
||||||
else if ( client->isAggregated() )
|
else if (client->isAggregated())
|
||||||
{
|
{
|
||||||
newClient = _aggregater->getAdapterClient(client);
|
newClient = _aggregater->getAdapterClient(client);
|
||||||
_aggregater->resetPingTimer(secure);
|
_aggregater->resetPingTimer(secure);
|
||||||
}
|
}
|
||||||
else if ( client->isQoSm1Proxy() )
|
else if (client->isQoSm1Proxy())
|
||||||
{
|
{
|
||||||
_qosm1Proxy->resetPingTimer(secure);
|
_qosm1Proxy->resetPingTimer(secure);
|
||||||
}
|
}
|
||||||
else if ( client->isAggregater() )
|
else if (client->isAggregater())
|
||||||
{
|
{
|
||||||
_aggregater->resetPingTimer(secure);
|
_aggregater->resetPingTimer(secure);
|
||||||
}
|
}
|
||||||
return newClient;
|
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];
|
char pbuf[SIZE_OF_LOG_PACKET * 3];
|
||||||
Forwarder* fwd = client->getForwarder();
|
Forwarder* fwd = client->getForwarder();
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
if ( fwd )
|
if (fwd)
|
||||||
{
|
{
|
||||||
MQTTSNGWEncapsulatedPacket encap(packet);
|
MQTTSNGWEncapsulatedPacket encap(packet);
|
||||||
WirelessNodeId* wnId = fwd->getWirelessNodeId(client);
|
WirelessNodeId* wnId = fwd->getWirelessNodeId(client);
|
||||||
encap.setWirelessNodeId(wnId);
|
encap.setWirelessNodeId(wnId);
|
||||||
task->log(client, packet);
|
task->log(client, packet);
|
||||||
WRITELOG(FORMAT_Y_W_G, currentDateTime(), encap.getName(), RIGHTARROW, fwd->getId(), encap.print(pbuf));
|
WRITELOG(FORMAT_Y_W_G, currentDateTime(), encap.getName(), RIGHTARROW,
|
||||||
rc = encap.unicast(_gateway->getSensorNetwork(),fwd->getSensorNetAddr());
|
fwd->getId(), encap.print(pbuf));
|
||||||
|
rc = encap.unicast(_gateway->getSensorNetwork(),
|
||||||
|
fwd->getSensorNetAddr());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
task->log(client, packet);
|
task->log(client, packet);
|
||||||
if ( client->isQoSm1Proxy() )
|
if (client->isQoSm1Proxy())
|
||||||
{
|
{
|
||||||
_qosm1Proxy->send(packet, client);
|
_qosm1Proxy->send(packet, client);
|
||||||
}
|
}
|
||||||
else if ( client->isAggregater() )
|
else if (client->isAggregater())
|
||||||
{
|
{
|
||||||
_aggregater->send(packet, client);
|
_aggregater->send(packet, client);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rc = packet->unicast(_gateway->getSensorNetwork(), client->getSensorNetAddress());
|
rc = packet->unicast(_gateway->getSensorNetwork(),
|
||||||
|
client->getSensorNetAddress());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
@@ -163,12 +168,12 @@ int AdapterManager::unicastToClient(Client* client, MQTTSNPacket* packet, Client
|
|||||||
|
|
||||||
void AdapterManager::checkConnection(void)
|
void AdapterManager::checkConnection(void)
|
||||||
{
|
{
|
||||||
if ( _aggregater->isActive())
|
if (_aggregater->isActive())
|
||||||
{
|
{
|
||||||
_aggregater->checkConnection();
|
_aggregater->checkConnection();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _qosm1Proxy->isActive())
|
if (_qosm1Proxy->isActive())
|
||||||
{
|
{
|
||||||
_qosm1Proxy->checkConnection();
|
_qosm1Proxy->checkConnection();
|
||||||
}
|
}
|
||||||
@@ -185,25 +190,25 @@ bool AdapterManager::isAggregaterActive(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
AggregateTopicElement* AdapterManager::findTopic(Topic* topic)
|
AggregateTopicElement* AdapterManager::findTopic(Topic* topic)
|
||||||
{
|
{
|
||||||
return _aggregater->findTopic(topic);
|
return _aggregater->findTopic(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
AggregateTopicElement* AdapterManager::addAggregateTopic(Topic* topic, Client* client)
|
AggregateTopicElement* AdapterManager::addAggregateTopic(Topic* topic, Client* client)
|
||||||
{
|
{
|
||||||
return _aggregater->addAggregateTopic(topic, client);
|
return _aggregater->addAggregateTopic(topic, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void AdapterManager::removeAggregateTopic(Topic* topic, Client* client)
|
void AdapterManager::removeAggregateTopic(Topic* topic, Client* client)
|
||||||
{
|
{
|
||||||
//_aggregater->removeAggregateTopic(topic, client);
|
//_aggregater->removeAggregateTopic(topic, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AdapterManager::removeAggregateTopicList(Topics* topics, Client* client)
|
void AdapterManager::removeAggregateTopicList(Topics* topics, Client* client)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|||||||
@@ -49,18 +49,16 @@ public:
|
|||||||
bool isAggregatedClient(Client* client);
|
bool isAggregatedClient(Client* client);
|
||||||
Client* getClient(Client* client);
|
Client* getClient(Client* client);
|
||||||
Client* convertClient(uint16_t msgId, uint16_t* clientMsgId);
|
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);
|
bool isAggregaterActive(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Gateway* _gateway {nullptr};
|
Gateway* _gateway { nullptr };
|
||||||
ForwarderList* _forwarders {nullptr};
|
ForwarderList* _forwarders { nullptr };
|
||||||
QoSm1Proxy* _qosm1Proxy {nullptr};
|
QoSm1Proxy* _qosm1Proxy { nullptr };
|
||||||
Aggregater* _aggregater {nullptr};
|
Aggregater* _aggregater { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWADAPTERMANAGER_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWADAPTERMANAGER_H_ */
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ AggregateTopicElement::AggregateTopicElement(Topic* topic, Client* client)
|
|||||||
{
|
{
|
||||||
_topic = topic;
|
_topic = topic;
|
||||||
ClientTopicElement* elm = new ClientTopicElement(client);
|
ClientTopicElement* elm = new ClientTopicElement(client);
|
||||||
if ( elm != nullptr )
|
if (elm != nullptr)
|
||||||
{
|
{
|
||||||
_head = elm;
|
_head = elm;
|
||||||
_tail = elm;
|
_tail = elm;
|
||||||
@@ -61,10 +61,10 @@ AggregateTopicElement::AggregateTopicElement(Topic* topic, Client* client)
|
|||||||
AggregateTopicElement::~AggregateTopicElement(void)
|
AggregateTopicElement::~AggregateTopicElement(void)
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
if ( _head != nullptr )
|
if (_head != nullptr)
|
||||||
{
|
{
|
||||||
ClientTopicElement* p = _tail;
|
ClientTopicElement* p = _tail;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
ClientTopicElement* pPrev = p;
|
ClientTopicElement* pPrev = p;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -78,14 +78,14 @@ AggregateTopicElement::~AggregateTopicElement(void)
|
|||||||
ClientTopicElement* AggregateTopicElement::add(Client* client)
|
ClientTopicElement* AggregateTopicElement::add(Client* client)
|
||||||
{
|
{
|
||||||
ClientTopicElement* elm = new ClientTopicElement(client);
|
ClientTopicElement* elm = new ClientTopicElement(client);
|
||||||
if ( elm == nullptr )
|
if (elm == nullptr)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
if ( _head == nullptr )
|
if (_head == nullptr)
|
||||||
{
|
{
|
||||||
_head = elm;
|
_head = elm;
|
||||||
_tail = elm;
|
_tail = elm;
|
||||||
@@ -93,7 +93,7 @@ ClientTopicElement* AggregateTopicElement::add(Client* client)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
ClientTopicElement* p = find(client);
|
ClientTopicElement* p = find(client);
|
||||||
if ( p == nullptr )
|
if (p == nullptr)
|
||||||
{
|
{
|
||||||
p = _tail;
|
p = _tail;
|
||||||
_tail = elm;
|
_tail = elm;
|
||||||
@@ -113,9 +113,9 @@ ClientTopicElement* AggregateTopicElement::add(Client* client)
|
|||||||
ClientTopicElement* AggregateTopicElement::find(Client* client)
|
ClientTopicElement* AggregateTopicElement::find(Client* client)
|
||||||
{
|
{
|
||||||
ClientTopicElement* p = _head;
|
ClientTopicElement* p = _head;
|
||||||
while ( p != nullptr )
|
while (p != nullptr)
|
||||||
{
|
{
|
||||||
if ( p->_client == client)
|
if (p->_client == client)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -129,7 +129,8 @@ ClientTopicElement* AggregateTopicElement::getFirstClientTopicElement(void)
|
|||||||
return _head;
|
return _head;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientTopicElement* AggregateTopicElement::getNextClientTopicElement(ClientTopicElement* elmClient)
|
ClientTopicElement* AggregateTopicElement::getNextClientTopicElement(
|
||||||
|
ClientTopicElement* elmClient)
|
||||||
{
|
{
|
||||||
return elmClient->_next;
|
return elmClient->_next;
|
||||||
}
|
}
|
||||||
@@ -139,12 +140,12 @@ void AggregateTopicElement::eraseClient(Client* client)
|
|||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
ClientTopicElement* p = find(client);
|
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;
|
_head = p->_next;
|
||||||
if ( p->_next == nullptr ) // head & only one
|
if (p->_next == nullptr) // head & only one
|
||||||
{
|
{
|
||||||
_tail = nullptr;
|
_tail = nullptr;
|
||||||
}
|
}
|
||||||
@@ -153,7 +154,7 @@ void AggregateTopicElement::eraseClient(Client* client)
|
|||||||
p->_next->_prev = nullptr; // head & midle
|
p->_next->_prev = nullptr; // head & midle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( p->_next != nullptr ) // middle
|
else if (p->_next != nullptr) // middle
|
||||||
{
|
{
|
||||||
p->_prev->_next = p->_next;
|
p->_prev->_next = p->_next;
|
||||||
}
|
}
|
||||||
@@ -186,9 +187,9 @@ AggregateTopicElement* AggregateTopicTable::add(Topic* topic, Client* client)
|
|||||||
AggregateTopicElement* elm = nullptr;
|
AggregateTopicElement* elm = nullptr;
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
elm = getAggregateTopicElement(topic);
|
elm = getAggregateTopicElement(topic);
|
||||||
if ( elm != nullptr )
|
if (elm != nullptr)
|
||||||
{
|
{
|
||||||
if ( elm->find(client) == nullptr )
|
if (elm->find(client) == nullptr)
|
||||||
{
|
{
|
||||||
elm->add(client);
|
elm->add(client);
|
||||||
}
|
}
|
||||||
@@ -197,7 +198,7 @@ AggregateTopicElement* AggregateTopicTable::add(Topic* topic, Client* client)
|
|||||||
{
|
{
|
||||||
Topic* newTopic = topic->duplicate();
|
Topic* newTopic = topic->duplicate();
|
||||||
elm = new AggregateTopicElement(newTopic, client);
|
elm = new AggregateTopicElement(newTopic, client);
|
||||||
if ( _head == nullptr )
|
if (_head == nullptr)
|
||||||
{
|
{
|
||||||
_head = elm;
|
_head = elm;
|
||||||
_tail = elm;
|
_tail = elm;
|
||||||
@@ -220,11 +221,11 @@ void AggregateTopicTable::erase(Topic* topic, Client* client)
|
|||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
elm = getAggregateTopicElement(topic);
|
elm = getAggregateTopicElement(topic);
|
||||||
|
|
||||||
if ( elm != nullptr )
|
if (elm != nullptr)
|
||||||
{
|
{
|
||||||
elm->eraseClient(client);
|
elm->eraseClient(client);
|
||||||
}
|
}
|
||||||
if ( elm->_head == nullptr )
|
if (elm->_head == nullptr)
|
||||||
{
|
{
|
||||||
erase(elm);
|
erase(elm);
|
||||||
}
|
}
|
||||||
@@ -234,12 +235,12 @@ void AggregateTopicTable::erase(Topic* topic, Client* client)
|
|||||||
|
|
||||||
void AggregateTopicTable::erase(AggregateTopicElement* elmTopic)
|
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;
|
_head = elmTopic->_next;
|
||||||
if ( elmTopic->_next == nullptr ) // head & only one
|
if (elmTopic->_next == nullptr) // head & only one
|
||||||
{
|
{
|
||||||
_tail = nullptr;
|
_tail = nullptr;
|
||||||
}
|
}
|
||||||
@@ -248,7 +249,7 @@ void AggregateTopicTable::erase(AggregateTopicElement* elmTopic)
|
|||||||
elmTopic->_next->_prev = nullptr; // head & midle
|
elmTopic->_next->_prev = nullptr; // head & midle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( elmTopic->_next != nullptr ) // middle
|
else if (elmTopic->_next != nullptr) // middle
|
||||||
{
|
{
|
||||||
elmTopic->_prev->_next = elmTopic->_next;
|
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;
|
AggregateTopicElement* elm = _head;
|
||||||
|
|
||||||
while( elm != nullptr )
|
while (elm != nullptr)
|
||||||
{
|
{
|
||||||
if ( elm->_topic->isMatch(topic->_topicName) )
|
if (elm->_topic->isMatch(topic->_topicName))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -279,7 +281,7 @@ AggregateTopicElement* AggregateTopicTable::getAggregateTopicElement(Topic* topi
|
|||||||
ClientTopicElement* AggregateTopicTable::getClientElement(Topic* topic)
|
ClientTopicElement* AggregateTopicTable::getClientElement(Topic* topic)
|
||||||
{
|
{
|
||||||
AggregateTopicElement* elm = getAggregateTopicElement(topic);
|
AggregateTopicElement* elm = getAggregateTopicElement(topic);
|
||||||
if ( elm != nullptr )
|
if (elm != nullptr)
|
||||||
{
|
{
|
||||||
return elm->_head;
|
return elm->_head;
|
||||||
}
|
}
|
||||||
@@ -294,18 +296,18 @@ void AggregateTopicTable::print(void)
|
|||||||
AggregateTopicElement* elm = _head;
|
AggregateTopicElement* elm = _head;
|
||||||
|
|
||||||
printf("Beginning of AggregateTopicTable\n");
|
printf("Beginning of AggregateTopicTable\n");
|
||||||
while( elm != nullptr )
|
while (elm != nullptr)
|
||||||
{
|
{
|
||||||
printf("%s\n", elm->_topic->getTopicName()->c_str());
|
printf("%s\n", elm->_topic->getTopicName()->c_str());
|
||||||
|
|
||||||
ClientTopicElement* clElm = elm->getFirstClientTopicElement();
|
ClientTopicElement* clElm = elm->getFirstClientTopicElement();
|
||||||
Client* client = clElm->getClient();
|
Client* client = clElm->getClient();
|
||||||
|
|
||||||
while ( client != nullptr )
|
while (client != nullptr)
|
||||||
{
|
{
|
||||||
printf(" %s\n", client->getClientId());
|
printf(" %s\n", client->getClientId());
|
||||||
clElm = clElm->getNextClientElement();
|
clElm = clElm->getNextClientElement();
|
||||||
if ( clElm != nullptr )
|
if (clElm != nullptr)
|
||||||
{
|
{
|
||||||
client = clElm->getClient();
|
client = clElm->getClient();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,10 +49,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
void erase(AggregateTopicElement* elmTopic);
|
void erase(AggregateTopicElement* elmTopic);
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
AggregateTopicElement* _head {nullptr};
|
AggregateTopicElement* _head { nullptr };
|
||||||
AggregateTopicElement* _tail {nullptr};
|
AggregateTopicElement* _tail { nullptr };
|
||||||
int _cnt {0};
|
int _cnt { 0 };
|
||||||
int _maxSize {MAX_MESSAGEID_TABLE_SIZE};
|
int _maxSize { MAX_MESSAGEID_TABLE_SIZE };
|
||||||
};
|
};
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
@@ -68,17 +68,18 @@ public:
|
|||||||
|
|
||||||
ClientTopicElement* add(Client* client);
|
ClientTopicElement* add(Client* client);
|
||||||
ClientTopicElement* getFirstClientTopicElement(void);
|
ClientTopicElement* getFirstClientTopicElement(void);
|
||||||
ClientTopicElement* getNextClientTopicElement(ClientTopicElement* elmClient);
|
ClientTopicElement* getNextClientTopicElement(
|
||||||
|
ClientTopicElement* elmClient);
|
||||||
void eraseClient(Client* client);
|
void eraseClient(Client* client);
|
||||||
ClientTopicElement* find(Client* client);
|
ClientTopicElement* find(Client* client);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
Topic* _topic {nullptr};
|
Topic* _topic { nullptr };
|
||||||
AggregateTopicElement* _next {nullptr};
|
AggregateTopicElement* _next { nullptr };
|
||||||
AggregateTopicElement* _prev {nullptr};
|
AggregateTopicElement* _prev { nullptr };
|
||||||
ClientTopicElement* _head {nullptr};
|
ClientTopicElement* _head { nullptr };
|
||||||
ClientTopicElement* _tail {nullptr};
|
ClientTopicElement* _tail { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
@@ -96,13 +97,11 @@ public:
|
|||||||
Client* getClient(void);
|
Client* getClient(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Client* _client {nullptr};
|
Client* _client { nullptr };
|
||||||
ClientTopicElement* _next {nullptr};
|
ClientTopicElement* _next { nullptr };
|
||||||
ClientTopicElement* _prev {nullptr};
|
ClientTopicElement* _prev { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWAGGREGATETOPICTABLE_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWAGGREGATETOPICTABLE_H_ */
|
||||||
|
|||||||
@@ -26,7 +26,8 @@
|
|||||||
|
|
||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
|
|
||||||
Aggregater::Aggregater(Gateway* gw) : Adapter(gw)
|
Aggregater::Aggregater(Gateway* gw) :
|
||||||
|
Adapter(gw)
|
||||||
{
|
{
|
||||||
_gateway = gw;
|
_gateway = gw;
|
||||||
}
|
}
|
||||||
@@ -63,13 +64,12 @@ Client* Aggregater::convertClient(uint16_t msgId, uint16_t* clientMsgId)
|
|||||||
return _msgIdTable.getClientMsgId(msgId, clientMsgId);
|
return _msgIdTable.getClientMsgId(msgId, clientMsgId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint16_t Aggregater::addMessageIdTable(Client* client, uint16_t msgId)
|
uint16_t Aggregater::addMessageIdTable(Client* client, uint16_t msgId)
|
||||||
{
|
{
|
||||||
/* set Non secure client`s nextMsgId. otherwise Id is duplicated.*/
|
/* set Non secure client`s nextMsgId. otherwise Id is duplicated.*/
|
||||||
|
|
||||||
MessageIdElement* elm = _msgIdTable.add(this, client, msgId);
|
MessageIdElement* elm = _msgIdTable.add(this, client, msgId);
|
||||||
if ( elm == nullptr )
|
if (elm == nullptr)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -84,12 +84,12 @@ uint16_t Aggregater::getMsgId(Client* client, uint16_t clientMsgId)
|
|||||||
return _msgIdTable.getMsgId(client, 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);
|
return _topicTable.add(topic, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Aggregater::removeAggregateTopic(Topic* topic, Client* client)
|
void Aggregater::removeAggregateTopic(Topic* topic, Client* client)
|
||||||
{
|
{
|
||||||
_topicTable.erase(topic, client);
|
_topicTable.erase(topic, client);
|
||||||
@@ -103,7 +103,7 @@ AggregateTopicElement* Aggregater::findTopic(Topic* topic)
|
|||||||
ClientTopicElement* Aggregater::getClientElement(Topic* topic)
|
ClientTopicElement* Aggregater::getClientElement(Topic* topic)
|
||||||
{
|
{
|
||||||
AggregateTopicElement* elm = findTopic(topic);
|
AggregateTopicElement* elm = findTopic(topic);
|
||||||
if ( elm != nullptr )
|
if (elm != nullptr)
|
||||||
{
|
{
|
||||||
return elm->getFirstClientTopicElement();
|
return elm->getFirstClientTopicElement();
|
||||||
}
|
}
|
||||||
@@ -123,26 +123,26 @@ bool Aggregater::testMessageIdTable(void)
|
|||||||
Client* client = new Client();
|
Client* client = new Client();
|
||||||
uint16_t msgId = 0;
|
uint16_t msgId = 0;
|
||||||
|
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,1));
|
printf("msgId=%d\n", addMessageIdTable(client, 1));
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,2));
|
printf("msgId=%d\n", addMessageIdTable(client, 2));
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,3));
|
printf("msgId=%d\n", addMessageIdTable(client, 3));
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,1));
|
printf("msgId=%d\n", addMessageIdTable(client, 1));
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,2));
|
printf("msgId=%d\n", addMessageIdTable(client, 2));
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,3));
|
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, 4));
|
||||||
printf("msgId=%d\n", addMessageIdTable(client,4));
|
printf("msgId=%d\n", addMessageIdTable(client, 4));
|
||||||
|
|
||||||
convertClient(1,&msgId);
|
convertClient(1, &msgId);
|
||||||
printf("msgId=%d\n",msgId);
|
printf("msgId=%d\n", msgId);
|
||||||
convertClient(2,&msgId);
|
convertClient(2, &msgId);
|
||||||
printf("msgId=%d\n",msgId);
|
printf("msgId=%d\n", msgId);
|
||||||
convertClient(5,&msgId);
|
convertClient(5, &msgId);
|
||||||
printf("msgId=%d\n",msgId);
|
printf("msgId=%d\n", msgId);
|
||||||
convertClient(4,&msgId);
|
convertClient(4, &msgId);
|
||||||
printf("msgId=%d\n",msgId);
|
printf("msgId=%d\n", msgId);
|
||||||
convertClient(3,&msgId);
|
convertClient(3, &msgId);
|
||||||
printf("msgId=%d\n",msgId);
|
printf("msgId=%d\n", msgId);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ class Topics;
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class Aggregater
|
Class Aggregater
|
||||||
=====================================*/
|
=====================================*/
|
||||||
class Aggregater : public Adapter
|
class Aggregater: public Adapter
|
||||||
{
|
{
|
||||||
friend class MessageIdTable;
|
friend class MessageIdTable;
|
||||||
public:
|
public:
|
||||||
@@ -65,18 +65,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
uint16_t msgId(void);
|
uint16_t msgId(void);
|
||||||
Gateway* _gateway {nullptr};
|
Gateway* _gateway { nullptr };
|
||||||
MessageIdTable _msgIdTable;
|
MessageIdTable _msgIdTable;
|
||||||
AggregateTopicTable _topicTable;
|
AggregateTopicTable _topicTable;
|
||||||
|
|
||||||
bool _isActive {false};
|
bool _isActive { false };
|
||||||
bool _isSecure {false};
|
bool _isSecure { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWAGGREGATER_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWAGGREGATER_H_ */
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ char* currentDateTime(void);
|
|||||||
BrokerRecvTask::BrokerRecvTask(Gateway* gateway)
|
BrokerRecvTask::BrokerRecvTask(Gateway* gateway)
|
||||||
{
|
{
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
_gateway->attach((Thread*)this);
|
_gateway->attach((Thread*) this);
|
||||||
_light = nullptr;
|
_light = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ void BrokerRecvTask::run(void)
|
|||||||
/* Prepare sockets list to read */
|
/* Prepare sockets list to read */
|
||||||
Client* client = _gateway->getClientList()->getClient(0);
|
Client* client = _gateway->getClientList()->getClient(0);
|
||||||
|
|
||||||
while ( client )
|
while (client)
|
||||||
{
|
{
|
||||||
if (client->getNetwork()->isValid())
|
if (client->getNetwork()->isValid())
|
||||||
{
|
{
|
||||||
@@ -104,7 +104,7 @@ void BrokerRecvTask::run(void)
|
|||||||
{
|
{
|
||||||
client = _gateway->getClientList()->getClient(0);
|
client = _gateway->getClientList()->getClient(0);
|
||||||
|
|
||||||
while ( client )
|
while (client)
|
||||||
{
|
{
|
||||||
_light->blueLight(false);
|
_light->blueLight(false);
|
||||||
if (client->getNetwork()->isValid())
|
if (client->getNetwork()->isValid())
|
||||||
@@ -117,9 +117,9 @@ void BrokerRecvTask::run(void)
|
|||||||
/* read sockets */
|
/* read sockets */
|
||||||
_light->blueLight(true);
|
_light->blueLight(true);
|
||||||
rc = packet->recv(client->getNetwork());
|
rc = packet->recv(client->getNetwork());
|
||||||
if ( rc > 0 )
|
if (rc > 0)
|
||||||
{
|
{
|
||||||
if ( log(client, packet) == -1 )
|
if (log(client, packet) == -1)
|
||||||
{
|
{
|
||||||
delete packet;
|
delete packet;
|
||||||
goto nextClient;
|
goto nextClient;
|
||||||
@@ -132,7 +132,7 @@ void BrokerRecvTask::run(void)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( rc == 0 ) // Disconnected
|
if (rc == 0) // Disconnected
|
||||||
{
|
{
|
||||||
client->getNetwork()->close();
|
client->getNetwork()->close();
|
||||||
delete packet;
|
delete packet;
|
||||||
@@ -140,7 +140,7 @@ void BrokerRecvTask::run(void)
|
|||||||
/* delete client when the client is not authorized & session is clean */
|
/* delete client when the client is not authorized & session is clean */
|
||||||
_gateway->getClientList()->erase(client);
|
_gateway->getClientList()->erase(client);
|
||||||
|
|
||||||
if ( client )
|
if (client)
|
||||||
{
|
{
|
||||||
client = client->getNextClient();
|
client = client->getNextClient();
|
||||||
}
|
}
|
||||||
@@ -148,20 +148,35 @@ void BrokerRecvTask::run(void)
|
|||||||
}
|
}
|
||||||
else if (rc == -1)
|
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;
|
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 */
|
/* disconnect the client */
|
||||||
packet = new MQTTGWPacket();
|
packet = new MQTTGWPacket();
|
||||||
@@ -173,8 +188,7 @@ void BrokerRecvTask::run(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
nextClient:
|
nextClient: client = client->getNextClient();
|
||||||
client = client->getNextClient();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -186,30 +200,38 @@ void BrokerRecvTask::run(void)
|
|||||||
*/
|
*/
|
||||||
int BrokerRecvTask::log(Client* client, MQTTGWPacket* packet)
|
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];
|
char msgId[6];
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
|
|
||||||
switch (packet->getType())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
case CONNACK:
|
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;
|
break;
|
||||||
case PUBLISH:
|
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;
|
break;
|
||||||
case PUBACK:
|
case PUBACK:
|
||||||
case PUBREC:
|
case PUBREC:
|
||||||
case PUBREL:
|
case PUBREL:
|
||||||
case PUBCOMP:
|
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;
|
break;
|
||||||
case SUBACK:
|
case SUBACK:
|
||||||
case UNSUBACK:
|
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;
|
break;
|
||||||
case PINGRESP:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
WRITELOG("Type=%x\n", packet->getType());
|
WRITELOG("Type=%x\n", packet->getType());
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ namespace MQTTSNGW
|
|||||||
class BrokerRecvTask: public Thread
|
class BrokerRecvTask: public Thread
|
||||||
{
|
{
|
||||||
MAGIC_WORD_FOR_THREAD;
|
MAGIC_WORD_FOR_THREAD;
|
||||||
;
|
|
||||||
public:
|
public:
|
||||||
BrokerRecvTask(Gateway* gateway);
|
BrokerRecvTask(Gateway* gateway);
|
||||||
~BrokerRecvTask();
|
~BrokerRecvTask();
|
||||||
@@ -44,5 +44,4 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGWBROKERRECVTASK_H_ */
|
#endif /* MQTTSNGWBROKERRECVTASK_H_ */
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ char* currentDateTime();
|
|||||||
BrokerSendTask::BrokerSendTask(Gateway* gateway)
|
BrokerSendTask::BrokerSendTask(Gateway* gateway)
|
||||||
{
|
{
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
_gateway->attach((Thread*)this);
|
_gateway->attach((Thread*) this);
|
||||||
_gwparams = nullptr;
|
_gwparams = nullptr;
|
||||||
_light = nullptr;
|
_light = nullptr;
|
||||||
}
|
}
|
||||||
@@ -68,14 +68,14 @@ void BrokerSendTask::run()
|
|||||||
{
|
{
|
||||||
ev = _gateway->getBrokerSendQue()->wait();
|
ev = _gateway->getBrokerSendQue()->wait();
|
||||||
|
|
||||||
if ( ev->getEventType() == EtStop )
|
if (ev->getEventType() == EtStop)
|
||||||
{
|
{
|
||||||
WRITELOG("\n%s BrokerSendTask stopped.", currentDateTime());
|
WRITELOG("\n%s BrokerSendTask stopped.", currentDateTime());
|
||||||
delete ev;
|
delete ev;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ev->getEventType() == EtBrokerSend)
|
if (ev->getEventType() == EtBrokerSend)
|
||||||
{
|
{
|
||||||
client = ev->getClient();
|
client = ev->getClient();
|
||||||
packet = ev->getMQTTGWPacket();
|
packet = ev->getMQTTGWPacket();
|
||||||
@@ -83,30 +83,39 @@ void BrokerSendTask::run()
|
|||||||
/* Check Client is managed by Adapters */
|
/* Check Client is managed by Adapters */
|
||||||
client = adpMgr->getClient(client);
|
client = adpMgr->getClient(client);
|
||||||
|
|
||||||
if ( packet->getType() == CONNECT && client->getNetwork()->isValid() )
|
if (packet->getType() == CONNECT && client->getNetwork()->isValid())
|
||||||
{
|
{
|
||||||
client->getNetwork()->close();
|
client->getNetwork()->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !client->getNetwork()->isValid() )
|
if (!client->getNetwork()->isValid())
|
||||||
{
|
{
|
||||||
/* connect to the broker and send a packet */
|
/* connect to the broker and send a packet */
|
||||||
|
|
||||||
if (client->isSecureNetwork())
|
if (client->isSecureNetwork())
|
||||||
{
|
{
|
||||||
rc = client->getNetwork()->connect((const char*)_gwparams->brokerName, (const char*)_gwparams->portSecure, (const char*)_gwparams->rootCApath,
|
rc = client->getNetwork()->connect(
|
||||||
(const char*)_gwparams->rootCAfile, (const char*)_gwparams->certKey, (const char*)_gwparams->privateKey);
|
(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
|
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 */
|
/* disconnect the broker and the client */
|
||||||
WRITELOG("%s BrokerSendTask: %s can't connect to the broker. errno=%d %s %s\n",
|
WRITELOG(
|
||||||
ERRMSG_HEADER, client->getClientId(), errno, strerror(errno), ERRMSG_FOOTER);
|
"%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;
|
delete ev;
|
||||||
client->getNetwork()->close();
|
client->getNetwork()->close();
|
||||||
continue;
|
continue;
|
||||||
@@ -115,13 +124,13 @@ void BrokerSendTask::run()
|
|||||||
|
|
||||||
/* send a packet */
|
/* send a packet */
|
||||||
_light->blueLight(true);
|
_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();
|
client->connectSended();
|
||||||
}
|
}
|
||||||
else if ( packet->getType() == DISCONNECT )
|
else if (packet->getType() == DISCONNECT)
|
||||||
{
|
{
|
||||||
client->getNetwork()->close();
|
client->getNetwork()->close();
|
||||||
client->disconnected();
|
client->disconnected();
|
||||||
@@ -130,9 +139,11 @@ void BrokerSendTask::run()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
WRITELOG("%s BrokerSendTask: %s can't send a packet to the broker. errno=%d %s %s\n",
|
WRITELOG(
|
||||||
ERRMSG_HEADER, client->getClientId(), rc == -1 ? errno : 0, strerror(errno), ERRMSG_FOOTER);
|
"%s BrokerSendTask: %s can't send a packet to the broker. errno=%d %s %s\n",
|
||||||
if ( errno != EBADF )
|
ERRMSG_HEADER, client->getClientId(),
|
||||||
|
rc == -1 ? errno : 0, strerror(errno), ERRMSG_FOOTER);
|
||||||
|
if ( errno != EBADF)
|
||||||
{
|
{
|
||||||
client->getNetwork()->close();
|
client->getNetwork()->close();
|
||||||
}
|
}
|
||||||
@@ -151,22 +162,24 @@ void BrokerSendTask::run()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* write message content into stdout or Ringbuffer
|
* write message content into stdout or Ringbuffer
|
||||||
*/
|
*/
|
||||||
void BrokerSendTask::log(Client* client, MQTTGWPacket* packet)
|
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];
|
char msgId[6];
|
||||||
|
|
||||||
switch (packet->getType())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
case CONNECT:
|
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;
|
break;
|
||||||
case PUBLISH:
|
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;
|
break;
|
||||||
case SUBSCRIBE:
|
case SUBSCRIBE:
|
||||||
case UNSUBSCRIBE:
|
case UNSUBSCRIBE:
|
||||||
@@ -174,13 +187,17 @@ void BrokerSendTask::log(Client* client, MQTTGWPacket* packet)
|
|||||||
case PUBREC:
|
case PUBREC:
|
||||||
case PUBREL:
|
case PUBREL:
|
||||||
case PUBCOMP:
|
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;
|
break;
|
||||||
case PINGREQ:
|
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;
|
break;
|
||||||
case DISCONNECT:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -27,9 +27,9 @@ class Adapter;
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class BrokerSendTask
|
Class BrokerSendTask
|
||||||
=====================================*/
|
=====================================*/
|
||||||
class BrokerSendTask : public Thread
|
class BrokerSendTask: public Thread
|
||||||
{
|
{
|
||||||
MAGIC_WORD_FOR_THREAD;
|
MAGIC_WORD_FOR_THREAD;
|
||||||
friend AdapterManager;
|
friend AdapterManager;
|
||||||
public:
|
public:
|
||||||
BrokerSendTask(Gateway* gateway);
|
BrokerSendTask(Gateway* gateway);
|
||||||
|
|||||||
@@ -28,11 +28,12 @@
|
|||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
char* currentDateTime(void);
|
char* currentDateTime(void);
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Client
|
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)
|
Client::Client(bool secure)
|
||||||
{
|
{
|
||||||
@@ -63,22 +64,22 @@ Client::Client(bool secure)
|
|||||||
|
|
||||||
Client::~Client()
|
Client::~Client()
|
||||||
{
|
{
|
||||||
if ( _topics )
|
if (_topics)
|
||||||
{
|
{
|
||||||
delete _topics;
|
delete _topics;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _clientId )
|
if (_clientId)
|
||||||
{
|
{
|
||||||
free(_clientId);
|
free(_clientId);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _willTopic )
|
if (_willTopic)
|
||||||
{
|
{
|
||||||
free(_willTopic);
|
free(_willTopic);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _willMsg )
|
if (_willMsg)
|
||||||
{
|
{
|
||||||
free(_willMsg);
|
free(_willMsg);
|
||||||
}
|
}
|
||||||
@@ -117,13 +118,15 @@ void Client::deleteFirstClientSleepPacket()
|
|||||||
int Client::setClientSleepPacket(MQTTGWPacket* packet)
|
int Client::setClientSleepPacket(MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
int rc = _clientSleepPacketQue.post(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
|
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;
|
return rc;
|
||||||
}
|
}
|
||||||
@@ -141,13 +144,15 @@ void Client::deleteFirstProxyPacket()
|
|||||||
int Client::setProxyPacket(MQTTSNPacket* packet)
|
int Client::setProxyPacket(MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
int rc = _proxyPacketQue.post(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
|
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;
|
return rc;
|
||||||
}
|
}
|
||||||
@@ -177,11 +182,13 @@ void Client::clearWaitedSubTopicId(void)
|
|||||||
_waitedSubTopicIdMap.clear();
|
_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);
|
_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);
|
_waitedSubTopicIdMap.add(msgId, topicId, type);
|
||||||
}
|
}
|
||||||
@@ -224,7 +231,8 @@ bool Client::erasable(void)
|
|||||||
|
|
||||||
void Client::updateStatus(MQTTSNPacket* packet)
|
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);
|
setKeepAlive(packet);
|
||||||
}
|
}
|
||||||
@@ -240,7 +248,7 @@ void Client::updateStatus(MQTTSNPacket* packet)
|
|||||||
case MQTTSN_PUBCOMP:
|
case MQTTSN_PUBCOMP:
|
||||||
case MQTTSN_PUBREL:
|
case MQTTSN_PUBREL:
|
||||||
case MQTTSN_PUBREC:
|
case MQTTSN_PUBREC:
|
||||||
if ( _clientType != Ctype_Proxy )
|
if (_clientType != Ctype_Proxy)
|
||||||
{
|
{
|
||||||
_keepAliveTimer.start(_keepAliveMsec * 1.5);
|
_keepAliveTimer.start(_keepAliveMsec * 1.5);
|
||||||
}
|
}
|
||||||
@@ -280,8 +288,7 @@ void Client::updateStatus(MQTTSNPacket* packet)
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
} DEBUGLOG("Client Status = %s\n", theClientStatus[_status]);
|
||||||
DEBUGLOG("Client Status = %s\n", theClientStatus[_status]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::updateStatus(ClientStatus stat)
|
void Client::updateStatus(ClientStatus stat)
|
||||||
@@ -319,7 +326,7 @@ void Client::tryConnect(void)
|
|||||||
|
|
||||||
bool Client::isConnectSendable(void)
|
bool Client::isConnectSendable(void)
|
||||||
{
|
{
|
||||||
if ( _status == Cstat_Lost || _status == Cstat_TryConnecting )
|
if (_status == Cstat_Lost || _status == Cstat_TryConnecting)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -332,7 +339,7 @@ bool Client::isConnectSendable(void)
|
|||||||
uint16_t Client::getNextPacketId(void)
|
uint16_t Client::getNextPacketId(void)
|
||||||
{
|
{
|
||||||
_packetId++;
|
_packetId++;
|
||||||
if ( _packetId == 0xffff )
|
if (_packetId == 0xffff)
|
||||||
{
|
{
|
||||||
_packetId = 1;
|
_packetId = 1;
|
||||||
}
|
}
|
||||||
@@ -441,49 +448,49 @@ Client* Client::getNextClient(void)
|
|||||||
|
|
||||||
void Client::setClientId(MQTTSNString id)
|
void Client::setClientId(MQTTSNString id)
|
||||||
{
|
{
|
||||||
if ( _clientId )
|
if (_clientId)
|
||||||
{
|
{
|
||||||
free(_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));
|
memcpy(_clientId, id.cstring, strlen(id.cstring));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* save clientId into (char*)_clientId NULL terminated */
|
/* save clientId into (char*)_clientId NULL terminated */
|
||||||
_clientId = (char*)calloc(MQTTSNstrlen(id) + 1, 1);
|
_clientId = (char*) calloc(MQTTSNstrlen(id) + 1, 1);
|
||||||
unsigned char* ptr = (unsigned char*)_clientId;
|
unsigned char* ptr = (unsigned char*) _clientId;
|
||||||
writeMQTTSNString((unsigned char**)&ptr, id);
|
writeMQTTSNString((unsigned char**) &ptr, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::setWillTopic(MQTTSNString willTopic)
|
void Client::setWillTopic(MQTTSNString willTopic)
|
||||||
{
|
{
|
||||||
if ( _willTopic )
|
if (_willTopic)
|
||||||
{
|
{
|
||||||
free(_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 */
|
/* save willTopic into (char*)_willTopic with NULL termination */
|
||||||
unsigned char* ptr = (unsigned char*)_willTopic;
|
unsigned char* ptr = (unsigned char*) _willTopic;
|
||||||
writeMQTTSNString((unsigned char**)&ptr, willTopic);
|
writeMQTTSNString((unsigned char**) &ptr, willTopic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::setWillMsg(MQTTSNString willMsg)
|
void Client::setWillMsg(MQTTSNString willMsg)
|
||||||
{
|
{
|
||||||
if ( _willMsg)
|
if (_willMsg)
|
||||||
{
|
{
|
||||||
free(_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 */
|
/* save willMsg into (char*)_willMsg with NULL termination */
|
||||||
unsigned char* ptr = (unsigned char*)_willMsg;
|
unsigned char* ptr = (unsigned char*) _willMsg;
|
||||||
writeMQTTSNString((unsigned char**)&ptr, willMsg);
|
writeMQTTSNString((unsigned char**) &ptr, willMsg);
|
||||||
}
|
}
|
||||||
|
|
||||||
char* Client::getClientId(void)
|
char* Client::getClientId(void)
|
||||||
@@ -528,7 +535,7 @@ bool Client::isAggregater(void)
|
|||||||
|
|
||||||
void Client::setAdapterType(AdapterType type)
|
void Client::setAdapterType(AdapterType type)
|
||||||
{
|
{
|
||||||
switch ( type )
|
switch (type)
|
||||||
{
|
{
|
||||||
case Atype_QoSm1Proxy:
|
case Atype_QoSm1Proxy:
|
||||||
_clientType = Ctype_Proxy;
|
_clientType = Ctype_Proxy;
|
||||||
@@ -577,8 +584,6 @@ bool Client::isHoldPingReqest(void)
|
|||||||
return _holdPingRequest;
|
return _holdPingRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class WaitREGACKPacket
|
Class WaitREGACKPacket
|
||||||
=====================================*/
|
=====================================*/
|
||||||
@@ -691,4 +696,3 @@ uint8_t WaitREGACKPacketList::getCount(void)
|
|||||||
return _cnt;
|
return _cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -49,7 +49,6 @@ public:
|
|||||||
_que = new Que<T>;
|
_que = new Que<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
~PacketQue()
|
~PacketQue()
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
@@ -72,8 +71,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int post(T* packet)
|
||||||
post(T* packet)
|
|
||||||
{
|
{
|
||||||
int rc;
|
int rc;
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
@@ -113,8 +111,6 @@ private:
|
|||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class WaitREGACKPacket
|
Class WaitREGACKPacket
|
||||||
=====================================*/
|
=====================================*/
|
||||||
@@ -151,20 +147,29 @@ private:
|
|||||||
waitREGACKPacket* _end;
|
waitREGACKPacket* _end;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Client
|
Class Client
|
||||||
=====================================*/
|
=====================================*/
|
||||||
typedef enum
|
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;
|
} ClientStatus;
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
Ctype_Regular = 0, Ctype_Forwarded, Ctype_QoS_1, Ctype_Aggregated, Ctype_Proxy, Ctype_Aggregater
|
Ctype_Regular = 0,
|
||||||
}ClientType;
|
Ctype_Forwarded,
|
||||||
|
Ctype_QoS_1,
|
||||||
|
Ctype_Aggregated,
|
||||||
|
Ctype_Proxy,
|
||||||
|
Ctype_Aggregater
|
||||||
|
} ClientType;
|
||||||
|
|
||||||
class Forwarder;
|
class Forwarder;
|
||||||
|
|
||||||
@@ -193,8 +198,10 @@ public:
|
|||||||
|
|
||||||
int setClientSleepPacket(MQTTGWPacket*);
|
int setClientSleepPacket(MQTTGWPacket*);
|
||||||
int setProxyPacket(MQTTSNPacket* packet);
|
int setProxyPacket(MQTTSNPacket* packet);
|
||||||
void setWaitedPubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
|
void setWaitedPubTopicId(uint16_t msgId, uint16_t topicId,
|
||||||
void setWaitedSubTopicId(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
|
MQTTSN_topicTypes type);
|
||||||
|
void setWaitedSubTopicId(uint16_t msgId, uint16_t topicId,
|
||||||
|
MQTTSN_topicTypes type);
|
||||||
|
|
||||||
bool checkTimeover(void);
|
bool checkTimeover(void);
|
||||||
void updateStatus(MQTTSNPacket*);
|
void updateStatus(MQTTSNPacket*);
|
||||||
@@ -299,7 +306,5 @@ private:
|
|||||||
Client* _prevClient;
|
Client* _prevClient;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif /* MQTTSNGWCLIENT_H_ */
|
#endif /* MQTTSNGWCLIENT_H_ */
|
||||||
|
|||||||
@@ -51,10 +51,10 @@ ClientList::~ClientList()
|
|||||||
|
|
||||||
void ClientList::initialize(bool aggregate)
|
void ClientList::initialize(bool aggregate)
|
||||||
{
|
{
|
||||||
if (theGateway->getGWParams()->clientAuthentication )
|
if (theGateway->getGWParams()->clientAuthentication)
|
||||||
{
|
{
|
||||||
int type = TRANSPEARENT_TYPE;
|
int type = TRANSPEARENT_TYPE;
|
||||||
if ( aggregate )
|
if (aggregate)
|
||||||
{
|
{
|
||||||
type = AGGREGATER_TYPE;
|
type = AGGREGATER_TYPE;
|
||||||
}
|
}
|
||||||
@@ -62,7 +62,7 @@ void ClientList::initialize(bool aggregate)
|
|||||||
_authorize = true;
|
_authorize = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( theGateway->getGWParams()->predefinedTopic )
|
if (theGateway->getGWParams()->predefinedTopic)
|
||||||
{
|
{
|
||||||
setPredefinedTopics(aggregate);
|
setPredefinedTopics(aggregate);
|
||||||
}
|
}
|
||||||
@@ -72,15 +72,18 @@ void ClientList::setClientList(int type)
|
|||||||
{
|
{
|
||||||
if (!createList(theGateway->getGWParams()->clientListName, 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)
|
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);
|
forwarder = (data.find("forwarder") != string::npos);
|
||||||
secure = (data.find("secureConnection") != string::npos);
|
secure = (data.find("secureConnection") != string::npos);
|
||||||
stable = !(data.find("unstableLine") != 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);
|
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);
|
createClient(&netAddr, &clientId, stable, secure, type);
|
||||||
}
|
}
|
||||||
@@ -179,7 +184,8 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
|||||||
FILE* fp;
|
FILE* fp;
|
||||||
char buf[MAX_CLIENTID_LENGTH + 256];
|
char buf[MAX_CLIENTID_LENGTH + 256];
|
||||||
size_t pos0, pos1;
|
size_t pos0, pos1;
|
||||||
MQTTSNString clientId = MQTTSNString_initializer;;
|
MQTTSNString clientId = MQTTSNString_initializer;
|
||||||
|
;
|
||||||
bool rc = false;
|
bool rc = false;
|
||||||
|
|
||||||
if ((fp = fopen(fileName, "r")) != 0)
|
if ((fp = fopen(fileName, "r")) != 0)
|
||||||
@@ -201,12 +207,12 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
|||||||
}
|
}
|
||||||
|
|
||||||
pos0 = data.find_first_of(",");
|
pos0 = data.find_first_of(",");
|
||||||
pos1 = data.find(",", pos0 + 1) ;
|
pos1 = data.find(",", pos0 + 1);
|
||||||
string id = data.substr(0, pos0);
|
string id = data.substr(0, pos0);
|
||||||
clientId.cstring = strdup(id.c_str());
|
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));
|
uint16_t topicID = stoul(data.substr(pos1 + 1));
|
||||||
createPredefinedTopic( &clientId, topicName, topicID, aggregate);
|
createPredefinedTopic(&clientId, topicName, topicID, aggregate);
|
||||||
free(clientId.cstring);
|
free(clientId.cstring);
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
@@ -214,7 +220,8 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
|||||||
}
|
}
|
||||||
else
|
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 false;
|
||||||
}
|
}
|
||||||
return rc;
|
return rc;
|
||||||
@@ -222,7 +229,7 @@ bool ClientList::readPredefinedList(const char* fileName, bool aggregate)
|
|||||||
|
|
||||||
void ClientList::erase(Client*& client)
|
void ClientList::erase(Client*& client)
|
||||||
{
|
{
|
||||||
if ( !_authorize && client->erasable())
|
if (!_authorize && client->erasable())
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
Client* prev = client->_prevClient;
|
Client* prev = client->_prevClient;
|
||||||
@@ -247,7 +254,7 @@ void ClientList::erase(Client*& client)
|
|||||||
}
|
}
|
||||||
_clientCnt--;
|
_clientCnt--;
|
||||||
Forwarder* fwd = client->getForwarder();
|
Forwarder* fwd = client->getForwarder();
|
||||||
if ( fwd )
|
if (fwd)
|
||||||
{
|
{
|
||||||
fwd->eraseClient(client);
|
fwd->eraseClient(client);
|
||||||
}
|
}
|
||||||
@@ -259,14 +266,14 @@ void ClientList::erase(Client*& client)
|
|||||||
|
|
||||||
Client* ClientList::getClient(SensorNetAddress* addr)
|
Client* ClientList::getClient(SensorNetAddress* addr)
|
||||||
{
|
{
|
||||||
if ( addr )
|
if (addr)
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
Client* client = _firstClient;
|
Client* client = _firstClient;
|
||||||
|
|
||||||
while (client != nullptr)
|
while (client != nullptr)
|
||||||
{
|
{
|
||||||
if (client->getSensorNetAddress()->isMatch(addr) )
|
if (client->getSensorNetAddress()->isMatch(addr))
|
||||||
{
|
{
|
||||||
_mutex.unlock();
|
_mutex.unlock();
|
||||||
return client;
|
return client;
|
||||||
@@ -282,9 +289,9 @@ Client* ClientList::getClient(int index)
|
|||||||
{
|
{
|
||||||
Client* client = _firstClient;
|
Client* client = _firstClient;
|
||||||
int p = 0;
|
int p = 0;
|
||||||
while ( client != nullptr )
|
while (client != nullptr)
|
||||||
{
|
{
|
||||||
if ( p == index )
|
if (p == index)
|
||||||
{
|
{
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@@ -297,21 +304,21 @@ Client* ClientList::getClient(int index)
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Client* ClientList::getClient(MQTTSNString* clientId)
|
Client* ClientList::getClient(MQTTSNString* clientId)
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
Client* client = _firstClient;
|
Client* client = _firstClient;
|
||||||
const char* clID =clientId->cstring;
|
const char* clID = clientId->cstring;
|
||||||
|
|
||||||
if (clID == nullptr )
|
if (clID == nullptr)
|
||||||
{
|
{
|
||||||
clID = clientId->lenstring.data;
|
clID = clientId->lenstring.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (client != nullptr)
|
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();
|
_mutex.unlock();
|
||||||
return client;
|
return client;
|
||||||
@@ -322,35 +329,37 @@ Client* ClientList::getClient(MQTTSNString* clientId)
|
|||||||
return 0;
|
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);
|
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;
|
Client* client = nullptr;
|
||||||
|
|
||||||
/* anonimous clients */
|
/* anonimous clients */
|
||||||
if ( _clientCnt > MAX_CLIENTS )
|
if (_clientCnt > MAX_CLIENTS)
|
||||||
{
|
{
|
||||||
return 0; // full of clients
|
return 0; // full of clients
|
||||||
}
|
}
|
||||||
|
|
||||||
client = getClient(addr);
|
client = getClient(addr);
|
||||||
if ( client )
|
if (client)
|
||||||
{
|
{
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* creat a new client */
|
/* creat a new client */
|
||||||
client = new Client(secure);
|
client = new Client(secure);
|
||||||
if ( addr )
|
if (addr)
|
||||||
{
|
{
|
||||||
client->setClientAddress(addr);
|
client->setClientAddress(addr);
|
||||||
}
|
}
|
||||||
client->setSensorNetType(unstableLine);
|
client->setSensorNetType(unstableLine);
|
||||||
if ( MQTTSNstrlen(*clientId) )
|
if (MQTTSNstrlen(*clientId))
|
||||||
{
|
{
|
||||||
client->setClientId(*clientId);
|
client->setClientId(*clientId);
|
||||||
}
|
}
|
||||||
@@ -362,11 +371,11 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
|||||||
free(dummyId.cstring);
|
free(dummyId.cstring);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( type == AGGREGATER_TYPE )
|
if (type == AGGREGATER_TYPE)
|
||||||
{
|
{
|
||||||
client->setAggregated();
|
client->setAggregated();
|
||||||
}
|
}
|
||||||
else if ( type == QOSM1PROXY_TYPE )
|
else if (type == QOSM1PROXY_TYPE)
|
||||||
{
|
{
|
||||||
client->setQoSm1();
|
client->setQoSm1();
|
||||||
}
|
}
|
||||||
@@ -374,7 +383,7 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
|||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
/* add the list */
|
/* add the list */
|
||||||
if ( _firstClient == nullptr )
|
if (_firstClient == nullptr)
|
||||||
{
|
{
|
||||||
_firstClient = client;
|
_firstClient = client;
|
||||||
_endClient = client;
|
_endClient = client;
|
||||||
@@ -390,47 +399,49 @@ Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
|||||||
return client;
|
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;
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Client* client = getClient(clientId);
|
Client* client = getClient(clientId);
|
||||||
|
|
||||||
if ( _authorize && client == nullptr )
|
if (_authorize && client == nullptr)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* anonimous clients */
|
/* anonimous clients */
|
||||||
if ( _clientCnt > MAX_CLIENTS )
|
if (_clientCnt > MAX_CLIENTS)
|
||||||
{
|
{
|
||||||
return nullptr; // full of clients
|
return nullptr; // full of clients
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( client == nullptr )
|
if (client == nullptr)
|
||||||
{
|
{
|
||||||
/* creat a new client */
|
/* creat a new client */
|
||||||
client = new Client();
|
client = new Client();
|
||||||
client->setClientId(*clientId);
|
client->setClientId(*clientId);
|
||||||
if ( aggregate )
|
if (aggregate)
|
||||||
{
|
{
|
||||||
client->setAggregated();
|
client->setAggregated();
|
||||||
}
|
}
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
|
|
||||||
/* add the list */
|
/* add the list */
|
||||||
if ( _firstClient == nullptr )
|
if (_firstClient == nullptr)
|
||||||
{
|
{
|
||||||
_firstClient = client;
|
_firstClient = client;
|
||||||
_endClient = client;
|
_endClient = client;
|
||||||
@@ -446,7 +457,7 @@ Client* ClientList::createPredefinedTopic( MQTTSNString* clientId, string topicN
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create Topic & Add it
|
// 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;
|
client->_hasPredefTopic = true;
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
@@ -462,4 +473,3 @@ bool ClientList::isAuthorized()
|
|||||||
return _authorize;
|
return _authorize;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,10 @@ public:
|
|||||||
void setClientList(int type);
|
void setClientList(int type);
|
||||||
void setPredefinedTopics(bool aggregate);
|
void setPredefinedTopics(bool aggregate);
|
||||||
void erase(Client*&);
|
void erase(Client*&);
|
||||||
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId,int type);
|
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||||
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine, bool secure, int type);
|
int type);
|
||||||
|
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId,
|
||||||
|
bool unstableLine, bool secure, int type);
|
||||||
bool createList(const char* fileName, int type);
|
bool createList(const char* fileName, int type);
|
||||||
Client* getClient(SensorNetAddress* addr);
|
Client* getClient(SensorNetAddress* addr);
|
||||||
Client* getClient(MQTTSNString* clientId);
|
Client* getClient(MQTTSNString* clientId);
|
||||||
@@ -55,18 +57,16 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool readPredefinedList(const char* fileName, bool _aggregate);
|
bool readPredefinedList(const char* fileName, bool _aggregate);
|
||||||
Gateway* _gateway {nullptr};
|
Gateway* _gateway { nullptr };
|
||||||
Client* createPredefinedTopic( MQTTSNString* clientId, string topicName, uint16_t toipcId, bool _aggregate);
|
Client* createPredefinedTopic(MQTTSNString* clientId, string topicName,
|
||||||
|
uint16_t toipcId, bool _aggregate);
|
||||||
Client* _firstClient;
|
Client* _firstClient;
|
||||||
Client* _endClient;
|
Client* _endClient;
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
uint16_t _clientCnt;
|
uint16_t _clientCnt;
|
||||||
bool _authorize {false};
|
bool _authorize { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTLIST_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWCLIENTLIST_H_ */
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ char* currentDateTime(void);
|
|||||||
ClientRecvTask::ClientRecvTask(Gateway* gateway)
|
ClientRecvTask::ClientRecvTask(Gateway* gateway)
|
||||||
{
|
{
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
_gateway->attach((Thread*)this);
|
_gateway->attach((Thread*) this);
|
||||||
_sensorNetwork = _gateway->getSensorNetwork();
|
_sensorNetwork = _gateway->getSensorNetwork();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@ ClientRecvTask::~ClientRecvTask()
|
|||||||
*/
|
*/
|
||||||
void ClientRecvTask::initialize(int argc, char** argv)
|
void ClientRecvTask::initialize(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if ( _sensorNetwork->initialize() < 0 )
|
if (_sensorNetwork->initialize() < 0)
|
||||||
{
|
{
|
||||||
throw Exception(" Can't open the sensor network.\n");
|
throw Exception(" Can't open the sensor network.\n");
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,8 @@ void ClientRecvTask::run()
|
|||||||
Event* ev = nullptr;
|
Event* ev = nullptr;
|
||||||
AdapterManager* adpMgr = _gateway->getAdapterManager();
|
AdapterManager* adpMgr = _gateway->getAdapterManager();
|
||||||
QoSm1Proxy* qosm1Proxy = adpMgr->getQoSm1Proxy();
|
QoSm1Proxy* qosm1Proxy = adpMgr->getQoSm1Proxy();
|
||||||
int clientType = adpMgr->isAggregaterActive() ? AGGREGATER_TYPE : TRANSPEARENT_TYPE;
|
int clientType =
|
||||||
|
adpMgr->isAggregaterActive() ? AGGREGATER_TYPE : TRANSPEARENT_TYPE;
|
||||||
ClientList* clientList = _gateway->getClientList();
|
ClientList* clientList = _gateway->getClientList();
|
||||||
EventQue* packetEventQue = _gateway->getPacketEventQue();
|
EventQue* packetEventQue = _gateway->getPacketEventQue();
|
||||||
|
|
||||||
@@ -82,19 +83,20 @@ void ClientRecvTask::run()
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packetLen < 2 )
|
if (packetLen < 2)
|
||||||
{
|
{
|
||||||
delete packet;
|
delete packet;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( packet->getType() <= MQTTSN_ADVERTISE || packet->getType() == MQTTSN_GWINFO )
|
if (packet->getType() <= MQTTSN_ADVERTISE
|
||||||
|
|| packet->getType() == MQTTSN_GWINFO)
|
||||||
{
|
{
|
||||||
delete packet;
|
delete packet;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( packet->getType() == MQTTSN_SEARCHGW )
|
if (packet->getType() == MQTTSN_SEARCHGW)
|
||||||
{
|
{
|
||||||
/* write log and post Event */
|
/* write log and post Event */
|
||||||
log(0, packet, 0);
|
log(0, packet, 0);
|
||||||
@@ -104,23 +106,26 @@ void ClientRecvTask::run()
|
|||||||
continue;
|
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;
|
MQTTSNString fwdName = MQTTSNString_initializer;
|
||||||
fwdName.cstring = const_cast<char *>( fwd->getName() );
|
fwdName.cstring = const_cast<char *>(fwd->getName());
|
||||||
log(0, packet, &fwdName);
|
log(0, packet, &fwdName);
|
||||||
|
|
||||||
/* get the packet from the encapsulation message */
|
/* get the packet from the encapsulation message */
|
||||||
MQTTSNGWEncapsulatedPacket encap;
|
MQTTSNGWEncapsulatedPacket encap;
|
||||||
encap.desirialize(packet->getPacketData(), packet->getPacketLength());
|
encap.desirialize(packet->getPacketData(),
|
||||||
nodeId.setId( encap.getWirelessNodeId() );
|
packet->getPacketLength());
|
||||||
|
nodeId.setId(encap.getWirelessNodeId());
|
||||||
client = fwd->getClient(&nodeId);
|
client = fwd->getClient(&nodeId);
|
||||||
packet = encap.getMQTTSNPacket();
|
packet = encap.getMQTTSNPacket();
|
||||||
}
|
}
|
||||||
@@ -129,36 +134,39 @@ void ClientRecvTask::run()
|
|||||||
{
|
{
|
||||||
/* Check the client belonging to QoS-1Proxy ? */
|
/* Check the client belonging to QoS-1Proxy ? */
|
||||||
|
|
||||||
if ( qosm1Proxy->isActive() )
|
if (qosm1Proxy->isActive())
|
||||||
{
|
{
|
||||||
const char* clientName = qosm1Proxy->getClientId(senderAddr);
|
const char* clientName = qosm1Proxy->getClientId(senderAddr);
|
||||||
|
|
||||||
if ( clientName != nullptr )
|
if (clientName != nullptr)
|
||||||
{
|
{
|
||||||
client = qosm1Proxy->getClient();
|
client = qosm1Proxy->getClient();
|
||||||
|
|
||||||
if ( !packet->isQoSMinusPUBLISH() )
|
if (!packet->isQoSMinusPUBLISH())
|
||||||
{
|
{
|
||||||
log(clientName, packet);
|
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;
|
delete packet;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( client == nullptr )
|
if (client == nullptr)
|
||||||
{
|
{
|
||||||
client = _gateway->getClientList()->getClient(senderAddr);
|
client = _gateway->getClientList()->getClient(senderAddr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( client != nullptr )
|
if (client != nullptr)
|
||||||
{
|
{
|
||||||
/* write log and post Event */
|
/* write log and post Event */
|
||||||
log(client, packet, 0);
|
log(client, packet, 0);
|
||||||
ev = new Event();
|
ev = new Event();
|
||||||
ev->setClientRecvEvent(client,packet);
|
ev->setClientRecvEvent(client, packet);
|
||||||
packetEventQue->post(ev);
|
packetEventQue->post(ev);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -168,32 +176,36 @@ void ClientRecvTask::run()
|
|||||||
{
|
{
|
||||||
MQTTSNPacket_connectData data;
|
MQTTSNPacket_connectData data;
|
||||||
memset(&data, 0, sizeof(MQTTSNPacket_connectData));
|
memset(&data, 0, sizeof(MQTTSNPacket_connectData));
|
||||||
if ( !packet->getCONNECT(&data) )
|
if (!packet->getCONNECT(&data))
|
||||||
{
|
{
|
||||||
log(0, packet, &data.clientID);
|
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;
|
delete packet;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
client = clientList->getClient(&data.clientID);
|
client = clientList->getClient(&data.clientID);
|
||||||
|
|
||||||
if ( fwd != nullptr )
|
if (fwd != nullptr)
|
||||||
{
|
{
|
||||||
if ( client == nullptr )
|
if (client == nullptr)
|
||||||
{
|
{
|
||||||
/* create a new client */
|
/* 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. */
|
/* Add to a forwarded client list of forwarder. */
|
||||||
fwd->addClient(client, &nodeId);
|
fwd->addClient(client, &nodeId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( client )
|
if (client)
|
||||||
{
|
{
|
||||||
/* Authentication is not required */
|
/* Authentication is not required */
|
||||||
if ( _gateway->getGWParams()->clientAuthentication == false)
|
if (_gateway->getGWParams()->clientAuthentication
|
||||||
|
== false)
|
||||||
{
|
{
|
||||||
client->setClientAddress(senderAddr);
|
client->setClientAddress(senderAddr);
|
||||||
}
|
}
|
||||||
@@ -201,15 +213,19 @@ void ClientRecvTask::run()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* create a new client */
|
/* create a new client */
|
||||||
client = clientList->createClient(senderAddr, &data.clientID, clientType);
|
client = clientList->createClient(senderAddr,
|
||||||
|
&data.clientID, clientType);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log(client, packet, &data.clientID);
|
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;
|
delete packet;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -222,13 +238,20 @@ void ClientRecvTask::run()
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
log(client, packet, 0);
|
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
|
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;
|
delete packet;
|
||||||
}
|
}
|
||||||
@@ -241,21 +264,21 @@ void ClientRecvTask::log(Client* client, MQTTSNPacket* packet, MQTTSNString* id)
|
|||||||
const char* clientId;
|
const char* clientId;
|
||||||
char cstr[MAX_CLIENTID_LENGTH + 1];
|
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;
|
clientId = cstr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memset((void*)cstr, 0, id->lenstring.len + 1);
|
memset((void*) cstr, 0, id->lenstring.len + 1);
|
||||||
strncpy(cstr, id->lenstring.data, id->lenstring.len );
|
strncpy(cstr, id->lenstring.data, id->lenstring.len);
|
||||||
clientId = cstr;
|
clientId = cstr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( client )
|
else if (client)
|
||||||
{
|
{
|
||||||
clientId = client->getClientId();
|
clientId = client->getClientId();
|
||||||
}
|
}
|
||||||
@@ -275,37 +298,46 @@ void ClientRecvTask::log(const char* clientId, MQTTSNPacket* packet)
|
|||||||
switch (packet->getType())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
case MQTTSN_SEARCHGW:
|
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;
|
break;
|
||||||
case MQTTSN_CONNECT:
|
case MQTTSN_CONNECT:
|
||||||
case MQTTSN_PINGREQ:
|
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;
|
break;
|
||||||
case MQTTSN_DISCONNECT:
|
case MQTTSN_DISCONNECT:
|
||||||
case MQTTSN_WILLTOPICUPD:
|
case MQTTSN_WILLTOPICUPD:
|
||||||
case MQTTSN_WILLMSGUPD:
|
case MQTTSN_WILLMSGUPD:
|
||||||
case MQTTSN_WILLTOPIC:
|
case MQTTSN_WILLTOPIC:
|
||||||
case MQTTSN_WILLMSG:
|
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;
|
break;
|
||||||
case MQTTSN_PUBLISH:
|
case MQTTSN_PUBLISH:
|
||||||
case MQTTSN_REGISTER:
|
case MQTTSN_REGISTER:
|
||||||
case MQTTSN_SUBSCRIBE:
|
case MQTTSN_SUBSCRIBE:
|
||||||
case MQTTSN_UNSUBSCRIBE:
|
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;
|
break;
|
||||||
case MQTTSN_REGACK:
|
case MQTTSN_REGACK:
|
||||||
case MQTTSN_PUBACK:
|
case MQTTSN_PUBACK:
|
||||||
case MQTTSN_PUBREC:
|
case MQTTSN_PUBREC:
|
||||||
case MQTTSN_PUBREL:
|
case MQTTSN_PUBREL:
|
||||||
case MQTTSN_PUBCOMP:
|
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;
|
break;
|
||||||
case MQTTSN_ENCAPSULATED:
|
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;
|
break;
|
||||||
default:
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,9 +26,9 @@ class AdapterManager;
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class ClientRecvTask
|
Class ClientRecvTask
|
||||||
=====================================*/
|
=====================================*/
|
||||||
class ClientRecvTask:public Thread
|
class ClientRecvTask: public Thread
|
||||||
{
|
{
|
||||||
MAGIC_WORD_FOR_THREAD;
|
MAGIC_WORD_FOR_THREAD;
|
||||||
friend AdapterManager;
|
friend AdapterManager;
|
||||||
public:
|
public:
|
||||||
ClientRecvTask(Gateway*);
|
ClientRecvTask(Gateway*);
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ char* currentDateTime(void);
|
|||||||
ClientSendTask::ClientSendTask(Gateway* gateway)
|
ClientSendTask::ClientSendTask(Gateway* gateway)
|
||||||
{
|
{
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
_gateway->attach((Thread*)this);
|
_gateway->attach((Thread*) this);
|
||||||
_sensorNetwork = _gateway->getSensorNetwork();
|
_sensorNetwork = _gateway->getSensorNetwork();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ void ClientSendTask::run()
|
|||||||
{
|
{
|
||||||
Event* ev = _gateway->getClientSendQue()->wait();
|
Event* ev = _gateway->getClientSendQue()->wait();
|
||||||
|
|
||||||
if (ev->getEventType() == EtStop || _gateway->IsStopping() )
|
if (ev->getEventType() == EtStop || _gateway->IsStopping())
|
||||||
{
|
{
|
||||||
WRITELOG("\n%s ClientSendTask stopped.", currentDateTime());
|
WRITELOG("\n%s ClientSendTask stopped.", currentDateTime());
|
||||||
delete ev;
|
delete ev;
|
||||||
@@ -61,9 +61,10 @@ void ClientSendTask::run()
|
|||||||
packet = ev->getMQTTSNPacket();
|
packet = ev->getMQTTSNPacket();
|
||||||
log(client, packet);
|
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);
|
ERRMSG_HEADER, errno, ERRMSG_FOOTER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,10 +83,14 @@ void ClientSendTask::run()
|
|||||||
rc = packet->unicast(_sensorNetwork, ev->getSensorNetAddress());
|
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",
|
WRITELOG(
|
||||||
ERRMSG_HEADER, (client ? (const char*)client->getClientId() : UNKNOWNCL ), errno, ERRMSG_FOOTER);
|
"%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;
|
delete ev;
|
||||||
@@ -96,13 +101,15 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
|
|||||||
{
|
{
|
||||||
char pbuf[SIZE_OF_LOG_PACKET * 3 + 1];
|
char pbuf[SIZE_OF_LOG_PACKET * 3 + 1];
|
||||||
char msgId[6];
|
char msgId[6];
|
||||||
const char* clientId = client ? (const char*)client->getClientId() : UNKNOWNCL ;
|
const char* clientId =
|
||||||
|
client ? (const char*) client->getClientId() : UNKNOWNCL;
|
||||||
|
|
||||||
switch (packet->getType())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
case MQTTSN_ADVERTISE:
|
case MQTTSN_ADVERTISE:
|
||||||
case MQTTSN_GWINFO:
|
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;
|
break;
|
||||||
case MQTTSN_CONNACK:
|
case MQTTSN_CONNACK:
|
||||||
case MQTTSN_DISCONNECT:
|
case MQTTSN_DISCONNECT:
|
||||||
@@ -111,11 +118,14 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
|
|||||||
case MQTTSN_WILLTOPICRESP:
|
case MQTTSN_WILLTOPICRESP:
|
||||||
case MQTTSN_WILLMSGRESP:
|
case MQTTSN_WILLMSGRESP:
|
||||||
case MQTTSN_PINGRESP:
|
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;
|
break;
|
||||||
case MQTTSN_REGISTER:
|
case MQTTSN_REGISTER:
|
||||||
case MQTTSN_PUBLISH:
|
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;
|
break;
|
||||||
case MQTTSN_REGACK:
|
case MQTTSN_REGACK:
|
||||||
case MQTTSN_PUBACK:
|
case MQTTSN_PUBACK:
|
||||||
@@ -124,7 +134,9 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
|
|||||||
case MQTTSN_PUBCOMP:
|
case MQTTSN_PUBCOMP:
|
||||||
case MQTTSN_SUBACK:
|
case MQTTSN_SUBACK:
|
||||||
case MQTTSN_UNSUBACK:
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class AdapterManager;
|
|||||||
=====================================*/
|
=====================================*/
|
||||||
class ClientSendTask: public Thread
|
class ClientSendTask: public Thread
|
||||||
{
|
{
|
||||||
MAGIC_WORD_FOR_THREAD;
|
MAGIC_WORD_FOR_THREAD;
|
||||||
friend AdapterManager;
|
friend AdapterManager;
|
||||||
public:
|
public:
|
||||||
ClientSendTask(Gateway* gateway);
|
ClientSendTask(Gateway* gateway);
|
||||||
|
|||||||
@@ -42,7 +42,8 @@ MQTTSNConnectionHandler::~MQTTSNConnectionHandler()
|
|||||||
void MQTTSNConnectionHandler::sendADVERTISE()
|
void MQTTSNConnectionHandler::sendADVERTISE()
|
||||||
{
|
{
|
||||||
MQTTSNPacket* adv = new MQTTSNPacket();
|
MQTTSNPacket* adv = new MQTTSNPacket();
|
||||||
adv->setADVERTISE(_gateway->getGWParams()->gatewayId, _gateway->getGWParams()->keepAlive);
|
adv->setADVERTISE(_gateway->getGWParams()->gatewayId,
|
||||||
|
_gateway->getGWParams()->keepAlive);
|
||||||
Event* ev1 = new Event();
|
Event* ev1 = new Event();
|
||||||
ev1->setBrodcastEvent(adv); //broadcast
|
ev1->setBrodcastEvent(adv); //broadcast
|
||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
@@ -66,16 +67,17 @@ void MQTTSNConnectionHandler::handleSearchgw(MQTTSNPacket* packet)
|
|||||||
/*
|
/*
|
||||||
* CONNECT
|
* CONNECT
|
||||||
*/
|
*/
|
||||||
void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet)
|
void MQTTSNConnectionHandler::handleConnect(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
MQTTSNPacket_connectData data;
|
MQTTSNPacket_connectData data;
|
||||||
if ( packet->getCONNECT(&data) == 0 )
|
if (packet->getCONNECT(&data) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* return CONNACK when the client is sleeping */
|
/* return CONNACK when the client is sleeping */
|
||||||
if ( client->isSleep() || client->isAwake() )
|
if (client->isSleep() || client->isAwake())
|
||||||
{
|
{
|
||||||
MQTTSNPacket* packet = new MQTTSNPacket();
|
MQTTSNPacket* packet = new MQTTSNPacket();
|
||||||
packet->setCONNACK(MQTTSN_RC_ACCEPTED);
|
packet->setCONNACK(MQTTSN_RC_ACCEPTED);
|
||||||
@@ -90,7 +92,7 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
|
|||||||
//* clear ConnectData of Client */
|
//* clear ConnectData of Client */
|
||||||
Connect* connectData = client->getConnectData();
|
Connect* connectData = client->getConnectData();
|
||||||
memset(connectData, 0, sizeof(Connect));
|
memset(connectData, 0, sizeof(Connect));
|
||||||
if ( !client->isAdapter() )
|
if (!client->isAdapter())
|
||||||
{
|
{
|
||||||
client->disconnected();
|
client->disconnected();
|
||||||
}
|
}
|
||||||
@@ -125,7 +127,8 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
|
|||||||
/* renew the TopicList */
|
/* renew the TopicList */
|
||||||
if (topics)
|
if (topics)
|
||||||
{
|
{
|
||||||
topics->eraseNormal();;
|
topics->eraseNormal();
|
||||||
|
;
|
||||||
}
|
}
|
||||||
client->setSessionStatus(true);
|
client->setSessionStatus(true);
|
||||||
}
|
}
|
||||||
@@ -146,7 +149,9 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
|
|||||||
/* CONNECT message was not qued in.
|
/* CONNECT message was not qued in.
|
||||||
* create CONNECT message & send it to the broker */
|
* create CONNECT message & send it to the broker */
|
||||||
MQTTGWPacket* mqMsg = new MQTTGWPacket();
|
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();
|
Event* ev1 = new Event();
|
||||||
ev1->setBrokerSendEvent(client, mqMsg);
|
ev1->setBrokerSendEvent(client, mqMsg);
|
||||||
_gateway->getBrokerSendQue()->post(ev1);
|
_gateway->getBrokerSendQue()->post(ev1);
|
||||||
@@ -156,13 +161,14 @@ void MQTTSNConnectionHandler::handleConnect(Client* client, MQTTSNPacket* packet
|
|||||||
/*
|
/*
|
||||||
* WILLTOPIC
|
* WILLTOPIC
|
||||||
*/
|
*/
|
||||||
void MQTTSNConnectionHandler::handleWilltopic(Client* client, MQTTSNPacket* packet)
|
void MQTTSNConnectionHandler::handleWilltopic(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
int willQos;
|
int willQos;
|
||||||
uint8_t willRetain;
|
uint8_t willRetain;
|
||||||
MQTTSNString willTopic = MQTTSNString_initializer;
|
MQTTSNString willTopic = MQTTSNString_initializer;
|
||||||
|
|
||||||
if ( packet->getWILLTOPIC(&willQos, &willRetain, &willTopic) == 0 )
|
if (packet->getWILLTOPIC(&willQos, &willRetain, &willTopic) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -186,9 +192,10 @@ void MQTTSNConnectionHandler::handleWilltopic(Client* client, MQTTSNPacket* pack
|
|||||||
/*
|
/*
|
||||||
* WILLMSG
|
* 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");
|
DEBUGLOG(" MQTTSNConnectionHandler::handleWillmsg WaitWillMsgFlg is off.\n");
|
||||||
return;
|
return;
|
||||||
@@ -197,10 +204,10 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
|
|||||||
MQTTSNString willmsg = MQTTSNString_initializer;
|
MQTTSNString willmsg = MQTTSNString_initializer;
|
||||||
Connect* connectData = client->getConnectData();
|
Connect* connectData = client->getConnectData();
|
||||||
|
|
||||||
if( client->isConnectSendable() )
|
if (client->isConnectSendable())
|
||||||
{
|
{
|
||||||
/* save WillMsg in the client */
|
/* save WillMsg in the client */
|
||||||
if ( packet->getWILLMSG(&willmsg) == 0 )
|
if (packet->getWILLMSG(&willmsg) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -209,7 +216,9 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
|
|||||||
/* create CONNECT message */
|
/* create CONNECT message */
|
||||||
MQTTGWPacket* mqttPacket = new MQTTGWPacket();
|
MQTTGWPacket* mqttPacket = new MQTTGWPacket();
|
||||||
connectData->willMsg = client->getWillMsg();
|
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 */
|
/* Send CONNECT to the broker */
|
||||||
Event* evt = new Event();
|
Event* evt = new Event();
|
||||||
@@ -222,13 +231,14 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
|
|||||||
/*
|
/*
|
||||||
* DISCONNECT
|
* DISCONNECT
|
||||||
*/
|
*/
|
||||||
void MQTTSNConnectionHandler::handleDisconnect(Client* client, MQTTSNPacket* packet)
|
void MQTTSNConnectionHandler::handleDisconnect(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
uint16_t duration = 0;
|
uint16_t duration = 0;
|
||||||
|
|
||||||
if ( packet->getDISCONNECT(&duration) != 0 )
|
if (packet->getDISCONNECT(&duration) != 0)
|
||||||
{
|
{
|
||||||
if ( duration == 0 )
|
if (duration == 0)
|
||||||
{
|
{
|
||||||
MQTTGWPacket* mqMsg = new MQTTGWPacket();
|
MQTTGWPacket* mqMsg = new MQTTGWPacket();
|
||||||
mqMsg->setHeader(DISCONNECT);
|
mqMsg->setHeader(DISCONNECT);
|
||||||
@@ -248,7 +258,8 @@ void MQTTSNConnectionHandler::handleDisconnect(Client* client, MQTTSNPacket* pac
|
|||||||
/*
|
/*
|
||||||
* WILLTOPICUPD
|
* WILLTOPICUPD
|
||||||
*/
|
*/
|
||||||
void MQTTSNConnectionHandler::handleWilltopicupd(Client* client, MQTTSNPacket* packet)
|
void MQTTSNConnectionHandler::handleWilltopicupd(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
/* send NOT_SUPPORTED responce to the client */
|
/* send NOT_SUPPORTED responce to the client */
|
||||||
MQTTSNPacket* respMsg = new MQTTSNPacket();
|
MQTTSNPacket* respMsg = new MQTTSNPacket();
|
||||||
@@ -261,7 +272,8 @@ void MQTTSNConnectionHandler::handleWilltopicupd(Client* client, MQTTSNPacket* p
|
|||||||
/*
|
/*
|
||||||
* WILLMSGUPD
|
* WILLMSGUPD
|
||||||
*/
|
*/
|
||||||
void MQTTSNConnectionHandler::handleWillmsgupd(Client* client, MQTTSNPacket* packet)
|
void MQTTSNConnectionHandler::handleWillmsgupd(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
/* send NOT_SUPPORTED responce to the client */
|
/* send NOT_SUPPORTED responce to the client */
|
||||||
MQTTSNPacket* respMsg = new MQTTSNPacket();
|
MQTTSNPacket* respMsg = new MQTTSNPacket();
|
||||||
@@ -274,9 +286,11 @@ void MQTTSNConnectionHandler::handleWillmsgupd(Client* client, MQTTSNPacket* pac
|
|||||||
/*
|
/*
|
||||||
* PINGREQ
|
* 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);
|
sendStoredPublish(client);
|
||||||
client->holdPingRequest();
|
client->holdPingRequest();
|
||||||
@@ -297,7 +311,7 @@ void MQTTSNConnectionHandler::sendStoredPublish(Client* client)
|
|||||||
{
|
{
|
||||||
MQTTGWPacket* msg = nullptr;
|
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.
|
// ToDo: This version can't re-send PUBLISH when PUBACK is not returned.
|
||||||
client->deleteFirstClientSleepPacket(); // pop the que to delete element.
|
client->deleteFirstClientSleepPacket(); // pop the que to delete element.
|
||||||
|
|||||||
@@ -62,7 +62,6 @@ typedef unsigned int uint32_t;
|
|||||||
==================================*/
|
==================================*/
|
||||||
//#define DEBUG // print out log for debug
|
//#define DEBUG // print out log for debug
|
||||||
//#define DEBUG_NWSTACK // print out SensorNetwork log
|
//#define DEBUG_NWSTACK // print out SensorNetwork log
|
||||||
|
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#define DEBUGLOG(...) printf(__VA_ARGS__)
|
#define DEBUGLOG(...) printf(__VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -21,17 +21,17 @@
|
|||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
WirelessNodeId::WirelessNodeId()
|
WirelessNodeId::WirelessNodeId() :
|
||||||
:
|
_len
|
||||||
_len{0},
|
{ 0 }, _nodeId
|
||||||
_nodeId{0}
|
{ 0 }
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
WirelessNodeId::~WirelessNodeId()
|
WirelessNodeId::~WirelessNodeId()
|
||||||
{
|
{
|
||||||
if ( _nodeId )
|
if (_nodeId)
|
||||||
{
|
{
|
||||||
free(_nodeId);
|
free(_nodeId);
|
||||||
}
|
}
|
||||||
@@ -39,12 +39,12 @@ WirelessNodeId::~WirelessNodeId()
|
|||||||
|
|
||||||
void WirelessNodeId::setId(uint8_t* id, uint8_t len)
|
void WirelessNodeId::setId(uint8_t* id, uint8_t len)
|
||||||
{
|
{
|
||||||
if ( _nodeId )
|
if (_nodeId)
|
||||||
{
|
{
|
||||||
free(_nodeId);
|
free(_nodeId);
|
||||||
}
|
}
|
||||||
uint8_t* buf = (uint8_t*)malloc(len);
|
uint8_t* buf = (uint8_t*) malloc(len);
|
||||||
if ( buf )
|
if (buf)
|
||||||
{
|
{
|
||||||
memcpy(buf, id, len);
|
memcpy(buf, id, len);
|
||||||
_len = len;
|
_len = len;
|
||||||
@@ -64,7 +64,7 @@ void WirelessNodeId::setId(WirelessNodeId* id)
|
|||||||
|
|
||||||
bool WirelessNodeId::operator ==(WirelessNodeId& id)
|
bool WirelessNodeId::operator ==(WirelessNodeId& id)
|
||||||
{
|
{
|
||||||
if ( _len == id._len )
|
if (_len == id._len)
|
||||||
{
|
{
|
||||||
return memcmp(_nodeId, id._nodeId, _len) == 0;
|
return memcmp(_nodeId, id._nodeId, _len) == 0;
|
||||||
}
|
}
|
||||||
@@ -77,16 +77,18 @@ bool WirelessNodeId::operator ==(WirelessNodeId& id)
|
|||||||
/*
|
/*
|
||||||
* Class MQTTSNGWEncapsulatedPacket
|
* Class MQTTSNGWEncapsulatedPacket
|
||||||
*/
|
*/
|
||||||
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket()
|
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket() :
|
||||||
: _mqttsn{0},
|
_mqttsn
|
||||||
_ctrl{0}
|
{ 0 }, _ctrl
|
||||||
|
{ 0 }
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket(MQTTSNPacket* packet)
|
MQTTSNGWEncapsulatedPacket::MQTTSNGWEncapsulatedPacket(MQTTSNPacket* packet) :
|
||||||
: _mqttsn{packet},
|
_mqttsn
|
||||||
_ctrl{0}
|
{ packet }, _ctrl
|
||||||
|
{ 0 }
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -96,7 +98,8 @@ MQTTSNGWEncapsulatedPacket::~MQTTSNGWEncapsulatedPacket()
|
|||||||
/* Do not delete the MQTTSNPacket. MQTTSNPacket is deleted by delete Event */
|
/* 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];
|
uint8_t buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||||
int len = serialize(buf);
|
int len = serialize(buf);
|
||||||
@@ -109,8 +112,8 @@ int MQTTSNGWEncapsulatedPacket::serialize(uint8_t* buf)
|
|||||||
buf[0] = _id._len + 3;
|
buf[0] = _id._len + 3;
|
||||||
buf[1] = MQTTSN_ENCAPSULATED;
|
buf[1] = MQTTSN_ENCAPSULATED;
|
||||||
buf[2] = _ctrl;
|
buf[2] = _ctrl;
|
||||||
memcpy( buf + 3, _id._nodeId, _id._len);
|
memcpy(buf + 3, _id._nodeId, _id._len);
|
||||||
if ( _mqttsn )
|
if (_mqttsn)
|
||||||
{
|
{
|
||||||
len = _mqttsn->getPacketLength();
|
len = _mqttsn->getPacketLength();
|
||||||
memcpy(buf + buf[0], _mqttsn->getPacketData(), len);
|
memcpy(buf + buf[0], _mqttsn->getPacketData(), len);
|
||||||
@@ -118,9 +121,10 @@ int MQTTSNGWEncapsulatedPacket::serialize(uint8_t* buf)
|
|||||||
return buf[0] + len;
|
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;
|
delete _mqttsn;
|
||||||
_mqttsn = nullptr;
|
_mqttsn = nullptr;
|
||||||
|
|||||||
@@ -60,6 +60,4 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWENCAPSULATEDPACKET_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWENCAPSULATEDPACKET_H_ */
|
||||||
|
|||||||
@@ -32,10 +32,10 @@ ForwarderList::ForwarderList()
|
|||||||
|
|
||||||
ForwarderList::~ForwarderList()
|
ForwarderList::~ForwarderList()
|
||||||
{
|
{
|
||||||
if ( _head )
|
if (_head)
|
||||||
{
|
{
|
||||||
Forwarder* p = _head;
|
Forwarder* p = _head;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
Forwarder* next = p->_next;
|
Forwarder* next = p->_next;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -44,20 +44,18 @@ ForwarderList::~ForwarderList()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ForwarderList::initialize(Gateway* gw)
|
void ForwarderList::initialize(Gateway* gw)
|
||||||
{
|
{
|
||||||
/* Create Fowarders from clients.conf */
|
/* Create Fowarders from clients.conf */
|
||||||
gw->getClientList()->setClientList(FORWARDER_TYPE);
|
gw->getClientList()->setClientList(FORWARDER_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Forwarder* ForwarderList::getForwarder(SensorNetAddress* addr)
|
Forwarder* ForwarderList::getForwarder(SensorNetAddress* addr)
|
||||||
{
|
{
|
||||||
Forwarder* p = _head;
|
Forwarder* p = _head;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_sensorNetAddr.isMatch(addr) )
|
if (p->_sensorNetAddr.isMatch(addr))
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -66,19 +64,20 @@ Forwarder* ForwarderList::getForwarder(SensorNetAddress* addr)
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
Forwarder* ForwarderList::addForwarder(SensorNetAddress* addr, MQTTSNString* forwarderId)
|
Forwarder* ForwarderList::addForwarder(SensorNetAddress* addr,
|
||||||
|
MQTTSNString* forwarderId)
|
||||||
{
|
{
|
||||||
Forwarder* fdr = new Forwarder(addr, forwarderId);
|
Forwarder* fdr = new Forwarder(addr, forwarderId);
|
||||||
if ( _head == nullptr )
|
if (_head == nullptr)
|
||||||
{
|
{
|
||||||
_head = fdr;
|
_head = fdr;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Forwarder* p = _head;
|
Forwarder* p = _head;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_next == nullptr )
|
if (p->_next == nullptr)
|
||||||
{
|
{
|
||||||
p->_next = fdr;
|
p->_next = fdr;
|
||||||
break;
|
break;
|
||||||
@@ -112,10 +111,10 @@ Forwarder::Forwarder(SensorNetAddress* addr, MQTTSNString* forwarderId)
|
|||||||
|
|
||||||
Forwarder::~Forwarder(void)
|
Forwarder::~Forwarder(void)
|
||||||
{
|
{
|
||||||
if ( _headClient )
|
if (_headClient)
|
||||||
{
|
{
|
||||||
ForwarderElement* p = _headClient;
|
ForwarderElement* p = _headClient;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
ForwarderElement* next = p->_next;
|
ForwarderElement* next = p->_next;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -136,11 +135,11 @@ void Forwarder::addClient(Client* client, WirelessNodeId* id)
|
|||||||
|
|
||||||
client->setForwarder(this);
|
client->setForwarder(this);
|
||||||
|
|
||||||
if ( p != nullptr )
|
if (p != nullptr)
|
||||||
{
|
{
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_client == client )
|
if (p->_client == client)
|
||||||
{
|
{
|
||||||
client->setForwarder(this);
|
client->setForwarder(this);
|
||||||
p->setWirelessNodeId(id);
|
p->setWirelessNodeId(id);
|
||||||
@@ -156,7 +155,7 @@ void Forwarder::addClient(Client* client, WirelessNodeId* id)
|
|||||||
fclient->setClient(client);
|
fclient->setClient(client);
|
||||||
fclient->setWirelessNodeId(id);
|
fclient->setWirelessNodeId(id);
|
||||||
|
|
||||||
if ( prev )
|
if (prev)
|
||||||
{
|
{
|
||||||
prev->_next = fclient;
|
prev->_next = fclient;
|
||||||
}
|
}
|
||||||
@@ -171,9 +170,9 @@ Client* Forwarder::getClient(WirelessNodeId* id)
|
|||||||
Client* cl = nullptr;
|
Client* cl = nullptr;
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
ForwarderElement* p = _headClient;
|
ForwarderElement* p = _headClient;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( *(p->_wirelessNodeId) == *id )
|
if (*(p->_wirelessNodeId) == *id)
|
||||||
{
|
{
|
||||||
cl = p->_client;
|
cl = p->_client;
|
||||||
break;
|
break;
|
||||||
@@ -197,9 +196,9 @@ WirelessNodeId* Forwarder::getWirelessNodeId(Client* client)
|
|||||||
WirelessNodeId* nodeId = nullptr;
|
WirelessNodeId* nodeId = nullptr;
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
ForwarderElement* p = _headClient;
|
ForwarderElement* p = _headClient;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_client == client )
|
if (p->_client == client)
|
||||||
{
|
{
|
||||||
nodeId = p->_wirelessNodeId;
|
nodeId = p->_wirelessNodeId;
|
||||||
break;
|
break;
|
||||||
@@ -219,11 +218,11 @@ void Forwarder::eraseClient(Client* client)
|
|||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
ForwarderElement* p = _headClient;
|
ForwarderElement* p = _headClient;
|
||||||
|
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_client == client )
|
if (p->_client == client)
|
||||||
{
|
{
|
||||||
if ( prev )
|
if (prev)
|
||||||
{
|
{
|
||||||
prev->_next = p->_next;
|
prev->_next = p->_next;
|
||||||
}
|
}
|
||||||
@@ -251,10 +250,11 @@ SensorNetAddress* Forwarder::getSensorNetAddr(void)
|
|||||||
* Class ForwardedClient
|
* Class ForwardedClient
|
||||||
*/
|
*/
|
||||||
|
|
||||||
ForwarderElement::ForwarderElement()
|
ForwarderElement::ForwarderElement() :
|
||||||
: _client{0}
|
_client
|
||||||
, _wirelessNodeId{0}
|
{ 0 }, _wirelessNodeId
|
||||||
, _next{0}
|
{ 0 }, _next
|
||||||
|
{ 0 }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,7 +273,7 @@ void ForwarderElement::setClient(Client* client)
|
|||||||
|
|
||||||
void ForwarderElement::setWirelessNodeId(WirelessNodeId* id)
|
void ForwarderElement::setWirelessNodeId(WirelessNodeId* id)
|
||||||
{
|
{
|
||||||
if ( _wirelessNodeId == nullptr )
|
if (_wirelessNodeId == nullptr)
|
||||||
{
|
{
|
||||||
_wirelessNodeId = new WirelessNodeId();
|
_wirelessNodeId = new WirelessNodeId();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
#include "MQTTSNGWEncapsulatedPacket.h"
|
#include "MQTTSNGWEncapsulatedPacket.h"
|
||||||
#include "SensorNetwork.h"
|
#include "SensorNetwork.h"
|
||||||
|
|
||||||
|
|
||||||
namespace MQTTSNGW
|
namespace MQTTSNGW
|
||||||
{
|
{
|
||||||
class Gateway;
|
class Gateway;
|
||||||
@@ -70,8 +69,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
string _forwarderName;
|
string _forwarderName;
|
||||||
SensorNetAddress _sensorNetAddr;
|
SensorNetAddress _sensorNetAddr;
|
||||||
ForwarderElement* _headClient{nullptr};
|
ForwarderElement* _headClient { nullptr };
|
||||||
Forwarder* _next {nullptr};
|
Forwarder* _next { nullptr };
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -94,6 +93,4 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWFORWARDER_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWFORWARDER_H_ */
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ void Logmonitor::run()
|
|||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
const char* data = getLog();
|
const char* data = getLog();
|
||||||
if ( *data == 0 )
|
if (*data == 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,10 +30,10 @@ MessageIdTable::MessageIdTable()
|
|||||||
MessageIdTable::~MessageIdTable()
|
MessageIdTable::~MessageIdTable()
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
if ( _head != nullptr )
|
if (_head != nullptr)
|
||||||
{
|
{
|
||||||
MessageIdElement* p = _tail;
|
MessageIdElement* p = _tail;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
MessageIdElement* pPrev = p;
|
MessageIdElement* pPrev = p;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -45,20 +45,21 @@ MessageIdTable::~MessageIdTable()
|
|||||||
_mutex.unlock();
|
_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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageIdElement* elm = new MessageIdElement(0, client, clientMsgId);
|
MessageIdElement* elm = new MessageIdElement(0, client, clientMsgId);
|
||||||
if ( elm == nullptr )
|
if (elm == nullptr)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
if ( _head == nullptr )
|
if (_head == nullptr)
|
||||||
{
|
{
|
||||||
elm->_msgId = aggregater->msgId();
|
elm->_msgId = aggregater->msgId();
|
||||||
_head = elm;
|
_head = elm;
|
||||||
@@ -68,7 +69,7 @@ MessageIdElement* MessageIdTable::add(Aggregater* aggregater, Client* client, ui
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
MessageIdElement* p = find(client, clientMsgId);
|
MessageIdElement* p = find(client, clientMsgId);
|
||||||
if ( p == nullptr )
|
if (p == nullptr)
|
||||||
{
|
{
|
||||||
elm->_msgId = aggregater->msgId();
|
elm->_msgId = aggregater->msgId();
|
||||||
p = _tail;
|
p = _tail;
|
||||||
@@ -90,9 +91,9 @@ MessageIdElement* MessageIdTable::add(Aggregater* aggregater, Client* client, ui
|
|||||||
MessageIdElement* MessageIdTable::find(uint16_t msgId)
|
MessageIdElement* MessageIdTable::find(uint16_t msgId)
|
||||||
{
|
{
|
||||||
MessageIdElement* p = _head;
|
MessageIdElement* p = _head;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_msgId == msgId)
|
if (p->_msgId == msgId)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -104,9 +105,9 @@ MessageIdElement* MessageIdTable::find(uint16_t msgId)
|
|||||||
MessageIdElement* MessageIdTable::find(Client* client, uint16_t clientMsgId)
|
MessageIdElement* MessageIdTable::find(Client* client, uint16_t clientMsgId)
|
||||||
{
|
{
|
||||||
MessageIdElement* p = _head;
|
MessageIdElement* p = _head;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_clientMsgId == clientMsgId && p->_client == client)
|
if (p->_clientMsgId == clientMsgId && p->_client == client)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -115,14 +116,13 @@ MessageIdElement* MessageIdTable::find(Client* client, uint16_t clientMsgId)
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Client* MessageIdTable::getClientMsgId(uint16_t msgId, uint16_t* clientMsgId)
|
Client* MessageIdTable::getClientMsgId(uint16_t msgId, uint16_t* clientMsgId)
|
||||||
{
|
{
|
||||||
Client* clt = nullptr;
|
Client* clt = nullptr;
|
||||||
*clientMsgId = 0;
|
*clientMsgId = 0;
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
MessageIdElement* p = find(msgId);
|
MessageIdElement* p = find(msgId);
|
||||||
if ( p != nullptr )
|
if (p != nullptr)
|
||||||
{
|
{
|
||||||
clt = p->_client;
|
clt = p->_client;
|
||||||
*clientMsgId = p->_clientMsgId;
|
*clientMsgId = p->_clientMsgId;
|
||||||
@@ -142,15 +142,15 @@ void MessageIdTable::erase(uint16_t msgId)
|
|||||||
|
|
||||||
void MessageIdTable::clear(MessageIdElement* elm)
|
void MessageIdTable::clear(MessageIdElement* elm)
|
||||||
{
|
{
|
||||||
if ( elm == nullptr )
|
if (elm == nullptr)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( elm->_prev == nullptr )
|
if (elm->_prev == nullptr)
|
||||||
{
|
{
|
||||||
_head = elm->_next;
|
_head = elm->_next;
|
||||||
if ( _head == nullptr)
|
if (_head == nullptr)
|
||||||
{
|
{
|
||||||
_tail = nullptr;
|
_tail = nullptr;
|
||||||
}
|
}
|
||||||
@@ -165,7 +165,7 @@ void MessageIdTable::clear(MessageIdElement* elm)
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
elm->_prev->_next = elm->_next;
|
elm->_prev->_next = elm->_next;
|
||||||
if ( elm->_next == nullptr )
|
if (elm->_next == nullptr)
|
||||||
{
|
{
|
||||||
_tail = elm->_prev;
|
_tail = elm->_prev;
|
||||||
}
|
}
|
||||||
@@ -179,12 +179,11 @@ void MessageIdTable::clear(MessageIdElement* elm)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
uint16_t MessageIdTable::getMsgId(Client* client, uint16_t clientMsgId)
|
uint16_t MessageIdTable::getMsgId(Client* client, uint16_t clientMsgId)
|
||||||
{
|
{
|
||||||
uint16_t msgId = 0;
|
uint16_t msgId = 0;
|
||||||
MessageIdElement* p = find(client, clientMsgId);
|
MessageIdElement* p = find(client, clientMsgId);
|
||||||
if ( p != nullptr )
|
if (p != nullptr)
|
||||||
{
|
{
|
||||||
msgId = p->_msgId;
|
msgId = p->_msgId;
|
||||||
}
|
}
|
||||||
@@ -194,18 +193,20 @@ uint16_t MessageIdTable::getMsgId(Client* client, uint16_t clientMsgId)
|
|||||||
/*===============================
|
/*===============================
|
||||||
* Class MessageIdElement
|
* Class MessageIdElement
|
||||||
===============================*/
|
===============================*/
|
||||||
MessageIdElement::MessageIdElement(void)
|
MessageIdElement::MessageIdElement(void) :
|
||||||
: _msgId{0}
|
_msgId
|
||||||
, _clientMsgId {0}
|
{ 0 }, _clientMsgId
|
||||||
, _client {nullptr}
|
{ 0 }, _client
|
||||||
, _next {nullptr}
|
{ nullptr }, _next
|
||||||
, _prev {nullptr}
|
{ nullptr }, _prev
|
||||||
|
{ nullptr }
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MessageIdElement::MessageIdElement(uint16_t msgId, Client* client, uint16_t clientMsgId)
|
MessageIdElement::MessageIdElement(uint16_t msgId, Client* client,
|
||||||
: MessageIdElement()
|
uint16_t clientMsgId) :
|
||||||
|
MessageIdElement()
|
||||||
{
|
{
|
||||||
_msgId = msgId;
|
_msgId = msgId;
|
||||||
_client = client;
|
_client = client;
|
||||||
|
|||||||
@@ -37,7 +37,8 @@ public:
|
|||||||
MessageIdTable();
|
MessageIdTable();
|
||||||
~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);
|
Client* getClientMsgId(uint16_t msgId, uint16_t* clientMsgId);
|
||||||
uint16_t getMsgId(Client* client, uint16_t clientMsgId);
|
uint16_t getMsgId(Client* client, uint16_t clientMsgId);
|
||||||
void erase(uint16_t msgId);
|
void erase(uint16_t msgId);
|
||||||
@@ -45,10 +46,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
MessageIdElement* find(uint16_t msgId);
|
MessageIdElement* find(uint16_t msgId);
|
||||||
MessageIdElement* find(Client* client, uint16_t clientMsgId);
|
MessageIdElement* find(Client* client, uint16_t clientMsgId);
|
||||||
MessageIdElement* _head {nullptr};
|
MessageIdElement* _head { nullptr };
|
||||||
MessageIdElement* _tail {nullptr};
|
MessageIdElement* _tail{ nullptr };
|
||||||
int _cnt {0};
|
int _cnt { 0 };
|
||||||
int _maxSize {MAX_MESSAGEID_TABLE_SIZE};
|
int _maxSize { MAX_MESSAGEID_TABLE_SIZE };
|
||||||
Mutex _mutex;
|
Mutex _mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -72,7 +73,6 @@ private:
|
|||||||
MessageIdElement* _prev;
|
MessageIdElement* _prev;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWMESSAGEIDTABLE_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWMESSAGEIDTABLE_H_ */
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ MQTTSNPacket::MQTTSNPacket(void)
|
|||||||
|
|
||||||
MQTTSNPacket::MQTTSNPacket(MQTTSNPacket& packet)
|
MQTTSNPacket::MQTTSNPacket(MQTTSNPacket& packet)
|
||||||
{
|
{
|
||||||
_buf = (unsigned char*)malloc(packet._bufLen);
|
_buf = (unsigned char*) malloc(packet._bufLen);
|
||||||
if (_buf)
|
if (_buf)
|
||||||
{
|
{
|
||||||
_bufLen = packet._bufLen;
|
_bufLen = packet._bufLen;
|
||||||
@@ -74,13 +74,13 @@ int MQTTSNPacket::serialize(uint8_t* buf)
|
|||||||
|
|
||||||
int MQTTSNPacket::desirialize(unsigned char* buf, unsigned short len)
|
int MQTTSNPacket::desirialize(unsigned char* buf, unsigned short len)
|
||||||
{
|
{
|
||||||
if ( _buf )
|
if (_buf)
|
||||||
{
|
{
|
||||||
free(_buf);
|
free(_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
_buf = (unsigned char*)calloc(len, sizeof(unsigned char));
|
_buf = (unsigned char*) calloc(len, sizeof(unsigned char));
|
||||||
if ( _buf )
|
if (_buf)
|
||||||
{
|
{
|
||||||
memcpy(_buf, buf, len);
|
memcpy(_buf, buf, len);
|
||||||
_bufLen = len;
|
_bufLen = len;
|
||||||
@@ -110,7 +110,7 @@ int MQTTSNPacket::recv(SensorNetwork* network)
|
|||||||
|
|
||||||
int MQTTSNPacket::getType(void)
|
int MQTTSNPacket::getType(void)
|
||||||
{
|
{
|
||||||
if ( _bufLen == 0 )
|
if (_bufLen == 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -121,13 +121,13 @@ int MQTTSNPacket::getType(void)
|
|||||||
|
|
||||||
bool MQTTSNPacket::isQoSMinusPUBLISH(void)
|
bool MQTTSNPacket::isQoSMinusPUBLISH(void)
|
||||||
{
|
{
|
||||||
if ( _bufLen == 0 )
|
if (_bufLen == 0)
|
||||||
{
|
{
|
||||||
return false;;
|
return false;;
|
||||||
}
|
}
|
||||||
int value = 0;
|
int value = 0;
|
||||||
int p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
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)
|
unsigned char* MQTTSNPacket::getPacketData(void)
|
||||||
@@ -158,7 +158,8 @@ int MQTTSNPacket::setGWINFO(uint8_t gatewayId)
|
|||||||
{
|
{
|
||||||
unsigned char buf[3];
|
unsigned char buf[3];
|
||||||
int buflen = sizeof(buf);
|
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);
|
return desirialize(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,14 +168,14 @@ int MQTTSNPacket::setConnect(void)
|
|||||||
unsigned char buf[40];
|
unsigned char buf[40];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
MQTTSNPacket_connectData data;
|
MQTTSNPacket_connectData data;
|
||||||
data.clientID.cstring = (char*)"client01";
|
data.clientID.cstring = (char*) "client01";
|
||||||
int len = MQTTSNSerialize_connect(buf, buflen, &data);
|
int len = MQTTSNSerialize_connect(buf, buflen, &data);
|
||||||
return desirialize(buf, len);
|
return desirialize(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MQTTSNPacket::isAccepted(void)
|
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)
|
int MQTTSNPacket::setCONNACK(uint8_t returnCode)
|
||||||
@@ -201,40 +202,45 @@ int MQTTSNPacket::setWILLMSGREQ(void)
|
|||||||
return desirialize(buf, len);
|
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];
|
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
int len = MQTTSNSerialize_register(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
|
int len = MQTTSNSerialize_register(buf, buflen, (unsigned short) topicId,
|
||||||
topicName);
|
(unsigned short) msgId, topicName);
|
||||||
return desirialize(buf, len);
|
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];
|
unsigned char buf[7];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
int len = MQTTSNSerialize_regack(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
|
int len = MQTTSNSerialize_regack(buf, buflen, (unsigned short) topicId,
|
||||||
(unsigned char) returnCode);
|
(unsigned short) msgId, (unsigned char) returnCode);
|
||||||
return desirialize(buf, len);
|
return desirialize(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int MQTTSNPacket::setPUBLISH(uint8_t dup, int qos, uint8_t retained, uint16_t msgId, MQTTSN_topicid topic,
|
int MQTTSNPacket::setPUBLISH(uint8_t dup, int qos, uint8_t retained,
|
||||||
uint8_t* payload, uint16_t payloadlen)
|
uint16_t msgId, MQTTSN_topicid topic, uint8_t* payload,
|
||||||
|
uint16_t payloadlen)
|
||||||
{
|
{
|
||||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
int len = MQTTSNSerialize_publish(buf, buflen, (unsigned char) dup, qos, (unsigned char) retained,
|
int len = MQTTSNSerialize_publish(buf, buflen, (unsigned char) dup, qos,
|
||||||
(unsigned short) msgId, topic, (unsigned char*) payload, (int) payloadlen);
|
(unsigned char) retained, (unsigned short) msgId, topic,
|
||||||
|
(unsigned char*) payload, (int) payloadlen);
|
||||||
return desirialize(buf, len);
|
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];
|
unsigned char buf[7];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
int len = MQTTSNSerialize_puback(buf, buflen, (unsigned short) topicId, (unsigned short) msgId,
|
int len = MQTTSNSerialize_puback(buf, buflen, (unsigned short) topicId,
|
||||||
(unsigned char) returnCode);
|
(unsigned short) msgId, (unsigned char) returnCode);
|
||||||
return desirialize(buf, len);
|
return desirialize(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -262,7 +268,8 @@ int MQTTSNPacket::setPUBCOMP(uint16_t msgId)
|
|||||||
return desirialize(buf, len);
|
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];
|
unsigned char buf[8];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
@@ -323,13 +330,14 @@ int MQTTSNPacket::setPINGREQ(MQTTSNString* clientId)
|
|||||||
{
|
{
|
||||||
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
unsigned char buf[MQTTSNGW_MAX_PACKET_SIZE];
|
||||||
int buflen = sizeof(buf);
|
int buflen = sizeof(buf);
|
||||||
int len = MQTTSNSerialize_pingreq( buf, buflen, *clientId);
|
int len = MQTTSNSerialize_pingreq(buf, buflen, *clientId);
|
||||||
return desirialize(buf, len);
|
return desirialize(buf, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
int MQTTSNPacket::getSERCHGW(uint8_t* radius)
|
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)
|
int MQTTSNPacket::getCONNECT(MQTTSNPacket_connectData* data)
|
||||||
@@ -342,9 +350,11 @@ int MQTTSNPacket::getCONNACK(uint8_t* returnCode)
|
|||||||
return MQTTSNSerialize_connack(_buf, _bufLen, (int) *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)
|
int MQTTSNPacket::getWILLMSG(MQTTSNString* willmsg)
|
||||||
@@ -352,28 +362,34 @@ int MQTTSNPacket::getWILLMSG(MQTTSNString* willmsg)
|
|||||||
return MQTTSNDeserialize_willmsg(willmsg, _buf, _bufLen);
|
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,
|
return MQTTSNDeserialize_register((unsigned short*) topicId,
|
||||||
_buf, _bufLen);
|
(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,
|
int MQTTSNPacket::getPUBLISH(uint8_t* dup, int* qos, uint8_t* retained,
|
||||||
uint8_t** payload, int* payloadlen)
|
uint16_t* msgId, MQTTSN_topicid* topic, uint8_t** payload,
|
||||||
|
int* payloadlen)
|
||||||
{
|
{
|
||||||
return MQTTSNDeserialize_publish((unsigned char*) dup, qos, (unsigned char*) retained, (unsigned short*) msgId,
|
return MQTTSNDeserialize_publish((unsigned char*) dup, qos,
|
||||||
topic, (unsigned char**) payload, (int*) payloadlen, _buf, _bufLen);
|
(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,
|
return MQTTSNDeserialize_puback((unsigned short*) topicId,
|
||||||
_buf, _bufLen);
|
(unsigned short*) msgId, (unsigned char*) returnCode, _buf, _bufLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
int MQTTSNPacket::getACK(uint16_t* msgId)
|
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);
|
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)
|
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)
|
int MQTTSNPacket::getPINGREQ(void)
|
||||||
{
|
{
|
||||||
if (getType() == MQTTSN_PINGRESP && _bufLen > 2 )
|
if (getType() == MQTTSN_PINGRESP && _bufLen > 2)
|
||||||
{
|
{
|
||||||
return _bufLen - 2;
|
return _bufLen - 2;
|
||||||
}
|
}
|
||||||
@@ -405,13 +424,15 @@ int MQTTSNPacket::getDISCONNECT(uint16_t* duration)
|
|||||||
{
|
{
|
||||||
int dur = 0;
|
int dur = 0;
|
||||||
int rc = MQTTSNDeserialize_disconnect(&dur, _buf, _bufLen);
|
int rc = MQTTSNDeserialize_disconnect(&dur, _buf, _bufLen);
|
||||||
*duration = (uint16_t)dur;
|
*duration = (uint16_t) dur;
|
||||||
return rc;
|
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)
|
int MQTTSNPacket::getWILLMSGUPD(MQTTSNString* willMsg)
|
||||||
@@ -439,11 +460,11 @@ char* MQTTSNPacket::getMsgId(char* pbuf)
|
|||||||
int value = 0;
|
int value = 0;
|
||||||
int p = 0;
|
int p = 0;
|
||||||
|
|
||||||
switch ( getType() )
|
switch (getType())
|
||||||
{
|
{
|
||||||
case MQTTSN_PUBLISH:
|
case MQTTSN_PUBLISH:
|
||||||
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
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]);
|
sprintf(pbuf, "+%02X%02X", _buf[p + 4], _buf[p + 5]);
|
||||||
}
|
}
|
||||||
@@ -475,7 +496,7 @@ char* MQTTSNPacket::getMsgId(char* pbuf)
|
|||||||
sprintf(pbuf, " ");
|
sprintf(pbuf, " ");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if ( strcmp(pbuf, " 0000") == 0 )
|
if (strcmp(pbuf, " 0000") == 0)
|
||||||
{
|
{
|
||||||
sprintf(pbuf, " ");
|
sprintf(pbuf, " ");
|
||||||
}
|
}
|
||||||
@@ -489,35 +510,35 @@ int MQTTSNPacket::getMsgId(void)
|
|||||||
int msgId = 0;
|
int msgId = 0;
|
||||||
char* ptr = 0;
|
char* ptr = 0;
|
||||||
|
|
||||||
switch ( getType() )
|
switch (getType())
|
||||||
{
|
{
|
||||||
case MQTTSN_PUBLISH:
|
case MQTTSN_PUBLISH:
|
||||||
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
||||||
ptr = (char*)_buf + p + 4;
|
ptr = (char*) _buf + p + 4;
|
||||||
msgId = readInt((char**)&ptr);
|
msgId = readInt((char**) &ptr);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_PUBACK:
|
case MQTTSN_PUBACK:
|
||||||
case MQTTSN_REGISTER:
|
case MQTTSN_REGISTER:
|
||||||
case MQTTSN_REGACK:
|
case MQTTSN_REGACK:
|
||||||
ptr = (char*)_buf + 4;
|
ptr = (char*) _buf + 4;
|
||||||
msgId = readInt((char**)&ptr);
|
msgId = readInt((char**) &ptr);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_PUBREC:
|
case MQTTSN_PUBREC:
|
||||||
case MQTTSN_PUBREL:
|
case MQTTSN_PUBREL:
|
||||||
case MQTTSN_PUBCOMP:
|
case MQTTSN_PUBCOMP:
|
||||||
case MQTTSN_UNSUBACK:
|
case MQTTSN_UNSUBACK:
|
||||||
ptr = (char*)_buf + 2;
|
ptr = (char*) _buf + 2;
|
||||||
msgId = readInt((char**)&ptr);
|
msgId = readInt((char**) &ptr);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_SUBSCRIBE:
|
case MQTTSN_SUBSCRIBE:
|
||||||
case MQTTSN_UNSUBSCRIBE:
|
case MQTTSN_UNSUBSCRIBE:
|
||||||
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
||||||
ptr = (char*)_buf + p + 2;
|
ptr = (char*) _buf + p + 2;
|
||||||
msgId = readInt((char**)&ptr);
|
msgId = readInt((char**) &ptr);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_SUBACK:
|
case MQTTSN_SUBACK:
|
||||||
ptr = (char*)_buf + 5;
|
ptr = (char*) _buf + 5;
|
||||||
msgId = readInt((char**)&ptr);
|
msgId = readInt((char**) &ptr);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@@ -531,20 +552,20 @@ void MQTTSNPacket::setMsgId(uint16_t msgId)
|
|||||||
int p = 0;
|
int p = 0;
|
||||||
//unsigned char* ptr = 0;
|
//unsigned char* ptr = 0;
|
||||||
|
|
||||||
switch ( getType() )
|
switch (getType())
|
||||||
{
|
{
|
||||||
case MQTTSN_PUBLISH:
|
case MQTTSN_PUBLISH:
|
||||||
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
||||||
_buf[p + 4] = (unsigned char)(msgId / 256);
|
_buf[p + 4] = (unsigned char) (msgId / 256);
|
||||||
_buf[p + 5] = (unsigned char)(msgId % 256);
|
_buf[p + 5] = (unsigned char) (msgId % 256);
|
||||||
//ptr = _buf + p + 4;
|
//ptr = _buf + p + 4;
|
||||||
//writeInt(&ptr, msgId);
|
//writeInt(&ptr, msgId);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_PUBACK:
|
case MQTTSN_PUBACK:
|
||||||
case MQTTSN_REGISTER:
|
case MQTTSN_REGISTER:
|
||||||
case MQTTSN_REGACK:
|
case MQTTSN_REGACK:
|
||||||
_buf[4] = (unsigned char)(msgId / 256);
|
_buf[4] = (unsigned char) (msgId / 256);
|
||||||
_buf[5] = (unsigned char)(msgId % 256);
|
_buf[5] = (unsigned char) (msgId % 256);
|
||||||
//ptr = _buf + 4;
|
//ptr = _buf + 4;
|
||||||
//writeInt(&ptr, msgId);
|
//writeInt(&ptr, msgId);
|
||||||
break;
|
break;
|
||||||
@@ -552,25 +573,25 @@ void MQTTSNPacket::setMsgId(uint16_t msgId)
|
|||||||
case MQTTSN_PUBREL:
|
case MQTTSN_PUBREL:
|
||||||
case MQTTSN_PUBCOMP:
|
case MQTTSN_PUBCOMP:
|
||||||
case MQTTSN_UNSUBACK:
|
case MQTTSN_UNSUBACK:
|
||||||
_buf[2] = (unsigned char)(msgId / 256);
|
_buf[2] = (unsigned char) (msgId / 256);
|
||||||
_buf[3] = (unsigned char)(msgId % 256);
|
_buf[3] = (unsigned char) (msgId % 256);
|
||||||
//ptr = _buf + 2;
|
//ptr = _buf + 2;
|
||||||
//writeInt(&ptr, msgId);
|
//writeInt(&ptr, msgId);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_SUBSCRIBE:
|
case MQTTSN_SUBSCRIBE:
|
||||||
case MQTTSN_UNSUBSCRIBE:
|
case MQTTSN_UNSUBSCRIBE:
|
||||||
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
||||||
_buf[p + 2] = (unsigned char)(msgId / 256);
|
_buf[p + 2] = (unsigned char) (msgId / 256);
|
||||||
_buf[p + 3] = (unsigned char)(msgId % 256);
|
_buf[p + 3] = (unsigned char) (msgId % 256);
|
||||||
//ptr = _buf + p + 2;
|
//ptr = _buf + p + 2;
|
||||||
//writeInt(&ptr, msgId);
|
//writeInt(&ptr, msgId);
|
||||||
break;
|
break;
|
||||||
case MQTTSN_SUBACK:
|
case MQTTSN_SUBACK:
|
||||||
_buf[5] = (unsigned char)(msgId / 256);
|
_buf[5] = (unsigned char) (msgId / 256);
|
||||||
_buf[6] = (unsigned char)(msgId % 256);
|
_buf[6] = (unsigned char) (msgId % 256);
|
||||||
//ptr = _buf + 5;
|
//ptr = _buf + 5;
|
||||||
//writeInt(&ptr, msgId);
|
//writeInt(&ptr, msgId);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -580,5 +601,5 @@ bool MQTTSNPacket::isDuplicate(void)
|
|||||||
{
|
{
|
||||||
int value = 0;
|
int value = 0;
|
||||||
int p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
int p = MQTTSNPacket_decode(_buf, _bufLen, &value);
|
||||||
return ( _buf[p + 1] & 0x80 );
|
return (_buf[p + 1] & 0x80);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,8 @@ public:
|
|||||||
int setPUBREC(uint16_t msgId);
|
int setPUBREC(uint16_t msgId);
|
||||||
int setPUBREL(uint16_t msgId);
|
int setPUBREL(uint16_t msgId);
|
||||||
int setPUBCOMP(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 setUNSUBACK(uint16_t msgId);
|
||||||
int setPINGRESP(void);
|
int setPINGRESP(void);
|
||||||
int setDISCONNECT(uint16_t duration);
|
int setDISCONNECT(uint16_t duration);
|
||||||
@@ -66,19 +67,23 @@ public:
|
|||||||
int getSERCHGW(uint8_t* radius);
|
int getSERCHGW(uint8_t* radius);
|
||||||
int getCONNECT(MQTTSNPacket_connectData* option);
|
int getCONNECT(MQTTSNPacket_connectData* option);
|
||||||
int getCONNACK(uint8_t* returnCode);
|
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 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 getREGACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode);
|
||||||
int getPUBLISH(uint8_t* dup, int* qos, uint8_t* retained, uint16_t* msgId,
|
int getPUBLISH(uint8_t* dup, int* qos, uint8_t* retained, uint16_t* msgId,
|
||||||
MQTTSN_topicid* topic, unsigned char** payload, int* payloadlen);
|
MQTTSN_topicid* topic, unsigned char** payload, int* payloadlen);
|
||||||
int getPUBACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode);
|
int getPUBACK(uint16_t* topicId, uint16_t* msgId, uint8_t* returnCode);
|
||||||
int getACK(uint16_t* msgId);
|
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 getUNSUBSCRIBE(uint16_t* msgId, MQTTSN_topicid* topicFilter);
|
||||||
int getPINGREQ(void);
|
int getPINGREQ(void);
|
||||||
int getDISCONNECT(uint16_t* duration);
|
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);
|
int getWILLMSGUPD(MQTTSNString* willMsg);
|
||||||
|
|
||||||
bool isAccepted(void);
|
bool isAccepted(void);
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ char* currentDateTime(void);
|
|||||||
PacketHandleTask::PacketHandleTask(Gateway* gateway)
|
PacketHandleTask::PacketHandleTask(Gateway* gateway)
|
||||||
{
|
{
|
||||||
_gateway = gateway;
|
_gateway = gateway;
|
||||||
_gateway->attach((Thread*)this);
|
_gateway->attach((Thread*) this);
|
||||||
_mqttConnection = new MQTTGWConnectionHandler(_gateway);
|
_mqttConnection = new MQTTGWConnectionHandler(_gateway);
|
||||||
_mqttPublish = new MQTTGWPublishHandler(_gateway);
|
_mqttPublish = new MQTTGWPublishHandler(_gateway);
|
||||||
_mqttSubscribe = new MQTTGWSubscribeHandler(_gateway);
|
_mqttSubscribe = new MQTTGWSubscribeHandler(_gateway);
|
||||||
@@ -61,32 +61,32 @@ PacketHandleTask::PacketHandleTask(Gateway* gateway)
|
|||||||
*/
|
*/
|
||||||
PacketHandleTask::~PacketHandleTask()
|
PacketHandleTask::~PacketHandleTask()
|
||||||
{
|
{
|
||||||
if ( _mqttConnection )
|
if (_mqttConnection)
|
||||||
{
|
{
|
||||||
delete _mqttConnection;
|
delete _mqttConnection;
|
||||||
}
|
}
|
||||||
if ( _mqttPublish )
|
if (_mqttPublish)
|
||||||
{
|
{
|
||||||
delete _mqttPublish;
|
delete _mqttPublish;
|
||||||
}
|
}
|
||||||
if ( _mqttSubscribe )
|
if (_mqttSubscribe)
|
||||||
{
|
{
|
||||||
delete _mqttSubscribe;
|
delete _mqttSubscribe;
|
||||||
}
|
}
|
||||||
if ( _mqttsnConnection )
|
if (_mqttsnConnection)
|
||||||
{
|
{
|
||||||
delete _mqttsnConnection;
|
delete _mqttsnConnection;
|
||||||
}
|
}
|
||||||
if ( _mqttsnPublish )
|
if (_mqttsnPublish)
|
||||||
{
|
{
|
||||||
delete _mqttsnPublish;
|
delete _mqttsnPublish;
|
||||||
}
|
}
|
||||||
if ( _mqttsnSubscribe )
|
if (_mqttsnSubscribe)
|
||||||
{
|
{
|
||||||
delete _mqttsnSubscribe;
|
delete _mqttsnSubscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _mqttsnAggrConnection )
|
if (_mqttsnAggrConnection)
|
||||||
{
|
{
|
||||||
delete _mqttsnAggrConnection;
|
delete _mqttsnAggrConnection;
|
||||||
}
|
}
|
||||||
@@ -124,7 +124,8 @@ void PacketHandleTask::run()
|
|||||||
if (_advertiseTimer.isTimeup())
|
if (_advertiseTimer.isTimeup())
|
||||||
{
|
{
|
||||||
_mqttsnConnection->sendADVERTISE();
|
_mqttsnConnection->sendADVERTISE();
|
||||||
_advertiseTimer.start(_gateway->getGWParams()->keepAlive * 1000UL);
|
_advertiseTimer.start(
|
||||||
|
_gateway->getGWParams()->keepAlive * 1000UL);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------ Check Adapters Connect or PINGREQ ------*/
|
/*------ 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));
|
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
|
aggregatePacketHandler(client, snPacket); // client is converted to Aggregater by BrokerSendTask
|
||||||
}
|
}
|
||||||
@@ -155,19 +156,17 @@ void PacketHandleTask::run()
|
|||||||
transparentPacketHandler(client, snPacket);
|
transparentPacketHandler(client, snPacket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Reset the Timer for PINGREQ. */
|
/* Reset the Timer for PINGREQ. */
|
||||||
client->updateStatus(snPacket);
|
client->updateStatus(snPacket);
|
||||||
}
|
}
|
||||||
/*------ Handle Messages form Broker ---------*/
|
/*------ Handle Messages form Broker ---------*/
|
||||||
else if ( ev->getEventType() == EtBrokerRecv )
|
else if (ev->getEventType() == EtBrokerRecv)
|
||||||
{
|
{
|
||||||
client = ev->getClient();
|
client = ev->getClient();
|
||||||
brPacket = ev->getMQTTGWPacket();
|
brPacket = ev->getMQTTGWPacket();
|
||||||
DEBUGLOG(" PacketHandleTask gets %s %s from the broker.\n", brPacket->getName(), brPacket->getMsgId(msgId));
|
DEBUGLOG(" PacketHandleTask gets %s %s from the broker.\n", brPacket->getName(), brPacket->getMsgId(msgId));
|
||||||
|
|
||||||
|
if (client->isAggregater())
|
||||||
if ( client->isAggregater() )
|
|
||||||
{
|
{
|
||||||
aggregatePacketHandler(client, brPacket);
|
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())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
@@ -239,8 +237,8 @@ void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTSNPacket* packe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PacketHandleTask::aggregatePacketHandler(Client*client,
|
||||||
void PacketHandleTask::aggregatePacketHandler(Client*client, MQTTGWPacket* packet)
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
switch (packet->getType())
|
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())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
@@ -333,8 +332,8 @@ void PacketHandleTask::transparentPacketHandler(Client*client, MQTTSNPacket* pac
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PacketHandleTask::transparentPacketHandler(Client*client,
|
||||||
void PacketHandleTask::transparentPacketHandler(Client*client, MQTTGWPacket* packet)
|
MQTTGWPacket* packet)
|
||||||
{
|
{
|
||||||
switch (packet->getType())
|
switch (packet->getType())
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -42,9 +42,9 @@ class Timer;
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class PacketHandleTask
|
Class PacketHandleTask
|
||||||
=====================================*/
|
=====================================*/
|
||||||
class PacketHandleTask : public Thread
|
class PacketHandleTask: public Thread
|
||||||
{
|
{
|
||||||
MAGIC_WORD_FOR_THREAD;
|
MAGIC_WORD_FOR_THREAD;
|
||||||
friend class MQTTGWAggregatePublishHandler;
|
friend class MQTTGWAggregatePublishHandler;
|
||||||
friend class MQTTGWAggregateSubscribeHandler;
|
friend class MQTTGWAggregateSubscribeHandler;
|
||||||
friend class MQTTSNAggregateConnectionHandler;
|
friend class MQTTSNAggregateConnectionHandler;
|
||||||
@@ -60,20 +60,19 @@ private:
|
|||||||
void transparentPacketHandler(Client*client, MQTTSNPacket* packet);
|
void transparentPacketHandler(Client*client, MQTTSNPacket* packet);
|
||||||
void transparentPacketHandler(Client*client, MQTTGWPacket* packet);
|
void transparentPacketHandler(Client*client, MQTTGWPacket* packet);
|
||||||
|
|
||||||
Gateway* _gateway {nullptr};
|
Gateway* _gateway
|
||||||
|
{ nullptr };
|
||||||
Timer _advertiseTimer;
|
Timer _advertiseTimer;
|
||||||
Timer _sendUnixTimer;
|
Timer _sendUnixTimer;
|
||||||
MQTTGWConnectionHandler* _mqttConnection {nullptr};
|
MQTTGWConnectionHandler* _mqttConnection { nullptr };
|
||||||
MQTTGWPublishHandler* _mqttPublish {nullptr};
|
MQTTGWPublishHandler* _mqttPublish { nullptr };
|
||||||
MQTTGWSubscribeHandler* _mqttSubscribe {nullptr};
|
MQTTGWSubscribeHandler* _mqttSubscribe { nullptr };
|
||||||
MQTTSNConnectionHandler* _mqttsnConnection {nullptr};
|
MQTTSNConnectionHandler* _mqttsnConnection { nullptr };
|
||||||
MQTTSNPublishHandler* _mqttsnPublish {nullptr};
|
MQTTSNPublishHandler* _mqttsnPublish { nullptr };
|
||||||
MQTTSNSubscribeHandler* _mqttsnSubscribe {nullptr};
|
MQTTSNSubscribeHandler* _mqttsnSubscribe { nullptr };
|
||||||
|
MQTTSNAggregateConnectionHandler* _mqttsnAggrConnection { nullptr };
|
||||||
MQTTSNAggregateConnectionHandler* _mqttsnAggrConnection {nullptr};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MQTTSNGWPACKETHANDLETASK_H_ */
|
#endif /* MQTTSNGWPACKETHANDLETASK_H_ */
|
||||||
|
|||||||
@@ -61,11 +61,11 @@ Process::Process()
|
|||||||
|
|
||||||
Process::~Process()
|
Process::~Process()
|
||||||
{
|
{
|
||||||
if (_rb )
|
if (_rb)
|
||||||
{
|
{
|
||||||
delete _rb;
|
delete _rb;
|
||||||
}
|
}
|
||||||
if ( _rbsem )
|
if (_rbsem)
|
||||||
{
|
{
|
||||||
delete _rbsem;
|
delete _rbsem;
|
||||||
}
|
}
|
||||||
@@ -88,17 +88,18 @@ void Process::initialize(int argc, char** argv)
|
|||||||
int opt;
|
int opt;
|
||||||
while ((opt = getopt(_argc, _argv, "f:")) != -1)
|
while ((opt = getopt(_argc, _argv, "f:")) != -1)
|
||||||
{
|
{
|
||||||
if ( opt == 'f' )
|
if (opt == 'f')
|
||||||
{
|
{
|
||||||
string config = string(optarg);
|
string config = string(optarg);
|
||||||
size_t pos = 0;
|
size_t pos = 0;
|
||||||
if ( (pos = config.find_last_of("/")) == string::npos )
|
if ((pos = config.find_last_of("/")) == string::npos)
|
||||||
{
|
{
|
||||||
_configFile = optarg;
|
_configFile = optarg;
|
||||||
}
|
}
|
||||||
else
|
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);
|
_configDir = config.substr(0, pos + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,7 +129,7 @@ void Process::putLog(const char* format, ...)
|
|||||||
va_end(arg);
|
va_end(arg);
|
||||||
if (strlen(_rbdata))
|
if (strlen(_rbdata))
|
||||||
{
|
{
|
||||||
if ( _log > 0 )
|
if (_log > 0)
|
||||||
{
|
{
|
||||||
_rb->put(_rbdata);
|
_rb->put(_rbdata);
|
||||||
_rbsem->post();
|
_rbsem->post();
|
||||||
@@ -212,7 +213,7 @@ const char* Process::getLog()
|
|||||||
while ((len = _rb->get(_rbdata, PROCESS_LOG_BUFFER_SIZE)) == 0)
|
while ((len = _rb->get(_rbdata, PROCESS_LOG_BUFFER_SIZE)) == 0)
|
||||||
{
|
{
|
||||||
_rbsem->timedwait(1000);
|
_rbsem->timedwait(1000);
|
||||||
if ( checkSignal() == SIGINT)
|
if (checkSignal() == SIGINT)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -279,7 +280,7 @@ void MultiTaskProcess::run(void)
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while(true)
|
while (true)
|
||||||
{
|
{
|
||||||
if (theProcess->checkSignal() == SIGINT)
|
if (theProcess->checkSignal() == SIGINT)
|
||||||
{
|
{
|
||||||
@@ -287,12 +288,10 @@ void MultiTaskProcess::run(void)
|
|||||||
}
|
}
|
||||||
sleep(1);
|
sleep(1);
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception* ex)
|
||||||
catch(Exception* ex)
|
|
||||||
{
|
{
|
||||||
ex->writeMessage();
|
ex->writeMessage();
|
||||||
}
|
} catch (...)
|
||||||
catch(...)
|
|
||||||
{
|
{
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
@@ -405,13 +404,14 @@ const int Exception::getExceptionNo()
|
|||||||
|
|
||||||
void Exception::writeMessage()
|
void Exception::writeMessage()
|
||||||
{
|
{
|
||||||
if (getExceptionNo() == 0 )
|
if (getExceptionNo() == 0)
|
||||||
{
|
{
|
||||||
WRITELOG("%s %s\n", currentDateTime(), what());
|
WRITELOG("%s %s\n", currentDateTime(), what());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
WRITELOG("%s:%-6d %s line %-4d %s() : %s\n", currentDateTime(), getExceptionNo(),
|
WRITELOG("%s:%-6d %s line %-4d %s() : %s\n", currentDateTime(),
|
||||||
getFileName(), getLineNo(), getFunctionName(), what());
|
getExceptionNo(), getFileName(), getLineNo(), getFunctionName(),
|
||||||
|
what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,8 +102,8 @@ class Exception: public exception
|
|||||||
public:
|
public:
|
||||||
Exception(const string& message);
|
Exception(const string& message);
|
||||||
Exception(const int exNo, const string& message);
|
Exception(const int exNo, const string& message);
|
||||||
Exception(const int exNo, const string& message,
|
Exception(const int exNo, const string& message, const char* file,
|
||||||
const char* file, const char* func, const int line);
|
const char* func, const int line);
|
||||||
virtual ~Exception() throw ();
|
virtual ~Exception() throw ();
|
||||||
const char* getFileName();
|
const char* getFileName();
|
||||||
const char* getFunctionName();
|
const char* getFunctionName();
|
||||||
@@ -120,7 +120,6 @@ private:
|
|||||||
int _line;
|
int _line;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class QueElement
|
Class QueElement
|
||||||
====================================*/
|
====================================*/
|
||||||
@@ -175,10 +174,10 @@ public:
|
|||||||
|
|
||||||
void pop(void)
|
void pop(void)
|
||||||
{
|
{
|
||||||
if ( _head )
|
if (_head)
|
||||||
{
|
{
|
||||||
QueElement<T>* head = _head;
|
QueElement<T>* head = _head;
|
||||||
if ( _head == _tail )
|
if (_head == _tail)
|
||||||
{
|
{
|
||||||
_head = _tail = nullptr;
|
_head = _tail = nullptr;
|
||||||
}
|
}
|
||||||
@@ -195,7 +194,7 @@ public:
|
|||||||
T* front(void)
|
T* front(void)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
if ( _head )
|
if (_head)
|
||||||
{
|
{
|
||||||
return _head->_element;
|
return _head->_element;
|
||||||
}
|
}
|
||||||
@@ -208,12 +207,12 @@ public:
|
|||||||
|
|
||||||
int post(T* t)
|
int post(T* t)
|
||||||
{
|
{
|
||||||
if ( t && ( _maxSize == 0 || _cnt < _maxSize ))
|
if (t && (_maxSize == 0 || _cnt < _maxSize))
|
||||||
{
|
{
|
||||||
QueElement<T>* elm = new QueElement<T>(t);
|
QueElement<T>* elm = new QueElement<T>(t);
|
||||||
if ( _head )
|
if (_head)
|
||||||
{
|
{
|
||||||
if ( _tail == _head )
|
if (_tail == _head)
|
||||||
{
|
{
|
||||||
elm->_prev = _tail;
|
elm->_prev = _tail;
|
||||||
_tail = elm;
|
_tail = elm;
|
||||||
@@ -262,8 +261,9 @@ private:
|
|||||||
#define TREE23_BI_NODE (2)
|
#define TREE23_BI_NODE (2)
|
||||||
#define TREE23_TRI_NODE (3)
|
#define TREE23_TRI_NODE (3)
|
||||||
|
|
||||||
template <typename K, typename V>
|
template<typename K, typename V>
|
||||||
class Tree23Elm{
|
class Tree23Elm
|
||||||
|
{
|
||||||
template<typename T, typename U> friend class Tree23;
|
template<typename T, typename U> friend class Tree23;
|
||||||
public:
|
public:
|
||||||
Tree23Elm()
|
Tree23Elm()
|
||||||
@@ -292,9 +292,9 @@ private:
|
|||||||
V* _val;
|
V* _val;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename K, typename V>
|
||||||
template <typename K, typename V>
|
class Tree23Node
|
||||||
class Tree23Node{
|
{
|
||||||
template<typename S, typename W> friend class Tree23;
|
template<typename S, typename W> friend class Tree23;
|
||||||
public:
|
public:
|
||||||
Tree23Node(const int type)
|
Tree23Node(const int type)
|
||||||
@@ -320,7 +320,8 @@ public:
|
|||||||
_left = _midle = _right = NULL;
|
_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;
|
_type = type;
|
||||||
_telm0 = telm;
|
_telm0 = telm;
|
||||||
@@ -330,7 +331,9 @@ public:
|
|||||||
_right = right;
|
_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;
|
_type = type;
|
||||||
_telm0 = telm0;
|
_telm0 = telm0;
|
||||||
@@ -354,8 +357,9 @@ private:
|
|||||||
Tree23Node<K, V>* _right;
|
Tree23Node<K, V>* _right;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename K, typename V>
|
template<typename K, typename V>
|
||||||
class Tree23{
|
class Tree23
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
Tree23()
|
Tree23()
|
||||||
{
|
{
|
||||||
@@ -364,7 +368,7 @@ public:
|
|||||||
|
|
||||||
~Tree23()
|
~Tree23()
|
||||||
{
|
{
|
||||||
if ( _root )
|
if (_root)
|
||||||
{
|
{
|
||||||
delete _root;
|
delete _root;
|
||||||
}
|
}
|
||||||
@@ -372,28 +376,28 @@ public:
|
|||||||
|
|
||||||
void add(K* key, V* val)
|
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);
|
_root->_type = abs(_root->_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
Tree23Node<K, V>* add(Tree23Node<K, V>* n, Tree23Elm<K, V>* elm)
|
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);
|
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, elm);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmp0 = elm->compare(n->_telm0);
|
int cmp0 = elm->compare(n->_telm0);
|
||||||
int cmp1 = 0;
|
int cmp1 = 0;
|
||||||
switch ( n->_type )
|
switch (n->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
n->_left = add(n->_left, elm);
|
n->_left = add(n->_left, elm);
|
||||||
return addLeft2(n);
|
return addLeft2(n);
|
||||||
}
|
}
|
||||||
else if ( cmp0 == 0 )
|
else if (cmp0 == 0)
|
||||||
{
|
{
|
||||||
n->_telm0 = elm;
|
n->_telm0 = elm;
|
||||||
return n;
|
return n;
|
||||||
@@ -406,22 +410,22 @@ public:
|
|||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
cmp1 = elm->compare(n->_telm1);
|
cmp1 = elm->compare(n->_telm1);
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
n->_left = add(n->_left, elm);
|
n->_left = add(n->_left, elm);
|
||||||
return addLeft3(n);
|
return addLeft3(n);
|
||||||
}
|
}
|
||||||
else if ( cmp0 == 0 )
|
else if (cmp0 == 0)
|
||||||
{
|
{
|
||||||
n->_telm0 = elm;
|
n->_telm0 = elm;
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
else if ( cmp1 < 0 )
|
else if (cmp1 < 0)
|
||||||
{
|
{
|
||||||
n->_midle = add(n->_midle, elm);
|
n->_midle = add(n->_midle, elm);
|
||||||
return addMidle3(n);
|
return addMidle3(n);
|
||||||
}
|
}
|
||||||
else if ( cmp1 == 0 )
|
else if (cmp1 == 0)
|
||||||
{
|
{
|
||||||
n->_telm1 = elm;
|
n->_telm1 = elm;
|
||||||
return n;
|
return n;
|
||||||
@@ -441,7 +445,7 @@ public:
|
|||||||
void remove(K* k)
|
void remove(K* k)
|
||||||
{
|
{
|
||||||
_root = remove(_root, k);
|
_root = remove(_root, k);
|
||||||
if ( _root != NULL && _root->_type == TREE23_DELETE_ACTIVE )
|
if (_root != NULL && _root->_type == TREE23_DELETE_ACTIVE)
|
||||||
{
|
{
|
||||||
_root = _root->_midle;
|
_root = _root->_midle;
|
||||||
}
|
}
|
||||||
@@ -449,23 +453,23 @@ public:
|
|||||||
|
|
||||||
Tree23Node<K, V>* remove(Tree23Node<K, V>* node, K* k)
|
Tree23Node<K, V>* remove(Tree23Node<K, V>* node, K* k)
|
||||||
{
|
{
|
||||||
if ( node == NULL )
|
if (node == NULL)
|
||||||
{
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
int cmp0 = k->compare(node->_telm0->_key);
|
int cmp0 = k->compare(node->_telm0->_key);
|
||||||
int cmp1 = 0;
|
int cmp1 = 0;
|
||||||
switch ( node->_type )
|
switch (node->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
node->_left = remove( node->_left, k);
|
node->_left = remove(node->_left, k);
|
||||||
return removeLeft2(node);
|
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);
|
return new Tree23Node<K, V>(TREE23_DELETE_ACTIVE);
|
||||||
}
|
}
|
||||||
@@ -481,14 +485,14 @@ public:
|
|||||||
}
|
}
|
||||||
case 3:
|
case 3:
|
||||||
cmp1 = k->compare(node->_telm1->_key);
|
cmp1 = k->compare(node->_telm1->_key);
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
node->_left = remove(node->_left, k);
|
node->_left = remove(node->_left, k);
|
||||||
return removeLeft3(node);
|
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);
|
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1);
|
||||||
}
|
}
|
||||||
@@ -497,14 +501,14 @@ public:
|
|||||||
node->_telm0 = maxLeft;
|
node->_telm0 = maxLeft;
|
||||||
return removeLeft3(node);
|
return removeLeft3(node);
|
||||||
}
|
}
|
||||||
else if ( cmp1 < 0 )
|
else if (cmp1 < 0)
|
||||||
{
|
{
|
||||||
node->_midle = remove(node->_midle, k);
|
node->_midle = remove(node->_midle, k);
|
||||||
return removeMidle3(node);
|
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);
|
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0);
|
||||||
}
|
}
|
||||||
@@ -534,8 +538,9 @@ public:
|
|||||||
switch (node->_type)
|
switch (node->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
if ( cmp0 < 0 ) node = node->_left;
|
if (cmp0 < 0)
|
||||||
else if ( cmp0 == 0 )
|
node = node->_left;
|
||||||
|
else if (cmp0 == 0)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -546,19 +551,19 @@ public:
|
|||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
cmp1 = key->compare(node->_telm1->_key);
|
cmp1 = key->compare(node->_telm1->_key);
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
node = node->_left;
|
node = node->_left;
|
||||||
}
|
}
|
||||||
else if ( cmp0 == 0 )
|
else if (cmp0 == 0)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if ( cmp1 < 0 )
|
else if (cmp1 < 0)
|
||||||
{
|
{
|
||||||
node = node->_midle;
|
node = node->_midle;
|
||||||
}
|
}
|
||||||
else if ( cmp1 == 0 )
|
else if (cmp1 == 0)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -574,7 +579,6 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
V* getVal(K* key)
|
V* getVal(K* key)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* node = _root;
|
Tree23Node<K, V>* node = _root;
|
||||||
@@ -585,11 +589,11 @@ public:
|
|||||||
switch (node->_type)
|
switch (node->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
node = node->_left;
|
node = node->_left;
|
||||||
}
|
}
|
||||||
else if ( cmp0 == 0 )
|
else if (cmp0 == 0)
|
||||||
{
|
{
|
||||||
return node->_telm0->_val;
|
return node->_telm0->_val;
|
||||||
}
|
}
|
||||||
@@ -600,19 +604,19 @@ public:
|
|||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
cmp1 = key->compare(node->_telm1->_key);
|
cmp1 = key->compare(node->_telm1->_key);
|
||||||
if ( cmp0 < 0 )
|
if (cmp0 < 0)
|
||||||
{
|
{
|
||||||
node = node->_left;
|
node = node->_left;
|
||||||
}
|
}
|
||||||
else if ( cmp0 == 0 )
|
else if (cmp0 == 0)
|
||||||
{
|
{
|
||||||
return node->_telm0->_val;
|
return node->_telm0->_val;
|
||||||
}
|
}
|
||||||
else if ( cmp1 < 0 )
|
else if (cmp1 < 0)
|
||||||
{
|
{
|
||||||
node = node->_midle;
|
node = node->_midle;
|
||||||
}
|
}
|
||||||
else if ( cmp1 == 0 )
|
else if (cmp1 == 0)
|
||||||
{
|
{
|
||||||
return node->_telm1->_val;
|
return node->_telm1->_val;
|
||||||
}
|
}
|
||||||
@@ -632,9 +636,10 @@ private:
|
|||||||
Tree23Node<K, V>* addLeft2(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* addLeft2(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_left;
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -642,11 +647,13 @@ private:
|
|||||||
Tree23Node<K, V>* addLeft3(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* addLeft3(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_left;
|
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;
|
n->_type = TREE23_BI_NODE;
|
||||||
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, node->_midle, node->_right);
|
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE,
|
||||||
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm0, n, nn);
|
node->_telm1, node->_midle, node->_right);
|
||||||
|
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm0, n,
|
||||||
|
nn);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -656,7 +663,8 @@ private:
|
|||||||
Tree23Node<K, V>* n = node->_right;
|
Tree23Node<K, V>* n = node->_right;
|
||||||
if (n != NULL && n->_type == TREE23_INSERT_ACTIVE)
|
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;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -664,10 +672,13 @@ private:
|
|||||||
Tree23Node<K, V>* addRight3(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* addRight3(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_right;
|
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;
|
n->_type = TREE23_BI_NODE;
|
||||||
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, node->_left, node->_midle);
|
Tree23Node<K, V>* nn = new Tree23Node<K, V>(TREE23_BI_NODE,
|
||||||
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm1, nn, n);
|
node->_telm0, node->_left, node->_midle);
|
||||||
|
return new Tree23Node<K, V>(TREE23_INSERT_ACTIVE, node->_telm1, nn,
|
||||||
|
n);
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
@@ -675,16 +686,17 @@ private:
|
|||||||
Tree23Node<K, V>* addMidle3(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* addMidle3(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_midle;
|
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->_left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
|
||||||
n->_right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, n->_right, node->_right);
|
node->_left, n->_left);
|
||||||
|
n->_right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1,
|
||||||
|
n->_right, node->_right);
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Tree23Node<K, V>* removeMax(Tree23Node<K, V>* node, Tree23Elm<K, V>* elm)
|
Tree23Node<K, V>* removeMax(Tree23Node<K, V>* node, Tree23Elm<K, V>* elm)
|
||||||
{
|
{
|
||||||
if (node->_right == NULL)
|
if (node->_right == NULL)
|
||||||
@@ -719,26 +731,29 @@ private:
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Tree23Node<K, V>* removeLeft2(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* removeLeft2(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_left;
|
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>* r = node->_right;
|
||||||
Tree23Node<K, V>* midle;
|
Tree23Node<K, V>* midle;
|
||||||
Tree23Node<K, V>* left;
|
Tree23Node<K, V>* left;
|
||||||
Tree23Node<K, V>* right;
|
Tree23Node<K, V>* right;
|
||||||
|
|
||||||
switch ( r->_type )
|
switch (r->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
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);
|
return new Tree23Node<K, V>(TREE23_DELETE_ACTIVE, midle);
|
||||||
case 3:
|
case 3:
|
||||||
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, n->_midle, r->_left);
|
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
|
||||||
right = new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm1, r->_midle, r->_right);
|
n->_midle, r->_left);
|
||||||
return new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm0, left, right);
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -749,7 +764,7 @@ private:
|
|||||||
Tree23Node<K, V>* removeRight2(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* removeRight2(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_right;
|
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>* l = node->_left;
|
||||||
Tree23Node<K, V>* midle;
|
Tree23Node<K, V>* midle;
|
||||||
@@ -759,12 +774,16 @@ private:
|
|||||||
switch (l->_type)
|
switch (l->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
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);
|
return new Tree23Node<K, V>(-1, midle);
|
||||||
case 3:
|
case 3:
|
||||||
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l->_right, n->_midle);
|
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
|
||||||
left = new Tree23Node<K, V>(TREE23_BI_NODE, l->_telm0, l->_left, l->_midle);
|
l->_right, n->_midle);
|
||||||
return new Tree23Node<K, V>(TREE23_BI_NODE, l->_telm1, left, right);
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -775,21 +794,27 @@ private:
|
|||||||
Tree23Node<K, V>* removeLeft3(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* removeLeft3(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_left;
|
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>* m = node->_midle;
|
||||||
Tree23Node<K, V>* r = node->_right;
|
Tree23Node<K, V>* r = node->_right;
|
||||||
Tree23Node<K, V>* left;
|
Tree23Node<K, V>* left;
|
||||||
Tree23Node<K, V>* midle;
|
Tree23Node<K, V>* midle;
|
||||||
|
|
||||||
switch (m->_type) {
|
switch (m->_type)
|
||||||
|
{
|
||||||
case 2:
|
case 2:
|
||||||
left = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, m->_telm0, n->_midle, m->_left, m->_right);
|
left = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0,
|
||||||
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, left, r);
|
m->_telm0, n->_midle, m->_left, m->_right);
|
||||||
|
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, left,
|
||||||
|
r);
|
||||||
case 3:
|
case 3:
|
||||||
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, n->_midle, m->_left);
|
left = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0,
|
||||||
midle = new Tree23Node<K, V>(TREE23_BI_NODE, m->_telm1, m->_midle, m->_right);
|
n->_midle, m->_left);
|
||||||
return new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0, node->_telm1, left, midle, r);
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -800,7 +825,7 @@ private:
|
|||||||
Tree23Node<K, V>* removeMidle3(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* removeMidle3(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_midle;
|
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>* l = node->_left;
|
||||||
Tree23Node<K, V>* r = node->_right;
|
Tree23Node<K, V>* r = node->_right;
|
||||||
@@ -809,12 +834,17 @@ private:
|
|||||||
switch (r->_type)
|
switch (r->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
right = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm1, r->_telm0, n->_midle, r->_left, r->_right);
|
right = new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm1,
|
||||||
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l, right);
|
r->_telm0, n->_midle, r->_left, r->_right);
|
||||||
|
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l,
|
||||||
|
right);
|
||||||
case 3:
|
case 3:
|
||||||
midle = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, n->_midle, r->_left);
|
midle = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1,
|
||||||
right = new Tree23Node<K, V>(TREE23_BI_NODE, r->_telm1, r->_midle, r->_right);
|
n->_midle, r->_left);
|
||||||
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, r->_telm0, l, midle, right);
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -825,7 +855,7 @@ private:
|
|||||||
Tree23Node<K, V>* removeRight3(Tree23Node<K, V>* node)
|
Tree23Node<K, V>* removeRight3(Tree23Node<K, V>* node)
|
||||||
{
|
{
|
||||||
Tree23Node<K, V>* n = node->_right;
|
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>* l = node->_left;
|
||||||
Tree23Node<K, V>* m = node->_midle;
|
Tree23Node<K, V>* m = node->_midle;
|
||||||
@@ -834,12 +864,17 @@ private:
|
|||||||
switch (m->_type)
|
switch (m->_type)
|
||||||
{
|
{
|
||||||
case 2:
|
case 2:
|
||||||
right = new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0, node->_telm1, m->_left, m->_right, n->_midle);
|
right = new Tree23Node<K, V>(TREE23_TRI_NODE, m->_telm0,
|
||||||
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l, right);
|
node->_telm1, m->_left, m->_right, n->_midle);
|
||||||
|
return new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm0, l,
|
||||||
|
right);
|
||||||
case 3:
|
case 3:
|
||||||
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1, m->_right, n->_midle);
|
right = new Tree23Node<K, V>(TREE23_BI_NODE, node->_telm1,
|
||||||
midle = new Tree23Node<K, V>(TREE23_BI_NODE, m->_telm0, m->_left, m->_midle);
|
m->_right, n->_midle);
|
||||||
return new Tree23Node<K, V>(TREE23_TRI_NODE, node->_telm0, m->_telm1, l, midle, right);
|
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:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -847,14 +882,13 @@ private:
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Tree23Node<K, V>* _root;
|
Tree23Node<K, V>* _root;
|
||||||
};
|
};
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class List
|
Class List
|
||||||
=====================================*/
|
=====================================*/
|
||||||
template <typename T>
|
template<typename T>
|
||||||
class ListElm
|
class ListElm
|
||||||
{
|
{
|
||||||
template<typename U> friend class List;
|
template<typename U> friend class List;
|
||||||
@@ -873,18 +907,23 @@ public:
|
|||||||
{
|
{
|
||||||
return _elm;
|
return _elm;
|
||||||
}
|
}
|
||||||
~ListElm(){}
|
~ListElm()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ListElm<T>* getNext(void){return _next;}
|
ListElm<T>* getNext(void)
|
||||||
|
{
|
||||||
|
return _next;
|
||||||
|
}
|
||||||
T* _elm;
|
T* _elm;
|
||||||
ListElm<T>* _prev;
|
ListElm<T>* _prev;
|
||||||
ListElm<T>* _next;
|
ListElm<T>* _next;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
template <typename T>
|
class List
|
||||||
class List{
|
{
|
||||||
public:
|
public:
|
||||||
List()
|
List()
|
||||||
{
|
{
|
||||||
@@ -899,11 +938,11 @@ public:
|
|||||||
int add(T* t)
|
int add(T* t)
|
||||||
{
|
{
|
||||||
ListElm<T>* elm = new ListElm<T>(t);
|
ListElm<T>* elm = new ListElm<T>(t);
|
||||||
if ( elm == nullptr )
|
if (elm == nullptr)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if ( _head == nullptr )
|
if (_head == nullptr)
|
||||||
{
|
{
|
||||||
_head = elm;
|
_head = elm;
|
||||||
_tail = elm;
|
_tail = elm;
|
||||||
@@ -920,13 +959,13 @@ public:
|
|||||||
|
|
||||||
void erase(ListElm<T>* elm)
|
void erase(ListElm<T>* elm)
|
||||||
{
|
{
|
||||||
if ( _head == elm )
|
if (_head == elm)
|
||||||
{
|
{
|
||||||
_head = elm->_next;
|
_head = elm->_next;
|
||||||
_size--;
|
_size--;
|
||||||
delete elm;
|
delete elm;
|
||||||
}
|
}
|
||||||
else if ( _tail == elm )
|
else if (_tail == elm)
|
||||||
{
|
{
|
||||||
_tail = elm->_prev;
|
_tail = elm->_prev;
|
||||||
elm->_prev->_next = nullptr;
|
elm->_prev->_next = nullptr;
|
||||||
@@ -944,7 +983,7 @@ public:
|
|||||||
void clear(void)
|
void clear(void)
|
||||||
{
|
{
|
||||||
ListElm<T>* p = _head;
|
ListElm<T>* p = _head;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
ListElm<T>* q = p->_next;
|
ListElm<T>* q = p->_next;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -970,14 +1009,12 @@ public:
|
|||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ListElm<T>* _head;
|
ListElm<T>* _head;
|
||||||
ListElm<T>* _tail;
|
ListElm<T>* _tail;
|
||||||
int _size;
|
int _size;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
extern Process* theProcess;
|
extern Process* theProcess;
|
||||||
extern MultiTaskProcess* theMultiTaskProcess;
|
extern MultiTaskProcess* theMultiTaskProcess;
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ MQTTSNPublishHandler::~MQTTSNPublishHandler()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
uint8_t dup;
|
uint8_t dup;
|
||||||
int qos;
|
int qos;
|
||||||
@@ -48,28 +49,30 @@ MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket*
|
|||||||
|
|
||||||
char shortTopic[2];
|
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;
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
pub.msgId = msgId;
|
pub.msgId = msgId;
|
||||||
pub.header.bits.dup = dup;
|
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;
|
pub.header.bits.retain = retained;
|
||||||
|
|
||||||
Topic* topic = nullptr;
|
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[0] = topicid.data.short_name[0];
|
||||||
shortTopic[1] = topicid.data.short_name[1];
|
shortTopic[1] = topicid.data.short_name[1];
|
||||||
@@ -79,56 +82,61 @@ MQTTGWPacket* MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket*
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
topic = client->getTopics()->getTopicById(&topicid);
|
topic = client->getTopics()->getTopicById(&topicid);
|
||||||
if ( !topic )
|
if (!topic)
|
||||||
{
|
{
|
||||||
topic = _gateway->getTopics()->getTopicById(&topicid);
|
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;
|
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;
|
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 */
|
/* Reply PubAck with INVALID_TOPIC_ID to the client */
|
||||||
MQTTSNPacket* pubAck = new MQTTSNPacket();
|
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();
|
Event* ev1 = new Event();
|
||||||
ev1->setClientSendEvent(client, pubAck);
|
ev1->setClientSendEvent(client, pubAck);
|
||||||
_gateway->getClientSendQue()->post(ev1);
|
_gateway->getClientSendQue()->post(ev1);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
if ( topic )
|
if (topic)
|
||||||
{
|
{
|
||||||
pub.topic = (char*)topic->getTopicName()->data();
|
pub.topic = (char*) topic->getTopicName()->data();
|
||||||
pub.topiclen = topic->getTopicName()->length();
|
pub.topiclen = topic->getTopicName()->length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Save a msgId & a TopicId pare for PUBACK */
|
/* 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);
|
client->setWaitedPubTopicId(msgId, topicid.data.id, topicid.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub.payload = (char*)payload;
|
pub.payload = (char*) payload;
|
||||||
pub.payloadlen = payloadlen;
|
pub.payloadlen = payloadlen;
|
||||||
|
|
||||||
MQTTGWPacket* publish = new MQTTGWPacket();
|
MQTTGWPacket* publish = new MQTTGWPacket();
|
||||||
publish->setPUBLISH(&pub);
|
publish->setPUBLISH(&pub);
|
||||||
|
|
||||||
if ( _gateway->getAdapterManager()->isAggregaterActive() && client->isAggregated() )
|
if (_gateway->getAdapterManager()->isAggregaterActive()
|
||||||
|
&& client->isAggregated())
|
||||||
{
|
{
|
||||||
return publish;
|
return publish;
|
||||||
}
|
}
|
||||||
@@ -147,16 +155,16 @@ void MQTTSNPublishHandler::handlePuback(Client* client, MQTTSNPacket* packet)
|
|||||||
uint16_t msgId;
|
uint16_t msgId;
|
||||||
uint8_t rc;
|
uint8_t rc;
|
||||||
|
|
||||||
if ( client->isActive() )
|
if (client->isActive())
|
||||||
{
|
{
|
||||||
if ( packet->getPUBACK(&topicId, &msgId, &rc) == 0 )
|
if (packet->getPUBACK(&topicId, &msgId, &rc) == 0)
|
||||||
{
|
{
|
||||||
return;
|
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();
|
MQTTGWPacket* pubAck = new MQTTGWPacket();
|
||||||
pubAck->setAck(PUBACK, msgId);
|
pubAck->setAck(PUBACK, msgId);
|
||||||
@@ -165,20 +173,21 @@ void MQTTSNPublishHandler::handlePuback(Client* client, MQTTSNPacket* packet)
|
|||||||
_gateway->getBrokerSendQue()->post(ev1);
|
_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);
|
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;
|
uint16_t msgId;
|
||||||
|
|
||||||
if ( client->isActive() )
|
if (client->isActive())
|
||||||
{
|
{
|
||||||
if ( packet->getACK(&msgId) == 0 )
|
if (packet->getACK(&msgId) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -194,12 +203,13 @@ void MQTTSNPublishHandler::handleRegister(Client* client, MQTTSNPacket* packet)
|
|||||||
{
|
{
|
||||||
uint16_t id;
|
uint16_t id;
|
||||||
uint16_t msgId;
|
uint16_t msgId;
|
||||||
MQTTSNString topicName = MQTTSNString_initializer;;
|
MQTTSNString topicName = MQTTSNString_initializer;
|
||||||
|
;
|
||||||
MQTTSN_topicid topicid;
|
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;
|
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 id;
|
||||||
uint16_t msgId;
|
uint16_t msgId;
|
||||||
uint8_t rc;
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTSNPacket* regAck = client->getWaitREGACKPacketList()->getPacket(msgId);
|
MQTTSNPacket* regAck = client->getWaitREGACKPacketList()->getPacket(
|
||||||
|
msgId);
|
||||||
|
|
||||||
if ( regAck != nullptr )
|
if (regAck != nullptr)
|
||||||
{
|
{
|
||||||
client->getWaitREGACKPacketList()->erase(msgId);
|
client->getWaitREGACKPacketList()->erase(msgId);
|
||||||
Event* ev = new Event();
|
Event* ev = new Event();
|
||||||
ev->setClientSendEvent(client, regAck);
|
ev->setClientSendEvent(client, regAck);
|
||||||
_gateway->getClientSendQue()->post(ev);
|
_gateway->getClientSendQue()->post(ev);
|
||||||
}
|
}
|
||||||
if (client->isHoldPingReqest() && client->getWaitREGACKPacketList()->getCount() == 0 )
|
if (client->isHoldPingReqest()
|
||||||
|
&& client->getWaitREGACKPacketList()->getCount() == 0)
|
||||||
{
|
{
|
||||||
/* send PINGREQ to the broker */
|
/* send PINGREQ to the broker */
|
||||||
client->resetPingRequest();
|
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;
|
int msgId = 0;
|
||||||
MQTTGWPacket* publish = handlePublish(client, packet);
|
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
|
else
|
||||||
{
|
{
|
||||||
msgId = _gateway->getAdapterManager()->getAggregater()->addMessageIdTable(client, packet->getMsgId());
|
msgId =
|
||||||
|
_gateway->getAdapterManager()->getAggregater()->addMessageIdTable(
|
||||||
|
client, packet->getMsgId());
|
||||||
}
|
}
|
||||||
publish->setMsgId(msgId);
|
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;
|
uint16_t msgId;
|
||||||
|
|
||||||
if ( packet->getACK(&msgId) == 0 )
|
if (packet->getACK(&msgId) == 0)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public:
|
|||||||
void handlePuback(Client* client, MQTTSNPacket* packet);
|
void handlePuback(Client* client, MQTTSNPacket* packet);
|
||||||
void handleAck(Client* client, MQTTSNPacket* packet, uint8_t packetType);
|
void handleAck(Client* client, MQTTSNPacket* packet, uint8_t packetType);
|
||||||
void handleRegister(Client* client, MQTTSNPacket* packet);
|
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 handleAggregatePublish(Client* client, MQTTSNPacket* packet);
|
||||||
void handleAggregateAck(Client* client, MQTTSNPacket* packet, int type);
|
void handleAggregateAck(Client* client, MQTTSNPacket* packet, int type);
|
||||||
|
|||||||
@@ -21,13 +21,13 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
|
||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class QoSm1Proxy
|
Class QoSm1Proxy
|
||||||
=====================================*/
|
=====================================*/
|
||||||
QoSm1Proxy:: QoSm1Proxy(Gateway* gw) : Adapter(gw)
|
QoSm1Proxy::QoSm1Proxy(Gateway* gw) :
|
||||||
|
Adapter(gw)
|
||||||
{
|
{
|
||||||
_gateway = gw;
|
_gateway = gw;
|
||||||
}
|
}
|
||||||
@@ -37,10 +37,9 @@ QoSm1Proxy::~QoSm1Proxy(void)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void QoSm1Proxy::initialize(char* gwName)
|
void QoSm1Proxy::initialize(char* gwName)
|
||||||
{
|
{
|
||||||
if ( _gateway->hasSecureConnection() )
|
if (_gateway->hasSecureConnection())
|
||||||
{
|
{
|
||||||
_isSecure = true;
|
_isSecure = true;
|
||||||
}
|
}
|
||||||
@@ -54,7 +53,6 @@ void QoSm1Proxy::initialize(char* gwName)
|
|||||||
_isActive = true;
|
_isActive = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool QoSm1Proxy::isActive(void)
|
bool QoSm1Proxy::isActive(void)
|
||||||
{
|
{
|
||||||
return _isActive;
|
return _isActive;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ class MQTTSNPacket;
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class QoSm1Proxy
|
Class QoSm1Proxy
|
||||||
=====================================*/
|
=====================================*/
|
||||||
class QoSm1Proxy : public Adapter
|
class QoSm1Proxy: public Adapter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QoSm1Proxy(Gateway* gw);
|
QoSm1Proxy(Gateway* gw);
|
||||||
@@ -41,13 +41,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
Gateway* _gateway;
|
Gateway* _gateway;
|
||||||
|
|
||||||
bool _isActive {false};
|
bool _isActive { false };
|
||||||
bool _isSecure {false};
|
bool _isSecure { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWQOSM1PROXY_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWQOSM1PROXY_H_ */
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ MQTTSNSubscribeHandler::~MQTTSNSubscribeHandler()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPacket* packet)
|
MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
uint8_t dup;
|
uint8_t dup;
|
||||||
int qos;
|
int qos;
|
||||||
@@ -46,26 +47,27 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
|
|||||||
Event* ev1;
|
Event* ev1;
|
||||||
Event* evsuback;
|
Event* evsuback;
|
||||||
|
|
||||||
if ( packet->getSUBSCRIBE(&dup, &qos, &msgId, &topicFilter) == 0 )
|
if (packet->getSUBSCRIBE(&dup, &qos, &msgId, &topicFilter) == 0)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( msgId == 0 )
|
if (msgId == 0)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( topicFilter.type == MQTTSN_TOPIC_TYPE_PREDEFINED )
|
if (topicFilter.type == MQTTSN_TOPIC_TYPE_PREDEFINED)
|
||||||
{
|
{
|
||||||
topic = client->getTopics()->getTopicById(&topicFilter);
|
topic = client->getTopics()->getTopicById(&topicFilter);
|
||||||
|
|
||||||
if ( !topic )
|
if (!topic)
|
||||||
{
|
{
|
||||||
topic = _gateway->getTopics()->getTopicById(&topicFilter);
|
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
|
else
|
||||||
{
|
{
|
||||||
@@ -74,25 +76,28 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
|
|||||||
}
|
}
|
||||||
topicId = topic->getTopicId();
|
topicId = topic->getTopicId();
|
||||||
subscribe = new MQTTGWPacket();
|
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)
|
else if (topicFilter.type == MQTTSN_TOPIC_TYPE_NORMAL)
|
||||||
{
|
{
|
||||||
topic = client->getTopics()->getTopicByName(&topicFilter);
|
topic = client->getTopics()->getTopicByName(&topicFilter);
|
||||||
if ( topic == nullptr )
|
if (topic == nullptr)
|
||||||
{
|
{
|
||||||
topic = client->getTopics()->add(&topicFilter);
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
topicId = topic->getTopicId();
|
topicId = topic->getTopicId();
|
||||||
subscribe = new MQTTGWPacket();
|
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
|
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[0] << 8;
|
||||||
topicId |= topicFilter.data.short_name[1];
|
topicId |= topicFilter.data.short_name[1];
|
||||||
subscribe = new MQTTGWPacket();
|
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);
|
client->setWaitedSubTopicId(msgId, topicId, topicFilter.type);
|
||||||
|
|
||||||
if ( !client->isAggregated() )
|
if (!client->isAggregated())
|
||||||
{
|
{
|
||||||
ev1 = new Event();
|
ev1 = new Event();
|
||||||
ev1->setBrokerSendEvent(client, subscribe);
|
ev1->setBrokerSendEvent(client, subscribe);
|
||||||
@@ -120,33 +125,32 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPack
|
|||||||
return subscribe;
|
return subscribe;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RespExit: MQTTSNPacket* sSuback = new MQTTSNPacket();
|
||||||
RespExit:
|
sSuback->setSUBACK(qos, topicFilter.data.id, msgId,
|
||||||
MQTTSNPacket* sSuback = new MQTTSNPacket();
|
MQTTSN_RC_NOT_SUPPORTED);
|
||||||
sSuback->setSUBACK(qos, topicFilter.data.id, msgId, MQTTSN_RC_NOT_SUPPORTED);
|
|
||||||
evsuback = new Event();
|
evsuback = new Event();
|
||||||
evsuback->setClientSendEvent(client, sSuback);
|
evsuback->setClientSendEvent(client, sSuback);
|
||||||
_gateway->getClientSendQue()->post(evsuback);
|
_gateway->getClientSendQue()->post(evsuback);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPacket* packet)
|
MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client,
|
||||||
|
MQTTSNPacket* packet)
|
||||||
{
|
{
|
||||||
uint16_t msgId;
|
uint16_t msgId;
|
||||||
MQTTSN_topicid topicFilter;
|
MQTTSN_topicid topicFilter;
|
||||||
MQTTGWPacket* unsubscribe = nullptr;
|
MQTTGWPacket* unsubscribe = nullptr;
|
||||||
|
|
||||||
if ( packet->getUNSUBSCRIBE(&msgId, &topicFilter) == 0 )
|
if (packet->getUNSUBSCRIBE(&msgId, &topicFilter) == 0)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( msgId == 0 )
|
if (msgId == 0)
|
||||||
{
|
{
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (topicFilter.type == MQTTSN_TOPIC_TYPE_SHORT)
|
if (topicFilter.type == MQTTSN_TOPIC_TYPE_SHORT)
|
||||||
{
|
{
|
||||||
char shortTopic[3];
|
char shortTopic[3];
|
||||||
@@ -169,7 +173,7 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPa
|
|||||||
topic = client->getTopics()->getTopicByName(&topicFilter);
|
topic = client->getTopics()->getTopicByName(&topicFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( topic == nullptr )
|
if (topic == nullptr)
|
||||||
{
|
{
|
||||||
MQTTSNPacket* sUnsuback = new MQTTSNPacket();
|
MQTTSNPacket* sUnsuback = new MQTTSNPacket();
|
||||||
sUnsuback->setUNSUBACK(msgId);
|
sUnsuback->setUNSUBACK(msgId);
|
||||||
@@ -185,7 +189,7 @@ MQTTGWPacket* MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPa
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !client->isAggregated() )
|
if (!client->isAggregated())
|
||||||
{
|
{
|
||||||
Event* ev1 = new Event();
|
Event* ev1 = new Event();
|
||||||
ev1->setBrokerSendEvent(client, unsubscribe);
|
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);
|
MQTTGWPacket* subscribe = handleSubscribe(client, packet);
|
||||||
|
|
||||||
if ( subscribe != nullptr )
|
if (subscribe != nullptr)
|
||||||
{
|
{
|
||||||
int msgId = 0;
|
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
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -224,7 +234,8 @@ void MQTTSNSubscribeHandler::handleAggregateSubscribe(Client* client, MQTTSNPack
|
|||||||
string* topicName = new string(str.data, str.len); // topicName is delete by topic
|
string* topicName = new string(str.data, str.len); // topicName is delete by topic
|
||||||
Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL);
|
Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL);
|
||||||
|
|
||||||
_gateway->getAdapterManager()->getAggregater()->addAggregateTopic(&topic, client);
|
_gateway->getAdapterManager()->getAggregater()->addAggregateTopic(
|
||||||
|
&topic, client);
|
||||||
|
|
||||||
subscribe->setMsgId(msgId);
|
subscribe->setMsgId(msgId);
|
||||||
Event* ev = new Event();
|
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);
|
MQTTGWPacket* unsubscribe = handleUnsubscribe(client, packet);
|
||||||
if ( unsubscribe != nullptr )
|
if (unsubscribe != nullptr)
|
||||||
{
|
{
|
||||||
int msgId = 0;
|
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
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
UTF8String str = unsubscribe->getTopic();
|
UTF8String str = unsubscribe->getTopic();
|
||||||
string* topicName = new string(str.data, str.len); // topicName is delete by topic
|
string* topicName = new string(str.data, str.len); // topicName is delete by topic
|
||||||
Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL);
|
Topic topic = Topic(topicName, MQTTSN_TOPIC_TYPE_NORMAL);
|
||||||
_gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(&topic, client);
|
_gateway->getAdapterManager()->getAggregater()->removeAggregateTopic(
|
||||||
|
&topic, client);
|
||||||
|
|
||||||
unsubscribe->setMsgId(msgId);
|
unsubscribe->setMsgId(msgId);
|
||||||
Event* ev = new Event();
|
Event* ev = new Event();
|
||||||
|
|||||||
@@ -42,5 +42,4 @@ private:
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGWSUBSCRIBEHANDLER_H_ */
|
#endif /* MQTTSNGWSUBSCRIBEHANDLER_H_ */
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ Topic::Topic(string* topic, MQTTSN_topicTypes type)
|
|||||||
|
|
||||||
Topic::~Topic()
|
Topic::~Topic()
|
||||||
{
|
{
|
||||||
if ( _topicName )
|
if (_topicName)
|
||||||
{
|
{
|
||||||
delete _topicName;
|
delete _topicName;
|
||||||
}
|
}
|
||||||
@@ -83,12 +83,12 @@ bool Topic::isMatch(string* topicName)
|
|||||||
string wildcard = "#";
|
string wildcard = "#";
|
||||||
string wildcards = "+";
|
string wildcards = "+";
|
||||||
|
|
||||||
while(true)
|
while (true)
|
||||||
{
|
{
|
||||||
loc = topicName->find('/', pos);
|
loc = topicName->find('/', pos);
|
||||||
tloc = _topicName->find('/', tpos);
|
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 subtopic = topicName->substr(pos, loc - pos);
|
||||||
string subtopict = _topicName->substr(tpos, tloc - tpos);
|
string subtopict = _topicName->substr(tpos, tloc - tpos);
|
||||||
@@ -98,11 +98,11 @@ bool Topic::isMatch(string* topicName)
|
|||||||
}
|
}
|
||||||
else if (subtopict == wildcards)
|
else if (subtopict == wildcards)
|
||||||
{
|
{
|
||||||
if ( (tpos = tloc + 1 ) > tlen )
|
if ((tpos = tloc + 1) > tlen)
|
||||||
{
|
{
|
||||||
pos = loc + 1;
|
pos = loc + 1;
|
||||||
loc = topicName->find('/', pos);
|
loc = topicName->find('/', pos);
|
||||||
if ( loc == string::npos )
|
if (loc == string::npos)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -113,13 +113,13 @@ bool Topic::isMatch(string* topicName)
|
|||||||
}
|
}
|
||||||
pos = loc + 1;
|
pos = loc + 1;
|
||||||
}
|
}
|
||||||
else if ( subtopic != subtopict )
|
else if (subtopic != subtopict)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if ( (tpos = tloc + 1) > tlen )
|
if ((tpos = tloc + 1) > tlen)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -127,15 +127,15 @@ bool Topic::isMatch(string* topicName)
|
|||||||
pos = loc + 1;
|
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 subtopic = topicName->substr(pos);
|
||||||
string subtopict = _topicName->substr(tpos);
|
string subtopict = _topicName->substr(tpos);
|
||||||
if ( subtopict == wildcard || subtopict == wildcards)
|
if (subtopict == wildcard || subtopict == wildcards)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else if ( subtopic == subtopict )
|
else if (subtopic == subtopict)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -144,11 +144,11 @@ bool Topic::isMatch(string* topicName)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if ( loc == string::npos && tloc != string::npos )
|
else if (loc == string::npos && tloc != string::npos)
|
||||||
{
|
{
|
||||||
string subtopic = topicName->substr(pos);
|
string subtopic = topicName->substr(pos);
|
||||||
string subtopict = _topicName->substr(tpos, tloc - tpos);
|
string subtopict = _topicName->substr(tpos, tloc - tpos);
|
||||||
if ( subtopic != subtopict)
|
if (subtopic != subtopict)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -157,7 +157,7 @@ bool Topic::isMatch(string* topicName)
|
|||||||
|
|
||||||
return _topicName->substr(tpos) == wildcard;
|
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;
|
return _topicName->substr(tpos) == wildcard;
|
||||||
}
|
}
|
||||||
@@ -166,7 +166,8 @@ bool Topic::isMatch(string* topicName)
|
|||||||
|
|
||||||
void Topic::print(void)
|
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);
|
string sname = string(ch, ch + topicid->data.long_.len);
|
||||||
while (p)
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_topicName->compare(sname) == 0 )
|
if (p->_topicName->compare(sname) == 0)
|
||||||
{
|
{
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
@@ -213,7 +214,7 @@ Topic* Topics::getTopicById(const MQTTSN_topicid* topicid)
|
|||||||
|
|
||||||
while (p)
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_type == topicid->type && p->_topicId == topicid->data.id )
|
if (p->_type == topicid->type && p->_topicId == topicid->data.id)
|
||||||
{
|
{
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
@@ -225,14 +226,14 @@ Topic* Topics::getTopicById(const MQTTSN_topicid* topicid)
|
|||||||
// For MQTTSN_TOPIC_TYPE_NORMAL */
|
// For MQTTSN_TOPIC_TYPE_NORMAL */
|
||||||
Topic* Topics::add(const MQTTSN_topicid* topicid)
|
Topic* Topics::add(const MQTTSN_topicid* topicid)
|
||||||
{
|
{
|
||||||
if (topicid->type != MQTTSN_TOPIC_TYPE_NORMAL )
|
if (topicid->type != MQTTSN_TOPIC_TYPE_NORMAL)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Topic* topic = getTopicByName(topicid);
|
Topic* topic = getTopicByName(topicid);
|
||||||
|
|
||||||
if ( topic )
|
if (topic)
|
||||||
{
|
{
|
||||||
return topic;
|
return topic;
|
||||||
}
|
}
|
||||||
@@ -244,18 +245,17 @@ Topic* Topics::add(const char* topicName, uint16_t id)
|
|||||||
{
|
{
|
||||||
MQTTSN_topicid topicId;
|
MQTTSN_topicid topicId;
|
||||||
|
|
||||||
if ( _cnt >= MAX_TOPIC_PAR_CLIENT )
|
if (_cnt >= MAX_TOPIC_PAR_CLIENT)
|
||||||
{
|
{
|
||||||
return 0;
|
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);
|
topicId.data.long_.len = strlen(topicName);
|
||||||
|
|
||||||
|
|
||||||
Topic* topic = getTopicByName(&topicId);
|
Topic* topic = getTopicByName(&topicId);
|
||||||
|
|
||||||
if ( topic )
|
if (topic)
|
||||||
{
|
{
|
||||||
return topic;
|
return topic;
|
||||||
}
|
}
|
||||||
@@ -270,7 +270,7 @@ Topic* Topics::add(const char* topicName, uint16_t id)
|
|||||||
string* name = new string(topicName);
|
string* name = new string(topicName);
|
||||||
topic->_topicName = name;
|
topic->_topicName = name;
|
||||||
|
|
||||||
if ( id == 0 )
|
if (id == 0)
|
||||||
{
|
{
|
||||||
topic->_type = MQTTSN_TOPIC_TYPE_NORMAL;
|
topic->_type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||||
topic->_topicId = getNextTopicId();
|
topic->_topicId = getNextTopicId();
|
||||||
@@ -283,7 +283,7 @@ Topic* Topics::add(const char* topicName, uint16_t id)
|
|||||||
|
|
||||||
_cnt++;
|
_cnt++;
|
||||||
|
|
||||||
if ( _first == nullptr)
|
if (_first == nullptr)
|
||||||
{
|
{
|
||||||
_first = topic;
|
_first = topic;
|
||||||
}
|
}
|
||||||
@@ -331,7 +331,6 @@ Topic* Topics::match(const MQTTSN_topicid* topicid)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Topics::eraseNormal(void)
|
void Topics::eraseNormal(void)
|
||||||
{
|
{
|
||||||
Topic* topic = _first;
|
Topic* topic = _first;
|
||||||
@@ -340,14 +339,14 @@ void Topics::eraseNormal(void)
|
|||||||
|
|
||||||
while (topic)
|
while (topic)
|
||||||
{
|
{
|
||||||
if ( topic->_type == MQTTSN_TOPIC_TYPE_NORMAL )
|
if (topic->_type == MQTTSN_TOPIC_TYPE_NORMAL)
|
||||||
{
|
{
|
||||||
next = topic->_next;
|
next = topic->_next;
|
||||||
if ( _first == topic )
|
if (_first == topic)
|
||||||
{
|
{
|
||||||
_first = next;
|
_first = next;
|
||||||
}
|
}
|
||||||
if ( prev )
|
if (prev)
|
||||||
{
|
{
|
||||||
prev->_next = next;
|
prev->_next = next;
|
||||||
}
|
}
|
||||||
@@ -376,7 +375,7 @@ Topic* Topics::getNextTopic(Topic* topic)
|
|||||||
void Topics::print(void)
|
void Topics::print(void)
|
||||||
{
|
{
|
||||||
Topic* topic = _first;
|
Topic* topic = _first;
|
||||||
if (topic == nullptr )
|
if (topic == nullptr)
|
||||||
{
|
{
|
||||||
WRITELOG("No Topic.\n");
|
WRITELOG("No Topic.\n");
|
||||||
}
|
}
|
||||||
@@ -398,7 +397,8 @@ uint8_t Topics::getCount(void)
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class TopicIdMap
|
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;
|
_msgId = msgId;
|
||||||
_topicId = topicId;
|
_topicId = topicId;
|
||||||
@@ -434,7 +434,7 @@ TopicIdMap::TopicIdMap()
|
|||||||
TopicIdMap::~TopicIdMap()
|
TopicIdMap::~TopicIdMap()
|
||||||
{
|
{
|
||||||
TopicIdMapElement* p = _first;
|
TopicIdMapElement* p = _first;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
TopicIdMapElement* q = p->_next;
|
TopicIdMapElement* q = p->_next;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -445,9 +445,9 @@ TopicIdMap::~TopicIdMap()
|
|||||||
TopicIdMapElement* TopicIdMap::getElement(uint16_t msgId)
|
TopicIdMapElement* TopicIdMap::getElement(uint16_t msgId)
|
||||||
{
|
{
|
||||||
TopicIdMapElement* p = _first;
|
TopicIdMapElement* p = _first;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
if ( p->_msgId == msgId )
|
if (p->_msgId == msgId)
|
||||||
{
|
{
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
@@ -456,23 +456,25 @@ TopicIdMapElement* TopicIdMap::getElement(uint16_t msgId)
|
|||||||
return 0;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
if ( getElement(msgId) )
|
if (getElement(msgId))
|
||||||
{
|
{
|
||||||
erase(msgId);
|
erase(msgId);
|
||||||
}
|
}
|
||||||
|
|
||||||
TopicIdMapElement* elm = new TopicIdMapElement(msgId, topicId, type);
|
TopicIdMapElement* elm = new TopicIdMapElement(msgId, topicId, type);
|
||||||
if ( elm == 0 )
|
if (elm == 0)
|
||||||
{
|
{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
if ( _first == nullptr )
|
if (_first == nullptr)
|
||||||
{
|
{
|
||||||
_first = elm;
|
_first = elm;
|
||||||
_end = elm;
|
_end = elm;
|
||||||
@@ -490,11 +492,11 @@ TopicIdMapElement* TopicIdMap::add(uint16_t msgId, uint16_t topicId, MQTTSN_topi
|
|||||||
void TopicIdMap::erase(uint16_t msgId)
|
void TopicIdMap::erase(uint16_t msgId)
|
||||||
{
|
{
|
||||||
TopicIdMapElement* p = _first;
|
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;
|
_first = p->_next;
|
||||||
}
|
}
|
||||||
@@ -503,7 +505,7 @@ void TopicIdMap::erase(uint16_t msgId)
|
|||||||
p->_prev->_next = p->_next;
|
p->_prev->_next = p->_next;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( p->_next == nullptr )
|
if (p->_next == nullptr)
|
||||||
{
|
{
|
||||||
_end = p->_prev;
|
_end = p->_prev;
|
||||||
}
|
}
|
||||||
@@ -523,7 +525,7 @@ void TopicIdMap::erase(uint16_t msgId)
|
|||||||
void TopicIdMap::clear(void)
|
void TopicIdMap::clear(void)
|
||||||
{
|
{
|
||||||
TopicIdMapElement* p = _first;
|
TopicIdMapElement* p = _first;
|
||||||
while ( p )
|
while (p)
|
||||||
{
|
{
|
||||||
TopicIdMapElement* q = p->_next;
|
TopicIdMapElement* q = p->_next;
|
||||||
delete p;
|
delete p;
|
||||||
@@ -534,5 +536,3 @@ void TopicIdMap::clear(void)
|
|||||||
_cnt = 0;
|
_cnt = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@
|
|||||||
namespace MQTTSNGW
|
namespace MQTTSNGW
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Topic
|
Class Topic
|
||||||
======================================*/
|
======================================*/
|
||||||
@@ -101,7 +100,8 @@ public:
|
|||||||
TopicIdMap();
|
TopicIdMap();
|
||||||
~TopicIdMap();
|
~TopicIdMap();
|
||||||
TopicIdMapElement* getElement(uint16_t msgId);
|
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 erase(uint16_t msgId);
|
||||||
void clear(void);
|
void clear(void);
|
||||||
private:
|
private:
|
||||||
@@ -112,9 +112,6 @@ private:
|
|||||||
int _maxInflight;
|
int _maxInflight;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWTOPIC_H_ */
|
#endif /* MQTTSNGATEWAY_SRC_MQTTSNGWTOPIC_H_ */
|
||||||
|
|||||||
@@ -17,6 +17,6 @@
|
|||||||
#ifndef MQTTSNGWVERSION_H_IN_
|
#ifndef MQTTSNGWVERSION_H_IN_
|
||||||
#define 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_ */
|
#endif /* MQTTSNGWVERSION_H_IN_ */
|
||||||
|
|||||||
@@ -43,74 +43,74 @@ Gateway::Gateway(void)
|
|||||||
|
|
||||||
Gateway::~Gateway()
|
Gateway::~Gateway()
|
||||||
{
|
{
|
||||||
if ( _params.loginId )
|
if (_params.loginId)
|
||||||
{
|
{
|
||||||
free(_params.loginId);
|
free(_params.loginId);
|
||||||
}
|
}
|
||||||
if ( _params.password )
|
if (_params.password)
|
||||||
{
|
{
|
||||||
free(_params.password);
|
free(_params.password);
|
||||||
}
|
}
|
||||||
if ( _params.gatewayName )
|
if (_params.gatewayName)
|
||||||
{
|
{
|
||||||
free(_params.gatewayName);
|
free(_params.gatewayName);
|
||||||
}
|
}
|
||||||
if ( _params.brokerName )
|
if (_params.brokerName)
|
||||||
{
|
{
|
||||||
free(_params.brokerName);
|
free(_params.brokerName);
|
||||||
}
|
}
|
||||||
if ( _params.port )
|
if (_params.port)
|
||||||
{
|
{
|
||||||
free(_params.port);
|
free(_params.port);
|
||||||
}
|
}
|
||||||
if ( _params.portSecure )
|
if (_params.portSecure)
|
||||||
{
|
{
|
||||||
free(_params.portSecure);
|
free(_params.portSecure);
|
||||||
}
|
}
|
||||||
if ( _params.certKey )
|
if (_params.certKey)
|
||||||
{
|
{
|
||||||
free(_params.certKey);
|
free(_params.certKey);
|
||||||
}
|
}
|
||||||
if ( _params.privateKey )
|
if (_params.privateKey)
|
||||||
{
|
{
|
||||||
free(_params.privateKey);
|
free(_params.privateKey);
|
||||||
}
|
}
|
||||||
if ( _params.rootCApath )
|
if (_params.rootCApath)
|
||||||
{
|
{
|
||||||
free(_params.rootCApath);
|
free(_params.rootCApath);
|
||||||
}
|
}
|
||||||
if ( _params.rootCAfile )
|
if (_params.rootCAfile)
|
||||||
{
|
{
|
||||||
free(_params.rootCAfile);
|
free(_params.rootCAfile);
|
||||||
}
|
}
|
||||||
if ( _params.clientListName )
|
if (_params.clientListName)
|
||||||
{
|
{
|
||||||
free(_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);
|
free(_params.configName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _params.qosMinusClientListName )
|
if (_params.qosMinusClientListName)
|
||||||
{
|
{
|
||||||
free(_params.qosMinusClientListName);
|
free(_params.qosMinusClientListName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _adapterManager )
|
if (_adapterManager)
|
||||||
{
|
{
|
||||||
delete _adapterManager;
|
delete _adapterManager;
|
||||||
}
|
}
|
||||||
if ( _clientList )
|
if (_clientList)
|
||||||
{
|
{
|
||||||
delete _clientList;
|
delete _clientList;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _topics )
|
if (_topics)
|
||||||
{
|
{
|
||||||
delete _topics;
|
delete _topics;
|
||||||
}
|
}
|
||||||
@@ -182,7 +182,7 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
|
|
||||||
if (_params.gatewayId == 0 || _params.gatewayId > 255)
|
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)
|
if (getParam("GatewayName", param) == 0)
|
||||||
@@ -190,9 +190,9 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
_params.gatewayName = strdup(param);
|
_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;
|
_params.mqttVersion = DEFAULT_MQTT_VERSION;
|
||||||
@@ -238,7 +238,7 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
|
|
||||||
if (getParam("PredefinedTopic", param) == 0)
|
if (getParam("PredefinedTopic", param) == 0)
|
||||||
{
|
{
|
||||||
if ( !strcasecmp(param, "YES") )
|
if (!strcasecmp(param, "YES"))
|
||||||
{
|
{
|
||||||
_params.predefinedTopic = true;
|
_params.predefinedTopic = true;
|
||||||
if (getParam("PredefinedTopicList", param) == 0)
|
if (getParam("PredefinedTopicList", param) == 0)
|
||||||
@@ -250,7 +250,7 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
|
|
||||||
if (getParam("AggregatingGateway", param) == 0)
|
if (getParam("AggregatingGateway", param) == 0)
|
||||||
{
|
{
|
||||||
if ( !strcasecmp(param, "YES") )
|
if (!strcasecmp(param, "YES"))
|
||||||
{
|
{
|
||||||
_params.aggregatingGw = true;
|
_params.aggregatingGw = true;
|
||||||
}
|
}
|
||||||
@@ -258,7 +258,7 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
|
|
||||||
if (getParam("Forwarder", param) == 0)
|
if (getParam("Forwarder", param) == 0)
|
||||||
{
|
{
|
||||||
if ( !strcasecmp(param, "YES") )
|
if (!strcasecmp(param, "YES"))
|
||||||
{
|
{
|
||||||
_params.forwarder = true;
|
_params.forwarder = true;
|
||||||
}
|
}
|
||||||
@@ -266,15 +266,15 @@ void Gateway::initialize(int argc, char** argv)
|
|||||||
|
|
||||||
if (getParam("QoS-1", param) == 0)
|
if (getParam("QoS-1", param) == 0)
|
||||||
{
|
{
|
||||||
if ( !strcasecmp(param, "YES") )
|
if (!strcasecmp(param, "YES"))
|
||||||
{
|
{
|
||||||
_params.qosMinus1 = true;
|
_params.qosMinus1 = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Initialize adapters */
|
/* 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 */
|
/* Setup ClientList and Predefined topics */
|
||||||
_clientList->initialize(_params.aggregatingGw);
|
_clientList->initialize(_params.aggregatingGw);
|
||||||
@@ -291,21 +291,23 @@ void Gateway::run(void)
|
|||||||
WRITELOG(" *\n%s\n", PAHO_COPYRIGHT3);
|
WRITELOG(" *\n%s\n", PAHO_COPYRIGHT3);
|
||||||
WRITELOG(" * Version: %s\n", PAHO_GATEWAY_VERSION);
|
WRITELOG(" * Version: %s\n", PAHO_GATEWAY_VERSION);
|
||||||
WRITELOG("%s\n", PAHO_COPYRIGHT4);
|
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);
|
WRITELOG(" ConfigFile: %s\n", _params.configName);
|
||||||
|
|
||||||
if ( _params.clientListName )
|
if (_params.clientListName)
|
||||||
{
|
{
|
||||||
WRITELOG(" ClientList: %s\n", _params.clientListName);
|
WRITELOG(" ClientList: %s\n", _params.clientListName);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( _params.predefinedTopicFileName )
|
if (_params.predefinedTopicFileName)
|
||||||
{
|
{
|
||||||
WRITELOG(" PreDefFile: %s\n", _params.predefinedTopicFileName);
|
WRITELOG(" PreDefFile: %s\n", _params.predefinedTopicFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
WRITELOG(" SensorN/W: %s\n", _sensorNetwork.getDescription());
|
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(" RootCApath: %s\n", _params.rootCApath);
|
||||||
WRITELOG(" RootCAfile: %s\n", _params.rootCAfile);
|
WRITELOG(" RootCAfile: %s\n", _params.rootCAfile);
|
||||||
WRITELOG(" CertKey: %s\n", _params.certKey);
|
WRITELOG(" CertKey: %s\n", _params.certKey);
|
||||||
@@ -388,10 +390,8 @@ Topics* Gateway::getTopics(void)
|
|||||||
|
|
||||||
bool Gateway::hasSecureConnection(void)
|
bool Gateway::hasSecureConnection(void)
|
||||||
{
|
{
|
||||||
return ( _params.certKey
|
return (_params.certKey && _params.privateKey && _params.rootCApath
|
||||||
&& _params.privateKey
|
&& _params.rootCAfile);
|
||||||
&& _params.rootCApath
|
|
||||||
&& _params.rootCAfile );
|
|
||||||
}
|
}
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class EventQue
|
Class EventQue
|
||||||
@@ -414,16 +414,16 @@ EventQue::~EventQue()
|
|||||||
|
|
||||||
void EventQue::setMaxSize(uint16_t maxSize)
|
void EventQue::setMaxSize(uint16_t maxSize)
|
||||||
{
|
{
|
||||||
_que.setMaxSize((int)maxSize);
|
_que.setMaxSize((int) maxSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
Event* EventQue::wait(void)
|
Event* EventQue::wait(void)
|
||||||
{
|
{
|
||||||
Event* ev = nullptr;
|
Event* ev = nullptr;
|
||||||
|
|
||||||
while(ev == nullptr)
|
while (ev == nullptr)
|
||||||
{
|
{
|
||||||
if ( _que.size() == 0 )
|
if (_que.size() == 0)
|
||||||
{
|
{
|
||||||
_sem.wait();
|
_sem.wait();
|
||||||
}
|
}
|
||||||
@@ -438,7 +438,7 @@ Event* EventQue::wait(void)
|
|||||||
Event* EventQue::timedwait(uint16_t millsec)
|
Event* EventQue::timedwait(uint16_t millsec)
|
||||||
{
|
{
|
||||||
Event* ev;
|
Event* ev;
|
||||||
if ( _que.size() == 0 )
|
if (_que.size() == 0)
|
||||||
{
|
{
|
||||||
_sem.timedwait(millsec);
|
_sem.timedwait(millsec);
|
||||||
}
|
}
|
||||||
@@ -460,10 +460,10 @@ Event* EventQue::timedwait(uint16_t millsec)
|
|||||||
|
|
||||||
void EventQue::post(Event* ev)
|
void EventQue::post(Event* ev)
|
||||||
{
|
{
|
||||||
if ( ev )
|
if (ev)
|
||||||
{
|
{
|
||||||
_mutex.lock();
|
_mutex.lock();
|
||||||
if ( _que.post(ev) )
|
if (_que.post(ev))
|
||||||
{
|
{
|
||||||
_sem.post();
|
_sem.post();
|
||||||
}
|
}
|
||||||
@@ -483,7 +483,6 @@ int EventQue::size()
|
|||||||
return sz;
|
return sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Event
|
Class Event
|
||||||
=====================================*/
|
=====================================*/
|
||||||
@@ -586,4 +585,3 @@ MQTTGWPacket* Event::getMQTTGWPacket(void)
|
|||||||
return _mqttGWPacket;
|
return _mqttGWPacket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,8 @@ namespace MQTTSNGW
|
|||||||
====================================*/
|
====================================*/
|
||||||
class Client;
|
class Client;
|
||||||
|
|
||||||
enum EventType{
|
enum EventType
|
||||||
|
{
|
||||||
Et_NA = 0,
|
Et_NA = 0,
|
||||||
EtStop,
|
EtStop,
|
||||||
EtTimeout,
|
EtTimeout,
|
||||||
@@ -88,8 +89,8 @@ enum EventType{
|
|||||||
EtSensornetSend
|
EtSensornetSend
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Event
|
||||||
class Event{
|
{
|
||||||
public:
|
public:
|
||||||
Event();
|
Event();
|
||||||
~Event();
|
~Event();
|
||||||
@@ -108,14 +109,13 @@ public:
|
|||||||
MQTTGWPacket* getMQTTGWPacket(void);
|
MQTTGWPacket* getMQTTGWPacket(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EventType _eventType {Et_NA};
|
EventType _eventType { Et_NA };
|
||||||
Client* _client {nullptr};
|
Client* _client { nullptr };
|
||||||
SensorNetAddress* _sensorNetAddr {nullptr};
|
SensorNetAddress* _sensorNetAddr { nullptr };
|
||||||
MQTTSNPacket* _mqttSNPacket {nullptr};
|
MQTTSNPacket* _mqttSNPacket { nullptr };
|
||||||
MQTTGWPacket* _mqttGWPacket {nullptr};
|
MQTTGWPacket* _mqttGWPacket { nullptr };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class EventQue
|
Class EventQue
|
||||||
====================================*/
|
====================================*/
|
||||||
@@ -136,8 +136,6 @@ private:
|
|||||||
Semaphore _sem;
|
Semaphore _sem;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class GatewayParams
|
Class GatewayParams
|
||||||
====================================*/
|
====================================*/
|
||||||
@@ -145,40 +143,39 @@ class GatewayParams
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
string configDir;
|
string configDir;
|
||||||
char* configName {nullptr};
|
char* configName { nullptr };
|
||||||
char* clientListName {nullptr};
|
char* clientListName { nullptr };
|
||||||
char* loginId {nullptr};
|
char* loginId { nullptr };
|
||||||
char* password {nullptr};
|
char* password { nullptr };
|
||||||
uint16_t keepAlive {0};
|
uint16_t keepAlive { 0 };
|
||||||
uint8_t gatewayId {0};
|
uint8_t gatewayId { 0 };
|
||||||
uint8_t mqttVersion {0};
|
uint8_t mqttVersion { 0 };
|
||||||
uint16_t maxInflightMsgs {0};
|
uint16_t maxInflightMsgs { 0 };
|
||||||
char* gatewayName {nullptr};
|
char* gatewayName { nullptr };
|
||||||
char* brokerName {nullptr};
|
char* brokerName { nullptr };
|
||||||
char* port {nullptr};
|
char* port { nullptr };
|
||||||
char* portSecure {nullptr};
|
char* portSecure{ nullptr };
|
||||||
char* rootCApath {nullptr};
|
char* rootCApath { nullptr };
|
||||||
char* rootCAfile {nullptr};
|
char* rootCAfile { nullptr };
|
||||||
char* certKey {nullptr};
|
char* certKey { nullptr };
|
||||||
char* predefinedTopicFileName {nullptr};
|
char* predefinedTopicFileName { nullptr };
|
||||||
char* privateKey {nullptr};
|
char* privateKey { nullptr };
|
||||||
char* qosMinusClientListName {nullptr};
|
char* qosMinusClientListName { nullptr };
|
||||||
bool clientAuthentication {false};
|
bool clientAuthentication { false };
|
||||||
bool predefinedTopic {false};
|
bool predefinedTopic { false };
|
||||||
bool aggregatingGw {false};
|
bool aggregatingGw { false };
|
||||||
bool qosMinus1 {false};
|
bool qosMinus1 { false };
|
||||||
bool forwarder {false};
|
bool forwarder { false };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Gateway
|
Class Gateway
|
||||||
=====================================*/
|
=====================================*/
|
||||||
class AdapterManager;
|
class AdapterManager;
|
||||||
class ClientList;
|
class ClientList;
|
||||||
|
|
||||||
class Gateway: public MultiTaskProcess{
|
class Gateway: public MultiTaskProcess
|
||||||
|
{
|
||||||
public:
|
public:
|
||||||
Gateway(void);
|
Gateway(void);
|
||||||
~Gateway();
|
~Gateway();
|
||||||
@@ -203,13 +200,13 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
GatewayParams _params;
|
GatewayParams _params;
|
||||||
ClientList* _clientList {nullptr};
|
ClientList* _clientList { nullptr };
|
||||||
EventQue _packetEventQue;
|
EventQue _packetEventQue;
|
||||||
EventQue _brokerSendQue;
|
EventQue _brokerSendQue;
|
||||||
EventQue _clientSendQue;
|
EventQue _clientSendQue;
|
||||||
LightIndicator _lightIndicator;
|
LightIndicator _lightIndicator;
|
||||||
SensorNetwork _sensorNetwork;
|
SensorNetwork _sensorNetwork;
|
||||||
AdapterManager* _adapterManager {nullptr};
|
AdapterManager* _adapterManager { nullptr };
|
||||||
Topics* _topics;
|
Topics* _topics;
|
||||||
bool _stopFlg;
|
bool _stopFlg;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -40,8 +40,7 @@ int main(int argc, char** argv)
|
|||||||
{
|
{
|
||||||
gateway.initialize(argc, argv);
|
gateway.initialize(argc, argv);
|
||||||
gateway.run();
|
gateway.run();
|
||||||
}
|
} catch (const std::exception &ex)
|
||||||
catch (const std::exception &ex)
|
|
||||||
{
|
{
|
||||||
WRITELOG("\nEclipse Paho MQTT-SN Gateway exception: %s\n", ex.what());
|
WRITELOG("\nEclipse Paho MQTT-SN Gateway exception: %s\n", ex.what());
|
||||||
WRITELOG("MQTT-SNGateway [-f Config file name]\n");
|
WRITELOG("MQTT-SNGateway [-f Config file name]\n");
|
||||||
|
|||||||
@@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
using namespace MQTTSNGW;
|
using namespace MQTTSNGW;
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Logmonitor process
|
* Logmonitor process
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user