• 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

34.9
/server/src/hardware/interface/loconetinterface.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 "loconetinterface.hpp"
23
#include "../decoder/list/decoderlist.hpp"
24
#include "../decoder/list/decoderlisttablemodel.hpp"
25
#include "../input/input.hpp"
26
#include "../input/list/inputlist.hpp"
27
#include "../output/list/outputlist.hpp"
28
#include "../identification/list/identificationlist.hpp"
29
#include "../identification/identification.hpp"
30
#include "../programming/lncv/lncvprogrammer.hpp"
31
#include "../protocol/dcc/dcc.hpp"
32
#include "../protocol/loconet/kernel.hpp"
33
#include "../protocol/loconet/settings.hpp"
34
#include "../protocol/loconet/iohandler/serialiohandler.hpp"
35
#include "../protocol/loconet/iohandler/simulationiohandler.hpp"
36
#include "../protocol/loconet/iohandler/tcpbinaryiohandler.hpp"
37
#include "../protocol/loconet/iohandler/lbserveriohandler.hpp"
38
#include "../protocol/loconet/iohandler/z21iohandler.hpp"
39
#include "../../core/attributes.hpp"
40
#include "../../core/controllerlist.hpp"
41
#include "../../core/eventloop.hpp"
42
#include "../../core/method.tpp"
43
#include "../../core/objectproperty.tpp"
44
#include "../../log/log.hpp"
45
#include "../../log/logmessageexception.hpp"
46
#include "../../utils/displayname.hpp"
47
#include "../../utils/inrange.hpp"
48
#include "../../utils/makearray.hpp"
49
#include "../../world/world.hpp"
50

51
constexpr auto decoderListColumns = DecoderListColumn::Id | DecoderListColumn::Name | DecoderListColumn::Address;
52
constexpr auto inputListColumns = InputListColumn::Address;
53
constexpr auto outputListColumns = OutputListColumn::Channel | OutputListColumn::Address;
54
constexpr auto identificationListColumns = IdentificationListColumn::Id | IdentificationListColumn::Name | IdentificationListColumn::Interface | IdentificationListColumn::Address;
55

56
CREATE_IMPL(LocoNetInterface)
11✔
57

58
LocoNetInterface::LocoNetInterface(World& world, std::string_view _id)
11✔
59
  : Interface(world, _id)
60
  , DecoderController(*this, decoderListColumns)
61
  , InputController(static_cast<IdObject&>(*this))
62
  , OutputController(static_cast<IdObject&>(*this))
63
  , IdentificationController(static_cast<IdObject&>(*this))
64
  , type{this, "type", LocoNetInterfaceType::Serial, PropertyFlags::ReadWrite | PropertyFlags::Store,
22✔
65
      [this](LocoNetInterfaceType /*value*/)
11✔
66
      {
UNCOV
67
        typeChanged();
×
68
      }}
×
69
  , device{this, "device", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
22✔
70
  , baudrate{this, "baudrate", 19200, PropertyFlags::ReadWrite | PropertyFlags::Store}
11✔
71
  , flowControl{this, "flow_control", SerialFlowControl::None, PropertyFlags::ReadWrite | PropertyFlags::Store}
11✔
72
  , hostname{this, "hostname", "", PropertyFlags::ReadWrite | PropertyFlags::Store}
22✔
73
  , port{this, "port", 5550, PropertyFlags::ReadWrite | PropertyFlags::Store}
11✔
74
  , loconet{this, "loconet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
22✔
75
{
76
  name = "LocoNet";
11✔
77
  loconet.setValueInternal(std::make_shared<LocoNet::Settings>(*this, loconet.name()));
11✔
78

79
  Attributes::addDisplayName(type, DisplayName::Interface::type);
11✔
80
  Attributes::addEnabled(type, !online);
11✔
81
  Attributes::addValues(type, locoNetInterfaceTypeValues);
11✔
82
  m_interfaceItems.insertBefore(type, notes);
11✔
83

84
  Attributes::addEnabled(device, !online);
11✔
85
  Attributes::addVisible(device, false);
11✔
86
  m_interfaceItems.insertBefore(device, notes);
11✔
87

88
  Attributes::addDisplayName(baudrate, DisplayName::Serial::baudrate);
11✔
89
  Attributes::addEnabled(baudrate, !online);
11✔
90
  Attributes::addVisible(baudrate, false);
11✔
91
  m_interfaceItems.insertBefore(baudrate, notes);
11✔
92

93
  Attributes::addDisplayName(flowControl, DisplayName::Serial::flowControl);
11✔
94
  Attributes::addEnabled(flowControl, !online);
11✔
95
  Attributes::addValues(flowControl, SerialFlowControlValues);
11✔
96
  Attributes::addVisible(flowControl, false);
11✔
97
  m_interfaceItems.insertBefore(flowControl, notes);
11✔
98

99
  Attributes::addDisplayName(hostname, DisplayName::IP::hostname);
11✔
100
  Attributes::addEnabled(hostname, !online);
11✔
101
  Attributes::addVisible(hostname, false);
11✔
102
  m_interfaceItems.insertBefore(hostname, notes);
11✔
103

104
  Attributes::addDisplayName(port, DisplayName::IP::port);
11✔
105
  Attributes::addEnabled(port, !online);
11✔
106
  Attributes::addVisible(port, false);
11✔
107
  m_interfaceItems.insertBefore(port, notes);
11✔
108

109
  Attributes::addDisplayName(loconet, DisplayName::Hardware::loconet);
11✔
110
  m_interfaceItems.insertBefore(loconet, notes);
11✔
111

112
  m_interfaceItems.insertBefore(decoders, notes);
11✔
113

114
  m_interfaceItems.insertBefore(inputs, notes);
11✔
115

116
  m_interfaceItems.insertBefore(outputs, notes);
11✔
117

118
  m_interfaceItems.insertBefore(identifications, notes);
11✔
119

120
  typeChanged();
11✔
121
}
11✔
122

123
LocoNetInterface::~LocoNetInterface() = default;
11✔
124

UNCOV
125
bool LocoNetInterface::send(std::span<uint8_t> packet)
×
126
{
UNCOV
127
  if(m_kernel)
×
128
    return m_kernel->send(packet);
×
129
  return false;
×
130
}
131

UNCOV
132
bool LocoNetInterface::immPacket(std::span<uint8_t> dccPacket, uint8_t repeat)
×
133
{
UNCOV
134
  if(m_kernel)
×
135
    return m_kernel->immPacket(dccPacket, repeat);
×
136
  return false;
×
137
}
138

UNCOV
139
void LocoNetInterface::readLNCV(uint16_t moduleId, uint16_t address, uint16_t lncv, std::function<void(uint16_t, std::error_code)> callback)
×
140
{
UNCOV
141
  if(m_kernel)
×
142
  {
UNCOV
143
    m_kernel->readLNCV(moduleId, address, lncv, std::move(callback));
×
144
  }
UNCOV
145
}
×
146

147
std::span<const DecoderProtocol> LocoNetInterface::decoderProtocols() const
10✔
148
{
149
  static constexpr std::array<DecoderProtocol, 2> protocols{DecoderProtocol::DCCShort, DecoderProtocol::DCCLong};
150
  return std::span<const DecoderProtocol>{protocols.data(), protocols.size()};
20✔
151
}
152

153
std::pair<uint16_t, uint16_t> LocoNetInterface::decoderAddressMinMax(DecoderProtocol protocol) const
5✔
154
{
155
  if(protocol == DecoderProtocol::DCCLong)
5✔
156
  {
UNCOV
157
    return {DCC::addressLongStart, DCC::addressLongMax};
×
158
  }
159
  return DecoderController::decoderAddressMinMax(protocol);
5✔
160
}
161

162
void LocoNetInterface::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber)
4✔
163
{
164
  if(m_kernel)
4✔
UNCOV
165
    m_kernel->decoderChanged(decoder, changes, functionNumber);
×
166
}
4✔
167

168
std::span<const InputChannel> LocoNetInterface::inputChannels() const
11✔
169
{
170
  static const auto values = makeArray(InputChannel::Input);
171
  return values;
11✔
172
}
173

UNCOV
174
std::pair<uint32_t, uint32_t> LocoNetInterface::inputAddressMinMax(InputChannel /*channel*/) const
×
175
{
UNCOV
176
  return {LocoNet::Kernel::inputAddressMin, LocoNet::Kernel::inputAddressMax};
×
177
}
178

UNCOV
179
void LocoNetInterface::inputSimulateChange(InputChannel channel, uint32_t address, SimulateInputAction action)
×
180
{
UNCOV
181
  if(m_kernel && inRange(address, inputAddressMinMax(channel)))
×
182
    m_kernel->simulateInputChange(address, action);
×
183
}
×
184

185
std::span<const OutputChannel> LocoNetInterface::outputChannels() const
40✔
186
{
187
  static const auto values = makeArray(OutputChannel::Accessory, OutputChannel::DCCext);
188
  return values;
40✔
189
}
190

191
std::pair<uint32_t, uint32_t> LocoNetInterface::outputAddressMinMax(OutputChannel channel) const
8✔
192
{
193
  if(channel == OutputChannel::Accessory)
8✔
194
  {
195
    return {LocoNet::Kernel::accessoryOutputAddressMin, LocoNet::Kernel::accessoryOutputAddressMax};
8✔
196
  }
UNCOV
197
  return OutputController::outputAddressMinMax(channel);
×
198
}
199

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

209
std::pair<uint32_t, uint32_t> LocoNetInterface::identificationAddressMinMax(uint32_t /*channel*/) const
6✔
210
{
211
  return {LocoNet::Kernel::identificationAddressMin, LocoNet::Kernel::identificationAddressMax};
6✔
212
}
213

214
void LocoNetInterface::identificationEvent(uint32_t channel, uint32_t address, IdentificationEventType eventType, uint16_t identifier, Direction direction, uint8_t category)
×
215
{
216
  // OPC_MULTI_SENSE direction:
217
  if(direction == Direction::Unknown && (eventType == IdentificationEventType::Present || eventType == IdentificationEventType::Absent))
×
218
  {
219
    constexpr uint32_t addressDirectionMask = 0x800;
×
220

221
    if(auto it = m_identifications.find({channel, address}); it != m_identifications.end() )
×
222
    {
223
      switch(it->second->opcMultiSenseDirection.value())
×
224
      {
225
        case OPCMultiSenseDirection::None:
×
226
          break;
×
227

228
        case OPCMultiSenseDirection::InSensorAddress:
×
229
          direction = Direction::Reverse;
×
230
          break;
×
231

232
        case OPCMultiSenseDirection::InTransponderAddress:
×
233
        {
234
          constexpr uint16_t identifierDirectionMask = 0x1000;
×
235
          direction = (identifier & identifierDirectionMask) ? Direction::Forward : Direction::Reverse;
×
236
          identifier &= ~identifierDirectionMask;
×
237
          break;
×
238
        }
239
      }
240
    }
241
    else if(address & addressDirectionMask)
×
242
    {
243
      address &= ~addressDirectionMask;
×
244

245
      if(it = m_identifications.find({channel, address});
×
246
          it != m_identifications.end() &&
×
247
          it->second->opcMultiSenseDirection == OPCMultiSenseDirection::InSensorAddress)
×
248
      {
249
        direction = Direction::Forward;
×
250
      }
251
    }
252
  }
253

254
  IdentificationController::identificationEvent(channel, address, eventType, identifier, direction, category);
×
255
}
×
256

257
bool LocoNetInterface::startLNCVProgramming(uint16_t moduleId, uint16_t moduleAddress)
×
258
{
259
  if(!m_kernel)
×
260
    return false;
×
261

262
  m_kernel->lncvStart(moduleId, moduleAddress);
×
263
  return true;
×
264
}
265

266
bool LocoNetInterface::readLNCV(uint16_t lncv)
×
267
{
268
  if(!m_kernel)
×
269
    return false;
×
270

271
  m_kernel->lncvRead(lncv);
×
272
  return true;
×
273
}
274

275
bool LocoNetInterface::writeLNCV(uint16_t lncv, uint16_t value)
×
276
{
277
  if(!m_kernel)
×
278
    return false;
×
279

280
  m_kernel->lncvWrite(lncv, value);
×
281
  return true;
×
282
}
283

284
bool LocoNetInterface::stopLNCVProgramming()
×
285
{
286
  if(!m_kernel)
×
287
    return false;
×
288

289
  m_kernel->lncvStop();
×
290
  return true;
×
291
}
292

293
bool LocoNetInterface::setOnline(bool& value, bool simulation)
×
294
{
295
  if(!m_kernel && value)
×
296
  {
297
    try
298
    {
299
      if(simulation)
×
300
      {
301
        m_kernel = LocoNet::Kernel::create<LocoNet::SimulationIOHandler>(id.value(), loconet->config());
×
302
      }
303
      else
304
      {
305
        switch(type)
×
306
        {
307
          case LocoNetInterfaceType::Serial:
×
308
            m_kernel = LocoNet::Kernel::create<LocoNet::SerialIOHandler>(id.value(), loconet->config(), device.value(), baudrate.value(), flowControl.value());
×
309
            break;
×
310

311
          case LocoNetInterfaceType::TCPBinary:
×
312
            m_kernel = LocoNet::Kernel::create<LocoNet::TCPBinaryIOHandler>(id.value(), loconet->config(), hostname.value(), port.value());
×
313
            break;
×
314

315
          case LocoNetInterfaceType::LBServer:
×
316
            m_kernel = LocoNet::Kernel::create<LocoNet::LBServerIOHandler>(id.value(), loconet->config(), hostname.value(), port.value());
×
317
            break;
×
318

319
          case LocoNetInterfaceType::Z21:
×
320
            m_kernel = LocoNet::Kernel::create<LocoNet::Z21IOHandler>(id.value(), loconet->config(), hostname.value());
×
321
            break;
×
322

323
          default:
×
324
            assert(false);
×
325
            return false;
326
        }
327
      }
328

329
      setState(InterfaceState::Initializing);
×
330

331
      m_kernel->setOnStarted(
×
332
        [this]()
×
333
        {
334
          setState(InterfaceState::Online);
×
335
          const auto worldState = m_world.state.value();
×
336
          m_kernel->setState(contains(worldState, WorldState::PowerOn), contains(worldState, WorldState::Run));
×
337
        });
×
338
      m_kernel->setOnError(
×
339
        [this]()
×
340
        {
341
          setState(InterfaceState::Error);
×
342
          online = false; // communication no longer possible
×
343
        });
×
344
      m_kernel->setOnStateChanged(
×
345
        [this](bool powerOn, bool run)
×
346
        {
347
          if(run && !contains(m_world.state.value(), WorldState::Run))
×
348
          {
349
            m_world.run();
×
350
          }
351
          else if(powerOn && !contains(m_world.state.value(), WorldState::PowerOn))
×
352
          {
353
            m_world.powerOn();
×
354
          }
355
          else if(!powerOn && contains(m_world.state.value(), WorldState::PowerOn))
×
356
          {
357
            m_world.powerOff();
×
358
          }
359
          else if(!run && contains(m_world.state.value(), WorldState::Run))
×
360
          {
361
            m_world.stop();
×
362
          }
363
        });
×
364
      m_kernel->setClock(m_world.clock.value());
×
365
      m_kernel->setDecoderController(this);
×
366
      m_kernel->setInputController(this);
×
367
      m_kernel->setOutputController(this);
×
368
      m_kernel->setIdentificationController(this);
×
369

370
      m_kernel->setOnLNCVReadResponse(
×
371
        [this](bool success, uint16_t lncv, uint16_t lncvValue)
×
372
        {
373
          if(auto* programmer = lncvProgrammer())
×
374
            programmer->readResponse(success, lncv, lncvValue);
×
375
        });
×
376

377
      m_kernel->start();
×
378

379
      m_loconetPropertyChanged = loconet->propertyChanged.connect(
×
380
        [this](BaseProperty& /*property*/)
×
381
        {
382
          m_kernel->setConfig(loconet->config());
×
383
        });
×
384

385
      Attributes::setEnabled(type, false);
×
386
      Attributes::setEnabled(device, false);
×
387
      Attributes::setEnabled(baudrate, false);
×
388
      Attributes::setEnabled(flowControl, false);
×
389
      Attributes::setEnabled(hostname, false);
×
390
      Attributes::setEnabled(port, false);
×
391
    }
392
    catch(const LogMessageException& e)
×
393
    {
394
      setState(InterfaceState::Offline);
×
395
      Log::log(*this, e.message(), e.args());
×
396
      return false;
×
397
    }
×
398
  }
399
  else if(m_kernel && !value)
×
400
  {
401
    Attributes::setEnabled(type, true);
×
402
    Attributes::setEnabled(device, true);
×
403
    Attributes::setEnabled(baudrate, true);
×
404
    Attributes::setEnabled(flowControl, true);
×
405
    Attributes::setEnabled(hostname, true);
×
406
    Attributes::setEnabled(port, true);
×
407

408
    m_loconetPropertyChanged.disconnect();
×
409

410
    m_kernel->stop();
×
411
    EventLoop::deleteLater(m_kernel.release());
×
412

413
    if(status->state != InterfaceState::Error)
×
414
      setState(InterfaceState::Offline);
×
415
  }
416
  return true;
×
417
}
418

419
void LocoNetInterface::addToWorld()
11✔
420
{
421
  Interface::addToWorld();
11✔
422
  DecoderController::addToWorld();
11✔
423
  InputController::addToWorld(inputListColumns);
11✔
424
  OutputController::addToWorld(outputListColumns);
11✔
425
  IdentificationController::addToWorld(identificationListColumns);
11✔
426
  LNCVProgrammingController::addToWorld();
11✔
427
  m_world.loconetInterfaces->add(Object::shared_ptr<LocoNetInterface>());
11✔
428
}
11✔
429

430
void LocoNetInterface::loaded()
×
431
{
432
  Interface::loaded();
×
433

434
  typeChanged();
×
435
}
×
436

437
void LocoNetInterface::destroying()
11✔
438
{
439
  m_world.loconetInterfaces->remove(Object::shared_ptr<LocoNetInterface>());
11✔
440
  LNCVProgrammingController::destroying();
11✔
441
  IdentificationController::destroying();
11✔
442
  OutputController::destroying();
11✔
443
  InputController::destroying();
11✔
444
  DecoderController::destroying();
11✔
445
  Interface::destroying();
11✔
446
}
11✔
447

448
void LocoNetInterface::worldEvent(WorldState state, WorldEvent event)
×
449
{
450
  Interface::worldEvent(state, event);
×
451

452
  if(m_kernel)
×
453
  {
454
    switch(event)
×
455
    {
456
      case WorldEvent::PowerOff:
×
457
      case WorldEvent::PowerOn:
458
      case WorldEvent::Stop:
459
      case WorldEvent::Run:
460
        m_kernel->setState(contains(state, WorldState::PowerOn), contains(state, WorldState::Run));
×
461
        break;
×
462

463
      default:
×
464
        break;
×
465
    }
466
  }
467
}
×
468

469
void LocoNetInterface::typeChanged()
11✔
470
{
471
  const bool serialVisible = isSerial(type);
11✔
472
  Attributes::setVisible(device, serialVisible);
11✔
473
  Attributes::setVisible(baudrate, serialVisible);
11✔
474
  Attributes::setVisible(flowControl, serialVisible);
11✔
475

476
  const bool networkVisible = isNetwork(type);
11✔
477
  Attributes::setVisible(hostname, networkVisible);
11✔
478
  Attributes::setVisible(port, networkVisible && type != LocoNetInterfaceType::Z21);
11✔
479
}
11✔
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