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
32 #include "oic_malloc.h"
33 #include "oic_string.h"
38 #include "ocpayload.h"
40 #include "securevirtualresourcetypes.h"
41 #include "srmresourcestrings.h" //@note: SRM's internal header
42 #include "doxmresource.h" //@note: SRM's internal header
43 #include "pstatresource.h" //@note: SRM's internal header
44 #include "verresource.h" //@note: SRM's internal header
47 #include "pmutility.h"
49 #include "srmutility.h"
51 #define TAG ("PM-UTILITY")
53 typedef struct _DiscoveryInfo{
54 OCProvisionDev_t **ppDevicesList;
55 bool isOwnedDiscovery;
56 bool isSingleDiscovery;
61 * Function to discover secre port information through unicast
63 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
64 * @param[in] clientResponse Response information(It will contain payload)
66 * @return OC_STACK_OK on success otherwise error.
68 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
69 const OCClientResponse *clientResponse);
72 * Function to discover security version information through unicast
74 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
75 * @param[in] clientResponse Response information(It will contain payload)
77 * @return OC_STACK_OK on success otherwise error.
79 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
80 const OCClientResponse *clientResponse);
83 * Callback handler for PMDeviceDiscovery API.
85 * @param[in] ctx User context
86 * @param[in] handle Handler for response
87 * @param[in] clientResponse Response information (It will contain payload)
88 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
89 * OC_STACK_DELETE_TRANSACTION to delete it.
91 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
92 OCClientResponse *clientResponse);
95 * Callback handler for getting secure port information using /oic/res discovery.
97 * @param[in] ctx user context
98 * @param[in] handle Handle for response
99 * @param[in] clientResponse Response information(It will contain payload)
101 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
102 * OC_STACK_DELETE_TRANSACTION to delete it.
104 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
105 OCClientResponse *clientResponse);
108 * Callback handler for security version discovery.
110 * @param[in] ctx User context
111 * @param[in] handle Handler for response
112 * @param[in] clientResponse Response information (It will contain payload)
113 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
114 * OC_STACK_DELETE_TRANSACTION to delete it.
116 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
117 OCClientResponse *clientResponse);
120 * Function to search node in linked list that matches given IP and port.
122 * @param[in] pList List of OCProvisionDev_t.
123 * @param[in] addr address of target device.
124 * @param[in] port port of remote server.
126 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
128 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
130 if(NULL == addr || NULL == *ppDevicesList)
132 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
136 OCProvisionDev_t *ptr = NULL;
137 LL_FOREACH(*ppDevicesList, ptr)
139 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
150 * Add device information to list.
152 * @param[in] pList List of OCProvisionDev_t.
153 * @param[in] endpoint target device endpoint.
154 * @param[in] connType connectivity type of endpoint
155 * @param[in] doxm pointer to doxm instance.
157 * @return OC_STACK_OK for success and error code otherwise.
159 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
160 OCConnectivityType connType, OicSecDoxm_t *doxm)
162 if (NULL == endpoint)
164 return OC_STACK_INVALID_PARAM;
167 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
170 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
173 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
174 return OC_STACK_NO_MEMORY;
177 ptr->endpoint = *endpoint;
179 ptr->securePort = DEFAULT_SECURE_PORT;
181 ptr->connType = connType;
182 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
183 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
185 LL_PREPEND(*ppDevicesList, ptr);
192 * Function to set secure port information from the given list of devices.
194 * @param[in] pList List of OCProvisionDev_t.
195 * @param[in] addr address of target device.
196 * @param[in] port port of remote server.
197 * @param[in] secureport secure port information.
199 * @return OC_STACK_OK for success and errorcode otherwise.
201 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
202 uint16_t port, uint16_t securePort
208 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
212 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
213 return OC_STACK_ERROR;
216 ptr->securePort = securePort;
219 ptr->tcpPort = tcpPort;
226 * Function to set security version information from the given list of devices.
228 * @param[in] pList List of OCProvisionDev_t.
229 * @param[in] addr address of target device.
230 * @param[in] port port of remote server.
231 * @param[in] secVer security version information.
233 * @return OC_STACK_OK for success and errorcode otherwise.
235 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
236 uint16_t port, const char* secVer)
240 return OC_STACK_INVALID_PARAM;
243 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
247 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
248 return OC_STACK_ERROR;
251 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
257 * This function deletes list of provision target devices
259 * @param[in] pDevicesList List of OCProvisionDev_t.
261 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
265 OCProvisionDev_t *del = NULL, *tmp = NULL;
266 LL_FOREACH_SAFE(pDevicesList, del, tmp)
268 LL_DELETE(pDevicesList, del);
270 DeleteDoxmBinData(del->doxm);
271 DeletePstatBinData(del->pstat);
277 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
279 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
283 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
287 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
288 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
289 VERIFY_NON_NULL(TAG, newDev, ERROR);
291 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
295 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
296 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
298 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
299 // We have to assign NULL for not necessary information to prevent memory corruption.
300 newDev->pstat->sm = NULL;
305 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
306 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
308 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
309 // We have to assign NULL for not necessary information to prevent memory corruption.
310 newDev->doxm->oxmType = NULL;
311 newDev->doxm->oxm = NULL;
314 if (0 == strlen(src->secVer))
316 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
320 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
323 newDev->securePort = src->securePort;
324 newDev->devStatus = src->devStatus;
325 newDev->connType = src->connType;
328 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
333 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
336 OICFree(newDev->pstat);
337 OICFree(newDev->doxm);
344 * Timeout implementation for secure discovery. When performing secure discovery,
345 * we should wait a certain period of time for getting response of each devices.
347 * @param[in] waittime Timeout in seconds.
348 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
349 * @return OC_STACK_OK on success otherwise error.
351 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
353 OCStackResult res = OC_STACK_OK;
355 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
356 while (OC_STACK_OK == res)
358 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
360 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
361 if (elapsed > waittime)
365 if (waitForStackResponse)
374 * Extract secure port information from payload of discovery response.
376 * @param[in] jsonStr response payload of /oic/res discovery.
378 * @return Secure port
380 uint16_t GetSecurePortFromJSON(char* jsonStr)
382 // TODO: Modify error handling
387 cJSON *jsonProp = NULL;
389 cJSON *jsonPort = NULL;
391 cJSON *jsonRoot = cJSON_Parse(jsonStr);
394 // TODO: Add error log & return default secure port
398 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
401 // TODO: Add error log & return default secure port
405 jsonP = cJSON_GetObjectItem(jsonProp, "p");
408 // TODO: Add error log & return default secure port
412 jsonPort = cJSON_GetObjectItem(jsonP, "port");
415 // TODO: Add error log & return default secure port
419 return (uint16_t)jsonPort->valueint;
422 bool PMGenerateQuery(bool isSecure,
423 const char* address, uint16_t port,
424 OCConnectivityType connType,
425 char* buffer, size_t bufferSize, const char* uri)
427 if(!address || !buffer || !uri)
429 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
434 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
436 switch(connType & CT_MASK_ADAPTER)
439 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
441 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
444 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
445 prefix, address, port, uri);
448 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
449 prefix, address, port, uri);
452 OIC_LOG(ERROR, TAG, "Unknown address format.");
455 // snprintf return value check
458 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
461 else if ((size_t)snRet >= bufferSize)
463 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
468 // TODO: We need to verify tinyDTLS in below cases
469 case CT_ADAPTER_GATT_BTLE:
470 case CT_ADAPTER_RFCOMM_BTEDR:
471 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
475 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
482 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
483 OCClientResponse *clientResponse)
487 OIC_LOG(ERROR, TAG, "Lost List of device information");
488 return OC_STACK_KEEP_TRANSACTION;
493 if (NULL == clientResponse->payload)
495 OIC_LOG(INFO, TAG, "Skiping Null payload");
496 return OC_STACK_KEEP_TRANSACTION;
498 if (OC_STACK_OK != clientResponse->result)
500 OIC_LOG(INFO, TAG, "Error in response");
501 return OC_STACK_KEEP_TRANSACTION;
505 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
507 OIC_LOG(INFO, TAG, "Unknown payload type");
508 return OC_STACK_KEEP_TRANSACTION;
511 OicSecVer_t *ptrVer = NULL;
512 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
513 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
515 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
516 if ((NULL == ptrVer) && (OC_STACK_OK != res))
518 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
519 return OC_STACK_KEEP_TRANSACTION;
523 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
525 //If this is owend device discovery we have to filter out the responses.
526 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
527 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
528 clientResponse->devAddr.port, ptrVer->secv);
529 if (OC_STACK_OK != res)
531 OIC_LOG(ERROR, TAG, "Error while getting security version.");
532 DeleteVerBinData(ptrVer);
533 return OC_STACK_KEEP_TRANSACTION;
536 OIC_LOG(INFO, TAG, "= Discovered security version =");
537 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
538 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
539 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
541 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
542 DeleteVerBinData(ptrVer);
543 if(pDInfo->isSingleDiscovery)
545 pDInfo->isFound = true;
552 OIC_LOG(INFO, TAG, "Skiping Null response");
553 return OC_STACK_KEEP_TRANSACTION;
556 return OC_STACK_DELETE_TRANSACTION;
559 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
560 OCClientResponse *clientResponse)
564 OIC_LOG(ERROR, TAG, "Lost List of device information");
565 return OC_STACK_DELETE_TRANSACTION;
570 if (NULL == clientResponse->payload)
572 OIC_LOG(INFO, TAG, "Skiping Null payload");
576 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
578 OIC_LOG(INFO, TAG, "Wrong payload type");
579 return OC_STACK_DELETE_TRANSACTION;
582 uint16_t securePort = 0;
583 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
585 // Use seure port of doxm for OTM and Provision.
588 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
590 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
591 OIC_LOG(INFO, TAG, "Found doxm resource.");
596 resPayload = resPayload->next;
599 if (NULL == resPayload)
601 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
602 return OC_STACK_DELETE_TRANSACTION;
604 if (resPayload && resPayload->secure)
606 securePort = resPayload->port;
610 OIC_LOG(INFO, TAG, "Can not find secure port information.");
611 return OC_STACK_DELETE_TRANSACTION;
614 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
616 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
617 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
618 clientResponse->devAddr.addr,
619 clientResponse->devAddr.port,
625 if (OC_STACK_OK != res)
627 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
628 return OC_STACK_DELETE_TRANSACTION;
631 res = SecurityVersionDiscovery(pDInfo, clientResponse);
632 if(OC_STACK_OK != res)
634 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
635 return OC_STACK_DELETE_TRANSACTION;
638 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
641 return OC_STACK_DELETE_TRANSACTION;
645 OIC_LOG(INFO, TAG, "Skiping Null response");
648 return OC_STACK_DELETE_TRANSACTION;
651 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
652 OCClientResponse *clientResponse)
656 OIC_LOG(ERROR, TAG, "Lost List of device information");
657 return OC_STACK_KEEP_TRANSACTION;
662 if (NULL == clientResponse->payload)
664 OIC_LOG(INFO, TAG, "Skiping Null payload");
665 return OC_STACK_KEEP_TRANSACTION;
667 if (OC_STACK_OK != clientResponse->result)
669 OIC_LOG(INFO, TAG, "Error in response");
670 return OC_STACK_KEEP_TRANSACTION;
674 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
676 OIC_LOG(INFO, TAG, "Unknown payload type");
677 return OC_STACK_KEEP_TRANSACTION;
680 OicSecDoxm_t *ptrDoxm = NULL;
681 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
682 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
684 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
685 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
687 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
688 return OC_STACK_KEEP_TRANSACTION;
692 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
694 //If this is owend device discovery we have to filter out the responses.
695 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
696 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
698 // Get my device ID from doxm resource
700 memset(&myId, 0, sizeof(myId));
701 OCStackResult res = GetDoxmDevOwnerId(&myId);
702 if(OC_STACK_OK != res)
704 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
705 DeleteDoxmBinData(ptrDoxm);
706 return OC_STACK_KEEP_TRANSACTION;
709 // If this is owned discovery response but owner is not me then discard it.
710 if( (pDInfo->isOwnedDiscovery) &&
711 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
713 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
714 DeleteDoxmBinData(ptrDoxm);
715 return OC_STACK_KEEP_TRANSACTION;
718 res = GetDoxmDeviceID(&myId);
719 if(OC_STACK_OK != res)
721 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
722 DeleteDoxmBinData(ptrDoxm);
723 return OC_STACK_KEEP_TRANSACTION;
725 //if this is owned discovery and this is PT's reply, discard it
726 if(((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
727 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
729 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
730 DeleteDoxmBinData(ptrDoxm);
731 return OC_STACK_KEEP_TRANSACTION;
734 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
735 clientResponse->connType, ptrDoxm);
736 if (OC_STACK_OK != res)
738 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
739 DeleteDoxmBinData(ptrDoxm);
740 return OC_STACK_KEEP_TRANSACTION;
743 res = SecurePortDiscovery(pDInfo, clientResponse);
744 if(OC_STACK_OK != res)
746 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
747 DeleteDoxmBinData(ptrDoxm);
748 return OC_STACK_KEEP_TRANSACTION;
751 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
752 if(pDInfo->isSingleDiscovery)
754 return OC_STACK_DELETE_TRANSACTION;
758 return OC_STACK_KEEP_TRANSACTION;
763 OIC_LOG(INFO, TAG, "Skiping Null response");
764 return OC_STACK_KEEP_TRANSACTION;
767 return OC_STACK_DELETE_TRANSACTION;
772 * Discover owned/unowned devices in the specified endpoint.
773 * It will return when found one or more device even though timeout is not exceeded
775 * @param[in] waittime Timeout in seconds
776 * @param[in] host address of target endpoint
777 * @param[in] connType connectivity type of endpoint
778 * @param[out] ppDevicesList List of OCProvisionDev_t
780 * @return OC_STACK_OK on success otherwise error.
782 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
783 OCConnectivityType connType, OCProvisionDev_t **ppDevicesList)
785 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
787 if (NULL != *ppDevicesList)
789 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
790 return OC_STACK_INVALID_PARAM;
793 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
796 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
797 return OC_STACK_NO_MEMORY;
800 pDInfo->ppDevicesList = ppDevicesList;
801 pDInfo->isOwnedDiscovery = false;
802 pDInfo->isSingleDiscovery = true;
803 pDInfo->isFound = false;
805 OCCallbackData cbData;
806 cbData.cb = &DeviceDiscoveryHandler;
807 cbData.context = (void *)pDInfo;
809 OCStackResult res = OC_STACK_ERROR;
811 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
816 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "%s/oic/sec/doxm", host);
818 OCDoHandle handle = NULL;
819 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
820 connType, OC_HIGH_QOS, &cbData, NULL, 0);
821 if (res != OC_STACK_OK)
823 OIC_LOG(ERROR, TAG, "OCStack resource error");
828 //Waiting for each response.
830 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
831 while (OC_STACK_OK == res && pDInfo->isFound == false)
833 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
835 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
836 if (elapsed > waittime)
843 if(OC_STACK_OK != res)
845 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
847 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
848 if(OC_STACK_OK != resCancel)
850 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
855 if(*ppDevicesList == NULL)
857 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
860 if (OC_STACK_OK != res)
862 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
866 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
873 * Discover owned/unowned devices in the same IP subnet. .
875 * @param[in] waittime Timeout in seconds.
876 * @param[in] isOwned bool flag for owned / unowned discovery
877 * @param[in] ppDevicesList List of OCProvisionDev_t.
879 * @return OC_STACK_OK on success otherwise error.
881 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
883 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
885 if (NULL != *ppDevicesList)
887 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
888 return OC_STACK_INVALID_PARAM;
891 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
892 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
894 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
897 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
898 return OC_STACK_NO_MEMORY;
901 pDInfo->ppDevicesList = ppDevicesList;
902 pDInfo->isOwnedDiscovery = isOwned;
903 pDInfo->isSingleDiscovery = false;
905 OCCallbackData cbData;
906 cbData.cb = &DeviceDiscoveryHandler;
907 cbData.context = (void *)pDInfo;
909 OCStackResult res = OC_STACK_ERROR;
911 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
912 DOXM_OWNED_FALSE_MULTICAST_QUERY;
914 OCDoHandle handle = NULL;
915 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
916 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
917 if (res != OC_STACK_OK)
919 OIC_LOG(ERROR, TAG, "OCStack resource error");
924 //Waiting for each response.
925 res = PMTimeout(waittime, true);
926 if(OC_STACK_OK != res)
928 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
930 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
931 if(OC_STACK_OK != resCancel)
933 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
937 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
938 if (OC_STACK_OK != res)
940 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
944 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
949 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
950 const OCClientResponse *clientResponse)
952 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
954 if(NULL == discoveryInfo || NULL == clientResponse)
956 return OC_STACK_INVALID_PARAM;
958 //Try to the unicast discovery to getting secure port
959 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
960 if(!PMGenerateQuery(false,
961 clientResponse->devAddr.addr, clientResponse->devAddr.port,
962 clientResponse->connType,
963 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
965 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
966 return OC_STACK_ERROR;
968 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
970 OCCallbackData cbData;
971 cbData.cb = &SecurePortDiscoveryHandler;
972 cbData.context = (void*)discoveryInfo;
974 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
975 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
976 if(OC_STACK_OK != ret)
978 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
983 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
986 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
991 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
992 const OCClientResponse *clientResponse)
994 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
996 if(NULL == discoveryInfo || NULL == clientResponse)
998 return OC_STACK_INVALID_PARAM;
1001 //Try to the unicast discovery to getting security version
1002 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1003 if(!PMGenerateQuery(false,
1004 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1005 clientResponse->connType,
1006 query, sizeof(query), OIC_RSRC_VER_URI))
1008 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1009 return OC_STACK_ERROR;
1011 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1013 OCCallbackData cbData;
1014 cbData.cb = &SecurityVersionDiscoveryHandler;
1015 cbData.context = (void*)discoveryInfo;
1017 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1018 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1019 if(OC_STACK_OK != ret)
1021 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1026 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1029 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1035 * Function to print OCProvisionDev_t for debug purpose.
1037 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1040 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1044 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1045 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1046 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1047 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1048 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1052 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1056 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1058 if(*pUuidList == NULL || targetId == NULL)
1062 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1063 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1065 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1067 LL_DELETE(*pUuidList, tmp1);