Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the uint64 and int64 data types to the RDM messaging #1976

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions common/messaging/DescriptorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ using std::vector;
using ola::messaging::BoolFieldDescriptor;
using ola::messaging::FieldDescriptor;
using ola::messaging::FieldDescriptorGroup;
using ola::messaging::Int16FieldDescriptor;
using ola::messaging::Int32FieldDescriptor;
using ola::messaging::Int64FieldDescriptor;
using ola::messaging::Int8FieldDescriptor;
using ola::messaging::IPV4FieldDescriptor;
using ola::messaging::IPV6FieldDescriptor;
using ola::messaging::MACFieldDescriptor;
using ola::messaging::StringFieldDescriptor;
using ola::messaging::UIDFieldDescriptor;
using ola::messaging::UInt16FieldDescriptor;
using ola::messaging::UInt32FieldDescriptor;
using ola::messaging::UInt64FieldDescriptor;
using ola::messaging::UInt8FieldDescriptor;

class DescriptorTest: public CppUnit::TestFixture {
Expand Down Expand Up @@ -113,7 +118,7 @@ void DescriptorTest::testFieldDescriptors() {
OLA_ASSERT_EQ(1u, uint8_descriptor.MaxSize());
OLA_ASSERT_FALSE(uint8_descriptor.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(10),
uint8_descriptor.Multiplier());
uint8_descriptor.Multiplier());
OLA_ASSERT_TRUE(uint8_descriptor.FixedSize());
OLA_ASSERT_TRUE(uint8_descriptor.LimitedSize());

Expand All @@ -122,7 +127,7 @@ void DescriptorTest::testFieldDescriptors() {
OLA_ASSERT_EQ(1u, uint8_descriptor2.MaxSize());
OLA_ASSERT_TRUE(uint8_descriptor2.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
uint8_descriptor2.Multiplier());
uint8_descriptor2.Multiplier());
OLA_ASSERT_TRUE(uint8_descriptor2.FixedSize());
OLA_ASSERT_TRUE(uint8_descriptor2.LimitedSize());

Expand All @@ -132,7 +137,7 @@ void DescriptorTest::testFieldDescriptors() {
OLA_ASSERT_EQ(2u, uint16_descriptor.MaxSize());
OLA_ASSERT_FALSE(uint16_descriptor.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(10),
uint16_descriptor.Multiplier());
uint16_descriptor.Multiplier());
OLA_ASSERT_TRUE(uint16_descriptor.FixedSize());
OLA_ASSERT_TRUE(uint16_descriptor.LimitedSize());

Expand All @@ -141,7 +146,7 @@ void DescriptorTest::testFieldDescriptors() {
OLA_ASSERT_EQ(2u, uint16_descriptor2.MaxSize());
OLA_ASSERT_TRUE(uint16_descriptor2.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
uint16_descriptor2.Multiplier());
uint16_descriptor2.Multiplier());
OLA_ASSERT_TRUE(uint16_descriptor2.FixedSize());
OLA_ASSERT_TRUE(uint16_descriptor2.LimitedSize());

Expand All @@ -151,7 +156,7 @@ void DescriptorTest::testFieldDescriptors() {
OLA_ASSERT_EQ(4u, uint32_descriptor.MaxSize());
OLA_ASSERT_FALSE(uint32_descriptor.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(10),
uint32_descriptor.Multiplier());
uint32_descriptor.Multiplier());
OLA_ASSERT_TRUE(uint32_descriptor.FixedSize());
OLA_ASSERT_TRUE(uint32_descriptor.LimitedSize());

Expand All @@ -160,9 +165,28 @@ void DescriptorTest::testFieldDescriptors() {
OLA_ASSERT_EQ(4u, uint32_descriptor2.MaxSize());
OLA_ASSERT_TRUE(uint32_descriptor2.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
uint32_descriptor2.Multiplier());
uint32_descriptor2.Multiplier());
OLA_ASSERT_TRUE(uint32_descriptor2.FixedSize());
OLA_ASSERT_TRUE(uint32_descriptor2.LimitedSize());

// uint64_t
UInt64FieldDescriptor uint64_descriptor("uint64", false, 10);
OLA_ASSERT_EQ(string("uint64"), uint64_descriptor.Name());
OLA_ASSERT_EQ(8u, uint64_descriptor.MaxSize());
OLA_ASSERT_FALSE(uint64_descriptor.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(10),
uint64_descriptor.Multiplier());
OLA_ASSERT_TRUE(uint64_descriptor.FixedSize());
OLA_ASSERT_TRUE(uint64_descriptor.LimitedSize());

UInt64FieldDescriptor uint64_descriptor2("uint64", true, -1);
OLA_ASSERT_EQ(string("uint64"), uint64_descriptor2.Name());
OLA_ASSERT_EQ(8u, uint64_descriptor2.MaxSize());
OLA_ASSERT_TRUE(uint64_descriptor2.IsLittleEndian());
OLA_ASSERT_EQ(static_cast<int8_t>(-1),
uint64_descriptor2.Multiplier());
OLA_ASSERT_TRUE(uint64_descriptor2.FixedSize());
OLA_ASSERT_TRUE(uint64_descriptor2.LimitedSize());
}


Expand Down
22 changes: 20 additions & 2 deletions common/messaging/MessagePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ void GenericMessagePrinter::Visit(const BasicMessageField<uint32_t> *message) {
}


void GenericMessagePrinter::Visit(const BasicMessageField<uint64_t> *message) {
const UInt64FieldDescriptor *descriptor = message->GetDescriptor();
AppendUInt(descriptor->Name(),
message->Value(),
descriptor->LookupValue(message->Value()),
descriptor->Multiplier());
}


void GenericMessagePrinter::Visit(const BasicMessageField<int8_t> *message) {
const Int8FieldDescriptor *descriptor = message->GetDescriptor();
AppendInt(descriptor->Name(),
Expand Down Expand Up @@ -140,6 +149,15 @@ void GenericMessagePrinter::Visit(const BasicMessageField<int32_t> *message) {
}


void GenericMessagePrinter::Visit(const BasicMessageField<int64_t> *message) {
const Int64FieldDescriptor *descriptor = message->GetDescriptor();
AppendInt(descriptor->Name(),
message->Value(),
descriptor->LookupValue(message->Value()),
descriptor->Multiplier());
}


void GenericMessagePrinter::Visit(const GroupMessageField *message) {
Stream() << string(m_indent, ' ')
<< TransformLabel(message->GetDescriptor()->Name()) << " {" << endl;
Expand All @@ -155,7 +173,7 @@ void GenericMessagePrinter::PostVisit(const GroupMessageField *message) {


void GenericMessagePrinter::AppendUInt(const string &name,
unsigned int value,
uint64_t value,
const string &label,
int8_t multiplier) {
Stream() << string(m_indent, ' ') << TransformLabel(name) << ": ";
Expand All @@ -170,7 +188,7 @@ void GenericMessagePrinter::AppendUInt(const string &name,


void GenericMessagePrinter::AppendInt(const string &name,
int value,
int64_t value,
const string &label,
int8_t multiplier) {
Stream() << string(m_indent, ' ') << TransformLabel(name) << ": ";
Expand Down
7 changes: 6 additions & 1 deletion common/messaging/MessagePrinterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ using ola::messaging::UIDFieldDescriptor;
using ola::messaging::UIDMessageField;
using ola::messaging::UInt32FieldDescriptor;
using ola::messaging::UInt32MessageField;
using ola::messaging::UInt64FieldDescriptor;
using ola::messaging::UInt64MessageField;
using ola::messaging::UInt8FieldDescriptor;
using ola::messaging::UInt8MessageField;

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

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

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

Expand Down
17 changes: 17 additions & 0 deletions common/messaging/SchemaPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ void SchemaPrinter::Visit(const UInt32FieldDescriptor *descriptor) {
}


void SchemaPrinter::Visit(const UInt64FieldDescriptor *descriptor) {
AppendHeading(descriptor->Name(), "uint64");
MaybeAppendIntervals(descriptor->Intervals());
MaybeAppendLabels(descriptor->Labels());
m_str << endl;
}


void SchemaPrinter::Visit(const Int8FieldDescriptor *descriptor) {
AppendHeading(descriptor->Name(), "int8");
MaybeAppendIntervals(descriptor->Intervals());
Expand All @@ -112,6 +120,15 @@ void SchemaPrinter::Visit(const Int32FieldDescriptor *descriptor) {
m_str << endl;
}


void SchemaPrinter::Visit(const Int64FieldDescriptor *descriptor) {
AppendHeading(descriptor->Name(), "int64");
MaybeAppendIntervals(descriptor->Intervals());
MaybeAppendLabels(descriptor->Labels());
m_str << endl;
}


void SchemaPrinter::Visit(const FieldDescriptorGroup *descriptor) {
m_str << string(m_indent, ' ') << descriptor->Name() << " {" << endl;
m_indent += m_indent_size;
Expand Down
5 changes: 5 additions & 0 deletions common/messaging/SchemaPrinterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ using ola::messaging::SchemaPrinter;
using ola::messaging::StringFieldDescriptor;
using ola::messaging::UInt16FieldDescriptor;
using ola::messaging::UInt32FieldDescriptor;
using ola::messaging::UInt64FieldDescriptor;
using ola::messaging::UInt8FieldDescriptor;
using ola::messaging::Int16FieldDescriptor;
using ola::messaging::Int32FieldDescriptor;
using ola::messaging::Int64FieldDescriptor;
using ola::messaging::Int8FieldDescriptor;
using ola::messaging::UIDFieldDescriptor;

Expand Down Expand Up @@ -235,5 +237,8 @@ void SchemaPrinterTest::testIntervalTypes() {
OLA_ASSERT_EQ(
string("Count: int32: (-70000, 82560)\n"),
GenerateIntervalString<Int32FieldDescriptor>(-70000, 82560));
OLA_ASSERT_EQ(
string("Count: int64: (-7000000000, 8256123456)\n"),
GenerateIntervalString<Int64FieldDescriptor>(-7000000000, 8256123456));
}

77 changes: 77 additions & 0 deletions common/network/NetworkUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ inline bool IsBigEndian() {
#endif // defined(HAVE_ENDIAN_H) && defined(__BIG_ENDIAN)
}

inline uint64_t ByteSwap64(uint64_t value) {
return ((value & 0x00000000000000ff) << 56) |
((value & 0x000000000000ff00) << 40) |
((value & 0x0000000000ff0000) << 24) |
((value & 0x00000000ff000000) << 8) |
((value & 0x000000ff00000000) >> 8) |
((value & 0x0000ff0000000000) >> 24) |
((value & 0x00ff000000000000) >> 40) |
((value & 0xff00000000000000) >> 56);
}

inline uint32_t ByteSwap32(uint32_t value) {
return ((value & 0x000000ff) << 24) |
((value & 0x0000ff00) << 8) |
Expand Down Expand Up @@ -148,6 +159,14 @@ uint32_t NetworkToHost(uint32_t value) {
return ntohl(value);
}

uint64_t NetworkToHost(uint64_t value) {
#ifdef HAVE_ENDIAN_H
return be64toh(value);
#else
#error "No be64toh for NetworkToHost, please report this."
#endif // HAVE_ENDIAN_H
}

int16_t NetworkToHost(int16_t value) {
return ntohs(value);
}
Expand All @@ -156,6 +175,14 @@ int32_t NetworkToHost(int32_t value) {
return ntohl(value);
}

int64_t NetworkToHost(int64_t value) {
#ifdef HAVE_ENDIAN_H
return be64toh(value);
#else
#error "No be64toh for NetworkToHost, please report this."
#endif // HAVE_ENDIAN_H
}

uint16_t HostToNetwork(uint16_t value) {
return htons(value);
}
Expand All @@ -172,6 +199,22 @@ int32_t HostToNetwork(int32_t value) {
return htonl(value);
}

uint64_t HostToNetwork(uint64_t value) {
#ifdef HAVE_ENDIAN_H
return htobe64(value);
#else
#error "No htobe64 for HostToNetwork, please report this."
#endif // HAVE_ENDIAN_H
}

int64_t HostToNetwork(int64_t value) {
#ifdef HAVE_ENDIAN_H
return htobe64(value);
#else
#error "No htobe64 for HostToNetwork, please report this."
#endif // HAVE_ENDIAN_H
}

uint16_t HostToLittleEndian(uint16_t value) {
if (IsBigEndian()) {
return ByteSwap16(value);
Expand Down Expand Up @@ -204,6 +247,22 @@ int32_t HostToLittleEndian(int32_t value) {
}
}

uint64_t HostToLittleEndian(uint64_t value) {
if (IsBigEndian()) {
return ByteSwap64(value);
} else {
return value;
}
}

int64_t HostToLittleEndian(int64_t value) {
if (IsBigEndian()) {
return ByteSwap64(value);
} else {
return value;
}
}

uint16_t LittleEndianToHost(uint16_t value) {
if (IsBigEndian()) {
return ByteSwap16(value);
Expand Down Expand Up @@ -239,6 +298,24 @@ int32_t LittleEndianToHost(int32_t value) {
}
}


uint64_t LittleEndianToHost(uint64_t value) {
if (IsBigEndian()) {
return ByteSwap64(value);
} else {
return value;
}
}


int64_t LittleEndianToHost(int64_t value) {
if (IsBigEndian()) {
return ByteSwap64(value);
} else {
return value;
}
}

string HostnameFromFQDN(const string &fqdn) {
string::size_type first_dot = fqdn.find_first_of(".");
if (first_dot == string::npos) {
Expand Down
18 changes: 14 additions & 4 deletions common/network/NetworkUtilsTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ void NetworkUtilsTest::tearDown() {
#endif // _WIN32
}


/*
* Check that we can convert to/from network byte order
*/
Expand All @@ -101,6 +102,9 @@ void NetworkUtilsTest::testToFromNetwork() {

uint32_t v3 = 0x01020304;
OLA_ASSERT_EQ(v3, NetworkToHost(HostToNetwork(v3)));

uint64_t v4 = 0x0102030405060708;
OLA_ASSERT_EQ(v4, NetworkToHost(HostToNetwork(v4)));
}


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

int8_t v4 = -10;
OLA_ASSERT_EQ(v4, HostToLittleEndian(v4));
uint64_t v4 = 0x0102030405060708;
OLA_ASSERT_EQ(v4, LittleEndianToHost(HostToLittleEndian(v4)));

int16_t v5 = -0x0102;
int8_t v5 = -10;
OLA_ASSERT_EQ(v5, HostToLittleEndian(v5));
OLA_ASSERT_EQ(v5, LittleEndianToHost(HostToLittleEndian(v5)));

int32_t v6 = -0x01020304;
int16_t v6 = -0x0102;
OLA_ASSERT_EQ(v6, LittleEndianToHost(HostToLittleEndian(v6)));

int32_t v7 = -0x01020304;
OLA_ASSERT_EQ(v7, LittleEndianToHost(HostToLittleEndian(v7)));

int64_t v8 = -0x0102030405060708;
OLA_ASSERT_EQ(v8, LittleEndianToHost(HostToLittleEndian(v8)));
}


Expand Down
Loading
Loading