/* * Function to device revocation * This function will remove credential of target device from all devices in subnet. * * @param[in] ctx Application context would be returned in result callback * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds) * @param[in] pTargetDev Device information to be revoked. * @param[in] resultCallback callback provided by API user, callback will be called when * credential revocation is finished. * @return OC_STACK_OK in case of success and other value otherwise. */ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery, const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback) { OIC_LOG(INFO, TAG, "IN OCRemoveDevice"); OCStackResult res = OC_STACK_ERROR; if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery) { OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters"); return OC_STACK_INVALID_PARAM; } if (!resultCallback) { OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback"); return OC_STACK_INVALID_CALLBACK; } // Send DELETE requests to linked devices OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not. resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback); if (OC_STACK_OK != resReq) { if (OC_STACK_CONTINUE == resReq) { OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT."); } else { OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice"); res = resReq; goto error; } } res = RemoveDeviceInfoFromLocal(pTargetDev); if(OC_STACK_OK != res) { OIC_LOG(ERROR, TAG, "Filed to remove the device information from local."); goto error; } if(OC_STACK_CONTINUE == resReq) { /** * If there is no linked device, PM does not send any request. * So we should directly invoke the result callback to inform the result of OCRemoveDevice. */ if(resultCallback) { resultCallback(ctx, 0, NULL, false); } res = OC_STACK_OK; } error: OIC_LOG(INFO, TAG, "OUT OCRemoveDevice"); return res; }
/* * Function to device revocation * This function will remove credential of target device from all devices in subnet. * * @param[in] ctx Application context would be returned in result callback * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device discovery.(seconds) * @param[in] pTargetDev Device information to be revoked. * @param[in] resultCallback callback provided by API user, callback will be called when * credential revocation is finished. * @return OC_STACK_OK in case of success and other value otherwise. */ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery, const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback) { OIC_LOG(INFO, TAG, "IN OCRemoveDevice"); OCStackResult res = OC_STACK_ERROR; if (!pTargetDev || 0 == waitTimeForOwnedDeviceDiscovery) { OIC_LOG(INFO, TAG, "OCRemoveDevice : Invalied parameters"); return OC_STACK_INVALID_PARAM; } if (!resultCallback) { OIC_LOG(INFO, TAG, "OCRemoveDevice : NULL Callback"); return OC_STACK_INVALID_CALLBACK; } // Send DELETE requests to linked devices OCStackResult resReq = OC_STACK_ERROR; // Check that we have to wait callback or not. resReq = SRPRemoveDevice(ctx, waitTimeForOwnedDeviceDiscovery, pTargetDev, resultCallback); if (OC_STACK_OK != resReq) { if (OC_STACK_CONTINUE == resReq) { OIC_LOG(DEBUG, TAG, "OCRemoveDevice : Revoked device has no linked device except PT."); } else { OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to invoke SRPRemoveDevice"); res = resReq; goto error; } } // Remove credential of revoked device from SVR database const OicSecCred_t *cred = NULL; cred = GetCredResourceData(&pTargetDev->doxm->deviceID); if (cred == NULL) { OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to get credential of remove device."); goto error; } res = RemoveCredential(&cred->subject); if (res != OC_STACK_RESOURCE_DELETED) { OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to remove credential."); goto error; } /** * Change the device status as stale status. * If all request are successed, this device information will be deleted. */ res = PDMSetDeviceStale(&pTargetDev->doxm->deviceID); if (res != OC_STACK_OK) { OIC_LOG(ERROR, TAG, "OCRemoveDevice : Failed to set device status as stale"); goto error; } // TODO: We need to add new mechanism to clean up the stale state of the device. res = resReq; //Close the DTLS session of the removed device. CAEndpoint_t* endpoint = (CAEndpoint_t *)&pTargetDev->endpoint; endpoint->port = pTargetDev->securePort; CAResult_t caResult = CACloseDtlsSession(endpoint); if(CA_STATUS_OK != caResult) { OIC_LOG_V(WARNING, TAG, "OCRemoveDevice : Failed to close DTLS session : %d", caResult); } /** * If there is no linked device, PM does not send any request. * So we should directly invoke the result callback to inform the result of OCRemoveDevice. */ if(OC_STACK_CONTINUE == res) { if(resultCallback) { resultCallback(ctx, 0, NULL, false); } res = OC_STACK_OK; } error: OIC_LOG(INFO, TAG, "OUT OCRemoveDevice"); return res; }
TEST(SRPRemoveDeviceTest, NullResultCallback) { unsigned short waitTime = 10; OCProvisionDev_t dev1; EXPECT_EQ(OC_STACK_INVALID_PARAM, SRPRemoveDevice(NULL, waitTime, &dev1, NULL)); }
TEST(SRPRemoveDeviceTest, NullTargetDevice) { unsigned short waitTime = 10 ; EXPECT_EQ(OC_STACK_INVALID_PARAM, SRPRemoveDevice(NULL, waitTime, NULL, provisioningCB)); }
TEST(SRPRemoveDeviceTest, ZeroWaitTime) { OCProvisionDev_t dev1; EXPECT_EQ(OC_STACK_INVALID_PARAM, SRPRemoveDevice(NULL, 0, &dev1, NULL)); }