mirror of
https://github.com/eclipse/paho.mqtt-sn.embedded-c.git
synced 2025-12-15 16:36:52 +01:00
Add errno to a Exception property
Signed-off-by: tomoaki <tomoaki@tomy-tech.com>
This commit is contained in:
@@ -45,10 +45,7 @@ ClientRecvTask::~ClientRecvTask()
|
|||||||
*/
|
*/
|
||||||
void ClientRecvTask::initialize(int argc, char** argv)
|
void ClientRecvTask::initialize(int argc, char** argv)
|
||||||
{
|
{
|
||||||
if (_sensorNetwork->initialize() < 0)
|
_sensorNetwork->initialize();
|
||||||
{
|
|
||||||
throw EXCEPTION(" Can't open the sensor network.\n", 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -336,19 +336,19 @@ int MultiTaskProcess::getParam(const char* parameter, char* value)
|
|||||||
/*=====================================
|
/*=====================================
|
||||||
Class Exception
|
Class Exception
|
||||||
======================================*/
|
======================================*/
|
||||||
Exception::Exception(const char* message, const int exNo)
|
Exception::Exception(const char* message, const int errNo)
|
||||||
{
|
{
|
||||||
_message = message;
|
_message = message;
|
||||||
_exNo = exNo;
|
_errNo = errNo;
|
||||||
_fileName = nullptr;
|
_fileName = nullptr;
|
||||||
_functionName = nullptr;
|
_functionName = nullptr;
|
||||||
_line = 0;
|
_line = 0;
|
||||||
}
|
}
|
||||||
Exception::Exception(const char* message, const int exNo, const char* file,
|
Exception::Exception(const char* message, const int errNo, const char* file,
|
||||||
const char* function, const int line)
|
const char* function, const int line)
|
||||||
{
|
{
|
||||||
_message = message;
|
_message = message;
|
||||||
_exNo = exNo;
|
_errNo = errNo;
|
||||||
_fileName = getFileName(file);;
|
_fileName = getFileName(file);;
|
||||||
_functionName = function;
|
_functionName = function;
|
||||||
_line = line;
|
_line = line;
|
||||||
@@ -379,36 +379,35 @@ const int Exception::getLineNo()
|
|||||||
return _line;
|
return _line;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int Exception::getExceptionNo()
|
const int Exception::getErrNo()
|
||||||
{
|
{
|
||||||
return _exNo;
|
return _errNo;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Exception::writeMessage()
|
void Exception::writeMessage()
|
||||||
{
|
{
|
||||||
if (_fileName == nullptr)
|
if (_fileName == nullptr)
|
||||||
{
|
{
|
||||||
if (_exNo == 0)
|
if (_errNo == 0)
|
||||||
{
|
{
|
||||||
WRITELOG("%s %s\n", currentDateTime(), _message);
|
WRITELOG("%s%s %s%s\n", currentDateTime(), RED_HDR, _message, CLR_HDR);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
WRITELOG("%s%s %s.\n errno=%d : %s%s\n", currentDateTime(), RED_HDR,_message, _errNo, strerror(_errNo), CLR_HDR);
|
||||||
WRITELOG("%s %s ExNo: %d\n", currentDateTime(), _message, _exNo);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (_exNo == 0)
|
if (_errNo == 0)
|
||||||
{
|
{
|
||||||
WRITELOG("%s %s line %-4d %s() : %s\n",
|
WRITELOG("%s%s %s. %s line %-4d %s()%s\n",
|
||||||
currentDateTime(), _fileName, _line, _functionName, _message);
|
currentDateTime(), RED_HDR, _message, _fileName, _line, _functionName, CLR_HDR);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
WRITELOG("%s %s line %-4d %s() : %s ExNo: %d\n",
|
WRITELOG("%s%s %s. %s line %-4d %s()\n errno=%d : %s%s\n",
|
||||||
currentDateTime(), _fileName, _line, _functionName, _message, _exNo);
|
currentDateTime(), RED_HDR, _message, _fileName, _line, _functionName, _errNo, strerror(_errNo), CLR_HDR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,19 +103,19 @@ private:
|
|||||||
class Exception: public exception
|
class Exception: public exception
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Exception(const char* message, const int exNo);
|
Exception(const char* message, const int errNo);
|
||||||
Exception(const char* message, const int exNo, const char* file, const char* func, int line);
|
Exception(const char* message, const int errNo, const char* file, const char* func, int line);
|
||||||
virtual ~Exception() throw ();
|
virtual ~Exception() throw ();
|
||||||
const char* getFileName();
|
const char* getFileName();
|
||||||
const char* getFunctionName();
|
const char* getFunctionName();
|
||||||
const int getLineNo();
|
const int getLineNo();
|
||||||
const int getExceptionNo();
|
const int getErrNo();
|
||||||
virtual const char* what() const throw ();
|
virtual const char* what() const throw ();
|
||||||
void writeMessage();
|
void writeMessage();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* getFileName(const char* file);
|
const char* getFileName(const char* file);
|
||||||
int _exNo;
|
int _errNo;
|
||||||
const char* _message;
|
const char* _message;
|
||||||
const char* _fileName;
|
const char* _fileName;
|
||||||
const char* _functionName;
|
const char* _functionName;
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ namespace MQTTSNGW
|
|||||||
#define MQTTSNGW_RB_MUTEX_KEY "rbmutex.key"
|
#define MQTTSNGW_RB_MUTEX_KEY "rbmutex.key"
|
||||||
#define MQTTSNGW_RB_SEMAPHOR_NAME "/rbsemaphor"
|
#define MQTTSNGW_RB_SEMAPHOR_NAME "/rbsemaphor"
|
||||||
|
|
||||||
|
#define RED_HDR "\033[0m\033[0;31m"
|
||||||
|
#define CLR_HDR "\033[0m\033[0;37m"
|
||||||
|
|
||||||
/*=====================================
|
/*=====================================
|
||||||
Class Mutex
|
Class Mutex
|
||||||
====================================*/
|
====================================*/
|
||||||
@@ -129,9 +132,8 @@ public: void EXECRUN() \
|
|||||||
} \
|
} \
|
||||||
catch ( Exception &ex ) \
|
catch ( Exception &ex ) \
|
||||||
{ \
|
{ \
|
||||||
WRITELOG("\033[0m\033[0;31m"); \
|
|
||||||
ex.writeMessage(); \
|
ex.writeMessage(); \
|
||||||
WRITELOG("\033[0m\033[0;37m%s caught an exception and stopped.\n", getTaskName()); \
|
WRITELOG("%s%s caught an exception and stopped.%s\n", RED_HDR, getTaskName(), CLR_HDR); \
|
||||||
theMultiTaskProcess->abort(); \
|
theMultiTaskProcess->abort(); \
|
||||||
} \
|
} \
|
||||||
theMultiTaskProcess->threadStopped(); \
|
theMultiTaskProcess->threadStopped(); \
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
|
|||||||
return LoRaLink::recv(buf, bufLen, &_clientAddr);
|
return LoRaLink::recv(buf, bufLen, &_clientAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SensorNetwork::initialize(void)
|
void SensorNetwork::initialize(void)
|
||||||
{
|
{
|
||||||
char param[MQTTSNGW_PARAM_MAX];
|
char param[MQTTSNGW_PARAM_MAX];
|
||||||
uint32_t baudrate = 115200;
|
uint32_t baudrate = 115200;
|
||||||
@@ -135,15 +135,22 @@ int SensorNetwork::initialize(void)
|
|||||||
theProcess->getParam("DeviceRxLoRaLink", param);
|
theProcess->getParam("DeviceRxLoRaLink", param);
|
||||||
_description += ", SerialRx ";
|
_description += ", SerialRx ";
|
||||||
_description += param;
|
_description += param;
|
||||||
|
errno = 0;
|
||||||
|
|
||||||
if ( LoRaLink::open(LORALINK_MODEM_RX, param, baudrate) < 0 )
|
if ( LoRaLink::open(LORALINK_MODEM_RX, param, baudrate) < 0 )
|
||||||
{
|
{
|
||||||
return -1;
|
throw EXCEPTION("Can't open a LoRaLink", errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
theProcess->getParam("DeviceTxLoRaLink", param);
|
theProcess->getParam("DeviceTxLoRaLink", param);
|
||||||
_description += ", SerialTx ";
|
_description += ", SerialTx ";
|
||||||
_description += param;
|
_description += param;
|
||||||
return LoRaLink::open(LORALINK_MODEM_TX, param, baudrate);
|
errno = 0;
|
||||||
|
|
||||||
|
if ( LoRaLink::open(LORALINK_MODEM_TX, param, baudrate) < 0 )
|
||||||
|
{
|
||||||
|
throw EXCEPTION("Can't open a LoRaLink", errno);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* SensorNetwork::getDescription(void)
|
const char* SensorNetwork::getDescription(void)
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ public:
|
|||||||
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
||||||
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
||||||
int read(uint8_t* buf, uint16_t bufLen);
|
int read(uint8_t* buf, uint16_t bufLen);
|
||||||
int initialize(void);
|
void initialize(void);
|
||||||
const char* getDescription(void);
|
const char* getDescription(void);
|
||||||
SensorNetAddress* getSenderAddress(void);
|
SensorNetAddress* getSenderAddress(void);
|
||||||
|
|
||||||
|
|||||||
@@ -177,9 +177,8 @@ int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
|
|||||||
* Prepare UDP sockets and description of SensorNetwork like
|
* Prepare UDP sockets and description of SensorNetwork like
|
||||||
* "UDP Multicast 225.1.1.1:1883 Gateway Port 10000".
|
* "UDP Multicast 225.1.1.1:1883 Gateway Port 10000".
|
||||||
* The description is for a start up prompt.
|
* The description is for a start up prompt.
|
||||||
* @return success = 0, error = -1
|
|
||||||
*/
|
*/
|
||||||
int SensorNetwork::initialize(void)
|
void SensorNetwork::initialize(void)
|
||||||
{
|
{
|
||||||
char param[MQTTSNGW_PARAM_MAX];
|
char param[MQTTSNGW_PARAM_MAX];
|
||||||
uint16_t multicastPortNo = 0;
|
uint16_t multicastPortNo = 0;
|
||||||
@@ -224,7 +223,11 @@ int SensorNetwork::initialize(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Prepare UDP sockets */
|
/* Prepare UDP sockets */
|
||||||
return UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo, ttl);
|
errno = 0;
|
||||||
|
if ( UDPPort::open(ip.c_str(), multicastPortNo, unicastPortNo, ttl) < 0 )
|
||||||
|
{
|
||||||
|
throw EXCEPTION("Can't open a UDP", errno);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* SensorNetwork::getDescription(void)
|
const char* SensorNetwork::getDescription(void)
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ public:
|
|||||||
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
||||||
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
||||||
int read(uint8_t* buf, uint16_t bufLen);
|
int read(uint8_t* buf, uint16_t bufLen);
|
||||||
int initialize(void);
|
void initialize(void);
|
||||||
const char* getDescription(void);
|
const char* getDescription(void);
|
||||||
SensorNetAddress* getSenderAddress(void);
|
SensorNetAddress* getSenderAddress(void);
|
||||||
|
|
||||||
|
|||||||
@@ -169,7 +169,7 @@ int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
|
|||||||
return UDPPort6::recv(buf, bufLen, &_clientAddr);
|
return UDPPort6::recv(buf, bufLen, &_clientAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SensorNetwork::initialize(void)
|
void SensorNetwork::initialize(void)
|
||||||
{
|
{
|
||||||
char param[MQTTSNGW_PARAM_MAX];
|
char param[MQTTSNGW_PARAM_MAX];
|
||||||
uint16_t unicastPortNo = 0;
|
uint16_t unicastPortNo = 0;
|
||||||
@@ -209,7 +209,12 @@ int SensorNetwork::initialize(void)
|
|||||||
_description += param;
|
_description += param;
|
||||||
}
|
}
|
||||||
|
|
||||||
return UDPPort6::open(ip.c_str(), unicastPortNo, broadcast.c_str(), interface.c_str(), hops);
|
errno = 0;
|
||||||
|
|
||||||
|
if ( UDPPort6::open(ip.c_str(), unicastPortNo, broadcast.c_str(), interface.c_str(), hops) < 0 )
|
||||||
|
{
|
||||||
|
throw EXCEPTION("Can't open a UDP6", errno);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* SensorNetwork::getDescription(void)
|
const char* SensorNetwork::getDescription(void)
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ public:
|
|||||||
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
||||||
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
||||||
int read(uint8_t* buf, uint16_t bufLen);
|
int read(uint8_t* buf, uint16_t bufLen);
|
||||||
int initialize(void);
|
void initialize(void);
|
||||||
const char* getDescription(void);
|
const char* getDescription(void);
|
||||||
SensorNetAddress* getSenderAddress(void);
|
SensorNetAddress* getSenderAddress(void);
|
||||||
|
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ int SensorNetwork::read(uint8_t* buf, uint16_t bufLen)
|
|||||||
return XBee::recv(buf, bufLen, &_clientAddr);
|
return XBee::recv(buf, bufLen, &_clientAddr);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SensorNetwork::initialize(void)
|
void SensorNetwork::initialize(void)
|
||||||
{
|
{
|
||||||
char param[MQTTSNGW_PARAM_MAX];
|
char param[MQTTSNGW_PARAM_MAX];
|
||||||
uint32_t baudrate = 9600;
|
uint32_t baudrate = 9600;
|
||||||
@@ -145,7 +145,12 @@ int SensorNetwork::initialize(void)
|
|||||||
_description += ", SerialDevice ";
|
_description += ", SerialDevice ";
|
||||||
_description += param;
|
_description += param;
|
||||||
|
|
||||||
return XBee::open(param, baudrate);
|
errno =0;
|
||||||
|
|
||||||
|
if ( XBee::open(param, baudrate) < 0 )
|
||||||
|
{
|
||||||
|
throw EXCEPTION("Can't open a XBee", errno);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* SensorNetwork::getDescription(void)
|
const char* SensorNetwork::getDescription(void)
|
||||||
|
|||||||
@@ -126,7 +126,7 @@ public:
|
|||||||
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
int unicast(const uint8_t* payload, uint16_t payloadLength, SensorNetAddress* sendto);
|
||||||
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
int broadcast(const uint8_t* payload, uint16_t payloadLength);
|
||||||
int read(uint8_t* buf, uint16_t bufLen);
|
int read(uint8_t* buf, uint16_t bufLen);
|
||||||
int initialize(void);
|
void initialize(void);
|
||||||
const char* getDescription(void);
|
const char* getDescription(void);
|
||||||
SensorNetAddress* getSenderAddress(void);
|
SensorNetAddress* getSenderAddress(void);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user