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;
59 * Function to discover secre port information through unicast
61 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
62 * @param[in] clientResponse Response information(It will contain payload)
64 * @return OC_STACK_OK on success otherwise error.
66 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
67 const OCClientResponse *clientResponse);
70 * Function to discover security version information through unicast
72 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
73 * @param[in] clientResponse Response information(It will contain payload)
75 * @return OC_STACK_OK on success otherwise error.
77 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
78 const OCClientResponse *clientResponse);
81 * Callback handler for PMDeviceDiscovery API.
83 * @param[in] ctx User context
84 * @param[in] handle Handler for response
85 * @param[in] clientResponse Response information (It will contain payload)
86 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
87 * OC_STACK_DELETE_TRANSACTION to delete it.
89 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
90 OCClientResponse *clientResponse);
93 * Callback handler for getting secure port information using /oic/res discovery.
95 * @param[in] ctx user context
96 * @param[in] handle Handle for response
97 * @param[in] clientResponse Response information(It will contain payload)
99 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
100 * OC_STACK_DELETE_TRANSACTION to delete it.
102 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
103 OCClientResponse *clientResponse);
106 * Callback handler for security version discovery.
108 * @param[in] ctx User context
109 * @param[in] handle Handler for response
110 * @param[in] clientResponse Response information (It will contain payload)
111 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
112 * OC_STACK_DELETE_TRANSACTION to delete it.
114 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
115 OCClientResponse *clientResponse);
118 * Function to search node in linked list that matches given IP and port.
120 * @param[in] pList List of OCProvisionDev_t.
121 * @param[in] addr address of target device.
122 * @param[in] port port of remote server.
124 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
126 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
128 if(NULL == addr || NULL == *ppDevicesList)
130 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
134 OCProvisionDev_t *ptr = NULL;
135 LL_FOREACH(*ppDevicesList, ptr)
137 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
148 * Add device information to list.
150 * @param[in] pList List of OCProvisionDev_t.
151 * @param[in] endpoint target device endpoint.
152 * @param[in] connType connectivity type of endpoint
153 * @param[in] doxm pointer to doxm instance.
155 * @return OC_STACK_OK for success and error code otherwise.
157 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
158 OCConnectivityType connType, OicSecDoxm_t *doxm)
160 if (NULL == endpoint)
162 return OC_STACK_INVALID_PARAM;
165 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
168 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
171 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
172 return OC_STACK_NO_MEMORY;
175 ptr->endpoint = *endpoint;
177 ptr->securePort = DEFAULT_SECURE_PORT;
179 ptr->connType = connType;
180 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
181 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
183 LL_PREPEND(*ppDevicesList, ptr);
190 * Function to set secure port information from the given list of devices.
192 * @param[in] pList List of OCProvisionDev_t.
193 * @param[in] addr address of target device.
194 * @param[in] port port of remote server.
195 * @param[in] secureport secure port information.
197 * @return OC_STACK_OK for success and errorcode otherwise.
199 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
200 uint16_t port, uint16_t securePort
206 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
210 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
211 return OC_STACK_ERROR;
214 ptr->securePort = securePort;
217 ptr->tcpPort = tcpPort;
224 * Function to set security version information from the given list of devices.
226 * @param[in] pList List of OCProvisionDev_t.
227 * @param[in] addr address of target device.
228 * @param[in] port port of remote server.
229 * @param[in] secVer security version information.
231 * @return OC_STACK_OK for success and errorcode otherwise.
233 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
234 uint16_t port, const char* secVer)
238 return OC_STACK_INVALID_PARAM;
241 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
245 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
246 return OC_STACK_ERROR;
249 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
255 * This function deletes list of provision target devices
257 * @param[in] pDevicesList List of OCProvisionDev_t.
259 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
263 OCProvisionDev_t *del = NULL, *tmp = NULL;
264 LL_FOREACH_SAFE(pDevicesList, del, tmp)
266 LL_DELETE(pDevicesList, del);
268 DeleteDoxmBinData(del->doxm);
269 DeletePstatBinData(del->pstat);
275 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
277 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
281 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
285 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
286 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
287 VERIFY_NON_NULL(TAG, newDev, ERROR);
289 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
293 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
294 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
296 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
297 // We have to assign NULL for not necessary information to prevent memory corruption.
298 newDev->pstat->sm = NULL;
303 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
304 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
306 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
307 // We have to assign NULL for not necessary information to prevent memory corruption.
308 newDev->doxm->oxmType = NULL;
309 newDev->doxm->oxm = NULL;
312 if (0 == strlen(src->secVer))
314 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
318 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
321 newDev->securePort = src->securePort;
322 newDev->devStatus = src->devStatus;
323 newDev->connType = src->connType;
326 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
331 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
334 OICFree(newDev->pstat);
335 OICFree(newDev->doxm);
342 * Timeout implementation for secure discovery. When performing secure discovery,
343 * we should wait a certain period of time for getting response of each devices.
345 * @param[in] waittime Timeout in seconds.
346 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
347 * @return OC_STACK_OK on success otherwise error.
349 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
351 OCStackResult res = OC_STACK_OK;
353 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
354 while (OC_STACK_OK == res)
356 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
358 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
359 if (elapsed > waittime)
363 if (waitForStackResponse)
372 * Extract secure port information from payload of discovery response.
374 * @param[in] jsonStr response payload of /oic/res discovery.
376 * @return Secure port
378 uint16_t GetSecurePortFromJSON(char* jsonStr)
380 // TODO: Modify error handling
385 cJSON *jsonProp = NULL;
387 cJSON *jsonPort = NULL;
389 cJSON *jsonRoot = cJSON_Parse(jsonStr);
392 // TODO: Add error log & return default secure port
396 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
399 // TODO: Add error log & return default secure port
403 jsonP = cJSON_GetObjectItem(jsonProp, "p");
406 // TODO: Add error log & return default secure port
410 jsonPort = cJSON_GetObjectItem(jsonP, "port");
413 // TODO: Add error log & return default secure port
417 return (uint16_t)jsonPort->valueint;
420 bool PMGenerateQuery(bool isSecure,
421 const char* address, uint16_t port,
422 OCConnectivityType connType,
423 char* buffer, size_t bufferSize, const char* uri)
425 if(!address || !buffer || !uri)
427 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
432 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
434 switch(connType & CT_MASK_ADAPTER)
437 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
439 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
442 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
443 prefix, address, port, uri);
446 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
447 prefix, address, port, uri);
450 OIC_LOG(ERROR, TAG, "Unknown address format.");
453 // snprintf return value check
456 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
459 else if ((size_t)snRet >= bufferSize)
461 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
466 // TODO: We need to verify tinyDTLS in below cases
467 case CT_ADAPTER_GATT_BTLE:
468 case CT_ADAPTER_RFCOMM_BTEDR:
469 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
473 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
480 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
481 OCClientResponse *clientResponse)
485 OIC_LOG(ERROR, TAG, "Lost List of device information");
486 return OC_STACK_KEEP_TRANSACTION;
491 if (NULL == clientResponse->payload)
493 OIC_LOG(INFO, TAG, "Skiping Null payload");
494 return OC_STACK_KEEP_TRANSACTION;
496 if (OC_STACK_OK != clientResponse->result)
498 OIC_LOG(INFO, TAG, "Error in response");
499 return OC_STACK_KEEP_TRANSACTION;
503 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
505 OIC_LOG(INFO, TAG, "Unknown payload type");
506 return OC_STACK_KEEP_TRANSACTION;
509 OicSecVer_t *ptrVer = NULL;
510 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
511 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
513 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
514 if ((NULL == ptrVer) && (OC_STACK_OK != res))
516 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
517 return OC_STACK_KEEP_TRANSACTION;
521 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
523 //If this is owend device discovery we have to filter out the responses.
524 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
525 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
526 clientResponse->devAddr.port, ptrVer->secv);
527 if (OC_STACK_OK != res)
529 OIC_LOG(ERROR, TAG, "Error while getting security version.");
530 DeleteVerBinData(ptrVer);
531 return OC_STACK_KEEP_TRANSACTION;
534 OIC_LOG(INFO, TAG, "= Discovered security version =");
535 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
536 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
537 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
539 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
540 DeleteVerBinData(ptrVer);
546 OIC_LOG(INFO, TAG, "Skiping Null response");
547 return OC_STACK_KEEP_TRANSACTION;
550 return OC_STACK_DELETE_TRANSACTION;
553 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
554 OCClientResponse *clientResponse)
558 OIC_LOG(ERROR, TAG, "Lost List of device information");
559 return OC_STACK_DELETE_TRANSACTION;
564 if (NULL == clientResponse->payload)
566 OIC_LOG(INFO, TAG, "Skiping Null payload");
570 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
572 OIC_LOG(INFO, TAG, "Wrong payload type");
573 return OC_STACK_DELETE_TRANSACTION;
576 uint16_t securePort = 0;
577 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
579 // Use seure port of doxm for OTM and Provision.
582 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
584 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
585 OIC_LOG(INFO, TAG, "Found doxm resource.");
590 resPayload = resPayload->next;
593 if (NULL == resPayload)
595 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
596 return OC_STACK_DELETE_TRANSACTION;
598 if (resPayload && resPayload->secure)
600 securePort = resPayload->port;
604 OIC_LOG(INFO, TAG, "Can not find secure port information.");
605 return OC_STACK_DELETE_TRANSACTION;
608 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
610 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
611 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
612 clientResponse->devAddr.addr,
613 clientResponse->devAddr.port,
619 if (OC_STACK_OK != res)
621 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
622 return OC_STACK_DELETE_TRANSACTION;
625 res = SecurityVersionDiscovery(pDInfo, clientResponse);
626 if(OC_STACK_OK != res)
628 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
629 return OC_STACK_DELETE_TRANSACTION;
632 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
635 return OC_STACK_DELETE_TRANSACTION;
639 OIC_LOG(INFO, TAG, "Skiping Null response");
642 return OC_STACK_DELETE_TRANSACTION;
645 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
646 OCClientResponse *clientResponse)
650 OIC_LOG(ERROR, TAG, "Lost List of device information");
651 return OC_STACK_KEEP_TRANSACTION;
656 if (NULL == clientResponse->payload)
658 OIC_LOG(INFO, TAG, "Skiping Null payload");
659 return OC_STACK_KEEP_TRANSACTION;
661 if (OC_STACK_OK != clientResponse->result)
663 OIC_LOG(INFO, TAG, "Error in response");
664 return OC_STACK_KEEP_TRANSACTION;
668 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
670 OIC_LOG(INFO, TAG, "Unknown payload type");
671 return OC_STACK_KEEP_TRANSACTION;
674 OicSecDoxm_t *ptrDoxm = NULL;
675 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
676 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
678 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
679 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
681 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
682 return OC_STACK_KEEP_TRANSACTION;
686 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
688 //If this is owend device discovery we have to filter out the responses.
689 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
690 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
692 // Get my device ID from doxm resource
694 memset(&myId, 0, sizeof(myId));
695 OCStackResult res = GetDoxmDevOwnerId(&myId);
696 if(OC_STACK_OK != res)
698 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
699 DeleteDoxmBinData(ptrDoxm);
700 return OC_STACK_KEEP_TRANSACTION;
703 // If this is owned discovery response but owner is not me then discard it.
704 if( (pDInfo->isOwnedDiscovery) &&
705 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
707 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
708 DeleteDoxmBinData(ptrDoxm);
709 return OC_STACK_KEEP_TRANSACTION;
712 res = GetDoxmDeviceID(&myId);
713 if(OC_STACK_OK != res)
715 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
716 DeleteDoxmBinData(ptrDoxm);
717 return OC_STACK_KEEP_TRANSACTION;
719 //if this is owned discovery and this is PT's reply, discard it
720 if((pDInfo->isOwnedDiscovery) &&
721 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
723 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
724 DeleteDoxmBinData(ptrDoxm);
725 return OC_STACK_KEEP_TRANSACTION;
728 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
729 clientResponse->connType, ptrDoxm);
730 if (OC_STACK_OK != res)
732 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
733 DeleteDoxmBinData(ptrDoxm);
734 return OC_STACK_KEEP_TRANSACTION;
737 res = SecurePortDiscovery(pDInfo, clientResponse);
738 if(OC_STACK_OK != res)
740 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
741 DeleteDoxmBinData(ptrDoxm);
742 return OC_STACK_KEEP_TRANSACTION;
745 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
748 return OC_STACK_KEEP_TRANSACTION;
753 OIC_LOG(INFO, TAG, "Skiping Null response");
754 return OC_STACK_KEEP_TRANSACTION;
757 return OC_STACK_DELETE_TRANSACTION;
761 * Discover owned/unowned devices in the same IP subnet. .
763 * @param[in] waittime Timeout in seconds.
764 * @param[in] isOwned bool flag for owned / unowned discovery
765 * @param[in] ppDevicesList List of OCProvisionDev_t.
767 * @return OC_STACK_OK on success otherwise error.
769 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
771 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
773 if (NULL != *ppDevicesList)
775 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
776 return OC_STACK_INVALID_PARAM;
779 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
780 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
782 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
785 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
786 return OC_STACK_NO_MEMORY;
789 pDInfo->ppDevicesList = ppDevicesList;
790 pDInfo->isOwnedDiscovery = isOwned;
792 OCCallbackData cbData;
793 cbData.cb = &DeviceDiscoveryHandler;
794 cbData.context = (void *)pDInfo;
796 OCStackResult res = OC_STACK_ERROR;
798 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
799 DOXM_OWNED_FALSE_MULTICAST_QUERY;
801 OCDoHandle handle = NULL;
802 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
803 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
804 if (res != OC_STACK_OK)
806 OIC_LOG(ERROR, TAG, "OCStack resource error");
811 //Waiting for each response.
812 res = PMTimeout(waittime, true);
813 if(OC_STACK_OK != res)
815 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
817 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
818 if(OC_STACK_OK != resCancel)
820 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
824 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
825 if (OC_STACK_OK != res)
827 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
831 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
836 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
837 const OCClientResponse *clientResponse)
839 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
841 if(NULL == discoveryInfo || NULL == clientResponse)
843 return OC_STACK_INVALID_PARAM;
845 //Try to the unicast discovery to getting secure port
846 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
847 if(!PMGenerateQuery(false,
848 clientResponse->devAddr.addr, clientResponse->devAddr.port,
849 clientResponse->connType,
850 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
852 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
853 return OC_STACK_ERROR;
855 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
857 OCCallbackData cbData;
858 cbData.cb = &SecurePortDiscoveryHandler;
859 cbData.context = (void*)discoveryInfo;
861 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
862 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
863 if(OC_STACK_OK != ret)
865 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
870 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
873 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
878 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
879 const OCClientResponse *clientResponse)
881 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
883 if(NULL == discoveryInfo || NULL == clientResponse)
885 return OC_STACK_INVALID_PARAM;
888 //Try to the unicast discovery to getting security version
889 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
890 if(!PMGenerateQuery(false,
891 clientResponse->devAddr.addr, clientResponse->devAddr.port,
892 clientResponse->connType,
893 query, sizeof(query), OIC_RSRC_VER_URI))
895 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
896 return OC_STACK_ERROR;
898 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
900 OCCallbackData cbData;
901 cbData.cb = &SecurityVersionDiscoveryHandler;
902 cbData.context = (void*)discoveryInfo;
904 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
905 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
906 if(OC_STACK_OK != ret)
908 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
913 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
916 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
922 * Function to print OCProvisionDev_t for debug purpose.
924 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
927 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
931 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
932 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
933 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
934 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
935 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
939 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
943 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
945 if(*pUuidList == NULL || targetId == NULL)
949 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
950 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
952 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
954 LL_DELETE(*pUuidList, tmp1);