Open Lighting Architecture  0.10.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
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  // Used to release a widget. Should be called from the thread running the
97  // SelectServerInterface that was passed to the constructor.
98  void FreeWidget(SerialWidgetInterface *widget);
99 
100  // blocks until the thread is running
101  void WaitUntilRunning();
102 
103  protected:
104  virtual bool RunScan();
105  void PerformDiscovery(const std::string &path,
106  ola::io::ConnectedDescriptor *descriptor);
107 
108  private:
109  ola::io::SelectServerInterface *m_other_ss;
110  ola::io::SelectServer m_ss; // ss for this thread
111  std::vector<WidgetDetectorInterface*> m_widget_detectors;
112  std::string m_directory; // directory to look for widgets in
113  std::vector<std::string> m_prefixes; // prefixes to try
114  std::set<std::string> m_ignored_devices; // devices to ignore
115  NewWidgetHandler *m_handler;
116  bool m_is_running;
117  unsigned int m_usb_pro_timeout;
118  unsigned int m_robe_timeout;
119  ola::thread::Mutex m_mutex;
120  ola::thread::ConditionVariable m_condition;
121 
122  // those paths that are either in discovery, or in use
123  std::set<std::string> m_active_paths;
124  // holds the path and current widget detector offset
125  typedef std::pair<std::string, int> DescriptorInfo;
126  // map of descriptor to DescriptorInfo
127  typedef std::map<ola::io::ConnectedDescriptor*, DescriptorInfo>
128  ActiveDescriptors;
129  // the descriptors that are in the discovery process
130  ActiveDescriptors m_active_descriptors;
131 
132  // called when we find new widgets of a particular type
133  void UsbProWidgetReady(ola::io::ConnectedDescriptor *descriptor,
134  const UsbProWidgetInformation *info);
135  void RobeWidgetReady(ola::io::ConnectedDescriptor *descriptor,
136  const RobeWidgetInformation *info);
137 
138  void DescriptorFailed(ola::io::ConnectedDescriptor *descriptor);
139  void PerformNextDiscoveryStep(ola::io::ConnectedDescriptor *descriptor);
140  void InternalFreeWidget(SerialWidgetInterface *widget);
141  void FreeDescriptor(ola::io::ConnectedDescriptor *descriptor);
142 
143  template<typename WidgetType, typename InfoType>
144  void DispatchWidget(WidgetType *widget, const InfoType *information);
145 
146  // All of these are called in a separate thread.
147  template<typename WidgetType, typename InfoType>
148  void SignalNewWidget(WidgetType *widget, const InfoType *information);
149 
150  void MarkAsRunning();
151 
152  static const unsigned int SCAN_INTERVAL_MS = 20000;
153 
154  // This is how device identification is done, see
155  // https://wiki.openlighting.org/index.php/USB_Protocol_Extensions
156  // OPEN_LIGHTING_ESTA_CODE is in Constants.h
157 
158  // DmxKing Device Models
159  static const uint16_t DMX_KING_DMX512_ID = 0;
160  static const uint16_t DMX_KING_ULTRA_ID = 1;
161  static const uint16_t DMX_KING_ULTRA_PRO_ID = 2;
162  static const uint16_t DMX_KING_ULTRA_MICRO_ID = 3;
163  static const uint16_t DMX_KING_ULTRA_RDM_ID = 4;
164 
165  // Jese device models.
166  static const uint16_t JESE_DMX_TRI_MK1_ID = 1; // Original DMX-TRI
167  static const uint16_t JESE_RDM_TRI_MK1_ID = 2; // Original RDM-TRI
168  static const uint16_t JESE_RDM_TRI_MK2_ID = 3;
169  static const uint16_t JESE_RDM_TXI_MK2_ID = 4;
170  // DMX-TRI, with new hardware
171  static const uint16_t JESE_DMX_TRI_MK1_SE_ID = 5;
172 
173  // Goddard device models
174  static const uint16_t GODDARD_DMXTER4_ID = 0x444d;
175  static const uint16_t GODDARD_MINI_DMXTER4_ID = 0x4d49;
176  static const uint16_t GODDARD_DMXTER4A_ID = 0x3441;
177 
178  // Open Lighting device models
179  static const uint16_t OPEN_LIGHTING_PACKETHEADS_ID = 2;
180  static const uint16_t OPEN_LIGHTING_RGB_MIXER_ID = 1;
181 
182  // ESTA Ids
183  static const uint16_t DMX_KING_ESTA_ID = 0x6a6b;
184  static const uint16_t GODDARD_ESTA_ID = 0x4744;
185  static const uint16_t JESE_ESTA_ID = 0x6864;
186 };
187 } // namespace usbpro
188 } // namespace plugin
189 } // namespace ola
190 #endif // PLUGINS_USBPRO_WIDGETDETECTORTHREAD_H_
A BidirectionalFileDescriptor that also generates notifications when closed.
Definition: Descriptor.h:282
void FreeWidget(SerialWidgetInterface *widget)
Definition: WidgetDetectorThread.cpp:166
virtual bool RunScan()
Definition: WidgetDetectorThread.cpp:193
void * Run()
Definition: WidgetDetectorThread.cpp:115
Definition: RobeWidget.h:115
Definition: RobeWidgetDetector.h:40
The interface for the SelectServer.
Definition: SelectServerInterface.h:42
Definition: Thread.h:52
void SetIgnoredDevices(const std::vector< std::string > &devices)
Definition: WidgetDetectorThread.cpp:104
bool Join(void *ptr)
Definition: WidgetDetectorThread.cpp:157
Definition: UltraDMXProWidget.h:38
Definition: DmxterWidget.h:130
Definition: SerialWidgetInterface.h:38
Definition: ArduinoWidget.h:89
A single threaded I/O event management system.
Definition: SelectServer.h:63
Definition: WidgetDetectorThread.h:74
Definition: EnttecUsbProWidget.h:83
void SetDeviceDirectory(const std::string &directory)
Definition: WidgetDetectorThread.cpp:85
Definition: DmxTriWidget.h:207
void PerformDiscovery(const std::string &path, ola::io::ConnectedDescriptor *descriptor)
Definition: WidgetDetectorThread.cpp:230
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
WidgetDetectorThread(NewWidgetHandler *widget_handler, ola::io::SelectServerInterface *ss, unsigned int usb_pro_timeout=200, unsigned int robe_timeout=200)
Definition: WidgetDetectorThread.cpp:64
Definition: UsbProWidgetDetector.h:41
void WaitUntilRunning()
Definition: WidgetDetectorThread.cpp:181
Definition: WidgetDetectorThread.h:52
Definition: Mutex.h:35
Definition: Mutex.h:75
void SetDevicePrefixes(const std::vector< std::string > &prefixes)
Definition: WidgetDetectorThread.cpp:95