chiark / gitweb /
multiple cache det compression
[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 <stdarg.h>
13 #include <string.h>
14 #include <errno.h>
15 #include <assert.h>
16 #include <stdlib.h>
17 #include <stdint.h>
18 #include <limits.h>
19 #include <stddef.h>
20 #include <ctype.h>
21 #include <math.h>
22
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <dirent.h>
30
31 #include "../layout/layout-data.h"
32
33 typedef struct Segment Segment;
34 typedef struct Train Train;
35 typedef struct TimeoutEvent TimeoutEvent;
36
37 /*---------- from retransmit.c ----------*/
38
39 typedef struct RetransmitRelaxedNode RetransmitRelaxedNode;
40 typedef union RetransmitUrgentNode RetransmitUrgentNode;
41 typedef unsigned Retransmit__Time;
42
43   /* Contents of the retransmission nodes is generally all for use by
44    * retransmit.c only; as a special exception, caller may edit pi
45    * directly.  Normally, though, pi is set by supplying an NMRA
46    * command to one of the _queue functions; iff the Nmra* is
47    * non-null, _queue will add an NMRA checksum and update pi.
48    */
49 struct RetransmitRelaxedNode {
50   PicInsn pi;
51   DLIST_NODE(RetransmitRelaxedNode) rr;
52 };
53 union RetransmitUrgentNode {
54   PicInsn pi;
55   struct {
56     RetransmitRelaxedNode relaxed;
57     int ix;
58     Retransmit__Time when;
59     DLIST_NODE(RetransmitUrgentNode) queue;
60   } u;
61 };
62
63 void retransmit_start(void);
64 void retransmit_something(void);
65
66 void retransmit_relaxed_queue(RetransmitRelaxedNode *rn, Nmra *n);
67 void retransmit_relaxed_requeue(RetransmitRelaxedNode *rn, Nmra *n);
68 void retransmit_relaxed_cancel(RetransmitRelaxedNode *rn);
69
70 void retransmit_urgent_queue(RetransmitUrgentNode *rn, Nmra *n);
71 void retransmit_urgent_queue_relaxed(RetransmitUrgentNode *urg, Nmra *n);
72 void retransmit_urgent_requeue(RetransmitUrgentNode *rn, Nmra *n);
73 void retransmit_urgent_cancel(RetransmitUrgentNode *rn);
74
75   /* ... NB: these are NOT idempotent.  Use _requeue it's queued;
76    * _requeue is just _cancel followed by queue. */
77
78 /*---------- features, filled in by record, used by features.c ----------*/
79
80 #define FEATURESADDR_TRANSMITS 4
81   /* 0..2 are func0to4 func5to8 func9to12 and speed cmd
82    * pi.l is 0 if not transmitting */
83
84 typedef struct FeaturesAddr {
85   struct FeaturesAddr *next;
86   int addr, cbitmap;
87   RetransmitRelaxedNode rn[FEATURESADDR_TRANSMITS];
88 } FeaturesAddr;
89
90 typedef struct {
91   FeaturesAddr *a;
92   int bitval; /* may have no or several bits set */
93   int speedstep; /* -ve means backwards; 0 means not to use motor for feat */
94 } FeaturesFeature;
95
96 typedef struct FeaturesTarget {
97   struct FeaturesTarget *next;
98   char *pname;
99   char *featchs; /* null-terminated */
100   FeaturesFeature **feats; /* same order as featchs */
101 } FeaturesTarget;
102
103 extern int n_trains;
104 extern Train *trains;
105 extern Segment *segments;
106
107 extern FeaturesTarget *feattargs;
108 extern FeaturesAddr *feataddrs;
109
110 /*---------- global variables, in realtime.c ----------*/
111
112 extern CommandInput cmdi;
113 extern int picio_send_noise;
114
115 #define UPO (&(cmdi.out))
116
117 #define CIXF_U                 0x0000ffffu
118 #define CIXF_FORCE             0x00010000u
119 #define CIXF_ANYSTA            0x00020000u
120
121 /*---------- from/for startup.c ----------*/
122
123 #include "stastate.h"
124
125 void sta_startup(void);
126 void sta_finalising_done(void);
127 void serial_moredata(PicInsn *buf);
128
129 extern StartupState sta_state;
130 extern const char *const stastatelist[];
131
132 void resolve_begin(void); /* from resolve.c */
133 int resolve_complete(void);
134 void resolve_motioncheck(void);
135
136 void waggle_settle(void); /* from movpos.c */
137 void waggle_startup_manual(void);
138
139 /*---------- from/for record.c and persist.c ----------*/
140
141 void records_parse(const char **argv);
142 void persist_entrails_interpret(void);
143 void persist_entrails_run_converter(void);
144 void persist_install(void);
145
146 extern const char *persist_fn;
147 extern char *persist_record_converted;
148
149 void persist_map_veryearly(void);
150
151 /*---------- from/for realtime.c ----------*/
152
153 void oupicio(const char *dirn, const PicInsnInfo *pii, int obj, int v);
154 void ouhexi(const char *word, const Byte *command, int length);
155 void ouhexo(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 /*---------- for/from simulate.c ----------*/
162
163 void serial_indata_process(int buf_used);
164
165 void sim_initialise(const char *logduplicate);
166 void sim_run(void);
167
168 void simlogv(const char *fmt, va_list al);
169 void simlog(const char *fmt, ...);
170 void simlog_serial(const Byte *data, int length);
171 void simlog_flush(void);
172 void simlog_open(const char *fn);
173
174 void mgettimeofday(struct timeval *tv); /* contains magic for simulation */
175 void *toev_callback(oop_source *source, struct timeval tv, void *t_v);
176
177 void sim_toev_start(TimeoutEvent *toev);
178 void sim_toev_stop(TimeoutEvent *toev);
179 void sim_mgettimeofday(struct timeval *tv);
180
181 extern int simlog_full;
182 extern const char *simulate;
183
184 extern PicInsn serial_buf;
185
186 /*---------- from actual.c ----------*/
187
188 int picinsn_polarity_testbit(const PicInsn *pi, const SegmentInfo *segi);
189   /* this belongs in {au,skel}proto-pic.[ch] really but it's
190    * more convenient here. */
191
192 /*---------- from movpos.c ----------*/
193
194 void points_turning_on(void);
195 void motions_all_abandon(void);
196
197 /*---------- from eventhelp.c ----------*/
198
199 extern const char toev_fast_pclass[];
200
201 typedef void TimeoutEventFn(TimeoutEvent*);
202 struct TimeoutEvent {         /* Undefined   Idle      Running     set by   */
203   int running;                /*  any         0         1           toev_   */
204   int duration; /*ms*/        /*  any         any[1]    any[1]      caller  */
205   TimeoutEventFn *callback;   /*  any         any       valid[2]    caller  */
206   struct timeval abs;         /*  any         any       valid       toev_   */
207   const char *pclass, *pinst; /*  any         any       valid       caller  */
208 };  /* [1] duration must be >=0 or -1 when toev_start is called;
209      * [2] callback may be modified while timeout is running;
210      *      value used is that prevailing when timeout happens
211      * when the timeout happens, TimeoutEvent's state goes from R to I
212      * and then callback member is read and the function called
213      */
214
215 void toev_init(TimeoutEvent*);    /* U -> I */
216 void toev_start(TimeoutEvent*);   /* IR -> R; reads duration */
217   /* if duration is -1 then is same as toev_stop */
218 void toev_stop(TimeoutEvent*);    /* IR -> I */
219
220 /*---------- from rtprio.c ----------*/
221
222 void realtime_priority(void);
223
224 #define RTFEAT_DEFAULTS  0100u /* turns on MLOCK and SCHEDPRIO iff not sim */
225 #define RTFEAT_MEM       0001u /* mlock */
226 #define RTFEAT_CPU       0002u /* hard CPU scheduling priority */
227
228 #define RTFEAT_ALL_SHIFT 16
229 #define RTFEAT_ALL(x) (RTFEAT_##x << RTFEAT_ALL_SHIFT)
230   /* RTFEAT_ALL(FOO) is relevant only if RTFEAT_FOO selected, and means
231    *  not to apply the normal limit to the grabbing of FOO */
232
233 extern unsigned rtfeats_use;
234
235 /*---------- tbi ----------*/
236
237 void choreographers_all_abandon(void);
238
239 #define DUPO(ctx) UPO, "debug " ctx " : "
240
241 #include "record.h"
242
243 #define PERSIST_CONVERT_OPTION "--persist-convert-entrails"
244
245 #include "safety.h"
246
247 #define CTYPE(isfoobar,ch) (isfoobar((unsigned char)(ch)))
248
249 #endif /*REALTIME_H*/