Merge branch '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
68     return copyOfRequest;
69 }
70
71 NSResult NSFreeOCEntityHandlerRequest(OCEntityHandlerRequest * entityHandlerRequest)
72 {
73     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - IN");
74
75     OICFree(entityHandlerRequest->query);
76     OCPayloadDestroy(entityHandlerRequest->payload);
77     OICFree(entityHandlerRequest);
78
79     NS_LOG(DEBUG, "NSFreeOCEntityHandlerRequest - OUT");
80
81     return NS_OK;
82 }
83
84 NSResult NSFreeMessage(NSMessage * obj)
85 {
86     if (!obj)
87     {
88         return NS_ERROR;
89     }
90
91     if (obj->messageId)
92     {
93         obj->messageId = 0;
94     }
95
96     if (obj->title)
97     {
98         OICFree(obj->title);
99         obj->title = NULL;
100     }
101
102     if (obj->contentText)
103     {
104         OICFree(obj->contentText);
105         obj->contentText = NULL;
106     }
107
108     if (obj->sourceName)
109     {
110         OICFree(obj->sourceName);
111         obj->sourceName = NULL;
112     }
113
114     OICFree(obj);
115
116     return NS_OK;
117 }
118
119 NSMessage * NSDuplicateMessage(NSMessage * copyMsg)
120 {
121     NSMessage * newMsg = NULL;
122
123     if(copyMsg == NULL)
124     {
125         NS_LOG(ERROR, "Copy Msg is NULL");
126         return NULL;
127     }
128
129     newMsg = (NSMessage *)OICMalloc(sizeof(NSMessage));
130     newMsg->contentText = NULL;
131     newMsg->messageId = 0;
132     newMsg->sourceName = NULL;
133     newMsg->title = NULL;
134
135     if (copyMsg->messageId)
136     {
137         newMsg->messageId = copyMsg->messageId;
138     }
139
140     if (copyMsg->title)
141     {
142         newMsg->title = OICStrdup(copyMsg->title);
143     }
144
145     if (copyMsg->contentText)
146     {
147         newMsg->contentText = OICStrdup(copyMsg->contentText);
148     }
149
150     if (copyMsg->sourceName)
151     {
152        newMsg->sourceName = OICStrdup(copyMsg->sourceName);
153     }
154
155     return newMsg;
156 }
157
158 NSResult NSFreeSync(NSSyncInfo * obj)
159 {
160     if (!obj)
161     {
162         return NS_ERROR;
163     }
164
165     OICFree(obj);
166
167     return NS_OK;
168 }
169
170 NSSyncInfo* NSDuplicateSync(NSSyncInfo * copyMsg)
171 {
172     NSSyncInfo * newMsg = NULL;
173
174     if(copyMsg == NULL)
175     {
176         NS_LOG(ERROR, "Copy Msg is NULL");
177         return NULL;
178     }
179
180     newMsg = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
181
182     newMsg->messageId = copyMsg->messageId;
183     OICStrcpy(newMsg->providerId, UUID_STRING_SIZE, copyMsg->providerId);
184     newMsg->state = copyMsg->state;
185
186     return newMsg;
187 }
188
189 NSResult NSFreeConsumer(NSConsumer * obj)
190 {
191     if (!obj)
192     {
193         return NS_ERROR;
194     }
195
196     (obj->consumerId)[0] = '\0';
197
198     OICFree(obj);
199     obj = NULL;
200
201     return NS_OK;
202 }
203
204 NSConsumer* NSDuplicateConsumer(NSConsumer * copyMsg)
205 {
206     NSConsumer * newMsg = NULL;
207
208     if(copyMsg == NULL)
209     {
210         NS_LOG(ERROR, "Copy Msg is NULL");
211         return NULL;
212     }
213
214     newMsg = (NSConsumer *)OICMalloc(sizeof(NSConsumer));
215     (newMsg->consumerId)[0] = '\0';
216
217     OICStrcpy(newMsg->consumerId, UUID_STRING_SIZE, copyMsg->consumerId);
218
219     return newMsg;
220 }
221
222 void NSDuplicateSetPropertyString(OCRepPayload** msgPayload, const char * name,
223         const char * copyString)
224 {
225     if(copyString)
226     {
227         OCRepPayloadSetPropString(*msgPayload, name, copyString);
228     }
229 }
230
231
232 NSSyncInfo * NSGetSyncInfo(OCPayload * payload)
233 {
234     NS_LOG(DEBUG, "NSGetSyncInfo - IN");
235     char * providerId = NULL;
236     int64_t state;
237
238     if(!payload)
239     {
240         return NULL;
241     }
242     NSSyncInfo * retSync = (NSSyncInfo *)OICMalloc(sizeof(NSSyncInfo));
243     if (!retSync)
244     {
245         return NULL;
246     }
247
248     retSync->messageId = 0;
249     retSync->state = NS_SYNC_READ;
250
251     OCRepPayload * repPayload = (OCRepPayload *)payload;
252     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_MESSAGE_ID, (int64_t *)&retSync->messageId))
253     {
254         OICFree(retSync);
255         return NULL;
256     }
257
258     if (!OCRepPayloadGetPropString(repPayload, NS_ATTRIBUTE_PROVIDER_ID, &providerId))
259     {
260         OICFree(retSync);
261         return NULL;
262     }
263
264     if (!OCRepPayloadGetPropInt(repPayload, NS_ATTRIBUTE_STATE, &state))
265     {
266         OICFree(retSync);
267         return NULL;
268     }
269
270     retSync->state = (NSSyncType) state;
271     OICStrcpy(retSync->providerId, UUID_STRING_SIZE, providerId);
272     OICFree(providerId);
273
274     NS_LOG_V(DEBUG, "Provider ID : %s", retSync->providerId);
275     NS_LOG_V(DEBUG, "Sync ID : %lld", retSync->messageId);
276     NS_LOG_V(DEBUG, "Sync State : %d", (int) retSync->state);
277
278     NS_LOG(DEBUG, "NSGetSyncInfo - OUT");
279
280     return retSync;
281 }
282
283 NSResult NSGenerateUUIDStr(char uuidStr[UUID_STRING_SIZE])
284 {
285     uint8_t uuid[UUID_SIZE] = { 0, };
286
287     if (RAND_UUID_OK == OCGenerateUuid(uuid))
288     {
289         if (RAND_UUID_OK == OCConvertUuidToString(uuid, uuidStr))
290         {
291             return NS_OK;
292         }
293     }
294     return NS_ERROR;
295 }
296
297 char * NSGetValueFromQuery(char *query, char * compareKey)
298 {
299
300     char *key = NULL;
301     char *value = NULL;
302     char *restOfQuery = NULL;
303     int numKeyValuePairsParsed = 0;
304
305     NS_LOG_V(INFO, "NS Query Params = %s", query);
306
307     char *keyValuePair = strtok_r (query, NS_QUERY_SEPARATOR, &restOfQuery);
308
309     while(keyValuePair)
310     {
311         if (numKeyValuePairsParsed >= 2)
312         {
313             NS_LOG(ERROR, "More than 2 queries params in URI.");
314             return NULL;
315         }
316
317         key = strtok_r(keyValuePair, NS_KEY_VALUE_DELIMITER, &value);
318
319         if (!key || !value)
320         {
321             NS_LOG(ERROR, "More than 2 queries params in URI.");
322             return NULL;
323         }
324
325         if (strcmp(key, compareKey) == 0)
326         {
327             return value;
328         }
329
330         ++numKeyValuePairsParsed;
331
332         keyValuePair = strtok_r(NULL, NS_QUERY_SEPARATOR, &restOfQuery);
333     }
334
335     return NULL;
336 }