Merge remote-tracking branch 'origin/master' into notification-service
[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         if (copyOfRequest->query)
36         {
37             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
38             if(!copyOfRequest->query)
39             {
40                 NS_LOG(ERROR, "Copy failed due to allocation failure");
41                 OICFree(copyOfRequest);
42                 return NULL;
43             }
44         }
45
46         if (entityHandlerRequest->payload)
47         {
48             copyOfRequest->payload = (OCPayload *)
49                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
50         }
51
52         // Ignore vendor specific header options for example
53         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
54         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
55     }
56
57     if (copyOfRequest)
58     {
59         NS_LOG(DEBUG, "Copied client request");
60     }
61     else
62     {
63         NS_LOG(DEBUG, "Error copying client request");
64     }
65
66     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
67     return copyOfRequest;
68 }
69
70 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
71 {
72     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
73
74     OICFree(entityHandlerRequest->query);
75     OCPayloadDestroy(entityHandlerRequest->payload);
76     OICFree(entityHandlerRequest);
77
78     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
79
80     return NS_OK;
81 }
82
83 NSResult NSFreeMessage(NSMessage * obj)
84 {
85     if (!obj)
86     {
87         return NS_ERROR;
88     }
89
90     obj->messageId = 0;
91     (obj->providerId)[0] = '\0';
92
93     NSFreeMalloc(&(obj->dateTime));
94     obj->ttl = 0;
95     NSFreeMalloc(&(obj->title));
96     NSFreeMalloc(&(obj->contentText));
97     NSFreeMalloc(&(obj->sourceName));
98     NSFreeMediaContents(obj->mediaContents);
99
100     OICFree(obj);
101
102     return NS_OK;
103 }
104
105 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
106 {
107     NSMessage * newMsg = NULL;
108
109     if(copyMsg == NULL)
110     {
111         NS_LOG(ERROR, "Copy Msg is NULL");
112         return NULL;
113     }
114
115     newMsg = NSInitializeMessage();
116
117     newMsg->messageId = copyMsg->messageId;
118     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
119
120     if(copyMsg->dateTime)
121     {
122         newMsg->dateTime = OICStrdup(copyMsg->dateTime);
123     }
124
125     newMsg->ttl = copyMsg->ttl;
126
127     if (copyMsg->title)
128     {
129         newMsg->title = OICStrdup(copyMsg->title);
130     }
131
132     if (copyMsg->contentText)
133     {
134         newMsg->contentText = OICStrdup(copyMsg->contentText);
135     }
136
137     if (copyMsg->sourceName)
138     {
139        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
140     }
141
142     if (copyMsg->mediaContents)
143     {
144        newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
145     }
146
147     return newMsg;
148 }
149
150 NSResult NSFreeSync(NSSyncInfo * obj)
151 {
152     if (!obj)
153     {
154         return NS_ERROR;
155     }
156
157     OICFree(obj);
158
159     return NS_OK;
160 }
161
162 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
163 {
164     NSSyncInfo * newMsg = NULL;
165
166     if(!copyMsg)
167     {
168         NS_LOG(ERROR, "Copy Msg is NULL");
169         return NULL;
170     }
171
172     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
173
174     if(!newMsg)
175     {
176         NS_LOG(ERROR, "newMsg is NULL");
177         return NULL;
178     }
179
180     newMsg->messageId = copyMsg->messageId;
181     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
182     newMsg->state = copyMsg->state;
183
184     return newMsg;
185 }
186
187 NSResult NSFreeConsumer(NSConsumer * obj)
188 {
189     if (!obj)
190     {
191         return NS_ERROR;
192     }
193
194     (obj->consumerId)[0] = '\0';
195
196     OICFree(obj);
197     obj = NULL;
198
199     return NS_OK;
200 }
201
202 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
203 {
204     NSConsumer * newMsg = NULL;
205
206     if(copyMsg == NULL)
207     {
208         NS_LOG(ERROR, "Copy Msg is NULL");
209         return NULL;
210     }
211
212     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
213
214     if(!newMsg)
215     {
216         NS_LOG(ERROR, "newMsg is NULL");
217         return NULL;
218     }
219
220     (newMsg->consumerId)[0] = '\0';
221
222     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
223
224     return newMsg;
225 }
226
227 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
228         const char * copyString)
229 {
230     if(copyString)
231     {
232         OCRepPayloadSetPropString(*msgPayload, name, copyString);
233     }
234 }
235
236 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
237         int64_t value)
238 {
239     if(value)
240     {
241         OCRepPayloadSetPropInt(*msgPayload, name, value);
242     }
243 }
244
245 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
246 {
247     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
248     char * providerId = NULL;
249     int64_t state = 0;
250
251     if(!payload)
252     {
253         return NULL;
254     }
255     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
256     if (!retSync)
257     {
258         return NULL;
259     }
260
261     retSync->messageId = 0;
262     retSync->state = NS_SYNC_READ;
263
264     OCRepPayload * repPayload = (OCRepPayload *)payload;
265     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
266     {
267         OICFree(retSync);
268         return NULL;
269     }
270
271     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
272     {
273         OICFree(retSync);
274         return NULL;
275     }
276
277     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
278     {
279         OICFree(retSync);
280         return NULL;
281     }
282
283     retSync->state = (NSSyncType) state;
284     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
285     OICFree(providerId);
286
287     NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
288     NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
289     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
290
291     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
292
293     return retSync;
294 }
295
296 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
297 {
298     uint8_t uuid[UUID_SIZE] = { 0, };
299
300     if (RAND_UUID_OK == OCGenerateUuid(uuid))
301     {
302         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
303         {
304             return NS_OK;
305         }
306     }
307     return NS_ERROR;
308 }
309
310 char * NSGetValueFromQuery(char *query, char * compareKey)
311 {
312     char *key = NULL;
313     char *value = NULL;
314     char *restOfQuery = NULL;
315     int numKeyValuePairsParsed = 0;
316
317     NS_LOG_V(INFO, "NS Query Params = %s", query);
318
319     if(!query || query[0] == '\0' || !strlen(query))
320     {
321         NS_LOG(ERROR, "query is null or \\0 or size is 0");
322         return NULL;
323     }
324
325     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
326
327     while(keyValuePair)
328     {
329         if (numKeyValuePairsParsed >= 2)
330         {
331             NS_LOG(ERROR, "More than 2 queries params in URI.");
332             return NULL;
333         }
334
335         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
336
337         if (!key || !value)
338         {
339             NS_LOG(ERROR, "More than 2 queries params in URI.");
340             return NULL;
341         }
342
343         if (strcmp(key, compareKey) == 0)
344         {
345             NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
346             return value;
347         }
348
349         ++numKeyValuePairsParsed;
350
351         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
352     }
353
354     return NULL;
355 }
356
357 NSResult NSFreeMalloc(char ** obj)
358 {
359     if(*obj)
360     {
361         OICFree(*obj);
362         *obj = NULL;
363         return NS_OK;
364     }
365
366     return NS_FAIL;
367 }
368
369 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
370 {
371     if(!copyObj)
372     {
373         return NULL;
374     }
375
376     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
377
378     if(!newObj)
379     {
380         NS_LOG(ERROR, "contents newObj is NULL");
381         return NULL;
382     }
383
384     if(copyObj->iconImage)
385     {
386         newObj->iconImage = OICStrdup(copyObj->iconImage);
387     }
388
389     return newObj;
390 }
391
392 NSResult NSFreeMediaContents(NSMediaContents * obj)
393 {
394     if(!obj)
395     {
396         return NS_FAIL;
397     }
398
399     NSFreeMalloc(&(obj->iconImage));
400     OICFree(obj);
401
402     return NS_OK;
403 }
404
405 NSMessage * NSInitializeMessage()
406 {
407     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
408
409     if(!msg)
410     {
411         NS_LOG(ERROR, "Msg is NULL");
412         return NULL;
413     }
414
415     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
416     (msg->providerId)[0] = '\0';
417     msg->type = 0;
418     msg->dateTime = NULL;
419     msg->ttl = 0;
420     msg->title = NULL;
421     msg->contentText = NULL;
422     msg->sourceName = NULL;
423     msg->mediaContents = NULL;
424
425     return msg;
426 }