©
本文档使用
php.cn手册 发布
This is an example of UPnP device/service, implementing the BinaryLight device and SwitchPower services to emulate a light switch.
The user interface was purposely simplified in order to show basic concepts and methods.
Example #1 Implementing light server
<?php
function set_target_cb ( $service , $action , $arg )
{
$target = gupnp_service_action_get ( $action , 'NewTargetValue' , GUPNP_TYPE_BOOLEAN );
if ( $target != $GLOBALS [ 'status' ]) {
$GLOBALS [ 'status' ] = $target ;
gupnp_service_notify ( $service , 'Status' , GUPNP_TYPE_BOOLEAN , $GLOBALS [ 'status' ]);
printf ( "The light is now %s.\n" , $GLOBALS [ 'status' ] ? "on" : "off" );
}
gupnp_service_action_return ( $action );
}
function get_target_cb ( $service , $action , $arg )
{
gupnp_service_action_set ( $action , 'RetTargetValue' , GUPNP_TYPE_BOOLEAN , $GLOBALS [ 'status' ]);
gupnp_service_action_return ( $action );
}
function get_status_cb ( $service , $action , $arg )
{
gupnp_service_action_set ( $action , 'ResultStatus' , GUPNP_TYPE_BOOLEAN , $GLOBALS [ 'status' ]);
gupnp_service_action_return ( $action );
}
$GLOBALS [ 'status' ] = false ;
printf ( "The light is now %s.\n" , $GLOBALS [ 'status' ] ? "on" : "off" );
$context = gupnp_context_new ();
if (! $context ) {
printf ( "Error creating the GUPnP context\n" );
exit(- 1 );
}
gupnp_context_host_path ( $context , "./web" , "" );
$location = "/BinaryLight.xml" ;
$dev = gupnp_root_device_new ( $context , $location );
gupnp_root_device_set_available ( $dev , true );
$service_type = "urn:schemas-upnp-org:service:SwitchPower:1" ;
$service = gupnp_device_info_get_service ( $dev , $service_type );
if (! $service ) {
die( "Cannot get SwitchPower1 service\n" );
}
gupnp_device_action_callback_set ( $service , GUPNP_SIGNAL_ACTION_INVOKED , "GetStatus" ,
"get_status_cb" , "action data, GetStatus" );
gupnp_device_action_callback_set ( $service , GUPNP_SIGNAL_ACTION_INVOKED , "GetTarget" ,
"get_target_cb" , "action data, GetTarget" );
gupnp_device_action_callback_set ( $service , GUPNP_SIGNAL_ACTION_INVOKED , "SetTarget" ,
"set_target_cb" , "action data, SetTarget" );
gupnp_root_device_start ( $dev );
?>
Example #2 Implementing light client
<?php
function service_proxy_available_cb ( $proxy , $arg )
{
$mode = $arg [ 'mode' ];
printf ( "Set subscribed\n" );
gupnp_service_proxy_set_subscribed ( $proxy , true );
if (! gupnp_service_proxy_add_notify ( $proxy , "Status" ,
GUPNP_TYPE_BOOLEAN , "status_changed_cb" , NULL )) {
printf ( "Failed to add notify\n" );
}
if ( $mode == 'TOGGLE' ) {
$target = gupnp_service_proxy_action_get ( $proxy , 'GetStatus' , 'ResultStatus' , GUPNP_TYPE_BOOLEAN );
$target = ! $target ;
} else {
$target = ( $mode == 'ON' ) ? true : false ;
}
if (! gupnp_service_proxy_action_set ( $proxy , 'SetTarget' , 'NewTargetValue' , $target , GUPNP_TYPE_BOOLEAN )) {
printf ( "Cannot set switch\n" );
} else {
printf ( "Set switch to %s.\n" , $target ? "on" : "off" );
}
gupnp_control_point_browse_stop ( $arg [ 'cp' ]);
}
function status_changed_cb ( $variable , $value , $arg )
{
printf ( "Status has been changed\n" );
printf ( "\tvariable name: %s\n" , $variable );
printf ( "\tvalue: %s\n" , (int) $value );
printf ( "\n" );
}
if ( count ( $argv ) != 2 ) {
printf ( "Usage: light-client.php [on|off|toggle]\n" );
exit(- 1 );
}
if ( $argv [ 1 ] == "on" ) {
$mode = 'ON' ;
} elseif ( $argv [ 1 ] == "off" ) {
$mode = 'OFF' ;
} elseif ( $argv [ 1 ] == "toggle" ) {
$mode = 'TOGGLE' ;
} else {
usage ();
exit(- 1 );
}
$context = gupnp_context_new ();
if (! $context ) {
printf ( "Error creating the GUPnP context\n" );
exit(- 1 );
}
$cp = gupnp_control_point_new ( $context ,
"urn:schemas-upnp-org:service:SwitchPower:1" );
$cb = "service_proxy_available_cb" ;
$arg = array( 'mode' => $mode , 'cp' => $cp );
gupnp_control_point_callback_set ( $cp , GUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE , $cb , $arg );
gupnp_control_point_browse_start ( $cp );
?>