/* LarkEventsB.c
   L. Stewart May 24, 1983  4:24 PM
 */

#include <Env.h>
#include <Queue.h>
#include <Encrypt.h>
#include <Pup.h>
#include <LarkNet.h>
#include <Lark.h>
#include <LarkSlave.h>
#include <LarkVCB.h>


/*
 struct WISArgs {
    int procType;
    int which;
    };
 */

struct SREvents {
  int next;
  int isNil;
  int lenLo;
  int lenHi;
  int length;
  struct SEvent eventseq[16];
  };

int WhatIsStatus(sp)
  int *sp;
  {
  int which;
  struct SREvents *sre;
  sre = (struct SREvents *) sp;
  which = Swab(sp[1]);
  switch (which) {
    case 0: return(FillInRelays(sre));
    default: {
      *sp++ = 0;  /* next = 0 */
      *sp++ = swapped1;  /* isNil = true */
      return (2);  /* two words of results */
      };
    };
  };

int FillInRelays(sre)
  struct SREvents *sre;
  {
  int *modep, prog;
  sre->next = 0;
  sre->isNil = 0;
  sre->length = sre->lenLo = Swab(12);
  sre->lenHi = 0;
  FillInEntry(sre, 0, 8, GetPIO(iapioa) & 0x0080); /* revert */
  FillInEntry(sre, 1, 24, GetPIO(ipioc) & 0x0020); /* revertHookswitch */
  FillInEntry(sre, 2, 9, (GetPIO(iapioa) & 0x0040) ↑ 0x0040); /* offHookRelay */
  FillInEntry(sre, 3, 25, (GetPIO(ipioc) & 0x0010) ↑ 0x0010); /* ARelay */

  FillInEntry(sre, 4, 10, (GetPIO(iapioc) & 0x0004) ↑ 0x0004); /* sidetone */
  FillInEntry(sre, 5, 11, (GetPIO(iapioc) & 0x0008) ↑ 0x0008); /* ringenable */
  FillInEntry(sre, 6, 12, (GetPIO(ipioa) & 0x0040) ↑ 0x0040); /* LED */

  FillInEntry(sre, 7, 2, ScanIn() & AISwitch); /* switch */
  FillInEntry(sre, 8, 7, ScanIn() & AIRing); /* ring detect */
  FillInEntry(sre, 9, 1, ScanIn() & AIHookSwitch); /* hookswitch */
  modep = (int *) ModeFlag;
  FillInEntry(sre, 10, 28, *modep & 3); /* spmode */
  modep = (int *) CodeLoc;
  switch (*modep) {
    case 1: prog = 159; break;
    case 2: prog = 160; break;
    default: prog = 146;
    };
  sre->eventseq[11].time = Swab(ReadTmr());
  sre->eventseq[11].device = 15;
  sre->eventseq[11].event = prog;

  return (24 + 5);
  };

FillInEntry(sre, i, device, on)
  struct SREvents *sre;
  int i, device, on;
  {
  sre->eventseq[i].time = Swab(ReadTmr());
  sre->eventseq[i].device = device;
  sre->eventseq[i].event = (on) ? 144: 145;
  };