| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include "DataModel/AccessoryConfig.h" |
| 24 |
#include "DataModel/FeedbackConfig.h" |
| 25 |
#include "DataModel/LocoConfig.h" |
| 26 |
#include "Hardware/Capabilities.h" |
| 27 |
#include "Hardware/AccessoryCache.h" |
| 28 |
#include "Hardware/FeedbackCache.h" |
| 29 |
#include "Hardware/HardwareInterface.h" |
| 30 |
#include "Hardware/HardwareParams.h" |
| 31 |
#include "Hardware/LocoCache.h" |
| 32 |
#include "Network/TcpClient.h" |
| 33 |
|
| 34 |
namespace Hardware |
| 35 |
{ |
| 36 |
namespace Protocols |
| 37 |
{ |
| 38 |
class EsuCAN: protected HardwareInterface |
| 39 |
{ |
| 40 |
public: |
| 41 |
EsuCAN() = delete; |
| 42 |
EsuCAN(const EsuCAN&) = delete; |
| 43 |
EsuCAN& operator=(const EsuCAN&) = delete; |
| 44 |
|
| 45 |
EsuCAN(const HardwareParams* params, const std::string& controlName); |
| 46 |
|
| 47 |
virtual ~EsuCAN(); |
| 48 |
|
| 49 |
inline Hardware::Capabilities GetCapabilities() const override |
| 50 |
{ |
| 51 |
return Hardware::CapabilityLoco |
| 52 |
| Hardware::CapabilityAccessory |
| 53 |
| Hardware::CapabilityFeedback |
| 54 |
| Hardware::CapabilityLocoDatabase |
| 55 |
| Hardware::CapabilityAccessoryDatabase |
| 56 |
| Hardware::CapabilityFeedbackDatabase |
| 57 |
; |
| 58 |
} |
| 59 |
|
| 60 |
void GetLocoProtocols(std::vector<Protocol>& protocols) const override |
| 61 |
{ |
| 62 |
protocols.push_back(ProtocolServer); |
| 63 |
} |
| 64 |
|
| 65 |
bool LocoProtocolSupported(Protocol protocol) const override |
| 66 |
{ |
| 67 |
return (protocol == ProtocolServer); |
| 68 |
} |
| 69 |
|
| 70 |
void GetAccessoryProtocols(std::vector<Protocol>& protocols) const override |
| 71 |
{ |
| 72 |
protocols.push_back(ProtocolServer); |
| 73 |
} |
| 74 |
|
| 75 |
bool AccessoryProtocolSupported(Protocol protocol) const override |
| 76 |
{ |
| 77 |
return (protocol == ProtocolServer); |
| 78 |
} |
| 79 |
|
| 80 |
static void GetArgumentTypesAndHint(std::map<unsigned char, ArgumentType>& argumentTypes, |
| 81 |
std::string& hint) |
| 82 |
{ |
| 83 |
argumentTypes[1] = ArgumentTypeIpAddress; |
| 84 |
hint = Languages::GetText(Languages::TextHintEcos); |
| 85 |
} |
| 86 |
|
| 87 |
void Booster(const BoosterState status) override |
| 88 |
{ |
| 89 |
Send(status == BoosterStateGo ? "set(1,go)\n" : "set(1,stop)\n"); |
| 90 |
} |
| 91 |
|
| 92 |
void LocoSpeed(const Protocol protocol, const Address address, const Speed speed) override; |
| 93 |
void LocoOrientation(const Protocol protocol, const Address address, const Orientation orientation) |
| 94 |
override; |
| 95 |
|
| 96 |
void LocoFunction(const Protocol protocol, |
| 97 |
const Address address, |
| 98 |
const DataModel::LocoFunctionNr function, |
| 99 |
const DataModel::LocoFunctionState on) override; |
| 100 |
|
| 101 |
void Accessory(const Protocol protocol, |
| 102 |
const Address address, |
| 103 |
const DataModel::AccessoryState state, |
| 104 |
const bool on, |
| 105 |
const DataModel::AccessoryPulseDuration duration) override; |
| 106 |
|
| 107 |
inline virtual const std::map<std::string, Hardware::LocoCacheEntry>& GetLocoDatabase() const override |
| 108 |
{ |
| 109 |
return locoCache.GetAll(); |
| 110 |
} |
| 111 |
|
| 112 |
inline virtual DataModel::LocoConfig GetLocoByMatchKey(const std::string& matchKey) const override |
| 113 |
{ |
| 114 |
return DataModel::LocoConfig(locoCache.Get(matchKey)); |
| 115 |
} |
| 116 |
|
| 117 |
inline virtual void SetLocoIdOfMatchKey(const LocoID locoId, const std::string& matchKey) override |
| 118 |
{ |
| 119 |
locoCache.SetLocoId(locoId, matchKey); |
| 120 |
} |
| 121 |
|
| 122 |
inline virtual const std::map<std::string, Hardware::AccessoryCacheEntry>& GetAccessoryDatabase() const override |
| 123 |
{ |
| 124 |
return accessoryCache.GetAll(); |
| 125 |
} |
| 126 |
|
| 127 |
inline virtual DataModel::AccessoryConfig GetAccessoryByMatchKey(const std::string& matchKey) const override |
| 128 |
{ |
| 129 |
return DataModel::AccessoryConfig(accessoryCache.Get(matchKey)); |
| 130 |
} |
| 131 |
|
| 132 |
inline virtual void SetAccessoryIdentifierOfMatchKey(const DataModel::ObjectIdentifier objectIdentifier, const std::string& matchKey) override |
| 133 |
{ |
| 134 |
accessoryCache.SetObjectIdentifier(objectIdentifier, matchKey); |
| 135 |
} |
| 136 |
|
| 137 |
inline virtual const std::map<std::string, Hardware::FeedbackCacheEntry>& GetFeedbackDatabase() const override |
| 138 |
{ |
| 139 |
return feedbackCache.GetAll(); |
| 140 |
} |
| 141 |
|
| 142 |
inline virtual DataModel::FeedbackConfig GetFeedbackByMatchKey(const std::string& matchKey) const override |
| 143 |
{ |
| 144 |
return DataModel::FeedbackConfig(feedbackCache.Get(matchKey)); |
| 145 |
} |
| 146 |
|
| 147 |
inline virtual void SetFeedbackIdOfMatchKey(const FeedbackID feedbackId, const std::string& matchKey) override |
| 148 |
{ |
| 149 |
feedbackCache.SetFeedbackId(feedbackId, matchKey); |
| 150 |
} |
| 151 |
|
| 152 |
static const char* const CommandActivateBoosterUpdates; |
| 153 |
static const char* const CommandQueryLocos; |
| 154 |
static const char* const CommandQueryAccessories; |
| 155 |
static const char* const CommandQueryFeedbacks; |
| 156 |
|
| 157 |
private: |
| 158 |
static const unsigned short MaxMessageSize = 1024; |
| 159 |
static const unsigned short EsuCANPort = 15471; |
| 160 |
|
| 161 |
void Send(const char* data); |
| 162 |
void Receiver(); |
| 163 |
void ReadLine(); |
| 164 |
void Parser(); |
| 165 |
void ParseReply(); |
| 166 |
void ParseQueryLocos(); |
| 167 |
void ParseQueryAccessories(); |
| 168 |
void ParseQueryFeedbacks(); |
| 169 |
void ParseLocoData(); |
| 170 |
void ParseAccessoryData(); |
| 171 |
void ParseFeedbackData(); |
| 172 |
|
| 173 |
void ParseEvent(); |
| 174 |
void ParseEventLine(); |
| 175 |
void ParseBoosterEvent(); |
| 176 |
void ParseLocoEvent(int loco); |
| 177 |
void ParseAccessoryEvent(int accessory); |
| 178 |
void ParseFeedbackEvent(int feedback); |
| 179 |
void CheckFeedbackDiff(unsigned int module, uint8_t data); |
| 180 |
|
| 181 |
void ParseOption(std::string& option, std::string& value); |
| 182 |
void ParseOptionInt(std::string& option, int& value); |
| 183 |
void ParseOptionHex(std::string& option, int& value); |
| 184 |
void ParseEndLine(); |
| 185 |
std::string ReadUntilChar(const char c); |
| 186 |
std::string ReadUntilLineEnd(); |
| 187 |
|
| 188 |
inline void SendActivateBoosterUpdates() |
| 189 |
{ |
| 190 |
Send(CommandActivateBoosterUpdates); |
| 191 |
} |
| 192 |
|
| 193 |
inline void SendQueryLocos() |
| 194 |
{ |
| 195 |
Send(CommandQueryLocos); |
| 196 |
} |
| 197 |
|
| 198 |
inline void SendQueryAccessories() |
| 199 |
{ |
| 200 |
Send(CommandQueryAccessories); |
| 201 |
} |
| 202 |
|
| 203 |
inline void SendQueryFeedbacks() |
| 204 |
{ |
| 205 |
Send(CommandQueryFeedbacks); |
| 206 |
} |
| 207 |
|
| 208 |
inline void SendActivateUpdates(const int id) |
| 209 |
{ |
| 210 |
std::string command = "request(" + std::to_string(id) + ",view)\n"; |
| 211 |
Send(command.c_str()); |
| 212 |
} |
| 213 |
|
| 214 |
inline void SendGetHandle(const int id) |
| 215 |
{ |
| 216 |
std::string command = "request(" + std::to_string(id) + ",control,force)\n"; |
| 217 |
Send(command.c_str()); |
| 218 |
} |
| 219 |
|
| 220 |
inline char GetChar(const size_t offset = 0) const |
| 221 |
{ |
| 222 |
size_t position = readBufferPosition + offset; |
| 223 |
if (position >= MaxMessageSize) |
| 224 |
{ |
| 225 |
return 0; |
| 226 |
} |
| 227 |
return readBuffer[position]; |
| 228 |
} |
| 229 |
|
| 230 |
inline char ReadAndConsumeChar() |
| 231 |
{ |
| 232 |
if (readBufferPosition >= MaxMessageSize) |
| 233 |
{ |
| 234 |
return 0; |
| 235 |
} |
| 236 |
return readBuffer[readBufferPosition++]; |
| 237 |
} |
| 238 |
|
| 239 |
inline bool CheckAndConsumeChar(const char charToCheck) |
| 240 |
{ |
| 241 |
if (readBufferPosition >= MaxMessageSize) |
| 242 |
{ |
| 243 |
return false; |
| 244 |
} |
| 245 |
return charToCheck == readBuffer[readBufferPosition++]; |
| 246 |
} |
| 247 |
|
| 248 |
inline bool CheckChar(const char charToCheck) |
| 249 |
{ |
| 250 |
if (readBufferPosition >= MaxMessageSize) |
| 251 |
{ |
| 252 |
return false; |
| 253 |
} |
| 254 |
return charToCheck == readBuffer[readBufferPosition]; |
| 255 |
} |
| 256 |
|
| 257 |
bool SkipOptionalChar(const char charToSkip); |
| 258 |
|
| 259 |
inline void SkipWhiteSpace() |
| 260 |
{ |
| 261 |
while (SkipOptionalChar(' ')); |
| 262 |
} |
| 263 |
|
| 264 |
bool Compare(const char* reference, const size_t size) const; |
| 265 |
bool CompareAndConsume(const char* reference, const size_t size); |
| 266 |
bool IsNumber() const; |
| 267 |
int ParseInt(); |
| 268 |
|
| 269 |
inline bool CheckGraterThenAtLineEnd() |
| 270 |
{ |
| 271 |
SkipWhiteSpace(); |
| 272 |
return CompareAndConsume(">\n", 2); |
| 273 |
} |
| 274 |
|
| 275 |
volatile bool run; |
| 276 |
std::thread receiverThread; |
| 277 |
|
| 278 |
Network::TcpConnection tcp; |
| 279 |
|
| 280 |
unsigned char readBuffer[MaxMessageSize]; |
| 281 |
ssize_t readBufferLength; |
| 282 |
size_t readBufferPosition; |
| 283 |
|
| 284 |
static const unsigned int MaxFeedbackModules = 128; |
| 285 |
uint8_t feedbackMemory[MaxFeedbackModules]; |
| 286 |
|
| 287 |
LocoCache locoCache; |
| 288 |
AccessoryCache accessoryCache; |
| 289 |
FeedbackCache feedbackCache; |
| 290 |
|
| 291 |
static const int MinLocoId = 1000; |
| 292 |
static const int OffsetLocoAddress = 999; |
| 293 |
static const int MinAccessoryId = 20000; |
| 294 |
static const int OffsetAccessoryAddress = 19999; |
| 295 |
static const int MinFeedbackModuleId = 100; |
| 296 |
static const int OffsetFeedbackModuleAddress = 99; |
| 297 |
}; |
| 298 |
} |
| 299 |
} |