ARD2PMD Web Server Web Server using the ARD2PMD adapter and the SPI2SD adapter with the Seeeduino Arch Pro to serve files from the micro SD card and provide RESTful access to the Pmod connector
Dependencies: ARD2PMD EthernetInterface MAX14661 PmodInterface SDFileSystem mbed-rtos mbed
Fork of ARD2PMD_WebServer by
main.cpp
00001 #include "mbed.h" 00002 #include "EthernetInterface.h" 00003 #include "SDFileSystem.h" 00004 #include "ARD2PMD.h" 00005 #include "PmodInterface.h" 00006 #include <stdio.h> 00007 #include <string.h> 00008 00009 #define HTTPD_SERVER_PORT 80 00010 #define HTTPD_MAX_REQ_LENGTH 1023 00011 #define HTTPD_MAX_HDR_LENGTH 255 00012 #define HTTPD_MAX_FNAME_LENGTH 127 00013 #define HTTPD_MAX_DNAME_LENGTH 127 00014 #define CGI_MAX_RESP_LENGTH 255 00015 #define UART_MAX_CMD_LENGTH 127 00016 00017 Serial uart(USBTX, USBRX); 00018 I2C i2c(D14, D15); 00019 ARD2PMD a2p; 00020 PmodInterface pInt; 00021 SDFileSystem sd(P0_18, P0_17, P0_15, P0_16, "sd"); // Seeeduino Arch Pro SPI2SD 00022 00023 EthernetInterface eth; 00024 TCPSocketServer server; 00025 TCPSocketConnection client; 00026 00027 char buffer[HTTPD_MAX_REQ_LENGTH+1]; 00028 char httpHeader[HTTPD_MAX_HDR_LENGTH+1]; 00029 char fileName[HTTPD_MAX_FNAME_LENGTH+1]; 00030 char dirName[HTTPD_MAX_DNAME_LENGTH+1]; 00031 char obuf[CGI_MAX_RESP_LENGTH+1]; 00032 char ibuf[UART_MAX_CMD_LENGTH+1]; 00033 char *uristr; 00034 char *eou; 00035 char *qrystr; 00036 00037 FILE *fp; 00038 int rdCnt; 00039 00040 void get_file(char* uri) 00041 { 00042 uart.printf("get_file %s\n", uri); 00043 char *lstchr = strrchr(uri, NULL) -1; 00044 if ('/' == *lstchr) { 00045 uart.printf("Open directory /sd%s\n", uri); 00046 *lstchr = 0; 00047 sprintf(fileName, "/sd%s", uri); 00048 DIR *d = opendir(fileName); 00049 if (d != NULL) { 00050 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00051 client.send(httpHeader,strlen(httpHeader)); 00052 sprintf(httpHeader,"<html><head><title>Directory Listing</title></head><body><h1>%s Directory Listing</h1><ul>", uri); 00053 client.send(httpHeader,strlen(httpHeader)); 00054 struct dirent *p; 00055 while((p = readdir(d)) != NULL) { 00056 sprintf(dirName, "%s/%s", fileName, p->d_name); 00057 uart.printf("%s\n", dirName); 00058 DIR *subDir = opendir(dirName); 00059 if (subDir != NULL) { 00060 sprintf(httpHeader,"<li><a href=\"./%s/\">%s/</a></li>", p->d_name, p->d_name); 00061 } else { 00062 sprintf(httpHeader,"<li><a href=\"./%s\">%s</a></li>", p->d_name, p->d_name); 00063 } 00064 client.send(httpHeader,strlen(httpHeader)); 00065 } 00066 } 00067 closedir(d); 00068 uart.printf("Directory closed\n"); 00069 sprintf(httpHeader,"</ul></body></html>"); 00070 client.send(httpHeader,strlen(httpHeader)); 00071 } else { 00072 sprintf(fileName, "/sd%s", uri); 00073 fp = fopen(fileName, "r"); 00074 if (fp == NULL) { 00075 sprintf(httpHeader,"HTTP/1.1 404 Not Found \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00076 client.send(httpHeader,strlen(httpHeader)); 00077 client.send(uri,strlen(uri)); 00078 } else { 00079 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00080 client.send(httpHeader,strlen(httpHeader)); 00081 while ((rdCnt = fread(buffer, sizeof( char ), 1024, fp)) == 1024) { 00082 client.send(buffer, rdCnt); 00083 } 00084 client.send(buffer, rdCnt); 00085 fclose(fp); 00086 } 00087 } 00088 } 00089 00090 void get_cgi(char* uri) 00091 { 00092 pInt.call(uri, obuf); 00093 sprintf(httpHeader,"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n"); 00094 client.send(httpHeader,strlen(httpHeader)); 00095 client.send(obuf,strlen(obuf)); 00096 uart.printf("resp: %s", obuf); 00097 } 00098 00099 void uart_cmd_thread(void const *args) 00100 { 00101 int i = 0; 00102 while(true) { 00103 if (uart.readable()) { 00104 ibuf[i]=uart.getc(); 00105 if (ibuf[i]!='\r') { 00106 if (i < 255) { 00107 i += 1; 00108 } 00109 } else { 00110 if (i < 255) { 00111 ibuf[i]=0; 00112 uart.printf("UART CMD: %s=", ibuf); 00113 pInt.call(ibuf, obuf); 00114 uart.printf("%s\n", obuf); 00115 } else { 00116 uart.printf("UART Input Command Too Long\n"); 00117 } 00118 i=0; 00119 } 00120 } 00121 } 00122 } 00123 00124 int main (void) 00125 { 00126 // Serial Interface Setup; 00127 uart.baud(115200); 00128 uart.printf("mbd2pmd Serial Started\n"); 00129 00130 // Mbed to Pmod Initialization; 00131 a2p.init(); 00132 00133 // Pmod Interface Setup 00134 pInt.init(&a2p.pmd[0], &i2c, &a2p.mux, a2p.mux_a, a2p.mux_p); 00135 00136 // Check File System 00137 DIR *d = opendir("/sd/"); 00138 if (d != NULL) { 00139 uart.printf("SD Card Present\n"); 00140 } else { 00141 uart.printf("SD Card Root Directory Not Found\n"); 00142 } 00143 00144 // EthernetInterface eth; 00145 eth.init(); //Use DHCP 00146 eth.connect(); 00147 uart.printf("IP Address is %s\n", eth.getIPAddress()); 00148 00149 // TCPSocketServer server; 00150 server.bind(HTTPD_SERVER_PORT); 00151 server.listen(); 00152 uart.printf("mbd2pmd Server Listening\n"); 00153 00154 // Start UART Command Thread 00155 Thread thread(uart_cmd_thread); 00156 uart.printf("UART Commands Listening\n"); 00157 00158 while (true) { 00159 uart.printf("\nWait for new connection...\r\n"); 00160 server.accept(client); 00161 client.set_blocking(false, 1500); // Timeout after (1.5)s 00162 00163 uart.printf("Connection from: %s\r\n", client.get_address()); 00164 while (true) { 00165 int n = client.receive(buffer, sizeof(buffer)); 00166 if (n <= 0) break; 00167 uart.printf("Recieved Data: %d\r\n\r\n%.*s\r\n",n,n,buffer); 00168 if (n >= 1024) { 00169 sprintf(httpHeader,"HTTP/1.1 413 Request Entity Too Large \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00170 client.send(httpHeader,strlen(httpHeader)); 00171 client.send(buffer,n); 00172 break; 00173 } else { 00174 buffer[n]=0; 00175 } 00176 if (!strncmp(buffer, "GET ", 4)) { 00177 uristr = buffer + 4; 00178 eou = strstr(uristr, " "); 00179 if (eou == NULL) { 00180 sprintf(httpHeader,"HTTP/1.1 400 Bad Request \r\nContent-Type: text\r\nConnection: Close\r\n\r\n"); 00181 client.send(httpHeader,strlen(httpHeader)); 00182 client.send(buffer,n); 00183 } else { 00184 *eou = 0; 00185 if (!strncmp(uristr, "/pmd/", 5)) { 00186 get_cgi(uristr+4); 00187 } else { 00188 get_file(uristr); 00189 } 00190 } 00191 } 00192 } 00193 00194 client.close(); 00195 } 00196 }
Generated on Tue Jul 19 2022 20:27:32 by 1.7.2