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

traintastic / traintastic / 20638780725

01 Jan 2026 12:44PM UTC coverage: 27.155% (-0.4%) from 27.527%
20638780725

push

github

reinder
bumped copyright year to 2026

7873 of 28993 relevant lines covered (27.15%)

191.86 hits per line

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

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

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

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

57
CREATE_IMPL(LocoNetInterface)
11✔
58

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

201
bool LocoNetInterface::setOutputValue(OutputChannel channel, uint32_t address, OutputValue value)
×
202
{
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