The program publishes files at local directory and SD filesystem. It is a full-fledged webServer somewhat.

Dependencies:   libMiMic mbed-rtos mbed NyFileSystems

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * @file * This is ModLocalFileSystem sample program.
00003  *
00004  * <pre>
00005  * 1. Compile this program and write to your mbed.
00006  * 2. Write files for testing to mbed disk.
00007  * 2. Access via web browser to http://192.168.0.39/local/<filename>
00008  * 3. You can see <filename> file on browser.
00009  * </pre>
00010  */
00011 #include "mbed.h"
00012 #include "rtos.h"
00013 #include "SDFileSystem.h"
00014 #include "mimic.h"
00015 #include "utils/PlatformInfo.h"
00016 #include "fsdata.h"
00017 
00018 DigitalOut mbedled(LED1);
00019 DigitalOut lpcxled(P0_22);
00020 
00021 /**
00022  * local filesystem support.
00023  */
00024 LocalFileSystem2 lf("local");
00025 SDFileSystem sd(p5, p6, p7, p8,"sd");
00026 
00027 unsigned int p;
00028 /**
00029  * MiMic RemoteMCU httpd.<br/>
00030  * Number of simultaneous connections:4
00031  * <p>Service list</p>
00032  * <pre>
00033  * /local/ - mbed LocalFileSystem
00034  * </pre>
00035  */
00036 class FsHttpd:public MiMic::Httpd
00037 {
00038 private:
00039     ModLocalFileSystem modlocal;
00040     ModLocalFileSystem modsd;
00041     ModUPnPDevice modupnp;
00042     ModRomFiles modromfs; //ROM file module
00043     
00044 public:
00045     FsHttpd(Net& i_net,NetConfig& i_cfg):Httpd(i_cfg.getHttpPort())
00046     {
00047     
00048         this->modromfs.setParam("rom",FSDATA,1);
00049         //bind local file system path to /local/*
00050         modlocal.setParam("local");
00051         modsd.setParam("sd",ModLocalFileSystem::FST_SDFATFS);
00052         this->modupnp.setParam(i_net);
00053     }
00054     virtual void onRequest(HttpdConnection& i_connection)
00055     {
00056         p++;
00057         switch(PlatformInfo::getPlatformType()){
00058         case PlatformInfo::PF_MBED:
00059             mbedled = p%2;
00060             break;
00061         case PlatformInfo::PF_LPCXPRESSO:        
00062             lpcxled = p%2;
00063             break;
00064         }
00065         //try to ModRomFS module.
00066         if(this->modromfs.execute(i_connection)){
00067             return;
00068         }        
00069         //try to ModLocalFileSystem
00070         if(this->modlocal.execute(i_connection)){
00071             return;
00072         }
00073         //try to ModLocalFileSystem(SD)
00074         if(this->modsd.execute(i_connection)){
00075             return;
00076         }
00077         if(this->modupnp.execute(i_connection)){
00078             return;
00079         }
00080         //Otherwise, Send simple top index page.
00081         i_connection.sendHeader(200,"text/html",NULL);
00082         if(i_connection.isMethodType(Http::MT_GET)){
00083             i_connection.sendBodyF(
00084                 "<!DOCTYPE html>"
00085                 "<html lang=\"ja\">"  
00086                 "<head></head>"
00087                 "<body>"
00088                 "<h1>This is MiMic Server!</h1>"
00089                 "<hr/>"
00090                 "<ul>"
00091                 "<li><a href=\"/local/\">mbed Local Filesystem</a></li>"
00092                 "<li><a href=\"/sd/\">SDCard</a></li>"
00093                 "</ul></body>");
00094         }
00095     }
00096 };
00097 
00098 FsHttpd* httpd; //create a httpd instance.
00099 
00100 int main()
00101 {
00102     NetConfig cfg; //create network configulation
00103     MiMicNetIf netif;
00104     Net net(netif);//Net constructor must be created after started RTOS
00105     //Prepare configulation.
00106     cfg.setUPnPIcon(64,64,8,"image/png","/rom/icon.png");
00107     cfg.setUPnPUdn(0xe29f7102,0x4ba2,0x01e0,0);
00108     cfg.setFriendlyName("MbedFileServer");
00109 
00110     //try to override setting by local file.
00111     if(!cfg.loadFromFile("/local/mimic.cfg")){
00112         Thread::wait(2000);//wait for SD card initialization.
00113         cfg.loadFromFile("/sd/mimic.cfg");
00114     }
00115     httpd=new FsHttpd(net,cfg); //create a httpd instance.
00116     net.start(cfg);
00117     httpd->loop();  //start httpd loop.
00118     return 0;
00119 }