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