home *** CD-ROM | disk | FTP | other *** search
/ RISC DISC 1 / RISC_DISC_1.iso / pd_share / code / oslib / Examples / p2-625 < prev    next >
Encoding:
Text File  |  1994-09-26  |  3.2 KB  |  115 lines

  1. #include <string.h>
  2.  
  3. #include "econet.h"
  4. #include "os.h"
  5. #include "osbyte.h"
  6. #include "osmodule.h"
  7. #include "osword.h"
  8.  
  9. typedef struct clock_list clock_list;
  10.  
  11. extern Event_Veneer (void);
  12.  
  13. struct clock_list {clock_list *next; econet_tx_cb tx; os_date_and_time clock;};
  14.  
  15. extern byte Command_Port;
  16. extern econet_rx_cb Rx_Handle;
  17. extern clock_list *Tx_List;
  18. extern int Reply_Count, Reply_Delay;
  19. extern byte Buffer [80];
  20.  
  21. os_error *start (void)
  22.  
  23. {  os_error *error = NULL;
  24.  
  25.    if ((error = xos_claim (EventV, (void *) &Event_Veneer, NULL)) != NULL)
  26.       goto finish;
  27.  
  28.    if ((error = xos_byte (osbyte_ENABLE_EVENT,
  29.          Event_EconetRx, SKIP, NULL, NULL)) != NULL)
  30.       goto finish;
  31.  
  32.    if ((error = xos_byte (osbyte_ENABLE_EVENT,
  33.          Event_EconetTx, SKIP, NULL, NULL)) != NULL)
  34.       goto finish;
  35.  
  36.    if ((error = xeconet_create_receive (Command_Port, 0, 0,
  37.          Buffer, sizeof Buffer, &Rx_Handle, NULL)) != NULL)
  38.       goto finish;
  39.  
  40. finish:
  41.    /*If error != NULL, should abandon receive (if created), disable
  42.       enabled events and release EventV (if claimed) here.*/
  43.    return error;
  44. }
  45.  
  46. os_error *event (int *event, econet_rx_cb rx, int status)
  47.            /*or (int *event, econet_tx_cb tx, int status)*/
  48.  
  49.    /*The mechanism by which event_handler() calls event() is not
  50.       detailed here. Assume that if event is set to -1 on exit, the call is
  51.       claimed.*/
  52.  
  53. {  os_error *error = NULL;
  54.    clock_list *clock_ptr;
  55.    byte port, station, net, *buffer;
  56.    oswordreadclock_utc_block utc;
  57.  
  58.    switch (*event)
  59.    {  case Event_EconetRx:
  60.          if (rx == Rx_Handle)
  61.          {  if ((error = xeconet_read_receive (rx, NULL, NULL, &port,
  62.                   &station, &net, &buffer, NULL)) != NULL)
  63.                goto finish;
  64.  
  65.             if ((error = xosmodule_alloc (sizeof *clock_ptr,
  66.                   (void **) &clock_ptr)) != NULL)
  67.                goto finish;
  68.  
  69.             utc.op = oswordreadclock_OP_UTC;
  70.             if ((error = xoswordreadclock_utc (&utc)) != NULL)
  71.                goto finish;
  72.             memcpy (clock_ptr->clock, utc.utc, sizeof (os_date_and_time));
  73.  
  74.             if ((error = xeconet_start_transmit (0, Buffer [0], station,
  75.                   net, (byte *) &clock_ptr->clock, sizeof clock_ptr->clock,
  76.                   Reply_Count, Reply_Delay, &clock_ptr->tx, NULL)) !=
  77.                   NULL)
  78.                goto finish;
  79.  
  80.             /*Add the list to new record.*/
  81.             clock_ptr->next = Tx_List;
  82.             Tx_List = clock_ptr;
  83.  
  84.             /*Now reopen the reception.*/
  85.             if ((error = xeconet_create_receive (Command_Port, 0, 0,
  86.                   Buffer, sizeof Buffer, &Rx_Handle, NULL)) != NULL)
  87.                goto finish;
  88.  
  89.             /*claim vector*/
  90.             *event = -1;
  91.          }
  92.       break;
  93.  
  94.       case Event_EconetTx:
  95.       {  econet_tx_cb tx = (econet_tx_cb) rx; /*change of name only*/
  96.          clock_list **t;
  97.  
  98.          for (t = &Tx_List; *t != NULL; t = &(*t)->next)
  99.             if ((*t)->tx == tx)
  100.             {  /*Remove from the list*/
  101.                *t = (*t)->next;
  102.                (void) xeconet_abandon_transmit (tx, NULL);
  103.                (void) xosmodule_free ((byte *) *t);
  104.             }
  105.  
  106.          /*claim vector*/
  107.          *event = -1;
  108.       }
  109.       break;
  110.    }
  111.  
  112. finish:
  113.    return error;
  114. }
  115.