Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RDMHTTPModule.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  * RDMHTTPModule.h
17  * This module acts as the http -> olad gateway for RDM commands.
18  * Copyright (C) 2010 Simon Newton
19  */
20 
21 #ifndef OLAD_RDMHTTPMODULE_H_
22 #define OLAD_RDMHTTPMODULE_H_
23 
24 #include <map>
25 #include <queue>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 #include "ola/OlaCallbackClient.h"
30 #include "ola/http/HTTPServer.h"
31 #include "ola/rdm/PidStore.h"
32 #include "ola/rdm/RDMAPI.h"
33 #include "ola/rdm/UID.h"
34 #include "ola/thread/Mutex.h"
35 #include "ola/web/JsonSections.h"
36 
37 namespace ola {
38 
42 using ola::rdm::UID;
43 using std::string;
44 
45 
46 /*
47  * The module that deals with RDM requests.
48  */
50  public:
51  RDMHTTPModule(HTTPServer *http_server,
52  class OlaCallbackClient *client);
53  ~RDMHTTPModule();
54 
55  void SetPidStore(const ola::rdm::RootPidStore *pid_store);
56 
57  int RunRDMDiscovery(const HTTPRequest *request, HTTPResponse *response);
58 
59  int JsonUIDs(const HTTPRequest *request, HTTPResponse *response);
60 
61  // these are used by the RDM Patcher
62  int JsonUIDInfo(const HTTPRequest *request, HTTPResponse *response);
63  int JsonUIDIdentifyMode(const HTTPRequest *request,
64  HTTPResponse *response);
65  int JsonUIDPersonalities(const HTTPRequest *request,
66  HTTPResponse *response);
67 
68  // these are used by the RDM Attributes Panel
69  int JsonSupportedPIDs(const HTTPRequest *request, HTTPResponse *response);
70  int JsonSupportedSections(const HTTPRequest *request,
71  HTTPResponse *response);
72  int JsonSectionInfo(const HTTPRequest *request,
73  HTTPResponse *response);
74  int JsonSaveSectionInfo(const HTTPRequest *request,
75  HTTPResponse *response);
76 
77  void PruneUniverseList(const vector<OlaUniverse> &universes);
78 
79  private:
80  typedef struct {
81  string manufacturer;
82  string device;
83  bool active;
84  } resolved_uid;
85 
86  typedef enum {
87  RESOLVE_MANUFACTURER,
88  RESOLVE_DEVICE,
89  } uid_resolve_action;
90 
91  typedef struct {
92  map<UID, resolved_uid> resolved_uids;
93  std::queue<std::pair<UID, uid_resolve_action> > pending_uids;
94  bool uid_resolution_running;
95  bool active;
96  } uid_resolution_state;
97 
98  HTTPServer *m_server;
99  class OlaCallbackClient *m_client;
100  ola::rdm::RDMAPI m_rdm_api;
101  map<unsigned int, uid_resolution_state*> m_universe_uids;
102 
103  ola::thread::Mutex m_pid_store_mu;
104  const ola::rdm::RootPidStore *m_pid_store; // GUARDED_BY(m_pid_store_mu);
105 
106  typedef struct {
107  string id;
108  string name;
109  string hint;
110  } section_info;
111 
112  struct lt_section_info {
113  bool operator()(const section_info &left, const section_info &right) {
114  return left.name < right.name;
115  }
116  };
117 
118  typedef struct {
119  unsigned int universe_id;
120  const UID uid;
121  string hint;
122  string device_model;
123  string software_version;
124  } device_info;
125 
126  typedef struct {
127  unsigned int universe_id;
128  const UID *uid;
129  bool include_descriptions;
130  bool return_as_section;
131  unsigned int active;
132  unsigned int next;
133  unsigned int total;
134  vector<std::pair<uint32_t, string> > personalities;
135  } personality_info;
136 
138  RDMHTTPModule& operator=(const RDMHTTPModule&);
139 
140  // uid resolution methods
141  void HandleUIDList(HTTPResponse *response,
142  unsigned int universe_id,
143  const ola::rdm::UIDSet &uids,
144  const string &error);
145 
146  void ResolveNextUID(unsigned int universe_id);
147 
148  void UpdateUIDManufacturerLabel(unsigned int universe,
149  UID uid,
150  const ola::rdm::ResponseStatus &status,
151  const string &device_label);
152 
153  void UpdateUIDDeviceLabel(unsigned int universe,
154  UID uid,
155  const ola::rdm::ResponseStatus &status,
156  const string &device_label);
157 
158  uid_resolution_state *GetUniverseUids(unsigned int universe);
159  uid_resolution_state *GetUniverseUidsOrCreate(unsigned int universe);
160 
161  // uid info handler
162  void UIDInfoHandler(HTTPResponse *response,
163  const ola::rdm::ResponseStatus &status,
164  const ola::rdm::DeviceDescriptor &device);
165 
166  // uid identify handler
167  void UIDIdentifyHandler(HTTPResponse *response,
168  const ola::rdm::ResponseStatus &status,
169  bool value);
170 
171  // personality handler
172  void SendPersonalityResponse(HTTPResponse *response,
173  personality_info *info);
174 
175 
176  // supported params / sections
177  void SupportedParamsHandler(HTTPResponse *response,
178  const ola::rdm::ResponseStatus &status,
179  const vector<uint16_t> &pids);
180  void SupportedSectionsHandler(HTTPResponse *response,
181  unsigned int universe,
182  UID uid,
183  const ola::rdm::ResponseStatus &status,
184  const vector<uint16_t> &pids);
185  void SupportedSectionsDeviceInfoHandler(
186  HTTPResponse *response,
187  const vector<uint16_t> pids,
188  const ola::rdm::ResponseStatus &status,
189  const ola::rdm::DeviceDescriptor &device);
190 
191  // section methods
192  string GetCommStatus(HTTPResponse *response,
193  unsigned int universe_id,
194  const UID &uid);
195 
196  void CommStatusHandler(HTTPResponse *response,
197  const ola::rdm::ResponseStatus &status,
198  uint16_t short_messages,
199  uint16_t length_mismatch,
200  uint16_t checksum_fail);
201 
202  string ClearCommsCounters(HTTPResponse *response,
203  unsigned int universe_id,
204  const UID &uid);
205 
206  string GetProxiedDevices(HTTPResponse *response,
207  unsigned int universe_id,
208  const UID &uid);
209 
210 
211  void ProxiedDevicesHandler(HTTPResponse *response,
212  unsigned int universe_id,
213  const ola::rdm::ResponseStatus &status,
214  const vector<UID> &uids);
215 
216  string GetDeviceInfo(const HTTPRequest *request,
217  HTTPResponse *response,
218  unsigned int universe_id,
219  const UID &uid);
220 
221  void GetSoftwareVersionHandler(HTTPResponse *response,
222  device_info dev_info,
223  const ola::rdm::ResponseStatus &status,
224  const string &software_version);
225 
226  void GetDeviceModelHandler(HTTPResponse *response,
227  device_info dev_info,
228  const ola::rdm::ResponseStatus &status,
229  const string &device_model);
230 
231  void GetDeviceInfoHandler(HTTPResponse *response,
232  device_info dev_info,
233  const ola::rdm::ResponseStatus &status,
234  const ola::rdm::DeviceDescriptor &device);
235 
236  string GetProductIds(const HTTPRequest *request,
237  HTTPResponse *response,
238  unsigned int universe_id,
239  const UID &uid);
240 
241  void GetProductIdsHandler(HTTPResponse *response,
242  const ola::rdm::ResponseStatus &status,
243  const vector<uint16_t> &ids);
244 
245  string GetManufacturerLabel(const HTTPRequest *request,
246  HTTPResponse *response,
247  unsigned int universe_id,
248  const UID &uid);
249 
250  void GetManufacturerLabelHandler(HTTPResponse *response,
251  unsigned int universe_id,
252  const UID uid,
253  const ola::rdm::ResponseStatus &status,
254  const string &label);
255 
256  string GetDeviceLabel(const HTTPRequest *request,
257  HTTPResponse *response,
258  unsigned int universe_id,
259  const UID &uid);
260 
261  void GetDeviceLabelHandler(HTTPResponse *response,
262  unsigned int universe_id,
263  const UID uid,
264  const ola::rdm::ResponseStatus &status,
265  const string &label);
266 
267  string SetDeviceLabel(const HTTPRequest *request,
268  HTTPResponse *response,
269  unsigned int universe_id,
270  const UID &uid);
271 
272  string GetFactoryDefaults(HTTPResponse *response,
273  unsigned int universe_id,
274  const UID &uid);
275 
276  void FactoryDefaultsHandler(HTTPResponse *response,
277  const ola::rdm::ResponseStatus &status,
278  bool defaults);
279 
280  string SetFactoryDefault(HTTPResponse *response,
281  unsigned int universe_id,
282  const UID &uid);
283 
284  string GetLanguage(HTTPResponse *response,
285  unsigned int universe_id,
286  const UID &uid);
287 
288  void GetSupportedLanguagesHandler(HTTPResponse *response,
289  unsigned int universe_id,
290  const UID uid,
291  const ola::rdm::ResponseStatus &status,
292  const vector<string> &languages);
293 
294  void GetLanguageHandler(HTTPResponse *response,
295  vector<string> languages,
296  const ola::rdm::ResponseStatus &status,
297  const string &language);
298 
299  string SetLanguage(const HTTPRequest *request,
300  HTTPResponse *response,
301  unsigned int universe_id,
302  const UID &uid);
303 
304  string GetBootSoftware(HTTPResponse *response,
305  unsigned int universe_id,
306  const UID &uid);
307 
308  void GetBootSoftwareLabelHandler(HTTPResponse *response,
309  unsigned int universe_id,
310  const UID uid,
311  const ola::rdm::ResponseStatus &status,
312  const string &label);
313 
314  void GetBootSoftwareVersionHandler(
315  HTTPResponse *response,
316  string label,
317  const ola::rdm::ResponseStatus &status,
318  uint32_t version);
319 
320  string GetPersonalities(const HTTPRequest *request,
321  HTTPResponse *response,
322  unsigned int universe_id,
323  const UID &uid,
324  bool return_as_section,
325  bool include_description = false);
326 
327  void GetPersonalityHandler(
328  HTTPResponse *response,
329  personality_info *info,
330  const ola::rdm::ResponseStatus &status,
331  uint8_t current,
332  uint8_t total);
333 
334  void GetNextPersonalityDescription(HTTPResponse *response,
335  personality_info *info);
336 
337  void GetPersonalityLabelHandler(
338  HTTPResponse *response,
339  personality_info *info,
340  const ola::rdm::ResponseStatus &status,
341  uint8_t personality,
342  uint16_t slot_count,
343  const string &label);
344 
345  void SendSectionPersonalityResponse(HTTPResponse *response,
346  personality_info *info);
347 
348  string SetPersonality(const HTTPRequest *request,
349  HTTPResponse *response,
350  unsigned int universe_id,
351  const UID &uid);
352 
353  string GetStartAddress(const HTTPRequest *request,
354  HTTPResponse *response,
355  unsigned int universe_id,
356  const UID &uid);
357 
358  void GetStartAddressHandler(HTTPResponse *response,
359  const ola::rdm::ResponseStatus &status,
360  uint16_t address);
361 
362  string SetStartAddress(const HTTPRequest *request,
363  HTTPResponse *response,
364  unsigned int universe_id,
365  const UID &uid);
366 
367  string GetSensor(const HTTPRequest *request,
368  HTTPResponse *response,
369  unsigned int universe_id,
370  const UID &uid);
371 
372  void SensorDefinitionHandler(HTTPResponse *response,
373  unsigned int universe_id,
374  const UID uid,
375  uint8_t sensor_id,
376  const ola::rdm::ResponseStatus &status,
377  const ola::rdm::SensorDescriptor &definition);
378 
379  void SensorValueHandler(HTTPResponse *response,
380  ola::rdm::SensorDescriptor *definition,
381  const ola::rdm::ResponseStatus &status,
382  const ola::rdm::SensorValueDescriptor &value);
383 
384  string RecordSensor(const HTTPRequest *request,
385  HTTPResponse *response,
386  unsigned int universe_id,
387  const UID &uid);
388 
389  string GetDeviceHours(const HTTPRequest *request,
390  HTTPResponse *response,
391  unsigned int universe_id,
392  const UID &uid);
393 
394  string SetDeviceHours(const HTTPRequest *request,
395  HTTPResponse *response,
396  unsigned int universe_id,
397  const UID &uid);
398 
399  string GetLampHours(const HTTPRequest *request,
400  HTTPResponse *response,
401  unsigned int universe_id,
402  const UID &uid);
403 
404  string SetLampHours(const HTTPRequest *request,
405  HTTPResponse *response,
406  unsigned int universe_id,
407  const UID &uid);
408 
409  string GetLampStrikes(const HTTPRequest *request,
410  HTTPResponse *response,
411  unsigned int universe_id,
412  const UID &uid);
413 
414  string SetLampStrikes(const HTTPRequest *request,
415  HTTPResponse *response,
416  unsigned int universe_id,
417  const UID &uid);
418 
419  string GetLampState(const HTTPRequest *request,
420  HTTPResponse *response,
421  unsigned int universe_id,
422  const UID &uid);
423 
424  void LampStateHandler(HTTPResponse *response,
425  const ola::rdm::ResponseStatus &status,
426  uint8_t state);
427 
428  string SetLampState(const HTTPRequest *request,
429  HTTPResponse *response,
430  unsigned int universe_id,
431  const UID &uid);
432 
433  string GetLampMode(const HTTPRequest *request,
434  HTTPResponse *response,
435  unsigned int universe_id,
436  const UID &uid);
437 
438  void LampModeHandler(HTTPResponse *response,
439  const ola::rdm::ResponseStatus &status,
440  uint8_t mode);
441 
442  string SetLampMode(const HTTPRequest *request,
443  HTTPResponse *response,
444  unsigned int universe_id,
445  const UID &uid);
446 
447  string GetPowerCycles(const HTTPRequest *request,
448  HTTPResponse *response,
449  unsigned int universe_id,
450  const UID &uid);
451 
452  string SetPowerCycles(const HTTPRequest *request,
453  HTTPResponse *response,
454  unsigned int universe_id,
455  const UID &uid);
456 
457  string GetDisplayInvert(HTTPResponse *response,
458  unsigned int universe_id,
459  const UID &uid);
460 
461  void DisplayInvertHandler(HTTPResponse *response,
462  const ola::rdm::ResponseStatus &status,
463  uint8_t value);
464 
465  string SetDisplayInvert(const HTTPRequest *request,
466  HTTPResponse *response,
467  unsigned int universe_id,
468  const UID &uid);
469 
470  string GetDisplayLevel(HTTPResponse *response,
471  unsigned int universe_id,
472  const UID &uid);
473 
474  void DisplayLevelHandler(HTTPResponse *response,
475  const ola::rdm::ResponseStatus &status,
476  uint8_t value);
477 
478  string SetDisplayLevel(const HTTPRequest *request,
479  HTTPResponse *response,
480  unsigned int universe_id,
481  const UID &uid);
482 
483  string GetPanInvert(HTTPResponse *response,
484  unsigned int universe_id,
485  const UID &uid);
486 
487  string SetPanInvert(const HTTPRequest *request,
488  HTTPResponse *response,
489  unsigned int universe_id,
490  const UID &uid);
491 
492  string GetTiltInvert(HTTPResponse *response,
493  unsigned int universe_id,
494  const UID &uid);
495 
496  string SetTiltInvert(const HTTPRequest *request,
497  HTTPResponse *response,
498  unsigned int universe_id,
499  const UID &uid);
500 
501  string GetPanTiltSwap(HTTPResponse *response,
502  unsigned int universe_id,
503  const UID &uid);
504 
505  string SetPanTiltSwap(const HTTPRequest *request,
506  HTTPResponse *response,
507  unsigned int universe_id,
508  const UID &uid);
509 
510  string GetClock(HTTPResponse *response,
511  unsigned int universe_id,
512  const UID &uid);
513 
514  void ClockHandler(HTTPResponse *response,
515  const ola::rdm::ResponseStatus &status,
516  const ola::rdm::ClockValue &clock);
517 
518  string SyncClock(HTTPResponse *response,
519  unsigned int universe_id,
520  const UID &uid);
521 
522  string GetIdentifyMode(HTTPResponse *response,
523  unsigned int universe_id,
524  const UID &uid);
525 
526  string SetIdentifyMode(const HTTPRequest *request,
527  HTTPResponse *response,
528  unsigned int universe_id,
529  const UID &uid);
530 
531  string GetPowerState(HTTPResponse *response,
532  unsigned int universe_id,
533  const UID &uid);
534 
535  void PowerStateHandler(HTTPResponse *response,
536  const ola::rdm::ResponseStatus &status,
537  uint8_t value);
538 
539  string SetPowerState(const HTTPRequest *request,
540  HTTPResponse *response,
541  unsigned int universe_id,
542  const UID &uid);
543 
544  string GetResetDevice(HTTPResponse *response);
545 
546  string SetResetDevice(const HTTPRequest *request,
547  HTTPResponse *response,
548  unsigned int universe_id,
549  const UID &uid);
550 
551  // util methods
552  bool CheckForInvalidId(const HTTPRequest *request,
553  unsigned int *universe_id);
554 
555  bool CheckForInvalidUid(const HTTPRequest *request, UID **uid);
556 
557  uint16_t SubDeviceOrRoot(const HTTPRequest *request);
558 
559  void SetHandler(HTTPResponse *response,
560  const ola::rdm::ResponseStatus &status);
561 
562  void GenericUIntHandler(HTTPResponse *response,
563  string description,
564  const ola::rdm::ResponseStatus &status,
565  uint32_t value);
566 
567  void GenericUInt8BoolHandler(HTTPResponse *response,
568  string description,
569  const ola::rdm::ResponseStatus &status,
570  uint8_t value);
571  void GenericBoolHandler(HTTPResponse *response,
572  string description,
573  const ola::rdm::ResponseStatus &status,
574  bool value);
575 
576  bool CheckForRDMError(HTTPResponse *response,
577  const ola::rdm::ResponseStatus &status);
578  int RespondWithError(HTTPResponse *response, const string &error);
579  void RespondWithSection(HTTPResponse *response,
580  const ola::web::JsonSection &section);
581 
582  bool CheckForRDMSuccess(const ola::rdm::ResponseStatus &status);
583  bool CheckForRDMSuccessWithError(const ola::rdm::ResponseStatus &status,
584  string *error);
585 
586  void HandleBoolResponse(HTTPResponse *response, const string &error);
587 
588  void AddSection(vector<section_info> *sections,
589  const string &section_id,
590  const string &section_name,
591  const string &hint="");
592 
593  static const uint32_t INVALID_PERSONALITY = 0xffff;
594  static const char BACKEND_DISCONNECTED_ERROR[];
595 
596  static const char HINT_KEY[];
597  static const char ID_KEY[];
598  static const char SECTION_KEY[];
599  static const char UID_KEY[];
600 
601  static const char ADDRESS_FIELD[];
602  static const char DISPLAY_INVERT_FIELD[];
603  static const char GENERIC_BOOL_FIELD[];
604  static const char GENERIC_STRING_FIELD[];
605  static const char GENERIC_UINT_FIELD[];
606  static const char IDENTIFY_FIELD[];
607  static const char LABEL_FIELD[];
608  static const char LANGUAGE_FIELD[];
609  static const char RECORD_SENSOR_FIELD[];
610  static const char SUB_DEVICE_FIELD[];
611 
612  static const char BOOT_SOFTWARE_SECTION[];
613  static const char CLOCK_SECTION[];
614  static const char COMMS_STATUS_SECTION[];
615  static const char DEVICE_HOURS_SECTION[];
616  static const char DEVICE_INFO_SECTION[];
617  static const char DEVICE_LABEL_SECTION[];
618  static const char DISPLAY_INVERT_SECTION[];
619  static const char DISPLAY_LEVEL_SECTION[];
620  static const char DMX_ADDRESS_SECTION[];
621  static const char FACTORY_DEFAULTS_SECTION[];
622  static const char IDENTIFY_SECTION[];
623  static const char LAMP_HOURS_SECTION[];
624  static const char LAMP_MODE_SECTION[];
625  static const char LAMP_STATE_SECTION[];
626  static const char LAMP_STRIKES_SECTION[];
627  static const char LANGUAGE_SECTION[];
628  static const char MANUFACTURER_LABEL_SECTION[];
629  static const char PAN_INVERT_SECTION[];
630  static const char PAN_TILT_SWAP_SECTION[];
631  static const char PERSONALITY_SECTION[];
632  static const char POWER_CYCLES_SECTION[];
633  static const char POWER_STATE_SECTION[];
634  static const char PRODUCT_DETAIL_SECTION[];
635  static const char PROXIED_DEVICES_SECTION[];
636  static const char RESET_DEVICE_SECTION[];
637  static const char SENSOR_SECTION[];
638  static const char TILT_INVERT_SECTION[];
639 
640  static const char BOOT_SOFTWARE_SECTION_NAME[];
641  static const char CLOCK_SECTION_NAME[];
642  static const char COMMS_STATUS_SECTION_NAME[];
643  static const char DEVICE_HOURS_SECTION_NAME[];
644  static const char DEVICE_INFO_SECTION_NAME[];
645  static const char DEVICE_LABEL_SECTION_NAME[];
646  static const char DISPLAY_INVERT_SECTION_NAME[];
647  static const char DISPLAY_LEVEL_SECTION_NAME[];
648  static const char DMX_ADDRESS_SECTION_NAME[];
649  static const char FACTORY_DEFAULTS_SECTION_NAME[];
650  static const char IDENTIFY_SECTION_NAME[];
651  static const char LAMP_HOURS_SECTION_NAME[];
652  static const char LAMP_MODE_SECTION_NAME[];
653  static const char LAMP_STATE_SECTION_NAME[];
654  static const char LAMP_STRIKES_SECTION_NAME[];
655  static const char LANGUAGE_SECTION_NAME[];
656  static const char MANUFACTURER_LABEL_SECTION_NAME[];
657  static const char PAN_INVERT_SECTION_NAME[];
658  static const char PAN_TILT_SWAP_SECTION_NAME[];
659  static const char PERSONALITY_SECTION_NAME[];
660  static const char POWER_CYCLES_SECTION_NAME[];
661  static const char POWER_STATE_SECTION_NAME[];
662  static const char PRODUCT_DETAIL_SECTION_NAME[];
663  static const char PROXIED_DEVICES_SECTION_NAME[];
664  static const char RESET_DEVICE_SECTION_NAME[];
665  static const char TILT_INVERT_SECTION_NAME[];
666 };
667 } // namespace ola
668 #endif // OLAD_RDMHTTPMODULE_H_