HTTP sdcard file server WIZwiki-W7500ECO

Dependencies:   SDFileSystem WIZnetInterface mbed

Fork of HTTP_SDCard_File_Server_WIZwiki-W7500ECO by Bohyun Bang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "SDFileSystem.h"
00004 #include <stdio.h>
00005 #include <string.h>
00006 
00007 
00008 #define MAC     "\x00\x08\xDC\x11\x34\x78"
00009 #define IP      "192.168.123.100"
00010 #define MASK    "255.255.255.0"
00011 #define GATEWAY "192.168.123.1"
00012 
00013 #define HTTPD_SERVER_PORT   80
00014 #define HTTPD_MAX_REQ_LENGTH   1023
00015 #define HTTPD_MAX_HDR_LENGTH   255
00016 #define HTTPD_MAX_FNAME_LENGTH   127
00017 #define HTTPD_MAX_DNAME_LENGTH   127
00018 
00019 Serial uart(USBTX, USBRX);
00020 
00021 //SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
00022 //SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
00023 //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // K64F
00024 SDFileSystem sd(PB_03, PB_02, PB_01, PB_00, "sd"); // WIZwiki-W7500ECO
00025 
00026 EthernetInterface eth;
00027 TCPSocketServer server;
00028 TCPSocketConnection client;
00029 
00030 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00031 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00032 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00033 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00034 char *uristr;
00035 char *eou;
00036 char *qrystr;
00037 
00038 FILE *fp;
00039 int rdCnt;
00040 
00041 DigitalOut led1(LED1); //server listning status
00042 DigitalOut led2(LED2); //socket connecting status
00043 
00044 Ticker ledTick;
00045 
00046 void ledTickfunc()
00047 {
00048     led1 = !led1;
00049 }
00050 
00051 
00052 
00053 void get_file(char* uri)
00054 {
00055     uart.printf("get_file %s\n", uri);
00056     char *lstchr = strrchr(uri, NULL) -1;
00057     if ('/' == *lstchr) {
00058         uart.printf("Open directory /sd%s\n", uri);
00059         *lstchr = 0;
00060         sprintf(fileName, "/sd%s", uri);
00061         DIR *d = opendir(fileName);
00062         if (d != NULL) {
00063             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00064             client.send(httpHeader,strlen(httpHeader));
00065             sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
00066             client.send(httpHeader,strlen(httpHeader));
00067             struct dirent *p;
00068             while((p = readdir(d)) != NULL) {
00069                 sprintf(dirName, "%s/%s", fileName, p->d_name);
00070                 uart.printf("%s\n", dirName);
00071                 DIR *subDir = opendir(dirName);
00072                 if (subDir != NULL) {
00073                     sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
00074                 } else {
00075                     sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
00076                 }
00077                 client.send(httpHeader,strlen(httpHeader));
00078             }
00079         }
00080         closedir(d);
00081         uart.printf("Directory closed\n");
00082         sprintf(httpHeader,"</ul></body></html>");
00083         client.send(httpHeader,strlen(httpHeader));
00084     } else {
00085         sprintf(fileName, "/sd%s", uri);
00086         fp = fopen(fileName, "r");
00087         if (fp == NULL) {
00088             uart.printf("File not found\n");
00089             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00090             client.send(httpHeader,strlen(httpHeader));
00091             client.send(uri,strlen(uri));
00092         } else {
00093             uart.printf("Sending: header");
00094             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00095             client.send(httpHeader,strlen(httpHeader));
00096             uart.printf(" file");
00097             while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
00098                 client.send(buffer, rdCnt);
00099                 uart.printf(".");
00100             }
00101             client.send(buffer, rdCnt);
00102             fclose(fp);
00103             uart.printf("done\n");
00104         }
00105     }
00106 }
00107 
00108 int main (void)
00109 {
00110     ledTick.attach(&ledTickfunc,0.5);
00111 //    Serial Interface eth;
00112     uart.baud(115200);
00113     uart.printf("Initializing\n");
00114 
00115 //    Check File System
00116     uart.printf("Checking File System\n");
00117     DIR *d = opendir("/sd/");
00118     if (d != NULL) {
00119         uart.printf("SD Card Present\n");
00120     } else {
00121         uart.printf("SD Card Root Directory Not Found\n");
00122     }
00123 
00124 //    EthernetInterface eth;
00125     uart.printf("Initializing Ethernet\n");
00126     //eth.init(); //Use DHCP
00127     //eth.init((uint8_t*)MAC);    // Use DHCP for WIZnetInterface
00128     eth.init((uint8_t*)MAC,IP,MASK,GATEWAY);  //IP,mask,Gateway
00129     uart.printf("Connecting\n");
00130     eth.connect();
00131     uart.printf("IP Address is %s\n", eth.getIPAddress());
00132 
00133 //    TCPSocketServer server;
00134     server.bind(HTTPD_SERVER_PORT);
00135     server.listen();
00136     uart.printf("Server Listening\n");
00137 
00138     while (true) {
00139         uart.printf("\nWait for new connection...\r\n");
00140         server.accept(client);
00141         client.set_blocking(false, 1500); // Timeout after (1.5)s
00142 
00143         uart.printf("Connection from: %s\r\n", client.get_address());
00144         while (true) {
00145             led2 = true;
00146             int n = client.receive(buffer, sizeof(buffer));
00147             if (n <= 0) break;
00148             uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
00149             if (n >= 1024) {
00150                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00151                 client.send(httpHeader,strlen(httpHeader));
00152                 client.send(buffer,n);
00153                 break;
00154             } else {
00155                 buffer[n]=0;
00156             }
00157             if (!strncmp(buffer, "GET ", 4)) {
00158                 uristr = buffer + 4;
00159                 eou = strstr(uristr, " ");
00160                 if (eou == NULL) {
00161                     sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
00162                     client.send(httpHeader,strlen(httpHeader));
00163                     client.send(buffer,n);
00164                 } else {
00165                     *eou = 0;
00166                     get_file(uristr);
00167                 }
00168             }
00169         }
00170         led2 = false;
00171         client.close();
00172     }
00173 }