this camera C328 http sdcard file server example is for academy

Dependencies:   CameraC328 SDFileSystem WIZnetInterface mbed

Insert below jpg file in your SDcard to take a picture in Web Server /media/uploads/IOP/take_a_picture.zip

Committer:
IOP
Date:
Tue Jul 28 07:27:08 2015 +0000
Revision:
0:0a851a60b7a6
Child:
1:a878f7545221
camera C328 hello world

Who changed what in which revision?

UserRevisionLine numberNew contents of line
IOP 0:0a851a60b7a6 1 #include "mbed.h"
IOP 0:0a851a60b7a6 2 #include "EthernetInterface.h"
IOP 0:0a851a60b7a6 3 #include "SDFileSystem.h"
IOP 0:0a851a60b7a6 4 #include "CameraC328.h"
IOP 0:0a851a60b7a6 5
IOP 0:0a851a60b7a6 6 #define EnDebugMSG false //true-> print debug message to PC USB terminal, false->not print
IOP 0:0a851a60b7a6 7 #include "filelib.h"
IOP 0:0a851a60b7a6 8
IOP 0:0a851a60b7a6 9 #define IP "192.168.240.100"
IOP 0:0a851a60b7a6 10 #define MASK "255.255.255.0"
IOP 0:0a851a60b7a6 11 #define GATEWAY "192.168.240.1"
IOP 0:0a851a60b7a6 12 #define PORT 80
IOP 0:0a851a60b7a6 13
IOP 0:0a851a60b7a6 14 Serial pc (USBTX,USBRX); // tx, rx
IOP 0:0a851a60b7a6 15 SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "wfs"); // the pinout on the mbed
IOP 0:0a851a60b7a6 16
IOP 0:0a851a60b7a6 17 char sMethod[7];
IOP 0:0a851a60b7a6 18 char sURL[250];
IOP 0:0a851a60b7a6 19 char sProtocol[8];
IOP 0:0a851a60b7a6 20
IOP 0:0a851a60b7a6 21
IOP 0:0a851a60b7a6 22 /* --------------------------------camera interface -----------------------------------------------*/
IOP 0:0a851a60b7a6 23
IOP 0:0a851a60b7a6 24 #define USE_JPEG_HIGH_RESOLUTION 1
IOP 0:0a851a60b7a6 25
IOP 0:0a851a60b7a6 26 CameraC328 camera(PA_13, PA_14, CameraC328::Baud115200);
IOP 0:0a851a60b7a6 27
IOP 0:0a851a60b7a6 28 int take_picture = 0;
IOP 0:0a851a60b7a6 29
IOP 0:0a851a60b7a6 30 /*
IOP 0:0a851a60b7a6 31 * Variables.
IOP 0:0a851a60b7a6 32 */
IOP 0:0a851a60b7a6 33 static const int CAPTURE_FRAMES = 2;
IOP 0:0a851a60b7a6 34 static FILE *fp_jpeg;
IOP 0:0a851a60b7a6 35
IOP 0:0a851a60b7a6 36 /**
IOP 0:0a851a60b7a6 37 * A callback function for jpeg images.
IOP 0:0a851a60b7a6 38 * You can block this function until saving the image datas.
IOP 0:0a851a60b7a6 39 *
IOP 0:0a851a60b7a6 40 * @param buf A pointer to the image buffer.
IOP 0:0a851a60b7a6 41 * @param siz A size of the image buffer.
IOP 0:0a851a60b7a6 42 */
IOP 0:0a851a60b7a6 43 void jpeg_callback(char *buf, size_t siz) {
IOP 0:0a851a60b7a6 44 for (int i = 0; i < (int)siz; i++) {
IOP 0:0a851a60b7a6 45 fprintf(fp_jpeg, "%c", buf[i]);
IOP 0:0a851a60b7a6 46 }
IOP 0:0a851a60b7a6 47 }
IOP 0:0a851a60b7a6 48
IOP 0:0a851a60b7a6 49 /**
IOP 0:0a851a60b7a6 50 * Synchronizing.
IOP 0:0a851a60b7a6 51 */
IOP 0:0a851a60b7a6 52 void sync(void) {
IOP 0:0a851a60b7a6 53 CameraC328::ErrorNumber err = CameraC328::NoError;
IOP 0:0a851a60b7a6 54
IOP 0:0a851a60b7a6 55 err = camera.sync();
IOP 0:0a851a60b7a6 56 if (CameraC328::NoError == err) {
IOP 0:0a851a60b7a6 57 printf("[ OK ] : CameraC328::sync\n");
IOP 0:0a851a60b7a6 58 } else {
IOP 0:0a851a60b7a6 59 printf("[FAIL] : CameraC328::sync (Error=%02X)\n", (int)err);
IOP 0:0a851a60b7a6 60 }
IOP 0:0a851a60b7a6 61 }
IOP 0:0a851a60b7a6 62
IOP 0:0a851a60b7a6 63 /**
IOP 0:0a851a60b7a6 64 * A test function for jpeg snapshot picture.
IOP 0:0a851a60b7a6 65 */
IOP 0:0a851a60b7a6 66 void test_jpeg_snapshot_picture(void) {
IOP 0:0a851a60b7a6 67 CameraC328::ErrorNumber err = CameraC328::NoError;
IOP 0:0a851a60b7a6 68
IOP 0:0a851a60b7a6 69 #if USE_JPEG_HIGH_RESOLUTION
IOP 0:0a851a60b7a6 70 err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution640x480);
IOP 0:0a851a60b7a6 71 #else
IOP 0:0a851a60b7a6 72 err = camera.init(CameraC328::Jpeg, CameraC328::RawResolution80x60, CameraC328::JpegResolution320x240);
IOP 0:0a851a60b7a6 73 #endif
IOP 0:0a851a60b7a6 74 if (CameraC328::NoError == err) {
IOP 0:0a851a60b7a6 75 printf("[ OK ] : CameraC328::init\n");
IOP 0:0a851a60b7a6 76 } else {
IOP 0:0a851a60b7a6 77 printf("[FAIL] : CameraC328::init (Error=%02X)\n", (int)err);
IOP 0:0a851a60b7a6 78 }
IOP 0:0a851a60b7a6 79
IOP 0:0a851a60b7a6 80 for (int i = 0; i < CAPTURE_FRAMES; i++) {
IOP 0:0a851a60b7a6 81 char fname[64];
IOP 0:0a851a60b7a6 82 snprintf(fname, sizeof(fname), "/wfs/jpss%04d.jpg", i);
IOP 0:0a851a60b7a6 83 fp_jpeg = fopen(fname, "w");
IOP 0:0a851a60b7a6 84
IOP 0:0a851a60b7a6 85 err = camera.getJpegSnapshotPicture(jpeg_callback);
IOP 0:0a851a60b7a6 86 if (CameraC328::NoError == err) {
IOP 0:0a851a60b7a6 87 printf("[ OK ] : CameraC328::getJpegSnapshotPicture\n");
IOP 0:0a851a60b7a6 88 } else {
IOP 0:0a851a60b7a6 89 printf("[FAIL] : CameraC328::getJpegSnapshotPicture (Error=%02X)\n", (int)err);
IOP 0:0a851a60b7a6 90 }
IOP 0:0a851a60b7a6 91
IOP 0:0a851a60b7a6 92 fclose(fp_jpeg);
IOP 0:0a851a60b7a6 93 }
IOP 0:0a851a60b7a6 94 }
IOP 0:0a851a60b7a6 95 /* ------------------------------------------------------------------------------------ */
IOP 0:0a851a60b7a6 96
IOP 0:0a851a60b7a6 97 EthernetInterface eth;
IOP 0:0a851a60b7a6 98
IOP 0:0a851a60b7a6 99 TCPSocketServer svr;
IOP 0:0a851a60b7a6 100 bool serverIsListened = false;
IOP 0:0a851a60b7a6 101
IOP 0:0a851a60b7a6 102 TCPSocketConnection client;
IOP 0:0a851a60b7a6 103 bool clientIsConnected = false;
IOP 0:0a851a60b7a6 104
IOP 0:0a851a60b7a6 105 char sentBuffer[1608] = {}; // 2*536=1072, 3*536=1608, 4*536=2144 !1500
IOP 0:0a851a60b7a6 106 char line_response[256]= {0};
IOP 0:0a851a60b7a6 107 char file_path[256] = {0};
IOP 0:0a851a60b7a6 108
IOP 0:0a851a60b7a6 109 DigitalOut led1(LED1); //server listning status
IOP 0:0a851a60b7a6 110 DigitalOut led2(LED2); //socket connecting status
IOP 0:0a851a60b7a6 111
IOP 0:0a851a60b7a6 112 Ticker ledTick;
IOP 0:0a851a60b7a6 113
IOP 0:0a851a60b7a6 114 void ledTickfunc()
IOP 0:0a851a60b7a6 115 {
IOP 0:0a851a60b7a6 116 if(serverIsListened) {
IOP 0:0a851a60b7a6 117 led1 = !led1;
IOP 0:0a851a60b7a6 118 } else {
IOP 0:0a851a60b7a6 119 led1 = false;
IOP 0:0a851a60b7a6 120 }
IOP 0:0a851a60b7a6 121 }
IOP 0:0a851a60b7a6 122
IOP 0:0a851a60b7a6 123 void send_HTTP_header(char* protocol, int code, char* title, char* mime_type, long long lengthBody)
IOP 0:0a851a60b7a6 124 {
IOP 0:0a851a60b7a6 125 snprintf(line_response, sizeof(line_response),"%s %d %s\r\n", protocol, code, title );
IOP 0:0a851a60b7a6 126 snprintf(sentBuffer, sizeof(sentBuffer),"%s",line_response);
IOP 0:0a851a60b7a6 127
IOP 0:0a851a60b7a6 128 if ( mime_type != NULL ) {
IOP 0:0a851a60b7a6 129 snprintf(line_response, sizeof(line_response), "Content-Type: %s\r\n", mime_type );
IOP 0:0a851a60b7a6 130 snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer
IOP 0:0a851a60b7a6 131 }
IOP 0:0a851a60b7a6 132 if ( lengthBody >= 0 ) {
IOP 0:0a851a60b7a6 133 snprintf(line_response, sizeof(line_response), "Content-Length: %lld\r\n", lengthBody );
IOP 0:0a851a60b7a6 134 snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer
IOP 0:0a851a60b7a6 135 }
IOP 0:0a851a60b7a6 136 snprintf(line_response, sizeof(line_response), "Connection: close\r\n" );
IOP 0:0a851a60b7a6 137 snprintf(sentBuffer, sizeof(sentBuffer),"%s%s\r\n",sentBuffer,line_response); //append to sentBuffer
IOP 0:0a851a60b7a6 138
IOP 0:0a851a60b7a6 139 if (EnDebugMSG)
IOP 0:0a851a60b7a6 140 printf("\n-->sent Header--\n");
IOP 0:0a851a60b7a6 141
IOP 0:0a851a60b7a6 142 client.send_all(sentBuffer,strlen(sentBuffer));
IOP 0:0a851a60b7a6 143 if (EnDebugMSG) {
IOP 0:0a851a60b7a6 144 printf(sentBuffer);
IOP 0:0a851a60b7a6 145 printf("\n--end Header-- bytes:%d",strlen(sentBuffer));
IOP 0:0a851a60b7a6 146 }
IOP 0:0a851a60b7a6 147 wait(0.2); //200ms important for browser!
IOP 0:0a851a60b7a6 148 }
IOP 0:0a851a60b7a6 149
IOP 0:0a851a60b7a6 150 void send_HTML_line(char* line, unsigned int length_line)
IOP 0:0a851a60b7a6 151 {
IOP 0:0a851a60b7a6 152 client.send_all(line,length_line);
IOP 0:0a851a60b7a6 153 if (EnDebugMSG)
IOP 0:0a851a60b7a6 154 printf("\n-->send HTML line:\n%s ...Ok!",line);
IOP 0:0a851a60b7a6 155 wait(0.01);
IOP 0:0a851a60b7a6 156 }
IOP 0:0a851a60b7a6 157
IOP 0:0a851a60b7a6 158 void send_HTML_error( int status_code, char* title, char* body_text)
IOP 0:0a851a60b7a6 159 {
IOP 0:0a851a60b7a6 160 send_HTTP_header("HTTP/1.1", status_code, title, "text/html", -1);
IOP 0:0a851a60b7a6 161 if (EnDebugMSG)
IOP 0:0a851a60b7a6 162 printf("\n-->send_error...\n");
IOP 0:0a851a60b7a6 163 sentBuffer[0]=NULL; //clear buffer
IOP 0:0a851a60b7a6 164 sprintf(line_response, "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>%d %s</title>\r\n</head>\r\n", status_code, title);
IOP 0:0a851a60b7a6 165 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 166 sprintf(line_response, "<body><center><h2><center>%d %s</center></h2>\r\n",status_code, title );
IOP 0:0a851a60b7a6 167 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 168 sprintf(line_response, "%s\r\n", body_text );
IOP 0:0a851a60b7a6 169 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 170 sprintf(line_response, "<p>mbed HTTP File Server</p>\r\n</center></body></html>\r\n");
IOP 0:0a851a60b7a6 171 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 172 send_HTML_line(sentBuffer, strlen(sentBuffer));
IOP 0:0a851a60b7a6 173 }
IOP 0:0a851a60b7a6 174
IOP 0:0a851a60b7a6 175 int send_file(char *path_file)
IOP 0:0a851a60b7a6 176 {
IOP 0:0a851a60b7a6 177 char *mime_type = {0};
IOP 0:0a851a60b7a6 178 unsigned int bytes_for_send=0;
IOP 0:0a851a60b7a6 179 long long filesize, all_send_bytes = 0;
IOP 0:0a851a60b7a6 180
IOP 0:0a851a60b7a6 181 if (strcmp(file_path,"/wfs//take_a_picture.jpg") == 0)
IOP 0:0a851a60b7a6 182 take_picture = 1;
IOP 0:0a851a60b7a6 183 else take_picture = 0;
IOP 0:0a851a60b7a6 184
IOP 0:0a851a60b7a6 185 mime_type = get_mime_type( path_file );
IOP 0:0a851a60b7a6 186 snprintf(file_path, sizeof(file_path),"/wfs/%s",path_file);
IOP 0:0a851a60b7a6 187 if (EnDebugMSG) {
IOP 0:0a851a60b7a6 188 printf("\n-->from send_file:%s",file_path);
IOP 0:0a851a60b7a6 189 printf("\n-->from send_file mime type:%s",mime_type);
IOP 0:0a851a60b7a6 190 }
IOP 0:0a851a60b7a6 191
IOP 0:0a851a60b7a6 192 if (Mystat(path_file, &myStatBuf)) { //fault with file
IOP 0:0a851a60b7a6 193 send_HTML_error( 403, "Forbidden", "403 - File access forbidden.");
IOP 0:0a851a60b7a6 194 return 403;
IOP 0:0a851a60b7a6 195 }
IOP 0:0a851a60b7a6 196 FILE* fp = NULL;
IOP 0:0a851a60b7a6 197 fp = fopen(file_path,"r");
IOP 0:0a851a60b7a6 198 if (fp==NULL ) {
IOP 0:0a851a60b7a6 199 send_HTML_error( 403, "Forbidden", "403 - File access forbidden.");
IOP 0:0a851a60b7a6 200 return 403;
IOP 0:0a851a60b7a6 201 }
IOP 0:0a851a60b7a6 202
IOP 0:0a851a60b7a6 203 filesize = myStatBuf.st_size;
IOP 0:0a851a60b7a6 204 send_HTTP_header("HTTP/1.1", 200, "Ok", mime_type, myStatBuf.st_size);
IOP 0:0a851a60b7a6 205 //binary send
IOP 0:0a851a60b7a6 206 all_send_bytes=0;
IOP 0:0a851a60b7a6 207 while(filesize) { //check for EOF !feof(fp)
IOP 0:0a851a60b7a6 208 bytes_for_send = filesize;
IOP 0:0a851a60b7a6 209 if (bytes_for_send > sizeof(sentBuffer)) {
IOP 0:0a851a60b7a6 210 bytes_for_send = sizeof(sentBuffer);
IOP 0:0a851a60b7a6 211 }
IOP 0:0a851a60b7a6 212 fread (sentBuffer,1,bytes_for_send,fp);
IOP 0:0a851a60b7a6 213 filesize -= bytes_for_send;
IOP 0:0a851a60b7a6 214 if (EnDebugMSG)
IOP 0:0a851a60b7a6 215 printf("\n---bytes_for_send...%d",bytes_for_send);
IOP 0:0a851a60b7a6 216 client.send_all(sentBuffer,bytes_for_send);
IOP 0:0a851a60b7a6 217 //Thread::wait(10);
IOP 0:0a851a60b7a6 218 all_send_bytes += bytes_for_send;
IOP 0:0a851a60b7a6 219 }
IOP 0:0a851a60b7a6 220 if (EnDebugMSG)
IOP 0:0a851a60b7a6 221 printf("\n---buffer fill end - all ...%lld", all_send_bytes);
IOP 0:0a851a60b7a6 222 //binary send
IOP 0:0a851a60b7a6 223
IOP 0:0a851a60b7a6 224 sprintf(line_response, "\r\n");
IOP 0:0a851a60b7a6 225 client.send_all(line_response,strlen(line_response));
IOP 0:0a851a60b7a6 226 if ( fp != NULL )
IOP 0:0a851a60b7a6 227 fclose(fp);
IOP 0:0a851a60b7a6 228 //Thread::wait(10);
IOP 0:0a851a60b7a6 229 return 0;
IOP 0:0a851a60b7a6 230 }
IOP 0:0a851a60b7a6 231
IOP 0:0a851a60b7a6 232 int send_directory(char *path)
IOP 0:0a851a60b7a6 233 {
IOP 0:0a851a60b7a6 234 char process_name[64]= {0};
IOP 0:0a851a60b7a6 235
IOP 0:0a851a60b7a6 236 char posOfLastSlash;
IOP 0:0a851a60b7a6 237 char *pLS;
IOP 0:0a851a60b7a6 238
IOP 0:0a851a60b7a6 239 struct dirent *p;
IOP 0:0a851a60b7a6 240 struct sMystat sb;
IOP 0:0a851a60b7a6 241 struct tm *timeinfo;
IOP 0:0a851a60b7a6 242 char timeBuf[40];
IOP 0:0a851a60b7a6 243
IOP 0:0a851a60b7a6 244 if (EnDebugMSG)
IOP 0:0a851a60b7a6 245 printf("\n-->from send_directory:%s",path);
IOP 0:0a851a60b7a6 246 snprintf(file_path,sizeof(file_path),"/wfs%s",path);
IOP 0:0a851a60b7a6 247 DIR *d = opendir(file_path);
IOP 0:0a851a60b7a6 248 if (EnDebugMSG && d!=NULL)
IOP 0:0a851a60b7a6 249 printf("\n-->from send_directory:%s ...open OK",file_path);
IOP 0:0a851a60b7a6 250 if (d==NULL) { //error open dir
IOP 0:0a851a60b7a6 251 send_HTML_error( 403, "Forbidden", "403 - Directory access forbidden.");
IOP 0:0a851a60b7a6 252 return -1;
IOP 0:0a851a60b7a6 253 }
IOP 0:0a851a60b7a6 254 send_HTTP_header("HTTP/1.1", 200, "Ok",NULL, -1);
IOP 0:0a851a60b7a6 255 sentBuffer[0]=NULL;
IOP 0:0a851a60b7a6 256 sprintf(line_response,"<!DOCTYPE html>\r\n<html>\n<head><title>Index of %s</title>\n",path);
IOP 0:0a851a60b7a6 257 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 258
IOP 0:0a851a60b7a6 259 sprintf(line_response,"<meta content=\"text/html; charset=iso-8859-1\" http-equiv=\"Content-Type\"></head>\n");
IOP 0:0a851a60b7a6 260 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 261
IOP 0:0a851a60b7a6 262 sprintf(line_response,"<body><center>\n<h3>Index of %s</h3>\n", path);
IOP 0:0a851a60b7a6 263 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 264 send_HTML_line(sentBuffer, strlen(sentBuffer));
IOP 0:0a851a60b7a6 265 //begin table
IOP 0:0a851a60b7a6 266 sentBuffer[0]=NULL; //clear buffer
IOP 0:0a851a60b7a6 267 sprintf(line_response,"<table border=\"0\">\n");
IOP 0:0a851a60b7a6 268 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 269 sprintf(line_response,"<tr><th align=\"left\" width=\"200\">Name</th><th align=\"right\" width=\"100\">Size(bytes)</th><th align=\"right\" width=\"200\">Date/Time</th></tr>\n");
IOP 0:0a851a60b7a6 270 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 271 //begin table
IOP 0:0a851a60b7a6 272
IOP 0:0a851a60b7a6 273 pLS=strrchr(path,'/');
IOP 0:0a851a60b7a6 274 posOfLastSlash=pLS-path+1;
IOP 0:0a851a60b7a6 275 if (EnDebugMSG)
IOP 0:0a851a60b7a6 276 printf("\r\n>>posOfLastSlash=%d",posOfLastSlash);
IOP 0:0a851a60b7a6 277 snprintf(process_name,posOfLastSlash+1,"%s",path);
IOP 0:0a851a60b7a6 278 if (EnDebugMSG)
IOP 0:0a851a60b7a6 279 printf("\r\n>>process_name=%s",process_name);
IOP 0:0a851a60b7a6 280 sprintf(line_response,"<tr><td align=\"left\"><a href=\"%s\">../refresh</a></td></tr>\n",process_name);
IOP 0:0a851a60b7a6 281 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 282 while((p = readdir(d)) != NULL) {
IOP 0:0a851a60b7a6 283 if (EnDebugMSG)
IOP 0:0a851a60b7a6 284 printf("\n :%s",p->d_name);
IOP 0:0a851a60b7a6 285 sprintf(file_path,"%s/%s",path,p->d_name);
IOP 0:0a851a60b7a6 286 Mystat( file_path, &sb );
IOP 0:0a851a60b7a6 287 if (get_dirInfo(file_path)==0 ) { //this is directory path
IOP 0:0a851a60b7a6 288 if (EnDebugMSG)
IOP 0:0a851a60b7a6 289 printf("\nDIR");
IOP 0:0a851a60b7a6 290 sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a><br></td></tr>\n",file_path,p->d_name);
IOP 0:0a851a60b7a6 291 if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent
IOP 0:0a851a60b7a6 292 send_HTML_line(sentBuffer, strlen(sentBuffer));
IOP 0:0a851a60b7a6 293 sentBuffer[0]=NULL; //clear buffer
IOP 0:0a851a60b7a6 294 }
IOP 0:0a851a60b7a6 295 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 296
IOP 0:0a851a60b7a6 297 } else { //this is file
IOP 0:0a851a60b7a6 298 if (EnDebugMSG)
IOP 0:0a851a60b7a6 299 printf("\nFILE");
IOP 0:0a851a60b7a6 300 timeinfo = localtime (&sb.st_mtime);
IOP 0:0a851a60b7a6 301 //strftime(timeBuf,40, "%I:%M:%S %p (%Y/%m/%d)\r\n", localtime(&sb.st_mtime));
IOP 0:0a851a60b7a6 302 strftime(timeBuf, 40, "%c", timeinfo);
IOP 0:0a851a60b7a6 303 sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a></td><td align=\"right\">%lld</td><td align=\"right\">%s</td></tr>\n", file_path, p->d_name,(long long) sb.st_size,timeBuf); // asctime(timeinfo) );
IOP 0:0a851a60b7a6 304
IOP 0:0a851a60b7a6 305 if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer))) { //buffer must be sent
IOP 0:0a851a60b7a6 306 send_HTML_line(sentBuffer, strlen(sentBuffer));
IOP 0:0a851a60b7a6 307 sentBuffer[0]=NULL; //clear buffer
IOP 0:0a851a60b7a6 308 }
IOP 0:0a851a60b7a6 309 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
IOP 0:0a851a60b7a6 310 }
IOP 0:0a851a60b7a6 311 }
IOP 0:0a851a60b7a6 312 send_HTML_line(sentBuffer, strlen(sentBuffer));
IOP 0:0a851a60b7a6 313 closedir(d);
IOP 0:0a851a60b7a6 314
IOP 0:0a851a60b7a6 315 sprintf(line_response, "</table>\n<br><h4>mbed HTTP File Server</h4>\n</center></body></html>\n");
IOP 0:0a851a60b7a6 316 send_HTML_line(line_response, strlen(line_response));
IOP 0:0a851a60b7a6 317
IOP 0:0a851a60b7a6 318 return 0;
IOP 0:0a851a60b7a6 319 }
IOP 0:0a851a60b7a6 320
IOP 0:0a851a60b7a6 321 void parseHTTPRequest(char* buffer)
IOP 0:0a851a60b7a6 322 {
IOP 0:0a851a60b7a6 323 char spacePos;
IOP 0:0a851a60b7a6 324 char *tmpBuffer;
IOP 0:0a851a60b7a6 325 spacePos = strcspn(buffer, " ") + 1; //position of first space character
IOP 0:0a851a60b7a6 326 snprintf(sMethod, spacePos,"%s", buffer);
IOP 0:0a851a60b7a6 327
IOP 0:0a851a60b7a6 328 //get Protocol
IOP 0:0a851a60b7a6 329 tmpBuffer=&(buffer[spacePos]); //move pointer to buffer (delete Method)
IOP 0:0a851a60b7a6 330 spacePos = strcspn(tmpBuffer, "\r\n") + 1;
IOP 0:0a851a60b7a6 331 tmpBuffer[spacePos]='\0'; //set end of string ...cut
IOP 0:0a851a60b7a6 332 sprintf(sProtocol, "%s", strrchr(tmpBuffer,' ')); //get string after last (space )
IOP 0:0a851a60b7a6 333 printf("\r\nsProtocol:%s", tmpBuffer);
IOP 0:0a851a60b7a6 334 buffer = &(sProtocol[1]); //cut first character (space)
IOP 0:0a851a60b7a6 335 sprintf(sProtocol, "%s", buffer);
IOP 0:0a851a60b7a6 336
IOP 0:0a851a60b7a6 337 //get URL
IOP 0:0a851a60b7a6 338 snprintf(sURL,strlen(tmpBuffer)-strlen(sProtocol),"%s\r\n", tmpBuffer); //URL is between Method and Protocol
IOP 0:0a851a60b7a6 339
IOP 0:0a851a60b7a6 340 printf("\nParse Method:%s",sMethod);
IOP 0:0a851a60b7a6 341 printf("\nParse URL:%s",sURL);
IOP 0:0a851a60b7a6 342 printf("\nParse PROTOCOL:%s",sProtocol);
IOP 0:0a851a60b7a6 343 printf("\n\r\n");
IOP 0:0a851a60b7a6 344 }
IOP 0:0a851a60b7a6 345
IOP 0:0a851a60b7a6 346 int processHTTP(char* sMethod, char* sURL, char* sProtocol)
IOP 0:0a851a60b7a6 347 {
IOP 0:0a851a60b7a6 348 int gdi, gfi; //status of get_dir_info(xxx), and get_file_info(xxx)
IOP 0:0a851a60b7a6 349
IOP 0:0a851a60b7a6 350 if (strcmp(sMethod,"GET")!=0) {
IOP 0:0a851a60b7a6 351 send_HTML_error( 501, "501 Not Implemented", "501 - The server either does not recognize the request method");
IOP 0:0a851a60b7a6 352 return 501;
IOP 0:0a851a60b7a6 353 }
IOP 0:0a851a60b7a6 354 if (sURL[0]!= '/') {
IOP 0:0a851a60b7a6 355 send_HTML_error( 400, "Bad Request", "400 - The request cannot be fulfilled due to bad syntax.");
IOP 0:0a851a60b7a6 356 return 400;
IOP 0:0a851a60b7a6 357 }
IOP 0:0a851a60b7a6 358 if (sURL[strlen(sURL)-1]=='/') {
IOP 0:0a851a60b7a6 359 sURL[strlen(sURL)-1]=sURL[strlen(sURL)]; //delete last symbol
IOP 0:0a851a60b7a6 360 if (EnDebugMSG)
IOP 0:0a851a60b7a6 361 printf("\n delete last:%s",sURL);
IOP 0:0a851a60b7a6 362 }
IOP 0:0a851a60b7a6 363 gdi= get_dirInfo(sURL);
IOP 0:0a851a60b7a6 364 gfi= get_fileInfo(sURL);
IOP 0:0a851a60b7a6 365 if (gfi!=0) { //!=0 file not found
IOP 0:0a851a60b7a6 366 if (gdi==0) { //0-ok this is directory
IOP 0:0a851a60b7a6 367 return send_directory(sURL);
IOP 0:0a851a60b7a6 368 }
IOP 0:0a851a60b7a6 369 if (EnDebugMSG)
IOP 0:0a851a60b7a6 370 printf("\n404-br File not found or...(Fresult is:%d)",gfi);
IOP 0:0a851a60b7a6 371 send_HTML_error( 404, "Not Found","404 - The requested resource could not be found.");
IOP 0:0a851a60b7a6 372 return 404;
IOP 0:0a851a60b7a6 373 } else { //==0 found
IOP 0:0a851a60b7a6 374 if (gdi==0) //0-ok this is directory
IOP 0:0a851a60b7a6 375 return send_directory(sURL);
IOP 0:0a851a60b7a6 376 else
IOP 0:0a851a60b7a6 377 return send_file(sURL);
IOP 0:0a851a60b7a6 378 }
IOP 0:0a851a60b7a6 379 }
IOP 0:0a851a60b7a6 380
IOP 0:0a851a60b7a6 381 int main()
IOP 0:0a851a60b7a6 382 {
IOP 0:0a851a60b7a6 383 printf("CameraC328\n");
IOP 0:0a851a60b7a6 384
IOP 0:0a851a60b7a6 385 sync();
IOP 0:0a851a60b7a6 386 test_jpeg_snapshot_picture();
IOP 0:0a851a60b7a6 387
IOP 0:0a851a60b7a6 388 uint8_t MAC_Addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
IOP 0:0a851a60b7a6 389 ledTick.attach(&ledTickfunc,0.5);
IOP 0:0a851a60b7a6 390 //ledTick.detach();
IOP 0:0a851a60b7a6 391 //setup ethernet interface
IOP 0:0a851a60b7a6 392 //eth.init(); //Use DHCP
IOP 0:0a851a60b7a6 393 eth.init(MAC_Addr,IP,MASK,GATEWAY); //MAC,IP,mask,Gateway
IOP 0:0a851a60b7a6 394 eth.connect();
IOP 0:0a851a60b7a6 395 printf("IP Address is %s\n\r", eth.getIPAddress());
IOP 0:0a851a60b7a6 396
IOP 0:0a851a60b7a6 397 //setup tcp socket
IOP 0:0a851a60b7a6 398 if(svr.bind(PORT)< 0) {
IOP 0:0a851a60b7a6 399 printf("tcp server bind failed.\n\r");
IOP 0:0a851a60b7a6 400 return -1;
IOP 0:0a851a60b7a6 401 } else {
IOP 0:0a851a60b7a6 402 printf("tcp server bind successed.\n\r");
IOP 0:0a851a60b7a6 403 serverIsListened = true;
IOP 0:0a851a60b7a6 404 }
IOP 0:0a851a60b7a6 405
IOP 0:0a851a60b7a6 406 if(svr.listen(1) < 0) {
IOP 0:0a851a60b7a6 407 printf("tcp server listen failed.\n\r");
IOP 0:0a851a60b7a6 408 return -1;
IOP 0:0a851a60b7a6 409 } else {
IOP 0:0a851a60b7a6 410 printf("tcp server is listening...\n\r");
IOP 0:0a851a60b7a6 411 }
IOP 0:0a851a60b7a6 412
IOP 0:0a851a60b7a6 413 //listening for http GET request
IOP 0:0a851a60b7a6 414 while (serverIsListened) {
IOP 0:0a851a60b7a6 415 //blocking mode(never timeout)
IOP 0:0a851a60b7a6 416 if(svr.accept(client)<0) {
IOP 0:0a851a60b7a6 417 printf("failed to accept connection.\n\r");
IOP 0:0a851a60b7a6 418 }
IOP 0:0a851a60b7a6 419 else if (take_picture) {
IOP 0:0a851a60b7a6 420 printf("start taking a picture\r\n");
IOP 0:0a851a60b7a6 421 sync();
IOP 0:0a851a60b7a6 422 test_jpeg_snapshot_picture();
IOP 0:0a851a60b7a6 423
IOP 0:0a851a60b7a6 424 take_picture = 0;
IOP 0:0a851a60b7a6 425 }else {
IOP 0:0a851a60b7a6 426 //client.set_blocking(false,5000); //5000=5sec
IOP 0:0a851a60b7a6 427 printf("connection success!\n\rIP: %s\n\r",client.get_address());
IOP 0:0a851a60b7a6 428 clientIsConnected = true;
IOP 0:0a851a60b7a6 429 led2 = true;
IOP 0:0a851a60b7a6 430 while(clientIsConnected) {
IOP 0:0a851a60b7a6 431 char buffer[1024] = {};
IOP 0:0a851a60b7a6 432 switch(client.receive(buffer, 1023)) {
IOP 0:0a851a60b7a6 433 case 0:
IOP 0:0a851a60b7a6 434 printf("recieved buffer is empty.\n\r");
IOP 0:0a851a60b7a6 435 clientIsConnected = false;
IOP 0:0a851a60b7a6 436 break;
IOP 0:0a851a60b7a6 437 case -1:
IOP 0:0a851a60b7a6 438 printf("failed to read data from client.\n\r");
IOP 0:0a851a60b7a6 439 clientIsConnected = false;
IOP 0:0a851a60b7a6 440 break;
IOP 0:0a851a60b7a6 441 default:
IOP 0:0a851a60b7a6 442 printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
IOP 0:0a851a60b7a6 443 parseHTTPRequest(buffer);
IOP 0:0a851a60b7a6 444 //if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) {
IOP 0:0a851a60b7a6 445 if (strcmp(sMethod, "GET" ) == 0 ) {
IOP 0:0a851a60b7a6 446 printf("GET request incomming.\n\r");
IOP 0:0a851a60b7a6 447 processHTTP(sMethod, sURL, sProtocol);
IOP 0:0a851a60b7a6 448 clientIsConnected = false;
IOP 0:0a851a60b7a6 449 }//if get
IOP 0:0a851a60b7a6 450 break;
IOP 0:0a851a60b7a6 451 } //switch
IOP 0:0a851a60b7a6 452 //ledTick.attach(&ledTickfunc,0.5);
IOP 0:0a851a60b7a6 453 }//while
IOP 0:0a851a60b7a6 454 printf("close connection.\n\rHTTP server is listening...\n\r\n");
IOP 0:0a851a60b7a6 455 client.close();
IOP 0:0a851a60b7a6 456 wait(0.05);
IOP 0:0a851a60b7a6 457
IOP 0:0a851a60b7a6 458 led2 = false;
IOP 0:0a851a60b7a6 459 }
IOP 0:0a851a60b7a6 460 }
IOP 0:0a851a60b7a6 461
IOP 0:0a851a60b7a6 462 }