Issue fixed about OCEntityHandlerRequest same pointer address. 31/8331/2
authorjaesick.shin <jaesick.shin@samsung.com>
Wed, 25 May 2016 11:18:38 +0000 (20:18 +0900)
committerUze Choi <uzchoi@samsung.com>
Wed, 25 May 2016 22:09:42 +0000 (22:09 +0000)
Modify duplicatation issue about
OCEntityHandlerRequest pointer address
when receiving entityhandler callback.
So, Added NSMessage, OCEntityHandlerRequest copy & free logic.

Change-Id: Ia4a273f11233f11fdfda055af019ab4b3a27db0c
Signed-off-by: jaesick.shin <jaesick.shin@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/8331
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
service/notification/examples/linux/notificationprovider.c
service/notification/src/common/NSUtil.c
service/notification/src/common/NSUtil.h
service/notification/src/provider/NSProviderDiscovery.c
service/notification/src/provider/NSProviderInterface.c
service/notification/src/provider/NSProviderListener.c
service/notification/src/provider/NSProviderListener.h
service/notification/src/provider/NSProviderNotification.c
service/notification/src/provider/NSProviderSubscription.c
service/notification/src/provider/NSProviderSubscription.h
service/notification/src/provider/cache/linux/NSProviderMemoryCache.c

index 95ea737..8dac9c0 100644 (file)
@@ -1,3 +1,23 @@
+/******************************************************************\r
+ *\r
+ * Copyright 2015 Samsung Electronics All Rights Reserved.\r
+ *\r
+ *\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *      http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *\r
+ ******************************************************************/\r
+\r
 #include <stdio.h>\r
 #include <stdbool.h>\r
 #include <stdlib.h>\r
@@ -17,7 +37,7 @@ bool isExit = false;
 \r
 int id;\r
 \r
-void OCProcessThread(void * ptr)\r
+void* OCProcessThread(void * ptr)\r
 {\r
     (void) ptr;\r
     while (!isExit)\r
@@ -25,9 +45,11 @@ void OCProcessThread(void * ptr)
         if (OCProcess() != OC_STACK_OK)\r
         {\r
             OIC_LOG(ERROR, TAG, "OCStack process error");\r
-            return;\r
+            return NULL;\r
         }\r
     }\r
+\r
+    return NULL;\r
 }\r
 \r
 void subscribeRequestCallback(NSConsumer *consumer)\r
@@ -108,11 +130,10 @@ int main()
                 printf("body : ");\r
                 gets(body);\r
 \r
-                printf("app - mId : %s \n", charID);\r
+                printf("app - mId : %d \n", charID);\r
                 printf("app - mTitle : %s \n", title);\r
                 printf("app - mContentText : %s \n", body);\r
 \r
-\r
                 NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));\r
 \r
                 sprintf(charID, "%d", id);\r
index 3781d1c..ce6aae2 100755 (executable)
@@ -20,8 +20,6 @@
 
 #include "NSUtil.h"
 
-#include "oic_malloc.h"
-
 NSResult NSFreeMessage(NSMessage * obj)
 {
     if (!obj)
@@ -52,4 +50,94 @@ NSResult NSFreeMessage(NSMessage * obj)
     return NS_OK;
 }
 
+NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
+{
+    if(copyMsg == NULL)
+    {
+        NS_LOG(ERROR, "Copy Msg is NULL");
+        return NULL;
+    }
+
+    NSMessage * newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
+
+    if (!copyMsg->mId)
+    {
+        newMsg->mId = OICStrdup(copyMsg->mId);
+    }
+
+    if (!copyMsg->mTitle)
+    {
+        newMsg->mTitle = OICStrdup(copyMsg->mTitle);
+    }
+
+    if (!copyMsg->mContentText)
+    {
+        newMsg->mContentText = OICStrdup(copyMsg->mContentText);
+    }
+
+    return newMsg;
+}
+
+OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
+{
+    NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
+
+    OCEntityHandlerRequest *copyOfRequest =
+            (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
+
+    if (copyOfRequest)
+    {
+        // Do shallow copy
+        memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
+
+
+        if (copyOfRequest->query)
+        {
+            copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
+            if(!copyOfRequest->query)
+            {
+                NS_LOG(ERROR, "Copy failed due to allocation failure");
+                OICFree(copyOfRequest);
+                return NULL;
+            }
+        }
+
+        if (entityHandlerRequest->payload)
+        {
+            copyOfRequest->payload = (OCPayload *)
+                    (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
+        }
+
+        // Ignore vendor specific header options for example
+        copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
+        copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
+    }
+
+    if (copyOfRequest)
+    {
+        NS_LOG(DEBUG, "Copied client request");
+    }
+    else
+    {
+        NS_LOG(DEBUG, "Error copying client request");
+    }
+
+    NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
+
+    return copyOfRequest;
+}
+
+NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
+{
+    NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
+
+    OICFree(entityHandlerRequest->query);
+    OCPayloadDestroy(entityHandlerRequest->payload);
+    OICFree(entityHandlerRequest);
+
+    NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
+
+    return NS_OK;
+}
+
 
index c52d9cd..90bd1db 100755 (executable)
 #include <stdbool.h>\r
 #include "ocstack.h"\r
 #include "ocpayload.h"\r
+#include "octypes.h"\r
 #include "NSStructs.h"\r
 #include "NSConstants.h"\r
+#include "oic_malloc.h"\r
+#include "oic_string.h"\r
 \r
 NSResult NSFreeMessage(NSMessage *);\r
+NSMessage * NSDuplicateMessage(NSMessage *);\r
+OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *);\r
+NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest *);\r
 \r
 #endif /* _NS_UTIL__H_ */\r
index 929a2b3..03bb975 100644 (file)
@@ -58,7 +58,7 @@ void * NSDiscoverySchedule(void * ptr)
 {\r
     if (ptr == NULL)\r
     {\r
-        OIC_LOG(INFO, DISCOVERY_TAG, "Create NSDiscoverySchedule");\r
+        OIC_LOG(INFO, DISCOVERY_TAG, "Create NSDiscoverySchedule\n");\r
         NS_LOG(INFO, "Create NSDiscoverySchedule");\r
     }\r
 \r
index 21bb1fc..490e3dd 100644 (file)
@@ -113,7 +113,16 @@ NSResult NSSendNotification(NSMessage *msg)
 {\r
     OIC_LOG(INFO, INTERFACE_TAG, "Send Notification");\r
     NS_LOG(DEBUG, "NSSendNotification - IN");\r
-    NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, msg);\r
+\r
+    NSMessage * newMsg = NSDuplicateMessage(msg);\r
+\r
+    if(newMsg == NULL)\r
+    {\r
+        NS_LOG(ERROR, "Msg is NULL");\r
+        return NS_ERROR;\r
+    }\r
+\r
+    NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg);\r
     NS_LOG(DEBUG, "NSSendNotification - OUT");\r
     return NS_OK;\r
 }\r
@@ -180,6 +189,7 @@ void * NSResponseSchedule(void * ptr)
                     consumer.mUserData = obId;\r
 \r
                     NSSubscribeRequestCb(&consumer);\r
+                    NSFreeOCEntityHandlerRequest(request);\r
 \r
                     break;\r
                 }\r
index 173143e..e4f669d 100644 (file)
@@ -57,7 +57,8 @@ OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag,
             OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_GET from client");\r
             NS_LOG(DEBUG, "NSEntityHandlerNotificationCb - OC_REST_GET");\r
 \r
-            NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY, (void *)entityHandlerRequest);\r
+            NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY,\r
+                    NSCopyOCEntityHandlerRequest(entityHandlerRequest));\r
             ehResult = OC_EH_OK;\r
 \r
         }\r
@@ -252,16 +253,17 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag,
 \r
     if (flag & OC_OBSERVE_FLAG)\r
     {\r
-        OIC_LOG(INFO, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG");\r
+        OIC_LOG(DEBUG, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG");\r
         NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_FLAG");\r
 \r
         if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)\r
         {\r
-            OIC_LOG (INFO, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client");\r
+            OIC_LOG (DEBUG, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client");\r
             NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_REGISTER");\r
             NS_LOG_V(DEBUG, "NSEntityHandlerMessageCb\n"\r
                     "Register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId);\r
-            NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION, entityHandlerRequest);\r
+            NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION,\r
+                    NSCopyOCEntityHandlerRequest(entityHandlerRequest));\r
         }\r
     }\r
 \r
@@ -316,19 +318,14 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,
         else if (OC_REST_POST == entityHandlerRequest->method)\r
         {\r
             /** Receive sync data from consumer which read or dismiss notification message.\r
-                And broadcast the sync data to all subscribers including provider app\r
-                to synchronize the notification message status. */\r
+                           And broadcast the sync data to all subscribers including provider app\r
+                           to synchronize the notification message status. */\r
+\r
             OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_POST from client");\r
             NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_REST_POST");\r
 \r
-            // send to subscribers\r
-            NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ,\r
-                    NSBuildOICNotificationSync(entityHandlerRequest->payload));\r
-\r
-            // send to provider app\r
             NSPushQueue(NOTIFICATION_SCHEDULER, TASK_RECV_READ,\r
                     NSBuildOICNotificationSync(entityHandlerRequest->payload));\r
-\r
             ehResult = OC_EH_OK;\r
         }\r
         else if (OC_REST_DELETE == entityHandlerRequest->method)\r
@@ -396,15 +393,17 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag,
         /** Requested by consumers to synchronize notification message status.\r
             Store the observer IDs to storage or cache */\r
 \r
-        OIC_LOG(INFO, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG");\r
+        OIC_LOG(DEBUG, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG");\r
+        NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_FLAG");\r
 \r
         if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)\r
         {\r
-            OIC_LOG (INFO, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client");\r
+            OIC_LOG (DEBUG, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client");\r
             NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_REGISTER");\r
             NS_LOG_V(DEBUG, "NSEntityHandlerSyncCb\n - "\r
-                    "Register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId);\r
-            NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SYNC_SUBSCRIPTION, entityHandlerRequest);\r
+                    "Register Sync observerID : %d\n", entityHandlerRequest->obsInfo.obsId);\r
+            NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SYNC_SUBSCRIPTION,\r
+                    NSCopyOCEntityHandlerRequest(entityHandlerRequest));\r
         }\r
     }\r
 \r
index 6b85494..2d8b065 100644 (file)
@@ -32,6 +32,7 @@
 #include "cautilinterface.h"\r
 #include "oic_string.h"\r
 #include "oic_malloc.h"\r
+#include "NSUtil.h"\r
 \r
 OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag,\r
         OCEntityHandlerRequest *entityHandlerRequest, void* callback);\r
index aaa9899..f0a943f 100644 (file)
@@ -144,6 +144,7 @@ NSResult NSSendMessage(NSMessage *msg)
         return NS_ERROR;
     }
     OCRepPayloadDestroy(payload);
+    NSFreeMessage(msg);
 
     NS_LOG(DEBUG, "NSSendMessage - OUT");
 
index 084f472..54a27c1 100644 (file)
@@ -106,6 +106,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
         return NS_ERROR;\r
     }\r
     OCRepPayloadDestroy(payload);\r
+    NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
 \r
     NS_LOG(DEBUG, "NSSendAccessPolicyResponse - OUT");\r
     return NS_OK;\r
@@ -131,6 +132,9 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
         element->data = (void*) subData;\r
         element->next = NULL;\r
 \r
+        NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id);\r
+        NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId);\r
+\r
         if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
             NS_LOG(DEBUG, "fail to write cache");\r
@@ -163,7 +167,7 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
         element->next = NULL;\r
 \r
         NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id);\r
-        NS_LOG_V(DEBUG, "SubList observation ID = [%d]", subData->syncObId);\r
+        NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId);\r
 \r
         if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
@@ -198,6 +202,7 @@ void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
     }\r
 \r
     NS_LOG(DEBUG, "NSHandleUnsubscription - IN");\r
+    NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
 }\r
 \r
 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)\r
@@ -290,6 +295,7 @@ NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest
     }\r
 \r
     NSSendResponse(entityHandlerRequest->devAddr.addr, accepted);\r
+    NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
 \r
     NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT");\r
     return NS_OK;\r
index 7a40256..4f27a20 100644 (file)
@@ -43,5 +43,6 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
 void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest);\r
 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest);\r
 NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted);\r
+NSResult NSSendResponse(const char * id, bool accepted);\r
 \r
 #endif /* _NS_PROVIDER_SUBSCRIPTION_H_ */\r
index d52eee1..476d892 100755 (executable)
@@ -101,8 +101,6 @@ NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, NSCacheSubData * upd
             NS_LOG_V(DEBUG, "currData_SyncObID = %d", itData->syncObId);\r
             NS_LOG_V(DEBUG, "currData_IsWhite = %d", itData->isWhite);\r
 \r
-            NS_LOG(DEBUG,"");\r
-\r
             NS_LOG_V(DEBUG, "updateData_ID = %s", updateData->id);\r
             NS_LOG_V(DEBUG, "updateData_MsgObID = %d", updateData->messageObId);\r
             NS_LOG_V(DEBUG, "updateData_SyncObID = %d", updateData->syncObId);\r
@@ -163,8 +161,6 @@ NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj)
                 NS_LOG_V(DEBUG, "currData_SyncObID = %d", itData->syncObId);\r
                 NS_LOG_V(DEBUG, "currData_IsWhite = %d", itData->isWhite);\r
 \r
-                NS_LOG(DEBUG,"");\r
-\r
                 NS_LOG_V(DEBUG, "subData_ID = %s", subData->id);\r
                 NS_LOG_V(DEBUG, "subData_MsgObID = %d", subData->messageObId);\r
                 NS_LOG_V(DEBUG, "subData_SyncObID = %d", subData->syncObId);\r