Skip to content

Commit a81c83d

Browse files
committed
First attempt at BrokerClientEntryPDU
(cherry picked from commit 2a43710)
1 parent 0906483 commit a81c83d

File tree

6 files changed

+383
-1
lines changed

6 files changed

+383
-1
lines changed

libs/acn/BrokerClientEntryHeader.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Library General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
*
16+
* BrokerClientEntryHeader.h
17+
* The E1.33 Broker Client Entry Header
18+
* Copyright (C) 2023 Peter Newman
19+
*/
20+
21+
#ifndef LIBS_ACN_BROKERCLIENTENTRYHEADER_H_
22+
#define LIBS_ACN_BROKERCLIENTENTRYHEADER_H_
23+
24+
#include <ola/acn/CID.h>
25+
#include <ola/base/Macro.h>
26+
27+
#include <stdint.h>
28+
29+
namespace ola {
30+
namespace acn {
31+
32+
// TODO(Peter): I think technically this probably shouldn't be a header and
33+
// instead is just data at this level!
34+
/*
35+
* Header for the Broker Client Entry level
36+
*/
37+
class BrokerClientEntryHeader {
38+
public:
39+
BrokerClientEntryHeader() {}
40+
41+
BrokerClientEntryHeader(const ola::acn::CID &client_cid)
42+
: m_client_cid(client_cid) {
43+
}
44+
~BrokerClientEntryHeader() {}
45+
46+
const ola::acn::CID ClientCid() const { return m_client_cid; }
47+
48+
bool operator==(const BrokerClientEntryHeader &other) const {
49+
return m_client_cid == other.m_client_cid;
50+
}
51+
52+
PACK(
53+
struct broker_client_entry_pdu_header_s {
54+
uint8_t client_cid[CID::CID_LENGTH];
55+
});
56+
typedef struct broker_client_entry_pdu_header_s broker_client_entry_pdu_header;
57+
58+
private:
59+
ola::acn::CID m_client_cid;
60+
};
61+
} // namespace acn
62+
} // namespace ola
63+
#endif // LIBS_ACN_BROKERCLIENTENTRYHEADER_H_

libs/acn/BrokerClientEntryPDU.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Library General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
*
16+
* BrokerClientEntryPDU.cpp
17+
* The BrokerClientEntryPDU
18+
* Copyright (C) 2023 Peter Newman
19+
*/
20+
21+
22+
#include "ola/Logging.h"
23+
#include "ola/base/Array.h"
24+
#include "ola/network/NetworkUtils.h"
25+
#include "libs/acn/BrokerClientEntryPDU.h"
26+
27+
namespace ola {
28+
namespace acn {
29+
30+
using ola::io::OutputStream;
31+
using ola::network::HostToNetwork;
32+
33+
/*
34+
* Size of the header portion.
35+
*/
36+
unsigned int BrokerClientEntryPDU::HeaderSize() const {
37+
return sizeof(BrokerClientEntryHeader::broker_client_entry_pdu_header);
38+
}
39+
40+
41+
/*
42+
* Size of the data portion
43+
*/
44+
unsigned int BrokerClientEntryPDU::DataSize() const {
45+
return m_pdu ? m_pdu->Size() : 0;
46+
}
47+
48+
49+
/*
50+
* Pack the header portion.
51+
*/
52+
bool BrokerClientEntryPDU::PackHeader(uint8_t *data, unsigned int *length) const {
53+
unsigned int header_size = HeaderSize();
54+
55+
if (*length < header_size) {
56+
OLA_WARN << "BrokerClientEntryPDU::PackHeader: buffer too small, got " << *length
57+
<< " required " << header_size;
58+
*length = 0;
59+
return false;
60+
}
61+
62+
BrokerClientEntryHeader::broker_client_entry_pdu_header header;
63+
m_header.ClientCid().Pack(header.client_cid);
64+
*length = sizeof(BrokerClientEntryHeader::broker_client_entry_pdu_header);
65+
memcpy(data, &header, *length);
66+
return true;
67+
}
68+
69+
70+
/*
71+
* Pack the data portion.
72+
*/
73+
bool BrokerClientEntryPDU::PackData(uint8_t *data, unsigned int *length) const {
74+
if (m_pdu)
75+
return m_pdu->Pack(data, length);
76+
*length = 0;
77+
return true;
78+
}
79+
80+
81+
/*
82+
* Pack the header into a buffer.
83+
*/
84+
void BrokerClientEntryPDU::PackHeader(OutputStream *stream) const {
85+
BrokerClientEntryHeader::broker_client_entry_pdu_header header;
86+
m_header.ClientCid().Pack(header.client_cid);
87+
stream->Write(reinterpret_cast<uint8_t*>(&header),
88+
sizeof(BrokerClientEntryHeader::broker_client_entry_pdu_header));
89+
}
90+
91+
92+
/*
93+
* Pack the data into a buffer
94+
*/
95+
void BrokerClientEntryPDU::PackData(OutputStream *stream) const {
96+
if (m_pdu)
97+
m_pdu->Write(stream);
98+
}
99+
100+
101+
void BrokerClientEntryPDU::PrependPDU(ola::io::IOStack *stack,
102+
uint32_t vector,
103+
const ola::acn::CID &client_cid) {
104+
BrokerClientEntryHeader::broker_client_entry_pdu_header header;
105+
client_cid.Pack(header.client_cid);
106+
stack->Write(reinterpret_cast<uint8_t*>(&header),
107+
sizeof(BrokerClientEntryHeader::broker_client_entry_pdu_header));
108+
109+
vector = HostToNetwork(vector);
110+
stack->Write(reinterpret_cast<uint8_t*>(&vector), sizeof(vector));
111+
PrependFlagsAndLength(stack, VFLAG_MASK | HFLAG_MASK | DFLAG_MASK, true);
112+
}
113+
} // namespace acn
114+
} // namespace ola

libs/acn/BrokerClientEntryPDU.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Library General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
*
16+
* BrokerClientEntryPDU.h
17+
* Interface for the BrokerClientEntryPDU class
18+
* Copyright (C) 2023 Peter Newman
19+
*/
20+
21+
#ifndef LIBS_ACN_BROKERCLIENTENTRYPDU_H_
22+
#define LIBS_ACN_BROKERCLIENTENTRYPDU_H_
23+
24+
#include <ola/acn/CID.h>
25+
#include <ola/io/IOStack.h>
26+
27+
#include "libs/acn/PDU.h"
28+
#include "libs/acn/BrokerClientEntryHeader.h"
29+
30+
namespace ola {
31+
namespace acn {
32+
33+
class BrokerClientEntryPDU: public PDU {
34+
public:
35+
BrokerClientEntryPDU(unsigned int vector,
36+
const BrokerClientEntryHeader &header,
37+
const PDU *pdu):
38+
PDU(vector, FOUR_BYTES, true),
39+
m_header(header),
40+
m_pdu(pdu) {}
41+
~BrokerClientEntryPDU() {}
42+
43+
unsigned int HeaderSize() const;
44+
unsigned int DataSize() const;
45+
bool PackHeader(uint8_t *data, unsigned int *length) const;
46+
bool PackData(uint8_t *data, unsigned int *length) const;
47+
48+
void PackHeader(ola::io::OutputStream *stream) const;
49+
void PackData(ola::io::OutputStream *stream) const;
50+
51+
static void PrependPDU(ola::io::IOStack *stack,
52+
uint32_t vector,
53+
const ola::acn::CID &client_cid);
54+
55+
private:
56+
BrokerClientEntryHeader m_header;
57+
const PDU *m_pdu;
58+
};
59+
} // namespace acn
60+
} // namespace ola
61+
#endif // LIBS_ACN_BROKERCLIENTENTRYPDU_H_

libs/acn/BrokerClientEntryPDUTest.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU Library General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15+
*
16+
* BrokerClientEntryPDUTest.cpp
17+
* Test fixture for the BrokerClientEntryPDU class
18+
* Copyright (C) 2023 Peter Newman
19+
*/
20+
21+
#include <cppunit/extensions/HelperMacros.h>
22+
23+
#include "ola/Logging.h"
24+
#include "ola/io/IOQueue.h"
25+
#include "ola/io/OutputStream.h"
26+
#include "ola/network/NetworkUtils.h"
27+
#include "ola/testing/TestUtils.h"
28+
#include "libs/acn/BrokerClientEntryPDU.h"
29+
#include "libs/acn/PDUTestCommon.h"
30+
31+
32+
namespace ola {
33+
namespace acn {
34+
35+
using ola::io::IOQueue;
36+
using ola::io::OutputStream;
37+
using ola::network::HostToNetwork;
38+
39+
class BrokerClientEntryPDUTest: public CppUnit::TestFixture {
40+
CPPUNIT_TEST_SUITE(BrokerClientEntryPDUTest);
41+
CPPUNIT_TEST(testSimpleBrokerClientEntryPDU);
42+
CPPUNIT_TEST(testSimpleBrokerClientEntryPDUToOutputStream);
43+
CPPUNIT_TEST_SUITE_END();
44+
45+
public:
46+
void testSimpleBrokerClientEntryPDU();
47+
void testSimpleBrokerClientEntryPDUToOutputStream();
48+
49+
void setUp() {
50+
ola::InitLogging(ola::OLA_LOG_DEBUG, ola::OLA_LOG_STDERR);
51+
}
52+
53+
private:
54+
static const unsigned int TEST_VECTOR;
55+
static const uint8_t TEST_DATA[];
56+
};
57+
58+
const uint8_t BrokerClientEntryPDUTest::TEST_DATA[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
59+
12, 13, 14, 15};
60+
61+
CPPUNIT_TEST_SUITE_REGISTRATION(BrokerClientEntryPDUTest);
62+
63+
const unsigned int BrokerClientEntryPDUTest::TEST_VECTOR = 39;
64+
65+
66+
/*
67+
* Test that packing a BrokerClientEntryPDU without data works.
68+
*/
69+
void BrokerClientEntryPDUTest::testSimpleBrokerClientEntryPDU() {
70+
const CID client_cid = CID::FromData(TEST_DATA);
71+
BrokerClientEntryHeader header(client_cid);
72+
BrokerClientEntryPDU pdu(TEST_VECTOR, header, NULL);
73+
74+
OLA_ASSERT_EQ(16u, pdu.HeaderSize());
75+
OLA_ASSERT_EQ(0u, pdu.DataSize());
76+
OLA_ASSERT_EQ(23u, pdu.Size());
77+
78+
unsigned int size = pdu.Size();
79+
uint8_t *data = new uint8_t[size];
80+
unsigned int bytes_used = size;
81+
OLA_ASSERT(pdu.Pack(data, &bytes_used));
82+
OLA_ASSERT_EQ(size, bytes_used);
83+
84+
// spot check the data
85+
OLA_ASSERT_EQ((uint8_t) 0xf0, data[0]);
86+
// bytes_used is technically data[1] and data[2] if > 255
87+
OLA_ASSERT_EQ((uint8_t) bytes_used, data[2]);
88+
unsigned int actual_value;
89+
memcpy(&actual_value, data + 3, sizeof(actual_value));
90+
OLA_ASSERT_EQ(HostToNetwork(TEST_VECTOR), actual_value);
91+
92+
uint8_t buffer[CID::CID_LENGTH];
93+
client_cid.Pack(buffer);
94+
OLA_ASSERT_DATA_EQUALS(&data[7], CID::CID_LENGTH, buffer, sizeof(buffer));
95+
96+
// test undersized buffer
97+
bytes_used = size - 1;
98+
OLA_ASSERT_FALSE(pdu.Pack(data, &bytes_used));
99+
OLA_ASSERT_EQ(0u, bytes_used);
100+
101+
// test oversized buffer
102+
bytes_used = size + 1;
103+
OLA_ASSERT(pdu.Pack(data, &bytes_used));
104+
OLA_ASSERT_EQ(size, bytes_used);
105+
delete[] data;
106+
}
107+
108+
109+
/*
110+
* Test that writing to an output stream works.
111+
*/
112+
void BrokerClientEntryPDUTest::testSimpleBrokerClientEntryPDUToOutputStream() {
113+
const ola::acn::CID client_cid = CID::FromData(TEST_DATA);
114+
BrokerClientEntryHeader header(client_cid);
115+
BrokerClientEntryPDU pdu(TEST_VECTOR, header, NULL);
116+
117+
OLA_ASSERT_EQ(16u, pdu.HeaderSize());
118+
OLA_ASSERT_EQ(0u, pdu.DataSize());
119+
OLA_ASSERT_EQ(23u, pdu.Size());
120+
121+
IOQueue output;
122+
OutputStream stream(&output);
123+
pdu.Write(&stream);
124+
OLA_ASSERT_EQ(23u, output.Size());
125+
126+
uint8_t *pdu_data = new uint8_t[output.Size()];
127+
unsigned int pdu_size = output.Peek(pdu_data, output.Size());
128+
OLA_ASSERT_EQ(output.Size(), pdu_size);
129+
130+
uint8_t EXPECTED[] = {
131+
0xf0, 0x00, 0x17,
132+
0, 0, 0, 39,
133+
0, 1, 2, 3, 4, 5, 6, 7,
134+
8, 9, 10, 11, 12, 13, 14, 15
135+
};
136+
OLA_ASSERT_DATA_EQUALS(EXPECTED, sizeof(EXPECTED), pdu_data, pdu_size);
137+
output.Pop(output.Size());
138+
delete[] pdu_data;
139+
}
140+
} // namespace acn
141+
} // namespace ola

libs/acn/BrokerPDU.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
#include <ola/io/IOStack.h>
2626

2727
#include "libs/acn/PDU.h"
28-
#include "libs/acn/BrokerHeader.h"
2928

3029
namespace ola {
3130
namespace acn {

0 commit comments

Comments
 (0)