1 #include <avr/io.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "ip_arp_udp_tcp.h"
5 #include "enc28j60.h"
6 #include "timeout.h"
7 #include "avr_compat.h"
8 #include "net.h"
9
10 // This software is a web server only.
11 //
12 static uint8_t mymac[6] = {0x54,0x55,0x58,0x10,0x00,0x29};
13 // the web server's own IP address:
14 static uint8_t myip[4] = {10,0,0,29};
15
16 // server listen port for www
17 #define MYWWWPORT 80
18
19 #define BUFFER_SIZE 550
20 static uint8_t buf[BUFFER_SIZE+1];
21
22 uint16_t http200ok(void)
23 {
24 return(fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 200 OK\r\n
Content-Type: text/html\r\nPragma: no-cache\r\n\r\n")));
25 }
26
27 // prepare the webpage by writing the data to the tcp send buffer
28 uint16_t print_webpage(uint8_t *buf)
29 {
30 uint16_t plen;
31 plen=http200ok();
32 plen=fill_tcp_data_p(buf,plen,PSTR("<pre>"));
33 plen=fill_tcp_data_p(buf,plen,PSTR("Hi!\nYour web server works great."));
34 plen=fill_tcp_data_p(buf,plen,PSTR("</pre>\n"));
35 return(plen);
36 }
37
38 int main(void){
39 uint16_t dat_p;
40
41 // set the clock speed
42 CLKPR=(1<<CLKPCE);
43 CLKPR=0; // 8 MHZ
44 _delay_loop_1(0); // 120us
45
46 //initialize the hardware driver for the enc28j60
47 enc28j60Init(mymac);
48 enc28j60clkout(2); // change clkout from 6.25MHz to 12.5MHz
49 _delay_loop_1(0); // 60us
50 enc28j60PhyWrite(PHLCON,0x476);
51 _delay_loop_1(0); // 60us
52
53 //init the ethernet/ip layer:
54 init_ip_arp_udp_tcp(mymac,myip,MYWWWPORT);
55
56 while(1){
57 // read packet, handle ping and wait for a tcp packet:
58 dat_p=packetloop_icmp_tcp(buf,
enc28j60PacketReceive(BUFFER_SIZE, buf));
59
60 /* dat_p will be unequal to zero if there is a valid http get */
61 if(dat_p==0){
62 // no http request
63 continue;
64 }
65 // tcp port 80 begin
66 if (strncmp("GET ",(char *)&(buf[dat_p]),4)!=0){
67 // head, post and other methods:
68 dat_p=http200ok();
69 dat_p=fill_tcp_data_p(buf,dat_p,
PSTR("<h1>200 OK</h1>"));
70 goto SENDTCP;
71 }
72 // just one web page in the "root directory" of the web server
73 if (strncmp("/ ",(char *)&(buf[dat_p+4]),2)==0){
74 dat_p=print_webpage(buf);
75 goto SENDTCP;
76 }else{
77 dat_p=fill_tcp_data_p(buf,0,PSTR("HTTP/1.0 401 Unauthorized
\r\nContent-Type: text/html\r\n\r\n<h1>401 Unauthorized</h1>"));
78 goto SENDTCP;
79 }
80 SENDTCP:
81 www_server_reply(buf,dat_p); // send web page data
82 // tcp port 80 end
83 }
84 return (0);
85 }
|