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
24 #include "iotivity_config.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
39 #include "ocpayload.h"
41 #include "securevirtualresourcetypes.h"
42 #include "srmresourcestrings.h" //@note: SRM's internal header
43 #include "doxmresource.h" //@note: SRM's internal header
44 #include "pstatresource.h" //@note: SRM's internal header
45 #include "verresource.h" //@note: SRM's internal header
48 #include "pmutility.h"
50 #include "srmutility.h"
52 #define TAG ("PM-UTILITY")
54 typedef struct _DiscoveryInfo{
55 OCProvisionDev_t **ppDevicesList;
56 bool isOwnedDiscovery;
57 bool isSingleDiscovery;
59 const OicUuid_t *targetId;
63 * Function to discover secre port information through unicast
65 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
66 * @param[in] clientResponse Response information(It will contain payload)
68 * @return OC_STACK_OK on success otherwise error.
70 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
71 const OCClientResponse *clientResponse);
74 * Function to discover security version information through unicast
76 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
77 * @param[in] clientResponse Response information(It will contain payload)
79 * @return OC_STACK_OK on success otherwise error.
81 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
82 const OCClientResponse *clientResponse);
85 * Callback handler for PMDeviceDiscovery API.
87 * @param[in] ctx User context
88 * @param[in] handle Handler for response
89 * @param[in] clientResponse Response information (It will contain payload)
90 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
91 * OC_STACK_DELETE_TRANSACTION to delete it.
93 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
94 OCClientResponse *clientResponse);
97 * Callback handler for getting secure port information using /oic/res discovery.
99 * @param[in] ctx user context
100 * @param[in] handle Handle for response
101 * @param[in] clientResponse Response information(It will contain payload)
103 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
104 * OC_STACK_DELETE_TRANSACTION to delete it.
106 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
107 OCClientResponse *clientResponse);
110 * Callback handler for security version discovery.
112 * @param[in] ctx User context
113 * @param[in] handle Handler for response
114 * @param[in] clientResponse Response information (It will contain payload)
115 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
116 * OC_STACK_DELETE_TRANSACTION to delete it.
118 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
119 OCClientResponse *clientResponse);
122 * Function to search node in linked list that matches given IP and port.
124 * @param[in] pList List of OCProvisionDev_t.
125 * @param[in] addr address of target device.
126 * @param[in] port port of remote server.
128 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
130 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
132 if(NULL == addr || NULL == *ppDevicesList)
134 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
138 OCProvisionDev_t *ptr = NULL;
139 LL_FOREACH(*ppDevicesList, ptr)
141 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
152 * Add device information to list.
154 * @param[in] pList List of OCProvisionDev_t.
155 * @param[in] endpoint target device endpoint.
156 * @param[in] connType connectivity type of endpoint
157 * @param[in] doxm pointer to doxm instance.
159 * @return OC_STACK_OK for success and error code otherwise.
161 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
162 OCConnectivityType connType, OicSecDoxm_t *doxm)
164 if (NULL == endpoint)
166 return OC_STACK_INVALID_PARAM;
169 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
172 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
175 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
176 return OC_STACK_NO_MEMORY;
179 ptr->endpoint = *endpoint;
181 ptr->securePort = DEFAULT_SECURE_PORT;
183 ptr->connType = connType;
184 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
185 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
187 LL_PREPEND(*ppDevicesList, ptr);
194 * Function to set secure port information from the given list of devices.
196 * @param[in] pList List of OCProvisionDev_t.
197 * @param[in] addr address of target device.
198 * @param[in] port port of remote server.
199 * @param[in] secureport secure port information.
201 * @return OC_STACK_OK for success and errorcode otherwise.
203 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
204 uint16_t port, uint16_t securePort
210 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
214 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
215 return OC_STACK_ERROR;
218 ptr->securePort = securePort;
221 ptr->tcpPort = tcpPort;
228 * Function to set security version information from the given list of devices.
230 * @param[in] pList List of OCProvisionDev_t.
231 * @param[in] addr address of target device.
232 * @param[in] port port of remote server.
233 * @param[in] secVer security version information.
235 * @return OC_STACK_OK for success and errorcode otherwise.
237 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
238 uint16_t port, const char* secVer)
242 return OC_STACK_INVALID_PARAM;
245 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
249 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
250 return OC_STACK_ERROR;
253 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
259 * This function deletes list of provision target devices
261 * @param[in] pDevicesList List of OCProvisionDev_t.
263 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
267 OCProvisionDev_t *del = NULL, *tmp = NULL;
268 LL_FOREACH_SAFE(pDevicesList, del, tmp)
270 LL_DELETE(pDevicesList, del);
272 DeleteDoxmBinData(del->doxm);
273 DeletePstatBinData(del->pstat);
279 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
281 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
285 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
289 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
290 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
291 VERIFY_NON_NULL(TAG, newDev, ERROR);
293 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
297 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
298 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
300 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
301 // We have to assign NULL for not necessary information to prevent memory corruption.
302 newDev->pstat->sm = NULL;
307 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
308 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
310 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
311 // We have to assign NULL for not necessary information to prevent memory corruption.
312 newDev->doxm->oxmType = NULL;
313 newDev->doxm->oxm = NULL;
316 if (0 == strlen(src->secVer))
318 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
322 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
325 newDev->securePort = src->securePort;
326 newDev->devStatus = src->devStatus;
327 newDev->connType = src->connType;
330 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
335 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
338 OICFree(newDev->pstat);
339 OICFree(newDev->doxm);
346 * Timeout implementation for secure discovery. When performing secure discovery,
347 * we should wait a certain period of time for getting response of each devices.
349 * @param[in] waittime Timeout in seconds.
350 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
351 * @return OC_STACK_OK on success otherwise error.
353 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
355 OCStackResult res = OC_STACK_OK;
357 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
358 while (OC_STACK_OK == res)
360 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
362 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
363 if (elapsed > waittime)
367 if (waitForStackResponse)
376 * Extract secure port information from payload of discovery response.
378 * @param[in] jsonStr response payload of /oic/res discovery.
380 * @return Secure port
382 uint16_t GetSecurePortFromJSON(char* jsonStr)
384 // TODO: Modify error handling
389 cJSON *jsonProp = NULL;
391 cJSON *jsonPort = NULL;
393 cJSON *jsonRoot = cJSON_Parse(jsonStr);
396 // TODO: Add error log & return default secure port
400 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
403 // TODO: Add error log & return default secure port
407 jsonP = cJSON_GetObjectItem(jsonProp, "p");
410 // TODO: Add error log & return default secure port
414 jsonPort = cJSON_GetObjectItem(jsonP, "port");
417 // TODO: Add error log & return default secure port
421 return (uint16_t)jsonPort->valueint;
424 bool PMGenerateQuery(bool isSecure,
425 const char* address, uint16_t port,
426 OCConnectivityType connType,
427 char* buffer, size_t bufferSize, const char* uri)
429 if(!address || !buffer || !uri)
431 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
436 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
438 switch(connType & CT_MASK_ADAPTER)
441 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
443 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
446 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
447 prefix, address, port, uri);
450 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
451 prefix, address, port, uri);
454 OIC_LOG(ERROR, TAG, "Unknown address format.");
457 // snprintf return value check
460 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
463 else if ((size_t)snRet >= bufferSize)
465 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
470 // TODO: We need to verify tinyDTLS in below cases
471 case CT_ADAPTER_GATT_BTLE:
472 case CT_ADAPTER_RFCOMM_BTEDR:
473 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
477 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
484 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
485 OCClientResponse *clientResponse)
489 OIC_LOG(ERROR, TAG, "Lost List of device information");
490 return OC_STACK_KEEP_TRANSACTION;
495 if (NULL == clientResponse->payload)
497 OIC_LOG(INFO, TAG, "Skiping Null payload");
498 return OC_STACK_KEEP_TRANSACTION;
500 if (OC_STACK_OK != clientResponse->result)
502 OIC_LOG(INFO, TAG, "Error in response");
503 return OC_STACK_KEEP_TRANSACTION;
507 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
509 OIC_LOG(INFO, TAG, "Unknown payload type");
510 return OC_STACK_KEEP_TRANSACTION;
513 OicSecVer_t *ptrVer = NULL;
514 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
515 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
517 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
518 if ((NULL == ptrVer) && (OC_STACK_OK != res))
520 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
521 return OC_STACK_KEEP_TRANSACTION;
525 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
527 //If this is owend device discovery we have to filter out the responses.
528 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
529 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
530 clientResponse->devAddr.port, ptrVer->secv);
531 if (OC_STACK_OK != res)
533 OIC_LOG(ERROR, TAG, "Error while getting security version.");
534 DeleteVerBinData(ptrVer);
535 return OC_STACK_KEEP_TRANSACTION;
538 OIC_LOG(INFO, TAG, "= Discovered security version =");
539 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
540 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
541 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
543 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
544 DeleteVerBinData(ptrVer);
550 OIC_LOG(INFO, TAG, "Skiping Null response");
551 return OC_STACK_KEEP_TRANSACTION;
554 return OC_STACK_DELETE_TRANSACTION;
557 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
558 OCClientResponse *clientResponse)
562 OIC_LOG(ERROR, TAG, "Lost List of device information");
563 return OC_STACK_DELETE_TRANSACTION;
568 if (NULL == clientResponse->payload)
570 OIC_LOG(INFO, TAG, "Skiping Null payload");
574 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
576 OIC_LOG(INFO, TAG, "Wrong payload type");
577 return OC_STACK_DELETE_TRANSACTION;
580 uint16_t securePort = 0;
581 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
583 // Use seure port of doxm for OTM and Provision.
586 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
588 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
589 OIC_LOG(INFO, TAG, "Found doxm resource.");
594 resPayload = resPayload->next;
597 if (NULL == resPayload)
599 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
600 return OC_STACK_DELETE_TRANSACTION;
602 if (resPayload && resPayload->secure)
604 securePort = resPayload->port;
608 OIC_LOG(INFO, TAG, "Can not find secure port information.");
609 return OC_STACK_DELETE_TRANSACTION;
612 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
614 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
615 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
616 clientResponse->devAddr.addr,
617 clientResponse->devAddr.port,
623 if (OC_STACK_OK != res)
625 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
626 return OC_STACK_DELETE_TRANSACTION;
629 if(pDInfo->isSingleDiscovery)
631 pDInfo->isFound = true;
635 * Since security version discovery does not used anymore, disable security version discovery.
636 * Need to discussion to removing all version discovery related codes.
639 res = SecurityVersionDiscovery(pDInfo, clientResponse);
640 if(OC_STACK_OK != res)
642 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
643 return OC_STACK_DELETE_TRANSACTION;
647 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
650 return OC_STACK_DELETE_TRANSACTION;
654 OIC_LOG(INFO, TAG, "Skiping Null response");
657 return OC_STACK_DELETE_TRANSACTION;
660 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
661 OCClientResponse *clientResponse)
665 OIC_LOG(ERROR, TAG, "Lost List of device information");
666 return OC_STACK_KEEP_TRANSACTION;
671 if (NULL == clientResponse->payload)
673 OIC_LOG(INFO, TAG, "Skiping Null payload");
674 return OC_STACK_KEEP_TRANSACTION;
676 if (OC_STACK_OK != clientResponse->result)
678 OIC_LOG(INFO, TAG, "Error in response");
679 return OC_STACK_KEEP_TRANSACTION;
683 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
685 OIC_LOG(INFO, TAG, "Unknown payload type");
686 return OC_STACK_KEEP_TRANSACTION;
689 OicSecDoxm_t *ptrDoxm = NULL;
690 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
691 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
693 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
694 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
696 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
697 return OC_STACK_KEEP_TRANSACTION;
701 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
703 //If this is owend device discovery we have to filter out the responses.
704 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
705 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
707 // Get my device ID from doxm resource
709 memset(&myId, 0, sizeof(myId));
710 OCStackResult res = GetDoxmDevOwnerId(&myId);
711 if(OC_STACK_OK != res)
713 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
714 DeleteDoxmBinData(ptrDoxm);
715 return OC_STACK_KEEP_TRANSACTION;
718 // If this is owned discovery response but owner is not me then discard it.
719 if( (pDInfo->isOwnedDiscovery) &&
720 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
722 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
723 DeleteDoxmBinData(ptrDoxm);
724 return OC_STACK_KEEP_TRANSACTION;
727 res = GetDoxmDeviceID(&myId);
728 if(OC_STACK_OK != res)
730 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
731 DeleteDoxmBinData(ptrDoxm);
732 return OC_STACK_KEEP_TRANSACTION;
734 //if targetId and discovered deviceID are different, discard it
735 if ((pDInfo->isSingleDiscovery) &&
736 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
738 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
739 DeleteDoxmBinData(ptrDoxm);
740 return OC_STACK_KEEP_TRANSACTION;
742 //if this is owned discovery and this is PT's reply, discard it
743 if (((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
744 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
746 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
747 DeleteDoxmBinData(ptrDoxm);
748 return OC_STACK_KEEP_TRANSACTION;
751 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
752 clientResponse->connType, ptrDoxm);
753 if (OC_STACK_OK != res)
755 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
756 DeleteDoxmBinData(ptrDoxm);
757 return OC_STACK_KEEP_TRANSACTION;
760 res = SecurePortDiscovery(pDInfo, clientResponse);
761 if(OC_STACK_OK != res)
763 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
764 DeleteDoxmBinData(ptrDoxm);
765 return OC_STACK_KEEP_TRANSACTION;
768 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
769 if(pDInfo->isSingleDiscovery)
771 return OC_STACK_DELETE_TRANSACTION;
775 return OC_STACK_KEEP_TRANSACTION;
780 OIC_LOG(INFO, TAG, "Skiping Null response");
781 return OC_STACK_KEEP_TRANSACTION;
784 return OC_STACK_DELETE_TRANSACTION;
789 * Discover owned/unowned device in the specified endpoint/deviceID.
790 * It will return the found device even though timeout is not exceeded.
792 * @param[in] waittime Timeout in seconds
793 * @param[in] deviceID deviceID of target device.
794 * @param[out] ppFoundDevice OCProvisionDev_t of found device
796 * @return OC_STACK_OK on success otherwise error.\n
797 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
799 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
800 OCProvisionDev_t **ppFoundDevice)
802 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
804 if (NULL != *ppFoundDevice)
806 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
807 return OC_STACK_INVALID_PARAM;
810 if (NULL == deviceID)
812 OIC_LOG(ERROR, TAG, "Invalid device ID");
813 return OC_STACK_INVALID_PARAM;
817 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
820 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
821 return OC_STACK_NO_MEMORY;
824 pDInfo->ppDevicesList = ppFoundDevice;
825 pDInfo->isOwnedDiscovery = false;
826 pDInfo->isSingleDiscovery = true;
827 pDInfo->isFound = false;
828 pDInfo->targetId = deviceID;
830 OCCallbackData cbData;
831 cbData.cb = &DeviceDiscoveryHandler;
832 cbData.context = (void *)pDInfo;
834 OCStackResult res = OC_STACK_ERROR;
836 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
837 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
839 OCDoHandle handle = NULL;
840 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
841 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
842 if (res != OC_STACK_OK)
844 OIC_LOG(ERROR, TAG, "OCStack resource error");
849 //Waiting for each response.
851 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
852 while (OC_STACK_OK == res && !pDInfo->isFound)
854 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
856 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
857 if (elapsed > waittime)
864 if(OC_STACK_OK != res)
866 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
868 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
869 if(OC_STACK_OK != resCancel)
871 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
876 if(NULL == *ppFoundDevice)
878 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
881 if (OC_STACK_OK != res)
883 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
887 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
894 * Discover owned/unowned devices in the same IP subnet. .
896 * @param[in] waittime Timeout in seconds.
897 * @param[in] isOwned bool flag for owned / unowned discovery
898 * @param[in] ppDevicesList List of OCProvisionDev_t.
900 * @return OC_STACK_OK on success otherwise error.
902 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
904 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
906 if (NULL != *ppDevicesList)
908 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
909 return OC_STACK_INVALID_PARAM;
912 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
913 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
915 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
918 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
919 return OC_STACK_NO_MEMORY;
922 pDInfo->ppDevicesList = ppDevicesList;
923 pDInfo->isOwnedDiscovery = isOwned;
924 pDInfo->isSingleDiscovery = false;
925 pDInfo->targetId = NULL;
927 OCCallbackData cbData;
928 cbData.cb = &DeviceDiscoveryHandler;
929 cbData.context = (void *)pDInfo;
931 OCStackResult res = OC_STACK_ERROR;
933 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
934 DOXM_OWNED_FALSE_MULTICAST_QUERY;
936 OCDoHandle handle = NULL;
937 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
938 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
939 if (res != OC_STACK_OK)
941 OIC_LOG(ERROR, TAG, "OCStack resource error");
946 //Waiting for each response.
947 res = PMTimeout(waittime, true);
948 if(OC_STACK_OK != res)
950 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
952 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
953 if(OC_STACK_OK != resCancel)
955 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
959 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
960 if (OC_STACK_OK != res)
962 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
966 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
971 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
972 const OCClientResponse *clientResponse)
974 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
976 if(NULL == discoveryInfo || NULL == clientResponse)
978 return OC_STACK_INVALID_PARAM;
980 //Try to the unicast discovery to getting secure port
981 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
982 if(!PMGenerateQuery(false,
983 clientResponse->devAddr.addr, clientResponse->devAddr.port,
984 clientResponse->connType,
985 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
987 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
988 return OC_STACK_ERROR;
990 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
992 OCCallbackData cbData;
993 cbData.cb = &SecurePortDiscoveryHandler;
994 cbData.context = (void*)discoveryInfo;
996 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
997 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
998 if(OC_STACK_OK != ret)
1000 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
1005 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1008 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1013 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1014 const OCClientResponse *clientResponse)
1016 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1018 if(NULL == discoveryInfo || NULL == clientResponse)
1020 return OC_STACK_INVALID_PARAM;
1023 //Try to the unicast discovery to getting security version
1024 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1025 if(!PMGenerateQuery(false,
1026 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1027 clientResponse->connType,
1028 query, sizeof(query), OIC_RSRC_VER_URI))
1030 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1031 return OC_STACK_ERROR;
1033 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1035 OCCallbackData cbData;
1036 cbData.cb = &SecurityVersionDiscoveryHandler;
1037 cbData.context = (void*)discoveryInfo;
1039 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1040 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1041 if(OC_STACK_OK != ret)
1043 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1048 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1051 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1057 * Function to print OCProvisionDev_t for debug purpose.
1059 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1062 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1066 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1067 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1068 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1069 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1070 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1074 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1078 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1080 if(*pUuidList == NULL || targetId == NULL)
1084 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1085 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1087 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1089 LL_DELETE(*pUuidList, tmp1);