mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-13 23:46:51 +01:00
Update & BugFix Add GatewayTester
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2009, 2014 IBM Corp.
|
||||
* Copyright (c) 2009, 2014 IBM Corp. Tomoaki YAMAGUCHI
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
@@ -196,7 +196,7 @@ int MQTTGWPacket::recv(Network* network)
|
||||
}
|
||||
if (network->recv(&c, 1) == -1)
|
||||
{
|
||||
return -2;
|
||||
return -1;
|
||||
}
|
||||
_remainingLength += (c & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
@@ -212,7 +212,11 @@ int MQTTGWPacket::recv(Network* network)
|
||||
/* read Payload */
|
||||
int remlen = network->recv(_data, _remainingLength);
|
||||
|
||||
if (remlen == -1 || remlen != _remainingLength )
|
||||
if (remlen == -1 )
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if ( remlen != _remainingLength )
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
@@ -277,9 +281,17 @@ int MQTTGWPacket::getPUBLISH(Publish* pub)
|
||||
pub->topiclen = readInt((char**) &ptr);
|
||||
pub->topic = (char*) _data + 2;
|
||||
ptr += pub->topiclen;
|
||||
pub->msgId = readInt(&ptr);
|
||||
if (_header.bits.qos > 0)
|
||||
{
|
||||
pub->msgId = readInt(&ptr);
|
||||
pub->payloadlen = _remainingLength - pub->topiclen - 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
pub->msgId = 0;
|
||||
pub->payloadlen = _remainingLength - pub->topiclen - 2;
|
||||
}
|
||||
pub->payload = ptr;
|
||||
pub->payloadlen = _remainingLength - pub->topiclen - 4;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -393,7 +405,14 @@ int MQTTGWPacket::setPUBLISH(Publish* pub)
|
||||
writeInt(&ptr, pub->topiclen);
|
||||
memcpy(ptr, pub->topic, pub->topiclen);
|
||||
ptr += pub->topiclen;
|
||||
writeInt(&ptr, pub->msgId);
|
||||
if ( _header.bits.qos > 0 )
|
||||
{
|
||||
writeInt(&ptr, pub->msgId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_remainingLength -= 2;
|
||||
}
|
||||
memcpy(ptr, pub->payload, pub->payloadlen);
|
||||
return 1;
|
||||
}
|
||||
@@ -505,23 +524,14 @@ char* MQTTGWPacket::getMsgId(char* pbuf)
|
||||
|
||||
char* MQTTGWPacket::print(char* pbuf)
|
||||
{
|
||||
uint8_t packetData[MQTTSNGW_MAX_PACKET_SIZE];
|
||||
char* ptr = pbuf;
|
||||
char** pptr = &pbuf;
|
||||
char digit[4];
|
||||
int len = getPacketData(packetData);
|
||||
|
||||
sprintf(*pptr, " %02X",(const unsigned char)_header.byte);
|
||||
*pptr += 3;
|
||||
int len = MQTTPacket_encode((char*) digit, _remainingLength);
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
sprintf(*pptr, " %02X", digit[i]);
|
||||
*pptr += 3;
|
||||
}
|
||||
|
||||
int size = _remainingLength > SIZEOF_LOG_PACKET ? SIZEOF_LOG_PACKET : _remainingLength;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
sprintf(*pptr, " %02X", *(_data + i));
|
||||
sprintf(*pptr, " %02X", packetData[i]);
|
||||
*pptr += 3;
|
||||
}
|
||||
**pptr = 0;
|
||||
|
||||
@@ -132,7 +132,7 @@ void MQTTGWPublishHandler::handlePublish(Client* client, MQTTGWPacket* packet)
|
||||
{
|
||||
/* client is sleeping. save PUBLISH */
|
||||
client->setClientSleepPacket(snPacket);
|
||||
WRITELOG(FORMAT_YE_NL, currentDateTime(), packet->getName(),
|
||||
WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(),
|
||||
RIGHTARROW, client->getClientId(), "is sleeping. a message was saved.");
|
||||
int type = 0;
|
||||
if (pub.header.bits.qos == 1)
|
||||
|
||||
@@ -55,9 +55,9 @@ void MQTTGWSubscribeHandler::handleSuback(Client* client, MQTTGWPacket* packet)
|
||||
qos = rc;
|
||||
}
|
||||
snPacket->setSUBACK(qos, topicId, msgId, returnCode);
|
||||
Event* ev1 = new Event();
|
||||
ev1->setClientSendEvent(client, snPacket);
|
||||
_gateway->getClientSendQue()->post(ev1);
|
||||
Event* evt = new Event();
|
||||
evt->setClientSendEvent(client, snPacket);
|
||||
_gateway->getClientSendQue()->post(evt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,8 +67,8 @@ void MQTTGWSubscribeHandler::handleUnsuback(Client* client, MQTTGWPacket* packet
|
||||
packet->getAck(&ack);
|
||||
MQTTSNPacket* snPacket = new MQTTSNPacket();
|
||||
snPacket->setUNSUBACK(ack.msgId);
|
||||
Event* ev1 = new Event();
|
||||
ev1->setClientSendEvent(client, snPacket);
|
||||
_gateway->getClientSendQue()->post(ev1);
|
||||
Event* evt = new Event();
|
||||
evt->setClientSendEvent(client, snPacket);
|
||||
_gateway->getClientSendQue()->post(evt);
|
||||
}
|
||||
|
||||
|
||||
@@ -166,7 +166,7 @@ void BrokerRecvTask::run(void)
|
||||
*/
|
||||
int BrokerRecvTask::log(Client* client, MQTTGWPacket* packet)
|
||||
{
|
||||
char pbuf[SIZEOF_LOG_PACKET * 3];
|
||||
char pbuf[(SIZEOF_LOG_PACKET + 5 )* 3];
|
||||
char msgId[6];
|
||||
int rc = 0;
|
||||
|
||||
@@ -174,23 +174,23 @@ int BrokerRecvTask::log(Client* client, MQTTGWPacket* packet)
|
||||
{
|
||||
case CONNACK:
|
||||
case DISCONNECT:
|
||||
WRITELOG(FORMAT_YE_GR_MSGID, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case PUBLISH:
|
||||
WRITELOG(FORMAT_GR_MSGID_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_Y_W_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
WRITELOG(FORMAT_GR_MSGID, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case SUBACK:
|
||||
case UNSUBACK:
|
||||
WRITELOG(FORMAT_GR_MSGID, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case PINGRESP:
|
||||
WRITELOG(FORMAT_YE_GR, currentDateTime(), packet->getName(), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), LEFTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
default:
|
||||
rc = -1;
|
||||
|
||||
@@ -35,6 +35,7 @@ BrokerSendTask::BrokerSendTask(Gateway* gateway)
|
||||
_gateway->attach((Thread*)this);
|
||||
_host = 0;
|
||||
_service = 0;
|
||||
_serviceSecure = 0;
|
||||
_light = 0;
|
||||
}
|
||||
|
||||
@@ -48,6 +49,10 @@ BrokerSendTask::~BrokerSendTask()
|
||||
{
|
||||
free(_service);
|
||||
}
|
||||
if (_serviceSecure)
|
||||
{
|
||||
free(_serviceSecure);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,6 +70,10 @@ void BrokerSendTask::initialize(int argc, char** argv)
|
||||
{
|
||||
_service = strdup(param);
|
||||
}
|
||||
if (_gateway->getParam("BrokerSecurePortNo", param) == 0)
|
||||
{
|
||||
_serviceSecure = strdup(param);
|
||||
}
|
||||
_light = _gateway->getLightIndicator();
|
||||
}
|
||||
|
||||
@@ -84,10 +93,21 @@ void BrokerSendTask::run()
|
||||
client = ev->getClient();
|
||||
packet = ev->getMQTTGWPacket();
|
||||
|
||||
if ( client->getNetwork()->isValid() && packet->getType() == CONNECT )
|
||||
{
|
||||
client->getNetwork()->close();
|
||||
}
|
||||
|
||||
if ( !client->getNetwork()->isValid() )
|
||||
{
|
||||
/* connect to the broker and send a packet */
|
||||
if ( !client->getNetwork()->connect(_host, _service) )
|
||||
char* service = _service;
|
||||
if (client->isSecureNetwork())
|
||||
{
|
||||
service = _serviceSecure;
|
||||
}
|
||||
|
||||
if ( !client->getNetwork()->connect(_host, service) )
|
||||
{
|
||||
/* disconnect the broker and chage the client's status */
|
||||
WRITELOG("%s BrokerSendTask can't open the socket. errno=%d %s%s\n",
|
||||
@@ -137,10 +157,10 @@ void BrokerSendTask::log(Client* client, MQTTGWPacket* packet)
|
||||
switch (packet->getType())
|
||||
{
|
||||
case CONNECT:
|
||||
WRITELOG(FORMAT_YE_GR, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case PUBLISH:
|
||||
WRITELOG(FORMAT_WH_GR_MSGID_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case SUBSCRIBE:
|
||||
case UNSUBSCRIBE:
|
||||
@@ -148,13 +168,13 @@ void BrokerSendTask::log(Client* client, MQTTGWPacket* packet)
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
WRITELOG(FORMAT_WH_GR_MSGID, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_Y_W, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case PINGREQ:
|
||||
WRITELOG(FORMAT_YE_GR, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case DISCONNECT:
|
||||
WRITELOG(FORMAT_YE_GR, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_Y_W, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -40,6 +40,7 @@ private:
|
||||
Gateway* _gateway;
|
||||
char* _host;
|
||||
char* _service;
|
||||
char* _serviceSecure;
|
||||
LightIndicator* _light;
|
||||
};
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ ClientList::~ClientList()
|
||||
bool ClientList::authorize(const char* fileName)
|
||||
{
|
||||
FILE* fp;
|
||||
char buf[258];
|
||||
char buf[MAX_CLIENTID_LENGTH + 256];
|
||||
size_t pos;;
|
||||
bool secure;
|
||||
bool stable;
|
||||
@@ -85,7 +85,7 @@ bool ClientList::authorize(const char* fileName)
|
||||
|
||||
if ((fp = fopen(fileName, "r")) != 0)
|
||||
{
|
||||
while (fgets(buf, 256, fp) != 0)
|
||||
while (fgets(buf, MAX_CLIENTID_LENGTH + 254, fp) != 0)
|
||||
{
|
||||
if (*buf == '#')
|
||||
{
|
||||
@@ -175,6 +175,25 @@ Client* ClientList::getClient(void)
|
||||
return _firstClient;
|
||||
}
|
||||
|
||||
Client* ClientList::getClient(uint8_t* clientId)
|
||||
{
|
||||
_mutex.lock();
|
||||
Client* client = _firstClient;
|
||||
|
||||
while (client != 0)
|
||||
{
|
||||
//printf("ClientList: clientId = %s\n", client->getClientId());
|
||||
if (strcmp((const char*)client->getClientId(), (const char*)clientId) == 0 )
|
||||
{
|
||||
_mutex.unlock();
|
||||
return client;
|
||||
}
|
||||
client = client->_nextClient;
|
||||
}
|
||||
_mutex.unlock();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Client* ClientList::createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine, bool secure)
|
||||
{
|
||||
Client* client = 0;
|
||||
@@ -644,6 +663,16 @@ const char* Client::getStatus(void)
|
||||
return theClientStatus[_status];
|
||||
}
|
||||
|
||||
Client* Client::getOTAClient(void)
|
||||
{
|
||||
return _otaClient;
|
||||
}
|
||||
|
||||
void Client::setOTAClient(Client* cl)
|
||||
{
|
||||
_otaClient =cl;
|
||||
}
|
||||
|
||||
/*=====================================
|
||||
Class Topic
|
||||
======================================*/
|
||||
@@ -808,7 +837,7 @@ Topic* Topics::getTopic(MQTTSN_topicid* topicid)
|
||||
Topic* p = _first;
|
||||
while (p)
|
||||
{
|
||||
if (p->_topicId == topicid->data.id)
|
||||
if (strncmp(p->_topicName->c_str(), topicid->data.long_.name, topicid->data.long_.len) == 0 )
|
||||
{
|
||||
return p;
|
||||
}
|
||||
|
||||
@@ -284,6 +284,8 @@ public:
|
||||
bool isWaitWillMsg(void);
|
||||
|
||||
Client* getNextClient(void);
|
||||
Client* getOTAClient(void);
|
||||
void setOTAClient(Client* cl);
|
||||
|
||||
private:
|
||||
PacketQue<MQTTSNPacket> _clientSleepPacketQue;
|
||||
@@ -316,7 +318,7 @@ private:
|
||||
|
||||
Client* _nextClient;
|
||||
Client* _prevClient;
|
||||
|
||||
Client* _otaClient;
|
||||
};
|
||||
|
||||
/*=====================================
|
||||
@@ -330,6 +332,7 @@ public:
|
||||
bool authorize(const char* fileName);
|
||||
void erase(Client*);
|
||||
Client* getClient(SensorNetAddress* addr);
|
||||
Client* getClient(uint8_t* clientId);
|
||||
Client* createClient(SensorNetAddress* addr, MQTTSNString* clientId, bool unstableLine,
|
||||
bool secure);
|
||||
uint16_t getClientCount(void);
|
||||
|
||||
@@ -101,7 +101,7 @@ void ClientRecvTask::run()
|
||||
packet->getCONNECT(&data);
|
||||
|
||||
/* create a client */
|
||||
client = _gateway->getClientList()->createClient(_sensorNetwork->getSenderAddress(), &data.clientID, false, false);
|
||||
client = _gateway->getClientList()->createClient(_sensorNetwork->getSenderAddress(), &data.clientID, false, _gateway->getGWParams()->secureConnection);
|
||||
|
||||
if (!client)
|
||||
{
|
||||
@@ -137,36 +137,34 @@ void ClientRecvTask::log(Client* client, MQTTSNPacket* packet)
|
||||
switch (packet->getType())
|
||||
{
|
||||
case MQTTSN_SEARCHGW:
|
||||
WRITELOG(FORMAT_CY_NL, currentDateTime(), packet->getName(), LEFTARROW, CLIENT, packet->print(pbuf));
|
||||
case MQTTSN_PINGREQ:
|
||||
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, CLIENT, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_CONNECT:
|
||||
WRITELOG(FORMAT_YE_WH_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_G_G_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_WILLTOPIC:
|
||||
case MQTTSN_WILLMSG:
|
||||
case MQTTSN_DISCONNECT:
|
||||
case MQTTSN_WILLTOPICUPD:
|
||||
case MQTTSN_WILLMSGUPD:
|
||||
WRITELOG(FORMAT_WHITE_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
case MQTTSN_WILLTOPIC:
|
||||
case MQTTSN_WILLMSG:
|
||||
WRITELOG(FORMAT_Y_G_G, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_PUBLISH:
|
||||
case MQTTSN_REGISTER:
|
||||
case MQTTSN_SUBSCRIBE:
|
||||
case MQTTSN_UNSUBSCRIBE:
|
||||
WRITELOG(FORMAT_WH_MSGID_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
WRITELOG(FORMAT_G_MSGID_G_G_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_REGACK:
|
||||
case MQTTSN_PUBACK:
|
||||
case MQTTSN_PUBREC:
|
||||
case MQTTSN_PUBREL:
|
||||
case MQTTSN_PUBCOMP:
|
||||
WRITELOG(FORMAT_WH_MSGID, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_PINGREQ:
|
||||
WRITELOG(FORMAT_YE_WH_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
WRITELOG(FORMAT_G_MSGID_G_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
default:
|
||||
WRITELOG(FORMAT_WH_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_NL, currentDateTime(), packet->getName(), LEFTARROW, clientId, packet->print(pbuf));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,21 +71,22 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
|
||||
{
|
||||
case MQTTSN_ADVERTISE:
|
||||
case MQTTSN_GWINFO:
|
||||
WRITELOG(FORMAT_CY_NL, currentDateTime(), packet->getName(), RIGHTARROW, CLIENTS, packet->print(pbuf));
|
||||
case MQTTSN_PINGRESP:
|
||||
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW, CLIENTS, packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_CONNACK:
|
||||
case MQTTSN_DISCONNECT:
|
||||
WRITELOG(FORMAT_YE_WH, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_WILLTOPICREQ:
|
||||
case MQTTSN_WILLMSGREQ:
|
||||
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_WILLTOPICRESP:
|
||||
case MQTTSN_WILLMSGRESP:
|
||||
WRITELOG(FORMAT_GR, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_Y_W_G, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_REGISTER:
|
||||
case MQTTSN_PUBLISH:
|
||||
WRITELOG(FORMAT_GR_WH_MSGID_NL, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_W_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_REGACK:
|
||||
case MQTTSN_PUBACK:
|
||||
@@ -94,10 +95,7 @@ void ClientSendTask::log(Client* client, MQTTSNPacket* packet)
|
||||
case MQTTSN_PUBCOMP:
|
||||
case MQTTSN_SUBACK:
|
||||
case MQTTSN_UNSUBACK:
|
||||
WRITELOG(FORMAT_GR_WH_MSGID, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
case MQTTSN_PINGRESP:
|
||||
WRITELOG(FORMAT_YE_WH, currentDateTime(), packet->getName(), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
WRITELOG(FORMAT_W_MSGID_W_G, currentDateTime(), packet->getName(), packet->getMsgId(msgId), RIGHTARROW, client->getClientId(), packet->print(pbuf));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@@ -168,6 +168,17 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
|
||||
if ( !client->isWaitWillMsg() )
|
||||
{
|
||||
DEBUGLOG(" MQTTSNConnectionHandler::handleWillmsg WaitWillMsgFlg is off.\n");
|
||||
if ( !client->isSecureNetwork() )
|
||||
{
|
||||
/* create CONNACK message */
|
||||
MQTTSNPacket* connack = new MQTTSNPacket();
|
||||
connack->setCONNACK(MQTTSN_RC_REJECTED_CONGESTED);
|
||||
|
||||
/* return to the client */
|
||||
Event* evt = new Event();
|
||||
evt->setClientSendEvent(client, connack);
|
||||
_gateway->getClientSendQue()->post(evt);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -186,9 +197,9 @@ void MQTTSNConnectionHandler::handleWillmsg(Client* client, MQTTSNPacket* packet
|
||||
mqttPacket->setCONNECT(connectData, (unsigned char*)_gateway->getGWParams()->loginId, (unsigned char*)_gateway->getGWParams()->password);
|
||||
|
||||
/* Send CONNECT to the broker */
|
||||
Event* ev1 = new Event();
|
||||
ev1->setBrokerSendEvent(client, mqttPacket);
|
||||
_gateway->getBrokerSendQue()->post(ev1);
|
||||
Event* evt = new Event();
|
||||
evt->setBrokerSendEvent(client, mqttPacket);
|
||||
_gateway->getBrokerSendQue()->post(evt);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,23 +19,19 @@
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
|
||||
/*=================================
|
||||
* Starting prompt
|
||||
==================================*/
|
||||
#define GATEWAY_VERSION "(Ver 0.2.1)"
|
||||
|
||||
/*=================================
|
||||
* Log controls
|
||||
==================================*/
|
||||
//#define DEBUG // print out log for debug
|
||||
//#define RINGBUFFER // print out Packets log into shared memory
|
||||
//#define DEBUG_NWSTACK // print out SensorNetwork log
|
||||
|
||||
/*=================================
|
||||
* Parametrs
|
||||
==================================*/
|
||||
#define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size
|
||||
#define SIZEOF_LOG_PACKET (128) // Length of the packet log in bytes
|
||||
#define MAX_CLIENTID_LENGTH (64) // Max length of clientID
|
||||
#define MQTTSNGW_MAX_PACKET_SIZE (1024) // Max Packet size (5+2+TopicLen+PayloadLen)
|
||||
#define SIZEOF_LOG_PACKET (500) // Length of the packet log in bytes
|
||||
|
||||
#define MQTTSNGW_TLS_CA_DIR "/etc/ssl/certs"
|
||||
|
||||
|
||||
@@ -391,6 +391,7 @@ char* MQTTSNPacket::getMsgId(char* pbuf)
|
||||
case MQTTSN_PUBREC:
|
||||
case MQTTSN_PUBREL:
|
||||
case MQTTSN_PUBCOMP:
|
||||
case MQTTSN_UNSUBACK:
|
||||
sprintf(pbuf, " %02X%02X", _buf[2], _buf[3]);
|
||||
break;
|
||||
case MQTTSN_SUBSCRIBE:
|
||||
@@ -399,7 +400,6 @@ char* MQTTSNPacket::getMsgId(char* pbuf)
|
||||
sprintf(pbuf, " %02X%02X", _buf[p + 2], _buf[p + 3]);
|
||||
break;
|
||||
case MQTTSN_SUBACK:
|
||||
case MQTTSN_UNSUBACK:
|
||||
sprintf(pbuf, " %02X%02X", _buf[5], _buf[6]);
|
||||
break;
|
||||
default:
|
||||
|
||||
@@ -53,14 +53,20 @@ Process::Process()
|
||||
{
|
||||
_argc = 0;
|
||||
_argv = 0;
|
||||
_rbsem = new Semaphore(MQTTSNGW_RB_SEMAPHOR_NAME, 0);
|
||||
_rb = new RingBuffer();
|
||||
_configDir = MQTTSNGW_CONFIG_DIRECTORY;
|
||||
_configFile = MQTTSNGW_CONFIG_FILE;
|
||||
}
|
||||
|
||||
Process::~Process()
|
||||
{
|
||||
delete _rb;
|
||||
delete _rbsem;
|
||||
if (_rb )
|
||||
{
|
||||
delete _rb;
|
||||
}
|
||||
if ( _rbsem )
|
||||
{
|
||||
delete _rbsem;
|
||||
}
|
||||
}
|
||||
|
||||
void Process::run()
|
||||
@@ -76,17 +82,26 @@ void Process::initialize(int argc, char** argv)
|
||||
signal(SIGTERM, signalHandler);
|
||||
signal(SIGHUP, signalHandler);
|
||||
|
||||
_configFile = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CONFIG_FILE);
|
||||
|
||||
int opt;
|
||||
while ((opt = getopt(_argc, _argv, "f:")) != -1)
|
||||
{
|
||||
if ( opt == 'f' )
|
||||
{
|
||||
_configFile = string(optarg);
|
||||
string config = string(optarg);
|
||||
size_t pos = 0;
|
||||
if ( (pos = config.find_last_of("/")) == string::npos )
|
||||
{
|
||||
_configFile = config;
|
||||
}
|
||||
else
|
||||
{
|
||||
_configDir = config.substr(0, pos + 1);
|
||||
_configFile = config.substr(pos + 1, config.size() - pos - 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
WRITELOG("Using config file:[%s]\n", _configFile.c_str());
|
||||
_rbsem = new Semaphore(MQTTSNGW_RB_SEMAPHOR_NAME, 0);
|
||||
_rb = new RingBuffer(_configDir.c_str());
|
||||
}
|
||||
|
||||
int Process::getArgc()
|
||||
@@ -106,10 +121,11 @@ int Process::getParam(const char* parameter, char* value)
|
||||
FILE *fp;
|
||||
|
||||
int i = 0, j = 0;
|
||||
string config = _configDir + _configFile;
|
||||
|
||||
if ((fp = fopen(_configFile.c_str(), "r")) == NULL)
|
||||
if ((fp = fopen(config.c_str(), "r")) == NULL)
|
||||
{
|
||||
WRITELOG("No config file:[%s]\n", _configFile.c_str());
|
||||
WRITELOG("No config file:[%s]\n", config.c_str());
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -195,6 +211,16 @@ int Process::checkSignal(void)
|
||||
return theSignaled;
|
||||
}
|
||||
|
||||
const string* Process::getConfigDirName(void)
|
||||
{
|
||||
return &_configDir;
|
||||
}
|
||||
|
||||
const string* Process::getConfigFileName(void)
|
||||
{
|
||||
return &_configFile;
|
||||
}
|
||||
|
||||
/*=====================================
|
||||
Class MultiTaskProcess
|
||||
====================================*/
|
||||
|
||||
@@ -32,8 +32,8 @@ namespace MQTTSNGW
|
||||
==================================*/
|
||||
#define MQTTSNGW_CONFIG_DIRECTORY "./"
|
||||
|
||||
#define MQTTSNGW_CONFIG_FILE "param.conf"
|
||||
#define MQTTSNGW_CLIENT_LIST "clientList.conf"
|
||||
#define MQTTSNGW_CONFIG_FILE "gateway.conf"
|
||||
#define MQTTSNGW_CLIENT_LIST "clients.conf"
|
||||
|
||||
#define MQTTSNGW_MAX_TASK 10 // number of Tasks
|
||||
#define PROCESS_LOG_BUFFER_SIZE 16384 // Ring buffer size for Logs
|
||||
@@ -66,10 +66,12 @@ public:
|
||||
int getParam(const char* parameter, char* value);
|
||||
const char* getLog(void);
|
||||
int checkSignal(void);
|
||||
|
||||
const string* getConfigDirName(void);
|
||||
const string* getConfigFileName(void);
|
||||
private:
|
||||
int _argc;
|
||||
char** _argv;
|
||||
string _configDir;
|
||||
string _configFile;
|
||||
RingBuffer* _rb;
|
||||
Semaphore* _rbsem;
|
||||
|
||||
@@ -67,10 +67,6 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
|
||||
if ( topicid.type == MQTTSN_TOPIC_TYPE_PREDEFINED)
|
||||
{
|
||||
/*
|
||||
* ToDo: PUBLISH predefined Topic procedures.
|
||||
*/
|
||||
|
||||
if(msgId)
|
||||
{
|
||||
/* Reply PubAck to the client */
|
||||
@@ -80,6 +76,54 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
ev1->setClientSendEvent(client, pubAck);
|
||||
_gateway->getClientSendQue()->post(ev1);
|
||||
}
|
||||
|
||||
#ifdef OTA_CLIENTS
|
||||
if ( topicid.data.id == PREDEFINEDID_OTA_REQ )
|
||||
{
|
||||
uint8_t clientId[MAX_CLIENTID_LENGTH + 1];
|
||||
|
||||
if ( payloadlen <= MAX_CLIENTID_LENGTH )
|
||||
{
|
||||
memcpy(clientId, payload, payloadlen);
|
||||
clientId[payloadlen] = 0;
|
||||
Client* cl = _gateway->getClientList()->getClient(clientId);
|
||||
|
||||
if ( cl )
|
||||
{
|
||||
WRITELOG("\033[0m\033[0;33m OTA Client : %s\033[0m\033[0;37m\n",cl->getClientId());
|
||||
MQTTSNPacket* pubota = new MQTTSNPacket();
|
||||
pubota->setPUBLISH(0, 0, 0, 0, topicid, 0, 0);
|
||||
cl->setOTAClient(client);
|
||||
Event* evt = new Event();
|
||||
evt->setClientSendEvent(cl, pubota);
|
||||
_gateway->getClientSendQue()->post(evt);
|
||||
}
|
||||
else
|
||||
{
|
||||
MQTTSNPacket* publish = new MQTTSNPacket();
|
||||
topicid.data.id = PREDEFINEDID_OTA_NO_CLIENT;
|
||||
publish->setPUBLISH(0, 0, 0, 0, topicid, clientId, (uint16_t)strlen((const char*)clientId));
|
||||
Event* evt = new Event();
|
||||
evt->setClientSendEvent(client, publish);
|
||||
_gateway->getClientSendQue()->post(evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( topicid.data.id == PREDEFINEDID_OTA_READY )
|
||||
{
|
||||
Client* cl = client->getOTAClient();
|
||||
if ( cl )
|
||||
{
|
||||
WRITELOG("\033[0m\033[0;33m OTA Manager : %s\033[0m\033[0;37m\n",cl->getClientId());
|
||||
MQTTSNPacket* pubota = new MQTTSNPacket();
|
||||
pubota->setPUBLISH(0, 0, 0, 0, topicid, payload, payloadlen);
|
||||
client->setOTAClient(0);
|
||||
Event* evt = new Event();
|
||||
evt->setClientSendEvent(cl, pubota);
|
||||
_gateway->getClientSendQue()->post(evt);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -96,7 +140,7 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
topic = client->getTopics()->getTopic(topicid.data.id);
|
||||
if( !topic && msgId && qos > 0 )
|
||||
{
|
||||
/* Reply PubAck of INVALID_TOPIC_ID to the client */
|
||||
/* Reply PubAck with INVALID_TOPIC_ID to the client */
|
||||
MQTTSNPacket* pubAck = new MQTTSNPacket();
|
||||
pubAck->setPUBACK( topicid.data.id, msgId, MQTTSN_RC_REJECTED_INVALID_TOPIC_ID);
|
||||
Event* ev1 = new Event();
|
||||
@@ -119,10 +163,10 @@ void MQTTSNPublishHandler::handlePublish(Client* client, MQTTSNPacket* packet)
|
||||
pub.payload = (char*)payload;
|
||||
pub.payloadlen = payloadlen;
|
||||
|
||||
MQTTGWPacket* pulish = new MQTTGWPacket();
|
||||
pulish->setPUBLISH(&pub);
|
||||
MQTTGWPacket* publish = new MQTTGWPacket();
|
||||
publish->setPUBLISH(&pub);
|
||||
Event* ev1 = new Event();
|
||||
ev1->setBrokerSendEvent(client, pulish);
|
||||
ev1->setBrokerSendEvent(client, publish);
|
||||
_gateway->getBrokerSendQue()->post(ev1);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +35,6 @@ MQTTSNSubscribeHandler::~MQTTSNSubscribeHandler()
|
||||
|
||||
void MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPacket* packet)
|
||||
{
|
||||
MQTTGWPacket* subscribe = new MQTTGWPacket();
|
||||
uint8_t dup;
|
||||
int qos;
|
||||
uint16_t msgId;
|
||||
@@ -57,8 +56,9 @@ void MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPacket* packe
|
||||
|
||||
switch (topicFilter.data.id)
|
||||
{
|
||||
case 1: // check topicIds are defined.
|
||||
case 2:
|
||||
case PREDEFINEDID_OTA_REQ: // check topicIds are defined.
|
||||
case PREDEFINEDID_OTA_READY:
|
||||
case PREDEFINEDID_OTA_NO_CLIENT:
|
||||
break;
|
||||
default:
|
||||
rc = MQTTSN_RC_REJECTED_INVALID_TOPIC_ID;
|
||||
@@ -86,6 +86,7 @@ void MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPacket* packe
|
||||
}
|
||||
else
|
||||
{
|
||||
MQTTGWPacket* subscribe = new MQTTGWPacket();
|
||||
topic = client->getTopics()->getTopic(&topicFilter);
|
||||
if (topic == 0)
|
||||
{
|
||||
@@ -132,6 +133,7 @@ void MQTTSNSubscribeHandler::handleSubscribe(Client* client, MQTTSNPacket* packe
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPacket* packet)
|
||||
{
|
||||
uint16_t msgId;
|
||||
@@ -161,9 +163,9 @@ void MQTTSNSubscribeHandler::handleUnsubscribe(Client* client, MQTTSNPacket* pac
|
||||
Event* evsuback = new Event();
|
||||
evsuback->setClientSendEvent(client, sUnsuback);
|
||||
_gateway->getClientSendQue()->post(evsuback);
|
||||
delete unsubscribe;
|
||||
return;
|
||||
}
|
||||
delete unsubscribe;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -25,12 +25,10 @@ char* currentDateTime(void);
|
||||
/*=====================================
|
||||
Class Gateway
|
||||
=====================================*/
|
||||
Gateway::Gateway() :
|
||||
MultiTaskProcess()
|
||||
Gateway::Gateway()
|
||||
{
|
||||
theMultiTaskProcess = this;
|
||||
theProcess = this;
|
||||
resetRingBuffer();
|
||||
_params.loginId = 0;
|
||||
_params.password = 0;
|
||||
}
|
||||
@@ -51,6 +49,7 @@ void Gateway::initialize(int argc, char** argv)
|
||||
{
|
||||
char param[MQTTSNGW_PARAM_MAX];
|
||||
MultiTaskProcess::initialize(argc, argv);
|
||||
resetRingBuffer();
|
||||
|
||||
_params.gatewayId = 0;
|
||||
if (getParam("GatewayID", param) == 0)
|
||||
@@ -102,30 +101,57 @@ void Gateway::initialize(int argc, char** argv)
|
||||
_params.password = (uint8_t*) strdup(param);
|
||||
}
|
||||
|
||||
if (getParam("ClientAuthorization", param) == 0)
|
||||
if (getParam("ClientAuthentication", param) == 0)
|
||||
{
|
||||
string fileName;
|
||||
if (!strcasecmp(param, "YES"))
|
||||
{
|
||||
string fileName = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_CLIENT_LIST);
|
||||
if (getParam("ClientList", param) == 0)
|
||||
{
|
||||
fileName = string(param);
|
||||
}
|
||||
else
|
||||
{
|
||||
fileName = *getConfigDirName() + string(MQTTSNGW_CLIENT_LIST);
|
||||
}
|
||||
|
||||
if (!_clientList.authorize(fileName.c_str()))
|
||||
{
|
||||
throw Exception("Gateway::initialize: can't authorize clients.");
|
||||
throw Exception("Gateway::initialize: No client list which defined by configuration.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (getParam("SecureConnection", param) == 0)
|
||||
{
|
||||
_params.secureConnection = !strcasecmp(param, "YES");
|
||||
}
|
||||
else
|
||||
{
|
||||
_params.secureConnection = false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Gateway::run(void)
|
||||
{
|
||||
_lightIndicator.redLight(true);
|
||||
WRITELOG("%s %s has been started. %s %s\n", currentDateTime(), _params.gatewayName, _sensorNetwork.getType(), GATEWAY_VERSION);
|
||||
WRITELOG("\n%s", PAHO_COPYRIGHT4);
|
||||
WRITELOG("\n%s\n", PAHO_COPYRIGHT0);
|
||||
WRITELOG("%s\n", PAHO_COPYRIGHT1);
|
||||
WRITELOG("%s\n", PAHO_COPYRIGHT2);
|
||||
WRITELOG(" *\n%s\n", PAHO_COPYRIGHT3);
|
||||
WRITELOG("%s\n", GATEWAY_VERSION);
|
||||
WRITELOG("%s\n", PAHO_COPYRIGHT4);
|
||||
WRITELOG("\n%s %s has been started.\n listening on, %s\n", currentDateTime(), _params.gatewayName, _sensorNetwork.getDescription());
|
||||
|
||||
if ( getClientList()->isAuthorized() )
|
||||
{
|
||||
WRITELOG("\n Client authentication is required by the configuration settings.\n");
|
||||
WRITELOG("\nClient authentication is required by the configuration settings.\n\n");
|
||||
}
|
||||
|
||||
/* execute threads & wait StopProcessEvent MQTTSNGWPacketHandleTask posts by CTL-C */
|
||||
|
||||
MultiTaskProcess::run();
|
||||
WRITELOG("%s MQTT-SN Gateway stoped\n", currentDateTime());
|
||||
_lightIndicator.allLightOff();
|
||||
|
||||
@@ -23,50 +23,6 @@
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
|
||||
/*==========================================================
|
||||
* Log Formats
|
||||
===========================================================*/
|
||||
#define BROKER "Broker"
|
||||
#define GATEWAY "Gateway"
|
||||
#define CLIENT "Client"
|
||||
#define CLIENTS "Clients"
|
||||
#define LEFTARROW "<---"
|
||||
#define RIGHTARROW "--->"
|
||||
|
||||
|
||||
#define FORMAT_WHITE_NL "%s %-18s%-6s%-26s%s\n"
|
||||
#define FORMAT_WH_NL "\n%s %-18s%-6s%-26s%s\n"
|
||||
#define FORMAT_WH_MSGID "%s %-11s%-5s %-6s%-26s%s\n"
|
||||
#define FORMAT_WH_MSGID_NL "\n%s %-11s%-5s %-6s%-26s%s\n"
|
||||
#define FORMAT_WH_GR "%s %-18s%-6s\x1b[0m\x1b[32m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_WH_GR_MSGID "%s %-11s%-5s %-6s\x1b[0m\x1b[32m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_WH_GR_MSGID_NL "\n%s %-11s%-5s %-6s\x1b[0m\x1b[32m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
|
||||
#define FORMAT_GR "%s \x1b[0m\x1b[32m%-18s%-6s\x1b[0m\x1b[37m%-26s%s\n"
|
||||
#define FORMAT_GR_NL "\n%s \x1b[0m\x1b[32m%-18s%-6s\x1b[0m\x1b[37m%-26s%s\n"
|
||||
#define FORMAT_GR_MSGID "%s \x1b[0m\x1b[32m%-11s%-5s %-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_GR_MSGID_NL "\n%s \x1b[0m\x1b[32m%-11s%-5s %-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_GR_WH_MSGID "%s \x1b[0m\x1b[32m%-11s%-5s %-6s\x1b[0m\x1b[37m%-26s%s\n"
|
||||
#define FORMAT_GR_WH_MSGID_NL "\n%s \x1b[0m\x1b[32m%-11s%-5s %-6s\x1b[0m\x1b[37m%-26s%s\n"
|
||||
|
||||
#define FORMAT_YE "%s \x1b[0m\x1b[33m%-18s%-6s%-44s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_YE_NL "\n%s \x1b[0m\x1b[33m%-18s%-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_YE_WH "%s \x1b[0m\x1b[33m%-18s%-6s\x1b[0m\x1b[37m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_YE_WH_NL "\n%s \x1b[0m\x1b[33m%-18s%-6s\x1b[0m\x1b[37m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_YE_GR "%s \x1b[0m\x1b[33m%-18s%-6s\x1b[0m\x1b[32m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_YE_GR_MSGID "%s \x1b[0m\x1b[33m%-11s%-5s %-6s\x1b[0m\x1b[32m%-26s\x1b[0m\x1b[37m%s\n"
|
||||
|
||||
#define FORMAT_CY_ANY "%s \x1b[0m\x1b[36m%-18s%-6s%-44s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_CY "%s \x1b[0m\x1b[36m%-18s%-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_CY_NL "\n%s \x1b[0m\x1b[36m%-18s%-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
|
||||
#define FORMAT_BL_NL "\n%s \x1b[0m\x1b[34m%-18s%-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_RED "%s \x1b[0m\x1b[31m%-18s%-6s%-44s\x1b[0m\x1b[37m%s\n"
|
||||
#define FORMAT_RED_NL "\n%s \x1b[0m\x1b[31m%-18s%-6s%-26s\x1b[0m\x1b[37m%s\n"
|
||||
#define ERRMSG_HEADER "\x1b[0m\x1b[31mError:"
|
||||
#define ERRMSG_FOOTER "\x1b[0m\x1b[37m"
|
||||
|
||||
/*==========================================================
|
||||
* Gateway default parameters
|
||||
===========================================================*/
|
||||
@@ -75,6 +31,60 @@ namespace MQTTSNGW
|
||||
#define DEFAULT_MQTT_VERSION (4) // Defualt MQTT version
|
||||
#define DEFAULT_INFLIGHTMESSAGE (10) // Number of inflight messages
|
||||
|
||||
/*=================================
|
||||
* Starting prompt
|
||||
==================================*/
|
||||
#define GATEWAY_VERSION " * Version: 0.3.3"
|
||||
|
||||
#define PAHO_COPYRIGHT0 " * MQTT-SN Transparent Gateway"
|
||||
#define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse"
|
||||
#define PAHO_COPYRIGHT2 " * (http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt-sn.embedded-c.git/)"
|
||||
#define PAHO_COPYRIGHT3 " * Author : Tomoaki YAMAGUCHI"
|
||||
#define PAHO_COPYRIGHT4 " ***************************************************************************"
|
||||
/*==========================================================
|
||||
* Log Formats
|
||||
*
|
||||
* RED : \033[0m\033[1;31m
|
||||
* green : \033[0m\033[0;32m
|
||||
* yellow : \033[0m\033[0;33m
|
||||
* blue : \033[0m\033[0;34m
|
||||
* white : \033[0m\033[0;37m
|
||||
===========================================================*/
|
||||
#define CLIENT "Client"
|
||||
#define CLIENTS "Clients"
|
||||
#define LEFTARROW "<---"
|
||||
#define RIGHTARROW "--->"
|
||||
|
||||
#define FORMAT_Y_G_G_NL "\n%s \033[0m\033[0;33m%-18s\033[0m\033[0;32m%-6s%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_Y_G_G "%s \033[0m\033[0;33m%-18s\033[0m\033[0;32m%-6s%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_Y_Y_G "%s \033[0m\033[0;33m%-18s%-6s\033[0m\033[0;32m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_Y_W_G "%s \033[0m\033[0;33m%-18s\033[0m\033[0;37m%-6s\033[0m\033[0;32m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_Y_Y_W "%s \033[0m\033[0;33m%-18s%-6s\033[0m\033[0;37m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
|
||||
#define FORMAT_G_MSGID_G_G_NL "\n%s \033[0m\033[0;32m%-11s%-5s %-6s%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_G_MSGID_G_G "%s \033[0m\033[0;32m%-11s%-5s %-6s%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_G_MSGID_W_G "%s \033[0m\033[0;32m%-11s%-5s \033[0m\033[0;37m%-6s\033[0m\033[0;32m%-34.32 s\033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_G_MSGID_Y_W "%s \033[0m\033[0;32m%-11s%-5s \033[0m\033[0;33m%-6s\033[0m\033[0;37m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
|
||||
#define FORMAT_W_MSGID_Y_W_NL "\n%s %-11s%-5s \033[0m\033[0;33m%-6s\033[0m\033[0;37m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_W_MSGID_Y_W "%s %-11s%-5s \033[0m\033[0;33m%-6s\033[0m\033[0;37m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_W_MSGID_W_G "%s %-11s%-5s %-6s\033[0m\033[0;32m%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_W_MSGID_G_G "%s %-11s%-5s \033[0m\033[0;32m%-6s%-34.32s \033[0m\033[0;34m%s\033[0m\033[0;37m\n"
|
||||
|
||||
#define FORMAT_BL_NL "\n%s \033[0m\033[0;34m%-18s%-6s%-34.32s %s\033[0m\033[0;37m\n"
|
||||
#define FORMAT_W_NL "\n%s %-18s%-6s%-34.32s %s\n"
|
||||
|
||||
#define ERRMSG_HEADER "\033[0m\033[0;31mError:"
|
||||
#define ERRMSG_FOOTER "\033[0m\033[0;37m"
|
||||
|
||||
/*=====================================
|
||||
Predefined TopicId for OTA
|
||||
====================================*/
|
||||
#define OTA_CLIENTS
|
||||
#define PREDEFINEDID_OTA_REQ (0x0ff0)
|
||||
#define PREDEFINEDID_OTA_READY (0x0ff1)
|
||||
#define PREDEFINEDID_OTA_NO_CLIENT (0x0ff2)
|
||||
|
||||
/*=====================================
|
||||
Class Event
|
||||
====================================*/
|
||||
@@ -144,6 +154,7 @@ typedef struct
|
||||
uint8_t mqttVersion;
|
||||
uint16_t maxInflightMsgs;
|
||||
uint8_t* gatewayName;
|
||||
bool secureConnection;
|
||||
}GatewayParams;
|
||||
|
||||
/*=====================================
|
||||
|
||||
@@ -235,13 +235,18 @@ void Semaphore::timedwait(uint16_t millsec)
|
||||
Class RingBuffer
|
||||
=========================================*/
|
||||
RingBuffer::RingBuffer()
|
||||
{
|
||||
RingBuffer(MQTTSNGW_CONFIG_DIRECTORY);
|
||||
}
|
||||
|
||||
RingBuffer::RingBuffer(const char* keyDirectory)
|
||||
{
|
||||
int fp = 0;
|
||||
string fileName = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_RINGBUFFER_KEY);
|
||||
string fileName = keyDirectory + string(MQTTSNGW_RINGBUFFER_KEY);
|
||||
fp = open(fileName.c_str(), O_CREAT, 0);
|
||||
close(fp);
|
||||
|
||||
fileName = string(MQTTSNGW_CONFIG_DIRECTORY) + string(MQTTSNGW_RB_MUTEX_KEY);
|
||||
fileName = keyDirectory + string(MQTTSNGW_RB_MUTEX_KEY);
|
||||
fp = open(fileName.c_str(), O_CREAT, 0);
|
||||
close(fp);
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ class RingBuffer
|
||||
{
|
||||
public:
|
||||
RingBuffer();
|
||||
RingBuffer(const char* keyDirctory);
|
||||
~RingBuffer();
|
||||
void put(char* buffer);
|
||||
int get(char* buffer, int bufferLength);
|
||||
|
||||
@@ -31,14 +31,14 @@ using namespace MQTTSNGW;
|
||||
=====================================*/
|
||||
char theCurrentTime[32];
|
||||
|
||||
char* currentDateTime()
|
||||
const char* currentDateTime()
|
||||
{
|
||||
struct timeval now;
|
||||
struct tm tstruct;
|
||||
gettimeofday(&now, 0);
|
||||
tstruct = *localtime(&now.tv_sec);
|
||||
strftime(theCurrentTime, sizeof(theCurrentTime), "%Y%m%d %H%M%S", &tstruct);
|
||||
sprintf(theCurrentTime + 15, " %03d", (int)now.tv_usec / 1000 );
|
||||
sprintf(theCurrentTime + 15, ".%03d", (int)now.tv_usec / 1000 );
|
||||
return theCurrentTime;
|
||||
}
|
||||
|
||||
|
||||
@@ -53,12 +53,12 @@ void SensorNetAddress::setAddress(uint32_t IpAddr, uint16_t port)
|
||||
|
||||
/**
|
||||
* convert Text data to SensorNetAddress
|
||||
* @param buf is pointer of PortNo@IP_Address format text
|
||||
* @param buf is pointer of IP_Address:PortNo format text
|
||||
* @return success = 0, Invalid format = -1
|
||||
*/
|
||||
int SensorNetAddress::setAddress(string* data)
|
||||
{
|
||||
size_t pos = data->find_first_of("@");
|
||||
size_t pos = data->find_first_of(":");
|
||||
|
||||
if ( pos == string::npos )
|
||||
{
|
||||
@@ -67,8 +67,8 @@ int SensorNetAddress::setAddress(string* data)
|
||||
return -1;
|
||||
}
|
||||
|
||||
string port = data->substr(0, pos);
|
||||
string ip = data->substr(pos + 1);
|
||||
string ip = data->substr(0, pos);
|
||||
string port = data->substr(pos + 1);
|
||||
int portNo = 0;
|
||||
|
||||
if ((portNo = atoi(port.c_str())) == 0 || (_IpAddr = inet_addr(ip.c_str())) == INADDR_NONE)
|
||||
@@ -124,23 +124,33 @@ int SensorNetwork::initialize(void)
|
||||
char param[MQTTSNGW_PARAM_MAX];
|
||||
uint16_t multicastPortNo = 0;
|
||||
uint16_t unicastPortNo = 0;
|
||||
string ip;
|
||||
|
||||
if (theProcess->getParam("MulticastIP", param) == 0)
|
||||
{
|
||||
ip = param;
|
||||
_description = "UDP Multicast ";
|
||||
_description += param;
|
||||
}
|
||||
if (theProcess->getParam("MulticastPortNo", param) == 0)
|
||||
{
|
||||
multicastPortNo = atoi(param);
|
||||
_description += ":";
|
||||
_description += param;
|
||||
}
|
||||
if (theProcess->getParam("GatewayPortNo", param) == 0)
|
||||
{
|
||||
unicastPortNo = atoi(param);
|
||||
_description += " and Gateway Port ";
|
||||
_description += param;
|
||||
}
|
||||
|
||||
theProcess->getParam("MulticastIP", param);
|
||||
return UDPPort::open(param, multicastPortNo, unicastPortNo);
|
||||
return UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo);
|
||||
}
|
||||
|
||||
const char* SensorNetwork::getType(void)
|
||||
const char* SensorNetwork::getDescription(void)
|
||||
{
|
||||
return "UDP";
|
||||
return _description.c_str();
|
||||
}
|
||||
|
||||
/*=========================================
|
||||
@@ -173,7 +183,7 @@ void UDPPort::close(void)
|
||||
}
|
||||
}
|
||||
|
||||
int UDPPort::open(char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo)
|
||||
int UDPPort::open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo)
|
||||
{
|
||||
char loopch = 0;
|
||||
const int reuse = 1;
|
||||
|
||||
@@ -31,7 +31,6 @@ namespace MQTTSNGW
|
||||
#define D_NWSTACK(...)
|
||||
#endif
|
||||
|
||||
#define SENSORNETWORK_TYPE "UDP"
|
||||
/*===========================================
|
||||
Class SensorNetAddreess
|
||||
============================================*/
|
||||
@@ -67,7 +66,7 @@ public:
|
||||
UDPPort();
|
||||
virtual ~UDPPort();
|
||||
|
||||
int open(char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo);
|
||||
int open(const char* ipAddress, uint16_t multiPortNo, uint16_t uniPortNo);
|
||||
void close(void);
|
||||
int unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* sendToAddr);
|
||||
int broadcast(const uint8_t* buf, uint32_t length);
|
||||
@@ -99,7 +98,7 @@ public:
|
||||
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
||||
int read(uint8_t* buf, uint16_t bufLen);
|
||||
int initialize(void);
|
||||
const char* getType(void);
|
||||
const char* getDescription(void);
|
||||
SensorNetAddress* getSenderAddress(void)
|
||||
{
|
||||
return &_clientAddr;
|
||||
@@ -108,6 +107,7 @@ public:
|
||||
|
||||
private:
|
||||
SensorNetAddress _clientAddr; // Sender's address. not gateway's one.
|
||||
string _description;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -25,10 +25,8 @@ using namespace MQTTSNGW;
|
||||
/*
|
||||
* Gateway Process
|
||||
*
|
||||
* Configure file "/usr/local/etc/mqttsnGateway/config/param.conf"
|
||||
* Clientlist file "/usr/local/etc/mqttsnGateway/config/clientList.conf"
|
||||
* Certificate file "/etc/ssl/certs"
|
||||
* These are defined in MQTTSNGWDefines.h
|
||||
* This is defined in MQTTSNGWDefines.h
|
||||
*/
|
||||
Gateway* gateway = new Gateway();
|
||||
PacketHandleTask* t0 = new PacketHandleTask(gateway);
|
||||
@@ -39,8 +37,9 @@ BrokerSendTask* t4 = new BrokerSendTask(gateway);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
theProcess->initialize(argc, argv);
|
||||
theProcess->run();
|
||||
gateway->initialize(argc, argv);
|
||||
gateway->run();
|
||||
delete gateway;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user