Open Lighting Architecture  0.9.3
 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * UID.h
17  * Representation of an RDM UID
18  * Copyright (C) 2005 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 
54 class UID {
55  public:
56  enum { LENGTH = 6 };
57 
63  UID(uint16_t esta_id, uint32_t device_id) {
64  m_uid.esta_id = esta_id;
65  m_uid.device_id = device_id;
66  }
67 
72  UID(const UID &uid) {
73  m_uid.esta_id = uid.m_uid.esta_id;
74  m_uid.device_id = uid.m_uid.device_id;
75  }
76 
82  explicit UID(const uint8_t *data) {
83  m_uid.esta_id = static_cast<uint16_t>((data[0] << 8) + data[1]);
84  m_uid.device_id = static_cast<uint32_t>(data[2] << 24) +
85  static_cast<uint32_t>(data[3] << 16) +
86  static_cast<uint32_t>(data[4] << 8) +
87  data[5];
88  }
89 
93  UID& operator=(const UID& other) {
94  if (this != &other) {
95  m_uid.esta_id = other.m_uid.esta_id;
96  m_uid.device_id = other.m_uid.device_id;
97  }
98  return *this;
99  }
100 
105  bool operator==(const UID &other) const {
106  return 0 == cmp(*this, other);
107  }
108 
113  bool operator!=(const UID &other) const {
114  return 0 != cmp(*this, other);
115  }
116 
121  bool operator>(const UID &other) const {
122  return cmp(*this, other) > 0;
123  }
124 
129  bool operator<(const UID &other) const {
130  return cmp(*this, other) < 0;
131  }
132 
137  uint16_t ManufacturerId() const { return m_uid.esta_id; }
138 
143  uint32_t DeviceId() const { return m_uid.device_id; }
144 
149  bool IsBroadcast() const { return m_uid.device_id == ALL_DEVICES; }
150 
174  bool DirectedToUID(const UID &uid) const {
175  return *this == uid ||
176  (IsBroadcast() &&
178  ManufacturerId() == uid.ManufacturerId()));
179  }
180 
185  std::string ToString() const {
186  std::ostringstream str;
187  str << std::setfill('0') << std::setw(4) << std::hex << m_uid.esta_id
188  << ":" << std::setw(8) << m_uid.device_id;
189  return str.str();
190  }
191 
197  friend std::ostream& operator<< (std::ostream &out, const UID &uid) {
198  return out << uid.ToString();
199  }
200 
207  bool Pack(uint8_t *buffer, unsigned int length) const {
208  if (length < UID_SIZE)
209  return false;
210  buffer[0] = static_cast<uint8_t>(m_uid.esta_id >> 8);
211  buffer[1] = static_cast<uint8_t>(m_uid.esta_id & 0xff);
212  buffer[2] = static_cast<uint8_t>(m_uid.device_id >> 24);
213  buffer[3] = static_cast<uint8_t>(m_uid.device_id >> 16);
214  buffer[4] = static_cast<uint8_t>(m_uid.device_id >> 8);
215  buffer[5] = static_cast<uint8_t>(m_uid.device_id & 0xff);
216  return true;
217  }
218 
223  static UID AllDevices() {
225  }
226 
233  static UID VendorcastAddress(uint16_t esta_id) {
234  return UID(esta_id, ALL_DEVICES);
235  }
236 
243  static UID VendorcastAddress(UID uid) {
244  return UID(uid.ManufacturerId(), ALL_DEVICES);
245  }
246 
253  static UID* FromString(const std::string &uid);
254 
258  enum {
259  UID_SIZE = 6
260  };
261 
265  static const uint16_t ALL_MANUFACTURERS = 0xffff;
266 
270  static const uint32_t ALL_DEVICES = 0xffffffff;
271 
272  private:
273  struct rdm_uid {
274  uint16_t esta_id;
275  uint32_t device_id;
276  };
277 
278  struct rdm_uid m_uid;
279 
280  int cmp(const UID &a, const UID &b) const {
281  if (a.m_uid.esta_id == b.m_uid.esta_id)
282  return cmp(a.m_uid.device_id, b.m_uid.device_id);
283  return cmp(a.m_uid.esta_id, b.m_uid.esta_id);
284  }
285 
286  int cmp(uint32_t a, uint32_t b) const {
287  if (a == b)
288  return 0;
289  return a < b ? -1 : 1;
290  }
291 };
292 } // namespace rdm
293 } // namespace ola
294 #endif // INCLUDE_OLA_RDM_UID_H_