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