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