Revert "[CONPRO-1337] Disabled Presence Feature"
[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         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
33         copyOfRequest->payload = NULL;
34         copyOfRequest->query = NULL;
35         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
36         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
37
38         if (entityHandlerRequest->query)
39         {
40             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
41             if (!copyOfRequest->query)
42             {
43                 NS_LOG(ERROR, "Copy failed due to allocation failure");
44                 OICFree(copyOfRequest);
45                 return NULL;
46             }
47         }
48
49         if (entityHandlerRequest->payload)
50         {
51             copyOfRequest->payload = (OCPayload *)
52                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
53         }
54     }
55
56     if (copyOfRequest)
57     {
58         NS_LOG(DEBUG, "Copied client request");
59     }
60     else
61     {
62         NS_LOG(DEBUG, "Error copying client request");
63     }
64
65     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
66     return copyOfRequest;
67 }
68
69 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
70 {
71     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
72
73     OICFree(entityHandlerRequest->query);
74     OCPayloadDestroy(entityHandlerRequest->payload);
75     OICFree(entityHandlerRequest);
76
77     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
78
79     return NS_OK;
80 }
81
82 NSResult NSFreeMessage(NSMessage * obj)
83 {
84     if (!obj)
85     {
86         return NS_ERROR;
87     }
88
89     obj->messageId = 0;
90     (obj->providerId)[0] = '\0';
91
92     NSFreeMalloc(&(obj->dateTime));
93     obj->ttl = 0;
94     NSFreeMalloc(&(obj->title));
95     NSFreeMalloc(&(obj->contentText));
96     NSFreeMalloc(&(obj->sourceName));
97     NSFreeMalloc(&(obj->topic));
98     NSFreeMediaContents(obj->mediaContents);
99     OCRepPayloadDestroy(obj->extraInfo);
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     NS_VERIFY_NOT_NULL(newMsg, NULL);
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
267     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
268
269     if (!retSync)
270     {
271         return NULL;
272     }
273
274     retSync->messageId = 0;
275     retSync->state = NS_SYNC_READ;
276     OCRepPayload * repPayload = (OCRepPayload *)payload;
277
278     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID,
279             (int64_t *)&retSync->messageId))
280     {
281         OICFree(retSync);
282         return NULL;
283     }
284
285     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
286     {
287         OICFree(retSync);
288         return NULL;
289     }
290
291     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
292     {
293         OICFree(retSync);
294         return NULL;
295     }
296
297     retSync->state = (NSSyncType) state;
298     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
299     OICFree(providerId);
300
301     NS_LOG_V(INFO_PRIVATE, "Provider ID : %s", retSync->providerId);
302     NS_LOG_V(DEBUG, "Sync ID : %lld", (long long int)retSync->messageId);
303     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
304
305     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
306
307     return retSync;
308 }
309
310 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
311 {
312     uint8_t uuid[UUID_SIZE] = { 0, };
313
314     if (RAND_UUID_OK == OCGenerateUuid(uuid))
315     {
316         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
317         {
318             return NS_OK;
319         }
320     }
321     return NS_ERROR;
322 }
323
324 char * NSGetValueFromQuery(char *query, char * compareKey)
325 {
326     char *key = NULL;
327     char *value = NULL;
328     char *restOfQuery = NULL;
329     int numKeyValuePairsParsed = 0;
330
331     NS_LOG_V(INFO, "NS Query Params = %s", query);
332
333     if (!query || query[0] == '\0' || !strlen(query))
334     {
335         NS_LOG(ERROR, "query is null or \\0 or size is 0");
336         return NULL;
337     }
338
339     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
340
341     while(keyValuePair)
342     {
343         if (numKeyValuePairsParsed >= 2)
344         {
345             NS_LOG(ERROR, "More than 2 queries params in URI.");
346             return NULL;
347         }
348
349         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
350
351         if (!key || !value)
352         {
353             NS_LOG(ERROR, "More than 2 queries params in URI.");
354             return NULL;
355         }
356
357         if (strcmp(key, compareKey) == 0)
358         {
359             NS_LOG_V(DEBUG, "found Key : [%s] - Value : [%s] = ", key, value);
360             return value;
361         }
362
363         ++numKeyValuePairsParsed;
364
365         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
366     }
367
368     return NULL;
369 }
370
371 NSResult NSFreeMalloc(char ** obj)
372 {
373     if (*obj)
374     {
375         OICFree(*obj);
376         *obj = NULL;
377         return NS_OK;
378     }
379
380     return NS_FAIL;
381 }
382
383 NSMediaContents * NSDuplicateMediaContents(NSMediaContents * copyObj)
384 {
385     if (!copyObj)
386     {
387         return NULL;
388     }
389
390     NSMediaContents * newObj = (NSMediaContents *)OICMalloc(sizeof(NSMediaContents));
391
392     if (!newObj)
393     {
394         NS_LOG(ERROR, "contents newObj is NULL");
395         return NULL;
396     }
397
398     if (copyObj->iconImage)
399     {
400         newObj->iconImage = OICStrdup(copyObj->iconImage);
401     }
402
403     return newObj;
404 }
405
406 NSResult NSFreeMediaContents(NSMediaContents * obj)
407 {
408     if (!obj)
409     {
410         return NS_FAIL;
411     }
412
413     NSFreeMalloc(&(obj->iconImage));
414     OICFree(obj);
415
416     return NS_OK;
417 }
418
419 NSMessage * NSInitializeMessage()
420 {
421     NSMessage * msg = (NSMessage *)OICMalloc(sizeof(NSMessage));
422
423     if (!msg)
424     {
425         NS_LOG(ERROR, "Msg is NULL");
426         return NULL;
427     }
428
429     msg->messageId = OICGetCurrentTime(TIME_IN_MS);
430     msg->messageId = msg->messageId & 0x000000007FFFFFFF;
431     (msg->providerId)[0] = '\0';
432     msg->type = 0;
433     msg->dateTime = NULL;
434     msg->ttl = 0;
435     msg->title = NULL;
436     msg->contentText = NULL;
437     msg->sourceName = NULL;
438     msg->mediaContents = NULL;
439     msg->topic = NULL;
440     msg->extraInfo = NULL;
441
442     return msg;
443 }
444
445 OCRepPayloadValue* NSPayloadFindValue(const OCRepPayload* payload, const char* name)
446 {
447     if (!payload || !name)
448     {
449         return NULL;
450     }
451
452     OCRepPayloadValue* val = payload->values;
453
454     while (val)
455     {
456         if (0 == strcmp(val->name, name))
457         {
458             return val;
459         }
460         val = val->next;
461     }
462
463     return NULL;
464 }
465
466 NSTopicList * NSInitializeTopicList()
467 {
468     NSTopicList * topicList = (NSTopicList *)OICMalloc(sizeof(NSTopicList));
469
470     if (!topicList)
471     {
472         NS_LOG(ERROR, "topicList is NULL");
473         return NULL;
474     }
475
476     (topicList->consumerId)[0] = '\0';
477     topicList->head = NULL;
478     topicList->tail = NULL;
479
480     return topicList;
481 }
482
483 OCDevAddr * NSChangeAddress(const char * inputaddress)
484 {
485     NS_VERIFY_NOT_NULL(inputaddress, NULL);
486
487     char * address = (char *)inputaddress;
488     char * schema = strstr(inputaddress, "//");
489     if (schema)
490     {
491         address = schema + 2;
492     }
493     size_t prefixLen = schema - inputaddress;
494     if (prefixLen <= 0)
495     {
496         NS_LOG(ERROR, "Invalid Input address.");
497         return NULL;
498     }
499
500     OCTransportFlags flags = OC_DEFAULT_FLAGS;
501     OCTransportAdapter adapter = OC_ADAPTER_IP;
502     if (strstr(inputaddress, "coap+tcp://"))
503     {
504         NS_LOG(DEBUG, "address : TCP");
505         adapter = OC_ADAPTER_TCP;
506     }
507     else if (strstr(inputaddress, "coaps://"))
508     {
509         NS_LOG(DEBUG, "address : UDP + SECURED");
510         flags |= OC_FLAG_SECURE;
511     }
512     else if (strstr(inputaddress, "coaps+tcp://"))
513     {
514         NS_LOG(DEBUG, "address : TCP + SECURED");
515         flags |= OC_FLAG_SECURE;
516         adapter = OC_ADAPTER_TCP;
517     }
518     else if (strstr(inputaddress, "coap://"))
519     {
520         NS_LOG(DEBUG, "address : UDP");
521     }
522     else
523     {
524         NS_LOG(ERROR, "Invalid CoAP Schema.");
525         return NULL;
526     }
527
528     OCDevAddr * retAddr = NULL;
529     retAddr = (OCDevAddr *) OICMalloc(sizeof(OCDevAddr));
530     NS_VERIFY_NOT_NULL(retAddr, NULL);
531
532     char * start = address;
533     char * end = address;
534     if (address[0] == '[')
535     {
536         flags |= OC_IP_USE_V6;
537         end = strchr(++address, ']');
538         if (!end || end <= start)
539         {
540             NS_LOG(ERROR, "Invalid Input Address - IPv6.");
541             NSOICFree(retAddr);
542             return NULL;
543         }
544         memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE);
545         OICStrcpy(retAddr->addr, (size_t)(end-start), address);
546     }
547     else
548     {
549         flags |= OC_IP_USE_V4;
550         end = strchr(address, ':');
551         if (!end || end <= start)
552         {
553             NS_LOG(ERROR, "Invalid Input Address - IPv4.");
554             NSOICFree(retAddr);
555             return NULL;
556         }
557         char * end2 = strchr(end + 1, ':');
558         if (end2)
559         {
560             NS_LOG(ERROR, "Invalid Input Address - IPv4.");
561             NSOICFree(retAddr);
562             return NULL;
563         }
564         memset(retAddr->addr, 0, (size_t)MAX_ADDR_STR_SIZE);
565         OICStrcpy(retAddr->addr, (size_t)(end-start)+1, address);
566     }
567
568     retAddr->adapter = adapter;
569     retAddr->flags = flags;
570
571     address = end + 1;
572     int tmp = 0;
573     if (flags & OC_IP_USE_V6)
574     {
575         address++;
576     }
577     uint16_t port = address[tmp++] - '0';
578
579     while(true)
580     {
581         if (address[tmp] == '\0' || address[tmp] > '9' || address[tmp] < '0')
582         {
583             break;
584         }
585         if (tmp >= 5 || (port >= 6553 && (address[tmp] -'0') >= 6))
586         {
587             NS_LOG_V(ERROR, "Invalid Input Address - Port. %d", tmp+1);
588             NSOICFree(retAddr);
589             return NULL;
590         }
591         port *= 10;
592         port += address[tmp++] - '0';
593     }
594
595     retAddr->port = port;
596
597     NS_LOG(DEBUG, "Change Address for TCP request");
598     NS_LOG_V(INFO_PRIVATE, "Origin : %s", inputaddress);
599     NS_LOG_V(INFO_PRIVATE, "Changed Addr : %s", retAddr->addr);
600     NS_LOG_V(INFO_PRIVATE, "Changed Port : %d", retAddr->port);
601
602     return retAddr;
603 }
604
605 bool NSOCResultToSuccess(OCStackResult ret)
606 {
607     switch (ret)
608     {
609         case OC_STACK_OK:
610         case OC_STACK_RESOURCE_CREATED:
611         case OC_STACK_RESOURCE_DELETED:
612         case OC_STACK_PRESENCE_STOPPED:
613         case OC_STACK_CONTINUE:
614         case OC_STACK_RESOURCE_CHANGED:
615             return true;
616         default:
617             NS_LOG_V(DEBUG, "OCStackResult : %d", (int)ret);
618             return false;
619     }
620 }
621