Open Lighting Architecture
 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 
45 using std::set;
46 using std::string;
47 using std::vector;
50 using ola::thread::Mutex;
51 
52 
61  public:
62  virtual ~NewWidgetHandler() {}
63 
64  virtual void NewWidget(class ArduinoWidget *widget,
65  const UsbProWidgetInformation &information) = 0;
66  virtual void NewWidget(class EnttecUsbProWidget *widget,
67  const UsbProWidgetInformation &information) = 0;
68  virtual void NewWidget(class DmxTriWidget *widget,
69  const UsbProWidgetInformation &information) = 0;
70  virtual void NewWidget(class DmxterWidget *widget,
71  const UsbProWidgetInformation &information) = 0;
72  virtual void NewWidget(class RobeWidget *widget,
73  const RobeWidgetInformation &information) = 0;
74  virtual void NewWidget(class UltraDMXProWidget *widget,
75  const UsbProWidgetInformation &information) = 0;
76 };
77 
78 
79 /*
80  * Discovers new USB Serial widgets and calls the handler.
81  */
83  public:
84  explicit WidgetDetectorThread(NewWidgetHandler *widget_handler,
86  unsigned int usb_pro_timeout = 200,
87  unsigned int robe_timeout = 200);
89 
90  // Must be called before Run()
91  void SetDeviceDirectory(const string &directory);
92  // Must be called before Run()
93  void SetDevicePrefixes(const vector<string> &prefixes);
94  // Must be called before Run()
95  void SetIgnoredDevices(const vector<string> &devices);
96 
97  // Start the thread, this will call the SuccessHandler whenever a new
98  // Widget is located.
99  void *Run();
100 
101  // Stop the thread.
102  bool Join(void *ptr);
103 
104  // Can be called from any thread.
105  void FreeWidget(SerialWidgetInterface *widget);
106 
107  // blocks until the thread is running
108  void WaitUntilRunning();
109 
110  protected:
111  virtual bool RunScan();
112  void PerformDiscovery(const string &path,
113  ConnectedDescriptor *descriptor);
114 
115  private:
116  ola::io::SelectServerInterface *m_other_ss;
117  ola::io::SelectServer m_ss; // ss for this thread
118  vector<WidgetDetectorInterface*> m_widget_detectors;
119  string m_directory; // directory to look for widgets in
120  vector<string> m_prefixes; // prefixes to try
121  set<string> m_ignored_devices; // devices to ignore
122  NewWidgetHandler *m_handler;
123  bool m_is_running;
124  unsigned int m_usb_pro_timeout;
125  unsigned int m_robe_timeout;
126  Mutex m_mutex;
127  ConditionVariable m_condition;
128 
129  // those paths that are either in discovery, or in use
130  set<string> m_active_paths;
131  // holds the path and current widget detector offset
132  typedef std::pair<string, int> DescriptorInfo;
133  // map of descriptor to DescriptorInfo
134  typedef map<ConnectedDescriptor*, DescriptorInfo>
135  ActiveDescriptors;
136  // the descriptors that are in the discovery process
137  ActiveDescriptors m_active_descriptors;
138 
139  // called when we find new widgets of a particular type
140  void UsbProWidgetReady(ConnectedDescriptor *descriptor,
141  const UsbProWidgetInformation *info);
142  void RobeWidgetReady(ConnectedDescriptor *descriptor,
143  const RobeWidgetInformation *info);
144 
145  void DescriptorFailed(ConnectedDescriptor *descriptor);
146  void PerformNextDiscoveryStep(ConnectedDescriptor *descriptor);
147  void InternalFreeWidget(SerialWidgetInterface *widget);
148  void FreeDescriptor(ConnectedDescriptor *descriptor);
149 
150  template<typename WidgetType, typename InfoType>
151  void DispatchWidget(WidgetType *widget, const InfoType *information);
152 
153  // All of these are called in a separate thread.
154  template<typename WidgetType, typename InfoType>
155  void SignalNewWidget(WidgetType *widget, const InfoType *information);
156 
157  void MarkAsRunning();
158 
159  static const unsigned int SCAN_INTERVAL_MS = 20000;
160 
161  // This is how device identification is done, see
162  // http://opendmx.net/index.php/USB_Protocol_Extensions
163  // OPEN_LIGHTING_ESTA_CODE is in BaseTypes.h
164 
165  // DmxKing Device Models
166  static const uint16_t DMX_KING_DMX512_ID = 0;
167  static const uint16_t DMX_KING_ULTRA_ID = 1;
168  static const uint16_t DMX_KING_ULTRA_PRO_ID = 2;
169  static const uint16_t DMX_KING_ULTRA_MICRO_ID = 3;
170  static const uint16_t DMX_KING_ULTRA_RDM_ID = 4;
171 
172  // Jese device models.
173  static const uint16_t JESE_DMX_TRI_MK1_ID = 1; // Original DMX-TRI
174  static const uint16_t JESE_RDM_TRI_MK1_ID = 2; // Original RDM-TRI
175  static const uint16_t JESE_RDM_TRI_MK2_ID = 3;
176  static const uint16_t JESE_RDM_TXI_MK2_ID = 4;
177  // DMX-TRI, with new hardware
178  static const uint16_t JESE_DMX_TRI_MK1_SE_ID = 5;
179 
180  // Goddard device models
181  static const uint16_t GODDARD_DMXTER4_ID = 0x444d;
182  static const uint16_t GODDARD_MINI_DMXTER4_ID = 0x4d49;
183 
184  // Open Lighting device models
185  static const uint16_t OPEN_LIGHTING_PACKETHEADS_ID = 2;
186  static const uint16_t OPEN_LIGHTING_RGB_MIXER_ID = 1;
187 
188  // ESTA Ids
189  static const uint16_t DMX_KING_ESTA_ID = 0x6a6b;
190  static const uint16_t GODDARD_ESTA_ID = 0x4744;
191  static const uint16_t JESE_ESTA_ID = 0x6864;
192 };
193 } // namespace usbpro
194 } // namespace plugin
195 } // namespace ola
196 #endif // PLUGINS_USBPRO_WIDGETDETECTORTHREAD_H_