home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume15 / ultrix-modem / statelook.c < prev    next >
C/C++ Source or Header  |  1988-05-24  |  3KB  |  100 lines

  1. /*
  2.  * Stuff to work out when to next change state (causes exit)
  3.  * and whether to apply a particular bit of the modem conversation.
  4.  */
  5.  
  6. #include <time.h>
  7.  
  8. #include "modem.h"
  9. #ifdef STATES
  10.  
  11. #define    SECS_IN_WEEK        (60L * 60L * 24L * 7L)
  12. #define WEEK_TIME(d,h,m)    ((((long)d*24L+(long)h)*60L+(long)m)*60L)
  13. #define EVENT_LEN        (sizeof(week_event)/sizeof(struct event))
  14. #define IND_LEN            (sizeof(ind_tab)/sizeof(struct ind))
  15.  
  16. /*
  17.     This is a table of when to turn on and off the autoanswer mode
  18.     of the modem.
  19. */
  20. struct event {
  21.     long s_time;
  22.     int s_state;
  23. } week_event[] = {
  24.        { WEEK_TIME (0,  7, 35), AA | SPK }, /* AA on, speaker on   7:35 Sun */
  25.        { WEEK_TIME (0, 22, 00), AA },       /* AA on, speaker off 22:00 Sun */
  26.     { WEEK_TIME (1,  7, 35), SPK },      /* AA off, speaker on  7:35 Mon */
  27.        { WEEK_TIME (1, 17, 25), AA | SPK }, /* AA on, speaker on  17:25 Mon */
  28.        { WEEK_TIME (1, 22, 00), AA },       /* AA on, speaker off 22:00 Mon */
  29.     { WEEK_TIME (2,  7, 35), SPK },      /* AA off, speaker on  7:35 Tue */
  30.        { WEEK_TIME (2, 17, 25), AA | SPK }, /* AA on, speaker on  17:25 Tue */
  31.        { WEEK_TIME (2, 22, 00), AA },       /* AA on, speaker off 22:00 Tue */
  32.     { WEEK_TIME (3,  7, 35), SPK },      /* AA off, speaker on  7:35 Wed */
  33.        { WEEK_TIME (3, 17, 25), AA | SPK }, /* AA on, speaker on  17:25 Wed */
  34.        { WEEK_TIME (3, 22, 00), AA },       /* AA on, speaker off 22:00 Wed */
  35.     { WEEK_TIME (4,  7, 35), SPK },      /* AA off, speaker on  7:35 Thu */
  36.        { WEEK_TIME (4, 17, 25), AA | SPK }, /* AA on, speaker on  17:25 Thu */
  37.        { WEEK_TIME (4, 22, 00), AA },       /* AA on, speaker off 22:00 Thu */
  38.     { WEEK_TIME (5,  7, 35), SPK },      /* AA off, speaker on  7:35 Fri */
  39.        { WEEK_TIME (5, 17, 25), AA | SPK }, /* AA on, speaker on  17:25 Fri */
  40.        { WEEK_TIME (5, 22, 00), AA },       /* AA on, speaker off 22:00 Fri */
  41.        { WEEK_TIME (6,  7, 35), AA | SPK }, /* AA on, speaker on   7:35 Sat */
  42.        { WEEK_TIME (6, 22, 00), AA }        /* AA on, speaker off 22:00 Sat */
  43. };
  44.  
  45. long duration(state)
  46. int *state;            /* 0 if new Auto Answer state is OFF, 1 if ON */
  47. {
  48.     long secs_into_week;    /* Seconds since midnight Sunday morning */
  49.     long t;
  50.     struct tm *cur_time;
  51.     int i, index;
  52.     long delta, tst_delta;
  53.  
  54.     t = time((long *) 0);
  55.     cur_time = localtime (&t);
  56.  
  57.     secs_into_week = ((((long)(cur_time->tm_wday)) * 24L + 
  58.         (long)(cur_time->tm_hour)) * 60L +
  59.         (long)(cur_time->tm_min)) * 60L + 
  60.         (long)(cur_time->tm_sec);
  61.  
  62.     delta = SECS_IN_WEEK + 1; /* SOMETHING has to be closer than this! */
  63.  
  64.     /* Loop looking for an entry with a better delta (closer to now) */
  65.  
  66.     for (i=0; i < EVENT_LEN; i++) {
  67.         tst_delta = week_event[i].s_time - secs_into_week;
  68.         if (tst_delta < 0L)
  69.             tst_delta += SECS_IN_WEEK; /* Adjust for wrap around */
  70.         if (tst_delta < delta) {
  71.             /* We found a closer event to now */
  72.             delta = tst_delta;
  73.             index = i;
  74.         }
  75.     }
  76.     /*
  77.      * Decrement index by one circularly (note table MUST be in order) --
  78.      */
  79.     if (index-- == 0)
  80.         index = EVENT_LEN-1;
  81.     *state = week_event[index].s_state;
  82. #ifdef DEBUG
  83.     printf("Currently in state %d.  Will change in %ld seconds\n",
  84.         *state, delta);
  85. #endif
  86.     return(delta);
  87. }
  88.  
  89. /*
  90.     This routine decides whether a particular init line should be executed.
  91. */
  92. applicable(p, state)
  93. struct conv *p;
  94. int state;
  95. {
  96.     return(p->c_flags & state);
  97. }
  98.  
  99. #endif
  100.