Debian Codegraph @

Hardware/Protocols/MaerklinCAN.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include "DataModel/AccessoryBase.h"
24 #include "DataModel/LocoFunctions.h"
25 #include "Hardware/Capabilities.h"
26 #include "Hardware/HardwareInterface.h"
27 #include "Hardware/HardwareParams.h"
28 #include "Hardware/LocoCache.h"
29 #include "Hardware/Protocols/MaerklinCANCommon.h"
30 #include "Logger/Logger.h"
31 #include "Utils/Integer.h"
32
33 // CAN protocol specification at http://streaming.maerklin.de/public-media/cs2/cs2CAN-Protokoll-2_0.pdf
34 // Very interesting is also http://www.mbernstein.de/modellbahn/can/bem.htm
35
36 namespace Hardware { namespace Protocols
37 {
38 class MaerklinCAN: protected HardwareInterface, protected MaerklinCANCommon
39 {
40 public:
41 MaerklinCAN() = delete;
42 MaerklinCAN(const MaerklinCAN&) = delete;
43 MaerklinCAN& operator=(const MaerklinCAN&) = delete;
44
45 inline Hardware::Capabilities GetCapabilities() const override
46 {
47 return Hardware::CapabilityLoco
48 | Hardware::CapabilityAccessory
49 | Hardware::CapabilityFeedback
50 | Hardware::CapabilityProgram
51 | Hardware::CapabilityProgramMmWrite
52 | Hardware::CapabilityProgramMfxRead
53 | Hardware::CapabilityProgramMfxWrite
54 | Hardware::CapabilityProgramDccDirectRead
55 | Hardware::CapabilityProgramDccDirectWrite
56 | Hardware::CapabilityProgramDccPomLocoWrite
57 | Hardware::CapabilityProgramDccPomAccessoryWrite
58 | Hardware::CapabilityLocoDatabase;
59 }
60
61 void GetLocoProtocols(std::vector<Protocol>& protocols) const override
62 {
63 protocols.push_back(ProtocolMM);
64 protocols.push_back(ProtocolMFX);
65 protocols.push_back(ProtocolDCC);
66 }
67
68 inline bool LocoProtocolSupported(Protocol protocol) const override
69 {
70 return ((protocol == ProtocolMM)
71 || (protocol == ProtocolMFX)
72 || (protocol == ProtocolDCC));
73 }
74
75 inline void GetAccessoryProtocols(std::vector<Protocol>& protocols) const override
76 {
77 protocols.push_back(ProtocolMM);
78 protocols.push_back(ProtocolDCC);
79 }
80
81 inline bool AccessoryProtocolSupported(Protocol protocol) const override
82 {
83 return ((protocol == ProtocolMM)
84 || (protocol == ProtocolDCC));
85 }
86
87 void Booster(const BoosterState status) override
88 {
89 MaerklinCANCommon::Booster(status);
90 }
91
92 void LocoSpeed(const Protocol protocol,
93 const Address address,
94 const Speed speed) override
95 {
96 MaerklinCANCommon::LocoSpeed(protocol, address, speed);
97 }
98 void LocoOrientation(const Protocol protocol,
99 const Address address,
100 const Orientation orientation) override
101 {
102 MaerklinCANCommon::LocoOrientation(protocol, address, orientation);
103 }
104
105 void LocoFunction(const Protocol protocol,
106 const Address address,
107 const DataModel::LocoFunctionNr function,
108 const DataModel::LocoFunctionState on) override
109 {
110 MaerklinCANCommon::LocoFunction(protocol, address, function, on);
111 }
112
113 void Accessory(const Protocol protocol,
114 const Address address,
115 const DataModel::AccessoryState state,
116 const bool on,
117 __attribute__((unused)) const DataModel::AccessoryPulseDuration duration) override
118 {
119 MaerklinCANCommon::Accessory(protocol, address, state, on);
120 }
121
122 void ProgramRead(const ProgramMode mode,
123 const Address address,
124 const CvNumber cv) override
125 {
126 MaerklinCANCommon::ProgramRead(mode, address, cv);
127 }
128
129 void ProgramWrite(const ProgramMode mode,
130 const Address address,
131 const CvNumber cv,
132 const CvValue value) override
133 {
134 MaerklinCANCommon::ProgramWrite(mode, address, cv, value);
135 }
136
137 const std::map<std::string, Hardware::LocoCacheEntry>& GetLocoDatabase() const override
138 {
139 return locoCache.GetAll();
140 }
141
142 DataModel::LocoConfig GetLocoByMatchKey(const std::string& matchKey) const override
143 {
144 return DataModel::LocoConfig(locoCache.Get(matchKey));
145 }
146
147 DataModel::LocoConfig GetMultipleUnitByMatchKey(const std::string& matchKey) const override
148 {
149 return DataModel::LocoConfig(locoCache.Get(matchKey));
150 }
151
152 void SetLocoIdOfMatchKey(const LocoID locoId, const std::string& matchKey) override
153 {
154 locoCache.SetLocoId(locoId, matchKey);
155 }
156
157 void SetMultipleUnitIdOfMatchKey(const LocoID locoId, const std::string& matchKey) override
158 {
159 locoCache.SetLocoId(locoId, matchKey);
160 }
161
162 protected:
163 inline MaerklinCAN(const HardwareParams* params,
164 const std::string& fullName,
165 const std::string& shortName)
166 : HardwareInterface(params->GetManager(),
167 params->GetControlID(),
168 fullName,
169 shortName),
170 MaerklinCANCommon("affeaffe",
171 params->GetControlID(),
172 params->GetManager(),
173 Utils::Utils::StringToBool(params->GetArg2()),
174 HardwareInterface::logger),
175 locoCache(params->GetControlID(), params->GetManager())
176 {
177 }
178
179 virtual ~MaerklinCAN()
180 {
181 }
182
183 protected:
184 virtual void CacheSave(LocoCacheEntry& entry) override
185 {
186 locoCache.Save(entry);
187 }
188
189 virtual void CacheSave(LocoCacheEntry& entry, const std::string& oldMatchKey) override
190 {
191 locoCache.Save(entry, oldMatchKey);
192 }
193
194 virtual LocoID CacheDelete(const std::string& matchKey) override
195 {
196 return locoCache.Delete(matchKey);
197 }
198
199 virtual void CacheUpdateSlaves() override
200 {
201 locoCache.UpdateSlaves();
202 }
203
204 private:
205 LocoCache locoCache;
206 };
207 }} // namespace