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 = {.tv_sec=0, .tv_nsec=0};
176 struct timespec currTime = {.tv_sec=0, .tv_nsec=0};
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, OCDoHandle UNUSED,
271 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, OCDoHandle UNUSED,
337 OCClientResponse *clientResponse)
341 OC_LOG(ERROR, TAG, "Lost List of device information");
342 return OC_STACK_KEEP_TRANSACTION;
347 if (NULL == clientResponse->payload)
349 OC_LOG(INFO, TAG, "Skiping Null payload");
350 return OC_STACK_KEEP_TRANSACTION;
352 if (OC_STACK_OK != clientResponse->result)
354 OC_LOG(INFO, TAG, "Error in response");
355 return OC_STACK_KEEP_TRANSACTION;
359 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
361 OC_LOG(INFO, TAG, "Unknown payload type");
362 return OC_STACK_KEEP_TRANSACTION;
364 OicSecDoxm_t *ptrDoxm = JSONToDoxmBin(
365 ((OCSecurityPayload*)clientResponse->payload)->securityData);
368 OC_LOG(INFO, TAG, "Ignoring malformed JSON");
369 return OC_STACK_KEEP_TRANSACTION;
373 OC_LOG(DEBUG, TAG, "Successfully converted doxm json to bin.");
375 OCProvisionDev_t **ppDevicesList = (OCProvisionDev_t**) ctx;
377 OCStackResult res = AddDevice(ppDevicesList, clientResponse->devAddr.addr,
378 clientResponse->devAddr.port,
379 clientResponse->devAddr.adapter, ptrDoxm);
380 if (OC_STACK_OK != res)
382 OC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
383 DeleteDoxmBinData(ptrDoxm);
384 return OC_STACK_KEEP_TRANSACTION;
387 //Try to the unicast discovery to getting secure port
388 char query[MAX_QUERY_LENGTH] = { 0, };
389 sprintf(query, "%s%s:%d%s",
391 clientResponse->devAddr.addr, clientResponse->devAddr.port,
392 OC_RSRVD_WELL_KNOWN_URI);
394 OCCallbackData cbData;
395 cbData.cb = &SecurePortDiscoveryHandler;
396 cbData.context = ctx;
398 OCStackResult ret = OCDoResource(NULL, OC_REST_GET, query, 0, 0,
399 CT_ADAPTER_IP, OC_LOW_QOS, &cbData, NULL, 0);
400 // TODO: Should we use the default secure port in case of error?
401 if(OC_STACK_OK != ret)
403 UpdateSecurePortOfDevice(ppDevicesList, clientResponse->devAddr.addr,
404 clientResponse->devAddr.port, DEFAULT_SECURE_PORT);
408 OC_LOG_V(ERROR, TAG, "OCDoResource with [%s] Success", query);
410 OC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
413 return OC_STACK_KEEP_TRANSACTION;
418 OC_LOG(INFO, TAG, "Skiping Null response");
419 return OC_STACK_KEEP_TRANSACTION;
422 return OC_STACK_DELETE_TRANSACTION;
426 * Discover owned/unowned devices in the same IP subnet. .
428 * @param[in] waittime Timeout in seconds.
429 * @param[in] isOwned bool flag for owned / unowned discovery
430 * @param[in] ppDevicesList List of OCProvisionDev_t.
432 * @return OC_STACK_OK on success otherwise error.
434 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
436 OC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
438 if (NULL != *ppDevicesList)
440 OC_LOG(ERROR, TAG, "List is not null can cause memory leak");
441 return OC_STACK_INVALID_PARAM;
444 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
445 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
447 OCCallbackData cbData;
448 cbData.cb = &DeviceDiscoveryHandler;
449 cbData.context = (void *)ppDevicesList;
451 OCStackResult res = OC_STACK_ERROR;
453 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
454 DOXM_OWNED_FALSE_MULTICAST_QUERY;
456 res = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
457 CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
458 if (res != OC_STACK_OK)
460 OC_LOG(ERROR, TAG, "OCStack resource error");
464 //Waiting for each response.
465 res = PMTimeout(waittime);
466 if(OC_STACK_OK != res)
468 OC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
472 OC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");