2 /// \file controller.cc
3 /// High level Barry API class
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "controller.h"
26 #include "protostructs.h"
31 #define __DEBUG_MODE__
37 // Controller constructor
39 /// Constructor for the Controller class. Requires a valid ProbeResult
40 /// object to find the USB device to talk to.
42 /// \param[in] device One of the ProbeResult objects from the
45 Controller::Controller(const ProbeResult
&device
)
50 , m_zero(m_dev
, device
.m_ep
.write
, device
.m_ep
.read
, device
.m_zeroSocketSequence
)
53 dout("Controller: Using non-threaded sockets");
58 // Controller constructor
60 /// Constructor for the Controller class. Requires a valid ProbeResult
61 /// object to find the USB device to talk to.
63 /// \param[in] device One of the ProbeResult objects from the
65 /// \param[in] queue Plugin router object for reading data
68 Controller::Controller(const ProbeResult
&device
, SocketRoutingQueue
&queue
)
73 , m_zero(queue
, device
.m_ep
.write
, device
.m_zeroSocketSequence
)
76 dout("Controller: Using threaded socket router");
80 // set the queue to use our device
81 queue
.SetUsbDevice(&m_dev
, device
.m_ep
.write
, device
.m_ep
.read
);
84 void Controller::SetupUsb(const ProbeResult
&device
)
87 if( !m_dev
.GetConfiguration(cfg
) )
88 throw Usb::Error(m_dev
.GetLastError(),
89 "Controller: GetConfiguration failed");
91 if( cfg
!= BLACKBERRY_CONFIGURATION
) {
92 if( !m_dev
.SetConfiguration(BLACKBERRY_CONFIGURATION
) )
93 throw Usb::Error(m_dev
.GetLastError(),
94 "Controller: SetConfiguration failed");
97 m_iface
= new Usb::Interface(m_dev
, device
.m_interface
);
99 m_dev
.ClearHalt(device
.m_ep
.read
);
100 m_dev
.ClearHalt(device
.m_ep
.write
);
103 Controller::~Controller()
105 // // trap exceptions in the destructor
107 // // a non-default socket has been opened, close it
110 // catch( std::runtime_error &re ) {
111 // // do nothing... log it?
112 // dout("Exception caught in ~Socket: " << re.what());
115 // detach the router from our device
117 m_queue
->ClearUsbDevice();
121 // cleanup the interface
124 // this happens when for some reason the Desktop mode
125 // is not fully opened, but the device has already recommended
126 // a socket to open... in this case, reset the device
127 // in the hopes that on next open, it will be in a
128 // recognizable state.
130 // FIXME - this should not be necessary, and someday we
131 // we should figure out how to handle the "already open"
132 // response we get for the Desktop
134 // FIXME - halfOpen now seems to be handled in the Socket class...
135 // perhaps move this there if needed
139 dout("Controller object destroyed in halfopen state, resetting device");
145 ///////////////////////////////////////////////////////////////////////////////
149 // Tells device which mode is desired, and returns the suggested
150 // socket ID to use for that mode.
152 uint16_t Controller::SelectMode(ModeType mode
)
155 Protocol::Packet packet
;
157 packet
.size
= htobs(SB_MODE_PACKET_COMMAND_SIZE
);
158 packet
.command
= SB_COMMAND_SELECT_MODE
;
159 packet
.u
.socket
.socket
= htobs(SB_MODE_REQUEST_SOCKET
);
160 packet
.u
.socket
.sequence
= 0; // updated by Socket::Send()
161 memset(packet
.u
.socket
.u
.mode
.name
, 0, sizeof(packet
.u
.socket
.u
.mode
.name
));
163 char *modeName
= (char *) packet
.u
.socket
.u
.mode
.name
;
167 strcpy(modeName
, "RIM Bypass");
171 strcpy(modeName
, "RIM Desktop");
175 strcpy(modeName
, "RIM_JavaLoader");
179 strcpy(modeName
, "RIM_JVMDebug");
183 strcpy(modeName
, "RIM_UsbSerData");
187 strcpy(modeName
, "RIM_UsbSerCtrl");
191 throw std::logic_error("Controller: Invalid mode in SelectMode");
195 // send mode command before we open, as a default socket is socket 0
196 Data
command(&packet
, btohs(packet
.size
));
200 m_zero
.Send(command
, response
);
202 // get the data socket number
203 // indicates the socket number that
204 // should be used below in the Open() call
205 Protocol::CheckSize(response
, SB_MODE_PACKET_RESPONSE_SIZE
);
206 MAKE_PACKET(modepack
, response
);
207 if( modepack
->command
!= SB_COMMAND_MODE_SELECTED
) {
208 eeout(command
, response
);
209 throw Error("Controller: mode not selected");
212 if (mode
== Desktop
) {
213 // On the BlackBerry Storm, I have to read a packet
214 // after opening a socket. (only for Desktop mode)
215 // Otherwise, barrybackup and opensync-plugin can crash (timeout)
216 // I don't know why ! Maybe a bug on the handheld.
217 m_zero
.HideSequencePacket(false);
218 m_zero
.Receive(response
);
219 m_zero
.HideSequencePacket(true);
222 // return the socket that the device is expecting us to use
223 return btohs(modepack
->u
.socket
.socket
);
225 catch( Usb::Error
& ) {
226 eout("Controller: error setting desktop mode");
227 eeout(command
, response
);
233 ///////////////////////////////////////////////////////////////////////////////