Open Lighting Architecture  Latest Git
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 class DispatchingUsbProWidget;
37 
38 /*
39  * Contains information about a USB Pro like device.
40  */
42  public:
43  typedef uint32_t DeviceSerialNumber;
44  typedef uint16_t DeviceFirmwareVersion;
45 
47  esta_id(0),
48  device_id(0),
49  serial(0),
50  firmware_version(0),
51  has_firmware_version(false),
52  dual_port(false) {
53  }
55  esta_id(other.esta_id),
56  device_id(other.device_id),
57  serial(other.serial),
58  firmware_version(other.firmware_version),
59  has_firmware_version(other.has_firmware_version),
60  manufacturer(other.manufacturer),
61  device(other.device),
62  dual_port(other.dual_port) {
63  }
64  UsbProWidgetInformation& operator=(const UsbProWidgetInformation &other);
65 
66  void SetFirmware(DeviceFirmwareVersion new_firmware_version) {
67  has_firmware_version = true;
68  firmware_version = new_firmware_version;
69  }
70 
71  enum {SERIAL_LENGTH = 4};
72 
73  uint16_t esta_id;
74  uint16_t device_id;
75  DeviceSerialNumber serial;
76  DeviceFirmwareVersion firmware_version;
77  bool has_firmware_version;
78  std::string manufacturer;
79  std::string device;
80  bool dual_port;
81 };
82 
83 
84 /*
85  * Handles the discovery routine for devices that behave like a Enttec Usb Pro.
86  */
88  public:
92 
94  SuccessHandler *on_success,
95  FailureHandler *on_failure,
96  unsigned int message_interval = 200);
98 
99  bool Discover(ola::io::ConnectedDescriptor *descriptor);
100 
101  private:
102  // Hold the discovery state for a widget
103  class DiscoveryState {
104  public:
105  DiscoveryState():
106  discovery_state(MANUFACTURER_SENT),
107  timeout_id(ola::thread::INVALID_TIMEOUT),
108  sniffer_packets(0),
109  hardware_version(0) {
110  }
111  ~DiscoveryState() {}
112 
113  typedef enum {
114  MANUFACTURER_SENT,
115  DEVICE_SENT,
116  SERIAL_SENT,
117  GET_PARAM_SENT,
118  HARDWARE_VERSION_SENT,
119  } widget_state;
120 
121  UsbProWidgetInformation information;
122  widget_state discovery_state;
123  ola::thread::timeout_id timeout_id;
124  unsigned int sniffer_packets;
125  uint8_t hardware_version;
126  };
127 
129  const std::auto_ptr<SuccessHandler> m_callback;
130  const std::auto_ptr<FailureHandler> m_failure_callback;
131 
132  typedef std::map<DispatchingUsbProWidget*, DiscoveryState> WidgetStateMap;
133  WidgetStateMap m_widgets;
134  unsigned int m_timeout_ms;
135 
136  void HandleMessage(DispatchingUsbProWidget *widget,
137  uint8_t label,
138  const uint8_t *data,
139  unsigned int length);
140  void WidgetRemoved(DispatchingUsbProWidget *widget);
141  void SetupTimeout(DispatchingUsbProWidget *widget,
142  DiscoveryState *discovery_state);
143  void RemoveTimeout(DiscoveryState *discovery_state);
144  void SendNameRequest(DispatchingUsbProWidget *widget);
145  void SendSerialRequest(DispatchingUsbProWidget *widget);
146  void SendGetParams(DispatchingUsbProWidget *widget);
147  void MaybeSendHardwareVersionRequest(DispatchingUsbProWidget *widget);
148  void SendAPIRequest(DispatchingUsbProWidget *widget);
149  void DiscoveryTimeout(DispatchingUsbProWidget *widget);
150  void HandleIdResponse(DispatchingUsbProWidget *widget,
151  unsigned int length,
152  const uint8_t *data,
153  bool is_device);
154  void HandleSerialResponse(DispatchingUsbProWidget *widget,
155  unsigned int length,
156  const uint8_t *data);
157  void HandleGetParams(DispatchingUsbProWidget *widget,
158  unsigned int length,
159  const uint8_t *data);
160  void HandleHardwareVersionResponse(DispatchingUsbProWidget *widget,
161  unsigned int length,
162  const uint8_t *data);
163  void HandleSnifferPacket(DispatchingUsbProWidget *widget);
164  void CompleteWidgetDiscovery(DispatchingUsbProWidget *widget);
165  void DispatchWidget(DispatchingUsbProWidget *widget,
166  const UsbProWidgetInformation *info);
167  void HandleSniffer(DispatchingUsbProWidget *widget);
168 
169  static const uint8_t ENTTEC_SNIFFER_LABEL = 0x81;
170  static const uint8_t USB_PRO_MKII_API_LABEL = 13;
171  static const uint8_t DMX_PRO_MKII_VERSION = 2;
172  static const uint8_t DMX_PRO_MKII_B_VERSION = 3;
173  // The API key associated with OLA
174  static const uint32_t USB_PRO_MKII_API_KEY = 0x0d11b2d7;
175 };
176 } // namespace usbpro
177 } // namespace plugin
178 } // namespace ola
179 #endif // PLUGINS_USBPRO_USBPROWIDGETDETECTOR_H_
static const timeout_id INVALID_TIMEOUT
An invalid / uninitialized timeout_id.
Definition: SchedulerInterface.h:39
Combines the ExecutorInterface and the SchedulerInterface.
Definition: SchedulingExecutorInterface.h:33
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
void * timeout_id
A timeout handle which can later be used to cancel a timeout.
Definition: SchedulerInterface.h:34
Definition: BaseUsbProWidget.h:106
Definition: UsbProWidgetDetector.h:87
Definition: WidgetDetectorInterface.h:39
A 2 argument callback which can be called multiple times.
Definition: Callback.h:1895
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: UsbProWidgetDetector.h:41
A 1 argument callback which can be called multiple times.
Definition: Callback.h:992