Esempio n. 1
0
    // gets the updated representation.
    // Updates the representation with latest internal state before
    // sending out.
    OCRepresentation get()
    {
        m_lightRep.setValue("state", m_state);
        m_lightRep.setValue("power", m_power);

        return m_lightRep;
    }
Esempio n. 2
0
    /// Constructor
    LightResource()
        :m_power(""), m_lightUri("/a/light") {
        // Initialize representation
        m_lightRep.setUri(m_lightUri);

        m_lightRep.setValue("power", m_power);
    }
Esempio n. 3
0
    OCRepresentation getLightRepresentation()
    {
        m_lightRep.setValue("state", m_lightState);
        m_lightRep.setValue("color", m_lightColor);

        return m_lightRep;
    }
Esempio n. 4
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueRepresentation
* Signature: (Ljava/lang/String;Lorg/iotivity/base/OcRepresentation;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation
(JNIEnv *env, jobject thiz, jstring jKey, jobject jValue)
{
    LOGD("OcRepresentation_setValueRepresentation");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);

    if (jValue)
    {
        OCRepresentation *value = JniOcRepresentation::getOCRepresentationPtr(env, jValue);
        if (!value)
        {
            return;
        }
        rep->setValue(key, *value);
    }
    else
    {
        rep->setNULL(key);
    }
}
Esempio n. 5
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueBooleanArray
* Signature: (Ljava/lang/String;[Z)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBooleanArray
(JNIEnv *env, jobject thiz, jstring jKey, jbooleanArray jValue)
{
    LOGD("OcRepresentation_setValueBooleanArray");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }

    const jsize len = env->GetArrayLength(jValue);
    jboolean* booleans = env->GetBooleanArrayElements(jValue, nullptr);

    std::vector<bool> value;
    for (jsize i = 0; i < len; ++i)
    {
        value.push_back(static_cast<bool>(booleans[i]));
    }
    env->ReleaseBooleanArrayElements(jValue, booleans, JNI_ABORT);

    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(key, value);
}
Esempio n. 6
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueDouble2DArray
* Signature: (Ljava/lang/String;[[D)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble2DArray
(JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
{
    LOGD("OcRepresentation_setValueDouble2DArray");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    std::vector<std::vector<double>> value;
    const jsize lenOuter = env->GetArrayLength(jValue);
    for (jsize j = 0; j < lenOuter; ++j)
    {
        jdoubleArray jInnerArray = static_cast<jdoubleArray>(env->GetObjectArrayElement(jValue, j));
        jdouble* doubles = env->GetDoubleArrayElements(jInnerArray, nullptr);
        std::vector<double> innerVector;
        const jsize lenInner = env->GetArrayLength(jInnerArray);
        for (jsize i = 0; i < lenInner; ++i)
        {
            innerVector.push_back(static_cast<double>(doubles[i]));
        }
        env->ReleaseDoubleArrayElements(jInnerArray, doubles, JNI_ABORT);
        env->DeleteLocalRef(jInnerArray);
        value.push_back(innerVector);
    }

    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(key, value);
}
Esempio n. 7
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueRepresentation2DArray
* Signature: (Ljava/lang/String;[[Lorg/iotivity/base/OcRepresentation;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentation2DArray
(JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
{
    LOGD("OcRepresentation_setValueRepresentation2DArray");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    std::vector<std::vector<OCRepresentation>> value;
    const jsize lenOuter = env->GetArrayLength(jValue);
    for (jsize j = 0; j < lenOuter; ++j)
    {
        jobjectArray jInnerArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, j));
        std::vector<OCRepresentation> innerVector;
        JniUtils::convertJavaRepresentationArrToVector(env, jInnerArray, innerVector);
        env->DeleteLocalRef(jInnerArray);
        value.push_back(innerVector);
    }

    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep)
    {
        return;
    }

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(key, value);
}
Esempio n. 8
0
void SensorResource::executeJumpingRule(int heartRate)
{
	std::cout << "Crazy Jumping enable flag: " << m_rr->m_crazyJumping << "current heartRate: " << heartRate << std::endl;
	if (m_rr->m_crazyJumping && m_led.s_active && m_led.s_resource) {
		OCRepresentation ledrep;
		PutCallback p (std::bind(&SensorResource::onPut, this, PH::_1, PH::_2, PH::_3));
		if (heartRate >= m_rr->m_heartRate){
			ledrep.setValue("ledColor", RED);
		} else {
			ledrep.setValue("ledColor", GREEN);

		}

		m_led.s_resource->put(ledrep, QueryParamsMap(), p);
	}
}
Esempio n. 9
0
OCStackResult OCAccountManager::refreshAccessToken(const std::string& userUuid,
                                                   const std::string& refreshToken,
                                                   PostCallback cloudConnectHandler)
{
    std::string uri = m_host + OC_RSRVD_ACCOUNT_TOKEN_REFRESH_URI;

    OCRepresentation rep;
    rep.setValue(OC_RSRVD_USER_UUID, userUuid);
    rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
    rep.setValue(OC_RSRVD_GRANT_TYPE, OC_RSRVD_GRANT_TYPE_REFRESH_TOKEN);
    rep.setValue(OC_RSRVD_REFRESH_TOKEN, refreshToken);

    return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
                         OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
                         m_connType, cloudConnectHandler, m_defaultQos);
}
WANIpConnection::ConnectionState WANIpConnection::forceTermination()
{
    unique_lock<mutex> wanIpLock(m_mutex);
    m_connectionState = {"", "", 0};
    OCRepresentation rep;
    rep.setValue("uri", m_resource->uri());
    OCRepresentation attributes;
    attributes.setValue("statusUpdateRequest", string("ForceTermination"));
    rep.setValue("connectionState", attributes);
    m_forceTerminationCB = bind(&WANIpConnection::onForceTermination, this, _1, _2, _3);
    m_resource->post(rep, QueryParamsMap(), m_forceTerminationCB);
    if (m_cv.wait_for(wanIpLock, chrono::seconds(MAX_WAIT_TIME_FOR_BLOCKING_CALL)) == cv_status::timeout)
    {
        cerr << "Remote device failed to respond to the request." << endl;
    }
    return m_connectionState;
}
Esempio n. 11
0
    /// Constructor
    FanResource() :
        m_speed(10), m_fanUri("/a/fan"), m_resourceHandle(0)
    {
        // Initialize representation
        m_fanRep.setUri(m_fanUri);

        m_fanRep.setValue("speed", m_speed);
    }
Esempio n. 12
0
    OCRepresentation getRepresentation()
    {
        OCRepresentation rep;

        rep.setValue("level", (int) m_pressure);

        return rep;
    }
Esempio n. 13
0
void putClientRepresentation(std::shared_ptr<OCResource> resource, string &key, int value)
{
	if(resource)
	{
		OCRepresentation rep;
		std::cout << "Putting representation..."<<std::endl;
		rep.setValue(key, value);
		resource->put(rep, QueryParamsMap(), &onPut);
	}
}
Esempio n. 14
0
void SensorResource::executeGasRule(int density)
{
	if (m_fan.s_active && m_fan.s_resource) {
		OCRepresentation fanrep;
		PutCallback p (std::bind(&SensorResource::onPut, this, PH::_1, PH::_2, PH::_3));
		if ((density > m_rr->m_density) && (m_fanState == false)) {
			std::cout << "Turning on the fan..."<<std::endl;

			fanrep.setValue("fanstate", std::string("on"));
			m_fan.s_resource->put(fanrep, QueryParamsMap(), p);
		}
		else if ((density <= m_rr->m_density) && (m_fanState == true)) {
			std::cout << "Turning off the fan..."<<std::endl;

			fanrep.setValue("fanstate", std::string("off"));
			m_fan.s_resource->put(fanrep, QueryParamsMap(), p);
		}
	}
}
        void RemoteEnrolleeResource::unprovisionEnrollee()
        {
            if (m_ocResource == nullptr)
            {
                throw ESBadRequestException("Resource is not initialized");
            }

            OCRepresentation provisioningRepresentation;

            provisioningRepresentation.setValue(OC_RSRVD_ES_TNN, "");
            provisioningRepresentation.setValue(OC_RSRVD_ES_CD, "");

            m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
                    std::function<
                            void(const HeaderOptions& headerOptions, const OCRepresentation& rep,
                                    const int eCode) >(
                    std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this,
                    std::placeholders::_1, std::placeholders::_2,
                    std::placeholders::_3)));
        }
Esempio n. 16
0
OCStackResult OCAccountManager::signInOut(const std::string& userUuid,
                                          const std::string& accessToken,
                                          bool isSignIn,
                                          PostCallback cloudConnectHandler)
{
    std::string uri = m_host + OC_RSRVD_ACCOUNT_SESSION_URI;

    OCRepresentation rep;
    if (isSignIn)
    {
        rep.setValue(OC_RSRVD_USER_UUID, userUuid);
        rep.setValue(OC_RSRVD_DEVICE_ID, m_deviceID);
        rep.setValue(OC_RSRVD_ACCESS_TOKEN, accessToken);
    }
    rep.setValue(OC_RSRVD_LOGIN, isSignIn);

    return checked_guard(m_clientWrapper.lock(), &IClientWrapper::PostResourceRepresentation,
                         OCDevAddr(), uri, rep, QueryParamsMap(), HeaderOptions(),
                         m_connType, cloudConnectHandler, m_defaultQos);
}
Esempio n. 17
0
void allBulbOff()
{
    OCRepresentation rep;

    rep.setValue("DoAction", std::string("AllBulbOff"));

    if (g_resource)
    {
        g_resource->post("a.collection", GROUP_INTERFACE, rep, QueryParamsMap(),
                &onPost);
    }
}
Esempio n. 18
0
void SensorResource::onObserve(const HeaderOptions headerOptions, const OCRepresentation& rep,
	const int& eCode, const int& sequenceNumber)
{
	int density = 0;
	int ledColor = 0;
	bool motion = false;
	try
	{
		if(eCode == OC_STACK_OK) {
			if (rep.hasAttribute("density")) {
				m_gas.s_active = true;
				rep.getValue("density", density);
				std::cout << "\tdensity: " << density << std::endl;

				if (m_rr->m_kitchenMonitor) {
					std::cout << "Kitchen Monitor is enabled" << std::endl;
					executeGasRule(density);
				}
			}

			if (rep.hasAttribute("fanstate")) {
				m_fan.s_active = true;
				std::string state = rep.getValue<std::string>("fanstate");
				m_fanState = (state == "on" ? true:false);
				std::cout << "\tfanstate: " << m_fanState << std::endl;
			}

			if (rep.hasAttribute("ledColor")) {
				m_led.s_active = true;
				rep.getValue("ledColor", ledColor);
				std::cout << "\tledColor: " << ledColor << std::endl;
			}
			if (rep.hasAttribute("motion")) {
				m_pri.s_active = true;
				rep.getValue("motion", motion);
				std::cout << "\tmotion: " << motion << std::endl;
				PutCallback p (std::bind(&SensorResource::onPut, this, PH::_1, PH::_2, PH::_3));
				if ((motion == true) && (m_led.s_active == true)) {
					OCRepresentation ledrep;
					ledrep.setValue("ledColor", BLUE);
					m_led.s_resource->put(ledrep, QueryParamsMap(), p);
				}
			}
		}
		else {
			std::cout << "onObserve Response error: " << eCode << std::endl;
		}
	}
	catch(std::exception& e) {
		std::cout << "Exception: " << e.what() << " in onObserve" << std::endl;
	}
}
Esempio n. 19
0
    /// Constructor
    RoomResource(): m_roomName("John's Room"), m_roomHandle(nullptr), m_lightState(false),
                    m_lightColor(0),m_lightHandle(nullptr),  m_fanState(false), m_fanSpeed(0),
                    m_fanHandle(nullptr)
    {
        m_roomUri = "/a/room"; // URI of the resource
        m_roomTypes.push_back("core.room"); // resource type name. In this case, it is light
        m_roomInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.
        m_roomInterfaces.push_back(BATCH_INTERFACE); // resource interface.
        m_roomInterfaces.push_back(LINK_INTERFACE); // resource interface.
        m_roomRep.setValue("name", m_roomName);
        m_roomRep.setUri(m_roomUri);
        m_roomRep.setResourceTypes(m_roomTypes);
        m_roomRep.setResourceInterfaces(m_roomInterfaces);


        m_lightUri = "/a/light"; // URI of the resource
        m_lightTypes.push_back("core.light"); // resource type name. In this case, it is light
        m_lightInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.

        m_lightRep.setUri(m_lightUri);
        m_lightRep.setResourceTypes(m_lightTypes);
        m_lightRep.setResourceInterfaces(m_lightInterfaces);
        m_lightRep.setValue("state", m_lightState);
        m_lightRep.setValue("color", m_lightColor);

        m_fanUri = "/a/fan"; // URI of the resource
        m_fanTypes.push_back("core.fan"); // resource type name. In this case, it is light
        m_fanInterfaces.push_back(DEFAULT_INTERFACE); // resource interface.

        m_fanRep.setUri(m_fanUri);
        m_fanRep.setResourceTypes(m_fanTypes);
        m_fanRep.setResourceInterfaces(m_fanInterfaces);
        m_fanRep.setValue("state", m_fanState);
        m_fanRep.setValue("speed", m_fanSpeed);

    }
Esempio n. 20
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueDouble
* Signature: (Ljava/lang/String;D)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueDouble
(JNIEnv *env, jobject thiz, jstring jKey, jdouble jValue)
{
    LOGD("OcRepresentation_setValueDouble");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep) return;

    std::string str = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(str, static_cast<double>(jValue));
}
Esempio n. 21
0
void allBulbOn()
{
    OCRepresentation rep;

    rep.setValue("DoAction", std::string("AllBulbOn"));

    if (g_resource)
    {
        OCStackResult res = g_resource->post("a.collection", GROUP_INTERFACE,
            rep, QueryParamsMap(), &onPost);

        if( res != OC_STACK_OK )
            cout << "failed" << endl;
    }
}
Esempio n. 22
0
OCStackResult GroupManager::cancelActionSet(std::shared_ptr< OCResource > resource,
        std::string actionsetName, PostCallback cb)
{
    if (resource != NULL)
    {
        OCRepresentation rep;

        rep.setValue("CancelAction", actionsetName);
        return resource->post(resource->getResourceTypes().front(), GROUP_INTERFACE, rep,
                QueryParamsMap(), cb);
    }
    else
    {
        return OC_STACK_ERROR;
    }
}
Esempio n. 23
0
// Local function to put a different state for this resource
void postLightRepresentation(std::shared_ptr<OCResource> resource)
{
    if (resource)
    {
        OCRepresentation rep;

        std::cout << "Posting light representation..." << std::endl;

        mylight.m_on_off = false;

        rep.setValue("on-off", mylight.m_on_off);

        // Invoke resource's post API with rep, query map and the callback parameter
        resource->post(rep, QueryParamsMap(), &onPost);
    }
}
Esempio n. 24
0
// Local function to put a different state for this resource
void putLightRepresentation(std::shared_ptr<OCResource> resource)
{
    if(resource)
    {
        OCRepresentation rep;

        std::cout << "Putting light representation..."<<std::endl;

        mylight.m_state = true;
        mylight.m_power = 15;

        rep.setValue("state", mylight.m_state);
        rep.setValue("power", mylight.m_power);

        // Invoke resource's put API with rep, query map and the callback parameter
        resource->put(rep, QueryParamsMap(), &onPut, OC::QualityOfService::HighQos);
    }
}
Esempio n. 25
0
// Local function to put a different state for this resource
void putLightRepresentation(std::shared_ptr<OCResource> resource)
{
    if (resource)
    {
        OCRepresentation rep;

        std::cout << "Putting light representation..." << std::endl;

        mylight.m_on_off = true;

        std::cout << "Sending request to: " << resource->uri() << std::endl;
        rep.setValue("on-off", mylight.m_on_off);

        // Invoke resource's put API with rep, query map and the callback parameter

        resource->put(rep, QueryParamsMap(), &onPut);
    }
}
Esempio n. 26
0
// Local function to put a different state for this resource
void postFanRepresentation(std::shared_ptr<OCResource> resource)
{
    if (resource)
    {
        OCRepresentation rep;

        std::cout << "Posting fan representation..." << std::endl;

        myfan.m_state = false;
        myfan.m_power = 105;

        rep.setValue("state", myfan.m_state);
        rep.setValue("power", myfan.m_power);

        // Invoke resource's post API with rep, query map and the callback parameter
        resource->post(rep, QueryParamsMap(), &onPost);
    }
}
void putLightRepresentation2(std::shared_ptr<OCResource> resource)
{
    if(resource)
    {
        OCRepresentation rep;

        std::cout << "Sending command to turn on the light..."<<std::endl;

        mylight.m_state = true;
        mylight.m_power = 66;

        rep.setValue("state", mylight.m_state);
        rep.setValue("power", mylight.m_power);

        // Invoke resource's put API with rep, query map and the callback parameter
        resource->put(rep, QueryParamsMap(), &onPut2);
    }
}
Esempio n. 28
0
// Local function to put a different state for this resource
void putLightRepresentation(std::shared_ptr<OCResource> resource)
{
    if(resource)
    {
        OCRepresentation rep;

        std::cout << "Putting light representation..."<<std::endl;

        myGarage.m_state = true;

        rep.setValue("state", myGarage.m_state);

        // Create QueryParameters Map and add query params (if any)
        QueryParamsMap queryParamsMap;

        // Invoke resource's pit API with rep, query map and the callback parameter
        resource->put(rep, queryParamsMap, &onPut);
    }
}
Esempio n. 29
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueRepresentationArray
* Signature: (Ljava/lang/String;[Lorg/iotivity/base/OcRepresentation;)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueRepresentationArray
(JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
{
    LOGD("OcRepresentation_setValueRepresentationArray");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }

    std::vector<OCRepresentation> value;
    JniUtils::convertJavaRepresentationArrToVector(env, jValue, value);

    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep) return;

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(key, value);
}
Esempio n. 30
0
/*
* Class:     org_iotivity_base_OcRepresentation
* Method:    setValueBoolean3DArray
* Signature: (Ljava/lang/String;[[[Z)V
*/
JNIEXPORT void JNICALL Java_org_iotivity_base_OcRepresentation_setValueBoolean3DArray
(JNIEnv *env, jobject thiz, jstring jKey, jobjectArray jValue)
{
    LOGD("OcRepresentation_setValueBoolean3DArray");
    if (!jKey)
    {
        ThrowOcException(OC_STACK_INVALID_PARAM, "key cannot be null");
        return;
    }
    std::vector<std::vector<std::vector<bool>>> value;
    const jsize lenOuter = env->GetArrayLength(jValue);
    for (jsize k = 0; k < lenOuter; ++k)
    {
        jobjectArray jMiddleArray = static_cast<jobjectArray>(env->GetObjectArrayElement(jValue, k));
        const jsize lenMiddle = env->GetArrayLength(jMiddleArray);
        std::vector<std::vector<bool>> middleArray;
        for (jsize j = 0; j < lenMiddle; ++j)
        {
            jbooleanArray jInnerArray = static_cast<jbooleanArray>(env->GetObjectArrayElement(jMiddleArray, j));
            const jsize lenInner = env->GetArrayLength(jInnerArray);
            jboolean* booleans = env->GetBooleanArrayElements(jInnerArray, nullptr);

            std::vector<bool> innerVector;
            for (jsize i = 0; i < lenInner; ++i)
            {
                innerVector.push_back(static_cast<bool>(booleans[i]));
            }
            env->ReleaseBooleanArrayElements(jInnerArray, booleans, JNI_ABORT);
            env->DeleteLocalRef(jInnerArray);
            middleArray.push_back(innerVector);
        }
        env->DeleteLocalRef(jMiddleArray);
        value.push_back(middleArray);
    }

    OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, thiz);
    if (!rep) return;

    std::string key = env->GetStringUTFChars(jKey, nullptr);
    rep->setValue(key, value);
}