#!/usr/bin/php
<?php
/**
 * testing upnp-av deamon - used for reversing gupnp bindings
 * warning: minefield, can even crash on inproper types
 */
function cb_generic($service, $action, $arg) {
	echo("generic: $arg\n");
}
function cb_GetSearchCapabilities($service, $action, $arg) {
	gupnp_service_action_set($action, 'SearchCapabilities', GUPNP_TYPE_STRING, 
		'dc:title,dc:creator,upnp:artist,upnp:genre,upnp:album,dc:date,upnp:originalTrackNumber,upnp:class,@id,@refID,@protocolInfo');
	gupnp_service_action_return($action); 
}
function cb_GetSortCapabilities($service, $action, $arg) {
	gupnp_service_action_set($action, 'SortCapabilities', GUPNP_TYPE_STRING, 
		'dc:title,dc:creator,upnp:artist,upnp:genre,upnp:album,dc:date,upnp:originalTrackNumber,Philips:shuffle');
	gupnp_service_action_return($action);
}
function cb_Browse($service, $action, $arg) {
	$object = gupnp_service_action_get($action, 'ObjectID', GUPNP_TYPE_STRING);
	$browseflag = gupnp_service_action_get($action, 'BrowseFlag', GUPNP_TYPE_STRING);
	echo("cb_Browse: $object - $browseflag\n");

	$resHeader = '
<DIDL-Lite xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:upnp="urn:schemas-upnp-org:metadata-1-0/upnp/" xmlns="urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/">';
	$resFooter = '</DIDL-Lite>';
	if($object == 0 && $browseflag == 'BrowseDirectChildren') {
		$countRecords = 1;
		$countStations = 32;
		$amount = 2;
		$update = 1;
		$result = '<container id="1" parentID="0" childCount="'.$countRecords.'" restricted="1" searchable="1">
  <dc:title>Aufnahmen</dc:title><upnp:class>object.container</upnp:class></container>
<container id="2" parentID="0" childCount="'.$countStations.'" restricted="1" searchable="1">
  <dc:title>TV Sender</dc:title><upnp:class>object.container</upnp:class></container>';
	} elseif($object == 1 && $browseflag == 'BrowseMetadata') {
		$result = '<container id="1" parentID="0" restricted="1" ><dc:title>Aufnahmen</dc:title><upnp:class>object.container</upnp:class></container>';
		$update = 2;
		$amount = 1;
//	} elseif($object == 1 && $browseflag == 'BrowseMetadata') {
	} elseif($object == 1 && $browseflag == 'BrowseDirectChildren') {
		// records
		$update = 1;
		$amount = 1;
		$result = '<item id="10" parentID="1" restricted="false">
      <dc:title>beispielvideo</dc:title><upnp:genre>Unknown</upnp:genre>
      <dc:date>2008-06-06</dc:date>
      <res size="99999999999999" resolution="528x576" protocolInfo="http-get:*:video/mpeg2:DLNA.ORG_PN=MPEG_TS_SD_EU_ISO;DLNA.ORG_OP=01;DLNA.ORG_FLAGS=01700000000000000000000000000000">http://192.168.1.100:89/udp/239.0.5.77_8208/wdr.ts</res>
      <upnp:class>object.item.videoItem.movie</upnp:class></item>';
	} elseif($object == 2) {
		// stations
		$update = 2;
		$amount = 0;
	} else {
		gupnp_service_action_return_error($action,701,'No such object');
		return;
	}

	gupnp_service_action_set($action, 'Result', GUPNP_TYPE_STRING, $resHeader.$result.$resFooter);
	gupnp_service_action_set($action, 'NumberReturned', GUPNP_TYPE_INT, $amount);
	gupnp_service_action_set($action, 'TotalMatches', GUPNP_TYPE_INT, $amount);
	gupnp_service_action_set($action, 'UpdateID', GUPNP_TYPE_INT, $update);
	gupnp_service_action_return($action); 
}


$context = gupnp_context_new();
gupnp_context_host_path($context, "/srv/pvr/fw/etc/upnp", "");

$dev = gupnp_root_device_new($context, '/description.xml');
gupnp_root_device_set_available($dev, true);

$serviceConnMan = gupnp_device_info_get_service($dev, "urn:schemas-upnp-org:service:ConnectionManager:1");
$serviceContDir = gupnp_device_info_get_service($dev, "urn:schemas-upnp-org:service:ContentDirectory:1");

$cbRegister = array(
	array($serviceConnMan,'generic','GetCurrentConnectionInfo'),
	array($serviceConnMan,'generic','GetProtocolInfo'),
	array($serviceConnMan,'generic','GetCurrentConnectionIDs'),
	array($serviceContDir,'generic','GetSystemUpdateID'),
	array($serviceContDir,'generic','Search'),
	array($serviceContDir,'GetSearchCapabilities','GetSearchCapabilities'),
	array($serviceContDir,'GetSortCapabilities','GetSortCapabilities'),
	array($serviceContDir,'Browse','Browse'));

foreach($cbRegister as $cb) {
	gupnp_device_action_callback_set($cb[0], GUPNP_SIGNAL_ACTION_INVOKED, $cb[2], 'cb_'.$cb[1], $cb[2]);
}
/* Run the main loop */
gupnp_root_device_start($dev);

//gupnp_root_device_stop($dev);

