npupnp_fsdevice.cpp 1.6 KB
Newer Older
Jean-Francois Dockes's avatar
none  
Jean-Francois Dockes committed
1

2
3
4
#include <upnp.h>
#include <upnpdebug.h>
#include <upnptools.h>
Jean-Francois Dockes's avatar
none  
Jean-Francois Dockes committed
5
6
7
8
9
10
11

#include <iostream>
#include <unistd.h>

static int mycb(Upnp_EventType etyp, const void *evt, void *ctxt)
{
    std::cerr << "mycb: et " << etyp << " ctxt " << ctxt << "\n";
Jean-Francois Dockes's avatar
none    
Jean-Francois Dockes committed
12
    std::cerr << "We should do something here...\n";
Jean-Francois Dockes's avatar
none  
Jean-Francois Dockes committed
13
14
15
    return 0;
}

Jean-Francois Dockes's avatar
none    
Jean-Francois Dockes committed
16
17
18
// Using a relative root path, not a good idea ! but simpler for the sample
std::string root("smallsamps");
std::string desc("smallsamps/dev-desc.xml");
Jean-Francois Dockes's avatar
none  
Jean-Francois Dockes committed
19
20
21
22
23

int main(int argc, char **argv)
{

    UpnpSetLogFileNames("", "");
Jean-Francois Dockes's avatar
none    
Jean-Francois Dockes committed
24
    UpnpSetLogLevel(UPNP_INFO);
Jean-Francois Dockes's avatar
none  
Jean-Francois Dockes committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    UpnpInitLog();

    int success = UpnpInit2("", 0);

    if (success != UPNP_E_SUCCESS) {
        std::cerr << "init failed: " << success <<
            UpnpGetErrorMessage(success) << "\n";
        return 1;
    }
    std::cout << "Init ok, address: " << UpnpGetServerIpAddress() << " port " <<
        UpnpGetServerPort() << "\n";

    success = UpnpSetWebServerRootDir(root.c_str());
    if (success != UPNP_E_SUCCESS) {
        std::cerr << "UpnpSetWebServerRootDir failed: " << success <<
            UpnpGetErrorMessage(success) << "\n";
        return 1;
    }
    UpnpDevice_Handle hdl;
    success = UpnpRegisterRootDevice2(
        UPNPREG_FILENAME_DESC, desc.c_str(), 0, 0, mycb, 0, &hdl);
    if (success != UPNP_E_SUCCESS) {
        std::cerr << "UpnpRegisterRootDevice2 failed: " << success <<
            UpnpGetErrorMessage(success) << "\n";
        return 1;
    }

    // Start the discovery advertisements. Only call once, this starts the loop.
    UpnpSendAdvertisement(hdl, 1800);
    
    while (true) {
        sleep(1000);
    }
    return 0;
}