added struct, enum and modified struct, enum
[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         OICFree(obj->messageId);
95         obj->messageId = NULL;
96     }
97
98     if (obj->title)
99     {
100         OICFree(obj->title);
101         obj->title = NULL;
102     }
103
104     if (obj->contentText)
105     {
106         OICFree(obj->contentText);
107         obj->contentText = NULL;
108     }
109
110     if (obj->sourceName)
111     {
112         OICFree(obj->sourceName);
113         obj->sourceName = NULL;
114     }
115
116     OICFree(obj);
117
118     return NS_OK;
119 }
120
121 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
122 {
123     NSMessage * newMsg = NULL;
124
125     if(copyMsg == NULL)
126     {
127         NS_LOG(ERROR, "Copy Msg is NULL");
128         return NULL;
129     }
130
131     newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
132     newMsg->contentText = NULL;
133     newMsg->messageId = NULL;
134     newMsg->sourceName = NULL;
135     newMsg->title = NULL;
136
137     if (copyMsg->messageId)
138     {
139         newMsg->messageId = OICStrdup(copyMsg->messageId);
140     }
141
142     if (copyMsg->title)
143     {
144         newMsg->title = OICStrdup(copyMsg->title);
145     }
146
147     if (copyMsg->contentText)
148     {
149         newMsg->contentText = OICStrdup(copyMsg->contentText);
150     }
151
152     if (copyMsg->sourceName)
153     {
154        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
155     }
156
157     return newMsg;
158 }
159
160 NSResult NSFreeSync(NSSyncInfo * obj)
161 {
162     if (!obj)
163     {
164         return NS_ERROR;
165     }
166
167     if (obj->messageId)
168     {
169         OICFree(obj->messageId);
170         obj->messageId = NULL;
171     }
172
173     if (obj->providerId)
174     {
175         NS_LOG_V(DEBUG, "obj->mSourceid = %s", obj->providerId);
176         OICFree(obj->providerId);
177         obj->providerId = NULL;
178     }
179
180     OICFree(obj);
181
182     return NS_OK;
183 }
184
185 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
186 {
187     NSSyncInfo * newMsg = NULL;
188
189     if(copyMsg == NULL)
190     {
191         NS_LOG(ERROR, "Copy Msg is NULL");
192         return NULL;
193     }
194
195     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
196     newMsg->messageId = NULL;
197     newMsg->providerId = NULL;
198     newMsg->state = -1;
199
200     if (copyMsg->messageId)
201     {
202         newMsg->messageId = OICStrdup(copyMsg->messageId);
203     }
204
205     if (copyMsg->providerId)
206     {
207         newMsg->providerId = OICStrdup(copyMsg->providerId);
208     }
209
210     newMsg->state = copyMsg->state;
211
212     return newMsg;
213 }
214
215 NSResult NSFreeConsumer(NSConsumer * obj)
216 {
217     if (!obj)
218     {
219         return NS_ERROR;
220     }
221
222     if (obj->mAddress)
223     {
224         OICFree(obj->mAddress);
225         obj->mAddress = NULL;
226     }
227
228     if (obj->mDeviceId)
229     {
230         OICFree(obj->mDeviceId);
231         obj->mDeviceId = NULL;
232     }
233
234     OICFree(obj);
235
236     return NS_OK;
237 }
238
239 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
240 {
241     NSConsumer * newMsg = NULL;
242
243     if(copyMsg == NULL)
244     {
245         NS_LOG(ERROR, "Copy Msg is NULL");
246         return NULL;
247     }
248
249     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
250     newMsg->mAddress = NULL;
251     newMsg->mDeviceId = NULL;
252
253     if (copyMsg->mAddress)
254     {
255         newMsg->mAddress = OICStrdup(copyMsg->mAddress);
256     }
257
258     if (copyMsg->mDeviceId)
259     {
260         newMsg->mDeviceId = OICStrdup(copyMsg->mDeviceId);
261     }
262
263     return newMsg;
264 }
265
266 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
267         const char * copyString)
268 {
269     if(copyString)
270     {
271         OCRepPayloadSetPropString(*msgPayload, name, copyString);
272     }
273     else
274     {
275         OCRepPayloadSetNull(*msgPayload, name);
276     }
277 }
278
279
280 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
281 {
282     NS_LOG(DEBUG, "NSBuildOICNotificationSync - IN");
283
284     if(!payload)
285     {
286         return NULL;
287     }
288     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
289     if (!retSync)
290     {
291         return NULL;
292     }
293
294     retSync->messageId = NULL;
295     retSync->state = NS_SYNC_READ;
296
297     OCRepPayload * repPayload = (OCRepPayload *)payload;
298     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_MESSAGE_ID, &retSync->messageId))
299     {
300         OICFree(retSync);
301         return NULL;
302     }
303     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &retSync->providerId))
304     {
305         OICFree(retSync);
306         return NULL;
307     }
308     int64_t state;
309     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
310     {
311         OICFree(retSync->messageId);
312         OICFree(retSync);
313         return NULL;
314     }
315
316     retSync->state = (NSSyncType) state;
317
318     NS_LOG_V(DEBUG, "Sync ID : %s", retSync->messageId);
319     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
320
321     NS_LOG(DEBUG, "NSBuildOICNotificationSync - OUT");
322
323     return retSync;
324 }