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

traintastic / traintastic / 22784367195

06 Mar 2026 10:20PM UTC coverage: 26.774% (-0.3%) from 27.099%
22784367195

push

github

reinder
[cbus] added basic DecoderController support and CANCMD simulation.

0 of 258 new or added lines in 7 files covered. (0.0%)

12 existing lines in 3 files now uncovered.

8182 of 30559 relevant lines covered (26.77%)

187.36 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 "../output/list/outputlist.hpp"
28
#include "../protocol/cbus/cbusconst.hpp"
29
#include "../protocol/cbus/cbuskernel.hpp"
30
#include "../protocol/cbus/iohandler/cbuscanusbiohandler.hpp"
31
#include "../protocol/cbus/iohandler/cbuscanetheriohandler.hpp"
32
#include "../protocol/cbus/iohandler/cbussimulationiohandler.hpp"
33
#include "../protocol/cbus/simulator/cbussimulator.hpp"
34
#include "../protocol/cbus/simulator/module/cbuscancmd.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 "../../world/world.hpp"
43

44
constexpr auto decoderListColumns = DecoderListColumn::Id | DecoderListColumn::Name | DecoderListColumn::Address;
45
constexpr auto outputListColumns = OutputListColumn::Channel | OutputListColumn::Address;
46

47
CREATE_IMPL(CBUSInterface)
×
48

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

66
  Attributes::addDisplayName(type, DisplayName::Interface::type);
×
67
  Attributes::addEnabled(type, !online);
×
68
  Attributes::addValues(type, CBUSInterfaceTypeValues);
×
69
  m_interfaceItems.insertBefore(type, notes);
×
70

71
  Attributes::addEnabled(device, !online);
×
72
  Attributes::addVisible(device, false);
×
73
  m_interfaceItems.insertBefore(device, notes);
×
74

75
  Attributes::addDisplayName(hostname, DisplayName::IP::hostname);
×
76
  Attributes::addEnabled(hostname, !online);
×
77
  Attributes::addVisible(hostname, false);
×
78
  m_interfaceItems.insertBefore(hostname, notes);
×
79

80
  Attributes::addDisplayName(port, DisplayName::IP::port);
×
81
  Attributes::addEnabled(port, !online);
×
82
  Attributes::addVisible(port, false);
×
83
  m_interfaceItems.insertBefore(port, notes);
×
84

85
  m_interfaceItems.insertBefore(cbus, notes);
×
86

NEW
87
  m_interfaceItems.insertBefore(decoders, notes);
×
88

UNCOV
89
  m_interfaceItems.insertBefore(outputs, notes);
×
90

91
  m_cbusPropertyChanged = cbus->propertyChanged.connect(
×
92
    [this](BaseProperty& /*property*/)
×
93
    {
94
      if(m_kernel)
×
95
      {
96
        m_kernel->setConfig(cbus->config());
×
97
      }
98
    });
×
99

100
  updateVisible();
×
101
}
×
102

103
CBUSInterface::~CBUSInterface() = default;
×
104

105
bool CBUSInterface::send(std::vector<uint8_t> message)
×
106
{
107
  if(m_kernel)
×
108
  {
109
    return m_kernel->send(std::move(message));
×
110
  }
111
  return false;
×
112
}
113

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

NEW
123
std::span<const DecoderProtocol> CBUSInterface::decoderProtocols() const
×
124
{
125
  static constexpr std::array<DecoderProtocol, 2> protocols{DecoderProtocol::DCCShort, DecoderProtocol::DCCLong};
NEW
126
  return std::span<const DecoderProtocol>{protocols.data(), protocols.size()};
×
127
}
128

NEW
129
void CBUSInterface::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber)
×
130
{
NEW
131
  if(m_kernel)
×
132
  {
NEW
133
    const bool longAddress = (decoder.protocol == DecoderProtocol::DCCLong);
×
134

NEW
135
    if(has(changes, DecoderChangeFlags::FunctionValue) && functionNumber <= CBUS::engineFunctionMax)
×
136
    {
NEW
137
      m_kernel->setEngineFunction(
×
138
        decoder.address,
139
        longAddress,
140
        static_cast<uint8_t>(functionNumber),
NEW
141
        decoder.getFunctionValue(functionNumber));
×
142
    }
143
    else
144
    {
NEW
145
      m_kernel->setEngineSpeedDirection(
×
146
        decoder.address,
147
        longAddress,
NEW
148
        Decoder::throttleToSpeedStep<uint8_t>(decoder.throttle, decoder.speedSteps),
×
149
        decoder.speedSteps,
150
        decoder.emergencyStop,
NEW
151
        decoder.direction == Direction::Forward);
×
152
    }
153
  }
NEW
154
}
×
155

UNCOV
156
std::span<const OutputChannel> CBUSInterface::outputChannels() const
×
157
{
158
  static const std::array<OutputChannel, 2> channels{
159
    OutputChannel::CBUSAccessoryShort,
160
    OutputChannel::CBUSAccessory,
161
    //OutputChannel::AccessoryDCC,
162
    //OutputChannel::DCCext,
163
  };
164
  return channels;
×
165
}
166

167
std::pair<uint32_t, uint32_t> CBUSInterface::outputAddressMinMax(OutputChannel channel) const
×
168
{
169
  switch(channel)
×
170
  {
171
    case OutputChannel::CBUSAccessory:
×
172
    case OutputChannel::CBUSAccessoryShort:
173
      return {std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max()};
×
174

175
    default:
×
176
      return OutputController::outputAddressMinMax(channel);
×
177
  }
178
}
179

180
bool CBUSInterface::setOutputValue(OutputChannel channel, uint32_t address, OutputValue value)
×
181
{
182
  if(m_kernel)
×
183
  {
184
    switch(channel)
×
185
    {
186
      case OutputChannel::CBUSAccessoryShort:
×
187
        if(auto v = std::get<TriState>(value); v != TriState::Undefined)
×
188
        {
189
          m_kernel->setAccessoryShort(static_cast<uint16_t>(address), v == TriState::True);
×
190
          return true;
×
191
        }
192
        break;
×
193

194
      case OutputChannel::CBUSAccessory:
×
195
        if(auto v = std::get<TriState>(value); v != TriState::Undefined)
×
196
        {
197
          m_kernel->setAccessory(static_cast<uint16_t>(address), v == TriState::True);
×
198
          return true;
×
199
        }
200
        break;
×
201

202
      default: [[unlikely]]
×
203
        assert(false);
×
204
        break;
205
    }
206
  }
207
  return false;
×
208
}
209

210
void CBUSInterface::addToWorld()
×
211
{
212
  Interface::addToWorld();
×
NEW
213
  DecoderController::addToWorld();
×
214
  OutputController::addToWorld(outputListColumns);
×
215
}
×
216

217
void CBUSInterface::loaded()
×
218
{
219
  Interface::loaded();
×
220

221
  updateVisible();
×
222
}
×
223

224
void CBUSInterface::destroying()
×
225
{
226
  m_cbusPropertyChanged.disconnect();
×
227
  OutputController::destroying();
×
NEW
228
  DecoderController::destroying();
×
229
  Interface::destroying();
×
230
}
×
231

232
void CBUSInterface::worldEvent(WorldState state, WorldEvent event)
×
233
{
234
  Interface::worldEvent(state, event);
×
235

236
  switch(event)
×
237
  {
238
    case WorldEvent::PowerOff:
×
239
      if(m_kernel)
×
240
      {
241
        m_kernel->trackOff();
×
242
      }
243
      break;
×
244

245
    case WorldEvent::PowerOn:
×
246
      if(m_kernel)
×
247
      {
248
        m_kernel->trackOn();
×
249
      }
250
      break;
×
251

252
    case WorldEvent::Stop:
×
253
      if(m_kernel)
×
254
      {
255
        m_kernel->requestEmergencyStop();
×
256
      }
257
      break;
×
258

259
    case WorldEvent::Run:
×
260
      if(m_kernel)
×
261
      {
262
        // TODO: send all known speed values
263
      }
264
      break;
×
265

266
    default:
×
267
      break;
×
268
  }
269
}
×
270

271
bool CBUSInterface::setOnline(bool& value, bool simulation)
×
272
{
273
  if(!m_kernel && value)
×
274
  {
275
    try
276
    {
277
      if(simulation)
×
278
      {
279
        m_simulator = std::make_unique<CBUS::Simulator>();
×
280
        m_simulator->addModule(std::make_unique<CBUS::Module::CANCMD>(*m_simulator));
×
281
        m_kernel = CBUS::Kernel::create<CBUS::SimulationIOHandler>(id.value(), cbus->config(), std::ref(*m_simulator));
×
282
      }
283
      else
284
      {
285
        switch(type)
×
286
        {
287
          case CBUSInterfaceType::CANUSB:
×
288
            m_kernel = CBUS::Kernel::create<CBUS::CANUSBIOHandler>(id.value(), cbus->config(), device.value());
×
289
            break;
×
290

291
          case CBUSInterfaceType::CANEther:
×
292
            m_kernel = CBUS::Kernel::create<CBUS::CANEtherIOHandler>(id.value(), cbus->config(), hostname.value(), port.value());
×
293
            break;
×
294
        }
295
      }
296

297
      setState(InterfaceState::Initializing);
×
298

299
      m_kernel->setOnStarted(
×
300
        [this]()
×
301
        {
302
          setState(InterfaceState::Online);
×
303
        });
×
304
      m_kernel->setOnError(
×
305
        [this]()
×
306
        {
307
          setState(InterfaceState::Error);
×
308
          online = false; // communication no longer possible
×
309
        });
×
310
      m_kernel->onTrackOff =
×
311
        [this]()
×
312
        {
313
          m_world.powerOff();
×
314
        };
×
315
      m_kernel->onTrackOn =
×
316
        [this]()
×
317
        {
318
          m_world.powerOn();
×
319
        };
×
320
      m_kernel->onEmergencyStop =
×
321
        [this]()
×
322
        {
323
          m_world.stop();
×
324
        };
×
325

326
      m_kernel->start();
×
327
    }
328
    catch(const LogMessageException& e)
×
329
    {
330
      setState(InterfaceState::Offline);
×
331
      Log::log(*this, e.message(), e.args());
×
332
      return false;
×
333
    }
×
334
  }
335
  else if(m_kernel && !value)
×
336
  {
337
    m_kernel->stop();
×
338
    EventLoop::deleteLater(m_kernel.release());
×
339
    EventLoop::deleteLater(m_simulator.release());
×
340

341
    if(status->state != InterfaceState::Error)
×
342
    {
343
      setState(InterfaceState::Offline);
×
344
    }
345
  }
346
  return true;
×
347
}
348

349
void CBUSInterface::updateVisible()
×
350
{
351
  const bool isSerial = (type == CBUSInterfaceType::CANUSB);
×
352
  Attributes::setVisible(device, isSerial);
×
353

354
  const bool isNetwork = (type == CBUSInterfaceType::CANEther);
×
355
  Attributes::setVisible(hostname, isNetwork);
×
356
  Attributes::setVisible(port, isNetwork);
×
357
}
×
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