From: Harish Kumara M Date: Thu, 16 Aug 2018 11:19:44 +0000 (+0530) Subject: New APIs for editing resource and interfaces types X-Git-Tag: accepted/tizen/4.0/unified/20181213.000255~19 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8fb99e47050de7137a7c477ee55b67a65dd9fddc;p=platform%2Fupstream%2Fiotivity.git New APIs for editing resource and interfaces types New APIs are introduced for modifying resource and interface types bound to a resource. These new APIs clear currently bound types and set newly passing resource/interface type. https://github.sec.samsung.net/RS7-IOTIVITY/IoTivity/pull/313 (cherry picked from commit 2ffab6058c93ff52d64bbbe6e8d29855916d3bcc) Change-Id: I1e51823f1d46fdc042694f9213bde026974b9ae3 Signed-off-by: Harish Kumara M Signed-off-by: Amit KS --- diff --git a/resource/csdk/stack/include/ocstack.h b/resource/csdk/stack/include/ocstack.h index 3ee7ec3..897d770 100644 --- a/resource/csdk/stack/include/ocstack.h +++ b/resource/csdk/stack/include/ocstack.h @@ -379,6 +379,19 @@ OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHand */ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle, const char *resourceTypeName); + +/** + * This function clears all bound resource types and bind newly passing + * resource type to resource. + * + * @param handle Handle to the resource. + * @param newResourceType Name of resource type. Example: "core.led". + * + * @return ::OC_STACK_OK on success, some other value upon failure. + */ +OCStackResult OCResetResourceTypes(OCResourceHandle handle, + const char *newResourceType); + /** * This function binds a resource interface to a resource. * @@ -391,6 +404,18 @@ OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle, const char *resourceInterfaceName); /** + * This function clears all bound interfaces and bind newly passing + * interface to resource. + * + * @param handle Handle to the resource. + * @param newResourceInterface Name of resource interface. Example: "core.rw". + * + * @return ::OC_STACK_OK on success, some other value upon failure. + */ +OCStackResult OCResetResourceInterfaces(OCResourceHandle handle, + const char *newResourceInterface); + +/** * This function binds an entity handler to the resource. * * @param handle Handle to the resource that the contained resource is to be bound. diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index 139ff3a..b6e30e6 100755 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -4009,6 +4009,37 @@ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle, return result; } +OCStackResult OCResetResourceTypes(OCResourceHandle handle, + const char *newResourceType) +{ + OCStackResult result = OC_STACK_ERROR; + OCResource *resource = NULL; + + resource = findResource((OCResource *) handle); + if (!resource) + { + OIC_LOG(ERROR, TAG, "Resource not found"); + return OC_STACK_ERROR; + } + + // Clear all bound resource types + deleteResourceType(resource->rsrcType); + resource->rsrcType = NULL; + + // Bind new resource type to resource + result = BindResourceTypeToResource(resource, newResourceType); + +#ifdef WITH_PRESENCE + if(presenceResource.handle) + { + ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom(); + SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE); + } +#endif + + return result; +} + OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle, const char *resourceInterfaceName) { @@ -4036,6 +4067,37 @@ OCStackResult OCBindResourceInterfaceToResource(OCResourceHandle handle, return result; } +OCStackResult OCResetResourceInterfaces(OCResourceHandle handle, + const char *newResourceInterface) +{ + OCStackResult result = OC_STACK_ERROR; + OCResource *resource = NULL; + + resource = findResource((OCResource *) handle); + if (!resource) + { + OIC_LOG(ERROR, TAG, "Resource not found"); + return OC_STACK_ERROR; + } + + // Clear all bound interface + deleteResourceInterface(resource->rsrcInterface); + resource->rsrcInterface = NULL; + + // Bind new interface to resource + result = BindResourceInterfaceToResource(resource, newResourceInterface); + +#ifdef WITH_PRESENCE + if (presenceResource.handle) + { + ((OCResource *)presenceResource.handle)->sequenceNum = OCGetRandom(); + SendPresenceNotification(resource->rsrcType, OC_PRESENCE_TRIGGER_CHANGE); + } +#endif + + return result; +} + OCStackResult OCGetNumberOfResources(uint8_t *numResources) { OCResource *pointer = headResource; diff --git a/resource/include/IServerWrapper.h b/resource/include/IServerWrapper.h index de5f019..741085b 100644 --- a/resource/include/IServerWrapper.h +++ b/resource/include/IServerWrapper.h @@ -59,14 +59,23 @@ namespace OC virtual OCStackResult unregisterResource( const OCResourceHandle& resourceHandle) = 0; + virtual OCStackResult bindTypeToResource( const OCResourceHandle& resourceHandle, const std::string& resourceTypeName) = 0; + virtual OCStackResult resetResourceTypes( + const OCResourceHandle& resourceHandle, + const std::string& newResourceType) = 0; + virtual OCStackResult bindInterfaceToResource( const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) = 0; + virtual OCStackResult resetResourceInterfaces( + const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface) = 0; + virtual OCStackResult startPresence(const unsigned int seconds) = 0; virtual OCStackResult stopPresence() = 0; diff --git a/resource/include/InProcServerWrapper.h b/resource/include/InProcServerWrapper.h index 6d54e40..b81a73a 100644 --- a/resource/include/InProcServerWrapper.h +++ b/resource/include/InProcServerWrapper.h @@ -57,10 +57,18 @@ namespace OC const OCResourceHandle& resourceHandle, const std::string& resourceTypeName); + virtual OCStackResult resetResourceTypes( + const OCResourceHandle& resourceHandle, + const std::string& newResourceType); + virtual OCStackResult bindInterfaceToResource( const OCResourceHandle& resourceHandle, const std::string& resourceInterface); + virtual OCStackResult resetResourceInterfaces( + const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface); + virtual OCStackResult startPresence(const unsigned int seconds); virtual OCStackResult stopPresence(); diff --git a/resource/include/OCPlatform.h b/resource/include/OCPlatform.h index 84e7163..d9fe21a 100644 --- a/resource/include/OCPlatform.h +++ b/resource/include/OCPlatform.h @@ -485,6 +485,17 @@ namespace OC const std::string& resourceTypeName); /** + * Reset resource types of resource to newly passed resource type. + * + * @param resourceHandle handle to the resource + * @param newResourceType new typename to bind to the resource + * + * @return Returns ::OC_STACK_OK if success. + */ + OCStackResult resetResourceTypes(const OCResourceHandle& resourceHandle, + const std::string& newResourceType); + + /** * Binds an interface to a particular resource * @param resourceHandle handle to the resource * @param resourceInterfaceName new interface to bind to the resource @@ -494,6 +505,16 @@ namespace OC OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName); + /** + * Reset resource interfaces of resource to newly passed interface. + * + * @param resourceHandle handle to the resource + * @param newResourceInterface new interface to bind to the resource + * + * @return Returns ::OC_STACK_OK if success. + */ + OCStackResult resetResourceInterfaces(const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface); /** * Start Presence announcements. diff --git a/resource/include/OCPlatform_impl.h b/resource/include/OCPlatform_impl.h index 1c3a276..275be47 100644 --- a/resource/include/OCPlatform_impl.h +++ b/resource/include/OCPlatform_impl.h @@ -226,9 +226,15 @@ namespace OC OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle, const std::string& resourceTypeName) const; + OCStackResult resetResourceTypes(const OCResourceHandle& resourceHandle, + const std::string& newResourceType) const; + OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) const; + OCStackResult resetResourceInterfaces(const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface) const; + OCStackResult startPresence(const unsigned int ttl); OCStackResult stopPresence(); diff --git a/resource/src/InProcServerWrapper.cpp b/resource/src/InProcServerWrapper.cpp index 2766816..b4b799e 100644 --- a/resource/src/InProcServerWrapper.cpp +++ b/resource/src/InProcServerWrapper.cpp @@ -534,6 +534,28 @@ namespace OC return result; } + OCStackResult InProcServerWrapper::resetResourceTypes(const OCResourceHandle& resourceHandle, + const std::string& newResourceType) + { + auto cLock = m_csdkLock.lock(); + OCStackResult result; + if(cLock) + { + std::lock_guard lock(*cLock); + result = OCResetResourceTypes(resourceHandle, newResourceType.c_str()); + } + else + { + result = OC_STACK_ERROR; + } + + if (result != OC_STACK_OK) + { + throw OCException(OC::Exception::BIND_TYPE_FAILED, result); + } + return result; + } + OCStackResult InProcServerWrapper::bindInterfaceToResource( const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) @@ -558,6 +580,30 @@ namespace OC return result; } + OCStackResult InProcServerWrapper::resetResourceInterfaces( + const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface) + { + auto cLock = m_csdkLock.lock(); + OCStackResult result; + if(cLock) + { + std::lock_guard lock(*cLock); + result = OCResetResourceInterfaces(resourceHandle, + newResourceInterface.c_str()); + } + else + { + result = OC_STACK_ERROR; + } + + if (result != OC_STACK_OK) + { + throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result); + } + return result; + } + OCStackResult InProcServerWrapper::startPresence(const unsigned int seconds) { auto cLock = m_csdkLock.lock(); diff --git a/resource/src/OCPlatform.cpp b/resource/src/OCPlatform.cpp index f4cb635..95355d2 100644 --- a/resource/src/OCPlatform.cpp +++ b/resource/src/OCPlatform.cpp @@ -266,6 +266,12 @@ namespace OC return OCPlatform_impl::Instance().bindTypeToResource(resourceHandle,resourceTypeName); } + OCStackResult resetResourceTypes(const OCResourceHandle& resourceHandle, + const std::string& newResourceType) + { + return OCPlatform_impl::Instance().resetResourceTypes(resourceHandle,newResourceType); + } + OCStackResult bindInterfaceToResource(const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) { @@ -273,6 +279,13 @@ namespace OC resourceInterfaceName); } + OCStackResult resetResourceInterfaces(const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface) + { + return OCPlatform_impl::Instance().resetResourceInterfaces(resourceHandle, + newResourceInterface); + } + OCStackResult startPresence(const unsigned int announceDurationSeconds) { return OCPlatform_impl::Instance().startPresence(announceDurationSeconds); diff --git a/resource/src/OCPlatform_impl.cpp b/resource/src/OCPlatform_impl.cpp index d782d6b..df2d1bd 100755 --- a/resource/src/OCPlatform_impl.cpp +++ b/resource/src/OCPlatform_impl.cpp @@ -474,6 +474,13 @@ namespace OC resourceHandle, resourceTypeName); } + OCStackResult OCPlatform_impl::resetResourceTypes(const OCResourceHandle& resourceHandle, + const std::string& newResourceType) const + { + return checked_guard(m_server, &IServerWrapper::resetResourceTypes, + resourceHandle, newResourceType); + } + OCStackResult OCPlatform_impl::bindInterfaceToResource(const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) const { @@ -481,6 +488,13 @@ namespace OC resourceHandle, resourceInterfaceName); } + OCStackResult OCPlatform_impl::resetResourceInterfaces(const OCResourceHandle& resourceHandle, + const std::string& newResourceInterface) const + { + return checked_guard(m_server, &IServerWrapper::resetResourceInterfaces, + resourceHandle, newResourceInterface); + } + OCStackResult OCPlatform_impl::startPresence(const unsigned int announceDurationSeconds) { return checked_guard(m_server, &IServerWrapper::startPresence,