HTTP SD Card File Server

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Fork of mURI_HTTPserver by Greg Steiert

Files at this revision

API Documentation at this revision

Comitter:
gsteiert
Date:
Sat Apr 19 05:26:40 2014 +0000
Parent:
2:5c9ce3b50de6
Child:
4:6e648db29edf
Commit message:
Initial Release of HTTP SD Card File Server

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
muri.cpp Show diff for this revision Revisions of this file
muri.h Show diff for this revision Revisions of this file
--- a/SDFileSystem.lib	Fri Dec 06 03:59:01 2013 +0000
+++ b/SDFileSystem.lib	Sat Apr 19 05:26:40 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
+http://mbed.org/users/mbed_official/code/SDFileSystem/#7b35d1709458
--- a/main.cpp	Fri Dec 06 03:59:01 2013 +0000
+++ b/main.cpp	Sat Apr 19 05:26:40 2014 +0000
@@ -1,7 +1,6 @@
 #include "mbed.h"
 #include "EthernetInterface.h"
 #include "SDFileSystem.h"
-#include "muri.h"
 #include <stdio.h>
 #include <string.h>
 
@@ -9,20 +8,21 @@
 #define HTTPD_MAX_REQ_LENGTH   1023
 #define HTTPD_MAX_HDR_LENGTH   255
 #define HTTPD_MAX_FNAME_LENGTH   127
-#define MURI_MAX_RESP_LENGTH  127
-#define I2C_BUFFER_SIZE 31
+#define HTTPD_MAX_DNAME_LENGTH   127
+
+Serial uart(USBTX, USBRX);
 
-SDFileSystem sd(p5, p6, p7, p8, "sd"); //
-// I2C i2c(p28, p27);
+//SDFileSystem sd(p5, p6, p7, p8, "sd"); // LPC1768 MBD2PMD
+SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD
+
 EthernetInterface eth;
 TCPSocketServer server;
 TCPSocketConnection client;
 
 char buffer[HTTPD_MAX_REQ_LENGTH+1];
 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
-char filename[HTTPD_MAX_FNAME_LENGTH+1];
-char obuf[MURI_MAX_RESP_LENGTH+1];
-char data[I2C_BUFFER_SIZE];
+char fileName[HTTPD_MAX_FNAME_LENGTH+1];
+char dirName[HTTPD_MAX_DNAME_LENGTH+1];
 char *uristr;
 char *eou;
 char *qrystr;
@@ -32,32 +32,38 @@
 
 void get_file(char* uri)
 {
-    printf("get_file %s\n", uri);
+    uart.printf("get_file %s\n", uri);
     char *lstchr = strrchr(uri, NULL) -1;
     if ('/' == *lstchr) {
-        printf("Open directory /sd%s\n", uri);
+        uart.printf("Open directory /sd%s\n", uri);
         *lstchr = 0;
-        sprintf(filename, "/sd%s", uri);
-        DIR *d = opendir(filename);
+        sprintf(fileName, "/sd%s", uri);
+        DIR *d = opendir(fileName);
         if (d != NULL) {
             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
             client.send(httpHeader,strlen(httpHeader));
-            sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s</h1><ul>", uri);
+            sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri);
             client.send(httpHeader,strlen(httpHeader));
             struct dirent *p;
             while((p = readdir(d)) != NULL) {
-                printf("%s\n", p->d_name);
-                sprintf(httpHeader,"<li>%s</li>", p->d_name);
+                sprintf(dirName, "%s/%s", fileName, p->d_name);
+                uart.printf("%s\n", dirName);
+                DIR *subDir = opendir(dirName);
+                if (subDir != NULL) {
+                    sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
+                } else {
+                    sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
+                }
                 client.send(httpHeader,strlen(httpHeader));
             }
         }
         closedir(d);
-        printf("Directory closed\n");
+        uart.printf("Directory closed\n");
         sprintf(httpHeader,"</ul></body></html>");
         client.send(httpHeader,strlen(httpHeader));
     } else {
-        sprintf(filename, "/sd%s", uri);
-        fp = fopen(filename, "r");
+        sprintf(fileName, "/sd%s", uri);
+        fp = fopen(fileName, "r");
         if (fp == NULL) {
             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
             client.send(httpHeader,strlen(httpHeader));
@@ -74,50 +80,40 @@
     }
 }
 
-void get_cgi(char* uri)
-{
-    char *result;
-    muri(uri, data, obuf);
-    if (!strncmp(obuf, "200 ", 4)) {
-        result = obuf +4;
-        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
-        client.send(httpHeader,strlen(httpHeader));
-        client.send(result,strlen(result));
-        printf("result:  %s", result);
-    } else {
-        sprintf(httpHeader,"HTTP/1.1 ");
-        client.send(httpHeader,strlen(httpHeader));
-        client.send(obuf,strlen(obuf));
-        sprintf(httpHeader,"\r\n");
-        client.send(httpHeader,strlen(httpHeader)); 
-    }
-    
-}
-
-
 int main (void)
 {
+//    Serial Interface eth;
+    uart.baud(115200);
+    uart.printf("RAPM Serial Started\n");
+
+//    Check File System
+    DIR *d = opendir("/sd/");
+    if (d != NULL) {
+        uart.printf("SD Card Present\n");
+    } else {
+        uart.printf("SD Card Root Directory Not Found\n");
+    }
+
 //    EthernetInterface eth;
     eth.init(); //Use DHCP
     eth.connect();
-    printf("IP Address is %s\n", eth.getIPAddress());
+    uart.printf("IP Address is %s\n", eth.getIPAddress());
 
 //    TCPSocketServer server;
     server.bind(HTTPD_SERVER_PORT);
     server.listen();
-    
-    init_dio(); //initialize pmd digital IO
+    uart.printf("RAPM Server Listening\n");
 
     while (true) {
-        printf("\nWait for new connection...\r\n");
+        uart.printf("\nWait for new connection...\r\n");
         server.accept(client);
         client.set_blocking(false, 1500); // Timeout after (1.5)s
 
-        printf("Connection from: %s\r\n", client.get_address());
+        uart.printf("Connection from: %s\r\n", client.get_address());
         while (true) {
             int n = client.receive(buffer, sizeof(buffer));
             if (n <= 0) break;
-            printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
+            uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer);
             if (n >= 1024) {
                 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
                 client.send(httpHeader,strlen(httpHeader));
@@ -135,11 +131,7 @@
                     client.send(buffer,n);
                 } else {
                     *eou = 0;
-                    if (!strncmp(uristr, "/muri/", 6)) {
-                        get_cgi(uristr+6);
-                    } else {
-                        get_file(uristr);
-                    }
+                    get_file(uristr);
                 }
             }
         }
--- a/mbed-rtos.lib	Fri Dec 06 03:59:01 2013 +0000
+++ b/mbed-rtos.lib	Sat Apr 19 05:26:40 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed-rtos/#869ef732a8a2
+http://mbed.org/users/mbed_official/code/mbed-rtos/#4ef72665e2c8
--- a/mbed.bld	Fri Dec 06 03:59:01 2013 +0000
+++ b/mbed.bld	Sat Apr 19 05:26:40 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/9c8f0e3462fb
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/6473597d706e
\ No newline at end of file
--- a/muri.cpp	Fri Dec 06 03:59:01 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,327 +0,0 @@
-#include "mbed.h"
-#include "rtos.h"
-#include "muri.h"
-
-I2C i2c(p28, p27);
-//PwmOut pwm1(p26);
-PwmOut pwm(p25);
-DigitalInOut pmd1(p14);
-DigitalInOut pmd2(p11);
-DigitalInOut pmd3(p12);
-DigitalInOut pmd4(p13);
-DigitalInOut pmd7(p24);
-DigitalInOut pmd8(p23);
-DigitalInOut pmd9(p22);
-DigitalInOut pmd10(p21);
-
-osTimerDef(poff, pwm_off);
-osTimerId off_timer;
-
-void init_dio()
-{
-    pmd1.mode(PullNone);
-    pmd1.mode(OpenDrain);
-    pmd1.write(1);
-    pmd1.output();
-    pmd2.mode(PullNone);
-    pmd2.mode(OpenDrain);
-    pmd2.write(1);
-    pmd2.output();
-    pmd3.mode(PullNone);
-    pmd3.mode(OpenDrain);
-    pmd3.write(1);
-    pmd3.output();
-    pmd4.mode(PullNone);
-    pmd4.mode(OpenDrain);
-    pmd4.write(1);
-    pmd4.output();
-    pmd7.mode(PullNone);
-    pmd7.mode(OpenDrain);
-    pmd7.write(1);
-    pmd7.output();
-    pmd8.mode(PullNone);
-    pmd8.mode(OpenDrain);
-    pmd8.write(1);
-    pmd8.output();
-    pmd9.mode(PullNone);
-    pmd9.mode(OpenDrain);
-    pmd9.write(1);
-    pmd9.output();
-    pmd10.mode(PullNone);
-    pmd10.mode(OpenDrain);
-    pmd10.write(1);
-    pmd10.output();
-    off_timer = osTimerCreate(osTimer(poff), osTimerOnce, NULL);
-}
-
-void cmd_i2c_write(char* qry, char* data, char* resp)
-{
-    int addr = 0;
-    int dlen = 0;
-    int dtmp = 0;
-    if (sscanf(qry, "?%2x", &addr) == 1) {
-        qry += 3;
-        while (sscanf(qry, "&%2x", &dtmp) == 1) {
-            data[dlen] = dtmp;
-            qry +=3;
-            dlen +=1;
-        }
-    } else {
-        dlen = -1;
-    }
-    if (dlen > 0 ) {
-        i2c.write(addr, data, dlen);
-        sprintf(resp,"200 %d\r\n", dlen);
-    } else {
-        if (dlen == 0) {
-            sprintf(resp,"204 No data to write: %s\r\n", qry);
-        } else {
-            sprintf(resp,"400 Invalid parameters: %s\r\n", qry);
-
-        }
-    }
-}
-
-void cmd_i2c_read(char* qry, char* data, char* resp)
-{
-    int addr = 0;
-    int dlen = 0;
-    int dcnt = 0;
-    if (sscanf(qry, "?%2x", &addr) == 1) {
-        qry += 3;
-        if (sscanf(qry, "&%2x", &dlen) == 1) {
-            if (i2c.read(addr, data, dlen)!=0) {
-                dlen = -1;
-                sprintf(resp, "500 I2C read failed\r\n");
-            }
-        } else {
-            sprintf(resp, "400 Invalid data length: %s\r\n", qry);
-        }
-    } else {
-        dlen = -1;
-        sprintf(resp, "400 Invalid address: %s\r\n", qry);
-    }
-    if (dlen > 0) {
-        sprintf(resp,"200");
-        resp += 3;
-        for (dcnt = 0; dcnt < dlen; dcnt++) {
-            sprintf(resp," %02x", data[dcnt]);
-            resp +=3;
-        }
-    }
-}
-
-void cmd_i2c_addr_read(char* qry, char* data, char* resp)
-{
-    int addr = 0;
-    int radd = 0;
-    int dlen = 0;
-    int dcnt = 0;
-    if (sscanf(qry, "?%2x", &addr) == 1) {
-        qry += 3;
-        if (sscanf(qry, "&%2x", &radd) == 1) {
-            qry += 3;
-            if (sscanf(qry, "&%2x", &dlen) == 1) {
-                data[0] = radd;
-                i2c.write(addr, data, 1, false);
-                if (i2c.read(addr, data, dlen)!=0) {
-                    dlen = -1;
-                    sprintf(resp, "500 I2C read failed\r\n");
-                }
-            } else {
-                sprintf(resp, "400 Invalid data length: %s\r\n", qry);
-            }
-        } else {
-            dlen = -1;
-            sprintf(resp, "400 Invalid register address: %s\r\n", qry);
-        }
-    } else {
-        dlen = -1;
-        sprintf(resp, "400 Invalid address: %s\r\n", qry);
-    }
-    if (dlen > 0) {
-        sprintf(resp,"200");
-        resp += 3;
-        for (dcnt = 0; dcnt < dlen; dcnt++) {
-            sprintf(resp," %02x", data[dcnt]);
-            resp +=3;
-        }
-    }
-}
-
-void cmd_pmd_setio(char* qry, char* data, char* resp)
-{
-    int pdir = -1;
-    int pdata = -1;
-    if (sscanf(qry, "?%2x", &pdir) == 1) {
-        if (pdir == 0) {
-            pmd1.mode(PullNone);
-            pmd2.mode(PullNone);
-            pmd3.mode(PullNone);
-            pmd4.mode(PullNone);
-            pmd7.mode(PullNone);
-            pmd8.mode(PullNone);
-            pmd9.mode(PullNone);
-            pmd10.mode(PullNone);
-        } else {
-            if (sscanf(qry+3, "&%2x", &pdata) == 1) {
-                if (pdir & (1 << 0)) {
-                    if (pdata & (1 << 0)) pmd1.mode(PullUp);
-                    else pmd1.mode(PullDown);
-                }  else {
-                    pmd1.mode(PullNone);
-                }
-                if (pdir & (1 << 1)) {
-                    if (pdata & (1 << 1)) pmd2.mode(PullUp);
-                    else pmd2.mode(PullDown);
-                }  else {
-                    pmd2.mode(PullNone);
-                }
-                if (pdir & (1 << 2)) {
-                    if (pdata & (1 << 2)) pmd3.mode(PullUp);
-                    else pmd3.mode(PullDown);
-                }  else {
-                    pmd3.mode(PullNone);
-                }
-                if (pdir & (1 << 3)) {
-                    if (pdata & (1 << 3)) pmd4.mode(PullUp);
-                    else pmd4.mode(PullDown);
-                }  else {
-                    pmd4.mode(PullNone);
-                }
-                if (pdir & (1 << 4)) {
-                    if (pdata & (1 << 4)) pmd7.mode(PullUp);
-                    else pmd7.mode(PullDown);
-                }  else {
-                    pmd7.mode(PullNone);
-                }
-                if (pdir & (1 << 5)) {
-                    if (pdata & (1 << 5)) pmd8.mode(PullUp);
-                    else pmd8.mode(PullDown);
-                }  else {
-                    pmd8.mode(PullNone);
-                }
-                if (pdir & (1 << 6)) {
-                    if (pdata & (1 << 6)) pmd9.mode(PullUp);
-                    else pmd9.mode(PullDown);
-                }  else {
-                    pmd9.mode(PullNone);
-                }
-                if (pdir & (1 << 7)) {
-                    if (pdata & (1 << 7)) pmd10.mode(PullUp);
-                    else pmd10.mode(PullDown);
-                }  else {
-                    pmd10.mode(PullNone);
-                }
-            }
-        }
-
-        pdata = 0;
-        pdata += (pmd1 << 0);
-        pdata += (pmd2 << 1);
-        pdata += (pmd3 << 2);
-        pdata += (pmd4 << 3);
-        pdata += (pmd7 << 4);
-        pdata += (pmd8 << 5);
-        pdata += (pmd9 << 6);
-        pdata += (pmd10 << 7);
-        sprintf(resp,"200 %02x", pdata);
-    } else {
-        sprintf(resp,"400 Invalid parameters: %s\r\n", qry);
-    }
-}
-
-void cmd_pmd_write(char* qry, char* data, char* resp)
-{
-    int pdata = -1;
-    if (sscanf(qry, "?%2x", &pdata) == 1) {
-        pmd1 = pdata & (1 << 0);
-        pmd2 = pdata & (1 << 1);
-        pmd3 = pdata & (1 << 2);
-        pmd4 = pdata & (1 << 3);
-        pmd7 = pdata & (1 << 4);
-        pmd8 = pdata & (1 << 5);
-        pmd9 = pdata & (1 << 6);
-        pmd10 = pdata & (1 << 7);
-        pdata = 0;
-        pdata += (pmd1 << 0);
-        pdata += (pmd2 << 1);
-        pdata += (pmd3 << 2);
-        pdata += (pmd4 << 3);
-        pdata += (pmd7 << 4);
-        pdata += (pmd8 << 5);
-        pdata += (pmd9 << 6);
-        pdata += (pmd10 << 7);
-        sprintf(resp,"200 %02x", pdata);
-    } else {
-        sprintf(resp,"400 Invalid parameters: %s\r\n", qry);
-
-    }
-}
-
-void cmd_pmd_read(char* qry, char* data, char* resp)
-{
-    int pdata = 0;
-    pdata += (pmd1 << 0);
-    pdata += (pmd2 << 1);
-    pdata += (pmd3 << 2);
-    pdata += (pmd4 << 3);
-    pdata += (pmd7 << 4);
-    pdata += (pmd8 << 5);
-    pdata += (pmd9 << 6);
-    pdata += (pmd10 << 7);
-    sprintf(resp,"200 %02x", pdata);
-}
-
-void pwm_off(void const *arg)
-{
-//        pwm.period_us(0);
-    pwm.pulsewidth_us(0);
-}
-
-void cmd_pwm_set(char* qry, char* data, char* resp)
-{
-    int pwmP = 0;
-    int pwmW = 0;
-    int pwmD = 0;
-    if (sscanf(qry, "?%4x&%4x&%4x", &pwmP, &pwmW, &pwmD) == 3) {
-        pwm.period_us(pwmP);
-        pwm.pulsewidth_us(pwmW);
-        osTimerStart(off_timer, pwmD);
-        sprintf(resp,"200 %04x %04x %04x", pwmP, pwmW, pwmD);
-    } else {
-        sprintf(resp,"400 Invalid parameters: %s\r\n", qry);
-
-    }
-}
-
-void muri(char* uri, char* data, char* resp)
-{
-//    sprintf(resp, "501 %s", uri);
-//}
-//void get_cgi(char* uri, char* qry)
-//{
-    char *qry;
-    printf("muri uri:  %s\n", uri);
-    qry = strstr(uri, "?");
-    if (!strncmp(uri, "i2cw?", 5)) { // i2cw [addr] [data] [data] [data] ...
-        cmd_i2c_write(qry, data, resp);
-    } else if (!strncmp(uri, "i2cr?", 5)) { // i2cr [addr] [count]
-        cmd_i2c_read(qry, data, resp);
-    } else if (!strncmp(uri, "i2car?", 6)) { // i2car [addr] [radd] [count]
-        cmd_i2c_addr_read(qry, data, resp);
-    } else if (!strncmp(uri, "pmdio?", 6)) { // pmdio [dir] [data]
-        cmd_pmd_setio(qry, data, resp);
-    } else if (!strncmp(uri, "pmdw?", 5)) { // pmdw [data]
-        cmd_pmd_write(qry, data, resp);
-    } else if (!strncmp(uri, "pmdr", 4)) { // pmdr
-        cmd_pmd_read(qry, data, resp);
-    } else if (!strncmp(uri, "pwmd?", 5)) { // pwmd [period] [width] [duration]
-        cmd_pwm_set(qry, data, resp);
-    } else {
-        sprintf(resp, "501 %s", uri);
-    }
-    printf("muri resp:  %s\n", resp);
-}
-
-
--- a/muri.h	Fri Dec 06 03:59:01 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-
-//I2C i2c(p28, p27);
-
-void init_dio();
-void pwm_off(void const *arg);
-
-void cmd_i2c_write(char* qry, char* data, char* resp);
-void cmd_i2c_read(char* qry, char* data, char* resp);
-void cmd_i2c_addr_read(char* qry, char* data, char* resp);
-void cmd_pmd_setio(char* qry, char* data, char* resp);
-void cmd_pmd_write(char* qry, char* data, char* resp);
-void cmd_pmd_read(char* qry, char* data, char* resp);
-void cmd_pwm_set(char* qry, char* data, char* resp);
-void muri(char* uri, char* data, char* resp);
-