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);
545 if(pDInfo->isSingleDiscovery)
547 pDInfo->isFound = true;
554 OIC_LOG(INFO, TAG, "Skiping Null response");
555 return OC_STACK_KEEP_TRANSACTION;
558 return OC_STACK_DELETE_TRANSACTION;
561 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
562 OCClientResponse *clientResponse)
566 OIC_LOG(ERROR, TAG, "Lost List of device information");
567 return OC_STACK_DELETE_TRANSACTION;
572 if (NULL == clientResponse->payload)
574 OIC_LOG(INFO, TAG, "Skiping Null payload");
578 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
580 OIC_LOG(INFO, TAG, "Wrong payload type");
581 return OC_STACK_DELETE_TRANSACTION;
584 uint16_t securePort = 0;
585 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
587 // Use seure port of doxm for OTM and Provision.
590 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
592 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
593 OIC_LOG(INFO, TAG, "Found doxm resource.");
598 resPayload = resPayload->next;
601 if (NULL == resPayload)
603 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
604 return OC_STACK_DELETE_TRANSACTION;
606 if (resPayload && resPayload->secure)
608 securePort = resPayload->port;
612 OIC_LOG(INFO, TAG, "Can not find secure port information.");
613 return OC_STACK_DELETE_TRANSACTION;
616 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
618 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
619 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
620 clientResponse->devAddr.addr,
621 clientResponse->devAddr.port,
627 if (OC_STACK_OK != res)
629 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
630 return OC_STACK_DELETE_TRANSACTION;
633 res = SecurityVersionDiscovery(pDInfo, clientResponse);
634 if(OC_STACK_OK != res)
636 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
637 return OC_STACK_DELETE_TRANSACTION;
640 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
643 return OC_STACK_DELETE_TRANSACTION;
647 OIC_LOG(INFO, TAG, "Skiping Null response");
650 return OC_STACK_DELETE_TRANSACTION;
653 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
654 OCClientResponse *clientResponse)
658 OIC_LOG(ERROR, TAG, "Lost List of device information");
659 return OC_STACK_KEEP_TRANSACTION;
664 if (NULL == clientResponse->payload)
666 OIC_LOG(INFO, TAG, "Skiping Null payload");
667 return OC_STACK_KEEP_TRANSACTION;
669 if (OC_STACK_OK != clientResponse->result)
671 OIC_LOG(INFO, TAG, "Error in response");
672 return OC_STACK_KEEP_TRANSACTION;
676 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
678 OIC_LOG(INFO, TAG, "Unknown payload type");
679 return OC_STACK_KEEP_TRANSACTION;
682 OicSecDoxm_t *ptrDoxm = NULL;
683 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
684 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
686 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
687 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
689 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
690 return OC_STACK_KEEP_TRANSACTION;
694 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
696 //If this is owend device discovery we have to filter out the responses.
697 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
698 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
700 // Get my device ID from doxm resource
702 memset(&myId, 0, sizeof(myId));
703 OCStackResult res = GetDoxmDevOwnerId(&myId);
704 if(OC_STACK_OK != res)
706 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
707 DeleteDoxmBinData(ptrDoxm);
708 return OC_STACK_KEEP_TRANSACTION;
711 // If this is owned discovery response but owner is not me then discard it.
712 if( (pDInfo->isOwnedDiscovery) &&
713 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
715 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
716 DeleteDoxmBinData(ptrDoxm);
717 return OC_STACK_KEEP_TRANSACTION;
720 res = GetDoxmDeviceID(&myId);
721 if(OC_STACK_OK != res)
723 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
724 DeleteDoxmBinData(ptrDoxm);
725 return OC_STACK_KEEP_TRANSACTION;
727 //if targetId and discovered deviceID are different, discard it
728 if ((pDInfo->isSingleDiscovery) &&
729 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
731 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
732 DeleteDoxmBinData(ptrDoxm);
733 return OC_STACK_KEEP_TRANSACTION;
735 //if this is owned discovery and this is PT's reply, discard it
736 if (((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
737 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
739 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
740 DeleteDoxmBinData(ptrDoxm);
741 return OC_STACK_KEEP_TRANSACTION;
744 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
745 clientResponse->connType, ptrDoxm);
746 if (OC_STACK_OK != res)
748 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
749 DeleteDoxmBinData(ptrDoxm);
750 return OC_STACK_KEEP_TRANSACTION;
753 res = SecurePortDiscovery(pDInfo, clientResponse);
754 if(OC_STACK_OK != res)
756 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
757 DeleteDoxmBinData(ptrDoxm);
758 return OC_STACK_KEEP_TRANSACTION;
761 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
762 if(pDInfo->isSingleDiscovery)
764 return OC_STACK_DELETE_TRANSACTION;
768 return OC_STACK_KEEP_TRANSACTION;
773 OIC_LOG(INFO, TAG, "Skiping Null response");
774 return OC_STACK_KEEP_TRANSACTION;
777 return OC_STACK_DELETE_TRANSACTION;
782 * Discover owned/unowned device in the specified endpoint/deviceID.
783 * It will return the found device even though timeout is not exceeded.
785 * @param[in] waittime Timeout in seconds
786 * @param[in] deviceID deviceID of target device.
787 * @param[out] ppFoundDevice OCProvisionDev_t of found device
789 * @return OC_STACK_OK on success otherwise error.\n
790 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
792 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
793 OCProvisionDev_t **ppFoundDevice)
795 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
797 if (NULL != *ppFoundDevice)
799 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
800 return OC_STACK_INVALID_PARAM;
803 if (NULL == deviceID)
805 OIC_LOG(ERROR, TAG, "Invalid device ID");
806 return OC_STACK_INVALID_PARAM;
810 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
813 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
814 return OC_STACK_NO_MEMORY;
817 pDInfo->ppDevicesList = ppFoundDevice;
818 pDInfo->isOwnedDiscovery = false;
819 pDInfo->isSingleDiscovery = true;
820 pDInfo->isFound = false;
821 pDInfo->targetId = deviceID;
823 OCCallbackData cbData;
824 cbData.cb = &DeviceDiscoveryHandler;
825 cbData.context = (void *)pDInfo;
827 OCStackResult res = OC_STACK_ERROR;
829 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
830 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
832 OCDoHandle handle = NULL;
833 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
834 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
835 if (res != OC_STACK_OK)
837 OIC_LOG(ERROR, TAG, "OCStack resource error");
842 //Waiting for each response.
844 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
845 while (OC_STACK_OK == res && !pDInfo->isFound)
847 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
849 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
850 if (elapsed > waittime)
857 if(OC_STACK_OK != res)
859 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
861 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
862 if(OC_STACK_OK != resCancel)
864 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
869 if(NULL == *ppFoundDevice)
871 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
874 if (OC_STACK_OK != res)
876 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
880 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
887 * Discover owned/unowned devices in the same IP subnet. .
889 * @param[in] waittime Timeout in seconds.
890 * @param[in] isOwned bool flag for owned / unowned discovery
891 * @param[in] ppDevicesList List of OCProvisionDev_t.
893 * @return OC_STACK_OK on success otherwise error.
895 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
897 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
899 if (NULL != *ppDevicesList)
901 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
902 return OC_STACK_INVALID_PARAM;
905 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
906 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
908 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
911 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
912 return OC_STACK_NO_MEMORY;
915 pDInfo->ppDevicesList = ppDevicesList;
916 pDInfo->isOwnedDiscovery = isOwned;
917 pDInfo->isSingleDiscovery = false;
918 pDInfo->targetId = NULL;
920 OCCallbackData cbData;
921 cbData.cb = &DeviceDiscoveryHandler;
922 cbData.context = (void *)pDInfo;
924 OCStackResult res = OC_STACK_ERROR;
926 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
927 DOXM_OWNED_FALSE_MULTICAST_QUERY;
929 OCDoHandle handle = NULL;
930 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
931 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
932 if (res != OC_STACK_OK)
934 OIC_LOG(ERROR, TAG, "OCStack resource error");
939 //Waiting for each response.
940 res = PMTimeout(waittime, true);
941 if(OC_STACK_OK != res)
943 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
945 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
946 if(OC_STACK_OK != resCancel)
948 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
952 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
953 if (OC_STACK_OK != res)
955 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
959 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
964 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
965 const OCClientResponse *clientResponse)
967 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
969 if(NULL == discoveryInfo || NULL == clientResponse)
971 return OC_STACK_INVALID_PARAM;
973 //Try to the unicast discovery to getting secure port
974 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
975 if(!PMGenerateQuery(false,
976 clientResponse->devAddr.addr, clientResponse->devAddr.port,
977 clientResponse->connType,
978 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
980 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
981 return OC_STACK_ERROR;
983 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
985 OCCallbackData cbData;
986 cbData.cb = &SecurePortDiscoveryHandler;
987 cbData.context = (void*)discoveryInfo;
989 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
990 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
991 if(OC_STACK_OK != ret)
993 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
998 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1001 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1006 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1007 const OCClientResponse *clientResponse)
1009 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1011 if(NULL == discoveryInfo || NULL == clientResponse)
1013 return OC_STACK_INVALID_PARAM;
1016 //Try to the unicast discovery to getting security version
1017 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1018 if(!PMGenerateQuery(false,
1019 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1020 clientResponse->connType,
1021 query, sizeof(query), OIC_RSRC_VER_URI))
1023 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1024 return OC_STACK_ERROR;
1026 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1028 OCCallbackData cbData;
1029 cbData.cb = &SecurityVersionDiscoveryHandler;
1030 cbData.context = (void*)discoveryInfo;
1032 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1033 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1034 if(OC_STACK_OK != ret)
1036 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1041 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1044 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1050 * Function to print OCProvisionDev_t for debug purpose.
1052 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1055 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1059 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1060 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1061 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1062 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1063 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1067 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1071 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1073 if(*pUuidList == NULL || targetId == NULL)
1077 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1078 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1080 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1082 LL_DELETE(*pUuidList, tmp1);