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@21:7dbe2100b419, 2013-08-09 (annotated)
- Committer:
- nyatla
- Date:
- Fri Aug 09 14:23:08 2013 +0000
- Revision:
- 21:7dbe2100b419
- Parent:
- 20:4b0b449ddb12
- Child:
- 26:302df93914df
add issue comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nyatla | 4:0a280ed0a848 | 1 | /** |
nyatla | 20:4b0b449ddb12 | 2 | * @file |
nyatla | 20:4b0b449ddb12 | 3 | * Simplest UPnP basic device.<br/> |
nyatla | 20:4b0b449ddb12 | 4 | * This program is upnp:BasicDeveice:1 template. |
nyatla | 20:4b0b449ddb12 | 5 | * |
nyatla | 20:4b0b449ddb12 | 6 | * <p> |
nyatla | 20:4b0b449ddb12 | 7 | * After starting program, check "network" by Exproler. |
nyatla | 20:4b0b449ddb12 | 8 | * MiMic basic device will be appeared. |
nyatla | 20:4b0b449ddb12 | 9 | * </p> |
nyatla | 4:0a280ed0a848 | 10 | */ |
nyatla | 5:6a2a1644ea2c | 11 | #include "mbed.h" |
nyatla | 19:66d729b94d16 | 12 | #include "rtos.h" |
nyatla | 5:6a2a1644ea2c | 13 | #include "SDFileSystem.h" |
nyatla | 0:ec1e45489427 | 14 | #include "mimic.h" |
nyatla | 10:80c05810f911 | 15 | #include "utils/PlatformInfo.h" |
nyatla | 19:66d729b94d16 | 16 | #include "fsdata.h" |
nyatla | 19:66d729b94d16 | 17 | |
nyatla | 0:ec1e45489427 | 18 | |
nyatla | 19:66d729b94d16 | 19 | Net* net; |
nyatla | 19:66d729b94d16 | 20 | |
nyatla | 0:ec1e45489427 | 21 | /** |
nyatla | 20:4b0b449ddb12 | 22 | * Httpd for UPnPService and presentation. |
nyatla | 0:ec1e45489427 | 23 | */ |
nyatla | 20:4b0b449ddb12 | 24 | class UPnPBasicDeviceHttpd:public MiMic::Httpd |
nyatla | 0:ec1e45489427 | 25 | { |
nyatla | 0:ec1e45489427 | 26 | private: |
nyatla | 19:66d729b94d16 | 27 | ModUPnPDevice modupnp; |
nyatla | 20:4b0b449ddb12 | 28 | ModRomFiles modromfs; //ROM file module |
nyatla | 0:ec1e45489427 | 29 | public: |
nyatla | 20:4b0b449ddb12 | 30 | UPnPBasicDeviceHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort()) |
nyatla | 0:ec1e45489427 | 31 | { |
nyatla | 20:4b0b449ddb12 | 32 | //prepare fs data (presentation.html,icon,image.) |
nyatla | 20:4b0b449ddb12 | 33 | this->modromfs.setParam("rom",FSDATA,3); |
nyatla | 20:4b0b449ddb12 | 34 | //bind upnp service to module. |
nyatla | 19:66d729b94d16 | 35 | this->modupnp.setParam(*net); |
nyatla | 0:ec1e45489427 | 36 | } |
nyatla | 0:ec1e45489427 | 37 | virtual void onRequest(HttpdConnection& i_connection) |
nyatla | 0:ec1e45489427 | 38 | { |
nyatla | 20:4b0b449ddb12 | 39 | //try to ModRomFS module. for icon,images. |
nyatla | 19:66d729b94d16 | 40 | if(this->modromfs.execute(i_connection)){ |
nyatla | 19:66d729b94d16 | 41 | return; |
nyatla | 19:66d729b94d16 | 42 | } |
nyatla | 20:4b0b449ddb12 | 43 | //try to UPnP service. for descriptions. |
nyatla | 19:66d729b94d16 | 44 | if(this->modupnp.execute(i_connection)){ |
nyatla | 19:66d729b94d16 | 45 | return; |
nyatla | 19:66d729b94d16 | 46 | } |
nyatla | 20:4b0b449ddb12 | 47 | //Otherwise, Send the redirect response to /rom/index.html |
nyatla | 20:4b0b449ddb12 | 48 | i_connection.sendHeader(302, |
nyatla | 20:4b0b449ddb12 | 49 | "text/html", |
nyatla | 20:4b0b449ddb12 | 50 | "Status: 302:Moved Temporarily\r\n" |
nyatla | 20:4b0b449ddb12 | 51 | "Location: /rom/index.html\r\n"); |
nyatla | 0:ec1e45489427 | 52 | } |
nyatla | 0:ec1e45489427 | 53 | }; |
nyatla | 0:ec1e45489427 | 54 | |
nyatla | 12:218b57d9a6d4 | 55 | NetConfig cfg; //create network configulation |
nyatla | 12:218b57d9a6d4 | 56 | |
nyatla | 0:ec1e45489427 | 57 | int main() |
nyatla | 0:ec1e45489427 | 58 | { |
nyatla | 19:66d729b94d16 | 59 | net=new Net();//Net constructor must be created after started RTOS |
nyatla | 19:66d729b94d16 | 60 | //Prepare configulation. |
nyatla | 20:4b0b449ddb12 | 61 | cfg.setUPnPIcon(64,64,8,"image/png","/rom/icon.png");//set upnp icon address |
nyatla | 20:4b0b449ddb12 | 62 | cfg.setUPnPUdn(0xe29f7103,0x4ba2,0x01e0,0); //set application timebase-uuid time and sequence field. |
nyatla | 20:4b0b449ddb12 | 63 | cfg.setFriendlyName("UPnPBasicDevice(LPC176x)"); //set friendly name |
nyatla | 20:4b0b449ddb12 | 64 | cfg.setUPnPPresentationURL("/rom/index.html"); //set presentationURL |
nyatla | 20:4b0b449ddb12 | 65 | cfg.setZeroconf(true);//AutoIP enable |
nyatla | 21:7dbe2100b419 | 66 | |
nyatla | 21:7dbe2100b419 | 67 | /* |
nyatla | 21:7dbe2100b419 | 68 | DHCP client has a bug which can not obtain IP address from a DHCP server. |
nyatla | 21:7dbe2100b419 | 69 | In that case, please turn off setZeroconf and give an IP address manually as temporary solution. |
nyatla | 21:7dbe2100b419 | 70 | cfg.setIpAddr(1,2,3,4); |
nyatla | 21:7dbe2100b419 | 71 | cfg.setNetMask(1,2,3,4); |
nyatla | 21:7dbe2100b419 | 72 | cfg.setGateway(1,2,3,4); |
nyatla | 21:7dbe2100b419 | 73 | */ |
nyatla | 21:7dbe2100b419 | 74 | |
nyatla | 12:218b57d9a6d4 | 75 | |
nyatla | 20:4b0b449ddb12 | 76 | UPnPBasicDeviceHttpd httpd(cfg); //create a httpd instance. |
nyatla | 19:66d729b94d16 | 77 | net->start(cfg); |
nyatla | 0:ec1e45489427 | 78 | httpd.loop(); //start httpd loop. |
nyatla | 0:ec1e45489427 | 79 | return 0; |
nyatla | 0:ec1e45489427 | 80 | } |