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