1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
20 #ifndef _POSIX_C_SOURCE
21 #define _POSIX_C_SOURCE 200112L
24 #include "iotivity_config.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
39 #include "ocpayload.h"
41 #include "securevirtualresourcetypes.h"
42 #include "srmresourcestrings.h" //@note: SRM's internal header
43 #include "doxmresource.h" //@note: SRM's internal header
44 #include "pstatresource.h" //@note: SRM's internal header
45 #include "verresource.h" //@note: SRM's internal header
48 #include "pmutility.h"
50 #include "srmutility.h"
52 #define TAG ("PM-UTILITY")
54 typedef struct _DiscoveryInfo{
55 OCProvisionDev_t **ppDevicesList;
56 OCProvisionDev_t *pCandidateList;
57 bool isOwnedDiscovery;
58 bool isSingleDiscovery;
60 const OicUuid_t *targetId;
64 * Function to discover secre port information through unicast
66 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
67 * @param[in] clientResponse Response information(It will contain payload)
69 * @return OC_STACK_OK on success otherwise error.
71 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
72 const OCClientResponse *clientResponse);
75 * Function to discover security version information through unicast
77 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
78 * @param[in] clientResponse Response information(It will contain payload)
80 * @return OC_STACK_OK on success otherwise error.
82 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
83 const OCClientResponse *clientResponse);
86 * Callback handler for PMDeviceDiscovery API.
88 * @param[in] ctx User context
89 * @param[in] handle Handler for response
90 * @param[in] clientResponse Response information (It will contain payload)
91 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
92 * OC_STACK_DELETE_TRANSACTION to delete it.
94 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
95 OCClientResponse *clientResponse);
98 * Callback handler for getting secure port information using /oic/res discovery.
100 * @param[in] ctx user context
101 * @param[in] handle Handle for response
102 * @param[in] clientResponse Response information(It will contain payload)
104 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
105 * OC_STACK_DELETE_TRANSACTION to delete it.
107 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
108 OCClientResponse *clientResponse);
111 * Callback handler for security version discovery.
113 * @param[in] ctx User context
114 * @param[in] handle Handler for response
115 * @param[in] clientResponse Response information (It will contain payload)
116 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
117 * OC_STACK_DELETE_TRANSACTION to delete it.
119 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
120 OCClientResponse *clientResponse);
123 * Function to search node in linked list that matches given IP and port.
125 * @param[in] pList List of OCProvisionDev_t.
126 * @param[in] addr address of target device.
127 * @param[in] port port of remote server.
129 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
131 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
133 if(NULL == addr || NULL == *ppDevicesList)
135 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
139 OCProvisionDev_t *ptr = NULL;
140 LL_FOREACH(*ppDevicesList, ptr)
142 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
153 * Add device information to list.
155 * @param[in] pList List of OCProvisionDev_t.
156 * @param[in] endpoint target device endpoint.
157 * @param[in] connType connectivity type of endpoint
158 * @param[in] doxm pointer to doxm instance.
160 * @return OC_STACK_OK for success and error code otherwise.
162 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
163 OCConnectivityType connType, OicSecDoxm_t *doxm)
165 if (NULL == endpoint)
167 return OC_STACK_INVALID_PARAM;
170 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
173 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
176 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
177 return OC_STACK_NO_MEMORY;
180 ptr->endpoint = *endpoint;
182 ptr->securePort = DEFAULT_SECURE_PORT;
184 ptr->connType = connType;
185 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
186 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
189 LL_PREPEND(*ppDevicesList, ptr);
196 * Move device object between two device lists.
198 * @param[in] ppDstDevicesList Destination list of OCProvisionDev_t.
199 * @param[in] ppSrcDevicesList Source list of OCProvisionDev_t.
200 * @param[in] endpoint target device endpoint.
202 * @return OC_STACK_OK for success and error code otherwise.
204 OCStackResult MoveDeviceList(OCProvisionDev_t **ppDstDevicesList,
205 OCProvisionDev_t **ppSrcDevicesList, OCDevAddr* endpoint)
207 if (NULL == ppSrcDevicesList || NULL == endpoint)
209 return OC_STACK_INVALID_PARAM;
212 OCProvisionDev_t *ptr = GetDevice(ppSrcDevicesList, endpoint->addr, endpoint->port);
215 LL_DELETE(*ppSrcDevicesList, ptr);
216 LL_PREPEND(*ppDstDevicesList, ptr);
217 OIC_LOG_V(DEBUG, TAG, "MoveDeviceList success : %s(%d)", endpoint->addr, endpoint->port);
221 return OC_STACK_ERROR;
225 * Function to set secure port information from the given list of devices.
227 * @param[in] pList List of OCProvisionDev_t.
228 * @param[in] addr address of target device.
229 * @param[in] port port of remote server.
230 * @param[in] secureport secure port information.
232 * @return OC_STACK_OK for success and errorcode otherwise.
234 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
235 uint16_t port, uint16_t securePort
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 ptr->securePort = securePort;
252 ptr->tcpPort = tcpPort;
259 * Function to set security version information from the given list of devices.
261 * @param[in] pList List of OCProvisionDev_t.
262 * @param[in] addr address of target device.
263 * @param[in] port port of remote server.
264 * @param[in] secVer security version information.
266 * @return OC_STACK_OK for success and errorcode otherwise.
268 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
269 uint16_t port, const char* secVer)
273 return OC_STACK_INVALID_PARAM;
276 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
280 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
281 return OC_STACK_ERROR;
284 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
290 * This function deletes list of provision target devices
292 * @param[in] pDevicesList List of OCProvisionDev_t.
294 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
298 OCProvisionDev_t *del = NULL, *tmp = NULL;
299 LL_FOREACH_SAFE(pDevicesList, del, tmp)
301 LL_DELETE(pDevicesList, del);
303 DeleteDoxmBinData(del->doxm);
304 DeletePstatBinData(del->pstat);
310 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
312 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
316 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
320 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
321 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
322 VERIFY_NON_NULL(TAG, newDev, ERROR);
324 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
328 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
329 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
331 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
332 // We have to assign NULL for not necessary information to prevent memory corruption.
333 newDev->pstat->sm = NULL;
338 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
339 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
341 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
342 // We have to assign NULL for not necessary information to prevent memory corruption.
343 newDev->doxm->oxmType = NULL;
344 newDev->doxm->oxm = NULL;
347 if (0 == strlen(src->secVer))
349 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
353 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
356 newDev->securePort = src->securePort;
357 newDev->devStatus = src->devStatus;
358 newDev->connType = src->connType;
361 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
366 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
369 OICFree(newDev->pstat);
370 OICFree(newDev->doxm);
377 * Timeout implementation for secure discovery. When performing secure discovery,
378 * we should wait a certain period of time for getting response of each devices.
380 * @param[in] waittime Timeout in seconds.
381 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
382 * @return OC_STACK_OK on success otherwise error.
384 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
386 OCStackResult res = OC_STACK_OK;
388 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
389 while (OC_STACK_OK == res)
391 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
393 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
394 if (elapsed > waittime)
398 if (waitForStackResponse)
407 * Extract secure port information from payload of discovery response.
409 * @param[in] jsonStr response payload of /oic/res discovery.
411 * @return Secure port
413 uint16_t GetSecurePortFromJSON(char* jsonStr)
415 // TODO: Modify error handling
420 cJSON *jsonProp = NULL;
422 cJSON *jsonPort = NULL;
424 cJSON *jsonRoot = cJSON_Parse(jsonStr);
427 // TODO: Add error log & return default secure port
431 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
434 // TODO: Add error log & return default secure port
438 jsonP = cJSON_GetObjectItem(jsonProp, "p");
441 // TODO: Add error log & return default secure port
445 jsonPort = cJSON_GetObjectItem(jsonP, "port");
448 // TODO: Add error log & return default secure port
452 return (uint16_t)jsonPort->valueint;
455 bool PMGenerateQuery(bool isSecure,
456 const char* address, uint16_t port,
457 OCConnectivityType connType,
458 char* buffer, size_t bufferSize, const char* uri)
460 if(!address || !buffer || !uri)
462 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
467 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
469 switch(connType & CT_MASK_ADAPTER)
472 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
474 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
477 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
478 prefix, address, port, uri);
481 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
482 prefix, address, port, uri);
485 OIC_LOG(ERROR, TAG, "Unknown address format.");
488 // snprintf return value check
491 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
494 else if ((size_t)snRet >= bufferSize)
496 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
501 // TODO: We need to verify tinyDTLS in below cases
502 case CT_ADAPTER_GATT_BTLE:
503 case CT_ADAPTER_RFCOMM_BTEDR:
504 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
508 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
515 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
516 OCClientResponse *clientResponse)
520 OIC_LOG(ERROR, TAG, "Lost List of device information");
521 return OC_STACK_KEEP_TRANSACTION;
526 if (NULL == clientResponse->payload)
528 OIC_LOG(INFO, TAG, "Skiping Null payload");
529 return OC_STACK_KEEP_TRANSACTION;
531 if (OC_STACK_OK != clientResponse->result)
533 OIC_LOG(INFO, TAG, "Error in response");
534 return OC_STACK_KEEP_TRANSACTION;
538 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
540 OIC_LOG(INFO, TAG, "Unknown payload type");
541 return OC_STACK_KEEP_TRANSACTION;
544 OicSecVer_t *ptrVer = NULL;
545 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
546 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
548 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
549 if ((NULL == ptrVer) && (OC_STACK_OK != res))
551 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
552 return OC_STACK_KEEP_TRANSACTION;
556 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
558 //If this is owend device discovery we have to filter out the responses.
559 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
560 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
561 clientResponse->devAddr.port, ptrVer->secv);
562 if (OC_STACK_OK != res)
564 OIC_LOG(ERROR, TAG, "Error while getting security version.");
565 DeleteVerBinData(ptrVer);
566 return OC_STACK_KEEP_TRANSACTION;
569 OIC_LOG(INFO, TAG, "= Discovered security version =");
570 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
571 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
572 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
574 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
575 DeleteVerBinData(ptrVer);
581 OIC_LOG(INFO, TAG, "Skiping Null response");
582 return OC_STACK_KEEP_TRANSACTION;
585 return OC_STACK_DELETE_TRANSACTION;
588 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
589 OCClientResponse *clientResponse)
593 OIC_LOG(ERROR, TAG, "Lost List of device information");
594 return OC_STACK_DELETE_TRANSACTION;
599 if (NULL == clientResponse->payload)
601 OIC_LOG(INFO, TAG, "Skiping Null payload");
605 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
607 OIC_LOG(INFO, TAG, "Wrong payload type");
608 return OC_STACK_DELETE_TRANSACTION;
611 uint16_t securePort = 0;
612 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
614 // Use seure port of doxm for OTM and Provision.
617 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
619 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
620 OIC_LOG(INFO, TAG, "Found doxm resource.");
625 resPayload = resPayload->next;
628 if (NULL == resPayload)
630 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
631 return OC_STACK_DELETE_TRANSACTION;
633 if (resPayload && resPayload->secure)
635 securePort = resPayload->port;
639 OIC_LOG(INFO, TAG, "Can not find secure port information.");
640 return OC_STACK_DELETE_TRANSACTION;
643 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
645 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
646 OCProvisionDev_t *ptr = GetDevice(&pDInfo->pCandidateList,
647 clientResponse->devAddr.addr,
648 clientResponse->devAddr.port);
651 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery candidate device list");
652 return OC_STACK_DELETE_TRANSACTION;
655 OCStackResult res = UpdateSecurePortOfDevice(&pDInfo->pCandidateList,
656 clientResponse->devAddr.addr,
657 clientResponse->devAddr.port,
663 if (OC_STACK_OK != res)
665 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
666 return OC_STACK_DELETE_TRANSACTION;
669 res = MoveDeviceList(pDInfo->ppDevicesList, &pDInfo->pCandidateList, &clientResponse->devAddr);
670 if(OC_STACK_OK != res)
672 OIC_LOG(ERROR, TAG, "Error while move the discovered device to list.");
673 return OC_STACK_DELETE_TRANSACTION;
676 if(pDInfo->isSingleDiscovery)
678 pDInfo->isFound = true;
682 * Since security version discovery does not used anymore, disable security version discovery.
683 * Need to discussion to removing all version discovery related codes.
686 res = SecurityVersionDiscovery(pDInfo, clientResponse);
687 if(OC_STACK_OK != res)
689 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
690 return OC_STACK_DELETE_TRANSACTION;
694 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
697 return OC_STACK_DELETE_TRANSACTION;
701 OIC_LOG(INFO, TAG, "Skiping Null response");
704 return OC_STACK_DELETE_TRANSACTION;
707 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
708 OCClientResponse *clientResponse)
712 OIC_LOG(ERROR, TAG, "Lost List of device information");
713 return OC_STACK_KEEP_TRANSACTION;
718 if (NULL == clientResponse->payload)
720 OIC_LOG(INFO, TAG, "Skiping Null payload");
721 return OC_STACK_KEEP_TRANSACTION;
723 if (OC_STACK_OK != clientResponse->result)
725 OIC_LOG(INFO, TAG, "Error in response");
726 return OC_STACK_KEEP_TRANSACTION;
730 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
732 OIC_LOG(INFO, TAG, "Unknown payload type");
733 return OC_STACK_KEEP_TRANSACTION;
736 OicSecDoxm_t *ptrDoxm = NULL;
737 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
738 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
740 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
741 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
743 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
744 return OC_STACK_KEEP_TRANSACTION;
748 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
750 //If this is owend device discovery we have to filter out the responses.
751 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
752 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
754 // Get my device ID from doxm resource
756 memset(&myId, 0, sizeof(myId));
757 OCStackResult res = GetDoxmDevOwnerId(&myId);
758 if(OC_STACK_OK != res)
760 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
761 DeleteDoxmBinData(ptrDoxm);
762 return OC_STACK_KEEP_TRANSACTION;
765 // If this is owned discovery response but owner is not me then discard it.
766 if( (pDInfo->isOwnedDiscovery) &&
767 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
769 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
770 DeleteDoxmBinData(ptrDoxm);
771 return OC_STACK_KEEP_TRANSACTION;
774 res = GetDoxmDeviceID(&myId);
775 if(OC_STACK_OK != res)
777 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
778 DeleteDoxmBinData(ptrDoxm);
779 return OC_STACK_KEEP_TRANSACTION;
781 //if targetId and discovered deviceID are different, discard it
782 if ((pDInfo->isSingleDiscovery) &&
783 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
785 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
786 DeleteDoxmBinData(ptrDoxm);
787 return OC_STACK_KEEP_TRANSACTION;
789 //if this is owned discovery and this is PT's reply, discard it
790 if (((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
791 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
793 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
794 DeleteDoxmBinData(ptrDoxm);
795 return OC_STACK_KEEP_TRANSACTION;
798 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
799 clientResponse->connType, ptrDoxm);
800 if (OC_STACK_OK != res)
802 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
803 DeleteDoxmBinData(ptrDoxm);
804 return OC_STACK_KEEP_TRANSACTION;
807 res = SecurePortDiscovery(pDInfo, clientResponse);
808 if(OC_STACK_OK != res)
810 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
811 DeleteDoxmBinData(ptrDoxm);
812 return OC_STACK_KEEP_TRANSACTION;
815 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
818 return OC_STACK_KEEP_TRANSACTION;
823 OIC_LOG(INFO, TAG, "Skiping Null response");
824 return OC_STACK_KEEP_TRANSACTION;
827 return OC_STACK_DELETE_TRANSACTION;
830 static void DeviceDiscoveryDeleteHandler(void *ctx)
832 OIC_LOG(DEBUG, TAG, "IN DeviceDiscoveryDeleteHandler");
835 OIC_LOG(WARNING, TAG, "Not found context in DeviceDiscoveryDeleteHandler");
839 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
840 if (NULL != pDInfo->pCandidateList)
842 OCProvisionDev_t *pDev = NULL;
843 LL_FOREACH(pDInfo->pCandidateList, pDev)
845 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
846 pDev->endpoint.addr, pDev->endpoint.port);
847 OCCancel(pDev->handle,OC_HIGH_QOS,NULL,0);
849 PMDeleteDeviceList(pDInfo->pCandidateList);
851 OIC_LOG(DEBUG, TAG, "OUT DeviceDiscoveryDeleteHandler");
855 * Discover owned/unowned device in the specified endpoint/deviceID.
856 * It will return the found device even though timeout is not exceeded.
858 * @param[in] waittime Timeout in seconds
859 * @param[in] deviceID deviceID of target device.
860 * @param[out] ppFoundDevice OCProvisionDev_t of found device
862 * @return OC_STACK_OK on success otherwise error.\n
863 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
865 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
866 OCProvisionDev_t **ppFoundDevice)
868 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
870 if (NULL != *ppFoundDevice)
872 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
873 return OC_STACK_INVALID_PARAM;
876 if (NULL == deviceID)
878 OIC_LOG(ERROR, TAG, "Invalid device ID");
879 return OC_STACK_INVALID_PARAM;
883 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
886 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
887 return OC_STACK_NO_MEMORY;
890 pDInfo->ppDevicesList = ppFoundDevice;
891 pDInfo->pCandidateList = NULL;
892 pDInfo->isOwnedDiscovery = false;
893 pDInfo->isSingleDiscovery = true;
894 pDInfo->isFound = false;
895 pDInfo->targetId = deviceID;
897 OCCallbackData cbData;
898 cbData.cb = &DeviceDiscoveryHandler;
899 cbData.context = (void *)pDInfo;
900 cbData.cd = &DeviceDiscoveryDeleteHandler;
902 OCStackResult res = OC_STACK_ERROR;
904 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
905 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
907 OCDoHandle handle = NULL;
908 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
909 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
910 if (res != OC_STACK_OK)
912 OIC_LOG(ERROR, TAG, "OCStack resource error");
917 //Waiting for each response.
919 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
920 while (OC_STACK_OK == res && !pDInfo->isFound)
922 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
924 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
925 if (elapsed > waittime)
932 if(OC_STACK_OK != res)
934 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
936 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
937 if(OC_STACK_OK != resCancel)
939 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
944 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
945 if (OC_STACK_OK != res)
947 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
951 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
958 * Discover owned/unowned devices in the same IP subnet. .
960 * @param[in] waittime Timeout in seconds.
961 * @param[in] isOwned bool flag for owned / unowned discovery
962 * @param[in] ppDevicesList List of OCProvisionDev_t.
964 * @return OC_STACK_OK on success otherwise error.
966 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
968 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
970 if (NULL != *ppDevicesList)
972 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
973 return OC_STACK_INVALID_PARAM;
976 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
977 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
979 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
982 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
983 return OC_STACK_NO_MEMORY;
986 pDInfo->ppDevicesList = ppDevicesList;
987 pDInfo->pCandidateList = NULL;
988 pDInfo->isOwnedDiscovery = isOwned;
989 pDInfo->isSingleDiscovery = false;
990 pDInfo->targetId = NULL;
992 OCCallbackData cbData;
993 cbData.cb = &DeviceDiscoveryHandler;
994 cbData.context = (void *)pDInfo;
995 cbData.cd = &DeviceDiscoveryDeleteHandler;
996 OCStackResult res = OC_STACK_ERROR;
998 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
999 DOXM_OWNED_FALSE_MULTICAST_QUERY;
1001 OCDoHandle handle = NULL;
1002 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1003 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1004 if (res != OC_STACK_OK)
1006 OIC_LOG(ERROR, TAG, "OCStack resource error");
1011 //Waiting for each response.
1012 res = PMTimeout(waittime, true);
1013 if(OC_STACK_OK != res)
1015 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1017 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1018 if(OC_STACK_OK != resCancel)
1020 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1024 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1025 if (OC_STACK_OK != res)
1027 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1031 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
1036 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
1037 const OCClientResponse *clientResponse)
1039 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
1041 if(NULL == discoveryInfo || NULL == clientResponse)
1043 return OC_STACK_INVALID_PARAM;
1046 OCProvisionDev_t *pDev = GetDevice(&discoveryInfo->pCandidateList,
1047 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1050 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to get device");
1051 return OC_STACK_ERROR;
1054 //Try to the unicast discovery to getting secure port
1055 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1056 if(!PMGenerateQuery(false,
1057 pDev->endpoint.addr, pDev->endpoint.port,
1059 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
1061 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
1062 return OC_STACK_ERROR;
1064 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1066 OCCallbackData cbData;
1067 cbData.cb = &SecurePortDiscoveryHandler;
1068 cbData.context = (void*)discoveryInfo;
1070 OCStackResult ret = OCDoResource(&pDev->handle, OC_REST_DISCOVER, query, 0, 0,
1071 pDev->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1072 if(OC_STACK_OK != ret)
1074 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
1079 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1082 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1087 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1088 const OCClientResponse *clientResponse)
1090 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1092 if(NULL == discoveryInfo || NULL == clientResponse)
1094 return OC_STACK_INVALID_PARAM;
1097 //Try to the unicast discovery to getting security version
1098 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1099 if(!PMGenerateQuery(false,
1100 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1101 clientResponse->connType,
1102 query, sizeof(query), OIC_RSRC_VER_URI))
1104 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1105 return OC_STACK_ERROR;
1107 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1109 OCCallbackData cbData;
1110 cbData.cb = &SecurityVersionDiscoveryHandler;
1111 cbData.context = (void*)discoveryInfo;
1113 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1114 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1115 if(OC_STACK_OK != ret)
1117 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1122 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1125 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1131 * Function to print OCProvisionDev_t for debug purpose.
1133 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1136 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1140 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1141 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1142 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1143 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1144 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1148 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1152 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1154 if(*pUuidList == NULL || targetId == NULL)
1158 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1159 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1161 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1163 LL_DELETE(*pUuidList, tmp1);