Mitesh Patel
/
EthernetTester123
main.cpp@0:e1c5c3c4f93c, 2010-12-13 (annotated)
- Committer:
- mitesh2patel
- Date:
- Mon Dec 13 07:03:57 2010 +0000
- Revision:
- 0:e1c5c3c4f93c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mitesh2patel | 0:e1c5c3c4f93c | 1 | #include "mbed.h" |
mitesh2patel | 0:e1c5c3c4f93c | 2 | #include "lwip/opt.h" |
mitesh2patel | 0:e1c5c3c4f93c | 3 | #include "lwip/stats.h" |
mitesh2patel | 0:e1c5c3c4f93c | 4 | #include "lwip/sys.h" |
mitesh2patel | 0:e1c5c3c4f93c | 5 | #include "lwip/pbuf.h" |
mitesh2patel | 0:e1c5c3c4f93c | 6 | #include "lwip/udp.h" |
mitesh2patel | 0:e1c5c3c4f93c | 7 | #include "lwip/tcp.h" |
mitesh2patel | 0:e1c5c3c4f93c | 8 | #include "lwip/dns.h" |
mitesh2patel | 0:e1c5c3c4f93c | 9 | #include "lwip/dhcp.h" |
mitesh2patel | 0:e1c5c3c4f93c | 10 | #include "lwip/init.h" |
mitesh2patel | 0:e1c5c3c4f93c | 11 | #include "lwip/netif.h" |
mitesh2patel | 0:e1c5c3c4f93c | 12 | #include "netif/etharp.h" |
mitesh2patel | 0:e1c5c3c4f93c | 13 | #include "netif/loopif.h" |
mitesh2patel | 0:e1c5c3c4f93c | 14 | #include "device.h" |
mitesh2patel | 0:e1c5c3c4f93c | 15 | #include "rpc.h" |
mitesh2patel | 0:e1c5c3c4f93c | 16 | Serial pc(USBTX, USBRX); |
mitesh2patel | 0:e1c5c3c4f93c | 17 | |
mitesh2patel | 0:e1c5c3c4f93c | 18 | LocalFileSystem fs("fs"); |
mitesh2patel | 0:e1c5c3c4f93c | 19 | |
mitesh2patel | 0:e1c5c3c4f93c | 20 | Ethernet ethernet; |
mitesh2patel | 0:e1c5c3c4f93c | 21 | DigitalOut ledLink(p30); |
mitesh2patel | 0:e1c5c3c4f93c | 22 | DigitalOut ledActivity(p29); |
mitesh2patel | 0:e1c5c3c4f93c | 23 | DigitalOut ledStage0 (LED1); |
mitesh2patel | 0:e1c5c3c4f93c | 24 | DigitalOut ledStage1 (LED2); |
mitesh2patel | 0:e1c5c3c4f93c | 25 | DigitalOut ledStage2 (LED3); |
mitesh2patel | 0:e1c5c3c4f93c | 26 | DigitalOut ledTCP80 (LED4); |
mitesh2patel | 0:e1c5c3c4f93c | 27 | |
mitesh2patel | 0:e1c5c3c4f93c | 28 | volatile char stage = 0; |
mitesh2patel | 0:e1c5c3c4f93c | 29 | |
mitesh2patel | 0:e1c5c3c4f93c | 30 | Ticker stage_blinker; |
mitesh2patel | 0:e1c5c3c4f93c | 31 | |
mitesh2patel | 0:e1c5c3c4f93c | 32 | struct netif netif_data; |
mitesh2patel | 0:e1c5c3c4f93c | 33 | |
mitesh2patel | 0:e1c5c3c4f93c | 34 | const char testPage[] = "HTTP/1.1 200 OK\r\n" |
mitesh2patel | 0:e1c5c3c4f93c | 35 | "Content-Type: text/html\r\n" |
mitesh2patel | 0:e1c5c3c4f93c | 36 | "Connection: Close\r\n\r\n" |
mitesh2patel | 0:e1c5c3c4f93c | 37 | "<html>" |
mitesh2patel | 0:e1c5c3c4f93c | 38 | "<head>" |
mitesh2patel | 0:e1c5c3c4f93c | 39 | "<title>mbed test page</title>" |
mitesh2patel | 0:e1c5c3c4f93c | 40 | "<style type='text/css'>" |
mitesh2patel | 0:e1c5c3c4f93c | 41 | "body{font-family:'Arial, sans-serif', sans-serif;font-size:.8em;background-color:#fff;}" |
mitesh2patel | 0:e1c5c3c4f93c | 42 | "</style>" |
mitesh2patel | 0:e1c5c3c4f93c | 43 | "</head>" |
mitesh2patel | 0:e1c5c3c4f93c | 44 | "<body>%s" |
mitesh2patel | 0:e1c5c3c4f93c | 45 | "<br><br>" |
mitesh2patel | 0:e1c5c3c4f93c | 46 | |
mitesh2patel | 0:e1c5c3c4f93c | 47 | "<button> LED1</button>" |
mitesh2patel | 0:e1c5c3c4f93c | 48 | "<A HREF='test1.html'> test1</A>" |
mitesh2patel | 0:e1c5c3c4f93c | 49 | "<a href='http://mbed.html/'>mbed</a>" |
mitesh2patel | 0:e1c5c3c4f93c | 50 | "<A HREF='http://www.google.com'> google</A>" |
mitesh2patel | 0:e1c5c3c4f93c | 51 | "<A HREF='PIC1234.html'> PIC1234</A>" |
mitesh2patel | 0:e1c5c3c4f93c | 52 | "<A HREF='http://192.168.1.3/PIC1234.html'> PIC1</A>" |
mitesh2patel | 0:e1c5c3c4f93c | 53 | "<A HREF='http://192.168.1.2'>refresh</A>" |
mitesh2patel | 0:e1c5c3c4f93c | 54 | |
mitesh2patel | 0:e1c5c3c4f93c | 55 | "<div id='my_area' align='center'>" |
mitesh2patel | 0:e1c5c3c4f93c | 56 | "<IMG SRC='JPPV0000.jpg'>" |
mitesh2patel | 0:e1c5c3c4f93c | 57 | "<div id='my_area' align='center'>" |
mitesh2patel | 0:e1c5c3c4f93c | 58 | "<IMG SRC='JPPV0000.BMP'>" |
mitesh2patel | 0:e1c5c3c4f93c | 59 | " <div id='my_area' align='center'>" |
mitesh2patel | 0:e1c5c3c4f93c | 60 | "<IMG SRC='JPPV0000.GIF'>" |
mitesh2patel | 0:e1c5c3c4f93c | 61 | " <div id='my_area' align='center'>" |
mitesh2patel | 0:e1c5c3c4f93c | 62 | "<IMG SRC='fs/JPPV0000.GIF'>" |
mitesh2patel | 0:e1c5c3c4f93c | 63 | " <div id='my_area' align='center'>" |
mitesh2patel | 0:e1c5c3c4f93c | 64 | "<IMG SRC='fs/JPPV0000.GIF'>" |
mitesh2patel | 0:e1c5c3c4f93c | 65 | |
mitesh2patel | 0:e1c5c3c4f93c | 66 | "<META HTTP-EQUIV='Refresh'CONTENT='60; URL=192.168.1.2'>" |
mitesh2patel | 0:e1c5c3c4f93c | 67 | "</body></html>\r\n\r\n"; |
mitesh2patel | 0:e1c5c3c4f93c | 68 | |
mitesh2patel | 0:e1c5c3c4f93c | 69 | |
mitesh2patel | 0:e1c5c3c4f93c | 70 | /* const char testPage[]= "HTTP/1.1 200 OK\r\n" |
mitesh2patel | 0:e1c5c3c4f93c | 71 | "Content-Type: text/html\r\n" |
mitesh2patel | 0:e1c5c3c4f93c | 72 | "Connection: Close\r\n\r\n" |
mitesh2patel | 0:e1c5c3c4f93c | 73 | "<html><head>" |
mitesh2patel | 0:e1c5c3c4f93c | 74 | "<script src='mbedRPC.js' language='javascript'></script>" |
mitesh2patel | 0:e1c5c3c4f93c | 75 | "<script type='text/javascript'>" |
mitesh2patel | 0:e1c5c3c4f93c | 76 | "mbed = new HTTPRPC();" |
mitesh2patel | 0:e1c5c3c4f93c | 77 | "led1 = new DigitalOut(mbed, LED1);" |
mitesh2patel | 0:e1c5c3c4f93c | 78 | "ain = new AnalogIn(mbed, p20);" |
mitesh2patel | 0:e1c5c3c4f93c | 79 | "led2 = new PwmOut(mbed, LED3);" |
mitesh2patel | 0:e1c5c3c4f93c | 80 | "function print(str){document.getElementById('t').innerHTML = str;}" |
mitesh2patel | 0:e1c5c3c4f93c | 81 | "</script></head><body>%s" |
mitesh2patel | 0:e1c5c3c4f93c | 82 | "<p>" |
mitesh2patel | 0:e1c5c3c4f93c | 83 | "<a href='http://mbed.org/cookbook/Interfacing-using-JavaScript'> for more information goto the cookbook </a>" |
mitesh2patel | 0:e1c5c3c4f93c | 84 | "<br>" |
mitesh2patel | 0:e1c5c3c4f93c | 85 | "</p><div id='t'><br/></div>" |
mitesh2patel | 0:e1c5c3c4f93c | 86 | |
mitesh2patel | 0:e1c5c3c4f93c | 87 | |
mitesh2patel | 0:e1c5c3c4f93c | 88 | "<script language='javascript'>" |
mitesh2patel | 0:e1c5c3c4f93c | 89 | "var i = 1;" |
mitesh2patel | 0:e1c5c3c4f93c | 90 | "var x = 0;" |
mitesh2patel | 0:e1c5c3c4f93c | 91 | "function tick(){" |
mitesh2patel | 0:e1c5c3c4f93c | 92 | "ledStage0=~ledStage0" |
mitesh2patel | 0:e1c5c3c4f93c | 93 | "led1.write(i);" |
mitesh2patel | 0:e1c5c3c4f93c | 94 | "if(i == 0){i = 1;" |
mitesh2patel | 0:e1c5c3c4f93c | 95 | "}else{i = 0;}" |
mitesh2patel | 0:e1c5c3c4f93c | 96 | "led2.write(x);" |
mitesh2patel | 0:e1c5c3c4f93c | 97 | "x = x + 0.1;" |
mitesh2patel | 0:e1c5c3c4f93c | 98 | "if(x >= 1) x = 0;" |
mitesh2patel | 0:e1c5c3c4f93c | 99 | "f = ain.read();" |
mitesh2patel | 0:e1c5c3c4f93c | 100 | "print(f);" |
mitesh2patel | 0:e1c5c3c4f93c | 101 | "}" |
mitesh2patel | 0:e1c5c3c4f93c | 102 | "setInterval('tick()',1000);" |
mitesh2patel | 0:e1c5c3c4f93c | 103 | "</script>" |
mitesh2patel | 0:e1c5c3c4f93c | 104 | "</body>" |
mitesh2patel | 0:e1c5c3c4f93c | 105 | "</html>\r\n\r\n"; */ |
mitesh2patel | 0:e1c5c3c4f93c | 106 | |
mitesh2patel | 0:e1c5c3c4f93c | 107 | |
mitesh2patel | 0:e1c5c3c4f93c | 108 | |
mitesh2patel | 0:e1c5c3c4f93c | 109 | char buffer[1024]; |
mitesh2patel | 0:e1c5c3c4f93c | 110 | char temp[1024]; |
mitesh2patel | 0:e1c5c3c4f93c | 111 | err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) { |
mitesh2patel | 0:e1c5c3c4f93c | 112 | struct netif *netif = &netif_data; |
mitesh2patel | 0:e1c5c3c4f93c | 113 | //ledTCP80 = true; |
mitesh2patel | 0:e1c5c3c4f93c | 114 | printf("TCP callback from %d.%d.%d.%d\r\n", ip4_addr1(&(pcb->remote_ip)),ip4_addr2(&(pcb->remote_ip)),ip4_addr3(&(pcb->remote_ip)),ip4_addr4(&(pcb->remote_ip))); |
mitesh2patel | 0:e1c5c3c4f93c | 115 | char *data; |
mitesh2patel | 0:e1c5c3c4f93c | 116 | /* Check if status is ok and data is arrived. */ |
mitesh2patel | 0:e1c5c3c4f93c | 117 | if (err == ERR_OK && p != NULL) { |
mitesh2patel | 0:e1c5c3c4f93c | 118 | /* Inform TCP that we have taken the data. */ |
mitesh2patel | 0:e1c5c3c4f93c | 119 | tcp_recved(pcb, p->tot_len); |
mitesh2patel | 0:e1c5c3c4f93c | 120 | data = static_cast<char *>(p->payload); |
mitesh2patel | 0:e1c5c3c4f93c | 121 | /* If the data is a GET request we can handle it. */ |
mitesh2patel | 0:e1c5c3c4f93c | 122 | if (strncmp(data, "GET ", 4) == 0) { |
mitesh2patel | 0:e1c5c3c4f93c | 123 | printf("Handling GET request...\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 124 | printf("Request:\r\n%s\r\n", data); |
mitesh2patel | 0:e1c5c3c4f93c | 125 | |
mitesh2patel | 0:e1c5c3c4f93c | 126 | //generate the test page |
mitesh2patel | 0:e1c5c3c4f93c | 127 | time_t seconds = time(NULL); |
mitesh2patel | 0:e1c5c3c4f93c | 128 | sprintf(temp, "<h1>Congratulations!</h1>If you can see this page, your mbed is working properly." |
mitesh2patel | 0:e1c5c3c4f93c | 129 | "<h2>mbed Configuration</h2>" |
mitesh2patel | 0:e1c5c3c4f93c | 130 | "mbed RTC time:%s<br/>" |
mitesh2patel | 0:e1c5c3c4f93c | 131 | "mbed HW address: %02x:%02x:%02x:%02x:%02x:%02x<br/>" |
mitesh2patel | 0:e1c5c3c4f93c | 132 | "mbed IP Address: %s<br/>" |
mitesh2patel | 0:e1c5c3c4f93c | 133 | "image1: <div id='my_area' align='center'><IMG SRC='fs\JPPV0000.GIF'><br/>" |
mitesh2patel | 0:e1c5c3c4f93c | 134 | "image2: <div id='my_area' align='center'><IMG SRC='JPPV0000.GIF'>" |
mitesh2patel | 0:e1c5c3c4f93c | 135 | "image2: <div id='my_area' align='center'><IMG SRC='JPPV0000.BMP'>" , |
mitesh2patel | 0:e1c5c3c4f93c | 136 | |
mitesh2patel | 0:e1c5c3c4f93c | 137 | |
mitesh2patel | 0:e1c5c3c4f93c | 138 | ctime(&seconds), |
mitesh2patel | 0:e1c5c3c4f93c | 139 | (char*) netif->hwaddr[0], |
mitesh2patel | 0:e1c5c3c4f93c | 140 | (char*) netif->hwaddr[1], |
mitesh2patel | 0:e1c5c3c4f93c | 141 | (char*) netif->hwaddr[2], |
mitesh2patel | 0:e1c5c3c4f93c | 142 | (char*) netif->hwaddr[3], |
mitesh2patel | 0:e1c5c3c4f93c | 143 | (char*) netif->hwaddr[4], |
mitesh2patel | 0:e1c5c3c4f93c | 144 | (char*) netif->hwaddr[5], |
mitesh2patel | 0:e1c5c3c4f93c | 145 | inet_ntoa(*(struct in_addr*)&(netif->ip_addr)) |
mitesh2patel | 0:e1c5c3c4f93c | 146 | ); |
mitesh2patel | 0:e1c5c3c4f93c | 147 | |
mitesh2patel | 0:e1c5c3c4f93c | 148 | sprintf(buffer, testPage, temp); |
mitesh2patel | 0:e1c5c3c4f93c | 149 | if (tcp_write(pcb, (void *)buffer, strlen(buffer), 1) == ERR_OK) { |
mitesh2patel | 0:e1c5c3c4f93c | 150 | tcp_output(pcb); |
mitesh2patel | 0:e1c5c3c4f93c | 151 | printf("Closing connection...\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 152 | tcp_close(pcb); |
mitesh2patel | 0:e1c5c3c4f93c | 153 | } |
mitesh2patel | 0:e1c5c3c4f93c | 154 | } |
mitesh2patel | 0:e1c5c3c4f93c | 155 | else |
mitesh2patel | 0:e1c5c3c4f93c | 156 | { |
mitesh2patel | 0:e1c5c3c4f93c | 157 | printf("Non GET request...\r\nRequest:\r\n%s\r\n", data); |
mitesh2patel | 0:e1c5c3c4f93c | 158 | } |
mitesh2patel | 0:e1c5c3c4f93c | 159 | |
mitesh2patel | 0:e1c5c3c4f93c | 160 | pbuf_free(p); |
mitesh2patel | 0:e1c5c3c4f93c | 161 | } |
mitesh2patel | 0:e1c5c3c4f93c | 162 | |
mitesh2patel | 0:e1c5c3c4f93c | 163 | else { |
mitesh2patel | 0:e1c5c3c4f93c | 164 | /* No data arrived */ |
mitesh2patel | 0:e1c5c3c4f93c | 165 | /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */ |
mitesh2patel | 0:e1c5c3c4f93c | 166 | /* We have to cleanup and destroy out TCPConnection. */ |
mitesh2patel | 0:e1c5c3c4f93c | 167 | printf("Connection closed by client.\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 168 | pbuf_free(p); |
mitesh2patel | 0:e1c5c3c4f93c | 169 | } |
mitesh2patel | 0:e1c5c3c4f93c | 170 | /* Don't panic! Everything is fine. */ |
mitesh2patel | 0:e1c5c3c4f93c | 171 | // ledTCP80 = false; |
mitesh2patel | 0:e1c5c3c4f93c | 172 | return ERR_OK; |
mitesh2patel | 0:e1c5c3c4f93c | 173 | } |
mitesh2patel | 0:e1c5c3c4f93c | 174 | /* Accept an incomming call on the registered port */ |
mitesh2patel | 0:e1c5c3c4f93c | 175 | err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) { |
mitesh2patel | 0:e1c5c3c4f93c | 176 | LWIP_UNUSED_ARG(arg); |
mitesh2patel | 0:e1c5c3c4f93c | 177 | /* Subscribe a receive callback function */ |
mitesh2patel | 0:e1c5c3c4f93c | 178 | tcp_recv(npcb, &recv_callback); |
mitesh2patel | 0:e1c5c3c4f93c | 179 | /* Don't panic! Everything is fine. */ |
mitesh2patel | 0:e1c5c3c4f93c | 180 | return ERR_OK; |
mitesh2patel | 0:e1c5c3c4f93c | 181 | } |
mitesh2patel | 0:e1c5c3c4f93c | 182 | |
mitesh2patel | 0:e1c5c3c4f93c | 183 | void stageblinker() |
mitesh2patel | 0:e1c5c3c4f93c | 184 | { |
mitesh2patel | 0:e1c5c3c4f93c | 185 | switch (stage) |
mitesh2patel | 0:e1c5c3c4f93c | 186 | { |
mitesh2patel | 0:e1c5c3c4f93c | 187 | case 0: |
mitesh2patel | 0:e1c5c3c4f93c | 188 | //ledStage0 = !ledStage0; |
mitesh2patel | 0:e1c5c3c4f93c | 189 | ledStage1 = false; |
mitesh2patel | 0:e1c5c3c4f93c | 190 | //ledStage2 = false; |
mitesh2patel | 0:e1c5c3c4f93c | 191 | break; |
mitesh2patel | 0:e1c5c3c4f93c | 192 | case 1: |
mitesh2patel | 0:e1c5c3c4f93c | 193 | // ledStage0 = true; |
mitesh2patel | 0:e1c5c3c4f93c | 194 | ledStage1 = !ledStage1; |
mitesh2patel | 0:e1c5c3c4f93c | 195 | |
mitesh2patel | 0:e1c5c3c4f93c | 196 | //ledStage2 = false; |
mitesh2patel | 0:e1c5c3c4f93c | 197 | break; |
mitesh2patel | 0:e1c5c3c4f93c | 198 | case 2: |
mitesh2patel | 0:e1c5c3c4f93c | 199 | // ledStage0 = true; |
mitesh2patel | 0:e1c5c3c4f93c | 200 | ledStage1 = true; |
mitesh2patel | 0:e1c5c3c4f93c | 201 | //ledStage2 = true; |
mitesh2patel | 0:e1c5c3c4f93c | 202 | stage_blinker.detach(); |
mitesh2patel | 0:e1c5c3c4f93c | 203 | break; |
mitesh2patel | 0:e1c5c3c4f93c | 204 | } |
mitesh2patel | 0:e1c5c3c4f93c | 205 | } |
mitesh2patel | 0:e1c5c3c4f93c | 206 | |
mitesh2patel | 0:e1c5c3c4f93c | 207 | int main() { |
mitesh2patel | 0:e1c5c3c4f93c | 208 | printf("mBed Ethernet Tester 1.0\r\nStarting Up...\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 209 | stage = 0; |
mitesh2patel | 0:e1c5c3c4f93c | 210 | struct netif *netif = &netif_data; |
mitesh2patel | 0:e1c5c3c4f93c | 211 | struct ip_addr ipaddr; |
mitesh2patel | 0:e1c5c3c4f93c | 212 | struct ip_addr netmask; |
mitesh2patel | 0:e1c5c3c4f93c | 213 | struct ip_addr gateway; |
mitesh2patel | 0:e1c5c3c4f93c | 214 | Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine; |
mitesh2patel | 0:e1c5c3c4f93c | 215 | stage_blinker.attach_us(&stageblinker, 1000*500); |
mitesh2patel | 0:e1c5c3c4f93c | 216 | |
mitesh2patel | 0:e1c5c3c4f93c | 217 | char *hostname = "my-mbed"; |
mitesh2patel | 0:e1c5c3c4f93c | 218 | |
mitesh2patel | 0:e1c5c3c4f93c | 219 | |
mitesh2patel | 0:e1c5c3c4f93c | 220 | Base::add_rpc_class<AnalogIn>(); |
mitesh2patel | 0:e1c5c3c4f93c | 221 | Base::add_rpc_class<AnalogOut>(); |
mitesh2patel | 0:e1c5c3c4f93c | 222 | Base::add_rpc_class<DigitalIn>(); |
mitesh2patel | 0:e1c5c3c4f93c | 223 | Base::add_rpc_class<DigitalOut>(); |
mitesh2patel | 0:e1c5c3c4f93c | 224 | Base::add_rpc_class<DigitalInOut>(); |
mitesh2patel | 0:e1c5c3c4f93c | 225 | Base::add_rpc_class<PwmOut>(); |
mitesh2patel | 0:e1c5c3c4f93c | 226 | Base::add_rpc_class<Timer>(); |
mitesh2patel | 0:e1c5c3c4f93c | 227 | Base::add_rpc_class<SPI>(); |
mitesh2patel | 0:e1c5c3c4f93c | 228 | Base::add_rpc_class<BusOut>(); |
mitesh2patel | 0:e1c5c3c4f93c | 229 | Base::add_rpc_class<BusIn>(); |
mitesh2patel | 0:e1c5c3c4f93c | 230 | Base::add_rpc_class<BusInOut>(); |
mitesh2patel | 0:e1c5c3c4f93c | 231 | Base::add_rpc_class<Serial>(); |
mitesh2patel | 0:e1c5c3c4f93c | 232 | // receive commands, and send back the responses |
mitesh2patel | 0:e1c5c3c4f93c | 233 | char buf[256], outbuf[256]; |
mitesh2patel | 0:e1c5c3c4f93c | 234 | |
mitesh2patel | 0:e1c5c3c4f93c | 235 | printf("Configuring device for DHCP...\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 236 | /* Start Network with DHCP */ |
mitesh2patel | 0:e1c5c3c4f93c | 237 | IP4_ADDR(&netmask, 255,255,255,0); |
mitesh2patel | 0:e1c5c3c4f93c | 238 | IP4_ADDR(&gateway, 192,168,1,3); |
mitesh2patel | 0:e1c5c3c4f93c | 239 | IP4_ADDR(&ipaddr, 192,168,1,3); |
mitesh2patel | 0:e1c5c3c4f93c | 240 | /* Initialise after configuration */ |
mitesh2patel | 0:e1c5c3c4f93c | 241 | lwip_init(); |
mitesh2patel | 0:e1c5c3c4f93c | 242 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
mitesh2patel | 0:e1c5c3c4f93c | 243 | device_address((char *)netif->hwaddr); |
mitesh2patel | 0:e1c5c3c4f93c | 244 | netif = netif_add(netif, &ipaddr, &netmask, &gateway, NULL, device_init, ip_input); |
mitesh2patel | 0:e1c5c3c4f93c | 245 | netif->hostname = hostname; |
mitesh2patel | 0:e1c5c3c4f93c | 246 | netif_set_default(netif); |
mitesh2patel | 0:e1c5c3c4f93c | 247 | dhcp_start(netif); // <-- Use DHCP |
mitesh2patel | 0:e1c5c3c4f93c | 248 | |
mitesh2patel | 0:e1c5c3c4f93c | 249 | /* Initialise all needed timers */ |
mitesh2patel | 0:e1c5c3c4f93c | 250 | tickARP.attach_us( ðarp_tmr, ARP_TMR_INTERVAL * 1000); |
mitesh2patel | 0:e1c5c3c4f93c | 251 | tickFast.attach_us(&tcp_fasttmr, TCP_FAST_INTERVAL * 1000); |
mitesh2patel | 0:e1c5c3c4f93c | 252 | tickSlow.attach_us(&tcp_slowtmr, TCP_SLOW_INTERVAL * 1000); |
mitesh2patel | 0:e1c5c3c4f93c | 253 | dns_tick.attach_us(&dns_tmr, DNS_TMR_INTERVAL * 1000); |
mitesh2patel | 0:e1c5c3c4f93c | 254 | dhcp_coarse.attach_us(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_MSECS * 1000); |
mitesh2patel | 0:e1c5c3c4f93c | 255 | dhcp_fine.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); |
mitesh2patel | 0:e1c5c3c4f93c | 256 | stage = 1; |
mitesh2patel | 0:e1c5c3c4f93c | 257 | while (!netif_is_up(netif)) { |
mitesh2patel | 0:e1c5c3c4f93c | 258 | ledLink = ethernet.link(); |
mitesh2patel | 0:e1c5c3c4f93c | 259 | device_poll(); |
mitesh2patel | 0:e1c5c3c4f93c | 260 | } |
mitesh2patel | 0:e1c5c3c4f93c | 261 | |
mitesh2patel | 0:e1c5c3c4f93c | 262 | /* |
mitesh2patel | 0:e1c5c3c4f93c | 263 | while (!(netif->dhcp->state == DHCP_BOUND || netif->dhcp->state == DHCP_PERMANENT)) |
mitesh2patel | 0:e1c5c3c4f93c | 264 | { |
mitesh2patel | 0:e1c5c3c4f93c | 265 | ledLink = ethernet.link(); |
mitesh2patel | 0:e1c5c3c4f93c | 266 | device_poll(); |
mitesh2patel | 0:e1c5c3c4f93c | 267 | //printf("Waiting for DHCP response, state = %d\r\n", netif->dhcp->state); |
mitesh2patel | 0:e1c5c3c4f93c | 268 | //wait_ms(100); |
mitesh2patel | 0:e1c5c3c4f93c | 269 | } |
mitesh2patel | 0:e1c5c3c4f93c | 270 | */ |
mitesh2patel | 0:e1c5c3c4f93c | 271 | stage = 2; |
mitesh2patel | 0:e1c5c3c4f93c | 272 | printf("Interface is up, local IP is %s\r\n", |
mitesh2patel | 0:e1c5c3c4f93c | 273 | inet_ntoa(*(struct in_addr*)&(netif->ip_addr))); |
mitesh2patel | 0:e1c5c3c4f93c | 274 | |
mitesh2patel | 0:e1c5c3c4f93c | 275 | printf("Starting Web Server...\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 276 | |
mitesh2patel | 0:e1c5c3c4f93c | 277 | /* Bind a function to a tcp port */ |
mitesh2patel | 0:e1c5c3c4f93c | 278 | struct tcp_pcb *pcb = tcp_new(); |
mitesh2patel | 0:e1c5c3c4f93c | 279 | if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) { |
mitesh2patel | 0:e1c5c3c4f93c | 280 | pcb = tcp_listen(pcb); |
mitesh2patel | 0:e1c5c3c4f93c | 281 | tcp_accept(pcb, &accept_callback); |
mitesh2patel | 0:e1c5c3c4f93c | 282 | } |
mitesh2patel | 0:e1c5c3c4f93c | 283 | |
mitesh2patel | 0:e1c5c3c4f93c | 284 | printf("Waiting for connection...\r\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 285 | /* while (netif_is_up(netif)) { |
mitesh2patel | 0:e1c5c3c4f93c | 286 | ledLink = ethernet.link(); |
mitesh2patel | 0:e1c5c3c4f93c | 287 | device_poll(); |
mitesh2patel | 0:e1c5c3c4f93c | 288 | }*/ |
mitesh2patel | 0:e1c5c3c4f93c | 289 | /* while(1) { |
mitesh2patel | 0:e1c5c3c4f93c | 290 | device_poll(); |
mitesh2patel | 0:e1c5c3c4f93c | 291 | ledLink = ethernet.link(); |
mitesh2patel | 0:e1c5c3c4f93c | 292 | }*/ |
mitesh2patel | 0:e1c5c3c4f93c | 293 | |
mitesh2patel | 0:e1c5c3c4f93c | 294 | |
mitesh2patel | 0:e1c5c3c4f93c | 295 | FILE *fp = fopen("/fs/pic1234.htm","w"); // Open local filename |
mitesh2patel | 0:e1c5c3c4f93c | 296 | // it ignores path and date defaults 1/1/2008 becausse RTC not set |
mitesh2patel | 0:e1c5c3c4f93c | 297 | // if I call ithe localfilesystem www rather than 'local' it writes and is seen when drive is refreshed |
mitesh2patel | 0:e1c5c3c4f93c | 298 | fprintf(fp, "<title> cam test page </title>\n"); |
mitesh2patel | 0:e1c5c3c4f93c | 299 | fprintf(fp,"<h1>pic of the day from camera</h1>"); |
mitesh2patel | 0:e1c5c3c4f93c | 300 | //fprintf(fp, "volts %f V at %s \r\n",Voltmeter.read(), ctime(&ctTime)); |
mitesh2patel | 0:e1c5c3c4f93c | 301 | fprintf(fp,"<div id='my_area' align='center'>"); |
mitesh2patel | 0:e1c5c3c4f93c | 302 | fprintf(fp, "<IMG SRC='JPPV0000.jpg'>"); |
mitesh2patel | 0:e1c5c3c4f93c | 303 | fprintf(fp,"<div id='my_area' align='center'>"); |
mitesh2patel | 0:e1c5c3c4f93c | 304 | fprintf(fp, "<IMG SRC='JPPV0000.gif'>"); |
mitesh2patel | 0:e1c5c3c4f93c | 305 | fprintf(fp, "</a>"); |
mitesh2patel | 0:e1c5c3c4f93c | 306 | fclose(fp); |
mitesh2patel | 0:e1c5c3c4f93c | 307 | |
mitesh2patel | 0:e1c5c3c4f93c | 308 | while(1) { |
mitesh2patel | 0:e1c5c3c4f93c | 309 | device_poll(); |
mitesh2patel | 0:e1c5c3c4f93c | 310 | ledLink = ethernet.link(); |
mitesh2patel | 0:e1c5c3c4f93c | 311 | // pc.gets(buf, 256); |
mitesh2patel | 0:e1c5c3c4f93c | 312 | // rpc(buf, outbuf); |
mitesh2patel | 0:e1c5c3c4f93c | 313 | // pc.printf("%s\n", outbuf); |
mitesh2patel | 0:e1c5c3c4f93c | 314 | |
mitesh2patel | 0:e1c5c3c4f93c | 315 | } |
mitesh2patel | 0:e1c5c3c4f93c | 316 | |
mitesh2patel | 0:e1c5c3c4f93c | 317 | |
mitesh2patel | 0:e1c5c3c4f93c | 318 | } |