Open Lighting Architecture  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PidStore.h
Go to the documentation of this file.
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  * PidStore.h
17  * Holds information about RDM PIDs.
18  * Copyright (C) 2011 Simon Newton
19  */
20 
29 #ifndef INCLUDE_OLA_RDM_PIDSTORE_H_
30 #define INCLUDE_OLA_RDM_PIDSTORE_H_
31 
32 #include <stdint.h>
33 #include <ola/messaging/Descriptor.h>
34 #include <ola/base/Macro.h>
35 #include <istream>
36 #include <map>
37 #include <memory>
38 #include <string>
39 #include <vector>
40 
41 namespace ola {
42 namespace rdm {
43 
44 class PidStore;
45 class PidDescriptor;
46 
47 // The following % before Device is to stop Doxygen interpretting it as a class
64 class RootPidStore {
65  public:
66  typedef std::map<uint16_t, const PidStore*> ManufacturerMap;
67 
74  RootPidStore(const PidStore *esta_store,
75  const ManufacturerMap &manufacturer_stores,
76  uint64_t version = 0)
77  : m_esta_store(esta_store),
78  m_manufacturer_store(manufacturer_stores),
79  m_version(version) {
80  }
81 
82  ~RootPidStore();
83 
89  uint64_t Version() const { return m_version; }
90 
96  const PidStore *EstaStore() const {
97  return m_esta_store.get();
98  }
99 
106  const PidStore *ManufacturerStore(uint16_t esta_id) const;
107 
113  const PidDescriptor *GetDescriptor(const std::string &pid_name) const;
114 
122  const PidDescriptor *GetDescriptor(const std::string &pid_name,
123  uint16_t manufacturer_id) const;
124 
130  const PidDescriptor *GetDescriptor(uint16_t pid_value) const;
131 
139  const PidDescriptor *GetDescriptor(uint16_t pid_value,
140  uint16_t manufacturer_id) const;
141 
148  static const RootPidStore *LoadFromFile(const std::string &file,
149  bool validate = true);
150 
158  static const RootPidStore *LoadFromDirectory(const std::string &directory,
159  bool validate = true);
160 
165  static const std::string DataLocation();
166 
167  private:
168  std::auto_ptr<const PidStore> m_esta_store;
169  ManufacturerMap m_manufacturer_store;
170  uint64_t m_version;
171 
172  const PidDescriptor *InternalESTANameLookup(
173  const std::string &pid_name) const;
174 
175  DISALLOW_COPY_AND_ASSIGN(RootPidStore);
176 };
177 
178 
182 class PidStore {
183  public:
192  explicit PidStore(const std::vector<const PidDescriptor*> &pids);
193 
197  ~PidStore();
198 
203  unsigned int PidCount() const { return m_pid_by_value.size(); }
204 
212  void AllPids(std::vector<const PidDescriptor*> *pids) const;
213 
219  const PidDescriptor *LookupPID(uint16_t pid_value) const;
220 
226  const PidDescriptor *LookupPID(const std::string &pid_name) const;
227 
228  private:
229  typedef std::map<uint16_t, const PidDescriptor*> PidMap;
230  typedef std::map<std::string, const PidDescriptor*> PidNameMap;
231  PidMap m_pid_by_value;
232  PidNameMap m_pid_by_name;
233 
234  DISALLOW_COPY_AND_ASSIGN(PidStore);
235 };
236 
237 
243  public:
244  // TODO(simon): use the enums from the Pids.proto instead of duplicating
245  // here.
246  typedef enum {
247  ROOT_DEVICE, // 0 only
248  ANY_SUB_DEVICE, // 0 - 512 or ALL_DEVICES
249  NON_BROADCAST_SUB_DEVICE, // 0 - 512
250  SPECIFIC_SUB_DEVICE, // 1- 512
251  } sub_device_validator;
252 
253  PidDescriptor(const std::string &name,
254  uint16_t value,
255  const ola::messaging::Descriptor *get_request,
256  const ola::messaging::Descriptor *get_response,
257  const ola::messaging::Descriptor *set_request,
258  const ola::messaging::Descriptor *set_response,
259  sub_device_validator get_sub_device_range,
260  sub_device_validator set_sub_device_range)
261  : m_name(name),
262  m_pid_value(value),
263  m_get_request(get_request),
264  m_get_response(get_response),
265  m_set_request(set_request),
266  m_set_response(set_response),
267  m_get_subdevice_range(get_sub_device_range),
268  m_set_subdevice_range(set_sub_device_range) {
269  }
270  ~PidDescriptor();
271 
272  const std::string &Name() const { return m_name; }
273  uint16_t Value() const { return m_pid_value; }
274  const ola::messaging::Descriptor *GetRequest() const { return m_get_request; }
275  const ola::messaging::Descriptor *GetResponse() const {
276  return m_get_response;
277  }
278  const ola::messaging::Descriptor *SetRequest() const { return m_set_request; }
279  const ola::messaging::Descriptor *SetResponse() const {
280  return m_set_response;
281  }
282 
283  bool IsGetValid(uint16_t sub_device) const;
284  bool IsSetValid(uint16_t sub_device) const;
285 
286  private:
287  const std::string m_name;
288  uint16_t m_pid_value;
289  const ola::messaging::Descriptor *m_get_request;
290  const ola::messaging::Descriptor *m_get_response;
291  const ola::messaging::Descriptor *m_set_request;
292  const ola::messaging::Descriptor *m_set_response;
293  sub_device_validator m_get_subdevice_range;
294  sub_device_validator m_set_subdevice_range;
295 
296  bool RequestValid(uint16_t sub_device,
297  const sub_device_validator &validator) const;
298 
299  DISALLOW_COPY_AND_ASSIGN(PidDescriptor);
300 };
301 } // namespace rdm
302 } // namespace ola
303 #endif // INCLUDE_OLA_RDM_PIDSTORE_H_