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     NSFreeMalloc(&(obj->topic));
99     NSFreeMediaContents(obj->mediaContents);
100
101     OICFree(obj);
102
103     return NS_OK;
104 }
105
106 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
107 {
108     NSMessage * newMsg = NULL;
109
110     if(copyMsg == NULL)
111     {
112         NS_LOG(ERROR, "Copy Msg is NULL");
113         return NULL;
114     }
115
116     newMsg = NSInitializeMessage();
117
118     newMsg->messageId = copyMsg->messageId;
119     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
120
121     if(copyMsg->dateTime)
122     {
123         newMsg->dateTime = OICStrdup(copyMsg->dateTime);
124     }
125
126     newMsg->ttl = copyMsg->ttl;
127
128     if (copyMsg->title)
129     {
130         newMsg->title = OICStrdup(copyMsg->title);
131     }
132
133     if (copyMsg->contentText)
134     {
135         newMsg->contentText = OICStrdup(copyMsg->contentText);
136     }
137
138     if (copyMsg->sourceName)
139     {
140        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
141     }
142
143     if (copyMsg->mediaContents)
144     {
145        newMsg->mediaContents = NSDuplicateMediaContents(copyMsg->mediaContents);
146     }
147
148     if (copyMsg->topic)
149     {
150         newMsg->topic = OICStrdup(copyMsg->topic);
151     }
152
153     return newMsg;
154 }
155
156 NSResult NSFreeSync(NSSyncInfo * obj)
157 {
158     if (!obj)
159     {
160         return NS_ERROR;
161     }
162
163     OICFree(obj);
164
165     return NS_OK;
166 }
167
168 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
169 {
170     NSSyncInfo * newMsg = NULL;
171
172     if(!copyMsg)
173     {
174         NS_LOG(ERROR, "Copy Msg is NULL");
175         return NULL;
176     }
177
178     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
179
180     if(!newMsg)
181     {
182         NS_LOG(ERROR, "newMsg is NULL");
183         return NULL;
184     }
185
186     newMsg->messageId = copyMsg->messageId;
187     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
188     newMsg->state = copyMsg->state;
189
190     return newMsg;
191 }
192
193 NSResult NSFreeConsumer(NSConsumer * obj)
194 {
195     if (!obj)
196     {
197         return NS_ERROR;
198     }
199
200     (obj->consumerId)[0] = '\0';
201
202     OICFree(obj);
203     obj = NULL;
204
205     return NS_OK;
206 }
207
208 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
209 {
210     NSConsumer * newMsg = NULL;
211
212     if(copyMsg == NULL)
213     {
214         NS_LOG(ERROR, "Copy Msg is NULL");
215         return NULL;
216     }
217
218     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
219
220     if(!newMsg)
221     {
222         NS_LOG(ERROR, "newMsg is NULL");
223         return NULL;
224     }
225
226     (newMsg->consumerId)[0] = '\0';
227
228     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
229
230     return newMsg;
231 }
232
233 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
234         const char * copyString)
235 {
236     if(copyString)
237     {
238         OCRepPayloadSetPropString(*msgPayload, name, copyString);
239     }
240 }
241
242 void NSDuplicateSetPropertyInt(OCRepPayload** msgPayload, const char * name,
243         int64_t value)
244 {
245     if(value)
246     {
247         OCRepPayloadSetPropInt(*msgPayload, name, value);
248     }
249 }
250
251 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
252 {
253     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
254     char * providerId = NULL;
255     int64_t state = 0;
256
257     if(!payload)
258     {
259         return NULL;
260     }
261     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
262     if (!retSync)
263     {
264         return NULL;
265     }
266
267     retSync->messageId = 0;
268     retSync->state = NS_SYNC_READ;
269
270     OCRepPayload * repPayload = (OCRepPayload *)payload;
271     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
272     {
273         OICFree(retSync);
274         return NULL;
275     }
276
277     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
278     {
279         OICFree(retSync);
280         return NULL;
281     }
282
283     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
284     {
285         OICFree(retSync);
286         return NULL;
287     }
288
289     retSync->state = (NSSyncType) state;
290     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
291     OICFree(providerId);
292
293     NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
294     NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
295     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
296
297     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
298
299     return retSync;
300 }
301
302 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
303 {
304     uint8_t uuid[UUID_SIZE] = { 0, };
305
306     if (RAND_UUID_OK == OCGenerateUuid(uuid))
307     {
308         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
309         {
310             return NS_OK;
311         }
312     }
313     return NS_ERROR;
314 }
315
316 char * NSGetValueFromQuery(char *query, char * compareKey)
317 {
318     char *key = NULL;
319     char *value = NULL;
320     char *restOfQuery = NULL;
321     int numKeyValuePairsParsed = 0;
322
323     NS_LOG_V(INFO, "NS Query Params = %s", query);
324
325     if(!query || query[0] == '\0' || !strlen(query))
326     {
327         NS_LOG(ERROR, "query is null or \\0 or size is 0");
328         return NULL;
329     }
330
331     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
332
333     while(keyValuePair)
334     {
335         if (numKeyValuePairsParsed >= 2)
336         {
337             NS_LOG(ERROR, "More than 2 queries params in URI.");
338             return NULL;
339         }
340
341         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
342
343         if (!key || !value)
344         {
345             NS_LOG(ERROR, "More than 2 queries params in URI.");
346             return NULL;
347         }
348
349         if (strcmp(key, compareKey) == 0)
350         {
351             NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
352             return value;
353         }
354
355         ++numKeyValuePairsParsed;
356
357         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
358     }
359
360     return NULL;
361 }
362
363 NSResult NSFreeMalloc(char ** obj)
364 {
365     if(*obj)
366     {
367         OICFree(*obj);
368         *obj = NULL;
369         return NS_OK;
370     }
371
372     return NS_FAIL;
373 }
374
375 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
376 {
377     if(!copyObj)
378     {
379         return NULL;
380     }
381
382     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
383
384     if(!newObj)
385     {
386         NS_LOG(ERROR, "contents newObj is NULL");
387         return NULL;
388     }
389
390     if(copyObj->iconImage)
391     {
392         newObj->iconImage = OICStrdup(copyObj->iconImage);
393     }
394
395     return newObj;
396 }
397
398 NSResult NSFreeMediaContents(NSMediaContents * obj)
399 {
400     if(!obj)
401     {
402         return NS_FAIL;
403     }
404
405     NSFreeMalloc(&(obj->iconImage));
406     OICFree(obj);
407
408     return NS_OK;
409 }
410
411 NSMessage * NSInitializeMessage()
412 {
413     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
414
415     if(!msg)
416     {
417         NS_LOG(ERROR, "Msg is NULL");
418         return NULL;
419     }
420
421     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
422     (msg->providerId)[0] = '\0';
423     msg->type = 0;
424     msg->dateTime = NULL;
425     msg->ttl = 0;
426     msg->title = NULL;
427     msg->contentText = NULL;
428     msg->sourceName = NULL;
429     msg->mediaContents = NULL;
430     msg->topic = NULL;
431
432     return msg;
433 }
434
435 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
436 {
437     if (!payload || !name)
438     {
439         return NULL;
440     }
441
442     OCRepPayloadValue* val = payload->values;
443     while(val)
444     {
445         if (0 == strcmp(val->name, name))
446         {
447             return val;
448         }
449         val = val->next;
450     }
451
452     return NULL;
453 }
454
455 NSTopicList * NSInitializeTopicList()
456 {
457     NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
458
459     if(!topicList)
460     {
461         NS_LOG(ERROR, "topicList is NULL");
462         return NULL;
463     }
464
465     (topicList->consumerId)[0] = '\0';
466     topicList->head = NULL;
467     topicList->tail = NULL;
468
469     return topicList;
470 }
471
472 NSResult NSFreeTopicList(NSTopicList * topicList)
473 {
474     if (!topicList)
475     {
476         return NS_ERROR;
477     }
478
479     //TODO:Free Topic List
480
481
482     return NS_OK;
483 }
484