+ OIC_LOG(DEBUG, TAG, "OUT PostUpdateOperationMode");
+
+ return res;
+}
+
+static OCStackResult SetupPDM(const OCProvisionDev_t* selectedDevice)
+{
+ OIC_LOG_V(DEBUG, TAG, "IN %s", __func__);
+
+ PdmDeviceState_t pdmState = PDM_DEVICE_UNKNOWN;
+ OCStackResult res = PDMGetDeviceState(&selectedDevice->doxm->deviceID, &pdmState);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMGetDeviceState : %d", res);
+ return res;
+ }
+
+ char* strUuid = NULL;
+ bool removeCredReq = false;
+ if (OC_STACK_OK != ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid))
+ {
+ OIC_LOG(WARNING, TAG, "Failed to covert uuid to string");
+ return OC_STACK_NO_MEMORY;
+ }
+
+ if (PDM_DEVICE_UNKNOWN == pdmState && !selectedDevice->doxm->owned)
+ {
+ removeCredReq = true;
+ }
+ else if (PDM_DEVICE_ACTIVE == pdmState && !selectedDevice->doxm->owned)
+ {
+ OIC_LOG_V(WARNING, TAG, "Unowned device[%s] dectected from PDM.", strUuid);
+ OIC_LOG_V(WARNING, TAG, "[%s] will be removed from PDM.", strUuid);
+ res = PDMDeleteDevice(&selectedDevice->doxm->deviceID);
+ if(OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to remove [%s] information from PDM.", strUuid);
+ goto exit;
+ }
+
+ removeCredReq = true;
+ }
+
+ if (removeCredReq)
+ {
+ OIC_LOG_V(WARNING, TAG, "[%s]'s credential will be removed.", strUuid);
+ res = RemoveCredential(&selectedDevice->doxm->deviceID);
+ if (OC_STACK_RESOURCE_DELETED != res)
+ {
+ OIC_LOG_V(WARNING, TAG, "Can not find [%s]'s credential.", strUuid);
+ }
+ }
+
+ //Checking duplication of Device ID.
+ bool isDuplicate = true;
+ res = PDMIsDuplicateDevice(&selectedDevice->doxm->deviceID, &isDuplicate);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMIsDuplicateDevice : %d", res);
+ goto exit;
+ }
+
+ if (isDuplicate)
+ {
+ char* strUuid = NULL;
+ res = ConvertUuidToStr(&selectedDevice->doxm->deviceID, &strUuid);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to convert UUID to str : %d", res);
+ goto exit;
+ }
+
+ if (PDM_DEVICE_STALE == pdmState)
+ {
+ OIC_LOG(INFO, TAG, "Detected duplicated UUID in stale status, "
+ "device status will revert back to initial status.");
+ res = PDMSetDeviceState(&selectedDevice->doxm->deviceID, PDM_DEVICE_INIT);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMSetDeviceState : %d", res);
+ goto exit;
+ }
+ }
+ else if (PDM_DEVICE_INIT == pdmState)
+ {
+ OIC_LOG_V(ERROR, TAG, "[%s]'s ownership transfer process is already started.", strUuid);
+ OICFree(strUuid);
+ res = OC_STACK_DUPLICATE_REQUEST;
+ goto exit;
+ }
+ else
+ {
+ OIC_LOG(ERROR, TAG, "Unknow device status while OTM.");
+ OICFree(strUuid);
+ res = OC_STACK_ERROR;
+ goto exit;
+ }
+ }
+ else
+ {
+ res = PDMAddDevice(&selectedDevice->doxm->deviceID);
+ if (OC_STACK_OK != res)
+ {
+ OIC_LOG_V(ERROR, TAG, "Internal error in PDMAddDevice : %d", res);
+ goto exit;
+ }
+ }