New APIs for editing resource and interfaces types 86/190686/1
authorHarish Kumara M <h.marappa@samsung.com>
Thu, 16 Aug 2018 11:19:44 +0000 (16:49 +0530)
committerAmit KS <amit.s12@samsung.com>
Thu, 4 Oct 2018 15:13:08 +0000 (20:43 +0530)
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 <h.marappa@samsung.com>
Signed-off-by: Amit KS <amit.s12@samsung.com>
resource/csdk/stack/include/ocstack.h
resource/csdk/stack/src/ocstack.c
resource/include/IServerWrapper.h
resource/include/InProcServerWrapper.h
resource/include/OCPlatform.h
resource/include/OCPlatform_impl.h
resource/src/InProcServerWrapper.cpp
resource/src/OCPlatform.cpp
resource/src/OCPlatform_impl.cpp

index 3ee7ec3..897d770 100644 (file)
@@ -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.
index 139ff3a..b6e30e6 100755 (executable)
@@ -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;
index de5f019..741085b 100644 (file)
@@ -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;
index 6d54e40..b81a73a 100644 (file)
@@ -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();
index 84e7163..d9fe21a 100644 (file)
@@ -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.
index 1c3a276..275be47 100644 (file)
@@ -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();
index 2766816..b4b799e 100644 (file)
@@ -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<std::recursive_mutex> 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<std::recursive_mutex> 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();
index f4cb635..95355d2 100644 (file)
@@ -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);
index d782d6b..df2d1bd 100755 (executable)
@@ -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,