Jim Abraham / Mbed 2 deprecated SumpPumpMonitor-WIZwiki-W7500

Dependencies:   STATIC_COLORS WIZnetInterface mbed

Fork of HTTP_SDCard_File_Server_WIZwiki-W7500 by FOURNET Olivier

Revision:
10:e8dd198d1056
Parent:
9:d0615c97295e
Child:
11:ee62fd965f0a
--- a/main.cpp	Thu Jul 30 02:21:13 2015 +0000
+++ b/main.cpp	Sun May 08 00:51:53 2016 +0000
@@ -6,9 +6,9 @@
 
 
 #define MAC     "\x00\x08\xDC\x11\x34\x78"
-#define IP      "192.168.77.100"
+#define IP      "192.168.0.170"
 #define MASK    "255.255.255.0"
-#define GATEWAY "192.168.77.1"
+#define GATEWAY "192.168.0.254"
 
 #define HTTPD_SERVER_PORT   80
 #define HTTPD_MAX_REQ_LENGTH   1023
@@ -43,35 +43,50 @@
 
 Ticker ledTick;
 
+//char str[] = "This is a sample string";
+char *pch;
+char ext[5];
+char ext_gif[] = "gif";
+char ext_jpg[] = "jpg";
+char ext_png[] = "png";
+int pos_ext;
+int extLen;
+
 void ledTickfunc()
 {
     led1 = !led1;
 }
 
-
-
 void get_file(char* uri)
 {
+    int i, ext_j;
+    
     uart.printf("get_file %s\n", uri);
     char *lstchr = strrchr(uri, NULL) -1;
-    if ('/' == *lstchr) {
+    if('/' == *lstchr) 
+    {
         uart.printf("Open directory /sd%s\n", uri);
         *lstchr = 0;
         sprintf(fileName, "/sd%s", uri);
         DIR *d = opendir(fileName);
-        if (d != NULL) {
+        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 Directory Listing</h1><ul>", uri);
             client.send(httpHeader,strlen(httpHeader));
             struct dirent *p;
-            while((p = readdir(d)) != NULL) {
+            while((p = readdir(d)) != NULL)
+            {
                 sprintf(dirName, "%s/%s", fileName, p->d_name);
                 uart.printf("%s\n", dirName);
                 DIR *subDir = opendir(dirName);
-                if (subDir != NULL) {
+                if(subDir != NULL) 
+                {
                     sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name);
-                } else {
+                } 
+                else 
+                {
                     sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name);
                 }
                 client.send(httpHeader,strlen(httpHeader));
@@ -81,20 +96,73 @@
         uart.printf("Directory closed\n");
         sprintf(httpHeader,"</ul></body></html>");
         client.send(httpHeader,strlen(httpHeader));
-    } else {
+    } 
+    else 
+    {
         sprintf(fileName, "/sd%s", uri);
         fp = fopen(fileName, "r");
-        if (fp == NULL) {
+        if(fp == NULL)
+        {
             uart.printf("File not found\n");
             sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n");
             client.send(httpHeader,strlen(httpHeader));
             client.send(uri,strlen(uri));
-        } else {
-            uart.printf("Sending: header");
-            sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
+        } 
+        else 
+        {
+         pch = strrchr(fileName,'.');
+         
+         if( pch != NULL)
+         {
+          pos_ext = pch - fileName + 1;
+          extLen = strlen(fileName) - pos_ext;
+         
+          uart.printf("Last occurence of '.' found at %d , extLen : %d\n", pos_ext, extLen );
+        
+          for(i=0; i<extLen; i=i+1)
+          {
+           ext[i] = fileName[i+pos_ext];   
+          }
+         
+          ext[extLen] = '\0';
+         
+            uart.printf("Sending: header # %s\n", fileName);
+            uart.printf("extension %s\n", ext);
+            
+            if( strcmp(ext, ext_gif) == 0 ) ext_j = 1;
+            
+            if( strcmp(ext, ext_jpg) == 0 ) ext_j = 2;
+            
+            if( strcmp(ext, ext_png) == 0 ) ext_j = 3;         
+          }
+          else
+          {
+             uart.printf("Sending: header # %s\n", fileName);
+             ext_j = 0;
+          }
+            
+            switch(ext_j)
+            {
+             case 1: // HTTP reply with GIF image mime type
+             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: image/gif\r\nConnection: Close\r\n\r\n"); 
+             break;
+             
+             case 2: // HTTP reply with JPG image mime type
+             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: image/jpg\r\nConnection: Close\r\n\r\n");
+             break;
+             
+             case 3: // HTTP reply with PNG image mime type
+             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: image/jpg\r\nConnection: Close\r\n\r\n");
+             break;
+             
+             default: // HTTP reply with HTML mime type
+             sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");        
+            }
+                
             client.send(httpHeader,strlen(httpHeader));
             uart.printf(" file");
-            while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) {
+            while((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024)
+            {
                 client.send(buffer, rdCnt);
                 uart.printf(".");
             }
@@ -109,7 +177,7 @@
 {
     ledTick.attach(&ledTickfunc,0.5);
 //    Serial Interface eth;
-    uart.baud(115200);
+    uart.baud(9600);
     uart.printf("Initializing\n");
 
 //    Check File System