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

traintastic / traintastic / 20435179075

22 Dec 2025 02:24PM UTC coverage: 27.527% (-0.02%) from 27.55%
20435179075

push

github

reinder
[loconet] improved handling of power commands

0 of 22 new or added lines in 2 files covered. (0.0%)

147 existing lines in 4 files now uncovered.

7843 of 28492 relevant lines covered (27.53%)

192.37 hits per line

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

35.08
/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/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
      {
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

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

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

139
std::span<const DecoderProtocol> LocoNetInterface::decoderProtocols() const
10✔
140
{
141
  static constexpr std::array<DecoderProtocol, 2> protocols{DecoderProtocol::DCCShort, DecoderProtocol::DCCLong};
142
  return std::span<const DecoderProtocol>{protocols.data(), protocols.size()};
20✔
143
}
144

145
std::pair<uint16_t, uint16_t> LocoNetInterface::decoderAddressMinMax(DecoderProtocol protocol) const
5✔
146
{
147
  if(protocol == DecoderProtocol::DCCLong)
5✔
148
  {
149
    return {DCC::addressLongStart, DCC::addressLongMax};
×
150
  }
151
  return DecoderController::decoderAddressMinMax(protocol);
5✔
152
}
153

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

160
std::span<const InputChannel> LocoNetInterface::inputChannels() const
11✔
161
{
162
  static const auto values = makeArray(InputChannel::Input);
163
  return values;
11✔
164
}
165

166
std::pair<uint32_t, uint32_t> LocoNetInterface::inputAddressMinMax(InputChannel /*channel*/) const
×
167
{
168
  return {LocoNet::Kernel::inputAddressMin, LocoNet::Kernel::inputAddressMax};
×
169
}
170

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

177
std::span<const OutputChannel> LocoNetInterface::outputChannels() const
40✔
178
{
179
  static const auto values = makeArray(OutputChannel::Accessory, OutputChannel::DCCext);
180
  return values;
40✔
181
}
182

183
std::pair<uint32_t, uint32_t> LocoNetInterface::outputAddressMinMax(OutputChannel channel) const
8✔
184
{
185
  if(channel == OutputChannel::Accessory)
8✔
186
  {
187
    return {LocoNet::Kernel::accessoryOutputAddressMin, LocoNet::Kernel::accessoryOutputAddressMax};
8✔
188
  }
189
  return OutputController::outputAddressMinMax(channel);
×
190
}
191

192
bool LocoNetInterface::setOutputValue(OutputChannel channel, uint32_t address, OutputValue value)
×
193
{
194
  return
195
      m_kernel &&
×
196
      inRange(address, outputAddressMinMax(channel)) &&
×
197
      m_kernel->setOutput(channel, static_cast<uint16_t>(address), value);
×
198
}
199

200
std::pair<uint32_t, uint32_t> LocoNetInterface::identificationAddressMinMax(uint32_t /*channel*/) const
6✔
201
{
202
  return {LocoNet::Kernel::identificationAddressMin, LocoNet::Kernel::identificationAddressMax};
6✔
203
}
204

205
void LocoNetInterface::identificationEvent(uint32_t channel, uint32_t address, IdentificationEventType eventType, uint16_t identifier, Direction direction, uint8_t category)
×
206
{
207
  // OPC_MULTI_SENSE direction:
208
  if(direction == Direction::Unknown && (eventType == IdentificationEventType::Present || eventType == IdentificationEventType::Absent))
×
209
  {
210
    constexpr uint32_t addressDirectionMask = 0x800;
×
211

212
    if(auto it = m_identifications.find({channel, address}); it != m_identifications.end() )
×
213
    {
214
      switch(it->second->opcMultiSenseDirection.value())
×
215
      {
216
        case OPCMultiSenseDirection::None:
×
217
          break;
×
218

219
        case OPCMultiSenseDirection::InSensorAddress:
×
220
          direction = Direction::Reverse;
×
221
          break;
×
222

223
        case OPCMultiSenseDirection::InTransponderAddress:
×
224
        {
225
          constexpr uint16_t identifierDirectionMask = 0x1000;
×
226
          direction = (identifier & identifierDirectionMask) ? Direction::Forward : Direction::Reverse;
×
227
          identifier &= ~identifierDirectionMask;
×
228
          break;
×
229
        }
230
      }
231
    }
232
    else if(address & addressDirectionMask)
×
233
    {
234
      address &= ~addressDirectionMask;
×
235

236
      if(it = m_identifications.find({channel, address});
×
237
          it != m_identifications.end() &&
×
238
          it->second->opcMultiSenseDirection == OPCMultiSenseDirection::InSensorAddress)
×
239
      {
240
        direction = Direction::Forward;
×
241
      }
242
    }
243
  }
244

245
  IdentificationController::identificationEvent(channel, address, eventType, identifier, direction, category);
×
246
}
×
247

248
bool LocoNetInterface::startLNCVProgramming(uint16_t moduleId, uint16_t moduleAddress)
×
249
{
250
  if(!m_kernel)
×
251
    return false;
×
252

253
  m_kernel->lncvStart(moduleId, moduleAddress);
×
254
  return true;
×
255
}
256

257
bool LocoNetInterface::readLNCV(uint16_t lncv)
×
258
{
259
  if(!m_kernel)
×
260
    return false;
×
261

262
  m_kernel->lncvRead(lncv);
×
263
  return true;
×
264
}
265

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

271
  m_kernel->lncvWrite(lncv, value);
×
272
  return true;
×
273
}
274

275
bool LocoNetInterface::stopLNCVProgramming()
×
276
{
277
  if(!m_kernel)
×
278
    return false;
×
279

280
  m_kernel->lncvStop();
×
281
  return true;
×
282
}
283

284
bool LocoNetInterface::setOnline(bool& value, bool simulation)
×
285
{
286
  if(!m_kernel && value)
×
287
  {
288
    try
289
    {
290
      if(simulation)
×
291
      {
292
        m_kernel = LocoNet::Kernel::create<LocoNet::SimulationIOHandler>(id.value(), loconet->config());
×
293
      }
294
      else
295
      {
296
        switch(type)
×
297
        {
298
          case LocoNetInterfaceType::Serial:
×
299
            m_kernel = LocoNet::Kernel::create<LocoNet::SerialIOHandler>(id.value(), loconet->config(), device.value(), baudrate.value(), flowControl.value());
×
300
            break;
×
301

302
          case LocoNetInterfaceType::TCPBinary:
×
303
            m_kernel = LocoNet::Kernel::create<LocoNet::TCPBinaryIOHandler>(id.value(), loconet->config(), hostname.value(), port.value());
×
304
            break;
×
305

306
          case LocoNetInterfaceType::LBServer:
×
307
            m_kernel = LocoNet::Kernel::create<LocoNet::LBServerIOHandler>(id.value(), loconet->config(), hostname.value(), port.value());
×
308
            break;
×
309

310
          case LocoNetInterfaceType::Z21:
×
311
            m_kernel = LocoNet::Kernel::create<LocoNet::Z21IOHandler>(id.value(), loconet->config(), hostname.value());
×
312
            break;
×
313

314
          default:
×
315
            assert(false);
×
316
            return false;
317
        }
318
      }
319

320
      setState(InterfaceState::Initializing);
×
321

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

361
      m_kernel->setOnLNCVReadResponse(
×
362
        [this](bool success, uint16_t lncv, uint16_t lncvValue)
×
363
        {
364
          if(auto* programmer = lncvProgrammer())
×
365
            programmer->readResponse(success, lncv, lncvValue);
×
366
        });
×
367

368
      m_kernel->start();
×
369

370
      m_loconetPropertyChanged = loconet->propertyChanged.connect(
×
371
        [this](BaseProperty& /*property*/)
×
372
        {
373
          m_kernel->setConfig(loconet->config());
×
374
        });
×
375

376
      Attributes::setEnabled(type, false);
×
377
      Attributes::setEnabled(device, false);
×
378
      Attributes::setEnabled(baudrate, false);
×
379
      Attributes::setEnabled(flowControl, false);
×
380
      Attributes::setEnabled(hostname, false);
×
381
      Attributes::setEnabled(port, false);
×
382
    }
383
    catch(const LogMessageException& e)
×
384
    {
385
      setState(InterfaceState::Offline);
×
386
      Log::log(*this, e.message(), e.args());
×
387
      return false;
×
388
    }
×
389
  }
390
  else if(m_kernel && !value)
×
391
  {
392
    Attributes::setEnabled(type, true);
×
393
    Attributes::setEnabled(device, true);
×
394
    Attributes::setEnabled(baudrate, true);
×
395
    Attributes::setEnabled(flowControl, true);
×
396
    Attributes::setEnabled(hostname, true);
×
397
    Attributes::setEnabled(port, true);
×
398

399
    m_loconetPropertyChanged.disconnect();
×
400

401
    m_kernel->stop();
×
402
    EventLoop::deleteLater(m_kernel.release());
×
403

404
    if(status->state != InterfaceState::Error)
×
405
      setState(InterfaceState::Offline);
×
406
  }
407
  return true;
×
408
}
409

410
void LocoNetInterface::addToWorld()
11✔
411
{
412
  Interface::addToWorld();
11✔
413
  DecoderController::addToWorld();
11✔
414
  InputController::addToWorld(inputListColumns);
11✔
415
  OutputController::addToWorld(outputListColumns);
11✔
416
  IdentificationController::addToWorld(identificationListColumns);
11✔
417
  LNCVProgrammingController::addToWorld();
11✔
418
}
11✔
419

420
void LocoNetInterface::loaded()
×
421
{
422
  Interface::loaded();
×
423

424
  typeChanged();
×
425
}
×
426

427
void LocoNetInterface::destroying()
11✔
428
{
429
  LNCVProgrammingController::destroying();
11✔
430
  IdentificationController::destroying();
11✔
431
  OutputController::destroying();
11✔
432
  InputController::destroying();
11✔
433
  DecoderController::destroying();
11✔
434
  Interface::destroying();
11✔
435
}
11✔
436

437
void LocoNetInterface::worldEvent(WorldState state, WorldEvent event)
×
438
{
439
  Interface::worldEvent(state, event);
×
440

441
  if(m_kernel)
×
442
  {
443
    switch(event)
×
444
    {
445
      case WorldEvent::PowerOff:
×
446
      case WorldEvent::PowerOn:
447
      case WorldEvent::Stop:
448
      case WorldEvent::Run:
449
        m_kernel->setState(contains(state, WorldState::PowerOn), contains(state, WorldState::Run));
×
450
        break;
×
451

452
      default:
×
453
        break;
×
454
    }
455
  }
456
}
×
457

458
void LocoNetInterface::typeChanged()
11✔
459
{
460
  const bool serialVisible = isSerial(type);
11✔
461
  Attributes::setVisible(device, serialVisible);
11✔
462
  Attributes::setVisible(baudrate, serialVisible);
11✔
463
  Attributes::setVisible(flowControl, serialVisible);
11✔
464

465
  const bool networkVisible = isNetwork(type);
11✔
466
  Attributes::setVisible(hostname, networkVisible);
11✔
467
  Attributes::setVisible(port, networkVisible && type != LocoNetInterfaceType::Z21);
11✔
468
}
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