Merge branch 'master' into notification-service
[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 = *(NSGetCacheList());
76     if (!cache)
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
86     NSResult ret = NS_ERROR;
87     NS_LOG_V(DEBUG, "Receive Event : %d", (int)task->taskType);
88     switch (task->taskType)
89     {
90         case TASK_CONSUMER_RECV_MESSAGE:
91         {
92             NS_LOG(DEBUG, "Receive New Notification");
93
94             ret = NSCacheUpdate(cache, task, Notification);
95             NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
96             break;
97         }
98         case TASK_RECV_READ:
99         {
100             NS_LOG(DEBUG, "Receive Read Notification");
101
102             ret = NSCacheUpdate(cache, task, Read);
103             NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
104             break;
105         }
106         case TASK_RECV_DISMISS:
107         {
108             NS_LOG(DEBUG, "Receive Dismiss Notification");
109
110             ret = NSCacheUpdate(cache, task, Dismiss);
111             NS_VERTIFY_NOT_NULL_V(ret == NS_OK ? (void *) 1 : NULL);
112             break;
113         }
114         default :
115         {
116             NS_LOG(ERROR, "Unknown TASK Type");
117             return ;
118         }
119     }
120 }