Implémentation du périphérique BinaryLight

Ceci est un exemple de périphérique/service UPnP, implémentant les services BinaryLight et SwitchPower pour émuler une interruption de lumière.

L'interface utilisateur a été simplifiée afin de montrer les conceptes et les méthodes basiques.

Exemple #1 Implémentation d'un serveur lumineux

<?php

/* Définit la cible */
function set_target_cb($service$action$arg)
{
    
/* Récupère la nouvelle valeur cible */
    
$target gupnp_service_action_get($action'NewTargetValue'GUPNP_TYPE_BOOLEAN);

    
/* Si la nouvelle cible ne correspond pas au statut courant, on modifie
       le statut et nous émettons une notification de modification du statut. */
    
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");
    }

    
/* Retourne le succès de l'opération au client */
    
gupnp_service_action_return($action);
}

/* Récupère la cible */
function get_target_cb($service$action$arg)
{
    
gupnp_service_action_set($action'RetTargetValue'GUPNP_TYPE_BOOLEAN$GLOBALS['status']);
    
gupnp_service_action_return($action);
}

/* Récupère le statut */
function get_status_cb($service$action$arg)
{
    
gupnp_service_action_set($action'ResultStatus'GUPNP_TYPE_BOOLEAN$GLOBALS['status']);
    
gupnp_service_action_return($action);
}

/* Par défaut, la lumière est éteinte */
$GLOBALS['status'] = false;
printf("The light is now %s.\n"$GLOBALS['status'] ? "on" "off");

/* Création du contexte UPnP */
$context gupnp_context_new();
if (!
$context) {
    
printf("Erreur lors de la création du contexte GUPnP\n");
    exit(-
1);
}

/* Héberge le dossier qui contient le fichier de description du périphérique et du service */
gupnp_context_host_path($context"./web""");

/* Crée un nouveau périphérique racine */
$location "/BinaryLight.xml";
$dev gupnp_root_device_new($context$location);
gupnp_root_device_set_available($devtrue);

/* Récupère le service de modification de lumière depuis le périphérique racine */
$service_type "urn:schemas-upnp-org:service:SwitchPower:1";
$service gupnp_device_info_get_service($dev$service_type);
if (!
$service) {
    die(
"Impossible de récupérer le service SwitchPower1\n");
}

/* Définit la fonction de rappel pour l'action GetStatus */
gupnp_device_action_callback_set($serviceGUPNP_SIGNAL_ACTION_INVOKED"GetStatus"
    
"get_status_cb""action data, GetStatus");

/* Définit la fonction de rappel pour l'action GetTarget */
gupnp_device_action_callback_set($serviceGUPNP_SIGNAL_ACTION_INVOKED"GetTarget"
    
"get_target_cb""action data, GetTarget");

/* Définit la fonction de rappel pour l'action SetTarget */
gupnp_device_action_callback_set($serviceGUPNP_SIGNAL_ACTION_INVOKED"SetTarget"
    
"set_target_cb""action data, SetTarget");

/* Exécute la boucle principale */
gupnp_root_device_start($dev);

?>

Exemple #2 Implémentation du client lumineux

<?php

function service_proxy_available_cb($proxy$arg)
{
    
$mode $arg['mode'];

    
printf("Set subscribed\n");
    
gupnp_service_proxy_set_subscribed($proxytrue);

    
/* Ajout une notification si le statut a changé */
    
if (!gupnp_service_proxy_add_notify($proxy"Status"
            
GUPNP_TYPE_BOOLEAN"status_changed_cb"NULL)) {
        
printf("Echec de l'ajout de la notification\n");
    }
    
    if (
$mode == 'TOGGLE') {
        
/* Nous basculons, aussi, en premier lieu, nous récupérons le statut courant */
        
$target gupnp_service_proxy_action_get($proxy'GetStatus''ResultStatus'GUPNP_TYPE_BOOLEAN);

        
/* Et ensuite, nous basculons */
        
$target = ! $target;
    } else {
        
/* Mode est un booléen, aussi, la cible est le mode que nous venons de choisir
           enumeration values. */
        
$target = ($mode == 'ON') ? true false;
    }

    
/* Définit la cible */
    
if (!gupnp_service_proxy_action_set($proxy'SetTarget''NewTargetValue'$targetGUPNP_TYPE_BOOLEAN)) {
        
printf("Impossible de basculer\n");
    } else {
        
printf("Set switch to %s.\n"$target "on" "off");
    }
    
    
/* Arrêt de la recherche */
    
gupnp_control_point_browse_stop($arg['cp']);
}

function 
status_changed_cb($variable$value$arg)
{
    
printf("Le statut a changé\n");
    
printf("\tvariable name: %s\n"$variable);
    
printf("\tvalue: %s\n", (int)$value);
    
printf("\n");
}

/* Vérifie et analyse les arguments de la ligne de commande */
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);
}

/* Crée un contexte UPnP */
$context gupnp_context_new();
if (!
$context) {
    
printf("Erreur lors de la création du contexte GUPnP\n");
    exit(-
1);
}

/* Crée un point de contrôle, et cherche les services SwitchPower */
$cp gupnp_control_point_new ($context,
        
"urn:schemas-upnp-org:service:SwitchPower:1");

/* Connexion à la fonction de rappel utilisée lorsque le service est trouvé */
$cb "service_proxy_available_cb";
$arg = array('mode' => $mode'cp' => $cp);
gupnp_control_point_callback_set($cpGUPNP_SIGNAL_SERVICE_PROXY_AVAILABLE$cb$arg);

/* Commence la recherche */
gupnp_control_point_browse_start($cp);

?>