SceneMemberResourceObject class is manager of Scene Member resource.
and it has handler of request from remote side.
This commit is included constants and common functions(SceneCommon.h).
Change-Id: Ic754acf02ed7d4564d2be6709e70090a2928d9c1
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/4917
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+/**
+ * @file
+ *
+ * This file contains the declaration of SceneUtils class and constant variables.
+ */
+
+#ifndef SCENE_COMMONS_H
+#define SCENE_COMMONS_H
+
+#include <string>
+
+namespace OIC
+{
+ namespace Service
+ {
+ const std::string SCENE_LIST_DEFAULT_NAME = "list of scene Collections";
+
+ const std::string SCENE_KEY_LAST_SCENE = "lastScene";
+ const std::string SCENE_KEY_SCENEVALUES = "sceneValues";
+ const std::string SCENE_KEY_NAME = "n";
+ const std::string SCENE_KEY_ID = "id";
+ const std::string SCENE_KEY_RTS = "rts";
+ const std::string SCENE_KEY_RT = "rt";
+ const std::string SCENE_KEY_IF = "if";
+ const std::string SCENE_KEY_PAYLOAD_LINK = "link";
+ const std::string SCENE_KEY_SCENEMAPPINGS = "sceneMappings";
+ const std::string SCENE_KEY_HREF = "href";
+ const std::string SCENE_KEY_SCENE = "scene";
+ const std::string SCENE_KEY_MEMBERPROPERTY = "memberProperty";
+ const std::string SCENE_KEY_MEMBERVALUE = "memberValue";
+ const std::string SCENE_KEY_CREATEDLINK = "createdlink";
+
+ const std::string SCENE_LIST_RT = "oic.wk.scenelist";
+ const std::string SCENE_MEMBER_RT = "oic.r.scenemember";
+ const std::string SCENE_COLLECTION_RT = "oic.wk.scenecollection";
+
+ const std::string COAP_TAG = "coap://";
+ const std::string SCENE_LIST_URI = "/SceneListResURI";
+ const std::string PREFIX_SCENE_COLLECTION_URI = "/a/sceneCollection";
+ const std::string PREFIX_SCENE_MEMBER_URI = "/a/sceneMember";
+
+ const int SCENE_RESPONSE_SUCCESS = 200;
+ const int SCENE_CLIENT_BADREQUEST = 400;
+ const int SCENE_SERVER_INTERNALSERVERERROR = 500;
+
+ class SceneUtils
+ {
+ public:
+ /**
+ * Returns UUID for Scene collection resource and members ID.
+ *
+ * @throw RCSException
+ */
+ static std::string OICGenerateUUIDStr();
+
+ /**
+ * Returns host resource's address and uri from coap address.
+ *
+ * @param address uri of host resource (e.g. coap://192.168.0.2:12345/a/light)
+ * @param[out] host host resource's address (e.g. 192.168.0.2:12345)
+ * @param[out] uri host resource's uri (e.g. /a/light)
+ *
+ * @throw RCSInvalidParameterException
+ */
+ static void getHostUriString(
+ const std::string address, std::string *host, std::string *uri);
+
+ /**
+ * Returns information of my own network address.
+ *
+ * This functionality use the CA interface for getting network information.
+ * But It has design issue. So, It will should change to other interface.
+ *
+ * @throw RCSException
+ */
+ static std::string getNetAddress();
+ };
+ }
+}
+
+#endif // SCENE_COMMONS_H
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "SceneMemberResourceObject.h"
+
+#include <atomic>
+#include "OCPlatform.h"
+
+namespace OIC
+{
+ namespace Service
+ {
+ namespace
+ {
+ std::atomic_int numOfSceneMember(0);
+ }
+
+ SceneMemberResourceObject::Ptr
+ SceneMemberResourceObject::createSceneMemberResource(
+ RCSRemoteResourceObject::Ptr remoteObject)
+ {
+ auto instance = new SceneMemberResourceObject();
+ SceneMemberResourceObject::Ptr newSceneMemberObject;
+ newSceneMemberObject.reset(instance);
+
+ newSceneMemberObject->m_Uri = PREFIX_SCENE_MEMBER_URI + "/" +
+ std::to_string(numOfSceneMember++);
+ newSceneMemberObject->m_SceneMemberResourceObj
+ = RCSResourceObject::Builder(
+ newSceneMemberObject->m_Uri,
+ SCENE_MEMBER_RT, OC_RSRVD_INTERFACE_DEFAULT).
+ setDiscoverable(true).setObservable(false).build();
+ newSceneMemberObject->m_RequestHandler.m_Owner
+ = std::weak_ptr<SceneMemberResourceObject>(newSceneMemberObject);
+
+ newSceneMemberObject->m_RemoteMemberObj = remoteObject;
+
+ auto resourceObj = newSceneMemberObject->m_SceneMemberResourceObj;
+
+ RCSResourceObject::LockGuard guard(resourceObj);
+ resourceObj->setAttribute(SCENE_KEY_ID, SceneUtils::OICGenerateUUIDStr());
+ resourceObj->setAttribute(SCENE_KEY_NAME, std::string());
+
+ RCSResourceAttributes subAtt;
+ subAtt[SCENE_KEY_HREF]
+ = RCSResourceAttributes::Value(
+ remoteObject->getAddress() + remoteObject->getUri());
+ subAtt[SCENE_KEY_IF] = RCSResourceAttributes::Value(remoteObject->getInterfaces());
+ subAtt[SCENE_KEY_RT] = RCSResourceAttributes::Value(remoteObject->getTypes());
+ resourceObj->setAttribute(SCENE_KEY_PAYLOAD_LINK, subAtt);
+
+ resourceObj->setAttribute(
+ SCENE_KEY_SCENEMAPPINGS, std::vector<RCSResourceAttributes>());
+
+ resourceObj->setSetRequestHandler(std::bind(
+ &SceneMemberResourceObject::SceneMemberRequestHandler::onSetRequest,
+ newSceneMemberObject->m_RequestHandler,
+ std::placeholders::_1, std::placeholders::_2));
+
+ return newSceneMemberObject;
+ }
+
+ SceneMemberResourceObject::Ptr
+ SceneMemberResourceObject::createSceneMemberResource(const RCSResourceAttributes & link)
+ {
+ return createSceneMemberResource(RCSResourceAttributes(link));
+ }
+
+ SceneMemberResourceObject::Ptr
+ SceneMemberResourceObject::createSceneMemberResource(RCSResourceAttributes && link)
+ {
+ auto href = link.at(SCENE_KEY_HREF).get<std::string>();
+
+ std::string address;
+ std::string uri;
+
+ SceneUtils::getHostUriString(href, &address, &uri);
+
+ auto ocResourcePtr
+ = OC::OCPlatform::constructResourceObject(
+ address, uri, OCConnectivityType::CT_ADAPTER_IP, false,
+ link.at(SCENE_KEY_RT).get<std::vector<std::string>>(),
+ link.at(SCENE_KEY_IF).get<std::vector<std::string>>());
+
+ return createSceneMemberResource(RCSRemoteResourceObject::fromOCResource(ocResourcePtr));
+ }
+
+ void SceneMemberResourceObject::addMappingInfo(MappingInfo && mInfo)
+ {
+ RCSResourceAttributes newAtt;
+ {
+ RCSResourceObject::LockGuard guard(m_SceneMemberResourceObj);
+ newAtt = m_SceneMemberResourceObj->getAttributes();
+ }
+
+ auto mappingInfo = newAtt.at(SCENE_KEY_SCENEMAPPINGS).
+ get<std::vector<RCSResourceAttributes>>();
+
+ struct FindSceneName
+ {
+ bool operator()(RCSResourceAttributes att) const
+ {
+ return att.at(SCENE_KEY_SCENE).get<std::string>() == name;
+ }
+ std::string name;
+ };
+
+ FindSceneName fScene;
+ fScene.name = mInfo.sceneName;
+ auto foundMInfo = std::find_if(mappingInfo.begin(), mappingInfo.end(), fScene);
+
+ if (foundMInfo != mappingInfo.end() &&
+ (* foundMInfo).at(SCENE_KEY_MEMBERPROPERTY).get<std::string>() == mInfo.key)
+ {
+ mappingInfo.erase(foundMInfo);
+ }
+ RCSResourceAttributes newMapInfo;
+ newMapInfo[SCENE_KEY_SCENE] = RCSResourceAttributes::Value(mInfo.sceneName);
+ newMapInfo[SCENE_KEY_MEMBERPROPERTY] = RCSResourceAttributes::Value(mInfo.key);
+ newMapInfo[SCENE_KEY_MEMBERVALUE] = mInfo.value;
+ mappingInfo.push_back(newMapInfo);
+
+ RCSResourceObject::LockGuard guard(m_SceneMemberResourceObj);
+ m_SceneMemberResourceObj->setAttribute(SCENE_KEY_SCENEMAPPINGS, mappingInfo);
+ }
+
+ void SceneMemberResourceObject::addMappingInfo(const MappingInfo & mInfo)
+ {
+ addMappingInfo(MappingInfo(mInfo));
+ }
+
+ std::vector<SceneMemberResourceObject::MappingInfo>
+ SceneMemberResourceObject::getMappingInfo()
+ {
+ auto mappingInfo
+ = m_SceneMemberResourceObj->getAttributeValue(SCENE_KEY_SCENEMAPPINGS).
+ get<std::vector<RCSResourceAttributes>>();
+
+ std::vector<MappingInfo> retMInfo;
+ for(unsigned int it = 0; it < mappingInfo.size(); ++it)
+ {
+ MappingInfo info(mappingInfo[it].at(
+ SCENE_KEY_SCENE).get<std::string>(),
+ mappingInfo[it].at(
+ SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
+ mappingInfo[it].at(
+ SCENE_KEY_MEMBERVALUE));
+ retMInfo.push_back(info);
+ }
+
+ return retMInfo;
+ }
+
+ std::string SceneMemberResourceObject::getId() const
+ {
+ return m_SceneMemberResourceObj->getAttributeValue(SCENE_KEY_ID).get<std::string>();
+ }
+
+ std::string SceneMemberResourceObject::getFullUri() const
+ {
+ return std::string(COAP_TAG + SceneUtils::getNetAddress() + m_Uri);
+ }
+
+ RCSRemoteResourceObject::Ptr SceneMemberResourceObject::getRemoteResourceObject() const
+ {
+ return m_RemoteMemberObj;
+ }
+
+ RCSResourceObject::Ptr SceneMemberResourceObject::getRCSResourceObject() const
+ {
+ return m_SceneMemberResourceObj;
+ }
+
+ void SceneMemberResourceObject::execute(std::string && sceneName)
+ {
+ execute(std::move(sceneName), nullptr);
+ }
+
+ void SceneMemberResourceObject::execute(const std::string & sceneName)
+ {
+ execute(std::string(sceneName));
+ }
+
+ void SceneMemberResourceObject::execute(
+ std::string && sceneName, executeCallback executeCB)
+ {
+ bool hasScene = false;
+ auto mInfo = getMappingInfo();
+
+ for (unsigned int it = 0; it < mInfo.size(); ++it)
+ {
+ if (mInfo[it].sceneName == sceneName)
+ {
+ hasScene = true;
+
+ RCSResourceAttributes setAtt;
+ setAtt[mInfo[it].key] = mInfo[it].value;
+ m_RemoteMemberObj->setRemoteAttributes(setAtt, executeCB);
+ }
+ }
+ if (!hasScene && executeCB != nullptr)
+ {
+ executeCB(RCSResourceAttributes(), SCENE_RESPONSE_SUCCESS);
+ }
+ }
+
+ void SceneMemberResourceObject::execute(
+ const std::string & sceneName, executeCallback executeCB)
+ {
+ execute(std::string(sceneName), std::move(executeCB));
+ }
+
+ RCSSetResponse SceneMemberResourceObject::SceneMemberRequestHandler::
+ onSetRequest(const RCSRequest & /*request*/, RCSResourceAttributes & attributes)
+ {
+ auto ptr = m_Owner.lock();
+ if (!ptr)
+ {
+ return RCSSetResponse::create(
+ RCSResourceAttributes(attributes), SCENE_CLIENT_BADREQUEST).
+ setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+ }
+
+ if (attributes.contains(SCENE_KEY_SCENEMAPPINGS))
+ {
+ auto sceneMappings
+ = attributes.at(SCENE_KEY_SCENEMAPPINGS).get<std::vector<RCSResourceAttributes>>();
+ for (unsigned int it = 0; it < sceneMappings.size(); ++it)
+ {
+ ptr->addMappingInfo(SceneMemberResourceObject::MappingInfo(
+ sceneMappings[it].at(SCENE_KEY_SCENE).get<std::string>(),
+ sceneMappings[it].at(SCENE_KEY_MEMBERPROPERTY).get<std::string>(),
+ sceneMappings[it].at(SCENE_KEY_MEMBERVALUE)));
+ }
+ }
+
+ return RCSSetResponse::create(RCSResourceAttributes(attributes)).
+ setAcceptanceMethod(RCSSetResponse::AcceptanceMethod::IGNORE);
+ }
+ }
+}
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+/**
+ * @file
+ *
+ * This file contains the declaration of classes and its members related to SceneMemberResrouceObject
+ */
+
+#ifndef SCENE_MEMBER_RESOURCE_OBJECT_H
+#define SCENE_MEMBER_RESOURCE_OBJECT_H
+
+#include "RCSResourceObject.h"
+#include "RCSRemoteResourceObject.h"
+#include "SceneCommons.h"
+
+namespace OIC
+{
+ namespace Service
+ {
+ class SceneMemberResourceObject
+ : public std::enable_shared_from_this<SceneMemberResourceObject>
+ {
+ public:
+ typedef std::shared_ptr< SceneMemberResourceObject > Ptr;
+
+ /**
+ * Callback definition to be invoked when the response of setRemoteAttribitues is received.
+ *
+ * @param attrs the result attributes
+ * @param eCode the error code received from the resource
+ *
+ * @see RCSRemoteResourceObject::setRemoteAttributes
+ */
+ typedef std::function< void(const RCSResourceAttributes & attrs, int eCode) >
+ executeCallback;
+
+ /**
+ * A Mapping information about each scene values.
+ */
+ struct MappingInfo
+ {
+ MappingInfo(
+ std::string scene,
+ std::string keyName,
+ RCSResourceAttributes::Value val)
+ :sceneName(scene), key(keyName), value(val) { }
+ MappingInfo(MappingInfo &&) = default;
+ MappingInfo(const MappingInfo &) = default;
+ std::string sceneName; ///< name of scene value
+ std::string key; ///< key to set at attributes of remote resource
+ RCSResourceAttributes::Value value; ///< val to set at attributes of remote resource
+ };
+
+ ~SceneMemberResourceObject() = default;
+
+ /**
+ * Register a Scene member resource and return a SceneMemberResourceObject
+ * using link information of remote resource.
+ *
+ * @param attrs information to make scene member resource
+ */
+ static SceneMemberResourceObject::Ptr
+ createSceneMemberResource(RCSResourceAttributes && attrs);
+
+ /**
+ * @overload
+ */
+ static SceneMemberResourceObject::Ptr
+ createSceneMemberResource(const RCSResourceAttributes &);
+
+ /**
+ * Register a Scene member resource and returns a SceneMemberResourceObject
+ * using information of RCSRemoteResourceObject.
+ *
+ * @param remoteObj information to make scene member resource
+ */
+ static SceneMemberResourceObject::Ptr
+ createSceneMemberResource(RCSRemoteResourceObject::Ptr remoteObj);
+
+ /**
+ * Add Scene mapping information at scene member resource.
+ *
+ * @param mappingInfo
+ */
+ void addMappingInfo(MappingInfo && mappingInfo);
+
+ /**
+ * @overload
+ */
+ void addMappingInfo(const MappingInfo &);
+
+ /**
+ * Returns all Mapping information of a scene member resource.
+ */
+ std::vector<MappingInfo> getMappingInfo();
+
+ /**
+ * Returns ID of a Scene member resource.
+ */
+ std::string getId() const;
+
+ /**
+ * Returns Uri of a Scene member resource. (e.g. coap://192.168.0.2.1:12345/SceneMember)
+ */
+ std::string getFullUri() const;
+
+ /**
+ * Returns RCSRemoteResourceObject about Scene member resource
+ */
+ RCSRemoteResourceObject::Ptr getRemoteResourceObject() const;
+
+ /**
+ * Returns RCSResourceObject of Scene member resource
+ */
+ RCSResourceObject::Ptr getRCSResourceObject() const;
+
+ /**
+ * Execute of Scene Action (with callback for response).
+ *
+ * @param sceneValue scene value to execute
+ * @param cb callback to response
+ */
+ void execute(std::string && sceneValue, executeCallback cb);
+
+ /**
+ * @overload
+ */
+ void execute(const std::string &, executeCallback);
+
+ /**
+ * Execute of Scene Action (without callback for response).
+ *
+ * @param sceneValue scene value to execute
+ */
+ void execute(std::string && sceneValue);
+
+ /**
+ * @overload
+ */
+ void execute(const std::string &);
+
+ private:
+ class SceneMemberRequestHandler
+ {
+ public:
+ SceneMemberRequestHandler() = default;
+ ~SceneMemberRequestHandler() = default;
+
+ std::weak_ptr<SceneMemberResourceObject> m_Owner;
+
+ RCSSetResponse onSetRequest(const RCSRequest & , RCSResourceAttributes &);
+ };
+
+ std::string m_Uri;
+ RCSResourceObject::Ptr m_SceneMemberResourceObj;
+ RCSRemoteResourceObject::Ptr m_RemoteMemberObj;
+ SceneMemberRequestHandler m_RequestHandler;
+
+ SceneMemberResourceObject() = default;
+
+ SceneMemberResourceObject(const SceneMemberResourceObject &) = delete;
+ SceneMemberResourceObject & operator = (const SceneMemberResourceObject &) = delete;
+
+ SceneMemberResourceObject(SceneMemberResourceObject &&) = delete;
+ SceneMemberResourceObject && operator = (SceneMemberResourceObject &&) = delete;
+ };
+ }
+}
+
+#endif // SCENE_MEMBER_RESOURCE_OBJECT_H
--- /dev/null
+//******************************************************************
+//
+// Copyright 2016 Samsung Electronics All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+#include "SceneCommons.h"
+
+#include <string>
+#include "ocrandom.h"
+#include "oic_malloc.h"
+#include "RCSException.h"
+#include "cainterface.h"
+
+namespace OIC
+{
+ namespace Service
+ {
+ std::string SceneUtils::OICGenerateUUIDStr()
+ {
+ uint8_t uuid[UUID_SIZE] = { 0, };
+ char uuidStr[UUID_STRING_SIZE] = { 0, };
+ if (RAND_UUID_OK == OCGenerateUuid(uuid))
+ {
+ if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
+ {
+ return std::string(uuidStr);
+ }
+ }
+
+ throw RCSException("Failed to generate UUID");
+ }
+
+ void SceneUtils::getHostUriString(
+ const std::string address, std::string *host, std::string *uri)
+ {
+ unsigned int nextStartIndex = 0;
+ int indexOfStr = 3;
+
+ if (address.find(COAP_TAG) == std::string::npos)
+ {
+ indexOfStr = 1;
+ }
+
+ for (int i = 0; i < indexOfStr; i++)
+ {
+ nextStartIndex
+ = address.find_first_of("/", nextStartIndex);
+ if (nextStartIndex == std::string::npos)
+ {
+ throw RCSInvalidParameterException("address is invalid");
+ }
+ nextStartIndex += 1;
+ }
+
+ *host = address.substr(0, nextStartIndex - 1);
+ *uri = address.substr(nextStartIndex - 1, std::string::npos);
+ }
+
+ std::string SceneUtils::getNetAddress()
+ {
+ CAEndpoint_t ** netInfo = (CAEndpoint_t **)OICMalloc(sizeof(CAEndpoint_t*)*5);
+
+ uint32_t size = 0;
+ CAGetNetworkInformation(netInfo, &size);
+
+ if (size == 0)
+ {
+ delete[] netInfo;
+ throw RCSException("Disabled Network");
+ }
+
+ for (uint32_t i = 0; i < size; ++i)
+ {
+ if (netInfo[i]->adapter == CATransportAdapter_t::CA_ADAPTER_IP)
+ {
+ std::string retAddress
+ = std::string(netInfo[i]->addr) + ":" + std::to_string(netInfo[i]->port);
+
+ delete[] netInfo;
+ return retAddress;
+ }
+ }
+
+ delete[] netInfo;
+ throw RCSException("Not supported Network");
+ }
+ }
+}