Svace memory leak fixes
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / cloud / utils.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 #include "utils.h"
21
22 #include "logger.h"
23 #include "payload_logging.h"
24 #include "oic_malloc.h"
25 #include "doxmresource.h"
26 #include "srmutility.h"
27 #include "pmutility.h"
28
29 #define TAG "OIC_CLOUD_UTILS"
30
31 typedef struct {
32     void *ctx;
33     OCCloudResponseCB cb;
34     UserFunctionCB fn;
35     void *params;
36 }ContextInfo_t;
37
38 char *DEFAULT_QUERY  = COAP_TCP_QUERY;
39 char *DEFAULT_PREFIX = COAP_TCP_PREFIX;
40
41 /**
42  * This function sets required CoAP prefix depending on
43  * secure or non-secure connection
44  *
45  * @param[in] secure   secure flag
46  */
47 void setCoapPrefix(bool secure)
48 {
49     if (secure)
50     {
51         DEFAULT_QUERY  = COAPS_TCP_QUERY;
52         DEFAULT_PREFIX = COAPS_TCP_PREFIX;
53     }
54     else
55     {
56         DEFAULT_QUERY  = COAP_TCP_QUERY;
57         DEFAULT_PREFIX = COAP_TCP_PREFIX;
58     }
59 }
60
61 /**
62  * This hepler function handles response from each request
63  * During execution it calls proper callbacks
64  *
65  * @param[in] ctx             context
66  * @param[in] handle          handle
67  * @param[in] clientResponse  response from peer
68  */
69 static OCStackApplicationResult handleResponse(void *ctx,
70                                               OCDoHandle handle,
71                                               OCClientResponse *clientResponse)
72 {
73     OC_UNUSED(handle);
74
75     if (NULL == clientResponse)
76     {
77         OIC_LOG_V(ERROR, TAG, "Received null response from client");
78         return OC_STACK_DELETE_TRANSACTION;
79     }
80
81     OIC_LOG_V(DEBUG, TAG, "Received callback with response code: %d", clientResponse->result);
82
83     if (clientResponse->payload)
84     {
85         OIC_LOG(DEBUG, TAG, "Payload received:");
86         OIC_LOG_PAYLOAD(DEBUG, clientResponse->payload);
87     }
88
89     if (ctx)
90     {
91         OCStackResult result = clientResponse->result;
92         void *data = NULL;
93
94         ContextInfo_t *info = (ContextInfo_t *)ctx;
95
96         if (info->fn)
97         {
98             result = ((UserFunctionCB)info->fn)(info->params, &data, clientResponse);
99         }
100
101         if (info->cb)
102         {
103             ((OCCloudResponseCB)info->cb)(info->ctx, result, data);
104         }
105     }
106
107     return OC_STACK_DELETE_TRANSACTION;
108 }
109
110 static void deleteContextInfo(void *info)
111 {
112     OICFree(info);
113 }
114
115 void fillCallbackData(OCCallbackData *cbData, void *ctx, OCCloudResponseCB cb,
116                                               UserFunctionCB fn, void *params)
117 {
118     memset(cbData, 0, sizeof(OCCallbackData));
119
120     ContextInfo_t *info = OICCalloc(1, sizeof(ContextInfo_t));
121     if (!info)
122     {
123         OIC_LOG(ERROR, TAG, "Can't allocate memory for info");
124         return;
125     }
126
127     info->ctx = ctx;
128     info->cb  = cb;
129     info->fn  = fn;
130     info->params = params;
131
132     cbData->cb = handleResponse;
133     cbData->cd = deleteContextInfo;
134     cbData->context = info;
135 }
136
137 char * getDeviceId()
138 {
139     char *deviceId = NULL;
140     OicUuid_t uuid;
141
142     OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
143
144     memset(&uuid, 0, sizeof(uuid));
145     if (OC_STACK_OK != GetDoxmDeviceID(&uuid))
146     {
147         OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
148         goto exit;
149     }
150
151     if (OC_STACK_OK != ConvertUuidToStr(&uuid, &deviceId))
152     {
153         OIC_LOG(ERROR, TAG, "Cann't get the device id(ConvertUuidToStr)");
154         goto exit;
155     }
156
157     OIC_LOG_V(DEBUG, TAG, "device Id: %s", deviceId);
158 exit:
159     OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
160     return deviceId;
161 }
162
163 /**
164  * Clear previously filled array of strings
165  *
166  * @param[out] list           array of strings structure to clear
167  * @param[in] count           elements count
168  */
169 void clearStringArray(stringArray_t *list)
170 {
171     if (!list || !list->array)
172     {
173         return;
174     }
175
176     for (size_t i = 0; i < list->length; i++)
177     {
178         OICFree(list->array[i]);
179     }
180     OICFree(list->array);
181 }
182