mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-13 07:26:52 +01:00
BugFix of Wildcard of Topic Issue #40
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -703,75 +703,95 @@ uint16_t Topic::getTopicId(void)
|
||||
return _topicId;
|
||||
}
|
||||
|
||||
int Topic::hasWildCard(unsigned int* pos)
|
||||
{
|
||||
unsigned int p = _topicName->find("+", 0);
|
||||
if (p != string::npos)
|
||||
{
|
||||
*pos = p;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
string::iterator it = _topicName->end();
|
||||
if (*it == '#')
|
||||
{
|
||||
*pos = _topicName->size() - 1;
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
*pos = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool Topic::isMatch(string* topicName)
|
||||
{
|
||||
unsigned int pos;
|
||||
|
||||
if (topicName->size() < _topicName->size())
|
||||
string::size_type tlen = _topicName->size();
|
||||
if (topicName->size() < tlen - 2)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (hasWildCard(&pos) == 1)
|
||||
string::size_type tpos = 0;
|
||||
string::size_type tloc = 0;
|
||||
string::size_type pos = 0;
|
||||
string::size_type loc = 0;
|
||||
string wildcard = "#";
|
||||
string wildcards = "+";
|
||||
|
||||
while(true)
|
||||
{
|
||||
if (_topicName->compare(0, pos - 1, *topicName, 0, pos - 1) == 0)
|
||||
loc = topicName->find('/', pos);
|
||||
tloc = _topicName->find('/', tpos);
|
||||
|
||||
if ( loc != string::npos && tloc != string::npos )
|
||||
{
|
||||
if (_topicName->compare(pos + 1, 1, "/") == 0)
|
||||
string subtopic = topicName->substr(pos, loc - pos);
|
||||
string subtopict = _topicName->substr(tpos, tloc - tpos);
|
||||
if (subtopict == wildcard)
|
||||
{
|
||||
unsigned int loc = topicName->find('/', pos);
|
||||
if (loc != 0)
|
||||
return true;
|
||||
}
|
||||
else if (subtopict == wildcards)
|
||||
{
|
||||
if ( (tpos = tloc + 1 ) > tlen )
|
||||
{
|
||||
if (_topicName->compare(pos + 1, _topicName->size() - pos - 1, *topicName, loc,
|
||||
topicName->size() - pos - 1) == 0)
|
||||
pos = loc + 1;
|
||||
loc = topicName->find('/', pos);
|
||||
if ( loc == string::npos )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned int loc = _topicName->find(pos, '/');
|
||||
if (loc != 0)
|
||||
{
|
||||
if (topicName->find('/', loc) != 0)
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pos = loc + 1;
|
||||
}
|
||||
else if ( subtopic != subtopict )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( (tpos = tloc + 1) > tlen )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
pos = loc + 1;
|
||||
}
|
||||
}
|
||||
else if ( loc == string::npos && tloc == string::npos )
|
||||
{
|
||||
string subtopic = topicName->substr(pos);
|
||||
string subtopict = _topicName->substr(tpos);
|
||||
if ( subtopict == wildcard || subtopict == wildcards)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if ( subtopic == subtopict )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else if ( loc != string::npos && tloc == string::npos )
|
||||
{
|
||||
tloc = _topicName->find('#', --tpos);
|
||||
if ( tloc == string::npos )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
else if (hasWildCard(&pos) == 2 && (_topicName->compare(0, pos, *topicName, 0, pos) == 0))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if (_topicName->compare(*topicName) == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*=====================================
|
||||
|
||||
@@ -109,10 +109,8 @@ public:
|
||||
Topic();
|
||||
Topic(string* topic);
|
||||
~Topic();
|
||||
|
||||
string* getTopicName(void);
|
||||
uint16_t getTopicId(void);
|
||||
int hasWildCard(unsigned int* pos);
|
||||
bool isMatch(string* topicName);
|
||||
|
||||
private:
|
||||
@@ -167,12 +165,10 @@ public:
|
||||
TopicIdMap();
|
||||
~TopicIdMap();
|
||||
uint16_t getTopicId(uint16_t msgId, MQTTSN_topicTypes* type);
|
||||
Topic* getTopic(MQTTSN_topicTypes type);
|
||||
int add(uint16_t msgId, uint16_t topicId, MQTTSN_topicTypes type);
|
||||
void erase(uint16_t msgId);
|
||||
void clear(void);
|
||||
private:
|
||||
int find(uint16_t msgId);
|
||||
uint16_t* _msgIds;
|
||||
TopicIdMapelement* _first;
|
||||
TopicIdMapelement* _end;
|
||||
|
||||
@@ -40,6 +40,7 @@ void ClientSendTask::run()
|
||||
{
|
||||
Client* client = 0;
|
||||
MQTTSNPacket* packet = 0;
|
||||
int rc = 0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
@@ -55,20 +56,27 @@ void ClientSendTask::run()
|
||||
{
|
||||
client = ev->getClient();
|
||||
packet = ev->getMQTTSNPacket();
|
||||
packet->unicast(_sensorNetwork, client->getSensorNetAddress());
|
||||
log(client, packet);
|
||||
rc = packet->unicast(_sensorNetwork, client->getSensorNetAddress());
|
||||
}
|
||||
else if (ev->getEventType() == EtBroadcast)
|
||||
{
|
||||
packet = ev->getMQTTSNPacket();
|
||||
packet->broadcast(_sensorNetwork);
|
||||
log(client, packet);
|
||||
rc = packet->broadcast(_sensorNetwork);
|
||||
}
|
||||
else if (ev->getEventType() == EtSensornetSend)
|
||||
{
|
||||
packet = ev->getMQTTSNPacket();
|
||||
packet->unicast(_sensorNetwork, ev->getSensorNetAddress());
|
||||
log(client, packet);
|
||||
rc = packet->unicast(_sensorNetwork, ev->getSensorNetAddress());
|
||||
}
|
||||
|
||||
log(client, packet);
|
||||
if ( rc < 0 )
|
||||
{
|
||||
WRITELOG("%s ClientSendTask can't send a packet to the client.\n",
|
||||
ERRMSG_HEADER, (client ? (const char*)client->getClientId() : UNKNOWNCL ), ERRMSG_FOOTER);
|
||||
}
|
||||
delete ev;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -334,7 +334,10 @@ int MQTTSNPacket::getPINGREQ(void)
|
||||
|
||||
int MQTTSNPacket::getDISCONNECT(uint16_t* duration)
|
||||
{
|
||||
return MQTTSNDeserialize_disconnect((int*) duration, _buf, _bufLen);
|
||||
int dur = 0;
|
||||
int rc = MQTTSNDeserialize_disconnect(&dur, _buf, _bufLen);
|
||||
*duration = (uint16_t)dur;
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTSNPacket::getWILLTOPICUPD(uint8_t* willQoS, uint8_t* willRetain, MQTTSNString* willTopic)
|
||||
|
||||
@@ -25,7 +25,7 @@ namespace MQTTSNGW
|
||||
/*=================================
|
||||
* Starting prompt
|
||||
==================================*/
|
||||
#define GATEWAY_VERSION " * Version: 0.9.2"
|
||||
#define GATEWAY_VERSION " * Version: 0.9.3"
|
||||
|
||||
#define PAHO_COPYRIGHT0 " * MQTT-SN Transparent Gateway"
|
||||
#define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse"
|
||||
|
||||
@@ -301,7 +301,6 @@ int UDPPort::unicast(const uint8_t* buf, uint32_t length, SensorNetAddress* addr
|
||||
dest.sin_family = AF_INET;
|
||||
dest.sin_port = addr->getPortNo();
|
||||
dest.sin_addr.s_addr = addr->getIpAddress();
|
||||
;
|
||||
|
||||
int status = ::sendto(_sockfdUnicast, buf, length, 0, (const sockaddr*) &dest, sizeof(dest));
|
||||
if (status < 0)
|
||||
|
||||
@@ -207,13 +207,11 @@ int XBee::open(char* device, int baudrate)
|
||||
int XBee::broadcast(const uint8_t* payload, uint16_t payloadLen){
|
||||
SensorNetAddress addr;
|
||||
addr.setBroadcastAddress();
|
||||
send(payload, (uint8_t) payloadLen, &addr);
|
||||
return 1;
|
||||
return send(payload, (uint8_t) payloadLen, &addr);
|
||||
}
|
||||
|
||||
int XBee:: unicast(const uint8_t* payload, uint16_t payloadLen, SensorNetAddress* addr){
|
||||
send(payload, (uint8_t) payloadLen, addr);
|
||||
return 1;
|
||||
return send(payload, (uint8_t) payloadLen, addr);
|
||||
}
|
||||
|
||||
int XBee::recv(uint8_t* buf, uint16_t bufLen, SensorNetAddress* clientAddr)
|
||||
@@ -355,14 +353,14 @@ int XBee::send(const uint8_t* payload, uint8_t pLen, SensorNetAddress* addr){
|
||||
D_NWSTACK("\r\n");
|
||||
|
||||
/* wait Txim Status 0x8B */
|
||||
_sem.timedwait(5000); // 5sec
|
||||
_sem.timedwait(XMIT_STATUS_TIME_OVER);
|
||||
|
||||
if ( _respCd || _frameId != _respId )
|
||||
{
|
||||
D_NWSTACK(" frameId = %02x Not Acknowleged\r\n", _frameId);
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
return (int)pLen;
|
||||
}
|
||||
|
||||
void XBee::send(uint8_t c)
|
||||
|
||||
@@ -38,7 +38,8 @@ namespace MQTTSNGW
|
||||
#define API_MODEMSTATUS 0x8A
|
||||
#define API_XMITSTATUS 0x8B
|
||||
|
||||
#define START_BYTE 0x7e
|
||||
#define XMIT_STATUS_TIME_OVER 5000
|
||||
|
||||
#define ESCAPE 0x7d
|
||||
#define XON 0x11
|
||||
#define XOFF 0x13
|
||||
|
||||
@@ -16,7 +16,10 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <cassert>
|
||||
#include "TestProcessFramework.h"
|
||||
#include "TestProcess.h"
|
||||
#include "TestTopics.h"
|
||||
#include "TestQue.h"
|
||||
#include "TestTopicIdMap.h"
|
||||
#include "MQTTSNGWProcess.h"
|
||||
#include "MQTTSNGWClient.h"
|
||||
#include "MQTTSNGWPacket.h"
|
||||
@@ -31,18 +34,18 @@ using namespace MQTTSNGW;
|
||||
|
||||
const char* currentDateTime(void);
|
||||
|
||||
TestProcessFramework::TestProcessFramework()
|
||||
TestProcess::TestProcess()
|
||||
{
|
||||
theMultiTaskProcess = this;
|
||||
theProcess = this;
|
||||
}
|
||||
|
||||
TestProcessFramework::~TestProcessFramework()
|
||||
TestProcess::~TestProcess()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TestProcessFramework::initialize(int argc, char** argv)
|
||||
void TestProcess::initialize(int argc, char** argv)
|
||||
{
|
||||
MultiTaskProcess::initialize(argc, argv);
|
||||
assert(0 == strcmp(CONFDIR, getConfigDirName()->c_str()));
|
||||
@@ -50,63 +53,28 @@ void TestProcessFramework::initialize(int argc, char** argv)
|
||||
resetRingBuffer();
|
||||
}
|
||||
|
||||
void TestProcessFramework::run(void)
|
||||
void TestProcess::run(void)
|
||||
{
|
||||
char value[256];
|
||||
int* v = 0;
|
||||
int i = 0;
|
||||
Timer tm;
|
||||
TestQue que;
|
||||
|
||||
/* Test command line parameter */
|
||||
|
||||
assert(1 == getArgc() || 3 == getArgc() );
|
||||
assert(0 == strcmp(ARGV, *getArgv()));
|
||||
getParam("BrokerName", value);
|
||||
assert(0 == strcmp("iot.eclipse.org", value));
|
||||
|
||||
/* Test RingBuffer */
|
||||
for ( i = 0; i < 1000; i++)
|
||||
{
|
||||
putLog("Test RingBuffer %d ", 1234567890);
|
||||
}
|
||||
putLog("\n\nRingBuffer Test complieted.\n");
|
||||
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
v = new int(i);
|
||||
que.post(v);
|
||||
}
|
||||
assert( 10 == que.size());
|
||||
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
assert(i == *que.front());
|
||||
int* p = que.front();
|
||||
if ( p )
|
||||
{
|
||||
assert(i == *p);
|
||||
que.pop();
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
assert(0 == que.front());
|
||||
assert(0 == que.size());
|
||||
|
||||
que.setMaxSize(5);
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
v = new int(i);
|
||||
que.post(v);
|
||||
assert( 5 >= que.size());
|
||||
}
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
int* p = que.front();
|
||||
if ( p )
|
||||
{
|
||||
que.pop();
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
putLog("\n\nRingBuffer Test complieted.\n\n");
|
||||
|
||||
/* Test Timer */
|
||||
printf("Timer Test start\n");
|
||||
printf("%s Timer start\n", currentDateTime());
|
||||
tm.start(1000);
|
||||
while (!tm.isTimeup());
|
||||
@@ -115,7 +83,26 @@ void TestProcessFramework::run(void)
|
||||
tm.start();
|
||||
while (!tm.isTimeup(1000));
|
||||
printf("%s Timer 1sec\n", currentDateTime());
|
||||
printf("Timer Test completed\n\n");
|
||||
|
||||
/* Test Que */
|
||||
TestQue* tque = new TestQue();
|
||||
tque->test();
|
||||
delete tque;
|
||||
|
||||
/* Test TopicTable */
|
||||
TestTopics* testTopic = new TestTopics();
|
||||
testTopic->test();
|
||||
delete testTopic;
|
||||
|
||||
/* Test TopicIdMap */
|
||||
TestTopicIdMap* testMap = new TestTopicIdMap();
|
||||
testMap->test();
|
||||
delete testMap;
|
||||
|
||||
|
||||
|
||||
/* Test EventQue */
|
||||
printf("EventQue test start.\n");
|
||||
Client* client = new Client();
|
||||
_evQue.setMaxSize(EVENT_CNT);
|
||||
@@ -128,40 +115,7 @@ void TestProcessFramework::run(void)
|
||||
_evQue.post(ev);
|
||||
}
|
||||
|
||||
|
||||
MultiTaskProcess::run();
|
||||
|
||||
printf("ProcessFramework test complited.\n");
|
||||
}
|
||||
|
||||
TestQue::TestQue()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TestQue::~TestQue()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int* TestQue::front(void)
|
||||
{
|
||||
return _que.front();
|
||||
}
|
||||
void TestQue::pop(void)
|
||||
{
|
||||
_que.pop();
|
||||
}
|
||||
int TestQue::size(void)
|
||||
{
|
||||
return _que.size();
|
||||
}
|
||||
void TestQue::setMaxSize(int maxsize)
|
||||
{
|
||||
_que.setMaxSize(maxsize);
|
||||
}
|
||||
|
||||
void TestQue::post(int* val)
|
||||
{
|
||||
_que.post(val);
|
||||
printf("\n\nAll Tests completed.\n");
|
||||
}
|
||||
@@ -13,18 +13,18 @@
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#ifndef TESTPROCESSFRAMEWORK_H_
|
||||
#define TESTPROCESSFRAMEWORK_H_
|
||||
#ifndef TESTPROCESS_H_
|
||||
#define TESTPROCESS_H_
|
||||
|
||||
#include "MQTTSNGWProcess.h"
|
||||
#include "MQTTSNGateway.h"
|
||||
#define EVENT_CNT 10
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
class TestProcessFramework: public MultiTaskProcess{
|
||||
class TestProcess: public MultiTaskProcess{
|
||||
public:
|
||||
TestProcessFramework();
|
||||
~TestProcessFramework();
|
||||
TestProcess();
|
||||
~TestProcess();
|
||||
virtual void initialize(int argc, char** argv);
|
||||
void run(void);
|
||||
EventQue* getEventQue(void) { return &_evQue; }
|
||||
@@ -33,19 +33,6 @@ private:
|
||||
EventQue _evQue;
|
||||
};
|
||||
|
||||
class TestQue
|
||||
{
|
||||
public:
|
||||
TestQue();
|
||||
~TestQue();
|
||||
void post(int*);
|
||||
int* front(void);
|
||||
void pop(void);
|
||||
int size(void);
|
||||
void setMaxSize(int maxsize);
|
||||
private:
|
||||
Que<int> _que;
|
||||
};
|
||||
}
|
||||
|
||||
#endif /* TESTPROCESSFRAMEWORK_H_ */
|
||||
#endif /* TESTPROCESS_H_ */
|
||||
101
MQTTSNGateway/src/tests/TestQue.cpp
Normal file
101
MQTTSNGateway/src/tests/TestQue.cpp
Normal file
@@ -0,0 +1,101 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2016, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#include <string.h>
|
||||
#include <tests/TestProcess.h>
|
||||
#include <cassert>
|
||||
#include "TestQue.h"
|
||||
using namespace std;
|
||||
using namespace MQTTSNGW;
|
||||
|
||||
TestQue::TestQue()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
TestQue::~TestQue()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TestQue::test(void)
|
||||
{
|
||||
int* v = 0;
|
||||
int i = 0;
|
||||
|
||||
printf("Que Test start.\n");
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
v = new int(i);
|
||||
this->post(v);
|
||||
}
|
||||
assert( 10 == this->size());
|
||||
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
assert(i == *this->front());
|
||||
int* p = this->front();
|
||||
if ( p )
|
||||
{
|
||||
assert(i == *p);
|
||||
this->pop();
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
assert(0 == this->front());
|
||||
assert(0 == this->size());
|
||||
|
||||
this->setMaxSize(5);
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
v = new int(i);
|
||||
this->post(v);
|
||||
assert( 5 >= this->size());
|
||||
}
|
||||
for ( i = 0; i < 10; i++ )
|
||||
{
|
||||
int* p = this->front();
|
||||
if ( p )
|
||||
{
|
||||
this->pop();
|
||||
delete p;
|
||||
}
|
||||
}
|
||||
printf("Que test completed.\n\n");
|
||||
}
|
||||
|
||||
int* TestQue::front(void)
|
||||
{
|
||||
return _que.front();
|
||||
}
|
||||
void TestQue::pop(void)
|
||||
{
|
||||
_que.pop();
|
||||
}
|
||||
int TestQue::size(void)
|
||||
{
|
||||
return _que.size();
|
||||
}
|
||||
void TestQue::setMaxSize(int maxsize)
|
||||
{
|
||||
_que.setMaxSize(maxsize);
|
||||
}
|
||||
|
||||
void TestQue::post(int* val)
|
||||
{
|
||||
_que.post(val);
|
||||
}
|
||||
|
||||
|
||||
41
MQTTSNGateway/src/tests/TestQue.h
Normal file
41
MQTTSNGateway/src/tests/TestQue.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2016, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_
|
||||
#define MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_
|
||||
|
||||
#include "MQTTSNGWProcess.h"
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
|
||||
class TestQue
|
||||
{
|
||||
public:
|
||||
TestQue();
|
||||
~TestQue();
|
||||
void post(int*);
|
||||
int* front(void);
|
||||
void pop(void);
|
||||
int size(void);
|
||||
void setMaxSize(int maxsize);
|
||||
void test(void);
|
||||
private:
|
||||
Que<int> _que;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTQUE_H_ */
|
||||
@@ -17,11 +17,11 @@
|
||||
#include <cassert>
|
||||
#include "TestTask.h"
|
||||
#include "Threading.h"
|
||||
|
||||
#include "TestProcess.h"
|
||||
using namespace std;
|
||||
using namespace MQTTSNGW;
|
||||
|
||||
TestTask::TestTask(TestProcessFramework* proc)
|
||||
TestTask::TestTask(TestProcess* proc)
|
||||
{
|
||||
proc->attach((Thread*)this);
|
||||
_proc = proc;
|
||||
@@ -39,23 +39,24 @@ void TestTask::initialize(int argc, char** argv)
|
||||
|
||||
void TestTask::run(void)
|
||||
{
|
||||
int evcnt = 0;
|
||||
EventQue* evQue = _proc->getEventQue();
|
||||
uint16_t duration = 0;
|
||||
int cnt = 0;
|
||||
|
||||
|
||||
while (true)
|
||||
{
|
||||
Event* ev = evQue->timedwait(5000);
|
||||
evcnt++;
|
||||
if ( ev->getEventType() == EtTimeout )
|
||||
{
|
||||
assert(EVENT_CNT == cnt);
|
||||
assert(EVENT_CNT + 1 == evcnt);
|
||||
delete ev;
|
||||
printf("EventQue test complete.\n\n");
|
||||
break;
|
||||
}
|
||||
cnt++;
|
||||
MQTTSNPacket* packet = ev->getMQTTSNPacket();
|
||||
packet->getDISCONNECT(&duration);
|
||||
printf("Event %d\n", duration);
|
||||
delete ev;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
#ifndef TESTTASK_H_
|
||||
#define TESTTASK_H_
|
||||
|
||||
#include "TestProcess.h"
|
||||
#include "Threading.h"
|
||||
#include "TestProcessFramework.h"
|
||||
|
||||
namespace MQTTSNGW
|
||||
{
|
||||
@@ -27,13 +27,13 @@ class TestTask: public Thread
|
||||
MAGIC_WORD_FOR_THREAD;
|
||||
;
|
||||
public:
|
||||
TestTask(TestProcessFramework* proc);
|
||||
TestTask(TestProcess* proc);
|
||||
~TestTask();
|
||||
void initialize(int argc, char** argv);
|
||||
void run(void);
|
||||
|
||||
private:
|
||||
TestProcessFramework* _proc;
|
||||
TestProcess* _proc;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
125
MQTTSNGateway/src/tests/TestTopicIdMap.cpp
Normal file
125
MQTTSNGateway/src/tests/TestTopicIdMap.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2016, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <cassert>
|
||||
#include "TestTopicIdMap.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace MQTTSNGW;
|
||||
|
||||
TestTopicIdMap::TestTopicIdMap()
|
||||
{
|
||||
_map = new TopicIdMap();
|
||||
}
|
||||
|
||||
TestTopicIdMap::~TestTopicIdMap()
|
||||
{
|
||||
delete _map;
|
||||
}
|
||||
|
||||
#define MAXID 30
|
||||
|
||||
void TestTopicIdMap::test(void)
|
||||
{
|
||||
uint16_t id[MAXID];
|
||||
printf("Test TopicIdMat start.\n");
|
||||
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
id[i] = i + 1;
|
||||
}
|
||||
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
_map->add(id[i], id[i], MQTTSN_TOPIC_TYPE_NORMAL);
|
||||
}
|
||||
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
|
||||
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
|
||||
assert((i <= MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == i) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0));
|
||||
}
|
||||
|
||||
//printf("\n");
|
||||
|
||||
for ( int i = 0; i < 5; i++ )
|
||||
{
|
||||
_map->erase(i);
|
||||
}
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
|
||||
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
|
||||
assert((i < 5 && topicId == 0) || (i >= 5 && topicId != 0) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0) );
|
||||
}
|
||||
|
||||
|
||||
_map->clear();
|
||||
//printf("\n");
|
||||
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_SHORT;
|
||||
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
|
||||
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
|
||||
assert( topicId == 0 );
|
||||
}
|
||||
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
_map->add(id[i], id[i], MQTTSN_TOPIC_TYPE_SHORT);
|
||||
}
|
||||
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
|
||||
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
|
||||
assert((i <= MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == i) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0));
|
||||
}
|
||||
|
||||
//printf("\n");
|
||||
|
||||
for ( int i = 0; i < 5; i++ )
|
||||
{
|
||||
_map->erase(i);
|
||||
}
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
|
||||
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
|
||||
assert((i < 5 && topicId == 0) || (i >= 5 && topicId != 0) || (i > MAX_INFLIGHTMESSAGES * 2 + 1 && topicId == 0) );
|
||||
}
|
||||
|
||||
|
||||
_map->clear();
|
||||
|
||||
//printf("\n");
|
||||
for ( int i = 0; i < MAXID; i++ )
|
||||
{
|
||||
MQTTSN_topicTypes type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
uint16_t topicId = _map->getTopicId((uint16_t)i, &type);
|
||||
//printf("TopicId=%d msgId=%d type=%d\n", topicId, i, type);
|
||||
assert( topicId == 0 );
|
||||
}
|
||||
printf("Test TopicIdMat completed.\n\n");
|
||||
}
|
||||
|
||||
32
MQTTSNGateway/src/tests/TestTopicIdMap.h
Normal file
32
MQTTSNGateway/src/tests/TestTopicIdMap.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2016, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_
|
||||
#define MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_
|
||||
|
||||
#include "MQTTSNGWClient.h"
|
||||
|
||||
class TestTopicIdMap
|
||||
{
|
||||
public:
|
||||
TestTopicIdMap();
|
||||
~TestTopicIdMap();
|
||||
void test(void);
|
||||
|
||||
private:
|
||||
TopicIdMap* _map;
|
||||
};
|
||||
|
||||
#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTTOPICIDMAP_H_ */
|
||||
183
MQTTSNGateway/src/tests/TestTopics.cpp
Normal file
183
MQTTSNGateway/src/tests/TestTopics.cpp
Normal file
@@ -0,0 +1,183 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2016, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <cassert>
|
||||
#include "TestTopics.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace MQTTSNGW;
|
||||
|
||||
TestTopics::TestTopics()
|
||||
{
|
||||
_topics = new Topics();
|
||||
}
|
||||
|
||||
TestTopics::~TestTopics()
|
||||
{
|
||||
delete _topics;
|
||||
}
|
||||
|
||||
void TestTopics::test(void)
|
||||
{
|
||||
printf("Topics Test start.\n");
|
||||
|
||||
MQTTSN_topicid topic[12];
|
||||
char tp[12][10];
|
||||
|
||||
/* create Topic */
|
||||
strcpy(tp[0], "Topic/+");
|
||||
tp[0][7] = 0;
|
||||
topic[0].type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic[0].data.long_.len = strlen(tp[0]);
|
||||
topic[0].data.long_.name = tp[0];
|
||||
|
||||
for ( int i = 1; i < 10 ; i++ )
|
||||
{
|
||||
strcpy(tp[i], "Topic/+/");
|
||||
tp[i][8] = 0x30 + i;
|
||||
tp[i][9] = 0;
|
||||
topic[i].type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic[i].data.long_.len = strlen(tp[i]);
|
||||
topic[i].data.long_.name = tp[i];
|
||||
}
|
||||
strcpy(tp[10], "TOPIC/#");
|
||||
tp[10][7] = 0;
|
||||
topic[10].type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic[10].data.long_.len = strlen(tp[10]);
|
||||
topic[10].data.long_.name = tp[10];
|
||||
|
||||
strcpy(tp[11], "+/0/#");
|
||||
tp[11][7] = 0;
|
||||
topic[11].type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
topic[11].data.long_.len = strlen(tp[11]);
|
||||
topic[11].data.long_.name = tp[11];
|
||||
|
||||
/* Add Topic to Topics */
|
||||
for ( int i = 0; i < 12; i++ )
|
||||
{
|
||||
MQTTSN_topicid pos = topic[i];
|
||||
Topic* t = _topics->add(&pos);
|
||||
//printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId());
|
||||
assert(t !=0);
|
||||
}
|
||||
|
||||
for ( int i = 0; i < 5; i++ )
|
||||
{
|
||||
string str = "Test/";
|
||||
str += 0x30 + i;
|
||||
Topic* t = _topics->add(&str);
|
||||
//printf("Topic=%s ID=%d\n", t->getTopicName()->c_str(), t->getTopicId());
|
||||
assert(t !=0);
|
||||
}
|
||||
|
||||
/* Get Topic by MQTTSN_topicid */
|
||||
for ( int i = 0; i < 12; i++ )
|
||||
{
|
||||
Topic* t = _topics->getTopic(&topic[i]);
|
||||
//printf("Topic=%s ID=%d ID=%d\n", t->getTopicName()->c_str(), t->getTopicId(),_topics->getTopicId(&topic[i]));
|
||||
assert(t->getTopicId() == i + 1);
|
||||
}
|
||||
|
||||
/* Get TopicId by MQTTSN_topicid */
|
||||
for ( int i = 0; i < 12; i++ )
|
||||
{
|
||||
uint16_t id = _topics->getTopicId(&topic[i]);
|
||||
//printf("ID=%d \n", id);
|
||||
assert(id == i + 1);
|
||||
}
|
||||
|
||||
/* Test Wilecard */
|
||||
for ( int i = 0; i < 10 ; i++ )
|
||||
{
|
||||
MQTTSN_topicid tp1;
|
||||
char tp0[10];
|
||||
strcpy(tp0, "Topic/");
|
||||
tp0[6] = 0x30 + i;
|
||||
tp0[7] = '/';
|
||||
tp0[8] = 0x30 + i;
|
||||
tp0[9] = 0;
|
||||
tp1.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
tp1.data.long_.len = strlen(tp0);
|
||||
tp1.data.long_.name = tp0;
|
||||
|
||||
Topic* t = _topics->match(&tp1);
|
||||
/*
|
||||
if (t)
|
||||
{
|
||||
printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Topic=%s unmatch\n", tp0);
|
||||
}
|
||||
*/
|
||||
assert(t != 0);
|
||||
}
|
||||
for ( int i = 0; i < 10 ; i++ )
|
||||
{
|
||||
MQTTSN_topicid tp1;
|
||||
char tp0[10];
|
||||
strcpy(tp0, "Topic/");
|
||||
tp0[6] = 0x30 + i;
|
||||
tp0[7] = 0;
|
||||
tp1.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
tp1.data.long_.len = strlen(tp0);
|
||||
tp1.data.long_.name = tp0;
|
||||
|
||||
Topic* t = _topics->match(&tp1);
|
||||
/*
|
||||
if (t)
|
||||
{
|
||||
printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Topic=%s unmatch\n", tp0);
|
||||
}
|
||||
*/
|
||||
assert(t != 0);
|
||||
}
|
||||
|
||||
for ( int i = 0; i < 10 ; i++ )
|
||||
{
|
||||
MQTTSN_topicid tpid1;
|
||||
char tp0[10];
|
||||
strcpy(tp0, "TOPIC/");
|
||||
tp0[6] = 0x30 + i;
|
||||
tp0[7] = '/';
|
||||
tp0[8] = 0x30 + i;
|
||||
tp0[9] = 0;
|
||||
tpid1.type = MQTTSN_TOPIC_TYPE_NORMAL;
|
||||
tpid1.data.long_.len = strlen(tp0);
|
||||
tpid1.data.long_.name = tp0;
|
||||
|
||||
Topic* t = _topics->match(&tpid1);
|
||||
/*
|
||||
if (t)
|
||||
{
|
||||
printf("Topic=%s match to %s\n", tp0, t->getTopicName()->c_str());
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Topic=%s unmatch\n", tp0);
|
||||
}
|
||||
*/
|
||||
assert( t != 0);
|
||||
}
|
||||
printf("Topics Test complete.\n\n");
|
||||
}
|
||||
32
MQTTSNGateway/src/tests/TestTopics.h
Normal file
32
MQTTSNGateway/src/tests/TestTopics.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/**************************************************************************************
|
||||
* Copyright (c) 2016, Tomoaki Yamaguchi
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#ifndef MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_
|
||||
#define MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_
|
||||
|
||||
#include "MQTTSNGWClient.h"
|
||||
|
||||
class TestTopics
|
||||
{
|
||||
public:
|
||||
TestTopics();
|
||||
~TestTopics();
|
||||
void test(void);
|
||||
|
||||
private:
|
||||
Topics* _topics;
|
||||
};
|
||||
|
||||
#endif /* MQTTSNGATEWAY_SRC_TESTS_TESTTOPICS_H_ */
|
||||
@@ -13,19 +13,19 @@
|
||||
* Contributors:
|
||||
* Tomoaki Yamaguchi - initial API and implementation
|
||||
**************************************************************************************/
|
||||
#include "TestProcessFramework.h"
|
||||
#include "TestProcess.h"
|
||||
#include "TestTask.h"
|
||||
|
||||
using namespace MQTTSNGW;
|
||||
|
||||
TestProcessFramework* proc = new TestProcessFramework();
|
||||
TestTask* task = new TestTask(proc);
|
||||
TestProcess* test = new TestProcess();
|
||||
TestTask* task = new TestTask(test);
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
proc->initialize(argc, argv);
|
||||
proc->run();
|
||||
delete proc;
|
||||
test->initialize(argc, argv);
|
||||
test->run();
|
||||
delete test;
|
||||
return 0;
|
||||
}
|
||||
|
||||
7
Makefile
7
Makefile
@@ -5,7 +5,7 @@ LPROGNAME := MQTT-SNLogmonitor
|
||||
LAPPL := mainLogmonitor
|
||||
|
||||
TESTPROGNAME := testPFW
|
||||
TESTAPPL := mainTestProcessFramework
|
||||
TESTAPPL := mainTestProcess
|
||||
|
||||
CONFIG := MQTTSNGateway/gateway.conf
|
||||
CLIENTS := MQTTSNGateway/clients.conf
|
||||
@@ -39,7 +39,10 @@ $(SRCDIR)/$(OS)/$(SENSORNET)/SensorNetwork.cpp \
|
||||
$(SRCDIR)/$(OS)/Timer.cpp \
|
||||
$(SRCDIR)/$(OS)/Network.cpp \
|
||||
$(SRCDIR)/$(OS)/Threading.cpp \
|
||||
$(SRCDIR)/$(TEST)/TestProcessFramework.cpp \
|
||||
$(SRCDIR)/$(TEST)/TestProcess.cpp \
|
||||
$(SRCDIR)/$(TEST)/TestQue.cpp \
|
||||
$(SRCDIR)/$(TEST)/TestTopics.cpp \
|
||||
$(SRCDIR)/$(TEST)/TestTopicIdMap.cpp \
|
||||
$(SRCDIR)/$(TEST)/TestTask.cpp
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user