Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UsbProDevice.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  * UsbProDevice.h
17  * A Enttec USB Pro device
18  * Copyright (C) 2006-2007 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBPRO_USBPRODEVICE_H_
22 #define PLUGINS_USBPRO_USBPRODEVICE_H_
23 
24 #include <string>
25 #include <vector>
26 #include "ola/DmxBuffer.h"
27 #include "olad/TokenBucket.h"
28 #include "olad/PluginAdaptor.h"
29 #include "olad/Port.h"
30 
31 #include "plugins/usbpro/EnttecUsbProWidget.h"
32 #include "plugins/usbpro/UsbSerialDevice.h"
33 #include "plugins/usbpro/messages/UsbProConfigMessages.pb.h"
34 
35 namespace ola {
36 namespace plugin {
37 namespace usbpro {
38 
39 using google::protobuf::RpcController;
40 using ola::plugin::usbpro::Request;
41 
42 
43 /*
44  * An Enttec Usb Pro device
45  */
47  public:
48  UsbProDevice(ola::PluginAdaptor *plugin_adaptor,
49  ola::AbstractPlugin *owner,
50  const string &name,
51  EnttecUsbProWidget *widget,
52  uint32_t serial,
53  unsigned int fps_limit);
54 
55  string DeviceId() const { return m_serial; }
56 
57  void Configure(RpcController *controller,
58  const string &request,
59  string *response,
60  google::protobuf::Closure *done);
61 
62  bool AllowMultiPortPatching() const { return true; }
63 
64  protected:
65  void PrePortStop();
66 
67  private:
68  struct PortParams {
69  bool got_parameters;
70  uint8_t break_time;
71  uint8_t mab_time;
72  uint8_t rate;
73  };
74 
75  void UpdateParams(unsigned int port_id, bool status,
76  const usb_pro_parameters &params);
77 
78  void HandleParametersRequest(RpcController *controller,
79  const Request *request,
80  string *response,
81  google::protobuf::Closure *done);
82 
83  void HandleParametersResponse(RpcController *controller,
84  string *response,
85  google::protobuf::Closure *done,
86  unsigned int port_id,
87  bool status,
88  const usb_pro_parameters &params);
89 
90  void HandleSerialRequest(RpcController *controller,
91  const Request *request,
92  string *response,
93  google::protobuf::Closure *done);
94 
95  void HandlePortAssignmentRequest(RpcController *controller,
96  const Request *request,
97  string *response,
98  google::protobuf::Closure *done);
99 
100  void HandlePortAssignmentResponse(RpcController *controller,
101  string *response,
102  google::protobuf::Closure *done,
103  bool status,
104  uint8_t port1_assignment,
105  uint8_t port2_assignment);
106 
107  static string SerialToString(uint32_t serial);
108 
109  EnttecUsbProWidget *m_pro_widget;
110  string m_serial;
111  vector<PortParams> m_port_params;
112 };
113 
114 
115 /*
116  * The Input port
117  */
119  public:
120  // The EnttecPort is owner by the caller.
122  EnttecPort *port,
123  unsigned int id,
124  ola::PluginAdaptor *plugin_adaptor,
125  const string &serial)
126  : BasicInputPort(parent, id, plugin_adaptor),
127  m_serial(serial),
128  m_port(port) {}
129 
130  const DmxBuffer &ReadDMX() const {
131  return m_port->FetchDMX();
132  }
133 
134  string Description() const { return "Serial #: " + m_serial; }
135 
136  private:
137  const string m_serial;
138  EnttecPort *m_port;
139 };
140 
141 
142 /*
143  * The output port
144  */
146  public:
147  // The EnttecPort is owner by the caller.
149  EnttecPort *port,
150  unsigned int id,
151  const string &serial,
152  const TimeStamp *wake_time,
153  unsigned int max_burst,
154  unsigned int rate)
155  : BasicOutputPort(parent, id, true, true),
156  m_serial(serial),
157  m_port(port),
158  m_bucket(max_burst, rate, max_burst, *wake_time),
159  m_wake_time(wake_time) {}
160 
161  bool WriteDMX(const DmxBuffer &buffer, uint8_t) {
162  if (m_bucket.GetToken(*m_wake_time))
163  return m_port->SendDMX(buffer);
164  else
165  OLA_INFO << "Port rated limited, dropping frame";
166  return true;
167  }
168 
169  void PostSetUniverse(Universe*, Universe *new_universe) {
170  if (!new_universe)
171  m_port->ChangeToReceiveMode(false);
172  }
173 
174  void SendRDMRequest(const ola::rdm::RDMRequest *request,
175  ola::rdm::RDMCallback *callback) {
176  m_port->SendRDMRequest(request, callback);
177  }
178 
179  void RunFullDiscovery(ola::rdm::RDMDiscoveryCallback *callback) {
180  m_port->RunFullDiscovery(callback);
181  }
182 
183  void RunIncrementalDiscovery(ola::rdm::RDMDiscoveryCallback *callback) {
184  m_port->RunIncrementalDiscovery(callback);
185  }
186 
187  string Description() const { return "Serial #: " + m_serial; }
188 
189  private:
190  const string m_serial;
191  EnttecPort *m_port;
192  TokenBucket m_bucket;
193  const TimeStamp *m_wake_time;
194 };
195 } // namespace usbpro
196 } // namespace plugin
197 } // namespace ola
198 #endif // PLUGINS_USBPRO_USBPRODEVICE_H_