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:
Fri Aug 09 09:06:09 2013 +0000
Revision:
19:66d729b94d16
Parent:
12:218b57d9a6d4
Child:
20:4b0b449ddb12
update libMiMic; support UPnP function.

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 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 10:80c05810f911 18 DigitalOut mbedled(LED1);
nyatla 10:80c05810f911 19 DigitalOut lpcxled(P0_22);
nyatla 0:ec1e45489427 20
nyatla 3:77431c2bd9cb 21 /**
nyatla 3:77431c2bd9cb 22 * local filesystem support.
nyatla 3:77431c2bd9cb 23 */
nyatla 10:80c05810f911 24 LocalFileSystem2 lf("local");
nyatla 5:6a2a1644ea2c 25 SDFileSystem sd(p5, p6, p7, p8,"sd");
nyatla 19:66d729b94d16 26 Net* net;
nyatla 19:66d729b94d16 27
nyatla 6:20dcb08e1b43 28 unsigned int p;
nyatla 0:ec1e45489427 29 /**
nyatla 0:ec1e45489427 30 * MiMic RemoteMCU httpd.<br/>
nyatla 5:6a2a1644ea2c 31 * Number of simultaneous connections:4
nyatla 0:ec1e45489427 32 * <p>Service list</p>
nyatla 0:ec1e45489427 33 * <pre>
nyatla 0:ec1e45489427 34 * /local/ - mbed LocalFileSystem
nyatla 0:ec1e45489427 35 * </pre>
nyatla 0:ec1e45489427 36 */
nyatla 5:6a2a1644ea2c 37 class FsHttpd:public MiMic::Httpd
nyatla 0:ec1e45489427 38 {
nyatla 0:ec1e45489427 39 private:
nyatla 5:6a2a1644ea2c 40 ModLocalFileSystem modlocal;
nyatla 5:6a2a1644ea2c 41 ModLocalFileSystem modsd;
nyatla 19:66d729b94d16 42 ModUPnPDevice modupnp;
nyatla 19:66d729b94d16 43 ModRomFiles modromfs; //ROM file module
nyatla 19:66d729b94d16 44
nyatla 0:ec1e45489427 45 public:
nyatla 19:66d729b94d16 46 FsHttpd(NetConfig& i_cfg):Httpd(i_cfg.getHttpPort())
nyatla 0:ec1e45489427 47 {
nyatla 19:66d729b94d16 48
nyatla 19:66d729b94d16 49 this->modromfs.setParam("rom",FSDATA,1);
nyatla 3:77431c2bd9cb 50 //bind local file system path to /local/*
nyatla 5:6a2a1644ea2c 51 modlocal.setParam("local");
nyatla 19:66d729b94d16 52 modsd.setParam("sd",ModLocalFileSystem::FST_SDFATFS);
nyatla 19:66d729b94d16 53 this->modupnp.setParam(*net);
nyatla 0:ec1e45489427 54 }
nyatla 0:ec1e45489427 55 virtual void onRequest(HttpdConnection& i_connection)
nyatla 0:ec1e45489427 56 {
nyatla 6:20dcb08e1b43 57 p++;
nyatla 10:80c05810f911 58 switch(PlatformInfo::getPlatformType()){
nyatla 10:80c05810f911 59 case PlatformInfo::PF_MBED:
nyatla 10:80c05810f911 60 mbedled = p%2;
nyatla 10:80c05810f911 61 break;
nyatla 10:80c05810f911 62 case PlatformInfo::PF_LPCXPRESSO:
nyatla 10:80c05810f911 63 lpcxled = p%2;
nyatla 10:80c05810f911 64 break;
nyatla 10:80c05810f911 65 }
nyatla 19:66d729b94d16 66 //try to ModRomFS module.
nyatla 19:66d729b94d16 67 if(this->modromfs.execute(i_connection)){
nyatla 19:66d729b94d16 68 return;
nyatla 19:66d729b94d16 69 }
nyatla 5:6a2a1644ea2c 70 //try to ModLocalFileSystem
nyatla 5:6a2a1644ea2c 71 if(this->modlocal.execute(i_connection)){
nyatla 5:6a2a1644ea2c 72 return;
nyatla 5:6a2a1644ea2c 73 }
nyatla 5:6a2a1644ea2c 74 //try to ModLocalFileSystem(SD)
nyatla 5:6a2a1644ea2c 75 if(this->modsd.execute(i_connection)){
nyatla 0:ec1e45489427 76 return;
nyatla 0:ec1e45489427 77 }
nyatla 19:66d729b94d16 78 if(this->modupnp.execute(i_connection)){
nyatla 19:66d729b94d16 79 return;
nyatla 19:66d729b94d16 80 }
nyatla 5:6a2a1644ea2c 81 //Otherwise, Send simple top index page.
nyatla 5:6a2a1644ea2c 82 i_connection.sendHeader(200,"text/html",NULL);
nyatla 5:6a2a1644ea2c 83 if(i_connection.isMethodType(Http::MT_GET)){
nyatla 5:6a2a1644ea2c 84 i_connection.sendBodyF(
nyatla 5:6a2a1644ea2c 85 "<!DOCTYPE html>"
nyatla 5:6a2a1644ea2c 86 "<html lang=\"ja\">"
nyatla 5:6a2a1644ea2c 87 "<head></head>"
nyatla 5:6a2a1644ea2c 88 "<body>"
nyatla 5:6a2a1644ea2c 89 "<h1>This is MiMic Server!</h1>"
nyatla 5:6a2a1644ea2c 90 "<hr/>"
nyatla 5:6a2a1644ea2c 91 "<ul>"
nyatla 5:6a2a1644ea2c 92 "<li><a href=\"/local/\">mbed Local Filesystem</a></li>"
nyatla 5:6a2a1644ea2c 93 "<li><a href=\"/sd/\">SDCard</a></li>"
nyatla 5:6a2a1644ea2c 94 "</ul></body>");
nyatla 5:6a2a1644ea2c 95 }
nyatla 0:ec1e45489427 96 }
nyatla 0:ec1e45489427 97 };
nyatla 0:ec1e45489427 98
nyatla 12:218b57d9a6d4 99 NetConfig cfg; //create network configulation
nyatla 12:218b57d9a6d4 100
nyatla 0:ec1e45489427 101 int main()
nyatla 0:ec1e45489427 102 {
nyatla 19:66d729b94d16 103 net=new Net();//Net constructor must be created after started RTOS
nyatla 19:66d729b94d16 104 //Prepare configulation.
nyatla 19:66d729b94d16 105 cfg.setUPnPIcon(64,64,8,"image/png","/rom/icon.png");
nyatla 19:66d729b94d16 106 cfg.setUPnPUdn(0xe29f7102,0x4ba2,0x01e0,0);
nyatla 19:66d729b94d16 107 cfg.setFriendlyName("MbedFileServer");
nyatla 12:218b57d9a6d4 108
nyatla 5:6a2a1644ea2c 109 //try to override setting by local file.
nyatla 5:6a2a1644ea2c 110 if(!cfg.loadFromFile("/local/mimic.cfg")){
nyatla 19:66d729b94d16 111 Thread::wait(2000);//wait for SD card initialization.
nyatla 5:6a2a1644ea2c 112 cfg.loadFromFile("/sd/mimic.cfg");
nyatla 12:218b57d9a6d4 113 }
nyatla 12:218b57d9a6d4 114 FsHttpd httpd(cfg); //create a httpd instance.
nyatla 19:66d729b94d16 115 net->start(cfg);
nyatla 0:ec1e45489427 116 httpd.loop(); //start httpd loop.
nyatla 0:ec1e45489427 117 return 0;
nyatla 0:ec1e45489427 118 }