Update snapshot(2018-01-17)
[platform/upstream/iotivity.git] / resource / csdk / security / provisioning / src / ownershiptransfermanager.c
index 9ce4414..92836d5 100644 (file)
@@ -2165,6 +2165,60 @@ exit:
     return res;
 }
 
+static OCStackResult StartCustomOwnershipTransfer(void* ctx, OCProvisionDev_t* selectedDevice,const OicSecOxm_t method)
+{
+    OIC_LOG(INFO, TAG, "IN StartOwnershipTransfer");
+    OCStackResult res = OC_STACK_INVALID_PARAM;
+
+    VERIFY_NON_NULL(TAG, selectedDevice, ERROR);
+    VERIFY_NON_NULL(TAG, selectedDevice->doxm, ERROR);
+
+    OTMContext_t* otmCtx = (OTMContext_t*)ctx;
+    otmCtx->selectedDeviceInfo = selectedDevice;
+
+    //Setup PDM to perform the OTM, PDM will be cleanup if necessary.
+    res = SetupPDM(selectedDevice);
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG_V(ERROR, TAG, "SetupPDM error : %d", res);
+        SetResult(otmCtx, res);
+        return res;
+    }
+
+    //Select the OxM to performing ownership transfer
+    selectedDevice->doxm->oxmSel = method;
+    OIC_LOG_V(DEBUG, TAG, "Selected provisoning method = %d", selectedDevice->doxm->oxmSel);
+
+    res = OTMSetOTCallback(selectedDevice->doxm->oxmSel, &otmCtx->otmCallback);
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG_V(ERROR, TAG, "Error in OTMSetOTCallback : %d", res);
+        return res;
+    }
+
+#if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
+    //Register TLS event handler, to catch the TLS handshake event
+    if(CA_STATUS_OK != CAregisterSslHandshakeCallback(DTLSHandshakeCB))
+    {
+        OIC_LOG(WARNING, TAG, "StartOwnershipTransfer : Failed to register TLS handshake callback.");
+    }
+#endif // __WITH_DTLS__ or __WITH_TLS__
+
+    //Send Req: POST /oic/sec/doxm [{..."OxmSel" :g_OTMCbDatas[Index of Selected OxM].OXMString,...}]
+    res = PostOwnerTransferModeToResource(otmCtx);
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG_V(WARNING, TAG, "Failed to select the provisioning method : %d", res);
+        SetResult(otmCtx, res);
+        return res;
+    }
+
+    OIC_LOG(INFO, TAG, "OUT StartOwnershipTransfer");
+
+exit:
+    return res;
+}
+
 OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallbackData_t* data)
 {
     OIC_LOG(DEBUG, TAG, "IN OTMSetOwnerTransferCallbackData");
@@ -2187,6 +2241,58 @@ OCStackResult OTMSetOwnershipTransferCallbackData(OicSecOxm_t oxmType, OTMCallba
     return OC_STACK_OK;
 }
 
+OCStackResult OTMDoCustomOwnershipTransfer(void* ctx,
+                                     OCProvisionDev_t *selectedDevice,
+                                     OCProvisionResultCB resultCallback,
+                                     const OicSecOxm_t method)
+{
+    OIC_LOG(DEBUG, TAG, "IN OTMDoCustomOwnershipTransfer");
+
+    if (NULL == selectedDevice)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+    if (NULL == resultCallback)
+    {
+        return OC_STACK_INVALID_CALLBACK;
+    }
+
+    OTMContext_t* otmCtx = (OTMContext_t*)OICCalloc(1,sizeof(OTMContext_t));
+    if(!otmCtx)
+    {
+        OIC_LOG(ERROR, TAG, "Failed to create OTM Context");
+        return OC_STACK_NO_MEMORY;
+    }
+
+    otmCtx->ctxResultCallback = resultCallback;
+    otmCtx->ctxHasError = false;
+    otmCtx->userCtx = ctx;
+
+    //Setting number of selected device.
+    otmCtx->ctxResultArraySize = 1;
+
+    otmCtx->ctxResultArray =
+        (OCProvisionResult_t*)OICCalloc(otmCtx->ctxResultArraySize, sizeof(OCProvisionResult_t));
+    if(NULL == otmCtx->ctxResultArray)
+    {
+        OIC_LOG(ERROR, TAG, "OTMDoOwnershipTransfer : Failed to memory allocation");
+        OICFree(otmCtx);
+        return OC_STACK_NO_MEMORY;
+    }
+
+    //Fill the device UUID for result array.
+        memcpy(otmCtx->ctxResultArray[0].deviceId.id,
+               selectedDevice->doxm->deviceID.id,
+               UUID_LENGTH);
+        otmCtx->ctxResultArray[0].res = OC_STACK_CONTINUE;
+
+    OCStackResult res = StartCustomOwnershipTransfer(otmCtx, selectedDevice, method);
+
+    OIC_LOG(DEBUG, TAG, "OUT OTMDoCustomOwnershipTransfer");
+
+    return res;
+}
+
 /**
  * NOTE : Unowned discovery should be done before performing OTMDoOwnershipTransfer
  */