2 /// \file controller.cc
3 /// High level Barry API class
7 Copyright (C) 2005-2008, 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"
30 #define __DEBUG_MODE__
36 // Controller constructor
38 /// Constructor for the Controller class. Requires a valid ProbeResult
39 /// object to find the USB device to talk to.
41 /// \param[in] device One of the ProbeResult objects from the
44 Controller::Controller(const ProbeResult
&device
)
49 , m_zero(m_dev
, device
.m_ep
.write
, device
.m_ep
.read
, device
.m_zeroSocketSequence
)
52 dout("Controller: Using non-threaded sockets");
57 // Controller constructor
59 /// Constructor for the Controller class. Requires a valid ProbeResult
60 /// object to find the USB device to talk to.
62 /// \param[in] device One of the ProbeResult objects from the
64 /// \param[in] queue Plugin router object for reading data
67 Controller::Controller(const ProbeResult
&device
, SocketRoutingQueue
&queue
)
72 , m_zero(queue
, device
.m_ep
.write
, device
.m_zeroSocketSequence
)
75 dout("Controller: Using threaded socket router");
79 // set the queue to use our device
80 queue
.SetUsbDevice(&m_dev
, device
.m_ep
.write
, device
.m_ep
.read
);
83 void Controller::SetupUsb(const ProbeResult
&device
)
86 if( !m_dev
.GetConfiguration(cfg
) )
87 throw Usb::Error(m_dev
.GetLastError(),
88 "Controller: GetConfiguration failed");
90 if( cfg
!= BLACKBERRY_CONFIGURATION
) {
91 if( !m_dev
.SetConfiguration(BLACKBERRY_CONFIGURATION
) )
92 throw Usb::Error(m_dev
.GetLastError(),
93 "Controller: SetConfiguration failed");
96 m_iface
= new Usb::Interface(m_dev
, device
.m_interface
);
98 m_dev
.ClearHalt(device
.m_ep
.read
);
99 m_dev
.ClearHalt(device
.m_ep
.write
);
102 Controller::~Controller()
104 // // trap exceptions in the destructor
106 // // a non-default socket has been opened, close it
109 // catch( std::runtime_error &re ) {
110 // // do nothing... log it?
111 // dout("Exception caught in ~Socket: " << re.what());
114 // detach the router from our device
116 m_queue
->ClearUsbDevice();
120 // cleanup the interface
123 // this happens when for some reason the Desktop mode
124 // is not fully opened, but the device has already recommended
125 // a socket to open... in this case, reset the device
126 // in the hopes that on next open, it will be in a
127 // recognizable state.
129 // FIXME - this should not be necessary, and someday we
130 // we should figure out how to handle the "already open"
131 // response we get for the Desktop
133 // FIXME - halfOpen now seems to be handled in the Socket class...
134 // perhaps move this there if needed
138 dout("Controller object destroyed in halfopen state, resetting device");
144 ///////////////////////////////////////////////////////////////////////////////
148 // Tells device which mode is desired, and returns the suggested
149 // socket ID to use for that mode.
151 uint16_t Controller::SelectMode(ModeType mode
)
154 Protocol::Packet packet
;
156 packet
.size
= htobs(SB_MODE_PACKET_COMMAND_SIZE
);
157 packet
.command
= SB_COMMAND_SELECT_MODE
;
158 packet
.u
.socket
.socket
= htobs(SB_MODE_REQUEST_SOCKET
);
159 packet
.u
.socket
.sequence
= 0; // updated by Socket::Send()
160 memset(packet
.u
.socket
.u
.mode
.name
, 0, sizeof(packet
.u
.socket
.u
.mode
.name
));
162 char *modeName
= (char *) packet
.u
.socket
.u
.mode
.name
;
166 strcpy(modeName
, "RIM Bypass");
170 strcpy(modeName
, "RIM Desktop");
174 strcpy(modeName
, "RIM_JavaLoader");
178 strcpy(modeName
, "RIM_UsbSerData");
182 strcpy(modeName
, "RIM_UsbSerCtrl");
186 throw std::logic_error("Controller: Invalid mode in SelectMode");
190 // send mode command before we open, as a default socket is socket 0
191 Data
command(&packet
, btohs(packet
.size
));
195 m_zero
.Send(command
, response
);
197 // get the data socket number
198 // indicates the socket number that
199 // should be used below in the Open() call
200 Protocol::CheckSize(response
, SB_MODE_PACKET_RESPONSE_SIZE
);
201 MAKE_PACKET(modepack
, response
);
202 if( modepack
->command
!= SB_COMMAND_MODE_SELECTED
) {
203 eeout(command
, response
);
204 throw Error("Controller: mode not selected");
207 // return the socket that the device is expecting us to use
208 return btohs(modepack
->u
.socket
.socket
);
210 catch( Usb::Error
& ) {
211 eout("Controller: error setting desktop mode");
212 eeout(command
, response
);
218 ///////////////////////////////////////////////////////////////////////////////