From: jaesick.shin Date: Wed, 25 May 2016 11:18:38 +0000 (+0900) Subject: Issue fixed about OCEntityHandlerRequest same pointer address. X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=7e4bb7c179c43df48044dfbf0ecedfbb341bf6c7;p=contrib%2Fiotivity.git Issue fixed about OCEntityHandlerRequest same pointer address. 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/8331 Tested-by: jenkins-iotivity Reviewed-by: Uze Choi --- diff --git a/service/notification/examples/linux/notificationprovider.c b/service/notification/examples/linux/notificationprovider.c index 95ea737..8dac9c0 100644 --- a/service/notification/examples/linux/notificationprovider.c +++ b/service/notification/examples/linux/notificationprovider.c @@ -1,3 +1,23 @@ +/****************************************************************** + * + * Copyright 2015 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 #include #include @@ -17,7 +37,7 @@ bool isExit = false; int id; -void OCProcessThread(void * ptr) +void* OCProcessThread(void * ptr) { (void) ptr; while (!isExit) @@ -25,9 +45,11 @@ void OCProcessThread(void * ptr) if (OCProcess() != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCStack process error"); - return; + return NULL; } } + + return NULL; } void subscribeRequestCallback(NSConsumer *consumer) @@ -108,11 +130,10 @@ int main() printf("body : "); gets(body); - printf("app - mId : %s \n", charID); + printf("app - mId : %d \n", charID); printf("app - mTitle : %s \n", title); printf("app - mContentText : %s \n", body); - NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage)); sprintf(charID, "%d", id); diff --git a/service/notification/src/common/NSUtil.c b/service/notification/src/common/NSUtil.c index 3781d1c..ce6aae2 100755 --- a/service/notification/src/common/NSUtil.c +++ b/service/notification/src/common/NSUtil.c @@ -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; +} + diff --git a/service/notification/src/common/NSUtil.h b/service/notification/src/common/NSUtil.h index c52d9cd..90bd1db 100755 --- a/service/notification/src/common/NSUtil.h +++ b/service/notification/src/common/NSUtil.h @@ -26,9 +26,15 @@ #include #include "ocstack.h" #include "ocpayload.h" +#include "octypes.h" #include "NSStructs.h" #include "NSConstants.h" +#include "oic_malloc.h" +#include "oic_string.h" NSResult NSFreeMessage(NSMessage *); +NSMessage * NSDuplicateMessage(NSMessage *); +OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *); +NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest *); #endif /* _NS_UTIL__H_ */ diff --git a/service/notification/src/provider/NSProviderDiscovery.c b/service/notification/src/provider/NSProviderDiscovery.c index 929a2b3..03bb975 100644 --- a/service/notification/src/provider/NSProviderDiscovery.c +++ b/service/notification/src/provider/NSProviderDiscovery.c @@ -58,7 +58,7 @@ void * NSDiscoverySchedule(void * ptr) { if (ptr == NULL) { - OIC_LOG(INFO, DISCOVERY_TAG, "Create NSDiscoverySchedule"); + OIC_LOG(INFO, DISCOVERY_TAG, "Create NSDiscoverySchedule\n"); NS_LOG(INFO, "Create NSDiscoverySchedule"); } diff --git a/service/notification/src/provider/NSProviderInterface.c b/service/notification/src/provider/NSProviderInterface.c index 21bb1fc..490e3dd 100644 --- a/service/notification/src/provider/NSProviderInterface.c +++ b/service/notification/src/provider/NSProviderInterface.c @@ -113,7 +113,16 @@ NSResult NSSendNotification(NSMessage *msg) { OIC_LOG(INFO, INTERFACE_TAG, "Send Notification"); NS_LOG(DEBUG, "NSSendNotification - IN"); - NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, msg); + + NSMessage * newMsg = NSDuplicateMessage(msg); + + if(newMsg == NULL) + { + NS_LOG(ERROR, "Msg is NULL"); + return NS_ERROR; + } + + NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_NOTIFICATION, newMsg); NS_LOG(DEBUG, "NSSendNotification - OUT"); return NS_OK; } @@ -180,6 +189,7 @@ void * NSResponseSchedule(void * ptr) consumer.mUserData = obId; NSSubscribeRequestCb(&consumer); + NSFreeOCEntityHandlerRequest(request); break; } diff --git a/service/notification/src/provider/NSProviderListener.c b/service/notification/src/provider/NSProviderListener.c index 173143e..e4f669d 100644 --- a/service/notification/src/provider/NSProviderListener.c +++ b/service/notification/src/provider/NSProviderListener.c @@ -57,7 +57,8 @@ OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag, OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_GET from client"); NS_LOG(DEBUG, "NSEntityHandlerNotificationCb - OC_REST_GET"); - NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY, (void *)entityHandlerRequest); + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SEND_POLICY, + NSCopyOCEntityHandlerRequest(entityHandlerRequest)); ehResult = OC_EH_OK; } @@ -252,16 +253,17 @@ OCEntityHandlerResult NSEntityHandlerMessageCb(OCEntityHandlerFlag flag, if (flag & OC_OBSERVE_FLAG) { - OIC_LOG(INFO, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG"); + OIC_LOG(DEBUG, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG"); NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_FLAG"); if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { - OIC_LOG (INFO, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client"); + OIC_LOG (DEBUG, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client"); NS_LOG(DEBUG, "NSEntityHandlerMessageCb - OC_OBSERVE_REGISTER"); NS_LOG_V(DEBUG, "NSEntityHandlerMessageCb\n" "Register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId); - NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION, entityHandlerRequest); + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_RECV_SUBSCRIPTION, + NSCopyOCEntityHandlerRequest(entityHandlerRequest)); } } @@ -316,19 +318,14 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag, else if (OC_REST_POST == entityHandlerRequest->method) { /** Receive sync data from consumer which read or dismiss notification message. - And broadcast the sync data to all subscribers including provider app - to synchronize the notification message status. */ + And broadcast the sync data to all subscribers including provider app + to synchronize the notification message status. */ + OIC_LOG (INFO, LISTENER_TAG, "Received OC_REST_POST from client"); NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_REST_POST"); - // send to subscribers - NSPushQueue(NOTIFICATION_SCHEDULER, TASK_SEND_READ, - NSBuildOICNotificationSync(entityHandlerRequest->payload)); - - // send to provider app NSPushQueue(NOTIFICATION_SCHEDULER, TASK_RECV_READ, NSBuildOICNotificationSync(entityHandlerRequest->payload)); - ehResult = OC_EH_OK; } else if (OC_REST_DELETE == entityHandlerRequest->method) @@ -396,15 +393,17 @@ OCEntityHandlerResult NSEntityHandlerSyncCb(OCEntityHandlerFlag flag, /** Requested by consumers to synchronize notification message status. Store the observer IDs to storage or cache */ - OIC_LOG(INFO, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG"); + OIC_LOG(DEBUG, LISTENER_TAG, "Flag includes OC_OBSERVE_FLAG"); + NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_FLAG"); if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) { - OIC_LOG (INFO, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client"); + OIC_LOG (DEBUG, LISTENER_TAG, "Received OC_OBSERVE_REGISTER from client"); NS_LOG(DEBUG, "NSEntityHandlerSyncCb - OC_OBSERVE_REGISTER"); NS_LOG_V(DEBUG, "NSEntityHandlerSyncCb\n - " - "Register message observerID : %d\n", entityHandlerRequest->obsInfo.obsId); - NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SYNC_SUBSCRIPTION, entityHandlerRequest); + "Register Sync observerID : %d\n", entityHandlerRequest->obsInfo.obsId); + NSPushQueue(SUBSCRIPTION_SCHEDULER, TASK_SYNC_SUBSCRIPTION, + NSCopyOCEntityHandlerRequest(entityHandlerRequest)); } } diff --git a/service/notification/src/provider/NSProviderListener.h b/service/notification/src/provider/NSProviderListener.h index 6b85494..2d8b065 100644 --- a/service/notification/src/provider/NSProviderListener.h +++ b/service/notification/src/provider/NSProviderListener.h @@ -32,6 +32,7 @@ #include "cautilinterface.h" #include "oic_string.h" #include "oic_malloc.h" +#include "NSUtil.h" OCEntityHandlerResult NSEntityHandlerNotificationCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *entityHandlerRequest, void* callback); diff --git a/service/notification/src/provider/NSProviderNotification.c b/service/notification/src/provider/NSProviderNotification.c index aaa9899..f0a943f 100644 --- a/service/notification/src/provider/NSProviderNotification.c +++ b/service/notification/src/provider/NSProviderNotification.c @@ -144,6 +144,7 @@ NSResult NSSendMessage(NSMessage *msg) return NS_ERROR; } OCRepPayloadDestroy(payload); + NSFreeMessage(msg); NS_LOG(DEBUG, "NSSendMessage - OUT"); diff --git a/service/notification/src/provider/NSProviderSubscription.c b/service/notification/src/provider/NSProviderSubscription.c index 084f472..54a27c1 100644 --- a/service/notification/src/provider/NSProviderSubscription.c +++ b/service/notification/src/provider/NSProviderSubscription.c @@ -106,6 +106,7 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest return NS_ERROR; } OCRepPayloadDestroy(payload); + NSFreeOCEntityHandlerRequest(entityHandlerRequest); NS_LOG(DEBUG, "NSSendAccessPolicyResponse - OUT"); return NS_OK; @@ -131,6 +132,9 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour element->data = (void*) subData; element->next = NULL; + NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id); + NS_LOG_V(DEBUG, "SubList message observation ID = [%d]", subData->messageObId); + if (NSCacheWrite(consumerSubList, element) != NS_OK) { NS_LOG(DEBUG, "fail to write cache"); @@ -163,7 +167,7 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour element->next = NULL; NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id); - NS_LOG_V(DEBUG, "SubList observation ID = [%d]", subData->syncObId); + NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId); if (NSCacheWrite(consumerSubList, element) != NS_OK) { @@ -198,6 +202,7 @@ void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest) } NS_LOG(DEBUG, "NSHandleUnsubscription - IN"); + NSFreeOCEntityHandlerRequest(entityHandlerRequest); } void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest) @@ -290,6 +295,7 @@ NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest } NSSendResponse(entityHandlerRequest->devAddr.addr, accepted); + NSFreeOCEntityHandlerRequest(entityHandlerRequest); NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT"); return NS_OK; diff --git a/service/notification/src/provider/NSProviderSubscription.h b/service/notification/src/provider/NSProviderSubscription.h index 7a40256..4f27a20 100644 --- a/service/notification/src/provider/NSProviderSubscription.h +++ b/service/notification/src/provider/NSProviderSubscription.h @@ -43,5 +43,6 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest); void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest); NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted); +NSResult NSSendResponse(const char * id, bool accepted); #endif /* _NS_PROVIDER_SUBSCRIPTION_H_ */ diff --git a/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c index d52eee1..476d892 100755 --- a/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c +++ b/service/notification/src/provider/cache/linux/NSProviderMemoryCache.c @@ -101,8 +101,6 @@ NSResult NSCacheUpdateSubScriptionState(NSCacheList * list, NSCacheSubData * upd NS_LOG_V(DEBUG, "currData_SyncObID = %d", itData->syncObId); NS_LOG_V(DEBUG, "currData_IsWhite = %d", itData->isWhite); - NS_LOG(DEBUG,""); - NS_LOG_V(DEBUG, "updateData_ID = %s", updateData->id); NS_LOG_V(DEBUG, "updateData_MsgObID = %d", updateData->messageObId); NS_LOG_V(DEBUG, "updateData_SyncObID = %d", updateData->syncObId); @@ -163,8 +161,6 @@ NSResult NSCacheWrite(NSCacheList * list, NSCacheElement * newObj) NS_LOG_V(DEBUG, "currData_SyncObID = %d", itData->syncObId); NS_LOG_V(DEBUG, "currData_IsWhite = %d", itData->isWhite); - NS_LOG(DEBUG,""); - NS_LOG_V(DEBUG, "subData_ID = %s", subData->id); NS_LOG_V(DEBUG, "subData_MsgObID = %d", subData->messageObId); NS_LOG_V(DEBUG, "subData_SyncObID = %d", subData->syncObId);