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