1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * *****************************************************************/
20 #ifndef _POSIX_C_SOURCE
21 #define _POSIX_C_SOURCE 200112L
24 #include "iotivity_config.h"
33 #include "oic_malloc.h"
34 #include "oic_string.h"
39 #include "ocpayload.h"
41 #include "securevirtualresourcetypes.h"
42 #include "srmresourcestrings.h" //@note: SRM's internal header
43 #include "doxmresource.h" //@note: SRM's internal header
44 #include "pstatresource.h" //@note: SRM's internal header
45 #include "verresource.h" //@note: SRM's internal header
48 #include "pmutility.h"
50 #include "srmutility.h"
52 #define TAG ("PM-UTILITY")
54 typedef struct _DiscoveryInfo{
55 OCProvisionDev_t **ppDevicesList;
56 bool isOwnedDiscovery;
57 bool isSingleDiscovery;
59 const OicUuid_t *targetId;
63 * Function to discover secre port information through unicast
65 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
66 * @param[in] clientResponse Response information(It will contain payload)
68 * @return OC_STACK_OK on success otherwise error.
70 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
71 const OCClientResponse *clientResponse);
74 * Function to discover security version information through unicast
76 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
77 * @param[in] clientResponse Response information(It will contain payload)
79 * @return OC_STACK_OK on success otherwise error.
81 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
82 const OCClientResponse *clientResponse);
85 * Callback handler for PMDeviceDiscovery API.
87 * @param[in] ctx User context
88 * @param[in] handle Handler for response
89 * @param[in] clientResponse Response information (It will contain payload)
90 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
91 * OC_STACK_DELETE_TRANSACTION to delete it.
93 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
94 OCClientResponse *clientResponse);
97 * Callback handler for getting secure port information using /oic/res discovery.
99 * @param[in] ctx user context
100 * @param[in] handle Handle for response
101 * @param[in] clientResponse Response information(It will contain payload)
103 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
104 * OC_STACK_DELETE_TRANSACTION to delete it.
106 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
107 OCClientResponse *clientResponse);
110 * Callback handler for security version discovery.
112 * @param[in] ctx User context
113 * @param[in] handle Handler for response
114 * @param[in] clientResponse Response information (It will contain payload)
115 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
116 * OC_STACK_DELETE_TRANSACTION to delete it.
118 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
119 OCClientResponse *clientResponse);
122 * Function to search node in linked list that matches given IP and port.
124 * @param[in] pList List of OCProvisionDev_t.
125 * @param[in] addr address of target device.
126 * @param[in] port port of remote server.
128 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
130 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
132 if(NULL == addr || NULL == *ppDevicesList)
134 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
138 OCProvisionDev_t *ptr = NULL;
139 LL_FOREACH(*ppDevicesList, ptr)
141 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
152 * Add device information to list.
154 * @param[in] pList List of OCProvisionDev_t.
155 * @param[in] endpoint target device endpoint.
156 * @param[in] connType connectivity type of endpoint
157 * @param[in] doxm pointer to doxm instance.
159 * @return OC_STACK_OK for success and error code otherwise.
161 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, OCDevAddr* endpoint,
162 OCConnectivityType connType, OicSecDoxm_t *doxm)
164 if (NULL == endpoint)
166 return OC_STACK_INVALID_PARAM;
169 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, endpoint->addr, endpoint->port);
172 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
175 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
176 return OC_STACK_NO_MEMORY;
179 ptr->endpoint = *endpoint;
181 ptr->securePort = DEFAULT_SECURE_PORT;
183 ptr->connType = connType;
184 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
185 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
187 LL_PREPEND(*ppDevicesList, ptr);
194 * Function to set secure port information from the given list of devices.
196 * @param[in] pList List of OCProvisionDev_t.
197 * @param[in] addr address of target device.
198 * @param[in] port port of remote server.
199 * @param[in] secureport secure port information.
201 * @return OC_STACK_OK for success and errorcode otherwise.
203 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
204 uint16_t port, uint16_t securePort
210 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
214 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
215 return OC_STACK_ERROR;
218 ptr->securePort = securePort;
221 ptr->tcpPort = tcpPort;
228 * Function to set security version information from the given list of devices.
230 * @param[in] pList List of OCProvisionDev_t.
231 * @param[in] addr address of target device.
232 * @param[in] port port of remote server.
233 * @param[in] secVer security version information.
235 * @return OC_STACK_OK for success and errorcode otherwise.
237 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
238 uint16_t port, const char* secVer)
242 return OC_STACK_INVALID_PARAM;
245 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
249 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
250 return OC_STACK_ERROR;
253 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
259 * This function deletes list of provision target devices
261 * @param[in] pDevicesList List of OCProvisionDev_t.
263 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
267 OCProvisionDev_t *del = NULL, *tmp = NULL;
268 LL_FOREACH_SAFE(pDevicesList, del, tmp)
270 LL_DELETE(pDevicesList, del);
272 DeleteDoxmBinData(del->doxm);
273 DeletePstatBinData(del->pstat);
279 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
281 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
285 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
289 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
290 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
291 VERIFY_NON_NULL(TAG, newDev, ERROR);
293 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
297 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
298 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
300 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
301 // We have to assign NULL for not necessary information to prevent memory corruption.
302 newDev->pstat->sm = NULL;
307 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
308 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
310 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
311 // We have to assign NULL for not necessary information to prevent memory corruption.
312 newDev->doxm->oxmType = NULL;
313 newDev->doxm->oxm = NULL;
316 if (0 == strlen(src->secVer))
318 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
322 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
325 newDev->securePort = src->securePort;
326 newDev->devStatus = src->devStatus;
327 newDev->connType = src->connType;
330 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
335 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
338 OICFree(newDev->pstat);
339 OICFree(newDev->doxm);
346 * Timeout implementation for secure discovery. When performing secure discovery,
347 * we should wait a certain period of time for getting response of each devices.
349 * @param[in] waittime Timeout in seconds.
350 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
351 * @return OC_STACK_OK on success otherwise error.
353 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
355 OCStackResult res = OC_STACK_OK;
357 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
358 while (OC_STACK_OK == res)
360 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
362 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
363 if (elapsed > waittime)
367 if (waitForStackResponse)
376 * Extract secure port information from payload of discovery response.
378 * @param[in] jsonStr response payload of /oic/res discovery.
380 * @return Secure port
382 uint16_t GetSecurePortFromJSON(char* jsonStr)
384 // TODO: Modify error handling
389 cJSON *jsonProp = NULL;
391 cJSON *jsonPort = NULL;
393 cJSON *jsonRoot = cJSON_Parse(jsonStr);
396 // TODO: Add error log & return default secure port
400 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
403 // TODO: Add error log & return default secure port
407 jsonP = cJSON_GetObjectItem(jsonProp, "p");
410 // TODO: Add error log & return default secure port
414 jsonPort = cJSON_GetObjectItem(jsonP, "port");
417 // TODO: Add error log & return default secure port
421 return (uint16_t)jsonPort->valueint;
424 bool PMGenerateQuery(bool isSecure,
425 const char* address, uint16_t port,
426 OCConnectivityType connType,
427 char* buffer, size_t bufferSize, const char* uri)
429 if(!address || !buffer || !uri)
431 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
436 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
438 switch(connType & CT_MASK_ADAPTER)
441 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
443 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
446 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
447 prefix, address, port, uri);
450 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
451 prefix, address, port, uri);
454 OIC_LOG(ERROR, TAG, "Unknown address format.");
457 // snprintf return value check
460 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
463 else if ((size_t)snRet >= bufferSize)
465 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
470 // TODO: We need to verify tinyDTLS in below cases
471 case CT_ADAPTER_GATT_BTLE:
472 case CT_ADAPTER_RFCOMM_BTEDR:
473 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
477 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
484 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
485 OCClientResponse *clientResponse)
489 OIC_LOG(ERROR, TAG, "Lost List of device information");
490 return OC_STACK_KEEP_TRANSACTION;
495 if (NULL == clientResponse->payload)
497 OIC_LOG(INFO, TAG, "Skiping Null payload");
498 return OC_STACK_KEEP_TRANSACTION;
500 if (OC_STACK_OK != clientResponse->result)
502 OIC_LOG(INFO, TAG, "Error in response");
503 return OC_STACK_KEEP_TRANSACTION;
507 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
509 OIC_LOG(INFO, TAG, "Unknown payload type");
510 return OC_STACK_KEEP_TRANSACTION;
513 OicSecVer_t *ptrVer = NULL;
514 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
515 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
517 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
518 if ((NULL == ptrVer) && (OC_STACK_OK != res))
520 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
521 return OC_STACK_KEEP_TRANSACTION;
525 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
527 //If this is owend device discovery we have to filter out the responses.
528 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
529 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
530 clientResponse->devAddr.port, ptrVer->secv);
531 if (OC_STACK_OK != res)
533 OIC_LOG(ERROR, TAG, "Error while getting security version.");
534 DeleteVerBinData(ptrVer);
535 return OC_STACK_KEEP_TRANSACTION;
538 OIC_LOG(INFO, TAG, "= Discovered security version =");
539 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
540 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
541 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
543 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
544 DeleteVerBinData(ptrVer);
550 OIC_LOG(INFO, TAG, "Skiping Null response");
551 return OC_STACK_KEEP_TRANSACTION;
554 return OC_STACK_DELETE_TRANSACTION;
557 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
558 OCClientResponse *clientResponse)
562 OIC_LOG(ERROR, TAG, "Lost List of device information");
563 return OC_STACK_DELETE_TRANSACTION;
568 if (NULL == clientResponse->payload)
570 OIC_LOG(INFO, TAG, "Skiping Null payload");
574 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
576 OIC_LOG(INFO, TAG, "Wrong payload type");
577 return OC_STACK_DELETE_TRANSACTION;
580 uint16_t securePort = 0;
581 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
583 // Use seure port of doxm for OTM and Provision.
586 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
588 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
589 OIC_LOG(INFO, TAG, "Found doxm resource.");
594 resPayload = resPayload->next;
597 if (NULL == resPayload)
599 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
600 return OC_STACK_DELETE_TRANSACTION;
602 if (resPayload && resPayload->secure)
604 securePort = resPayload->port;
608 OIC_LOG(INFO, TAG, "Can not find secure port information.");
609 return OC_STACK_DELETE_TRANSACTION;
612 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
614 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
615 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
616 clientResponse->devAddr.addr,
617 clientResponse->devAddr.port,
623 if (OC_STACK_OK != res)
625 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
626 return OC_STACK_DELETE_TRANSACTION;
629 if(pDInfo->isSingleDiscovery)
631 pDInfo->isFound = true;
635 * Since security version discovery does not used anymore, disable security version discovery.
636 * Need to discussion to removing all version discovery related codes.
639 res = SecurityVersionDiscovery(pDInfo, clientResponse);
640 if(OC_STACK_OK != res)
642 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
643 return OC_STACK_DELETE_TRANSACTION;
647 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
650 return OC_STACK_DELETE_TRANSACTION;
654 OIC_LOG(INFO, TAG, "Skiping Null response");
657 return OC_STACK_DELETE_TRANSACTION;
660 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
661 OCClientResponse *clientResponse)
665 OIC_LOG(ERROR, TAG, "Lost List of device information");
666 return OC_STACK_KEEP_TRANSACTION;
671 if (NULL == clientResponse->payload)
673 OIC_LOG(INFO, TAG, "Skiping Null payload");
674 return OC_STACK_KEEP_TRANSACTION;
676 if (OC_STACK_OK != clientResponse->result)
678 OIC_LOG(INFO, TAG, "Error in response");
679 return OC_STACK_KEEP_TRANSACTION;
683 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
685 OIC_LOG(INFO, TAG, "Unknown payload type");
686 return OC_STACK_KEEP_TRANSACTION;
689 OicSecDoxm_t *ptrDoxm = NULL;
690 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
691 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
693 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
694 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
696 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
697 return OC_STACK_KEEP_TRANSACTION;
701 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
703 //If this is owend device discovery we have to filter out the responses.
704 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
705 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
707 // Get my device ID from doxm resource
709 memset(&myId, 0, sizeof(myId));
710 OCStackResult res = GetDoxmDevOwnerId(&myId);
711 if(OC_STACK_OK != res)
713 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
714 DeleteDoxmBinData(ptrDoxm);
715 return OC_STACK_KEEP_TRANSACTION;
718 // If this is owned discovery response but owner is not me then discard it.
719 if( (pDInfo->isOwnedDiscovery) &&
720 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
722 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
723 DeleteDoxmBinData(ptrDoxm);
724 return OC_STACK_KEEP_TRANSACTION;
727 res = GetDoxmDeviceID(&myId);
728 if(OC_STACK_OK != res)
730 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
731 DeleteDoxmBinData(ptrDoxm);
732 return OC_STACK_KEEP_TRANSACTION;
734 //if targetId and discovered deviceID are different, discard it
735 if ((pDInfo->isSingleDiscovery) &&
736 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
738 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
739 DeleteDoxmBinData(ptrDoxm);
740 return OC_STACK_KEEP_TRANSACTION;
742 //if this is owned discovery and this is PT's reply, discard it
743 if (((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
744 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
746 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
747 DeleteDoxmBinData(ptrDoxm);
748 return OC_STACK_KEEP_TRANSACTION;
751 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
752 clientResponse->connType, ptrDoxm);
753 if (OC_STACK_OK != res)
755 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
756 DeleteDoxmBinData(ptrDoxm);
757 return OC_STACK_KEEP_TRANSACTION;
760 res = SecurePortDiscovery(pDInfo, clientResponse);
761 if(OC_STACK_OK != res)
763 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
764 DeleteDoxmBinData(ptrDoxm);
765 return OC_STACK_KEEP_TRANSACTION;
768 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
771 return OC_STACK_KEEP_TRANSACTION;
776 OIC_LOG(INFO, TAG, "Skiping Null response");
777 return OC_STACK_KEEP_TRANSACTION;
780 return OC_STACK_DELETE_TRANSACTION;
785 * Discover owned/unowned device in the specified endpoint/deviceID.
786 * It will return the found device even though timeout is not exceeded.
788 * @param[in] waittime Timeout in seconds
789 * @param[in] deviceID deviceID of target device.
790 * @param[out] ppFoundDevice OCProvisionDev_t of found device
792 * @return OC_STACK_OK on success otherwise error.\n
793 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
795 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
796 OCProvisionDev_t **ppFoundDevice)
798 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
800 if (NULL != *ppFoundDevice)
802 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
803 return OC_STACK_INVALID_PARAM;
806 if (NULL == deviceID)
808 OIC_LOG(ERROR, TAG, "Invalid device ID");
809 return OC_STACK_INVALID_PARAM;
813 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
816 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
817 return OC_STACK_NO_MEMORY;
820 pDInfo->ppDevicesList = ppFoundDevice;
821 pDInfo->isOwnedDiscovery = false;
822 pDInfo->isSingleDiscovery = true;
823 pDInfo->isFound = false;
824 pDInfo->targetId = deviceID;
826 OCCallbackData cbData;
827 cbData.cb = &DeviceDiscoveryHandler;
828 cbData.context = (void *)pDInfo;
830 OCStackResult res = OC_STACK_ERROR;
832 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
833 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
835 OCDoHandle handle = NULL;
836 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
837 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
838 if (res != OC_STACK_OK)
840 OIC_LOG(ERROR, TAG, "OCStack resource error");
845 //Waiting for each response.
847 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
848 while (OC_STACK_OK == res && !pDInfo->isFound)
850 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
852 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
853 if (elapsed > waittime)
860 if(OC_STACK_OK != res)
862 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
864 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
865 if(OC_STACK_OK != resCancel)
867 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
872 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
873 if (OC_STACK_OK != res)
875 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
879 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
886 * Discover owned/unowned devices in the same IP subnet. .
888 * @param[in] waittime Timeout in seconds.
889 * @param[in] isOwned bool flag for owned / unowned discovery
890 * @param[in] ppDevicesList List of OCProvisionDev_t.
892 * @return OC_STACK_OK on success otherwise error.
894 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
896 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
898 if (NULL != *ppDevicesList)
900 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
901 return OC_STACK_INVALID_PARAM;
904 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
905 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
907 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
910 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
911 return OC_STACK_NO_MEMORY;
914 pDInfo->ppDevicesList = ppDevicesList;
915 pDInfo->isOwnedDiscovery = isOwned;
916 pDInfo->isSingleDiscovery = false;
917 pDInfo->targetId = NULL;
919 OCCallbackData cbData;
920 cbData.cb = &DeviceDiscoveryHandler;
921 cbData.context = (void *)pDInfo;
923 OCStackResult res = OC_STACK_ERROR;
925 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
926 DOXM_OWNED_FALSE_MULTICAST_QUERY;
928 OCDoHandle handle = NULL;
929 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
930 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
931 if (res != OC_STACK_OK)
933 OIC_LOG(ERROR, TAG, "OCStack resource error");
938 //Waiting for each response.
939 res = PMTimeout(waittime, true);
940 if(OC_STACK_OK != res)
942 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
944 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
945 if(OC_STACK_OK != resCancel)
947 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
951 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
952 if (OC_STACK_OK != res)
954 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
958 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
963 #ifdef _ENABLE_MULTIPLE_OWNER_
964 static OCStackApplicationResult MOTDeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
965 OCClientResponse *clientResponse)
969 OIC_LOG(ERROR, TAG, "Lost List of device information");
970 return OC_STACK_KEEP_TRANSACTION;
975 if (NULL == clientResponse->payload)
977 OIC_LOG(INFO, TAG, "Skipping Null payload");
978 return OC_STACK_KEEP_TRANSACTION;
980 if (OC_STACK_OK != clientResponse->result)
982 OIC_LOG(INFO, TAG, "Error in response");
983 return OC_STACK_KEEP_TRANSACTION;
987 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
989 OIC_LOG(INFO, TAG, "Unknown payload type");
990 return OC_STACK_KEEP_TRANSACTION;
993 OicSecDoxm_t *ptrDoxm = NULL;
994 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
995 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
997 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
998 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
1000 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
1001 return OC_STACK_KEEP_TRANSACTION;
1005 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
1007 //If this is owend device discovery we have to filter out the responses.
1008 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
1009 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
1011 // Get my device ID from doxm resource
1013 memset(&myId, 0, sizeof(myId));
1014 OCStackResult res = GetDoxmDevOwnerId(&myId);
1015 if(OC_STACK_OK != res)
1017 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
1018 DeleteDoxmBinData(ptrDoxm);
1019 return OC_STACK_KEEP_TRANSACTION;
1022 res = GetDoxmDeviceID(&myId);
1023 if(OC_STACK_OK != res)
1025 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
1026 DeleteDoxmBinData(ptrDoxm);
1027 return OC_STACK_KEEP_TRANSACTION;
1029 //if this is owned discovery and this is PT's reply, discard it
1030 if((pDInfo->isOwnedDiscovery) &&
1031 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
1033 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
1034 DeleteDoxmBinData(ptrDoxm);
1035 return OC_STACK_KEEP_TRANSACTION;
1038 if(pDInfo->isOwnedDiscovery)
1040 OicSecSubOwner_t* subOwner = NULL;
1041 LL_FOREACH(ptrDoxm->subOwners, subOwner)
1043 if(memcmp(myId.id, subOwner->uuid.id, sizeof(myId.id)) == 0)
1051 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1052 clientResponse->connType, ptrDoxm);
1053 if (OC_STACK_OK != res)
1055 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1056 DeleteDoxmBinData(ptrDoxm);
1057 return OC_STACK_KEEP_TRANSACTION;
1060 res = SecurePortDiscovery(pDInfo, clientResponse);
1061 if(OC_STACK_OK != res)
1063 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1064 DeleteDoxmBinData(ptrDoxm);
1065 return OC_STACK_KEEP_TRANSACTION;
1070 OIC_LOG(ERROR, TAG, "discarding device's reply, because not a SubOwner.");
1071 DeleteDoxmBinData(ptrDoxm);
1072 return OC_STACK_KEEP_TRANSACTION;
1077 if(ptrDoxm->mom && OIC_MULTIPLE_OWNER_DISABLE != ptrDoxm->mom->mode)
1079 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1080 clientResponse->connType, ptrDoxm);
1081 if (OC_STACK_OK != res)
1083 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1084 DeleteDoxmBinData(ptrDoxm);
1085 return OC_STACK_KEEP_TRANSACTION;
1088 res = SecurePortDiscovery(pDInfo, clientResponse);
1089 if(OC_STACK_OK != res)
1091 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1092 DeleteDoxmBinData(ptrDoxm);
1093 return OC_STACK_KEEP_TRANSACTION;
1098 OIC_LOG(ERROR, TAG, "discarding mom disabled device's reply");
1099 DeleteDoxmBinData(ptrDoxm);
1100 return OC_STACK_KEEP_TRANSACTION;
1104 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
1107 return OC_STACK_KEEP_TRANSACTION;
1112 OIC_LOG(INFO, TAG, "Skiping Null response");
1113 return OC_STACK_KEEP_TRANSACTION;
1116 return OC_STACK_DELETE_TRANSACTION;
1121 * Discover multiple OTM enabled devices in the same IP subnet.
1123 * @param[in] waittime Timeout in seconds.
1124 * @param[in] ppDevicesList List of OCProvisionDev_t.
1126 * @return OC_STACK_OK on success otherwise error.
1128 OCStackResult PMMultipleOwnerDeviceDiscovery(unsigned short waittime, bool isMultipleOwned, OCProvisionDev_t **ppDevicesList)
1130 OIC_LOG(DEBUG, TAG, "IN PMMultipleOwnerEnabledDeviceDiscovery");
1132 if (NULL != *ppDevicesList)
1134 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1135 return OC_STACK_INVALID_PARAM;
1138 const char *DOXM_MOM_ENABLE_MULTICAST_QUERY = "/oic/sec/doxm?mom!=0&owned=TRUE";
1139 const char *DOXM_MULTIPLE_OWNED_MULTICAST_QUERY = "/oic/sec/doxm?owned=TRUE";
1141 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
1144 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
1145 return OC_STACK_NO_MEMORY;
1148 pDInfo->ppDevicesList = ppDevicesList;
1149 pDInfo->isOwnedDiscovery = isMultipleOwned;
1151 OCCallbackData cbData;
1152 cbData.cb = &MOTDeviceDiscoveryHandler;
1153 cbData.context = (void *)pDInfo;
1155 OCStackResult res = OC_STACK_ERROR;
1157 const char* query = isMultipleOwned ? DOXM_MULTIPLE_OWNED_MULTICAST_QUERY :
1158 DOXM_MOM_ENABLE_MULTICAST_QUERY;
1160 OCDoHandle handle = NULL;
1161 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1162 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1163 if (res != OC_STACK_OK)
1165 OIC_LOG(ERROR, TAG, "OCStack resource error");
1170 //Waiting for each response.
1171 res = PMTimeout(waittime, true);
1172 if(OC_STACK_OK != res)
1174 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1176 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1177 if(OC_STACK_OK != resCancel)
1179 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1183 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1184 if (OC_STACK_OK != res)
1186 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1190 OIC_LOG(DEBUG, TAG, "OUT PMMultipleOwnerEnabledDeviceDiscovery");
1195 #endif //_ENABLE_MULTIPLE_OWNER_
1197 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
1198 const OCClientResponse *clientResponse)
1200 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
1202 if(NULL == discoveryInfo || NULL == clientResponse)
1204 return OC_STACK_INVALID_PARAM;
1206 //Try to the unicast discovery to getting secure port
1207 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1208 if(!PMGenerateQuery(false,
1209 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1210 clientResponse->connType,
1211 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
1213 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
1214 return OC_STACK_ERROR;
1216 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1218 OCCallbackData cbData;
1219 cbData.cb = &SecurePortDiscoveryHandler;
1220 cbData.context = (void*)discoveryInfo;
1222 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1223 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1224 if(OC_STACK_OK != ret)
1226 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
1231 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1234 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1239 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1240 const OCClientResponse *clientResponse)
1242 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1244 if(NULL == discoveryInfo || NULL == clientResponse)
1246 return OC_STACK_INVALID_PARAM;
1249 //Try to the unicast discovery to getting security version
1250 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1251 if(!PMGenerateQuery(false,
1252 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1253 clientResponse->connType,
1254 query, sizeof(query), OIC_RSRC_VER_URI))
1256 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1257 return OC_STACK_ERROR;
1259 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1261 OCCallbackData cbData;
1262 cbData.cb = &SecurityVersionDiscoveryHandler;
1263 cbData.context = (void*)discoveryInfo;
1265 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1266 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1267 if(OC_STACK_OK != ret)
1269 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1274 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1277 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1283 * Function to print OCProvisionDev_t for debug purpose.
1285 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1288 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1292 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1293 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1294 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1295 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1296 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1300 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1304 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1306 if(*pUuidList == NULL || targetId == NULL)
1310 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1311 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1313 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1315 LL_DELETE(*pUuidList, tmp1);