chiark / gitweb /
protocol improvements wip
[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
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 /*---------- from/for startup.c ----------*/
118
119 #include "stastate.h"
120
121 void sta_startup(void);
122 void sta_finalising_done(void);
123 void serial_moredata(PicInsn *buf);
124
125 extern StartupState sta_state;
126 extern const char *const stastatelist[];
127
128 void resolve_begin(void); /* from resolve.c */
129 int resolve_complete(void);
130 void resolve_motioncheck(void);
131
132 /*---------- from/for record.c and persist.c ----------*/
133
134 void records_parse(const char **argv);
135 void persist_entrails_interpret(void);
136 void persist_entrails_run_converter(void);
137 void persist_install(void);
138
139 extern const char *persist_fn;
140 extern char *persist_record_converted;
141
142 void persist_map_veryearly(void);
143
144 /*---------- from/for realtime.c ----------*/
145
146 void oupicio(const char *dirn, const PicInsnInfo *pii, int objnum);
147 void ouhex(const char *word, const Byte *command, int length);
148
149 void serial_transmit(const PicInsn *pi);
150
151 /*---------- from actual.c ----------*/
152
153 int picinsn_polarity_testbit(const PicInsn *pi, const SegmentInfo *segi);
154   /* this belongs in {au,skel}proto-pic.[ch] really but it's
155    * more convenient here. */
156
157 /*---------- from movpos.c ----------*/
158
159 void points_turning_on(void);
160 void points_all_abandon(void);
161
162 /*---------- tbi ----------*/
163
164 void choreographers_all_abandon(void);
165
166 #define DUPO(ctx) UPO, "debug " ctx " : "
167
168 #include "record.h"
169
170 #define PERSIST_CONVERT_OPTION "--persist-convert-entrails"
171
172 #include "safety.h"
173
174 #define CTYPE(isfoobar,ch) (isfoobar((unsigned char)(ch)))
175
176 #endif /*REALTIME_H*/