replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / sample / cloud / cloudDiscovery.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
22 #include "ocstack.h"
23 #include "logger.h"
24 #include "ocpayload.h"
25 #include "payload_logging.h"
26 #include "oic_string.h"
27 #include "srmresourcestrings.h"
28 #include "pmutility.h"
29
30 #include "cloudCommon.h"
31 #include "cloudWrapper.h"
32
33 #define TAG "cloudDiscovery"
34
35 #define DISCOVERY_URI "/oic/res"
36
37 static OCDevAddr endPoint;
38 static OCConnectivityType ocConnType = CT_DEFAULT;
39 static char gUri[MAX_URI_LENGTH] = {0};
40
41 bool withTcp = true;
42
43 /**
44  * This function clears internal data
45  */
46 static void clearData()
47 {
48     memset(&endPoint, 0, sizeof(endPoint));
49     memset(gUri, 0, sizeof(gUri));
50     ocConnType = CT_DEFAULT;
51 }
52
53 /**
54  * This function parses Discovery payload
55  * It tries to find secure devices
56  *
57  * @param[in] clientResponse   response from peer
58  */
59 static void parseClientResponse(OCClientResponse * clientResponse)
60 {
61     OCResourcePayload* res = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
62
63     while (res)
64     {
65         char *uri = res->uri;
66         OIC_LOG_V(INFO, TAG, "Uri -- %s", uri);
67
68         if (0 == strcmp(uri, OIC_RSRC_DOXM_URI))
69         {
70             OIC_LOG(INFO,TAG,"Skip: doxm is secure virtual resource");
71             goto next;
72         }
73         if (0 == strcmp(uri, OIC_RSRC_PSTAT_URI))
74         {
75             OIC_LOG(INFO,TAG,"Skip: pstat is secure virtual resource");
76             goto next;
77         }
78
79         OIC_LOG_V(INFO, TAG, "Secure -- %s", res->secure ? "YES" : "NO");
80
81         if (res->secure)
82         {
83             ocConnType = clientResponse->connType;
84             endPoint = clientResponse->devAddr;
85
86             if(withTcp)
87             {
88                 OIC_LOG_V(INFO,TAG,"SECUREPORT tcp: %d",res->tcpPort);
89                 endPoint.port = res->tcpPort;
90             }
91             else
92             {
93                 OIC_LOG_V(INFO,TAG,"SECUREPORT udp: %d",res->port);
94                 endPoint.port = res->port;
95             }
96             OICStrcpy(gUri, sizeof(gUri), uri);
97             break;
98         }
99
100         next:
101         res = res->next;
102     }
103 }
104
105 /**
106  * This function handles discovery response
107  *
108  * @param[in] ctx                    context
109  * @param[in] handle                 handle
110  * @param[in] response               response from peer
111  * @return  OCStackApplicationResult application result
112  */
113 static OCStackApplicationResult handleDiscoveryResponse(void *ctx, OCDoHandle handle,
114                                                         OCClientResponse * clientResponse)
115 {
116     OC_UNUSED(ctx);
117     OC_UNUSED(handle);
118
119     if (NULL == clientResponse)
120     {
121         OIC_LOG(ERROR, TAG, "Receive NULL response");
122         return OC_STACK_DELETE_TRANSACTION;
123     }
124
125     if (OC_STACK_OK == clientResponse->result)
126     {
127         OIC_LOG(INFO, TAG, "Discovery successful!");
128     }
129     else
130     {
131         OIC_LOG_V(INFO, TAG, "Discovery fails with result: %d", clientResponse->result);
132     }
133
134     if (clientResponse->payload)
135     {
136         OIC_LOG_PAYLOAD(INFO, clientResponse->payload);
137     }
138     else
139     {
140         OIC_LOG(ERROR, TAG, "Receive NULL payload");
141     }
142
143     OIC_LOG_V(INFO, TAG,
144             "Device =============> Discovered @ %s:%d",
145             clientResponse->devAddr.addr,
146             clientResponse->devAddr.port);
147
148     parseClientResponse(clientResponse);
149
150     return OC_STACK_KEEP_TRANSACTION;
151 }
152
153 /**
154  * This function handles any response from secure device
155  *
156  * @param[in] ctx                    context
157  * @param[in] handle                 handle
158  * @param[in] response               response from peer
159  * @return  OCStackApplicationResult application result
160  */
161 static OCStackApplicationResult handleResponse(void *ctx, OCDoHandle handle,
162                                                OCClientResponse * clientResponse)
163 {
164     OC_UNUSED(ctx);
165     OC_UNUSED(handle);
166
167     if(!clientResponse)
168     {
169         OIC_LOG_V(ERROR, TAG, "%s: Received NULL response",  __func__);
170         return OC_STACK_DELETE_TRANSACTION;
171     }
172
173     if (NULL == ctx)
174     {
175         OIC_LOG_V(ERROR, TAG, "%s: Received NULL context, exit",  __func__);
176         return OC_STACK_DELETE_TRANSACTION;
177     }
178
179     char *str = NULL;
180     OCMethod method = *((OCMethod*)ctx);
181
182     switch (method)
183     {
184         case OC_REST_GET:
185             str = "Get";
186             break;
187         case OC_REST_PUT:
188             str = "Put";
189             break;
190         case OC_REST_POST:
191             str = "Post";
192             break;
193         default:
194             OIC_LOG_V(INFO, TAG, "Received Wrong method %d. Skip response", method);
195             return OC_STACK_ERROR;
196     }
197
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);
202
203     return OC_STACK_DELETE_TRANSACTION;
204 }
205
206 OCStackResult InitDiscovery()
207 {
208     clearData();
209
210     OCCallbackData cbData;
211     memset(&cbData, 0, sizeof(OCCallbackData));
212     cbData.cb = handleDiscoveryResponse;
213     cbData.cd = unlockMenu;
214
215     return OCDoResource(NULL, OC_REST_DISCOVER, DISCOVERY_URI, NULL, 0,
216                         CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
217 }
218
219 OCStackResult InitRequest(OCMethod method)
220 {
221     if (0 == endPoint.port || 0 == endPoint.addr[0])
222     {
223         OIC_LOG(ERROR, TAG, "Host or port are missing. Please execute Discovery first!");
224         return OC_STACK_ERROR;
225     }
226
227     OCMethod *tmp = OICCalloc(1, sizeof(OCMethod));
228     if (!tmp)
229     {
230         OIC_LOG(ERROR, TAG, "Can't allocate tmp");
231         return OC_STACK_NO_MEMORY;
232     }
233
234     *tmp = method;
235
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);
238
239     endPoint.flags = (OCTransportFlags)(endPoint.flags | OC_SECURE);
240     endPoint.adapter = OC_ADAPTER_TCP;
241
242
243     OCCallbackData cbData;
244     memset(&cbData, 0, sizeof(OCCallbackData));
245     cbData.cd = unlockMenu;
246     cbData.cb = handleResponse;
247     cbData.context = (void *)tmp;
248
249     return OCDoResource(NULL, method, gUri, &endPoint, 0,
250                         ocConnType, OC_LOW_QOS, &cbData, NULL, 0);
251 }