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
30 #include "oic_malloc.h"
31 #include "oic_string.h"
35 #include "ocpayload.h"
37 #include "securevirtualresourcetypes.h"
38 #include "srmresourcestrings.h" //@note: SRM's internal header
39 #include "doxmresource.h" //@note: SRM's internal header
40 #include "pstatresource.h" //@note: SRM's internal header
41 #include "verresource.h" //@note: SRM's internal header
44 #include "pmutility.h"
46 #include "srmutility.h"
48 #define TAG ("PM-UTILITY")
50 typedef struct _DiscoveryInfo{
51 OCProvisionDev_t **ppDevicesList;
52 bool isOwnedDiscovery;
56 * Function to discover secre port information through unicast
58 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
59 * @param[in] clientResponse Response information(It will contain payload)
61 * @return OC_STACK_OK on success otherwise error.
63 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
64 const OCClientResponse *clientResponse);
67 * Function to discover security version 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 SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
75 const OCClientResponse *clientResponse);
78 * Callback handler for PMDeviceDiscovery API.
80 * @param[in] ctx User context
81 * @param[in] handle Handler for response
82 * @param[in] clientResponse Response information (It will contain payload)
83 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
84 * OC_STACK_DELETE_TRANSACTION to delete it.
86 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
87 OCClientResponse *clientResponse);
90 * Callback handler for getting secure port information using /oic/res discovery.
92 * @param[in] ctx user context
93 * @param[in] handle Handle for response
94 * @param[in] clientResponse Response information(It will contain payload)
96 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
97 * OC_STACK_DELETE_TRANSACTION to delete it.
99 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
100 OCClientResponse *clientResponse);
103 * Callback handler for security version discovery.
105 * @param[in] ctx User context
106 * @param[in] handle Handler for response
107 * @param[in] clientResponse Response information (It will contain payload)
108 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
109 * OC_STACK_DELETE_TRANSACTION to delete it.
111 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
112 OCClientResponse *clientResponse);
115 * Function to search node in linked list that matches given IP and port.
117 * @param[in] pList List of OCProvisionDev_t.
118 * @param[in] addr address of target device.
119 * @param[in] port port of remote server.
121 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
123 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
125 if(NULL == addr || NULL == *ppDevicesList)
127 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
131 OCProvisionDev_t *ptr = NULL;
132 LL_FOREACH(*ppDevicesList, ptr)
134 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
145 * Add device information to list.
147 * @param[in] pList List of OCProvisionDev_t.
148 * @param[in] addr address of target device.
149 * @param[in] port port of remote server.
150 * @param[in] adapter adapter type of endpoint.
151 * @param[in] doxm pointer to doxm instance.
152 * @param[in] connType connectivity type of endpoint
154 * @return OC_STACK_OK for success and errorcode otherwise.
156 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port,
157 OCTransportAdapter adapter, OCConnectivityType connType, OicSecDoxm_t *doxm)
161 return OC_STACK_INVALID_PARAM;
164 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
167 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
170 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
171 return OC_STACK_NO_MEMORY;
174 OICStrcpy(ptr->endpoint.addr, MAX_ADDR_STR_SIZE, addr);
175 ptr->endpoint.port = port;
177 ptr->securePort = DEFAULT_SECURE_PORT;
178 ptr->endpoint.adapter = adapter;
180 ptr->connType = connType;
181 ptr->devStatus = DEV_STATUS_ON; //AddDevice is called when discovery(=alive)
182 OICStrcpy(ptr->secVer, strlen("0.0.0"), "0.0.0"); // version initialization
184 LL_PREPEND(*ppDevicesList, ptr);
191 * Function to set secure port information from the given list of devices.
193 * @param[in] pList List of OCProvisionDev_t.
194 * @param[in] addr address of target device.
195 * @param[in] port port of remote server.
196 * @param[in] secureport secure port information.
198 * @return OC_STACK_OK for success and errorcode otherwise.
200 OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
201 uint16_t port, uint16_t securePort)
203 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
207 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
208 return OC_STACK_ERROR;
211 ptr->securePort = securePort;
217 * Function to set security version information from the given list of devices.
219 * @param[in] pList List of OCProvisionDev_t.
220 * @param[in] addr address of target device.
221 * @param[in] port port of remote server.
222 * @param[in] secVer security version information.
224 * @return OC_STACK_OK for success and errorcode otherwise.
226 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
227 uint16_t port, const char* secVer)
231 return OC_STACK_INVALID_PARAM;
234 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
238 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
239 return OC_STACK_ERROR;
242 OICStrcpy(ptr->secVer, strlen(secVer), secVer);
248 * This function deletes list of provision target devices
250 * @param[in] pDevicesList List of OCProvisionDev_t.
252 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
256 OCProvisionDev_t *del = NULL, *tmp = NULL;
257 LL_FOREACH_SAFE(pDevicesList, del, tmp)
259 LL_DELETE(pDevicesList, del);
261 DeleteDoxmBinData(del->doxm);
262 DeletePstatBinData(del->pstat);
268 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
270 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
274 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
278 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
279 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
280 VERIFY_NON_NULL(TAG, newDev, ERROR);
282 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
286 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
287 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
289 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
290 // We have to assign NULL for not necessary information to prevent memory corruption.
291 newDev->pstat->sm = NULL;
296 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
297 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
299 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
300 // We have to assign NULL for not necessary information to prevent memory corruption.
301 newDev->doxm->oxmType = NULL;
302 newDev->doxm->oxm = NULL;
305 if (0 == strlen(src->secVer))
307 OICStrcpy(newDev->secVer, strlen("0.0.0"), "0.0.0");
311 OICStrcpy(newDev->secVer, strlen(src->secVer), src->secVer);
314 newDev->securePort = src->securePort;
315 newDev->devStatus = src->devStatus;
316 newDev->connType = src->connType;
319 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
324 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
327 OICFree(newDev->pstat);
328 OICFree(newDev->doxm);
335 * Timeout implementation for secure discovery. When performing secure discovery,
336 * we should wait a certain period of time for getting response of each devices.
338 * @param[in] waittime Timeout in seconds.
339 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
340 * @return OC_STACK_OK on success otherwise error.
342 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
344 struct timespec startTime = {.tv_sec=0, .tv_nsec=0};
345 struct timespec currTime = {.tv_sec=0, .tv_nsec=0};
347 OCStackResult res = OC_STACK_OK;
348 #ifdef _POSIX_MONOTONIC_CLOCK
349 int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
351 int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
355 return OC_STACK_ERROR;
357 while (OC_STACK_OK == res)
359 #ifdef _POSIX_MONOTONIC_CLOCK
360 clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
362 clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
366 return OC_STACK_TIMEOUT;
368 long elapsed = (currTime.tv_sec - startTime.tv_sec);
369 if (elapsed > waittime)
373 if (waitForStackResponse)
382 * Extract secure port information from payload of discovery response.
384 * @param[in] jsonStr response payload of /oic/res discovery.
386 * @return Secure port
388 uint16_t GetSecurePortFromJSON(char* jsonStr)
390 // TODO: Modify error handling
395 cJSON *jsonProp = NULL;
397 cJSON *jsonPort = NULL;
399 cJSON *jsonRoot = cJSON_Parse(jsonStr);
402 // TODO: Add error log & return default secure port
406 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
409 // TODO: Add error log & return default secure port
413 jsonP = cJSON_GetObjectItem(jsonProp, "p");
416 // TODO: Add error log & return default secure port
420 jsonPort = cJSON_GetObjectItem(jsonP, "port");
423 // TODO: Add error log & return default secure port
427 return (uint16_t)jsonPort->valueint;
430 bool PMGenerateQuery(bool isSecure,
431 const char* address, const uint16_t port,
432 const OCConnectivityType connType,
433 char* buffer, size_t bufferSize, const char* uri)
435 if(!address || !buffer || !uri)
437 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
442 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
444 switch(connType & CT_MASK_ADAPTER)
447 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
450 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
451 prefix, address, port, uri);
454 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
455 prefix, address, port, uri);
458 OIC_LOG(ERROR, TAG, "Unknown address format.");
461 // snprintf return value check
464 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
467 else if ((size_t)snRet >= bufferSize)
469 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
474 // TODO: We need to verify tinyDTLS in below cases
475 case CT_ADAPTER_GATT_BTLE:
476 case CT_ADAPTER_RFCOMM_BTEDR:
477 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
481 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
488 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
489 OCClientResponse *clientResponse)
493 OIC_LOG(ERROR, TAG, "Lost List of device information");
494 return OC_STACK_KEEP_TRANSACTION;
499 if (NULL == clientResponse->payload)
501 OIC_LOG(INFO, TAG, "Skiping Null payload");
502 return OC_STACK_KEEP_TRANSACTION;
504 if (OC_STACK_OK != clientResponse->result)
506 OIC_LOG(INFO, TAG, "Error in response");
507 return OC_STACK_KEEP_TRANSACTION;
511 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
513 OIC_LOG(INFO, TAG, "Unknown payload type");
514 return OC_STACK_KEEP_TRANSACTION;
517 OicSecVer_t *ptrVer = NULL;
518 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData1;
519 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
520 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
521 if ((NULL == ptrVer) && (OC_STACK_OK != res))
523 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
524 return OC_STACK_KEEP_TRANSACTION;
528 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
530 //If this is owend device discovery we have to filter out the responses.
531 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
532 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
533 clientResponse->devAddr.port, ptrVer->secv);
534 if (OC_STACK_OK != res)
536 OIC_LOG(ERROR, TAG, "Error while getting security version.");
537 DeleteVerBinData(ptrVer);
538 return OC_STACK_KEEP_TRANSACTION;
541 OIC_LOG(INFO, TAG, "= Discovered security version =");
542 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
543 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
544 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
546 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
547 DeleteVerBinData(ptrVer);
553 OIC_LOG(INFO, TAG, "Skiping Null response");
554 return OC_STACK_KEEP_TRANSACTION;
557 return OC_STACK_DELETE_TRANSACTION;
560 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
561 OCClientResponse *clientResponse)
565 OIC_LOG(ERROR, TAG, "Lost List of device information");
566 return OC_STACK_DELETE_TRANSACTION;
571 if (NULL == clientResponse->payload)
573 OIC_LOG(INFO, TAG, "Skiping Null payload");
577 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
579 OIC_LOG(INFO, TAG, "Wrong payload type");
580 return OC_STACK_DELETE_TRANSACTION;
583 uint16_t securePort = 0;
584 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
586 if (resPayload && resPayload->secure)
588 securePort = resPayload->port;
592 OIC_LOG(INFO, TAG, "Can not find secure port information.");
593 return OC_STACK_DELETE_TRANSACTION;
596 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
597 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
598 clientResponse->devAddr.addr,
599 clientResponse->devAddr.port, securePort);
600 if (OC_STACK_OK != res)
602 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
603 return OC_STACK_DELETE_TRANSACTION;
606 res = SecurityVersionDiscovery(pDInfo, clientResponse);
607 if(OC_STACK_OK != res)
609 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
610 return OC_STACK_DELETE_TRANSACTION;
613 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
616 return OC_STACK_DELETE_TRANSACTION;
620 OIC_LOG(INFO, TAG, "Skiping Null response");
623 return OC_STACK_DELETE_TRANSACTION;
626 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
627 OCClientResponse *clientResponse)
631 OIC_LOG(ERROR, TAG, "Lost List of device information");
632 return OC_STACK_KEEP_TRANSACTION;
637 if (NULL == clientResponse->payload)
639 OIC_LOG(INFO, TAG, "Skiping Null payload");
640 return OC_STACK_KEEP_TRANSACTION;
642 if (OC_STACK_OK != clientResponse->result)
644 OIC_LOG(INFO, TAG, "Error in response");
645 return OC_STACK_KEEP_TRANSACTION;
649 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
651 OIC_LOG(INFO, TAG, "Unknown payload type");
652 return OC_STACK_KEEP_TRANSACTION;
655 OicSecDoxm_t *ptrDoxm = NULL;
656 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData1;
657 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
658 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
659 if ((NULL == ptrDoxm) && (OC_STACK_OK != res))
661 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
662 return OC_STACK_KEEP_TRANSACTION;
666 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
668 //If this is owend device discovery we have to filter out the responses.
669 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
670 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
672 // Get my device ID from doxm resource
674 memset(&myId, 0, sizeof(myId));
675 OCStackResult res = GetDoxmDevOwnerId(&myId);
676 if(OC_STACK_OK != res)
678 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
679 DeleteDoxmBinData(ptrDoxm);
680 return OC_STACK_KEEP_TRANSACTION;
683 // If this is owned discovery response but owner is not me then discard it.
684 if( (pDInfo->isOwnedDiscovery) &&
685 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
687 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
688 DeleteDoxmBinData(ptrDoxm);
689 return OC_STACK_KEEP_TRANSACTION;
692 res = AddDevice(ppDevicesList, clientResponse->devAddr.addr,
693 clientResponse->devAddr.port,
694 clientResponse->devAddr.adapter,
695 clientResponse->connType, ptrDoxm);
696 if (OC_STACK_OK != res)
698 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
699 DeleteDoxmBinData(ptrDoxm);
700 return OC_STACK_KEEP_TRANSACTION;
703 res = SecurePortDiscovery(pDInfo, clientResponse);
704 if(OC_STACK_OK != res)
706 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
707 DeleteDoxmBinData(ptrDoxm);
708 return OC_STACK_KEEP_TRANSACTION;
711 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
714 return OC_STACK_KEEP_TRANSACTION;
719 OIC_LOG(INFO, TAG, "Skiping Null response");
720 return OC_STACK_KEEP_TRANSACTION;
723 return OC_STACK_DELETE_TRANSACTION;
727 * Discover owned/unowned devices in the same IP subnet. .
729 * @param[in] waittime Timeout in seconds.
730 * @param[in] isOwned bool flag for owned / unowned discovery
731 * @param[in] ppDevicesList List of OCProvisionDev_t.
733 * @return OC_STACK_OK on success otherwise error.
735 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
737 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
739 if (NULL != *ppDevicesList)
741 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
742 return OC_STACK_INVALID_PARAM;
745 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
746 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
748 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
751 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
752 return OC_STACK_NO_MEMORY;
755 pDInfo->ppDevicesList = ppDevicesList;
756 pDInfo->isOwnedDiscovery = isOwned;
758 OCCallbackData cbData;
759 cbData.cb = &DeviceDiscoveryHandler;
760 cbData.context = (void *)pDInfo;
762 OCStackResult res = OC_STACK_ERROR;
764 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
765 DOXM_OWNED_FALSE_MULTICAST_QUERY;
767 OCDoHandle handle = NULL;
768 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
769 CT_DEFAULT, OC_LOW_QOS, &cbData, NULL, 0);
770 if (res != OC_STACK_OK)
772 OIC_LOG(ERROR, TAG, "OCStack resource error");
777 //Waiting for each response.
778 res = PMTimeout(waittime, true);
779 if(OC_STACK_OK != res)
781 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
783 OCStackResult resCancel = OCCancel(handle, OC_LOW_QOS, NULL, 0);
784 if(OC_STACK_OK != resCancel)
786 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
790 res = OCCancel(handle,OC_LOW_QOS,NULL,0);
791 if (OC_STACK_OK != res)
793 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
797 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
802 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
803 const OCClientResponse *clientResponse)
805 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
807 if(NULL == discoveryInfo || NULL == clientResponse)
809 return OC_STACK_INVALID_PARAM;
812 char rsrc_uri[MAX_URI_LENGTH+1] = {0};
813 int wr_len = snprintf(rsrc_uri, sizeof(rsrc_uri), "%s?%s=%s",
814 OC_RSRVD_WELL_KNOWN_URI, OC_RSRVD_RESOURCE_TYPE, OIC_RSRC_TYPE_SEC_DOXM);
815 if(wr_len <= 0 || (size_t)wr_len >= sizeof(rsrc_uri))
817 OIC_LOG(ERROR, TAG, "rsrc_uri_string_print failed");
818 return OC_STACK_ERROR;
820 //Try to the unicast discovery to getting secure port
821 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
822 if(!PMGenerateQuery(false,
823 clientResponse->devAddr.addr, clientResponse->devAddr.port,
824 clientResponse->connType,
825 query, sizeof(query), rsrc_uri))
827 OIC_LOG(ERROR, TAG, "SecurePortDiscovery : Failed to generate query");
828 return OC_STACK_ERROR;
830 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
832 OCCallbackData cbData;
833 cbData.cb = &SecurePortDiscoveryHandler;
834 cbData.context = (void*)discoveryInfo;
836 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
837 clientResponse->connType, OC_LOW_QOS, &cbData, NULL, 0);
838 if(OC_STACK_OK != ret)
840 OIC_LOG(ERROR, TAG, "Failed to Secure Port Discovery");
845 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
848 OIC_LOG(DEBUG, TAG, "OUT SecurePortDiscovery");
853 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
854 const OCClientResponse *clientResponse)
856 OIC_LOG(DEBUG, TAG, "IN SecurityVersionDiscovery");
858 if(NULL == discoveryInfo || NULL == clientResponse)
860 return OC_STACK_INVALID_PARAM;
863 //Try to the unicast discovery to getting security version
864 char query[MAX_URI_LENGTH+MAX_QUERY_LENGTH+1] = {0};
865 if(!PMGenerateQuery(false,
866 clientResponse->devAddr.addr, clientResponse->devAddr.port,
867 clientResponse->connType,
868 query, sizeof(query), OIC_RSRC_VER_URI))
870 OIC_LOG(ERROR, TAG, "SecurityVersionDiscovery : Failed to generate query");
871 return OC_STACK_ERROR;
873 OIC_LOG_V(DEBUG, TAG, "Query=%s", query);
875 OCCallbackData cbData;
876 cbData.cb = &SecurityVersionDiscoveryHandler;
877 cbData.context = (void*)discoveryInfo;
879 OCStackResult ret = OCDoResource(NULL, OC_REST_DISCOVER, query, 0, 0,
880 clientResponse->connType, OC_LOW_QOS, &cbData, NULL, 0);
881 if(OC_STACK_OK != ret)
883 OIC_LOG(ERROR, TAG, "Failed to Security Version Discovery");
888 OIC_LOG_V(INFO, TAG, "OCDoResource with [%s] Success", query);
891 OIC_LOG(DEBUG, TAG, "OUT SecurityVersionDiscovery");
897 * Function to print OCProvisionDev_t for debug purpose.
899 * @param[in] pDev Pointer to OCProvisionDev_t. It's information will be printed by OIC_LOG_XX
902 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
906 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t Information +++++");
907 OIC_LOG_V(DEBUG, TAG, "IP %s", pDev->endpoint.addr);
908 OIC_LOG_V(DEBUG, TAG, "PORT %d", pDev->endpoint.port);
909 OIC_LOG_V(DEBUG, TAG, "S-PORT %d", pDev->securePort);
910 OIC_LOG(DEBUG, TAG, "++++++++++++++++++++++++++++++++++++++++");
914 OIC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
918 bool PMDeleteFromUUIDList(OCUuidList_t *pUuidList, OicUuid_t *targetId)
920 if(pUuidList == NULL || targetId == NULL)
924 OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
925 LL_FOREACH_SAFE(pUuidList, tmp1, tmp2)
927 if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
929 LL_DELETE(pUuidList, tmp1);