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"
32 #define __DEBUG_MODE__
38 // Controller constructor
40 /// Constructor for the Controller class. Requires a valid ProbeResult
41 /// object to find the USB device to talk to.
43 /// \param[in] device One of the ProbeResult objects from the
45 /// \param[in] default_timeout Override Usb::Device's default timeout
47 Controller::Controller(const ProbeResult
&device
,
50 , m_dev(device
.m_dev
, default_timeout
)
53 , m_zero(m_dev
, device
.m_ep
.write
, device
.m_ep
.read
, device
.m_zeroSocketSequence
)
56 dout("Controller: Using non-threaded sockets");
61 // Controller constructor
63 /// Constructor for the Controller class. Requires a valid ProbeResult
64 /// object to find the USB device to talk to.
66 /// \param[in] device One of the ProbeResult objects from the
68 /// \param[in] queue Plugin router object for reading data
70 /// \param[in] default_timeout Override Usb::Device's default timeout
72 Controller::Controller(const ProbeResult
&device
,
73 SocketRoutingQueue
&queue
,
76 , m_dev(device
.m_dev
, default_timeout
)
79 , m_zero(queue
, device
.m_ep
.write
, device
.m_zeroSocketSequence
)
82 dout("Controller: Using threaded socket router");
86 // set the queue to use our device
87 queue
.SetUsbDevice(&m_dev
, device
.m_ep
.write
, device
.m_ep
.read
);
90 void Controller::SetupUsb(const ProbeResult
&device
)
93 if( !m_dev
.GetConfiguration(cfg
) )
94 throw Usb::Error(m_dev
.GetLastError(),
95 "Controller: GetConfiguration failed");
97 if( cfg
!= BLACKBERRY_CONFIGURATION
|| MUST_SET_CONFIGURATION
) {
98 if( !m_dev
.SetConfiguration(BLACKBERRY_CONFIGURATION
) )
99 throw Usb::Error(m_dev
.GetLastError(),
100 "Controller: SetConfiguration failed");
103 m_iface
= new Usb::Interface(m_dev
, device
.m_interface
);
105 if( device
.m_needSetAltInterface
) {
106 m_dev
.SetAltInterface(device
.m_interface
);
109 if( device
.m_needClearHalt
) {
110 m_dev
.ClearHalt(device
.m_ep
.read
);
111 m_dev
.ClearHalt(device
.m_ep
.write
);
115 Controller::~Controller()
117 // // trap exceptions in the destructor
119 // // a non-default socket has been opened, close it
122 // catch( std::runtime_error &re ) {
123 // // do nothing... log it?
124 // dout("Exception caught in ~Socket: " << re.what());
127 // detach the router from our device
129 m_queue
->ClearUsbDevice();
133 // cleanup the interface
136 // this happens when for some reason the Desktop mode
137 // is not fully opened, but the device has already recommended
138 // a socket to open... in this case, reset the device
139 // in the hopes that on next open, it will be in a
140 // recognizable state.
142 // FIXME - this should not be necessary, and someday we
143 // we should figure out how to handle the "already open"
144 // response we get for the Desktop
146 // FIXME - halfOpen now seems to be handled in the Socket class...
147 // perhaps move this there if needed
151 dout("Controller object destroyed in halfopen state, resetting device");
157 ///////////////////////////////////////////////////////////////////////////////
161 // Tells device which mode is desired, and returns the suggested
162 // socket ID to use for that mode.
164 uint16_t Controller::SelectMode(ModeType mode
)
166 return SelectMode(mode
, NULL
);
169 // Tells device which mode is desired, and returns the suggested
170 // socket ID to use for that mode.
172 // If explicitModeName is not NULL then it will be used as the mode name.
173 // Otherwise the default mode name for the given mode will be used.
174 // It should be a nul terminated string if it is provided.
176 // The RawChannel mode requires an explicitModeName to be specified.
178 uint16_t Controller::SelectMode(ModeType mode
, const char *explicitModeName
)
181 Protocol::Packet packet
;
183 packet
.size
= htobs(SB_MODE_PACKET_COMMAND_SIZE
);
184 packet
.command
= SB_COMMAND_SELECT_MODE
;
185 packet
.u
.socket
.socket
= htobs(SB_MODE_REQUEST_SOCKET
);
186 packet
.u
.socket
.sequence
= 0; // updated by Socket::Send()
187 memset(packet
.u
.socket
.u
.mode
.name
, 0, sizeof(packet
.u
.socket
.u
.mode
.name
));
189 char *modeName
= (char *) packet
.u
.socket
.u
.mode
.name
;
191 if( explicitModeName
) {
192 if( strlen(explicitModeName
) >= sizeof(packet
.u
.socket
.u
.mode
.name
) ) {
193 throw std::logic_error("Controller: explicit mode name too long");
195 strcpy(modeName
, explicitModeName
);
198 // No modeName given, use the default
202 strcpy(modeName
, "RIM Bypass");
206 strcpy(modeName
, "RIM Desktop");
210 strcpy(modeName
, "RIM_JavaLoader");
214 strcpy(modeName
, "RIM_JVMDebug");
218 strcpy(modeName
, "RIM_UsbSerData");
222 strcpy(modeName
, "RIM_UsbSerCtrl");
226 throw std::logic_error("Controller: No channel name given with RawChannel mode");
230 throw std::logic_error("Controller: Invalid mode in SelectMode");
235 // send mode command before we open, as a default socket is socket 0
236 Data
command(&packet
, btohs(packet
.size
));
240 m_zero
.Send(command
, response
);
242 // get the data socket number
243 // indicates the socket number that
244 // should be used below in the Open() call
245 MAKE_PACKET(modepack
, response
);
246 if( modepack
->command
== SB_COMMAND_MODE_NOT_SELECTED
) {
247 throw Error("Controller: requested mode not supported");
249 if( modepack
->command
!= SB_COMMAND_MODE_SELECTED
) {
250 eeout(command
, response
);
251 throw Error("Controller: mode not selected");
254 if( mode
== Desktop
) {
255 // On the BlackBerry Storm, I have to read a packet
256 // after opening a socket. (only for Desktop mode)
257 // Otherwise, barrybackup and opensync-plugin can crash (timeout)
258 // I don't know why ! Maybe a bug on the handheld.
259 m_zero
.HideSequencePacket(false);
260 m_zero
.Receive(response
);
261 m_zero
.HideSequencePacket(true);
263 // return the socket that the device is expecting us to use
264 return btohs(modepack
->u
.socket
.socket
);
266 catch( Usb::Error
& ) {
267 eout("Controller: error setting desktop mode");
268 eeout(command
, response
);
274 ///////////////////////////////////////////////////////////////////////////////