1 /* *****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * *****************************************************************/
23 #include "payload_logging.h"
24 #include "oic_malloc.h"
25 #include "doxmresource.h"
26 #include "srmutility.h"
27 #include "pmutility.h"
29 #define TAG "OIC_CLOUD_UTILS"
38 char *DEFAULT_QUERY = COAP_TCP_QUERY;
39 char *DEFAULT_PREFIX = COAP_TCP_PREFIX;
42 * This function sets required CoAP prefix depending on
43 * secure or non-secure connection
45 * @param[in] secure secure flag
47 void setCoapPrefix(bool secure)
51 DEFAULT_QUERY = COAPS_TCP_QUERY;
52 DEFAULT_PREFIX = COAPS_TCP_PREFIX;
56 DEFAULT_QUERY = COAP_TCP_QUERY;
57 DEFAULT_PREFIX = COAP_TCP_PREFIX;
62 * This hepler function handles response from each request
63 * During execution it calls proper callbacks
65 * @param[in] ctx context
66 * @param[in] handle handle
67 * @param[in] clientResponse response from peer
69 static OCStackApplicationResult handleResponse(void *ctx,
71 OCClientResponse *clientResponse)
75 if (NULL == clientResponse)
77 OIC_LOG_V(ERROR, TAG, "Received null response from client");
78 return OC_STACK_DELETE_TRANSACTION;
81 OIC_LOG_V(DEBUG, TAG, "Received callback with response code: %d", clientResponse->result);
83 if (clientResponse->payload)
85 OIC_LOG(DEBUG, TAG, "Payload received:");
86 OIC_LOG_PAYLOAD(DEBUG, clientResponse->payload);
91 OCStackResult result = clientResponse->result;
94 ContextInfo_t *info = (ContextInfo_t *)ctx;
98 result = ((UserFunctionCB)info->fn)(info->params, &data, clientResponse);
103 ((OCCloudResponseCB)info->cb)(info->ctx, result, data);
107 return OC_STACK_DELETE_TRANSACTION;
110 static void deleteContextInfo(void *info)
115 void fillCallbackData(OCCallbackData *cbData, void *ctx, OCCloudResponseCB cb,
116 UserFunctionCB fn, void *params)
118 memset(cbData, 0, sizeof(OCCallbackData));
120 ContextInfo_t *info = OICCalloc(1, sizeof(ContextInfo_t));
123 OIC_LOG(ERROR, TAG, "Can't allocate memory for info");
130 info->params = params;
132 cbData->cb = handleResponse;
133 cbData->cd = deleteContextInfo;
134 cbData->context = info;
139 char *deviceId = NULL;
142 OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__);
144 memset(&uuid, 0, sizeof(uuid));
145 if (OC_STACK_OK != GetDoxmDeviceID(&uuid))
147 OIC_LOG(ERROR, TAG, "Cann't get the device id(GetDoxmDeviceID)");
151 if (OC_STACK_OK != ConvertUuidToStr(&uuid, &deviceId))
153 OIC_LOG(ERROR, TAG, "Cann't get the device id(ConvertUuidToStr)");
157 OIC_LOG_V(DEBUG, TAG, "device Id: %s", deviceId);
159 OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__);
164 * Clear previously filled array of strings
166 * @param[out] list array of strings structure to clear
167 * @param[in] count elements count
169 void clearStringArray(stringArray_t *list)
171 if (!list || !list->array)
176 for (size_t i = 0; i < list->length; i++)
178 OICFree(list->array[i]);
180 OICFree(list->array);