embedded Web site ,files store on SD card

Dependencies:   sd-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "TCPServer.h"
00005 #include "TCPSocket.h"
00006 #include "SDBlockDevice.h"
00007 #include "FATFileSystem.h" 
00008 #include <stdio.h>
00009 #include <string.h>
00010 #define HTTPD_SERVER_PORT   80
00011 #define HTTPD_MAX_REQ_LENGTH   1024
00012 #define HTTPD_MAX_HDR_LENGTH   255
00013 #define HTTPD_MAX_FNAME_LENGTH   127
00014 #define HTTPD_MAX_DNAME_LENGTH   127
00015 static const char*          mbedIp       = "192.168.11.102";  //IP
00016 static const char*          mbedMask     = "255.255.255.0";  // Mask
00017 static const char*          mbedGateway  = "192.168.11.1";    //Gateway
00018 char buffer[HTTPD_MAX_REQ_LENGTH+1];
00019 char httpHeader[HTTPD_MAX_HDR_LENGTH+1];
00020 char fileName[HTTPD_MAX_FNAME_LENGTH+1];
00021 char dirName[HTTPD_MAX_DNAME_LENGTH+1];
00022 char *uristr;
00023 char *eou;
00024 char *qrystr;
00025 DigitalOut myled(PF_12);
00026 TCPServer server;
00027 TCPSocket client;
00028 SocketAddress client_addr;
00029 SDBlockDevice sd(PC_12,PC_11,PC_10,PC_9,40000000,40000000);
00030  FATFileSystem fs("sd", &sd);
00031  Mutex sd_mutex;
00032 void geturistr(char *reqstr,char * uri,int n)
00033  { int i;
00034    char * p;
00035   
00036    p=uri;
00037      i=0;
00038      while(i<n)
00039      {
00040         if (*p=='/') i++;
00041         p++;
00042      
00043      }
00044    while((*p!='/')&&(*p!=0)) 
00045       *reqstr++=*p++;
00046     *reqstr=0x00;
00047     
00048      }
00049 int getparamName(char *name,char * params)
00050 { int i=0;
00051 char *p;
00052 char *n;
00053 p=params;
00054 n=name;
00055    while(*p!='=')
00056      { 
00057      *n++=*p++;
00058      i++;
00059      }
00060       *n=0x00;
00061       i++;
00062       return i;
00063 } 
00064 int getparamValue(char *value,char * param)
00065 { int i=0;
00066 char *p;
00067 char *v;
00068 p=param;
00069 v=value;
00070    while((*p!='&')&&(*p!=0x00))
00071     { 
00072      *v++=*p++;
00073     i++;
00074     }
00075       *v=0x00;
00076       return i;
00077 }        
00078 bool response_File(char * filename,const char *type)
00079 {int n;
00080   FILE *fp = fopen(filename, "r");
00081   if (fp==NULL) return false;
00082 if (strcmp(type,"text/css")||strcmp(type,"text/javascript"))
00083     sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nCache-Control: max-age=2592000, public\r\n\r\n",type);
00084     else
00085      sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: %s\r\n",type);
00086   
00087             client.send(httpHeader,strlen(httpHeader));
00088            // sd_mutex.lock();     
00089            
00090              n=1024;
00091              while (n == 1024) {
00092                 n = fread(buffer, sizeof( char ), 1024, fp);
00093                 client.send(buffer, n);
00094                    }
00095           fclose(fp);   
00096         //  sd_mutex.unlock();
00097         return true;  
00098   }  
00099   void getEXT( char *ext,char * fileName)
00100   {
00101     char *f;
00102     char *e;
00103 e=ext;
00104 f=fileName;
00105 while(*f!='.')
00106     f++;
00107     f++;
00108     while(*f!=0x00)
00109       *e++=*f++;
00110    *e=0x00;
00111       }  
00112      
00113 int main()
00114 {   char fileName[32];
00115     char reqstr[32];
00116     char temp[32];
00117     char ext[8];
00118     char *params; 
00119     bool res;  
00120     printf("Basic HTTP server example\n");
00121     EthernetInterface eth;
00122     eth.set_network(mbedIp,mbedMask,mbedGateway);
00123     eth.connect();
00124     printf("The target IP address is '%s'\n", eth.get_ip_address());      
00125     /* Open the server on ethernet stack */
00126     server.open(&eth);
00127     
00128     /* Bind the HTTP port (TCP 80) to the server */
00129     server.bind(eth.get_ip_address(), 80);
00130     
00131     /* Can handle 5 simultaneous connections */
00132     server.listen(8);
00133  myled=1;
00134     while (true) {
00135         server.accept(&client, &client_addr);
00136      //   client.set_blocking(true);
00137         printf("Connection from: %s\r\n", client_addr.get_ip_address());   
00138      // while (true) {
00139            int n = client.recv(buffer, sizeof(buffer));
00140         //    if (n <= 0) break;         
00141            if (strncmp(buffer, "GET ", 4)==0)
00142            {
00143             uristr = buffer + 4;
00144             eou = strstr(uristr, " ");
00145             *eou = 0;
00146            geturistr(reqstr,uristr,1);
00147            printf("request=%s+\n",uristr);
00148            if (strcmp(reqstr,"index")==0){     //GET index.html
00149           // printf("OK\n");
00150        res=   response_File("/sd/views/index.html","text/html");
00151              } else
00152           if  (strcmp(reqstr,"images")==0){   //GET images
00153                   geturistr(reqstr,uristr,2);
00154                 // printf("image name%s\n",reqstr);
00155                 strcpy(fileName,"/sd/images/");
00156                 strcat(fileName,reqstr);
00157                // printf("filename=%s\n",fileName);
00158                getEXT(ext,fileName);
00159                strcpy(temp,"image/");
00160                strcat(temp,ext);
00161            res=      response_File(fileName,temp);
00162              }else
00163            if  (strcmp(reqstr,"css")==0){  //GET css
00164                   geturistr(reqstr,uristr,2);
00165                 strcpy(fileName,"/sd/css/");
00166                 strcat(fileName,reqstr);
00167                 response_File(fileName,"text/css");
00168                } else 
00169             if  (strcmp(reqstr,"js")==0){ //GET  js
00170                   geturistr(reqstr,uristr,2);
00171                 strcpy(fileName,"/sd/js/");
00172                 strcat(fileName,reqstr);
00173                res=  response_File(fileName,"text/javascript");
00174                } else
00175         //  if  (strcmp(reqstr,"views")==0){ //GET  js
00176                { 
00177                 // geturistr(reqstr,uristr,2);
00178                 strcpy(fileName,"/sd/views/");
00179                 strcat(fileName,reqstr);
00180               //   strcat(fileName,".html");
00181                  printf("fileName=%s\n",fileName);
00182                res=  response_File(fileName,"text/html");
00183              
00184                 }
00185               if (!res)
00186               {
00187                    sprintf(httpHeader,"HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\n");
00188                     client.send(httpHeader,strlen(httpHeader));           
00189                       client.send("Page not found!\n",16);
00190                   }  
00191             }   else
00192               if (strncmp(buffer, "PUT ", 4)==0){
00193                 uristr = buffer + 4;
00194                 eou = strstr(uristr, " ");
00195                 *eou = 0;
00196                 geturistr(reqstr,uristr,1);
00197                  printf("PUT%s+\n",uristr);
00198                if (strcmp(reqstr,"LEDControl")==0){ 
00199                  geturistr(reqstr,uristr,2);
00200                  printf("%s\n",reqstr);
00201                  params= reqstr ;
00202                  int n=getparamName(temp, params);
00203                  printf("name=%s\n",temp);
00204                  params=params+n;
00205                    n=getparamValue(temp,params);
00206                  printf("val=%s\n",temp);
00207                      if (strcmp(temp,"1")==0)
00208                     { myled=1;}
00209                      else {myled=0;}
00210                        sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n");
00211                        client.send(httpHeader,strlen(httpHeader));           
00212                       client.send("OK\n",3);
00213                  }
00214                }else {
00215                sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n");
00216             client.send(httpHeader,strlen(httpHeader));           
00217             client.send("BAD The World!\n",15);
00218              }      
00219     //     }             
00220       client.close();   
00221     }
00222 }