Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WidgetDetectorThread.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  * WidgetDetectorThread.h
17  * A thread that periodically looks for usb serial devices, and runs the
18  * callbacks if they are valid widgets.
19  * Copyright (C) 2011 Simon Newton
20  */
21 
22 #ifndef PLUGINS_USBPRO_WIDGETDETECTORTHREAD_H_
23 #define PLUGINS_USBPRO_WIDGETDETECTORTHREAD_H_
24 
25 #include <map>
26 #include <set>
27 #include <string>
28 #include <vector>
29 #include <utility>
30 #include "ola/Callback.h"
31 #include "ola/io/Descriptor.h"
32 #include "ola/io/SelectServer.h"
33 #include "ola/thread/Thread.h"
34 #include "plugins/usbpro/BaseUsbProWidget.h"
35 #include "plugins/usbpro/RobeWidget.h"
36 #include "plugins/usbpro/RobeWidgetDetector.h"
37 #include "plugins/usbpro/UsbProWidgetDetector.h"
38 #include "plugins/usbpro/SerialWidgetInterface.h"
39 #include "plugins/usbpro/WidgetDetectorInterface.h"
40 
41 namespace ola {
42 namespace plugin {
43 namespace usbpro {
44 
53  public:
54  virtual ~NewWidgetHandler() {}
55 
56  virtual void NewWidget(class ArduinoWidget *widget,
57  const UsbProWidgetInformation &information) = 0;
58  virtual void NewWidget(class EnttecUsbProWidget *widget,
59  const UsbProWidgetInformation &information) = 0;
60  virtual void NewWidget(class DmxTriWidget *widget,
61  const UsbProWidgetInformation &information) = 0;
62  virtual void NewWidget(class DmxterWidget *widget,
63  const UsbProWidgetInformation &information) = 0;
64  virtual void NewWidget(class RobeWidget *widget,
65  const RobeWidgetInformation &information) = 0;
66  virtual void NewWidget(class UltraDMXProWidget *widget,
67  const UsbProWidgetInformation &information) = 0;
68 };
69 
70 
71 /*
72  * Discovers new USB Serial widgets and calls the handler.
73  */
75  public:
76  explicit WidgetDetectorThread(NewWidgetHandler *widget_handler,
78  unsigned int usb_pro_timeout = 200,
79  unsigned int robe_timeout = 200);
81 
82  // Must be called before Run()
83  void SetDeviceDirectory(const std::string &directory);
84  // Must be called before Run()
85  void SetDevicePrefixes(const std::vector<std::string> &prefixes);
86  // Must be called before Run()
87  void SetIgnoredDevices(const std::vector<std::string> &devices);
88 
89  // Start the thread, this will call the SuccessHandler whenever a new
90  // Widget is located.
91  void *Run();
92 
93  // Stop the thread.
94  bool Join(void *ptr);
95 
96  // Can be called from any thread.
97  void FreeWidget(SerialWidgetInterface *widget);
98 
99  // blocks until the thread is running
100  void WaitUntilRunning();
101 
102  protected:
103  virtual bool RunScan();
104  void PerformDiscovery(const std::string &path,
105  ola::io::ConnectedDescriptor *descriptor);
106 
107  private:
108  ola::io::SelectServerInterface *m_other_ss;
109  ola::io::SelectServer m_ss; // ss for this thread
110  std::vector<WidgetDetectorInterface*> m_widget_detectors;
111  std::string m_directory; // directory to look for widgets in
112  std::vector<std::string> m_prefixes; // prefixes to try
113  std::set<std::string> m_ignored_devices; // devices to ignore
114  NewWidgetHandler *m_handler;
115  bool m_is_running;
116  unsigned int m_usb_pro_timeout;
117  unsigned int m_robe_timeout;
118  ola::thread::Mutex m_mutex;
119  ola::thread::ConditionVariable m_condition;
120 
121  // those paths that are either in discovery, or in use
122  std::set<std::string> m_active_paths;
123  // holds the path and current widget detector offset
124  typedef std::pair<std::string, int> DescriptorInfo;
125  // map of descriptor to DescriptorInfo
126  typedef std::map<ola::io::ConnectedDescriptor*, DescriptorInfo>
127  ActiveDescriptors;
128  // the descriptors that are in the discovery process
129  ActiveDescriptors m_active_descriptors;
130 
131  // called when we find new widgets of a particular type
132  void UsbProWidgetReady(ola::io::ConnectedDescriptor *descriptor,
133  const UsbProWidgetInformation *info);
134  void RobeWidgetReady(ola::io::ConnectedDescriptor *descriptor,
135  const RobeWidgetInformation *info);
136 
137  void DescriptorFailed(ola::io::ConnectedDescriptor *descriptor);
138  void PerformNextDiscoveryStep(ola::io::ConnectedDescriptor *descriptor);
139  void InternalFreeWidget(SerialWidgetInterface *widget);
140  void FreeDescriptor(ola::io::ConnectedDescriptor *descriptor);
141 
142  template<typename WidgetType, typename InfoType>
143  void DispatchWidget(WidgetType *widget, const InfoType *information);
144 
145  // All of these are called in a separate thread.
146  template<typename WidgetType, typename InfoType>
147  void SignalNewWidget(WidgetType *widget, const InfoType *information);
148 
149  void MarkAsRunning();
150 
151  static const unsigned int SCAN_INTERVAL_MS = 20000;
152 
153  // This is how device identification is done, see
154  // http://opendmx.net/index.php/USB_Protocol_Extensions
155  // OPEN_LIGHTING_ESTA_CODE is in BaseTypes.h
156 
157  // DmxKing Device Models
158  static const uint16_t DMX_KING_DMX512_ID = 0;
159  static const uint16_t DMX_KING_ULTRA_ID = 1;
160  static const uint16_t DMX_KING_ULTRA_PRO_ID = 2;
161  static const uint16_t DMX_KING_ULTRA_MICRO_ID = 3;
162  static const uint16_t DMX_KING_ULTRA_RDM_ID = 4;
163 
164  // Jese device models.
165  static const uint16_t JESE_DMX_TRI_MK1_ID = 1; // Original DMX-TRI
166  static const uint16_t JESE_RDM_TRI_MK1_ID = 2; // Original RDM-TRI
167  static const uint16_t JESE_RDM_TRI_MK2_ID = 3;
168  static const uint16_t JESE_RDM_TXI_MK2_ID = 4;
169  // DMX-TRI, with new hardware
170  static const uint16_t JESE_DMX_TRI_MK1_SE_ID = 5;
171 
172  // Goddard device models
173  static const uint16_t GODDARD_DMXTER4_ID = 0x444d;
174  static const uint16_t GODDARD_MINI_DMXTER4_ID = 0x4d49;
175 
176  // Open Lighting device models
177  static const uint16_t OPEN_LIGHTING_PACKETHEADS_ID = 2;
178  static const uint16_t OPEN_LIGHTING_RGB_MIXER_ID = 1;
179 
180  // ESTA Ids
181  static const uint16_t DMX_KING_ESTA_ID = 0x6a6b;
182  static const uint16_t GODDARD_ESTA_ID = 0x4744;
183  static const uint16_t JESE_ESTA_ID = 0x6864;
184 };
185 } // namespace usbpro
186 } // namespace plugin
187 } // namespace ola
188 #endif // PLUGINS_USBPRO_WIDGETDETECTORTHREAD_H_