Skip to content

Commit 2da14b4

Browse files
committed
[xpressnet] Report hardware info in interface window like in Z21
1 parent 459e194 commit 2da14b4

7 files changed

Lines changed: 100 additions & 31 deletions

File tree

server/src/hardware/interface/xpressnetinterface.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "../../core/objectproperty.tpp"
3939
#include "../../log/log.hpp"
4040
#include "../../log/logmessageexception.hpp"
41+
#include "../../utils/category.hpp"
4142
#include "../../utils/displayname.hpp"
4243
#include "../../utils/inrange.hpp"
4344
#include "../../utils/makearray.hpp"
@@ -96,6 +97,8 @@ XpressNetInterface::XpressNetInterface(World& world, std::string_view _id)
9697
, s88StartAddress{this, "s88_start_address", XpressNet::RoSoftS88XpressNetLI::S88StartAddress::startAddressDefault, PropertyFlags::ReadWrite | PropertyFlags::Store}
9798
, s88ModuleCount{this, "s88_module_count", XpressNet::RoSoftS88XpressNetLI::S88ModuleCount::moduleCountDefault, PropertyFlags::ReadWrite | PropertyFlags::Store}
9899
, xpressnet{this, "xpressnet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
100+
, xbusVersion{this, "xbus_version", "", PropertyFlags::ReadOnly | PropertyFlags::NoStore}
101+
, commandStationType{this, "command_station_type", "", PropertyFlags::ReadOnly | PropertyFlags::NoStore}
99102
, luaSendMsg{*this, "send_msg", MethodFlags::ScriptCallable,
100103
[this](const std::string &msgHexStr)
101104
{
@@ -159,6 +162,12 @@ XpressNetInterface::XpressNetInterface(World& world, std::string_view _id)
159162

160163
m_interfaceItems.insertBefore(outputs, notes);
161164

165+
Attributes::addCategory(xbusVersion, Category::info);
166+
m_interfaceItems.insertBefore(xbusVersion, notes);
167+
168+
Attributes::addCategory(commandStationType, Category::info);
169+
m_interfaceItems.insertBefore(commandStationType, notes);
170+
162171
m_interfaceItems.add(luaSendMsg);
163172

164173
decoderAddedRemovedConn = std::static_pointer_cast<Object>(decoders.value())->propertyChanged.connect(
@@ -356,6 +365,20 @@ bool XpressNetInterface::setOnline(bool& value, bool simulation)
356365
setState(InterfaceState::Error);
357366
online = false; // communication no longer possible
358367
});
368+
m_kernel->setOnHardwareInfoChanged(
369+
[this](XpressNet::HardwareType hwType, uint8_t versionMajor, uint8_t versionMinor)
370+
{
371+
commandStationType.setValueInternal(std::string(XpressNet::toString(hwType)));
372+
Log::log(*this, LogMessage::I2002_HARDWARE_TYPE_X, commandStationType.value());
373+
374+
if(versionMajor != 0)
375+
{
376+
xbusVersion.setValueInternal(std::to_string(versionMajor).append(".").append(std::to_string(versionMinor)));
377+
Log::log(*this, LogMessage::I2003_FIRMWARE_VERSION_X, xbusVersion.value());
378+
}
379+
else
380+
xbusVersion.setValueInternal("");
381+
});
359382
m_kernel->setOnTrackPowerChanged(
360383
[this](bool powerOn, bool isStopped)
361384
{
@@ -431,6 +454,8 @@ bool XpressNetInterface::setOnline(bool& value, bool simulation)
431454
EventLoop::deleteLater(m_kernel.release());
432455

433456
setState(InterfaceState::Offline);
457+
xbusVersion.setValueInternal("");
458+
commandStationType.setValueInternal("");
434459
}
435460
return true;
436461
}

server/src/hardware/interface/xpressnetinterface.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class XpressNetInterface final
8585
Property<uint8_t> s88StartAddress;
8686
Property<uint8_t> s88ModuleCount;
8787
ObjectProperty<XpressNet::Settings> xpressnet;
88+
Property<std::string> xbusVersion;
89+
Property<std::string> commandStationType;
8890

8991
Method<void(std::string)> luaSendMsg;
9092

server/src/hardware/protocol/xpressnet/kernel.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,18 @@ void Kernel::pollDecoders()
139139
}
140140
}
141141

142-
void Kernel::setCentralVersion(uint8_t version)
142+
void Kernel::setCentralVersion(uint8_t version, uint8_t commandStationId)
143143
{
144144
assert(isKernelThread());
145145
m_centralVersion = CentralVersion(version);
146-
EventLoop::call([this, version]()
146+
EventLoop::call([this, version, commandStationId]()
147147
{
148148
m_centralVersionEventLoop = CentralVersion(version);
149+
150+
if(m_onHardwareInfoChanged)
151+
m_onHardwareInfoChanged(HardwareType(commandStationId),
152+
xbusVersionMajor(version),
153+
xbusVersionMinor(version));
149154
});
150155
}
151156

@@ -347,15 +352,15 @@ void Kernel::receive(const Message& message)
347352
const auto& reply = static_cast<const CentralVersionReplyOLD&>(message);
348353
if(reply.db1 == idCentralVersion)
349354
{
350-
setCentralVersion(reply.versionHex);
355+
setCentralVersion(reply.versionHex, HardwareType::HWT_UNKNOWN);
351356
}
352357
}
353358
else if(message.header == REPLY_VERSION_3_0)
354359
{
355360
const auto& reply = static_cast<const CentralVersionReplyV3&>(message);
356361
if(reply.db1 == idCentralVersion)
357362
{
358-
setCentralVersion(reply.versionHex);
363+
setCentralVersion(reply.versionHex, reply.commandStationId());
359364
}
360365
}
361366
break;

server/src/hardware/protocol/xpressnet/kernel.hpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class OutputController;
4343
namespace XpressNet {
4444

4545
struct Message;
46+
enum HardwareType : uint8_t;
4647

4748
class Kernel : public ::KernelBase
4849
{
@@ -71,6 +72,8 @@ class Kernel : public ::KernelBase
7172
std::unique_ptr<IOHandler> m_ioHandler;
7273
const bool m_simulation;
7374

75+
std::function<void(HardwareType, uint8_t, uint8_t)> m_onHardwareInfoChanged;
76+
7477
/*!
7578
* \brief m_trackPowerOn caches command station track power state.
7679
*
@@ -137,7 +140,7 @@ class Kernel : public ::KernelBase
137140
void onPendingQueryTimeout(const boost::system::error_code &ec);
138141
uint16_t popAddressQuerySendNext(PendingQuery::QueryType type);
139142
void pollDecoders();
140-
void setCentralVersion(uint8_t version);
143+
void setCentralVersion(uint8_t version, uint8_t commandStationId);
141144

142145
Kernel(std::string logId_, const Config& config, bool simulation);
143146

@@ -202,6 +205,17 @@ class Kernel : public ::KernelBase
202205
*/
203206
void setConfig(const Config& config);
204207

208+
/**
209+
* @brief ...
210+
* @param[in] callback ...
211+
* @note This function may not be called when the kernel is running.
212+
*/
213+
inline void setOnHardwareInfoChanged(std::function<void(HardwareType, uint8_t, uint8_t)> callback)
214+
{
215+
assert(!m_started);
216+
m_onHardwareInfoChanged = std::move(callback);
217+
}
218+
205219
/**
206220
* @brief ...
207221
* @param[in] callback ...

server/src/hardware/protocol/xpressnet/messages.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ std::string toString(const Message& message, bool raw, const PendingQuery &pendi
116116
{
117117
s = "CS_VERSION_OLD";
118118
s.append(" version=");
119-
s.append(std::to_string(reply.versionMajor()));
119+
s.append(std::to_string(xbusVersionMajor(reply.versionHex)));
120120
s.append(".");
121-
s.append(std::to_string(reply.versionMinor()));
121+
s.append(std::to_string(xbusVersionMinor(reply.versionHex)));
122122
}
123123
else
124124
raw = true;
@@ -131,9 +131,9 @@ std::string toString(const Message& message, bool raw, const PendingQuery &pendi
131131
{
132132
s = "CS_VERSION_V3";
133133
s.append(" version=");
134-
s.append(std::to_string(reply.versionMajor()));
134+
s.append(std::to_string(xbusVersionMajor(reply.versionHex)));
135135
s.append(".");
136-
s.append(std::to_string(reply.versionMinor()));
136+
s.append(std::to_string(xbusVersionMinor(reply.versionHex)));
137137
s.append(" id=");
138138
s.append(std::to_string(reply.commandStationId()));
139139
}

server/src/hardware/protocol/xpressnet/messages.hpp

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@ enum Header : uint8_t
8888
LOCO_INFO = 0xEF
8989
};
9090

91+
enum HardwareType : uint8_t
92+
{
93+
HWT_LZ100 = 0x00,
94+
HWT_LZ200 = 0x01,
95+
HWT_DPC = 0x02,
96+
HWT_multiMAUS = 0x10,
97+
HWT_UNKNOWN = 0xFF
98+
};
99+
100+
constexpr std::string_view toString(HardwareType value)
101+
{
102+
switch(value)
103+
{
104+
case HWT_LZ100:
105+
return "Lenz LZ100";
106+
107+
case HWT_LZ200:
108+
return "Lenz LZ200";
109+
110+
case HWT_DPC:
111+
return "Lenz DPC (Compact und Commander)";
112+
113+
case HWT_multiMAUS:
114+
return "ROCO multiMAUS";
115+
116+
case HWT_UNKNOWN:
117+
default:
118+
break;
119+
}
120+
121+
return {};
122+
}
123+
91124
struct Message;
92125

93126
inline uint8_t calcChecksum(const Message& msg);
@@ -189,16 +222,6 @@ struct CentralVersionReplyOLD : Message
189222
versionHex = versionHex_;
190223
updateChecksum();
191224
}
192-
193-
uint8_t versionMajor() const
194-
{
195-
return (versionHex >> 4) & 0x0F;
196-
}
197-
198-
uint8_t versionMinor() const
199-
{
200-
return versionHex & 0x0F;
201-
}
202225
} ATTRIBUTE_PACKED;
203226
static_assert(sizeof(CentralVersionReplyOLD) == 4);
204227

@@ -218,19 +241,9 @@ struct CentralVersionReplyV3 : Message
218241
updateChecksum();
219242
}
220243

221-
uint8_t versionMajor() const
222-
{
223-
return (versionHex >> 4) & 0x0F;
224-
}
225-
226-
uint8_t versionMinor() const
227-
{
228-
return versionHex & 0x0F;
229-
}
230-
231-
uint8_t commandStationId() const
244+
HardwareType commandStationId() const
232245
{
233-
return db_csId;
246+
return HardwareType(db_csId);
234247
}
235248
} ATTRIBUTE_PACKED;
236249
static_assert(sizeof(CentralVersionReplyV3) == 5);

server/src/hardware/protocol/xpressnet/utils.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ struct PendingQuery
4040
QueryType type = QueryType::LocoInfoAndF0F12;
4141
};
4242

43+
static constexpr uint8_t xbusVersionMajor(uint8_t versionHex)
44+
{
45+
return (versionHex >> 4) & 0x0F;
46+
}
47+
48+
static constexpr uint8_t xbusVersionMinor(uint8_t versionHex)
49+
{
50+
return versionHex & 0x0F;
51+
}
52+
4353
}
4454

4555
#endif

0 commit comments

Comments
 (0)