Open Lighting Architecture  0.9.6
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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  // Must be called before Run()
89  void SetUUCPLockFilePaths(const std::vector<std::string> &paths);
90 
91  // Start the thread, this will call the SuccessHandler whenever a new
92  // Widget is located.
93  void *Run();
94 
95  // Stop the thread.
96  bool Join(void *ptr);
97 
98  // Used to release a widget. Should be called from the thread running the
99  // SelectServerInterface that was passed to the constructor.
100  void FreeWidget(SerialWidgetInterface *widget);
101 
102  // blocks until the thread is running
103  void WaitUntilRunning();
104 
105  protected:
106  virtual bool RunScan();
107  void PerformDiscovery(const std::string &path,
108  ola::io::ConnectedDescriptor *descriptor);
109 
110  private:
111  ola::io::SelectServerInterface *m_other_ss;
112  ola::io::SelectServer m_ss; // ss for this thread
113  std::vector<WidgetDetectorInterface*> m_widget_detectors;
114  std::string m_directory; // directory to look for widgets in
115  std::vector<std::string> m_prefixes; // prefixes to try
116  std::set<std::string> m_ignored_devices; // devices to ignore
117  std::vector<std::string> m_uucp_lock_paths; // uucp lock path
118  NewWidgetHandler *m_handler;
119  bool m_is_running;
120  unsigned int m_usb_pro_timeout;
121  unsigned int m_robe_timeout;
122  ola::thread::Mutex m_mutex;
123  ola::thread::ConditionVariable m_condition;
124 
125  // those paths that are either in discovery, or in use
126  std::set<std::string> m_active_paths;
127  // holds the path and current widget detector offset
128  typedef std::pair<std::string, int> DescriptorInfo;
129  // map of descriptor to DescriptorInfo
130  typedef std::map<ola::io::ConnectedDescriptor*, DescriptorInfo>
131  ActiveDescriptors;
132  // the descriptors that are in the discovery process
133  ActiveDescriptors m_active_descriptors;
134 
135  // called when we find new widgets of a particular type
136  void UsbProWidgetReady(ola::io::ConnectedDescriptor *descriptor,
137  const UsbProWidgetInformation *info);
138  void RobeWidgetReady(ola::io::ConnectedDescriptor *descriptor,
139  const RobeWidgetInformation *info);
140 
141  void DescriptorFailed(ola::io::ConnectedDescriptor *descriptor);
142  void PerformNextDiscoveryStep(ola::io::ConnectedDescriptor *descriptor);
143  void InternalFreeWidget(SerialWidgetInterface *widget);
144  void FreeDescriptor(ola::io::ConnectedDescriptor *descriptor);
145 
146  template<typename WidgetType, typename InfoType>
147  void DispatchWidget(WidgetType *widget, const InfoType *information);
148 
149  // All of these are called in a separate thread.
150  template<typename WidgetType, typename InfoType>
151  void SignalNewWidget(WidgetType *widget, const InfoType *information);
152 
153  void MarkAsRunning();
154 
155  static const unsigned int SCAN_INTERVAL_MS = 20000;
156 
157  // This is how device identification is done, see
158  // http://opendmx.net/index.php/USB_Protocol_Extensions
159  // OPEN_LIGHTING_ESTA_CODE is in Constants.h
160 
161  // DmxKing Device Models
162  static const uint16_t DMX_KING_DMX512_ID = 0;
163  static const uint16_t DMX_KING_ULTRA_ID = 1;
164  static const uint16_t DMX_KING_ULTRA_PRO_ID = 2;
165  static const uint16_t DMX_KING_ULTRA_MICRO_ID = 3;
166  static const uint16_t DMX_KING_ULTRA_RDM_ID = 4;
167 
168  // Jese device models.
169  static const uint16_t JESE_DMX_TRI_MK1_ID = 1; // Original DMX-TRI
170  static const uint16_t JESE_RDM_TRI_MK1_ID = 2; // Original RDM-TRI
171  static const uint16_t JESE_RDM_TRI_MK2_ID = 3;
172  static const uint16_t JESE_RDM_TXI_MK2_ID = 4;
173  // DMX-TRI, with new hardware
174  static const uint16_t JESE_DMX_TRI_MK1_SE_ID = 5;
175 
176  // Goddard device models
177  static const uint16_t GODDARD_DMXTER4_ID = 0x444d;
178  static const uint16_t GODDARD_MINI_DMXTER4_ID = 0x4d49;
179 
180  // Open Lighting device models
181  static const uint16_t OPEN_LIGHTING_PACKETHEADS_ID = 2;
182  static const uint16_t OPEN_LIGHTING_RGB_MIXER_ID = 1;
183 
184  // ESTA Ids
185  static const uint16_t DMX_KING_ESTA_ID = 0x6a6b;
186  static const uint16_t GODDARD_ESTA_ID = 0x4744;
187  static const uint16_t JESE_ESTA_ID = 0x6864;
188 };
189 } // namespace usbpro
190 } // namespace plugin
191 } // namespace ola
192 #endif // PLUGINS_USBPRO_WIDGETDETECTORTHREAD_H_