diff --git a/services/devicemanagerservice/include/device_manager_service.h b/services/devicemanagerservice/include/device_manager_service.h index 31278f7b74b45d614c80938b18fa74a21ea46a11..7f49ab1e153aca9974d8fe32705d29b0ad34f96e 100644 --- a/services/devicemanagerservice/include/device_manager_service.h +++ b/services/devicemanagerservice/include/device_manager_service.h @@ -22,6 +22,8 @@ #include "dm_ability_manager.h" #include "dm_auth_manager.h" #include "dm_device_info.h" +#include "dm_dfx_constants.h" +#include "dm_hidumper.h" #include "dm_device_info_manager.h" #include "dm_device_state_manager.h" #include "dm_discovery_manager.h" @@ -137,6 +139,13 @@ public: */ bool IsServiceInitialized(); + /** + * @tc.name: DeviceManagerService::DmHiDumper + * @tc.desc: HiDumper of the Device Manager Service + * @tc.type: FUNC + */ + int32_t DmHiDumper(const std::vector& args, std::string &result); + private: DeviceManagerService() = default; bool intFlag_ = false; diff --git a/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h b/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h index 2fa555d06b3730f968f9dcb77c9f312cd48dd59e..5f1acdd00ea51ad5c428191a589a7a5e65732164 100644 --- a/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h +++ b/services/devicemanagerservice/include/ipc/standard/ipc_server_stub.h @@ -114,6 +114,13 @@ public: */ const sptr GetDmListener(std::string pkgName) const; + /** + * @tc.name: IpcServerStub::Dump + * @tc.desc: Dump of the Device Manager Service + * @tc.type: FUNC + */ + int32_t Dump(int32_t fd, const std::vector& args) override; + private: IpcServerStub(); ~IpcServerStub() = default; diff --git a/services/devicemanagerservice/src/device_manager_service.cpp b/services/devicemanagerservice/src/device_manager_service.cpp index 0c97d2342277ae3d2c44217948980873cd570f75..53ce2573708e62f614d78ff92029e604f39fc31b 100644 --- a/services/devicemanagerservice/src/device_manager_service.cpp +++ b/services/devicemanagerservice/src/device_manager_service.cpp @@ -335,5 +335,23 @@ bool DeviceManagerService::IsServiceInitialized() { return intFlag_; } + +int32_t DeviceManagerService::DmHiDumper(const std::vector& args, std::string &result) +{ + std::vector dumpflag; + HidumpHelper::GetInstance().GetArgsType(args, dumpflag); + for (unsigned int i = 0; i < dumpflag.size(); i++) { + if (dumpflag[i] == HidumperFlag::HIDUMPER_GET_TRUSTED_LIST) { + std::vector deviceList; + std::string extra; + GetTrustedDeviceList(DM_PKG_NAME, extra, deviceList); + for (unsigned int j = 0; j < deviceList.size(); j++) { + HidumpHelper::GetInstance().SetNodeInfo(deviceList[j]); + } + } + } + HidumpHelper::GetInstance().HiDump(args, result); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS diff --git a/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp b/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp index 9d247b899033bfac9949e0df5f35cd9c7de23d77..12a7180f5f7292491053a4eeacf2a6e6f258ecff 100644 --- a/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp +++ b/services/devicemanagerservice/src/ipc/standard/ipc_server_stub.cpp @@ -22,6 +22,7 @@ #include "device_manager_service.h" #include "dm_constants.h" #include "dm_log.h" +#include "dm_hidumper.h" #include "if_system_ability_manager.h" #include "ipc_cmd_register.h" #include "ipc_skeleton.h" @@ -195,6 +196,26 @@ const sptr IpcServerStub::GetDmListener(std::string pkgName) co return dmListener; } +int32_t IpcServerStub::Dump(int32_t fd, const std::vector& args) +{ + LOGI("DistributedHardwareService Dump."); + std::vector argsStr {}; + for (auto item : args) { + argsStr.emplace_back(Str16ToStr8(item)); + } + std::string result(""); + int ret = DeviceManagerService::GetInstance().DmHiDumper(argsStr, result); + if (ret != DM_OK) { + LOGE("Dump error, ret = %d", ret); + } + ret = dprintf(fd, "%s\n", result.c_str()); + if (ret < 0) { + LOGE("HiDumper dprintf error"); + ret = ERR_DM_FAILED; + } + return ret; +} + void AppDeathRecipient::OnRemoteDied(const wptr &remote) { LOGW("AppDeathRecipient: OnRemoteDied"); diff --git a/test/unittest/UTTest_ipc_server_stub.cpp b/test/unittest/UTTest_ipc_server_stub.cpp index 97082dc2d61c5e21bbc0467d71a22a586854b968..0da5df5964f887b3bc555909a46df54aa4a217bc 100644 --- a/test/unittest/UTTest_ipc_server_stub.cpp +++ b/test/unittest/UTTest_ipc_server_stub.cpp @@ -541,6 +541,79 @@ HWTEST_F(IpcServerStubTest, GetDmListener_005, testing::ext::TestSize.Level0) // 3. check ret is nullptr ASSERT_EQ(ret, nullptr); } + + +/** + * @tc.name: dump + * @tc.desc: 1. Set fd is 4802 + * 2. Set str is help + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, Dump_001, testing::ext::TestSize.Level0) +{ + // 1. Set fd is 4802 and str is getTrustlist + int32_t fd = 4802; + int result = 0; + std::string str = "-help"; + std::u16string tmpvector = std::wstring_convert, char16_t>{}.from_bytes(str); + std::vector vector1; + vector1.push_back(tmpvector); + + // 2. Call IpcServerStub Dump with param + result = IpcServerStub::GetInstance().Dump(fd, vector1); + // 3. check ret is ERR_DM_POINT_NULL + ASSERT_EQ(result, DM_OK); +} + +/** + * @tc.name: dump + * @tc.desc: 1. Set fd is 4802 + * 2. Set str is getTrustlist + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, Dump_002, testing::ext::TestSize.Level0) +{ + // 1. Set fd is 4802 and str is getTrustlist + int32_t fd = 4802; + int result = 0; + std::string str = "-getTrustlist"; + std::u16string tmpvector = std::wstring_convert, char16_t>{}.from_bytes(str); + std::vector vector1; + vector1.push_back(tmpvector); + + // 2. Call IpcServerStub Dump with param + result = IpcServerStub::GetInstance().Dump(fd, vector1); + // 3. check ret is ERR_DM_POINT_NULL + ASSERT_EQ(result, DM_OK); +} + +/** + * @tc.name: dump + * @tc.desc: 1. Set fd is 4802 + * 2. Set str is ill + * 3. check ret is DM_OK + * @tc.type: FUNC + * @tc.require: AR000GHSJK + */ +HWTEST_F(IpcServerStubTest, Dump_003, testing::ext::TestSize.Level0) +{ + // 1. Set fd is 4802 and str is getTrustlist + int32_t fd = 4802; + int result = 0; + std::string str = "-121312312321"; + std::u16string tmpvector = std::wstring_convert, char16_t>{}.from_bytes(str); + std::vector vector1; + vector1.push_back(tmpvector); + + // 2. Call IpcServerStub Dump with param + result = IpcServerStub::GetInstance().Dump(fd, vector1); + // 3. check ret is DM_OK + ASSERT_EQ(result, DM_OK); +} } // namespace } // namespace DistributedHardware } // namespace OHOS diff --git a/utils/BUILD.gn b/utils/BUILD.gn index c411fc3bbedf9392334482048c0b674e453074e3..d090755d4889bbb9654925514a0b7573b1fe0507 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -42,6 +42,7 @@ if (defined(ohos_lite)) { ] sources = [ + "${utils_path}/src/dfx/lite/dm_hidumper.cpp", "${utils_path}/src/dfx/lite/dm_hisysevent.cpp", "${utils_path}/src/dfx/lite/dm_hitrace.cpp", "${utils_path}/src/dm_anonymous.cpp", @@ -98,6 +99,7 @@ if (defined(ohos_lite)) { "//foundation/distributedschedule/samgr_lite/interfaces/kits/samgr", ] sources = [ + "src/dfx/lite/dm_hidumper.cpp", "src/dfx/lite/dm_hisysevent.cpp", "src/dfx/lite/dm_hitrace.cpp", "src/dm_anonymous.cpp", @@ -137,6 +139,7 @@ if (defined(ohos_lite)) { "${common_path}/include", "${common_path}/include/ipc", "${common_path}/include/ipc/model", + "${innerkits_path}/native_cpp/include", "include/permission/standard", "//third_party/mbedtls/include/mbedtls", ] @@ -144,6 +147,7 @@ if (defined(ohos_lite)) { ohos_shared_library("devicemanagerutils") { sources = [ + "src/dfx/standard/dm_hidumper.cpp", "src/dfx/standard/dm_hisysevent.cpp", "src/dfx/standard/dm_hitrace.cpp", "src/dm_anonymous.cpp", diff --git a/utils/include/dfx/dm_dfx_constants.h b/utils/include/dfx/dm_dfx_constants.h index 3d4f17687290f1da44b82f864b410888cfb4e7a5..9d761ef4a04a1c8ce4cb81498053378f1510c20b 100644 --- a/utils/include/dfx/dm_dfx_constants.h +++ b/utils/include/dfx/dm_dfx_constants.h @@ -16,6 +16,7 @@ #define OHOS_DM_DFX_CONSTANTS_H #include +#include namespace OHOS { namespace DistributedHardware { @@ -64,6 +65,22 @@ const std::string DM_HITRACE_AUTH_TO_CONSULT = "DM_HITRACE_AUTH_TO_CONSULT"; const std::string DM_HITRACE_AUTH_TO_OPPEN_SESSION = "DM_HITRACE_AUTH_TO_OPPEN_SESSION"; const std::string DM_HITRACE_DEVICE_ONLINE = "DM_HITRACE_DEVICE_ONLINE"; const std::string DM_HITRACE_INIT = "DM_HITRACE_INIT"; + +// HiDumper Flag +enum class HidumperFlag { + HIDUMPER_UNKNOWN = 0, + HIDUMPER_GET_HELP, + HIDUMPER_GET_TRUSTED_LIST, +}; + +// HiDumper info +const std::string ARGS_HELP = "-help"; +const std::string HIDUMPER_GET_TRUSTED_LIST = "-getTrustlist"; + +const std::unordered_map MAP_ARGS = { + { ARGS_HELP, HidumperFlag::HIDUMPER_GET_HELP }, + { HIDUMPER_GET_TRUSTED_LIST, HidumperFlag::HIDUMPER_GET_TRUSTED_LIST }, +}; } // namespace DistributedHardware } // namespace OHOS #endif // OHOS_DM_DFX_CONSTANTS_H diff --git a/utils/include/dfx/lite/dm_hidumper.h b/utils/include/dfx/lite/dm_hidumper.h new file mode 100644 index 0000000000000000000000000000000000000000..2142558076e0f1fc8f6333fa06d92af0fd3a2c5b --- /dev/null +++ b/utils/include/dfx/lite/dm_hidumper.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2022 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 OHOS_DM_HIDUMPER_H +#define OHOS_DM_HIDUMPER_H + +#include +#include +#include +#include +#include + +#include "dm_constants.h" +#include "dm_dfx_constants.h" +#include "single_instance.h" + +namespace OHOS { +namespace DistributedHardware { +class HidumpHelper { +IMPLEMENT_SINGLE_INSTANCE(HidumpHelper); +public: + int32_t HiDump(const std::vector& args, std::string &result); + +private: + int32_t ProcessDump(const HidumperFlag &flag, std::string &result); + int32_t ShowAllLoadTrustedList(std::string &result); + int32_t ShowHelp(std::string &result); + int32_t ShowIllealInfomation(std::string &result); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_HIDUMPER_H diff --git a/utils/include/dfx/standard/dm_hidumper.h b/utils/include/dfx/standard/dm_hidumper.h new file mode 100644 index 0000000000000000000000000000000000000000..b4176bbf7ed38c41fdab9f0b289d11db62a023a3 --- /dev/null +++ b/utils/include/dfx/standard/dm_hidumper.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2022 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 OHOS_DM_HIDUMPER_H +#define OHOS_DM_HIDUMPER_H + +#include +#include +#include +#include + +#include "dm_constants.h" +#include "dm_dfx_constants.h" +#include "single_instance.h" +#include "dm_log.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +class HidumpHelper { +DECLARE_SINGLE_INSTANCE(HidumpHelper); +public: + int32_t HiDump(const std::vector& args, std::string &result); + void GetArgsType(const std::vector& args, std::vector &Flag); + void SetNodeInfo(const DmDeviceInfo& deviceInfo); +private: + int32_t ProcessDump(const HidumperFlag &flag, std::string &result); + int32_t ShowAllLoadTrustedList(std::string &result); + int32_t ShowHelp(std::string &result); + int32_t ShowIllealInfomation(std::string &result); + +private: + std::vector nodeInfos_; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif // OHOS_DM_HIDUMPER_H diff --git a/utils/src/dfx/lite/dm_hidumper.cpp b/utils/src/dfx/lite/dm_hidumper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..933c16d826e133e1c4016c44c48ec6d1495ea25c --- /dev/null +++ b/utils/src/dfx/lite/dm_hidumper.cpp @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 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 "dm_hidumper.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(HidumpHelper); +int32_t HidumpHelper::HiDump(const std::vector& args, std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ShowAllLoadTrustedList(std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ShowHelp(std::string &result) +{ + return DM_OK; +} + +int32_t HidumpHelper::ShowIllealInfomation(std::string &result) +{ + return ERR_DM_FAILED; +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/utils/src/dfx/standard/dm_hidumper.cpp b/utils/src/dfx/standard/dm_hidumper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9341e158535201110e0df4376f0f8e845270191a --- /dev/null +++ b/utils/src/dfx/standard/dm_hidumper.cpp @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2022 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 "dm_hidumper.h" +#include "dm_anonymous.h" +#include "dm_dfx_constants.h" +#include "dm_device_info.h" + +namespace OHOS { +namespace DistributedHardware { +IMPLEMENT_SINGLE_INSTANCE(HidumpHelper); +int32_t HidumpHelper::HiDump(const std::vector& args, std::string &result) +{ + LOGI("HidumpHelper hidumper start."); + result.clear(); + int32_t errCode = ERR_DM_FAILED; + if (args.empty()) { + return ProcessDump(HidumperFlag::HIDUMPER_GET_HELP, result); + } + auto flag = MAP_ARGS.find(args[0]); + if ((args.size() > 1) || (flag == MAP_ARGS.end())) { + errCode = ProcessDump(HidumperFlag::HIDUMPER_UNKNOWN, result); + } else { + errCode = ProcessDump(flag->second, result); + } + return errCode; +} + +void HidumpHelper::SetNodeInfo(const DmDeviceInfo& deviceInfo) +{ + nodeInfos_.push_back(deviceInfo); +} + +int32_t HidumpHelper::ProcessDump(const HidumperFlag &flag, std::string &result) +{ + LOGI("Process Dump."); + int32_t ret = ERR_DM_FAILED; + switch (flag) { + case HidumperFlag::HIDUMPER_GET_HELP: { + ret = ShowHelp(result); + break; + } + case HidumperFlag::HIDUMPER_GET_TRUSTED_LIST: { + ret = ShowAllLoadTrustedList(result); + break; + } + default: { + ret = ShowIllealInfomation(result); + break; + } + } + return ret; +} + +int32_t HidumpHelper::ShowAllLoadTrustedList(std::string &result) +{ + LOGI("Dump Show All Load Trust List."); + int32_t ret = DM_OK; + + if (nodeInfos_.size() == 0) { + LOGE("Hidumper get trusted list is 0"); + } + for (unsigned int i = 0; i < nodeInfos_.size(); ++i) { + result.append("\n{\n deviceId : ").append(GetAnonyString(nodeInfos_[i].deviceId).c_str()); + result.append("\n{\n deviceName : ").append(nodeInfos_[i].deviceName); + result.append("\n{\n networkId : ").append(GetAnonyString(nodeInfos_[i].networkId).c_str()); + } + nodeInfos_.clear(); + result.replace(result.size() - 1, 1, "\n"); + + LOGI("HidumpHelper ShowAllLoadTrustedList %s", result.c_str()); + return ret; +} + +int32_t HidumpHelper::ShowHelp(std::string &result) +{ + LOGI("Show hidumper help."); + result.append("DistributedHardwareDeviceManager hidumper options:\n"); + result.append(" -help "); + result.append(": Show help\n"); + result.append(" -getTrustlist "); + result.append(": Show all get trusted list:\n"); + result.append(" -getOnlineDeviceInfo "); + result.append(": Show all get online device list\n"); + result.append(" -getOfflineDeviceInfo "); + result.append(": Show all get offline device list\n\n"); + LOGI("result is %s", result.c_str()); + return DM_OK; +} + +int32_t HidumpHelper::ShowIllealInfomation(std::string &result) +{ + LOGI("ShowIllealInfomation Dump."); + result.clear(); + result.append("Unrecognized option, -h for help."); + return ERR_DM_FAILED; +} + +void HidumpHelper::GetArgsType(const std::vector& args, std::vector &Flag) +{ + LOGI("HidumpHelper::GetArgsType."); + if (args.empty()) { + Flag.push_back(HidumperFlag::HIDUMPER_GET_HELP); + } + + auto flag = MAP_ARGS.find(args[0]); + if ((args.size() > 1) || (flag == MAP_ARGS.end())) { + Flag.push_back(flag->second); + } +} +} // namespace DistributedHardware +} // namespace OHOS