Updated sturcture of consumer.
[platform/upstream/iotivity.git] / service / notification / src / common / NSUtil.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 "NSUtil.h"
22
23 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
24 {
25     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
26
27     OCEntityHandlerRequest *copyOfRequest =
28             (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
29
30     if (copyOfRequest)
31     {
32         // Do shallow copy
33         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
34
35
36         if (copyOfRequest->query)
37         {
38             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
39             if(!copyOfRequest->query)
40             {
41                 NS_LOG(ERROR, "Copy failed due to allocation failure");
42                 OICFree(copyOfRequest);
43                 return NULL;
44             }
45         }
46
47         if (entityHandlerRequest->payload)
48         {
49             copyOfRequest->payload = (OCPayload *)
50                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
51         }
52
53         // Ignore vendor specific header options for example
54         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
55         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
56     }
57
58     if (copyOfRequest)
59     {
60         NS_LOG(DEBUG, "Copied client request");
61     }
62     else
63     {
64         NS_LOG(DEBUG, "Error copying client request");
65     }
66
67     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
68
69     return copyOfRequest;
70 }
71
72 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
73 {
74     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
75
76     OICFree(entityHandlerRequest->query);
77     OCPayloadDestroy(entityHandlerRequest->payload);
78     OICFree(entityHandlerRequest);
79
80     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
81
82     return NS_OK;
83 }
84
85 NSResult NSFreeMessage(NSMessage * obj)
86 {
87     if (!obj)
88     {
89         return NS_ERROR;
90     }
91
92     if (obj->messageId)
93     {
94         obj->messageId = 0;
95     }
96
97     if (obj->title)
98     {
99         OICFree(obj->title);
100         obj->title = NULL;
101     }
102
103     if (obj->contentText)
104     {
105         OICFree(obj->contentText);
106         obj->contentText = NULL;
107     }
108
109     if (obj->sourceName)
110     {
111         OICFree(obj->sourceName);
112         obj->sourceName = NULL;
113     }
114
115     OICFree(obj);
116
117     return NS_OK;
118 }
119
120 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
121 {
122     NSMessage * newMsg = NULL;
123
124     if(copyMsg == NULL)
125     {
126         NS_LOG(ERROR, "Copy Msg is NULL");
127         return NULL;
128     }
129
130     newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
131     newMsg->contentText = NULL;
132     newMsg->messageId = 0;
133     newMsg->sourceName = NULL;
134     newMsg->title = NULL;
135
136     if (copyMsg->messageId)
137     {
138         newMsg->messageId = copyMsg->messageId;
139     }
140
141     if (copyMsg->title)
142     {
143         newMsg->title = OICStrdup(copyMsg->title);
144     }
145
146     if (copyMsg->contentText)
147     {
148         newMsg->contentText = OICStrdup(copyMsg->contentText);
149     }
150
151     if (copyMsg->sourceName)
152     {
153        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
154     }
155
156     return newMsg;
157 }
158
159 NSResult NSFreeSync(NSSyncInfo * obj)
160 {
161     if (!obj)
162     {
163         return NS_ERROR;
164     }
165
166     if (obj->messageId)
167     {
168         obj->messageId = 0;
169     }
170
171     if (obj->providerId)
172     {
173         NS_LOG_V(DEBUG, "obj->mSourceid = %s", obj->providerId);
174         OICFree(obj->providerId);
175         obj->providerId = NULL;
176     }
177
178     OICFree(obj);
179
180     return NS_OK;
181 }
182
183 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
184 {
185     NSSyncInfo * newMsg = NULL;
186
187     if(copyMsg == NULL)
188     {
189         NS_LOG(ERROR, "Copy Msg is NULL");
190         return NULL;
191     }
192
193     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
194     newMsg->messageId = 0;
195     newMsg->providerId = NULL;
196     newMsg->state = -1;
197
198     if (copyMsg->messageId)
199     {
200         newMsg->messageId = copyMsg->messageId;
201     }
202
203     if (copyMsg->providerId)
204     {
205         newMsg->providerId = OICStrdup(copyMsg->providerId);
206     }
207
208     newMsg->state = copyMsg->state;
209
210     return newMsg;
211 }
212
213 NSResult NSFreeConsumer(NSConsumer * obj)
214 {
215     if (!obj)
216     {
217         return NS_ERROR;
218     }
219
220     if (obj->mAddress)
221     {
222         OICFree(obj->mAddress);
223         obj->mAddress = NULL;
224     }
225
226     if (obj->mDeviceId)
227     {
228         OICFree(obj->mDeviceId);
229         obj->mDeviceId = NULL;
230     }
231
232     OICFree(obj);
233
234     return NS_OK;
235 }
236
237 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
238 {
239     NSConsumer * newMsg = NULL;
240
241     if(copyMsg == NULL)
242     {
243         NS_LOG(ERROR, "Copy Msg is NULL");
244         return NULL;
245     }
246
247     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
248     newMsg->mAddress = NULL;
249     newMsg->mDeviceId = NULL;
250
251     if (copyMsg->mAddress)
252     {
253         newMsg->mAddress = OICStrdup(copyMsg->mAddress);
254     }
255
256     if (copyMsg->mDeviceId)
257     {
258         newMsg->mDeviceId = OICStrdup(copyMsg->mDeviceId);
259     }
260
261     return newMsg;
262 }
263
264 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
265         const char * copyString)
266 {
267     if(copyString)
268     {
269         OCRepPayloadSetPropString(*msgPayload, name, copyString);
270     }
271     else
272     {
273         OCRepPayloadSetNull(*msgPayload, name);
274     }
275 }
276
277
278 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
279 {
280     NS_LOG(DEBUG, "NSBuildOICNotificationSync - IN");
281
282     if(!payload)
283     {
284         return NULL;
285     }
286     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
287     if (!retSync)
288     {
289         return NULL;
290     }
291
292     retSync->messageId = 0;
293     retSync->state = NS_SYNC_READ;
294
295     OCRepPayload * repPayload = (OCRepPayload *)payload;
296     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
297     {
298         OICFree(retSync);
299         return NULL;
300     }
301     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &retSync->providerId))
302     {
303         OICFree(retSync);
304         return NULL;
305     }
306     int64_t state;
307     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
308     {
309         OICFree(retSync);
310         return NULL;
311     }
312
313     retSync->state = (NSSyncType) state;
314
315     NS_LOG_V(DEBUG, "Sync ID : %ld", retSync->messageId);
316     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
317
318     NS_LOG(DEBUG, "NSBuildOICNotificationSync - OUT");
319
320     return retSync;
321 }