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:
Tue Apr 09 10:48:14 2013 +0000
Revision:
4:0a280ed0a848
Parent:
3:77431c2bd9cb
Child:
5:6a2a1644ea2c
update comment

Who changed what in which revision?

UserRevisionLine numberNew 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 0:ec1e45489427 11 #include "mimic.h"
nyatla 0:ec1e45489427 12
nyatla 0:ec1e45489427 13
nyatla 3:77431c2bd9cb 14 /**
nyatla 3:77431c2bd9cb 15 * local filesystem support.
nyatla 3:77431c2bd9cb 16 */
nyatla 3:77431c2bd9cb 17 LocalFileSystem lf("local");
nyatla 0:ec1e45489427 18
nyatla 0:ec1e45489427 19 /**
nyatla 0:ec1e45489427 20 * MiMic RemoteMCU httpd.<br/>
nyatla 0:ec1e45489427 21 * <p>Service list</p>
nyatla 0:ec1e45489427 22 * <pre>
nyatla 0:ec1e45489427 23 * /local/ - mbed LocalFileSystem
nyatla 0:ec1e45489427 24 * </pre>
nyatla 0:ec1e45489427 25 */
nyatla 3:77431c2bd9cb 26 class LfsHttpd:public MiMic::Httpd
nyatla 0:ec1e45489427 27 {
nyatla 0:ec1e45489427 28 private:
nyatla 3:77431c2bd9cb 29 ModLocalFileSystem modurl; //basic URL parser
nyatla 0:ec1e45489427 30 public:
nyatla 3:77431c2bd9cb 31 LfsHttpd():Httpd(80)
nyatla 0:ec1e45489427 32 {
nyatla 3:77431c2bd9cb 33 //bind local file system path to /local/*
nyatla 3:77431c2bd9cb 34 modurl.setParam("local");
nyatla 0:ec1e45489427 35 }
nyatla 0:ec1e45489427 36 virtual void onRequest(HttpdConnection& i_connection)
nyatla 0:ec1e45489427 37 {
nyatla 2:28fd59d6be76 38 //call ModUrl module.
nyatla 3:77431c2bd9cb 39 if(!this->modurl.execute(i_connection)){
nyatla 3:77431c2bd9cb 40 //send 430
nyatla 3:77431c2bd9cb 41 i_connection.sendHeader(403,"text/html",NULL);
nyatla 3:77431c2bd9cb 42 i_connection.sendBodyF("<html><body>403 Forbidden</body></html>");
nyatla 0:ec1e45489427 43 return;
nyatla 0:ec1e45489427 44 }
nyatla 0:ec1e45489427 45 return;
nyatla 0:ec1e45489427 46 }
nyatla 0:ec1e45489427 47 };
nyatla 0:ec1e45489427 48
nyatla 0:ec1e45489427 49 int main()
nyatla 0:ec1e45489427 50 {
nyatla 0:ec1e45489427 51 NetConfig cfg; //create network configulation
nyatla 0:ec1e45489427 52 Net net(cfg); //create a net instance.
nyatla 3:77431c2bd9cb 53 LfsHttpd httpd; //create a httpd instance.
nyatla 0:ec1e45489427 54 httpd.loop(); //start httpd loop.
nyatla 0:ec1e45489427 55 return 0;
nyatla 0:ec1e45489427 56 }