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