Open Lighting Architecture
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UID.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  * UID.h
17  * Representation of an RDM UID
18  * Copyright (C) 2005-2010 Simon Newton
19  */
20 
28 #ifndef INCLUDE_OLA_RDM_UID_H_
29 #define INCLUDE_OLA_RDM_UID_H_
30 
31 #include <stdint.h>
32 #include <iomanip>
33 #include <sstream>
34 #include <string>
35 
36 namespace ola {
37 namespace rdm {
38 
39 using std::ostream;
40 using std::string;
41 
57 class UID {
58  public:
64  UID(uint16_t esta_id, uint32_t device_id) {
65  m_uid.esta_id = esta_id;
66  m_uid.device_id = device_id;
67  }
68 
73  UID(const UID &uid) {
74  m_uid.esta_id = uid.m_uid.esta_id;
75  m_uid.device_id = uid.m_uid.device_id;
76  }
77 
83  explicit UID(const uint8_t *data) {
84  m_uid.esta_id = static_cast<uint16_t>((data[0] << 8) + data[1]);
85  m_uid.device_id = static_cast<uint32_t>(data[2] << 24) +
86  static_cast<uint32_t>(data[3] << 16) +
87  static_cast<uint32_t>(data[4] << 8) +
88  data[5];
89  }
90 
94  UID& operator=(const UID& other) {
95  if (this != &other) {
96  m_uid.esta_id = other.m_uid.esta_id;
97  m_uid.device_id = other.m_uid.device_id;
98  }
99  return *this;
100  }
101 
106  bool operator==(const UID &other) const {
107  return 0 == cmp(*this, other);
108  }
109 
114  bool operator!=(const UID &other) const {
115  return 0 != cmp(*this, other);
116  }
117 
122  bool operator>(const UID &other) const {
123  return cmp(*this, other) > 0;
124  }
125 
130  bool operator<(const UID &other) const {
131  return cmp(*this, other) < 0;
132  }
133 
138  uint16_t ManufacturerId() const { return m_uid.esta_id; }
139 
144  uint32_t DeviceId() const { return m_uid.device_id; }
145 
150  bool IsBroadcast() const { return m_uid.device_id == ALL_DEVICES; }
151 
175  bool DirectedToUID(const UID &uid) const {
176  return *this == uid ||
177  (IsBroadcast() &&
179  ManufacturerId() == uid.ManufacturerId()));
180  }
181 
186  std::string ToString() const {
187  std::stringstream str;
188  str << std::setfill('0') << std::setw(4) << std::hex << m_uid.esta_id
189  << ":" << std::setw(8) << m_uid.device_id;
190  return str.str();
191  }
192 
198  friend ostream& operator<< (ostream &out, const UID &uid) {
199  return out << uid.ToString();
200  }
201 
208  bool Pack(uint8_t *buffer, unsigned int length) const {
209  if (length < UID_SIZE)
210  return false;
211  buffer[0] = static_cast<uint8_t>(m_uid.esta_id >> 8);
212  buffer[1] = static_cast<uint8_t>(m_uid.esta_id & 0xff);
213  buffer[2] = static_cast<uint8_t>(m_uid.device_id >> 24);
214  buffer[3] = static_cast<uint8_t>(m_uid.device_id >> 16);
215  buffer[4] = static_cast<uint8_t>(m_uid.device_id >> 8);
216  buffer[5] = static_cast<uint8_t>(m_uid.device_id & 0xff);
217  return true;
218  }
219 
224  static UID AllDevices() {
226  }
227 
234  static UID VendorcastAddress(uint16_t esta_id) {
235  return UID(esta_id, ALL_DEVICES);
236  }
237 
244  static UID VendorcastAddress(UID uid) {
245  return UID(uid.ManufacturerId(), ALL_DEVICES);
246  }
247 
254  static UID* FromString(const string &uid);
255 
259  enum {
260  UID_SIZE = 6
261  };
262 
266  static const uint16_t ALL_MANUFACTURERS = 0xffff;
267 
271  static const uint32_t ALL_DEVICES = 0xffffffff;
272 
273  private:
274  struct rdm_uid {
275  uint16_t esta_id;
276  uint32_t device_id;
277  };
278 
279  struct rdm_uid m_uid;
280 
281  int cmp(const UID &a, const UID &b) const {
282  if (a.m_uid.esta_id == b.m_uid.esta_id)
283  return cmp(a.m_uid.device_id, b.m_uid.device_id);
284  return cmp(a.m_uid.esta_id, b.m_uid.esta_id);
285  }
286 
287  int cmp(uint32_t a, uint32_t b) const {
288  if (a == b)
289  return 0;
290  return a < b ? -1 : 1;
291  }
292 };
293 } // namespace rdm
294 } // namespace ola
295 #endif // INCLUDE_OLA_RDM_UID_H_