Open Lighting Architecture 0.10.9
Loading...
Searching...
No Matches
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
39namespace ola {
40namespace rdm {
41
57class 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 UID(const UID &uid) {
76 m_uid.esta_id = uid.m_uid.esta_id;
77 m_uid.device_id = uid.m_uid.device_id;
78 }
79
85 explicit UID(const uint8_t *data) {
86 m_uid.esta_id = ola::utils::JoinUInt8(data[0], data[1]);
87 m_uid.device_id = ola::utils::JoinUInt8(data[2], data[3], data[4],
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::ostringstream 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 std::ostream& operator<< (std::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
245 return UID(uid.ManufacturerId(), ALL_DEVICES);
246 }
247
254 static UID* FromString(const std::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_
Represents a RDM UID.
Definition UID.h:57
uint16_t ManufacturerId() const
The manufacturer ID for this UID.
Definition UID.h:138
static UID VendorcastAddress(uint16_t esta_id)
Returns a UID that matches all devices for a particular manufacturer.
Definition UID.h:234
uint32_t DeviceId() const
The device ID for this UID.
Definition UID.h:144
UID(const UID &uid)
Copy constructor.
Definition UID.h:75
static const uint32_t ALL_DEVICES
The value for the 'all devices' id.
Definition UID.h:271
static UID VendorcastAddress(UID uid)
Returns a UID that matches all devices for a particular manufacturer.
Definition UID.h:244
UID & operator=(const UID &other)
Assignment operator.
Definition UID.h:94
UID(const uint8_t *data)
Construct a new UID from binary data.
Definition UID.h:85
UID(uint16_t esta_id, uint32_t device_id)
Constructs a new UID.
Definition UID.h:66
bool operator>(const UID &other) const
Greater than.
Definition UID.h:122
static UID AllDevices()
Returns a UID that matches all devices (ffff:ffffffff).
Definition UID.h:224
static const uint16_t ALL_MANUFACTURERS
The value for the 'all manufacturers' id.
Definition UID.h:266
@ UID_SIZE
Definition UID.h:260
bool Pack(uint8_t *buffer, unsigned int length) const
Write the binary representation of the UID to memory.
Definition UID.h:208
bool operator<(const UID &other) const
Less than.
Definition UID.h:130
bool IsBroadcast() const
Check if this UID is a broadcast or vendorcast UID.
Definition UID.h:150
bool DirectedToUID(const UID &uid) const
Check if this UID matches against another.
Definition UID.h:175
bool operator!=(const UID &other) const
Inequality operator.
Definition UID.h:114
static UID * FromString(const std::string &uid)
Return a new UID from a string.
Definition UID.cpp:32
std::string ToString() const
Convert a UID to a human readable string.
Definition UID.h:186
bool operator==(const UID &other) const
Equality operator.
Definition UID.h:106
friend std::ostream & operator<<(std::ostream &out, const UID &uid)
A helper function to write a UID to an ostream.
Definition UID.h:198
The namespace containing all OLA symbols.
Definition Credentials.cpp:44