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