Download this file

typedservice.cpp    81 lines (69 with data), 2.0 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <string>
#include <iostream>
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/control/typedservice.hxx"
using namespace std;
using namespace UPnPClient;
using namespace UPnPP;
class MReporter : public UPnPClient::VarEventReporter {
public:
void changed(const char *nm, int value) {
cerr << "Reporter: changed(char *, int) invoked for nm " << nm <<
" ??\n";
}
void changed(const char *nm, const char *value) {
cout << "Changed: " << nm << " : " << value << endl;
}
};
int main(int argc, char **argv)
{
argv++;argc--;
if (argc < 3) {
cerr << "Usage: tpservice NameOrUid partialservicetype action "
"[arg [...]]\n";
return 1;
}
string devname(*argv++);
argc--;
string servtp(*argv++);
argc--;
string actnm(*argv++);
argc--;
vector<string> args;
while (argc--) {
args.push_back(*argv++);
}
// Initialize libupnpp logging
Logger::getTheLog("stderr")->setLogLevel(Logger::LLDEB1);
// Explicitely initialize libupnpp so that we can display a
// possible error
LibUPnP *mylib = LibUPnP::getLibUPnP();
if (!mylib) {
cerr << "Can't get LibUPnP" << endl;
return 1;
}
if (!mylib->ok()) {
cerr << "Lib init failed: " <<
mylib->errAsString("main", mylib->getInitError()) << endl;
return 1;
}
TypedService *srv = findTypedService(devname, servtp, true);
if (!srv) {
cerr << "Service " << devname << "/" << servtp << " not found" << endl;
return 1;
}
map<string, string> data;
int ret = srv->runAction(actnm, args, data);
if (ret == 0) {
for (auto& entry: data) {
cout << entry.first << "->" << entry.second << endl;
}
} else {
cerr << "runAction failed with code " << ret << endl;
return 1;
}
MReporter reporter;
srv->installReporter(&reporter);
sleep(1000);
return 0;
}