Simplest UPnP basic device example. This program to run UPnP basic device on the mbed.
Dependencies: NyFileSystems libMiMic mbed-rtos mbed
Fork of MbedFileServer by
This is UPnP BasicDevice by MiMicSDK. BasicDevice is most simplest UPnP device.
How To Use
- Write firmware to your mbed.
- Reset mbed and update firmware.
- Check your "network computer" folder by Exproler. (in case of windows.)
- You can find UPnPBasicDevice hosted by mbed. (If you can not find device then reflesh exploler information.)
- If you double-click UPnPBasicDevice, the presentation page on device is opened.
Function
- AutoIP
- SSDP
- DeviceDescription hosting (Httpd)
- SOAP (not implemented)
- GENA (not implemented)
Source Code
It is simple and short!
/** * @file * Simplest UPnP basic device.<br/> * This program is upnp:BasicDeveice:1 template. * * <p> * After starting program, check "network" by Exproler. * MiMic basic device will be appeared. * </p> */ #include "mbed.h" #include "rtos.h" #include "SDFileSystem.h" #include "mimic.h" #include "utils/PlatformInfo.h" #include "fsdata.h" Net* net; /** * Httpd for UPnPService and presentation. */ class UPnPBasicDeviceHttpd:public MiMic::Httpd { private: ModUPnPDevice modupnp; ModRomFiles modromfs; //ROM file module public: UPnPBasicDeviceHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort()) { //prepare fs data (presentation.html,icon,image.) this->modromfs.setParam("rom",FSDATA,3); //bind upnp service to module. this->modupnp.setParam(*net); } virtual void onRequest(HttpdConnection& i_connection) { //try to ModRomFS module. for icon,images. if(this->modromfs.execute(i_connection)){ return; } //try to UPnP service. for descriptions. if(this->modupnp.execute(i_connection)){ return; } //Otherwise, Send the redirect response to /rom/index.html i_connection.sendHeader(302, "text/html", "Status: 302:Moved Temporarily\r\n" "Location: /rom/index.html\r\n"); } }; NetConfig cfg; //create network configulation int main() { net=new Net();//Net constructor must be created after started RTOS //Prepare configulation. cfg.setUPnPIcon(64,64,8,"image/png","/rom/icon.png");//set upnp icon address cfg.setUPnPUdn(0xe29f7103,0x4ba2,0x01e0,0); //set application timebase-uuid time and sequence field. cfg.setFriendlyName("UPnPBasicDevice(LPC176x)"); //set friendly name cfg.setUPnPPresentationURL("/rom/index.html"); //set presentationURL cfg.setZeroconf(true);//AutoIP enable UPnPBasicDeviceHttpd httpd(cfg); //create a httpd instance. net->start(cfg); httpd.loop(); //start httpd loop. return 0; }
I think that this code helps to make Web connection to your application easily.
main.cpp@12:218b57d9a6d4, 2013-06-20 (annotated)
- Committer:
- nyatla
- Date:
- Thu Jun 20 02:11:11 2013 +0000
- Revision:
- 12:218b57d9a6d4
- Parent:
- 10:80c05810f911
- Child:
- 19:66d729b94d16
update libMiMic; support DHCP,autoIP,mDNS
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nyatla | 4:0a280ed0a848 | 1 | /** |
nyatla | 4:0a280ed0a848 | 2 | * @file * This is ModLocalFileSystem sample program. |
nyatla | 4:0a280ed0a848 | 3 | * |
nyatla | 4:0a280ed0a848 | 4 | * <pre> |
nyatla | 4:0a280ed0a848 | 5 | * 1. Compile this program and write to your mbed. |
nyatla | 4:0a280ed0a848 | 6 | * 2. Write files for testing to mbed disk. |
nyatla | 4:0a280ed0a848 | 7 | * 2. Access via web browser to http://192.168.0.39/local/<filename> |
nyatla | 4:0a280ed0a848 | 8 | * 3. You can see <filename> file on browser. |
nyatla | 4:0a280ed0a848 | 9 | * </pre> |
nyatla | 4:0a280ed0a848 | 10 | */ |
nyatla | 5:6a2a1644ea2c | 11 | #include "mbed.h" |
nyatla | 5:6a2a1644ea2c | 12 | #include "SDFileSystem.h" |
nyatla | 0:ec1e45489427 | 13 | #include "mimic.h" |
nyatla | 10:80c05810f911 | 14 | #include "utils/PlatformInfo.h" |
nyatla | 10:80c05810f911 | 15 | DigitalOut mbedled(LED1); |
nyatla | 10:80c05810f911 | 16 | DigitalOut lpcxled(P0_22); |
nyatla | 0:ec1e45489427 | 17 | |
nyatla | 3:77431c2bd9cb | 18 | /** |
nyatla | 3:77431c2bd9cb | 19 | * local filesystem support. |
nyatla | 3:77431c2bd9cb | 20 | */ |
nyatla | 10:80c05810f911 | 21 | LocalFileSystem2 lf("local"); |
nyatla | 5:6a2a1644ea2c | 22 | SDFileSystem sd(p5, p6, p7, p8,"sd"); |
nyatla | 6:20dcb08e1b43 | 23 | unsigned int p; |
nyatla | 0:ec1e45489427 | 24 | /** |
nyatla | 0:ec1e45489427 | 25 | * MiMic RemoteMCU httpd.<br/> |
nyatla | 5:6a2a1644ea2c | 26 | * Number of simultaneous connections:4 |
nyatla | 0:ec1e45489427 | 27 | * <p>Service list</p> |
nyatla | 0:ec1e45489427 | 28 | * <pre> |
nyatla | 0:ec1e45489427 | 29 | * /local/ - mbed LocalFileSystem |
nyatla | 0:ec1e45489427 | 30 | * </pre> |
nyatla | 0:ec1e45489427 | 31 | */ |
nyatla | 5:6a2a1644ea2c | 32 | class FsHttpd:public MiMic::Httpd |
nyatla | 0:ec1e45489427 | 33 | { |
nyatla | 0:ec1e45489427 | 34 | private: |
nyatla | 5:6a2a1644ea2c | 35 | ModLocalFileSystem modlocal; |
nyatla | 5:6a2a1644ea2c | 36 | ModLocalFileSystem modsd; |
nyatla | 0:ec1e45489427 | 37 | public: |
nyatla | 12:218b57d9a6d4 | 38 | FsHttpd(NetConfig& i_cfg):Httpd(i_cfg._inst.services.http_port) |
nyatla | 0:ec1e45489427 | 39 | { |
nyatla | 3:77431c2bd9cb | 40 | //bind local file system path to /local/* |
nyatla | 5:6a2a1644ea2c | 41 | modlocal.setParam("local"); |
nyatla | 5:6a2a1644ea2c | 42 | modsd.setParam("sd"); |
nyatla | 0:ec1e45489427 | 43 | } |
nyatla | 0:ec1e45489427 | 44 | virtual void onRequest(HttpdConnection& i_connection) |
nyatla | 0:ec1e45489427 | 45 | { |
nyatla | 6:20dcb08e1b43 | 46 | p++; |
nyatla | 10:80c05810f911 | 47 | switch(PlatformInfo::getPlatformType()){ |
nyatla | 10:80c05810f911 | 48 | case PlatformInfo::PF_MBED: |
nyatla | 10:80c05810f911 | 49 | mbedled = p%2; |
nyatla | 10:80c05810f911 | 50 | break; |
nyatla | 10:80c05810f911 | 51 | case PlatformInfo::PF_LPCXPRESSO: |
nyatla | 10:80c05810f911 | 52 | lpcxled = p%2; |
nyatla | 10:80c05810f911 | 53 | break; |
nyatla | 10:80c05810f911 | 54 | } |
nyatla | 5:6a2a1644ea2c | 55 | //try to ModLocalFileSystem |
nyatla | 5:6a2a1644ea2c | 56 | if(this->modlocal.execute(i_connection)){ |
nyatla | 5:6a2a1644ea2c | 57 | return; |
nyatla | 5:6a2a1644ea2c | 58 | } |
nyatla | 5:6a2a1644ea2c | 59 | //try to ModLocalFileSystem(SD) |
nyatla | 5:6a2a1644ea2c | 60 | if(this->modsd.execute(i_connection)){ |
nyatla | 0:ec1e45489427 | 61 | return; |
nyatla | 0:ec1e45489427 | 62 | } |
nyatla | 5:6a2a1644ea2c | 63 | //Otherwise, Send simple top index page. |
nyatla | 5:6a2a1644ea2c | 64 | i_connection.sendHeader(200,"text/html",NULL); |
nyatla | 5:6a2a1644ea2c | 65 | if(i_connection.isMethodType(Http::MT_GET)){ |
nyatla | 5:6a2a1644ea2c | 66 | i_connection.sendBodyF( |
nyatla | 5:6a2a1644ea2c | 67 | "<!DOCTYPE html>" |
nyatla | 5:6a2a1644ea2c | 68 | "<html lang=\"ja\">" |
nyatla | 5:6a2a1644ea2c | 69 | "<head></head>" |
nyatla | 5:6a2a1644ea2c | 70 | "<body>" |
nyatla | 5:6a2a1644ea2c | 71 | "<h1>This is MiMic Server!</h1>" |
nyatla | 5:6a2a1644ea2c | 72 | "<hr/>" |
nyatla | 5:6a2a1644ea2c | 73 | "<ul>" |
nyatla | 5:6a2a1644ea2c | 74 | "<li><a href=\"/local/\">mbed Local Filesystem</a></li>" |
nyatla | 5:6a2a1644ea2c | 75 | "<li><a href=\"/sd/\">SDCard</a></li>" |
nyatla | 5:6a2a1644ea2c | 76 | "</ul></body>"); |
nyatla | 5:6a2a1644ea2c | 77 | } |
nyatla | 0:ec1e45489427 | 78 | } |
nyatla | 0:ec1e45489427 | 79 | }; |
nyatla | 0:ec1e45489427 | 80 | |
nyatla | 12:218b57d9a6d4 | 81 | NetConfig cfg; //create network configulation |
nyatla | 12:218b57d9a6d4 | 82 | |
nyatla | 0:ec1e45489427 | 83 | int main() |
nyatla | 0:ec1e45489427 | 84 | { |
nyatla | 12:218b57d9a6d4 | 85 | Net net; //create a net instance. |
nyatla | 12:218b57d9a6d4 | 86 | |
nyatla | 5:6a2a1644ea2c | 87 | //try to override setting by local file. |
nyatla | 5:6a2a1644ea2c | 88 | if(!cfg.loadFromFile("/local/mimic.cfg")){ |
nyatla | 10:80c05810f911 | 89 | wait_ms(1000); |
nyatla | 5:6a2a1644ea2c | 90 | cfg.loadFromFile("/sd/mimic.cfg"); |
nyatla | 12:218b57d9a6d4 | 91 | } |
nyatla | 12:218b57d9a6d4 | 92 | FsHttpd httpd(cfg); //create a httpd instance. |
nyatla | 12:218b57d9a6d4 | 93 | net.start(cfg); |
nyatla | 0:ec1e45489427 | 94 | httpd.loop(); //start httpd loop. |
nyatla | 0:ec1e45489427 | 95 | return 0; |
nyatla | 0:ec1e45489427 | 96 | } |