forked from sde1000/NanodeUIP
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathNanodeUIP.cpp
212 lines (179 loc) · 5.78 KB
/
NanodeUIP.cpp
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "NanodeUIP.h"
#include <avr/pgmspace.h>
#include <stdio.h>
#include <inttypes.h>
#include "uip.h"
#include "uip_arp.h"
#include "enc28j60.h"
#include "dhcpc.h"
#include "resolv.h"
#undef PSTR
#define PSTR(s) (__extension__({static const char __c[] __attribute__ (( section (".progmem") )) = (s); &__c[0];}))
void __attribute__((weak)) nanode_log(char *msg) {
Serial.println(msg);
Serial.flush();
}
void __attribute__((weak)) nanode_log_P(PGM_P msg) {
printf_P(PSTR("%lu: %S\r\n"),millis(),msg);
Serial.flush();
}
void dhcpc_configured(const struct dhcpc_state *s) {
uip_sethostaddr(s->ipaddr);
uip_setnetmask(s->netmask);
uip_setdraddr(s->default_router);
/* We don't call resolv_conf here, because that would drag in all
the resolver code and state whether or not it's used by the
sketch. Instead we pass the address of the DNS server to the
DHCP status callback code provided by the sketch and allow that
to initialise the resolver if desired. */
// resolv_conf(s->dnsaddr);
if (uip.dhcp_status_callback!=NULL) {
uip.dhcp_status_callback(DHCP_STATUS_OK,s->dnsaddr);
}
}
void resolv_found(char *name, u16_t *ipaddr);
void resolv_found(char *name, u16_t *ipaddr)
{
if (uip.resolv_status_callback!=NULL) {
uip.resolv_status_callback(name,ipaddr);
}
}
NanodeUIP::NanodeUIP(void) {
// We don't initialise the UIP code in this constructor, because
// apparently in some circumstances this can be called before the
// init() function in arduino's wiring.c and several library
// functions (like delay()) don't work.
dhcp_status_callback=NULL;
}
void NanodeUIP::init(const byte *macaddr, uint8_t cs_pin) {
const struct uip_eth_addr *mac=(struct uip_eth_addr *)macaddr;
uip_setethaddr((*mac));
enc28j60SpiInit();
enc28j60InitWithCs(macaddr, cs_pin);
enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
delay(10);
timer_set(&periodic_timer, CLOCK_SECOND / 2);
timer_set(&arp_timer, CLOCK_SECOND * 10);
uip_init();
}
void NanodeUIP::wait_for_link(void) {
while (!enc28j60linkup());
}
boolean NanodeUIP::link_is_up(void) {
return enc28j60linkup()!=0;
}
void NanodeUIP::set_ip_addr(byte a, byte b, byte c, byte d) {
uip_ipaddr_t ipaddr;
uip_ipaddr(ipaddr, a,b,c,d);
uip_sethostaddr(ipaddr);
}
void NanodeUIP::set_netmask(byte a, byte b, byte c, byte d) {
uip_ipaddr_t ipaddr;
uip_ipaddr(ipaddr, a,b,c,d);
uip_setnetmask(ipaddr);
}
void NanodeUIP::set_gateway_addr(byte a, byte b, byte c, byte d) {
uip_ipaddr_t ipaddr;
uip_ipaddr(ipaddr, a,b,c,d);
uip_setdraddr(ipaddr);
}
void NanodeUIP::set_nameserver_addr(byte a, byte b, byte c, byte d) {
uip_ipaddr_t ipaddr;
uip_ipaddr(ipaddr, a,b,c,d);
resolv_conf(ipaddr);
}
// Requires a buffer of at least 18 bytes to format into
void NanodeUIP::get_mac_str(char *buf) {
sprintf_P(buf,PSTR("%02X:%02X:%02X:%02X:%02X:%02X"),
uip_ethaddr.addr[0], uip_ethaddr.addr[1], uip_ethaddr.addr[2],
uip_ethaddr.addr[3], uip_ethaddr.addr[4], uip_ethaddr.addr[5]);
}
// Requires a buffer of at least 16 bytes to format into
void NanodeUIP::format_ipaddr(char *buf,uint16_t *addr) {
sprintf_P(buf,PSTR("%d.%d.%d.%d"),addr[0]&0xff,addr[0]>>8,
addr[1]&0xff,addr[1]>>8);
}
void NanodeUIP::get_ip_addr_str(char *buf) {
format_ipaddr(buf,uip_hostaddr);
}
void NanodeUIP::get_netmask_str(char *buf) {
format_ipaddr(buf,uip_netmask);
}
void NanodeUIP::get_gateway_str(char *buf) {
format_ipaddr(buf,uip_draddr);
}
boolean NanodeUIP::start_dhcp(dhcp_status_fn *callback) {
dhcp_status_callback=callback;
return dhcpc_init(&uip_ethaddr,6);
}
void NanodeUIP::init_resolv(resolv_result_fn *callback) {
resolv_status_callback=callback;
resolv_init();
}
void NanodeUIP::query_name(const char *name) {
resolv_query(name);
}
uint16_t *lookup_name(const char *name) {
return resolv_lookup(name);
}
#define BUF ((struct uip_eth_hdr *)&uip_buf[0])
void NanodeUIP::poll(void) {
int i;
uip_len = enc28j60PacketReceive(UIP_BUFSIZE,uip_buf);
if(uip_len > 0) {
if(BUF->type == UIP_HTONS(UIP_ETHTYPE_IP)) {
uip_arp_ipin();
uip_input();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
uip_arp_out();
enc28j60PacketSend(uip_len,uip_buf);
}
} else if(BUF->type == UIP_HTONS(UIP_ETHTYPE_ARP)) {
uip_arp_arpin();
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
enc28j60PacketSend(uip_len,uip_buf);
}
}
} else if(timer_expired(&periodic_timer)) {
timer_reset(&periodic_timer);
for(i = 0; i < UIP_CONNS; i++) {
uip_periodic(i);
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
uip_arp_out();
enc28j60PacketSend(uip_len,uip_buf);
}
}
#if UIP_UDP
for(i = 0; i < UIP_UDP_CONNS; i++) {
uip_udp_periodic(i);
/* If the above function invocation resulted in data that
should be sent out on the network, the global variable
uip_len is set to a value > 0. */
if(uip_len > 0) {
uip_arp_out();
enc28j60PacketSend(uip_len,uip_buf);
}
}
#endif /* UIP_UDP */
/* Call the ARP timer function every 10 seconds. */
if(timer_expired(&arp_timer)) {
timer_reset(&arp_timer);
uip_arp_timer();
}
}
}
/* It seems ugly to define the only instance of the NanodeUIP class
here, but it's a consequence of trying to wrap up the C UIP code in
a C++ class so we can be an Arduino library. If this wasn't here,
callbacks from the UIP code wouldn't have the address of the
NanodeUIP instance. */
NanodeUIP uip;