Added TELLSTICK_ERROR_BROKEN_PIPE to distinguate between when communication with TellStick fails due to timeout or actual error.

This commit is contained in:
Stefan Persson 2012-01-12 11:39:04 +01:00
parent 69059b935b
commit b84e8b3806
8 changed files with 34 additions and 27 deletions

View file

@ -306,7 +306,7 @@ std::wstring Client::sendToService(const Message &msg) {
msleep(500); msleep(500);
continue; //retry continue; //retry
} }
readData = s.read(10000); //TODO changed to 10000 from 5000, how much does this do...? readData = s.read(8000); //TODO changed to 10000 from 5000, how much does this do...?
if(readData == L""){ if(readData == L""){
printf("Readdata nothing\n\r"); printf("Readdata nothing\n\r");
msleep(500); msleep(500);

View file

@ -57,6 +57,9 @@ using namespace TelldusCore;
* @def TELLSTICK_SUCCESS * @def TELLSTICK_SUCCESS
* Error code. Returned when the command succeeded. * Error code. Returned when the command succeeded.
* *
* @def TELLSTICK_ERROR_BROKEN_PIPE
* Error code. Pipe broken during communication.
*
* @def TELLSTICK_ERROR_NOT_FOUND * @def TELLSTICK_ERROR_NOT_FOUND
* Error code. Returned if a TellStick was not found on the system. * Error code. Returned if a TellStick was not found on the system.
* *
@ -487,10 +490,11 @@ int WINAPI tdMethods(int id, int methodsSupported){
* @sa TELLSTICK_ERROR_CONNECTING_SERVICE * @sa TELLSTICK_ERROR_CONNECTING_SERVICE
* @sa TELLSTICK_ERROR_UNKNOWN_RESPONSE * @sa TELLSTICK_ERROR_UNKNOWN_RESPONSE
* @sa TELLSTICK_ERROR_SYNTAX * @sa TELLSTICK_ERROR_SYNTAX
* @sa TELLSTICK_ERROR_BROKEN_PIPE
* @sa TELLSTICK_ERROR_UNKNOWN * @sa TELLSTICK_ERROR_UNKNOWN
*/ */
char * WINAPI tdGetErrorString(int intErrorNo) { char * WINAPI tdGetErrorString(int intErrorNo) {
const int numResponses = 9; const int numResponses = 10;
const char *responses[numResponses] = { const char *responses[numResponses] = {
"Success", "Success",
"TellStick not found", "TellStick not found",
@ -500,7 +504,8 @@ char * WINAPI tdGetErrorString(int intErrorNo) {
"An error occurred while communicating with TellStick", "An error occurred while communicating with TellStick",
"Could not connect to the Telldus Service", "Could not connect to the Telldus Service",
"Received an unknown response", "Received an unknown response",
"Syntax error" "Syntax error",
"Broken pipe"
}; };
std::string strReturn; std::string strReturn;
intErrorNo = abs(intErrorNo); //We don't use negative values here. intErrorNo = abs(intErrorNo); //We don't use negative values here.

View file

@ -116,6 +116,7 @@ extern "C" {
#define TELLSTICK_ERROR_CONNECTING_SERVICE -6 #define TELLSTICK_ERROR_CONNECTING_SERVICE -6
#define TELLSTICK_ERROR_UNKNOWN_RESPONSE -7 #define TELLSTICK_ERROR_UNKNOWN_RESPONSE -7
#define TELLSTICK_ERROR_SYNTAX -8 #define TELLSTICK_ERROR_SYNTAX -8
#define TELLSTICK_ERROR_BROKEN_PIPE -9
#define TELLSTICK_ERROR_UNKNOWN -99 #define TELLSTICK_ERROR_UNKNOWN -99
//Device typedef //Device typedef

View file

@ -1,7 +1,6 @@
#include "ClientCommunicationHandler.h" #include "ClientCommunicationHandler.h"
#include "Message.h" #include "Message.h"
#include "Strings.h" #include "Strings.h"
#include "Log.h" //Debug
#include <stdlib.h> #include <stdlib.h>

View file

@ -140,11 +140,11 @@ void ControllerManager::queryControllerStatus(){
std::string noop = "N+"; std::string noop = "N+";
for(std::list<TellStick *>::iterator it = tellStickControllers.begin(); it != tellStickControllers.end(); ++it) { for(std::list<TellStick *>::iterator it = tellStickControllers.begin(); it != tellStickControllers.end(); ++it) {
int success = (*it)->send(noop); int success = (*it)->send(noop);
if(success == TELLSTICK_ERROR_COMMUNICATION){ if(success == TELLSTICK_ERROR_BROKEN_PIPE){
Log::warning("TellStick query: Error in communication with TellStick, resetting USB"); Log::warning("TellStick query: Error in communication with TellStick, resetting USB");
resetController(*it); resetController(*it);
} }
if(success == TELLSTICK_ERROR_COMMUNICATION || success == TELLSTICK_ERROR_NOT_FOUND){ if(success == TELLSTICK_ERROR_BROKEN_PIPE || success == TELLSTICK_ERROR_NOT_FOUND){
reloadControllers = true; reloadControllers = true;
} }
} }

View file

@ -441,11 +441,11 @@ int DeviceManager::doAction(int deviceId, int action, unsigned char data){
if(controller){ if(controller){
retval = device->doAction(action, data, controller); retval = device->doAction(action, data, controller);
if(retval == TELLSTICK_ERROR_COMMUNICATION){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE){
Log::warning("Error in communication with TellStick when executing action. Resetting USB"); Log::warning("Error in communication with TellStick when executing action. Resetting USB");
d->controllerManager->resetController(controller); d->controllerManager->resetController(controller);
} }
if(retval == TELLSTICK_ERROR_COMMUNICATION || retval == TELLSTICK_ERROR_NOT_FOUND){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE || retval == TELLSTICK_ERROR_NOT_FOUND){
Log::warning("Rescanning USB ports"); Log::warning("Rescanning USB ports");
d->controllerManager->loadControllers(); d->controllerManager->loadControllers();
controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId());
@ -751,10 +751,10 @@ int DeviceManager::sendRawCommand(const std::wstring &command, int reserved){
int retval = TELLSTICK_ERROR_UNKNOWN; int retval = TELLSTICK_ERROR_UNKNOWN;
if(controller){ if(controller){
retval = controller->send(TelldusCore::wideToString(command)); retval = controller->send(TelldusCore::wideToString(command));
if(retval == TELLSTICK_ERROR_COMMUNICATION){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE){
d->controllerManager->resetController(controller); d->controllerManager->resetController(controller);
} }
if(retval == TELLSTICK_ERROR_COMMUNICATION || retval == TELLSTICK_ERROR_NOT_FOUND){ if(retval == TELLSTICK_ERROR_BROKEN_PIPE || retval == TELLSTICK_ERROR_NOT_FOUND){
d->controllerManager->loadControllers(); d->controllerManager->loadControllers();
controller = d->controllerManager->getBestControllerById(-1); controller = d->controllerManager->getBestControllerById(-1);
if(!controller){ if(!controller){

View file

@ -217,42 +217,44 @@ int TellStick::send( const std::string &strMessage ) {
delete[] tempMessage; delete[] tempMessage;
Log::notice("Message: %s", strMessage.c_str()); Log::notice("Message: %s", strMessage.c_str());
Log::notice("FWVersion: %d", firmwareVersion());
Log::notice("Pid: %X", pid());
if(!c){
Log::debug("Broken pipe on send");
return TELLSTICK_ERROR_BROKEN_PIPE;
}
if(d->ignoreControllerConfirmation || (strMessage == "N+" && ((pid() == 0x0C31 && firmwareVersion() < 5) || (pid() == 0x0C30 && firmwareVersion() < 6)))){ if(d->ignoreControllerConfirmation || (strMessage == "N+" && ((pid() == 0x0C31 && firmwareVersion() < 5) || (pid() == 0x0C30 && firmwareVersion() < 6)))){
//these firmware versions doesn't implement ack to noop, just check that the noop can be sent correctly //these firmware versions doesn't implement ack to noop, just check that the noop can be sent correctly
Log::notice("Too old firmware, accepting this"); Log::notice("Too old firmware, accepting this, just return success");
if(c){ return TELLSTICK_SUCCESS;
Log::notice("Success");
return TELLSTICK_SUCCESS;
}
else{
Log::notice("Fail");
return TELLSTICK_ERROR_COMMUNICATION;
}
} }
Log::warning("Continuing"); Log::warning("Continuing");
int retrycnt = 250; int retrycnt = 250;
unsigned char in; unsigned char in;
while(c && --retrycnt) { while(--retrycnt) {
ret = ftdi_read_data( &d->ftHandle, &in, 1); ret = ftdi_read_data( &d->ftHandle, &in, 1);
if (ret > 0) { if (ret > 0) {
Log::notice("Returned %c", in); Log::warning("%c", in);
if (in == '\n') { if (in == '\n') {
Log::notice("BREAK"); Log::warning("Received an end");
break; break;
} }
} else if(ret == 0) { // No data available } else if(ret == 0) { // No data available
usleep(100); usleep(100);
} else { //Error } else { //Error
c = false; Log::debug("Broken pipe on read");
return TELLSTICK_ERROR_BROKEN_PIPE;
} }
} }
Log::warning("Retry ready");
if (!retrycnt) { if (!retrycnt) {
c = false; Log::warning("Error in communication, retrycount ended");
}
if (!c) {
return TELLSTICK_ERROR_COMMUNICATION; return TELLSTICK_ERROR_COMMUNICATION;
} }
Log::warning("Success");
return TELLSTICK_SUCCESS; return TELLSTICK_SUCCESS;
} }

View file

@ -43,7 +43,7 @@ void TelldusMain::start(void) {
EventRef dataEvent = d->eventHandler.addEvent(); EventRef dataEvent = d->eventHandler.addEvent();
EventRef janitor = d->eventHandler.addEvent(); //Used for regular cleanups EventRef janitor = d->eventHandler.addEvent(); //Used for regular cleanups
Timer supervisor(janitor); //Tells the janitor to go back to work Timer supervisor(janitor); //Tells the janitor to go back to work
supervisor.setInterval(10); //TODO Once every minute supervisor.setInterval(60); //Once every minute
supervisor.start(); supervisor.start();
ControllerManager controllerManager(dataEvent.get()); ControllerManager controllerManager(dataEvent.get());