Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
FileDownloader.cpp
00001 #include "FileDownloader.h" 00002 #include "SDFileSystem.h" 00003 00004 extern SDFileSystem sd; 00005 HTTPResult result; 00006 bool completed = false; 00007 HTTPClient http; 00008 HTTPStream stream; 00009 00010 00011 void request_callback(HTTPResult r) { 00012 result = r; 00013 completed = true; 00014 } 00015 00016 HTTPResult GetFile(const char *uri, const char *filename, FILE *fp, char * buffer, int buffer_size) 00017 { 00018 00019 completed = false; 00020 00021 fp = fopen(filename, "w"); 00022 if(fp == NULL) { 00023 error("Could not open file for write\n"); 00024 } 00025 else { 00026 printf("Opened file\r\n"); 00027 } 00028 00029 00030 buffer[buffer_size-1] = 0x00; 00031 stream.readNext((byte*)buffer, buffer_size-2); //Point to buffer for the first read 00032 HTTPResult r = http.get(uri, &stream, request_callback); 00033 00034 while (!completed) { 00035 Net::poll(); //Polls the Networking stack 00036 if (stream.readable()) { 00037 buffer[stream.readLen()] = 0; //Transform this buffer in a zero-terminated char* string 00038 00039 int p=0; 00040 while (buffer[p] != 0) 00041 { 00042 fprintf(fp,"%c",buffer[p]); 00043 p++; 00044 } 00045 stream.readNext((byte*)buffer, buffer_size-2); //Buffer has been read, now we can put more data in it 00046 } 00047 } 00048 00049 fclose(fp); 00050 00051 return result; 00052 }
Generated on Tue Jul 12 2022 16:21:00 by
1.7.2