Open Lighting Architecture  0.9.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ResponderSettings.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * ResponderSettings.h
17  * Copyright (C) 2013 Simon Newton
18  */
19 
20 #ifndef INCLUDE_OLA_RDM_RESPONDERSETTINGS_H_
21 #define INCLUDE_OLA_RDM_RESPONDERSETTINGS_H_
22 
23 #include <ola/base/Macro.h>
24 #include <ola/rdm/RDMCommand.h>
26 #include <stdint.h>
27 #include <string>
28 #include <vector>
29 
30 namespace ola {
31 namespace rdm {
32 
37  public:
38  virtual ~SettingInterface() {}
39 
44  virtual std::string Description() const = 0;
45 
49  virtual unsigned int DescriptionResponseSize() const = 0;
50 
56  virtual unsigned int GenerateDescriptionResponse(uint8_t index,
57  uint8_t *data) const = 0;
58 };
59 
64  public:
65  typedef const char* ArgType;
66 
71  explicit BasicSetting(const ArgType description);
72 
77  std::string Description() const { return m_description; }
78 
79  unsigned int DescriptionResponseSize() const {
80  return sizeof(description_s);
81  }
82 
83  unsigned int GenerateDescriptionResponse(uint8_t index,
84  uint8_t *data) const;
85 
86  private:
87  PACK(
88  struct description_s {
89  uint8_t setting;
90  char description[MAX_RDM_STRING_LENGTH];
91  });
92 
93  std::string m_description;
94 };
95 
96 
102  public:
107  uint32_t frequency;
108  const char *description;
109  };
110 
112 
117  explicit FrequencyModulationSetting(const ArgType &arg);
118 
123  std::string Description() const { return m_description; }
124 
128  uint32_t Frequency() const { return m_frequency; }
129 
130  unsigned int DescriptionResponseSize() const {
131  return sizeof(description_s);
132  }
133 
134  unsigned int GenerateDescriptionResponse(uint8_t index,
135  uint8_t *data) const;
136 
137  private:
138  PACK(
139  struct description_s {
140  uint8_t setting;
141  uint32_t frequency;
142  char description[MAX_RDM_STRING_LENGTH];
143  });
144 
145  uint32_t m_frequency;
146  std::string m_description;
147 };
148 
149 
158 template <class SettingType>
160  public:
166  SettingCollection(const typename SettingType::ArgType args[],
167  unsigned int arg_count,
168  bool zero_offset = false)
169  : m_zero_offset(zero_offset) {
170  for (unsigned int i = 0; i < arg_count; i++) {
171  m_settings.push_back(SettingType(args[i]));
172  }
173  }
174 
175  uint8_t Count() const { return m_settings.size(); }
176 
177  const SettingType *Lookup(uint8_t index) const {
178  if (index > m_settings.size()) {
179  return NULL;
180  }
181  return &m_settings[index];
182  }
183 
184  unsigned int Offset() const {
185  return m_zero_offset ? 0 : 1;
186  }
187 
188  protected:
189  SettingCollection() {}
190 
191  private:
192  std::vector<SettingType> m_settings;
193  const bool m_zero_offset;
194 };
195 
196 
200 template <class SettingType>
202  public:
203  explicit SettingManager(const SettingCollection<SettingType> *settings)
204  : m_settings(settings),
205  m_current_setting(settings->Offset()) {
206  }
207 
208  virtual ~SettingManager() {}
209 
210  const RDMResponse *Get(const RDMRequest *request) const;
211  const RDMResponse *Set(const RDMRequest *request);
212  const RDMResponse *GetDescription(const RDMRequest *request) const;
213 
214  uint8_t Count() const {
215  return m_settings->Count();
216  }
217 
218  uint8_t CurrentSetting() const {
219  return m_current_setting + m_settings->Offset();
220  }
221 
222  bool ChangeSetting(uint8_t state);
223 
224  private:
225  const SettingCollection<SettingType> *m_settings;
226  uint8_t m_current_setting;
227 };
228 
231 
232 template <class SettingType>
234  const RDMRequest *request) const {
235  uint16_t data = ((m_current_setting + m_settings->Offset()) << 8 |
236  m_settings->Count());
237  if (m_settings->Offset() == 0) {
238  // don't count the 0-state
239  data--;
240  }
241  return ResponderHelper::GetUInt16Value(request, data);
242 }
243 
244 template <class SettingType>
245 const RDMResponse *SettingManager<SettingType>::Set(
246  const RDMRequest *request) {
247  uint8_t arg;
248  if (!ResponderHelper::ExtractUInt8(request, &arg)) {
249  return NackWithReason(request, NR_FORMAT_ERROR);
250  }
251 
252  unsigned int offset = m_settings->Offset();
253  if (arg < offset || arg >= m_settings->Count() + offset) {
254  return NackWithReason(request, NR_DATA_OUT_OF_RANGE);
255  } else {
256  m_current_setting = arg - offset;
257  return ResponderHelper::EmptySetResponse(request);
258  }
259 }
260 
261 template <class SettingType>
262 const RDMResponse *SettingManager<SettingType>::GetDescription(
263  const RDMRequest *request) const {
264  uint8_t arg;
265  if (!ResponderHelper::ExtractUInt8(request, &arg)) {
266  return NackWithReason(request, NR_FORMAT_ERROR);
267  }
268 
269  unsigned int offset = m_settings->Offset();
270  // never reply for the first setting - see LOCK_STATE
271  if (arg == 0 || arg >= m_settings->Count() + offset) {
272  return NackWithReason(request, NR_DATA_OUT_OF_RANGE);
273  } else {
274  const SettingType *setting = m_settings->Lookup(arg - offset);
275  uint8_t output[setting->DescriptionResponseSize()]; // NOLINT
276  unsigned int size = setting->GenerateDescriptionResponse(arg, output);
277  return GetResponseFromData(request, output, size, RDM_ACK);
278  }
279 }
280 
281 template <class SettingType>
282 bool SettingManager<SettingType>::ChangeSetting(uint8_t new_setting) {
283  uint8_t offset = m_settings->Offset();
284 
285  if (new_setting < offset || new_setting >= m_settings->Count() + offset)
286  return false;
287 
288  m_current_setting = new_setting - offset;
289  return true;
290 }
291 } // namespace rdm
292 } // namespace ola
293 #endif // INCLUDE_OLA_RDM_RESPONDERSETTINGS_H_