mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-13 15:36:51 +01:00
BugFix: Termination by ctl + C
Update: API mode is configurable Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -20,8 +20,9 @@ BrokerSecurePortNo=8883
|
||||
ClientAuthentication=NO
|
||||
#ClientsList=/path/to/your_clients.conf
|
||||
|
||||
RootCAfile=/usr/share/ca-certificates/ISRG_Root_X1.crt
|
||||
RootCApath=/etc/ssl/certs/
|
||||
RootCAfile=/etc/ssl/certs/ca-certificates.crt
|
||||
#RootCApath=/etc/ssl/certs/
|
||||
#CertsDirectory=/usr/share/GW/IoTcerts/
|
||||
|
||||
GatewayID=1
|
||||
GatewayName=PahoGateway-01
|
||||
@@ -37,3 +38,5 @@ MulticastPortNo=1883
|
||||
# XBee
|
||||
Baudrate=38400
|
||||
SerialDevice=/dev/ttyUSB0
|
||||
ApiMode=2
|
||||
|
||||
|
||||
@@ -91,12 +91,12 @@ void Process::initialize(int argc, char** argv)
|
||||
size_t pos = 0;
|
||||
if ( (pos = config.find_last_of("/")) == string::npos )
|
||||
{
|
||||
_configFile = config;
|
||||
_configFile = optarg;
|
||||
}
|
||||
else
|
||||
{
|
||||
_configFile = config.substr(pos + 1, config.size() - 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;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -232,13 +232,7 @@ MultiTaskProcess::MultiTaskProcess()
|
||||
|
||||
MultiTaskProcess::~MultiTaskProcess()
|
||||
{
|
||||
for (int i = 0; i < _threadCount; i++)
|
||||
{
|
||||
if ( _threadList[i] )
|
||||
{
|
||||
delete _threadList[i];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void MultiTaskProcess::initialize(int argc, char** argv)
|
||||
@@ -264,6 +258,13 @@ void MultiTaskProcess::run(void)
|
||||
}
|
||||
catch (Exception* ex)
|
||||
{
|
||||
for (int i = 0; i < _threadCount; i++)
|
||||
{
|
||||
if ( _threadList[i] )
|
||||
{
|
||||
_threadList[i]->cancel();;
|
||||
}
|
||||
}
|
||||
ex->writeMessage();
|
||||
}
|
||||
}
|
||||
@@ -363,7 +364,7 @@ void Exception::writeMessage()
|
||||
{
|
||||
if (getExceptionNo() == 0 )
|
||||
{
|
||||
WRITELOG("%s : %s\n", currentDateTime(), what());
|
||||
WRITELOG("%s %s\n", currentDateTime(), what());
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -71,8 +71,8 @@ public:
|
||||
private:
|
||||
int _argc;
|
||||
char** _argv;
|
||||
string _configDir;
|
||||
string _configFile;
|
||||
string _configDir;
|
||||
string _configFile;
|
||||
RingBuffer* _rb;
|
||||
Semaphore* _rbsem;
|
||||
Mutex _mt;
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace MQTTSNGW
|
||||
/*=================================
|
||||
* 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_COPYRIGHT1 " * Part of Project Paho in Eclipse"
|
||||
|
||||
@@ -511,7 +511,7 @@ void Thread::stopProcess(void)
|
||||
_stopProcessEvent->post();
|
||||
}
|
||||
|
||||
void Thread::testThreadCancel(void)
|
||||
void Thread::cancel(void)
|
||||
{
|
||||
pthread_testcancel();
|
||||
pthread_cancel(_threadID);
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ public:
|
||||
static bool equals(pthread_t*, pthread_t*);
|
||||
virtual void initialize(int argc, char** argv);
|
||||
void stopProcess(void);
|
||||
void testThreadCancel(void);
|
||||
void cancel(void);
|
||||
private:
|
||||
pthread_t _threadID;
|
||||
Semaphore* _stopProcessEvent;
|
||||
|
||||
@@ -153,6 +153,11 @@ const char* SensorNetwork::getDescription(void)
|
||||
return _description.c_str();
|
||||
}
|
||||
|
||||
SensorNetAddress* SensorNetwork::getSenderAddress(void)
|
||||
{
|
||||
return &_clientAddr;
|
||||
}
|
||||
|
||||
/*=========================================
|
||||
Class udpStack
|
||||
=========================================*/
|
||||
|
||||
@@ -99,11 +99,7 @@ public:
|
||||
int read(uint8_t* buf, uint16_t bufLen);
|
||||
int initialize(void);
|
||||
const char* getDescription(void);
|
||||
SensorNetAddress* getSenderAddress(void)
|
||||
{
|
||||
return &_clientAddr;
|
||||
}
|
||||
|
||||
SensorNetAddress* getSenderAddress(void);
|
||||
|
||||
private:
|
||||
SensorNetAddress _clientAddr; // Sender's address. not gateway's one.
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
|
||||
#include "SensorNetwork.h"
|
||||
#include "MQTTSNGWProcess.h"
|
||||
#include "Threading.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace MQTTSNGW;
|
||||
@@ -111,17 +110,27 @@ int SensorNetwork::initialize(void)
|
||||
{
|
||||
char param[MQTTSNGW_PARAM_MAX];
|
||||
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);
|
||||
}
|
||||
_description = "Baudrate ";
|
||||
_description += ", Baudrate ";
|
||||
sprintf(param ,"%d", baudrate);
|
||||
_description += param;
|
||||
|
||||
theProcess->getParam("SerialDevice", param);
|
||||
_description = "SerialDevice ";
|
||||
_description += ", SerialDevice ";
|
||||
_description += param;
|
||||
|
||||
return XBee::open(param, baudrate);
|
||||
@@ -132,6 +141,11 @@ const char* SensorNetwork::getDescription(void)
|
||||
return _description.c_str();
|
||||
}
|
||||
|
||||
SensorNetAddress* SensorNetwork::getSenderAddress(void)
|
||||
{
|
||||
return &_clientAddr;
|
||||
}
|
||||
|
||||
/*===========================================
|
||||
Class XBee
|
||||
============================================*/
|
||||
@@ -140,6 +154,7 @@ XBee::XBee(){
|
||||
_respCd = 0;
|
||||
_dataLen = 0;
|
||||
_frameId = 0;
|
||||
_apiMode = 2;
|
||||
}
|
||||
|
||||
XBee::~XBee(){
|
||||
@@ -340,7 +355,7 @@ int XBee::send(const uint8_t* payload, uint8_t pLen, SensorNetAddress* addr){
|
||||
|
||||
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(c ^ 0x20);
|
||||
}else{
|
||||
@@ -352,7 +367,7 @@ int XBee::recv(uint8_t* buf)
|
||||
{
|
||||
if (_serialPort->recv(buf) )
|
||||
{
|
||||
if ( *buf == ESCAPE)
|
||||
if ( *buf == ESCAPE && _apiMode == 2 )
|
||||
{
|
||||
_serialPort->recv(buf);
|
||||
*buf = 0x20 ^ *buf;
|
||||
@@ -362,6 +377,11 @@ int XBee::recv(uint8_t* buf)
|
||||
return -1;
|
||||
}
|
||||
|
||||
void XBee::setApiMode(uint8_t mode)
|
||||
{
|
||||
_apiMode = mode;
|
||||
}
|
||||
|
||||
/*=========================================
|
||||
Class SerialPort
|
||||
=========================================*/
|
||||
|
||||
@@ -94,6 +94,7 @@ public:
|
||||
int unicast(const uint8_t* buf, uint16_t length, SensorNetAddress* sendToAddr);
|
||||
int broadcast(const uint8_t* buf, uint16_t length);
|
||||
int recv(uint8_t* buf, uint16_t len, SensorNetAddress* addr);
|
||||
void setApiMode(uint8_t mode);
|
||||
|
||||
private:
|
||||
int readApiFrame(uint8_t* recvData);
|
||||
@@ -105,10 +106,10 @@ private:
|
||||
Mutex _meutex;
|
||||
SerialPort* _serialPort;
|
||||
uint8_t _frameId;
|
||||
|
||||
uint8_t _respCd;
|
||||
uint8_t _respId;
|
||||
uint8_t _dataLen;
|
||||
uint8_t _apiMode;
|
||||
};
|
||||
|
||||
/*===========================================
|
||||
@@ -125,11 +126,7 @@ public:
|
||||
int read(uint8_t* buf, uint16_t bufLen);
|
||||
int initialize(void);
|
||||
const char* getDescription(void);
|
||||
SensorNetAddress* getSenderAddress(void)
|
||||
{
|
||||
return &_clientAddr;
|
||||
}
|
||||
|
||||
SensorNetAddress* getSenderAddress(void);
|
||||
|
||||
private:
|
||||
SensorNetAddress _clientAddr; // Sender's address. not gateway's one.
|
||||
|
||||
Reference in New Issue
Block a user