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"
38 #include "ocpayload.h"
40 #include "securevirtualresourcetypes.h"
41 #include "srmresourcestrings.h" //@note: SRM's internal header
42 #include "doxmresource.h" //@note: SRM's internal header
43 #include "pstatresource.h" //@note: SRM's internal header
44 #include "verresource.h" //@note: SRM's internal header
47 #include "pmutility.h"
49 #include "srmutility.h"
51 #define TAG ("OIC_PM_UTILITY")
53 typedef struct _DiscoveryInfo{
54 OCProvisionDev_t **ppDevicesList;
55 OCProvisionDev_t *pCandidateList;
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
188 LL_PREPEND(*ppDevicesList, ptr);
195 * Move device object between two device lists.
197 * @param[in] ppDstDevicesList Destination list of OCProvisionDev_t.
198 * @param[in] ppSrcDevicesList Source list of OCProvisionDev_t.
199 * @param[in] endpoint target device endpoint.
201 * @return OC_STACK_OK for success and error code otherwise.
203 OCStackResult MoveDeviceList(OCProvisionDev_t **ppDstDevicesList,
204 OCProvisionDev_t **ppSrcDevicesList, OCDevAddr* endpoint)
206 if (NULL == ppSrcDevicesList || NULL == endpoint)
208 return OC_STACK_INVALID_PARAM;
211 OCProvisionDev_t *ptr = GetDevice(ppSrcDevicesList, endpoint->addr, endpoint->port);
214 LL_DELETE(*ppSrcDevicesList, ptr);
215 LL_PREPEND(*ppDstDevicesList, ptr);
216 OIC_LOG_V(DEBUG, TAG, "MoveDeviceList success : %s(%d)", endpoint->addr, endpoint->port);
220 return OC_STACK_ERROR;
224 * Function to set secure port information from the given list of devices.
226 * @param[in] pList List of OCProvisionDev_t.
227 * @param[in] addr address of target device.
228 * @param[in] port port of remote server.
229 * @param[in] secureport secure port information.
231 * @return OC_STACK_OK for success and errorcode otherwise.
233 static OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
234 uint16_t port, uint16_t securePort
240 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
244 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
245 return OC_STACK_ERROR;
248 ptr->securePort = securePort;
251 ptr->tcpPort = tcpPort;
258 * Function to set security version information from the given list of devices.
260 * @param[in] pList List of OCProvisionDev_t.
261 * @param[in] addr address of target device.
262 * @param[in] port port of remote server.
263 * @param[in] secVer security version information.
265 * @return OC_STACK_OK for success and errorcode otherwise.
267 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
268 uint16_t port, const char* secVer)
272 return OC_STACK_INVALID_PARAM;
275 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
279 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
280 return OC_STACK_ERROR;
283 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
289 * This function deletes list of provision target devices
291 * @param[in] pDevicesList List of OCProvisionDev_t.
293 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
297 OCProvisionDev_t *del = NULL, *tmp = NULL;
298 LL_FOREACH_SAFE(pDevicesList, del, tmp)
300 LL_DELETE(pDevicesList, del);
302 DeleteDoxmBinData(del->doxm);
303 DeletePstatBinData(del->pstat);
309 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
311 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
315 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
319 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
320 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
321 VERIFY_NON_NULL(TAG, newDev, ERROR);
323 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
327 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
328 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
330 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
331 // We have to assign NULL for not necessary information to prevent memory corruption.
332 newDev->pstat->sm = NULL;
337 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
338 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
340 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
341 // We have to assign NULL for not necessary information to prevent memory corruption.
342 newDev->doxm->oxmType = NULL;
343 newDev->doxm->oxm = NULL;
346 if (0 == strlen(src->secVer))
348 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
352 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
355 newDev->securePort = src->securePort;
356 newDev->devStatus = src->devStatus;
357 newDev->connType = src->connType;
360 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
365 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
368 OICFree(newDev->pstat);
369 OICFree(newDev->doxm);
376 * Timeout implementation for secure discovery. When performing secure discovery,
377 * we should wait a certain period of time for getting response of each devices.
379 * @param[in] waittime Timeout in seconds.
380 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
381 * @return OC_STACK_OK on success otherwise error.
383 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
385 OCStackResult res = OC_STACK_OK;
387 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
388 while (OC_STACK_OK == res)
390 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
392 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
393 if (elapsed > waittime)
397 if (waitForStackResponse)
405 bool PMGenerateQuery(bool isSecure,
406 const char* address, uint16_t port,
407 OCConnectivityType connType,
408 char* buffer, size_t bufferSize, const char* uri)
410 if(!address || !buffer || !uri)
412 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
417 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
419 switch(connType & CT_MASK_ADAPTER)
422 prefix = (isSecure == true) ? COAPS_TCP_PREFIX : COAP_TCP_PREFIX;
424 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
427 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
428 prefix, address, port, uri);
432 char addressEncoded[128] = {0};
434 OCStackResult result = OCEncodeAddressForRFC6874(addressEncoded,
435 sizeof(addressEncoded),
437 if (OC_STACK_OK != result)
439 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : encoding error %d\n", result);
443 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
444 prefix, addressEncoded, port, uri);
448 OIC_LOG(ERROR, TAG, "Unknown address format.");
451 // snprintf return value check
454 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
457 else if ((size_t)snRet >= bufferSize)
459 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
464 // TODO: We need to verify tinyDTLS in below cases
465 case CT_ADAPTER_GATT_BTLE:
466 case CT_ADAPTER_RFCOMM_BTEDR:
467 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
471 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
478 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
479 OCClientResponse *clientResponse)
483 OIC_LOG(ERROR, TAG, "Lost List of device information");
484 return OC_STACK_KEEP_TRANSACTION;
489 if (NULL == clientResponse->payload)
491 OIC_LOG(INFO, TAG, "Skiping Null payload");
492 return OC_STACK_KEEP_TRANSACTION;
494 if (OC_STACK_OK != clientResponse->result)
496 OIC_LOG(INFO, TAG, "Error in response");
497 return OC_STACK_KEEP_TRANSACTION;
501 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
503 OIC_LOG(INFO, TAG, "Unknown payload type");
504 return OC_STACK_KEEP_TRANSACTION;
507 OicSecVer_t *ptrVer = NULL;
508 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
509 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
511 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
512 if ((NULL == ptrVer) && (OC_STACK_OK != res))
514 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
515 return OC_STACK_KEEP_TRANSACTION;
519 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
521 //If this is owend device discovery we have to filter out the responses.
522 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
523 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
524 clientResponse->devAddr.port, ptrVer->secv);
525 if (OC_STACK_OK != res)
527 OIC_LOG(ERROR, TAG, "Error while getting security version.");
528 DeleteVerBinData(ptrVer);
529 return OC_STACK_KEEP_TRANSACTION;
532 OIC_LOG(INFO, TAG, "= Discovered security version =");
533 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
534 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
535 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
537 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
538 DeleteVerBinData(ptrVer);
544 OIC_LOG(INFO, TAG, "Skiping Null response");
545 return OC_STACK_KEEP_TRANSACTION;
548 return OC_STACK_DELETE_TRANSACTION;
551 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
552 OCClientResponse *clientResponse)
556 OIC_LOG(ERROR, TAG, "Lost List of device information");
557 return OC_STACK_DELETE_TRANSACTION;
562 if (NULL == clientResponse->payload)
564 OIC_LOG(INFO, TAG, "Skiping Null payload");
568 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
570 OIC_LOG(INFO, TAG, "Wrong payload type");
571 return OC_STACK_DELETE_TRANSACTION;
574 uint16_t securePort = 0;
575 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
577 // Use seure port of doxm for OTM and Provision.
580 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI)))
582 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
583 OIC_LOG(INFO, TAG, "Found doxm resource.");
588 resPayload = resPayload->next;
591 if (NULL == resPayload)
593 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
594 return OC_STACK_DELETE_TRANSACTION;
596 if (resPayload && resPayload->secure)
598 securePort = resPayload->port;
602 OIC_LOG(INFO, TAG, "Can not find secure port information.");
603 return OC_STACK_DELETE_TRANSACTION;
606 OIC_LOG_V(DEBUG, TAG, "%s: TCP port from discovery = %d", __func__, resPayload->tcpPort);
608 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
609 OCProvisionDev_t *ptr = GetDevice(&pDInfo->pCandidateList,
610 clientResponse->devAddr.addr,
611 clientResponse->devAddr.port);
614 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery candidate device list");
615 return OC_STACK_DELETE_TRANSACTION;
618 OCStackResult res = UpdateSecurePortOfDevice(&pDInfo->pCandidateList,
619 clientResponse->devAddr.addr,
620 clientResponse->devAddr.port,
626 if (OC_STACK_OK != res)
628 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
629 return OC_STACK_DELETE_TRANSACTION;
632 res = MoveDeviceList(pDInfo->ppDevicesList, &pDInfo->pCandidateList, &clientResponse->devAddr);
633 if(OC_STACK_OK != res)
635 OIC_LOG(ERROR, TAG, "Error while move the discovered device to list.");
636 return OC_STACK_DELETE_TRANSACTION;
639 if(pDInfo->isSingleDiscovery)
641 pDInfo->isFound = true;
645 * Since security version discovery does not used anymore, disable security version discovery.
646 * Need to discussion to removing all version discovery related codes.
649 res = SecurityVersionDiscovery(pDInfo, clientResponse);
650 if(OC_STACK_OK != res)
652 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
653 return OC_STACK_DELETE_TRANSACTION;
657 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
660 return OC_STACK_DELETE_TRANSACTION;
664 OIC_LOG(INFO, TAG, "Skiping Null response");
667 return OC_STACK_DELETE_TRANSACTION;
670 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
671 OCClientResponse *clientResponse)
675 OIC_LOG(ERROR, TAG, "Lost List of device information");
676 return OC_STACK_KEEP_TRANSACTION;
681 if (NULL == clientResponse->payload)
683 OIC_LOG(INFO, TAG, "Skiping Null payload");
684 return OC_STACK_KEEP_TRANSACTION;
686 if (OC_STACK_OK != clientResponse->result)
688 OIC_LOG(INFO, TAG, "Error in response");
689 return OC_STACK_KEEP_TRANSACTION;
693 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
695 OIC_LOG(INFO, TAG, "Unknown payload type");
696 return OC_STACK_KEEP_TRANSACTION;
699 OicSecDoxm_t *ptrDoxm = NULL;
700 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
701 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
703 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
704 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
706 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
707 return OC_STACK_KEEP_TRANSACTION;
711 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
713 //If this is owend device discovery we have to filter out the responses.
714 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
715 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
717 // Get my device ID from doxm resource
719 memset(&myId, 0, sizeof(myId));
720 OCStackResult res = GetDoxmDevOwnerId(&myId);
721 if(OC_STACK_OK != res)
723 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
724 DeleteDoxmBinData(ptrDoxm);
725 return OC_STACK_KEEP_TRANSACTION;
728 // If this is owned discovery response but owner is not me then discard it.
729 if( (pDInfo->isOwnedDiscovery) &&
730 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
732 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
733 DeleteDoxmBinData(ptrDoxm);
734 return OC_STACK_KEEP_TRANSACTION;
737 res = GetDoxmDeviceID(&myId);
738 if(OC_STACK_OK != res)
740 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
741 DeleteDoxmBinData(ptrDoxm);
742 return OC_STACK_KEEP_TRANSACTION;
744 //if targetId and discovered deviceID are different, discard it
745 if ((pDInfo->isSingleDiscovery) &&
746 (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
748 OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
749 DeleteDoxmBinData(ptrDoxm);
750 return OC_STACK_KEEP_TRANSACTION;
752 //if this is owned discovery and this is PT's reply, discard it
753 if (((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
754 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
756 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
757 DeleteDoxmBinData(ptrDoxm);
758 return OC_STACK_KEEP_TRANSACTION;
761 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
762 clientResponse->connType, ptrDoxm);
763 if (OC_STACK_OK != res)
765 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
766 DeleteDoxmBinData(ptrDoxm);
767 return OC_STACK_KEEP_TRANSACTION;
770 res = SecurePortDiscovery(pDInfo, clientResponse);
771 if(OC_STACK_OK != res)
773 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
774 DeleteDoxmBinData(ptrDoxm);
775 return OC_STACK_KEEP_TRANSACTION;
778 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
781 return OC_STACK_KEEP_TRANSACTION;
786 OIC_LOG(INFO, TAG, "Skiping Null response");
787 return OC_STACK_KEEP_TRANSACTION;
790 return OC_STACK_DELETE_TRANSACTION;
793 static void DeviceDiscoveryDeleteHandler(void *ctx)
795 OIC_LOG(DEBUG, TAG, "IN DeviceDiscoveryDeleteHandler");
798 OIC_LOG(WARNING, TAG, "Not found context in DeviceDiscoveryDeleteHandler");
802 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
803 if (NULL != pDInfo->pCandidateList)
805 OCProvisionDev_t *pDev = NULL;
806 LL_FOREACH(pDInfo->pCandidateList, pDev)
808 OIC_LOG_V(DEBUG, TAG, "OCCancel - %s : %d",
809 pDev->endpoint.addr, pDev->endpoint.port);
810 if(OC_STACK_OK != OCCancel(pDev->handle,OC_HIGH_QOS,NULL,0))
812 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
815 PMDeleteDeviceList(pDInfo->pCandidateList);
817 OIC_LOG(DEBUG, TAG, "OUT DeviceDiscoveryDeleteHandler");
821 * Discover owned/unowned device in the specified endpoint/deviceID.
822 * It will return the found device even though timeout is not exceeded.
824 * @param[in] waittime Timeout in seconds
825 * @param[in] deviceID deviceID of target device.
826 * @param[out] ppFoundDevice OCProvisionDev_t of found device
828 * @return OC_STACK_OK on success otherwise error.\n
829 * OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
831 OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
832 OCProvisionDev_t **ppFoundDevice)
834 OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
836 if (NULL != *ppFoundDevice)
838 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
839 return OC_STACK_INVALID_PARAM;
842 if (NULL == deviceID)
844 OIC_LOG(ERROR, TAG, "Invalid device ID");
845 return OC_STACK_INVALID_PARAM;
849 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
852 OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
853 return OC_STACK_NO_MEMORY;
856 pDInfo->ppDevicesList = ppFoundDevice;
857 pDInfo->pCandidateList = NULL;
858 pDInfo->isOwnedDiscovery = false;
859 pDInfo->isSingleDiscovery = true;
860 pDInfo->isFound = false;
861 pDInfo->targetId = deviceID;
863 OCCallbackData cbData;
864 cbData.cb = &DeviceDiscoveryHandler;
865 cbData.context = (void *)pDInfo;
866 cbData.cd = &DeviceDiscoveryDeleteHandler;
868 OCStackResult res = OC_STACK_ERROR;
870 char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
871 snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
873 OCDoHandle handle = NULL;
874 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
875 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
876 if (res != OC_STACK_OK)
878 OIC_LOG(ERROR, TAG, "OCStack resource error");
883 //Waiting for each response.
885 uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
886 while (OC_STACK_OK == res && !pDInfo->isFound)
888 uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
890 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
891 if (elapsed > waittime)
898 if(OC_STACK_OK != res)
900 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
902 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
903 if(OC_STACK_OK != resCancel)
905 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
910 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
911 if (OC_STACK_OK != res)
913 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
917 OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
924 * Discover owned/unowned devices in the same IP subnet. .
926 * @param[in] waittime Timeout in seconds.
927 * @param[in] isOwned bool flag for owned / unowned discovery
928 * @param[in] ppDevicesList List of OCProvisionDev_t.
930 * @return OC_STACK_OK on success otherwise error.
932 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
934 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
936 if (NULL != *ppDevicesList)
938 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
939 return OC_STACK_INVALID_PARAM;
942 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
943 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
945 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
948 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
949 return OC_STACK_NO_MEMORY;
952 pDInfo->ppDevicesList = ppDevicesList;
953 pDInfo->pCandidateList = NULL;
954 pDInfo->isOwnedDiscovery = isOwned;
955 pDInfo->isSingleDiscovery = false;
956 pDInfo->targetId = NULL;
958 OCCallbackData cbData;
959 cbData.cb = &DeviceDiscoveryHandler;
960 cbData.context = (void *)pDInfo;
961 cbData.cd = &DeviceDiscoveryDeleteHandler;
962 OCStackResult res = OC_STACK_ERROR;
964 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
965 DOXM_OWNED_FALSE_MULTICAST_QUERY;
967 OCDoHandle handle = NULL;
968 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
969 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
970 if (res != OC_STACK_OK)
972 OIC_LOG(ERROR, TAG, "OCStack resource error");
977 //Waiting for each response.
978 res = PMTimeout(waittime, true);
979 if(OC_STACK_OK != res)
981 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
983 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
984 if(OC_STACK_OK != resCancel)
986 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
990 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
991 if (OC_STACK_OK != res)
993 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
997 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
1002 #ifdef MULTIPLE_OWNER
1003 static OCStackApplicationResult MOTDeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
1004 OCClientResponse *clientResponse)
1008 OIC_LOG(ERROR, TAG, "Lost List of device information");
1009 return OC_STACK_KEEP_TRANSACTION;
1014 if (NULL == clientResponse->payload)
1016 OIC_LOG(INFO, TAG, "Skipping Null payload");
1017 return OC_STACK_KEEP_TRANSACTION;
1019 if (OC_STACK_OK != clientResponse->result)
1021 OIC_LOG(INFO, TAG, "Error in response");
1022 return OC_STACK_KEEP_TRANSACTION;
1026 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
1028 OIC_LOG(INFO, TAG, "Unknown payload type");
1029 return OC_STACK_KEEP_TRANSACTION;
1032 OicSecDoxm_t *ptrDoxm = NULL;
1033 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
1034 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
1036 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
1037 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
1039 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
1040 return OC_STACK_KEEP_TRANSACTION;
1044 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
1046 //If this is owend device discovery we have to filter out the responses.
1047 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
1048 OCProvisionDev_t **ppDevicesList = &pDInfo->pCandidateList;
1050 // Get my device ID from doxm resource
1052 memset(&myId, 0, sizeof(myId));
1053 OCStackResult res = GetDoxmDevOwnerId(&myId);
1054 if(OC_STACK_OK != res)
1056 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
1057 DeleteDoxmBinData(ptrDoxm);
1058 return OC_STACK_KEEP_TRANSACTION;
1061 res = GetDoxmDeviceID(&myId);
1062 if(OC_STACK_OK != res)
1064 OIC_LOG(ERROR, TAG, "Error while getting my UUID.");
1065 DeleteDoxmBinData(ptrDoxm);
1066 return OC_STACK_KEEP_TRANSACTION;
1068 //if this is owned discovery and this is PT's reply, discard it
1069 if((pDInfo->isOwnedDiscovery) &&
1070 (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
1072 OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
1073 DeleteDoxmBinData(ptrDoxm);
1074 return OC_STACK_KEEP_TRANSACTION;
1077 if(pDInfo->isOwnedDiscovery)
1079 OicSecSubOwner_t* subOwner = NULL;
1080 LL_FOREACH(ptrDoxm->subOwners, subOwner)
1082 if(memcmp(myId.id, subOwner->uuid.id, sizeof(myId.id)) == 0)
1090 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1091 clientResponse->connType, ptrDoxm);
1092 if (OC_STACK_OK != res)
1094 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1095 DeleteDoxmBinData(ptrDoxm);
1096 return OC_STACK_KEEP_TRANSACTION;
1099 res = SecurePortDiscovery(pDInfo, clientResponse);
1100 if(OC_STACK_OK != res)
1102 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1103 DeleteDoxmBinData(ptrDoxm);
1104 return OC_STACK_KEEP_TRANSACTION;
1109 OIC_LOG(ERROR, TAG, "discarding device's reply, because not a SubOwner.");
1110 DeleteDoxmBinData(ptrDoxm);
1111 return OC_STACK_KEEP_TRANSACTION;
1116 if(ptrDoxm->mom && OIC_MULTIPLE_OWNER_DISABLE != ptrDoxm->mom->mode)
1118 res = AddDevice(ppDevicesList, &clientResponse->devAddr,
1119 clientResponse->connType, ptrDoxm);
1120 if (OC_STACK_OK != res)
1122 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
1123 DeleteDoxmBinData(ptrDoxm);
1124 return OC_STACK_KEEP_TRANSACTION;
1127 res = SecurePortDiscovery(pDInfo, clientResponse);
1128 if(OC_STACK_OK != res)
1130 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
1131 DeleteDoxmBinData(ptrDoxm);
1132 return OC_STACK_KEEP_TRANSACTION;
1137 OIC_LOG(ERROR, TAG, "discarding mom disabled device's reply");
1138 DeleteDoxmBinData(ptrDoxm);
1139 return OC_STACK_KEEP_TRANSACTION;
1143 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
1146 return OC_STACK_KEEP_TRANSACTION;
1151 OIC_LOG(INFO, TAG, "Skiping Null response");
1152 return OC_STACK_KEEP_TRANSACTION;
1155 return OC_STACK_DELETE_TRANSACTION;
1160 * Discover multiple OTM enabled devices in the same IP subnet.
1162 * @param[in] waittime Timeout in seconds.
1163 * @param[in] ppDevicesList List of OCProvisionDev_t.
1165 * @return OC_STACK_OK on success otherwise error.
1167 OCStackResult PMMultipleOwnerDeviceDiscovery(unsigned short waittime, bool isMultipleOwned, OCProvisionDev_t **ppDevicesList)
1169 OIC_LOG(DEBUG, TAG, "IN PMMultipleOwnerEnabledDeviceDiscovery");
1171 if (NULL != *ppDevicesList)
1173 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
1174 return OC_STACK_INVALID_PARAM;
1177 const char *DOXM_MOM_ENABLE_MULTICAST_QUERY = "/oic/sec/doxm?mom!=0&owned=TRUE";
1178 const char *DOXM_MULTIPLE_OWNED_MULTICAST_QUERY = "/oic/sec/doxm?owned=TRUE";
1180 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
1183 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
1184 return OC_STACK_NO_MEMORY;
1187 pDInfo->ppDevicesList = ppDevicesList;
1188 pDInfo->pCandidateList = NULL;
1189 pDInfo->isOwnedDiscovery = isMultipleOwned;
1191 OCCallbackData cbData;
1192 cbData.cb = &MOTDeviceDiscoveryHandler;
1193 cbData.context = (void *)pDInfo;
1195 OCStackResult res = OC_STACK_ERROR;
1197 const char* query = isMultipleOwned ? DOXM_MULTIPLE_OWNED_MULTICAST_QUERY :
1198 DOXM_MOM_ENABLE_MULTICAST_QUERY;
1200 OCDoHandle handle = NULL;
1201 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
1202 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
1203 if (res != OC_STACK_OK)
1205 OIC_LOG(ERROR, TAG, "OCStack resource error");
1210 //Waiting for each response.
1211 res = PMTimeout(waittime, true);
1212 if(OC_STACK_OK != res)
1214 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
1216 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
1217 if(OC_STACK_OK != resCancel)
1219 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1223 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
1224 if (OC_STACK_OK != res)
1226 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
1230 OIC_LOG(DEBUG, TAG, "OUT PMMultipleOwnerEnabledDeviceDiscovery");
1235 #endif //MULTIPLE_OWNER
1237 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
1238 const OCClientResponse *clientResponse)
1240 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
1242 if(NULL == discoveryInfo || NULL == clientResponse)
1244 return OC_STACK_INVALID_PARAM;
1247 OCProvisionDev_t *pDev = GetDevice(&discoveryInfo->pCandidateList,
1248 clientResponse->devAddr.addr, clientResponse->devAddr.port);
1251 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to get device");
1252 return OC_STACK_ERROR;
1255 //Try to the unicast discovery to getting secure port
1256 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1257 if(!PMGenerateQuery(false,
1258 pDev->endpoint.addr, pDev->endpoint.port,
1260 query, sizeof(query), OC_RSRVD_WELL_KNOWN_URI))
1262 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
1263 return OC_STACK_ERROR;
1265 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1267 // Set filter query with rt=oic.r.doxm
1268 const char RES_DOXM_QUERY_FMT[] = "%s?%s=%s";
1269 char uri[MAX_URI_LENGTH + MAX_QUERY_LENGTH] = {0};
1270 snprintf(uri, sizeof(uri), RES_DOXM_QUERY_FMT, query,
1271 OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
1273 OIC_LOG_V(DEBUG, TAG, "URI=%s", uri);
1275 OCCallbackData cbData;
1276 cbData.cb = &SecurePortDiscoveryHandler;
1277 cbData.context = (void*)discoveryInfo;
1279 OCStackResult ret = OCDoResource(&pDev->handle, OC_REST_DISCOVER, uri, 0, 0,
1280 pDev->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1281 if(OC_STACK_OK != ret)
1283 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
1288 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1291 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
1296 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
1297 const OCClientResponse *clientResponse)
1299 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
1301 if(NULL == discoveryInfo || NULL == clientResponse)
1303 return OC_STACK_INVALID_PARAM;
1306 //Try to the unicast discovery to getting security version
1307 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
1308 if(!PMGenerateQuery(false,
1309 clientResponse->devAddr.addr, clientResponse->devAddr.port,
1310 clientResponse->connType,
1311 query, sizeof(query), OIC_RSRC_VER_URI))
1313 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
1314 return OC_STACK_ERROR;
1316 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
1318 OCCallbackData cbData;
1319 cbData.cb = &SecurityVersionDiscoveryHandler;
1320 cbData.context = (void*)discoveryInfo;
1322 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
1323 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
1324 if(OC_STACK_OK != ret)
1326 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
1331 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
1334 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
1340 * Function to print OCProvisionDev_t for debug purpose.
1342 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
1345 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
1349 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
1350 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
1351 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
1352 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
1353 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
1357 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
1361 bool PMDeleteFromUUIDList(OCUuidList_t **pUuidList, OicUuid_t *targetId)
1363 if(*pUuidList == NULL || targetId == NULL)
1367 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
1368 LL_FOREACH_SAFE(*pUuidList, tmp1, tmp2)
1370 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
1372 LL_DELETE(*pUuidList, tmp1);