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;
493 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
496 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
497 prefix, address, port, uri);
501 char addressEncoded[128] = {0};
503 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
504 sizeof(addressEncoded),
506 if (OC_STACK_OK != result)
508 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : encoding error %d\n", result);
512 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
513 prefix, addressEncoded, port, uri);
517 OIC_LOG(ERROR, TAG, "Unknown address format.");
520 // snprintf return value check
523 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
526 else if ((size_t)snRet >= bufferSize)
528 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
533 case CT_ADAPTER_GATT_BTLE:
534 snRet = snprintf(buffer, bufferSize, "%s%s%s",
535 prefix, address, uri);
536 // snprintf return value check
539 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
542 else if ((size_t)snRet >= bufferSize)
544 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
549 // TODO: We need to verify tinyDTLS in below cases
550 case CT_ADAPTER_RFCOMM_BTEDR:
551 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
555 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
563 * Since security version discovery does not used anymore, disable security version discovery.
564 * Need to discussion to removing all version discovery related codes.
567 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
568 OCClientResponse *clientResponse)
572 OIC_LOG(ERROR, TAG, "Lost List of device information");
573 return OC_STACK_KEEP_TRANSACTION;
578 if (NULL == clientResponse->payload)
580 OIC_LOG(INFO, TAG, "Skiping Null payload");
581 return OC_STACK_KEEP_TRANSACTION;
583 if (OC_STACK_OK != clientResponse->result)
585 OIC_LOG(INFO, TAG, "Error in response");
586 return OC_STACK_KEEP_TRANSACTION;
590 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
592 OIC_LOG(INFO, TAG, "Unknown payload type");
593 return OC_STACK_KEEP_TRANSACTION;
596 OicSecVer_t *ptrVer = NULL;
597 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
598 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
600 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
601 if ((NULL == ptrVer) && (OC_STACK_OK != res))
603 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
604 return OC_STACK_KEEP_TRANSACTION;
608 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
610 //If this is owend device discovery we have to filter out the responses.
611 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
612 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
613 clientResponse->devAddr.port, ptrVer->secv);
614 if (OC_STACK_OK != res)
616 OIC_LOG(ERROR, TAG, "Error while getting security version.");
617 DeleteVerBinData(ptrVer);
618 return OC_STACK_KEEP_TRANSACTION;
621 OIC_LOG(INFO, TAG, "= Discovered security version =");
622 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
623 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
624 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
626 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
627 DeleteVerBinData(ptrVer);
633 OIC_LOG(INFO, TAG, "Skiping Null response");
634 return OC_STACK_KEEP_TRANSACTION;
637 return OC_STACK_DELETE_TRANSACTION;
641 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
642 OCClientResponse *clientResponse)
646 OIC_LOG(ERROR, TAG, "Lost List of device information");
647 return OC_STACK_DELETE_TRANSACTION;
652 if (NULL == clientResponse->payload)
654 OIC_LOG(INFO, TAG, "Skiping Null payload");
658 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
660 OIC_LOG(INFO, TAG, "Wrong payload type");
661 return OC_STACK_DELETE_TRANSACTION;
664 uint16_t securePort = 0;
665 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
667 // Use seure port of doxm for OTM and Provision.
670 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
672 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
673 OIC_LOG(INFO, TAG, "Found doxm resource.");
678 resPayload = resPayload->next;
681 if (NULL == resPayload)
683 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
684 return OC_STACK_DELETE_TRANSACTION;
686 if (resPayload && resPayload->secure)
688 securePort = resPayload->port;
692 OIC_LOG(INFO, TAG, "Can not find secure port information.");
693 return OC_STACK_DELETE_TRANSACTION;
696 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
698 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
700 if(pDInfo->isSingleDiscovery && pDInfo->isFound)
702 OIC_LOG(INFO, TAG, "Device is already founded at Single mode.");
703 return OC_STACK_DELETE_TRANSACTION;
706 OCProvisionDev_t *ptr = GetDevice(&pDInfo->pCandidateList,
707 clientResponse->devAddr.addr,
708 clientResponse->devAddr.port);
711 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery candidate device list");
712 return OC_STACK_DELETE_TRANSACTION;
715 OCStackResult res = UpdateSecurePortOfDevice(&pDInfo->pCandidateList,
716 clientResponse->devAddr.addr,
717 clientResponse->devAddr.port,
723 if (OC_STACK_OK != res)
725 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
726 return OC_STACK_DELETE_TRANSACTION;
729 res = MoveDeviceList(pDInfo->ppDevicesList, &pDInfo->pCandidateList, &clientResponse->devAddr);
730 if(OC_STACK_OK != res)
732 OIC_LOG(ERROR, TAG, "Error while move the discovered device to list.");
733 return OC_STACK_DELETE_TRANSACTION;
736 if(pDInfo->isSingleDiscovery)
738 pDInfo->isFound = true;
742 * Since security version discovery does not used anymore, disable security version discovery.
743 * Need to discussion to removing all version discovery related codes.
746 res = SecurityVersionDiscovery(pDInfo, clientResponse);
747 if(OC_STACK_OK != res)
749 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
750 return OC_STACK_DELETE_TRANSACTION;
753 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
756 return OC_STACK_DELETE_TRANSACTION;
760 OIC_LOG(INFO, TAG, "Skiping Null response");
763 return OC_STACK_DELETE_TRANSACTION;
766 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
767 OCClientResponse *clientResponse)
771 OIC_LOG(ERROR, TAG, "Lost List of device information");
772 return OC_STACK_KEEP_TRANSACTION;
777 if (NULL == clientResponse->payload)
779 OIC_LOG(INFO, TAG, "Skiping Null payload");
780 return OC_STACK_KEEP_TRANSACTION;
782 if (OC_STACK_OK != clientResponse->result)
784 OIC_LOG(INFO, TAG, "Error in response");
785 return OC_STACK_KEEP_TRANSACTION;
789 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
791 OIC_LOG(INFO, TAG, "Unknown payload type");
792 return OC_STACK_KEEP_TRANSACTION;
795 OicSecDoxm_t *ptrDoxm = NULL;
796 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
797 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
799 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
800 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
802 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
803 return OC_STACK_KEEP_TRANSACTION;
807 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
809 //If this is owend device discovery we have to filter out the responses.
810 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
811 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
813 // Get my device ID from doxm resource
815 memset(&myId, 0, sizeof(myId));
816 OCStackResult res = GetDoxmDeviceID(&myId);
817 if(OC_STACK_OK != res)
819 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
820 DeleteDoxmBinData(ptrDoxm);
821 return OC_STACK_KEEP_TRANSACTION;
824 // If this is owned discovery response but owner is not me then discard it.
825 if( (pDInfo->isOwnedDiscovery) &&
826 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
828 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
829 DeleteDoxmBinData(ptrDoxm);
830 return OC_STACK_KEEP_TRANSACTION;
833 //if targetId and discovered deviceID are different, discard it
834 if ((pDInfo->isSingleDiscovery) &&
835 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
837 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
838 DeleteDoxmBinData(ptrDoxm);
839 return OC_STACK_KEEP_TRANSACTION;
841 //If self reply, discard it
842 if (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id)))
844 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
845 DeleteDoxmBinData(ptrDoxm);
846 return OC_STACK_KEEP_TRANSACTION;
849 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
850 clientResponse->connType, ptrDoxm);
851 if (OC_STACK_OK != res)
853 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
854 DeleteDoxmBinData(ptrDoxm);
855 return OC_STACK_KEEP_TRANSACTION;
857 res = SecurePortDiscovery(pDInfo, clientResponse);
858 if(OC_STACK_OK != res)
860 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
861 DeleteDoxmBinData(ptrDoxm);
862 return OC_STACK_KEEP_TRANSACTION;
865 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
867 return OC_STACK_KEEP_TRANSACTION;
872 OIC_LOG(INFO, TAG, "Skiping Null response");
873 return OC_STACK_KEEP_TRANSACTION;
876 return OC_STACK_DELETE_TRANSACTION;
879 static void DeviceDiscoveryDeleteHandler(void *ctx)
881 OIC_LOG(DEBUG, TAG, "IN DeviceDiscoveryDeleteHandler");
884 OIC_LOG(WARNING, TAG, "Not found context in DeviceDiscoveryDeleteHandler");
888 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
889 if (NULL != pDInfo->pCandidateList)
891 OCProvisionDev_t *pDev = NULL;
892 LL_FOREACH(pDInfo->pCandidateList, pDev)
894 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
895 pDev->endpoint.addr, pDev->endpoint.port);
896 if(OC_STACK_OK != OCCancel(pDev->handle,OC_HIGH_QOS,NULL,0))
898 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
901 PMDeleteDeviceList(pDInfo->pCandidateList);
903 OIC_LOG(DEBUG, TAG, "OUT DeviceDiscoveryDeleteHandler");
907 * Discover owned/unowned device in the specified endpoint/deviceID.
908 * It will return the found device even though timeout is not exceeded.
910 * @param[in] waittime Timeout in seconds
911 * @param[in] deviceID deviceID of target device.
912 * @param[out] ppFoundDevice OCProvisionDev_t of found device
914 * @return OC_STACK_OK on success otherwise error.\n
915 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
917 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
918 OCProvisionDev_t **ppFoundDevice)
920 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
922 if (NULL != *ppFoundDevice)
924 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
925 return OC_STACK_INVALID_PARAM;
928 if (NULL == deviceID)
930 OIC_LOG(ERROR, TAG, "Invalid device ID");
931 return OC_STACK_INVALID_PARAM;
935 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
938 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
939 return OC_STACK_NO_MEMORY;
942 pDInfo->ppDevicesList = ppFoundDevice;
943 pDInfo->pCandidateList = NULL;
944 pDInfo->isOwnedDiscovery = false;
945 pDInfo->isSingleDiscovery = true;
946 pDInfo->isFound = false;
947 pDInfo->targetId = deviceID;
949 OCCallbackData cbData;
950 cbData.cb = &DeviceDiscoveryHandler;
951 cbData.context = (void *)pDInfo;
952 cbData.cd = &DeviceDiscoveryDeleteHandler;
954 OCStackResult res = OC_STACK_ERROR;
956 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
957 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
959 OCDoHandle handle = NULL;
960 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
961 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
962 if (res != OC_STACK_OK)
964 OIC_LOG(ERROR, TAG, "OCStack resource error");
969 //Waiting for each response.
971 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
972 while (OC_STACK_OK == res && !pDInfo->isFound)
974 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
976 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
977 if (elapsed > waittime)
984 if(OC_STACK_OK != res)
986 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
988 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
989 if(OC_STACK_OK != resCancel)
991 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
996 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
997 if (OC_STACK_OK != res)
999 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1003 if (!pDInfo->isFound)
1005 res = OC_STACK_TIMEOUT;
1008 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
1013 * Discover owned/unowned devices in the same IP subnet. .
1015 * @param[in] waittime Timeout in seconds.
1016 * @param[in] isOwned bool flag for owned / unowned discovery
1017 * @param[in] ppDevicesList List of OCProvisionDev_t.
1019 * @return OC_STACK_OK on success otherwise error.
1021 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
1023 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
1025 if (NULL != *ppDevicesList)
1027 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1028 return OC_STACK_INVALID_PARAM;
1031 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
1032 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
1034 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
1037 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
1038 return OC_STACK_NO_MEMORY;
1041 pDInfo->ppDevicesList = ppDevicesList;
1042 pDInfo->pCandidateList = NULL;
1043 pDInfo->isOwnedDiscovery = isOwned;
1044 pDInfo->isSingleDiscovery = false;
1045 pDInfo->targetId = NULL;
1047 OCCallbackData cbData;
1048 cbData.cb = &DeviceDiscoveryHandler;
1049 cbData.context = (void *)pDInfo;
1050 cbData.cd = &DeviceDiscoveryDeleteHandler;
1051 OCStackResult res = OC_STACK_ERROR;
1053 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
1054 DOXM_OWNED_FALSE_MULTICAST_QUERY;
1056 OCDoHandle handle = NULL;
1057 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1058 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1059 if (res != OC_STACK_OK)
1061 OIC_LOG(ERROR, TAG, "OCStack resource error");
1067 //Waiting for each response.
1068 res = PMTimeout(waittime, true);
1069 if(OC_STACK_OK != res)
1071 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1074 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1075 if(OC_STACK_OK != resCancel)
1077 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1081 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1082 if (OC_STACK_OK != res)
1084 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1089 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
1095 OCStackResult PMSingleDeviceDiscoveryInUnicast(unsigned short waittime, const OicUuid_t* deviceID,
1096 const char* hostAddress, OCConnectivityType connType,
1097 OCProvisionDev_t **ppFoundDevice)
1099 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscoveryInUnicast");
1101 if (NULL != *ppFoundDevice)
1103 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1104 return OC_STACK_INVALID_PARAM;
1107 if (NULL == deviceID)
1109 OIC_LOG(ERROR, TAG, "Invalid device ID");
1110 return OC_STACK_INVALID_PARAM;
1113 DiscoveryInfo *pDInfo = (DiscoveryInfo*)OICCalloc(1, sizeof(DiscoveryInfo));
1116 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscoveryInUnicast : Memory allocation failed.");
1117 return OC_STACK_NO_MEMORY;
1120 pDInfo->ppDevicesList = ppFoundDevice;
1121 pDInfo->pCandidateList = NULL;
1122 pDInfo->isOwnedDiscovery = false;
1123 pDInfo->isSingleDiscovery = true;
1124 pDInfo->isFound = false;
1125 pDInfo->targetId = deviceID;
1127 OCCallbackData cbData;
1128 cbData.cb = &DeviceDiscoveryHandler;
1129 cbData.context = (void *)pDInfo;
1130 cbData.cd = &DeviceDiscoveryDeleteHandler;
1132 OCStackResult res = OC_STACK_ERROR;
1134 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
1135 if (hostAddress == NULL)
1139 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "%s/oic/sec/doxm", hostAddress);
1140 connType = connType & CT_MASK_ADAPTER;
1142 OCDoHandle handle = NULL;
1143 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1144 connType, OC_HIGH_QOS, &cbData, NULL, 0);
1146 if (res != OC_STACK_OK)
1148 OIC_LOG(ERROR, TAG, "OCStack resource error");
1155 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
1156 while (OC_STACK_OK == res && !pDInfo->isFound)
1158 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
1160 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
1161 if (elapsed > waittime)
1168 if (OC_STACK_OK != res)
1170 OIC_LOG (ERROR, TAG, "Failed to wait response for secure discovery.");
1173 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1174 if (OC_STACK_OK != resCancel)
1176 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1181 res = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1182 if (OC_STACK_OK != res)
1184 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1189 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscoveryInUnicast");
1191 if (!pDInfo->isFound)
1193 res = OC_STACK_TIMEOUT;
1201 #ifdef MULTIPLE_OWNER
1202 static OCStackApplicationResult MOTDeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
1203 OCClientResponse *clientResponse)
1207 OIC_LOG(ERROR, TAG, "Lost List of device information");
1208 return OC_STACK_KEEP_TRANSACTION;
1213 if (NULL == clientResponse->payload)
1215 OIC_LOG(INFO, TAG, "Skipping Null payload");
1216 return OC_STACK_KEEP_TRANSACTION;
1218 if (OC_STACK_OK != clientResponse->result)
1220 OIC_LOG(INFO, TAG, "Error in response");
1221 return OC_STACK_KEEP_TRANSACTION;
1225 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
1227 OIC_LOG(INFO, TAG, "Unknown payload type");
1228 return OC_STACK_KEEP_TRANSACTION;
1231 OicSecDoxm_t *ptrDoxm = NULL;
1232 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1233 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1235 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
1236 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
1238 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
1239 return OC_STACK_KEEP_TRANSACTION;
1243 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
1245 //If this is owend device discovery we have to filter out the responses.
1246 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
1247 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
1249 // Get my device ID from doxm resource
1251 memset(&myId, 0, sizeof(myId));
1252 OCStackResult res = GetDoxmDevOwnerId(&myId);
1253 if(OC_STACK_OK != res)
1255 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
1256 DeleteDoxmBinData(ptrDoxm);
1257 return OC_STACK_KEEP_TRANSACTION;
1260 res = GetDoxmDeviceID(&myId);
1261 if(OC_STACK_OK != res)
1263 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
1264 DeleteDoxmBinData(ptrDoxm);
1265 return OC_STACK_KEEP_TRANSACTION;
1267 //if this is owned discovery and this is PT's reply, discard it
1268 if((pDInfo->isOwnedDiscovery) &&
1269 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
1271 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
1272 DeleteDoxmBinData(ptrDoxm);
1273 return OC_STACK_KEEP_TRANSACTION;
1276 if(pDInfo->isOwnedDiscovery)
1278 OicSecSubOwner_t* subOwner = NULL;
1279 LL_FOREACH(ptrDoxm->subOwners, subOwner)
1281 if(memcmp(myId.id, subOwner->uuid.id, sizeof(myId.id)) == 0)
1289 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1290 clientResponse->connType, ptrDoxm);
1291 if (OC_STACK_OK != res)
1293 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1294 DeleteDoxmBinData(ptrDoxm);
1295 return OC_STACK_KEEP_TRANSACTION;
1298 res = SecurePortDiscovery(pDInfo, clientResponse);
1299 if(OC_STACK_OK != res)
1301 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1302 DeleteDoxmBinData(ptrDoxm);
1303 return OC_STACK_KEEP_TRANSACTION;
1308 OIC_LOG(ERROR, TAG, "discarding device's reply, because not a SubOwner.");
1309 DeleteDoxmBinData(ptrDoxm);
1310 return OC_STACK_KEEP_TRANSACTION;
1315 if(ptrDoxm->mom && OIC_MULTIPLE_OWNER_DISABLE != ptrDoxm->mom->mode)
1317 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1318 clientResponse->connType, ptrDoxm);
1319 if (OC_STACK_OK != res)
1321 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1322 DeleteDoxmBinData(ptrDoxm);
1323 return OC_STACK_KEEP_TRANSACTION;
1326 res = SecurePortDiscovery(pDInfo, clientResponse);
1327 if(OC_STACK_OK != res)
1329 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1330 DeleteDoxmBinData(ptrDoxm);
1331 return OC_STACK_KEEP_TRANSACTION;
1336 OIC_LOG(ERROR, TAG, "discarding mom disabled device's reply");
1337 DeleteDoxmBinData(ptrDoxm);
1338 return OC_STACK_KEEP_TRANSACTION;
1342 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
1345 return OC_STACK_KEEP_TRANSACTION;
1350 OIC_LOG(INFO, TAG, "Skiping Null response");
1351 return OC_STACK_KEEP_TRANSACTION;
1354 return OC_STACK_DELETE_TRANSACTION;
1359 * Discover multiple OTM enabled devices in the same IP subnet.
1361 * @param[in] waittime Timeout in seconds.
1362 * @param[in] ppDevicesList List of OCProvisionDev_t.
1364 * @return OC_STACK_OK on success otherwise error.
1366 OCStackResult PMMultipleOwnerDeviceDiscovery(unsigned short waittime, bool isMultipleOwned, OCProvisionDev_t **ppDevicesList)
1368 OIC_LOG(DEBUG, TAG, "IN PMMultipleOwnerEnabledDeviceDiscovery");
1370 if (NULL != *ppDevicesList)
1372 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1373 return OC_STACK_INVALID_PARAM;
1376 char DOXM_MOM_ENABLE_MULTICAST_QUERY[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1377 char DOXM_MULTIPLE_OWNED_MULTICAST_QUERY[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1378 snprintf(DOXM_MOM_ENABLE_MULTICAST_QUERY, sizeof(DOXM_MOM_ENABLE_MULTICAST_QUERY),
1380 OIC_RSRC_DOXM_URI, OIC_JSON_MOM_NAME, OIC_MULTIPLE_OWNER_DISABLE,
1381 OIC_JSON_OWNED_NAME);
1382 snprintf(DOXM_MULTIPLE_OWNED_MULTICAST_QUERY, sizeof(DOXM_MOM_ENABLE_MULTICAST_QUERY),
1384 OIC_RSRC_DOXM_URI, OIC_JSON_OWNED_NAME);
1386 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
1389 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
1390 return OC_STACK_NO_MEMORY;
1393 pDInfo->ppDevicesList = ppDevicesList;
1394 pDInfo->pCandidateList = NULL;
1395 pDInfo->isOwnedDiscovery = isMultipleOwned;
1397 OCCallbackData cbData;
1398 cbData.cb = &MOTDeviceDiscoveryHandler;
1399 cbData.context = (void *)pDInfo;
1401 OCStackResult res = OC_STACK_ERROR;
1403 const char* query = isMultipleOwned ? DOXM_MULTIPLE_OWNED_MULTICAST_QUERY :
1404 DOXM_MOM_ENABLE_MULTICAST_QUERY;
1406 OCDoHandle handle = NULL;
1407 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1408 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1409 if (res != OC_STACK_OK)
1411 OIC_LOG(ERROR, TAG, "OCStack resource error");
1416 //Waiting for each response.
1417 res = PMTimeout(waittime, true);
1418 if(OC_STACK_OK != res)
1420 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1422 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1423 if(OC_STACK_OK != resCancel)
1425 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1429 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1430 if (OC_STACK_OK != res)
1432 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1436 OIC_LOG(DEBUG, TAG, "OUT PMMultipleOwnerEnabledDeviceDiscovery");
1441 #endif //MULTIPLE_OWNER
1443 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
1444 const OCClientResponse *clientResponse)
1446 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
1448 if(NULL == discoveryInfo || NULL == clientResponse)
1450 return OC_STACK_INVALID_PARAM;
1453 OCProvisionDev_t *pDev = GetDevice(&discoveryInfo->pCandidateList,
1454 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1457 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to get device");
1458 return OC_STACK_ERROR;
1461 //Try to the unicast discovery to getting secure port
1462 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1463 if(!PMGenerateQuery(false,
1464 pDev->endpoint.addr, pDev->endpoint.port,
1466 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
1468 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
1469 return OC_STACK_ERROR;
1471 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1473 // Set filter query with rt=oic.r.doxm
1474 const char RES_DOXM_QUERY_FMT[] = "%s?%s=%s";
1475 char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1476 snprintf(uri, sizeof(uri), RES_DOXM_QUERY_FMT, query,
1477 OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
1479 OIC_LOG_V(DEBUG, TAG, "URI=%s", uri);
1481 OCCallbackData cbData;
1482 cbData.cb = &SecurePortDiscoveryHandler;
1483 cbData.context = (void*)discoveryInfo;
1485 OCStackResult ret = OCDoResource(&pDev->handle, OC_REST_DISCOVER, uri, 0, 0,
1486 pDev->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1487 if(OC_STACK_OK != ret)
1489 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
1494 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1497 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1503 * Since security version discovery does not used anymore, disable security version discovery.
1504 * Need to discussion to removing all version discovery related codes.
1507 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1508 const OCClientResponse *clientResponse)
1510 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1512 if(NULL == discoveryInfo || NULL == clientResponse)
1514 return OC_STACK_INVALID_PARAM;
1517 //Try to the unicast discovery to getting security version
1518 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1519 if(!PMGenerateQuery(false,
1520 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1521 clientResponse->connType,
1522 query, sizeof(query), OIC_RSRC_VER_URI))
1524 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1525 return OC_STACK_ERROR;
1527 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1529 OCCallbackData cbData;
1530 cbData.cb = &SecurityVersionDiscoveryHandler;
1531 cbData.context = (void*)discoveryInfo;
1533 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1534 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1535 if(OC_STACK_OK != ret)
1537 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1542 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1545 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1552 * Function to print OCProvisionDev_t for debug purpose.
1554 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1557 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1561 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1562 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1563 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1564 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1565 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1569 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1573 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1575 if(*pUuidList == NULL || targetId == NULL)
1579 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1580 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1582 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1584 LL_DELETE(*pUuidList, tmp1);