Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OlaClientCore.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library 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 GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * OlaClientCore.h
17  * The OLA Client Core class
18  * Copyright (C) 2005-2008 Simon Newton
19  */
20 
21 #ifndef OLA_OLACLIENTCORE_H_
22 #define OLA_OLACLIENTCORE_H_
23 
24 #include <google/protobuf/stubs/common.h>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include "common/protocol/Ola.pb.h"
30 #include "common/rpc/SimpleRpcController.h"
31 #include "common/rpc/StreamRpcChannel.h"
32 #include "ola/Callback.h"
33 #include "ola/DmxBuffer.h"
34 #include "ola/OlaCallbackClient.h"
35 #include "ola/OlaDevice.h"
36 #include "ola/common.h"
37 #include "ola/network/Socket.h"
38 #include "ola/plugin_id.h"
40 #include "ola/rdm/UID.h"
41 #include "ola/rdm/UIDSet.h"
42 #include "ola/timecode/TimeCode.h"
43 
44 namespace ola {
45 
46 class OlaClientCoreServiceImpl;
47 
48 using std::string;
52 
53 class OlaClientCore: public ola::proto::OlaClientService {
54  public:
57 
58  typedef Callback4<void, unsigned int, uint8_t, const DmxBuffer&,
59  const string&> DmxCallbackWithPriority;
60 
61  explicit OlaClientCore(ConnectedDescriptor *descriptor);
62  ~OlaClientCore();
63 
64  bool Setup();
65  bool Stop();
66 
68 
69  // plugin methods
70  bool FetchPluginList(
71  SingleUseCallback2<void,
72  const vector<class OlaPlugin>&,
73  const string&> *callback);
74 
75  bool FetchPluginDescription(
76  ola_plugin_id plugin_id,
78 
79  bool FetchPluginState(ola_plugin_id plugin_id,
81 
82  // device methods
83  bool FetchDeviceInfo(
84  ola_plugin_id filter,
85  SingleUseCallback2<void,
86  const vector <class OlaDevice>&,
87  const string&> *callback);
88 
89  bool FetchCandidatePorts(
90  SingleUseCallback2<void,
91  const vector <class OlaDevice>&,
92  const string&> *callback);
93 
94  bool FetchCandidatePorts(
95  unsigned int universe_id,
96  SingleUseCallback2<void,
97  const vector <class OlaDevice>&,
98  const string&> *callback);
99 
100  bool ConfigureDevice(
101  unsigned int device_alias,
102  const string &msg,
104 
105  // port methods
106  bool SetPortPriorityInherit(
107  unsigned int device_alias,
108  unsigned int port,
109  PortDirection port_direction,
111  bool SetPortPriorityOverride(
112  unsigned int device_alias,
113  unsigned int port,
114  PortDirection port_direction,
115  uint8_t value,
117 
118  // universe methods
119  bool FetchUniverseList(
120  SingleUseCallback2<void,
121  const vector <class OlaUniverse>&,
122  const string &> *callback);
123  bool FetchUniverseInfo(
124  unsigned int universe,
125  SingleUseCallback2<void,
126  class OlaUniverse&,
127  const string &> *callback);
128  bool SetUniverseName(
129  unsigned int uni,
130  const string &name,
132  bool SetUniverseMergeMode(
133  unsigned int uni,
134  OlaUniverse::merge_mode mode,
136 
137  // patching
138  bool Patch(
139  unsigned int device_alias,
140  unsigned int port,
141  ola::PortDirection port_direction,
142  ola::PatchAction action,
143  unsigned int uni,
145 
146  // dmx methods
147  void SetDmxCallback(DmxCallback *callback);
148  void SetDmxCallback(DmxCallbackWithPriority *callback);
149 
150  bool RegisterUniverse(
151  unsigned int universe,
152  ola::RegisterAction register_action,
154  bool SendDmx(
155  unsigned int universe,
156  const DmxBuffer &data,
158  bool SendDmx(
159  unsigned int universe,
160  const DmxBuffer &data,
162  bool SendDmx(unsigned int universe, const DmxBuffer &data);
163  bool FetchDmx(
164  unsigned int universe,
166 
167  // rdm methods
168  bool FetchUIDList(
169  unsigned int universe,
170  SingleUseCallback2<void,
171  const ola::rdm::UIDSet&,
172  const string&> *callback);
173  bool RunDiscovery(
174  unsigned int universe,
175  bool full,
177  const ola::rdm::UIDSet&,
178  const string&> *callback);
179  bool SetSourceUID(const ola::rdm::UID &uid,
181 
182  bool RDMGet(ola::rdm::RDMAPIImplInterface::rdm_callback *callback,
183  unsigned int universe,
184  const ola::rdm::UID &uid,
185  uint16_t sub_device,
186  uint16_t pid,
187  const uint8_t *data,
188  unsigned int data_length);
190  unsigned int universe,
191  const ola::rdm::UID &uid,
192  uint16_t sub_device,
193  uint16_t pid,
194  const uint8_t *data,
195  unsigned int data_length);
196  bool RDMSet(ola::rdm::RDMAPIImplInterface::rdm_callback *callback,
197  unsigned int universe,
198  const ola::rdm::UID &uid,
199  uint16_t sub_device,
200  uint16_t pid,
201  const uint8_t *data,
202  unsigned int data_length);
204  unsigned int universe,
205  const ola::rdm::UID &uid,
206  uint16_t sub_device,
207  uint16_t pid,
208  const uint8_t *data,
209  unsigned int data_length);
210 
211  // timecode
213  const ola::timecode::TimeCode &timecode);
214 
215  /*
216  * This is called by the channel when new DMX data turns up
217  */
218  void UpdateDmxData(::google::protobuf::RpcController* controller,
219  const ola::proto::DmxData* request,
220  ola::proto::Ack* response,
221  ::google::protobuf::Closure* done);
222 
223  // unfortunately all of these need to be public because they're used in the
224  // closures. That's why this class is wrapped in OlaClient or
225  // OlaCallbackClient.
226  typedef struct {
227  SimpleRpcController *controller;
228  ola::proto::PluginListReply *reply;
229  SingleUseCallback2<void,
230  const vector<class OlaPlugin>&,
231  const string&> *callback;
232  } plugin_list_arg;
233 
234  void HandlePluginList(plugin_list_arg *args);
235 
236  typedef struct {
237  SimpleRpcController *controller;
238  ola::proto::PluginDescriptionReply *reply;
241 
242  void HandlePluginDescription(plugin_description_arg *args);
243 
244  typedef struct {
245  SimpleRpcController *controller;
246  ola::proto::PluginStateReply *reply;
249 
250  void HandlePluginState(plugin_state_arg *args);
251 
252  typedef struct {
253  SimpleRpcController *controller;
254  ola::proto::DeviceInfoReply *reply;
255  SingleUseCallback2<void,
256  const vector <class OlaDevice> &,
257  const string &> *callback;
258  } device_info_arg;
259 
260  void HandleDeviceInfo(device_info_arg *arg);
261 
262  typedef struct {
263  SimpleRpcController *controller;
264  ola::proto::DeviceConfigReply *reply;
267 
268  void HandleDeviceConfig(configure_device_args *arg);
269 
270  typedef struct {
271  SimpleRpcController *controller;
272  ola::proto::Ack *reply;
274  } ack_args;
275 
276  void HandleAck(ack_args *args);
277 
278  typedef struct {
279  SimpleRpcController *controller;
280  ola::proto::UniverseInfoReply *reply;
281  SingleUseCallback2<void,
282  const vector <class OlaUniverse>&,
283  const string&> *callback;
285 
286  void HandleUniverseList(universe_list_args *args);
287 
288  typedef struct {
289  SimpleRpcController *controller;
290  ola::proto::UniverseInfoReply *reply;
293 
294  void HandleUniverseInfo(universe_info_args *args);
295 
296  typedef struct {
297  SimpleRpcController *controller;
298  ola::proto::DmxData *reply;
300  } get_dmx_args;
301 
302  void HandleGetDmx(get_dmx_args *args);
303 
304  typedef struct {
305  SimpleRpcController *controller;
306  ola::proto::UIDListReply *reply;
307  SingleUseCallback2<void,
308  const ola::rdm::UIDSet&,
309  const string&> *callback;
310  } uid_list_args;
311 
312  void HandleUIDList(uid_list_args *args);
313 
314  typedef struct {
316  SimpleRpcController *controller;
317  ola::proto::RDMResponse *reply;
319 
320  void HandleRDM(rdm_response_args *args);
321 
322  typedef struct {
324  SimpleRpcController *controller;
325  ola::proto::RDMResponse *reply;
327 
329 
330  private:
332  OlaClientCore operator=(const OlaClientCore&);
333 
334  bool GenericSendDmx(
335  unsigned int universe,
336  const DmxBuffer &data,
338 
339  bool GenericFetchCandidatePorts(
340  unsigned int universe_id,
341  bool include_universe,
342  SingleUseCallback2<void,
343  const vector <class OlaDevice>&,
344  const string&> *callback);
345 
346  bool RDMCommand(ola::rdm::RDMAPIImplInterface::rdm_callback *callback,
347  bool is_set,
348  unsigned int universe,
349  const ola::rdm::UID &uid,
350  uint16_t sub_device,
351  uint16_t pid,
352  const uint8_t *data,
353  unsigned int data_length);
354  bool RDMCommandWithPid(
356  bool is_set,
357  unsigned int universe,
358  const ola::rdm::UID &uid,
359  uint16_t sub_device,
360  uint16_t pid,
361  const uint8_t *data,
362  unsigned int data_length);
363 
364  void CheckRDMResponseStatus(SimpleRpcController *controller,
365  ola::proto::RDMResponse *reply,
366  ola::rdm::ResponseStatus *new_status);
367 
368  void GetParamFromReply(
369  const string &message_type,
370  ola::proto::RDMResponse *reply,
371  ola::rdm::ResponseStatus *new_status);
372 
373  void UpdateResponseAckData(
374  ola::proto::RDMResponse *reply,
375  ola::rdm::ResponseStatus *new_status);
376 
377  template <typename arg_type, typename reply_type, typename callback_type>
378  arg_type *NewArgs(
379  SimpleRpcController *controller,
380  reply_type reply,
381  callback_type callback);
382 
383  template <typename arg_type>
384  void FreeArgs(arg_type *args);
385 
386  ConnectedDescriptor *m_descriptor;
387  std::auto_ptr<DmxCallback> m_dmx_callback;
388  std::auto_ptr<DmxCallbackWithPriority> m_dmx_callback_with_priority;
389  StreamRpcChannel *m_channel;
390  ola::proto::OlaServerService_Stub *m_stub;
391  int m_connected;
392 };
393 
394 
398 template <typename arg_type, typename reply_type, typename callback_type>
399 arg_type *OlaClientCore::NewArgs(
400  SimpleRpcController *controller,
401  reply_type reply,
402  callback_type callback) {
403  arg_type *args = new arg_type();
404  args->controller = controller;
405  args->reply = reply;
406  args->callback = callback;
407  return args;
408 }
409 
410 
414 template <typename arg_type>
415 void OlaClientCore::FreeArgs(arg_type *args) {
416  delete args->controller;
417  delete args->reply;
418  delete args;
419 }
420 } // namespace ola
421 #endif // OLA_OLACLIENTCORE_H_