2 * main program for realtime control
7 const char *progname= "realtime";
9 /*---------- global variables ----------*/
12 int picio_send_noise= 1;
14 static const char *device= "/dev/ttya0";
16 /*---------- general event handling ----------*/
18 static void comms_error(const char *ch, const char *e1,
19 const char *e2 /* may be 0 */) {
20 if (!e1 && !e2) e1= "end of file";
21 die("communications error: %s: %s%s%s", ch, e1, e2?": ":"", e2?e2:"");
24 static void *read_exception(oop_source *evts, int fd,
25 oop_event evt, void *cl_v) {
30 ch= (fd==UPO->fd ? UPO->desc :
31 fd==serial_fd ? "serial port" :
34 r= read(fd, &bufc, 1);
35 if (r==-1) comms_error(ch, "read error", strerror(errno));
36 else if (r==0) comms_error(ch, "reports exception, reads EOF", 0);
37 else comms_error(ch, "reports exception, but readable", 0);
42 /*---------- logging etc. ----------*/
44 static char *transegn2suffixstring(Train *tra, Segment *seg) {
45 /* Either arg may be 0, in which case it's not included.
46 * Result string will be empty, or start with ": "
47 * Result string is from malloc.
50 const char *trapn, *segpn;
54 segpn= seg ? seg->i->pname : 0;
55 trapn= tra ? tra->pname : 0;
57 r= asprintf(&s, "%s%s%s%s%s",
58 segpn||trapn ? ":" : "",
63 if (r<0) diee("vasprintf failed in transegn2suffixstring");
67 void vlogmsg(ErrorCode ec, Train *tra, const SegmentInfo *segi,
68 const char *fmt, va_list al) {
69 oprintf(UPO, "message %s ", ec ? errorcodelist[ec] : "info");
71 if (segi || tra) oprintf(UPO, ":");
72 if (segi) oprintf(UPO, " @%s", segi->pname);
73 if (tra) oprintf(UPO, " %s", tra->pname);
77 void logmsg(ErrorCode ec, Train *tra, const SegmentInfo *segi,
78 const char *fmt,...) {
81 vlogmsg(ec,tra,segi,fmt,al);
85 void safety_vpanic(Train *tra, Segment *seg,const char *fmt,va_list al) {
90 serial_transmit(&piob);
92 if (vasprintf(&msg,fmt,al) < 0)
93 diee("vasprintf failed in safety_vpanic fmt=\"%s\"", fmt);
95 where= transegn2suffixstring(tra,seg);
96 die("fatal safety problem: %s%s", msg, where);
99 void safety_panic(Train *tra, Segment *seg, const char *fmt, ...) {
102 safety_vpanic(tra, seg, fmt, al);
105 ErrorCode safety_problem(Train *tra, Segment *seg, const char *fmt,...) {
108 vlogmsg(EC_Safety, tra, seg?seg->i:0, fmt, al);
113 /*---------- printing nmra data ----------*/
118 unsigned datapending;
121 static void nmradec_init(NmraDecCtx *d, const PicInsn *pi) {
128 static void nmradec_getbits(NmraDecCtx *d, int *nbits_io, unsigned *bits_r) {
130 while (d->npending < use) {
131 if (d->i >= d->o) break;
132 d->datapending <<= 7;
133 d->datapending |= *d->i & 0x7f;
135 if (!(*d->i++ & 0x80)) break;
137 if (d->npending < use) use= d->npending;
139 *bits_r= (d->datapending >> d->npending) & ~(~0u << use);
143 static void nmra_decodeforpic(const PicInsn *pi,
144 void (*on_idle)(void *u, int),
145 void (*on_packet)(void *u, const Nmra*),
146 void (*on_error)(void *u, int reasonchar),
150 * ! - too little idle
155 int need_idle=7, got_idle=0;
161 nbits=1; nmradec_getbits(&d,&nbits,&got);
162 if (got) { got_idle++; continue; }
163 if (got_idle) on_idle(u,got_idle);
165 if (got_idle < need_idle) on_error(u,'!');
170 nbits=8; nmradec_getbits(&d,&nbits,&got);
173 if (nbits<8) on_error(u,'$');
175 nbits=1; nmradec_getbits(&d,&nbits,&got);
176 if (nbits<1) on_error(u,'$');
177 } while (nbits && !got);
179 if (csum) on_error(u,'#');
187 static void opn_idle(void *u, int idle) {
188 static const char dots[]= "......."; /* at least base/2 */
190 int largers= idle / base;
191 int units= idle % base;
193 oprintf(UPO," %.*s%.*s%.*s",
195 largers, ":::::::" /* enough */,
198 static void opn_error(void *u, int rc) { oprintf(UPO," %c",rc); }
199 static void opn_packet(void *u, const Nmra *n) {
201 const char *delim= " <";
202 for (i=0; i<n->l; i++) {
203 oprintf(UPO,"%s%02x",delim,n->d[i]);
209 static void oprint_nmradata(const PicInsn *pi) {
210 oprintf(UPO,"picio out nmradata");
211 nmra_decodeforpic(pi, opn_idle,opn_packet,opn_error, 0);
215 /*---------- command channel handling (oop_read, obc) ----------*/
217 static void command_doline(ParseState *ps, CommandInput *cmdi) {
219 ci= some_needword_lookup(ps, toplevel_cmds, "command");
220 if (ci) ci->fn(ps,ci);
223 void oupicio(const char *dirn, const PicInsnInfo *pii, int objnum) {
225 oprintf(UPO, "picio %s %s\n", dirn, pii->name);
227 oprintf(UPO, "picio %s %s %u\n", dirn, pii->name, objnum);
230 void vbadcmd(ParseState *ps, const char *fmt, va_list al) {
234 static void obc_error(OutBufferChain *ch, const char *e1, const char *e2) {
235 comms_error(ch->desc, e1, e2);
238 void ouhex(const char *word, const Byte *command, int length) {
239 oprintf(UPO, "%s", word);
241 oprintf(UPO, " %02x", *command++);
247 void die_vprintf_hook(const char *fmt, va_list al) {
248 if (events) ovprintf(UPO, fmt, al);
251 void die_hook(void) {
253 e= events ? obc_tryflush(UPO) : 0;
254 if (e) fprintf(stderr,"(unwritten command output: %s)\n",strerror(e));
257 /*---------- serial input (via oop) ----------*/
259 static PicInsn serial_buf;
261 static void *serial_readable(oop_source *evts, int fd,
262 oop_event evt, void *u0) {
265 r= read(serial_fd, serial_buf.d + serial_buf.l,
266 sizeof(serial_buf.d) - serial_buf.l);
268 if (r==0) die("serial port - eof");
270 if (errno == EWOULDBLOCK || errno == EINTR)
272 diee("serial port - read error");
276 buf_used= serial_buf.l + r;
279 serial_buf.l= buf_used;
280 serial_moredata(&serial_buf);
281 if (!serial_buf.l) break;
282 buf_used -= serial_buf.l;
283 memmove(serial_buf.d, serial_buf.d + serial_buf.l, buf_used);
284 if (!buf_used) break;
286 serial_buf.l= buf_used;
290 /*---------- serial port output (not via liboop) ----------*/
292 void serial_transmit(const PicInsn *pi) {
293 const PicInsnInfo *pii;
294 int objnum, suppress=0;
296 if ((pi->d[0] & 0xf8) == 0x90) {
298 const SegmentInfo *segi;
301 oprintf(UPO,"picio out polarity <");
302 for (segn=0, segi=info_segments, delim="";
303 segn < info_nsegments;
305 if (!segi->invertible) continue;
306 if (!picinsn_polarity_testbit(pi,segi)) continue;
307 oprintf(UPO,"%s%s", delim, segi->pname);
311 } else if (pi->d[0] == 0xff) {
312 if (picio_send_noise < 3)
317 picinsn_decode(pi, pic_command_infos, &pii, &objnum);
319 oprintf(UPO, "picio out unknown\n");
320 else if (pii->noiselevel > picio_send_noise)
323 oupicio("out",pii,objnum);
326 if (!suppress && picio_send_noise >= 2)
327 ouhex("picioh out", pi->d, pi->l);
329 /* note that the serial port is still in nonblocking mode. if
330 * we ever buffer up far enough that the kernel wants to make us
331 * block, we should die! */
332 serial_transmit_now(pi->d, pi->l);
335 /*---------- initialisation ----------*/
337 int main(int argc, const char **argv) {
338 oop_source_sys *sys_events;
342 persist_map_veryearly();
344 if (argv[0] && argv[1] && !strcmp(argv[1],PERSIST_CONVERT_OPTION))
345 /* do this before we call malloc so that MAP_FIXED is sure to work */
346 persist_entrails_run_converter();
348 sys_events= oop_sys_new(); if (!sys_events) diee("oop_sys_new");
349 events= oop_sys_source(sys_events); massert(events);
351 cmdi.out.desc= (char*)"command";
353 cmdi.out.error= obc_error;
354 cmdi.doline= command_doline;
358 while ((arg=*++argv) && *arg=='-') {
361 case 's': device= arg; break;
362 case 'p': persist_fn= arg; break;
363 case 'v': picio_send_noise= atoi(arg); break;
364 default: badusage("unknown option");
368 persist_entrails_interpret();
372 r= oop_fd_nonblock(serial_fd, 1); if (r) diee("nonblock(serial_fd,1)");
374 events->on_fd(events, serial_fd, OOP_READ, serial_readable, 0);
375 events->on_fd(events, serial_fd, OOP_EXCEPTION, read_exception, 0);
378 oop_sys_run(sys_events);
382 DEFINE_ERRORCODELIST_DATA