Open Lighting Architecture
 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 <istream>
35 #include <map>
36 #include <string>
37 #include <vector>
38 
39 namespace ola {
40 namespace rdm {
41 
42 using std::map;
43 using std::vector;
44 using std::string;
46 
47 class PidStore;
48 class PidDescriptor;
49 
53 class RootPidStore {
54  public:
55  typedef map<uint16_t, const PidStore*> ManufacturerMap;
56 
57  RootPidStore(const PidStore *esta_store,
58  const ManufacturerMap &manufacturer_stores,
59  uint64_t version = 0)
60  : m_esta_store(esta_store),
61  m_manufacturer_store(manufacturer_stores),
62  m_version(version) {
63  }
64  ~RootPidStore();
65 
66  // Seconds since epoch in UTC
67  uint64_t Version() const { return m_version; }
68 
69  // This holds the ESTA PIDs
70  const PidStore *EstaStore() const {
71  return m_esta_store;
72  }
73 
74  // Holds Manufacturer PID data
75  const PidStore *ManufacturerStore(uint16_t esta_id) const;
76 
77  // Lookup Descriptors by name
78  const PidDescriptor *GetDescriptor(const string &pid_name) const;
79  const PidDescriptor *GetDescriptor(const string &pid_name,
80  uint16_t manufacturer_id) const;
81  // Lookup Descriptors by pid value
82  const PidDescriptor *GetDescriptor(uint16_t pid_value) const;
83  const PidDescriptor *GetDescriptor(uint16_t pid_value,
84  uint16_t manufacturer_id) const;
85 
86  // Load a RootPidStore from a file
87  static const RootPidStore *LoadFromFile(const std::string &file,
88  bool validate = true);
89 
90  // Load a RootPidStore from a directory
91  static const RootPidStore *LoadFromDirectory(const std::string &directory,
92  bool validate = true);
93 
94  private:
95  const PidStore *m_esta_store;
96  ManufacturerMap m_manufacturer_store;
97  uint64_t m_version;
98 
99  RootPidStore(const RootPidStore&);
100  RootPidStore& operator=(const RootPidStore&);
101  const PidDescriptor *InternalESTANameLookup(const string &pid_name) const;
102  void CleanStore();
103 };
104 
105 
109 class PidStore {
110  public:
111  explicit PidStore(const vector<const PidDescriptor*> &pids);
112  ~PidStore();
113 
114  unsigned int PidCount() const { return m_pid_by_value.size(); }
115  void AllPids(vector<const PidDescriptor*> *pids) const;
116  const PidDescriptor *LookupPID(uint16_t pid_value) const;
117  const PidDescriptor *LookupPID(const string &pid_name) const;
118 
119  private:
120  typedef map<uint16_t, const PidDescriptor*> PidMap;
121  typedef map<string, const PidDescriptor*> PidNameMap;
122  PidMap m_pid_by_value;
123  PidNameMap m_pid_by_name;
124 
125  PidStore(const PidStore&);
126  PidStore& operator=(const PidStore&);
127 };
128 
129 
135  public:
136  // TODO(simon): use the enums from the Pids.proto instead of duplicating
137  // here.
138  typedef enum {
139  ROOT_DEVICE, // 0 only
140  ANY_SUB_DEVICE, // 0 - 512 or ALL_DEVICES
141  NON_BROADCAST_SUB_DEVICE, // 0 - 512
142  SPECIFIC_SUB_DEVICE, // 1- 512
143  } sub_device_valiator;
144 
145  PidDescriptor(const string &name,
146  uint16_t value,
147  const Descriptor *get_request,
148  const Descriptor *get_response,
149  const Descriptor *set_request,
150  const Descriptor *set_response,
151  sub_device_valiator get_sub_device_range,
152  sub_device_valiator set_sub_device_range)
153  : m_name(name),
154  m_pid_value(value),
155  m_get_request(get_request),
156  m_get_response(get_response),
157  m_set_request(set_request),
158  m_set_response(set_response),
159  m_get_subdevice_range(get_sub_device_range),
160  m_set_subdevice_range(set_sub_device_range) {
161  }
162  ~PidDescriptor();
163 
164  const string &Name() const { return m_name; }
165  uint16_t Value() const { return m_pid_value; }
166  const Descriptor *GetRequest() const { return m_get_request; }
167  const Descriptor *GetResponse() const { return m_get_response; }
168  const Descriptor *SetRequest() const { return m_set_request; }
169  const Descriptor *SetResponse() const { return m_set_response; }
170 
171  bool IsGetValid(uint16_t sub_device) const;
172  bool IsSetValid(uint16_t sub_device) const;
173 
174  private:
175  const string m_name;
176  uint16_t m_pid_value;
177  const Descriptor *m_get_request;
178  const Descriptor *m_get_response;
179  const Descriptor *m_set_request;
180  const Descriptor *m_set_response;
181  sub_device_valiator m_get_subdevice_range;
182  sub_device_valiator m_set_subdevice_range;
183 
185  PidDescriptor& operator=(const PidDescriptor&);
186 
187  bool RequestValid(uint16_t sub_device,
188  const sub_device_valiator &validator) const;
189 };
190 } // namespace rdm
191 } // namespace ola
192 #endif // INCLUDE_OLA_RDM_PIDSTORE_H_