HTTP server for GSwifi see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/

Dependencies:   GSwifi mbed

Committer:
gsfan
Date:
Tue Feb 26 03:14:51 2013 +0000
Revision:
5:6f40b73257da
Parent:
4:29ecfd0f8723
fix;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gsfan 4:29ecfd0f8723 1 /*
gsfan 4:29ecfd0f8723 2 * HTTP server for GSwifi
gsfan 4:29ecfd0f8723 3 *
gsfan 4:29ecfd0f8723 4 * create the index.htm for LocalFileSystem.
gsfan 4:29ecfd0f8723 5 * html access http://IP address/ , http://IP address/test/
gsfan 4:29ecfd0f8723 6 *
gsfan 4:29ecfd0f8723 7 * CGI access http://IP address/cgi-bin/hoge?hage
gsfan 4:29ecfd0f8723 8 */
gsfan 4:29ecfd0f8723 9
gsfan 4:29ecfd0f8723 10 #include "mbed.h"
gsfan 4:29ecfd0f8723 11 #include "GSwifi.h"
gsfan 4:29ecfd0f8723 12
gsfan 4:29ecfd0f8723 13 #define PORT 80
gsfan 4:29ecfd0f8723 14
gsfan 4:29ecfd0f8723 15 #define SECURE GSwifi::GSSEC_WPA_PSK
gsfan 4:29ecfd0f8723 16 #define SSID "SSID"
gsfan 4:29ecfd0f8723 17 #define PASS "PASSPHRASE"
gsfan 4:29ecfd0f8723 18
gsfan 5:6f40b73257da 19 GSwifi gs(p13, p14, p20); // TX, RX, Reset (no flow control)
gsfan 5:6f40b73257da 20 //GSwifi gs(p13, p14, p12, P0_22, p20, NC, 115200); // TX, RX, CTS, RTS, Reset, Alarm
gsfan 4:29ecfd0f8723 21
gsfan 4:29ecfd0f8723 22 LocalFileSystem local("local");
gsfan 4:29ecfd0f8723 23 Serial pc(USBTX, USBRX);
gsfan 4:29ecfd0f8723 24 DigitalOut led1(LED1), led2(LED2);
gsfan 4:29ecfd0f8723 25
gsfan 4:29ecfd0f8723 26
gsfan 4:29ecfd0f8723 27 void cgi (int cid, GSwifi::GS_httpd *gshttpd) {
gsfan 4:29ecfd0f8723 28 int i;
gsfan 4:29ecfd0f8723 29
gsfan 4:29ecfd0f8723 30 led2 = 1;
gsfan 4:29ecfd0f8723 31 pc.printf("CGI %d: %s ? %s '%s' %d\r\n", cid, gshttpd->file, gshttpd->query, gshttpd->buf, gshttpd->len);
gsfan 4:29ecfd0f8723 32
gsfan 4:29ecfd0f8723 33 gs.send(cid, "HTTP/1.1 200 OK\r\n", 17);
gsfan 4:29ecfd0f8723 34 gs.send(cid, "Content-type: text/plain\r\n", 26);
gsfan 4:29ecfd0f8723 35 gs.send(cid, "\r\n", 2);
gsfan 4:29ecfd0f8723 36
gsfan 4:29ecfd0f8723 37 gs.send(cid, "REQUEST_METHOD: ", 16);
gsfan 4:29ecfd0f8723 38 if (gshttpd[cid].type == GSwifi::GSPROT_HTTPGET) {
gsfan 4:29ecfd0f8723 39 gs.send(cid, "GET\r\n", 5);
gsfan 4:29ecfd0f8723 40 } else {
gsfan 4:29ecfd0f8723 41 gs.send(cid, "POST\r\n", 6);
gsfan 4:29ecfd0f8723 42 }
gsfan 4:29ecfd0f8723 43 gs.send(cid, "SCRIPT_NAME: ", 13);
gsfan 4:29ecfd0f8723 44 gs.send(cid, gshttpd->file, strlen(gshttpd->file));
gsfan 4:29ecfd0f8723 45 gs.send(cid, "\r\n", 2);
gsfan 4:29ecfd0f8723 46 gs.send(cid, "QUERY_STRING: ", 14);
gsfan 4:29ecfd0f8723 47 gs.send(cid, gshttpd->query, strlen(gshttpd->query));
gsfan 4:29ecfd0f8723 48 gs.send(cid, "\r\n", 2);
gsfan 4:29ecfd0f8723 49 gs.send(cid, "POST_BODY: ", 11);
gsfan 4:29ecfd0f8723 50 gs.send(cid, gshttpd->buf, strlen(gshttpd->buf));
gsfan 4:29ecfd0f8723 51 gs.send(cid, "\r\n", 2);
gsfan 4:29ecfd0f8723 52
gsfan 4:29ecfd0f8723 53 }
gsfan 4:29ecfd0f8723 54
gsfan 4:29ecfd0f8723 55 int main () {
gsfan 4:29ecfd0f8723 56 IpAddr ipaddr, netmask, gateway, nameserver;
gsfan 4:29ecfd0f8723 57
gsfan 4:29ecfd0f8723 58 led1 = 1;
gsfan 4:29ecfd0f8723 59 pc.baud(115200);
gsfan 4:29ecfd0f8723 60
gsfan 4:29ecfd0f8723 61 pc.printf("connecting...\r\n");
gsfan 4:29ecfd0f8723 62 if (gs.connect(SECURE, SSID, PASS)) {
gsfan 4:29ecfd0f8723 63 return -1;
gsfan 4:29ecfd0f8723 64 }
gsfan 4:29ecfd0f8723 65 gs.getAddress(ipaddr, netmask, gateway, nameserver);
gsfan 4:29ecfd0f8723 66 pc.printf("ip %d.%d.%d.%d\r\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
gsfan 4:29ecfd0f8723 67
gsfan 4:29ecfd0f8723 68 led2 = 1;
gsfan 4:29ecfd0f8723 69 pc.printf("httpd\r\n");
gsfan 4:29ecfd0f8723 70 gs.httpd(PORT);
gsfan 4:29ecfd0f8723 71 gs.attach_httpd("/test/", "/local/");
gsfan 4:29ecfd0f8723 72 gs.attach_httpd("/cgi-bin/", &cgi);
gsfan 4:29ecfd0f8723 73 gs.attach_httpd("/", "/local/");
gsfan 4:29ecfd0f8723 74
gsfan 4:29ecfd0f8723 75 for (;;) {
gsfan 4:29ecfd0f8723 76 gs.poll();
gsfan 4:29ecfd0f8723 77
gsfan 4:29ecfd0f8723 78 wait_ms(50);
gsfan 4:29ecfd0f8723 79 led1 = !led1;
gsfan 4:29ecfd0f8723 80 led2 = 0;
gsfan 4:29ecfd0f8723 81 }
gsfan 4:29ecfd0f8723 82 }