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