• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 22823086659

08 Mar 2026 02:30PM UTC coverage: 26.831% (+0.06%) from 26.774%
22823086659

push

github

reinder
[cbus] renamed CBUSAccessory(Short) to Long/Short event and added node setting for Long events.

0 of 15 new or added lines in 2 files covered. (0.0%)

1909 existing lines in 38 files now uncovered.

8230 of 30674 relevant lines covered (26.83%)

186.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

42.98
/server/src/hardware/interface/xpressnetinterface.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2019-2026 Reinder Feenstra
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
 */
21

22
#include "xpressnetinterface.hpp"
23
#include "../decoder/list/decoderlist.hpp"
24
#include "../input/input.hpp"
25
#include "../input/list/inputlist.hpp"
26
#include "../output/list/outputlist.hpp"
27
#include "../protocol/xpressnet/kernel.hpp"
28
#include "../protocol/xpressnet/settings.hpp"
29
#include "../protocol/xpressnet/messages.hpp"
30
#include "../protocol/xpressnet/iohandler/serialiohandler.hpp"
31
#include "../protocol/xpressnet/iohandler/simulationiohandler.hpp"
32
#include "../protocol/xpressnet/iohandler/liusbiohandler.hpp"
33
#include "../protocol/xpressnet/iohandler/rosofts88xpressnetliiohandler.hpp"
34
#include "../protocol/xpressnet/iohandler/tcpiohandler.hpp"
35
#include "../../core/attributes.hpp"
36
#include "../../core/eventloop.hpp"
37
#include "../../core/method.tpp"
38
#include "../../core/objectproperty.tpp"
39
#include "../../log/log.hpp"
40
#include "../../log/logmessageexception.hpp"
41
#include "../../utils/displayname.hpp"
42
#include "../../utils/inrange.hpp"
43
#include "../../utils/makearray.hpp"
44
#include "../../world/world.hpp"
45

46
constexpr auto decoderListColumns = DecoderListColumn::Id | DecoderListColumn::Name | DecoderListColumn::Address;
47
constexpr auto inputListColumns = InputListColumn::Address;
48
constexpr auto outputListColumns = OutputListColumn::Address;
49

50
CREATE_IMPL(XpressNetInterface)
9✔
51

52
XpressNetInterface::XpressNetInterface(World& world, std::string_view _id)
9✔
53
  : Interface(world, _id)
54
  , DecoderController(*this, decoderListColumns)
55
  , InputController(static_cast<IdObject&>(*this))
56
  , OutputController(static_cast<IdObject&>(*this))
57
  , type{this, "type", XpressNetInterfaceType::Serial, PropertyFlags::ReadWrite | PropertyFlags::Store,
18✔
58
      [this](XpressNetInterfaceType /*value*/)
9✔
59
      {
UNCOV
60
        updateVisible();
×
61
      }}
×
62
  , serialInterfaceType{this, "interface", XpressNetSerialInterfaceType::LenzLI100, PropertyFlags::ReadWrite | PropertyFlags::Store,
18✔
63
      [this](XpressNetSerialInterfaceType value)
9✔
64
      {
UNCOV
65
        switch(value)
×
66
        {
UNCOV
67
          case XpressNetSerialInterfaceType::LenzLI100:
×
68
          case XpressNetSerialInterfaceType::RoSoftS88XPressNetLI:
UNCOV
69
            baudrate.setValueInternal(9600);
×
70
            flowControl.setValueInternal(SerialFlowControl::Hardware);
×
71
            break;
×
72

UNCOV
73
          case XpressNetSerialInterfaceType::LenzLI100F:
×
74
          case XpressNetSerialInterfaceType::LenzLI101F:
UNCOV
75
            baudrate.setValueInternal(19200);
×
76
            flowControl.setValueInternal(SerialFlowControl::Hardware);
×
77
            break;
×
78

UNCOV
79
          case XpressNetSerialInterfaceType::LenzLIUSB:
×
80
            baudrate.setValueInternal(57600);
×
81
            flowControl.setValueInternal(SerialFlowControl::None);
×
82
            break;
×
83

UNCOV
84
          case XpressNetSerialInterfaceType::DigikeijsDR5000:
×
85
            baudrate.setValueInternal(115200);
×
86
            flowControl.setValueInternal(SerialFlowControl::None);
×
87
            break;
×
88
        }
UNCOV
89
        updateVisible();
×
90
      }}
×
91
  , device{this, "device", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
18✔
92
  , baudrate{this, "baudrate", 19200, PropertyFlags::ReadWrite | PropertyFlags::Store}
9✔
93
  , flowControl{this, "flow_control", SerialFlowControl::None, PropertyFlags::ReadWrite | PropertyFlags::Store}
9✔
94
  , hostname{this, "hostname", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
18✔
95
  , port{this, "port", 5550, PropertyFlags::ReadWrite | PropertyFlags::Store}
9✔
96
  , s88StartAddress{this, "s88_start_address", XpressNet::RoSoftS88XpressNetLI::S88StartAddress::startAddressDefault, PropertyFlags::ReadWrite | PropertyFlags::Store}
9✔
97
  , s88ModuleCount{this, "s88_module_count", XpressNet::RoSoftS88XpressNetLI::S88ModuleCount::moduleCountDefault, PropertyFlags::ReadWrite | PropertyFlags::Store}
9✔
98
  , xpressnet{this, "xpressnet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
18✔
99
{
100
  name = "XpressNet";
9✔
101
  xpressnet.setValueInternal(std::make_shared<XpressNet::Settings>(*this, xpressnet.name()));
9✔
102

103
  Attributes::addDisplayName(type, DisplayName::Interface::type);
9✔
104
  Attributes::addEnabled(type, !online);
9✔
105
  Attributes::addValues(type, xpressNetInterfaceTypeValues);
9✔
106
  m_interfaceItems.insertBefore(type, notes);
9✔
107

108
  Attributes::addValues(serialInterfaceType, XpressNetSerialInterfaceTypeValues);
9✔
109
  Attributes::addEnabled(serialInterfaceType, !online);
9✔
110
  Attributes::addVisible(serialInterfaceType, false);
9✔
111
  m_interfaceItems.insertBefore(serialInterfaceType, notes);
9✔
112

113
  Attributes::addEnabled(device, !online);
9✔
114
  Attributes::addVisible(device, false);
9✔
115
  m_interfaceItems.insertBefore(device, notes);
9✔
116

117
  Attributes::addDisplayName(baudrate, DisplayName::Serial::baudrate);
9✔
118
  Attributes::addEnabled(baudrate, !online);
9✔
119
  Attributes::addVisible(baudrate, false);
9✔
120
  m_interfaceItems.insertBefore(baudrate, notes);
9✔
121

122
  Attributes::addDisplayName(flowControl, DisplayName::Serial::flowControl);
9✔
123
  Attributes::addEnabled(flowControl, !online);
9✔
124
  Attributes::addValues(flowControl, SerialFlowControlValues);
9✔
125
  Attributes::addVisible(flowControl, false);
9✔
126
  m_interfaceItems.insertBefore(flowControl, notes);
9✔
127

128
  Attributes::addDisplayName(hostname, DisplayName::IP::hostname);
9✔
129
  Attributes::addEnabled(hostname, !online);
9✔
130
  Attributes::addVisible(hostname, false);
9✔
131
  m_interfaceItems.insertBefore(hostname, notes);
9✔
132

133
  Attributes::addDisplayName(port, DisplayName::IP::port);
9✔
134
  Attributes::addEnabled(port, !online);
9✔
135
  Attributes::addVisible(port, false);
9✔
136
  m_interfaceItems.insertBefore(port, notes);
9✔
137

138
  Attributes::addMinMax(s88StartAddress, XpressNet::RoSoftS88XpressNetLI::S88StartAddress::startAddressMin, XpressNet::RoSoftS88XpressNetLI::S88StartAddress::startAddressMax);
9✔
139
  Attributes::addEnabled(s88StartAddress, !online);
9✔
140
  Attributes::addVisible(s88StartAddress, false);
9✔
141
  m_interfaceItems.insertBefore(s88StartAddress, notes);
9✔
142

143
  Attributes::addMinMax(s88ModuleCount, XpressNet::RoSoftS88XpressNetLI::S88ModuleCount::moduleCountMin, XpressNet::RoSoftS88XpressNetLI::S88ModuleCount::moduleCountMax);
9✔
144
  Attributes::addEnabled(s88ModuleCount, !online);
9✔
145
  Attributes::addVisible(s88ModuleCount, false);
9✔
146
  m_interfaceItems.insertBefore(s88ModuleCount, notes);
9✔
147

148
  Attributes::addDisplayName(xpressnet, DisplayName::Hardware::xpressnet);
9✔
149
  m_interfaceItems.insertBefore(xpressnet, notes);
9✔
150

151
  m_interfaceItems.insertBefore(decoders, notes);
9✔
152

153
  m_interfaceItems.insertBefore(inputs, notes);
9✔
154

155
  m_interfaceItems.insertBefore(outputs, notes);
9✔
156

157
  updateVisible();
9✔
158
}
9✔
159

160
XpressNetInterface::~XpressNetInterface() = default;
9✔
161

162
std::span<const DecoderProtocol> XpressNetInterface::decoderProtocols() const
10✔
163
{
164
  static constexpr std::array<DecoderProtocol, 2> protocols{DecoderProtocol::DCCShort, DecoderProtocol::DCCLong};
165
  return std::span<const DecoderProtocol>{protocols.data(), protocols.size()};
20✔
166
}
167

168
std::pair<uint16_t, uint16_t> XpressNetInterface::decoderAddressMinMax(DecoderProtocol protocol) const
5✔
169
{
170
  switch(protocol)
5✔
171
  {
172
    case DecoderProtocol::DCCShort:
5✔
173
      return {XpressNet::shortAddressMin, XpressNet::shortAddressMax};
5✔
174

UNCOV
175
    case DecoderProtocol::DCCLong:
×
176
      return {XpressNet::longAddressMin, XpressNet::longAddressMax};
×
177

UNCOV
178
    default: /*[[unlikely]]*/
×
179
      return DecoderController::decoderAddressMinMax(protocol);
×
180
  }
181
}
182

183
std::span<const uint8_t> XpressNetInterface::decoderSpeedSteps(DecoderProtocol protocol) const
5✔
184
{
185
  static constexpr std::array<uint8_t, 4> dccSpeedSteps{{14, 27, 28, 128}}; // XpressNet also support 27 steps
186

187
  switch(protocol)
5✔
188
  {
189
    case DecoderProtocol::DCCShort:
5✔
190
    case DecoderProtocol::DCCLong:
191
      return dccSpeedSteps;
5✔
192

UNCOV
193
    default: /*[[unlikely]]*/
×
194
      return DecoderController::decoderSpeedSteps(protocol);
×
195
  }
196
}
197

198
void XpressNetInterface::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber)
4✔
199
{
200
  if(m_kernel)
4✔
UNCOV
201
    m_kernel->decoderChanged(decoder, changes, functionNumber);
×
202
}
4✔
203

204
std::span<const InputChannel> XpressNetInterface::inputChannels() const
9✔
205
{
206
  static const auto values = makeArray(InputChannel::Input);
207
  return values;
9✔
208
}
209

UNCOV
210
std::pair<uint32_t, uint32_t> XpressNetInterface::inputAddressMinMax(InputChannel /*channel*/) const
×
211
{
UNCOV
212
  return {XpressNet::Kernel::inputAddressMin, XpressNet::Kernel::inputAddressMax};
×
213
}
214

UNCOV
215
void XpressNetInterface::inputSimulateChange(InputChannel channel, uint32_t address, SimulateInputAction action)
×
216
{
UNCOV
217
  if(m_kernel && inRange(address, inputAddressMinMax(channel)))
×
218
    m_kernel->simulateInputChange(address, action);
×
219
}
×
220

221
std::span<const OutputChannel> XpressNetInterface::outputChannels() const
20✔
222
{
223
  static const auto values = makeArray(OutputChannel::Accessory);
224
  return values;
20✔
225
}
226

227
std::pair<uint32_t, uint32_t> XpressNetInterface::outputAddressMinMax(OutputChannel /*channel*/) const
2✔
228
{
229
  return {XpressNet::Kernel::accessoryOutputAddressMin, XpressNet::Kernel::accessoryOutputAddressMax};
2✔
230
}
231

UNCOV
232
bool XpressNetInterface::setOutputValue(OutputChannel channel, const OutputLocation& location, OutputValue value)
×
233
{
UNCOV
234
  assert(isOutputChannel(channel));
×
235
  const auto address = std::get<OutputAddress>(location).address;
×
236
  return
237
      m_kernel &&
×
238
      inRange(address, outputAddressMinMax(channel)) &&
×
239
      m_kernel->setOutput(static_cast<uint16_t>(address), std::get<OutputPairValue>(value));
×
240
}
241

242
bool XpressNetInterface::setOnline(bool& value, bool simulation)
×
243
{
244
  if(!m_kernel && value)
×
245
  {
246
    try
247
    {
248
      if(simulation)
×
249
      {
250
        m_kernel = XpressNet::Kernel::create<XpressNet::SimulationIOHandler>(id.value(), xpressnet->config());
×
251
      }
252
      else
253
      {
254
        switch(type)
×
255
        {
256
          case XpressNetInterfaceType::Serial:
×
257
            switch(serialInterfaceType)
×
258
            {
259
              case XpressNetSerialInterfaceType::LenzLI100:
×
260
              case XpressNetSerialInterfaceType::LenzLI100F:
261
              case XpressNetSerialInterfaceType::LenzLI101F:
262
                m_kernel = XpressNet::Kernel::create<XpressNet::SerialIOHandler>(id.value(), xpressnet->config(), device.value(), baudrate.value(), flowControl.value());
×
263
                break;
×
264

265
              case XpressNetSerialInterfaceType::RoSoftS88XPressNetLI:
×
266
                m_kernel = XpressNet::Kernel::create<XpressNet::RoSoftS88XPressNetLIIOHandler>(id.value(), xpressnet->config(), device.value(), baudrate.value(), flowControl.value(), s88StartAddress.value(), s88ModuleCount.value());
×
267
                break;
×
268

269
              case XpressNetSerialInterfaceType::LenzLIUSB:
×
270
              case XpressNetSerialInterfaceType::DigikeijsDR5000:
271
                m_kernel = XpressNet::Kernel::create<XpressNet::LIUSBIOHandler>(id.value(), xpressnet->config(), device.value(), baudrate.value(), flowControl.value());
×
272
                break;
×
273
            }
274
            break;
×
275

276
          case XpressNetInterfaceType::Network:
×
277
            m_kernel = XpressNet::Kernel::create<XpressNet::TCPIOHandler>(id.value(), xpressnet->config(), hostname.value(), port.value());
×
278
            break;
×
279
        }
280
      }
281

282
      if(!m_kernel)
×
283
      {
284
        assert(false);
×
285
        return false;
286
      }
287

288
      setState(InterfaceState::Initializing);
×
289

290
      m_kernel->setOnStarted(
×
291
        [this]()
×
292
        {
293
          setState(InterfaceState::Online);
×
294
        });
×
295
      m_kernel->setOnError(
×
296
        [this]()
×
297
        {
298
          setState(InterfaceState::Error);
×
299
          online = false; // communication no longer possible
×
300
        });
×
301
      m_kernel->setOnTrackPowerChanged(
×
302
        [this](bool powerOn, bool isStopped)
×
303
        {
304
          if(powerOn)
×
305
          {
306
            /* NOTE:
307
             * Setting stop and powerOn together is not an atomic operation,
308
             * so it would trigger 2 state changes with in the middle state.
309
             * Fortunately this does not happen because at least one of the state is already set.
310
             * Because if we are in Run state we go to PowerOn,
311
             * and if we are on PowerOff then we go to PowerOn.
312
             */
313

314
            // First of all, stop if we have to, otherwhise we might inappropiately run trains
315
            if(isStopped && contains(m_world.state.value(), WorldState::Run))
×
316
            {
317
              m_world.stop();
×
318
            }
319
            else if(!contains(m_world.state.value(), WorldState::Run) && !isStopped)
×
320
            {
321
              m_world.run(); // Run trains yay!
×
322
            }
323

324
            // EmergencyStop in XpressNet also means power is still on
325
            if(!contains(m_world.state.value(), WorldState::PowerOn) && isStopped)
×
326
            {
327
              m_world.powerOn(); // Just power on but keep stopped
×
328
            }
329
          }
330
          else
331
          {
332
            // Power off regardless of stop state
333
            if(contains(m_world.state.value(), WorldState::PowerOn))
×
334
              m_world.powerOff();
×
335
          }
336
        });
×
337

338
      m_kernel->setDecoderController(this);
×
339
      m_kernel->setInputController(this);
×
340
      m_kernel->setOutputController(this);
×
341
      m_kernel->start();
×
342

343
      m_xpressnetPropertyChanged = xpressnet->propertyChanged.connect(
×
344
        [this](BaseProperty& /*property*/)
×
345
        {
346
          m_kernel->setConfig(xpressnet->config());
×
347
        });
×
348

349
      // Avoid to set multiple power states in rapid succession
350
      if(!contains(m_world.state.value(), WorldState::PowerOn))
×
351
        m_kernel->stopOperations(); // Stop by powering off
×
352
      else if(!contains(m_world.state.value(), WorldState::Run))
×
353
        m_kernel->stopAllLocomotives(); // Emergency stop with power on
×
354
      else
355
        m_kernel->resumeOperations(); // Run trains
×
356

357
      Attributes::setEnabled({type, serialInterfaceType, device, baudrate, flowControl, hostname, port, s88StartAddress, s88ModuleCount}, false);
×
358
    }
359
    catch(const LogMessageException& e)
×
360
    {
361
      setState(InterfaceState::Offline);
×
362
      Log::log(*this, e.message(), e.args());
×
363
      return false;
×
364
    }
×
365
  }
366
  else if(m_kernel && !value)
×
367
  {
368
    Attributes::setEnabled({type, serialInterfaceType, device, baudrate, flowControl, hostname, port, s88StartAddress, s88ModuleCount}, true);
×
369

370
    m_xpressnetPropertyChanged.disconnect();
×
371

372
    m_kernel->stop();
×
373
    EventLoop::deleteLater(m_kernel.release());
×
374

375
    setState(InterfaceState::Offline);
×
376
  }
377
  return true;
×
378
}
379

380
void XpressNetInterface::addToWorld()
9✔
381
{
382
  Interface::addToWorld();
9✔
383
  DecoderController::addToWorld();
9✔
384
  InputController::addToWorld(inputListColumns);
9✔
385
  OutputController::addToWorld(outputListColumns);
9✔
386
}
9✔
387

388
void XpressNetInterface::loaded()
×
389
{
390
  Interface::loaded();
×
391

392
  updateVisible();
×
393
}
×
394

395
void XpressNetInterface::destroying()
9✔
396
{
397
  OutputController::destroying();
9✔
398
  InputController::destroying();
9✔
399
  DecoderController::destroying();
9✔
400
  Interface::destroying();
9✔
401
}
9✔
402

403
void XpressNetInterface::worldEvent(WorldState state, WorldEvent event)
×
404
{
405
  Interface::worldEvent(state, event);
×
406

407
  if(m_kernel)
×
408
  {
409
    switch(event)
×
410
    {
411
      case WorldEvent::PowerOff:
×
412
      {
413
        m_kernel->stopOperations();
×
414
        break;
×
415
      }
416
      case WorldEvent::PowerOn:
×
417
      {
418
        if(contains(state, WorldState::Run))
×
419
          m_kernel->resumeOperations();
×
420
        else
421
          m_kernel->stopAllLocomotives(); // In XpressNet E-Stop means power on but not running
×
422
        break;
×
423
      }
424
      case WorldEvent::Stop:
×
425
      {
426
        if(contains(state, WorldState::PowerOn))
×
427
        {
428
          // In XpressNet E-Stop means power is on but trains are not running
429
          m_kernel->stopAllLocomotives();
×
430
        }
431
        else
432
        {
433
          // This Stops everything by removing power
434
          m_kernel->stopOperations();
×
435
        }
436
        break;
×
437
      }
438
      case WorldEvent::Run:
×
439
      {
440
        if(contains(state, WorldState::PowerOn))
×
441
          m_kernel->resumeOperations();
×
442
        break;
×
443
      }
444
      default:
×
445
        break;
×
446
    }
447
  }
448
}
×
449

450
void XpressNetInterface::updateVisible()
9✔
451
{
452
  const bool isSerial = (type == XpressNetInterfaceType::Serial);
9✔
453
  Attributes::setVisible(serialInterfaceType, isSerial);
9✔
454
  Attributes::setVisible(device, isSerial);
9✔
455
  Attributes::setVisible(baudrate, isSerial);
9✔
456
  Attributes::setVisible(flowControl, isSerial);
9✔
457

458
  const bool isNetwork = (type == XpressNetInterfaceType::Network);
9✔
459
  Attributes::setVisible(hostname, isNetwork);
9✔
460
  Attributes::setVisible(port, isNetwork);
9✔
461

462
  const bool isRoSoftS88XPressNetLI = isSerial && (serialInterfaceType == XpressNetSerialInterfaceType::RoSoftS88XPressNetLI);
9✔
463
  Attributes::setVisible(s88StartAddress, isRoSoftS88XPressNetLI);
9✔
464
  Attributes::setVisible(s88ModuleCount, isRoSoftS88XPressNetLI);
9✔
465
}
9✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc