Quick and dirty CoOS + LWIP ( Webserver )

Dependencies:   mbed lwip

Committer:
astroboy
Date:
Sat Sep 10 22:41:10 2011 +0000
Revision:
0:94897d537b31

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
astroboy 0:94897d537b31 1 #include "lwip/opt.h"
astroboy 0:94897d537b31 2 #include "lwip/stats.h"
astroboy 0:94897d537b31 3 #include "lwip/sys.h"
astroboy 0:94897d537b31 4 #include "lwip/pbuf.h"
astroboy 0:94897d537b31 5 #include "lwip/udp.h"
astroboy 0:94897d537b31 6 #include "lwip/tcp.h"
astroboy 0:94897d537b31 7 #include "lwip/dns.h"
astroboy 0:94897d537b31 8 #include "lwip/dhcp.h"
astroboy 0:94897d537b31 9 #include "lwip/init.h"
astroboy 0:94897d537b31 10 #include "lwip/netif.h"
astroboy 0:94897d537b31 11 #include "netif/etharp.h"
astroboy 0:94897d537b31 12 #include "netif/loopif.h"
astroboy 0:94897d537b31 13 #include "device.h"
astroboy 0:94897d537b31 14
astroboy 0:94897d537b31 15 //#include "stdint.h"
astroboy 0:94897d537b31 16 #include "mbed.h"
astroboy 0:94897d537b31 17 #include "CoOS.h"
astroboy 0:94897d537b31 18
astroboy 0:94897d537b31 19 DigitalOut led1(LED1);
astroboy 0:94897d537b31 20 DigitalOut led2(LED2);
astroboy 0:94897d537b31 21 struct netif lpc17xx_netif;
astroboy 0:94897d537b31 22
astroboy 0:94897d537b31 23 /*---------------------------------- Define ---------------------------------*/
astroboy 0:94897d537b31 24 #define STK_SIZE 128 /*!< Stack size. */
astroboy 0:94897d537b31 25 #define INIT_TASK_PRIO 11 /*!< Priority of "init_task" task. */
astroboy 0:94897d537b31 26 #define A_TASK_PRIO 10 /*!< Priority of "A" task. */
astroboy 0:94897d537b31 27 #define B_TASK_PRIO 10 /*!< Priority of "B" task. */
astroboy 0:94897d537b31 28 #define CLK_TASK_PRIO 10 /*!< Priority of "clock" task. */
astroboy 0:94897d537b31 29
astroboy 0:94897d537b31 30 /*---------------------------------- Variable Define -------------------------*/
astroboy 0:94897d537b31 31 OS_STK init_task_stk[STK_SIZE]; /*!< The stack of "init_task" task. */
astroboy 0:94897d537b31 32 OS_STK a_task_stk[STK_SIZE]; /*!< The stack of "a" task. */
astroboy 0:94897d537b31 33 OS_STK b_task_stk[STK_SIZE]; /*!< The stack of "b" task. */
astroboy 0:94897d537b31 34 OS_STK clk_task_stk[STK_SIZE]; /*!< The stack of "clcok" task. */
astroboy 0:94897d537b31 35
astroboy 0:94897d537b31 36 OS_MutexID mut_LED; /*!< Mutex id related to LED. */
astroboy 0:94897d537b31 37
astroboy 0:94897d537b31 38 OS_FlagID a_flg;
astroboy 0:94897d537b31 39 OS_FlagID b_flg;
astroboy 0:94897d537b31 40 U8 errInfo[32]; /*!< Save error code. */
astroboy 0:94897d537b31 41
astroboy 0:94897d537b31 42 const char *page[] = {
astroboy 0:94897d537b31 43 "HTTP/1.1 200 OK\n"
astroboy 0:94897d537b31 44 "Server:mbed embedded\n"
astroboy 0:94897d537b31 45 "Content-Type: text/html\n"
astroboy 0:94897d537b31 46 "Content-Length: 190\n"
astroboy 0:94897d537b31 47 "\n\n"
astroboy 0:94897d537b31 48 "<html>\r\n"
astroboy 0:94897d537b31 49 " <header>\r\n"
astroboy 0:94897d537b31 50 " <title>Whats up?</title>\r\n"
astroboy 0:94897d537b31 51 " </header>\r\n"
astroboy 0:94897d537b31 52 " <body>\r\n"
astroboy 0:94897d537b31 53 " <h1>Hello World!!11elf</h1>\r\n"
astroboy 0:94897d537b31 54 " <p>Have a greate day.</p>\r\n"
astroboy 0:94897d537b31 55 " <a href=\"/1.html\">turn </a>\r\n"
astroboy 0:94897d537b31 56 " </body>\r\n"
astroboy 0:94897d537b31 57 "</html>\r\n"
astroboy 0:94897d537b31 58 "\n\n"
astroboy 0:94897d537b31 59 ,
astroboy 0:94897d537b31 60 "HTTP/1.1 200 OK\r\n"
astroboy 0:94897d537b31 61 "Server:mbed embedded\r\n"
astroboy 0:94897d537b31 62 "Content-Type: text/html\r\n"
astroboy 0:94897d537b31 63 "Content-Length: 178\r\n"
astroboy 0:94897d537b31 64 "\r\n"
astroboy 0:94897d537b31 65 "<html>\r\n"
astroboy 0:94897d537b31 66 " <header>\r\n"
astroboy 0:94897d537b31 67 " <title>LED Power</title>\r\n"
astroboy 0:94897d537b31 68 " </header>\r\n"
astroboy 0:94897d537b31 69 " <body>\r\n"
astroboy 0:94897d537b31 70 " <h1>Lets rotate!!11elf</h1>\r\n"
astroboy 0:94897d537b31 71 " <p>Bam!!!!!!!!!!.</p>\r\n"
astroboy 0:94897d537b31 72 " <a href=\"/\">back</a>\r\n"
astroboy 0:94897d537b31 73 " </body>\r\n"
astroboy 0:94897d537b31 74 "</html>\r\n"
astroboy 0:94897d537b31 75 "\r\n"
astroboy 0:94897d537b31 76 };
astroboy 0:94897d537b31 77
astroboy 0:94897d537b31 78
astroboy 0:94897d537b31 79 err_t recv_callback(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) {
astroboy 0:94897d537b31 80 int i,x;
astroboy 0:94897d537b31 81 char *data;
astroboy 0:94897d537b31 82
astroboy 0:94897d537b31 83 /* Check if status is ok and data is arrived. */
astroboy 0:94897d537b31 84 if (err == ERR_OK && p != NULL) {
astroboy 0:94897d537b31 85 /* Inform TCP that we have taken the data. */
astroboy 0:94897d537b31 86 tcp_recved(pcb, p->tot_len);
astroboy 0:94897d537b31 87 data = static_cast<char *>(p->payload);
astroboy 0:94897d537b31 88
astroboy 0:94897d537b31 89 /* If the data is a GET request we can handle it. */
astroboy 0:94897d537b31 90 if (strncmp(data, "GET ", 4) == 0) {
astroboy 0:94897d537b31 91 for (i = 0; i < 40; i++) {
astroboy 0:94897d537b31 92 if (((char *)data + 4)[i] == ' ' ||
astroboy 0:94897d537b31 93 ((char *)data + 4)[i] == '\r' ||
astroboy 0:94897d537b31 94 ((char *)data + 4)[i] == '\n') {
astroboy 0:94897d537b31 95 ((char *)data + 4)[i] = 0;
astroboy 0:94897d537b31 96 }
astroboy 0:94897d537b31 97 }
astroboy 0:94897d537b31 98
astroboy 0:94897d537b31 99 /* Check the URL */
astroboy 0:94897d537b31 100 /* We only know two URLs /1.html and default */
astroboy 0:94897d537b31 101 /* store the URL information in our global x variable to change twinkle behaviour. */
astroboy 0:94897d537b31 102 x = (strncmp((char *)(data + 4), "/1.html", 7) == 0)? 1: 0;
astroboy 0:94897d537b31 103 /* Clean up the Memory */
astroboy 0:94897d537b31 104 pbuf_free(p);
astroboy 0:94897d537b31 105
astroboy 0:94897d537b31 106 /* Send the right page as answere. */
astroboy 0:94897d537b31 107 if (tcp_write(pcb, (void *)page[x], strlen(page[x]), 1) != ERR_OK) {
astroboy 0:94897d537b31 108 //error("Could not write", 0);
astroboy 0:94897d537b31 109 }
astroboy 0:94897d537b31 110 } else {
astroboy 0:94897d537b31 111 /* No data arrived */
astroboy 0:94897d537b31 112 /* That means the client closes the connection and sent us a packet with FIN flag set to 1. */
astroboy 0:94897d537b31 113 /* We have to cleanup and destroy out TCPConnection. */
astroboy 0:94897d537b31 114 pbuf_free(p);
astroboy 0:94897d537b31 115 }
astroboy 0:94897d537b31 116 }
astroboy 0:94897d537b31 117
astroboy 0:94897d537b31 118 /* Don't panic! Everything is fine. */
astroboy 0:94897d537b31 119 return ERR_OK;
astroboy 0:94897d537b31 120 }
astroboy 0:94897d537b31 121
astroboy 0:94897d537b31 122 /* Accept an incomming call on the registered port */
astroboy 0:94897d537b31 123 err_t accept_callback(void *arg, struct tcp_pcb *npcb, err_t err) {
astroboy 0:94897d537b31 124 LWIP_UNUSED_ARG(arg);
astroboy 0:94897d537b31 125 /* Subscribe a receive callback function */
astroboy 0:94897d537b31 126 tcp_recv(npcb, &recv_callback);
astroboy 0:94897d537b31 127
astroboy 0:94897d537b31 128 /* Don't panic! Everything is fine. */
astroboy 0:94897d537b31 129 return ERR_OK;
astroboy 0:94897d537b31 130 }
astroboy 0:94897d537b31 131
astroboy 0:94897d537b31 132 void taskB (void *pdata) {
astroboy 0:94897d537b31 133 for (;;) {
astroboy 0:94897d537b31 134 led1 = !led1;
astroboy 0:94897d537b31 135 CoTickDelay(200);
astroboy 0:94897d537b31 136 }
astroboy 0:94897d537b31 137 }
astroboy 0:94897d537b31 138
astroboy 0:94897d537b31 139 void taskA (void *pdata) {
astroboy 0:94897d537b31 140 printf("starting task\n");
astroboy 0:94897d537b31 141 unsigned char x = 0;
astroboy 0:94897d537b31 142 struct ip_addr ipaddr, netmask, gw;
astroboy 0:94897d537b31 143 char *hostname = "Dichi";
astroboy 0:94897d537b31 144 struct netif *netif = &lpc17xx_netif;
astroboy 0:94897d537b31 145 //Ticker tickFast, tickSlow, tickARP, eth_tick, dns_tick, dhcp_coarse, dhcp_fine;
astroboy 0:94897d537b31 146
astroboy 0:94897d537b31 147 //lpc17xx_netif = (netif *)mem_malloc(sizeof(struct netif));
astroboy 0:94897d537b31 148
astroboy 0:94897d537b31 149 IP4_ADDR(&gw, 0,0,0,0);
astroboy 0:94897d537b31 150 IP4_ADDR(&ipaddr, 0,0,0,0);
astroboy 0:94897d537b31 151 IP4_ADDR(&netmask, 255,255,255,255);
astroboy 0:94897d537b31 152 lwip_init();
astroboy 0:94897d537b31 153
astroboy 0:94897d537b31 154 netif->hwaddr_len = ETHARP_HWADDR_LEN;
astroboy 0:94897d537b31 155 device_address((char *)netif->hwaddr);
astroboy 0:94897d537b31 156
astroboy 0:94897d537b31 157 netif = netif_add(netif, &ipaddr, &netmask, &gw, NULL, device_init, ip_input);
astroboy 0:94897d537b31 158 netif->hostname = hostname;
astroboy 0:94897d537b31 159 netif_set_default(netif);
astroboy 0:94897d537b31 160 netif->hostname = hostname;
astroboy 0:94897d537b31 161 netif_set_up(netif);
astroboy 0:94897d537b31 162 dhcp_start(netif);
astroboy 0:94897d537b31 163
astroboy 0:94897d537b31 164 struct tcp_pcb *pcb = tcp_new();
astroboy 0:94897d537b31 165 if (tcp_bind(pcb, IP_ADDR_ANY, 80) == ERR_OK) {
astroboy 0:94897d537b31 166 pcb = tcp_listen(pcb);
astroboy 0:94897d537b31 167 tcp_accept(pcb, &accept_callback);
astroboy 0:94897d537b31 168 }
astroboy 0:94897d537b31 169
astroboy 0:94897d537b31 170 printf("entering while loop\n");
astroboy 0:94897d537b31 171 for (;;) {
astroboy 0:94897d537b31 172 CoTickDelay(10);
astroboy 0:94897d537b31 173 device_poll();
astroboy 0:94897d537b31 174 if ( ++x % 100 == 0) {
astroboy 0:94897d537b31 175 etharp_tmr();
astroboy 0:94897d537b31 176 tcp_fasttmr();
astroboy 0:94897d537b31 177 tcp_slowtmr();
astroboy 0:94897d537b31 178 dns_tmr();
astroboy 0:94897d537b31 179 dhcp_coarse_tmr();
astroboy 0:94897d537b31 180 dhcp_fine_tmr();
astroboy 0:94897d537b31 181 x=0;
astroboy 0:94897d537b31 182 }
astroboy 0:94897d537b31 183 }
astroboy 0:94897d537b31 184 }
astroboy 0:94897d537b31 185
astroboy 0:94897d537b31 186
astroboy 0:94897d537b31 187
astroboy 0:94897d537b31 188 int main() {
astroboy 0:94897d537b31 189 //SystemInit();
astroboy 0:94897d537b31 190 CoInitOS();
astroboy 0:94897d537b31 191 CoCreateTask(taskA,0,A_TASK_PRIO,&a_task_stk[STK_SIZE-1],STK_SIZE);
astroboy 0:94897d537b31 192 CoCreateTask(taskB,0,B_TASK_PRIO,&b_task_stk[STK_SIZE-1],STK_SIZE);
astroboy 0:94897d537b31 193 CoStartOS();
astroboy 0:94897d537b31 194 while (1);
astroboy 0:94897d537b31 195 }
astroboy 0:94897d537b31 196