Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UsbProWidgetDetector.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  *
16  * UsbProWidgetDetector.h
17  * Handles the discovery process for widgets that implement the Usb Pro frame
18  * format.
19  * Copyright (C) 2010 Simon Newton
20  */
21 
22 #ifndef PLUGINS_USBPRO_USBPROWIDGETDETECTOR_H_
23 #define PLUGINS_USBPRO_USBPROWIDGETDETECTOR_H_
24 
25 #include <ola/thread/SchedulingExecutorInterface.h>
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include "ola/Callback.h"
30 #include "plugins/usbpro/WidgetDetectorInterface.h"
31 
32 namespace ola {
33 namespace plugin {
34 namespace usbpro {
35 
36 using std::auto_ptr;
37 
38 class DispatchingUsbProWidget;
39 
40 /*
41  * Contains information about a USB Pro like device.
42  */
44  public:
45  typedef uint32_t DeviceSerialNumber;
46 
48  esta_id(0),
49  device_id(0),
50  serial(0),
51  dual_port(false) {
52  }
54  esta_id(other.esta_id),
55  device_id(other.device_id),
56  serial(other.serial),
57  manufacturer(other.manufacturer),
58  device(other.device),
59  dual_port(other.dual_port) {
60  }
61  UsbProWidgetInformation& operator=(const UsbProWidgetInformation &other);
62  enum {SERIAL_LENGTH = 4};
63 
64  uint16_t esta_id;
65  uint16_t device_id;
66  DeviceSerialNumber serial;
67  string manufacturer;
68  string device;
69  bool dual_port;
70 };
71 
72 
73 /*
74  * Handles the discovery routine for devices that behave like a Enttec Usb Pro.
75  */
77  public:
78  typedef ola::Callback2<void,
81  typedef ola::Callback1<void,
83 
86  SuccessHandler *on_success,
87  FailureHandler *on_failure,
88  unsigned int message_interval = 200);
90 
91  bool Discover(ola::io::ConnectedDescriptor *descriptor);
92 
93  private:
94  // Hold the discovery state for a widget
95  class DiscoveryState {
96  public:
97  DiscoveryState():
98  discovery_state(MANUFACTURER_SENT),
99  timeout_id(ola::thread::INVALID_TIMEOUT),
100  sniffer_packets(0),
101  hardware_version(0) {
102  }
103  ~DiscoveryState() {}
104 
105  typedef enum {
106  MANUFACTURER_SENT,
107  DEVICE_SENT,
108  SERIAL_SENT,
109  HARDWARE_VERSION_SENT,
110  } widget_state;
111 
112  UsbProWidgetInformation information;
113  widget_state discovery_state;
114  ola::thread::timeout_id timeout_id;
115  unsigned int sniffer_packets;
116  uint8_t hardware_version;
117  };
118 
120  const auto_ptr<SuccessHandler> m_callback;
121  const auto_ptr<FailureHandler> m_failure_callback;
122 
123  typedef std::map<DispatchingUsbProWidget*, DiscoveryState> WidgetStateMap;
124  WidgetStateMap m_widgets;
125  unsigned int m_timeout_ms;
126 
127  void HandleMessage(DispatchingUsbProWidget *widget,
128  uint8_t label,
129  const uint8_t *data,
130  unsigned int length);
131  void WidgetRemoved(DispatchingUsbProWidget *widget);
132  void SetupTimeout(DispatchingUsbProWidget *widget,
133  DiscoveryState *discovery_state);
134  void RemoveTimeout(DiscoveryState *discovery_state);
135  void SendNameRequest(DispatchingUsbProWidget *widget);
136  void SendSerialRequest(DispatchingUsbProWidget *widget);
137  void SendHardwareVersionRequest(DispatchingUsbProWidget *widget);
138  void SendAPIRequest(DispatchingUsbProWidget *widget);
139  void DiscoveryTimeout(DispatchingUsbProWidget *widget);
140  void HandleIdResponse(DispatchingUsbProWidget *widget,
141  unsigned int length,
142  const uint8_t *data,
143  bool is_device);
144  void HandleSerialResponse(DispatchingUsbProWidget *widget,
145  unsigned int length,
146  const uint8_t *data);
147  void HandleHardwareVersionResponse(DispatchingUsbProWidget *widget,
148  unsigned int length,
149  const uint8_t *data);
150  void HandleSnifferPacket(DispatchingUsbProWidget *widget);
151  void CompleteWidgetDiscovery(DispatchingUsbProWidget *widget);
152  void DispatchWidget(DispatchingUsbProWidget *widget,
153  const UsbProWidgetInformation *info);
154  void HandleSniffer(DispatchingUsbProWidget *widget);
155 
156  static const uint8_t ENTTEC_SNIFFER_LABEL = 0x81;
157  static const uint8_t USB_PRO_MKII_API_LABEL = 13;
158  static const uint8_t DMX_PRO_MKII_VERISON = 2;
159  // The API key associated with OLA
160  static const uint32_t USB_PRO_MKII_API_KEY = 0x0d11b2d7;
161 };
162 } // namespace usbpro
163 } // namespace plugin
164 } // namespace ola
165 #endif // PLUGINS_USBPRO_USBPROWIDGETDETECTOR_H_