1 /* 2 * Copyright (c) 2022 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 <tuple> 23 #include <vector> 24 25 #include "ipc_remote_broker.h" 26 #include "iremote_stub.h" 27 #include "single_instance.h" 28 #include "system_ability.h" 29 30 namespace OHOS { 31 namespace DistributedHardware { 32 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 33 34 class AppDeathRecipient : public IRemoteObject::DeathRecipient { 35 public: 36 /** 37 * @tc.name: AppDeathRecipient::OnRemoteDied 38 * @tc.desc: OnRemoteDied function of the App DeathRecipient 39 * @tc.type: FUNC 40 */ 41 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 42 AppDeathRecipient() = default; 43 ~AppDeathRecipient() = default; 44 }; 45 46 class IpcServerStub : public SystemAbility, public IRemoteStub<IpcRemoteBroker> { 47 DECLARE_SYSTEM_ABILITY(IpcServerStub); 48 DECLARE_SINGLE_INSTANCE_BASE(IpcServerStub); 49 50 public: 51 /** 52 * @tc.name: IpcServerStub::OnStart 53 * @tc.desc: OnStart of the IpcServerStub 54 * @tc.type: FUNC 55 */ 56 void OnStart() override; 57 58 /** 59 * @tc.name: IpcServerStub::OnStop 60 * @tc.desc: OnStop of the IpcServerStub 61 * @tc.type: FUNC 62 */ 63 void OnStop() override; 64 65 /** 66 * @tc.name: IpcServerStub::OnRemoteRequest 67 * @tc.desc: On Remote Request of the IpcServerStub 68 * @tc.type: FUNC 69 */ 70 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 71 72 /** 73 * @tc.name: IpcServerStub::SendCmd 74 * @tc.desc: Send Cmd of the IpcServerStub 75 * @tc.type: FUNC 76 */ 77 int32_t SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp) override; 78 79 /** 80 * @tc.name: IpcServerStub::RegisterDeviceManagerListener 81 * @tc.desc: Register DeviceManager Listener of the IpcServerStub 82 * @tc.type: FUNC 83 */ 84 int32_t RegisterDeviceManagerListener(std::string &pkgName, sptr<IRemoteObject> listener); 85 86 /** 87 * @tc.name: IpcServerStub::UnRegisterDeviceManagerListener 88 * @tc.desc: UnRegister DeviceManager Listener of the IpcServerStub 89 * @tc.type: FUNC 90 */ 91 int32_t UnRegisterDeviceManagerListener(std::string &pkgName); 92 93 /** 94 * @tc.name: IpcServerStub::QueryServiceState 95 * @tc.desc: Query Service State of the IpcServerStub 96 * @tc.type: FUNC 97 */ 98 ServiceRunningState QueryServiceState() const; 99 100 /** 101 * @tc.name: IpcServerStub::GetDmListener 102 * @tc.desc: GetDmListener of the IpcServerStub 103 * @tc.type: FUNC 104 */ 105 const std::map<std::string, sptr<IRemoteObject>> &GetDmListener(); 106 107 /** 108 * @tc.name: IpcServerStub::GetDmListener 109 * @tc.desc: Get DmListener of the IpcServerStub 110 * @tc.type: FUNC 111 */ 112 const sptr<IpcRemoteBroker> GetDmListener(std::string pkgName) const; 113 114 /** 115 * @tc.name: IpcServerStub::Dump 116 * @tc.desc: Dump of the Device Manager Service 117 * @tc.type: FUNC 118 */ 119 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 120 121 /** 122 * @tc.name: IpcServerStub::OnAddSystemAbility 123 * @tc.desc: OnAddSystemAbility of the IpcServerStub 124 * @tc.type: FUNC 125 */ 126 void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 127 128 /** 129 * @tc.name: IpcServerStub::OnRemoveSystemAbility 130 * @tc.desc: OnRemoveSystemAbility of the IpcServerStub 131 * @tc.type: FUNC 132 */ 133 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; 134 135 private: 136 IpcServerStub(); 137 ~IpcServerStub() = default; 138 bool Init(); 139 140 private: 141 bool registerToService_; 142 ServiceRunningState state_; 143 mutable std::mutex listenerLock_; 144 std::map<std::string, sptr<AppDeathRecipient>> appRecipient_; 145 std::map<std::string, sptr<IRemoteObject>> dmListener_; 146 }; 147 } // namespace DistributedHardware 148 } // namespace OHOS 149 #endif // OHOS_DM_IPC_SERVER_STUB_H 150