diff --git a/interfaces/native/include/sensor_agent.h b/interfaces/native/include/sensor_agent.h index ead362c940d06fbf0c0bbac8ae409765c13a1933..9ca013e857c876b7655cd273418885b297a6bf22 100755 --- a/interfaces/native/include/sensor_agent.h +++ b/interfaces/native/include/sensor_agent.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -127,6 +127,53 @@ int32_t DeactivateSensor(int32_t sensorTypeId, const SensorUser *user); * @since 5 */ int32_t SetMode(int32_t sensorTypeId, const SensorUser *user, int32_t mode); +/** + * @brief 休眠一个进程订阅的所有传感器 + * + * @param pid 进程号 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t SuspendSensors(int32_t pid); +/** + * @brief 唤醒一个进程订阅的所有传感器 + * + * @param pid 进程号 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t ResumeSensors(int32_t pid); +/** + * @brief 查询一个进程打开的所有传感器的信息 + * + * @param pid 进程号 + * @param sensorActiveInfos 返回进程打开的所有传感器信息 + * @param count 返回进程打开的传感器数量 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t GetSensorActiveInfos(int32_t pid, SensorActiveInfo **sensorActiveInfos, int32_t *count); +/** + * @brief 注册传感器打开信息上报函数 + * + * @param callback 回调函数 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t Register(SensorActiveInfoCB callback); +/** + * @brief 取消注册传感器打开信息上报函数 + * + * @param callback 回调函数 + * @return 返回0表示成功,否则表示失败 + * + * @since 10 + */ +int32_t Unregister(SensorActiveInfoCB callback); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/native/src/sensor_agent.cpp b/interfaces/native/src/sensor_agent.cpp index ccae1ff7ad4a70a9a3263e0613f6de74f845fc1e..c81a7b01ae5f1680b595680746fe0695bc125adc 100755 --- a/interfaces/native/src/sensor_agent.cpp +++ b/interfaces/native/src/sensor_agent.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -146,4 +146,81 @@ int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode) return SERVICE_EXCEPTION; } return proxy->SetMode(sensorId, user, mode); +} + +int32_t SuspendSensors(int32_t pid) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("Proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->SuspendSensors(pid); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Suspend sensors failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t ResumeSensors(int32_t pid) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("Proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->ResumeSensors(pid); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Resume sensors failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t GetSensorActiveInfos(int32_t pid, SensorActiveInfo **sensorActiveInfos, int32_t *count) +{ + CHKPR(sensorActiveInfos, OHOS::Sensors::ERROR); + CHKPR(count, OHOS::Sensors::ERROR); + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("Proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->GetSensorActiveInfos(pid, sensorActiveInfos, count); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Get sensor active infos failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t Register(SensorActiveInfoCB callback) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->Register(callback); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Register sensor active Info callback failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; +} + +int32_t Unregister(SensorActiveInfoCB callback) +{ + const SensorAgentProxy *proxy = GetInstance(); + if (proxy == nullptr) { + SEN_HILOGE("proxy is nullptr"); + return SERVICE_EXCEPTION; + } + int32_t ret = proxy->Unregister(callback); + if (ret != OHOS::ERR_OK) { + SEN_HILOGE("Unregister sensor active info callback failed, ret:%{public}d", ret); + return NormalizeErrCode(ret); + } + return ret; } \ No newline at end of file diff --git a/interfaces/native/test/BUILD.gn b/interfaces/native/test/BUILD.gn index 2bbfd149bd896f967c7ce65f5a05f03df1783738..7870ea003ff00725b90d03a904674a845fe1449b 100644 --- a/interfaces/native/test/BUILD.gn +++ b/interfaces/native/test/BUILD.gn @@ -70,10 +70,38 @@ ohos_unittest("SensorAlgorithmTest") { ] } +ohos_unittest("SensorPowerTest") { + module_out_path = "sensors/sensor/interfaces" + + sources = [ "unittest/sensor_power_test.cpp" ] + + include_dirs = [ + "$SUBSYSTEM_DIR/utils/common/include", + "$SUBSYSTEM_DIR/interfaces/native/include", + ] + + deps = [ + "$SUBSYSTEM_DIR/interfaces/native:sensor_ndk_target", + "$SUBSYSTEM_DIR/utils/common:libsensor_utils", + "//third_party/googletest:gmock_main", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] +} + group("unittest") { testonly = true deps = [ ":SensorAgentTest", ":SensorAlgorithmTest", + ":SensorPowerTest", ] } diff --git a/interfaces/native/test/unittest/sensor_power_test.cpp b/interfaces/native/test/unittest/sensor_power_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7ecc777d9126cd8f41f22134744ecd19836f1905 --- /dev/null +++ b/interfaces/native/test/unittest/sensor_power_test.cpp @@ -0,0 +1,297 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" + +#include "sensor_agent.h" +#include "sensors_errors.h" + +namespace OHOS { +namespace Sensors { +using namespace testing::ext; +using namespace OHOS::HiviewDFX; +using namespace Security::AccessToken; +using Security::AccessToken::AccessTokenID; + +namespace { +constexpr HiLogLabel LABEL = { LOG_CORE, OHOS::Sensors::SENSOR_LOG_DOMAIN, "SensorPowerTest" }; +constexpr int32_t sensorId { 1 }; +constexpr int32_t invalidValue { -1 }; +} // namespace + +class SensorPowerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +int32_t process_pid = 0; + +void SensorPowerTest::SetUpTestCase() +{ + const char **perms = new const char *[1]; + perms[0] = "ohos.permission.ACCELEROMETER"; + TokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 1, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .processName = "SensorPowerTest", + .aplStr = "system_core", + }; + uint64_t tokenId = GetAccessTokenId(&infoInstance); + ASSERT_EQ(SetSelfTokenID(tokenId), 0); + AccessTokenKit::ReloadNativeTokenInfo(); + delete[] perms; + + process_pid = getpid(); + SEN_HILOGI("Current process pid is %{public}d", process_pid); + ASSERT_NE(process_pid, 0); +} + +void SensorPowerTest::TearDownTestCase() {} + +void SensorPowerTest::SetUp() {} + +void SensorPowerTest::TearDown() {} + +void SensorDataCallbackImpl(SensorEvent *event) +{ + if (event == nullptr) { + SEN_HILOGE("SensorEvent is null"); + return; + } + float *sensorData = (float *)event[0].data; + SEN_HILOGI("SensorId:%{public}d, version:%{public}d,dataLen:%{public}d,data:%{public}f", + event[0].sensorTypeId, event[0].version, event[0].dataLen, *(sensorData)); +} + +void SensorActiveInfoCBImpl(SensorActiveInfo &sensorActiveInfo) +{ + SEN_HILOGI("pid:%{public}d, sensorId:%{public}d, samplingPeriodNs:%{public}" PRId64 ", " + "maxReportDelayNs:%{public}" PRId64 "", sensorActiveInfo.pid, sensorActiveInfo.sensorId, + sensorActiveInfo.samplingPeriodNs, sensorActiveInfo.maxReportDelayNs); +} + +void SensorActiveInfoCBImpl2(SensorActiveInfo &sensorActiveInfo) +{ + SEN_HILOGI("pid:%{public}d, sensorId:%{public}d, samplingPeriodNs:%{public}" PRId64 ", " + "maxReportDelayNs:%{public}" PRId64 "", sensorActiveInfo.pid, sensorActiveInfo.sensorId, + sensorActiveInfo.samplingPeriodNs, sensorActiveInfo.maxReportDelayNs); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_001, TestSize.Level1) +{ + // 休眠接口异常用例 + SEN_HILOGI("SensorPowerTest_001 in"); + int32_t ret = SuspendSensors(invalidValue); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_002, TestSize.Level1) +{ + // 恢复接口异常用例 + SEN_HILOGI("SensorPowerTest_002 in"); + int32_t ret = ResumeSensors(invalidValue); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_003, TestSize.Level1) +{ + // 查询接口异常用例 + SEN_HILOGI("SensorPowerTest_003 in"); + SensorActiveInfo *sensorActiveInfos {nullptr}; + int32_t count { 0 }; + int32_t ret = GetSensorActiveInfos(invalidValue, &sensorActiveInfos, &count); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_004, TestSize.Level1) +{ + // 注册接口异常用例 + SEN_HILOGI("SensorPowerTest_004 in"); + SensorActiveInfoCB callback = nullptr; + int32_t ret = Register(callback); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_005, TestSize.Level1) +{ + // 取消注册接口异常用例 + SEN_HILOGI("SensorPowerTest_005 in"); + SensorActiveInfoCB callback = nullptr; + int32_t ret = Unregister(callback); + ASSERT_NE(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_006, TestSize.Level1) +{ + // 场景用例1,订阅并打开ACC传感器》休眠进程的传感器》恢复进程的传感器》关闭并取消订阅ACC传感器 + SEN_HILOGI("SensorPowerTest_006 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 0); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = SuspendSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = ResumeSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_007, TestSize.Level1) +{ + // 场景用例2,订阅并打开ACC传感器》查询传感器打开数据》休眠进程的传感器》恢复进程的传感器》关闭并取消订阅ACC传感器 + SEN_HILOGI("SensorPowerTest_007 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + + int32_t ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 0); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + SensorActiveInfo *sensorActiveInfos {nullptr}; + int32_t count { 0 }; + ret = GetSensorActiveInfos(process_pid, &sensorActiveInfos, &count); + for (int32_t i = 0; i < count; ++i) { + SensorActiveInfo *curSensorActiveInfo = sensorActiveInfos + i; + SEN_HILOGI("pid:%{public}d, sensorId:%{public}d, samplingPeriodNs:%{public}" PRId64 ", " + "maxReportDelayNs:%{public}" PRId64 "", curSensorActiveInfo->pid, curSensorActiveInfo->sensorId, + curSensorActiveInfo->samplingPeriodNs, curSensorActiveInfo->maxReportDelayNs); + } + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SuspendSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = ResumeSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_008, TestSize.Level1) +{ + // 场景用例3,注册回调函数》订阅并打开ACC传感器》休眠进程的传感器》恢复进程的传感器》关闭并取消订阅ACC传感器》取消注册回调函数 + SEN_HILOGI("SensorPowerTest_008 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + SensorActiveInfoCB callback = SensorActiveInfoCBImpl; + + int32_t ret = Register(callback); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 0); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = SuspendSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = ResumeSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = Unregister(callback); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} + +HWTEST_F(SensorPowerTest, SensorPowerTest_009, TestSize.Level1) +{ + // 场景用例4,注册回调函数1》注册回调函数2》订阅ACC传感器》休眠进程的传感器》恢复进程的传感器》取消订阅ACC传感器》取消注册回调函数1》取消注册回调函数2 + SEN_HILOGI("SensorPowerTest_009 in"); + SensorUser user; + user.callback = SensorDataCallbackImpl; + SensorActiveInfoCB callback = SensorActiveInfoCBImpl; + SensorActiveInfoCB callback2 = SensorActiveInfoCBImpl2; + + int32_t ret = Register(callback); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = Register(callback2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = SubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = SetBatch(sensorId, &user, 100000000, 0); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = ActivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = SuspendSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + + ret = ResumeSensors(process_pid); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + std::this_thread::sleep_for(std::chrono::milliseconds(500)); + + ret = DeactivateSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = UnsubscribeSensor(sensorId, &user); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + + ret = Unregister(callback); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); + ret = Unregister(callback2); + ASSERT_EQ(ret, OHOS::Sensors::SUCCESS); +} +} // namespace Sensors +} // namespace OHOS \ No newline at end of file