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"
37 #if defined (__TIZENRT__)
38 #include <apps/netutils/cJSON.h>
43 #include "ocpayload.h"
45 #include "securevirtualresourcetypes.h"
46 #include "srmresourcestrings.h" //@note: SRM's internal header
47 #include "doxmresource.h" //@note: SRM's internal header
48 #include "pstatresource.h" //@note: SRM's internal header
49 #include "verresource.h" //@note: SRM's internal header
52 #include "pmutility.h"
54 #include "srmutility.h"
56 #define TAG ("OIC_PM_UTILITY")
58 typedef struct _DiscoveryInfo{
59 OCProvisionDev_t **ppDevicesList;
60 OCProvisionDev_t *pCandidateList;
61 bool isOwnedDiscovery;
62 bool isSingleDiscovery;
64 const OicUuid_t *targetId;
68 * Function to discover secre port information through unicast
70 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
71 * @param[in] clientResponse Response information(It will contain payload)
73 * @return OC_STACK_OK on success otherwise error.
75 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
76 const OCClientResponse *clientResponse);
79 * Callback handler for PMDeviceDiscovery API.
81 * @param[in] ctx User context
82 * @param[in] handle Handler for response
83 * @param[in] clientResponse Response information (It will contain payload)
84 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
85 * OC_STACK_DELETE_TRANSACTION to delete it.
87 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
88 OCClientResponse *clientResponse);
91 * Since security version discovery does not used anymore, disable security version discovery.
92 * Need to discussion to removing all version discovery related codes.
96 * Function to discover security version information through unicast
98 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
99 * @param[in] clientResponse Response information(It will contain payload)
101 * @return OC_STACK_OK on success otherwise error.
103 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
104 const OCClientResponse *clientResponse);
107 * Callback handler for getting secure port information using /oic/res discovery.
109 * @param[in] ctx user context
110 * @param[in] handle Handle for response
111 * @param[in] clientResponse Response information(It will contain payload)
113 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
114 * OC_STACK_DELETE_TRANSACTION to delete it.
116 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
117 OCClientResponse *clientResponse);
121 * Since security version discovery does not used anymore, disable security version discovery.
122 * Need to discussion to removing all version discovery related codes.
126 * Callback handler for security version discovery.
128 * @param[in] ctx User context
129 * @param[in] handle Handler for response
130 * @param[in] clientResponse Response information (It will contain payload)
131 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
132 * OC_STACK_DELETE_TRANSACTION to delete it.
134 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
135 OCClientResponse *clientResponse);
139 * Function to search node in linked list that matches given IP and port.
141 * @param[in] pList List of OCProvisionDev_t.
142 * @param[in] addr address of target device.
143 * @param[in] port port of remote server.
145 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
147 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
149 if(NULL == addr || NULL == *ppDevicesList)
151 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
155 OCProvisionDev_t *ptr = NULL;
156 LL_FOREACH(*ppDevicesList, ptr)
158 if( (ptr && ptr->endpoint.addr) && strncmp(ptr->endpoint.addr, addr, MAX_ADDR_STR_SIZE) == 0 && port == ptr->endpoint.port)
169 * Add device information to list.
171 * @param[in] pList List of OCProvisionDev_t.
172 * @param[in] endpoint target device endpoint.
173 * @param[in] connType connectivity type of endpoint
174 * @param[in] doxm pointer to doxm instance.
176 * @return OC_STACK_OK for success and error code otherwise.
178 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
179 OCConnectivityType connType, OicSecDoxm_t *doxm)
181 if (NULL == endpoint)
183 return OC_STACK_INVALID_PARAM;
186 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
189 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
192 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
193 return OC_STACK_NO_MEMORY;
196 ptr->endpoint = *endpoint;
198 ptr->securePort = (CT_ADAPTER_GATT_BTLE == connType) ?
199 endpoint->port : DEFAULT_SECURE_PORT;
201 ptr->connType = connType;
202 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
203 OICStrcpy(ptr->secVer, OIC_SEC_MAX_VER_LEN, DEFAULT_SEC_VERSION); // version initialization
206 LL_PREPEND(*ppDevicesList, ptr);
213 * Move device object between two device lists.
215 * @param[in] ppDstDevicesList Destination list of OCProvisionDev_t.
216 * @param[in] ppSrcDevicesList Source list of OCProvisionDev_t.
217 * @param[in] endpoint target device endpoint.
219 * @return OC_STACK_OK for success and error code otherwise.
221 OCStackResult MoveDeviceList(OCProvisionDev_t **ppDstDevicesList,
222 OCProvisionDev_t **ppSrcDevicesList, OCDevAddr* endpoint)
224 if (NULL == ppSrcDevicesList || NULL == endpoint)
226 return OC_STACK_INVALID_PARAM;
229 OCProvisionDev_t *ptr = GetDevice(ppSrcDevicesList, endpoint->addr, endpoint->port);
232 LL_DELETE(*ppSrcDevicesList, ptr);
233 LL_PREPEND(*ppDstDevicesList, ptr);
234 OIC_LOG_V(DEBUG, TAG, "MoveDeviceList success : %s(%d)", endpoint->addr, endpoint->port);
238 return OC_STACK_ERROR;
242 * Function to set secure port information from the given list of devices.
244 * @param[in] pList List of OCProvisionDev_t.
245 * @param[in] addr address of target device.
246 * @param[in] port port of remote server.
247 * @param[in] secureport secure port information.
249 * @return OC_STACK_OK for success and errorcode otherwise.
251 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
252 uint16_t port, uint16_t securePort
258 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
262 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
263 return OC_STACK_ERROR;
266 ptr->securePort = (OC_ADAPTER_GATT_BTLE == ptr->endpoint.adapter) ?
267 ptr->endpoint.port : securePort;
270 ptr->tcpPort = tcpPort;
277 * Function to set security version information from the given list of devices.
279 * @param[in] pList List of OCProvisionDev_t.
280 * @param[in] addr address of target device.
281 * @param[in] port port of remote server.
282 * @param[in] secVer security version information.
284 * @return OC_STACK_OK for success and errorcode otherwise.
286 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
287 uint16_t port, const char* secVer)
291 return OC_STACK_INVALID_PARAM;
294 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
298 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
299 return OC_STACK_ERROR;
302 OICStrcpy(ptr->secVer, OIC_SEC_MAX_VER_LEN, secVer);
308 * This function deletes list of provision target devices
310 * @param[in] pDevicesList List of OCProvisionDev_t.
312 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
316 OCProvisionDev_t *del = NULL, *tmp = NULL;
317 LL_FOREACH_SAFE(pDevicesList, del, tmp)
319 LL_DELETE(pDevicesList, del);
321 DeleteDoxmBinData(del->doxm);
322 DeletePstatBinData(del->pstat);
328 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
330 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
334 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
338 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
339 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
340 VERIFY_NON_NULL(TAG, newDev, ERROR);
342 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
346 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
347 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
349 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
350 // We have to assign NULL for not necessary information to prevent memory corruption.
351 newDev->pstat->sm = NULL;
356 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
357 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
359 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
360 // We have to assign NULL for not necessary information to prevent memory corruption.
361 newDev->doxm->oxmType = NULL;
362 newDev->doxm->oxm = NULL;
365 if (0 == strlen(src->secVer))
367 OICStrcpy(newDev->secVer, OIC_SEC_MAX_VER_LEN, DEFAULT_SEC_VERSION);
371 OICStrcpy(newDev->secVer, OIC_SEC_MAX_VER_LEN, src->secVer);
374 newDev->securePort = src->securePort;
375 newDev->devStatus = src->devStatus;
376 newDev->connType = src->connType;
379 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
384 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
387 OICFree(newDev->pstat);
388 OICFree(newDev->doxm);
395 * Timeout implementation for secure discovery. When performing secure discovery,
396 * we should wait a certain period of time for getting response of each devices.
398 * @param[in] waittime Timeout in seconds.
399 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
400 * @return OC_STACK_OK on success otherwise error.
402 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
404 OCStackResult res = OC_STACK_OK;
406 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
407 while (OC_STACK_OK == res)
409 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
411 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
412 if (elapsed > waittime)
416 if (waitForStackResponse)
425 * Extract secure port information from payload of discovery response.
427 * @param[in] jsonStr response payload of /oic/res discovery.
429 * @return Secure port
431 uint16_t GetSecurePortFromJSON(char* jsonStr)
433 // TODO: Modify error handling
438 cJSON *jsonProp = NULL;
440 cJSON *jsonPort = NULL;
442 cJSON *jsonRoot = cJSON_Parse(jsonStr);
445 // TODO: Add error log & return default secure port
449 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
452 // TODO: Add error log & return default secure port
456 jsonP = cJSON_GetObjectItem(jsonProp, "p");
459 // TODO: Add error log & return default secure port
463 jsonPort = cJSON_GetObjectItem(jsonP, "port");
466 // TODO: Add error log & return default secure port
470 return (uint16_t)jsonPort->valueint;
473 bool PMGenerateQuery(bool isSecure,
474 const char* address, uint16_t port,
475 OCConnectivityType connType,
476 char* buffer, size_t bufferSize, const char* uri)
478 if(!address || !buffer || !uri)
480 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
485 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
487 switch(connType & CT_MASK_ADAPTER)
490 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
492 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
495 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
496 prefix, address, port, uri);
500 char addressEncoded[128] = {0};
502 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
503 sizeof(addressEncoded),
505 if (OC_STACK_OK != result)
507 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : encoding error %d\n", result);
511 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
512 prefix, addressEncoded, port, uri);
516 OIC_LOG(ERROR, TAG, "Unknown address format.");
519 // snprintf return value check
522 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
525 else if ((size_t)snRet >= bufferSize)
527 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
532 case CT_ADAPTER_GATT_BTLE:
533 snRet = snprintf(buffer, bufferSize, "%s%s%s",
534 prefix, address, uri);
535 // snprintf return value check
538 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
541 else if ((size_t)snRet >= bufferSize)
543 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
548 // TODO: We need to verify tinyDTLS in below cases
549 case CT_ADAPTER_RFCOMM_BTEDR:
550 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
554 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
562 * Since security version discovery does not used anymore, disable security version discovery.
563 * Need to discussion to removing all version discovery related codes.
566 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
567 OCClientResponse *clientResponse)
571 OIC_LOG(ERROR, TAG, "Lost List of device information");
572 return OC_STACK_KEEP_TRANSACTION;
577 if (NULL == clientResponse->payload)
579 OIC_LOG(INFO, TAG, "Skiping Null payload");
580 return OC_STACK_KEEP_TRANSACTION;
582 if (OC_STACK_OK != clientResponse->result)
584 OIC_LOG(INFO, TAG, "Error in response");
585 return OC_STACK_KEEP_TRANSACTION;
589 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
591 OIC_LOG(INFO, TAG, "Unknown payload type");
592 return OC_STACK_KEEP_TRANSACTION;
595 OicSecVer_t *ptrVer = NULL;
596 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
597 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
599 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
600 if ((NULL == ptrVer) && (OC_STACK_OK != res))
602 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
603 return OC_STACK_KEEP_TRANSACTION;
607 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
609 //If this is owend device discovery we have to filter out the responses.
610 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
611 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
612 clientResponse->devAddr.port, ptrVer->secv);
613 if (OC_STACK_OK != res)
615 OIC_LOG(ERROR, TAG, "Error while getting security version.");
616 DeleteVerBinData(ptrVer);
617 return OC_STACK_KEEP_TRANSACTION;
620 OIC_LOG(INFO, TAG, "= Discovered security version =");
621 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
622 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
623 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
625 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
626 DeleteVerBinData(ptrVer);
632 OIC_LOG(INFO, TAG, "Skiping Null response");
633 return OC_STACK_KEEP_TRANSACTION;
636 return OC_STACK_DELETE_TRANSACTION;
640 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
641 OCClientResponse *clientResponse)
645 OIC_LOG(ERROR, TAG, "Lost List of device information");
646 return OC_STACK_DELETE_TRANSACTION;
651 if (NULL == clientResponse->payload)
653 OIC_LOG(INFO, TAG, "Skiping Null payload");
657 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
659 OIC_LOG(INFO, TAG, "Wrong payload type");
660 return OC_STACK_DELETE_TRANSACTION;
663 uint16_t securePort = 0;
664 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
666 // Use seure port of doxm for OTM and Provision.
669 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
671 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
672 OIC_LOG(INFO, TAG, "Found doxm resource.");
677 resPayload = resPayload->next;
680 if (NULL == resPayload)
682 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
683 return OC_STACK_DELETE_TRANSACTION;
685 if (resPayload && resPayload->secure)
687 securePort = resPayload->port;
691 OIC_LOG(INFO, TAG, "Can not find secure port information.");
692 return OC_STACK_DELETE_TRANSACTION;
695 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
697 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
699 if(pDInfo->isSingleDiscovery && pDInfo->isFound)
701 OIC_LOG(INFO, TAG, "Device is already founded at Single mode.");
702 return OC_STACK_DELETE_TRANSACTION;
705 OCProvisionDev_t *ptr = GetDevice(&pDInfo->pCandidateList,
706 clientResponse->devAddr.addr,
707 clientResponse->devAddr.port);
710 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery candidate device list");
711 return OC_STACK_DELETE_TRANSACTION;
714 OCStackResult res = UpdateSecurePortOfDevice(&pDInfo->pCandidateList,
715 clientResponse->devAddr.addr,
716 clientResponse->devAddr.port,
722 if (OC_STACK_OK != res)
724 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
725 return OC_STACK_DELETE_TRANSACTION;
728 res = MoveDeviceList(pDInfo->ppDevicesList, &pDInfo->pCandidateList, &clientResponse->devAddr);
729 if(OC_STACK_OK != res)
731 OIC_LOG(ERROR, TAG, "Error while move the discovered device to list.");
732 return OC_STACK_DELETE_TRANSACTION;
735 if(pDInfo->isSingleDiscovery)
737 pDInfo->isFound = true;
741 * Since security version discovery does not used anymore, disable security version discovery.
742 * Need to discussion to removing all version discovery related codes.
745 res = SecurityVersionDiscovery(pDInfo, clientResponse);
746 if(OC_STACK_OK != res)
748 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
749 return OC_STACK_DELETE_TRANSACTION;
752 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
755 return OC_STACK_DELETE_TRANSACTION;
759 OIC_LOG(INFO, TAG, "Skiping Null response");
762 return OC_STACK_DELETE_TRANSACTION;
765 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
766 OCClientResponse *clientResponse)
770 OIC_LOG(ERROR, TAG, "Lost List of device information");
771 return OC_STACK_KEEP_TRANSACTION;
776 if (NULL == clientResponse->payload)
778 OIC_LOG(INFO, TAG, "Skiping Null payload");
779 return OC_STACK_KEEP_TRANSACTION;
781 if (OC_STACK_OK != clientResponse->result)
783 OIC_LOG(INFO, TAG, "Error in response");
784 return OC_STACK_KEEP_TRANSACTION;
788 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
790 OIC_LOG(INFO, TAG, "Unknown payload type");
791 return OC_STACK_KEEP_TRANSACTION;
794 OicSecDoxm_t *ptrDoxm = NULL;
795 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
796 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
798 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
799 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
801 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
802 return OC_STACK_KEEP_TRANSACTION;
806 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
808 //If this is owend device discovery we have to filter out the responses.
809 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
810 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
812 // Get my device ID from doxm resource
814 memset(&myId, 0, sizeof(myId));
815 OCStackResult res = GetDoxmDeviceID(&myId);
816 if(OC_STACK_OK != res)
818 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
819 DeleteDoxmBinData(ptrDoxm);
820 return OC_STACK_KEEP_TRANSACTION;
823 // If this is owned discovery response but owner is not me then discard it.
824 if( (pDInfo->isOwnedDiscovery) &&
825 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
827 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
828 DeleteDoxmBinData(ptrDoxm);
829 return OC_STACK_KEEP_TRANSACTION;
832 //if targetId and discovered deviceID are different, discard it
833 if ((pDInfo->isSingleDiscovery) &&
834 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
836 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
837 DeleteDoxmBinData(ptrDoxm);
838 return OC_STACK_KEEP_TRANSACTION;
840 //If self reply, discard it
841 if (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id)))
843 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
844 DeleteDoxmBinData(ptrDoxm);
845 return OC_STACK_KEEP_TRANSACTION;
848 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
849 clientResponse->connType, ptrDoxm);
850 if (OC_STACK_OK != res)
852 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
853 DeleteDoxmBinData(ptrDoxm);
854 return OC_STACK_KEEP_TRANSACTION;
856 res = SecurePortDiscovery(pDInfo, clientResponse);
857 if(OC_STACK_OK != res)
859 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
860 DeleteDoxmBinData(ptrDoxm);
861 return OC_STACK_KEEP_TRANSACTION;
864 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
866 return OC_STACK_KEEP_TRANSACTION;
871 OIC_LOG(INFO, TAG, "Skiping Null response");
872 return OC_STACK_KEEP_TRANSACTION;
875 return OC_STACK_DELETE_TRANSACTION;
878 static void DeviceDiscoveryDeleteHandler(void *ctx)
880 OIC_LOG(DEBUG, TAG, "IN DeviceDiscoveryDeleteHandler");
883 OIC_LOG(WARNING, TAG, "Not found context in DeviceDiscoveryDeleteHandler");
887 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
888 if (NULL != pDInfo->pCandidateList)
890 OCProvisionDev_t *pDev = NULL;
891 LL_FOREACH(pDInfo->pCandidateList, pDev)
893 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
894 pDev->endpoint.addr, pDev->endpoint.port);
895 if(OC_STACK_OK != OCCancel(pDev->handle,OC_HIGH_QOS,NULL,0))
897 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
900 PMDeleteDeviceList(pDInfo->pCandidateList);
902 OIC_LOG(DEBUG, TAG, "OUT DeviceDiscoveryDeleteHandler");
906 * Discover owned/unowned device in the specified endpoint/deviceID.
907 * It will return the found device even though timeout is not exceeded.
909 * @param[in] waittime Timeout in seconds
910 * @param[in] deviceID deviceID of target device.
911 * @param[out] ppFoundDevice OCProvisionDev_t of found device
913 * @return OC_STACK_OK on success otherwise error.\n
914 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
916 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
917 OCProvisionDev_t **ppFoundDevice)
919 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
921 if (NULL != *ppFoundDevice)
923 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
924 return OC_STACK_INVALID_PARAM;
927 if (NULL == deviceID)
929 OIC_LOG(ERROR, TAG, "Invalid device ID");
930 return OC_STACK_INVALID_PARAM;
934 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
937 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
938 return OC_STACK_NO_MEMORY;
941 pDInfo->ppDevicesList = ppFoundDevice;
942 pDInfo->pCandidateList = NULL;
943 pDInfo->isOwnedDiscovery = false;
944 pDInfo->isSingleDiscovery = true;
945 pDInfo->isFound = false;
946 pDInfo->targetId = deviceID;
948 OCCallbackData cbData;
949 cbData.cb = &DeviceDiscoveryHandler;
950 cbData.context = (void *)pDInfo;
951 cbData.cd = &DeviceDiscoveryDeleteHandler;
953 OCStackResult res = OC_STACK_ERROR;
955 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
956 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
958 OCDoHandle handle = NULL;
959 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
960 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
961 if (res != OC_STACK_OK)
963 OIC_LOG(ERROR, TAG, "OCStack resource error");
968 //Waiting for each response.
970 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
971 while (OC_STACK_OK == res && !pDInfo->isFound)
973 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
975 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
976 if (elapsed > waittime)
983 if(OC_STACK_OK != res)
985 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
987 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
988 if(OC_STACK_OK != resCancel)
990 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
995 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
996 if (OC_STACK_OK != res)
998 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1002 if (!pDInfo->isFound)
1004 res = OC_STACK_TIMEOUT;
1007 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
1012 * Discover owned/unowned devices in the same IP subnet. .
1014 * @param[in] waittime Timeout in seconds.
1015 * @param[in] isOwned bool flag for owned / unowned discovery
1016 * @param[in] ppDevicesList List of OCProvisionDev_t.
1018 * @return OC_STACK_OK on success otherwise error.
1020 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
1022 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
1024 if (NULL != *ppDevicesList)
1026 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1027 return OC_STACK_INVALID_PARAM;
1030 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
1031 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
1033 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
1036 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
1037 return OC_STACK_NO_MEMORY;
1040 pDInfo->ppDevicesList = ppDevicesList;
1041 pDInfo->pCandidateList = NULL;
1042 pDInfo->isOwnedDiscovery = isOwned;
1043 pDInfo->isSingleDiscovery = false;
1044 pDInfo->targetId = NULL;
1046 OCCallbackData cbData;
1047 cbData.cb = &DeviceDiscoveryHandler;
1048 cbData.context = (void *)pDInfo;
1049 cbData.cd = &DeviceDiscoveryDeleteHandler;
1050 OCStackResult res = OC_STACK_ERROR;
1052 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
1053 DOXM_OWNED_FALSE_MULTICAST_QUERY;
1055 OCDoHandle handle = NULL;
1056 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1057 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1058 if (res != OC_STACK_OK)
1060 OIC_LOG(ERROR, TAG, "OCStack resource error");
1066 //Waiting for each response.
1067 res = PMTimeout(waittime, true);
1068 if(OC_STACK_OK != res)
1070 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1073 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1074 if(OC_STACK_OK != resCancel)
1076 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1080 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1081 if (OC_STACK_OK != res)
1083 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1088 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
1094 OCStackResult PMSingleDeviceDiscoveryInUnicast(unsigned short waittime, const OicUuid_t* deviceID,
1095 const char* hostAddress, OCConnectivityType connType,
1096 OCProvisionDev_t **ppFoundDevice)
1098 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscoveryInUnicast");
1100 if (NULL != *ppFoundDevice)
1102 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1103 return OC_STACK_INVALID_PARAM;
1106 if (NULL == deviceID)
1108 OIC_LOG(ERROR, TAG, "Invalid device ID");
1109 return OC_STACK_INVALID_PARAM;
1112 DiscoveryInfo *pDInfo = (DiscoveryInfo*)OICCalloc(1, sizeof(DiscoveryInfo));
1115 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscoveryInUnicast : Memory allocation failed.");
1116 return OC_STACK_NO_MEMORY;
1119 pDInfo->ppDevicesList = ppFoundDevice;
1120 pDInfo->pCandidateList = NULL;
1121 pDInfo->isOwnedDiscovery = false;
1122 pDInfo->isSingleDiscovery = true;
1123 pDInfo->isFound = false;
1124 pDInfo->targetId = deviceID;
1126 OCCallbackData cbData;
1127 cbData.cb = &DeviceDiscoveryHandler;
1128 cbData.context = (void *)pDInfo;
1129 cbData.cd = &DeviceDiscoveryDeleteHandler;
1131 OCStackResult res = OC_STACK_ERROR;
1133 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
1134 if (hostAddress == NULL)
1138 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "%s/oic/sec/doxm", hostAddress);
1139 connType = connType & CT_MASK_ADAPTER;
1141 OCDoHandle handle = NULL;
1142 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1143 connType, OC_HIGH_QOS, &cbData, NULL, 0);
1145 if (res != OC_STACK_OK)
1147 OIC_LOG(ERROR, TAG, "OCStack resource error");
1154 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
1155 while (OC_STACK_OK == res && !pDInfo->isFound)
1157 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
1159 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
1160 if (elapsed > waittime)
1167 if (OC_STACK_OK != res)
1169 OIC_LOG (ERROR, TAG, "Failed to wait response for secure discovery.");
1172 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1173 if (OC_STACK_OK != resCancel)
1175 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1180 res = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1181 if (OC_STACK_OK != res)
1183 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1188 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscoveryInUnicast");
1190 if (!pDInfo->isFound)
1192 res = OC_STACK_TIMEOUT;
1200 #ifdef MULTIPLE_OWNER
1201 static OCStackApplicationResult MOTDeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
1202 OCClientResponse *clientResponse)
1206 OIC_LOG(ERROR, TAG, "Lost List of device information");
1207 return OC_STACK_KEEP_TRANSACTION;
1212 if (NULL == clientResponse->payload)
1214 OIC_LOG(INFO, TAG, "Skipping Null payload");
1215 return OC_STACK_KEEP_TRANSACTION;
1217 if (OC_STACK_OK != clientResponse->result)
1219 OIC_LOG(INFO, TAG, "Error in response");
1220 return OC_STACK_KEEP_TRANSACTION;
1224 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
1226 OIC_LOG(INFO, TAG, "Unknown payload type");
1227 return OC_STACK_KEEP_TRANSACTION;
1230 OicSecDoxm_t *ptrDoxm = NULL;
1231 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1232 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1234 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
1235 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
1237 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
1238 return OC_STACK_KEEP_TRANSACTION;
1242 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
1244 //If this is owend device discovery we have to filter out the responses.
1245 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
1246 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
1248 // Get my device ID from doxm resource
1250 memset(&myId, 0, sizeof(myId));
1251 OCStackResult res = GetDoxmDevOwnerId(&myId);
1252 if(OC_STACK_OK != res)
1254 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
1255 DeleteDoxmBinData(ptrDoxm);
1256 return OC_STACK_KEEP_TRANSACTION;
1259 res = GetDoxmDeviceID(&myId);
1260 if(OC_STACK_OK != res)
1262 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
1263 DeleteDoxmBinData(ptrDoxm);
1264 return OC_STACK_KEEP_TRANSACTION;
1266 //if this is owned discovery and this is PT's reply, discard it
1267 if((pDInfo->isOwnedDiscovery) &&
1268 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
1270 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
1271 DeleteDoxmBinData(ptrDoxm);
1272 return OC_STACK_KEEP_TRANSACTION;
1275 if(pDInfo->isOwnedDiscovery)
1277 OicSecSubOwner_t* subOwner = NULL;
1278 LL_FOREACH(ptrDoxm->subOwners, subOwner)
1280 if(memcmp(myId.id, subOwner->uuid.id, sizeof(myId.id)) == 0)
1288 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1289 clientResponse->connType, ptrDoxm);
1290 if (OC_STACK_OK != res)
1292 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1293 DeleteDoxmBinData(ptrDoxm);
1294 return OC_STACK_KEEP_TRANSACTION;
1297 res = SecurePortDiscovery(pDInfo, clientResponse);
1298 if(OC_STACK_OK != res)
1300 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1301 DeleteDoxmBinData(ptrDoxm);
1302 return OC_STACK_KEEP_TRANSACTION;
1307 OIC_LOG(ERROR, TAG, "discarding device's reply, because not a SubOwner.");
1308 DeleteDoxmBinData(ptrDoxm);
1309 return OC_STACK_KEEP_TRANSACTION;
1314 if(ptrDoxm->mom && OIC_MULTIPLE_OWNER_DISABLE != ptrDoxm->mom->mode)
1316 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1317 clientResponse->connType, ptrDoxm);
1318 if (OC_STACK_OK != res)
1320 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1321 DeleteDoxmBinData(ptrDoxm);
1322 return OC_STACK_KEEP_TRANSACTION;
1325 res = SecurePortDiscovery(pDInfo, clientResponse);
1326 if(OC_STACK_OK != res)
1328 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1329 DeleteDoxmBinData(ptrDoxm);
1330 return OC_STACK_KEEP_TRANSACTION;
1335 OIC_LOG(ERROR, TAG, "discarding mom disabled device's reply");
1336 DeleteDoxmBinData(ptrDoxm);
1337 return OC_STACK_KEEP_TRANSACTION;
1341 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
1344 return OC_STACK_KEEP_TRANSACTION;
1349 OIC_LOG(INFO, TAG, "Skiping Null response");
1350 return OC_STACK_KEEP_TRANSACTION;
1353 return OC_STACK_DELETE_TRANSACTION;
1358 * Discover multiple OTM enabled devices in the same IP subnet.
1360 * @param[in] waittime Timeout in seconds.
1361 * @param[in] ppDevicesList List of OCProvisionDev_t.
1363 * @return OC_STACK_OK on success otherwise error.
1365 OCStackResult PMMultipleOwnerDeviceDiscovery(unsigned short waittime, bool isMultipleOwned, OCProvisionDev_t **ppDevicesList)
1367 OIC_LOG(DEBUG, TAG, "IN PMMultipleOwnerEnabledDeviceDiscovery");
1369 if (NULL != *ppDevicesList)
1371 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1372 return OC_STACK_INVALID_PARAM;
1375 char DOXM_MOM_ENABLE_MULTICAST_QUERY[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1376 char DOXM_MULTIPLE_OWNED_MULTICAST_QUERY[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1377 snprintf(DOXM_MOM_ENABLE_MULTICAST_QUERY, sizeof(DOXM_MOM_ENABLE_MULTICAST_QUERY),
1379 OIC_RSRC_DOXM_URI, OIC_JSON_MOM_NAME, OIC_MULTIPLE_OWNER_DISABLE,
1380 OIC_JSON_OWNED_NAME);
1381 snprintf(DOXM_MULTIPLE_OWNED_MULTICAST_QUERY, sizeof(DOXM_MOM_ENABLE_MULTICAST_QUERY),
1383 OIC_RSRC_DOXM_URI, OIC_JSON_OWNED_NAME);
1385 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
1388 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
1389 return OC_STACK_NO_MEMORY;
1392 pDInfo->ppDevicesList = ppDevicesList;
1393 pDInfo->pCandidateList = NULL;
1394 pDInfo->isOwnedDiscovery = isMultipleOwned;
1396 OCCallbackData cbData;
1397 cbData.cb = &MOTDeviceDiscoveryHandler;
1398 cbData.context = (void *)pDInfo;
1400 OCStackResult res = OC_STACK_ERROR;
1402 const char* query = isMultipleOwned ? DOXM_MULTIPLE_OWNED_MULTICAST_QUERY :
1403 DOXM_MOM_ENABLE_MULTICAST_QUERY;
1405 OCDoHandle handle = NULL;
1406 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1407 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1408 if (res != OC_STACK_OK)
1410 OIC_LOG(ERROR, TAG, "OCStack resource error");
1415 //Waiting for each response.
1416 res = PMTimeout(waittime, true);
1417 if(OC_STACK_OK != res)
1419 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1421 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1422 if(OC_STACK_OK != resCancel)
1424 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1428 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1429 if (OC_STACK_OK != res)
1431 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1435 OIC_LOG(DEBUG, TAG, "OUT PMMultipleOwnerEnabledDeviceDiscovery");
1440 #endif //MULTIPLE_OWNER
1442 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
1443 const OCClientResponse *clientResponse)
1445 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
1447 if(NULL == discoveryInfo || NULL == clientResponse)
1449 return OC_STACK_INVALID_PARAM;
1452 OCProvisionDev_t *pDev = GetDevice(&discoveryInfo->pCandidateList,
1453 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1456 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to get device");
1457 return OC_STACK_ERROR;
1460 //Try to the unicast discovery to getting secure port
1461 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1462 if(!PMGenerateQuery(false,
1463 pDev->endpoint.addr, pDev->endpoint.port,
1465 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
1467 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
1468 return OC_STACK_ERROR;
1470 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1472 // Set filter query with rt=oic.r.doxm
1473 const char RES_DOXM_QUERY_FMT[] = "%s?%s=%s";
1474 char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1475 snprintf(uri, sizeof(uri), RES_DOXM_QUERY_FMT, query,
1476 OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
1478 OIC_LOG_V(DEBUG, TAG, "URI=%s", uri);
1480 OCCallbackData cbData;
1481 cbData.cb = &SecurePortDiscoveryHandler;
1482 cbData.context = (void*)discoveryInfo;
1484 OCStackResult ret = OCDoResource(&pDev->handle, OC_REST_DISCOVER, uri, 0, 0,
1485 pDev->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1486 if(OC_STACK_OK != ret)
1488 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
1493 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1496 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1502 * Since security version discovery does not used anymore, disable security version discovery.
1503 * Need to discussion to removing all version discovery related codes.
1506 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1507 const OCClientResponse *clientResponse)
1509 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1511 if(NULL == discoveryInfo || NULL == clientResponse)
1513 return OC_STACK_INVALID_PARAM;
1516 //Try to the unicast discovery to getting security version
1517 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1518 if(!PMGenerateQuery(false,
1519 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1520 clientResponse->connType,
1521 query, sizeof(query), OIC_RSRC_VER_URI))
1523 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1524 return OC_STACK_ERROR;
1526 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1528 OCCallbackData cbData;
1529 cbData.cb = &SecurityVersionDiscoveryHandler;
1530 cbData.context = (void*)discoveryInfo;
1532 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1533 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1534 if(OC_STACK_OK != ret)
1536 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1541 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1544 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1551 * Function to print OCProvisionDev_t for debug purpose.
1553 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1556 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1560 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1561 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1562 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1563 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1564 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1568 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1572 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1574 if(*pUuidList == NULL || targetId == NULL)
1578 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1579 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1581 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1583 LL_DELETE(*pUuidList, tmp1);