Implemented error codes for the methods devTurnOn(), devTurnOff(), devBell() and devDim().

A new function now exists, devGetErrorString(), which returns a human readable string for all of the error codes.
This commit is contained in:
Micke Prag 2008-09-12 11:08:19 +00:00
parent 2be36e0fe1
commit 3602f43359
14 changed files with 155 additions and 85 deletions

View file

@ -17,28 +17,28 @@ Device::~Device(void)
/* /*
* Turn on, virtual * Turn on, virtual
*/ */
void Device::turnOn(void){ int Device::turnOn(void){
//do nothing //do nothing
} }
/* /*
* Turn off, virtual * Turn off, virtual
*/ */
void Device::turnOff(void){ int Device::turnOff(void){
//do nothing //do nothing
} }
/* /*
* Bell, virtual * Bell, virtual
*/ */
void Device::bell(void){ int Device::bell(void){
//do nothing //do nothing
} }
/* /*
* Dim, virtual * Dim, virtual
*/ */
void Device::dim(unsigned char level){ int Device::dim(unsigned char level){
//do nothing //do nothing
} }

View file

@ -8,10 +8,10 @@ public:
Device(); Device();
~Device(void); ~Device(void);
virtual void turnOn(void); virtual int turnOn(void);
virtual void turnOff(void); virtual int turnOff(void);
virtual void bell(void); virtual int bell(void);
virtual void dim(unsigned char level); virtual int dim(unsigned char level);
virtual int methods(char* strModel); virtual int methods(char* strModel);
#ifndef _WINDOWS #ifndef _WINDOWS
@ -21,5 +21,5 @@ protected:
#endif #endif
protected: protected:
void send(char* strMessage); int send(char* strMessage);
}; };

View file

@ -51,49 +51,52 @@ DeviceIkea::~DeviceIkea(void)
/* /*
* Turn on this device * Turn on this device
*/ */
void DeviceIkea::turnOn(void){ int DeviceIkea::turnOn(void){
try{ try{
string strCode = getStringCode(255); string strCode = getStringCode(255);
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*
* Turn off this device * Turn off this device
*/ */
void DeviceIkea::turnOff(void){ int DeviceIkea::turnOff(void){
try{ try{
string strCode = getStringCode(0); string strCode = getStringCode(0);
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*
* Turn off this device * Turn off this device
*/ */
void DeviceIkea::dim(unsigned char level){ int DeviceIkea::dim(unsigned char level){
try{ try{
string strCode = getStringCode(level); string strCode = getStringCode(level);
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*

View file

@ -6,9 +6,9 @@ class DeviceIkea : public Device
{ {
public: public:
DeviceIkea(char *strSystem, char *strUnits, char *strFadeStyle); DeviceIkea(char *strSystem, char *strUnits, char *strFadeStyle);
virtual void turnOn(void); virtual int turnOn(void);
virtual void turnOff(void); virtual int turnOff(void);
virtual void dim(unsigned char level); virtual int dim(unsigned char level);
virtual int methods(char* strModel); virtual int methods(char* strModel);
public: public:

View file

@ -39,7 +39,7 @@ DeviceNexa::~DeviceNexa(void)
/* /*
* Turn on this device * Turn on this device
*/ */
void DeviceNexa::turnOn(void){ int DeviceNexa::turnOn(void){
try{ try{
string strCode = getStringCode(intHouse); string strCode = getStringCode(intHouse);
@ -51,17 +51,18 @@ void DeviceNexa::turnOn(void){
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*
* Turn off this device * Turn off this device
*/ */
void DeviceNexa::turnOff(void){ int DeviceNexa::turnOff(void){
try{ try{
string strCode = getStringCode(intHouse); string strCode = getStringCode(intHouse);
@ -73,17 +74,18 @@ void DeviceNexa::turnOff(void){
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*
* Send a bell * Send a bell
*/ */
void DeviceNexa::bell(void){ int DeviceNexa::bell(void){
try{ try{
string strCode = getStringCode(intHouse); string strCode = getStringCode(intHouse);
@ -94,11 +96,12 @@ void DeviceNexa::bell(void){
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*

View file

@ -6,9 +6,9 @@ class DeviceNexa : public Device
{ {
public: public:
DeviceNexa(char *strHouse, char *strCode); DeviceNexa(char *strHouse, char *strCode);
virtual void turnOn(void); virtual int turnOn(void);
virtual void turnOff(void); virtual int turnOff(void);
virtual void bell(void); virtual int bell(void);
virtual int methods(char* strModel); virtual int methods(char* strModel);
public: public:

View file

@ -25,7 +25,7 @@ DeviceSartano::~DeviceSartano(void)
/* /*
* Turn on this device * Turn on this device
*/ */
void DeviceSartano::turnOn(void){ int DeviceSartano::turnOn(void){
try{ try{
string strCode = getStringCode(); string strCode = getStringCode();
@ -35,17 +35,18 @@ void DeviceSartano::turnOn(void){
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*
* Turn off this device * Turn off this device
*/ */
void DeviceSartano::turnOff(void){ int DeviceSartano::turnOff(void){
try{ try{
string strCode = getStringCode(); string strCode = getStringCode();
@ -55,11 +56,12 @@ void DeviceSartano::turnOff(void){
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*

View file

@ -7,8 +7,8 @@ class DeviceSartano : public Device
{ {
public: public:
DeviceSartano(char *strCode); DeviceSartano(char *strCode);
virtual void turnOn(void); virtual int turnOn(void);
virtual void turnOff(void); virtual int turnOff(void);
virtual int methods(char* strModel); virtual int methods(char* strModel);
~DeviceSartano(void); ~DeviceSartano(void);

View file

@ -18,7 +18,7 @@ DeviceWaveman::DeviceWaveman(char *strNewHouse, char *strNewCode)
/* /*
* Turn off this device * Turn off this device
*/ */
void DeviceWaveman::turnOff(void){ int DeviceWaveman::turnOff(void){
try{ try{
string strCode = getStringCode(intHouse); string strCode = getStringCode(intHouse);
@ -30,11 +30,12 @@ void DeviceWaveman::turnOff(void){
char* strMessage = const_cast<char*>(strCode.c_str()); char* strMessage = const_cast<char*>(strCode.c_str());
Device::send(strMessage); return Device::send(strMessage);
} }
catch(...){ catch(...){
throw; throw;
} }
return TELLSTICK_ERROR_UNKNOWN;
} }
/* /*

View file

@ -6,6 +6,6 @@ class DeviceWaveman : public DeviceNexa
{ {
public: public:
DeviceWaveman(char *strHouse, char *strCode); DeviceWaveman(char *strHouse, char *strCode);
virtual void turnOff(void); virtual int turnOff(void);
virtual int methods(char* strModel); virtual int methods(char* strModel);
}; };

View file

@ -1,6 +1,7 @@
/** /**
* @defgroup core telldus-core * @defgroup core telldus-core
* Telldus Core is the base module used to interface a Telldus TellStick. * Telldus Core is the base module used to interface a Telldus TellStick.
* @{
*/ */
#ifdef _WINDOWS #ifdef _WINDOWS
@ -14,29 +15,25 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string.h> #include <string.h>
#include <stdlib.h>
void handleException(std::exception e); void handleException(std::exception e);
using namespace std; using namespace std;
/** /**
* @def TELLSTICK_TURNON * @def TELLSTICK_TURNON
* @ingroup core
* Device-flag for devices supporting the devTurnOn() call. * Device-flag for devices supporting the devTurnOn() call.
* *
* @def TELLSTICK_TURNOFF * @def TELLSTICK_TURNOFF
* @ingroup core
* Device-flag for devices supporting the devTurnOff() call. * Device-flag for devices supporting the devTurnOff() call.
* *
* @def TELLSTICK_BELL * @def TELLSTICK_BELL
* @ingroup core
* Device-flag for devices supporting the devBell() call. * Device-flag for devices supporting the devBell() call.
* *
* @def TELLSTICK_TOGGLE * @def TELLSTICK_TOGGLE
* @ingroup core
* This method is currently unimplemented * This method is currently unimplemented
* *
* @def TELLSTICK_DIM * @def TELLSTICK_DIM
* @ingroup core
* Device-flag for devices supporting the devDim() call. * Device-flag for devices supporting the devDim() call.
*/ */
@ -51,27 +48,33 @@ using namespace std;
* Make sure the device supports this by calling devMethods() before any * Make sure the device supports this by calling devMethods() before any
* call to this function. * call to this function.
* @param intDeviceId The device id to turn on. * @param intDeviceId The device id to turn on.
* @ingroup core
**/ **/
bool WINAPI devTurnOn(int intDeviceId){ int WINAPI devTurnOn(int intDeviceId){
try{ try{
TelldusSettings ts; TelldusSettings ts;
Device* dev = ts.getDevice(intDeviceId); Device* dev = ts.getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
dev->turnOn(); int methods = dev->methods( ts.getModel( intDeviceId ) );
int retval = 0;
if ( !(methods & TELLSTICK_TURNON) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else {
retval = dev->turnOn();
}
delete(dev); delete(dev);
return true; return retval;
} }
else{ else{
return false; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
catch(exception e){ catch(exception e){
handleException(e); handleException(e);
} }
return false; return TELLSTICK_ERROR_UNKNOWN;
} }
/** /**
@ -79,27 +82,33 @@ bool WINAPI devTurnOn(int intDeviceId){
* Make sure the device supports this by calling devMethods() before any * Make sure the device supports this by calling devMethods() before any
* call to this function. * call to this function.
* @param intDeviceId The device id to turn off. * @param intDeviceId The device id to turn off.
* @ingroup core
*/ */
bool WINAPI devTurnOff(int intDeviceId){ int WINAPI devTurnOff(int intDeviceId){
try{ try{
TelldusSettings ts; TelldusSettings ts;
Device* dev = ts.getDevice(intDeviceId); Device* dev = ts.getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
dev->turnOff(); int methods = dev->methods( ts.getModel( intDeviceId ) );
int retval = 0;
if ( !(methods & TELLSTICK_TURNOFF) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else {
retval = dev->turnOff();
}
delete(dev); delete(dev);
return true; return retval;
} }
else{ else{
return false; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
catch(exception e){ catch(exception e){
handleException(e); handleException(e);
} }
return false; return TELLSTICK_ERROR_UNKNOWN;
} }
/** /**
@ -107,27 +116,33 @@ bool WINAPI devTurnOff(int intDeviceId){
* Make sure the device supports this by calling devMethods() before any * Make sure the device supports this by calling devMethods() before any
* call to this function. * call to this function.
* @param intDeviceId The device id to send bell to * @param intDeviceId The device id to send bell to
* @ingroup core
*/ */
bool WINAPI devBell(int intDeviceId){ int WINAPI devBell(int intDeviceId){
try{ try{
TelldusSettings ts; TelldusSettings ts;
Device* dev = ts.getDevice(intDeviceId); Device* dev = ts.getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
dev->bell(); int methods = dev->methods( ts.getModel( intDeviceId ) );
int retval = 0;
if ( !(methods & TELLSTICK_BELL) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else {
retval = dev->bell();
}
delete(dev); delete(dev);
return true; return retval;
} }
else{ else{
return false; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
catch(exception e){ catch(exception e){
handleException(e); handleException(e);
} }
return false; return TELLSTICK_ERROR_UNKNOWN;
} }
/** /**
@ -136,38 +151,43 @@ bool WINAPI devBell(int intDeviceId){
* call to this function. * call to this function.
* @param intDeviceId The device id to dim * @param intDeviceId The device id to dim
* @param level The level the device should dim to. This value should be 0-255 * @param level The level the device should dim to. This value should be 0-255
* @ingroup core
*/ */
bool WINAPI devDim(int intDeviceId, unsigned char level){ int WINAPI devDim(int intDeviceId, unsigned char level){
try{ try{
TelldusSettings ts; TelldusSettings ts;
Device* dev = ts.getDevice(intDeviceId); Device* dev = ts.getDevice(intDeviceId);
if(dev != NULL){ if(dev != NULL){
if (level == 0) { int methods = dev->methods( ts.getModel( intDeviceId ) );
dev->turnOff(); int retval = 0;
} else if (level == 255) {
dev->turnOn(); if ( !(methods & TELLSTICK_DIM) ) {
retval = TELLSTICK_ERROR_METHOD_NOT_SUPPORTED;
} else { } else {
dev->dim(level); if (level == 0) {
retval = dev->turnOff();
} else if (level == 255) {
retval = dev->turnOn();
} else {
retval = dev->dim(level);
}
} }
delete(dev); delete(dev);
return true; return retval;
} }
else{ else{
return false; return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
} }
} }
catch(exception e){ catch(exception e){
handleException(e); handleException(e);
} }
return false; return TELLSTICK_ERROR_UNKNOWN;
} }
/** /**
* This function returns the number of devices configured * This function returns the number of devices configured
* @returns an integer of the total number of devices configured * @returns an integer of the total number of devices configured
* @ingroup core
*/ */
int WINAPI devGetNumberOfDevices(void){ int WINAPI devGetNumberOfDevices(void){
int intReturn = -1; int intReturn = -1;
@ -194,7 +214,6 @@ int WINAPI devGetNumberOfDevices(void){
* \endcode * \endcode
* @param intDeviceIndex The device index to query. The index starts from 0. * @param intDeviceIndex The device index to query. The index starts from 0.
* @returns the unique id for the device or -1 if the device is not found. * @returns the unique id for the device or -1 if the device is not found.
* @ingroup core
*/ */
int WINAPI devGetDeviceId(int intDeviceIndex){ int WINAPI devGetDeviceId(int intDeviceIndex){
int intReturn = -1; int intReturn = -1;
@ -213,7 +232,6 @@ int WINAPI devGetDeviceId(int intDeviceIndex){
* Query a device for it's name. * Query a device for it's name.
* @param intDeviceId The unique id of the device to query * @param intDeviceId The unique id of the device to query
* @returns The name of the device or an empty string if the device is not found. * @returns The name of the device or an empty string if the device is not found.
* @ingroup core
*/ */
char * WINAPI devGetName(int intDeviceId){ char * WINAPI devGetName(int intDeviceId){
char* strReturn; char* strReturn;
@ -362,7 +380,6 @@ bool WINAPI devRemoveDevice(int intDeviceId){
* Query a device for which methods it supports. * Query a device for which methods it supports.
* @param id The device id to query * @param id The device id to query
* @returns The method-flags OR'ed into an integer. * @returns The method-flags OR'ed into an integer.
* @ingroup core
* @sa TELLSTICK_TURNON * @sa TELLSTICK_TURNON
* @sa TELLSTICK_TURNOFF * @sa TELLSTICK_TURNOFF
* @sa TELLSTICK_BELL * @sa TELLSTICK_BELL
@ -387,13 +404,38 @@ int WINAPI devMethods(int id){
return intMethods; return intMethods;
} }
char * WINAPI devGetErrorString(int intErrorNo) {
const int numResponses = 5;
const char *responses[numResponses] = {
"Success",
"TellStick not found",
"Permission denied",
"Device not found",
"The method you tried to use is not supported by the device"
};
char *strReturn;
intErrorNo = abs(intErrorNo); //We don't use negative values here.
if (intErrorNo >= numResponses) {
strReturn = "Unknown error";
} else {
// Copy the error string to strReturn
strReturn = (char *)malloc( sizeof(char) * strlen(responses[intErrorNo]) );
strcpy( strReturn, responses[intErrorNo] );
}
#ifdef _WINDOWS
strReturn = (char *)SysAllocStringByteLen (strReturn, lstrlen(strReturn));
#endif
return strReturn;
}
//******** //********
//* Error management, set strLogName to "" to turn off //* Error management, set strLogName to "" to turn off
//* //*
void handleException(exception e){ void handleException(exception e){
char* strLogName = "c:\\errorlog.txt"; char* strLogName = "errorlog.txt";
//char* strLogName = ""; //char* strLogName = "";
if(strlen(strLogName) > 0){ if(strlen(strLogName) > 0){
@ -405,3 +447,4 @@ void handleException(exception e){
} }
} }
/*\@}*/

View file

@ -21,3 +21,5 @@ EXPORTS
devTurnOff @15 devTurnOff @15
devBell @16 devBell @16
devDim @17 devDim @17
devGetErrorString @18

View file

@ -20,22 +20,21 @@
#define WINAPI #define WINAPI
#define TELLSTICK_API #define TELLSTICK_API
#endif #endif
#ifndef __cplusplus
#define bool char
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
TELLSTICK_API bool WINAPI devTurnOn(int intDeviceId); TELLSTICK_API int WINAPI devTurnOn(int intDeviceId);
TELLSTICK_API bool WINAPI devTurnOff(int intDeviceId); TELLSTICK_API int WINAPI devTurnOff(int intDeviceId);
TELLSTICK_API bool WINAPI devBell(int intDeviceId); TELLSTICK_API int WINAPI devBell(int intDeviceId);
TELLSTICK_API bool WINAPI devDim(int intDeviceId, unsigned char level); TELLSTICK_API int WINAPI devDim(int intDeviceId, unsigned char level);
TELLSTICK_API int WINAPI devMethods(int id); TELLSTICK_API int WINAPI devMethods(int id);
TELLSTICK_API int WINAPI devGetNumberOfDevices(); TELLSTICK_API int WINAPI devGetNumberOfDevices();
TELLSTICK_API int WINAPI devGetDeviceId(int intDeviceIndex); TELLSTICK_API int WINAPI devGetDeviceId(int intDeviceIndex);
TELLSTICK_API char * WINAPI devGetErrorString(int intErrorNo);
TELLSTICK_API char * WINAPI devGetName(int intDeviceId); TELLSTICK_API char * WINAPI devGetName(int intDeviceId);
TELLSTICK_API bool WINAPI devSetName(int intDeviceId, const char* chNewName); TELLSTICK_API bool WINAPI devSetName(int intDeviceId, const char* chNewName);
TELLSTICK_API char * WINAPI devGetVendor(int intDeviceId); TELLSTICK_API char * WINAPI devGetVendor(int intDeviceId);
@ -52,12 +51,21 @@ extern "C" {
} }
#endif #endif
//Device methods
#define TELLSTICK_TURNON 1 #define TELLSTICK_TURNON 1
#define TELLSTICK_TURNOFF 2 #define TELLSTICK_TURNOFF 2
#define TELLSTICK_BELL 4 #define TELLSTICK_BELL 4
#define TELLSTICK_TOGGLE 8 #define TELLSTICK_TOGGLE 8
#define TELLSTICK_DIM 16 #define TELLSTICK_DIM 16
//Error codes
#define TELLSTICK_SUCCESS 0
#define TELLSTICK_ERROR_NOT_FOUND -1
#define TELLSTICK_ERROR_PERMISSION_DENIED -2
#define TELLSTICK_ERROR_DEVICE_NOT_FOUND -3
#define TELLSTICK_ERROR_METHOD_NOT_SUPPORTED -4
#define TELLSTICK_ERROR_UNKNOWN -99
//Protocol Nexa //Protocol Nexa
#define TELLSTICK_DEVICE_YCR3500 "1" #define TELLSTICK_DEVICE_YCR3500 "1"
#define TELLSTICK_DEVICE_YCR300D "2" #define TELLSTICK_DEVICE_YCR300D "2"

View file

@ -3,16 +3,22 @@
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
#include <string.h> #include <string.h>
#include <errno.h>
/* /*
* Send message to the USB dongle * Send message to the USB dongle
*/ */
void Device::send(char* strMessage) { int Device::send(char* strMessage) {
int fd = -1; int fd = -1;
struct termios tio; struct termios tio;
if( 0 > ( fd = open( strDevice, O_RDWR ) ) ) { if( 0 > ( fd = open( strDevice, O_RDWR ) ) ) {
return; if (errno == ENOENT) {
return TELLSTICK_ERROR_NOT_FOUND;
} else if (errno == EACCES) {
return TELLSTICK_ERROR_PERMISSION_DENIED;
}
return TELLSTICK_ERROR_UNKNOWN;
} }
/* adjust serial port parameters */ /* adjust serial port parameters */
@ -23,9 +29,11 @@ void Device::send(char* strMessage) {
tcflush(fd, TCIFLUSH); tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&tio); tcsetattr(fd,TCSANOW,&tio);
write(fd, strMessage, strlen(strMessage)); write(fd, strMessage, strlen(strMessage));
close(fd); close(fd);
return TELLSTICK_SUCCESS;
} }
void Device::setDevice(const char *device) { void Device::setDevice(const char *device) {