/* Copyright (C) 2013 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
#include <functional>
using namespace std::placeholders;
#include <upnp/upnp.h>
#include <upnp/upnptools.h>
#include "libupnpp/upnpp_p.hxx"
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/ixmlwrap.hxx"
#include "libupnpp/control/cdirectory.hxx"
#include "libupnpp/control/cdircontent.hxx"
#include "libupnpp/discovery.hxx"
#include "libupnpp/soaphelp.hxx"
#include "libupnpp/log.hxx"
namespace UPnPClient {
// The service type string for Content Directories:
const string ContentDirectoryService::SType("urn:schemas-upnp-org:service:ContentDirectory:1");
// We don't include a version in comparisons, as we are satisfied with
// version 1
bool ContentDirectoryService::isCDService(const string& st)
{
const string::size_type sz(SType.size()-2);
return !SType.compare(0, sz, st, 0, sz);
}
static bool DSAccum(vector<ContentDirectoryService>* out,
const UPnPDeviceDesc& device,
const UPnPServiceDesc& service)
{
if (ContentDirectoryService::isCDService(service.serviceType)) {
out->push_back(ContentDirectoryService(device, service));
}
return true;
}
bool ContentDirectoryService::getServices(vector<ContentDirectoryService>& vds)
{
//LOGDEB("UPnPDeviceDirectory::getDirServices" << endl);
UPnPDeviceDirectory::Visitor visitor = bind(DSAccum, &vds, _1, _2);
UPnPDeviceDirectory::getTheDir()->traverse(visitor);
return !vds.empty();
}
static bool DSFriendlySelect(const string& friendlyName,
bool *found,
ContentDirectoryService *out,
const UPnPDeviceDesc& device,
const UPnPServiceDesc& service)
{
if (ContentDirectoryService::isCDService(service.serviceType)) {
if (!friendlyName.compare(device.friendlyName)) {
*out = ContentDirectoryService(device, service);
*found = true;
return false;
}
}
return true;
}
// Get server by friendly name.
bool ContentDirectoryService::getServerByName(const string& friendlyName,
ContentDirectoryService& server)
{
bool found = false;
UPnPDeviceDirectory::Visitor visitor =
bind(DSFriendlySelect, friendlyName, &found, &server, _1, _2);
UPnPDeviceDirectory::getTheDir()->traverse(visitor);
return found;
}
#if 0
static int asyncReaddirCB(Upnp_EventType et, void *vev, void *cookie)
{
LOGDEB("asyncReaddirCB: " << LibUPnP::evTypeAsString(et) << endl);
struct Upnp_Action_Complete *act = (struct Upnp_Action_Complete*)vev;
LOGDEB("asyncReaddirCB: errcode " << act->ErrCode <<
" cturl " << UpnpString_get_String(act->CtrlUrl) <<
" actionrequest " << endl <<
ixmlPrintDocument(act->ActionRequest) << endl <<
" actionresult " << ixmlPrintDocument(act->ActionResult) << endl);
return 0;
}
int ret =
UpnpSendActionAsync(hdl, m_actionURL.c_str(), m_serviceType.c_str(),
0 /*devUDN*/, request, asyncReaddirCB, 0);
sleep(10);
return -1;
#endif
int ContentDirectoryService::readDirSlice(const string& objectId, int offset,
int count, UPnPDirContent& dirbuf,
int *didreadp, int *totalp)
{
LOGDEB("CDService::readDirSlice: objId [" << objectId << "] offset " <<
offset << " count " << count << endl);
// Create request
// Some devices require an empty SortCriteria, else bad params
SoapData args(m_serviceType, "Browse");
args("ObjectID", objectId)
("BrowseFlag", "BrowseDirectChildren")
("Filter", "*")
("SortCriteria", "")
("StartingIndex", SoapHelp::i2s(offset))
("RequestedCount", SoapHelp::i2s(count));
SoapArgs data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
return ret;
}
int didread;
string tbuf;
if (!data.getInt("NumberReturned", &didread) ||
!data.getInt("TotalMatches", totalp) ||
!data.getString("Result", &tbuf)) {
LOGERR("CDService::readDir: missing elts in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (didread <= 0) {
LOGINF("CDService::readDir: got -1 or 0 entries" << endl);
return UPNP_E_BAD_RESPONSE;
}
#if 0
cerr << "CDService::readDirSlice: count " << count <<
" offset " << offset <<
" total " << *totalp << endl;
cerr << " result " << tbuf << endl;
#endif
dirbuf.parse(tbuf);
*didreadp = didread;
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::readDir(const string& objectId,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::readDir: url [" << m_actionURL << "] type [" <<
m_serviceType << "] udn [" << m_deviceId << "] objId [" <<
objectId << endl);
int offset = 0;
int total = 1000;// Updated on first read.
while (offset < total) {
int count;
int error = readDirSlice(objectId, offset, m_rdreqcnt, dirbuf,
&count, &total);
if (error != UPNP_E_SUCCESS)
return error;
offset += count;
}
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::search(const string& objectId,
const string& ss,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::search: url [" << m_actionURL << "] type [" <<
m_serviceType << "] udn [" << m_deviceId << "] objid [" <<
objectId << "] search [" << ss << "]" << endl);
int offset = 0;
int total = 1000;// Updated on first read.
while (offset < total) {
// Create request
SoapData args(m_serviceType, "Search");
args("ContainerID", objectId)
("SearchCriteria", ss)
("Filter", "*")
("SortCriteria", "")
("StartingIndex", SoapHelp::i2s(offset))
("RequestedCount", "10");
SoapArgs data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::search: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
int count = -1;
string tbuf;
if (!data.getInt("NumberReturned", &count) ||
!data.getInt("TotalMatches", &total) ||
!data.getString("Result", &tbuf)) {
LOGERR("CDService::search: missing elts in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (count <= 0) {
LOGINF("CDService::search: got -1 or 0 entries" << endl);
return count < 0 ? UPNP_E_BAD_RESPONSE : UPNP_E_SUCCESS;
}
offset += count;
dirbuf.parse(tbuf);
}
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::getSearchCapabilities(set<string>& result)
{
LOGDEB("CDService::getSearchCapabilities:" << endl);
SoapData args(m_serviceType, "GetSearchCapabilities");
SoapArgs data;
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::getSearchCapa: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
string tbuf;
if (!data.getString("SearchCaps", &tbuf)) {
LOGERR("CDService::getSearchCaps: missing Result in response" << endl);
cerr << tbuf << endl;
return UPNP_E_BAD_RESPONSE;
}
result.clear();
if (!tbuf.compare("*")) {
result.insert(result.end(), "*");
} else if (!tbuf.empty()) {
if (!csvToStrings(tbuf, result)) {
return UPNP_E_BAD_RESPONSE;
}
}
return UPNP_E_SUCCESS;
}
int ContentDirectoryService::getMetadata(const string& objectId,
UPnPDirContent& dirbuf)
{
LOGDEB("CDService::getMetadata: url [" << m_actionURL << "] type [" <<
m_serviceType << "] udn [" << m_deviceId << "] objId [" <<
objectId << "]" << endl);
SoapData args(m_serviceType, "Browse");
SoapArgs data;
args("ObjectID", objectId)
("BrowseFlag", "BrowseMetadata")
("Filter", "*")
("SortCriteria", "")
("StartingIndex", "0")
("RequestedCount", "1");
int ret = runAction(args, data);
if (ret != UPNP_E_SUCCESS) {
LOGINF("CDService::getmetadata: UpnpSendAction failed: " <<
UpnpGetErrorMessage(ret) << endl);
return ret;
}
string tbuf;
if (!data.getString("Result", &tbuf)) {
LOGERR("CDService::getmetadata: missing Result in response" << endl);
return UPNP_E_BAD_RESPONSE;
}
if (dirbuf.parse(tbuf))
return UPNP_E_SUCCESS;
else
return UPNP_E_BAD_RESPONSE;
}
} // namespace UPnPClient