OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
                                                OCRepPayload** payload)
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (!ehRequest)
    {
        OC_LOG(ERROR, ES_RH_TAG, "Request is Null");
        return ehResult;
    }
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
        OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
        OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
        return ehResult;
    }
    char* tr;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
    {

        // Triggering
        ehResult = OC_EH_OK;
    }

    g_flag = 1;

    return ehResult;
}
Ejemplo n.º 2
0
OCEntityHandlerResult LcdOCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest * entityHandlerRequest,
                                        void *callbackParam)
{
	OCEntityHandlerResult ehRet = OC_EH_OK;
	OCEntityHandlerResponse response = {0};
	OCRepPayload* payload = OCRepPayloadCreate();
	if(!payload) {
		OC_LOG(ERROR, TAG, ("Failed to allocate Payload"));
		return OC_EH_ERROR;
	}

	if(entityHandlerRequest && (flag & OC_REQUEST_FLAG)) {
		OC_LOG (INFO, TAG, ("Flag includes OC_REQUEST_FLAG"));

		if(OC_REST_GET == entityHandlerRequest->method) {
			OCRepPayloadSetUri(payload, "/grove/lcd");
			OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str);
		} else if(OC_REST_PUT == entityHandlerRequest->method) {
			OC_LOG(INFO, TAG, ("PUT request"));
			OCRepPayload *rep = (OCRepPayload *)entityHandlerRequest->payload;
			OCRepPayloadGetPropString(rep, "lcd", &lcd.str);
			OC_LOG_V(INFO, TAG, "LCD string: %s", lcd.str);
			lcd_put();
			OCRepPayloadSetPropString(payload, "lcd", (const char *)lcd.str);
		}

		if (ehRet == OC_EH_OK) {
			// Format the response.  Note this requires some info about the request
			response.requestHandle = entityHandlerRequest->requestHandle;
			response.resourceHandle = entityHandlerRequest->resource;
			response.ehResult = ehRet;
			response.payload = (OCPayload*) payload;
			response.numSendVendorSpecificHeaderOptions = 0;
			memset(response.sendVendorSpecificHeaderOptions, 0, sizeof response.sendVendorSpecificHeaderOptions);
			memset(response.resourceUri, 0, sizeof response.resourceUri);
			// Indicate that response is NOT in a persistent buffer
			response.persistentBufferFlag = 0;

			// Send the response
			if (OCDoResponse(&response) != OC_STACK_OK) {
				OC_LOG(ERROR, TAG, "Error sending response");
				ehRet = OC_EH_ERROR;
			}
		}
	}

	if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG)) {
		if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) {
			OC_LOG (INFO, TAG, ("Received OC_OBSERVE_REGISTER from client"));
			gLightUnderObservation = 1;
		} else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) {
			OC_LOG (INFO, TAG, ("Received OC_OBSERVE_DEREGISTER from client"));
			gLightUnderObservation = 0;
		}
	}
	OCRepPayloadDestroy(payload);
	return ehRet;
}
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
                                               OCRepPayload** payload)
{

    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
        OC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
        OC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
        return ehResult;
    }

    char* tnn;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
    {
        sprintf(g_prov.tnn, "%s", tnn);
    }

    char* cd;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
    {
        sprintf(g_prov.cd, "%s", cd);
    }

    g_flag = 1;

    OCRepPayload *getResp = constructResponse(ehRequest);
    if (!getResp)
    {
        OC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}
OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle,
                                                   OCClientResponse *clientResponse) {
    OIC_LOG_V(DEBUG, TAG, "INSIDE ProvisionEnrolleeResponse");

    // If user stopped the process then return from this function;
    if (IsSetupStopped()) {
        ErrorCallback(DEVICE_NOT_PROVISIONED);
        ClearMemory();
        return OC_STACK_DELETE_TRANSACTION;
    }

    ProvisioningInfo *provInfo;

    if (!ValidateEnrolleResponse(clientResponse)) {
        ErrorCallback(DEVICE_NOT_PROVISIONED);
        return OC_STACK_DELETE_TRANSACTION;
    }

    char *tnn;
    char *cd;

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

    while (input) {

        int64_t ps;
        if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps)) {

            if (ps == 1) {
                input = input->next;
                continue;
            }
            else {
                OIC_LOG_V(DEBUG, TAG, "PS is NOT proper");
                goto Error;

            }
        }

        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn)) {
            if (!strcmp(tnn, netProvInfo->netAddressInfo.WIFI.ssid)) {
                OIC_LOG_V(DEBUG, TAG, "SSID is proper");
                input = input->next;
                continue;
            }
            else {
                OIC_LOG_V(DEBUG, TAG, "SSID is NOT proper");
                goto Error;
            }
        }

        if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd)) {
            if (!strcmp(cd, netProvInfo->netAddressInfo.WIFI.pwd)) {
                OIC_LOG_V(DEBUG, TAG, "Password is proper");
                input = input->next;
                continue;
            }
            else {
                OIC_LOG_V(DEBUG, TAG, "Password is NOT proper");
                goto Error;
            }
        }

        LogProvisioningResponse(input->values);

        input = input->next;

    }

    SuccessCallback(clientResponse);

    return OC_STACK_KEEP_TRANSACTION;

    Error:
    {

        ErrorCallback(DEVICE_NOT_PROVISIONED);

        return OC_STACK_DELETE_TRANSACTION;
    }

}
Ejemplo n.º 5
0
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
{
    OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
        return ehResult;
    }

    int64_t tr;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_TR, &tr))
    {
        // Triggering
        gProvResource.tr = tr;
    }

    //ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
    // A new request for provisioning means overriding existing network provisioning information.
    if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED && tr == ES_PS_TRIGGER_CONNECTION)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
                "Tiggering the network connection");

        if (gNetworkInfoProvEventCb)
        {
            gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
            ehResult = OC_EH_OK;
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
                    "Network handler not registered. Failed to connect to the network");
            ehResult = OC_EH_ERROR;
        }

        return ehResult;
    }
    else if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. "
                "This a request to override the existing the network provisioning information");
    }
    else
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
    }

    OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), "");
    OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), "");

    char* tnn;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn))
    {
        OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), tnn);
        OIC_LOG(INFO, ES_RH_TAG, "got ssid");
    }

    OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn);

    char* cd;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
    {
        OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), cd);
        OIC_LOG(INFO, ES_RH_TAG, "got password");
    }OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);

    gProvResource.ps = ES_PS_PROVISIONING_COMPLETED;

    OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %lld", gProvResource.ps);

    OCRepPayload *getResp = constructResponse(ehRequest);
    if (!getResp)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}
Ejemplo n.º 6
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.º 7
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.º 8
0
bool things_get_value(struct things_representation_s *rep, char *key, char **value)
{
	return OCRepPayloadGetPropString((OCRepPayload *) rep->payload, key, (char **)value);
}