forked from ethersex/ethersex
/
network.c
154 lines (121 loc) · 3.89 KB
/
network.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
*
* Copyright (c) by Alexander Neumann <alexander@bumpern.de>
* Copyright (c) 2007,2008 by Stefan Siegl <stesie@brokenpipe.de>
* Copyright (c) 2008 by Christian Dietrich <stettberger@dokucode.de>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License (either version 2 or
* version 3) as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* For more information on the GPL, please go to:
* http://www.gnu.org/copyleft/gpl.html
*/
#include <avr/eeprom.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "network.h"
#include "config.h"
#include "core/eeprom.h"
#include "protocols/uip/ipv6.h"
#include "protocols/zbus/zbus.h"
#include "core/debug.h"
#include "protocols/uip/uip.h"
#include "protocols/uip/uip_neighbor.h"
#include "protocols/uip/uip_rfm12.h"
#include "protocols/uip/uip_zbus.h"
#include "services/tftp/tftp.h"
#include "hardware/ethernet/enc28j60.h"
#ifdef BOOTLOADER_SUPPORT
extern uint8_t bootload_delay;
#endif
extern void ethersex_meta_netinit (void);
void
network_init(void)
{
uip_ipaddr_t ip;
(void) ip; /* Keep GCC quiet. */
uip_init();
#if defined(RFM12_IP_SUPPORT) && defined(UIP_MULTI_STACK)
uip_stack_set_active(STACK_RFM12);
rfm12_stack_init();
#endif
#if defined(ZBUS_SUPPORT) && defined(UIP_MULTI_STACK)
uip_stack_set_active(STACK_ZBUS);
zbus_stack_init();
#endif
#ifdef OPENVPN_SUPPORT
uip_stack_set_active(STACK_OPENVPN);
openvpn_init();
#endif
/* load base network settings */
# ifdef DEBUG_NET_CONFIG
debug_printf("net: loading base network settings\n");
# endif
#ifdef ETHERNET_SUPPORT
# ifdef ENC28J60_SUPPORT
uip_stack_set_active(STACK_ENC);
# else /* TAP_SUPPORT */
uip_stack_set_active(STACK_TAP);
#endif
/* use uip buffer as generic space here, since when this function is called,
* no network packets will be processed */
#ifdef EEPROM_SUPPORT
/* use global network packet buffer for configuration */
uint8_t checksum = eeprom_get_chksum();
uint8_t saved_checksum;
eeprom_restore_char(crc, &saved_checksum);
if (checksum != saved_checksum)
eeprom_init();
#endif
#ifdef ETHERNET_SUPPORT
network_config_load();
#endif
/* Do the autoconfiguration after the MAC is set */
# if UIP_CONF_IPV6 && !defined(IPV6_STATIC_SUPPORT)
uip_setprefixlen(64);
uip_ip6autoconfig(0xFE80, 0x0000, 0x0000, 0x0000);
# endif
# if defined(IPV6_STATIC_SUPPORT) && defined(TFTPOMATIC_SUPPORT)
const char *filename = CONF_TFTP_IMAGE;
set_CONF_TFTP_IP(&ip);
tftp_fire_tftpomatic(&ip, filename);
bootload_delay = CONF_BOOTLOAD_DELAY;
# endif /* IPV6_STATIC_SUPPORT && TFTPOMATIC_SUPPORT */
# elif !defined(ROUTER_SUPPORT) /* and not ETHERNET_SUPPORT */
/* Don't allow for eeprom-based configuration of rfm12/zbus IP address,
mainly for code size reasons. */
set_CONF_ETHERRAPE_IP(&ip);
uip_sethostaddr(&ip);
# endif /* not ETHERNET_SUPPORT and not ROUTER_SUPPORT */
ethersex_meta_netinit();
# ifdef ENC28J60_SUPPORT
init_enc28j60();
# endif
# ifdef ETHERNET_SUPPORT
# if UIP_CONF_IPV6
uip_neighbor_init();
# else
uip_arp_init();
# endif
# else /* ETHERNET_SUPPORT */
/* set at least fixed default gateway address
* to allow multi stack routing */
set_CONF_ETHERRAPE_GATEWAY(&ip);
uip_setdraddr(&ip);
# endif /* ETHERNET_SUPPORT */
}
/*
-- Ethersex META --
header(network.h)
init(network_init)
*/