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 #include "ipc_server_stub.h"
17
18 #include "device_manager_ipc_interface_code.h"
19 #include "device_manager_service.h"
20 #include "dm_constants.h"
21 #include "dm_log.h"
22 #include "if_system_ability_manager.h"
23 #include "ipc_cmd_register.h"
24 #include "ipc_skeleton.h"
25 #include "ipc_types.h"
26 #include "iservice_registry.h"
27 #include "string_ex.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32 IMPLEMENT_SINGLE_INSTANCE(IpcServerStub);
33
34 const bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&IpcServerStub::GetInstance());
35
IpcServerStub()36 IpcServerStub::IpcServerStub() : SystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID, true)
37 {
38 registerToService_ = false;
39 state_ = ServiceRunningState::STATE_NOT_START;
40 }
41
OnStart()42 void IpcServerStub::OnStart()
43 {
44 LOGI("IpcServerStub::OnStart start");
45 if (state_ == ServiceRunningState::STATE_RUNNING) {
46 LOGI("IpcServerStub has already started.");
47 return;
48 }
49 if (!Init()) {
50 LOGE("failed to init IpcServerStub");
51 return;
52 }
53 state_ = ServiceRunningState::STATE_RUNNING;
54
55 LOGI("called:AddAbilityListener begin!");
56 AddSystemAbilityListener(SOFTBUS_SERVER_SA_ID);
57 AddSystemAbilityListener(DISTRIBUTED_HARDWARE_SA_ID);
58 LOGI("called:AddAbilityListener end!");
59 }
60
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)61 void IpcServerStub::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
62 {
63 LOGI("OnAddSystemAbility systemAbilityId:%d added!", systemAbilityId);
64 if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
65 DeviceManagerService::GetInstance().InitSoftbusListener();
66 }
67 }
68
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)69 void IpcServerStub::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
70 {
71 LOGI("OnRemoveSystemAbility systemAbilityId:%d removed!", systemAbilityId);
72 if (systemAbilityId == SOFTBUS_SERVER_SA_ID) {
73 DeviceManagerService::GetInstance().UninitSoftbusListener();
74 } else if (systemAbilityId == DISTRIBUTED_HARDWARE_SA_ID) {
75 DeviceManagerService::GetInstance().LoadHardwareFwkService();
76 }
77 }
78
Init()79 bool IpcServerStub::Init()
80 {
81 LOGI("IpcServerStub::Init ready to init.");
82 DeviceManagerService::GetInstance().InitDMServiceListener();
83 if (!registerToService_) {
84 bool ret = Publish(this);
85 if (!ret) {
86 LOGE("IpcServerStub::Init Publish failed!");
87 return false;
88 }
89 registerToService_ = true;
90 }
91 return true;
92 }
93
OnStop()94 void IpcServerStub::OnStop()
95 {
96 LOGI("IpcServerStub::OnStop ready to stop service.");
97 DeviceManagerService::GetInstance().UninitDMServiceListener();
98 state_ = ServiceRunningState::STATE_NOT_START;
99 registerToService_ = false;
100 }
101
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)102 int32_t IpcServerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
103 {
104 auto remoteDescriptor = data.ReadInterfaceToken();
105 if (GetDescriptor() != remoteDescriptor) {
106 LOGI("ReadInterfaceToken fail!");
107 return ERR_DM_IPC_READ_FAILED;
108 }
109 int32_t ret = IpcCmdRegister::GetInstance().OnIpcCmd(static_cast<int32_t>(code), data, reply);
110 if (ret == ERR_DM_UNSUPPORTED_IPC_COMMAND) {
111 LOGW("unsupported code: %d", code);
112 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
113 }
114 return ret;
115 }
116
SendCmd(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)117 int32_t IpcServerStub::SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
118 {
119 MessageParcel data;
120 MessageParcel reply;
121 MessageOption option;
122 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
123 LOGE("IpcServerStub::SendCmd error: Invalid para, cmdCode: %d", (int32_t)cmdCode);
124 return IPCObjectStub::OnRemoteRequest(cmdCode, data, reply, option);
125 }
126
127 if (IpcCmdRegister::GetInstance().SetRequest(cmdCode, req, data) != DM_OK) {
128 LOGE("set request cmd failed");
129 return ERR_DM_IPC_SEND_REQUEST_FAILED;
130 }
131 int32_t ret = IpcCmdRegister::GetInstance().OnIpcCmd(cmdCode, data, reply);
132 if (ret == ERR_DM_UNSUPPORTED_IPC_COMMAND) {
133 LOGW("unsupported code: %d", cmdCode);
134 return IPCObjectStub::OnRemoteRequest(cmdCode, data, reply, option);
135 }
136 return IpcCmdRegister::GetInstance().ReadResponse(cmdCode, reply, rsp);
137 }
138
QueryServiceState() const139 ServiceRunningState IpcServerStub::QueryServiceState() const
140 {
141 return state_;
142 }
143
RegisterDeviceManagerListener(std::string & pkgName,sptr<IRemoteObject> listener)144 int32_t IpcServerStub::RegisterDeviceManagerListener(std::string &pkgName, sptr<IRemoteObject> listener)
145 {
146 if (pkgName.empty() || listener == nullptr) {
147 LOGE("RegisterDeviceManagerListener error: input parameter invalid.");
148 return ERR_DM_POINT_NULL;
149 }
150
151 LOGI("Register device manager listener for package name: %s", pkgName.c_str());
152 std::lock_guard<std::mutex> autoLock(listenerLock_);
153 auto iter = dmListener_.find(pkgName);
154 if (iter != dmListener_.end()) {
155 LOGI("RegisterDeviceManagerListener: listener already exists");
156 auto recipientIter = appRecipient_.find(pkgName);
157 if (recipientIter == appRecipient_.end()) {
158 LOGI("RegisterDeviceManagerListener: appRecipient not exists");
159 dmListener_.erase(pkgName);
160 } else {
161 auto listener = iter->second;
162 auto appRecipient = recipientIter->second;
163 listener->RemoveDeathRecipient(appRecipient);
164 appRecipient_.erase(pkgName);
165 dmListener_.erase(pkgName);
166 }
167 }
168
169 sptr<AppDeathRecipient> appRecipient = sptr<AppDeathRecipient>(new AppDeathRecipient());
170 if (!listener->AddDeathRecipient(appRecipient)) {
171 LOGE("RegisterDeviceManagerListener: AddDeathRecipient Failed");
172 }
173 dmListener_[pkgName] = listener;
174 appRecipient_[pkgName] = appRecipient;
175 LOGI("RegisterDeviceManagerListener: Register listener complete.");
176 return DM_OK;
177 }
178
UnRegisterDeviceManagerListener(std::string & pkgName)179 int32_t IpcServerStub::UnRegisterDeviceManagerListener(std::string &pkgName)
180 {
181 if (pkgName.empty()) {
182 LOGE("Invalid parameter, pkgName is empty.");
183 return ERR_DM_INPUT_PARA_INVALID;
184 }
185 LOGI("IpcServerStub::UnRegisterDeviceManagerListener In, pkgName: %s", pkgName.c_str());
186 std::lock_guard<std::mutex> autoLock(listenerLock_);
187 auto listenerIter = dmListener_.find(pkgName);
188 if (listenerIter == dmListener_.end()) {
189 LOGI("UnRegisterDeviceManagerListener: listener not exists");
190 return DM_OK;
191 }
192 auto recipientIter = appRecipient_.find(pkgName);
193 if (recipientIter == appRecipient_.end()) {
194 LOGI("UnRegisterDeviceManagerListener: appRecipient not exists");
195 dmListener_.erase(pkgName);
196 return DM_OK;
197 }
198 auto listener = listenerIter->second;
199 auto appRecipient = recipientIter->second;
200 listener->RemoveDeathRecipient(appRecipient);
201 appRecipient_.erase(pkgName);
202 dmListener_.erase(pkgName);
203 return DM_OK;
204 }
205
GetDmListener()206 const std::map<std::string, sptr<IRemoteObject>> &IpcServerStub::GetDmListener()
207 {
208 std::lock_guard<std::mutex> autoLock(listenerLock_);
209 return dmListener_;
210 }
211
SendALL(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)212 int32_t IpcServerStub::SendALL(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
213 {
214 std::lock_guard<std::mutex> autoLock(listenerLock_);
215 for (const auto &iter : dmListener_) {
216 auto pkgName = iter.first;
217 auto remote = iter.second;
218 req->SetPkgName(pkgName);
219 sptr<IpcRemoteBroker> listener = iface_cast<IpcRemoteBroker>(remote);
220 if (listener == nullptr) {
221 LOGE("IpcServerStub::SendALL, listener is nullptr, pkgName : %s.", pkgName.c_str());
222 continue;
223 }
224 listener->SendCmd(cmdCode, req, rsp);
225 }
226 return DM_OK;
227 }
228
GetAllPkgName()229 std::vector<std::string> IpcServerStub::GetAllPkgName()
230 {
231 std::vector<std::string> PkgNameVec;
232 std::lock_guard<std::mutex> autoLock(listenerLock_);
233 for (const auto &iter : dmListener_) {
234 PkgNameVec.push_back(iter.first);
235 }
236 return PkgNameVec;
237 }
238
GetDmListener(std::string pkgName) const239 const sptr<IpcRemoteBroker> IpcServerStub::GetDmListener(std::string pkgName) const
240 {
241 if (pkgName.empty()) {
242 LOGE("Invalid parameter, pkgName is empty.");
243 return nullptr;
244 }
245 std::lock_guard<std::mutex> autoLock(listenerLock_);
246 auto iter = dmListener_.find(pkgName);
247 if (iter == dmListener_.end()) {
248 return nullptr;
249 }
250 auto remote = iter->second;
251 sptr<IpcRemoteBroker> dmListener = iface_cast<IpcRemoteBroker>(remote);
252 return dmListener;
253 }
254
GetDmListenerPkgName(const wptr<IRemoteObject> & remote) const255 const std::string IpcServerStub::GetDmListenerPkgName(const wptr<IRemoteObject> &remote) const
256 {
257 std::string pkgName = "";
258 std::lock_guard<std::mutex> autoLock(listenerLock_);
259 for (const auto &iter : dmListener_) {
260 if (iter.second == remote.promote()) {
261 pkgName = iter.first;
262 break;
263 }
264 }
265 return pkgName;
266 }
267
Dump(int32_t fd,const std::vector<std::u16string> & args)268 int32_t IpcServerStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
269 {
270 LOGI("DistributedHardwareService Dump.");
271 std::vector<std::string> argsStr {};
272 for (auto item : args) {
273 argsStr.emplace_back(Str16ToStr8(item));
274 }
275
276 std::string result("");
277 int ret = DeviceManagerService::GetInstance().DmHiDumper(argsStr, result);
278 if (ret != DM_OK) {
279 LOGE("Dump error, ret = %d", ret);
280 }
281
282 ret = dprintf(fd, "%s\n", result.c_str());
283 if (ret < 0) {
284 LOGE("HiDumper dprintf error");
285 ret = ERR_DM_FAILED;
286 }
287 return ret;
288 }
289
OnRemoteDied(const wptr<IRemoteObject> & remote)290 void AppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
291 {
292 std::string pkgName = IpcServerStub::GetInstance().GetDmListenerPkgName(remote);
293 LOGI("AppDeathRecipient: OnRemoteDied for %s", pkgName.c_str());
294 IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
295 }
296 } // namespace DistributedHardware
297 } // namespace OHOS
298