/***************************************************************************** * * Project : lwIp Web * Subproject : * Name : main.c * Function : Main loop * Designer : K. Sterckx * Creation date : 22/01/2007 * Compiler : GNU ARM * Processor : LPC2368 * Last update : * Last updated by : * History : * ***************************************************************************** * * Called from startarm.s * ****************************************************************************/ #include #include "LPC23xx.H" #include "portlcd.h" #include "ticker.h" #include "lwip\opt.h" #include "lwip\stats.h" #include "lwip\sys.h" #include "lwip\pbuf.h" #include "lwip\udp.h" #include "lwip\tcp.h" #if LWIP_SNMP #include "lwip/snmp.h" #include "lwip/snmp_msg.h" #endif //LWIP_SNMP #include "netif\etharp.h" #include "netif\loopif.h" #include "arch\ethernetif.h" #include "arch\clock.h" static unsigned char lcd_text[2][16+1] = {"lwIPWeb " __TIME__, /* Buffer for LCD text */ __DATE__ }; static struct netif netif_eth0_data; static struct netif* netif_eth0 = &netif_eth0_data; static struct ip_addr my_ipaddr_data; static struct ip_addr my_netmask_data; static struct ip_addr my_gw_data; static struct ip_addr my_snmp_dest; static u32_t last_arp_time; static u32_t last_tcpslow_time; static u32_t last_tcpfast_time; static u32_t last_led_time; static u32_t last_led_state; #if LWIP_SNMP static u32_t last_snmp_time; #endif static char webpage[] = "HTTP/1.0 200 OK\r\n\ Content-type: text/html\r\n\ \r\n\ \ lwIP Web \ \

lwIP Web

\

This is a website hosted by the embedded Webserver lwIP WEB.

\

Hardware:

\ \ \ "; static void lwip_init(void) { stats_init(); sys_init(); mem_init(); memp_init(); pbuf_init(); etharp_init(); ip_init(); udp_init(); #if LWIP_SNMP // start SNMP snmp_init(); #endif // LWIP_SNMP tcp_init(); IP4_ADDR(&my_ipaddr_data, 192, 168, 2, 15); IP4_ADDR(&my_netmask_data, 255, 255, 255, 0); IP4_ADDR(&my_gw_data, 192, 168, 2, 254); netif_add(netif_eth0, &my_ipaddr_data, &my_netmask_data, &my_gw_data, NULL, ethernetif_init, ip_input); netif_set_default(netif_eth0); netif_set_up(netif_eth0); } err_t http_recv_callback(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) { char* rq; if (p != NULL) { rq = p->payload; if (rq[0] == 'G' && rq[1] == 'E' && rq[2] == 'T' && rq[3] == ' ' && rq[4] == '/') { u32_t bytestosend; u32_t* newarg; bytestosend = sizeof(webpage); newarg = (u32_t*) mem_malloc(sizeof(u32_t)); *newarg = bytestosend; if (tcp_write(tpcb, webpage, sizeof(webpage), 0) != ERR_OK) { mem_free(newarg); tcp_close(tpcb); } else { tcp_arg(tpcb, newarg); } } tcp_recved(tpcb, p->len); pbuf_free(p); } return ERR_OK; } err_t http_poll_callback(void *arg, struct tcp_pcb *tpcb) { if (arg != NULL) { u32_t bytestosend = *((u32_t*) arg); if (bytestosend == 0) { mem_free(arg); tcp_arg(tpcb, NULL); tcp_close(tpcb); } } return ERR_OK; } err_t http_send_callback(void *arg, struct tcp_pcb *tpcb, u16_t len) { u32_t bytestosend; if (arg != NULL) { bytestosend = *((u32_t*) arg); if ((bytestosend - len) >= 0) bytestosend -= len; else bytestosend = 0; *((u32_t*) arg) = bytestosend; } return ERR_OK; } err_t http_accept_callback(void *arg, struct tcp_pcb *newpcb, err_t err) { tcp_arg(newpcb, NULL); tcp_recv(newpcb, http_recv_callback); tcp_sent(newpcb, http_send_callback); tcp_poll(newpcb, http_poll_callback, 1); return ERR_OK; } static void http_init(void) { struct tcp_pcb* tcpweb; struct tcp_pcb* tcpweb_listen; tcpweb = tcp_new(); if (tcpweb == NULL) return; /* Bind to port 80 for any address */ if (tcp_bind(tcpweb, IP_ADDR_ANY, 80) != ERR_OK) return; tcpweb_listen = tcp_listen(tcpweb); if (tcpweb_listen == NULL) { tcp_abort(tcpweb); tcpweb = NULL; return; } tcpweb = tcpweb_listen; tcp_accept(tcpweb, http_accept_callback); } #if LWIP_SNMP const u8_t SYSCONTACT[] = "bob@commnetsystems.com"; const u8_t SIZE_SYSCONTACT = sizeof(SYSCONTACT) - 1; const u8_t SYSLOC[] = "Monroe Site"; const u8_t SIZE_SYSLOC = sizeof(SYSLOC) - 1; const u8_t AUTHENTRAPS[] = "1234"; #endif //LWIP_SNMP int main (void) { PINSEL10 = 0; FIO2DIR = 0x000000FF; FIO2MASK = 0x00000000; FIO2CLR = 0xFF; /* Update LCD Module display text. */ LCD_cls(); LCD_puts(lcd_text[0]); LCD_gotoxy(1,2); LCD_puts(lcd_text[1]); #if LWIP_SNMP // setup SNMP stuff snmp_set_syscontact(SYSCONTACT, &SIZE_SYSCONTACT); snmp_set_syslocation(SYSLOC, &SIZE_SYSLOC); snmp_set_snmpenableauthentraps(AUTHENTRAPS); IP4_ADDR(&my_snmp_dest, 192, 168, 2, 4); snmp_trap_dst_enable(0, 1); snmp_trap_dst_ip_set(0, &my_snmp_dest); #endif lwip_init(); http_init(); TICKER_Start(); while(1) { ethernetif_handlepackets(netif_eth0); if (clock() - last_arp_time == ETHARP_TMR_INTERVAL * CLOCK_MS) { etharp_tmr(); last_arp_time = clock(); } if (clock() - last_tcpfast_time == TCP_FAST_INTERVAL * CLOCK_MS) { last_tcpfast_time = clock(); tcp_fasttmr(); } if (clock() - last_tcpslow_time == TCP_SLOW_INTERVAL * CLOCK_MS) { last_tcpslow_time = clock(); tcp_slowtmr(); } if (clock() - last_led_time == 1000 * CLOCK_MS) { last_led_time = clock(); if (last_led_state) { last_led_state = 0; FIO2CLR = 0xFF; } else { last_led_state = 1; FIO2SET = 0xFF; } } #if LWIP_SNMP { // should be updated "exactly" every 10ms u32_t curr_time; // so need to store time (so we don't get err) curr_time = clock(); if(curr_time - last_snmp_time == 10 * CLOCK_MS){ last_snmp_time = curr_time; snmp_inc_sysuptime(); } } #endif // LWIP_SNMP } }