chiark / gitweb /
db981f5284bd30951ae9926f0121324995617584
[trains.git] / hostside / hostside.h
1 /*
2  * declarations for hostside controller daemon
3  */
4
5 #ifndef HOSTSIDE_H
6 #define HOSTSIDE_H
7
8 #include "common.h"
9
10 #include <oop.h>
11 #include <oop-read.h>
12
13 typedef struct Client Client;
14 typedef struct ParseState ParseState;
15 typedef struct CmdInfo CmdInfo;
16 typedef struct RetransmitNode RetransmitNode;
17
18 /*---------- from obc.c ----------*/
19
20 typedef struct OutBuffer OutBuffer;
21 typedef struct OutBufferChain OutBufferChain;
22 typedef void OutBufferError(OutBufferChain*, const char *e1, const char *e2);
23
24 struct OutBufferChain {
25   /* set by user: */
26   char *desc;
27   int fd;
28   OutBufferError *error;
29   /* set/used by obc_..., oprintf, etc., only */
30   int done_of_head;
31   struct { OutBuffer *head, *tail; } obs;
32 };
33
34 void obc_init(OutBufferChain *ch);
35
36 void ovprintf(OutBufferChain *ch, const char *fmt, va_list al)
37      __attribute__((format(printf,2,0)));
38 void oprintf(OutBufferChain *ch, const char *msg, ...)
39      __attribute__((format(printf,2,3)));
40 void owrite(OutBufferChain *ch, const char *data, int l);
41
42 /*---------- from output.c ----------*/
43
44 typedef unsigned long Selector;
45 #include "selectors.h"
46
47 void oovprintf(Selector sel, const char *fmt, va_list al)
48      __attribute__((format(printf,2,0)));
49 void ooprintf(Selector sel, const char *fmt, ...)
50      __attribute__((format(printf,2,3)));
51 void oowrite(Selector sel, const char *data, int l);
52
53 void serial_transmit(const PicInsn *pi);
54
55 void output_hex(Selector sel, const char *work,
56                 const Byte *command, int length);
57
58 /*---------- from protocol.c ----------*/
59
60 typedef struct OrdinaryPicMessage OrdinaryPicMessage;
61 struct OrdinaryPicMessage {
62   const char *title;
63   unsigned opcode;
64   int operand_bits; /* 0: no operand.  >=7, insn is a 2-byte message */
65 };
66      
67 /*---------- from hostside.c ----------*/
68
69 extern oop_source *events;
70
71 /*---------- from client.c ----------*/
72
73 struct ClientList { Client *head, *tail; };
74 extern struct ClientList clients;
75
76 struct Client {
77   OutBufferChain ch;
78   Client *back, *next;
79   Selector sel;
80   oop_read *rd;
81 };
82
83 struct ParseState {
84   Client *cl;
85   const char *remain;
86   const char *thisword;
87   int lthisword;
88 };
89
90 struct CmdInfo {
91   const char *name;
92   void (*fn)(ParseState *ps, const CmdInfo *ci);
93   int xarg;
94 };
95
96 int lstrstrcmp(const char *a, int la, const char *b);
97 int thiswordstrcmp(ParseState *ps, const char *b);
98
99 int ps_word(ParseState *ps);
100 int ps_needword(ParseState *ps);
101 int ps_needhextoend(ParseState *ps, Byte *dbuf, int *len_io);
102 void ps_callword(ParseState *ps, const CmdInfo *infs, const char *what);
103 int ps_neednumber(ParseState *ps, long *r, long min, long max, const char *wh);
104 int ps_neednoargs(ParseState *ps);
105
106 void vbadcmd(ParseState *ps, const char *fmt, va_list al)
107      __attribute__((format(printf,2,0)));
108 void badcmd(ParseState *ps, const char *fmt, ...)
109      __attribute__((format(printf,2,3)));
110
111 void stdin_client(void);
112
113 extern const CmdInfo toplevel_cmds[]; /* defined in commands.c*/
114
115 /*---------- from retransmit.c ----------*/
116
117 struct RetransmitNode {
118   /* set by caller: */
119   PicInsn pi;
120   /* internal: */
121 };
122
123 void retransmit_queue(RetransmitNode *rn);
124 void retransmit_cancel(RetransmitNode *rn);
125
126 /*---------- macro for table lookups, with help from client.c ----------*/
127
128 #define some_lookup(ps, infos)                  \
129   ((const typeof(infos[0])*)                    \
130    any_lookup((ps),(infos),sizeof((infos)[0])))
131
132 #define some_needword_lookup(ps, infos, what)                   \
133   ((const typeof(infos[0])*)                                    \
134    any_needword_lookup((ps),(infos),sizeof((infos)[0]),(what)))
135
136 const void *any_lookup(ParseState *ps, const void *infos, size_t infosz);
137 const void *any_needword_lookup(ParseState *ps, const void *infos,
138                                 size_t sz, const char *what);
139
140 #endif /*HOSTSIDE_H*/