tools: add playbook driver switch
[barry/progweb.git] / src / probe.h
blobcf8ed1ab31750f922cd99d130a37a1493c38522d
1 ///
2 /// \file probe.h
3 /// USB Blackberry detection routines
4 ///
6 /*
7 Copyright (C) 2005-2012, 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 #ifndef __BARRY_PROBE_H__
23 #define __BARRY_PROBE_H__
25 #include "dll.h"
26 #include "usbwrap.h"
27 #include "pin.h"
28 #include <vector>
29 #include <iosfwd>
30 #include <stdint.h>
32 namespace Barry {
34 class Probe;
36 struct BXEXPORT ProbeResult
38 friend class Probe;
39 friend class ProbePlayBook;
41 Usb::DeviceID m_dev;
42 unsigned char m_class;
43 unsigned char m_interface;
44 unsigned char m_altsetting;
45 Barry::Pin m_pin;
46 Usb::EndpointPair m_ep;
47 Usb::EndpointPair m_epModem;
48 // Specifies if it's necessary to clear halt on the
49 // endpoints before using them. On some devices such
50 // as the 8830 it's essential to clear halt. On other
51 // devices such as the Curve 8520 calling clear halt
52 // can cause them to get into a state where they drop
53 // packets.
54 bool m_needClearHalt;
55 // Specifies if it's necessary to call usb_set_altinterface()
56 // before attempting to use the end points for this device.
58 // This can help to re-synchronize the state between the USB
59 // host and the device. However it can also cause usb-storage
60 // URBs to be lost on some device, so it's only used as a
61 // last resort.
62 bool m_needSetAltInterface;
63 uint8_t m_zeroSocketSequence;
64 std::string m_description;
66 // data from a possible ConfigFile (filled in automatically by
67 // the probe code if available)
68 std::string m_cfgDeviceName;
70 private:
71 // All ProbeResult objects should come from Probe, therefore
72 // this constructor is private to force the issue.
73 ProbeResult()
74 : m_dev(0), m_interface(0), m_pin(0)
75 , m_needClearHalt(false), m_needSetAltInterface(false)
76 , m_zeroSocketSequence(0)
79 public:
80 void DumpAll(std::ostream &os) const;
81 bool HasIpModem() const { return m_epModem.IsComplete(); }
83 std::string GetDisplayName() const;
85 bool operator==(const Barry::Pin &pin) const
87 return m_pin == pin;
91 BXEXPORT std::ostream& operator<< (std::ostream &os, const ProbeResult &pr);
94 class BXEXPORT Probe
96 friend class ProbePlayBook;
98 public:
99 typedef std::vector<ProbeResult> Results;
101 enum LogExceptionBits {
102 LOG_BUSY = 0x0001,
103 LOG_ACCESS = 0x0002,
104 LOG_PERM = 0x0004
107 private:
108 Usb::DeviceList m_devices;
109 Results m_results;
111 unsigned int m_log_exceptions;
112 std::vector<std::string> m_fail_msgs;
113 int m_fail_count;
115 bool m_epp_override;
116 Usb::EndpointPair m_epp;
118 BXLOCAL bool CheckSize(const Data &data, unsigned int required);
119 BXLOCAL bool ParsePIN(const Data &data, uint32_t &pin);
120 BXLOCAL bool ParseDesc(const Data &data, std::string &desc);
122 protected:
123 void ProbeMatching(int vendor, int product,
124 const char *busname, const char *devname);
125 virtual void ProbeDevice(Usb::DeviceID& devid);
126 void ProbeDeviceEndpoints(Usb::Device &dev, Usb::EndpointPairings &ed, ProbeResult &result);
127 bool ProbePair(Usb::Device &dev, const Usb::EndpointPair &ep,
128 uint32_t &pin, std::string &desc, uint8_t &zeroSocketSequence,
129 bool &needClearHalt);
130 bool ProbeModem(Usb::Device &dev, const Usb::EndpointPair &ep);
132 public:
133 /// log_exceptions is a bitmask of low level USB errors to
134 /// log instead of throw on. If 0, all USB errors will cause
135 /// an exception, and thereby stop the probe. If the error
136 /// is set in the bitmask, the exception will be caught, and
137 /// logged in m_fail_msgs, which can be retrieved through
138 /// GetFailCount() and GetFailMsg(). If auto_dump_log is true,
139 /// all logged messages will be dumped as well, via the eout() macro,
140 /// to make sure they are seen, even if the application
141 /// programmer doesn't deal with them via the API.
142 Probe(const char *busname = 0, const char *devname = 0,
143 const Usb::EndpointPair *epp = 0,
144 unsigned int log_exceptions = LOG_BUSY | LOG_ACCESS | LOG_PERM,
145 bool auto_dump_log = true);
147 const Results& GetResults() const { return m_results; }
149 int GetCount() const { return m_results.size(); }
150 int GetFailCount() const { return m_fail_count; }
152 const std::string& GetFailMsg(int index) const { return m_fail_msgs.at(index); }
153 const ProbeResult& Get(int index) const { return m_results.at(index); }
155 int FindActive(Barry::Pin pin = 0) const; // returns -1 if pin not found
156 // or if no devices
157 static int FindActive(const Results &results, Barry::Pin pin = 0);
158 static int Find(const Results &results, Barry::Pin pin = 0);
162 class BXEXPORT ProbePlayBook : Probe
164 private:
165 int m_fail_count;
167 bool m_epp_override;
169 protected:
170 void ProbeDevice(Usb::DeviceID& devid);
172 public:
173 ProbePlayBook(const char *busname = 0, const char *devname = 0,
174 const Usb::EndpointPair *epp = 0);
178 } // namespace Barry
180 #endif