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
36 #ifdef HAVE_SYS_TIME_H
41 #include "oic_malloc.h"
42 #include "oic_string.h"
46 #include "ocpayload.h"
48 #include "securevirtualresourcetypes.h"
49 #include "srmresourcestrings.h" //@note: SRM's internal header
50 #include "doxmresource.h" //@note: SRM's internal header
51 #include "pstatresource.h" //@note: SRM's internal header
52 #include "verresource.h" //@note: SRM's internal header
55 #include "pmutility.h"
57 #include "srmutility.h"
59 #define TAG ("PM-UTILITY")
61 typedef struct _DiscoveryInfo{
62 OCProvisionDev_t **ppDevicesList;
63 bool isOwnedDiscovery;
67 * Function to discover secre port information through unicast
69 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
70 * @param[in] clientResponse Response information(It will contain payload)
72 * @return OC_STACK_OK on success otherwise error.
74 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
75 const OCClientResponse *clientResponse);
78 * Function to discover security version information through unicast
80 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
81 * @param[in] clientResponse Response information(It will contain payload)
83 * @return OC_STACK_OK on success otherwise error.
85 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
86 const OCClientResponse *clientResponse);
89 * Callback handler for PMDeviceDiscovery API.
91 * @param[in] ctx User context
92 * @param[in] handle Handler for response
93 * @param[in] clientResponse Response information (It will contain payload)
94 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
95 * OC_STACK_DELETE_TRANSACTION to delete it.
97 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
98 OCClientResponse *clientResponse);
101 * Callback handler for getting secure port information using /oic/res discovery.
103 * @param[in] ctx user context
104 * @param[in] handle Handle for response
105 * @param[in] clientResponse Response information(It will contain payload)
107 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
108 * OC_STACK_DELETE_TRANSACTION to delete it.
110 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
111 OCClientResponse *clientResponse);
114 * Callback handler for security version discovery.
116 * @param[in] ctx User context
117 * @param[in] handle Handler for response
118 * @param[in] clientResponse Response information (It will contain payload)
119 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
120 * OC_STACK_DELETE_TRANSACTION to delete it.
122 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
123 OCClientResponse *clientResponse);
126 * Function to search node in linked list that matches given IP and port.
128 * @param[in] pList List of OCProvisionDev_t.
129 * @param[in] addr address of target device.
130 * @param[in] port port of remote server.
132 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
134 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
136 if(NULL == addr || NULL == *ppDevicesList)
138 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
142 OCProvisionDev_t *ptr = NULL;
143 LL_FOREACH(*ppDevicesList, ptr)
145 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
156 * Add device information to list.
158 * @param[in] pList List of OCProvisionDev_t.
159 * @param[in] addr address of target device.
160 * @param[in] port port of remote server.
161 * @param[in] adapter adapter type of endpoint.
162 * @param[in] doxm pointer to doxm instance.
163 * @param[in] connType connectivity type of endpoint
165 * @return OC_STACK_OK for success and errorcode otherwise.
167 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port,
168 OCTransportAdapter adapter, OCConnectivityType connType, OicSecDoxm_t *doxm)
172 return OC_STACK_INVALID_PARAM;
175 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
178 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
181 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
182 return OC_STACK_NO_MEMORY;
185 OICStrcpy(ptr->endpoint.addr, MAX_ADDR_STR_SIZE, addr);
186 ptr->endpoint.port = port;
188 ptr->securePort = DEFAULT_SECURE_PORT;
189 ptr->endpoint.adapter = adapter;
191 ptr->connType = connType;
192 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
193 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION); // version initialization
195 LL_PREPEND(*ppDevicesList, ptr);
202 * Function to set secure port information from the given list of devices.
204 * @param[in] pList List of OCProvisionDev_t.
205 * @param[in] addr address of target device.
206 * @param[in] port port of remote server.
207 * @param[in] secureport secure port information.
209 * @return OC_STACK_OK for success and errorcode otherwise.
211 OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
212 uint16_t port, uint16_t securePort)
214 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
218 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
219 return OC_STACK_ERROR;
222 ptr->securePort = securePort;
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;
356 #if defined(HAVE_GETSYSTEMTIMEASFILETIME)
357 FILETIME startTime = {0};
358 FILETIME currTime = {0};
360 GetSystemTimeAsFileTime(&startTime);
361 #elif defined(HAVE_CLOCK_GETTIME)
362 struct timespec startTime = {.tv_sec=0, .tv_nsec=0};
363 struct timespec currTime = {.tv_sec=0, .tv_nsec=0};
365 # if defined(_POSIX_MONOTONIC_CLOCK)
366 int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
368 int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
369 # endif // defined(_POSIX_MONOTONIC_CLOCK)
372 return OC_STACK_ERROR;
376 ERROR Need PMTimeout implementation
377 return OC_STACK_ERROR;
380 while (OC_STACK_OK == res)
382 #if defined(HAVE_GETSYSTEMTIMEASFILETIME)
383 GetSystemTimeAsFileTime(&currTime);
384 #elif defined(HAVE_CLOCK_GETTIME)
386 # if defined(_POSIX_MONOTONIC_CLOCK)
387 clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
389 clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
393 return OC_STACK_TIMEOUT;
396 ERROR Need PMTimeout implementation
399 #if defined(HAVE_GETSYSTEMTIMEASFILETIME)
400 #define HNS_TO_S(VAL) ((VAL)/(10*1000*1000))
401 ULARGE_INTEGER currTimeInt;
402 ULARGE_INTEGER startTimeInt;
404 currTimeInt.LowPart = currTime.dwLowDateTime;
405 currTimeInt.HighPart = currTime.dwHighDateTime;
407 startTimeInt.LowPart = startTime.dwLowDateTime;
408 startTimeInt.HighPart = startTime.dwHighDateTime;
410 long elapsed = (long)HNS_TO_S(currTimeInt.QuadPart - startTimeInt.QuadPart);
411 #elif defined(HAVE_CLOCK_GETTIME)
412 long elapsed = (currTime.tv_sec - startTime.tv_sec);
414 ERROR Need PMTimeout implementation
416 if (elapsed > waittime)
420 if (waitForStackResponse)
429 * Extract secure port information from payload of discovery response.
431 * @param[in] jsonStr response payload of /oic/res discovery.
433 * @return Secure port
435 uint16_t GetSecurePortFromJSON(char* jsonStr)
437 // TODO: Modify error handling
442 cJSON *jsonProp = NULL;
444 cJSON *jsonPort = NULL;
446 cJSON *jsonRoot = cJSON_Parse(jsonStr);
449 // TODO: Add error log & return default secure port
453 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
456 // TODO: Add error log & return default secure port
460 jsonP = cJSON_GetObjectItem(jsonProp, "p");
463 // TODO: Add error log & return default secure port
467 jsonPort = cJSON_GetObjectItem(jsonP, "port");
470 // TODO: Add error log & return default secure port
474 return (uint16_t)jsonPort->valueint;
477 bool PMGenerateQuery(bool isSecure,
478 const char* address, uint16_t port,
479 OCConnectivityType connType,
480 char* buffer, size_t bufferSize, const char* uri)
482 if(!address || !buffer || !uri)
484 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
489 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
491 switch(connType & CT_MASK_ADAPTER)
494 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
497 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
498 prefix, address, port, uri);
501 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
502 prefix, address, port, uri);
505 OIC_LOG(ERROR, TAG, "Unknown address format.");
508 // snprintf return value check
511 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
514 else if ((size_t)snRet >= bufferSize)
516 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
521 // TODO: We need to verify tinyDTLS in below cases
522 case CT_ADAPTER_GATT_BTLE:
523 case CT_ADAPTER_RFCOMM_BTEDR:
524 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
528 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
535 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
536 OCClientResponse *clientResponse)
540 OIC_LOG(ERROR, TAG, "Lost List of device information");
541 return OC_STACK_KEEP_TRANSACTION;
546 if (NULL == clientResponse->payload)
548 OIC_LOG(INFO, TAG, "Skiping Null payload");
549 return OC_STACK_KEEP_TRANSACTION;
551 if (OC_STACK_OK != clientResponse->result)
553 OIC_LOG(INFO, TAG, "Error in response");
554 return OC_STACK_KEEP_TRANSACTION;
558 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
560 OIC_LOG(INFO, TAG, "Unknown payload type");
561 return OC_STACK_KEEP_TRANSACTION;
564 OicSecVer_t *ptrVer = NULL;
565 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
566 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
568 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
569 if ((NULL == ptrVer) && (OC_STACK_OK != res))
571 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
572 return OC_STACK_KEEP_TRANSACTION;
576 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
578 //If this is owend device discovery we have to filter out the responses.
579 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
580 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
581 clientResponse->devAddr.port, ptrVer->secv);
582 if (OC_STACK_OK != res)
584 OIC_LOG(ERROR, TAG, "Error while getting security version.");
585 DeleteVerBinData(ptrVer);
586 return OC_STACK_KEEP_TRANSACTION;
589 OIC_LOG(INFO, TAG, "= Discovered security version =");
590 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
591 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
592 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
594 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
595 DeleteVerBinData(ptrVer);
601 OIC_LOG(INFO, TAG, "Skiping Null response");
602 return OC_STACK_KEEP_TRANSACTION;
605 return OC_STACK_DELETE_TRANSACTION;
608 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
609 OCClientResponse *clientResponse)
613 OIC_LOG(ERROR, TAG, "Lost List of device information");
614 return OC_STACK_DELETE_TRANSACTION;
619 if (NULL == clientResponse->payload)
621 OIC_LOG(INFO, TAG, "Skiping Null payload");
625 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
627 OIC_LOG(INFO, TAG, "Wrong payload type");
628 return OC_STACK_DELETE_TRANSACTION;
631 uint16_t securePort = 0;
632 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
634 if (resPayload && resPayload->secure)
636 securePort = resPayload->port;
640 OIC_LOG(INFO, TAG, "Can not find secure port information.");
641 return OC_STACK_DELETE_TRANSACTION;
644 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
645 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
646 clientResponse->devAddr.addr,
647 clientResponse->devAddr.port, securePort);
648 if (OC_STACK_OK != res)
650 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
651 return OC_STACK_DELETE_TRANSACTION;
654 res = SecurityVersionDiscovery(pDInfo, clientResponse);
655 if(OC_STACK_OK != res)
657 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
658 return OC_STACK_DELETE_TRANSACTION;
661 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
664 return OC_STACK_DELETE_TRANSACTION;
668 OIC_LOG(INFO, TAG, "Skiping Null response");
671 return OC_STACK_DELETE_TRANSACTION;
674 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
675 OCClientResponse *clientResponse)
679 OIC_LOG(ERROR, TAG, "Lost List of device information");
680 return OC_STACK_KEEP_TRANSACTION;
685 if (NULL == clientResponse->payload)
687 OIC_LOG(INFO, TAG, "Skiping Null payload");
688 return OC_STACK_KEEP_TRANSACTION;
690 if (OC_STACK_OK != clientResponse->result)
692 OIC_LOG(INFO, TAG, "Error in response");
693 return OC_STACK_KEEP_TRANSACTION;
697 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
699 OIC_LOG(INFO, TAG, "Unknown payload type");
700 return OC_STACK_KEEP_TRANSACTION;
703 OicSecDoxm_t *ptrDoxm = NULL;
704 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
705 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
707 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
708 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
710 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
711 return OC_STACK_KEEP_TRANSACTION;
715 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
717 //If this is owend device discovery we have to filter out the responses.
718 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
719 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
721 // Get my device ID from doxm resource
723 memset(&myId, 0, sizeof(myId));
724 OCStackResult res = GetDoxmDevOwnerId(&myId);
725 if(OC_STACK_OK != res)
727 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
728 DeleteDoxmBinData(ptrDoxm);
729 return OC_STACK_KEEP_TRANSACTION;
732 // If this is owned discovery response but owner is not me then discard it.
733 if( (pDInfo->isOwnedDiscovery) &&
734 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
736 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
737 DeleteDoxmBinData(ptrDoxm);
738 return OC_STACK_KEEP_TRANSACTION;
741 res = AddDevice(ppDevicesList, clientResponse->devAddr.addr,
742 clientResponse->devAddr.port,
743 clientResponse->devAddr.adapter,
744 clientResponse->connType, ptrDoxm);
745 if (OC_STACK_OK != res)
747 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
748 DeleteDoxmBinData(ptrDoxm);
749 return OC_STACK_KEEP_TRANSACTION;
752 res = SecurePortDiscovery(pDInfo, clientResponse);
753 if(OC_STACK_OK != res)
755 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
756 DeleteDoxmBinData(ptrDoxm);
757 return OC_STACK_KEEP_TRANSACTION;
760 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
763 return OC_STACK_KEEP_TRANSACTION;
768 OIC_LOG(INFO, TAG, "Skiping Null response");
769 return OC_STACK_KEEP_TRANSACTION;
772 return OC_STACK_DELETE_TRANSACTION;
776 * Discover owned/unowned devices in the same IP subnet. .
778 * @param[in] waittime Timeout in seconds.
779 * @param[in] isOwned bool flag for owned / unowned discovery
780 * @param[in] ppDevicesList List of OCProvisionDev_t.
782 * @return OC_STACK_OK on success otherwise error.
784 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
786 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
788 if (NULL != *ppDevicesList)
790 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
791 return OC_STACK_INVALID_PARAM;
794 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
795 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
797 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
800 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
801 return OC_STACK_NO_MEMORY;
804 pDInfo->ppDevicesList = ppDevicesList;
805 pDInfo->isOwnedDiscovery = isOwned;
807 OCCallbackData cbData;
808 cbData.cb = &DeviceDiscoveryHandler;
809 cbData.context = (void *)pDInfo;
811 OCStackResult res = OC_STACK_ERROR;
813 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
814 DOXM_OWNED_FALSE_MULTICAST_QUERY;
816 OCDoHandle handle = NULL;
817 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
818 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
819 if (res != OC_STACK_OK)
821 OIC_LOG(ERROR, TAG, "OCStack resource error");
826 //Waiting for each response.
827 res = PMTimeout(waittime, true);
828 if(OC_STACK_OK != res)
830 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
832 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
833 if(OC_STACK_OK != resCancel)
835 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
839 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
840 if (OC_STACK_OK != res)
842 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
846 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
851 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
852 const OCClientResponse *clientResponse)
854 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
856 if(NULL == discoveryInfo || NULL == clientResponse)
858 return OC_STACK_INVALID_PARAM;
861 char rsrc_uri[MAX_URI_LENGTH+1] = {0};
862 int wr_len = snprintf(rsrc_uri, sizeof(rsrc_uri), "%s?%s=%s",
863 OC_RSRVD_WELL_KNOWN_URI, OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
864 if(wr_len <= 0 || (size_t)wr_len >= sizeof(rsrc_uri))
866 OIC_LOG(ERROR, TAG, "rsrc_uri_string_print failed");
867 return OC_STACK_ERROR;
869 //Try to the unicast discovery to getting secure port
870 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
871 if(!PMGenerateQuery(false,
872 clientResponse->devAddr.addr, clientResponse->devAddr.port,
873 clientResponse->connType,
874 query, sizeof(query), rsrc_uri))
876 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
877 return OC_STACK_ERROR;
879 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
881 OCCallbackData cbData;
882 cbData.cb = &SecurePortDiscoveryHandler;
883 cbData.context = (void*)discoveryInfo;
885 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
886 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
887 if(OC_STACK_OK != ret)
889 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
894 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
897 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
902 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
903 const OCClientResponse *clientResponse)
905 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
907 if(NULL == discoveryInfo || NULL == clientResponse)
909 return OC_STACK_INVALID_PARAM;
912 //Try to the unicast discovery to getting security version
913 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
914 if(!PMGenerateQuery(false,
915 clientResponse->devAddr.addr, clientResponse->devAddr.port,
916 clientResponse->connType,
917 query, sizeof(query), OIC_RSRC_VER_URI))
919 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
920 return OC_STACK_ERROR;
922 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
924 OCCallbackData cbData;
925 cbData.cb = &SecurityVersionDiscoveryHandler;
926 cbData.context = (void*)discoveryInfo;
928 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
929 clientResponse->connType, OC_HIGH_QOS, &cbData, NULL, 0);
930 if(OC_STACK_OK != ret)
932 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
937 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
940 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
946 * Function to print OCProvisionDev_t for debug purpose.
948 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
951 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
955 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
956 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
957 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
958 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
959 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
963 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
967 bool PMDeleteFromUUIDList(OCUuidList_t *pUuidList, OicUuid_t *targetId)
969 if(pUuidList == NULL || targetId == NULL)
973 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
974 LL_FOREACH_SAFE(pUuidList, tmp1, tmp2)
976 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
978 LL_DELETE(pUuidList, tmp1);