]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/centralstation.h
Strip Id tags and copyright notices from files
[r2c2.git] / source / libr2c2 / centralstation.h
1 #ifndef LIBR2C2_CENTRALSTATION_H_
2 #define LIBR2C2_CENTRALSTATION_H_
3
4 #include <msp/net/streamsocket.h>
5 #include <msp/time/timestamp.h>
6 #include "driver.h"
7
8 namespace R2C2 {
9
10 class CentralStation: public Driver
11 {
12 private:
13         struct Tag
14         {
15                 std::string type;
16                 unsigned code;
17                 std::string value;
18
19                 Tag();
20
21                 operator bool() const;
22         };
23
24         struct Message
25         {
26                 typedef std::multimap<std::string, std::string> AttribMap;
27                 typedef std::map<unsigned, AttribMap> ObjectMap;
28         
29                 Tag header;
30                 ObjectMap content;
31                 Tag footer;
32
33                 operator bool() const;
34         };
35
36         enum Error
37         {
38                 OK,
39                 NERROR_UNKNOWNCOMMAND = 10,
40                 NERROR_UNKNOWNARG = 11,
41                 NERROR_UNKNOWNPARAM = 12,
42                 NERROR_UNKNOWNID = 15,
43                 NERROR_SIZE = 20,
44                 NERROR_NOTALLOWED = 22,
45                 NERROR_NOCONTROL = 25,
46                 NERROR_NOCREATE = 28,
47                 NERROR_NOARG = 29
48         };
49
50         enum Protocol
51         {
52                 MM,
53                 MM_27,
54                 MFX
55         };
56
57         struct Locomotive
58         {
59                 std::string name;
60                 Protocol protocol;
61                 unsigned address;
62                 unsigned speed;
63                 bool reverse;
64                 unsigned func_mask;
65                 unsigned funcs;
66                 bool control;
67
68                 Locomotive();
69         };
70
71         struct Turnout
72         {
73                 enum Symbol
74                 {
75                         LEFT,
76                         RIGHT,
77                         THREEWAY,
78                         DOUBLESLIP
79                 };
80
81                 unsigned address;
82                 unsigned symbol;
83                 unsigned state;
84                 unsigned bits;
85                 bool synced;
86
87                 Turnout();
88         };
89
90         struct Sensor
91         {
92                 bool state;
93
94                 Sensor();
95         };
96
97         typedef std::map<unsigned, unsigned> AddressMap;
98         typedef std::map<unsigned, Locomotive> LocoMap;
99         typedef std::map<unsigned, Turnout> TurnoutMap;
100         typedef std::map<unsigned, Sensor> SensorMap;
101
102         Msp::Net::StreamSocket socket;
103         std::list<std::string> cmd_queue;
104         unsigned pending_commands;
105         bool power;
106         bool halted;
107         std::string in_buffer;
108         LocoMap locos;
109         AddressMap loco_addr;
110         bool locos_synced;
111         TurnoutMap turnouts;
112         AddressMap turnout_addr;
113         bool turnouts_synced;
114         SensorMap sensors;
115         std::vector<unsigned> s88;
116         bool sensors_synced;
117
118 public:
119         CentralStation(const std::string &);
120         ~CentralStation();
121
122         virtual void set_power(bool);
123         virtual bool get_power() const { return power; }
124         virtual void halt(bool);
125         virtual bool is_halted() const { return halted; }
126
127         virtual const char *enumerate_protocols(unsigned) const;
128         virtual unsigned get_protocol_speed_steps(const std::string &) const;
129
130         virtual void add_loco(unsigned, const std::string &, const VehicleType &);
131         virtual void set_loco_speed(unsigned, unsigned);
132         virtual void set_loco_reverse(unsigned, bool);
133         virtual void set_loco_function(unsigned, unsigned, bool);
134
135         virtual void add_turnout(unsigned, const TrackType &);
136         virtual void set_turnout(unsigned, unsigned);
137         virtual unsigned get_turnout(unsigned) const;
138
139         virtual void add_sensor(unsigned);
140         virtual void set_sensor(unsigned, bool) { }
141         virtual bool get_sensor(unsigned) const;
142
143         virtual void tick();
144         virtual void flush();
145
146 private:
147         void command(const std::string &, bool = false);
148         Message receive();
149         void process_reply(const Message &);
150         void process_event(const Message &);
151         void process_object(unsigned, const Message::AttribMap &);
152         Protocol map_protocol(const std::string &) const;
153         template<typename T>
154         unsigned map_address(const std::map<unsigned, T> &, const AddressMap &, unsigned) const;
155         void skip(std::string::iterator &, const std::string::iterator &, const std::string &) const;
156         std::string parse_token(std::string::iterator &, const std::string::iterator &, const std::string &) const;
157         Tag parse_tag(std::string::iterator &, const std::string::iterator &) const;
158         Message parse_message(std::string::iterator &, const std::string::iterator &) const;
159 };
160
161 } // namespace R2C2
162
163 #endif