Added Initial version of notification.
[platform/upstream/iotivity.git] / service / notification / src / consumer / NSConsumerCache.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 "NSConsumerCache.h"
22 #include "NSConsumerCommon.h"
23
24 #include "stdlib.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
27
28 NSCacheList * NSConsumerCacheInit()
29 {
30     NSCacheList * retNSCacheList = (NSCacheList *)OICMalloc(sizeof(NSCacheList));
31     if (!retNSCacheList)
32     {
33         return NULL;
34     }
35
36     retNSCacheList->head = NULL;
37     retNSCacheList->last = NULL;
38
39     return retNSCacheList;
40 }
41
42 NSResult NSConsumerCacheDestroy(NSCacheList * list)
43 {
44     NSCacheObject * iter = list->head;
45
46     NSCacheObject * next = NULL;
47     while (iter)
48     {
49         next = (NSCacheObject *)iter->next;
50         if (NS_OK != NSConsumerCacheDelete(list, iter))
51         {
52             return NS_ERROR;
53         }
54         iter = next;
55     }
56
57     OICFree((NSCacheList *) list);
58
59     return NS_OK;
60 }
61
62 NSResult NSConsumerCacheInsert(NSCacheList * list, NSCacheObject * newObj)
63 {
64     NSCacheObject * obj = (NSCacheObject *)OICMalloc(sizeof(NSCacheObject));
65     if (!obj)
66     {
67         NS_CONSUMER_LOG(ERROR, "Fail to Create New Object");
68         return NS_ERROR;
69     }
70
71     NSMessage_consumer * msgObj = (NSMessage_consumer *) OICMalloc(sizeof(NSMessage_consumer));
72     NSMessage_consumer * newMsgObj = (NSMessage_consumer *) newObj->data;
73
74     NS_CONSUMER_LOG_V(DEBUG, "Title: %s", newMsgObj->mTitle);
75     NS_CONSUMER_LOG_V(DEBUG, "ID: %s", newMsgObj->mId);
76     NS_CONSUMER_LOG_V(DEBUG, "TEXT: %s", newMsgObj->mContentText);
77
78     msgObj->mTitle = OICStrdup(newMsgObj->mTitle);
79     msgObj->mId = OICStrdup(newMsgObj->mId);
80     msgObj->mContentText = OICStrdup(newMsgObj->mContentText);
81
82     if (!msgObj->mId)
83     {
84         NS_CONSUMER_LOG(ERROR, "Notification Attributes copy fail");
85     }
86
87     msgObj->addr = (OCDevAddr *)OICMalloc(sizeof(OCDevAddr));
88     if (!msgObj->addr)
89     {
90         NS_CONSUMER_LOG(ERROR, "OCDevAddr allocation is failed.");
91     }
92
93     memcpy(msgObj->addr, newMsgObj->addr, sizeof(OCDevAddr));
94     msgObj->type = newMsgObj->type;
95
96     //obj->data = (NSCacheData *)OICMalloc(sizeof(NSCacheData));
97     obj->data = (NSCacheData *) msgObj;
98     //memcpy(obj->data, (NSCacheData *)msgObj, sizeof(NSCacheData));
99     obj->next = NULL;
100
101     msgObj = NULL;
102     msgObj = (NSMessage_consumer *) obj->data;
103
104     if (!list->head)
105     {
106         list->head = obj;
107         return NS_OK;
108     }
109
110     NSCacheObject * iter = list->head;
111     while (iter)
112     {
113         if (iter == obj)
114         {
115             return NS_ERROR;
116         }
117         iter = (NSCacheObject *) iter->next;
118     }
119
120     if (list->last)
121     {
122         (list->last)->next = obj;
123     }
124
125     list->last = obj;
126
127     return NS_OK;
128 }
129
130 NSResult NSConsumerCacheDelete(NSCacheList * list, NSCacheObject * delObj) // Free?
131 {
132     NSCacheObject * beDelete;
133     if (list->head == delObj)
134     {
135         beDelete = list->head;
136         list->head = (NSCacheObject *) list->head->next;
137         return NS_OK;
138     }
139
140     NSCacheObject * pre = list->head;
141     beDelete = (NSCacheObject *) pre->next;
142     while (beDelete)
143     {
144         if (beDelete == delObj)
145         {
146             if (beDelete == list->last)
147             {
148                 pre->next = NULL;
149                 list->last = pre;
150             }
151             else
152             {
153                 pre->next = beDelete->next;
154             }
155             OICFree((NSCacheObject *) beDelete);
156             break;
157         }
158         pre = beDelete;
159         beDelete = (NSCacheObject *) beDelete->next;
160     }
161
162     return NS_OK;
163 }
164
165 NSCacheObject * NSConsumerCacheFind(NSCacheList * list, char * findId)
166 {
167
168     NS_CONSUMER_LOG_V(DEBUG, "findID: %s", findId);
169     NSCacheObject * retObj = list->head;
170     if (!retObj)
171     {
172         NS_CONSUMER_LOG(DEBUG, "findcache is null");
173         return retObj;
174     }
175
176     NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
177     NS_CONSUMER_LOG_V(DEBUG, "objmID: %s", obj->mId);
178
179     while (retObj)
180     {
181         NSMessage_consumer * obj = (NSMessage_consumer *) retObj->data;
182         NS_CONSUMER_LOG_V(DEBUG, "objmID: %s", obj->mId);
183         if (!strcmp(obj->mId, findId))
184         {
185             return retObj;
186         }
187         retObj = (NSCacheObject *) retObj->next;
188     }
189
190     return retObj;
191 }
192
193 NSResult NSConsumerCacheWrite(NSCacheList * list, NSCacheObject * writeObj)
194 {
195     NSCacheObject * beModify = NSConsumerCacheFind(list, ((NSMessage_consumer *) writeObj->data)->mId);
196     if (!beModify)
197     {
198         return NS_ERROR;
199     }
200
201     NSMessage_consumer * msgObj = (NSMessage_consumer *) beModify->data;
202     NSMessage_consumer * msgWriteObj = (NSMessage_consumer *) writeObj->data;
203
204     if (msgWriteObj->mId)
205     {
206         strcpy(msgObj->mId, msgWriteObj->mId);
207     }
208     if (msgWriteObj->mTitle)
209     {
210         strcpy(msgObj->mTitle, msgWriteObj->mTitle);
211     }
212     if (msgWriteObj->mContentText)
213     {
214         strcpy(msgObj->mContentText, msgWriteObj->mContentText);
215     }
216
217     memcpy(msgObj->addr, msgWriteObj->addr, sizeof(OCDevAddr));
218     msgObj->type = msgWriteObj->type;
219
220     return NS_OK;
221 }
222
223 NSCacheObject * NSConsumerCacheRead(NSCacheList * list, NSCacheObject * readObj)
224 {
225     NSCacheObject * retObj = NSConsumerCacheFind(list, ((NSMessage_consumer *) readObj->data)->mId);
226     if (!retObj)
227     {
228         return NULL;
229     }
230
231     return retObj;
232 }
233
234
235