chiark / gitweb /
hostside: realtime.h: add comment referring to definitions of Train and Segment
[trains.git] / hostside / realtime.h
1 /*
2  * declarations for realtime daemon
3  */
4
5 #ifndef REALTIME_H
6 #define REALTIME_H
7
8 #include "daemons.h"
9 #include "auproto-pic.h"
10 #include "dliste.h"
11
12 #include <sys/time.h>
13 #include <sys/stat.h>
14 #include <sys/mman.h>
15
16 #include <fcntl.h>
17 #include <dirent.h>
18
19 #include <sys/types.h>
20 #include <sys/time.h>
21
22 #include "../.git-revid-autoversion.h"
23 #include "../layout/layout-data.h"
24 #include "realtime+dflags.h"
25
26 typedef struct Segment Segment; /* from safety.h */
27 typedef struct Train Train; /* from safety.h */
28 typedef struct TimeoutEvent TimeoutEvent;
29
30 /*---------- from retransmit.c ----------*/
31
32 typedef struct RetransmitRelaxedNode RetransmitRelaxedNode;
33 typedef union RetransmitUrgentNode RetransmitUrgentNode;
34 typedef unsigned Retransmit__Time;
35
36   /* Contents of the retransmission nodes is generally all for use by
37    * retransmit.c only; as a special exception, caller may edit pi
38    * directly.  Normally, though, pi is set by supplying an NMRA
39    * command to one of the _queue functions; iff the Nmra* is
40    * non-null, _queue will add an NMRA checksum (modifying *n)
41    * and update pi (which always results in a nonzero pi.l).
42    */
43 struct RetransmitRelaxedNode {
44   PicInsn pi;
45   DLIST_NODE(RetransmitRelaxedNode) rr;
46 };
47 union RetransmitUrgentNode {
48   PicInsn pi;
49   struct {
50     RetransmitRelaxedNode relaxed;
51     int ix;
52     Retransmit__Time when;
53     DLIST_NODE(RetransmitUrgentNode) queue;
54   } u;
55 };
56
57 void retransmit_start(void);
58 void retransmit_something(void);
59
60 void retransmit_urgent_queue(RetransmitUrgentNode *rn, Nmra *n);
61 void retransmit_urgent_queue_relaxed(RetransmitUrgentNode *urg, Nmra *n);
62 void retransmit_urgent_requeue(RetransmitUrgentNode *rn, Nmra *n);
63 void retransmit_urgent_cancel(RetransmitUrgentNode *rn);
64
65 void retransmit_relaxed_queue(RetransmitRelaxedNode *rn, Nmra *n);
66 void retransmit_relaxed_cancel(RetransmitRelaxedNode *rn);
67
68   /* ... NB: these are NOT idempotent and NOT interchangeable.  Use
69    * urgent_requeue if it's queued and has changed and must get a new
70    * quota of urgency; _requeue is just _cancel followed by queue. */
71
72 /*---------- adjuncts, filled in by record, used by adjuncts.c ----------*/
73
74 #define ADJUNCTSADDR_TRANSMITS 4
75   /* 0..2 are func0to4 func5to8 func9to12 and speed cmd
76    * pi.l is 0 if not transmitting */
77
78 #define ADJS_SPEEDSTEP_BIT     0x4000u /* a->{current,all,permit}, f->bits */
79 #define ADJS_SPEEDSTEP_REVERSE 0x8000u /* in a->all */
80
81 typedef struct AdjunctsAddr {
82   struct AdjunctsAddr *next;
83   int addr, speedstep;
84   unsigned current, permit, all;
85   RetransmitRelaxedNode rn[ADJUNCTSADDR_TRANSMITS];
86 } AdjunctsAddr;
87
88 typedef struct {
89   char *pname; /* first, for pname1st_compar */
90   AdjunctsAddr *a;
91   unsigned bits; /* may have no or several bits set */
92 } AdjunctsAdjunct;
93
94 typedef struct AdjunctsTarget {
95   char *pname; /* first, for pname1st_compar */
96   int n_adjs;
97   AdjunctsAdjunct *adjs;
98 } AdjunctsTarget;
99
100 extern int n_trains;
101 extern Train *trains;
102 extern Segment *segments;
103
104 extern int n_adjtargs, n_adjaddrs;
105 extern AdjunctsTarget *adjtargs;
106 extern AdjunctsAddr **adjaddrs;
107
108 /*---------- global variables, in realtime.c ----------*/
109
110 extern CommandInput cmdi;
111 extern int picio_send_noise;
112 extern int disable_watchdog;
113
114 #define CIXF_U                 0x0000ffffu
115 #define CIXF_FORCE             0x00010000u
116 #define CIXF_ANYSTA            0x00020000u
117
118 /*---------- from/for startup.c ----------*/
119
120 #include "stastate.h"
121
122 void sta_startup(void);
123 void sta_finalising_done(void);
124 void serial_moredata(PicInsn *buf);
125
126 extern StartupState sta_state;
127 extern const char *const stastatelist[];
128 void cmdi_output_bufferempty(OutBufferChain *obc);
129
130 void resolve_begin(void); /* from resolve.c */
131 int resolve_complete(void);
132 void resolve_motioncheck(void);
133
134 void waggle_settle(void); /* from movpos.c */
135 void waggle_startup_manual(void);
136
137 /*---------- from/for record.c and persist.c ----------*/
138
139 void records_parse(const char **argv);
140 void persist_entrails_interpret(void);
141 void persist_entrails_run_converter(void);
142 void persist_install(void);
143
144 extern const char *persist_fn;
145 extern char *persist_record_converted;
146
147 void persist_map_veryearly(void);
148
149 /*---------- from/for realtime.c ----------*/
150
151 void oupicio(const char *dirn, const PicInsnInfo *pii, int obj, int v,
152              void (*qprintf)(const char *fmt, ...));
153 void ouhex(const char *word, const Byte *command, int length);
154 void ouhex_nosim(const char *word, const Byte *command, int length);
155
156 void serial_transmit(const PicInsn *pi);
157 void command_doline(ParseState *ps, CommandInput *cmdi_arg);
158 const CmdInfo *current_cmd;
159
160 void check_rusage_baseline(void);
161 void check_rusage_check(int always_report);
162
163 /*---------- for/from simulate.c ----------*/
164
165 void serial_indata_process(int buf_used);
166
167 void sim_initialise(const char *logduplicate);
168 void sim_run(void);
169
170 void simlog_ccb(char *m, size_t l, void *u);
171 void simlogv(const char *fmt, va_list al);
172 void simlog(const char *fmt, ...);
173 void simlog_serial(const Byte *data, int length);
174 void simlog_flush(void);
175 void simlog_open(const char *fn);
176
177 void mgettimeofday(struct timeval *tv); /* contains magic for simulation */
178 void *toev_callback(oop_source *source, struct timeval tv, void *t_v);
179
180 void sim_toev_start(TimeoutEvent *toev);
181 void sim_toev_stop(TimeoutEvent *toev);
182 void sim_mgettimeofday(struct timeval *tv);
183
184 extern int simlog_full;
185 extern const char *simulate;
186
187 extern PicInsn serial_buf;
188
189 /*---------- from actual.c ----------*/
190
191 int picinsn_polarity_testbit(const PicInsn *pi, const SegmentInfo *segi);
192   /* this belongs in {au,skel}proto-pic.[ch] really but it's
193    * more convenient here. */
194
195 void adjuncts_start_xmit(void);
196 void adjuncts_updated(AdjunctsAddr *a);
197
198 /*---------- from movpos.c ----------*/
199
200 void points_turning_on(void);
201 void motions_all_abandon(void);
202
203 /*---------- from eventhelp.c ----------*/
204
205 extern const char toev_fast_pclass[];
206
207 typedef void TimeoutEventFn(TimeoutEvent*);
208 struct TimeoutEvent {         /* Undefined   Idle      Running     set by   */
209   int running;                /*  any         0         1           toev_   */
210   int duration; /*ms*/        /*  any         any[1]    any[1]      caller  */
211   TimeoutEventFn *callback;   /*  any         any       valid[2]    caller  */
212   struct timeval abs;         /*  any         any       valid       toev_   */
213   const char *pclass, *pinst; /*  any         any       valid       caller  */
214 };  /* [1] duration must be >=0 or -1 when toev_start is called;
215      * [2] callback may be modified while timeout is running;
216      *      value used is that prevailing when timeout happens
217      * when the timeout happens, TimeoutEvent's state goes from R to I
218      * and then callback member is read and the function called
219      */
220
221 void toev_init(TimeoutEvent*);    /* U -> I */
222 void toev_start(TimeoutEvent*);   /* IR -> R; reads duration */
223   /* if duration is -1 then is same as toev_stop */
224 void toev_stop(TimeoutEvent*);    /* IR -> I */
225
226 /*---------- from rtprio.c ----------*/
227
228 void realtime_priority(void);
229
230 #define RTFEAT_DEFAULTS  0100u /* turns things on iff not sim */
231 #define RTFEAT_MEM       0001u /* mlock */
232 #define RTFEAT_CPU       0002u /* hard CPU scheduling priority */
233 #define RTFEAT_RUSAGE    0004u /* check up on faults etc. in getrusage */
234
235 #define RTFEAT_ALL_SHIFT 16
236 #define RTFEAT_ALL(x) (RTFEAT_##x << RTFEAT_ALL_SHIFT)
237   /* RTFEAT_ALL(FOO) is relevant only if RTFEAT_FOO selected, and means
238    *  not to apply the normal limit to the grabbing of FOO */
239
240 extern unsigned rtfeats_use;
241
242 void ouvprintf(const char *fmt, va_list al)
243      __attribute__((format(printf,1,0)));
244 void ouprintf(const char *fmt, ...)
245      __attribute__((format(printf,1,2)));
246
247 void ouvprintf_only(const char *fmt, va_list al)
248      __attribute__((format(printf,1,0)));
249 void ouprintf_only(const char *fmt, ...)
250      __attribute__((format(printf,1,2)));
251
252 extern unsigned long eventcounter; /* for debugging use only ! */
253 void debug_count_event(const char *what);
254
255 #define DEBUGP(a,k) (dflags_##a & DBIT_##a##_##k)
256
257 #define DPRINTFA ouprintf_only
258
259 #define COND_DPRINTF(cond, a,k, fmt, ...) \
260  ((cond) ? DPRINTFA("debug " #a "/" #k " : " fmt,##__VA_ARGS__) : (void)0)
261
262 #define DPRINTF(a,k,f,...) COND_DPRINTF(DEBUGP(a,k),a,k,f,##__VA_ARGS__)
263 #define DPRINTF1(a,k,f,...) COND_DPRINTF((DP)=DEBUGP(a,k), a,k,f,##__VA_ARGS__)
264 #define DPRINTF2(f,...) ((DP) ? DPRINTFA(f,##__VA_ARGS__) : (void)0)
265
266 /*---------- tbi ----------*/
267
268 void choreographers_all_abandon(void);
269
270 #include "record.h"
271
272 #define PERSIST_CONVERT_OPTION "--persist-convert-entrails"
273
274 #include "safety.h"
275
276
277 #endif /*REALTIME_H*/