Open Lighting Architecture  Latest Git
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 
33 #include <ola/util/Utils.h>
34 
35 #include <iomanip>
36 #include <sstream>
37 #include <string>
38 
39 namespace ola {
40 namespace rdm {
41 
57 class UID {
58  public:
59  enum { LENGTH = 6 };
60 
66  UID(uint16_t esta_id, uint32_t device_id) {
67  m_uid.esta_id = esta_id;
68  m_uid.device_id = device_id;
69  }
70 
75  explicit UID(uint64_t uid) {
76  m_uid.esta_id = static_cast<uint16_t>(uid >> 32);
77  m_uid.device_id = static_cast<uint32_t>(uid);
78  }
79 
84  UID(const UID &uid) {
85  m_uid.esta_id = uid.m_uid.esta_id;
86  m_uid.device_id = uid.m_uid.device_id;
87  }
88 
94  explicit UID(const uint8_t *data) {
95  m_uid.esta_id = ola::utils::JoinUInt8(data[0], data[1]);
96  m_uid.device_id = ola::utils::JoinUInt8(data[2], data[3], data[4],
97  data[5]);
98  }
99 
103  UID& operator=(const UID& other) {
104  if (this != &other) {
105  m_uid.esta_id = other.m_uid.esta_id;
106  m_uid.device_id = other.m_uid.device_id;
107  }
108  return *this;
109  }
110 
115  bool operator==(const UID &other) const {
116  return 0 == cmp(*this, other);
117  }
118 
123  bool operator!=(const UID &other) const {
124  return 0 != cmp(*this, other);
125  }
126 
131  bool operator>(const UID &other) const {
132  return cmp(*this, other) > 0;
133  }
134 
139  bool operator<(const UID &other) const {
140  return cmp(*this, other) < 0;
141  }
142 
147  bool operator>=(const UID &other) const {
148  return cmp(*this, other) >= 0;
149  }
150 
155  bool operator<=(const UID &other) const {
156  return cmp(*this, other) <= 0;
157  }
158 
163  uint16_t ManufacturerId() const { return m_uid.esta_id; }
164 
169  uint32_t DeviceId() const { return m_uid.device_id; }
170 
175  bool IsBroadcast() const { return m_uid.device_id == ALL_DEVICES; }
176 
200  bool DirectedToUID(const UID &uid) const {
201  return *this == uid ||
202  (IsBroadcast() &&
204  ManufacturerId() == uid.ManufacturerId()));
205  }
206 
211  uint64_t ToUInt64() const {
212  return ((static_cast<uint64_t>(m_uid.esta_id) << 32) + m_uid.device_id);
213  }
214 
219  std::string ToString() const {
220  std::ostringstream str;
221  str << std::setfill('0') << std::setw(4) << std::hex << m_uid.esta_id
222  << ":" << std::setw(8) << m_uid.device_id;
223  return str.str();
224  }
225 
231  friend std::ostream& operator<< (std::ostream &out, const UID &uid) {
232  return out << uid.ToString();
233  }
234 
241  bool Pack(uint8_t *buffer, unsigned int length) const {
242  if (length < UID_SIZE)
243  return false;
244  buffer[0] = static_cast<uint8_t>(m_uid.esta_id >> 8);
245  buffer[1] = static_cast<uint8_t>(m_uid.esta_id & 0xff);
246  buffer[2] = static_cast<uint8_t>(m_uid.device_id >> 24);
247  buffer[3] = static_cast<uint8_t>(m_uid.device_id >> 16);
248  buffer[4] = static_cast<uint8_t>(m_uid.device_id >> 8);
249  buffer[5] = static_cast<uint8_t>(m_uid.device_id & 0xff);
250  return true;
251  }
252 
257  static UID AllDevices() {
259  }
260 
267  static UID VendorcastAddress(uint16_t esta_id) {
268  return UID(esta_id, ALL_DEVICES);
269  }
270 
277  static UID VendorcastAddress(UID uid) {
278  return UID(uid.ManufacturerId(), ALL_DEVICES);
279  }
280 
287  static UID* FromString(const std::string &uid);
288 
292  enum {
293  UID_SIZE = LENGTH
294  };
295 
299  static const uint16_t ALL_MANUFACTURERS = 0xffff;
300 
304  static const uint32_t ALL_DEVICES = 0xffffffff;
305 
306  private:
307  struct rdm_uid {
308  uint16_t esta_id;
309  uint32_t device_id;
310  };
311 
312  struct rdm_uid m_uid;
313 
314  int cmp(const UID &a, const UID &b) const {
315  if (a.m_uid.esta_id == b.m_uid.esta_id) {
316  return cmp(a.m_uid.device_id, b.m_uid.device_id);
317  }
318  return cmp(a.m_uid.esta_id, b.m_uid.esta_id);
319  }
320 
321  int cmp(uint32_t a, uint32_t b) const {
322  if (a == b) {
323  return 0;
324  }
325  return a < b ? -1 : 1;
326  }
327 };
328 } // namespace rdm
329 } // namespace ola
330 #endif // INCLUDE_OLA_RDM_UID_H_
UID(const UID &uid)
Copy constructor.
Definition: UID.h:84
bool operator<=(const UID &other) const
Less than or equal to.
Definition: UID.h:155
static const uint16_t ALL_MANUFACTURERS
The value for the &#39;all manufacturers&#39; id.
Definition: UID.h:299
bool IsBroadcast() const
Check if this UID is a broadcast or vendorcast UID.
Definition: UID.h:175
bool DirectedToUID(const UID &uid) const
Check if this UID matches against another.
Definition: UID.h:200
static const uint32_t ALL_DEVICES
The value for the &#39;all devices&#39; id.
Definition: UID.h:304
uint16_t ManufacturerId() const
The manufacturer ID for this UID.
Definition: UID.h:163
bool operator==(const UID &other) const
Equality operator.
Definition: UID.h:115
UID(uint64_t uid)
Constructs a new UID from a uint64_t.
Definition: UID.h:75
static UID VendorcastAddress(uint16_t esta_id)
Returns a UID that matches all devices for a particular manufacturer.
Definition: UID.h:267
UID(const uint8_t *data)
Construct a new UID from binary data.
Definition: UID.h:94
Definition: UID.h:293
friend std::ostream & operator<<(std::ostream &out, const UID &uid)
A helper function to write a UID to an ostream.
Definition: UID.h:231
uint16_t JoinUInt8(uint8_t high, uint8_t low)
Convert two uint8_t&#39;s to a uint16_t.
Definition: Utils.h:50
UID & operator=(const UID &other)
Assignment operator.
Definition: UID.h:103
UID(uint16_t esta_id, uint32_t device_id)
Constructs a new UID.
Definition: UID.h:66
static UID * FromString(const std::string &uid)
Return a new UID from a string.
Definition: UID.cpp:32
Represents a RDM UID.
Definition: UID.h:57
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
bool operator>(const UID &other) const
Greater than.
Definition: UID.h:131
uint64_t ToUInt64() const
Convert a UID to a uint64_t.
Definition: UID.h:211
uint32_t DeviceId() const
The device ID for this UID.
Definition: UID.h:169
std::string ToString() const
Convert a UID to a human readable string.
Definition: UID.h:219
static UID VendorcastAddress(UID uid)
Returns a UID that matches all devices for a particular manufacturer.
Definition: UID.h:277
bool operator!=(const UID &other) const
Inequality operator.
Definition: UID.h:123
bool operator<(const UID &other) const
Less than.
Definition: UID.h:139
static UID AllDevices()
Returns a UID that matches all devices (ffff:ffffffff).
Definition: UID.h:257
bool operator>=(const UID &other) const
Greater than or equal to.
Definition: UID.h:147
bool Pack(uint8_t *buffer, unsigned int length) const
Write the binary representation of the UID to memory.
Definition: UID.h:241