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 Ryo Iizuka

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.) /media/uploads/nyatla/upnp-exploler.png
  • If you double-click UPnPBasicDevice, the presentation page on device is opened. /media/uploads/nyatla/upnp-presentation.png

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.

Committer:
nyatla
Date:
Mon Oct 27 15:03:05 2014 +0000
Revision:
26:302df93914df
Parent:
21:7dbe2100b419
Child:
28:3b83785b9b03
update library; fix stack overflow

Who changed what in which revision?

UserRevisionLine numberNew 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
nyatla 0:ec1e45489427 20 /**
nyatla 20:4b0b449ddb12 21 * Httpd for UPnPService and presentation.
nyatla 0:ec1e45489427 22 */
nyatla 20:4b0b449ddb12 23 class UPnPBasicDeviceHttpd:public MiMic::Httpd
nyatla 0:ec1e45489427 24 {
nyatla 0:ec1e45489427 25 private:
nyatla 19:66d729b94d16 26 ModUPnPDevice modupnp;
nyatla 20:4b0b449ddb12 27 ModRomFiles modromfs; //ROM file module
nyatla 0:ec1e45489427 28 public:
nyatla 26:302df93914df 29 UPnPBasicDeviceHttpd(Net& net,NetConfig& i_cfg):Httpd(i_cfg.getHttpPort())
nyatla 0:ec1e45489427 30 {
nyatla 20:4b0b449ddb12 31 //prepare fs data (presentation.html,icon,image.)
nyatla 20:4b0b449ddb12 32 this->modromfs.setParam("rom",FSDATA,3);
nyatla 20:4b0b449ddb12 33 //bind upnp service to module.
nyatla 26:302df93914df 34 this->modupnp.setParam(net);
nyatla 0:ec1e45489427 35 }
nyatla 0:ec1e45489427 36 virtual void onRequest(HttpdConnection& i_connection)
nyatla 0:ec1e45489427 37 {
nyatla 20:4b0b449ddb12 38 //try to ModRomFS module. for icon,images.
nyatla 19:66d729b94d16 39 if(this->modromfs.execute(i_connection)){
nyatla 19:66d729b94d16 40 return;
nyatla 19:66d729b94d16 41 }
nyatla 20:4b0b449ddb12 42 //try to UPnP service. for descriptions.
nyatla 19:66d729b94d16 43 if(this->modupnp.execute(i_connection)){
nyatla 19:66d729b94d16 44 return;
nyatla 19:66d729b94d16 45 }
nyatla 20:4b0b449ddb12 46 //Otherwise, Send the redirect response to /rom/index.html
nyatla 20:4b0b449ddb12 47 i_connection.sendHeader(302,
nyatla 20:4b0b449ddb12 48 "text/html",
nyatla 20:4b0b449ddb12 49 "Status: 302:Moved Temporarily\r\n"
nyatla 20:4b0b449ddb12 50 "Location: /rom/index.html\r\n");
nyatla 0:ec1e45489427 51 }
nyatla 0:ec1e45489427 52 };
nyatla 0:ec1e45489427 53
nyatla 26:302df93914df 54 UPnPBasicDeviceHttpd* httpd;
nyatla 0:ec1e45489427 55 int main()
nyatla 0:ec1e45489427 56 {
nyatla 26:302df93914df 57 NetConfig cfg; //create network configulation
nyatla 26:302df93914df 58 Net net;//Net constructor must be created after started RTOS
nyatla 26:302df93914df 59
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 26:302df93914df 76 httpd=new UPnPBasicDeviceHttpd(net,cfg); //create a httpd instance.
nyatla 26:302df93914df 77 net.start(cfg);
nyatla 26:302df93914df 78 httpd->loop(); //start httpd loop.
nyatla 0:ec1e45489427 79 return 0;
nyatla 0:ec1e45489427 80 }