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
32 #include "oic_malloc.h"
33 #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 ("PM-UTILITY")
53 typedef struct _DiscoveryInfo{
54 OCProvisionDev_t **ppDevicesList;
55 bool isOwnedDiscovery;
59 * Function to discover secre port information through unicast
61 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
62 * @param[in] clientResponse Response information(It will contain payload)
64 * @return OC_STACK_OK on success otherwise error.
66 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
67 const OCClientResponse *clientResponse);
70 * Function to discover security version information through unicast
72 * @param[in] discoveryInfo The pointer of discovery information to matain result of discovery
73 * @param[in] clientResponse Response information(It will contain payload)
75 * @return OC_STACK_OK on success otherwise error.
77 static OCStackResult SecurityVersionDiscovery(DiscoveryInfo* discoveryInfo,
78 const OCClientResponse *clientResponse);
81 * Callback handler for PMDeviceDiscovery API.
83 * @param[in] ctx User context
84 * @param[in] handle Handler for response
85 * @param[in] clientResponse Response information (It will contain payload)
86 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
87 * OC_STACK_DELETE_TRANSACTION to delete it.
89 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
90 OCClientResponse *clientResponse);
93 * Callback handler for getting secure port information using /oic/res discovery.
95 * @param[in] ctx user context
96 * @param[in] handle Handle for response
97 * @param[in] clientResponse Response information(It will contain payload)
99 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
100 * OC_STACK_DELETE_TRANSACTION to delete it.
102 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
103 OCClientResponse *clientResponse);
106 * Callback handler for security version discovery.
108 * @param[in] ctx User context
109 * @param[in] handle Handler for response
110 * @param[in] clientResponse Response information (It will contain payload)
111 * @return OC_STACK_KEEP_TRANSACTION to keep transaction and
112 * OC_STACK_DELETE_TRANSACTION to delete it.
114 static OCStackApplicationResult SecVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
115 OCClientResponse *clientResponse);
118 * Function to search node in linked list that matches given IP and port.
120 * @param[in] pList List of OCProvisionDev_t.
121 * @param[in] addr address of target device.
122 * @param[in] port port of remote server.
124 * @return pointer of OCProvisionDev_t if exist, otherwise NULL
126 OCProvisionDev_t* GetDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port)
128 if(NULL == addr || NULL == *ppDevicesList)
130 OIC_LOG_V(ERROR, TAG, "Invalid Input parameters in [%s]\n", __FUNCTION__);
134 OCProvisionDev_t *ptr = NULL;
135 LL_FOREACH(*ppDevicesList, ptr)
137 if( strcmp(ptr->endpoint.addr, addr) == 0 && port == ptr->endpoint.port)
148 * Add device information to list.
150 * @param[in] pList List of OCProvisionDev_t.
151 * @param[in] addr address of target device.
152 * @param[in] port port of remote server.
153 * @param[in] adapter adapter type of endpoint.
154 * @param[in] doxm pointer to doxm instance.
155 * @param[in] connType connectivity type of endpoint
157 * @return OC_STACK_OK for success and errorcode otherwise.
159 OCStackResult AddDevice(OCProvisionDev_t **ppDevicesList, const char* addr, const uint16_t port,
160 OCTransportAdapter adapter, OCConnectivityType connType, OicSecDoxm_t *doxm)
164 return OC_STACK_INVALID_PARAM;
167 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
170 ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
173 OIC_LOG(ERROR, TAG, "Error while allocating memory for linkedlist node !!");
174 return OC_STACK_NO_MEMORY;
177 OICStrcpy(ptr->endpoint.addr, MAX_ADDR_STR_SIZE, addr);
178 ptr->endpoint.port = port;
180 ptr->securePort = DEFAULT_SECURE_PORT;
181 ptr->endpoint.adapter = adapter;
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 OCStackResult UpdateSecurePortOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
204 uint16_t port, uint16_t securePort)
206 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
210 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
211 return OC_STACK_ERROR;
214 ptr->securePort = securePort;
220 * Function to set security version information from the given list of devices.
222 * @param[in] pList List of OCProvisionDev_t.
223 * @param[in] addr address of target device.
224 * @param[in] port port of remote server.
225 * @param[in] secVer security version information.
227 * @return OC_STACK_OK for success and errorcode otherwise.
229 OCStackResult UpdateSecVersionOfDevice(OCProvisionDev_t **ppDevicesList, const char *addr,
230 uint16_t port, const char* secVer)
234 return OC_STACK_INVALID_PARAM;
237 OCProvisionDev_t *ptr = GetDevice(ppDevicesList, addr, port);
241 OIC_LOG(ERROR, TAG, "Can not find device information in the discovery device list");
242 return OC_STACK_ERROR;
245 OICStrcpy(ptr->secVer, MAX_VERSION_LEN, secVer);
251 * This function deletes list of provision target devices
253 * @param[in] pDevicesList List of OCProvisionDev_t.
255 void PMDeleteDeviceList(OCProvisionDev_t *pDevicesList)
259 OCProvisionDev_t *del = NULL, *tmp = NULL;
260 LL_FOREACH_SAFE(pDevicesList, del, tmp)
262 LL_DELETE(pDevicesList, del);
264 DeleteDoxmBinData(del->doxm);
265 DeletePstatBinData(del->pstat);
271 OCProvisionDev_t* PMCloneOCProvisionDev(const OCProvisionDev_t* src)
273 OIC_LOG(DEBUG, TAG, "IN PMCloneOCProvisionDev");
277 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Invalid parameter");
281 // TODO: Consider use VERIFY_NON_NULL instead of if ( null check ) { goto exit; }
282 OCProvisionDev_t* newDev = (OCProvisionDev_t*)OICCalloc(1, sizeof(OCProvisionDev_t));
283 VERIFY_NON_NULL(TAG, newDev, ERROR);
285 memcpy(&newDev->endpoint, &src->endpoint, sizeof(OCDevAddr));
289 newDev->pstat= (OicSecPstat_t*)OICCalloc(1, sizeof(OicSecPstat_t));
290 VERIFY_NON_NULL(TAG, newDev->pstat, ERROR);
292 memcpy(newDev->pstat, src->pstat, sizeof(OicSecPstat_t));
293 // We have to assign NULL for not necessary information to prevent memory corruption.
294 newDev->pstat->sm = NULL;
299 newDev->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
300 VERIFY_NON_NULL(TAG, newDev->doxm, ERROR);
302 memcpy(newDev->doxm, src->doxm, sizeof(OicSecDoxm_t));
303 // We have to assign NULL for not necessary information to prevent memory corruption.
304 newDev->doxm->oxmType = NULL;
305 newDev->doxm->oxm = NULL;
308 if (0 == strlen(src->secVer))
310 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, DEFAULT_SEC_VERSION);
314 OICStrcpy(newDev->secVer, MAX_VERSION_LEN, src->secVer);
317 newDev->securePort = src->securePort;
318 newDev->devStatus = src->devStatus;
319 newDev->connType = src->connType;
322 OIC_LOG(DEBUG, TAG, "OUT PMCloneOCProvisionDev");
327 OIC_LOG(ERROR, TAG, "PMCloneOCProvisionDev : Failed to allocate memory");
330 OICFree(newDev->pstat);
331 OICFree(newDev->doxm);
338 * Timeout implementation for secure discovery. When performing secure discovery,
339 * we should wait a certain period of time for getting response of each devices.
341 * @param[in] waittime Timeout in seconds.
342 * @param[in] waitForStackResponse if true timeout function will call OCProcess while waiting.
343 * @return OC_STACK_OK on success otherwise error.
345 OCStackResult PMTimeout(unsigned short waittime, bool waitForStackResponse)
347 OCStackResult res = OC_STACK_OK;
351 startTime = OICGetCurrentTime(TIME_IN_MS);
352 while (OC_STACK_OK == res)
354 currTime = OICGetCurrentTime(TIME_IN_MS);
356 long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
357 if (elapsed > waittime)
361 if (waitForStackResponse)
370 * Extract secure port information from payload of discovery response.
372 * @param[in] jsonStr response payload of /oic/res discovery.
374 * @return Secure port
376 uint16_t GetSecurePortFromJSON(char* jsonStr)
378 // TODO: Modify error handling
383 cJSON *jsonProp = NULL;
385 cJSON *jsonPort = NULL;
387 cJSON *jsonRoot = cJSON_Parse(jsonStr);
390 // TODO: Add error log & return default secure port
394 jsonProp = cJSON_GetObjectItem(jsonRoot, "prop");
397 // TODO: Add error log & return default secure port
401 jsonP = cJSON_GetObjectItem(jsonProp, "p");
404 // TODO: Add error log & return default secure port
408 jsonPort = cJSON_GetObjectItem(jsonP, "port");
411 // TODO: Add error log & return default secure port
415 return (uint16_t)jsonPort->valueint;
418 bool PMGenerateQuery(bool isSecure,
419 const char* address, uint16_t port,
420 OCConnectivityType connType,
421 char* buffer, size_t bufferSize, const char* uri)
423 if(!address || !buffer || !uri)
425 OIC_LOG(ERROR, TAG, "PMGenerateQuery : Invalid parameters.");
430 char* prefix = (isSecure == true) ? COAPS_PREFIX : COAP_PREFIX;
432 switch(connType & CT_MASK_ADAPTER)
435 switch(connType & CT_MASK_FLAGS & ~CT_FLAG_SECURE)
438 snRet = snprintf(buffer, bufferSize, "%s%s:%d%s",
439 prefix, address, port, uri);
442 snRet = snprintf(buffer, bufferSize, "%s[%s]:%d%s",
443 prefix, address, port, uri);
446 OIC_LOG(ERROR, TAG, "Unknown address format.");
449 // snprintf return value check
452 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Error (snprintf) %d\n", snRet);
455 else if ((size_t)snRet >= bufferSize)
457 OIC_LOG_V(ERROR, TAG, "PMGenerateQuery : Truncated (snprintf) %d\n", snRet);
462 // TODO: We need to verify tinyDTLS in below cases
463 case CT_ADAPTER_GATT_BTLE:
464 case CT_ADAPTER_RFCOMM_BTEDR:
465 OIC_LOG(ERROR, TAG, "Not supported connectivity adapter.");
469 OIC_LOG(ERROR, TAG, "Unknown connectivity adapter.");
476 static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
477 OCClientResponse *clientResponse)
481 OIC_LOG(ERROR, TAG, "Lost List of device information");
482 return OC_STACK_KEEP_TRANSACTION;
487 if (NULL == clientResponse->payload)
489 OIC_LOG(INFO, TAG, "Skiping Null payload");
490 return OC_STACK_KEEP_TRANSACTION;
492 if (OC_STACK_OK != clientResponse->result)
494 OIC_LOG(INFO, TAG, "Error in response");
495 return OC_STACK_KEEP_TRANSACTION;
499 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
501 OIC_LOG(INFO, TAG, "Unknown payload type");
502 return OC_STACK_KEEP_TRANSACTION;
505 OicSecVer_t *ptrVer = NULL;
506 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
507 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
509 OCStackResult res = CBORPayloadToVer(payload, size, &ptrVer);
510 if ((NULL == ptrVer) && (OC_STACK_OK != res))
512 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
513 return OC_STACK_KEEP_TRANSACTION;
517 OIC_LOG(DEBUG, TAG, "Successfully converted ver cbor to bin.");
519 //If this is owend device discovery we have to filter out the responses.
520 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
521 res = UpdateSecVersionOfDevice(pDInfo->ppDevicesList, clientResponse->devAddr.addr,
522 clientResponse->devAddr.port, ptrVer->secv);
523 if (OC_STACK_OK != res)
525 OIC_LOG(ERROR, TAG, "Error while getting security version.");
526 DeleteVerBinData(ptrVer);
527 return OC_STACK_KEEP_TRANSACTION;
530 OIC_LOG(INFO, TAG, "= Discovered security version =");
531 OIC_LOG_V(DEBUG, TAG, "IP %s", clientResponse->devAddr.addr);
532 OIC_LOG_V(DEBUG, TAG, "PORT %d", clientResponse->devAddr.port);
533 OIC_LOG_V(DEBUG, TAG, "VERSION %s", ptrVer->secv);
535 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
536 DeleteVerBinData(ptrVer);
542 OIC_LOG(INFO, TAG, "Skiping Null response");
543 return OC_STACK_KEEP_TRANSACTION;
546 return OC_STACK_DELETE_TRANSACTION;
549 static OCStackApplicationResult SecurePortDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
550 OCClientResponse *clientResponse)
554 OIC_LOG(ERROR, TAG, "Lost List of device information");
555 return OC_STACK_DELETE_TRANSACTION;
560 if (NULL == clientResponse->payload)
562 OIC_LOG(INFO, TAG, "Skiping Null payload");
566 if (PAYLOAD_TYPE_DISCOVERY != clientResponse->payload->type)
568 OIC_LOG(INFO, TAG, "Wrong payload type");
569 return OC_STACK_DELETE_TRANSACTION;
572 uint16_t securePort = 0;
573 OCResourcePayload* resPayload = ((OCDiscoveryPayload*)clientResponse->payload)->resources;
575 // Use seure port of doxm for OTM and Provision.
578 if (0 == strncmp(resPayload->uri, OIC_RSRC_DOXM_URI, sizeof(OIC_RSRC_DOXM_URI)))
580 OIC_LOG_V(INFO,TAG,"resPaylod->uri:%s",resPayload->uri);
581 OIC_LOG(INFO, TAG, "Found doxm resource.");
586 resPayload = resPayload->next;
589 if (NULL == resPayload)
591 OIC_LOG(ERROR, TAG, "Can not find doxm resource.");
592 return OC_STACK_DELETE_TRANSACTION;
594 if (resPayload && resPayload->secure)
596 securePort = resPayload->port;
600 OIC_LOG(INFO, TAG, "Can not find secure port information.");
601 return OC_STACK_DELETE_TRANSACTION;
604 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
605 OCStackResult res = UpdateSecurePortOfDevice(pDInfo->ppDevicesList,
606 clientResponse->devAddr.addr,
607 clientResponse->devAddr.port, securePort);
608 if (OC_STACK_OK != res)
610 OIC_LOG(ERROR, TAG, "Error while getting secure port.");
611 return OC_STACK_DELETE_TRANSACTION;
614 res = SecurityVersionDiscovery(pDInfo, clientResponse);
615 if(OC_STACK_OK != res)
617 OIC_LOG(ERROR, TAG, "Failed to SecurityVersionDiscovery");
618 return OC_STACK_DELETE_TRANSACTION;
621 OIC_LOG(INFO, TAG, "Exiting SecurePortDiscoveryHandler.");
624 return OC_STACK_DELETE_TRANSACTION;
628 OIC_LOG(INFO, TAG, "Skiping Null response");
631 return OC_STACK_DELETE_TRANSACTION;
634 static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNUSED,
635 OCClientResponse *clientResponse)
639 OIC_LOG(ERROR, TAG, "Lost List of device information");
640 return OC_STACK_KEEP_TRANSACTION;
645 if (NULL == clientResponse->payload)
647 OIC_LOG(INFO, TAG, "Skiping Null payload");
648 return OC_STACK_KEEP_TRANSACTION;
650 if (OC_STACK_OK != clientResponse->result)
652 OIC_LOG(INFO, TAG, "Error in response");
653 return OC_STACK_KEEP_TRANSACTION;
657 if (PAYLOAD_TYPE_SECURITY != clientResponse->payload->type)
659 OIC_LOG(INFO, TAG, "Unknown payload type");
660 return OC_STACK_KEEP_TRANSACTION;
663 OicSecDoxm_t *ptrDoxm = NULL;
664 uint8_t *payload = ((OCSecurityPayload*)clientResponse->payload)->securityData;
665 size_t size = ((OCSecurityPayload*)clientResponse->payload)->payloadSize;
667 OCStackResult res = CBORPayloadToDoxm(payload, size, &ptrDoxm);
668 if ((NULL == ptrDoxm) || (OC_STACK_OK != res))
670 OIC_LOG(INFO, TAG, "Ignoring malformed CBOR");
671 return OC_STACK_KEEP_TRANSACTION;
675 OIC_LOG(DEBUG, TAG, "Successfully converted doxm cbor to bin.");
677 //If this is owend device discovery we have to filter out the responses.
678 DiscoveryInfo* pDInfo = (DiscoveryInfo*)ctx;
679 OCProvisionDev_t **ppDevicesList = pDInfo->ppDevicesList;
681 // Get my device ID from doxm resource
683 memset(&myId, 0, sizeof(myId));
684 OCStackResult res = GetDoxmDevOwnerId(&myId);
685 if(OC_STACK_OK != res)
687 OIC_LOG(ERROR, TAG, "Error while getting my device ID.");
688 DeleteDoxmBinData(ptrDoxm);
689 return OC_STACK_KEEP_TRANSACTION;
692 // If this is owned discovery response but owner is not me then discard it.
693 if( (pDInfo->isOwnedDiscovery) &&
694 (0 != memcmp(&ptrDoxm->owner.id, &myId.id, sizeof(myId.id))) )
696 OIC_LOG(DEBUG, TAG, "Discovered device is not owend by me");
697 DeleteDoxmBinData(ptrDoxm);
698 return OC_STACK_KEEP_TRANSACTION;
701 res = AddDevice(ppDevicesList, clientResponse->devAddr.addr,
702 clientResponse->devAddr.port,
703 clientResponse->devAddr.adapter,
704 clientResponse->connType, ptrDoxm);
705 if (OC_STACK_OK != res)
707 OIC_LOG(ERROR, TAG, "Error while adding data to linkedlist.");
708 DeleteDoxmBinData(ptrDoxm);
709 return OC_STACK_KEEP_TRANSACTION;
712 res = SecurePortDiscovery(pDInfo, clientResponse);
713 if(OC_STACK_OK != res)
715 OIC_LOG(ERROR, TAG, "Failed to SecurePortDiscovery");
716 DeleteDoxmBinData(ptrDoxm);
717 return OC_STACK_KEEP_TRANSACTION;
720 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
723 return OC_STACK_KEEP_TRANSACTION;
728 OIC_LOG(INFO, TAG, "Skiping Null response");
729 return OC_STACK_KEEP_TRANSACTION;
732 return OC_STACK_DELETE_TRANSACTION;
736 * Discover owned/unowned devices in the same IP subnet. .
738 * @param[in] waittime Timeout in seconds.
739 * @param[in] isOwned bool flag for owned / unowned discovery
740 * @param[in] ppDevicesList List of OCProvisionDev_t.
742 * @return OC_STACK_OK on success otherwise error.
744 OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisionDev_t **ppDevicesList)
746 OIC_LOG(DEBUG, TAG, "IN PMDeviceDiscovery");
748 if (NULL != *ppDevicesList)
750 OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
751 return OC_STACK_INVALID_PARAM;
754 const char DOXM_OWNED_FALSE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=FALSE";
755 const char DOXM_OWNED_TRUE_MULTICAST_QUERY[] = "/oic/sec/doxm?Owned=TRUE";
757 DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
760 OIC_LOG(ERROR, TAG, "PMDeviceDiscovery : Memory allocation failed.");
761 return OC_STACK_NO_MEMORY;
764 pDInfo->ppDevicesList = ppDevicesList;
765 pDInfo->isOwnedDiscovery = isOwned;
767 OCCallbackData cbData;
768 cbData.cb = &DeviceDiscoveryHandler;
769 cbData.context = (void *)pDInfo;
771 OCStackResult res = OC_STACK_ERROR;
773 const char* query = isOwned ? DOXM_OWNED_TRUE_MULTICAST_QUERY :
774 DOXM_OWNED_FALSE_MULTICAST_QUERY;
776 OCDoHandle handle = NULL;
777 res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
778 CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
779 if (res != OC_STACK_OK)
781 OIC_LOG(ERROR, TAG, "OCStack resource error");
786 //Waiting for each response.
787 res = PMTimeout(waittime, true);
788 if(OC_STACK_OK != res)
790 OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
792 OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
793 if(OC_STACK_OK != resCancel)
795 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
799 res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
800 if (OC_STACK_OK != res)
802 OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
806 OIC_LOG(DEBUG, TAG, "OUT PMDeviceDiscovery");
811 static OCStackResult SecurePortDiscovery(DiscoveryInfo* discoveryInfo,
812 const OCClientResponse *clientResponse)
814 OIC_LOG(DEBUG, TAG, "IN SecurePortDiscovery");
816 if(NULL == discoveryInfo || NULL == clientResponse)
818 return OC_STACK_INVALID_PARAM;
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), OC_RSRVD_WELL_KNOWN_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_HIGH_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_HIGH_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);