RDM  HEAD (unstable)
Implementation of ANSI E1.31 (Streaming ACN)
View other versions:
uid.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2020 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************
16  * This file is a part of RDM. For more information, go to:
17  * https://github.com/ETCLabs/RDM
18  *****************************************************************************/
19 
22 
23 #ifndef RDM_CPP_UID_H_
24 #define RDM_CPP_UID_H_
25 
26 #include <cstdint>
27 #include <string>
28 #include "etcpal/cpp/common.h"
29 #include "etcpal/cpp/hash.h"
30 #include "rdm/uid.h"
31 
32 namespace rdm
33 {
43 
81 class Uid
82 {
83 public:
85  Uid() = default;
86  constexpr Uid(uint16_t manu_val, uint32_t id_val) noexcept;
87 
88  constexpr Uid(const RdmUid& c_uid) noexcept;
89  Uid& operator=(const RdmUid& c_uid) noexcept;
90 
91  constexpr const RdmUid& get() const noexcept;
92  ETCPAL_CONSTEXPR_14 RdmUid& get() noexcept;
93  constexpr uint16_t manufacturer_id() const noexcept;
94  constexpr uint32_t device_id() const noexcept;
95  std::string ToString() const;
96 
97  constexpr bool IsValid() const noexcept;
98  bool IsBroadcast() const noexcept;
99  bool IsControllerBroadcast() const noexcept;
100  bool IsDeviceBroadcast() const noexcept;
101  bool IsDeviceManufacturerBroadcast() const noexcept;
102  constexpr bool BroadcastManufacturerIdMatches(uint16_t manu_val) const noexcept;
103  constexpr uint16_t DeviceBroadcastManufacturerId() const noexcept;
104 
105  constexpr bool IsDynamic() const noexcept;
106  constexpr bool IsDynamicUidRequest() const noexcept;
107  constexpr bool IsStatic() const noexcept;
108 
109  void SetManufacturerId(uint16_t manu_val) noexcept;
110  void SetDeviceId(uint32_t id_val) noexcept;
111 
112  static Uid FromString(const std::string& uid_str) noexcept;
113  static Uid FromString(const char* uid_str) noexcept;
114  static Uid Broadcast() noexcept;
115  static Uid ControllerBroadcast() noexcept;
116  static Uid DeviceBroadcast() noexcept;
117  static Uid DeviceBroadcast(uint16_t manu_val) noexcept;
118  static Uid Static(uint16_t manu_val, uint32_t id_val) noexcept;
119  static Uid DynamicUidRequest(uint16_t manu_val) noexcept;
120 
121 private:
122  RdmUid uid_{};
123 };
124 
126 constexpr Uid::Uid(uint16_t manu_val, uint32_t id_val) noexcept : uid_{manu_val, id_val}
127 {
128 }
129 
131 constexpr Uid::Uid(const RdmUid& c_uid) noexcept : uid_(c_uid)
132 {
133 }
134 
136 inline Uid& Uid::operator=(const RdmUid& c_uid) noexcept
137 {
138  uid_ = c_uid;
139  return *this;
140 }
141 
143 constexpr const RdmUid& Uid::get() const noexcept
144 {
145  return uid_;
146 }
147 
150 {
151  return uid_;
152 }
153 
155 constexpr uint16_t Uid::manufacturer_id() const noexcept
156 {
157  return RDM_GET_MANUFACTURER_ID(&uid_);
158 }
159 
161 constexpr uint32_t Uid::device_id() const noexcept
162 {
163  return RDM_GET_DEVICE_ID(&uid_);
164 }
165 
169 {
170  char str_buf[RDM_UID_STRING_BYTES];
171  if (rdm_uid_to_string(&uid_, str_buf))
172  return str_buf;
173  else
174  return std::string();
175 }
176 
178 constexpr bool Uid::IsValid() const noexcept
179 {
180  return !RDM_UID_IS_NULL(&uid_);
181 }
182 
184 inline bool Uid::IsBroadcast() const noexcept
185 {
186  return RDM_UID_IS_BROADCAST(&uid_);
187 }
188 
190 inline bool Uid::IsControllerBroadcast() const noexcept
191 {
193 }
194 
196 inline bool Uid::IsDeviceBroadcast() const noexcept
197 {
198  return RDMNET_UID_IS_DEVICE_BROADCAST(&uid_);
199 }
200 
203 inline bool Uid::IsDeviceManufacturerBroadcast() const noexcept
204 {
206 }
207 
210 constexpr bool Uid::BroadcastManufacturerIdMatches(uint16_t manu_val) const noexcept
211 {
212  return RDMNET_DEVICE_BROADCAST_MANU_MATCHES(&uid_, manu_val);
213 }
214 
216 constexpr uint16_t Uid::DeviceBroadcastManufacturerId() const noexcept
217 {
218  return RDMNET_DEVICE_BROADCAST_MANU_ID(&uid_);
219 }
220 
223 constexpr bool Uid::IsDynamic() const noexcept
224 {
225  return RDMNET_UID_IS_DYNAMIC(&uid_);
226 }
227 
229 constexpr bool Uid::IsDynamicUidRequest() const noexcept
230 {
231  return RDMNET_UID_IS_DYNAMIC_UID_REQUEST(&uid_);
232 }
233 
236 constexpr bool Uid::IsStatic() const noexcept
237 {
238  return RDMNET_UID_IS_STATIC(&uid_);
239 }
240 
244 inline void Uid::SetManufacturerId(uint16_t manu_val) noexcept
245 {
246  // Preserve the state of the top bit
247  uid_.manu = (uid_.manu & 0x8000) | (manu_val & 0x7fff);
248 }
249 
251 inline void Uid::SetDeviceId(uint32_t id_val) noexcept
252 {
253  uid_.id = id_val;
254 }
255 
258 inline Uid Uid::FromString(const std::string& uid_str) noexcept
259 {
260  return FromString(uid_str.c_str());
261 }
262 
265 inline Uid Uid::FromString(const char* uid_str) noexcept
266 {
267  Uid uid;
268  if (rdm_string_to_uid(uid_str, &uid.uid_))
269  return uid;
270  else
271  return Uid{};
272 }
273 
275 inline Uid Uid::Broadcast() noexcept
276 {
277  return kRdmBroadcastUid;
278 }
279 
281 inline Uid Uid::ControllerBroadcast() noexcept
282 {
284 }
285 
287 inline Uid Uid::DeviceBroadcast() noexcept
288 {
290 }
291 
293 inline Uid Uid::DeviceBroadcast(uint16_t manu_val) noexcept
294 {
295  Uid uid;
296  RDMNET_INIT_DEVICE_MANU_BROADCAST(&uid.uid_, manu_val);
297  return uid;
298 }
299 
303 inline Uid Uid::Static(uint16_t manu_val, uint32_t id_val) noexcept
304 {
305  return Uid((manu_val & 0x7fff), id_val);
306 }
307 
309 inline Uid Uid::DynamicUidRequest(uint16_t manu_val) noexcept
310 {
311  Uid uid;
312  RDMNET_INIT_DYNAMIC_UID_REQUEST(&uid.uid_, manu_val);
313  return uid;
314 }
315 
318 
321 
322 // Special operators for comparing with RdmUids
323 
324 constexpr bool operator==(const RdmUid& c_uid, const Uid& uid) noexcept
325 {
326  return c_uid == uid.get();
327 }
328 
329 constexpr bool operator!=(const RdmUid& c_uid, const Uid& uid) noexcept
330 {
331  return !(c_uid == uid);
332 }
333 
334 constexpr bool operator==(const Uid& uid, const RdmUid& c_uid) noexcept
335 {
336  return uid.get() == c_uid;
337 }
338 
339 constexpr bool operator!=(const Uid& uid, const RdmUid& c_uid) noexcept
340 {
341  return !(uid == c_uid);
342 }
343 
344 // Standard operators
345 
346 constexpr bool operator==(const Uid& a, const Uid& b) noexcept
347 {
348  return a.get() == b.get();
349 }
350 
351 constexpr bool operator!=(const Uid& a, const Uid& b) noexcept
352 {
353  return !(a == b);
354 }
355 
356 constexpr bool operator<(const Uid& a, const Uid& b) noexcept
357 {
358  return a.get() < b.get();
359 }
360 
361 constexpr bool operator>(const Uid& a, const Uid& b) noexcept
362 {
363  return b < a;
364 }
365 
366 constexpr bool operator<=(const Uid& a, const Uid& b) noexcept
367 {
368  return !(b < a);
369 }
370 
371 constexpr bool operator>=(const Uid& a, const Uid& b) noexcept
372 {
373  return !(a < b);
374 }
375 
378 
379 }; // namespace rdm
380 
382 
383 // Inject a new std::hash specialization for rdm::Uid, so that it can be used in hash-based containers (e.g.
384 // unordered_map and unordered_set) without a user needing to create a hash specialization.
385 //
386 // The std::hash specialization uses an efficient combination of hashes of the underlying data.
387 namespace std
388 {
389 template <>
390 struct hash<::rdm::Uid>
391 {
392  std::size_t operator()(const ::rdm::Uid& uid) const noexcept
393  {
394  // Use .get() for manu & id so the static bit isn't filtered out.
395 #if SIZE_MAX >= 0xFFFFFFFFFFFF // Can the hash hold all 48 bits of the UID?
396  return (static_cast<size_t>(uid.get().manu) << 32) | uid.get().id;
397 #else
398  size_t seed = 0u;
399  ::etcpal::HashCombine(seed, uid.get().manu);
400  ::etcpal::HashCombine(seed, uid.get().id);
401 
402  return seed;
403 #endif
404  }
405 };
406 }; // namespace std
407 
409 
410 #endif // RDM_CPP_UID_H_
A C++ wrapper class for the RDM UID type.
Definition: uid.h:82
bool IsDeviceManufacturerBroadcast() const noexcept
Determine whether a UID is one of the E1.33 values defined by RPT_ALL_MID_DEVICES.
Definition: uid.h:203
constexpr bool BroadcastManufacturerIdMatches(uint16_t manu_val) const noexcept
Determine whether an RDMnet Device Manufacturer Broadcast UID matches a specific ESTA Manufacturer ID...
Definition: uid.h:210
void SetDeviceId(uint32_t id_val) noexcept
Set the Device ID portion of this UID.
Definition: uid.h:251
std::string ToString() const
Convert a UID to a string representation.
Definition: uid.h:168
Uid & operator=(const RdmUid &c_uid) noexcept
Assign an instance of the C RdmUid type to an instance of this class.
Definition: uid.h:136
Uid()=default
Constructs a null UID by default.
bool IsControllerBroadcast() const noexcept
Determine whether a UID is the E1.33 value RPT_ALL_CONTROLLERS.
Definition: uid.h:190
constexpr bool IsDynamicUidRequest() const noexcept
Determine whether a UID is a Dynamic UID Request as defined in ANSI E1.33.
Definition: uid.h:229
static Uid FromString(const std::string &uid_str) noexcept
Create a UID from a string representation.
Definition: uid.h:258
constexpr bool IsValid() const noexcept
Determine if a UID is valid. All UIDs are valid unless they are null (all 0's).
Definition: uid.h:178
constexpr uint32_t device_id() const noexcept
Get the Device ID portion of this UID.
Definition: uid.h:161
static Uid DynamicUidRequest(uint16_t manu_val) noexcept
Create an RDMnet Dynamic UID Request from a given ESTA Manufacturer ID.
Definition: uid.h:309
bool IsDeviceBroadcast() const noexcept
Determine whether a UID is the E1.33 value RPT_ALL_DEVICES.
Definition: uid.h:196
constexpr bool IsStatic() const noexcept
Determine whether a UID is a Static UID as defined in ANSI E1.33.
Definition: uid.h:236
static Uid ControllerBroadcast() noexcept
Create a UID representing RPT_ALL_CONTROLLERS as defined in ANSI E1.33.
Definition: uid.h:281
constexpr bool IsDynamic() const noexcept
Determine whether a UID is a Dynamic UID as defined in ANSI E1.33.
Definition: uid.h:223
constexpr const RdmUid & get() const noexcept
Get a const reference to the underlying C type.
Definition: uid.h:143
static Uid Static(uint16_t manu_val, uint32_t id_val) noexcept
Explicitly create an RDMnet Static UID from an ESTA Manufacturer ID and Device ID.
Definition: uid.h:303
bool IsBroadcast() const noexcept
Determine whether a UID is the E1.20 value BROADCAST_ALL_DEVICES_ID.
Definition: uid.h:184
static Uid DeviceBroadcast() noexcept
Create a UID representing RPT_ALL_DEVICES as defined in ANSI E1.33.
Definition: uid.h:287
constexpr uint16_t DeviceBroadcastManufacturerId() const noexcept
Get the ESTA Manufacturer ID from an RDMnet Device Manufacturer Broadcast UID.
Definition: uid.h:216
static Uid Broadcast() noexcept
Create a UID representing BROADCAST_ALL_DEVICES_ID as defined in ANSI E1.20.
Definition: uid.h:275
void SetManufacturerId(uint16_t manu_val) noexcept
Set the ESTA Manufacturer ID portion of this UID.
Definition: uid.h:244
constexpr uint16_t manufacturer_id() const noexcept
Get the ESTA Manufacturer ID portion of this UID.
Definition: uid.h:155
void HashCombine(size_t &seed, const T &val)
#define ETCPAL_CONSTEXPR_14_OR_INLINE
#define RDM_GET_MANUFACTURER_ID(uidptr)
Get the ESTA Manufacturer ID from a UID.
Definition: uid.h:237
#define RDM_UID_IS_NULL(uidptr)
Determine if a UID is null.
Definition: uid.h:197
#define RDMNET_UID_IS_DEVICE_MANU_BROADCAST(uidptr)
Determine whether a UID is one of the E1.33 values defined by RPT_ALL_MID_DEVICES.
Definition: uid.h:159
const RdmUid kRdmnetControllerBroadcastUid
Definition: uid.c:28
#define RDMNET_UID_IS_CONTROLLER_BROADCAST(uidptr)
Determine whether a UID is the E1.33 value RPT_ALL_CONTROLLERS.
Definition: uid.h:135
#define RDMNET_UID_IS_DYNAMIC(uidptr)
Determine whether a UID is a Dynamic UID as defined in ANSI E1.33.
Definition: uid.h:216
#define RDM_UID_IS_BROADCAST(uidptr)
Determine whether a UID is the E1.20 value BROADCAST_ALL_DEVICES_ID.
Definition: uid.h:127
#define RDMNET_UID_IS_DEVICE_BROADCAST(uidptr)
Determine whether a UID is the E1.33 value RPT_ALL_DEVICES.
Definition: uid.h:144
#define RDMNET_UID_IS_DYNAMIC_UID_REQUEST(uidptr)
Determine whether a UID is a Dynamic UID Request as defined in ANSI E1.33.
Definition: uid.h:205
bool rdm_uid_to_string(const RdmUid *uid, char *buf)
Create a string representation of a UID.
Definition: uid.c:79
const RdmUid kRdmnetDeviceBroadcastUid
Definition: uid.c:29
#define RDM_GET_DEVICE_ID(uidptr)
Get the Device ID from a UID.
Definition: uid.h:244
#define RDMNET_DEVICE_BROADCAST_MANU_ID(uidptr)
Get the ESTA Manufacturer ID from an RDMnet Device Manufacturer Broadcast UID.
Definition: uid.h:185
#define RDMNET_INIT_DEVICE_MANU_BROADCAST(uidptr, manu_val)
Initialize an RDMnet Device Manufacturer Broadcast UID with a Manufacturer ID.
Definition: uid.h:112
#define RDMNET_UID_IS_STATIC(uidptr)
Determine whether a UID is a Static UID as defined in ANSI E1.33.
Definition: uid.h:230
#define RDMNET_INIT_DYNAMIC_UID_REQUEST(uidptr, manu_val)
Definition: uid.h:100
#define RDM_UID_STRING_BYTES
Definition: uid.h:256
bool rdm_string_to_uid(const char *str, RdmUid *uid)
Create a UID from a string representation.
Definition: uid.c:100
const RdmUid kRdmBroadcastUid
Definition: uid.c:27
#define RDMNET_DEVICE_BROADCAST_MANU_MATCHES(uidptr, manu_val)
Determine whether an RDMnet Device Manufacturer Broadcast UID matches a specific ESTA Manufacturer ID...
Definition: uid.h:174
T operator()(T... args)
T operator!=(T... args)
Definition: uid.h:65
A type and helper functions for the Unique ID (UID) used in the RDM family of protocols.