implementation NSStopProvider and bug fixed.
[platform/upstream/iotivity.git] / service / notification / src / provider / NSProviderSubscription.c
index a0aae6d..49c76fd 100644 (file)
 \r
 NSResult NSInitSubscriptionList()\r
 {\r
+    NS_LOG(DEBUG, "NSInitSubscriptionList - IN");\r
+\r
     consumerSubList = NSCacheCreate();\r
     consumerSubList->cacheType = NS_PROVIDER_CACHE_SUBSCRIBER;\r
+\r
+    NS_LOG(DEBUG, "NSInitSubscriptionList - OUT");\r
     return NS_OK;\r
 }\r
 \r
 NSResult NSSetSubscriptionAcceptPolicy(NSAccessPolicy policy)\r
 {\r
+    NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - IN");\r
+\r
     if (policy == NS_ACCEPTER_PROVIDER)\r
     {\r
-        OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Provider as a subscription accepter");\r
+        NS_LOG(DEBUG, "Place Provider as a subscription accepter");\r
     }\r
     else if (policy == NS_ACCEPTER_CONSUMER)\r
     {\r
-        OIC_LOG(INFO, SUBSCRIPTION_TAG, "Place Consumer as a subscription accepter");\r
+        NS_LOG(DEBUG, "Place Consumer as a subscription accepter");\r
     }\r
 \r
     NSSubscriptionAccepter = policy;\r
 \r
+    NS_LOG(DEBUG, "NSSetSubscriptionAcceptPolicy - OUT");\r
     return NS_OK;\r
 }\r
 \r
@@ -50,14 +57,14 @@ int NSGetSubscriptionAccepter()
 \r
 NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest)\r
 {\r
-    OIC_LOG(DEBUG, NOTIFICATION_TAG, "Send Notification Policy to consumer");\r
+    NS_LOG(DEBUG, "NSSendAccessPolicyResponse - IN");\r
 \r
     // put notification resource\r
     OCResourceHandle notificationResourceHandle;\r
     if (NSPutNotificationResource(NSGetSubscriptionAccepter(), &notificationResourceHandle)\r
             != NS_OK)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to put notification resource"));\r
+        NS_LOG(ERROR, "Fail to put notification resource");\r
         return NS_ERROR;\r
     }\r
 \r
@@ -69,10 +76,9 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
     memset(response.resourceUri, 0, sizeof response.resourceUri);\r
 \r
     OCRepPayload* payload = OCRepPayloadCreate();\r
-    //response.payload = OCRepPayloadCreate();\r
     if (!payload)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));\r
+        NS_LOG(ERROR, "payload is NULL");\r
         return NS_ERROR;\r
     }\r
 \r
@@ -90,30 +96,26 @@ NSResult NSSendAccessPolicyResponse(OCEntityHandlerRequest *entityHandlerRequest
     // Send Response\r
     if (OCDoResponse(&response) != OC_STACK_OK)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Fail to send response"));\r
+        NS_LOG(ERROR, "Fail to AccessPolicy send response");\r
         return NS_ERROR;\r
     }\r
     OCRepPayloadDestroy(payload);\r
+    NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
 \r
+    NS_LOG(DEBUG, "NSSendAccessPolicyResponse - OUT");\r
     return NS_OK;\r
 }\r
 \r
 void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResourceType resourceType)\r
 {\r
-\r
-    OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to subscription process");\r
-\r
-    printf("state = 0 \n");\r
-\r
-    // write consumer info to cache\r
+    NS_LOG(DEBUG, "NSHandleSubscription - IN");\r
 \r
     if (resourceType == NS_RESOURCE_MESSAGE)\r
     {\r
-        int obId = entityHandlerRequest->obsInfo.obsId;\r
-\r
-        NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement));\r
+        NS_LOG(DEBUG, "resourceType == NS_RESOURCE_MESSAGE");\r
+        NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
+        NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
 \r
-        NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData));\r
         subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
         subData->isWhite = false;\r
         subData->messageObId = entityHandlerRequest->obsInfo.obsId;\r
@@ -122,34 +124,32 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
         element->data = (void*) subData;\r
         element->next = NULL;\r
 \r
-        printf("NS_ message ob Id = %d\n", subData->messageObId);\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
-            printf("Cache Write Error\n");\r
+            NS_LOG(DEBUG, "fail to write cache");\r
         }\r
 \r
         if (NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\r
         {\r
-            // OCDevAddr --> NSConsumer\r
-            printf("before NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");\r
+            NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_PROVIDER");\r
             NSAskAcceptanceToUser(entityHandlerRequest);\r
-            printf("After NSGetSubscriptionAccepter() == NS_ACCEPTER_PROVIDER)\n");\r
         }\r
         else if (NSGetSubscriptionAccepter() == NS_ACCEPTER_CONSUMER)\r
         {\r
-            printf("state = 0 - 3\n");\r
-            NSSendSubscriptionResponse(entityHandlerRequest, NS_ACCEPTER_CONSUMER, true);\r
+            NS_LOG(DEBUG, "NSGetSubscriptionAccepter == NS_ACCEPTER_CONSUMER");\r
+            NSSendSubscriptionResponse(entityHandlerRequest, true);\r
         }\r
     }\r
     else if (resourceType == NS_RESOURCE_SYNC)\r
     {\r
-        int obId = entityHandlerRequest->obsInfo.obsId;\r
+        NS_LOG(DEBUG, "resourceType == NS_RESOURCE_SYNC");\r
+        NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
 \r
-        NSCacheElement * element = (NSCacheElement *) malloc(sizeof(NSCacheElement));\r
+        NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
 \r
-        NSCacheSubData * subData = (NSCacheSubData *) malloc(sizeof(NSCacheSubData));\r
-        printf("NS_ entityHandlerRequest->devAddr.addr = %s\n", entityHandlerRequest->devAddr.addr);\r
         subData->id = OICStrdup(entityHandlerRequest->devAddr.addr);\r
         subData->isWhite = false;\r
         subData->syncObId = entityHandlerRequest->obsInfo.obsId;\r
@@ -158,23 +158,21 @@ void NSHandleSubscription(OCEntityHandlerRequest *entityHandlerRequest, NSResour
         element->data = (void*) subData;\r
         element->next = NULL;\r
 \r
-        printf("NS_ sync ob Id = %d\n", subData->syncObId);\r
+        NS_LOG_V(DEBUG, "SubList IP[ID] = [%s]", subData->id);\r
+        NS_LOG_V(DEBUG, "SubList sync observation ID = [%d]", subData->syncObId);\r
 \r
         if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
-            printf("Cache Write Error\n");\r
+            NS_LOG(ERROR, "Fail to write cache");\r
         }\r
     }\r
+\r
+    NS_LOG(DEBUG, "NSHandleSubscription - OUT");\r
 }\r
 \r
 void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)\r
 {\r
-    OIC_LOG(INFO, SUBSCRIPTION_TAG, "Start to unsubscription process");\r
-\r
-    // ProcessObserveUnregister (entityHandlerRequest);\r
-\r
-    // write consumer info to cache\r
-    int obId = entityHandlerRequest->obsInfo.obsId;\r
+    NS_LOG(DEBUG, "NSHandleUnsubscription - IN");\r
 \r
     NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
     NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
@@ -185,34 +183,42 @@ void NSHandleUnsubscription(OCEntityHandlerRequest *entityHandlerRequest)
     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 observation ID = [%d]", subData->syncObId);\r
+\r
     if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
+        NS_LOG(ERROR, "fail to write consumer white list");\r
     }\r
+\r
+    NS_LOG(DEBUG, "NSHandleUnsubscription - IN");\r
+    NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
 }\r
 \r
 void NSAskAcceptanceToUser(OCEntityHandlerRequest *entityHandlerRequest)\r
 {\r
-    OIC_LOG(DEBUG, SUBSCRIPTION_TAG, "Ask for user to Allow or Deny");\r
+    NS_LOG(DEBUG, "NSAskAcceptanceToUser - IN");\r
 \r
     NSPushQueue(RESPONSE_SCHEDULER, TASK_CB_SUBSCRIPTION, entityHandlerRequest);\r
-    // one queue is required for response callback to ask subscription allowance\r
+\r
+    NS_LOG(DEBUG, "NSAskAcceptanceToUser - OUT");\r
 }\r
 \r
 NSResult NSSendResponse(const char * id, bool accepted)\r
 {\r
+    NS_LOG(DEBUG, "NSSendResponse - IN");\r
 \r
     OCRepPayload* payload = OCRepPayloadCreate();\r
     if (!payload)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, PCF("Failed to allocate Payload"));\r
+        NS_LOG(ERROR, "fail to create playload");\r
         return NS_ERROR;\r
     }\r
 \r
     OCResourceHandle rHandle;\r
     if (NSPutMessageResource(NULL, &rHandle) != NS_OK)\r
     {\r
-        OIC_LOG(ERROR, NOTIFICATION_TAG, PCF("Fail to put notification resource"));\r
+        NS_LOG(ERROR, "Fail to put notification resource");\r
         return NS_ERROR;\r
     }\r
 \r
@@ -221,39 +227,41 @@ NSResult NSSendResponse(const char * id, bool accepted)
     OCRepPayloadSetPropBool(payload, NS_ATTRIBUTE_ACCPETANCE, accepted);\r
 \r
     NSCacheElement * element = NSCacheRead(consumerSubList, id);\r
-    NSCacheSubData * subData = element->data;\r
 \r
-    if (OCNotifyListOfObservers(rHandle, &subData->messageObId, 1, payload, OC_HIGH_QOS)\r
+    if(element == NULL)\r
+    {\r
+        NS_LOG(ERROR, "element is NULL");\r
+        return NS_ERROR;\r
+    }\r
+    NSCacheSubData * subData = (NSCacheSubData*) element->data;\r
+\r
+    if (OCNotifyListOfObservers(rHandle, (OCObservationId*)&subData->messageObId, 1, payload, OC_HIGH_QOS)\r
             != OC_STACK_OK)\r
     {\r
-        OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to send Acceptance");\r
+        NS_LOG(ERROR, "fail to send Acceptance");\r
         OCRepPayloadDestroy(payload);\r
         return NS_ERROR;\r
 \r
     }\r
     OCRepPayloadDestroy(payload);\r
 \r
+    NS_LOG(DEBUG, "NSSendResponse - OUT");\r
     return NS_OK;\r
 }\r
 \r
-NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest,\r
-        NSAccessPolicy accepter, bool accepted)\r
+NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest, bool accepted)\r
 {\r
-    OIC_LOG(INFO, SUBSCRIPTION_TAG, "Send ACCEPT message to consumer");\r
+    NS_LOG(DEBUG, "NSSendSubscriptionResponse - IN");\r
 \r
     if (!entityHandlerRequest)\r
     {\r
-        OIC_LOG (ERROR, LISTENER_TAG, "Invalid request pointer");\r
+        NS_LOG(ERROR, "Invalid request pointer");\r
         return OC_EH_ERROR;\r
     }\r
 \r
-    // write consumer info to cache\r
-\r
-    printf("accepted 1 \n");\r
     if (accepted)\r
     {\r
-        int obId = entityHandlerRequest->obsInfo.obsId;\r
-\r
+        NS_LOG(DEBUG, "accepted is true");\r
         NSCacheElement * element = (NSCacheElement *) OICMalloc(sizeof(NSCacheElement));\r
 \r
         NSCacheSubData * subData = (NSCacheSubData *) OICMalloc(sizeof(NSCacheSubData));\r
@@ -266,22 +274,22 @@ NSResult NSSendSubscriptionResponse(OCEntityHandlerRequest *entityHandlerRequest
 \r
         if (NSCacheWrite(consumerSubList, element) != NS_OK)\r
         {\r
-            OIC_LOG(ERROR, SUBSCRIPTION_TAG, "fail to write consumer white list");\r
+            NS_LOG(ERROR, "fail to write consumer white list");\r
         }\r
     }\r
 \r
-    NSSendResponse(&entityHandlerRequest->obsInfo.obsId, accepted);\r
+    NSSendResponse(entityHandlerRequest->devAddr.addr, accepted);\r
+    NSFreeOCEntityHandlerRequest(entityHandlerRequest);\r
 \r
+    NS_LOG(DEBUG, "NSSendSubscriptionResponse - OUT");\r
     return NS_OK;\r
-\r
 }\r
 \r
 void * NSSubScriptionSchedule(void *ptr)\r
 {\r
-\r
     if (ptr == NULL)\r
     {\r
-        OIC_LOG(INFO, SUBSCRIPTION_TAG, "Create NSSubscriptionSchedule");\r
+        NS_LOG(DEBUG, "Create NSSubScriptionSchedule");\r
     }\r
 \r
     while (NSIsRunning[SUBSCRIPTION_SCHEDULER])\r
@@ -296,63 +304,56 @@ void * NSSubScriptionSchedule(void *ptr)
 \r
             switch (node->taskType)\r
             {\r
-\r
                 case TASK_SEND_POLICY:\r
-\r
+                    NS_LOG(DEBUG, "CASE TASK_SEND_POLICY : ");\r
                     NSSendAccessPolicyResponse((OCEntityHandlerRequest*) node->taskData);\r
                     break;\r
 \r
                 case TASK_RECV_SUBSCRIPTION:\r
-                    printf("before TASK_RECV_SUBSCRIPTION:\n");\r
+                    NS_LOG(DEBUG, "CASE TASK_RECV_SUBSCRIPTION : ");\r
                     NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,\r
                             NS_RESOURCE_MESSAGE);\r
-                    printf("after TASK_RECV_SUBSCRIPTION:\n");\r
                     break;\r
 \r
                 case TASK_RECV_UNSUBSCRIPTION:\r
-\r
+                    NS_LOG(DEBUG, "CASE TASK_RECV_UNSUBSCRIPTION : ");\r
                     NSHandleUnsubscription((OCEntityHandlerRequest*) node->taskData);\r
                     break;\r
 \r
                 case TASK_SEND_ALLOW:\r
                 {\r
+                    NS_LOG(DEBUG, "CASE TASK_SEND_ALLOW : ");\r
                     NSConsumer * consumer = (NSConsumer *) node->taskData;\r
-                    int * pObId = (int *) consumer->mUserData;\r
-\r
-                    NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData));\r
-                    subData->id = OICStrdup(consumer->mId);\r
-                    subData->isWhite = true;\r
-                    subData->messageObId = 0;\r
-                    subData->syncObId = 0;\r
 \r
-                    NSCacheUpdateSubScriptionState(consumerSubList, subData);\r
-\r
-                    printf("observer ID = %d\n", *pObId);\r
-\r
-                    NSSendResponse(consumer->mId, true);\r
+                    char * id = OICStrdup(consumer->mDeviceId);\r
 \r
+                    NSCacheUpdateSubScriptionState(consumerSubList, id, true);\r
+                    NSSendResponse(id, true);\r
+                    OICFree(id);\r
+                    NSFreeConsumer(consumer);\r
                     break;\r
                 }\r
                 case TASK_SEND_DENY:\r
                 {\r
+                    NS_LOG(DEBUG, "CASE TASK_SEND_DENY : ");\r
                     NSConsumer * consumer = (NSConsumer *) node->taskData;\r
-                    int * pObId = (int *) consumer->mUserData;\r
 \r
-                    NSCacheSubData * subData = (NSCacheMsgData *) OICMalloc(sizeof(NSCacheSubData));\r
-                    subData->id = OICStrdup(consumer->mId);\r
-                    subData->isWhite = false;\r
-                    subData->messageObId = 0;\r
-                    subData->syncObId = 0;\r
+                    char * id = OICStrdup(consumer->mDeviceId);\r
+\r
+                    NSCacheUpdateSubScriptionState(consumerSubList, id, false);\r
+                    NSSendResponse(id, false);\r
+                    OICFree(id);\r
+                    NSFreeConsumer(consumer);\r
 \r
-                    NSCacheUpdateSubScriptionState(consumerSubList, subData);\r
-                    printf("observer ID = %d\n", *pObId);\r
-                    NSSendResponse(consumer->mId, false);\r
                     break;\r
                 }\r
                 case TASK_SYNC_SUBSCRIPTION:\r
+                    NS_LOG(DEBUG, "CASE TASK_SYNC_SUBSCRIPTION : ");\r
                     NSHandleSubscription((OCEntityHandlerRequest*) node->taskData,\r
                             NS_RESOURCE_SYNC);\r
                     break;\r
+                default:\r
+                    break;\r
 \r
             }\r
             OICFree(node);\r
@@ -361,5 +362,6 @@ void * NSSubScriptionSchedule(void *ptr)
         pthread_mutex_unlock(&NSMutex[SUBSCRIPTION_SCHEDULER]);\r
 \r
     }\r
+    NS_LOG(INFO, "Destroy NSSubScriptionSchedule");\r
     return NULL;\r
 }\r