Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvught committed Apr 29, 2024
1 parent 748d0f1 commit 049c85f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 73 deletions.
30 changes: 15 additions & 15 deletions lib-artnet/include/artnetpolltable.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,40 +39,40 @@ static constexpr uint32_t POLL_INTERVAL_MILLIS = (POLL_INTERVAL_SECONDS * 1000U)
static constexpr uint32_t POLL_TABLE_SIZE_ENRIES = 255;
static constexpr uint32_t POLL_TABLE_SIZE_NODE_UNIVERSES = 64;
static constexpr uint32_t POLL_TABLE_SIZE_UNIVERSES = 512;
} // namespace artnet

struct TArtNetNodeEntryUniverse {
struct NodeEntryUniverse {
uint8_t ShortName[artnet::SHORT_NAME_LENGTH];
uint32_t nLastUpdateMillis;
uint16_t nUniverse;
};

struct TArtNetNodeEntry {
struct NodeEntry {
uint32_t IPAddress;
uint8_t Mac[artnet::MAC_SIZE];
uint8_t LongName[artnet::LONG_NAME_LENGTH];
uint16_t nUniversesCount;
struct TArtNetNodeEntryUniverse Universe[artnet::POLL_TABLE_SIZE_NODE_UNIVERSES];
struct NodeEntryUniverse Universe[artnet::POLL_TABLE_SIZE_NODE_UNIVERSES];
};

struct TArtNetPollTableUniverses {
struct PollTableUniverses {
uint16_t nUniverse;
uint16_t nCount;
uint32_t *pIpAddresses;
};

struct TArtNetPollTableClean {
struct PollTableClean {
uint32_t nTableIndex;
uint16_t nUniverseIndex;
bool bOffLine;
};
} // namespace artnet

class ArtNetPollTable {
public:
ArtNetPollTable();
~ArtNetPollTable();

const TArtNetNodeEntry *GetPollTable() const {
const artnet::NodeEntry *GetPollTable() const {
return m_pPollTable;
}

Expand All @@ -83,21 +83,21 @@ class ArtNetPollTable {
void Add(const struct artnet::ArtPollReply *ptArtPollReply);
void Clean();

const struct TArtNetPollTableUniverses *GetIpAddress(uint16_t nUniverse) const;
const struct artnet::PollTableUniverses *GetIpAddress(uint16_t nUniverse) const;

void Dump();
void DumpTableUniverses();

private:
void ProcessUniverse(uint32_t nIpAddress, uint16_t nUniverse);
void RemoveIpAddress(uint16_t nUniverse, uint32_t nIpAddress);
void ProcessUniverse(const uint32_t nIpAddress, const uint16_t nUniverse);
void RemoveIpAddress(const uint16_t nUniverse, const uint32_t nIpAddress);

private:
TArtNetNodeEntry *m_pPollTable;
uint32_t m_nPollTableEntries{0};
TArtNetPollTableUniverses *m_pTableUniverses;
uint32_t m_nTableUniversesEntries{0};
TArtNetPollTableClean m_tTableClean;
artnet::NodeEntry *m_pPollTable;
artnet::PollTableUniverses *m_pTableUniverses;
uint32_t m_nPollTableEntries { 0 };
uint32_t m_nTableUniversesEntries { 0 };
artnet::PollTableClean m_PollTableClean;
};

#endif /* ARTNETPOLLTABLE_H_ */
2 changes: 1 addition & 1 deletion lib-artnet/src/controller/artnetcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void ArtNetController::HandleDmxOut(uint16_t nUniverse, const uint8_t *pDmxData,
#endif

uint32_t nCount = 0;
auto IpAddresses = const_cast<struct TArtNetPollTableUniverses*>(GetIpAddress(nUniverse));
auto IpAddresses = const_cast<struct artnet::PollTableUniverses *>(GetIpAddress(nUniverse));

if (m_bUnicast && !m_bForceBroadcast) {
if (IpAddresses != nullptr) {
Expand Down
106 changes: 52 additions & 54 deletions lib-artnet/src/controller/artnetpolltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
* THE SOFTWARE.
*/

#undef NDEBUG

#if !defined(__clang__) // Needed for compiling on MacOS
# if __GNUC__ < 9
# pragma GCC diagnostic push
Expand All @@ -47,39 +45,40 @@

#include "debug.h"

using namespace artnet;

union uip {
uint32_t u32;
uint8_t u8[4];
} static ip;

ArtNetPollTable::ArtNetPollTable() {
m_pPollTable = new TArtNetNodeEntry[POLL_TABLE_SIZE_ENRIES];
DEBUG_ENTRY

m_pPollTable = new artnet::NodeEntry[artnet::POLL_TABLE_SIZE_ENRIES];
assert(m_pPollTable != nullptr);

memset(m_pPollTable, 0, sizeof(TArtNetNodeEntry[POLL_TABLE_SIZE_ENRIES]));
memset(m_pPollTable, 0, sizeof(artnet::NodeEntry[artnet::POLL_TABLE_SIZE_ENRIES]));

m_pTableUniverses = new TArtNetPollTableUniverses[POLL_TABLE_SIZE_UNIVERSES];
m_pTableUniverses = new artnet::PollTableUniverses[artnet::POLL_TABLE_SIZE_UNIVERSES];
assert(m_pTableUniverses != nullptr);

memset(m_pTableUniverses, 0, sizeof(TArtNetPollTableUniverses[POLL_TABLE_SIZE_UNIVERSES]));
memset(m_pTableUniverses, 0, sizeof(artnet::PollTableUniverses[artnet::POLL_TABLE_SIZE_UNIVERSES]));

for (uint32_t nIndex = 0; nIndex < POLL_TABLE_SIZE_UNIVERSES; nIndex++) {
m_pTableUniverses[nIndex].pIpAddresses = new uint32_t[POLL_TABLE_SIZE_ENRIES];
for (uint32_t nIndex = 0; nIndex < artnet::POLL_TABLE_SIZE_UNIVERSES; nIndex++) {
m_pTableUniverses[nIndex].pIpAddresses = new uint32_t[artnet::POLL_TABLE_SIZE_ENRIES];
assert(m_pTableUniverses[nIndex].pIpAddresses != nullptr);
}

// DEBUG_PRINTF("TArtNetNodeEntry[%d] = %u bytes [%u Kb]", ARTNET_POLL_TABLE_SIZE_ENRIES, (sizeof(TArtNetNodeEntry[ARTNET_POLL_TABLE_SIZE_ENRIES])), (sizeof(TArtNetNodeEntry[ARTNET_POLL_TABLE_SIZE_ENRIES])) / 1024);
// DEBUG_PRINTF("TArtNetPollTableUniverses[%d] = %u bytes [%u Kb]", ARTNET_POLL_TABLE_SIZE_UNIVERSES, (sizeof(TArtNetPollTableUniverses[ARTNET_POLL_TABLE_SIZE_UNIVERSES])), (sizeof(TArtNetPollTableUniverses[ARTNET_POLL_TABLE_SIZE_UNIVERSES])) / 1024);
m_PollTableClean.nTableIndex = 0;
m_PollTableClean.nUniverseIndex = 0;
m_PollTableClean.bOffLine = true;

m_tTableClean.nTableIndex = 0;
m_tTableClean.nUniverseIndex = 0;
m_tTableClean.bOffLine = true;
DEBUG_PRINTF("NodeEntry[%d] = %u bytes [%u Kb]", artnet::POLL_TABLE_SIZE_ENRIES, static_cast<unsigned>(sizeof(artnet::NodeEntry[artnet::POLL_TABLE_SIZE_ENRIES])), static_cast<unsigned>(sizeof(artnet::NodeEntry[artnet::POLL_TABLE_SIZE_ENRIES])) / 1024U);
DEBUG_PRINTF("PollTableUniverses[%d] = %u bytes [%u Kb]", artnet::POLL_TABLE_SIZE_UNIVERSES, static_cast<unsigned>(sizeof(artnet::PollTableUniverses[artnet::POLL_TABLE_SIZE_UNIVERSES])), static_cast<unsigned>(sizeof(artnet::PollTableUniverses[artnet::POLL_TABLE_SIZE_UNIVERSES])) / 1024U);
DEBUG_EXIT
}

ArtNetPollTable::~ArtNetPollTable() {
for (uint32_t nIndex = 0; nIndex < POLL_TABLE_SIZE_UNIVERSES; nIndex++) {
for (uint32_t nIndex = 0; nIndex < artnet::POLL_TABLE_SIZE_UNIVERSES; nIndex++) {
delete[] m_pTableUniverses[nIndex].pIpAddresses;
m_pTableUniverses[nIndex].pIpAddresses = nullptr;
}
Expand All @@ -91,14 +90,14 @@ ArtNetPollTable::~ArtNetPollTable() {
m_pPollTable = nullptr;
}

const struct TArtNetPollTableUniverses *ArtNetPollTable::GetIpAddress(uint16_t nUniverse) const {
const struct artnet::PollTableUniverses *ArtNetPollTable::GetIpAddress(uint16_t nUniverse) const {
if (m_nTableUniversesEntries == 0) {
return nullptr;
}

// FIXME Universe lookup
for (uint32_t nEntry = 0; nEntry < m_nTableUniversesEntries; nEntry++) {
TArtNetPollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
artnet::PollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
assert(pTableUniverses != nullptr);

if (pTableUniverses->nUniverse == nUniverse) {
Expand All @@ -110,7 +109,7 @@ const struct TArtNetPollTableUniverses *ArtNetPollTable::GetIpAddress(uint16_t n
}


void ArtNetPollTable::RemoveIpAddress(uint16_t nUniverse, uint32_t nIpAddress) {
void ArtNetPollTable::RemoveIpAddress(const uint16_t nUniverse, const uint32_t nIpAddress) {
if (m_nTableUniversesEntries == 0) {
return;
}
Expand All @@ -119,7 +118,7 @@ void ArtNetPollTable::RemoveIpAddress(uint16_t nUniverse, uint32_t nIpAddress) {

// FIXME Universe lookup
for (nEntry = 0; nEntry < m_nTableUniversesEntries; nEntry++) {
TArtNetPollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
artnet::PollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
assert(pTableUniverses != nullptr);

if (pTableUniverses->nUniverse == nUniverse) {
Expand All @@ -132,7 +131,7 @@ void ArtNetPollTable::RemoveIpAddress(uint16_t nUniverse, uint32_t nIpAddress) {
return;
}

TArtNetPollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
artnet::PollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
assert(pTableUniverses->nCount > 0);

uint32_t nIpAddressIndex = 0;
Expand All @@ -158,7 +157,7 @@ void ArtNetPollTable::RemoveIpAddress(uint16_t nUniverse, uint32_t nIpAddress) {
if (pTableUniverses->nCount == 0) {
DEBUG_PRINTF("Delete Universe -> m_nTableUniversesEntries=%u, nEntry=%u", m_nTableUniversesEntries, nEntry);

TArtNetPollTableUniverses *p = m_pTableUniverses;
artnet::PollTableUniverses *p = m_pTableUniverses;

for (i = static_cast<int32_t>(nEntry); i < static_cast<int32_t>(m_nTableUniversesEntries) - 1; i++) {
p[i].nUniverse = p[i + 1].nUniverse;
Expand All @@ -173,10 +172,10 @@ void ArtNetPollTable::RemoveIpAddress(uint16_t nUniverse, uint32_t nIpAddress) {
}
}

void ArtNetPollTable::ProcessUniverse(uint32_t nIpAddress, uint16_t nUniverse) {
void ArtNetPollTable::ProcessUniverse(const uint32_t nIpAddress, const uint16_t nUniverse) {
DEBUG_ENTRY

if (POLL_TABLE_SIZE_UNIVERSES == m_nTableUniversesEntries) {
if (artnet::POLL_TABLE_SIZE_UNIVERSES == m_nTableUniversesEntries) {
DEBUG_PUTS("m_pTableUniverses is full");
DEBUG_EXIT
return;
Expand All @@ -187,7 +186,7 @@ void ArtNetPollTable::ProcessUniverse(uint32_t nIpAddress, uint16_t nUniverse) {
uint32_t nEntry = 0;

for (nEntry = 0; nEntry < m_nTableUniversesEntries; nEntry++) {
TArtNetPollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];
auto *pTableUniverses = &m_pTableUniverses[nEntry];
assert(pTableUniverses != nullptr);

if (pTableUniverses->nUniverse == nUniverse) {
Expand All @@ -197,8 +196,7 @@ void ArtNetPollTable::ProcessUniverse(uint32_t nIpAddress, uint16_t nUniverse) {
}
}

TArtNetPollTableUniverses *pTableUniverses = &m_pTableUniverses[nEntry];

auto *pTableUniverses = &m_pTableUniverses[nEntry];
auto bFoundIp = false;
uint32_t nCount = 0;

Expand All @@ -219,7 +217,7 @@ void ArtNetPollTable::ProcessUniverse(uint32_t nIpAddress, uint16_t nUniverse) {
}

if (!bFoundIp) {
if (pTableUniverses->nCount < POLL_TABLE_SIZE_ENRIES) {
if (pTableUniverses->nCount < artnet::POLL_TABLE_SIZE_ENRIES) {
pTableUniverses->pIpAddresses[pTableUniverses->nCount] = nIpAddress;
pTableUniverses->nCount++;
DEBUG_PUTS("It is a new IP for the Universe");
Expand Down Expand Up @@ -261,7 +259,7 @@ void ArtNetPollTable::Add(const struct artnet::ArtPollReply *ptArtPollReply) {
}

if (!bFound) {
if (m_nPollTableEntries == POLL_TABLE_SIZE_ENRIES) {
if (m_nPollTableEntries == artnet::POLL_TABLE_SIZE_ENRIES) {
DEBUG_PUTS("Full");
return;
}
Expand All @@ -275,13 +273,13 @@ void ArtNetPollTable::Add(const struct artnet::ArtPollReply *ptArtPollReply) {
assert(nLow >= 0);

for (int32_t i = static_cast<int32_t>(m_nPollTableEntries) - 1;i >= nLow; i--) {
const struct TArtNetNodeEntry *pSrc = &pArtNetNodeEntry[i];
struct TArtNetNodeEntry *pDst = &pArtNetNodeEntry[i + 1];
memcpy(pDst, pSrc, sizeof(struct TArtNetNodeEntry));
const struct artnet::NodeEntry *pSrc = &pArtNetNodeEntry[i];
struct artnet::NodeEntry *pDst = &pArtNetNodeEntry[i + 1];
memcpy(pDst, pSrc, sizeof(struct artnet::NodeEntry));
}

auto *pDst = &pArtNetNodeEntry[nLow];
memset(pDst, 0, sizeof(struct TArtNetNodeEntry));
memset(pDst, 0, sizeof(struct artnet::NodeEntry));

i = nLow;
} else {
Expand Down Expand Up @@ -318,7 +316,7 @@ void ArtNetPollTable::Add(const struct artnet::ArtPollReply *ptArtPollReply) {

if (nIndexUniverse == m_pPollTable[i].nUniversesCount) {
// Not found
if (m_pPollTable[i].nUniversesCount < POLL_TABLE_SIZE_NODE_UNIVERSES) {
if (m_pPollTable[i].nUniversesCount < artnet::POLL_TABLE_SIZE_NODE_UNIVERSES) {
m_pPollTable[i].nUniversesCount++;
m_pPollTable[i].Universe[nIndexUniverse].nUniverse = nUniverse;
const uint8_t *pSrc = ptArtPollReply->ShortName;
Expand All @@ -343,55 +341,55 @@ void ArtNetPollTable::Clean() {
return;
}

assert(m_tTableClean.nTableIndex < m_nPollTableEntries);
assert(m_tTableClean.nUniverseIndex < POLL_TABLE_SIZE_NODE_UNIVERSES);
assert(m_PollTableClean.nTableIndex < m_nPollTableEntries);
assert(m_PollTableClean.nUniverseIndex < artnet::POLL_TABLE_SIZE_NODE_UNIVERSES);

if (m_tTableClean.nUniverseIndex == 0) {
m_tTableClean.bOffLine = true;
if (m_PollTableClean.nUniverseIndex == 0) {
m_PollTableClean.bOffLine = true;
}

auto *pArtNetNodeEntryBind = &m_pPollTable[m_tTableClean.nTableIndex].Universe[m_tTableClean.nUniverseIndex];
auto *pArtNetNodeEntryBind = &m_pPollTable[m_PollTableClean.nTableIndex].Universe[m_PollTableClean.nUniverseIndex];

if (pArtNetNodeEntryBind->nLastUpdateMillis != 0) {
if ((Hardware::Get()->Millis() - pArtNetNodeEntryBind->nLastUpdateMillis) > (1.5 * POLL_INTERVAL_MILLIS)) {
if ((Hardware::Get()->Millis() - pArtNetNodeEntryBind->nLastUpdateMillis) > (1.5 * artnet::POLL_INTERVAL_MILLIS)) {
pArtNetNodeEntryBind->nLastUpdateMillis = 0;
RemoveIpAddress(pArtNetNodeEntryBind->nUniverse, m_pPollTable[m_tTableClean.nTableIndex].IPAddress);
RemoveIpAddress(pArtNetNodeEntryBind->nUniverse, m_pPollTable[m_PollTableClean.nTableIndex].IPAddress);
} else {
m_tTableClean.bOffLine = false;
m_PollTableClean.bOffLine = false;
}
}

m_tTableClean.nUniverseIndex++;
m_PollTableClean.nUniverseIndex++;

if (m_tTableClean.nUniverseIndex == POLL_TABLE_SIZE_NODE_UNIVERSES) {
if (m_tTableClean.bOffLine) {
if (m_PollTableClean.nUniverseIndex == artnet::POLL_TABLE_SIZE_NODE_UNIVERSES) {
if (m_PollTableClean.bOffLine) {
DEBUG_PUTS("Node is off-line");

auto *pArtNetNodeEntry = m_pPollTable;
// Move
for (uint32_t i = m_tTableClean.nTableIndex; i < (m_nPollTableEntries - 1); i++) {
for (uint32_t i = m_PollTableClean.nTableIndex; i < (m_nPollTableEntries - 1); i++) {
const auto *pSrc = &pArtNetNodeEntry[i + 1];
auto *pDst = &pArtNetNodeEntry[i];
memcpy(pDst, pSrc, sizeof(struct TArtNetNodeEntry));
memcpy(pDst, pSrc, sizeof(struct artnet::NodeEntry));
}

m_nPollTableEntries--;

auto *pDst = &pArtNetNodeEntry[m_nPollTableEntries];
pDst->IPAddress = 0;
pDst->nUniversesCount = 0;
memset(pDst->Universe, 0, sizeof(struct TArtNetNodeEntryUniverse[POLL_TABLE_SIZE_NODE_UNIVERSES]));
memset(pDst->Universe, 0, sizeof(struct artnet::NodeEntryUniverse[artnet::POLL_TABLE_SIZE_NODE_UNIVERSES]));
#ifndef NDEBUG
memset(pDst->Mac, 0, artnet::MAC_SIZE + artnet::SHORT_NAME_LENGTH + artnet::LONG_NAME_LENGTH);
memset(pDst->Mac, 0, artnet::MAC_SIZE + artnet::LONG_NAME_LENGTH);
#endif
}

m_tTableClean.nUniverseIndex = 0;
m_tTableClean.bOffLine = true;
m_tTableClean.nTableIndex++;
m_PollTableClean.nUniverseIndex = 0;
m_PollTableClean.bOffLine = true;
m_PollTableClean.nTableIndex++;

if (m_tTableClean.nTableIndex >= m_nPollTableEntries) {
m_tTableClean.nTableIndex = 0;
if (m_PollTableClean.nTableIndex >= m_nPollTableEntries) {
m_PollTableClean.nTableIndex = 0;
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions lib-artnet/src/controller/json_get_polltable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
namespace remoteconfig {
namespace artnet {
namespace controller {

static uint32_t get_port(const struct TArtNetNodeEntryUniverse *pArtNetNodeEntryUniverse, char *pOutBuffer, const uint32_t nOutBufferSize) {
static uint32_t get_port(const struct ::artnet::NodeEntryUniverse *pArtNetNodeEntryUniverse, char *pOutBuffer, const uint32_t nOutBufferSize) {
const auto nLength = static_cast<uint32_t>(snprintf(pOutBuffer, nOutBufferSize,
"{\"name\":\"%s\",\"universe\":%u},",
pArtNetNodeEntryUniverse->ShortName, pArtNetNodeEntryUniverse->nUniverse));
Expand Down Expand Up @@ -74,7 +73,6 @@ static uint32_t get_entry(const uint32_t nIndex, char *pOutBuffer, const uint32_
}

uint32_t json_get_polltable(char *pOutBuffer, const uint32_t nOutBufferSize) {

const auto nBufferSize = nOutBufferSize - 2U;
pOutBuffer[0] = '[';

Expand Down

0 comments on commit 049c85f

Please sign in to comment.