1 /* 2 * Copyright (c) 2022-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DM_IPC_SERVER_STUB_H 17 #define OHOS_DM_IPC_SERVER_STUB_H 18 19 #include <map> 20 #include <memory> 21 #include <mutex> 22 #include <set> 23 #include <tuple> 24 #include <unordered_set> 25 #include <vector> 26 27 #include "ipc_remote_broker.h" 28 #include "ipc_req.h" 29 #include "ipc_rsp.h" 30 #include "iremote_stub.h" 31 #include "system_ability.h" 32 33 #include "dm_device_info.h" 34 #include "dm_single_instance.h" 35 36 namespace OHOS { 37 namespace DistributedHardware { 38 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 39 40 class AppDeathRecipient : public IRemoteObject::DeathRecipient { 41 public: 42 /** 43 * @tc.name: AppDeathRecipient::OnRemoteDied 44 * @tc.desc: OnRemoteDied function of the App DeathRecipient 45 * @tc.type: FUNC 46 */ 47 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 48 AppDeathRecipient() = default; 49 ~AppDeathRecipient() override = default; 50 }; 51 52 class IpcServerStub : public SystemAbility, public IRemoteStub<IpcRemoteBroker> { 53 DECLARE_SYSTEM_ABILITY(IpcServerStub); 54 DM_DECLARE_SINGLE_INSTANCE_BASE(IpcServerStub); 55 56 public: 57 /** 58 * @tc.name: IpcServerStub::OnStart 59 * @tc.desc: OnStart of the IpcServerStub 60 * @tc.type: FUNC 61 */ 62 void OnStart() override; 63 64 /** 65 * @tc.name: IpcServerStub::OnStop 66 * @tc.desc: OnStop of the IpcServerStub 67 * @tc.type: FUNC 68 */ 69 void OnStop() override; 70 71 /** 72 * @tc.name: IpcServerStub::OnRemoteRequest 73 * @tc.desc: On Remote Request of the IpcServerStub 74 * @tc.type: FUNC 75 */ 76 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 77 78 /** 79 * @tc.name: IpcServerStub::SendCmd 80 * @tc.desc: Send Cmd of the IpcServerStub 81 * @tc.type: FUNC 82 */ 83 int32_t SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp) override; 84 85 /** 86 * @tc.name: IpcServerStub::RegisterDeviceManagerListener 87 * @tc.desc: Register DeviceManager Listener of the IpcServerStub 88 * @tc.type: FUNC 89 */ 90 int32_t RegisterDeviceManagerListener(const ProcessInfo &processInfo, sptr<IpcRemoteBroker> listener); 91 92 /** 93 * @tc.name: IpcServerStub::UnRegisterDeviceManagerListener 94 * @tc.desc: UnRegister DeviceManager Listener of the IpcServerStub 95 * @tc.type: FUNC 96 */ 97 int32_t UnRegisterDeviceManagerListener(const ProcessInfo &processInfo); 98 99 /** 100 * @tc.name: IpcServerStub::QueryServiceState 101 * @tc.desc: Query Service State of the IpcServerStub 102 * @tc.type: FUNC 103 */ 104 ServiceRunningState QueryServiceState() const; 105 106 /** 107 * @tc.name: IpcServerStub::GetAllProcessInfo 108 * @tc.desc: Get All PkgName from dmListener_ 109 * @tc.type: FUNC 110 */ 111 std::vector<ProcessInfo> GetAllProcessInfo(); 112 113 /** 114 * @tc.name: IpcServerStub::GetDmListener 115 * @tc.desc: Get DmListener of the IpcServerStub 116 * @tc.type: FUNC 117 */ 118 const sptr<IpcRemoteBroker> GetDmListener(ProcessInfo processInfo) const; 119 120 /** 121 * @tc.name: IpcServerStub::GetDmListenerPkgName 122 * @tc.desc: Get DmListener PkgName of the IpcServerStub 123 * @tc.type: FUNC 124 */ 125 const ProcessInfo GetDmListenerPkgName(const wptr<IRemoteObject> &remote) const; 126 127 /** 128 * @tc.name: IpcServerStub::Dump 129 * @tc.desc: Dump of the Device Manager Service 130 * @tc.type: FUNC 131 */ 132 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 133 134 /** 135 * @tc.name: IpcServerStub::OnAddSystemAbility 136 * @tc.desc: OnAddSystemAbility of the IpcServerStub 137 * @tc.type: FUNC 138 */ 139 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 140 141 /** 142 * @tc.name: IpcServerStub::OnRemoveSystemAbility 143 * @tc.desc: OnRemoveSystemAbility of the IpcServerStub 144 * @tc.type: FUNC 145 */ 146 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 147 std::set<std::string> GetSystemSA(); 148 149 private: 150 IpcServerStub(); 151 ~IpcServerStub() override = default; 152 bool Init(); 153 void AddSystemSA(const std::string &pkgName); 154 void RemoveSystemSA(const std::string &pkgName); 155 std::string JoinPath(const std::string &prefixPath, const std::string &midPath, 156 const std::string &subPath); 157 std::string JoinPath(const std::string &prefixPath, const std::string &subPath); 158 std::string AddDelimiter(const std::string &path); 159 void ReclaimMemmgrFileMemForDM(); 160 void HandleSoftBusServerAdd(); 161 162 private: 163 bool registerToService_; 164 ServiceRunningState state_; 165 mutable std::mutex listenerLock_; 166 std::map<ProcessInfo, sptr<AppDeathRecipient>> appRecipient_; 167 std::map<ProcessInfo, sptr<IpcRemoteBroker>> dmListener_; 168 std::set<std::string> systemSA_; 169 }; 170 } // namespace DistributedHardware 171 } // namespace OHOS 172 #endif // OHOS_DM_IPC_SERVER_STUB_H 173