New APIs for editing resource and interfaces types 16/190716/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>
Fri, 5 Oct 2018 03:32:49 +0000 (09:02 +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 3ee7ec3dda9e900567233188924a2a34bae2559b..897d7700eac482fc3f4f0fc12e06e392b8553e34 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.
  *
@@ -390,6 +403,18 @@ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle,
 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.
  *
index 139ff3a2a1659c7c4834e1baa28c5a195af07b84..b6e30e61b1289b1c13e94c392c51bf3eefe3fb33 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 de5f019bfab7a97aff07de0fea12ac06177fb2dd..741085b0ed51d6ea5e7b28c948394e96e21afe7b 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 6d54e4025e0b011de4f4eee59c0b23e71d26b9f1..b81a73a0f75d088ab8f83551ddd435dbb9dc60ed 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 84e7163c9125bfeef30bf6d4c0c803989843c14a..d9fe21ae7bd3d61126dc16ee8133aa11eb7f0781 100644 (file)
@@ -484,6 +484,17 @@ namespace OC
         OCStackResult bindTypeToResource(const OCResourceHandle& resourceHandle,
                         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
@@ -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 1c3a27666f73bee307609513c51e751bbb73a052..275be472f8b9d6d3fdaa9625f075dd80c0ffcae0 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 2766816b5c45c7b0f6f6e77f763a44a52b745ce4..b4b799ea79a5894e07c0a602f7ef680e79b1670e 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 f4cb6357bb43a470bb019cfdb617787b809ff621..95355d20d7366cb48cf7ee4c3b8c4e38e8d48478 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 d782d6b73c4146becce1d5da269ea8a99cd80559..df2d1bd3438ca9bc0941a98b0d6eab9d9037d9d7 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,