Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2006 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <debug.h> |
| 24 | #include <malloc.h> |
| 25 | #include <err.h> |
| 26 | #include <dev/ethernet.h> |
| 27 | |
| 28 | #include <lwip/err.h> |
| 29 | #include <lwip/stats.h> |
| 30 | #include <lwip/sys.h> |
| 31 | #include <lwip/ip.h> |
| 32 | #include <lwip/mem.h> |
| 33 | #include <lwip/memp.h> |
| 34 | #include <lwip/ip_addr.h> |
| 35 | #include <lwip/netif.h> |
| 36 | #include <lwip/dhcp.h> |
| 37 | #include <lwip/tcpip.h> |
| 38 | #include <netif/etharp.h> |
| 39 | |
| 40 | static void |
| 41 | arp_timer(void *arg) |
| 42 | { |
| 43 | etharp_tmr(); |
| 44 | sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); |
| 45 | } |
| 46 | |
| 47 | #if WITH_DHCP |
| 48 | static void |
| 49 | dhcp_coarse_timer(void *arg) |
| 50 | { |
| 51 | dhcp_coarse_tmr(); |
| 52 | sys_timeout(60*1000, dhcp_coarse_timer, NULL); |
| 53 | } |
| 54 | |
| 55 | static void |
| 56 | dhcp_fine_timer(void *arg) |
| 57 | { |
| 58 | dhcp_fine_tmr(); |
| 59 | sys_timeout(500, dhcp_fine_timer, NULL); |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | int lwip_init(void) |
| 64 | { |
| 65 | stats_init(); |
| 66 | sys_init(); |
| 67 | mem_init(); |
| 68 | memp_init(); |
| 69 | pbuf_init(); |
| 70 | |
| 71 | tcpip_init(NULL, NULL); |
| 72 | |
| 73 | thread_sleep(1000); |
| 74 | |
| 75 | if (ethernet_init() < NO_ERROR) { |
| 76 | dprintf("lwip_init: error initializing ethernet, aborting...\n"); |
| 77 | return ERROR; |
| 78 | } |
| 79 | |
| 80 | etharp_init(); |
| 81 | sys_timeout(ARP_TMR_INTERVAL, arp_timer, NULL); |
| 82 | |
| 83 | #if WITH_STATIC_IP |
| 84 | struct ip_addr ipaddr; |
| 85 | struct ip_addr gwaddr; |
| 86 | struct ip_addr netmask; |
| 87 | |
| 88 | ipaddr.addr = htonl(IP_ADDR); |
| 89 | gwaddr.addr = htonl(GW_ADDR); |
| 90 | netmask.addr = htonl(NETMASK); |
| 91 | |
| 92 | netif_set_ipaddr(netif_default, &ipaddr); |
| 93 | netif_set_netmask(netif_default, &netmask); |
| 94 | netif_set_gw(netif_default, &gwaddr); |
| 95 | #endif |
| 96 | |
| 97 | netif_set_up(netif_default); |
| 98 | |
| 99 | #if WITH_DHCP |
| 100 | dprintf("starting dhcp on default netif\n"); |
| 101 | dhcp_start(netif_default); |
| 102 | |
| 103 | /* start some dhcp timers */ |
| 104 | sys_timeout(60*1000, dhcp_coarse_timer, NULL); |
| 105 | sys_timeout(500, dhcp_fine_timer, NULL); |
| 106 | #endif |
| 107 | return 0; |
| 108 | } |