Skip to content

Commit 7e83985

Browse files
committed
Allow an LLRPProbeReplyPDU to be sent
(cherry picked from commit d972695)
1 parent 0fff671 commit 7e83985

File tree

3 files changed

+147
-1
lines changed

3 files changed

+147
-1
lines changed

libs/acn/LLRPProbeReplyPDU.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,26 @@ using ola::network::HostToNetwork;
3232
using ola::network::MACAddress;
3333
using ola::rdm::UID;
3434

35+
bool LLRPProbeReplyPDU::PackData(uint8_t *data, unsigned int *length) const {
36+
llrp_probe_reply_pdu_data pdu_data;
37+
m_target_uid.Pack(pdu_data.target_uid, sizeof(pdu_data.target_uid));
38+
m_hardware_address.Pack(pdu_data.hardware_address, sizeof(pdu_data.hardware_address));
39+
pdu_data.type = HostToNetwork(static_cast<uint8_t>(m_type));
40+
41+
*length = sizeof(llrp_probe_reply_pdu_data);
42+
memcpy(data, &pdu_data, *length);
43+
return true;
44+
}
45+
46+
void LLRPProbeReplyPDU::PackData(ola::io::OutputStream *stream) const {
47+
llrp_probe_reply_pdu_data data;
48+
m_target_uid.Pack(data.target_uid, sizeof(data.target_uid));
49+
m_hardware_address.Pack(data.hardware_address, sizeof(data.hardware_address));
50+
data.type = HostToNetwork(static_cast<uint8_t>(m_type));
51+
stream->Write(reinterpret_cast<uint8_t*>(&data),
52+
static_cast<unsigned int>(sizeof(llrp_probe_reply_pdu_data)));
53+
}
54+
3555
void LLRPProbeReplyPDU::PrependPDU(ola::io::IOStack *stack,
3656
const UID &target_uid,
3757
const MACAddress &hardware_address,

libs/acn/LLRPProbeReplyPDU.h

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
namespace ola {
3131
namespace acn {
3232

33-
class LLRPProbeReplyPDU : private PDU {
33+
class LLRPProbeReplyPDU : public PDU {
3434
public:
3535
typedef enum {
3636
LLRP_COMPONENT_TYPE_RPT_DEVICE = 0, /**< Device */
@@ -39,6 +39,27 @@ class LLRPProbeReplyPDU : private PDU {
3939
LLRP_COMPONENT_TYPE_NON_RDMNET = 0xff, /**< Non-RDMnet */
4040
} LLRPComponentType;
4141

42+
explicit LLRPProbeReplyPDU(unsigned int vector,
43+
const ola::rdm::UID &target_uid,
44+
const ola::network::MACAddress &hardware_address,
45+
const LLRPComponentType type):
46+
PDU(vector, ONE_BYTE, true),
47+
m_target_uid(target_uid),
48+
m_hardware_address(hardware_address),
49+
m_type(type) {}
50+
51+
unsigned int HeaderSize() const { return 0; }
52+
bool PackHeader(OLA_UNUSED uint8_t *data,
53+
unsigned int *length) const {
54+
*length = 0;
55+
return true;
56+
}
57+
void PackHeader(OLA_UNUSED ola::io::OutputStream *stream) const {}
58+
59+
unsigned int DataSize() const { return sizeof(llrp_probe_reply_pdu_data); }
60+
bool PackData(uint8_t *data, unsigned int *length) const;
61+
void PackData(ola::io::OutputStream *stream) const;
62+
4263
static void PrependPDU(ola::io::IOStack *stack,
4364
const ola::rdm::UID &target_uid,
4465
const ola::network::MACAddress &hardware_address,
@@ -53,6 +74,11 @@ class LLRPProbeReplyPDU : private PDU {
5374
uint8_t type;
5475
});
5576
typedef struct llrp_probe_reply_pdu_data_s llrp_probe_reply_pdu_data;
77+
78+
private:
79+
const ola::rdm::UID m_target_uid;
80+
const ola::network::MACAddress m_hardware_address;
81+
const LLRPComponentType m_type;
5682
};
5783
} // namespace acn
5884
} // namespace ola

libs/acn/LLRPProbeReplyPDUTest.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
#include <string>
2424

2525
#include "ola/Logging.h"
26+
#include "ola/io/IOQueue.h"
2627
#include "ola/io/IOStack.h"
28+
#include "ola/io/OutputStream.h"
2729
#include "ola/network/NetworkUtils.h"
2830
#include "ola/rdm/UID.h"
2931
#include "ola/rdm/UIDSet.h"
@@ -35,16 +37,23 @@ namespace ola {
3537
namespace acn {
3638

3739
using ola::acn::LLRPProbeReplyPDU;
40+
using ola::io::IOQueue;
3841
using ola::io::IOStack;
42+
using ola::io::OutputStream;
43+
using ola::network::HostToNetwork;
3944
using ola::network::MACAddress;
4045
using ola::rdm::UID;
4146

4247
class LLRPProbeReplyPDUTest: public CppUnit::TestFixture {
4348
CPPUNIT_TEST_SUITE(LLRPProbeReplyPDUTest);
49+
CPPUNIT_TEST(testSimpleLLRPProbeReplyPDU);
50+
CPPUNIT_TEST(testSimpleLLRPProbeReplyPDUToOutputStream);
4451
CPPUNIT_TEST(testPrepend);
4552
CPPUNIT_TEST_SUITE_END();
4653

4754
public:
55+
void testSimpleLLRPProbeReplyPDU();
56+
void testSimpleLLRPProbeReplyPDUToOutputStream();
4857
void testPrepend();
4958

5059
private:
@@ -53,6 +62,97 @@ class LLRPProbeReplyPDUTest: public CppUnit::TestFixture {
5362

5463
CPPUNIT_TEST_SUITE_REGISTRATION(LLRPProbeReplyPDUTest);
5564

65+
const unsigned int LLRPProbeReplyPDUTest::TEST_VECTOR = 39;
66+
67+
68+
/*
69+
* Test that packing a LLRPProbeReplyPDU works.
70+
*/
71+
void LLRPProbeReplyPDUTest::testSimpleLLRPProbeReplyPDU() {
72+
UID target_uid = UID(0x4321, 0x12345678);
73+
MACAddress hardware_address;
74+
MACAddress::FromString("01:23:45:67:89:ab", &hardware_address);
75+
LLRPProbeReplyPDU pdu(
76+
TEST_VECTOR,
77+
target_uid,
78+
hardware_address,
79+
LLRPProbeReplyPDU::LLRP_COMPONENT_TYPE_NON_RDMNET);
80+
81+
OLA_ASSERT_EQ(0u, pdu.HeaderSize());
82+
OLA_ASSERT_EQ(13u, pdu.DataSize());
83+
OLA_ASSERT_EQ(17u, pdu.Size());
84+
85+
unsigned int size = pdu.Size();
86+
uint8_t *data = new uint8_t[size];
87+
unsigned int bytes_used = size;
88+
OLA_ASSERT(pdu.Pack(data, &bytes_used));
89+
OLA_ASSERT_EQ(size, bytes_used);
90+
91+
// spot check the data
92+
OLA_ASSERT_EQ((uint8_t) 0xf0, data[0]);
93+
// bytes_used is technically data[1] and data[2] if > 255
94+
OLA_ASSERT_EQ((uint8_t) bytes_used, data[2]);
95+
OLA_ASSERT_EQ(HostToNetwork((uint8_t) TEST_VECTOR), data[3]);
96+
97+
uint8_t buffer[UID::LENGTH];
98+
target_uid.Pack(buffer, sizeof(buffer));
99+
OLA_ASSERT_DATA_EQUALS(&data[4], UID::LENGTH, buffer, sizeof(buffer));
100+
uint8_t buffer2[MACAddress::LENGTH];
101+
hardware_address.Pack(buffer2, sizeof(buffer2));
102+
OLA_ASSERT_DATA_EQUALS(&data[10], MACAddress::LENGTH, buffer2, sizeof(buffer2));
103+
104+
// test undersized buffer
105+
bytes_used = size - 1;
106+
OLA_ASSERT_FALSE(pdu.Pack(data, &bytes_used));
107+
OLA_ASSERT_EQ(0u, bytes_used);
108+
109+
// test oversized buffer
110+
bytes_used = size + 1;
111+
OLA_ASSERT(pdu.Pack(data, &bytes_used));
112+
OLA_ASSERT_EQ(size, bytes_used);
113+
delete[] data;
114+
}
115+
116+
117+
/*
118+
* Test that writing to an output stream works.
119+
*/
120+
void LLRPProbeReplyPDUTest::testSimpleLLRPProbeReplyPDUToOutputStream() {
121+
UID target_uid = UID(0x4321, 0x12345678);
122+
MACAddress hardware_address;
123+
MACAddress::FromString("01:23:45:67:89:ab", &hardware_address);
124+
LLRPProbeReplyPDU pdu(
125+
TEST_VECTOR,
126+
target_uid,
127+
hardware_address,
128+
LLRPProbeReplyPDU::LLRP_COMPONENT_TYPE_NON_RDMNET);
129+
130+
OLA_ASSERT_EQ(0u, pdu.HeaderSize());
131+
OLA_ASSERT_EQ(13u, pdu.DataSize());
132+
OLA_ASSERT_EQ(17u, pdu.Size());
133+
134+
IOQueue output;
135+
OutputStream stream(&output);
136+
pdu.Write(&stream);
137+
OLA_ASSERT_EQ(17u, output.Size());
138+
139+
uint8_t *pdu_data = new uint8_t[output.Size()];
140+
unsigned int pdu_size = output.Peek(pdu_data, output.Size());
141+
OLA_ASSERT_EQ(output.Size(), pdu_size);
142+
143+
uint8_t EXPECTED[] = {
144+
0xf0, 0x00, 0x11,
145+
39,
146+
0x43, 0x21, 0x12, 0x34, 0x56, 0x78,
147+
0x01, 0x23, 0x45, 0x67, 0x89, 0xab,
148+
0xff
149+
};
150+
OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), pdu_data, pdu_size);
151+
output.Pop(output.Size());
152+
delete[] pdu_data;
153+
}
154+
155+
56156
void LLRPProbeReplyPDUTest::testPrepend() {
57157
IOStack stack;
58158
UID target_uid = UID(0x4321, 0x12345678);

0 commit comments

Comments
 (0)