chiark / gitweb /
wip new safety - before change way trackloc movpos is done
[trains.git] / hostside / movpos.c
1 /*
2  * Handling of points and other moveable features.
3  */
4
5 #include "realtime.h"
6
7 /*========== declarations ==========*/
8
9 typedef struct {
10   const MovFeatInfo *i;
11   Small posn;
12 } Motion;
13
14 typedef struct KindInfo KindInfo;
15
16 /* Kind-independent code is responsible for determining
17  * the method, doing a bit of cleanup, and adjusting the flow
18  * slightly.  Per-kind code does the actual work and is mostly in
19  * charge - it is also responsible for updating seg->moving.
20  */
21 /* The following states exist for each MovPosChange
22  * at points when control flow  passes between kind and indep:
23  *   U  Unallocated  no memory allocated (MovPosChange does not exist)
24  *   A  Allocated    memory allocation done
25  *   R  Reserved     reservation was successful
26  *   C  Confirmed    motion queued and will occur
27  *   D  Done         motion is complete and callback just needs to be made
28  *   E  Erroneous    indep must call destroy straight away
29  * seg->moving is in one of the states UC
30  */
31
32 typedef struct MovPosChange {      /* valid in:   filled in by and when:     */
33   const KindInfo *ki;              /*  ARCDE       indep after allocate()    */
34   Segment *move;                   /*  ARCDE       indep after allocate()    */
35   MovPosComb actual;               /*  CD          see below                 */
36   MovPosComb intent;               /*  RCD         indep after allocate()    */
37   /* kind-specific data follows */ /*  varies     kind-specific code, varies */
38 } Change;
39   /* `actual' contains the kind's public opinion about the physical
40    * state.  It is initialised by indep (just before confirm) from
41    * move->moving->actual or move->movposcomb as the case may be.  It
42    * should be updated by the kind, since it is used by indep for
43    * calculating the number and identities of the features which may
44    * need to change when a new move request is intended to replace an
45    * existing one - ie, the contents of the motions[] provided to a
46    * subsequent confirm().  So while a change is Confirmed, the
47    * physical state is recorded only in the relevant change, and not
48    * in the segment's movposcomb.  Once a change goes to Confirmed,
49    * the indep code never untangles it so the kind can manage the
50    * proper transition.  */
51
52 struct KindInfo {
53   Change *(*allocate)(int alloc_motions); /* U->A (always succeeds) */
54   ErrorCode (*reserve)(Change*, Segment*, int ms); /* A->R; error: A->E */
55   ErrorCode (*confirm)(Change*, Segment*, int n_motions,
56                        const Motion*, int ms);   /* [AR]->C; error; [AR]->E */
57   void (*destroy)(Change*);                        /* [ARCE]->U */
58   /* indep guarantees that
59    *   alloc_motions >= move->i->n_motions    on reserve
60    *   alloc_motions >= n_motions             on confirm
61    * and that if on entry to reserve move->moving is non-0,
62    *  it is of the same kind
63    */
64 };
65
66 /*========== points ==========*/
67
68 /*
69  * We maintain two queues, one for reserved one for actually confirmed
70  *  requests where we know what we're doing.
71  *
72  * We divide time into discrete slots, numbered with clock arithmetic.
73  *
74  *     cslot         cslot+1      cslot+2
75  *
76  *     currently     next in      after
77  *     changing      line         that
78  *
79  * We increment cslot when we issue a POINT command to the PIC.
80  * In a request, the deadline represents the latest allowable value
81  * of cslot just before that increment.
82  */
83
84 typedef unsigned PtSlot;
85 typedef int PtSlotSigned;
86
87 /* We think there are three states: Allocated, Reserved and Confirmed.
88  * (plus of course Unallocated where we don't have a request at all).
89  * These correspond to the indep code as follows:
90  *
91  *  indep state   pt state    queues checked and plan viable
92  *   Unallocated   n/a         yes
93  *   Allocated     Allocated   yes
94  *   Reserved      Reserved    yes
95  *   Confirmed     Confirmed   yes
96  *   Erroneous     A/R/C       no
97  *
98  * Erroneous exists only after a failed reserve() or confirm() so it's
99  * not that confusing to have this slightly malleable terminology.
100  */
101
102 typedef struct {                 /* Allocated  Reserved   Confirmed       */
103                      /* in queue?    absent     reserved   confirmed      */
104   Change h;
105   PtSlot deadline;   /*              ~0         relative   absolute    <- */
106   MovPosComb actual; /*              undef      undef      see below      */
107   int n_motions;     /*              alloc'd    alloc'd    undone         */
108   Motion motions[];  /*   [0].i:     0          0          non-0       <- */
109                      /*  [..].i:     undef      undef      non-0          */
110                      /*   .posn:     undef      undef      defined        */
111 } PointReq;
112   /* We can determine the the state by looking at the two
113    * `statedet' fields, marked <- above.
114    * There are also intermediate states where the req's
115    *  statedet fields do not agree with the queue it's on.
116    *  We write these as, for example,
117    *      AR   to mean  statedet says Allocated, but queued on pt_reserved
118    *      A?   to mean  statedet says Allocated, but may be queued
119    *  etc.  They are only allowed while we are in a pt_... method function.
120    */
121   /* PointReq.actual is subtly differnet to MovPosChange.actual,
122    * as follows:
123    *                              in MovPosChange     in PointReq
124    *  Position unknown              -1                  0
125    *  Position partly known         -1                  unknown feats are 0
126    *  Position completely known     exact               exact
127    *
128    * The partial knowledge positions can only occur in requests that
129    * are confirmed with as many motions as features, so we know that
130    * if we complete a request we know that we can copy actual out
131    * to MovPosChange.
132    *
133    * If we abandon a half-done change to a multi-feat segment
134    * we lose the partial knowledge.
135    */
136
137 #define CDU_RECHARGE   250 /*ms*/
138 #define POINT_MOVEMENT  50 /*ms*/
139 #define PT_MAX_QUEUE  15
140
141 typedef struct {
142   int n;
143   PointReq *l[PT_MAX_QUEUE];
144 } PointQueue;
145
146 /*
147  * CDU and point queue states:
148  *
149  *
150  *    ____________                  pt_cdu_     conf'd
151  *   /   points_  \                 charged       .n
152  *   |   all_      |
153  *   |   abaondon  |
154  *   |             V
155  *   |from       INACTIVE               -1      0
156  *   |any       <=Sta_Settling
157  *  ^^^^^^^^     (start)
158  *                 |
159  *     ___________ |turning
160  *    /           \| _on
161  *   |             V
162  *   |           CHARGING               0       any
163  *   |          >=Sta_Resolving
164  *   |             |
165  *   |             |on_pic
166  *   |             |_charged
167  *   |             V
168  *   ^           READY                  1       any
169  *   |             |
170  *   |             |pt_check_action
171  *   |             | fires a point
172  *    \___________/
173  *
174  */
175
176 static PtSlot pt_cslot;
177 static int pt_cdu_charged;
178 static PointQueue pt_confirmed, pt_reserved;
179
180 static void pt_check_action(void);
181
182 static PtSlot pt_maxdelay_reldeadline(int maxdelay_ms) {
183   return (maxdelay_ms - POINT_MOVEMENT + CDU_RECHARGE) / CDU_RECHARGE;
184 }
185
186 static void pt_queue_remove_index(PointQueue *q, int index) {
187   q->n--;
188   memmove(&q->l[index], &q->l[index+1], sizeof(q->l[0]) * (q->n - index));
189 }
190
191 static int pt_req_compar(const void *av, const void *bv) {
192   PointReq *const *a= av;
193   PointReq *const *b= av;
194   return (PtSlotSigned)((*b)->deadline - (*a)->deadline);
195 }
196
197 static void pt_queue_remove_item(PointQueue *q, PointReq *r) {
198   PointReq **entry;
199   entry= bsearch(r, q->l, q->n, sizeof(q->l[0]), pt_req_compar);
200   assert(entry);
201   pt_queue_remove_index(q, entry - q->l);
202 }
203
204 static void pt_dequeue(PointReq *r) { /* X->XA */
205   if (r->motions[0].i) {
206     pt_queue_remove_item(&pt_confirmed, r);
207   } else if (~r->deadline) {
208     pt_queue_remove_item(&pt_reserved, r);
209   }
210 }
211
212 static void pt_mark_as_allocated(PointReq *r) { /* AX->X */
213   /* Sets statedet fields for Allocated */
214   r->deadline= ~(PtSlot)0;
215   r->motions[0].i=0;
216 }
217
218 static ErrorCode pt_check_plan(void) {
219   /* Checks whether we can meet the currently queued commitments */
220   int future, conf, resv, usewhen;
221
222   conf=resv=0;
223
224   /* If CDU is charged we can do one thing right away */
225   while (conf < pt_confirmed.n &&
226          pt_confirmed.l[0]->deadline==pt_cslot) {
227     if (!pt_cdu_charged) return EC_MovFeatTooLate;
228     if (conf) return EC_MovFeatTooLate;
229     conf++;
230   }
231
232   future=1;
233   for (;;) {
234     PointReq *confr= conf < pt_confirmed.n ? pt_confirmed.l[conf] : 0;
235     PointReq *resvr= resv < pt_reserved .n ? pt_reserved .l[conf] : 0;
236     if (!confr && !resvr) break;
237     int confwhen= confr ? confr->deadline - pt_cslot : INT_MAX;
238     int resvwhen= resvr ? resvr->deadline            : INT_MAX;
239     if (resvwhen < confwhen) {
240       usewhen= resvwhen;
241       resv++;
242     } else {
243       usewhen= confwhen;
244       conf++;
245     }
246     if (usewhen > future) return EC_MovFeatTooLate;
247     future++;
248   }
249   return 0;
250 }
251
252 static ErrorCode pt_enqueue(PointQueue *q, PointReq *r) { /* XA -> X */
253   int insat;                 /* ... where X is R or C and corresponds to q */
254                              /* or on error,   XA -> A */
255
256   if (q->n == PT_MAX_QUEUE) {
257     return EC_BufferFull;
258   }
259
260 fprintf(stderr,"  pt_enqueue\n");
261   for (insat= q->n;
262        insat>0 && (PtSlotSigned)(r->deadline - q->l[insat-1]->deadline) < 0;
263        insat--)
264     q->l[insat]= q->l[insat-1];
265   q->l[insat]= r;
266   q->n++;
267
268   return pt_check_plan();
269   /* if this fails, indep machinery calls pt_destroy which dequeues */
270 }
271
272 /*---------- kind method entrypoints ----------*/
273
274 static Change *point_allocate(int alloc_motions) {
275   PointReq *r;
276
277 fprintf(stderr,"  point allocate %d\n",alloc_motions);
278   assert(pt_cdu_charged>=0);
279   if (!alloc_motions)
280     /* we need at least one motion in the table so we can tell
281      *  the difference between the states by looking at motions[0].i */
282     alloc_motions= 1;
283
284   r= mmalloc(sizeof(*r) + alloc_motions * sizeof(r->motions[0]));
285   r->deadline= ~(PtSlot)0;
286   r->n_motions= alloc_motions;
287   r->motions[0].i= 0;
288   return (Change*)r;
289 }
290
291 static ErrorCode point_reserve(Change *chg, Segment *move,
292                                int maxdelay_ms) {
293   PointReq *r= (PointReq*)chg;
294   r->deadline= pt_maxdelay_reldeadline(maxdelay_ms);
295   if (!r->deadline) { pt_mark_as_allocated(r); return EC_MovFeatTooLate; }
296   return pt_enqueue(&pt_reserved, r);
297 }
298
299 static ErrorCode point_confirm(Change *chg, Segment *move,
300                                int n_motions, const Motion *motions,
301                                int maxdelay_ms) {
302   PointReq *r= (PointReq*)chg;
303   PtSlot newdeadline;
304   int allow_failure;
305   ErrorCode ec;
306
307 fprintf(stderr,"  point confirm\n");
308
309   /* If the segment is moving, these motions are already based on the
310    * actual physical position which is stored in the existing request.
311    * So we try removing the existing request from the queue and put
312    * it back if it doesn't work.
313    */
314
315   if (n_motions >= r->n_motions)
316     return EC_MovFeatReservationInapplicable;
317   assert(n_motions <= r->n_motions);
318   newdeadline= pt_maxdelay_reldeadline(maxdelay_ms) + pt_cslot;
319   allow_failure= newdeadline < r->deadline;
320
321   /* state A or R */
322   pt_dequeue(r);
323                                            /* states of existing: */
324   PointReq *existing= (PointReq*)move->moving;     /* U or C */
325   if (existing) pt_dequeue(existing);              /* U or CA */
326
327   /* state A or RA */
328   memcpy(r->motions, motions, sizeof(r->motions[0])*n_motions);
329   if (!n_motions) r->motions[0].i= move->i->movfeats;
330   assert(r->motions[0].i);
331   r->n_motions= n_motions;
332   r->deadline= newdeadline + pt_cslot;
333
334   if (n_motions == move->i->n_movfeats)
335     r->actual= 0;
336   else
337     r->actual= chg->actual;
338   assert(r->actual >= 0);
339
340   /* state CA */
341   ec= pt_enqueue(&pt_confirmed, r);
342   assert(allow_failure || !ec);
343
344   if (existing) {                                  /* CA */
345     if (ec) { /* state C but bad */
346       pt_dequeue(r); /* state CA */
347       pt_mark_as_allocated(r); /* state A */
348       ErrorCode ec_putback= pt_enqueue(&pt_confirmed, existing);
349       assert(!ec_putback);                         /* C */
350     } else { /* state C and good */
351       free(existing);                              /* U */
352     }
353   }
354   /* either  ec=0   state C                            U
355    *     or  ec!=0  state A                            C
356    *     or  ec!=0  state C but bad                    C
357    */
358
359   if (!ec) {
360     move->moving= chg;
361     move->movposcomb= -1;
362     pt_check_action();
363   }
364
365   return ec;
366 }
367
368 static void point_destroy(Change *chg) { /* X->XA and then free it */
369   PointReq *r= (PointReq*)chg;
370   pt_dequeue(r);
371   free(r);
372 }
373
374 /*---------- actually firing points, yay! ----------*/
375
376 static void pt_check_action(void) {
377   PicInsn piob;
378
379   if (!pt_confirmed.n) {
380     if (sta_state == Sta_Finalising) resolve_motioncheck();
381     return;
382   }
383
384   PointReq *r= pt_confirmed.l[0];
385
386   if (r->n_motions && pt_cdu_charged) {
387     /* look for something to fire */
388     Motion *m= &r->motions[--r->n_motions];
389     assert(m->posn < m->i->posns);
390     enco_pic_point(&piob, m->i->boob[m->posn]);
391     serial_transmit(&piob);
392     pt_cdu_charged= 0;
393
394     MovPosComb above_weight= m->i->weight * m->i->posns;
395     MovPosComb above= r->actual / above_weight;
396     MovPosComb below= r->actual % m->i->weight;
397     r->actual= above*above_weight + m->posn*m->i->weight + below;
398     if (r->h.actual >= 0 || !r->n_motions)
399       r->h.actual= r->actual;
400   }
401
402   if (!r->n_motions) {
403     /* look for something to report
404      * we can get things here other than from the above
405      * eg if we are asked to move the 
406      */
407     Segment *move= r->h.move;
408     assert(move->moving == (Change*)r);
409     pt_queue_remove_index(&pt_confirmed,0);
410     pt_mark_as_allocated(r); /* now state A aka Done */
411     move->movposcomb= r->h.actual;
412     move->moving= 0;
413     free(r);
414     pt_check_action();
415   }
416 }
417
418 /*---------- entrypoints from rest of program ----------*/
419
420 void points_all_abandon(void) {
421   int i;
422
423   assert(!pt_reserved.n);
424
425   for (i=0; i<pt_confirmed.n; i++) {
426     PointReq *r= pt_confirmed.l[i];
427     Segment *move= r->h.move;
428     assert(move->moving == (Change*)r);
429     move->moving= 0;
430     move->movposcomb= r->h.actual;
431     free(r);
432   }
433   pt_confirmed.n= 0;
434   pt_cdu_charged= -1;
435 }
436
437 void points_turning_on(void) {
438   pt_cdu_charged= 0;
439 }
440
441 void on_pic_charged(const PicInsnInfo *pii, const PicInsn *pi, int objnum) {
442   if (pt_cdu_charged<0) return;
443   pt_cdu_charged= 1;
444   pt_check_action();
445 }
446
447 /*========== dummy `nomove' kind ==========*/
448
449 static Change *nomove_allocate(int alloc_motions) {
450 fprintf(stderr,"  nomove allocate %d\n",alloc_motions);
451   return mmalloc(sizeof(Change));
452 }
453 static void nomove_destroy(Change *chg) {
454   free(chg);
455 }
456
457 static ErrorCode nomove_reserve(Change *chg, Segment *move, int ms) {
458 fprintf(stderr,"  nomove reserve\n");
459   return 0;
460 }
461 static ErrorCode nomove_confirm(Change *chg, Segment *move, int n_motions,
462                        const Motion *motions, int ms) {
463 fprintf(stderr,"  nomove confirm\n");
464   nomove_destroy(chg);
465   return 0;
466 }
467
468 /*========== method-independent machinery ==========*/
469
470 static const KindInfo methodinfos[]= {
471   { nomove_allocate, nomove_reserve, nomove_confirm, nomove_destroy },
472   { point_allocate,  point_reserve,  point_confirm,  point_destroy  },
473   { 0 }
474 };
475
476 static Change *mp_allocate(const KindInfo *ki, Segment *move,
477                            int alloc_motions, MovPosComb target) {
478   assert(sta_state >= Sta_Resolving);
479   Change *chg= ki->allocate(alloc_motions);
480   chg->ki=        ki;
481   chg->move=      move;
482   chg->intent=    target;
483   return chg;
484 }
485
486 static int change_needed(const MovFeatInfo *feati, MovPosComb target,
487                         MovPosComb startpoint) {
488   return
489     startpoint<0 ||
490     (target - startpoint) / feati->weight % feati->posns;
491 }  
492
493 static int evaluate_target(Segment *move, MovPosComb target,
494                            MovPosComb startpoint, MovFeatKind *kind_r) {
495   /* returns number of features which have to change to reach target,
496    * or -1 for mixed kinds.  kind_r may be 0. */
497   const SegmentInfo *movei= move->i;
498   int feat, tchanges;
499   const MovFeatInfo *feati;
500   MovFeatKind kind;
501
502   if (startpoint<0) startpoint= movpos_poscomb_actual(move);
503
504   for (feat=0, feati=movei->movfeats, tchanges=0, kind= mfk_none;;
505        feat<movei->n_movfeats;
506        feat++, feati++) {
507     if (!change_needed(feati,target,startpoint)) continue;
508     tchanges++;
509     if (kind && feati->kind != kind) return -1;
510     kind= feati->kind;
511   }
512
513   if (kind_r) *kind_r= kind;
514   return tchanges;
515 }
516
517 ErrorCode movpos_findcomb_bysegs(Segment *back, Segment *move, Segment *fwd,
518                                  MovPosComb startpoint, MovPosComb *chosen_r) {
519   const SegmentInfo *movei= move->i;
520   MovPosComb tcomb, bestcomb=-1;
521   int tchanges, bestchanges=INT_MAX;
522   const SegPosCombInfo *pci;
523
524   for (tcomb=0, pci=movei->poscombs;
525        tcomb<movei->n_poscombs;
526        tcomb++, pci++) {
527     Segment *tback= &segments[pci->backwards.next];
528     Segment *tfwd=  &segments[pci->forwards .next];
529     if (back && !(back==tback || back==tfwd)) continue;
530     if (fwd  && !(fwd ==tback || fwd ==tfwd)) continue;
531
532     if (movei->n_movfeats>1) {
533       //fprintf(stderr,"  several feats\n");
534       /* we have to search for the one which is least effort, then */
535       tchanges= evaluate_target(move,tcomb,startpoint,0);
536       if (tchanges >= bestchanges) /* prefer low-numbered movposcombs */
537         continue;
538       if (tchanges==-1) {
539         tchanges= INT_MAX-1;
540         /* fall through and update */
541       }
542     } else {
543       tchanges= 1;
544     }
545     bestcomb= tcomb;
546     bestchanges= tchanges;
547   }
548   if (*chosen_r) *chosen_r= bestcomb;
549   return
550     tchanges==INT_MAX ? EC_Invalid :
551     tchanges==INT_MAX-1 ? EC_MovFeatKindsCombination :
552     0;
553 }
554
555 ErrorCode movpos_change(Segment *move, MovPosComb target,
556                         int maxdelay_ms, MovPosChange *chg) {
557   const SegmentInfo *movei= move->i;
558   const MovFeatInfo *feati;
559   int feat;
560   MovPosComb actual;
561   ErrorCode ec;
562   MovFeatKind kind= mfk_none;
563
564   if (!move->moving) {
565     actual= move->movposcomb;
566     assert(!move->motion);
567   } else {
568     kind= move->motion->ki - methodinfos;
569     actual= move->motion->actual;
570   }
571
572   {
573     int n_motions=0;
574     Motion motions[movei->n_movfeats];
575
576 fprintf(stderr," motions...  best=%lu actual=%ld\n",target,actual);
577     for (feat=0, feati=movei->movfeats;
578          feat<movei->n_movfeats;
579          feat++, feati++) {
580 fprintf(stderr,"  checking %s w=%lu posns=%d\n",
581         feati->pname,feati->weight,(int)feati->posns);
582       if (!change_needed(feati,actual,target))
583         continue;
584       MovPosComb posn= target / feati->weight % feati->posns;
585 fprintf(stderr,"    motion %s %lu kind=%d\n",feati->pname,posn,kind);
586       if (kind) {
587         if (feati->kind != kind) { ec= EC_MovFeatKindsCombination; goto x; }
588       } else {
589         kind= feati->kind;
590       }
591       motions[n_motions].i= feati;
592       motions[n_motions].posn= posn;
593       n_motions++;
594     }
595
596     const KindInfo *ki= &methodinfos[kind];
597
598     if (chg) {
599       if (chg->ki != ki ||
600           chg->move != move ||
601           chg->intent != intent)
602         return EC_MovFeatReservationInapplicable;
603     } else {
604       chg= mp_allocate(ki,move,n_motions,target);
605     }
606     chg->actual= actual;
607
608 fprintf(stderr," confirming %d motions...\n",n_motions);
609     ec= ki->confirm(chg, move, n_motions, motions, maxdelay_ms);
610 fprintf(stderr," confirming gave %s\n",errorcodelist[ec]);
611     if (ec) goto x;
612   }
613   return 0;
614
615  x:
616   movpos_unreserve(chg);
617   return ec;
618 }
619
620 ErrorCode
621 movpos_reserve(Segment *move, int maxdelay_ms, MovPosChange **res_r,
622                MovPosComb target, MovPosComb startpoint /*as for findcomb*/) {
623   MovFeatKind kind= mfk_none;
624   const MovFeatInfo *feati;
625   ErrorCode ec;
626   int feat, nchanges;
627
628   nchanges= evaluate_target(move,target,startpoint,&kind);
629   if (nchanges=-1) return EC_MovFeatKindsCombination;
630
631   const KindInfo *ki= &methodinfos[kind];
632   Change *chg= mp_allocate(ki, move, move->i->n_movfeats, target);
633   ec= ki->reserve(chg, move, maxdelay_ms);
634   if (ec) goto x;
635
636   *res_r= chg;
637   return 0;
638
639  x:
640   movpos_unreserve(chg);
641   return ec;
642 }
643
644 void movpos_unreserve(MovPosChange *res) {
645   if (!res) return;
646   res->ki->destroy(res);
647 }
648
649 MovPosComb movpos_poscomb_actual(Segment *seg) {
650  return seg->moving ? seg->moving->actual : seg->movposcomb;
651 }
652
653 MovPosComb movpos_change_intent(MovPosChange *chg) {
654   return chg->intent;
655 }