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

traintastic / traintastic / 23259071638

18 Mar 2026 05:50PM UTC coverage: 26.526% (-0.02%) from 26.544%
23259071638

push

github

reinder
[cbus] added DCCext output channel

0 of 21 new or added lines in 3 files covered. (0.0%)

3 existing lines in 2 files now uncovered.

8246 of 31086 relevant lines covered (26.53%)

184.9 hits per line

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

0.0
/server/src/hardware/interface/cbusinterface.cpp
1
/**
2
 * This file is part of Traintastic,
3
 * see <https://github.com/traintastic/traintastic>.
4
 *
5
 * Copyright (C) 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 "cbusinterface.hpp"
23
#include "cbus/cbussettings.hpp"
24
#include "../decoder/decoderchangeflags.hpp"
25
#include "../decoder/list/decoderlist.hpp"
26
#include "../decoder/list/decoderlisttablemodel.hpp"
27
#include "../input/list/inputlist.hpp"
28
#include "../output/list/outputlist.hpp"
29
#include "../protocol/cbus/cbusconst.hpp"
30
#include "../protocol/cbus/cbuskernel.hpp"
31
#include "../protocol/cbus/iohandler/cbuscanusbiohandler.hpp"
32
#include "../protocol/cbus/iohandler/cbuscanetheriohandler.hpp"
33
#include "../protocol/cbus/iohandler/cbussimulationiohandler.hpp"
34
#include "../protocol/cbus/simulator/cbussimulator.hpp"
35
#include "../protocol/cbus/simulator/module/cbuscancmd.hpp"
36
#include "../../core/attributes.hpp"
37
#include "../../core/eventloop.hpp"
38
#include "../../core/method.tpp"
39
#include "../../core/objectproperty.tpp"
40
#include "../../log/log.hpp"
41
#include "../../log/logmessageexception.hpp"
42
#include "../../utils/displayname.hpp"
43
#include "../../utils/inrange.hpp"
44
#include "../../world/world.hpp"
45

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

50
constexpr auto nodeNumberRange = std::make_pair(std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max());
51
constexpr auto eventNumberRange = std::make_pair(std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max());
52

53
CREATE_IMPL(CBUSInterface)
×
54

55
CBUSInterface::CBUSInterface(World& world, std::string_view _id)
×
56
  : Interface(world, _id)
57
  , DecoderController(*this, decoderListColumns)
58
  , InputController(static_cast<IdObject&>(*this))
59
  , OutputController(static_cast<IdObject&>(*this))
60
  , type{this, "type", CBUSInterfaceType::CANUSB, PropertyFlags::ReadWrite | PropertyFlags::Store,
×
61
      [this](CBUSInterfaceType /*value*/)
×
62
      {
63
        updateVisible();
×
64
      }}
×
65
  , device{this, "device", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
×
66
  , hostname{this, "hostname", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
×
67
  , port{this, "port", 0, PropertyFlags::ReadWrite | PropertyFlags::Store}
×
68
  , cbus{this, "cbus", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
×
69
{
70
  name = "CBUS/VLCB";
×
71
  cbus.setValueInternal(std::make_shared<CBUSSettings>(*this, cbus.name()));
×
72

73
  Attributes::addDisplayName(type, DisplayName::Interface::type);
×
74
  Attributes::addEnabled(type, !online);
×
75
  Attributes::addValues(type, CBUSInterfaceTypeValues);
×
76
  m_interfaceItems.insertBefore(type, notes);
×
77

78
  Attributes::addEnabled(device, !online);
×
79
  Attributes::addVisible(device, false);
×
80
  m_interfaceItems.insertBefore(device, notes);
×
81

82
  Attributes::addDisplayName(hostname, DisplayName::IP::hostname);
×
83
  Attributes::addEnabled(hostname, !online);
×
84
  Attributes::addVisible(hostname, false);
×
85
  m_interfaceItems.insertBefore(hostname, notes);
×
86

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

92
  m_interfaceItems.insertBefore(cbus, notes);
×
93

94
  m_interfaceItems.insertBefore(decoders, notes);
×
95

96
  m_interfaceItems.insertBefore(inputs, notes);
×
97

98
  m_interfaceItems.insertBefore(outputs, notes);
×
99

100
  m_cbusPropertyChanged = cbus->propertyChanged.connect(
×
101
    [this](BaseProperty& /*property*/)
×
102
    {
103
      if(m_kernel)
×
104
      {
105
        m_kernel->setConfig(cbus->config());
×
106
      }
107
    });
×
108

109
  updateVisible();
×
110
}
×
111

112
CBUSInterface::~CBUSInterface() = default;
×
113

114
bool CBUSInterface::send(std::vector<uint8_t> message)
×
115
{
116
  if(m_kernel)
×
117
  {
118
    return m_kernel->send(std::move(message));
×
119
  }
120
  return false;
×
121
}
122

123
bool CBUSInterface::sendDCC(std::vector<uint8_t> dccPacket, uint8_t repeat)
×
124
{
125
  if(m_kernel)
×
126
  {
127
    return m_kernel->sendDCC(std::move(dccPacket), repeat);
×
128
  }
129
  return false;
×
130
}
131

132
std::span<const DecoderProtocol> CBUSInterface::decoderProtocols() const
×
133
{
134
  static constexpr std::array<DecoderProtocol, 2> protocols{DecoderProtocol::DCCShort, DecoderProtocol::DCCLong};
135
  return std::span<const DecoderProtocol>{protocols.data(), protocols.size()};
×
136
}
137

138
void CBUSInterface::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber)
×
139
{
140
  if(m_kernel)
×
141
  {
142
    const bool longAddress = (decoder.protocol == DecoderProtocol::DCCLong);
×
143

144
    if(has(changes, DecoderChangeFlags::FunctionValue) && functionNumber <= CBUS::engineFunctionMax)
×
145
    {
146
      m_kernel->setEngineFunction(
×
147
        decoder.address,
148
        longAddress,
149
        static_cast<uint8_t>(functionNumber),
150
        decoder.getFunctionValue(functionNumber));
×
151
    }
152
    else
153
    {
154
      m_kernel->setEngineSpeedDirection(
×
155
        decoder.address,
156
        longAddress,
157
        Decoder::throttleToSpeedStep<uint8_t>(decoder.throttle, decoder.speedSteps),
×
158
        decoder.speedSteps,
159
        decoder.emergencyStop,
160
        decoder.direction == Direction::Forward);
×
161
    }
162
  }
163
}
×
164

165
std::span<const InputChannel> CBUSInterface::inputChannels() const
×
166
{
167
  static const std::array<InputChannel, 2> channels{
168
    InputChannel::ShortEvent,
169
    InputChannel::LongEvent,
170
  };
171
  return channels;
×
172
}
173

174
bool CBUSInterface::isInputLocation(InputChannel channel, const InputLocation& location) const
×
175
{
176
  if(hasNodeAddressLocation(channel))
×
177
  {
178
    return
179
      inRange<uint32_t>(std::get<InputNodeAddress>(location).node, nodeNumberRange) &&
×
180
      inRange<uint32_t>(std::get<InputNodeAddress>(location).address, inputAddressMinMax(channel));
×
181
  }
182
  return InputController::isInputLocation(channel, location);
×
183
}
184

185
std::pair<uint32_t, uint32_t> CBUSInterface::inputAddressMinMax(InputChannel /*channel*/) const
×
186
{
187
  return eventNumberRange;
×
188
}
189

190
void CBUSInterface::inputSimulateChange(InputChannel channel, const InputLocation& location, SimulateInputAction action)
×
191
{
192
  if(m_simulator)
×
193
  {
194
    m_kernel->ioContext().post(
×
195
      [this, channel, location, action]
×
196
      {
197
        switch(channel)
×
198
        {
199
          case InputChannel::ShortEvent:
×
200
            m_simulator->shortEvent(std::get<InputAddress>(location).address, action);
×
201
            break;
×
202

203
          case InputChannel::LongEvent:
×
204
            m_simulator->longEvent(std::get<InputNodeAddress>(location).node, std::get<InputNodeAddress>(location).address, action);
×
205
            break;
×
206

207
          default: [[unlikely]]
×
208
            assert(false);
×
209
            break;
210
        }
211
      });
×
212
  }
213
}
×
214

215
std::span<const OutputChannel> CBUSInterface::outputChannels() const
×
216
{
217
  static const std::array<OutputChannel, 3> channels{
218
    OutputChannel::ShortEvent,
219
    OutputChannel::LongEvent,
220
    //OutputChannel::AccessoryDCC,
221
    OutputChannel::DCCext,
222
  };
223
  return channels;
×
224
}
225

226
std::pair<uint32_t, uint32_t> CBUSInterface::outputNodeMinMax(OutputChannel /*channel*/) const
×
227
{
228
  return eventNumberRange;
×
229
}
230

231
std::pair<uint32_t, uint32_t> CBUSInterface::outputAddressMinMax(OutputChannel channel) const
×
232
{
233
  switch(channel)
×
234
  {
235
    case OutputChannel::LongEvent:
×
236
    case OutputChannel::ShortEvent:
237
      return {std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max()};
×
238

239
    default:
×
240
      return OutputController::outputAddressMinMax(channel);
×
241
  }
242
}
243

244
bool CBUSInterface::setOutputValue(OutputChannel channel, const OutputLocation& location, OutputValue value)
×
245
{
246
  if(m_kernel)
×
247
  {
248
    switch(channel)
×
249
    {
250
      case OutputChannel::ShortEvent:
×
NEW
251
        assert(std::holds_alternative<TriState>(value));
×
UNCOV
252
        if(auto v = std::get<TriState>(value); v != TriState::Undefined)
×
253
        {
254
          const auto address = static_cast<uint16_t>(std::get<OutputAddress>(location).address);
×
255
          m_kernel->setAccessoryShort(address, v == TriState::True);
×
256
          return true;
×
257
        }
258
        break;
×
259

260
      case OutputChannel::LongEvent:
×
NEW
261
        assert(std::holds_alternative<TriState>(value));
×
UNCOV
262
        if(auto v = std::get<TriState>(value); v != TriState::Undefined)
×
263
        {
264
          const auto node = static_cast<uint16_t>(std::get<OutputNodeAddress>(location).node);
×
265
          const auto address = static_cast<uint16_t>(std::get<OutputNodeAddress>(location).address);
×
266
          m_kernel->setAccessory(node, address, v == TriState::True);
×
267
          return true;
×
268
        }
269
        break;
×
270

NEW
271
      case OutputChannel::DCCext:
×
NEW
272
        assert(std::holds_alternative<int16_t>(value));
×
NEW
273
        if(const auto v = std::get<int16_t>(value); inRange<int16_t>(v, std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max()))
×
274
        {
NEW
275
          const auto address = static_cast<uint16_t>(std::get<OutputAddress>(location).address);
×
NEW
276
          m_kernel->setDccAdvancedAccessoryValue(address, static_cast<uint8_t>(v));
×
NEW
277
          return true;
×
278
        }
NEW
279
        break;
×
280

281
      default: [[unlikely]]
×
282
        assert(false);
×
283
        break;
284
    }
285
  }
286
  return false;
×
287
}
288

289
void CBUSInterface::addToWorld()
×
290
{
291
  Interface::addToWorld();
×
292
  DecoderController::addToWorld();
×
293
  InputController::addToWorld(inputListColumns);
×
294
  OutputController::addToWorld(outputListColumns);
×
295
}
×
296

297
void CBUSInterface::loaded()
×
298
{
299
  Interface::loaded();
×
300

301
  updateVisible();
×
302
}
×
303

304
void CBUSInterface::destroying()
×
305
{
306
  m_cbusPropertyChanged.disconnect();
×
307
  OutputController::destroying();
×
308
  InputController::destroying();
×
309
  DecoderController::destroying();
×
310
  Interface::destroying();
×
311
}
×
312

313
void CBUSInterface::worldEvent(WorldState state, WorldEvent event)
×
314
{
315
  Interface::worldEvent(state, event);
×
316

317
  switch(event)
×
318
  {
319
    case WorldEvent::PowerOff:
×
320
      if(m_kernel)
×
321
      {
322
        m_kernel->trackOff();
×
323
      }
324
      break;
×
325

326
    case WorldEvent::PowerOn:
×
327
      if(m_kernel)
×
328
      {
329
        m_kernel->trackOn();
×
330
      }
331
      break;
×
332

333
    case WorldEvent::Stop:
×
334
      if(m_kernel)
×
335
      {
336
        m_kernel->requestEmergencyStop();
×
337
      }
338
      break;
×
339

340
    case WorldEvent::Run:
×
341
      if(m_kernel)
×
342
      {
343
        // TODO: send all known speed values
344
      }
345
      break;
×
346

347
    default:
×
348
      break;
×
349
  }
350
}
×
351

352
bool CBUSInterface::setOnline(bool& value, bool simulation)
×
353
{
354
  if(!m_kernel && value)
×
355
  {
356
    try
357
    {
358
      if(simulation)
×
359
      {
360
        m_simulator = std::make_unique<CBUS::Simulator>();
×
361
        m_simulator->addModule(std::make_unique<CBUS::Module::CANCMD>(*m_simulator));
×
362
        m_kernel = CBUS::Kernel::create<CBUS::SimulationIOHandler>(id.value(), cbus->config(), std::ref(*m_simulator));
×
363
      }
364
      else
365
      {
366
        switch(type)
×
367
        {
368
          case CBUSInterfaceType::CANUSB:
×
369
            m_kernel = CBUS::Kernel::create<CBUS::CANUSBIOHandler>(id.value(), cbus->config(), device.value());
×
370
            break;
×
371

372
          case CBUSInterfaceType::CANEther:
×
373
            m_kernel = CBUS::Kernel::create<CBUS::CANEtherIOHandler>(id.value(), cbus->config(), hostname.value(), port.value());
×
374
            break;
×
375
        }
376
      }
377

378
      setState(InterfaceState::Initializing);
×
379

380
      m_kernel->setOnStarted(
×
381
        [this]()
×
382
        {
383
          setState(InterfaceState::Online);
×
384
        });
×
385
      m_kernel->setOnError(
×
386
        [this]()
×
387
        {
388
          setState(InterfaceState::Error);
×
389
          online = false; // communication no longer possible
×
390
        });
×
391
      m_kernel->onTrackOff =
×
392
        [this]()
×
393
        {
394
          if(contains(m_world.state, WorldState::PowerOn))
×
395
          {
396
            m_world.powerOff();
×
397
          }
398
        };
×
399
      m_kernel->onTrackOn =
×
400
        [this]()
×
401
        {
402
          if(!contains(m_world.state, WorldState::PowerOn))
×
403
          {
404
            m_world.powerOn();
×
405
          }
406
        };
×
407
      m_kernel->onEmergencyStop =
×
408
        [this]()
×
409
        {
410
          if(contains(m_world.state, WorldState::Run))
×
411
          {
412
            m_world.stop();
×
413
          }
414
        };
×
415
      m_kernel->onShortEvent =
×
416
        [this](uint16_t eventNumber, bool on)
×
417
        {
418
          updateInputValue(InputChannel::ShortEvent, InputAddress(eventNumber), toTriState(on));
×
419
          updateOutputValue(OutputChannel::ShortEvent, OutputAddress(eventNumber), toTriState(on));
×
420
        };
×
421
      m_kernel->onLongEvent =
×
422
        [this](uint16_t nodeNumber, uint16_t eventNumber, bool on)
×
423
        {
424
          updateInputValue(InputChannel::LongEvent, InputNodeAddress(nodeNumber, eventNumber), toTriState(on));
×
425
          updateOutputValue(OutputChannel::LongEvent, OutputNodeAddress(nodeNumber, eventNumber), toTriState(on));
×
426
        };
×
427

428
      m_kernel->start();
×
429
    }
430
    catch(const LogMessageException& e)
×
431
    {
432
      setState(InterfaceState::Offline);
×
433
      Log::log(*this, e.message(), e.args());
×
434
      return false;
×
435
    }
×
436
  }
437
  else if(m_kernel && !value)
×
438
  {
439
    m_kernel->stop();
×
440
    EventLoop::deleteLater(m_kernel.release());
×
441
    EventLoop::deleteLater(m_simulator.release());
×
442

443
    if(status->state != InterfaceState::Error)
×
444
    {
445
      setState(InterfaceState::Offline);
×
446
    }
447
  }
448
  return true;
×
449
}
450

451
void CBUSInterface::updateVisible()
×
452
{
453
  const bool isSerial = (type == CBUSInterfaceType::CANUSB);
×
454
  Attributes::setVisible(device, isSerial);
×
455

456
  const bool isNetwork = (type == CBUSInterfaceType::CANEther);
×
457
  Attributes::setVisible(hostname, isNetwork);
×
458
  Attributes::setVisible(port, isNetwork);
×
459
}
×
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