Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 #include "mbed.h"
shoaib_ahmed 0:791a779d6220 2 #include "SDFileSystem.h"
shoaib_ahmed 0:791a779d6220 3 #include <jpeglib.h> //convert
shoaib_ahmed 0:791a779d6220 4 #include <stdio.h> //convert
shoaib_ahmed 0:791a779d6220 5 #include <stdlib.h> //convert
shoaib_ahmed 0:791a779d6220 6 #include <string.h> //convert
shoaib_ahmed 0:791a779d6220 7 #include "LCD_DISCO_F746NG.h" //screen
shoaib_ahmed 0:791a779d6220 8 #include "EthernetInterface.h" //lan
shoaib_ahmed 0:791a779d6220 9 #define EnDebugMSG false //true-> print debug message
shoaib_ahmed 0:791a779d6220 10 #include "filelib.h"
shoaib_ahmed 0:791a779d6220 11 #define IP "192.168.240.100"
shoaib_ahmed 0:791a779d6220 12 #define MASK "255.255.255.0"
shoaib_ahmed 0:791a779d6220 13 #define GATEWAY "192.168.240.1"
shoaib_ahmed 0:791a779d6220 14 #define PORT 80
shoaib_ahmed 0:791a779d6220 15 #define DEBMSG printf
shoaib_ahmed 0:791a779d6220 16 #define NEWLINE() printf("\r\n")
shoaib_ahmed 0:791a779d6220 17 #if USE_SDCARD
shoaib_ahmed 0:791a779d6220 18 #define FILENAME "/sd/IMG_%04d.jpg"
shoaib_ahmed 0:791a779d6220 19 #else
shoaib_ahmed 0:791a779d6220 20 #define FILENAME "/sd/IMG_%04d.jpg"
shoaib_ahmed 0:791a779d6220 21 SDFileSystem sd("sd");
shoaib_ahmed 0:791a779d6220 22 #endif
shoaib_ahmed 0:791a779d6220 23
shoaib_ahmed 0:791a779d6220 24
shoaib_ahmed 0:791a779d6220 25 DigitalIn pb(USER_BUTTON);
shoaib_ahmed 0:791a779d6220 26 LCD_DISCO_F746NG lcd;
shoaib_ahmed 0:791a779d6220 27
shoaib_ahmed 0:791a779d6220 28
shoaib_ahmed 0:791a779d6220 29 //lan module
shoaib_ahmed 0:791a779d6220 30 char sMethod[7];
shoaib_ahmed 0:791a779d6220 31 char sURL[250];
shoaib_ahmed 0:791a779d6220 32 char sProtocol[8];
shoaib_ahmed 0:791a779d6220 33 EthernetInterface eth;
shoaib_ahmed 0:791a779d6220 34 TCPSocketServer svr;
shoaib_ahmed 0:791a779d6220 35 bool serverIsListened = false;
shoaib_ahmed 0:791a779d6220 36 TCPSocketConnection client;
shoaib_ahmed 0:791a779d6220 37 bool clientIsConnected = false;
shoaib_ahmed 0:791a779d6220 38 char sentBuffer[1072] = {}; // 2*536=1072, 3*536=1608, 4*536=2144
shoaib_ahmed 0:791a779d6220 39 char line_response[256]= {0};
shoaib_ahmed 0:791a779d6220 40 char file_path[256] = {0};
shoaib_ahmed 0:791a779d6220 41 DigitalOut led1(LED1); //server listning status
shoaib_ahmed 0:791a779d6220 42 DigitalOut led2(LED2); //socket connecting status
shoaib_ahmed 0:791a779d6220 43 Ticker ledTick;
shoaib_ahmed 0:791a779d6220 44 //~lan module
shoaib_ahmed 0:791a779d6220 45
shoaib_ahmed 0:791a779d6220 46 //convert module
shoaib_ahmed 0:791a779d6220 47 unsigned char *raw_image = NULL;
shoaib_ahmed 0:791a779d6220 48
shoaib_ahmed 0:791a779d6220 49 typedef struct work
shoaib_ahmed 0:791a779d6220 50 {
shoaib_ahmed 0:791a779d6220 51 FILE *fp;
shoaib_ahmed 0:791a779d6220 52 } work_t;
shoaib_ahmed 0:791a779d6220 53 work_t work;
shoaib_ahmed 0:791a779d6220 54
shoaib_ahmed 0:791a779d6220 55 void callback_func(int done, int total, uint8_t *buf, size_t siz)
shoaib_ahmed 0:791a779d6220 56 {
shoaib_ahmed 0:791a779d6220 57 fwrite(buf, siz, 1, work.fp);
shoaib_ahmed 0:791a779d6220 58 static int n = 0;
shoaib_ahmed 0:791a779d6220 59 int tmp = done * 100 / total;
shoaib_ahmed 0:791a779d6220 60 if (n != tmp)
shoaib_ahmed 0:791a779d6220 61 {
shoaib_ahmed 0:791a779d6220 62 n = tmp;
shoaib_ahmed 0:791a779d6220 63 DEBMSG("Writing...: %3d%%", n);
shoaib_ahmed 0:791a779d6220 64 NEWLINE();
shoaib_ahmed 0:791a779d6220 65 }
shoaib_ahmed 0:791a779d6220 66 }
shoaib_ahmed 0:791a779d6220 67
shoaib_ahmed 0:791a779d6220 68 /* dimensions of the image we want to write */
shoaib_ahmed 0:791a779d6220 69 int width;
shoaib_ahmed 0:791a779d6220 70 int height;
shoaib_ahmed 0:791a779d6220 71 int bytes_per_pixel;
shoaib_ahmed 0:791a779d6220 72
shoaib_ahmed 0:791a779d6220 73 /* or 1 for GRACYSCALE images */
shoaib_ahmed 0:791a779d6220 74 int color_space; /* or JCS_GRAYSCALE for grayscale images */
shoaib_ahmed 0:791a779d6220 75
shoaib_ahmed 0:791a779d6220 76 typedef struct{
shoaib_ahmed 0:791a779d6220 77 long filesize;
shoaib_ahmed 0:791a779d6220 78 char reserved[2];
shoaib_ahmed 0:791a779d6220 79 long headersize;
shoaib_ahmed 0:791a779d6220 80 long infoSize;
shoaib_ahmed 0:791a779d6220 81 long width;
shoaib_ahmed 0:791a779d6220 82 long depth;
shoaib_ahmed 0:791a779d6220 83 short biPlanes;
shoaib_ahmed 0:791a779d6220 84 short bits;
shoaib_ahmed 0:791a779d6220 85 long biCompression;
shoaib_ahmed 0:791a779d6220 86 long biSizeImage;
shoaib_ahmed 0:791a779d6220 87 long biXPelsPerMeter;
shoaib_ahmed 0:791a779d6220 88 long biYPelsPerMeter;
shoaib_ahmed 0:791a779d6220 89 long biClrUsed;
shoaib_ahmed 0:791a779d6220 90 long biClrImportant;
shoaib_ahmed 0:791a779d6220 91 } BMPHEAD;
shoaib_ahmed 0:791a779d6220 92 // ~convertjpegtobmp
shoaib_ahmed 0:791a779d6220 93
shoaib_ahmed 0:791a779d6220 94 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 95 // convert .jpeg image to .bmp image //
shoaib_ahmed 0:791a779d6220 96 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 97
shoaib_ahmed 0:791a779d6220 98 int Convert_output(char *filename)
shoaib_ahmed 0:791a779d6220 99 {
shoaib_ahmed 0:791a779d6220 100 sd.mount();
shoaib_ahmed 0:791a779d6220 101
shoaib_ahmed 0:791a779d6220 102 BMPHEAD bh;
shoaib_ahmed 0:791a779d6220 103
shoaib_ahmed 0:791a779d6220 104 memset((char *)&bh, 0, sizeof(BMPHEAD)); /* sets everything to 0 */
shoaib_ahmed 0:791a779d6220 105 bh.headersize = 54L; //for 24 bit images)
shoaib_ahmed 0:791a779d6220 106 bh.infoSize = 0x28L; //for 24 bit images)
shoaib_ahmed 0:791a779d6220 107 bh.width = width; //in pixels of your image
shoaib_ahmed 0:791a779d6220 108 bh.depth = height; // in pixels of your image
shoaib_ahmed 0:791a779d6220 109 bh.biPlanes = 1; //for 24 bit images)
shoaib_ahmed 0:791a779d6220 110 bh.bits = 24; //for 24 bit images)
shoaib_ahmed 0:791a779d6220 111 bh.biCompression = 0L;; //no compression)
shoaib_ahmed 0:791a779d6220 112
shoaib_ahmed 0:791a779d6220 113 int bytesPerLine;
shoaib_ahmed 0:791a779d6220 114
shoaib_ahmed 0:791a779d6220 115 bytesPerLine = width * 3; /* (for 24 bit images) */
shoaib_ahmed 0:791a779d6220 116 /* round up to a dword boundary */
shoaib_ahmed 0:791a779d6220 117 if (bytesPerLine & 0x0003)
shoaib_ahmed 0:791a779d6220 118 {
shoaib_ahmed 0:791a779d6220 119 bytesPerLine |= 0x0003;
shoaib_ahmed 0:791a779d6220 120 ++bytesPerLine;
shoaib_ahmed 0:791a779d6220 121 }
shoaib_ahmed 0:791a779d6220 122 bh.filesize = bh.headersize + (long)bytesPerLine*bh.depth;
shoaib_ahmed 0:791a779d6220 123
shoaib_ahmed 0:791a779d6220 124 FILE * bmpfile;
shoaib_ahmed 0:791a779d6220 125
shoaib_ahmed 0:791a779d6220 126 printf("Bytes per line : %d\n", bytesPerLine);
shoaib_ahmed 0:791a779d6220 127
shoaib_ahmed 0:791a779d6220 128 bmpfile = fopen(filename, "wb");
shoaib_ahmed 0:791a779d6220 129 if (bmpfile == NULL)
shoaib_ahmed 0:791a779d6220 130 {
shoaib_ahmed 0:791a779d6220 131 printf("Error opening output file\n");
shoaib_ahmed 0:791a779d6220 132 /* -- close all open files and free any allocated memory -- */
shoaib_ahmed 0:791a779d6220 133 exit(1);
shoaib_ahmed 0:791a779d6220 134 }
shoaib_ahmed 0:791a779d6220 135 fwrite("BM", 1, 2, bmpfile);
shoaib_ahmed 0:791a779d6220 136 fwrite((char *)&bh, 1, sizeof(bh), bmpfile);
shoaib_ahmed 0:791a779d6220 137
shoaib_ahmed 0:791a779d6220 138 char *linebuf;
shoaib_ahmed 0:791a779d6220 139
shoaib_ahmed 0:791a779d6220 140 linebuf = (char *)calloc(1, bytesPerLine);
shoaib_ahmed 0:791a779d6220 141 if (linebuf == NULL)
shoaib_ahmed 0:791a779d6220 142 {
shoaib_ahmed 0:791a779d6220 143 printf("Error allocating memory\n");
shoaib_ahmed 0:791a779d6220 144 free(raw_image);
shoaib_ahmed 0:791a779d6220 145 /* -- close all open files and free any allocated memory -- */
shoaib_ahmed 0:791a779d6220 146 exit(1);
shoaib_ahmed 0:791a779d6220 147 }
shoaib_ahmed 0:791a779d6220 148
shoaib_ahmed 0:791a779d6220 149
shoaib_ahmed 0:791a779d6220 150 int line, x;
shoaib_ahmed 0:791a779d6220 151
shoaib_ahmed 0:791a779d6220 152 for (line = height - 1; line >= 0; line--)
shoaib_ahmed 0:791a779d6220 153 {
shoaib_ahmed 0:791a779d6220 154 /* fill line linebuf with the image data for that line */
shoaib_ahmed 0:791a779d6220 155 for (x = 0; x < width; x++)
shoaib_ahmed 0:791a779d6220 156 {
shoaib_ahmed 0:791a779d6220 157 *(linebuf + x*bytes_per_pixel) = *(raw_image + (x + line*width)*bytes_per_pixel + 2);
shoaib_ahmed 0:791a779d6220 158 *(linebuf + x*bytes_per_pixel + 1) = *(raw_image + (x + line*width)*bytes_per_pixel + 1);
shoaib_ahmed 0:791a779d6220 159 *(linebuf + x*bytes_per_pixel + 2) = *(raw_image + (x + line*width)*bytes_per_pixel + 0);
shoaib_ahmed 0:791a779d6220 160 }
shoaib_ahmed 0:791a779d6220 161 fwrite(linebuf, 1, bytesPerLine, bmpfile);
shoaib_ahmed 0:791a779d6220 162 }
shoaib_ahmed 0:791a779d6220 163 free(linebuf);
shoaib_ahmed 0:791a779d6220 164 fclose(bmpfile);
shoaib_ahmed 0:791a779d6220 165 printf("bytes writing process complete");
shoaib_ahmed 0:791a779d6220 166 }
shoaib_ahmed 0:791a779d6220 167
shoaib_ahmed 0:791a779d6220 168
shoaib_ahmed 0:791a779d6220 169 int Convert_input(char *filename)
shoaib_ahmed 0:791a779d6220 170 {
shoaib_ahmed 0:791a779d6220 171 /* these are standard libjpeg structures for reading(decompression) */
shoaib_ahmed 0:791a779d6220 172 struct jpeg_decompress_struct cinfo;
shoaib_ahmed 0:791a779d6220 173 struct jpeg_error_mgr jerr;
shoaib_ahmed 0:791a779d6220 174 /* libjpeg data structure for storing one row, that is, scanline of an image */
shoaib_ahmed 0:791a779d6220 175 JSAMPROW row_pointer[1];
shoaib_ahmed 0:791a779d6220 176
shoaib_ahmed 0:791a779d6220 177 FILE *infile = fopen(filename,"rb");
shoaib_ahmed 0:791a779d6220 178 unsigned long location = 0;
shoaib_ahmed 0:791a779d6220 179 int i = 0;
shoaib_ahmed 0:791a779d6220 180
shoaib_ahmed 0:791a779d6220 181 if (!infile)
shoaib_ahmed 0:791a779d6220 182 {
shoaib_ahmed 0:791a779d6220 183 printf("Error opening jpeg file %s\n!", filename);
shoaib_ahmed 0:791a779d6220 184 return -1;
shoaib_ahmed 0:791a779d6220 185 }
shoaib_ahmed 0:791a779d6220 186
shoaib_ahmed 0:791a779d6220 187 /* here we set up the standard libjpeg error handler */
shoaib_ahmed 0:791a779d6220 188 cinfo.err = jpeg_std_error(&jerr);
shoaib_ahmed 0:791a779d6220 189 /* setup decompression process and source, then read JPEG header */
shoaib_ahmed 0:791a779d6220 190 jpeg_create_decompress(&cinfo);
shoaib_ahmed 0:791a779d6220 191 /* this makes the library read from infile */
shoaib_ahmed 0:791a779d6220 192 jpeg_stdio_src(&cinfo, infile);
shoaib_ahmed 0:791a779d6220 193 /* reading the image header which contains image information */
shoaib_ahmed 0:791a779d6220 194 jpeg_read_header(&cinfo, TRUE);
shoaib_ahmed 0:791a779d6220 195 /* Uncomment the following to output image information, if needed. */
shoaib_ahmed 0:791a779d6220 196
shoaib_ahmed 0:791a779d6220 197 printf("JPEG File Information: \n");
shoaib_ahmed 0:791a779d6220 198 printf("Image width and height: %d pixels and %d pixels.\n", width = cinfo.image_width, height = cinfo.image_height);
shoaib_ahmed 0:791a779d6220 199 printf("Color components per pixel: %d.\n", bytes_per_pixel = cinfo.num_components);
shoaib_ahmed 0:791a779d6220 200 printf("Color space: %d.\n", cinfo.jpeg_color_space);
shoaib_ahmed 0:791a779d6220 201
shoaib_ahmed 0:791a779d6220 202 /* Start decompression jpeg here */
shoaib_ahmed 0:791a779d6220 203 jpeg_start_decompress(&cinfo);
shoaib_ahmed 0:791a779d6220 204
shoaib_ahmed 0:791a779d6220 205 /* allocate memory to hold the uncompressed image */
shoaib_ahmed 0:791a779d6220 206 raw_image = (unsigned char*)malloc(cinfo.output_width*cinfo.output_height*cinfo.num_components);
shoaib_ahmed 0:791a779d6220 207 /* now actually read the jpeg into the raw buffer */
shoaib_ahmed 0:791a779d6220 208 row_pointer[0] = (unsigned char *)malloc(cinfo.output_width*cinfo.num_components);
shoaib_ahmed 0:791a779d6220 209 /* read one scan line at a time */
shoaib_ahmed 0:791a779d6220 210 while (cinfo.output_scanline < cinfo.image_height)
shoaib_ahmed 0:791a779d6220 211 {
shoaib_ahmed 0:791a779d6220 212 jpeg_read_scanlines(&cinfo, row_pointer, 1);
shoaib_ahmed 0:791a779d6220 213 for (i = 0; i<cinfo.image_width*cinfo.num_components; i++)
shoaib_ahmed 0:791a779d6220 214 raw_image[location++] = row_pointer[0][i];
shoaib_ahmed 0:791a779d6220 215 }
shoaib_ahmed 0:791a779d6220 216
shoaib_ahmed 0:791a779d6220 217 jpeg_finish_decompress(&cinfo);
shoaib_ahmed 0:791a779d6220 218 jpeg_destroy_decompress(&cinfo);
shoaib_ahmed 0:791a779d6220 219 free(row_pointer[0]);
shoaib_ahmed 0:791a779d6220 220 fclose(infile);
shoaib_ahmed 0:791a779d6220 221 return 1;
shoaib_ahmed 0:791a779d6220 222 }
shoaib_ahmed 0:791a779d6220 223
shoaib_ahmed 0:791a779d6220 224 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 225 // ~convert .jpeg image to .bmp image //
shoaib_ahmed 0:791a779d6220 226 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 227
shoaib_ahmed 0:791a779d6220 228 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 229 // HTTP File SERVER //
shoaib_ahmed 0:791a779d6220 230 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 231
shoaib_ahmed 0:791a779d6220 232
shoaib_ahmed 0:791a779d6220 233 void ledTickfunc()
shoaib_ahmed 0:791a779d6220 234 {
shoaib_ahmed 0:791a779d6220 235 if(serverIsListened)
shoaib_ahmed 0:791a779d6220 236 {
shoaib_ahmed 0:791a779d6220 237 led1 = !led1;
shoaib_ahmed 0:791a779d6220 238 }
shoaib_ahmed 0:791a779d6220 239 else
shoaib_ahmed 0:791a779d6220 240 {
shoaib_ahmed 0:791a779d6220 241 led1 = false;
shoaib_ahmed 0:791a779d6220 242 }
shoaib_ahmed 0:791a779d6220 243 }
shoaib_ahmed 0:791a779d6220 244
shoaib_ahmed 0:791a779d6220 245 void send_HTTP_header(char* protocol, int code, char* title, char* mime_type, long long lengthBody)
shoaib_ahmed 0:791a779d6220 246 {
shoaib_ahmed 0:791a779d6220 247 snprintf(line_response, sizeof(line_response),"%s %d %s\r\n", protocol, code, title );
shoaib_ahmed 0:791a779d6220 248 snprintf(sentBuffer, sizeof(sentBuffer),"%s",line_response);
shoaib_ahmed 0:791a779d6220 249
shoaib_ahmed 0:791a779d6220 250 if ( mime_type != NULL )
shoaib_ahmed 0:791a779d6220 251 {
shoaib_ahmed 0:791a779d6220 252 snprintf(line_response, sizeof(line_response), "Content-Type: %s\r\n", mime_type );
shoaib_ahmed 0:791a779d6220 253 snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer
shoaib_ahmed 0:791a779d6220 254 }
shoaib_ahmed 0:791a779d6220 255 if ( lengthBody >= 0 )
shoaib_ahmed 0:791a779d6220 256 {
shoaib_ahmed 0:791a779d6220 257 snprintf(line_response, sizeof(line_response), "Content-Length: %lld\r\n", lengthBody );
shoaib_ahmed 0:791a779d6220 258 snprintf(sentBuffer, sizeof(sentBuffer), "%s%s",sentBuffer,line_response); //append to sentBuffer
shoaib_ahmed 0:791a779d6220 259 }
shoaib_ahmed 0:791a779d6220 260 snprintf(line_response, sizeof(line_response), "Connection: close\r\n" );
shoaib_ahmed 0:791a779d6220 261 snprintf(sentBuffer, sizeof(sentBuffer),"%s%s\r\n",sentBuffer,line_response); //append to sentBuffer
shoaib_ahmed 0:791a779d6220 262
shoaib_ahmed 0:791a779d6220 263 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 264 printf("\r\n-->sent Header--\r\n");
shoaib_ahmed 0:791a779d6220 265
shoaib_ahmed 0:791a779d6220 266 client.send_all(sentBuffer,strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 267
shoaib_ahmed 0:791a779d6220 268 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 269 {
shoaib_ahmed 0:791a779d6220 270 printf(sentBuffer);
shoaib_ahmed 0:791a779d6220 271 printf("\r\n--end Header-- bytes:%d",strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 272 }
shoaib_ahmed 0:791a779d6220 273 wait(0.2); //200ms important for browser!
shoaib_ahmed 0:791a779d6220 274 }
shoaib_ahmed 0:791a779d6220 275
shoaib_ahmed 0:791a779d6220 276 void send_HTML_line(char* line, unsigned int length_line)
shoaib_ahmed 0:791a779d6220 277 {
shoaib_ahmed 0:791a779d6220 278 client.send_all(line,length_line);
shoaib_ahmed 0:791a779d6220 279 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 280 printf("\r\n-->send HTML line:\r\n%s ...Ok!",line);
shoaib_ahmed 0:791a779d6220 281 wait(0.01);
shoaib_ahmed 0:791a779d6220 282 }
shoaib_ahmed 0:791a779d6220 283
shoaib_ahmed 0:791a779d6220 284 void send_HTML_error( int status_code, char* title, char* body_text)
shoaib_ahmed 0:791a779d6220 285 {
shoaib_ahmed 0:791a779d6220 286 send_HTTP_header("HTTP/1.1", status_code, title, "text/html", -1);
shoaib_ahmed 0:791a779d6220 287 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 288 printf("\r\n-->send_error...\r\n");
shoaib_ahmed 0:791a779d6220 289 sentBuffer[0]=NULL; //clear buffer
shoaib_ahmed 0:791a779d6220 290 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);
shoaib_ahmed 0:791a779d6220 291 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 292 sprintf(line_response, "<body><center><h2><center>%d %s</center></h2>\r\n",status_code, title );
shoaib_ahmed 0:791a779d6220 293 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 294 sprintf(line_response, "%s\r\n", body_text );
shoaib_ahmed 0:791a779d6220 295 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 296 sprintf(line_response, "<p>mbed HTTP File Server</p>\r\n</center></body></html>\r\n");
shoaib_ahmed 0:791a779d6220 297 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 298 send_HTML_line(sentBuffer, strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 299 }
shoaib_ahmed 0:791a779d6220 300
shoaib_ahmed 0:791a779d6220 301
shoaib_ahmed 0:791a779d6220 302 int send_file(char *path_file)
shoaib_ahmed 0:791a779d6220 303 {
shoaib_ahmed 0:791a779d6220 304 char *mime_type = {0};
shoaib_ahmed 0:791a779d6220 305 unsigned int bytes_for_send=0;
shoaib_ahmed 0:791a779d6220 306 long long filesize, all_send_bytes = 0;
shoaib_ahmed 0:791a779d6220 307
shoaib_ahmed 0:791a779d6220 308 mime_type = get_mime_type( path_file );
shoaib_ahmed 0:791a779d6220 309 snprintf(file_path, sizeof(file_path),"/sd/%s",path_file);
shoaib_ahmed 0:791a779d6220 310
shoaib_ahmed 0:791a779d6220 311 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 312 {
shoaib_ahmed 0:791a779d6220 313 printf("\r\n-->from send_file:%s",file_path);
shoaib_ahmed 0:791a779d6220 314 printf("\r\n-->from send_file mime type:%s",mime_type);
shoaib_ahmed 0:791a779d6220 315 }
shoaib_ahmed 0:791a779d6220 316
shoaib_ahmed 0:791a779d6220 317 if (Mystat(path_file, &myStatBuf))
shoaib_ahmed 0:791a779d6220 318 {
shoaib_ahmed 0:791a779d6220 319 //fault with file
shoaib_ahmed 0:791a779d6220 320 send_HTML_error( 403, "Forbidden", "403 - File access forbidden.");
shoaib_ahmed 0:791a779d6220 321 return 403;
shoaib_ahmed 0:791a779d6220 322 }
shoaib_ahmed 0:791a779d6220 323
shoaib_ahmed 0:791a779d6220 324 FILE* fp = NULL;
shoaib_ahmed 0:791a779d6220 325 fp = fopen(file_path,"r");
shoaib_ahmed 0:791a779d6220 326
shoaib_ahmed 0:791a779d6220 327 if (fp==NULL )
shoaib_ahmed 0:791a779d6220 328 {
shoaib_ahmed 0:791a779d6220 329 send_HTML_error( 403, "Forbidden", "403 - File access forbidden.");
shoaib_ahmed 0:791a779d6220 330 return 403;
shoaib_ahmed 0:791a779d6220 331 }
shoaib_ahmed 0:791a779d6220 332
shoaib_ahmed 0:791a779d6220 333 filesize = myStatBuf.st_size;
shoaib_ahmed 0:791a779d6220 334 send_HTTP_header("HTTP/1.1", 200, "Ok", mime_type, myStatBuf.st_size);
shoaib_ahmed 0:791a779d6220 335
shoaib_ahmed 0:791a779d6220 336 //binary send
shoaib_ahmed 0:791a779d6220 337 all_send_bytes=0;
shoaib_ahmed 0:791a779d6220 338
shoaib_ahmed 0:791a779d6220 339 while(filesize)
shoaib_ahmed 0:791a779d6220 340 {
shoaib_ahmed 0:791a779d6220 341 //check for EOF !feof(fp)
shoaib_ahmed 0:791a779d6220 342 bytes_for_send = filesize;
shoaib_ahmed 0:791a779d6220 343 if (bytes_for_send > sizeof(sentBuffer))
shoaib_ahmed 0:791a779d6220 344 {
shoaib_ahmed 0:791a779d6220 345 bytes_for_send = sizeof(sentBuffer);
shoaib_ahmed 0:791a779d6220 346 }
shoaib_ahmed 0:791a779d6220 347 fread (sentBuffer,1,bytes_for_send,fp);
shoaib_ahmed 0:791a779d6220 348 filesize -= bytes_for_send;
shoaib_ahmed 0:791a779d6220 349 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 350 printf("\r\n---bytes_for_send...%d",bytes_for_send);
shoaib_ahmed 0:791a779d6220 351 client.send_all(sentBuffer,bytes_for_send);
shoaib_ahmed 0:791a779d6220 352 sentBuffer[0]=NULL; //clear buffer
shoaib_ahmed 0:791a779d6220 353 //Thread::wait(10);
shoaib_ahmed 0:791a779d6220 354 all_send_bytes += bytes_for_send;
shoaib_ahmed 0:791a779d6220 355 }
shoaib_ahmed 0:791a779d6220 356 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 357 printf("\r\n---buffer fill end - all ...%lld", all_send_bytes);
shoaib_ahmed 0:791a779d6220 358 //binary send
shoaib_ahmed 0:791a779d6220 359
shoaib_ahmed 0:791a779d6220 360 sprintf(line_response, "\r\n");
shoaib_ahmed 0:791a779d6220 361 client.send_all(line_response,strlen(line_response));
shoaib_ahmed 0:791a779d6220 362 if ( fp != NULL )
shoaib_ahmed 0:791a779d6220 363 fclose(fp);
shoaib_ahmed 0:791a779d6220 364 //Thread::wait(10);
shoaib_ahmed 0:791a779d6220 365 return 0;
shoaib_ahmed 0:791a779d6220 366 }
shoaib_ahmed 0:791a779d6220 367
shoaib_ahmed 0:791a779d6220 368 int send_directory(char *path)
shoaib_ahmed 0:791a779d6220 369 {
shoaib_ahmed 0:791a779d6220 370 char process_name[64]= {0};
shoaib_ahmed 0:791a779d6220 371 char posOfLastSlash;
shoaib_ahmed 0:791a779d6220 372 char *pLS;
shoaib_ahmed 0:791a779d6220 373 struct dirent *p;
shoaib_ahmed 0:791a779d6220 374 struct sMystat sb;
shoaib_ahmed 0:791a779d6220 375 struct tm *timeinfo;
shoaib_ahmed 0:791a779d6220 376 char timeBuf[40];
shoaib_ahmed 0:791a779d6220 377
shoaib_ahmed 0:791a779d6220 378 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 379 printf("\n-->from send_directory:%s",path);
shoaib_ahmed 0:791a779d6220 380 snprintf(file_path,sizeof(file_path),"/sd%s",path);
shoaib_ahmed 0:791a779d6220 381 DIR *d = opendir(file_path);
shoaib_ahmed 0:791a779d6220 382 if (EnDebugMSG && d!=NULL)
shoaib_ahmed 0:791a779d6220 383 printf("\n-->from send_directory:%s ...open OK",file_path);
shoaib_ahmed 0:791a779d6220 384 if (d==NULL)
shoaib_ahmed 0:791a779d6220 385 {
shoaib_ahmed 0:791a779d6220 386 //error open dir
shoaib_ahmed 0:791a779d6220 387 send_HTML_error( 403, "Forbidden", "403 - Directory access forbidden.");
shoaib_ahmed 0:791a779d6220 388 return -1;
shoaib_ahmed 0:791a779d6220 389 }
shoaib_ahmed 0:791a779d6220 390 send_HTTP_header("HTTP/1.1", 200, "Ok",NULL, -1);
shoaib_ahmed 0:791a779d6220 391 sentBuffer[0]=NULL;
shoaib_ahmed 0:791a779d6220 392 sprintf(line_response,"<!DOCTYPE html>\r\n<html>\n<head><title>Index of %s</title>\n",path);
shoaib_ahmed 0:791a779d6220 393 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 394 sprintf(line_response,"<meta content=\"text/html; charset=iso-8859-1\" http-equiv=\"Content-Type\"></head>\n");
shoaib_ahmed 0:791a779d6220 395 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 396 sprintf(line_response,"<body><center>\n<h3>Index of %s</h3>\n", path);
shoaib_ahmed 0:791a779d6220 397 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 398 send_HTML_line(sentBuffer, strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 399 //begin table
shoaib_ahmed 0:791a779d6220 400 sentBuffer[0]=NULL; //clear buffer
shoaib_ahmed 0:791a779d6220 401 sprintf(line_response,"<table border=\"0\">\n");
shoaib_ahmed 0:791a779d6220 402 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 403 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");
shoaib_ahmed 0:791a779d6220 404 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 405 //begin table
shoaib_ahmed 0:791a779d6220 406 pLS=strrchr(path,'/');
shoaib_ahmed 0:791a779d6220 407 posOfLastSlash=pLS-path+1;
shoaib_ahmed 0:791a779d6220 408 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 409 printf("\r\n>>posOfLastSlash=%d",posOfLastSlash);
shoaib_ahmed 0:791a779d6220 410 snprintf(process_name,posOfLastSlash+1,"%s",path);
shoaib_ahmed 0:791a779d6220 411 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 412 printf("\r\n>>process_name=%s",process_name);
shoaib_ahmed 0:791a779d6220 413 //sprintf(line_response,"<tr><td align=\"left\"><a href=\"%s\">../</a></td></tr>\n",process_name);
shoaib_ahmed 0:791a779d6220 414 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 415 d= opendir("/sd/");
shoaib_ahmed 0:791a779d6220 416 while((p = readdir(d)) != NULL)
shoaib_ahmed 0:791a779d6220 417 {
shoaib_ahmed 0:791a779d6220 418 printf("\n inside while loop p = readdir(d)\n" );
shoaib_ahmed 0:791a779d6220 419 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 420 printf("\n :%s",p->d_name);
shoaib_ahmed 0:791a779d6220 421 sprintf(file_path,"%s/%s",path,p->d_name);
shoaib_ahmed 0:791a779d6220 422 Mystat( file_path, &sb );
shoaib_ahmed 0:791a779d6220 423 if (get_dirInfo(file_path)==0 )
shoaib_ahmed 0:791a779d6220 424 {
shoaib_ahmed 0:791a779d6220 425 //this is directory path
shoaib_ahmed 0:791a779d6220 426 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 427 printf("\nDIR");
shoaib_ahmed 0:791a779d6220 428 sprintf(line_response, "<tr><td align=\"left\"><a href=\"%s\">%s</a><br></td></tr>\n",file_path,p->d_name);
shoaib_ahmed 0:791a779d6220 429 if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer)))
shoaib_ahmed 0:791a779d6220 430 {
shoaib_ahmed 0:791a779d6220 431 //buffer must be sent
shoaib_ahmed 0:791a779d6220 432 send_HTML_line(sentBuffer, strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 433 sentBuffer[0]=NULL; //clear buffer
shoaib_ahmed 0:791a779d6220 434 }
shoaib_ahmed 0:791a779d6220 435 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 436
shoaib_ahmed 0:791a779d6220 437 }
shoaib_ahmed 0:791a779d6220 438 else
shoaib_ahmed 0:791a779d6220 439 { //this is file
shoaib_ahmed 0:791a779d6220 440 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 441 printf("\nFILE");
shoaib_ahmed 0:791a779d6220 442 timeinfo = localtime (&sb.st_mtime);
shoaib_ahmed 0:791a779d6220 443 //strftime(timeBuf,40, "%I:%M:%S %p (%Y/%m/%d)\r\n", localtime(&sb.st_mtime));
shoaib_ahmed 0:791a779d6220 444 strftime(timeBuf, 40, "%c", timeinfo);
shoaib_ahmed 0:791a779d6220 445 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) );
shoaib_ahmed 0:791a779d6220 446
shoaib_ahmed 0:791a779d6220 447 if (strlen(line_response)>(sizeof(sentBuffer)-strlen(sentBuffer)))
shoaib_ahmed 0:791a779d6220 448 {
shoaib_ahmed 0:791a779d6220 449 //buffer must be sent
shoaib_ahmed 0:791a779d6220 450 send_HTML_line(sentBuffer, strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 451 sentBuffer[0]=NULL; //clear buffer
shoaib_ahmed 0:791a779d6220 452 }
shoaib_ahmed 0:791a779d6220 453 snprintf(&(sentBuffer[strlen(sentBuffer)]),sizeof(sentBuffer),"%s",line_response); //append to buffer
shoaib_ahmed 0:791a779d6220 454 }
shoaib_ahmed 0:791a779d6220 455 }
shoaib_ahmed 0:791a779d6220 456 send_HTML_line(sentBuffer, strlen(sentBuffer));
shoaib_ahmed 0:791a779d6220 457 closedir(d);
shoaib_ahmed 0:791a779d6220 458
shoaib_ahmed 0:791a779d6220 459 sprintf(line_response, "</table>\n<br><h4>mbed HTTP File Server</h4>\n</center></body></html>\n");
shoaib_ahmed 0:791a779d6220 460 send_HTML_line(line_response, strlen(line_response));
shoaib_ahmed 0:791a779d6220 461
shoaib_ahmed 0:791a779d6220 462 return 0;
shoaib_ahmed 0:791a779d6220 463 }
shoaib_ahmed 0:791a779d6220 464
shoaib_ahmed 0:791a779d6220 465 void parseHTTPRequest(char* buffer)
shoaib_ahmed 0:791a779d6220 466 {
shoaib_ahmed 0:791a779d6220 467 sd.mount();
shoaib_ahmed 0:791a779d6220 468 char spacePos;
shoaib_ahmed 0:791a779d6220 469 char *tmpBuffer;
shoaib_ahmed 0:791a779d6220 470 spacePos = strcspn(buffer, " ") + 1; //position of first space character
shoaib_ahmed 0:791a779d6220 471 snprintf(sMethod, spacePos,"%s", buffer);
shoaib_ahmed 0:791a779d6220 472
shoaib_ahmed 0:791a779d6220 473 //get Protocol
shoaib_ahmed 0:791a779d6220 474 tmpBuffer=&(buffer[spacePos]); //move pointer to buffer (delete Method)
shoaib_ahmed 0:791a779d6220 475 spacePos = strcspn(tmpBuffer, "\r\n") + 1;
shoaib_ahmed 0:791a779d6220 476 tmpBuffer[spacePos]='\0'; //set end of string ...cut
shoaib_ahmed 0:791a779d6220 477 sprintf(sProtocol, "%s", strrchr(tmpBuffer,' ')); //get string after last (space )
shoaib_ahmed 0:791a779d6220 478 printf("\r\nsProtocol:%s", tmpBuffer);
shoaib_ahmed 0:791a779d6220 479 buffer = &(sProtocol[1]); //cut first character (space)
shoaib_ahmed 0:791a779d6220 480 sprintf(sProtocol, "%s", buffer);
shoaib_ahmed 0:791a779d6220 481
shoaib_ahmed 0:791a779d6220 482 //get URL
shoaib_ahmed 0:791a779d6220 483 snprintf(sURL,strlen(tmpBuffer)-strlen(sProtocol),"%s\r\n", tmpBuffer); //URL is between Method and Protocol
shoaib_ahmed 0:791a779d6220 484
shoaib_ahmed 0:791a779d6220 485 printf("\nParse Method:%s",sMethod);
shoaib_ahmed 0:791a779d6220 486 printf("\nParse URL:%s",sURL);
shoaib_ahmed 0:791a779d6220 487 printf("\nParse PROTOCOL:%s",sProtocol);
shoaib_ahmed 0:791a779d6220 488 printf("\n\r\n");
shoaib_ahmed 0:791a779d6220 489 }
shoaib_ahmed 0:791a779d6220 490
shoaib_ahmed 0:791a779d6220 491 int processHTTP(char* sMethod, char* sURL, char* sProtocol)
shoaib_ahmed 0:791a779d6220 492 {
shoaib_ahmed 0:791a779d6220 493 int gdi, gfi; //status of get_dir_info(xxx), and get_file_info(xxx)
shoaib_ahmed 0:791a779d6220 494
shoaib_ahmed 0:791a779d6220 495 if (strcmp(sMethod,"GET")!=0)
shoaib_ahmed 0:791a779d6220 496 {
shoaib_ahmed 0:791a779d6220 497 send_HTML_error( 501, "501 Not Implemented", "501 - The server either does not recognize the request method");
shoaib_ahmed 0:791a779d6220 498 return 501;
shoaib_ahmed 0:791a779d6220 499 }
shoaib_ahmed 0:791a779d6220 500 if (sURL[0]!= '/')
shoaib_ahmed 0:791a779d6220 501 {
shoaib_ahmed 0:791a779d6220 502 send_HTML_error( 400, "Bad Request", "400 - The request cannot be fulfilled due to bad syntax.");
shoaib_ahmed 0:791a779d6220 503 return 400;
shoaib_ahmed 0:791a779d6220 504 }
shoaib_ahmed 0:791a779d6220 505 if (sURL[strlen(sURL)-1]=='/')
shoaib_ahmed 0:791a779d6220 506 {
shoaib_ahmed 0:791a779d6220 507 sURL[strlen(sURL)-1]=sURL[strlen(sURL)]; //delete last symbol
shoaib_ahmed 0:791a779d6220 508 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 509 printf("\n delete last:%s",sURL);
shoaib_ahmed 0:791a779d6220 510 }
shoaib_ahmed 0:791a779d6220 511 // send_file(sURL);
shoaib_ahmed 0:791a779d6220 512
shoaib_ahmed 0:791a779d6220 513 gdi= get_dirInfo(sURL);
shoaib_ahmed 0:791a779d6220 514 gfi= get_fileInfo(sURL);
shoaib_ahmed 0:791a779d6220 515
shoaib_ahmed 0:791a779d6220 516 if (gfi!=0)
shoaib_ahmed 0:791a779d6220 517 {
shoaib_ahmed 0:791a779d6220 518 //!=0 file not found
shoaib_ahmed 0:791a779d6220 519 if (gdi==0)
shoaib_ahmed 0:791a779d6220 520 {
shoaib_ahmed 0:791a779d6220 521 //0-ok this is directory
shoaib_ahmed 0:791a779d6220 522 return send_directory(sURL);
shoaib_ahmed 0:791a779d6220 523 }
shoaib_ahmed 0:791a779d6220 524 if (EnDebugMSG)
shoaib_ahmed 0:791a779d6220 525 printf("\n404-br File not found or...(Fresult is:%d)",gfi);
shoaib_ahmed 0:791a779d6220 526 send_HTML_error( 404, "Not Found","404 - The requested resource could not be found.");
shoaib_ahmed 0:791a779d6220 527 return 404;
shoaib_ahmed 0:791a779d6220 528 }
shoaib_ahmed 0:791a779d6220 529 else
shoaib_ahmed 0:791a779d6220 530 {
shoaib_ahmed 0:791a779d6220 531 //==0 found
shoaib_ahmed 0:791a779d6220 532 if (gdi==0) //0-ok this is directory
shoaib_ahmed 0:791a779d6220 533 return send_directory(sURL);
shoaib_ahmed 0:791a779d6220 534 else
shoaib_ahmed 0:791a779d6220 535 return send_file(sURL);
shoaib_ahmed 0:791a779d6220 536 }
shoaib_ahmed 0:791a779d6220 537 }
shoaib_ahmed 0:791a779d6220 538 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 539 // HTTP File SERVER //
shoaib_ahmed 0:791a779d6220 540 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 541
shoaib_ahmed 0:791a779d6220 542 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 543 // Main //
shoaib_ahmed 0:791a779d6220 544 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
shoaib_ahmed 0:791a779d6220 545
shoaib_ahmed 0:791a779d6220 546
shoaib_ahmed 0:791a779d6220 547 int main()
shoaib_ahmed 0:791a779d6220 548 {
shoaib_ahmed 0:791a779d6220 549
shoaib_ahmed 0:791a779d6220 550 int check=1;
shoaib_ahmed 0:791a779d6220 551 int newcheck=10;
shoaib_ahmed 0:791a779d6220 552 int insidecondition=0;
shoaib_ahmed 0:791a779d6220 553
shoaib_ahmed 0:791a779d6220 554 while(1)
shoaib_ahmed 0:791a779d6220 555 {
shoaib_ahmed 0:791a779d6220 556 pb.mode(PullUp);
shoaib_ahmed 0:791a779d6220 557 newcheck=pb;
shoaib_ahmed 0:791a779d6220 558 if(check==newcheck)
shoaib_ahmed 0:791a779d6220 559 {
shoaib_ahmed 0:791a779d6220 560 eth.init("192.168.240.100","255.255.255.0","192.168.240.1");
shoaib_ahmed 0:791a779d6220 561 eth.connect();
shoaib_ahmed 0:791a779d6220 562 printf("IP Address is %s\n\r", eth.getIPAddress());
shoaib_ahmed 0:791a779d6220 563
shoaib_ahmed 0:791a779d6220 564 //setup tcp socket
shoaib_ahmed 0:791a779d6220 565 if(svr.bind(PORT)< 0)
shoaib_ahmed 0:791a779d6220 566 {
shoaib_ahmed 0:791a779d6220 567 printf("tcp server bind failed.\n\r");
shoaib_ahmed 0:791a779d6220 568 return -1;
shoaib_ahmed 0:791a779d6220 569 }
shoaib_ahmed 0:791a779d6220 570 else
shoaib_ahmed 0:791a779d6220 571 {
shoaib_ahmed 0:791a779d6220 572 printf("tcp server bind successed.\n\r");
shoaib_ahmed 0:791a779d6220 573 serverIsListened = true;
shoaib_ahmed 0:791a779d6220 574 }
shoaib_ahmed 0:791a779d6220 575
shoaib_ahmed 0:791a779d6220 576 if(svr.listen(1) < 0)
shoaib_ahmed 0:791a779d6220 577 {
shoaib_ahmed 0:791a779d6220 578 printf("tcp server listen failed.\n\r");
shoaib_ahmed 0:791a779d6220 579 return -1;
shoaib_ahmed 0:791a779d6220 580 }
shoaib_ahmed 0:791a779d6220 581 else
shoaib_ahmed 0:791a779d6220 582 {
shoaib_ahmed 0:791a779d6220 583 sd.mount();
shoaib_ahmed 0:791a779d6220 584 char Input_Filename[] = "/sd/IMG_0000.jpg \n";
shoaib_ahmed 0:791a779d6220 585 char Output_Filename[] = "/sd/Output.bmp \n";
shoaib_ahmed 0:791a779d6220 586 Convert_input(Input_Filename);
shoaib_ahmed 0:791a779d6220 587 Convert_output(Output_Filename);
shoaib_ahmed 0:791a779d6220 588 free(raw_image);
shoaib_ahmed 0:791a779d6220 589 printf("\n image convertion process complete \n");
shoaib_ahmed 0:791a779d6220 590 printf("\n waiting for WEB file request \n");
shoaib_ahmed 0:791a779d6220 591 }
shoaib_ahmed 0:791a779d6220 592
shoaib_ahmed 0:791a779d6220 593 while (serverIsListened)
shoaib_ahmed 0:791a779d6220 594 {
shoaib_ahmed 0:791a779d6220 595 //blocking mode(never timeout)
shoaib_ahmed 0:791a779d6220 596 if(svr.accept(client)<0)
shoaib_ahmed 0:791a779d6220 597 {
shoaib_ahmed 0:791a779d6220 598 printf("failed to accept connection.\n\r");
shoaib_ahmed 0:791a779d6220 599 }
shoaib_ahmed 0:791a779d6220 600 else
shoaib_ahmed 0:791a779d6220 601 {
shoaib_ahmed 0:791a779d6220 602 if(insidecondition==2)
shoaib_ahmed 0:791a779d6220 603 {
shoaib_ahmed 0:791a779d6220 604 lcd.DrawBitmap(0,0,(uint8_t *)"/sd/new.bmp"); //displaying pic to lcd
shoaib_ahmed 0:791a779d6220 605 }
shoaib_ahmed 0:791a779d6220 606
shoaib_ahmed 0:791a779d6220 607 printf("connection success!\n\rIP: %s\n\r",client.get_address());
shoaib_ahmed 0:791a779d6220 608 clientIsConnected = true;
shoaib_ahmed 0:791a779d6220 609 led2 = true;
shoaib_ahmed 0:791a779d6220 610 while(clientIsConnected)
shoaib_ahmed 0:791a779d6220 611 {
shoaib_ahmed 0:791a779d6220 612 char buffer[512] = {};
shoaib_ahmed 0:791a779d6220 613 switch(client.receive(buffer, 511))
shoaib_ahmed 0:791a779d6220 614 {
shoaib_ahmed 0:791a779d6220 615 case 0:
shoaib_ahmed 0:791a779d6220 616 printf("recieved buffer is empty.\n\r");
shoaib_ahmed 0:791a779d6220 617 clientIsConnected = false;
shoaib_ahmed 0:791a779d6220 618 break;
shoaib_ahmed 0:791a779d6220 619 case -1:
shoaib_ahmed 0:791a779d6220 620 printf("failed to read data from client.\n\r");
shoaib_ahmed 0:791a779d6220 621 clientIsConnected = false;
shoaib_ahmed 0:791a779d6220 622 break;
shoaib_ahmed 0:791a779d6220 623 default:
shoaib_ahmed 0:791a779d6220 624 printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
shoaib_ahmed 0:791a779d6220 625 parseHTTPRequest(buffer);
shoaib_ahmed 0:791a779d6220 626
shoaib_ahmed 0:791a779d6220 627 if (strcmp(sMethod, "GET" ) == 0 )
shoaib_ahmed 0:791a779d6220 628 {
shoaib_ahmed 0:791a779d6220 629 printf("GET request incomming.\n\r");
shoaib_ahmed 0:791a779d6220 630 processHTTP(sMethod, sURL, sProtocol);
shoaib_ahmed 0:791a779d6220 631 clientIsConnected = false;
shoaib_ahmed 0:791a779d6220 632 }
shoaib_ahmed 0:791a779d6220 633 break;
shoaib_ahmed 0:791a779d6220 634 }
shoaib_ahmed 0:791a779d6220 635
shoaib_ahmed 0:791a779d6220 636 }
shoaib_ahmed 0:791a779d6220 637 printf("close connection.\n\rHTTP server is listening...\n\r\n");
shoaib_ahmed 0:791a779d6220 638 insidecondition=insidecondition+1;
shoaib_ahmed 0:791a779d6220 639 client.close();
shoaib_ahmed 0:791a779d6220 640 wait(1);
shoaib_ahmed 0:791a779d6220 641 led2 = false;
shoaib_ahmed 0:791a779d6220 642 }
shoaib_ahmed 0:791a779d6220 643 }
shoaib_ahmed 0:791a779d6220 644 }
shoaib_ahmed 0:791a779d6220 645 else
shoaib_ahmed 0:791a779d6220 646 {
shoaib_ahmed 0:791a779d6220 647 printf("Press button to start FS and Convert \n");
shoaib_ahmed 0:791a779d6220 648 }
shoaib_ahmed 0:791a779d6220 649
shoaib_ahmed 0:791a779d6220 650 }
shoaib_ahmed 0:791a779d6220 651
shoaib_ahmed 0:791a779d6220 652
shoaib_ahmed 0:791a779d6220 653 }