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;
62 * Function to discover secre port information through unicast
64 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
65 * @param[in] clientResponse Response information(It will contain payload)
67 * @return OC_STACK_OK on success otherwise error.
69 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
70 const OCClientResponse *clientResponse);
73 * Function to discover security version information through unicast
75 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
76 * @param[in] clientResponse Response information(It will contain payload)
78 * @return OC_STACK_OK on success otherwise error.
80 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
81 const OCClientResponse *clientResponse);
84 * Callback handler for PMDeviceDiscovery API.
86 * @param[in] ctx User context
87 * @param[in] handle Handler for response
88 * @param[in] clientResponse Response information (It will contain payload)
89 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
90 * OC_STACK_DELETE_TRANSACTION to delete it.
92 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
93 OCClientResponse *clientResponse);
96 * Callback handler for getting secure port information using /oic/res discovery.
98 * @param[in] ctx user context
99 * @param[in] handle Handle for response
100 * @param[in] clientResponse Response information(It will contain payload)
102 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
103 * OC_STACK_DELETE_TRANSACTION to delete it.
105 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
106 OCClientResponse *clientResponse);
109 * Callback handler for security version discovery.
111 * @param[in] ctx User context
112 * @param[in] handle Handler for response
113 * @param[in] clientResponse Response information (It will contain payload)
114 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
115 * OC_STACK_DELETE_TRANSACTION to delete it.
117 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
118 OCClientResponse *clientResponse);
121 * Function to search node in linked list that matches given IP and port.
123 * @param[in] pList List of OCProvisionDev_t.
124 * @param[in] addr address of target device.
125 * @param[in] port port of remote server.
127 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
129 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
131 if(NULL == addr || NULL == *ppDevicesList)
133 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
137 OCProvisionDev_t *ptr = NULL;
138 LL_FOREACH(*ppDevicesList, ptr)
140 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
151 * Add device information to list.
153 * @param[in] pList List of OCProvisionDev_t.
154 * @param[in] endpoint target device endpoint.
155 * @param[in] connType connectivity type of endpoint
156 * @param[in] doxm pointer to doxm instance.
158 * @return OC_STACK_OK for success and error code otherwise.
160 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
161 OCConnectivityType connType, OicSecDoxm_t *doxm)
163 if (NULL == endpoint)
165 return OC_STACK_INVALID_PARAM;
168 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
171 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
174 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
175 return OC_STACK_NO_MEMORY;
178 ptr->endpoint = *endpoint;
180 ptr->securePort = DEFAULT_SECURE_PORT;
182 ptr->connType = connType;
183 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
184 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
186 LL_PREPEND(*ppDevicesList, ptr);
193 * Function to set secure port information from the given list of devices.
195 * @param[in] pList List of OCProvisionDev_t.
196 * @param[in] addr address of target device.
197 * @param[in] port port of remote server.
198 * @param[in] secureport secure port information.
200 * @return OC_STACK_OK for success and errorcode otherwise.
202 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
203 uint16_t port, uint16_t securePort
209 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
213 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
214 return OC_STACK_ERROR;
217 ptr->securePort = securePort;
220 ptr->tcpPort = tcpPort;
227 * Function to set security version information from the given list of devices.
229 * @param[in] pList List of OCProvisionDev_t.
230 * @param[in] addr address of target device.
231 * @param[in] port port of remote server.
232 * @param[in] secVer security version information.
234 * @return OC_STACK_OK for success and errorcode otherwise.
236 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
237 uint16_t port, const char* secVer)
241 return OC_STACK_INVALID_PARAM;
244 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
248 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
249 return OC_STACK_ERROR;
252 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
258 * This function deletes list of provision target devices
260 * @param[in] pDevicesList List of OCProvisionDev_t.
262 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
266 OCProvisionDev_t *del = NULL, *tmp = NULL;
267 LL_FOREACH_SAFE(pDevicesList, del, tmp)
269 LL_DELETE(pDevicesList, del);
271 DeleteDoxmBinData(del->doxm);
272 DeletePstatBinData(del->pstat);
278 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
280 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
284 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
288 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
289 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
290 VERIFY_NON_NULL(TAG, newDev, ERROR);
292 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
296 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
297 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
299 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
300 // We have to assign NULL for not necessary information to prevent memory corruption.
301 newDev->pstat->sm = NULL;
306 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
307 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
309 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
310 // We have to assign NULL for not necessary information to prevent memory corruption.
311 newDev->doxm->oxmType = NULL;
312 newDev->doxm->oxm = NULL;
315 if (0 == strlen(src->secVer))
317 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
321 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
324 newDev->securePort = src->securePort;
325 newDev->devStatus = src->devStatus;
326 newDev->connType = src->connType;
329 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
334 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
337 OICFree(newDev->pstat);
338 OICFree(newDev->doxm);
345 * Timeout implementation for secure discovery. When performing secure discovery,
346 * we should wait a certain period of time for getting response of each devices.
348 * @param[in] waittime Timeout in seconds.
349 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
350 * @return OC_STACK_OK on success otherwise error.
352 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
354 OCStackResult res = OC_STACK_OK;
356 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
357 while (OC_STACK_OK == res)
359 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
361 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
362 if (elapsed > waittime)
366 if (waitForStackResponse)
375 * Extract secure port information from payload of discovery response.
377 * @param[in] jsonStr response payload of /oic/res discovery.
379 * @return Secure port
381 uint16_t GetSecurePortFromJSON(char* jsonStr)
383 // TODO: Modify error handling
388 cJSON *jsonProp = NULL;
390 cJSON *jsonPort = NULL;
392 cJSON *jsonRoot = cJSON_Parse(jsonStr);
395 // TODO: Add error log & return default secure port
399 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
402 // TODO: Add error log & return default secure port
406 jsonP = cJSON_GetObjectItem(jsonProp, "p");
409 // TODO: Add error log & return default secure port
413 jsonPort = cJSON_GetObjectItem(jsonP, "port");
416 // TODO: Add error log & return default secure port
420 return (uint16_t)jsonPort->valueint;
423 bool PMGenerateQuery(bool isSecure,
424 const char* address, uint16_t port,
425 OCConnectivityType connType,
426 char* buffer, size_t bufferSize, const char* uri)
428 if(!address || !buffer || !uri)
430 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
435 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
437 switch(connType & CT_MASK_ADAPTER)
440 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
442 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
445 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
446 prefix, address, port, uri);
449 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
450 prefix, address, port, uri);
453 OIC_LOG(ERROR, TAG, "Unknown address format.");
456 // snprintf return value check
459 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
462 else if ((size_t)snRet >= bufferSize)
464 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
469 // TODO: We need to verify tinyDTLS in below cases
470 case CT_ADAPTER_GATT_BTLE:
471 case CT_ADAPTER_RFCOMM_BTEDR:
472 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
476 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
483 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
484 OCClientResponse *clientResponse)
488 OIC_LOG(ERROR, TAG, "Lost List of device information");
489 return OC_STACK_KEEP_TRANSACTION;
494 if (NULL == clientResponse->payload)
496 OIC_LOG(INFO, TAG, "Skiping Null payload");
497 return OC_STACK_KEEP_TRANSACTION;
499 if (OC_STACK_OK != clientResponse->result)
501 OIC_LOG(INFO, TAG, "Error in response");
502 return OC_STACK_KEEP_TRANSACTION;
506 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
508 OIC_LOG(INFO, TAG, "Unknown payload type");
509 return OC_STACK_KEEP_TRANSACTION;
512 OicSecVer_t *ptrVer = NULL;
513 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
514 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
516 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
517 if ((NULL == ptrVer) && (OC_STACK_OK != res))
519 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
520 return OC_STACK_KEEP_TRANSACTION;
524 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
526 //If this is owend device discovery we have to filter out the responses.
527 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
528 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
529 clientResponse->devAddr.port, ptrVer->secv);
530 if (OC_STACK_OK != res)
532 OIC_LOG(ERROR, TAG, "Error while getting security version.");
533 DeleteVerBinData(ptrVer);
534 return OC_STACK_KEEP_TRANSACTION;
537 OIC_LOG(INFO, TAG, "= Discovered security version =");
538 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
539 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
540 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
542 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
543 DeleteVerBinData(ptrVer);
544 if(pDInfo->isSingleDiscovery)
546 pDInfo->isFound = true;
553 OIC_LOG(INFO, TAG, "Skiping Null response");
554 return OC_STACK_KEEP_TRANSACTION;
557 return OC_STACK_DELETE_TRANSACTION;
560 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
561 OCClientResponse *clientResponse)
565 OIC_LOG(ERROR, TAG, "Lost List of device information");
566 return OC_STACK_DELETE_TRANSACTION;
571 if (NULL == clientResponse->payload)
573 OIC_LOG(INFO, TAG, "Skiping Null payload");
577 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
579 OIC_LOG(INFO, TAG, "Wrong payload type");
580 return OC_STACK_DELETE_TRANSACTION;
583 uint16_t securePort = 0;
584 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
586 // Use seure port of doxm for OTM and Provision.
589 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
591 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
592 OIC_LOG(INFO, TAG, "Found doxm resource.");
597 resPayload = resPayload->next;
600 if (NULL == resPayload)
602 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
603 return OC_STACK_DELETE_TRANSACTION;
605 if (resPayload && resPayload->secure)
607 securePort = resPayload->port;
611 OIC_LOG(INFO, TAG, "Can not find secure port information.");
612 return OC_STACK_DELETE_TRANSACTION;
615 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
617 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
618 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
619 clientResponse->devAddr.addr,
620 clientResponse->devAddr.port,
626 if (OC_STACK_OK != res)
628 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
629 return OC_STACK_DELETE_TRANSACTION;
632 res = SecurityVersionDiscovery(pDInfo, clientResponse);
633 if(OC_STACK_OK != res)
635 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
636 return OC_STACK_DELETE_TRANSACTION;
639 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
642 return OC_STACK_DELETE_TRANSACTION;
646 OIC_LOG(INFO, TAG, "Skiping Null response");
649 return OC_STACK_DELETE_TRANSACTION;
652 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
653 OCClientResponse *clientResponse)
657 OIC_LOG(ERROR, TAG, "Lost List of device information");
658 return OC_STACK_KEEP_TRANSACTION;
663 if (NULL == clientResponse->payload)
665 OIC_LOG(INFO, TAG, "Skiping Null payload");
666 return OC_STACK_KEEP_TRANSACTION;
668 if (OC_STACK_OK != clientResponse->result)
670 OIC_LOG(INFO, TAG, "Error in response");
671 return OC_STACK_KEEP_TRANSACTION;
675 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
677 OIC_LOG(INFO, TAG, "Unknown payload type");
678 return OC_STACK_KEEP_TRANSACTION;
681 OicSecDoxm_t *ptrDoxm = NULL;
682 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
683 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
685 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
686 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
688 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
689 return OC_STACK_KEEP_TRANSACTION;
693 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
695 //If this is owend device discovery we have to filter out the responses.
696 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
697 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
699 // Get my device ID from doxm resource
701 memset(&myId, 0, sizeof(myId));
702 OCStackResult res = GetDoxmDevOwnerId(&myId);
703 if(OC_STACK_OK != res)
705 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
706 DeleteDoxmBinData(ptrDoxm);
707 return OC_STACK_KEEP_TRANSACTION;
710 // If this is owned discovery response but owner is not me then discard it.
711 if( (pDInfo->isOwnedDiscovery) &&
712 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
714 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
715 DeleteDoxmBinData(ptrDoxm);
716 return OC_STACK_KEEP_TRANSACTION;
719 res = GetDoxmDeviceID(&myId);
720 if(OC_STACK_OK != res)
722 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
723 DeleteDoxmBinData(ptrDoxm);
724 return OC_STACK_KEEP_TRANSACTION;
726 //if this is owned discovery and this is PT's reply, discard it
727 if(((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
728 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
730 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
731 DeleteDoxmBinData(ptrDoxm);
732 return OC_STACK_KEEP_TRANSACTION;
735 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
736 clientResponse->connType, ptrDoxm);
737 if (OC_STACK_OK != res)
739 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
740 DeleteDoxmBinData(ptrDoxm);
741 return OC_STACK_KEEP_TRANSACTION;
744 res = SecurePortDiscovery(pDInfo, clientResponse);
745 if(OC_STACK_OK != res)
747 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
748 DeleteDoxmBinData(ptrDoxm);
749 return OC_STACK_KEEP_TRANSACTION;
752 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
753 if(pDInfo->isSingleDiscovery)
755 return OC_STACK_DELETE_TRANSACTION;
759 return OC_STACK_KEEP_TRANSACTION;
764 OIC_LOG(INFO, TAG, "Skiping Null response");
765 return OC_STACK_KEEP_TRANSACTION;
768 return OC_STACK_DELETE_TRANSACTION;
773 * Discover owned/unowned devices in the specified endpoint.
774 * It will return when found one or more device even though timeout is not exceeded
776 * @param[in] waittime Timeout in seconds
777 * @param[in] host address of target endpoint
778 * @param[in] connType connectivity type of endpoint
779 * @param[out] ppDevicesList List of OCProvisionDev_t
781 * @return OC_STACK_OK on success otherwise error.
783 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
784 OCConnectivityType connType, OCProvisionDev_t **ppDevicesList)
786 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
788 if (NULL != *ppDevicesList)
790 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
791 return OC_STACK_INVALID_PARAM;
794 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
797 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
798 return OC_STACK_NO_MEMORY;
801 pDInfo->ppDevicesList = ppDevicesList;
802 pDInfo->isOwnedDiscovery = false;
803 pDInfo->isSingleDiscovery = true;
804 pDInfo->isFound = false;
806 OCCallbackData cbData;
807 cbData.cb = &DeviceDiscoveryHandler;
808 cbData.context = (void *)pDInfo;
810 OCStackResult res = OC_STACK_ERROR;
812 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
817 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "%s/oic/sec/doxm", host);
819 OCDoHandle handle = NULL;
820 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
821 connType, OC_HIGH_QOS, &cbData, NULL, 0);
822 if (res != OC_STACK_OK)
824 OIC_LOG(ERROR, TAG, "OCStack resource error");
829 //Waiting for each response.
831 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
832 while (OC_STACK_OK == res && pDInfo->isFound == false)
834 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
836 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
837 if (elapsed > waittime)
844 if(OC_STACK_OK != res)
846 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
848 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
849 if(OC_STACK_OK != resCancel)
851 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
856 if(*ppDevicesList == NULL)
858 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
861 if (OC_STACK_OK != res)
863 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
867 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
874 * Discover owned/unowned devices in the same IP subnet. .
876 * @param[in] waittime Timeout in seconds.
877 * @param[in] isOwned bool flag for owned / unowned discovery
878 * @param[in] ppDevicesList List of OCProvisionDev_t.
880 * @return OC_STACK_OK on success otherwise error.
882 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
884 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
886 if (NULL != *ppDevicesList)
888 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
889 return OC_STACK_INVALID_PARAM;
892 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
893 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
895 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
898 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
899 return OC_STACK_NO_MEMORY;
902 pDInfo->ppDevicesList = ppDevicesList;
903 pDInfo->isOwnedDiscovery = isOwned;
904 pDInfo->isSingleDiscovery = false;
906 OCCallbackData cbData;
907 cbData.cb = &DeviceDiscoveryHandler;
908 cbData.context = (void *)pDInfo;
910 OCStackResult res = OC_STACK_ERROR;
912 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
913 DOXM_OWNED_FALSE_MULTICAST_QUERY;
915 OCDoHandle handle = NULL;
916 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
917 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
918 if (res != OC_STACK_OK)
920 OIC_LOG(ERROR, TAG, "OCStack resource error");
925 //Waiting for each response.
926 res = PMTimeout(waittime, true);
927 if(OC_STACK_OK != res)
929 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
931 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
932 if(OC_STACK_OK != resCancel)
934 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
938 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
939 if (OC_STACK_OK != res)
941 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
945 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
950 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
951 const OCClientResponse *clientResponse)
953 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
955 if(NULL == discoveryInfo || NULL == clientResponse)
957 return OC_STACK_INVALID_PARAM;
959 //Try to the unicast discovery to getting secure port
960 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
961 if(!PMGenerateQuery(false,
962 clientResponse->devAddr.addr, clientResponse->devAddr.port,
963 clientResponse->connType,
964 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
966 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
967 return OC_STACK_ERROR;
969 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
971 OCCallbackData cbData;
972 cbData.cb = &SecurePortDiscoveryHandler;
973 cbData.context = (void*)discoveryInfo;
975 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
976 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
977 if(OC_STACK_OK != ret)
979 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
984 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
987 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
992 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
993 const OCClientResponse *clientResponse)
995 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
997 if(NULL == discoveryInfo || NULL == clientResponse)
999 return OC_STACK_INVALID_PARAM;
1002 //Try to the unicast discovery to getting security version
1003 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1004 if(!PMGenerateQuery(false,
1005 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1006 clientResponse->connType,
1007 query, sizeof(query), OIC_RSRC_VER_URI))
1009 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1010 return OC_STACK_ERROR;
1012 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1014 OCCallbackData cbData;
1015 cbData.cb = &SecurityVersionDiscoveryHandler;
1016 cbData.context = (void*)discoveryInfo;
1018 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1019 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1020 if(OC_STACK_OK != ret)
1022 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1027 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1030 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1036 * Function to print OCProvisionDev_t for debug purpose.
1038 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1041 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1045 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1046 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1047 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1048 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1049 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1053 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1057 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1059 if(*pUuidList == NULL || targetId == NULL)
1063 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1064 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1066 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1068 LL_DELETE(*pUuidList, tmp1);