Reorganized sample ppp options files
[barry/pauldeden.git] / src / controller.cc
blobe741c7be9e14bb2ad1dd5eee392acbc08232dfa2
1 ///
2 /// \file controller.cc
3 /// High level Barry API class
4 ///
6 /*
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"
23 #include "probe.h"
24 #include "common.h"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "endian.h"
30 #define __DEBUG_MODE__
31 #include "debug.h"
33 namespace Barry {
36 // Controller constructor
38 /// Constructor for the Controller class. Requires a valid ProbeResult
39 /// object to find the USB device to talk to.
40 ///
41 /// \param[in] device One of the ProbeResult objects from the
42 /// Probe class.
43 ///
44 Controller::Controller(const ProbeResult &device)
45 : m_result(device)
46 , m_dev(device.m_dev)
47 , m_iface(0)
48 , m_pin(device.m_pin)
49 , m_zero(m_dev, device.m_ep.write, device.m_ep.read, device.m_zeroSocketSequence)
50 , m_queue(0)
52 dout("Controller: Using non-threaded sockets");
53 SetupUsb(device);
57 // Controller constructor
59 /// Constructor for the Controller class. Requires a valid ProbeResult
60 /// object to find the USB device to talk to.
61 ///
62 /// \param[in] device One of the ProbeResult objects from the
63 /// Probe class.
64 /// \param[in] queue Plugin router object for reading data
65 /// from sockets.
66 ///
67 Controller::Controller(const ProbeResult &device, SocketRoutingQueue &queue)
68 : m_result(device)
69 , m_dev(device.m_dev)
70 , m_iface(0)
71 , m_pin(device.m_pin)
72 , m_zero(queue, device.m_ep.write, device.m_zeroSocketSequence)
73 , m_queue(&queue)
75 dout("Controller: Using threaded socket router");
77 SetupUsb(device);
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)
85 unsigned char cfg;
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
105 // try {
106 // // a non-default socket has been opened, close it
107 // m_socket.Close();
108 // }
109 // catch( std::runtime_error &re ) {
110 // // do nothing... log it?
111 // dout("Exception caught in ~Socket: " << re.what());
112 // }
114 // detach the router from our device
115 if( m_queue ) {
116 m_queue->ClearUsbDevice();
117 m_queue = 0;
120 // cleanup the interface
121 delete m_iface;
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
137 if( m_halfOpen ) {
138 dout("Controller object destroyed in halfopen state, resetting device");
139 m_dev.Reset();
144 ///////////////////////////////////////////////////////////////////////////////
145 // protected members
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)
153 // select mode
154 Protocol::Packet packet;
155 packet.socket = 0;
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;
163 switch( mode )
165 case Bypass:
166 strcpy(modeName, "RIM Bypass");
167 break;
169 case Desktop:
170 strcpy(modeName, "RIM Desktop");
171 break;
173 case JavaLoader:
174 strcpy(modeName, "RIM_JavaLoader");
175 break;
177 case UsbSerData:
178 strcpy(modeName, "RIM_UsbSerData");
179 break;
181 case UsbSerCtrl:
182 strcpy(modeName, "RIM_UsbSerCtrl");
183 break;
185 default:
186 throw std::logic_error("Controller: Invalid mode in SelectMode");
187 break;
190 // send mode command before we open, as a default socket is socket 0
191 Data command(&packet, btohs(packet.size));
192 Data response;
194 try {
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);
213 throw;
218 ///////////////////////////////////////////////////////////////////////////////
219 // public API
222 } // namespace Barry