Changeset 06ac02


Ignore:
Timestamp:
01/10/12 09:55:26 (4 months ago)
Author:
Stefan Persson <stefan.persson@…>
Branches:
('master', 'deebf2045e7119c339412580f37a1e653f7d5715')('controller-upgrade', '00f95d22e12d96ef089e0902ef62ae8ce841dc6f')
Children:
dfae48ef33f9c218e80c5ec9ec1d96a1ae5fa625
Parents:
e019f416d9aa3212aa5dc3a677eb8e2cf8ad8290
git-author:
Stefan Persson <stefan.persson@telldus.se>2012-01-10 09:55:26+01:00
git-committer:
Stefan Persson <stefan.persson@telldus.se>2012-01-10 09:55:26+01:00
Message:

Resetting USB and retrying send once if TellStick is not found, in Linux

Location:
telldus-core/service
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • telldus-core/service/Controller.h

    r99f0b1 r06ac02  
    1717        virtual int firmwareVersion() = 0; 
    1818        virtual int send( const std::string &message ) = 0; 
     19        virtual int reset() = 0; 
    1920 
    2021protected: 
  • telldus-core/service/ControllerManager.cpp

    rbf4589 r06ac02  
    33#include "Mutex.h" 
    44#include "TellStick.h" 
     5#include "Log.h" 
    56 
    67#include <map> 
     
    9192 
    9293        std::list<TellStickDescriptor>::iterator it = list.begin(); 
     94        Log::notice("Before for-loop"); 
    9395        for(; it != list.end(); ++it) { 
     96                Log::notice("Something in the loop"); 
    9497                //Most backend only report non-opened devices. 
    9598                //If they don't make sure we don't open them twice 
     
    97100                ControllerMap::const_iterator cit = d->controllers.begin(); 
    98101                for(; cit != d->controllers.end(); ++cit) { 
     102                        Log::notice("Something in second the loop"); 
    99103                        TellStick *tellstick = reinterpret_cast<TellStick*>(cit->second); 
    100104                        if (!tellstick) { 
     105                                Log::notice("No tellstick"); 
    101106                                continue; 
    102107                        } 
    103108                        if (tellstick->isSameAsDescriptor(*it)) { 
    104109                                found = true; 
     110                                Log::notice("FOUND"); 
    105111                                break; 
    106112                        } 
     
    112118                int controllerId = d->lastControllerId-1; 
    113119                TellStick *controller = new TellStick(controllerId, d->event, *it); 
     120                Log::notice("Is it open?"); 
    114121                if (!controller->isOpen()) { 
     122                        Log::notice("Yes it was"); 
    115123                        delete controller; 
    116124                        continue; 
     
    120128        } 
    121129} 
     130 
     131int ControllerManager::resetController(Controller *controller) { 
     132        TellStick *tellstick = reinterpret_cast<TellStick*>(controller); 
     133        if (!tellstick) { 
     134                return true; //not tellstick, nothing to reset at the moment, just return true 
     135        } 
     136        Log::notice("resettingController"); 
     137        int success = controller->reset(); //ehh, här är väl controllern borttagen förresten? 
     138        Log::notice("Remove device"); 
     139        deviceInsertedOrRemoved(tellstick->vid(), tellstick->pid(), tellstick->serial(), false); //remove from list and delete 
     140        Log::notice("Device removed"); 
     141        return success; 
     142} 
  • telldus-core/service/ControllerManager.h

    rcccaf8 r06ac02  
    1515 
    1616        Controller *getBestControllerById(int id); 
    17  
    18 protected: 
    1917        void loadControllers(); 
     18        int resetController(Controller *controller); 
    2019 
    2120private: 
  • telldus-core/service/DeviceManager.cpp

    r1646eb r06ac02  
    77#include "Message.h" 
    88#include "common.h" 
     9#include "Log.h" 
    910 
    1011#include <map> 
     
    432433        } 
    433434        else{ 
     435                Log::notice("Getting a controller..."); 
    434436                Controller *controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); 
     437                Log::notice("Doing action..."); 
     438                if(!controller){ 
     439                        Log::warning("No controller found, rescanning USB ports"); 
     440                        //no controller found, scan for one, and retry once 
     441                        d->controllerManager->loadControllers(); 
     442                        controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); 
     443                } 
     444 
    435445                if(controller){ 
     446                        Log::notice("But now, a controller!"); 
    436447                        retval = device->doAction(action, data, controller); 
     448                        Log::notice("Retval received, %d.", retval); 
     449                        if(retval == TELLSTICK_ERROR_COMMUNICATION){ 
     450                                Log::warning("Error in communication with TellStick, resetting USB"); 
     451                                d->controllerManager->resetController(controller); 
     452                                Log::notice("Controller reset"); 
     453                        } 
     454                        if(retval == TELLSTICK_ERROR_COMMUNICATION || retval == TELLSTICK_ERROR_NOT_FOUND){ 
     455                                Log::warning("Rescanning USB ports"); 
     456                                d->controllerManager->loadControllers(); 
     457                                Log::notice("Loaded, get one"); 
     458                                controller = d->controllerManager->getBestControllerById(device->getPreferredControllerId()); 
     459                                if(!controller){ 
     460                                        Log::error("No contoller (TellStick) found, even after reset. Giving up."); 
     461                                        return TELLSTICK_ERROR_NOT_FOUND; 
     462                                } 
     463                                Log::notice("Got a new, do action"); 
     464                                retval = device->doAction(action, data, controller); //retry one more time 
     465                                Log::notice("Did action"); 
     466                        } 
    437467                } else { 
     468                        Log::error("No contoller (TellStick) found after one retry. Giving up."); 
    438469                        return TELLSTICK_ERROR_NOT_FOUND; 
    439470                } 
     
    720751 
    721752        Controller *controller = d->controllerManager->getBestControllerById(-1); 
     753 
     754        if(!controller){ 
     755                //no controller found, scan for one, and retry once 
     756                d->controllerManager->loadControllers(); 
     757                controller = d->controllerManager->getBestControllerById(-1); 
     758        } 
     759 
     760        int retval = TELLSTICK_ERROR_UNKNOWN; 
    722761        if(controller){ 
    723                 return controller->send(TelldusCore::wideToString(command)); 
    724         } 
    725         else{ 
     762                retval = controller->send(TelldusCore::wideToString(command)); 
     763                if(retval == TELLSTICK_ERROR_COMMUNICATION){ 
     764                        d->controllerManager->resetController(controller); 
     765                } 
     766                if(retval == TELLSTICK_ERROR_COMMUNICATION || retval == TELLSTICK_ERROR_NOT_FOUND){ 
     767                        d->controllerManager->loadControllers(); 
     768                        controller = d->controllerManager->getBestControllerById(-1); 
     769                        if(!controller){ 
     770                                return TELLSTICK_ERROR_NOT_FOUND; 
     771                        } 
     772                        retval = controller->send(TelldusCore::wideToString(command));  //retry one more time 
     773                } 
     774                return retval; 
     775        } else { 
    726776                return TELLSTICK_ERROR_NOT_FOUND; 
    727777        } 
  • telldus-core/service/TellStick.h

    r05a816 r06ac02  
    3030        virtual int firmwareVersion(); 
    3131        virtual int pid() const; 
     32        virtual int vid() const; 
     33        virtual std::string serial() const; 
     34 
    3235        bool isOpen() const; 
    3336        bool isSameAsDescriptor(const TellStickDescriptor &d) const; 
     37        virtual int reset(); 
    3438        virtual int send( const std::string &message ); 
    3539        bool stillConnected() const; 
  • telldus-core/service/TellStick_libftdi.cpp

    re019f4 r06ac02  
    2222#include "Strings.h" 
    2323#include "common.h" 
     24#include "Log.h" 
    2425 
    2526#include <unistd.h> 
     
    9899int TellStick::pid() const { 
    99100        return d->pid; 
     101} 
     102 
     103int TellStick::vid() const { 
     104        return d->vid; 
     105} 
     106 
     107std::string TellStick::serial() const { 
     108        return d->serial; 
    100109} 
    101110 
     
    136145} 
    137146 
     147int TellStick::reset(){ 
     148        Log::notice("Resetting one"); 
     149        int success = ftdi_usb_reset( &d->ftHandle ); 
     150        Log::notice("Has reset one"); 
     151        if(success < 0){ 
     152                return TELLSTICK_ERROR_UNKNOWN; //-1 = FTDI reset failed, -2 = USB device unavailable 
     153        } 
     154        return success; 
     155} 
     156 
    138157void TellStick::run() { 
    139158        int dwBytesRead = 0; 
     
    274293        ftdi_init(&ftdic); 
    275294 
     295        Log::notice("Trying to find Duo"); 
    276296        int ret = ftdi_usb_find_all(&ftdic, &devlist, vid, pid); 
    277297        if (ret > 0) { 
     298                Log::notice("Curdev > 0"); 
    278299                for (curdev = devlist; curdev != NULL; curdev = curdev->next) { 
     300                        Log::notice("Something in the loop"); 
    279301                        ret = ftdi_usb_get_strings(&ftdic, curdev->dev, NULL, 0, NULL, 0, serialBuffer, 10); 
     302                        Log::notice("Ret: %d",ret); 
     303                        //blir -9 efter felen, "get serial number failed", även lsusb -v ger annat svar än innan...? 
    280304                        if (ret != 0) { 
    281305                                continue; 
Note: See TracChangeset for help on using the changeset viewer.