Ejemplo n.º 1
0
//This function takes the request as an input and returns the response
//in JSON format.
OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
{
    LEDResource *currLEDResource = &LED;

    OC_LOG(INFO, TAG, "Entering constructResponse");

    if (ehRequest->resource == gLedInstance[0].handle)
    {
        OC_LOG(INFO, TAG, "handle 0");
        currLEDResource = &gLedInstance[0];
        gResourceUri = const_cast<char *>("a/led/0");
    }
    else if (ehRequest->resource == gLedInstance[1].handle)
    {
        OC_LOG(INFO, TAG, "handle 1");
        currLEDResource = &gLedInstance[1];
        gResourceUri = const_cast<char *>("a/led/1");
    }

    if(OC_REST_PUT == ehRequest->method)
    {
        if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
        {
            OC_LOG(ERROR, TAG, PCF("Incoming payload not a representation"));
            return nullptr;
        }

        OCRepPayload *putPayload = reinterpret_cast<OCRepPayload*> (ehRequest->payload);

        int64_t power;
        bool state;

        if (OCRepPayloadGetPropBool(putPayload, "state", &state))
        {
            currLEDResource->state = state;
        }
        if (OCRepPayloadGetPropInt (putPayload, "power", &power))
        {
            currLEDResource->power = power;
        }
    }

    OCRepPayload *response = OCRepPayloadCreate();

    if (!response)
    {
        OC_LOG_V(ERROR, TAG, "Memory allocation for response payload failed.");
    }

    OCRepPayloadSetUri (response, gResourceUri);
    OCRepPayloadSetPropBool(response, "state", currLEDResource->state);
    OCRepPayloadSetPropInt(response, "power", currLEDResource->power);

    return response;
}
//This function takes the request as an input and returns the response
OCRepPayload* constructResponse (OCEntityHandlerRequest *ehRequest)
{
    if(ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
        OIC_LOG(ERROR, TAG, "Incoming payload not a representation");
        return NULL;
    }

    OCRepPayload* input = (OCRepPayload*)(ehRequest->payload);

    LEDResource *currLEDResource = &LED;

    if (ehRequest->resource == gLedInstance[0].handle)
    {
        currLEDResource = &gLedInstance[0];
        gResourceUri = (char *) "/a/led/0";
    }
    else if (ehRequest->resource == gLedInstance[1].handle)
    {
        currLEDResource = &gLedInstance[1];
        gResourceUri = (char *) "/a/led/1";
    }

    if(OC_REST_PUT == ehRequest->method)
    {
        // Get pointer to query
        int64_t pow;
        if(OCRepPayloadGetPropInt(input, "power", &pow))
        {
            currLEDResource->power =pow;
        }

        bool state;
        if(OCRepPayloadGetPropBool(input, "state", &state))
        {
            currLEDResource->state = state;
        }
    }

    return getPayload(gResourceUri, currLEDResource->power, currLEDResource->state);
}
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;
}
Ejemplo n.º 4
0
bool getZigBeeAttributesIfValid (char * OICResourceType,
                                    AttributeList *attributeList,
                                    OCRepPayload *payload) // Put
{
    if(!OICResourceType)
    {
        return false;
    }
    if(strcmp(OICResourceType, OIC_TEMPERATURE_SENSOR) == 0)
    {
        // Cant really PUT on the temp sensor, but the code is still there.
        int64_t temperature = 0;

        // TODO: This if should only look for attributes it supports and ignore the rest
        // or examine every attribute in the payload and complain about unsupported attributes?
        if(OCRepPayloadGetPropInt(payload, OIC_TEMPERATURE_ATTRIBUTE, &temperature))
        {
            attributeList->count = 1;
            attributeList->list[0].oicAttribute = OICStrdup(OIC_TEMPERATURE_ATTRIBUTE);
            attributeList->list[0].zigBeeAttribute = ZB_TEMPERATURE_ATTRIBUTE_ID;
            attributeList->list[0].oicType = OIC_ATTR_DOUBLE;
            attributeList->list[0].val.d = temperature;
            attributeList->list[0].zigbeeType = ZB_16_SINT;
            attributeList->CIEMask = (CIECommandMask) 0;

            return true;
        }
    }
    else if (strcmp (OICResourceType, OIC_DIMMABLE_LIGHT) == 0)
    {
        int64_t onLevel = 0;

        if(OCRepPayloadGetPropInt(payload, OIC_DIMMING_ATTRIBUTE, &onLevel))
        {
            attributeList->count = 1;
            attributeList->list[0].oicAttribute = OICStrdup(OIC_DIMMING_ATTRIBUTE);
            attributeList->list[0].zigBeeAttribute = ZB_ON_LEVEL_ATTRIBUTE;
            attributeList->list[0].oicType = OIC_ATTR_INT;
            attributeList->list[0].val.i = onLevel;
            attributeList->list[0].zigbeeType = ZB_8_UINT;

            // Level control cluster is dealing with level in the PUT payload.
            attributeList->CIEMask = attributeList->CIEMask | CIE_MOVE_TO_LEVEL;
            return true;
        }
    }
    else if (strcmp (OICResourceType, OIC_CONTACT_SENSOR) == 0)
    {
        int64_t value = 0;

        if(OCRepPayloadGetPropInt(payload, OIC_CONTACT_ATTRIBUTE, &value))
        {
            attributeList->count = 1;
            attributeList->list[0].oicAttribute = OICStrdup(OIC_CONTACT_ATTRIBUTE);
            attributeList->list[0].zigBeeAttribute = ZB_CONTACT_ATTRIBUTE_ID;
            attributeList->list[0].oicType = OIC_ATTR_BOOL;
            attributeList->list[0].val.i = value;
            attributeList->list[0].zigbeeType = ZB_BOOL;
            attributeList->CIEMask = (CIECommandMask) 0;

            return true;
        }
    }
    else if (strcmp (OICResourceType, OIC_BINARY_SWITCH) == 0)
    {
        bool value = 0;

        if(OCRepPayloadGetPropBool(payload, OIC_ON_OFF_ATTRIBUTE, &value))
        {
            attributeList->count = 1;
            attributeList->list[0].oicAttribute = OICStrdup(OIC_ON_OFF_ATTRIBUTE);
            attributeList->list[0].zigBeeAttribute = ZB_ON_OFF_ATTRIBUTE_ID;
            attributeList->list[0].oicType = OIC_ATTR_BOOL;
            attributeList->list[0].val.b = value;
            attributeList->list[0].zigbeeType = ZB_BOOL;

            attributeList->CIEMask = attributeList->CIEMask | CIE_RON_OFF;
            return true;
        }
    }
    return false;
}
Ejemplo n.º 5
0
 /**
 * @brief Called when a REST PUT is request
 *
 * @param OCBaseResource base resource attributes
 *
 * @return result of the entityHandler
 */
 OCEntityHandlerResult putRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload* payload, OCBaseResourceT *resource)
 {
    // Set the new states
    OCRepPayload* inputPayload = (OCRepPayload*)(ehRequest->payload);

    OCAttributeT *current = resource->attribute;
    while(current != NULL)
    {
        switch(current->value.dataType)
        {
        case INT:
        {
            int64_t value(0);
            if(OCRepPayloadGetPropInt(inputPayload, current->name, &value))
            {
                //OIC_LOG_V(DEBUG, TAG, "PUT: Type is int: %i", (int) value);
                //*((int*)current->value.data) = (int) value;
                current->value.data.i = value;
            }
            OCRepPayloadSetPropInt(payload, current->name, value);
            break;
        }
        case DOUBLE:
        {
            double value(0);
            if(OCRepPayloadGetPropDouble(inputPayload, current->name, &value))
            {
                //OIC_LOG_V(DEBUG, TAG, "PUT: type is double: &d", value);
                //*((double*)current->value.data) = value;
                current->value.data.d = value;
            }
            OCRepPayloadSetPropDouble(payload, current->name, value);
            break;
        }
        case STRING:
        {
            char* value("");
            if(OCRepPayloadGetPropString(inputPayload, current->name, &value))
            {
                //OIC_LOG_V(DEBUG, TAG, "PUT: type is string: %s", value);
                //*((char**)current->value.data) = value;
                current->value.data.str = value;
            }
            OCRepPayloadSetPropString(payload, current->name, value);
        }
        case BOOL:
        {
            bool value(false);
            if(OCRepPayloadGetPropBool(inputPayload, current->name, &value))
            {
                //OIC_LOG_V(DEBUG, TAG, "PUT: Type is bool: %s", value ? "true" : "false");
                //*((bool*)current->value.data) = value;
                current->value.data.b = value;
            }
            OCRepPayloadSetPropBool(payload, current->name, value);
            break;
        }

        }

        current = current->next;
    }

    // Set the output pins
    if(resource->OCIOhandler)
    {
        OIC_LOG_V(DEBUG, TAG, "Value of underObservation is: %s", resource->underObservation ? "true" : "false");
        resource->OCIOhandler(resource->attribute, OUTPUT, resource->handle, &resource->underObservation);
    }
    else
    {
        OIC_LOG(ERROR, TAG, "Resource OutputHandler has not been set");
    }

    OIC_LOG(DEBUG, TAG, "Leaving putRequest");

    return OC_EH_OK;
 }
Ejemplo n.º 6
0
static bool add_property_in_post_req_msg(st_things_set_request_message_s *req_msg, OCRepPayload *req_payload, things_attribute_info_s *prop)
{
	RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg);
	RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep);
	RET_FALSE_IF_PARAM_IS_NULL(TAG, req_msg->rep->payload);
	RET_FALSE_IF_PARAM_IS_NULL(TAG, req_payload);
	RET_FALSE_IF_PARAM_IS_NULL(TAG, prop);

	OCRepPayload *resp_payload = req_msg->rep->payload;

	THINGS_LOG_D(TAG, "Property Key is %s", prop->key);
	THINGS_LOG_D(TAG, "Property type is %d", prop->type);

	// Based on the property type, call appropriate methods to copy
	// the property from request payload to request representation.
	bool result = false;
	switch (prop->type) {
	case BOOL_ID: {
		bool value = false;
		if (OCRepPayloadGetPropBool(req_payload, prop->key, &value)) {
			result = OCRepPayloadSetPropBool(resp_payload, prop->key, value);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the boolean value of '%s' in request message.", prop->key);
			}
		} else {
			THINGS_LOG_E(TAG, "Failed to get the boolean value of '%s' for request message.", prop->key);
		}
	}
	break;
	case INT_ID: {
		int64_t value = 0;
		if (OCRepPayloadGetPropInt(req_payload, prop->key, &value)) {
			result = OCRepPayloadSetPropInt(resp_payload, prop->key, value);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the integer value of '%s' in request message", prop->key);
			}
		} else {
			THINGS_LOG_E(TAG, "Failed to get the integer value of '%s' for request message", prop->key);
		}
	}
	break;
	case DOUBLE_ID: {
		double value = 0.0;
		if (OCRepPayloadGetPropDouble(req_payload, prop->key, &value)) {
			result = OCRepPayloadSetPropDouble(resp_payload, prop->key, value);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the double value of '%s' in request message", prop->key);
			}
		} else {
			THINGS_LOG_E(TAG, "Failed to get the double value of '%s' for request message", prop->key);
		}
	}
	break;
	case STRING_ID: {
		char *value = NULL;
		if (OCRepPayloadGetPropString(req_payload, prop->key, &value)) {
			result = OCRepPayloadSetPropString(resp_payload, prop->key, value);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the string value of '%s' in request message", prop->key);
			}

			things_free(value);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the string value of '%s' for request message", prop->key);
		}
	}
	break;
	case OBJECT_ID: {
		OCRepPayload *value = NULL;
		if (OCRepPayloadGetPropObject(req_payload, prop->key, &value)) {
			result = OCRepPayloadSetPropObject(resp_payload, prop->key, value);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the object value of '%s' in request message", prop->key);
			}

			OCRepPayloadDestroy(value);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the object value of '%s' for request message", prop->key);
		}
	}
	break;
	case BYTE_ID: {
		OCByteString byte_value;
		if (OCRepPayloadGetPropByteString(req_payload, prop->key, &byte_value)) {
			result = OCRepPayloadSetPropByteString(resp_payload, prop->key, byte_value);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the byte string value of '%s' in request message", prop->key);
			}

			things_free(byte_value.bytes);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the byte string value of '%s' for request message", prop->key);
		}
	}
	break;
	case INT_ARRAY_ID: {
		int64_t *value = NULL;
		size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
		if (OCRepPayloadGetIntArray(req_payload, prop->key, &value, dimensions)) {
			result = OCRepPayloadSetIntArray(resp_payload, prop->key, value, dimensions);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the integer array value of '%s' in request message", prop->key);
			}

			things_free(value);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the integer array value of '%s' for request message", prop->key);
		}
	}
	break;
	case DOUBLE_ARRAY_ID: {
		double *value = NULL;
		size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
		if (OCRepPayloadGetDoubleArray(req_payload, prop->key, &value, dimensions)) {
			result = OCRepPayloadSetDoubleArray(resp_payload, prop->key, value, dimensions);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the double array value of '%s' in request message", prop->key);
			}

			things_free(value);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the double array value of '%s' for request message", prop->key);
		}
	}
	break;
	case STRING_ARRAY_ID: {
		char **value = NULL;
		size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
		if (OCRepPayloadGetStringArray(req_payload, prop->key, &value, dimensions)) {
			result = OCRepPayloadSetStringArray(resp_payload, prop->key, value, dimensions);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the string array value of '%s' in request message", prop->key);
			}

			size_t len = calcDimTotal(dimensions);
			things_free_str_array(value, len);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the string array value of '%s' for request message", prop->key);
		}
	}
	break;
	case OBJECT_ARRAY_ID: {
		OCRepPayload **value = NULL;
		size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
		if (OCRepPayloadGetPropObjectArray(req_payload, prop->key, &value, dimensions)) {
			result = OCRepPayloadSetPropObjectArray(resp_payload, prop->key, value, dimensions);
			if (!result) {
				THINGS_LOG_E(TAG, "Failed to set the object array value of '%s' in request message", prop->key);
			}

			size_t len = calcDimTotal(dimensions);
			for (size_t index = 0; index < len; index++) {
				OCRepPayloadDestroy(value[index]);
			}
			things_free(value);
		} else {
			THINGS_LOG_E(TAG, "Failed to get the object array value of '%s' for request message", prop->key);
		}
	}
	break;
	default:
		THINGS_LOG_E(TAG, "Invalid property type (%d).", prop->type);
		break;
	}

	return result;
}
Ejemplo n.º 7
0
bool things_get_bool_value(struct things_representation_s *rep, char *key, bool *value)
{
	return OCRepPayloadGetPropBool((OCRepPayload *) rep->payload, key, value);
}