Skip to content

Commit c4adb50

Browse files
authored
Merge pull request #1976 from peternewman/uint64-type
Add support for the uint64 and int64 data types to the RDM messaging
2 parents 568e20c + 2016da6 commit c4adb50

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+612
-110
lines changed

common/messaging/DescriptorTest.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,18 @@ using std::vector;
3333
using ola::messaging::BoolFieldDescriptor;
3434
using ola::messaging::FieldDescriptor;
3535
using ola::messaging::FieldDescriptorGroup;
36+
using ola::messaging::Int16FieldDescriptor;
37+
using ola::messaging::Int32FieldDescriptor;
38+
using ola::messaging::Int64FieldDescriptor;
39+
using ola::messaging::Int8FieldDescriptor;
3640
using ola::messaging::IPV4FieldDescriptor;
3741
using ola::messaging::IPV6FieldDescriptor;
3842
using ola::messaging::MACFieldDescriptor;
3943
using ola::messaging::StringFieldDescriptor;
4044
using ola::messaging::UIDFieldDescriptor;
4145
using ola::messaging::UInt16FieldDescriptor;
4246
using ola::messaging::UInt32FieldDescriptor;
47+
using ola::messaging::UInt64FieldDescriptor;
4348
using ola::messaging::UInt8FieldDescriptor;
4449

4550
class DescriptorTest: public CppUnit::TestFixture {
@@ -113,7 +118,7 @@ void DescriptorTest::testFieldDescriptors() {
113118
OLA_ASSERT_EQ(1u, uint8_descriptor.MaxSize());
114119
OLA_ASSERT_FALSE(uint8_descriptor.IsLittleEndian());
115120
OLA_ASSERT_EQ(static_cast<int8_t>(10),
116-
uint8_descriptor.Multiplier());
121+
uint8_descriptor.Multiplier());
117122
OLA_ASSERT_TRUE(uint8_descriptor.FixedSize());
118123
OLA_ASSERT_TRUE(uint8_descriptor.LimitedSize());
119124

@@ -122,7 +127,7 @@ void DescriptorTest::testFieldDescriptors() {
122127
OLA_ASSERT_EQ(1u, uint8_descriptor2.MaxSize());
123128
OLA_ASSERT_TRUE(uint8_descriptor2.IsLittleEndian());
124129
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
125-
uint8_descriptor2.Multiplier());
130+
uint8_descriptor2.Multiplier());
126131
OLA_ASSERT_TRUE(uint8_descriptor2.FixedSize());
127132
OLA_ASSERT_TRUE(uint8_descriptor2.LimitedSize());
128133

@@ -132,7 +137,7 @@ void DescriptorTest::testFieldDescriptors() {
132137
OLA_ASSERT_EQ(2u, uint16_descriptor.MaxSize());
133138
OLA_ASSERT_FALSE(uint16_descriptor.IsLittleEndian());
134139
OLA_ASSERT_EQ(static_cast<int8_t>(10),
135-
uint16_descriptor.Multiplier());
140+
uint16_descriptor.Multiplier());
136141
OLA_ASSERT_TRUE(uint16_descriptor.FixedSize());
137142
OLA_ASSERT_TRUE(uint16_descriptor.LimitedSize());
138143

@@ -141,7 +146,7 @@ void DescriptorTest::testFieldDescriptors() {
141146
OLA_ASSERT_EQ(2u, uint16_descriptor2.MaxSize());
142147
OLA_ASSERT_TRUE(uint16_descriptor2.IsLittleEndian());
143148
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
144-
uint16_descriptor2.Multiplier());
149+
uint16_descriptor2.Multiplier());
145150
OLA_ASSERT_TRUE(uint16_descriptor2.FixedSize());
146151
OLA_ASSERT_TRUE(uint16_descriptor2.LimitedSize());
147152

@@ -151,7 +156,7 @@ void DescriptorTest::testFieldDescriptors() {
151156
OLA_ASSERT_EQ(4u, uint32_descriptor.MaxSize());
152157
OLA_ASSERT_FALSE(uint32_descriptor.IsLittleEndian());
153158
OLA_ASSERT_EQ(static_cast<int8_t>(10),
154-
uint32_descriptor.Multiplier());
159+
uint32_descriptor.Multiplier());
155160
OLA_ASSERT_TRUE(uint32_descriptor.FixedSize());
156161
OLA_ASSERT_TRUE(uint32_descriptor.LimitedSize());
157162

@@ -160,9 +165,28 @@ void DescriptorTest::testFieldDescriptors() {
160165
OLA_ASSERT_EQ(4u, uint32_descriptor2.MaxSize());
161166
OLA_ASSERT_TRUE(uint32_descriptor2.IsLittleEndian());
162167
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
163-
uint32_descriptor2.Multiplier());
168+
uint32_descriptor2.Multiplier());
164169
OLA_ASSERT_TRUE(uint32_descriptor2.FixedSize());
165170
OLA_ASSERT_TRUE(uint32_descriptor2.LimitedSize());
171+
172+
// uint64_t
173+
UInt64FieldDescriptor uint64_descriptor("uint64", false, 10);
174+
OLA_ASSERT_EQ(string("uint64"), uint64_descriptor.Name());
175+
OLA_ASSERT_EQ(8u, uint64_descriptor.MaxSize());
176+
OLA_ASSERT_FALSE(uint64_descriptor.IsLittleEndian());
177+
OLA_ASSERT_EQ(static_cast<int8_t>(10),
178+
uint64_descriptor.Multiplier());
179+
OLA_ASSERT_TRUE(uint64_descriptor.FixedSize());
180+
OLA_ASSERT_TRUE(uint64_descriptor.LimitedSize());
181+
182+
UInt64FieldDescriptor uint64_descriptor2("uint64", true, -1);
183+
OLA_ASSERT_EQ(string("uint64"), uint64_descriptor2.Name());
184+
OLA_ASSERT_EQ(8u, uint64_descriptor2.MaxSize());
185+
OLA_ASSERT_TRUE(uint64_descriptor2.IsLittleEndian());
186+
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
187+
uint64_descriptor2.Multiplier());
188+
OLA_ASSERT_TRUE(uint64_descriptor2.FixedSize());
189+
OLA_ASSERT_TRUE(uint64_descriptor2.LimitedSize());
166190
}
167191

168192

common/messaging/MessagePrinter.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,15 @@ void GenericMessagePrinter::Visit(const BasicMessageField<uint32_t> *message) {
113113
}
114114

115115

116+
void GenericMessagePrinter::Visit(const BasicMessageField<uint64_t> *message) {
117+
const UInt64FieldDescriptor *descriptor = message->GetDescriptor();
118+
AppendUInt(descriptor->Name(),
119+
message->Value(),
120+
descriptor->LookupValue(message->Value()),
121+
descriptor->Multiplier());
122+
}
123+
124+
116125
void GenericMessagePrinter::Visit(const BasicMessageField<int8_t> *message) {
117126
const Int8FieldDescriptor *descriptor = message->GetDescriptor();
118127
AppendInt(descriptor->Name(),
@@ -140,6 +149,15 @@ void GenericMessagePrinter::Visit(const BasicMessageField<int32_t> *message) {
140149
}
141150

142151

152+
void GenericMessagePrinter::Visit(const BasicMessageField<int64_t> *message) {
153+
const Int64FieldDescriptor *descriptor = message->GetDescriptor();
154+
AppendInt(descriptor->Name(),
155+
message->Value(),
156+
descriptor->LookupValue(message->Value()),
157+
descriptor->Multiplier());
158+
}
159+
160+
143161
void GenericMessagePrinter::Visit(const GroupMessageField *message) {
144162
Stream() << string(m_indent, ' ')
145163
<< TransformLabel(message->GetDescriptor()->Name()) << " {" << endl;
@@ -155,7 +173,7 @@ void GenericMessagePrinter::PostVisit(const GroupMessageField *message) {
155173

156174

157175
void GenericMessagePrinter::AppendUInt(const string &name,
158-
unsigned int value,
176+
uint64_t value,
159177
const string &label,
160178
int8_t multiplier) {
161179
Stream() << string(m_indent, ' ') << TransformLabel(name) << ": ";
@@ -170,7 +188,7 @@ void GenericMessagePrinter::AppendUInt(const string &name,
170188

171189

172190
void GenericMessagePrinter::AppendInt(const string &name,
173-
int value,
191+
int64_t value,
174192
const string &label,
175193
int8_t multiplier) {
176194
Stream() << string(m_indent, ' ') << TransformLabel(name) << ": ";

common/messaging/MessagePrinterTest.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ using ola::messaging::UIDFieldDescriptor;
6161
using ola::messaging::UIDMessageField;
6262
using ola::messaging::UInt32FieldDescriptor;
6363
using ola::messaging::UInt32MessageField;
64+
using ola::messaging::UInt64FieldDescriptor;
65+
using ola::messaging::UInt64MessageField;
6466
using ola::messaging::UInt8FieldDescriptor;
6567
using ola::messaging::UInt8MessageField;
6668

@@ -101,6 +103,7 @@ void GenericMessagePrinterTest::testSimplePrinter() {
101103
UInt8FieldDescriptor uint8_descriptor("Count", false, -3);
102104
Int8FieldDescriptor int8_descriptor("Delta", false, 1);
103105
Int16FieldDescriptor int16_descriptor("Rate", false, -1);
106+
UInt64FieldDescriptor uint64_descriptor("Data");
104107

105108
// try a simple print first
106109
vector<const ola::messaging::MessageFieldInterface*> fields;
@@ -120,12 +123,14 @@ void GenericMessagePrinterTest::testSimplePrinter() {
120123
fields.push_back(new UInt8MessageField(&uint8_descriptor, 4));
121124
fields.push_back(new Int8MessageField(&int8_descriptor, 10));
122125
fields.push_back(new Int16MessageField(&int16_descriptor, 10));
126+
fields.push_back(new UInt64MessageField(&uint64_descriptor, 424242424242));
123127

124128
Message message(fields);
125129
string expected = (
126130
"On/Off: false\nip: 10.0.0.1\nipv6: ::ffff:192.168.0.1\n"
127131
"mac: 01:23:45:67:89:ab\nuid: 7a70:00000001\nName: foobar\nId: 42\n"
128-
"Count: 4 x 10 ^ -3\nDelta: 10 x 10 ^ 1\nRate: 10 x 10 ^ -1\n");
132+
"Count: 4 x 10 ^ -3\nDelta: 10 x 10 ^ 1\nRate: 10 x 10 ^ -1\n"
133+
"Data: 424242424242\n");
129134
OLA_ASSERT_EQ(expected, m_printer.AsString(&message));
130135
}
131136

common/messaging/SchemaPrinter.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ void SchemaPrinter::Visit(const UInt32FieldDescriptor *descriptor) {
8989
}
9090

9191

92+
void SchemaPrinter::Visit(const UInt64FieldDescriptor *descriptor) {
93+
AppendHeading(descriptor->Name(), "uint64");
94+
MaybeAppendIntervals(descriptor->Intervals());
95+
MaybeAppendLabels(descriptor->Labels());
96+
m_str << endl;
97+
}
98+
99+
92100
void SchemaPrinter::Visit(const Int8FieldDescriptor *descriptor) {
93101
AppendHeading(descriptor->Name(), "int8");
94102
MaybeAppendIntervals(descriptor->Intervals());
@@ -112,6 +120,15 @@ void SchemaPrinter::Visit(const Int32FieldDescriptor *descriptor) {
112120
m_str << endl;
113121
}
114122

123+
124+
void SchemaPrinter::Visit(const Int64FieldDescriptor *descriptor) {
125+
AppendHeading(descriptor->Name(), "int64");
126+
MaybeAppendIntervals(descriptor->Intervals());
127+
MaybeAppendLabels(descriptor->Labels());
128+
m_str << endl;
129+
}
130+
131+
115132
void SchemaPrinter::Visit(const FieldDescriptorGroup *descriptor) {
116133
m_str << string(m_indent, ' ') << descriptor->Name() << " {" << endl;
117134
m_indent += m_indent_size;

common/messaging/SchemaPrinterTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ using ola::messaging::SchemaPrinter;
4242
using ola::messaging::StringFieldDescriptor;
4343
using ola::messaging::UInt16FieldDescriptor;
4444
using ola::messaging::UInt32FieldDescriptor;
45+
using ola::messaging::UInt64FieldDescriptor;
4546
using ola::messaging::UInt8FieldDescriptor;
4647
using ola::messaging::Int16FieldDescriptor;
4748
using ola::messaging::Int32FieldDescriptor;
49+
using ola::messaging::Int64FieldDescriptor;
4850
using ola::messaging::Int8FieldDescriptor;
4951
using ola::messaging::UIDFieldDescriptor;
5052

@@ -235,5 +237,8 @@ void SchemaPrinterTest::testIntervalTypes() {
235237
OLA_ASSERT_EQ(
236238
string("Count: int32: (-70000, 82560)\n"),
237239
GenerateIntervalString<Int32FieldDescriptor>(-70000, 82560));
240+
OLA_ASSERT_EQ(
241+
string("Count: int64: (-7000000000, 8256123456)\n"),
242+
GenerateIntervalString<Int64FieldDescriptor>(-7000000000, 8256123456));
238243
}
239244

common/network/NetworkUtils.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ inline bool IsBigEndian() {
106106
#endif // defined(HAVE_ENDIAN_H) && defined(__BIG_ENDIAN)
107107
}
108108

109+
inline uint64_t ByteSwap64(uint64_t value) {
110+
return ((value & 0x00000000000000ff) << 56) |
111+
((value & 0x000000000000ff00) << 40) |
112+
((value & 0x0000000000ff0000) << 24) |
113+
((value & 0x00000000ff000000) << 8) |
114+
((value & 0x000000ff00000000) >> 8) |
115+
((value & 0x0000ff0000000000) >> 24) |
116+
((value & 0x00ff000000000000) >> 40) |
117+
((value & 0xff00000000000000) >> 56);
118+
}
119+
109120
inline uint32_t ByteSwap32(uint32_t value) {
110121
return ((value & 0x000000ff) << 24) |
111122
((value & 0x0000ff00) << 8) |
@@ -148,6 +159,14 @@ uint32_t NetworkToHost(uint32_t value) {
148159
return ntohl(value);
149160
}
150161

162+
uint64_t NetworkToHost(uint64_t value) {
163+
#ifdef HAVE_ENDIAN_H
164+
return be64toh(value);
165+
#else
166+
#error "No be64toh for NetworkToHost, please report this."
167+
#endif // HAVE_ENDIAN_H
168+
}
169+
151170
int16_t NetworkToHost(int16_t value) {
152171
return ntohs(value);
153172
}
@@ -156,6 +175,14 @@ int32_t NetworkToHost(int32_t value) {
156175
return ntohl(value);
157176
}
158177

178+
int64_t NetworkToHost(int64_t value) {
179+
#ifdef HAVE_ENDIAN_H
180+
return be64toh(value);
181+
#else
182+
#error "No be64toh for NetworkToHost, please report this."
183+
#endif // HAVE_ENDIAN_H
184+
}
185+
159186
uint16_t HostToNetwork(uint16_t value) {
160187
return htons(value);
161188
}
@@ -172,6 +199,22 @@ int32_t HostToNetwork(int32_t value) {
172199
return htonl(value);
173200
}
174201

202+
uint64_t HostToNetwork(uint64_t value) {
203+
#ifdef HAVE_ENDIAN_H
204+
return htobe64(value);
205+
#else
206+
#error "No htobe64 for HostToNetwork, please report this."
207+
#endif // HAVE_ENDIAN_H
208+
}
209+
210+
int64_t HostToNetwork(int64_t value) {
211+
#ifdef HAVE_ENDIAN_H
212+
return htobe64(value);
213+
#else
214+
#error "No htobe64 for HostToNetwork, please report this."
215+
#endif // HAVE_ENDIAN_H
216+
}
217+
175218
uint16_t HostToLittleEndian(uint16_t value) {
176219
if (IsBigEndian()) {
177220
return ByteSwap16(value);
@@ -204,6 +247,22 @@ int32_t HostToLittleEndian(int32_t value) {
204247
}
205248
}
206249

250+
uint64_t HostToLittleEndian(uint64_t value) {
251+
if (IsBigEndian()) {
252+
return ByteSwap64(value);
253+
} else {
254+
return value;
255+
}
256+
}
257+
258+
int64_t HostToLittleEndian(int64_t value) {
259+
if (IsBigEndian()) {
260+
return ByteSwap64(value);
261+
} else {
262+
return value;
263+
}
264+
}
265+
207266
uint16_t LittleEndianToHost(uint16_t value) {
208267
if (IsBigEndian()) {
209268
return ByteSwap16(value);
@@ -239,6 +298,24 @@ int32_t LittleEndianToHost(int32_t value) {
239298
}
240299
}
241300

301+
302+
uint64_t LittleEndianToHost(uint64_t value) {
303+
if (IsBigEndian()) {
304+
return ByteSwap64(value);
305+
} else {
306+
return value;
307+
}
308+
}
309+
310+
311+
int64_t LittleEndianToHost(int64_t value) {
312+
if (IsBigEndian()) {
313+
return ByteSwap64(value);
314+
} else {
315+
return value;
316+
}
317+
}
318+
242319
string HostnameFromFQDN(const string &fqdn) {
243320
string::size_type first_dot = fqdn.find_first_of(".");
244321
if (first_dot == string::npos) {

common/network/NetworkUtilsTest.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void NetworkUtilsTest::tearDown() {
8888
#endif // _WIN32
8989
}
9090

91+
9192
/*
9293
* Check that we can convert to/from network byte order
9394
*/
@@ -101,6 +102,9 @@ void NetworkUtilsTest::testToFromNetwork() {
101102

102103
uint32_t v3 = 0x01020304;
103104
OLA_ASSERT_EQ(v3, NetworkToHost(HostToNetwork(v3)));
105+
106+
uint64_t v4 = 0x0102030405060708;
107+
OLA_ASSERT_EQ(v4, NetworkToHost(HostToNetwork(v4)));
104108
}
105109

106110

@@ -118,15 +122,21 @@ void NetworkUtilsTest::testToFromLittleEndian() {
118122
uint32_t v3 = 0x01020304;
119123
OLA_ASSERT_EQ(v3, LittleEndianToHost(HostToLittleEndian(v3)));
120124

121-
int8_t v4 = -10;
122-
OLA_ASSERT_EQ(v4, HostToLittleEndian(v4));
125+
uint64_t v4 = 0x0102030405060708;
123126
OLA_ASSERT_EQ(v4, LittleEndianToHost(HostToLittleEndian(v4)));
124127

125-
int16_t v5 = -0x0102;
128+
int8_t v5 = -10;
129+
OLA_ASSERT_EQ(v5, HostToLittleEndian(v5));
126130
OLA_ASSERT_EQ(v5, LittleEndianToHost(HostToLittleEndian(v5)));
127131

128-
int32_t v6 = -0x01020304;
132+
int16_t v6 = -0x0102;
129133
OLA_ASSERT_EQ(v6, LittleEndianToHost(HostToLittleEndian(v6)));
134+
135+
int32_t v7 = -0x01020304;
136+
OLA_ASSERT_EQ(v7, LittleEndianToHost(HostToLittleEndian(v7)));
137+
138+
int64_t v8 = -0x0102030405060708;
139+
OLA_ASSERT_EQ(v8, LittleEndianToHost(HostToLittleEndian(v8)));
130140
}
131141

132142

0 commit comments

Comments
 (0)