chiark / gitweb /
prevent execution of speed and movfeat commands in < Sta_Run
[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 <limits.h>
18 #include <stddef.h>
19 #include <ctype.h>
20 #include <math.h>
21
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <sys/stat.h>
25 #include <sys/mman.h>
26 #include <sys/wait.h>
27
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <dirent.h>
31
32 #include "../layout/layout-data.h"
33
34 typedef struct Segment Segment;
35 typedef struct Train Train;
36 typedef struct TimeoutEvent TimeoutEvent;
37
38 /*---------- from retransmit.c ----------*/
39
40 typedef struct RetransmitRelaxedNode RetransmitRelaxedNode;
41 typedef union RetransmitUrgentNode RetransmitUrgentNode;
42 typedef unsigned Retransmit__Time;
43
44   /* Contents of the retransmission nodes is generally all for use by
45    * retransmit.c only; as a special exception, caller may edit pi
46    * directly.  Normally, though, pi is set by supplying an NMRA
47    * command to one of the _queue functions; iff the Nmra* is
48    * non-null, _queue will add an NMRA checksum and update pi.
49    */
50 struct RetransmitRelaxedNode {
51   PicInsn pi;
52   DLIST_NODE(RetransmitRelaxedNode) rr;
53 };
54 union RetransmitUrgentNode {
55   PicInsn pi;
56   struct {
57     RetransmitRelaxedNode relaxed;
58     int ix;
59     Retransmit__Time when;
60     DLIST_NODE(RetransmitUrgentNode) queue;
61   } u;
62 };
63
64 void retransmit_start(void);
65 void retransmit_something(void);
66
67 void retransmit_relaxed_queue(RetransmitRelaxedNode *rn, Nmra *n);
68 void retransmit_relaxed_requeue(RetransmitRelaxedNode *rn, Nmra *n);
69 void retransmit_relaxed_cancel(RetransmitRelaxedNode *rn);
70
71 void retransmit_urgent_queue(RetransmitUrgentNode *rn, Nmra *n);
72 void retransmit_urgent_queue_relaxed(RetransmitUrgentNode *urg, Nmra *n);
73 void retransmit_urgent_requeue(RetransmitUrgentNode *rn, Nmra *n);
74 void retransmit_urgent_cancel(RetransmitUrgentNode *rn);
75
76   /* ... NB: these are NOT idempotent.  Use _requeue it's queued;
77    * _requeue is just _cancel followed by queue. */
78
79 /*---------- features, filled in by record, used by features.c ----------*/
80
81 #define FEATURESADDR_TRANSMITS 4
82   /* 0..2 are func0to4 func5to8 func9to12 and speed cmd
83    * pi.l is 0 if not transmitting */
84
85 typedef struct FeaturesAddr {
86   struct FeaturesAddr *next;
87   int addr, cbitmap;
88   RetransmitRelaxedNode rn[FEATURESADDR_TRANSMITS];
89 } FeaturesAddr;
90
91 typedef struct {
92   FeaturesAddr *a;
93   int bitval; /* may have no or several bits set */
94   int speedstep; /* -ve means backwards; 0 means not to use motor for feat */
95 } FeaturesFeature;
96
97 typedef struct FeaturesTarget {
98   struct FeaturesTarget *next;
99   char *pname;
100   char *featchs; /* null-terminated */
101   FeaturesFeature **feats; /* same order as featchs */
102 } FeaturesTarget;
103
104 extern int n_trains;
105 extern Train *trains;
106 extern Segment *segments;
107
108 extern FeaturesTarget *feattargs;
109 extern FeaturesAddr *feataddrs;
110
111 /*---------- global variables, in realtime.c ----------*/
112
113 extern CommandInput cmdi;
114 extern int picio_send_noise;
115
116 #define UPO (&(cmdi.out))
117
118 #define CIXF_ANYSTA   1u
119 #define CIXF_FORCE    2u
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 /*---------- 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 objnum);
151 void ouhexi(const char *word, const Byte *command, int length);
152 void ouhexo(const char *word, const Byte *command, int length);
153
154 void serial_transmit(const PicInsn *pi);
155
156 /*---------- for/from simulate.c ----------*/
157
158 const char *simulate;
159 void serial_indata_process(int buf_used);
160
161 void sim_initialise(const char *logduplicate);
162 void sim_run(void);
163
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 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 points_all_abandon(void);
189
190 /*---------- from eventhelp.c ----------*/
191
192 typedef void TimeoutEventFn(TimeoutEvent*);
193 struct TimeoutEvent {         /* Undefined   Idle      Running     set by   */
194   int running;                /*  any         0         1           toev_   */
195   int duration; /*ms*/        /*  any         any[1]    any[1]      caller  */
196   TimeoutEventFn *callback;   /*  any         any       valid[2]    caller  */
197   struct timeval abs;         /*  any         any       valid       toev_   */
198   const char *pclass, *pinst; /*  any         any       valid       caller  */
199 };  /* [1] duration must be >=0 or -1 when toev_start is called;
200      * [2] callback may be modified while timeout is running;
201      *      value used is that prevailing when timeout happens
202      * when the timeout happens, TimeoutEvent's state goes from R to I
203      * and then callback member is read and the function called
204      */
205
206 void toev_init(TimeoutEvent*);    /* U -> I */
207 void toev_start(TimeoutEvent*);   /* IR -> R; reads duration */
208   /* if duration is -1 then is same as toev_stop */
209 void toev_stop(TimeoutEvent*);    /* IR -> I */
210
211 /*---------- tbi ----------*/
212
213 void choreographers_all_abandon(void);
214
215 #define DUPO(ctx) UPO, "debug " ctx " : "
216
217 #include "record.h"
218
219 #define PERSIST_CONVERT_OPTION "--persist-convert-entrails"
220
221 #include "safety.h"
222
223 #define CTYPE(isfoobar,ch) (isfoobar((unsigned char)(ch)))
224
225 #endif /*REALTIME_H*/