controller.cc

Go to the documentation of this file.
00001 ///
00002 /// \file       controller.cc
00003 ///             High level Barry API class
00004 ///
00005 
00006 /*
00007     Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
00008 
00009     This program is free software; you can redistribute it and/or modify
00010     it under the terms of the GNU General Public License as published by
00011     the Free Software Foundation; either version 2 of the License, or
00012     (at your option) any later version.
00013 
00014     This program is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00017 
00018     See the GNU General Public License in the COPYING file at the
00019     root directory of this project for more details.
00020 */
00021 
00022 #include "controller.h"
00023 #include "probe.h"
00024 #include "common.h"
00025 #include "protocol.h"
00026 #include "protostructs.h"
00027 #include "data.h"
00028 #include "endian.h"
00029 #include <string.h>
00030 
00031 #define __DEBUG_MODE__
00032 #include "debug.h"
00033 
00034 namespace Barry {
00035 
00036 //
00037 // Controller constructor
00038 //
00039 /// Constructor for the Controller class.  Requires a valid ProbeResult
00040 /// object to find the USB device to talk to.
00041 ///
00042 /// \param[in]  device          One of the ProbeResult objects from the
00043 ///                             Probe class.
00044 /// \param[in]  default_timeout Override Usb::Device's default timeout
00045 ///
00046 Controller::Controller(const ProbeResult &device,
00047                         int default_timeout)
00048         : m_result(device)
00049         , m_dev(device.m_dev, default_timeout)
00050         , m_iface(0)
00051         , m_pin(device.m_pin)
00052         , m_zero(m_dev, device.m_ep.write, device.m_ep.read, device.m_zeroSocketSequence)
00053         , m_queue(0)
00054 {
00055         dout("Controller: Using non-threaded sockets");
00056         SetupUsb(device);
00057 }
00058 
00059 //
00060 // Controller constructor
00061 //
00062 /// Constructor for the Controller class.  Requires a valid ProbeResult
00063 /// object to find the USB device to talk to.
00064 ///
00065 /// \param[in]  device          One of the ProbeResult objects from the
00066 ///                             Probe class.
00067 /// \param[in]  queue           Plugin router object for reading data
00068 ///                             from sockets.
00069 /// \param[in]  default_timeout Override Usb::Device's default timeout
00070 ///
00071 Controller::Controller(const ProbeResult &device,
00072                         SocketRoutingQueue &queue,
00073                         int default_timeout)
00074         : m_result(device)
00075         , m_dev(device.m_dev, default_timeout)
00076         , m_iface(0)
00077         , m_pin(device.m_pin)
00078         , m_zero(queue, device.m_ep.write, device.m_zeroSocketSequence)
00079         , m_queue(&queue)
00080 {
00081         dout("Controller: Using threaded socket router");
00082 
00083         SetupUsb(device);
00084 
00085         // set the queue to use our device
00086         queue.SetUsbDevice(&m_dev, device.m_ep.write, device.m_ep.read);
00087 }
00088 
00089 void Controller::SetupUsb(const ProbeResult &device)
00090 {
00091         unsigned char cfg;
00092         if( !m_dev.GetConfiguration(cfg) )
00093                 throw Usb::Error(m_dev.GetLastError(),
00094                         "Controller: GetConfiguration failed");
00095 
00096         if( cfg != BLACKBERRY_CONFIGURATION ) {
00097                 if( !m_dev.SetConfiguration(BLACKBERRY_CONFIGURATION) )
00098                         throw Usb::Error(m_dev.GetLastError(),
00099                                 "Controller: SetConfiguration failed");
00100         }
00101 
00102         m_iface = new Usb::Interface(m_dev, device.m_interface);
00103 
00104         m_dev.ClearHalt(device.m_ep.read);
00105         m_dev.ClearHalt(device.m_ep.write);
00106 }
00107 
00108 Controller::~Controller()
00109 {
00110 //      // trap exceptions in the destructor
00111 //      try {
00112 //              // a non-default socket has been opened, close it
00113 //              m_socket.Close();
00114 //      }
00115 //      catch( std::runtime_error &re ) {
00116 //              // do nothing... log it?
00117 //              dout("Exception caught in ~Socket: " << re.what());
00118 //      }
00119 
00120         // detach the router from our device
00121         if( m_queue ) {
00122                 m_queue->ClearUsbDevice();
00123                 m_queue = 0;
00124         }
00125 
00126         // cleanup the interface
00127         delete m_iface;
00128 
00129         // this happens when for some reason the Desktop mode
00130         // is not fully opened, but the device has already recommended
00131         // a socket to open... in this case, reset the device
00132         // in the hopes that on next open, it will be in a
00133         // recognizable state.
00134         //
00135         // FIXME - this should not be necessary, and someday we
00136         // we should figure out how to handle the "already open"
00137         // response we get for the Desktop
00138         //
00139         // FIXME - halfOpen now seems to be handled in the Socket class...
00140         // perhaps move this there if needed
00141         //
00142 /*
00143         if( m_halfOpen ) {
00144                 dout("Controller object destroyed in halfopen state, resetting device");
00145                 m_dev.Reset();
00146         }
00147 */
00148 }
00149 
00150 ///////////////////////////////////////////////////////////////////////////////
00151 // protected members
00152 
00153 //
00154 // Tells device which mode is desired, and returns the suggested
00155 // socket ID to use for that mode.
00156 //
00157 uint16_t Controller::SelectMode(ModeType mode)
00158 {
00159         // select mode
00160         Protocol::Packet packet;
00161         packet.socket = 0;
00162         packet.size = htobs(SB_MODE_PACKET_COMMAND_SIZE);
00163         packet.command = SB_COMMAND_SELECT_MODE;
00164         packet.u.socket.socket = htobs(SB_MODE_REQUEST_SOCKET);
00165         packet.u.socket.sequence = 0; // updated by Socket::Send()
00166         memset(packet.u.socket.u.mode.name, 0, sizeof(packet.u.socket.u.mode.name));
00167 
00168         char *modeName = (char *) packet.u.socket.u.mode.name;
00169         switch( mode )
00170         {
00171         case Bypass:
00172                 strcpy(modeName, "RIM Bypass");
00173                 break;
00174 
00175         case Desktop:
00176                 strcpy(modeName, "RIM Desktop");
00177                 break;
00178 
00179         case JavaLoader:
00180                 strcpy(modeName, "RIM_JavaLoader");
00181                 break;
00182 
00183         case JVMDebug:
00184                 strcpy(modeName, "RIM_JVMDebug");
00185                 break;
00186 
00187         case UsbSerData:
00188                 strcpy(modeName, "RIM_UsbSerData");
00189                 break;
00190 
00191         case UsbSerCtrl:
00192                 strcpy(modeName, "RIM_UsbSerCtrl");
00193                 break;
00194 
00195         default:
00196                 throw std::logic_error("Controller: Invalid mode in SelectMode");
00197                 break;
00198         }
00199 
00200         // send mode command before we open, as a default socket is socket 0
00201         Data command(&packet, btohs(packet.size));
00202         Data response;
00203 
00204         try {
00205                 m_zero.Send(command, response);
00206 
00207                 // get the data socket number
00208                 // indicates the socket number that
00209                 // should be used below in the Open() call
00210                 Protocol::CheckSize(response, SB_MODE_PACKET_RESPONSE_SIZE);
00211                 MAKE_PACKET(modepack, response);
00212                 if( modepack->command != SB_COMMAND_MODE_SELECTED ) {
00213                         eeout(command, response);
00214                         throw Error("Controller: mode not selected");
00215                 }
00216 
00217                 if (mode == Desktop) {
00218                         // On the BlackBerry Storm, I have to read a packet
00219                         // after opening a socket. (only for Desktop mode)
00220                         // Otherwise, barrybackup and opensync-plugin can crash (timeout)
00221                         // I don't know why ! Maybe a bug on the handheld.
00222                         m_zero.HideSequencePacket(false);
00223                         m_zero.Receive(response);
00224                         m_zero.HideSequencePacket(true);
00225                 }
00226 
00227                 // return the socket that the device is expecting us to use
00228                 return btohs(modepack->u.socket.socket);
00229         }
00230         catch( Usb::Error & ) {
00231                 eout("Controller: error setting desktop mode");
00232                 eeout(command, response);
00233                 throw;
00234         }
00235 }
00236 
00237 
00238 ///////////////////////////////////////////////////////////////////////////////
00239 // public API
00240 
00241 
00242 } // namespace Barry
00243 
Generated by  doxygen 1.6.2-20100208