implementation NSStopProvider and bug fixed.
[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
36         if (copyOfRequest->query)
37         {
38             copyOfRequest->query = OICStrdup(entityHandlerRequest->query);
39             if(!copyOfRequest->query)
40             {
41                 NS_LOG(ERROR, "Copy failed due to allocation failure");
42                 OICFree(copyOfRequest);
43                 return NULL;
44             }
45         }
46
47         if (entityHandlerRequest->payload)
48         {
49             copyOfRequest->payload = (OCPayload *)
50                     (OCRepPayloadClone ((OCRepPayload*) entityHandlerRequest->payload));
51         }
52
53         // Ignore vendor specific header options for example
54         copyOfRequest->numRcvdVendorSpecificHeaderOptions = 0;
55         copyOfRequest->rcvdVendorSpecificHeaderOptions = NULL;
56     }
57
58     if (copyOfRequest)
59     {
60         NS_LOG(DEBUG, "Copied client request");
61     }
62     else
63     {
64         NS_LOG(DEBUG, "Error copying client request");
65     }
66
67     NS_LOG(DEBUG, "NSCopyOCEntityHandlerRequest - OUT");
68
69     return copyOfRequest;
70 }
71
72 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
73 {
74     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
75
76     OICFree(entityHandlerRequest->query);
77     OCPayloadDestroy(entityHandlerRequest->payload);
78     OICFree(entityHandlerRequest);
79
80     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
81
82     return NS_OK;
83 }
84
85 NSResult NSFreeMessage(NSMessage * obj)
86 {
87     if (!obj)
88     {
89         return NS_ERROR;
90     }
91
92     if (obj->mId)
93     {
94         OICFree(obj->mId);
95         obj->mId = NULL;
96     }
97
98     if (obj->mTitle)
99     {
100         OICFree(obj->mTitle);
101         obj->mTitle = NULL;
102     }
103
104     if (obj->mContentText)
105     {
106         OICFree(obj->mContentText);
107         obj->mContentText = NULL;
108     }
109
110     if (obj->mSource)
111     {
112         OICFree(obj->mSource);
113         obj->mSource = NULL;
114     }
115
116     OICFree(obj);
117
118     return NS_OK;
119 }
120
121 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
122 {
123     NSMessage * newMsg = NULL;
124
125     if(copyMsg == NULL)
126     {
127         NS_LOG(ERROR, "Copy Msg is NULL");
128         return NULL;
129     }
130
131     newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
132     newMsg->mContentText = NULL;
133     newMsg->mId = NULL;
134     newMsg->mSource = NULL;
135     newMsg->mTitle = NULL;
136
137     if (copyMsg->mId)
138     {
139         newMsg->mId = OICStrdup(copyMsg->mId);
140     }
141
142     if (copyMsg->mTitle)
143     {
144         newMsg->mTitle = OICStrdup(copyMsg->mTitle);
145     }
146
147     if (copyMsg->mContentText)
148     {
149         newMsg->mContentText = OICStrdup(copyMsg->mContentText);
150     }
151
152     if (copyMsg->mSource)
153     {
154        newMsg->mSource = OICStrdup(copyMsg->mSource);
155     }
156
157     return newMsg;
158 }
159
160 NSResult NSFreeSync(NSSync * obj)
161 {
162     if (!obj)
163     {
164         return NS_ERROR;
165     }
166
167     if (obj->mMessageId)
168     {
169         OICFree(obj->mMessageId);
170         obj->mMessageId = NULL;
171     }
172
173     if (obj->mSourceId)
174     {
175         OICFree(obj->mSourceId);
176         obj->mSourceId = NULL;
177     }
178
179     OICFree(obj);
180
181     return NS_OK;
182 }
183
184 NSSync* NSDuplicateSync(NSSync * copyMsg)
185 {
186     NSSync * newMsg = NULL;
187
188     if(copyMsg == NULL)
189     {
190         NS_LOG(ERROR, "Copy Msg is NULL");
191         return NULL;
192     }
193
194     newMsg = (NSSync *)OICMalloc(sizeof(NSSync));
195     newMsg->mMessageId = NULL;
196     newMsg->mSourceId = NULL;
197     newMsg->mState = -1;
198
199     if (copyMsg->mMessageId)
200     {
201         newMsg->mMessageId = OICStrdup(copyMsg->mMessageId);
202     }
203
204     if (copyMsg->mSourceId)
205     {
206         newMsg->mSourceId = OICStrdup(copyMsg->mSourceId);
207     }
208
209     return newMsg;
210 }
211
212 NSResult NSFreeConsumer(NSConsumer * obj)
213 {
214     if (!obj)
215     {
216         return NS_ERROR;
217     }
218
219     if (obj->mAddress)
220     {
221         OICFree(obj->mAddress);
222         obj->mAddress = NULL;
223     }
224
225     if (obj->mDeviceId)
226     {
227         OICFree(obj->mDeviceId);
228         obj->mDeviceId = NULL;
229     }
230
231     OICFree(obj);
232
233     return NS_OK;
234 }
235
236 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
237 {
238     NSConsumer * newMsg = NULL;
239
240     if(copyMsg == NULL)
241     {
242         NS_LOG(ERROR, "Copy Msg is NULL");
243         return NULL;
244     }
245
246     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
247     newMsg->mAddress = NULL;
248     newMsg->mDeviceId = NULL;
249
250     if (copyMsg->mAddress)
251     {
252         newMsg->mAddress = OICStrdup(copyMsg->mAddress);
253     }
254
255     if (copyMsg->mDeviceId)
256     {
257         newMsg->mDeviceId = OICStrdup(copyMsg->mDeviceId);
258     }
259
260     return newMsg;
261 }