BugFix: Termination by ctl + C

Update: API mode is configurable

Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
tomoaki
2016-09-25 17:06:05 +09:00
parent 2537dd76dd
commit e3dd9fa01a
10 changed files with 60 additions and 38 deletions

View File

@@ -20,8 +20,9 @@ BrokerSecurePortNo=8883
ClientAuthentication=NO ClientAuthentication=NO
#ClientsList=/path/to/your_clients.conf #ClientsList=/path/to/your_clients.conf
RootCAfile=/usr/share/ca-certificates/ISRG_Root_X1.crt RootCAfile=/etc/ssl/certs/ca-certificates.crt
RootCApath=/etc/ssl/certs/ #RootCApath=/etc/ssl/certs/
#CertsDirectory=/usr/share/GW/IoTcerts/
GatewayID=1 GatewayID=1
GatewayName=PahoGateway-01 GatewayName=PahoGateway-01
@@ -37,3 +38,5 @@ MulticastPortNo=1883
# XBee # XBee
Baudrate=38400 Baudrate=38400
SerialDevice=/dev/ttyUSB0 SerialDevice=/dev/ttyUSB0
ApiMode=2

View File

@@ -91,12 +91,12 @@ void Process::initialize(int argc, char** argv)
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 = config; _configFile = optarg;
} }
else else
{ {
_configFile = config.substr(pos + 1, config.size() - pos - 1);;
_configDir = config.substr(0, pos + 1); _configDir = config.substr(0, pos + 1);
_configFile = config.substr(pos + 1, config.size() - pos - 1);
} }
} }
} }
@@ -121,11 +121,11 @@ int Process::getParam(const char* parameter, char* value)
FILE *fp; FILE *fp;
int i = 0, j = 0; int i = 0, j = 0;
string config = _configDir + _configFile; string configPath = _configDir + _configFile;
if ((fp = fopen(config.c_str(), "r")) == NULL) if ((fp = fopen(configPath.c_str(), "r")) == NULL)
{ {
WRITELOG("No config file:[%s]\n", config.c_str()); WRITELOG("No config file:[%s]\n", configPath.c_str());
return -1; return -1;
} }
@@ -232,13 +232,7 @@ MultiTaskProcess::MultiTaskProcess()
MultiTaskProcess::~MultiTaskProcess() MultiTaskProcess::~MultiTaskProcess()
{ {
for (int i = 0; i < _threadCount; i++)
{
if ( _threadList[i] )
{
delete _threadList[i];
}
}
} }
void MultiTaskProcess::initialize(int argc, char** argv) void MultiTaskProcess::initialize(int argc, char** argv)
@@ -264,6 +258,13 @@ void MultiTaskProcess::run(void)
} }
catch (Exception* ex) catch (Exception* ex)
{ {
for (int i = 0; i < _threadCount; i++)
{
if ( _threadList[i] )
{
_threadList[i]->cancel();;
}
}
ex->writeMessage(); ex->writeMessage();
} }
} }
@@ -363,7 +364,7 @@ void Exception::writeMessage()
{ {
if (getExceptionNo() == 0 ) if (getExceptionNo() == 0 )
{ {
WRITELOG("%s : %s\n", currentDateTime(), what()); WRITELOG("%s %s\n", currentDateTime(), what());
} }
else else
{ {

View File

@@ -71,8 +71,8 @@ public:
private: private:
int _argc; int _argc;
char** _argv; char** _argv;
string _configDir; string _configDir;
string _configFile; string _configFile;
RingBuffer* _rb; RingBuffer* _rb;
Semaphore* _rbsem; Semaphore* _rbsem;
Mutex _mt; Mutex _mt;

View File

@@ -31,7 +31,7 @@ namespace MQTTSNGW
/*================================= /*=================================
* Starting prompt * Starting prompt
==================================*/ ==================================*/
#define GATEWAY_VERSION " * Version: 0.4.0" #define GATEWAY_VERSION " * Version: 0.5.0"
#define PAHO_COPYRIGHT0 " * MQTT-SN Transparent Gateway" #define PAHO_COPYRIGHT0 " * MQTT-SN Transparent Gateway"
#define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse" #define PAHO_COPYRIGHT1 " * Part of Project Paho in Eclipse"

View File

@@ -511,7 +511,7 @@ void Thread::stopProcess(void)
_stopProcessEvent->post(); _stopProcessEvent->post();
} }
void Thread::testThreadCancel(void) void Thread::cancel(void)
{ {
pthread_testcancel(); pthread_cancel(_threadID);
} }

View File

@@ -131,7 +131,7 @@ public:
static bool equals(pthread_t*, pthread_t*); static bool equals(pthread_t*, pthread_t*);
virtual void initialize(int argc, char** argv); virtual void initialize(int argc, char** argv);
void stopProcess(void); void stopProcess(void);
void testThreadCancel(void); void cancel(void);
private: private:
pthread_t _threadID; pthread_t _threadID;
Semaphore* _stopProcessEvent; Semaphore* _stopProcessEvent;

View File

@@ -153,6 +153,11 @@ const char* SensorNetwork::getDescription(void)
return _description.c_str(); return _description.c_str();
} }
SensorNetAddress* SensorNetwork::getSenderAddress(void)
{
return &_clientAddr;
}
/*========================================= /*=========================================
Class udpStack Class udpStack
=========================================*/ =========================================*/

View File

@@ -99,11 +99,7 @@ public:
int read(uint8_t* buf, uint16_t bufLen); int read(uint8_t* buf, uint16_t bufLen);
int initialize(void); int initialize(void);
const char* getDescription(void); const char* getDescription(void);
SensorNetAddress* getSenderAddress(void) SensorNetAddress* getSenderAddress(void);
{
return &_clientAddr;
}
private: private:
SensorNetAddress _clientAddr; // Sender's address. not gateway's one. SensorNetAddress _clientAddr; // Sender's address. not gateway's one.

View File

@@ -24,7 +24,6 @@
#include "SensorNetwork.h" #include "SensorNetwork.h"
#include "MQTTSNGWProcess.h" #include "MQTTSNGWProcess.h"
#include "Threading.h"
using namespace std; using namespace std;
using namespace MQTTSNGW; using namespace MQTTSNGW;
@@ -111,17 +110,27 @@ int SensorNetwork::initialize(void)
{ {
char param[MQTTSNGW_PARAM_MAX]; char param[MQTTSNGW_PARAM_MAX];
uint16_t baudrate = 9600; uint16_t baudrate = 9600;
uint8_t apimode = 2;
if (theProcess->getParam("XBee Baudrate", param) == 0) if (theProcess->getParam("ApiMode", param) == 0)
{
apimode = (uint8_t)atoi(param);
}
setApiMode(apimode);
_description = "API mode ";
sprintf(param, "%d", apimode);
_description += param;
if (theProcess->getParam("Baudrate", param) == 0)
{ {
baudrate = (uint16_t)atoi(param); baudrate = (uint16_t)atoi(param);
} }
_description = "Baudrate "; _description += ", Baudrate ";
sprintf(param ,"%d", baudrate); sprintf(param ,"%d", baudrate);
_description += param; _description += param;
theProcess->getParam("SerialDevice", param); theProcess->getParam("SerialDevice", param);
_description = "SerialDevice "; _description += ", SerialDevice ";
_description += param; _description += param;
return XBee::open(param, baudrate); return XBee::open(param, baudrate);
@@ -132,6 +141,11 @@ const char* SensorNetwork::getDescription(void)
return _description.c_str(); return _description.c_str();
} }
SensorNetAddress* SensorNetwork::getSenderAddress(void)
{
return &_clientAddr;
}
/*=========================================== /*===========================================
Class XBee Class XBee
============================================*/ ============================================*/
@@ -140,6 +154,7 @@ XBee::XBee(){
_respCd = 0; _respCd = 0;
_dataLen = 0; _dataLen = 0;
_frameId = 0; _frameId = 0;
_apiMode = 2;
} }
XBee::~XBee(){ XBee::~XBee(){
@@ -340,7 +355,7 @@ int XBee::send(const uint8_t* payload, uint8_t pLen, SensorNetAddress* addr){
void XBee::send(uint8_t c) void XBee::send(uint8_t c)
{ {
if(c == START_BYTE || c == ESCAPE || c == XON || c == XOFF){ if(_apiMode == 2 && (c == START_BYTE || c == ESCAPE || c == XON || c == XOFF)){
_serialPort->send(ESCAPE); _serialPort->send(ESCAPE);
_serialPort->send(c ^ 0x20); _serialPort->send(c ^ 0x20);
}else{ }else{
@@ -352,7 +367,7 @@ int XBee::recv(uint8_t* buf)
{ {
if (_serialPort->recv(buf) ) if (_serialPort->recv(buf) )
{ {
if ( *buf == ESCAPE) if ( *buf == ESCAPE && _apiMode == 2 )
{ {
_serialPort->recv(buf); _serialPort->recv(buf);
*buf = 0x20 ^ *buf; *buf = 0x20 ^ *buf;
@@ -362,6 +377,11 @@ int XBee::recv(uint8_t* buf)
return -1; return -1;
} }
void XBee::setApiMode(uint8_t mode)
{
_apiMode = mode;
}
/*========================================= /*=========================================
Class SerialPort Class SerialPort
=========================================*/ =========================================*/

View File

@@ -94,6 +94,7 @@ public:
int unicast(const uint8_t* buf, uint16_t length, SensorNetAddress* sendToAddr); int unicast(const uint8_t* buf, uint16_t length, SensorNetAddress* sendToAddr);
int broadcast(const uint8_t* buf, uint16_t length); int broadcast(const uint8_t* buf, uint16_t length);
int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr); int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr);
void setApiMode(uint8_t mode);
private: private:
int readApiFrame(uint8_t* recvData); int readApiFrame(uint8_t* recvData);
@@ -105,10 +106,10 @@ private:
Mutex _meutex; Mutex _meutex;
SerialPort* _serialPort; SerialPort* _serialPort;
uint8_t _frameId; uint8_t _frameId;
uint8_t _respCd; uint8_t _respCd;
uint8_t _respId; uint8_t _respId;
uint8_t _dataLen; uint8_t _dataLen;
uint8_t _apiMode;
}; };
/*=========================================== /*===========================================
@@ -125,11 +126,7 @@ public:
int read(uint8_t* buf, uint16_t bufLen); int read(uint8_t* buf, uint16_t bufLen);
int initialize(void); int initialize(void);
const char* getDescription(void); const char* getDescription(void);
SensorNetAddress* getSenderAddress(void) SensorNetAddress* getSenderAddress(void);
{
return &_clientAddr;
}
private: private:
SensorNetAddress _clientAddr; // Sender's address. not gateway's one. SensorNetAddress _clientAddr; // Sender's address. not gateway's one.