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