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 
25 #ifndef RDM_UID_H_
26 #define RDM_UID_H_
27 
28 #include <stdbool.h>
29 #include <stdint.h>
30 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 
64 typedef struct RdmUid
65 {
66  uint16_t manu;
67  uint32_t id;
69 
70 /*************************** UID Comparison Macros ***************************/
71 
78 #define RDM_UID_EQUAL(uidptr1, uidptr2) ((uidptr1)->manu == (uidptr2)->manu && (uidptr1)->id == (uidptr2)->id)
79 
80 /************************* UID Initialization Macros *************************/
81 
88 #define RDM_INIT_STATIC_UID(uidptr, manu_val, id_val) \
89  do \
90  { \
91  (uidptr)->manu = (manu_val); \
92  (uidptr)->id = (id_val); \
93  } while (0)
94 
100 #define RDMNET_INIT_DYNAMIC_UID_REQUEST(uidptr, manu_val) \
101  do \
102  { \
103  (uidptr)->manu = (0x8000u | (manu_val)); \
104  (uidptr)->id = 0; \
105  } while (0)
106 
112 #define RDMNET_INIT_DEVICE_MANU_BROADCAST(uidptr, manu_val) \
113  do \
114  { \
115  (uidptr)->manu = kRdmnetDeviceBroadcastUid.manu; \
116  (uidptr)->id = (kRdmnetDeviceBroadcastUid.id & ((manu_val << 16) | 0xffffu)); \
117  } while (0)
118 
119 /**************************** UID Broadcast Macros ***************************/
120 
127 #define RDM_UID_IS_BROADCAST(uidptr) ((uidptr)->manu == kRdmBroadcastUid.manu && (uidptr)->id == kRdmBroadcastUid.id)
128 
135 #define RDMNET_UID_IS_CONTROLLER_BROADCAST(uidptr) \
136  ((uidptr)->manu == kRdmnetControllerBroadcastUid.manu && (uidptr)->id == kRdmnetControllerBroadcastUid.id)
137 
144 #define RDMNET_UID_IS_DEVICE_BROADCAST(uidptr) \
145  ((uidptr)->manu == kRdmnetDeviceBroadcastUid.manu && (uidptr)->id == kRdmnetDeviceBroadcastUid.id)
146 
159 #define RDMNET_UID_IS_DEVICE_MANU_BROADCAST(uidptr) \
160  ((uidptr)->manu == kRdmnetDeviceBroadcastUid.manu && (((uidptr)->id & 0xffffu) == 0xffffu))
161 
174 #define RDMNET_DEVICE_BROADCAST_MANU_MATCHES(uidptr, manu_val) (RDMNET_DEVICE_BROADCAST_MANU_ID(uidptr) == manu_val)
175 
185 #define RDMNET_DEVICE_BROADCAST_MANU_ID(uidptr) ((uint16_t)((uidptr)->id >> 16))
186 
187 /******************************* UID Inspection ******************************/
188 
197 #define RDM_UID_IS_NULL(uidptr) ((uidptr)->manu == 0 && (uidptr)->id == 0)
198 
205 #define RDMNET_UID_IS_DYNAMIC_UID_REQUEST(uidptr) ((((uidptr)->manu & 0x8000u) != 0) && (uidptr)->id == 0u)
206 
216 #define RDMNET_UID_IS_DYNAMIC(uidptr) \
217  ((((uidptr)->manu & 0x8000u) != 0) && !RDMNET_UID_IS_CONTROLLER_BROADCAST(uidptr) && \
218  !RDMNET_UID_IS_DEVICE_MANU_BROADCAST(uidptr) && !RDM_UID_IS_BROADCAST(uidptr) && \
219  !RDMNET_UID_IS_DYNAMIC_UID_REQUEST(uidptr))
220 
230 #define RDMNET_UID_IS_STATIC(uidptr) (!RDM_UID_IS_NULL(uidptr) && ((uidptr)->manu & 0x8000u) == 0)
231 
237 #define RDM_GET_MANUFACTURER_ID(uidptr) ((uidptr)->manu & 0x7fffu)
238 
244 #define RDM_GET_DEVICE_ID(uidptr) ((uidptr)->id)
245 
247 extern const RdmUid kRdmBroadcastUid;
248 
251 
253 extern const RdmUid kRdmnetDeviceBroadcastUid;
254 
256 #define RDM_UID_STRING_BYTES 14
257 
258 int rdm_uid_compare(const RdmUid* a, const RdmUid* b);
259 bool rdm_uid_to_string(const RdmUid* uid, char* buf);
260 bool rdm_string_to_uid(const char* str, RdmUid* uid);
261 
262 #ifdef __cplusplus
263 }
264 #endif
265 
266 #ifdef __cplusplus
267 /* C++ utilities */
268 
269 /* Comparison operators for UIDs */
270 
271 constexpr bool operator==(const RdmUid& a, const RdmUid& b) noexcept
272 {
273  return ((a.manu == b.manu && a.id == b.id));
274 }
275 
276 constexpr bool operator!=(const RdmUid& a, const RdmUid& b) noexcept
277 {
278  return !(a == b);
279 }
280 
281 constexpr bool operator<(const RdmUid& a, const RdmUid& b) noexcept
282 {
283  return ((a.manu == b.manu) ? (a.id < b.id) : (a.manu < b.manu));
284 }
285 
286 constexpr bool operator>(const RdmUid& a, const RdmUid& b) noexcept
287 {
288  return b < a;
289 }
290 
291 constexpr bool operator<=(const RdmUid& a, const RdmUid& b) noexcept
292 {
293  return !(b < a);
294 }
295 
296 constexpr bool operator>=(const RdmUid& a, const RdmUid& b) noexcept
297 {
298  return !(a < b);
299 }
300 
301 #endif /* __cplusplus */
306 #endif /* RDM_UID_H_ */
const RdmUid kRdmnetControllerBroadcastUid
Definition: uid.c:28
int rdm_uid_compare(const RdmUid *a, const RdmUid *b)
Compare two UIDs.
Definition: uid.c:51
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
struct RdmUid RdmUid
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
T operator!=(T... args)
Definition: uid.h:65
uint16_t manu
Definition: uid.h:66
uint32_t id
Definition: uid.h:67