Exemple #1
0
static void
service_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                            GUPnPServiceProxy               *proxy)
{
  GError *error = NULL;
  gboolean target;

  if (mode == TOGGLE) {
    /* We're toggling, so first fetch the current status */
    if (!gupnp_service_proxy_send_action
        (proxy, "GetStatus", &error,
         /* IN args */ NULL,
         /* OUT args */ "ResultStatus", G_TYPE_BOOLEAN, &target, NULL)) {
      goto error;
    }
    /* And then toggle it */
    target = ! target;
  } else {
    /* Mode is a boolean, so the target is the mode thanks to our well chosen
       enumeration values. */
    target = mode;
  }

  /* Set the target */
  if (!gupnp_service_proxy_send_action (proxy, "SetTarget", &error,
                                        /* IN args */
                                        "NewTargetValue", G_TYPE_BOOLEAN, target, NULL,
                                        /* OUT args */
                                        NULL)) {
    goto error;
  } else {
    g_print ("Set switch to %s.\n", target ? "on" : "off");
  }
  
 done:
  /* Only manipulate the first light switch that is found */
  g_main_loop_quit (main_loop);
  return;

 error:
  g_printerr ("Cannot set switch: %s\n", error->message);
  g_error_free (error);
  goto done;
}
OCEntityHandlerResult UpnpRenderingControl::processGetRequest(string uri, OCRepPayload *payload, string resourceType)
{
    if (payload == NULL)
    {
        throw "payload is null";
    }
    (void) resourceType;

    //TODO use async version with callback
    bool muteValue = false;
    int upnpVolumeValue = 0;
    GError *error = NULL;

    // get mute
    if (! gupnp_service_proxy_send_action(m_proxy, getMuteAction, &error,
        // IN args
        instanceIdParamName, G_TYPE_UINT, defaultInstanceID,
        channelParamName, G_TYPE_STRING, defaultChannel,
        NULL,
        // OUT args
        currentMuteParamName, G_TYPE_BOOLEAN, &muteValue,
        NULL))
    {
        ERROR_PRINT(getMuteAction << " action failed");
        if (error)
        {
            DEBUG_PRINT("Error message: " << error->message);
            g_error_free(error);
        }
        return OC_EH_ERROR;
    }

    if (!OCRepPayloadSetPropBool(payload, mutePropertyName, muteValue))
    {
        throw "Failed to set mute value in payload";
    }
    DEBUG_PRINT(mutePropertyName << ": " << (muteValue ? "true" : "false"));

    // get volume
    if (! gupnp_service_proxy_send_action(m_proxy, getVolumeAction, &error,
        // IN args
        instanceIdParamName, G_TYPE_UINT, defaultInstanceID,
        channelParamName, G_TYPE_STRING, defaultChannel,
        NULL,
        // OUT args
        currentVolumeParamName, G_TYPE_UINT, &upnpVolumeValue,
        NULL))
    {
        ERROR_PRINT(getVolumeAction << " action failed");
        if (error)
        {
            DEBUG_PRINT("Error message: " << error->message);
            g_error_free(error);
        }
        return OC_EH_ERROR;
    }

    int64_t volumeValue = upnpVolumeValue;
    if (!OCRepPayloadSetPropInt(payload, volumePropertyName, volumeValue))
    {
        throw "Failed to set volume value in payload";
    }
    DEBUG_PRINT(volumePropertyName << ": " << volumeValue);

    return UpnpService::processGetRequest(uri, payload, resourceType);
}
OCEntityHandlerResult UpnpRenderingControl::processPutRequest(OCEntityHandlerRequest *ehRequest,
        string uri, string resourceType, OCRepPayload *payload)
{
    (void) uri;
    if (!ehRequest || !ehRequest->payload ||
            ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
        throw "Incoming payload is NULL or not a representation";
    }

    OCRepPayload *input = reinterpret_cast<OCRepPayload *>(ehRequest->payload);
    if (!input)
    {
        throw "PUT payload is null";
    }

    if (UPNP_OIC_TYPE_AUDIO == resourceType)
    {
        //TODO use async version with callback
        GError *error = NULL;

        // set mute
        bool muteValue = false;
        if (OCRepPayloadGetPropBool(input, mutePropertyName, &muteValue))
        {
            DEBUG_PRINT("New " << mutePropertyName << ": " << (muteValue ? "true" : "false"));
            if (! gupnp_service_proxy_send_action(m_proxy, setMuteAction, &error,
                // IN args
                instanceIdParamName, G_TYPE_UINT, defaultInstanceID,
                channelParamName, G_TYPE_STRING, defaultChannel,
                desiredMuteParamName, G_TYPE_BOOLEAN, muteValue,
                NULL,
                // OUT args (none)
                NULL))
            {
                ERROR_PRINT(setMuteAction << " action failed");
                if (error)
                {
                    DEBUG_PRINT("Error message: " << error->message);
                    g_error_free(error);
                }
                return OC_EH_ERROR;
            }

            if (!OCRepPayloadSetPropBool(payload, mutePropertyName, muteValue))
            {
                throw "Failed to set mute value in payload";
            }
            DEBUG_PRINT(mutePropertyName << ": " << (muteValue ? "true" : "false"));
        }

        // set volume
        int64_t volumeValue = 0;
        if (OCRepPayloadGetPropInt(input, volumePropertyName, &volumeValue))
        {
            DEBUG_PRINT("New " << volumePropertyName << ": " << volumeValue);
            int upnpVolumeValue = volumeValue;
            if (! gupnp_service_proxy_send_action(m_proxy, setVolumeAction, &error,
                // IN args
                instanceIdParamName, G_TYPE_UINT, defaultInstanceID,
                channelParamName, G_TYPE_STRING, defaultChannel,
                desiredVolumeParamName, G_TYPE_UINT, upnpVolumeValue,
                NULL,
                // OUT args (none)
                NULL))
            {
                ERROR_PRINT(setVolumeAction << " action failed");
                if (error)
                {
                    DEBUG_PRINT("Error message: " << error->message);
                    g_error_free(error);
                }
                return OC_EH_ERROR;
            }

            if (!OCRepPayloadSetPropInt(payload, volumePropertyName, volumeValue))
            {
                throw "Failed to set volume value in payload";
            }
            DEBUG_PRINT(volumePropertyName << ": " << volumeValue);
        }
    }
    else
    {
        throw "Failed due to unknown resource type";
    }

    return OC_EH_OK;
}
Exemple #4
0
static void
service_proxy_available_cb (G_GNUC_UNUSED GUPnPControlPoint *cp,
                            GUPnPServiceProxy               *proxy)
{
        const char *location;
        char *result = NULL;
        guint count, total;
        GError *error = NULL;

        location = gupnp_service_info_get_location (GUPNP_SERVICE_INFO (proxy));

        g_print ("ContentDirectory available:\n");
        g_print ("\tlocation: %s\n", location);

        /* We want to be notified whenever SystemUpdateID (of type uint)
         * changes */
        gupnp_service_proxy_add_notify (proxy,
                                        "SystemUpdateID",
                                        G_TYPE_UINT,
                                        notify_cb,
                                        (gpointer) "Test");

        /* Subscribe */
        g_signal_connect (proxy,
                          "subscription-lost",
                          G_CALLBACK (subscription_lost_cb),
                          NULL);

        gupnp_service_proxy_set_subscribed (proxy, TRUE);

        /* And test action IO */
        gupnp_service_proxy_send_action (proxy,
                                         "Browse",
                                         &error,
                                         /* IN args */
                                         "ObjectID",
                                                G_TYPE_STRING,
                                                "0",
                                         "BrowseFlag",
                                                G_TYPE_STRING,
                                                "BrowseDirectChildren",
                                         "Filter",
                                                G_TYPE_STRING,
                                                "*",
                                         "StartingIndex",
                                                G_TYPE_UINT,
                                                0,
                                         "RequestedCount",
                                                G_TYPE_UINT,
                                                0,
                                         "SortCriteria",
                                                G_TYPE_STRING,
                                                "",
                                         NULL,
                                         /* OUT args */
                                         "Result",
                                                G_TYPE_STRING,
                                                &result,
                                         "NumberReturned",
                                                G_TYPE_UINT,
                                                &count,
                                         "TotalMatches",
                                                G_TYPE_UINT,
                                                &total,
                                         NULL);

        if (error) {
                g_printerr ("Error: %s\n", error->message);
                g_error_free (error);

                return;
        }

        g_print ("Browse returned:\n");
        g_print ("\tResult:         %s\n", result);
        g_print ("\tNumberReturned: %u\n", count);
        g_print ("\tTotalMatches:   %u\n", total);

        g_free (result);
}