chiark / gitweb /
realtime: do not retransmit NMRA idle packets for nonexistent adjucts - instead,...
[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
112 #define CIXF_U                 0x0000ffffu
113 #define CIXF_FORCE             0x00010000u
114 #define CIXF_ANYSTA            0x00020000u
115
116 /*---------- from/for startup.c ----------*/
117
118 #include "stastate.h"
119
120 void sta_startup(void);
121 void sta_finalising_done(void);
122 void serial_moredata(PicInsn *buf);
123
124 extern StartupState sta_state;
125 extern const char *const stastatelist[];
126 void cmdi_output_bufferempty(OutBufferChain *obc);
127
128 void resolve_begin(void); /* from resolve.c */
129 int resolve_complete(void);
130 void resolve_motioncheck(void);
131
132 void waggle_settle(void); /* from movpos.c */
133 void waggle_startup_manual(void);
134
135 /*---------- from/for record.c and persist.c ----------*/
136
137 void records_parse(const char **argv);
138 void persist_entrails_interpret(void);
139 void persist_entrails_run_converter(void);
140 void persist_install(void);
141
142 extern const char *persist_fn;
143 extern char *persist_record_converted;
144
145 void persist_map_veryearly(void);
146
147 /*---------- from/for realtime.c ----------*/
148
149 void oupicio(const char *dirn, const PicInsnInfo *pii, int obj, int v,
150              void (*qprintf)(const char *fmt, ...));
151 void ouhex(const char *word, const Byte *command, int length);
152 void ouhex_nosim(const char *word, const Byte *command, int length);
153
154 void serial_transmit(const PicInsn *pi);
155 void command_doline(ParseState *ps, CommandInput *cmdi_arg);
156 const CmdInfo *current_cmd;
157
158 /*---------- for/from simulate.c ----------*/
159
160 void serial_indata_process(int buf_used);
161
162 void sim_initialise(const char *logduplicate);
163 void sim_run(void);
164
165 void simlog_ccb(char *m, size_t l, void *u);
166 void simlogv(const char *fmt, va_list al);
167 void simlog(const char *fmt, ...);
168 void simlog_serial(const Byte *data, int length);
169 void simlog_flush(void);
170 void simlog_open(const char *fn);
171
172 void mgettimeofday(struct timeval *tv); /* contains magic for simulation */
173 void *toev_callback(oop_source *source, struct timeval tv, void *t_v);
174
175 void sim_toev_start(TimeoutEvent *toev);
176 void sim_toev_stop(TimeoutEvent *toev);
177 void sim_mgettimeofday(struct timeval *tv);
178
179 extern int simlog_full;
180 extern const char *simulate;
181
182 extern PicInsn serial_buf;
183
184 /*---------- from actual.c ----------*/
185
186 int picinsn_polarity_testbit(const PicInsn *pi, const SegmentInfo *segi);
187   /* this belongs in {au,skel}proto-pic.[ch] really but it's
188    * more convenient here. */
189
190 void adjuncts_start_xmit(void);
191 void adjuncts_updated(AdjunctsAddr *a);
192
193 /*---------- from movpos.c ----------*/
194
195 void points_turning_on(void);
196 void motions_all_abandon(void);
197
198 /*---------- from eventhelp.c ----------*/
199
200 extern const char toev_fast_pclass[];
201
202 typedef void TimeoutEventFn(TimeoutEvent*);
203 struct TimeoutEvent {         /* Undefined   Idle      Running     set by   */
204   int running;                /*  any         0         1           toev_   */
205   int duration; /*ms*/        /*  any         any[1]    any[1]      caller  */
206   TimeoutEventFn *callback;   /*  any         any       valid[2]    caller  */
207   struct timeval abs;         /*  any         any       valid       toev_   */
208   const char *pclass, *pinst; /*  any         any       valid       caller  */
209 };  /* [1] duration must be >=0 or -1 when toev_start is called;
210      * [2] callback may be modified while timeout is running;
211      *      value used is that prevailing when timeout happens
212      * when the timeout happens, TimeoutEvent's state goes from R to I
213      * and then callback member is read and the function called
214      */
215
216 void toev_init(TimeoutEvent*);    /* U -> I */
217 void toev_start(TimeoutEvent*);   /* IR -> R; reads duration */
218   /* if duration is -1 then is same as toev_stop */
219 void toev_stop(TimeoutEvent*);    /* IR -> I */
220
221 /*---------- from rtprio.c ----------*/
222
223 void realtime_priority(void);
224
225 #define RTFEAT_DEFAULTS  0100u /* turns on MLOCK and SCHEDPRIO iff not sim */
226 #define RTFEAT_MEM       0001u /* mlock */
227 #define RTFEAT_CPU       0002u /* hard CPU scheduling priority */
228
229 #define RTFEAT_ALL_SHIFT 16
230 #define RTFEAT_ALL(x) (RTFEAT_##x << RTFEAT_ALL_SHIFT)
231   /* RTFEAT_ALL(FOO) is relevant only if RTFEAT_FOO selected, and means
232    *  not to apply the normal limit to the grabbing of FOO */
233
234 extern unsigned rtfeats_use;
235
236 void ouvprintf(const char *fmt, va_list al)
237      __attribute__((format(printf,1,0)));
238 void ouprintf(const char *fmt, ...)
239      __attribute__((format(printf,1,2)));
240
241 void ouvprintf_only(const char *fmt, va_list al)
242      __attribute__((format(printf,1,0)));
243 void ouprintf_only(const char *fmt, ...)
244      __attribute__((format(printf,1,2)));
245
246 extern unsigned long eventcounter; /* for debugging use only ! */
247 void debug_count_event(const char *what);
248
249 #define DEBUGP(a,k) (dflags_##a & DBIT_##a##_##k)
250
251 #define DPRINTFA ouprintf_only
252
253 #define COND_DPRINTF(cond, a,k, fmt, ...) \
254  ((cond) ? DPRINTFA("debug " #a "/" #k " : " fmt,##__VA_ARGS__) : (void)0)
255
256 #define DPRINTF(a,k,f,...) COND_DPRINTF(DEBUGP(a,k),a,k,f,##__VA_ARGS__)
257 #define DPRINTF1(a,k,f,...) COND_DPRINTF((DP)=DEBUGP(a,k), a,k,f,##__VA_ARGS__)
258 #define DPRINTF2(f,...) ((DP) ? DPRINTFA(f,##__VA_ARGS__) : (void)0)
259
260 /*---------- tbi ----------*/
261
262 void choreographers_all_abandon(void);
263
264 #include "record.h"
265
266 #define PERSIST_CONVERT_OPTION "--persist-convert-entrails"
267
268 #include "safety.h"
269
270
271 #endif /*REALTIME_H*/