Switch to unified view

a b/libupnpp/control/ohreceiver.cxx
1
/* Copyright (C) 2014 J.F.Dockes
2
 *       This program is free software; you can redistribute it and/or modify
3
 *       it under the terms of the GNU General Public License as published by
4
 *       the Free Software Foundation; either version 2 of the License, or
5
 *       (at your option) any later version.
6
 *
7
 *       This program is distributed in the hope that it will be useful,
8
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *       GNU General Public License for more details.
11
 *
12
 *       You should have received a copy of the GNU General Public License
13
 *       along with this program; if not, write to the
14
 *       Free Software Foundation, Inc.,
15
 *       59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
#include "libupnpp/control/ohreceiver.hxx"
18
19
#include <stdlib.h>                     // for atoi
20
#include <string.h>                     // for strcmp
21
#include <upnp/upnp.h>                  // for UPNP_E_BAD_RESPONSE, etc
22
23
#include <functional>                   // for _Bind, bind, _1
24
#include <ostream>                      // for endl, basic_ostream, etc
25
#include <string>                       // for string, basic_string, etc
26
#include <utility>                      // for pair
27
#include <vector>                       // for vector
28
29
#include "libupnpp/upnpavutils.hxx"
30
#include "libupnpp/control/service.hxx"  // for VarEventReporter, Service
31
#include "libupnpp/log.hxx"             // for LOGERR, LOGDEB1, LOGINF
32
#include "libupnpp/soaphelp.hxx"        // for SoapIncoming, etc
33
#include "libupnpp/upnpp_p.hxx"         // for stringToBool
34
#include "libupnpp/control/ohreceiver.hxx"
35
36
using namespace std;
37
using namespace STD_PLACEHOLDERS;
38
using namespace UPnPP;
39
40
namespace UPnPClient {
41
42
const string OHReceiver::SType("urn:av-openhome-org:service:Receiver:1");
43
44
// Check serviceType string (while walking the descriptions. We don't
45
// include a version in comparisons, as we are satisfied with version1
46
bool OHReceiver::isOHRcService(const string& st)
47
{
48
    const string::size_type sz(SType.size()-2);
49
    return !SType.compare(0, sz, st, 0, sz);
50
}
51
52
void OHReceiver::evtCallback(
53
    const STD_UNORDERED_MAP<std::string, std::string>& props)
54
{
55
    LOGDEB1("OHReceiver::evtCallback:getReporter(): " << getReporter() << endl);
56
    for (STD_UNORDERED_MAP<std::string, std::string>::const_iterator it = 
57
             props.begin(); it != props.end(); it++) {
58
        if (!getReporter()) {
59
            LOGDEB1("OHReceiver::evtCallback: " << it->first << " -> " 
60
                    << it->second << endl);
61
            continue;
62
        }
63
64
        if (!it->first.compare("TransportState")) {
65
            OHPlaylist::TPState tp;
66
            OHPlaylist::stringToTpState(it->second, &tp);
67
            getReporter()->changed(it->first.c_str(), int(tp));
68
        } else if (!it->first.compare("Metadata")) {
69
            getReporter()->changed(it->first.c_str(), 
70
                                   it->second.c_str());
71
        } else if (!it->first.compare("Uri")) {
72
            getReporter()->changed(it->first.c_str(), 
73
                                   it->second.c_str());
74
        } else if (!it->first.compare("ProtocolInfo")) {
75
            getReporter()->changed(it->first.c_str(), 
76
                                   it->second.c_str());
77
        } else {
78
            LOGERR("OHReceiver event: unknown variable: name [" <<
79
                   it->first << "] value [" << it->second << endl);
80
            getReporter()->changed(it->first.c_str(), it->second.c_str());
81
        }
82
    }
83
}
84
85
void OHReceiver::registerCallback()
86
{
87
    Service::registerCallback(bind(&OHReceiver::evtCallback, this, _1));
88
}
89
90
int OHReceiver::play()
91
{
92
    return runTrivialAction("Play");
93
}
94
int OHReceiver::stop()
95
{
96
    return runTrivialAction("Stop");
97
}
98
99
int OHReceiver::setSender(const string& uri, const string& didl)
100
{
101
    SoapOutgoing args(getServiceType(), "SetSender");
102
    args("Uri", uri)
103
        ("Metadata", didl);
104
    SoapIncoming data;
105
    return runAction(args, data);
106
}
107
108
int OHReceiver::sender(string& uri, string& meta)
109
{
110
    SoapOutgoing args(getServiceType(), "Sender");
111
    SoapIncoming data;
112
    int ret = runAction(args, data);
113
    if (ret != UPNP_E_SUCCESS) {
114
        return ret;
115
    }
116
    if (!data.get("Uri", &uri)) {
117
        LOGERR("OHReceiver::Sender: missing Uri in response" << endl);
118
        return UPNP_E_BAD_RESPONSE;
119
    }
120
    if (!data.get("Metadata", &meta)) {
121
        LOGERR("OHReceiver::Sender: missing Metadata in response" << endl);
122
        return UPNP_E_BAD_RESPONSE;
123
    }
124
    return 0;
125
}
126
127
int OHReceiver::transportState(OHPlaylist::TPState *tpp)
128
{
129
    string value;
130
    int ret;
131
132
    if ((ret = runSimpleGet("TransportState", "Value", &value)))
133
        return ret;
134
135
    return OHPlaylist::stringToTpState(value, tpp);
136
}
137
138
int OHReceiver::protocolInfo(std::string *proto)
139
{
140
    SoapOutgoing args(getServiceType(), "ProtocolInfo");
141
    SoapIncoming data;
142
    int ret = runAction(args, data);
143
    if (ret != UPNP_E_SUCCESS) {
144
        return ret;
145
    }
146
    if (!data.get("Value", proto)) {
147
        LOGERR("OHReceiver::protocolInfo: missing Value in response" << endl);
148
        return UPNP_E_BAD_RESPONSE;
149
    }
150
    return 0;
151
}
152
153
} // End namespace UPnPClient