Skip to content

Commit

Permalink
Ignore requests with unknown Unit ID (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
sikmir committed May 30, 2023
1 parent 7cd2d71 commit 1eecb52
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
wb-mqtt-mbgate (1.5.5) stable; urgency=medium

* Ignore requests with unknown Unit ID

-- Nikolay Korotkiy <nikolay.korotkiy@wirenboard.com> Mon, 29 May 2023 17:30:00 +0400

wb-mqtt-mbgate (1.5.4) stable; urgency=medium

* Enable debug logging from libmodbus
Expand Down
9 changes: 8 additions & 1 deletion src/modbus_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ void TModbusServer::Observe(PModbusServerObserver o,
#undef PROCESS
}

bool TModbusServer::IsObserved(uint8_t slave_id) const
{
return _maxSlaveAddresses.find(slave_id) != _maxSlaveAddresses.end();
}

static void _callCacheAllocate(const TModbusAddressRange& range, uint8_t slave_id, TStoreType store, void* cache_start)
{
map<PModbusServerObserver, TModbusCacheAddressRange> observers;
Expand Down Expand Up @@ -121,8 +126,10 @@ int TModbusServer::Loop(int timeoutMilliS)
// receive message, process, run callback
while (mb->Available()) {
TModbusQuery q = mb->ReceiveQuery();
if (q.size > 0)
auto slave_id = q.data[q.header_length - 1];
if (q.size > 0 && IsObserved(slave_id)) {
_ProcessQuery(q);
}
}

return 0;
Expand Down
6 changes: 6 additions & 0 deletions src/modbus_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ class TModbusServer
uint8_t slave_id = 0);
/* virtual void Unobserve(PModbusServerObserver o) = 0; */

/*! Check if slave ID is observed
* \param slave Slave ID
* \return true if slave ID is observed
*/
virtual bool IsObserved(uint8_t slave_id) const;

private:
void _ProcessQuery(const TModbusQuery& query);
void _ProcessReadQuery(TStoreType type,
Expand Down

0 comments on commit 1eecb52

Please sign in to comment.