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 * *****************************************************************/
24 #include "ocpayload.h"
25 #include "payload_logging.h"
26 #include "oic_string.h"
27 #include "srmresourcestrings.h"
28 #include "pmutility.h"
30 #include "cloudCommon.h"
31 #include "cloudWrapper.h"
33 #define TAG "cloudDiscovery"
35 #define DISCOVERY_URI "/oic/res"
37 static OCDevAddr endPoint;
38 static OCConnectivityType ocConnType = CT_DEFAULT;
39 static char gUri[MAX_URI_LENGTH] = {0};
44 * This function clears internal data
46 static void clearData()
48 memset(&endPoint, 0, sizeof(endPoint));
49 memset(gUri, 0, sizeof(gUri));
50 ocConnType = CT_DEFAULT;
54 * This function parses Discovery payload
55 * It tries to find secure devices
57 * @param[in] clientResponse response from peer
59 static void parseClientResponse(OCClientResponse * clientResponse)
61 OCResourcePayload* res = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
66 OIC_LOG_V(INFO, TAG, "Uri -- %s", uri);
68 if (0 == strcmp(uri, OIC_RSRC_DOXM_URI))
70 OIC_LOG(INFO,TAG,"Skip: doxm is secure virtual resource");
73 if (0 == strcmp(uri, OIC_RSRC_PSTAT_URI))
75 OIC_LOG(INFO,TAG,"Skip: pstat is secure virtual resource");
79 OIC_LOG_V(INFO, TAG, "Secure -- %s", res->secure ? "YES" : "NO");
83 ocConnType = clientResponse->connType;
84 endPoint = clientResponse->devAddr;
88 OIC_LOG_V(INFO,TAG,"SECUREPORT tcp: %d",res->tcpPort);
89 endPoint.port = res->tcpPort;
93 OIC_LOG_V(INFO,TAG,"SECUREPORT udp: %d",res->port);
94 endPoint.port = res->port;
96 OICStrcpy(gUri, sizeof(gUri), uri);
106 * This function handles discovery response
108 * @param[in] ctx context
109 * @param[in] handle handle
110 * @param[in] response response from peer
111 * @return OCStackApplicationResult application result
113 static OCStackApplicationResult handleDiscoveryResponse(void *ctx, OCDoHandle handle,
114 OCClientResponse * clientResponse)
119 if (NULL == clientResponse)
121 OIC_LOG(ERROR, TAG, "Receive NULL response");
122 return OC_STACK_DELETE_TRANSACTION;
125 if (OC_STACK_OK == clientResponse->result)
127 OIC_LOG(INFO, TAG, "Discovery successful!");
131 OIC_LOG_V(INFO, TAG, "Discovery fails with result: %d", clientResponse->result);
134 if (clientResponse->payload)
136 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
140 OIC_LOG(ERROR, TAG, "Receive NULL payload");
144 "Device =============> Discovered @ %s:%d",
145 clientResponse->devAddr.addr,
146 clientResponse->devAddr.port);
148 parseClientResponse(clientResponse);
150 return OC_STACK_KEEP_TRANSACTION;
154 * This function handles any response from secure device
156 * @param[in] ctx context
157 * @param[in] handle handle
158 * @param[in] response response from peer
159 * @return OCStackApplicationResult application result
161 static OCStackApplicationResult handleResponse(void *ctx, OCDoHandle handle,
162 OCClientResponse * clientResponse)
169 OIC_LOG_V(ERROR, TAG, "%s: Received NULL response", __func__);
170 return OC_STACK_DELETE_TRANSACTION;
175 OIC_LOG_V(ERROR, TAG, "%s: Received NULL context, exit", __func__);
176 return OC_STACK_DELETE_TRANSACTION;
180 OCMethod method = *((OCMethod*)ctx);
194 OIC_LOG_V(INFO, TAG, "Received Wrong method %d. Skip response", method);
195 return OC_STACK_ERROR;
198 OIC_LOG_V(INFO, TAG, "StackResult: %d", clientResponse->result);
199 OIC_LOG_V(INFO, TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber);
200 OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
201 OIC_LOG_V(INFO, TAG, "=============> %s Response", str);
203 return OC_STACK_DELETE_TRANSACTION;
206 OCStackResult InitDiscovery()
210 OCCallbackData cbData;
211 memset(&cbData, 0, sizeof(OCCallbackData));
212 cbData.cb = handleDiscoveryResponse;
213 cbData.cd = unlockMenu;
215 return OCDoResource(NULL, OC_REST_DISCOVER, DISCOVERY_URI, NULL, 0,
216 CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
219 OCStackResult InitRequest(OCMethod method)
221 if (0 == endPoint.port || 0 == endPoint.addr[0])
223 OIC_LOG(ERROR, TAG, "Host or port are missing. Please execute Discovery first!");
224 return OC_STACK_ERROR;
227 OCMethod *tmp = OICCalloc(1, sizeof(OCMethod));
230 OIC_LOG(ERROR, TAG, "Can't allocate tmp");
231 return OC_STACK_NO_MEMORY;
236 OIC_LOG_V(INFO, TAG, "Executing uri = %s", gUri);
237 OIC_LOG_V(INFO, TAG, "Send Request to %s:%d", endPoint.addr, endPoint.port);
239 endPoint.flags = (OCTransportFlags)(endPoint.flags | OC_SECURE);
240 endPoint.adapter = OC_ADAPTER_TCP;
243 OCCallbackData cbData;
244 memset(&cbData, 0, sizeof(OCCallbackData));
245 cbData.cd = unlockMenu;
246 cbData.cb = handleResponse;
247 cbData.context = (void *)tmp;
249 return OCDoResource(NULL, method, gUri, &endPoint, 0,
250 ocConnType, OC_LOW_QOS, &cbData, NULL, 0);