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