1 /* *****************************************************************
3 * Copyright 2015 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 * *****************************************************************/
20 #ifndef _POSIX_C_SOURCE
21 #define _POSIX_C_SOURCE 200112L
30 #include "oic_malloc.h"
31 #include "oic_string.h"
35 #include "ocpayload.h"
37 #include "securevirtualresourcetypes.h"
38 #include "srmresourcestrings.h" //@note: SRM's internal header
39 #include "doxmresource.h" //@note: SRM's internal header
40 #include "pstatresource.h" //@note: SRM's internal header
43 #include "pmutility.h"
45 #define TAG ("PM-UTILITY")
48 * Function to search node in linked list that matches given IP and port.
50 * @param[in] pList List of OCProvisionDev_t.
51 * @param[in] addr address of target device.
52 * @param[in] port port of remote server.
54 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
56 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
58 if(NULL == addr || NULL == *ppDevicesList)
60 OC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
64 OCProvisionDev_t *ptr = NULL;
65 LL_FOREACH(*ppDevicesList, ptr)
67 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
78 * Add device information to list.
80 * @param[in] pList List of OCProvisionDev_t.
81 * @param[in] addr address of target device.
82 * @param[in] port port of remote server.
83 * @param[in] adapter adapter type of endpoint.
84 * @param[in] doxm pointer to doxm instance.
86 * @return OC_STACK_OK for success and errorcode otherwise.
88 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port,
89 OCTransportAdapter adapter, OicSecDoxm_t *doxm)
93 return OC_STACK_INVALID_PARAM;
96 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
99 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
102 OC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
103 return OC_STACK_NO_MEMORY;
106 OICStrcpy(ptr->endpoint.addr, MAX_ADDR_STR_SIZE, addr);
107 ptr->endpoint.port = port;
109 ptr->securePort = DEFAULT_SECURE_PORT;
110 ptr->endpoint.adapter = adapter;
113 LL_PREPEND(*ppDevicesList, ptr);
120 * Function to set secure port information from the given list of devices.
122 * @param[in] pList List of OCProvisionDev_t.
123 * @param[in] addr address of target device.
124 * @param[in] port port of remote server.
125 * @param[in] secureport secure port information.
127 * @return OC_STACK_OK for success and errorcode otherwise.
129 OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr, uint16_t port,
132 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
136 OC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
137 return OC_STACK_ERROR;
140 ptr->securePort = securePort;
146 * This function deletes list of provision target devices
148 * @param[in] pList List of OCProvisionDev_t.
150 void DeleteDeviceList(OCProvisionDev_t **ppDevicesList)
154 OCProvisionDev_t *del = NULL, *tmp = NULL;
155 LL_FOREACH_SAFE(*ppDevicesList, del, tmp)
157 LL_DELETE(*ppDevicesList, del);
159 DeleteDoxmBinData(del->doxm);
160 DeletePstatBinData(del->pstat);
167 * Timeout implementation for secure discovery. When performing secure discovery,
168 * we should wait a certain period of time for getting response of each devices.
170 * @param[in] waittime Timeout in seconds.
171 * @return OC_STACK_OK on success otherwise error.
173 OCStackResult PMTimeout(unsigned short waittime)
175 struct timespec startTime = {};
176 struct timespec currTime = {};
178 OCStackResult res = OC_STACK_OK;
179 #ifdef _POSIX_MONOTONIC_CLOCK
180 int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
182 int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
186 return OC_STACK_ERROR;
188 while (OC_STACK_OK == res)
190 #ifdef _POSIX_MONOTONIC_CLOCK
191 clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
193 clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
197 return OC_STACK_TIMEOUT;
199 long elapsed = (currTime.tv_sec - startTime.tv_sec);
200 if (elapsed > waittime)
211 * Extract secure port information from payload of discovery response.
213 * @param[in] jsonStr response payload of /oic/res discovery.
215 * @return Secure port
217 uint16_t GetSecurePortFromJSON(char* jsonStr)
219 // TODO: Modify error handling
224 cJSON *jsonProp = NULL;
226 cJSON *jsonPort = NULL;
228 cJSON *jsonRoot = cJSON_Parse(jsonStr);
231 // TODO: Add error log & return default secure port
235 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
238 // TODO: Add error log & return default secure port
242 jsonP = cJSON_GetObjectItem(jsonProp, "p");
245 // TODO: Add error log & return default secure port
249 jsonPort = cJSON_GetObjectItem(jsonP, "port");
252 // TODO: Add error log & return default secure port
256 return (uint16_t)jsonPort->valueint;
261 * Callback handler for getting secure port information using /oic/res discovery.
263 * @param[in] ctx user context
264 * @param[in] handle Handle for response
265 * @param[in] clientResponse Response information(It will contain payload)
267 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
268 * OC_STACK_DELETE_TRANSACTION to delete it.
270 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx,
271 OCDoHandle handle, OCClientResponse *clientResponse)
275 OC_LOG(ERROR, TAG, "Lost List of device information");
276 return OC_STACK_KEEP_TRANSACTION;
281 if (NULL == clientResponse->payload)
283 OC_LOG(INFO, TAG, "Skiping Null payload");
287 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
289 OC_LOG(INFO, TAG, "Wrong payload type");
290 return OC_STACK_KEEP_TRANSACTION;
293 OCDiscoveryPayload* discover = (OCDiscoveryPayload*) clientResponse->payload;
294 uint16_t securePort = 0;
296 if (discover && discover->resources && discover->resources->secure)
298 securePort = discover->resources->port;
302 OC_LOG(INFO, TAG, "Secure Port info is missing");
303 return OC_STACK_KEEP_TRANSACTION;
306 OCProvisionDev_t** ppDevicesList = (OCProvisionDev_t**) ctx;
308 OCStackResult res = UpdateSecurePortOfDevice(ppDevicesList, clientResponse->devAddr.addr,
309 clientResponse->devAddr.port, securePort);
310 if (OC_STACK_OK != res)
312 OC_LOG(ERROR, TAG, "Error while getting secure port.");
313 return OC_STACK_KEEP_TRANSACTION;
315 OC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
318 return OC_STACK_KEEP_TRANSACTION;
322 OC_LOG(INFO, TAG, "Skiping Null response");
324 return OC_STACK_DELETE_TRANSACTION;
328 * Callback handler for PMDeviceDiscovery API.
330 * @param[in] ctx User context
331 * @param[in] handle Handler for response
332 * @param[in] clientResponse Response information (It will contain payload)
333 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
334 * OC_STACK_DELETE_TRANSACTION to delete it.
336 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx,
337 OCDoHandle handle, OCClientResponse *clientResponse)
341 OC_LOG(ERROR, TAG, "Lost List of device information");
342 return OC_STACK_KEEP_TRANSACTION;
346 if (NULL == clientResponse->payload)
348 OC_LOG(INFO, TAG, "Skiping Null payload");
349 return OC_STACK_KEEP_TRANSACTION;
351 if (OC_STACK_OK != clientResponse->result)
353 OC_LOG(INFO, TAG, "Error in response");
354 return OC_STACK_KEEP_TRANSACTION;
358 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
360 OC_LOG(INFO, TAG, "Unknown payload type");
361 return OC_STACK_KEEP_TRANSACTION;
363 OicSecDoxm_t *ptrDoxm = JSONToDoxmBin(
364 ((OCSecurityPayload*)clientResponse->payload)->securityData);
367 OC_LOG(INFO, TAG, "Ignoring malformed JSON");
368 return OC_STACK_KEEP_TRANSACTION;
372 OC_LOG(DEBUG, TAG, "Successfully converted doxm json to bin.");
374 OCProvisionDev_t **ppDevicesList = (OCProvisionDev_t**) ctx;
376 OCStackResult res = AddDevice(ppDevicesList, clientResponse->devAddr.addr,
377 clientResponse->devAddr.port,
378 clientResponse->devAddr.adapter, ptrDoxm);
379 if (OC_STACK_OK != res)
381 OC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
382 DeleteDoxmBinData(ptrDoxm);
383 return OC_STACK_KEEP_TRANSACTION;
386 //Try to the unicast discovery to getting secure port
387 char query[MAX_QUERY_LENGTH] = { 0, };
388 sprintf(query, "%s%s:%d%s",
390 clientResponse->devAddr.addr, clientResponse->devAddr.port,
391 OC_RSRVD_WELL_KNOWN_URI);
393 OCCallbackData cbData;
394 cbData.cb = &SecurePortDiscoveryHandler;
395 cbData.context = ctx;
397 OCStackResult ret = OCDoResource(NULL, OC_REST_GET, query, 0, 0,
398 CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
399 // TODO: Should we use the default secure port in case of error?
400 if(OC_STACK_OK != ret)
402 UpdateSecurePortOfDevice(ppDevicesList, clientResponse->devAddr.addr,
403 clientResponse->devAddr.port, DEFAULT_SECURE_PORT);
407 OC_LOG_V(ERROR, TAG, "OCDoResource with [%s] Success", query);
409 OC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
412 return OC_STACK_KEEP_TRANSACTION;
417 OC_LOG(INFO, TAG, "Skiping Null response");
418 return OC_STACK_KEEP_TRANSACTION;
421 return OC_STACK_DELETE_TRANSACTION;
425 * Discover owned/unowned devices in the same IP subnet. .
427 * @param[in] waittime Timeout in seconds.
428 * @param[in] isOwned bool flag for owned / unowned discovery
429 * @param[in] ppDevicesList List of OCProvisionDev_t.
431 * @return OC_STACK_OK on success otherwise error.
433 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
435 OC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
437 if (NULL != *ppDevicesList)
439 OC_LOG(ERROR, TAG, "List is not null can cause memory leak");
440 return OC_STACK_INVALID_PARAM;
443 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
444 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
446 OCCallbackData cbData;
447 cbData.cb = &DeviceDiscoveryHandler;
448 cbData.context = (void *)ppDevicesList;
450 OCStackResult res = OC_STACK_ERROR;
452 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
453 DOXM_OWNED_FALSE_MULTICAST_QUERY;
455 res = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
456 CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
457 if (res != OC_STACK_OK)
459 OC_LOG(ERROR, TAG, "OCStack resource error");
463 //Waiting for each response.
464 res = PMTimeout(waittime);
465 if(OC_STACK_OK != res)
467 OC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
471 OC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");