Open Lighting Architecture  0.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Device.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  * Device.h
17  * Header file for the Device class
18  * Copyright (C) 2005 Simon Newton
19  */
20 
21 #ifndef INCLUDE_OLAD_DEVICE_H_
22 #define INCLUDE_OLAD_DEVICE_H_
23 
24 #include <ola/base/Macro.h>
25 #include <olad/Port.h>
26 #include <stdint.h>
27 #include <map>
28 #include <string>
29 #include <vector>
30 
31 namespace ola {
32 namespace rpc {
33  class RpcController;
34 }
35 }
36 
37 namespace ola {
38 
39 class AbstractPlugin;
40 
41 /*
42  * The interface for a Device
43  */
45  public:
47 
48  AbstractDevice() {}
49  virtual ~AbstractDevice() {}
50 
51  // return the name of this device
52  virtual const std::string Name() const = 0;
53  // return the plugin that owns this device
54  virtual AbstractPlugin *Owner() const = 0;
55 
56  // return the a unique id of this device, this is guaranteed to be unique
57  // and persist across restarts.
58  virtual std::string UniqueId() const = 0;
59 
60  // stop the device
61  virtual bool Stop() = 0;
62 
63  // allow input and output ports to be patched to the same univese
64  virtual bool AllowLooping() const = 0;
65 
66  // allow multiple ports of the same type to be patched to the same
67  // universe.
68  virtual bool AllowMultiPortPatching() const = 0;
69 
70  // Fetch a list of all ports in this device
71  virtual void InputPorts(std::vector<InputPort*> *ports) const = 0;
72  virtual void OutputPorts(std::vector<OutputPort*> *ports) const = 0;
73 
74  // Lookup a particular port in this device
75  virtual InputPort *GetInputPort(unsigned int port_id) const = 0;
76  virtual OutputPort *GetOutputPort(unsigned int port_id) const = 0;
77 
78  // configure this device
79  virtual void Configure(ola::rpc::RpcController *controller,
80  const std::string &request,
81  std::string *response,
82  ConfigureCallback *done) = 0;
83 };
84 
85 
86 /*
87  * A partial implementation of a Device.
88  */
89 class Device: public AbstractDevice {
90  public:
91  Device(AbstractPlugin *owner, const std::string &name);
92  virtual ~Device();
93 
94  const std::string Name() const { return m_name; }
95  void SetName(const std::string &name) { m_name = name; }
96 
97  AbstractPlugin *Owner() const { return m_owner; }
98  std::string UniqueId() const;
99 
100  // Returns an id which is unique within the plugin
101  virtual std::string DeviceId() const = 0;
102 
103  bool IsEnabled() const { return m_enabled; }
104 
105  bool Start();
106  bool Stop();
107 
108  // sane defaults
109  bool AllowLooping() const { return false; }
110  bool AllowMultiPortPatching() const { return false; }
111 
112  bool AddPort(InputPort *port);
113  bool AddPort(OutputPort *port);
114  void InputPorts(std::vector<InputPort*> *ports) const;
115  void OutputPorts(std::vector<OutputPort*> *ports) const;
116 
117  InputPort *GetInputPort(unsigned int port_id) const;
118  OutputPort *GetOutputPort(unsigned int port_id) const;
119 
120  // Free all ports
121  void DeleteAllPorts();
122 
123  // Handle a Configure request
124  virtual void Configure(ola::rpc::RpcController *controller,
125  const std::string &request,
126  std::string *response,
127  ConfigureCallback *done);
128 
129  protected:
130  virtual bool StartHook() { return true; }
131  virtual void PrePortStop() {}
132  virtual void PostPortStop() {}
133 
134  private:
135  typedef std::map<unsigned int, InputPort*> input_port_map;
136  typedef std::map<unsigned int, OutputPort*> output_port_map;
137 
138  bool m_enabled;
139  AbstractPlugin *m_owner; // which plugin owns this device
140  std::string m_name; // device name
141  mutable std::string m_unique_id; // device id
142  input_port_map m_input_ports;
143  output_port_map m_output_ports;
144 
145  template<class PortClass>
146  bool GenericAddPort(PortClass *port,
147  std::map<unsigned int, PortClass*> *ports);
148 
149  template <class PortClass>
150  void GenericDeletePort(PortClass *p);
151 
152  DISALLOW_COPY_AND_ASSIGN(Device);
153 };
154 } // namespace ola
155 #endif // INCLUDE_OLAD_DEVICE_H_