From bbb8c03b5928fe53c44465af35670c36768f73fe Mon Sep 17 00:00:00 2001 From: zhouge Date: Sat, 13 May 2023 10:32:03 +0000 Subject: [PATCH 1/4] add driver_extension_controller Signed-off-by: zhouge --- .../driver_extension_controller/BUILD.gn | 58 +++++++++++++++++ .../include/driver_extension_controller.h | 29 +++++++++ .../src/driver_extension_controller.cpp | 65 +++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 framework/driver_extension_controller/BUILD.gn create mode 100644 framework/driver_extension_controller/include/driver_extension_controller.h create mode 100644 framework/driver_extension_controller/src/driver_extension_controller.cpp diff --git a/framework/driver_extension_controller/BUILD.gn b/framework/driver_extension_controller/BUILD.gn new file mode 100644 index 0000000..663faef --- /dev/null +++ b/framework/driver_extension_controller/BUILD.gn @@ -0,0 +1,58 @@ +# 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. + +import("//build/ohos.gni") # 导入编译模板 +import("//drivers/external_device_manager/extdevmgr.gni") + +config("driver_extension_controller_public_config") { + include_dirs = [ + "include", + ] +} +config("driver_extension_controller_private_config") { + include_dirs = [ + "//third_party/jsoncpp/include/json", + ] +} +ohos_shared_library("driver_extension_controller") { # 可执行模块 + sources = [ # 模块源码 + "src/driver_extension_controller.cpp", + ] + include_dirs = [] # 模块依赖头文件目录 + cflags = [] + cflags_c = [] + cflags_cc = [] + ldflags = [] + configs = [ + ":driver_extension_controller_public_config", + ":driver_extension_controller_private_config", + "${part_path}:coverage_flags", + "${utils_path}:utils_config" + ] + public_configs = [ + ":driver_extension_controller_public_config", + ] + deps =[ + "//third_party/jsoncpp:jsoncpp", + ] # 部件内部依赖 + part_name = "drivers_external_device_manager" # 所属部件名称,必选 + install_enable = true # 是否默认安装(缺省默认不安装),可选 + external_deps = [ + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ability_runtime:ability_manager", + "ability_base:want" + ] +} + + diff --git a/framework/driver_extension_controller/include/driver_extension_controller.h b/framework/driver_extension_controller/include/driver_extension_controller.h new file mode 100644 index 0000000..f4b2eaf --- /dev/null +++ b/framework/driver_extension_controller/include/driver_extension_controller.h @@ -0,0 +1,29 @@ +/* + * 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. + */ + +#ifndef DRIVER_EXTENSION_CONTROLLER_H +#define DRIVER_EXTENSION_CONTROLLER_H +#include + +class DriverExtensionController { +public: + static int StartDriverExtension( + std::string baudleName, + std::string abilityName); + static int StopDriverExtension( + std::string bundleName, + std::string abilityName); +}; +#endif \ No newline at end of file diff --git a/framework/driver_extension_controller/src/driver_extension_controller.cpp b/framework/driver_extension_controller/src/driver_extension_controller.cpp new file mode 100644 index 0000000..33d4cc7 --- /dev/null +++ b/framework/driver_extension_controller/src/driver_extension_controller.cpp @@ -0,0 +1,65 @@ +/* + * 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 "driver_extension_controller.h" +#include "ability_manager_client.h" +#include "hilog_wrapper.h" +#define MODULE_USB_SERVICE +#define UEC_OK 0 +using namespace OHOS; + +int DriverExtensionController::StartDriverExtension( + std::string bundleName, + std::string abilityName) +{ + DEVMGR_LOGI("Begin to start DriverExtension, bundle:%{public}s, ability:%{public}s",bundleName.c_str(), abilityName.c_str()); + auto abmc = AAFwk::AbilityManagerClient::GetInstance(); + if (abmc == nullptr) { + DEVMGR_LOGE("Get AMC Instance failed"); + return -1; + } + AAFwk::Want want; + want.SetElementName(bundleName, abilityName); + + auto ret = abmc->StartExtensionAbility(want, nullptr); + if (ret != UEC_OK) { + DEVMGR_LOGE("StartExtensionAbility failed %{public}d", ret); + return ret; + } + DEVMGR_LOGI("StartExtensionAbility success"); + return 0; +} + + +int DriverExtensionController::StopDriverExtension( + std::string bundleName, + std::string abilityName) +{ + DEVMGR_LOGI("Begin to stop DriverExtension, bundle:%{public}s, ability:%{public}s",bundleName.c_str(), abilityName.c_str()); + auto abmc = AAFwk::AbilityManagerClient::GetInstance(); + if (abmc == nullptr) { + DEVMGR_LOGE("Get AMC Instance failed"); + return -1; + } + AAFwk::Want want; + want.SetElementName(bundleName, abilityName); + auto ret = abmc->StopExtensionAbility(want, nullptr); + if (ret != UEC_OK) { + DEVMGR_LOGE("StopExtensionAbility failed %{public}d", ret); + return ret; + } + DEVMGR_LOGI("StopExtensionAbility success"); + return 0; +} \ No newline at end of file -- Gitee From f94082ae126c14caa8253207e26ed3780d36790c Mon Sep 17 00:00:00 2001 From: zhouge Date: Sat, 13 May 2023 10:32:27 +0000 Subject: [PATCH 2/4] add driver_extension_controller ut Signed-off-by: zhouge --- .../src/driver_extension_controller_test.cpp | 102 ++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp diff --git a/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp b/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp new file mode 100644 index 0000000..397bcd85 --- /dev/null +++ b/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp @@ -0,0 +1,102 @@ +/* + * 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 "driver_extension_controller.h" +#include "iostream" +#include "hilog_wrapper.h" +#include "string_ex.h" +#include "iservice_registry.h" +#include "accesstoken_kit.h" +#include "token_setproc.h" +#include "nativetoken_kit.h" +#include "gtest/gtest.h" +#define USB_SYSTEM_ABILITY_ID 180 +using namespace OHOS; +using namespace std; +using namespace testing::ext; +class DrvExtCtrlTest: public testing::Test { +public: + void SetUp() override { + cout << "DrvExtCtrlTest SetUp" << endl; + } + void TearDown() override { + cout << "DrvExtCtrlTest TearDown" << endl; + } +}; + + +#define TEST_BUNDLE_NAME "com.huawei.ohos.screenshot" + +#define TEST_ABILITY_NAME "com.huawei.ohos.screenshot.ServiceExtAbility" +#define TEST_ABILITY_NAME_ERR "com.huawei.ohos.screenshot.XXX" + +using namespace OHOS::Security::AccessToken; + +TokenInfoParams g_normalInfoInstance = { + .dcapsNum = 0, + .permsNum = 0, + .aclsNum = 0, + .dcaps = nullptr, + .perms = nullptr, + .acls = nullptr, + .processName = "usb_manager", + .aplStr = "normal", +}; + +TokenInfoParams g_sysInfoInstance = { + .dcapsNum = 0, + .permsNum = 0, + .aclsNum = 0, + .dcaps = nullptr, + .perms = nullptr, + .acls = nullptr, + .processName = "usb_manager", + .aplStr = "system_basic", +}; + +class AccessTokenTest +{ +public: + static void SetTestCaseNative (TokenInfoParams *infoInstance) + { + uint64_t tokenId = GetAccessTokenId(infoInstance); + int ret = SetSelfTokenID(tokenId); + if (ret == 0) { + DEVMGR_LOGI("SetSelfTokenID success"); + } else { + DEVMGR_LOGE("SetSelfTokenID fail"); + } + AccessTokenKit::ReloadNativeTokenInfo(); + } +}; +HWTEST_F(DrvExtCtrlTest, DrvExtCtrlWithSATest, TestSize.Level1) +{ + AccessTokenTest::SetTestCaseNative(&g_sysInfoInstance); + int ret = 0; + ret = DriverExtensionController::StartDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ASSERT_EQ(ret, 0); + ret = DriverExtensionController::StopDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME); + ASSERT_EQ(ret, 0); +} + +HWTEST_F(DrvExtCtrlTest, DrvExtCtrlWithoutSATest, TestSize.Level1) +{ + AccessTokenTest::SetTestCaseNative(&g_normalInfoInstance); + int ret = 0; + ret = DriverExtensionController::StartDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME_ERR); + ASSERT_NE(ret, 0); + ret = DriverExtensionController::StopDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME_ERR); + ASSERT_NE(ret, 0); +} -- Gitee From 72b578a43b7cdbaab378dee092c10d7cf2c66175 Mon Sep 17 00:00:00 2001 From: zhouge Date: Thu, 18 May 2023 08:02:13 +0000 Subject: [PATCH 3/4] change dir Signed-off-by: zhouge --- .../driver_extension_controller/BUILD.gn | 58 ------------------- .../driver_extension_controller.h | 5 +- .../driver_extension_controller.cpp | 12 ++-- 3 files changed, 11 insertions(+), 64 deletions(-) delete mode 100644 framework/driver_extension_controller/BUILD.gn rename {framework/driver_extension_controller/include => services/native/driver_extension_manager/include/driver_controller}/driver_extension_controller.h (95%) rename {framework/driver_extension_controller/src => services/native/driver_extension_manager/src/driver_controller}/driver_extension_controller.cpp (90%) diff --git a/framework/driver_extension_controller/BUILD.gn b/framework/driver_extension_controller/BUILD.gn deleted file mode 100644 index 663faef..0000000 --- a/framework/driver_extension_controller/BUILD.gn +++ /dev/null @@ -1,58 +0,0 @@ -# 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. - -import("//build/ohos.gni") # 导入编译模板 -import("//drivers/external_device_manager/extdevmgr.gni") - -config("driver_extension_controller_public_config") { - include_dirs = [ - "include", - ] -} -config("driver_extension_controller_private_config") { - include_dirs = [ - "//third_party/jsoncpp/include/json", - ] -} -ohos_shared_library("driver_extension_controller") { # 可执行模块 - sources = [ # 模块源码 - "src/driver_extension_controller.cpp", - ] - include_dirs = [] # 模块依赖头文件目录 - cflags = [] - cflags_c = [] - cflags_cc = [] - ldflags = [] - configs = [ - ":driver_extension_controller_public_config", - ":driver_extension_controller_private_config", - "${part_path}:coverage_flags", - "${utils_path}:utils_config" - ] - public_configs = [ - ":driver_extension_controller_public_config", - ] - deps =[ - "//third_party/jsoncpp:jsoncpp", - ] # 部件内部依赖 - part_name = "drivers_external_device_manager" # 所属部件名称,必选 - install_enable = true # 是否默认安装(缺省默认不安装),可选 - external_deps = [ - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - "ability_runtime:ability_manager", - "ability_base:want" - ] -} - - diff --git a/framework/driver_extension_controller/include/driver_extension_controller.h b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h similarity index 95% rename from framework/driver_extension_controller/include/driver_extension_controller.h rename to services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h index f4b2eaf..e8b61f1 100644 --- a/framework/driver_extension_controller/include/driver_extension_controller.h +++ b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h @@ -16,7 +16,8 @@ #ifndef DRIVER_EXTENSION_CONTROLLER_H #define DRIVER_EXTENSION_CONTROLLER_H #include - +namespace OHOS { +namespace ExtDevMgr { class DriverExtensionController { public: static int StartDriverExtension( @@ -26,4 +27,6 @@ public: std::string bundleName, std::string abilityName); }; +} +} #endif \ No newline at end of file diff --git a/framework/driver_extension_controller/src/driver_extension_controller.cpp b/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp similarity index 90% rename from framework/driver_extension_controller/src/driver_extension_controller.cpp rename to services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp index 33d4cc7..7b73fe9 100644 --- a/framework/driver_extension_controller/src/driver_extension_controller.cpp +++ b/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp @@ -13,18 +13,19 @@ * limitations under the License. */ -#include "driver_extension_controller.h" #include "ability_manager_client.h" #include "hilog_wrapper.h" -#define MODULE_USB_SERVICE -#define UEC_OK 0 +#include "driver_extension_controller.h" +const uint32_t UEC_OK = 0 using namespace OHOS; +using namespace OHOS::ExtDevMgr; int DriverExtensionController::StartDriverExtension( std::string bundleName, std::string abilityName) { - DEVMGR_LOGI("Begin to start DriverExtension, bundle:%{public}s, ability:%{public}s",bundleName.c_str(), abilityName.c_str()); + DEVMGR_LOGI("Begin to start DriverExtension, bundle:%{public}s, ability:%{public}s", \ + bundleName.c_str(), abilityName.c_str()); auto abmc = AAFwk::AbilityManagerClient::GetInstance(); if (abmc == nullptr) { DEVMGR_LOGE("Get AMC Instance failed"); @@ -47,7 +48,8 @@ int DriverExtensionController::StopDriverExtension( std::string bundleName, std::string abilityName) { - DEVMGR_LOGI("Begin to stop DriverExtension, bundle:%{public}s, ability:%{public}s",bundleName.c_str(), abilityName.c_str()); + DEVMGR_LOGI("Begin to stop DriverExtension, bundle:%{public}s, ability:%{public}s", \ + bundleName.c_str(), abilityName.c_str()); auto abmc = AAFwk::AbilityManagerClient::GetInstance(); if (abmc == nullptr) { DEVMGR_LOGE("Get AMC Instance failed"); -- Gitee From 02f2aba22e8f2b4a8b4c436311d1a48e8b64862a Mon Sep 17 00:00:00 2001 From: zhouge Date: Thu, 18 May 2023 08:33:39 +0000 Subject: [PATCH 4/4] clean code check modify Signed-off-by: zhouge --- .../driver_extension_controller.h | 6 ++-- .../driver_extension_controller.cpp | 20 +++++------ .../src/driver_extension_controller_test.cpp | 36 ++++++++++--------- 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h index e8b61f1..8862658 100644 --- a/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h +++ b/services/native/driver_extension_manager/include/driver_controller/driver_extension_controller.h @@ -17,13 +17,13 @@ #define DRIVER_EXTENSION_CONTROLLER_H #include namespace OHOS { -namespace ExtDevMgr { +namespace ExternalDeviceManager { class DriverExtensionController { public: - static int StartDriverExtension( + static uint32_t StartDriverExtension( std::string baudleName, std::string abilityName); - static int StopDriverExtension( + static uint32_t StopDriverExtension( std::string bundleName, std::string abilityName); }; diff --git a/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp b/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp index 7b73fe9..63bad62 100644 --- a/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp +++ b/services/native/driver_extension_manager/src/driver_controller/driver_extension_controller.cpp @@ -16,19 +16,19 @@ #include "ability_manager_client.h" #include "hilog_wrapper.h" #include "driver_extension_controller.h" -const uint32_t UEC_OK = 0 using namespace OHOS; -using namespace OHOS::ExtDevMgr; +using namespace OHOS::ExternalDeviceManager; +static const uint32_t UEC_OK = 0 int DriverExtensionController::StartDriverExtension( std::string bundleName, std::string abilityName) { - DEVMGR_LOGI("Begin to start DriverExtension, bundle:%{public}s, ability:%{public}s", \ + EDM_LOGI("Begin to start DriverExtension, bundle:%{public}s, ability:%{public}s", \ bundleName.c_str(), abilityName.c_str()); auto abmc = AAFwk::AbilityManagerClient::GetInstance(); if (abmc == nullptr) { - DEVMGR_LOGE("Get AMC Instance failed"); + EDM_LOGE("Get AMC Instance failed"); return -1; } AAFwk::Want want; @@ -36,10 +36,10 @@ int DriverExtensionController::StartDriverExtension( auto ret = abmc->StartExtensionAbility(want, nullptr); if (ret != UEC_OK) { - DEVMGR_LOGE("StartExtensionAbility failed %{public}d", ret); + EDM_LOGE("StartExtensionAbility failed %{public}d", ret); return ret; } - DEVMGR_LOGI("StartExtensionAbility success"); + EDM_LOGI("StartExtensionAbility success"); return 0; } @@ -48,20 +48,20 @@ int DriverExtensionController::StopDriverExtension( std::string bundleName, std::string abilityName) { - DEVMGR_LOGI("Begin to stop DriverExtension, bundle:%{public}s, ability:%{public}s", \ + EDM_LOGI("Begin to stop DriverExtension, bundle:%{public}s, ability:%{public}s", \ bundleName.c_str(), abilityName.c_str()); auto abmc = AAFwk::AbilityManagerClient::GetInstance(); if (abmc == nullptr) { - DEVMGR_LOGE("Get AMC Instance failed"); + EDM_LOGE("Get AMC Instance failed"); return -1; } AAFwk::Want want; want.SetElementName(bundleName, abilityName); auto ret = abmc->StopExtensionAbility(want, nullptr); if (ret != UEC_OK) { - DEVMGR_LOGE("StopExtensionAbility failed %{public}d", ret); + EDM_LOGE("StopExtensionAbility failed %{public}d", ret); return ret; } - DEVMGR_LOGI("StopExtensionAbility success"); + EDM_LOGI("StopExtensionAbility success"); return 0; } \ No newline at end of file diff --git a/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp b/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp index 397bcd85..750d4fa 100644 --- a/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp +++ b/test/unittest/driver_extension_controller_test/src/driver_extension_controller_test.cpp @@ -13,34 +13,36 @@ * limitations under the License. */ -#include "driver_extension_controller.h" +#include "gtest/gtest.h" #include "iostream" -#include "hilog_wrapper.h" #include "string_ex.h" #include "iservice_registry.h" #include "accesstoken_kit.h" #include "token_setproc.h" #include "nativetoken_kit.h" -#include "gtest/gtest.h" -#define USB_SYSTEM_ABILITY_ID 180 +#include "hilog_wrapper.h" +#include "driver_extension_controller.h" + +namespace OHOS { +namespace ExternalDeviceManager { using namespace OHOS; using namespace std; using namespace testing::ext; -class DrvExtCtrlTest: public testing::Test { +class DrvExtCtrlTest : public testing::Test { public: - void SetUp() override { + void SetUp() override + { cout << "DrvExtCtrlTest SetUp" << endl; } - void TearDown() override { + void TearDown() override + { cout << "DrvExtCtrlTest TearDown" << endl; } }; - -#define TEST_BUNDLE_NAME "com.huawei.ohos.screenshot" - -#define TEST_ABILITY_NAME "com.huawei.ohos.screenshot.ServiceExtAbility" -#define TEST_ABILITY_NAME_ERR "com.huawei.ohos.screenshot.XXX" +const char *TEST_BUNDLE_NAME "com.usb.right.MainAbility" +const char *TEST_ABILITY_NAME "UsbServiceExtAbility" +const char *TEST_ABILITY_NAME_ERR "XXX" using namespace OHOS::Security::AccessToken; @@ -66,21 +68,21 @@ TokenInfoParams g_sysInfoInstance = { .aplStr = "system_basic", }; -class AccessTokenTest -{ +class AccessTokenTest { public: static void SetTestCaseNative (TokenInfoParams *infoInstance) { uint64_t tokenId = GetAccessTokenId(infoInstance); int ret = SetSelfTokenID(tokenId); if (ret == 0) { - DEVMGR_LOGI("SetSelfTokenID success"); + EDM_LOGI("SetSelfTokenID success"); } else { - DEVMGR_LOGE("SetSelfTokenID fail"); + EDM_LOGE("SetSelfTokenID fail"); } AccessTokenKit::ReloadNativeTokenInfo(); } }; + HWTEST_F(DrvExtCtrlTest, DrvExtCtrlWithSATest, TestSize.Level1) { AccessTokenTest::SetTestCaseNative(&g_sysInfoInstance); @@ -100,3 +102,5 @@ HWTEST_F(DrvExtCtrlTest, DrvExtCtrlWithoutSATest, TestSize.Level1) ret = DriverExtensionController::StopDriverExtension(TEST_BUNDLE_NAME, TEST_ABILITY_NAME_ERR); ASSERT_NE(ret, 0); } +} +} \ No newline at end of file -- Gitee