Modified Error handle of consumer.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerInternalTaskController.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "NSConstants.h"
22 #include "NSConsumerCommon.h"
23 #include "NSConsumerInternalTaskController.h"
24 #include "NSStructs.h"
25
26 #include "oic_malloc.h"
27
28 NSCacheList ** NSGetCacheList()
29 {
30     static NSCacheList * cache = NULL;
31     return & cache;
32 }
33
34 void NSSetCacheList(NSCacheList * cache)
35 {
36     *(NSGetCacheList()) = cache;
37 }
38
39 void NSDestroyCacheList()
40 {
41     NSCacheList * cache;
42     cache = *(NSGetCacheList());
43     if (cache)
44     {
45         NSStorageDestroy(cache);
46     }
47 }
48
49 NSResult NSCacheUpdate(NSCacheList * cache, NSTask * task, NSConsumerMessageTypes type)
50 {
51     NSMessage_consumer * noti = (NSMessage_consumer *) task->taskData;
52     noti->type = type;
53
54     NSCacheElement * obj = (NSCacheElement *)OICMalloc(sizeof(NSCacheElement));
55     NS_VERTIFY_NOT_NULL(obj, NS_ERROR);
56
57     obj->data = (NSCacheData *) noti;
58     obj->next = NULL;
59
60     NS_LOG(DEBUG, "try to write to storage");
61     NSResult ret = NSStorageWrite(cache, obj);
62     NS_VERTIFY_NOT_NULL_WITH_POST_CLEANING(ret == NS_OK ? (void *) 1 : NULL,
63             NS_ERROR, NSRemoveMessage(noti));
64
65     NSRemoveMessage(noti);
66     OICFree(obj);
67
68     return NS_OK;
69 }
70
71 void NSConsumerSubscriptionTaskProcessing(NSTask * task)
72 {
73     NS_VERTIFY_NOT_NULL_V(task);
74
75     NSCacheList * cache;
76     if (!*(NSGetCacheList()))
77     {
78         NS_LOG(DEBUG, "Cache Init");
79         cache = NSStorageCreate();
80         NS_VERTIFY_NOT_NULL_V(cache);
81
82         cache->cacheType = NS_CONSUMER_CACHE_MESSAGE;
83         NSSetCacheList(cache);
84     }
85     cache = *(NSGetCacheList());
86
87     NSResult ret = NS_ERROR;
88     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
89     switch (task->taskType)
90     {
91         case TASK_CONSUMER_RECV_MESSAGE:
92         {
93             NS_LOG(DEBUG, "Receive New Notification");
94
95             ret = NSCacheUpdate(cache, task, Notification);
96             NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
97             break;
98         }
99         case TASK_RECV_READ:
100         {
101             NS_LOG(DEBUG, "Receive Read Notification");
102
103             ret = NSCacheUpdate(cache, task, Read);
104             NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
105             break;
106         }
107         case TASK_RECV_DISMISS:
108         {
109             NS_LOG(DEBUG, "Receive Dismiss Notification");
110
111             ret = NSCacheUpdate(cache, task, Dismiss);
112             NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
113             break;
114         }
115         default :
116         {
117             NS_LOG(ERROR, "Unknown TASK Type");
118             return ;
119         }
120     }
121 }