Skip to content

Commit

Permalink
AdsNotification: allow dynamic sized samples
Browse files Browse the repository at this point in the history
It seems some ADS devices like AMSPORT_LOGGER will send notifications
with variable sizes. To support them we can lessen our size check, but
we have to pass the real size to Notification::Notify()
Signed-off-by: Patrick Bruenn <[email protected]>
  • Loading branch information
pbruenn committed Dec 20, 2024
1 parent e85ef69 commit a898434
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
14 changes: 8 additions & 6 deletions AdsLib/AdsNotification.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,29 @@ struct Notification {
: connection({__port, __amsAddr}),
callback(__func),
buffer(sizeof(AdsNotificationHeader) + length),
hUser(__hUser)
hUser(__hUser),
size(length)
{
auto header = reinterpret_cast<AdsNotificationHeader*>(buffer.data());
header->hNotification = 0;
header->cbSampleSize = length;
}

void Notify(uint64_t timestamp, RingBuffer& ring)
void Notify(RingBuffer& ring, const uint64_t timestamp, const uint32_t sampleSize)
{
auto header = reinterpret_cast<AdsNotificationHeader*>(buffer.data());
uint8_t* data = reinterpret_cast<uint8_t*>(header + 1);
for (size_t i = 0; i < header->cbSampleSize; ++i) {
header->cbSampleSize = sampleSize;
header->nTimeStamp = timestamp;
for (size_t i = 0; i < sampleSize; ++i) {
data[i] = ring.ReadFromLittleEndian<uint8_t>();
}
header->nTimeStamp = timestamp;
callback(&connection.second, header, hUser);
}

uint32_t Size() const
{
auto header = reinterpret_cast<const AdsNotificationHeader*>(buffer.data());
return header->cbSampleSize;
return size;
}

void hNotify(uint32_t value)
Expand All @@ -57,4 +58,5 @@ struct Notification {
const PAdsNotificationFuncEx callback;
std::vector<uint8_t> buffer;
const uint32_t hUser;
const uint32_t size;
};
6 changes: 3 additions & 3 deletions AdsLib/standalone/NotificationDispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ void NotificationDispatcher::Run()
fullLength -= sizeof(hNotify) + sizeof(size);
const auto notification = Find(hNotify);
if (notification) {
if (size != notification->Size()) {
LOG_WARN("Notification sample size: " << size << " doesn't match: " << notification->Size());
if (size > notification->Size()) {
LOG_WARN("Notification sample size: " << size << " to large: " << notification->Size());
goto cleanup;
}
notification->Notify(timestamp, ring);
notification->Notify(ring, timestamp, size);
} else {
ring.Read(size);
}
Expand Down

0 comments on commit a898434

Please sign in to comment.