adamdunkels | 9fcf9d6 | 2003-09-04 19:46:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2003, Adam Dunkels. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. The name of the author may not be used to endorse or promote |
| 14 | * products derived from this software without specific prior |
| 15 | * written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS |
| 18 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 21 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 23 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | * |
| 29 | * This file is part of the Contiki desktop OS fpr the C64 |
| 30 | * |
| 31 | * $Id: slip-drv.c,v 1.1 2003/09/04 19:46:33 adamdunkels Exp $ |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | |
| 36 | /* uip_main.c: initialization code and main event loop. */ |
| 37 | |
| 38 | #include "uip.h" |
| 39 | #include "uip_arp.h" |
| 40 | #include "uip-signal.h" |
| 41 | #include "rs232dev.h" |
| 42 | #include "loader.h" |
| 43 | #include "ek.h" |
| 44 | |
| 45 | #define BUF ((struct uip_eth_hdr *)&uip_buf[0]) |
| 46 | |
| 47 | static u8_t i, arptimer; |
| 48 | static u16_t start, current; |
| 49 | |
| 50 | static void slip_drv_idle(void); |
| 51 | static DISPATCHER_SIGHANDLER(slip_drv_sighandler, s, data); |
| 52 | static struct dispatcher_proc p = |
| 53 | {DISPATCHER_PROC("TCP/IP/SLIP driver", slip_drv_idle, |
| 54 | slip_drv_sighandler, NULL)}; |
| 55 | static ek_id_t id; |
| 56 | |
| 57 | |
| 58 | /*-----------------------------------------------------------------------------------*/ |
| 59 | static void |
| 60 | timer(void) |
| 61 | { |
| 62 | for(i = 0; i < UIP_CONNS; ++i) { |
| 63 | uip_periodic(i); |
| 64 | if(uip_len > 0) { |
| 65 | rs232dev_send(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | for(i = 0; i < UIP_UDP_CONNS; i++) { |
| 70 | uip_udp_periodic(i); |
| 71 | /* If the above function invocation resulted in data that |
| 72 | should be sent out on the network, the global variable |
| 73 | uip_len is set to a value > 0. */ |
| 74 | if(uip_len > 0) { |
| 75 | rs232dev_send(); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | /*-----------------------------------------------------------------------------------*/ |
| 80 | static void |
| 81 | slip_drv_idle(void) |
| 82 | { |
| 83 | uip_len = rs232dev_poll(); |
| 84 | if(uip_len > 0) { |
| 85 | uip_input(); |
| 86 | if(uip_len > 0) { |
| 87 | rs232dev_send(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /* Check the clock so see if we should call the periodic uIP |
| 92 | processing. */ |
| 93 | current = ek_clock(); |
| 94 | |
| 95 | if((current - start) >= CLK_TCK/2) { |
| 96 | timer(); |
| 97 | start = current; |
| 98 | } |
| 99 | } |
| 100 | /*-----------------------------------------------------------------------------------*/ |
| 101 | LOADER_INIT_FUNC(slip_drv_init, arg) |
| 102 | { |
| 103 | arg_free(arg); |
| 104 | |
| 105 | if(id == EK_ID_NONE) { |
| 106 | id = dispatcher_start(&p); |
| 107 | |
| 108 | rs232dev_init(); |
| 109 | |
| 110 | arptimer = 0; |
| 111 | start = ek_clock(); |
| 112 | |
| 113 | dispatcher_listen(uip_signal_poll); |
| 114 | dispatcher_listen(uip_signal_poll_udp); |
| 115 | dispatcher_listen(uip_signal_uninstall); |
| 116 | } |
| 117 | } |
| 118 | /*-----------------------------------------------------------------------------------*/ |
| 119 | static |
| 120 | DISPATCHER_SIGHANDLER(slip_drv_sighandler, s, data) |
| 121 | { |
| 122 | DISPATCHER_SIGHANDLER_ARGS(s, data); |
| 123 | |
| 124 | if(s == uip_signal_poll) { |
| 125 | uip_periodic_conn(data); |
| 126 | if(uip_len > 0) { |
| 127 | rs232dev_send(); |
| 128 | } |
| 129 | } else if(s == uip_signal_poll_udp) { |
| 130 | uip_udp_periodic_conn(data); |
| 131 | if(uip_len > 0) { |
| 132 | rs232dev_send(); |
| 133 | } |
| 134 | } else if(s == dispatcher_signal_quit || |
| 135 | s == uip_signal_uninstall) { |
| 136 | dispatcher_exit(&p); |
| 137 | id = EK_ID_NONE; |
| 138 | LOADER_UNLOAD(); |
| 139 | } |
| 140 | } |
| 141 | /*-----------------------------------------------------------------------------------*/ |