• 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

39.73
/server/src/hardware/interface/dccexinterface.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 2021-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 "dccexinterface.hpp"
23
#include "../decoder/list/decoderlist.hpp"
24
#include "../decoder/list/decoderlisttablemodel.hpp"
25
#include "../input/list/inputlist.hpp"
26
#include "../output/list/outputlist.hpp"
27
#include "../protocol/dcc/dcc.hpp"
28
#include "../protocol/dccex/kernel.hpp"
29
#include "../protocol/dccex/settings.hpp"
30
#include "../protocol/dccex/messages.hpp"
31
#include "../protocol/dccex/iohandler/serialiohandler.hpp"
32
#include "../protocol/dccex/iohandler/tcpiohandler.hpp"
33
#include "../protocol/dccex/iohandler/simulationiohandler.hpp"
34
#include "../../core/attributes.hpp"
35
#include "../../core/eventloop.hpp"
36
#include "../../core/method.tpp"
37
#include "../../core/objectproperty.tpp"
38
#include "../../log/log.hpp"
39
#include "../../log/logmessageexception.hpp"
40
#include "../../utils/displayname.hpp"
41
#include "../../utils/inrange.hpp"
42
#include "../../utils/makearray.hpp"
43
#include "../../utils/serialport.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::Channel | OutputListColumn::Address;
49

50
CREATE_IMPL(DCCEXInterface)
8✔
51

52
DCCEXInterface::DCCEXInterface(World& world, std::string_view _id)
8✔
53
  : Interface(world, _id)
54
  , DecoderController(*this, decoderListColumns)
55
  , InputController(static_cast<IdObject&>(*this))
56
  , OutputController(static_cast<IdObject&>(*this))
57
  , type{this, "type", DCCEXInterfaceType::Serial, PropertyFlags::ReadWrite | PropertyFlags::Store,
16✔
58
      [this](DCCEXInterfaceType /*value*/)
8✔
59
      {
UNCOV
60
        updateVisible();
×
61
      }}
×
62
  , device{this, "device", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
16✔
63
  , baudrate{this, "baudrate", 115200, PropertyFlags::ReadWrite | PropertyFlags::Store}
8✔
64
  , hostname{this, "hostname", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
16✔
65
  , port{this, "port", 2560, PropertyFlags::ReadWrite | PropertyFlags::Store}
8✔
66
  , dccex{this, "dccex", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
16✔
67
{
68
  name = "DCC-EX";
8✔
69
  dccex.setValueInternal(std::make_shared<DCCEX::Settings>(*this, dccex.name()));
8✔
70

71
  Attributes::addDisplayName(type, DisplayName::Interface::type);
8✔
72
  Attributes::addEnabled(type, !online);
8✔
73
  Attributes::addValues(type, DCCEXInterfaceTypeValues);
8✔
74
  m_interfaceItems.insertBefore(type, notes);
8✔
75

76
  Attributes::addEnabled(device, !online);
8✔
77
  Attributes::addVisible(device, false);
8✔
78
  m_interfaceItems.insertBefore(device, notes);
8✔
79

80
  Attributes::addDisplayName(baudrate, DisplayName::Serial::baudrate);
8✔
81
  Attributes::addEnabled(baudrate, !online);
8✔
82
  Attributes::addVisible(baudrate, false);
8✔
83
  Attributes::addMinMax(baudrate, SerialPort::baudrateMin, SerialPort::baudrateMax);
8✔
84
  Attributes::addValues(baudrate, SerialPort::baudrateValues);
8✔
85
  m_interfaceItems.insertBefore(baudrate, notes);
8✔
86

87
  Attributes::addDisplayName(hostname, DisplayName::IP::hostname);
8✔
88
  Attributes::addEnabled(hostname, !online);
8✔
89
  Attributes::addVisible(hostname, false);
8✔
90
  m_interfaceItems.insertBefore(hostname, notes);
8✔
91

92
  Attributes::addDisplayName(port, DisplayName::IP::port);
8✔
93
  Attributes::addEnabled(port, !online);
8✔
94
  Attributes::addVisible(port, false);
8✔
95
  m_interfaceItems.insertBefore(port, notes);
8✔
96

97
  Attributes::addDisplayName(dccex, DisplayName::Hardware::dccex);
8✔
98
  m_interfaceItems.insertBefore(dccex, notes);
8✔
99

100
  m_interfaceItems.insertBefore(decoders, notes);
8✔
101

102
  m_interfaceItems.insertBefore(inputs, notes);
8✔
103

104
  m_interfaceItems.insertBefore(outputs, notes);
8✔
105

106
  m_dccexPropertyChanged = dccex->propertyChanged.connect(
24✔
107
    [this](BaseProperty& property)
8✔
108
    {
UNCOV
109
      if(m_kernel && &property != &dccex->startupDelay)
×
110
        m_kernel->setConfig(dccex->config());
×
111

UNCOV
112
      if(&property == &dccex->speedSteps)
×
113
      {
114
        // update speedsteps of all decoders, DCC-EX only has a global speedsteps setting
UNCOV
115
        const auto values = decoderSpeedSteps(DecoderProtocol::DCCShort); // identical for DCCLong
×
116
        assert(values.size() == 1);
×
117
        for(const auto& decoder : *decoders)
×
118
        {
UNCOV
119
          Attributes::setValues(decoder->speedSteps, values);
×
120
          decoder->speedSteps.setValueInternal(values.front());
×
121
        }
122
      }
123
    });
8✔
124

125
  updateEnabled();
8✔
126
  updateVisible();
8✔
127
}
8✔
128

129
DCCEXInterface::~DCCEXInterface() = default;
8✔
130

131
std::span<const DecoderProtocol> DCCEXInterface::decoderProtocols() const
10✔
132
{
133
  static constexpr std::array<DecoderProtocol, 2> protocols{DecoderProtocol::DCCShort, DecoderProtocol::DCCLong};
134
  return std::span<const DecoderProtocol>{protocols.data(), protocols.size()};
20✔
135
}
136

137
std::pair<uint16_t, uint16_t> DCCEXInterface::decoderAddressMinMax(DecoderProtocol protocol) const
5✔
138
{
139
  if(protocol == DecoderProtocol::DCCLong)
5✔
UNCOV
140
    return {DCC::addressLongStart, DCC::addressLongMax}; // DCC-EX considers all addresses below 128 as short.
×
141
  return DecoderController::decoderAddressMinMax(protocol);
5✔
142
}
143

144
std::span<const uint8_t> DCCEXInterface::decoderSpeedSteps(DecoderProtocol protocol) const
5✔
145
{
146
  (void)protocol; // silence unused warning for release build
147
  assert(protocol == DecoderProtocol::DCCShort || protocol == DecoderProtocol::DCCLong);
5✔
148
  const auto& speedStepValues = DCCEX::Settings::speedStepValues;
5✔
149
  // find value in array so we can create a span, using a span of a variable won't work due to the compare with prevous value in the attribute setter
150
  if(const auto it = std::find(speedStepValues.begin(), speedStepValues.end(), dccex->speedSteps); it != speedStepValues.end()) /*[[likely]]/*/ // NOLINT(readability-qualified-auto) windows requires const auto
5✔
151
    return {&(*it), 1};
5✔
UNCOV
152
  assert(false);
×
153
  return {};
154
}
155

156
void DCCEXInterface::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber)
4✔
157
{
158
  if(m_kernel)
4✔
UNCOV
159
    m_kernel->decoderChanged(decoder, changes, functionNumber);
×
160
}
4✔
161

162
std::span<const InputChannel> DCCEXInterface::inputChannels() const
8✔
163
{
164
  static const auto values = makeArray(InputChannel::Input);
165
  return values;
8✔
166
}
167

UNCOV
168
std::pair<uint32_t, uint32_t> DCCEXInterface::inputAddressMinMax(InputChannel /*channel*/) const
×
169
{
UNCOV
170
  return {DCCEX::Kernel::idMin, DCCEX::Kernel::idMax};
×
171
}
172

UNCOV
173
void DCCEXInterface::inputSimulateChange(InputChannel channel, uint32_t address, SimulateInputAction action)
×
174
{
UNCOV
175
  if(m_kernel && inRange(address, inputAddressMinMax(channel)))
×
176
    m_kernel->simulateInputChange(address, action);
×
177
}
×
178

179
std::span<const OutputChannel> DCCEXInterface::outputChannels() const
40✔
180
{
181
  static const auto values = makeArray(OutputChannel::Accessory, OutputChannel::Turnout, OutputChannel::Output, OutputChannel::DCCext);
182
  return values;
40✔
183
}
184

UNCOV
185
std::pair<uint32_t, uint32_t> DCCEXInterface::outputAddressMinMax(OutputChannel channel) const
×
186
{
187
  using namespace DCCEX;
188

UNCOV
189
  switch(channel)
×
190
  {
UNCOV
191
    case OutputChannel::Accessory:
×
192
      return OutputController::outputAddressMinMax(OutputChannel::AccessoryDCC);
×
193

UNCOV
194
    case OutputChannel::Turnout:
×
195
    case OutputChannel::Output:
UNCOV
196
      return {Kernel::idMin, Kernel::idMax};
×
197

UNCOV
198
    default:
×
199
      return OutputController::outputAddressMinMax(channel);
×
200
  }
201
}
202

UNCOV
203
bool DCCEXInterface::setOutputValue(OutputChannel channel, const OutputLocation& location, OutputValue value)
×
204
{
UNCOV
205
  const auto address = std::get<OutputAddress>(location).address;
×
206
  return
207
    m_kernel &&
×
208
    inRange(address, outputAddressMinMax(channel)) &&
×
209
    m_kernel->setOutput(channel, static_cast<uint16_t>(address), value);
×
210
}
211

212
bool DCCEXInterface::setOnline(bool& value, bool simulation)
×
213
{
214
  if(!m_kernel && value)
×
215
  {
216
    try
217
    {
218
      if(simulation)
×
219
      {
220
        m_kernel = DCCEX::Kernel::create<DCCEX::SimulationIOHandler>(id.value(), dccex->config());
×
221
      }
222
      else
223
      {
224
        switch(type)
×
225
        {
226
          case DCCEXInterfaceType::Serial:
×
227
            m_kernel = DCCEX::Kernel::create<DCCEX::SerialIOHandler>(id.value(), dccex->config(), device.value(), baudrate.value(), SerialFlowControl::None);
×
228
            break;
×
229

230
          case DCCEXInterfaceType::NetworkTCP:
×
231
            m_kernel = DCCEX::Kernel::create<DCCEX::TCPIOHandler>(id.value(), dccex->config(), hostname.value(), port.value());
×
232
            break;
×
233
        }
234

235
      }
236

237
      setState(InterfaceState::Initializing);
×
238

239
      m_kernel->setOnStarted(
×
240
        [this]()
×
241
        {
242
          setState(InterfaceState::Online);
×
243

244
          const bool powerOn = contains(m_world.state.value(), WorldState::PowerOn);
×
245

246
          if(!powerOn)
×
247
            m_kernel->powerOff();
×
248

249
          if(contains(m_world.state.value(), WorldState::Run))
×
250
          {
251
            m_kernel->clearEmergencyStop();
×
252
            restoreDecoderSpeed();
×
253
          }
254
          else
255
            m_kernel->emergencyStop();
×
256

257
          if(powerOn)
×
258
            m_kernel->powerOn();
×
259
        });
×
260
      m_kernel->setOnError(
×
261
        [this]()
×
262
        {
263
          setState(InterfaceState::Error);
×
264
          online = false; // communication no longer possible
×
265
        });
×
266
      m_kernel->setOnPowerOnChanged(
×
267
        [this](bool powerOn)
×
268
        {
269
          if(powerOn && !contains(m_world.state.value(), WorldState::PowerOn))
×
270
            m_world.powerOn();
×
271
          else if(!powerOn && contains(m_world.state.value(), WorldState::PowerOn))
×
272
            m_world.powerOff();
×
273
        });
×
274
      m_kernel->setDecoderController(this);
×
275
      m_kernel->setInputController(this);
×
276
      m_kernel->setOutputController(this);
×
277
      m_kernel->start();
×
278

279
      Attributes::setEnabled({type, device, baudrate, hostname, port}, false);
×
280
    }
281
    catch(const LogMessageException& e)
×
282
    {
283
      setState(InterfaceState::Offline);
×
284
      Log::log(*this, e.message(), e.args());
×
285
      return false;
×
286
    }
×
287
  }
288
  else if(m_kernel && !value)
×
289
  {
290
    Attributes::setEnabled({type, device, baudrate, hostname, port}, true);
×
291

292
    m_kernel->stop();
×
293
    EventLoop::deleteLater(m_kernel.release());
×
294

295
    if(status->state != InterfaceState::Error)
×
296
    {
297
      setState(InterfaceState::Offline);
×
298
    }
299
  }
300
  return true;
×
301
}
302

303
void DCCEXInterface::addToWorld()
8✔
304
{
305
  Interface::addToWorld();
8✔
306
  DecoderController::addToWorld();
8✔
307
  InputController::addToWorld(inputListColumns);
8✔
308
  OutputController::addToWorld(outputListColumns);
8✔
309
}
8✔
310

311
void DCCEXInterface::loaded()
×
312
{
313
  Interface::loaded();
×
314

315
  check();
×
316
  updateEnabled();
×
317
  updateVisible();
×
318
}
×
319

320
void DCCEXInterface::destroying()
8✔
321
{
322
  m_dccexPropertyChanged.disconnect();
8✔
323
  OutputController::destroying();
8✔
324
  InputController::destroying();
8✔
325
  DecoderController::destroying();
8✔
326
  Interface::destroying();
8✔
327
}
8✔
328

329
void DCCEXInterface::worldEvent(WorldState state, WorldEvent event)
×
330
{
331
  Interface::worldEvent(state, event);
×
332

333
  switch(event)
×
334
  {
335
    case WorldEvent::EditEnabled:
×
336
    case WorldEvent::EditDisabled:
337
      updateEnabled();
×
338
      break;
×
339

340
    case WorldEvent::PowerOff:
×
341
      if(m_kernel)
×
342
      {
343
        m_kernel->powerOff();
×
344
        m_kernel->emergencyStop();
×
345
      }
346
      break;
×
347

348
    case WorldEvent::PowerOn:
×
349
      if(m_kernel)
×
350
      {
351
        m_kernel->powerOn();
×
352
      }
353
      break;
×
354

355
    case WorldEvent::Stop:
×
356
      if(m_kernel)
×
357
      {
358
        m_kernel->emergencyStop();
×
359
      }
360
      updateEnabled();
×
361
      break;
×
362

363
    case WorldEvent::Run:
×
364
      if(m_kernel)
×
365
      {
366
        m_kernel->powerOn();
×
367
        m_kernel->clearEmergencyStop();
×
368
      }
369
      restoreDecoderSpeed();
×
370
      updateEnabled();
×
371
      break;
×
372

373
    default:
×
374
      break;
×
375
  }
376
}
×
377

378
void DCCEXInterface::check() const
×
379
{
380
  for(const auto& decoder : *decoders)
×
381
    checkDecoder(*decoder);
×
382
}
×
383

384
void DCCEXInterface::checkDecoder(const Decoder& decoder)
×
385
{
386
  for(const auto& function : *decoder.functions)
×
387
    if(function->number > DCCEX::Config::functionNumberMax)
×
388
    {
389
      Log::log(decoder, LogMessage::W2002_COMMAND_STATION_DOESNT_SUPPORT_FUNCTIONS_ABOVE_FX, DCCEX::Config::functionNumberMax);
×
390
      break;
×
391
    }
392
}
×
393

394
void DCCEXInterface::updateEnabled()
8✔
395
{
396
  const bool editable = contains(m_world.state, WorldState::Edit);
8✔
397
  const bool stopped = !contains(m_world.state, WorldState::Run);
8✔
398

399
  Attributes::setEnabled(dccex->speedSteps, editable && stopped);
8✔
400
}
8✔
401

402
void DCCEXInterface::updateVisible()
8✔
403
{
404
  const bool isSerial = (type == DCCEXInterfaceType::Serial);
8✔
405
  Attributes::setVisible(device, isSerial);
8✔
406
  Attributes::setVisible(baudrate, isSerial);
8✔
407

408
  const bool isNetwork = (type == DCCEXInterfaceType::NetworkTCP);
8✔
409
  Attributes::setVisible(hostname, isNetwork);
8✔
410
  Attributes::setVisible(port, isNetwork);
8✔
411
}
8✔
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