Add "Source" attribute in NSMessage Resource
[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 NSResult NSFreeMessage(NSMessage * obj)
24 {
25     if (!obj)
26     {
27         return NS_ERROR;
28     }
29
30     if (obj->mId)
31     {
32         OICFree(obj->mId);
33         obj->mId = NULL;
34     }
35
36     if (obj->mTitle)
37     {
38         OICFree(obj->mTitle);
39         obj->mTitle = NULL;
40     }
41
42     if (obj->mContentText)
43     {
44         OICFree(obj->mContentText);
45         obj->mContentText = NULL;
46     }
47
48     if (obj->mSource)
49     {
50         OICFree(obj->mSource);
51         obj->mSource = NULL;
52     }
53
54     OICFree(obj);
55
56     return NS_OK;
57 }
58
59 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
60 {
61     if(copyMsg == NULL)
62     {
63         NS_LOG(ERROR, "Copy Msg is NULL");
64         return NULL;
65     }
66
67     NSMessage * newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
68
69     if (!copyMsg->mId)
70     {
71         newMsg->mId = OICStrdup(copyMsg->mId);
72     }
73
74     if (!copyMsg->mTitle)
75     {
76         newMsg->mTitle = OICStrdup(copyMsg->mTitle);
77     }
78
79     if (!copyMsg->mContentText)
80     {
81         newMsg->mContentText = OICStrdup(copyMsg->mContentText);
82     }
83
84     if (!copyMsg->mSource)
85     {
86         newMsg->mSource = OICStrdup(copyMsg->mSource);
87     }
88
89     return newMsg;
90 }
91
92 OCEntityHandlerRequest *NSCopyOCEntityHandlerRequest(OCEntityHandlerRequest *entityHandlerRequest)
93 {
94     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - IN");
95
96     OCEntityHandlerRequest *copyOfRequest =
97             (OCEntityHandlerRequest *)OICMalloc(sizeof(OCEntityHandlerRequest));
98
99     if (copyOfRequest)
100     {
101         // Do shallow copy
102         memcpy(copyOfRequest, entityHandlerRequest, sizeof(OCEntityHandlerRequest));
103
104
105         if (copyOfRequest->query)
106         {
107             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
108             if(!copyOfRequest->query)
109             {
110                 NS_LOG(ERROR, "Copy failed due to allocation failure");
111                 OICFree(copyOfRequest);
112                 return NULL;
113             }
114         }
115
116         if (entityHandlerRequest->payload)
117         {
118             copyOfRequest->payload = (OCPayload *)
119                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
120         }
121
122         // Ignore vendor specific header options for example
123         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
124         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
125     }
126
127     if (copyOfRequest)
128     {
129         NS_LOG(DEBUG, "Copied client request");
130     }
131     else
132     {
133         NS_LOG(DEBUG, "Error copying client request");
134     }
135
136     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
137
138     return copyOfRequest;
139 }
140
141 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
142 {
143     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
144
145     OICFree(entityHandlerRequest->query);
146     OCPayloadDestroy(entityHandlerRequest->payload);
147     OICFree(entityHandlerRequest);
148
149     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
150
151     return NS_OK;
152 }
153
154