More changes: we now get some data printed though it's not helpful.
[barry.git] / src / controller.cc
bloba3799d86cc7e4ebd37b1924981e8843bc6401f52
1 ///
2 /// \file controller.cc
3 /// High level Barry API class
4 ///
6 /*
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"
23 #include "probe.h"
24 #include "common.h"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "endian.h"
29 #include <string.h>
31 #define __DEBUG_MODE__
32 #include "debug.h"
34 namespace Barry {
37 // Controller constructor
39 /// Constructor for the Controller class. Requires a valid ProbeResult
40 /// object to find the USB device to talk to.
41 ///
42 /// \param[in] device One of the ProbeResult objects from the
43 /// Probe class.
44 /// \param[in] default_timeout Override Usb::Device's default timeout
45 ///
46 Controller::Controller(const ProbeResult &device,
47 int default_timeout)
48 : m_result(device)
49 , m_dev(device.m_dev, default_timeout)
50 , m_iface(0)
51 , m_pin(device.m_pin)
52 , m_zero(m_dev, device.m_ep.write, device.m_ep.read, device.m_zeroSocketSequence)
53 , m_queue(0)
55 dout("Controller: Using non-threaded sockets");
56 SetupUsb(device);
60 // Controller constructor
62 /// Constructor for the Controller class. Requires a valid ProbeResult
63 /// object to find the USB device to talk to.
64 ///
65 /// \param[in] device One of the ProbeResult objects from the
66 /// Probe class.
67 /// \param[in] queue Plugin router object for reading data
68 /// from sockets.
69 /// \param[in] default_timeout Override Usb::Device's default timeout
70 ///
71 Controller::Controller(const ProbeResult &device,
72 SocketRoutingQueue &queue,
73 int default_timeout)
74 : m_result(device)
75 , m_dev(device.m_dev, default_timeout)
76 , m_iface(0)
77 , m_pin(device.m_pin)
78 , m_zero(queue, device.m_ep.write, device.m_zeroSocketSequence)
79 , m_queue(&queue)
81 dout("Controller: Using threaded socket router");
83 SetupUsb(device);
85 // set the queue to use our device
86 queue.SetUsbDevice(&m_dev, device.m_ep.write, device.m_ep.read);
89 void Controller::SetupUsb(const ProbeResult &device)
91 unsigned char cfg;
92 if( !m_dev.GetConfiguration(cfg) )
93 throw Usb::Error(m_dev.GetLastError(),
94 "Controller: GetConfiguration failed");
96 if( cfg != BLACKBERRY_CONFIGURATION ) {
97 if( !m_dev.SetConfiguration(BLACKBERRY_CONFIGURATION) )
98 throw Usb::Error(m_dev.GetLastError(),
99 "Controller: SetConfiguration failed");
102 m_iface = new Usb::Interface(m_dev, device.m_interface);
104 m_dev.ClearHalt(device.m_ep.read);
105 m_dev.ClearHalt(device.m_ep.write);
108 Controller::~Controller()
110 // // trap exceptions in the destructor
111 // try {
112 // // a non-default socket has been opened, close it
113 // m_socket.Close();
114 // }
115 // catch( std::runtime_error &re ) {
116 // // do nothing... log it?
117 // dout("Exception caught in ~Socket: " << re.what());
118 // }
120 // detach the router from our device
121 if( m_queue ) {
122 m_queue->ClearUsbDevice();
123 m_queue = 0;
126 // cleanup the interface
127 delete m_iface;
129 // this happens when for some reason the Desktop mode
130 // is not fully opened, but the device has already recommended
131 // a socket to open... in this case, reset the device
132 // in the hopes that on next open, it will be in a
133 // recognizable state.
135 // FIXME - this should not be necessary, and someday we
136 // we should figure out how to handle the "already open"
137 // response we get for the Desktop
139 // FIXME - halfOpen now seems to be handled in the Socket class...
140 // perhaps move this there if needed
143 if( m_halfOpen ) {
144 dout("Controller object destroyed in halfopen state, resetting device");
145 m_dev.Reset();
150 ///////////////////////////////////////////////////////////////////////////////
151 // protected members
154 // Tells device which mode is desired, and returns the suggested
155 // socket ID to use for that mode.
157 uint16_t Controller::SelectMode(ModeType mode)
159 // select mode
160 Protocol::Packet packet;
161 packet.socket = 0;
162 packet.size = htobs(SB_MODE_PACKET_COMMAND_SIZE);
163 packet.command = SB_COMMAND_SELECT_MODE;
164 packet.u.socket.socket = htobs(SB_MODE_REQUEST_SOCKET);
165 packet.u.socket.sequence = 0; // updated by Socket::Send()
166 memset(packet.u.socket.u.mode.name, 0, sizeof(packet.u.socket.u.mode.name));
168 char *modeName = (char *) packet.u.socket.u.mode.name;
169 switch( mode )
171 case Bypass:
172 strcpy(modeName, "RIM Bypass");
173 break;
175 case Desktop:
176 strcpy(modeName, "RIM Desktop");
177 break;
179 case JavaLoader:
180 strcpy(modeName, "RIM_JavaLoader");
181 break;
183 case JVMDebug:
184 strcpy(modeName, "RIM_JVMDebug");
185 break;
187 case UsbSerData:
188 strcpy(modeName, "RIM_UsbSerData");
189 break;
191 case UsbSerCtrl:
192 strcpy(modeName, "RIM_UsbSerCtrl");
193 break;
195 case VNCServer:
196 strcpy(modeName, "VNC_Server");
197 break;
199 default:
200 throw std::logic_error("Controller: Invalid mode in SelectMode");
201 break;
204 // send mode command before we open, as a default socket is socket 0
205 Data command(&packet, btohs(packet.size));
206 Data response;
208 try {
209 m_zero.Send(command, response);
211 // get the data socket number
212 // indicates the socket number that
213 // should be used below in the Open() call
214 Protocol::CheckSize(response, SB_MODE_PACKET_RESPONSE_SIZE);
215 MAKE_PACKET(modepack, response);
216 if( modepack->command != SB_COMMAND_MODE_SELECTED ) {
217 eeout(command, response);
218 throw Error("Controller: mode not selected");
221 if (mode == Desktop) {
222 // On the BlackBerry Storm, I have to read a packet
223 // after opening a socket. (only for Desktop mode)
224 // Otherwise, barrybackup and opensync-plugin can crash (timeout)
225 // I don't know why ! Maybe a bug on the handheld.
226 m_zero.HideSequencePacket(false);
227 m_zero.Receive(response);
228 m_zero.HideSequencePacket(true);
231 // return the socket that the device is expecting us to use
232 return btohs(modepack->u.socket.socket);
234 catch( Usb::Error & ) {
235 eout("Controller: error setting desktop mode");
236 eeout(command, response);
237 throw;
242 ///////////////////////////////////////////////////////////////////////////////
243 // public API
246 } // namespace Barry