• 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 #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<IpcRemoteBroker> listener)144 int32_t IpcServerStub::RegisterDeviceManagerListener(std::string &pkgName, sptr<IpcRemoteBroker> 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->AsObject()->RemoveDeathRecipient(appRecipient);
164             appRecipient_.erase(pkgName);
165             dmListener_.erase(pkgName);
166         }
167     }
168 
169     sptr<AppDeathRecipient> appRecipient = sptr<AppDeathRecipient>(new AppDeathRecipient());
170     if (!listener->AsObject()->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->AsObject()->RemoveDeathRecipient(appRecipient);
201     appRecipient_.erase(pkgName);
202     dmListener_.erase(pkgName);
203     return DM_OK;
204 }
205 
SendALL(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)206 int32_t IpcServerStub::SendALL(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
207 {
208     std::lock_guard<std::mutex> autoLock(listenerLock_);
209     for (const auto &iter : dmListener_) {
210         auto pkgName = iter.first;
211         auto listener = iter.second;
212         req->SetPkgName(pkgName);
213         if (listener == nullptr) {
214             LOGE("IpcServerStub::SendALL, listener is nullptr, pkgName : %s.", pkgName.c_str());
215             continue;
216         }
217         listener->SendCmd(cmdCode, req, rsp);
218     }
219     return DM_OK;
220 }
221 
GetAllPkgName()222 std::vector<std::string> IpcServerStub::GetAllPkgName()
223 {
224     std::vector<std::string> PkgNameVec;
225     std::lock_guard<std::mutex> autoLock(listenerLock_);
226     for (const auto &iter : dmListener_) {
227         PkgNameVec.push_back(iter.first);
228     }
229     return PkgNameVec;
230 }
231 
GetDmListener(std::string pkgName) const232 const sptr<IpcRemoteBroker> IpcServerStub::GetDmListener(std::string pkgName) const
233 {
234     if (pkgName.empty()) {
235         LOGE("Invalid parameter, pkgName is empty.");
236         return nullptr;
237     }
238     std::lock_guard<std::mutex> autoLock(listenerLock_);
239     auto iter = dmListener_.find(pkgName);
240     if (iter == dmListener_.end()) {
241         return nullptr;
242     }
243     return iter->second;
244 }
245 
GetDmListenerPkgName(const wptr<IRemoteObject> & remote) const246 const std::string IpcServerStub::GetDmListenerPkgName(const wptr<IRemoteObject> &remote) const
247 {
248     std::string pkgName = "";
249     std::lock_guard<std::mutex> autoLock(listenerLock_);
250     for (const auto &iter : dmListener_) {
251         if ((iter.second)->AsObject() == remote.promote()) {
252             pkgName = iter.first;
253             break;
254         }
255     }
256     return pkgName;
257 }
258 
Dump(int32_t fd,const std::vector<std::u16string> & args)259 int32_t IpcServerStub::Dump(int32_t fd, const std::vector<std::u16string>& args)
260 {
261     LOGI("DistributedHardwareService Dump.");
262     std::vector<std::string> argsStr {};
263     for (auto item : args) {
264         argsStr.emplace_back(Str16ToStr8(item));
265     }
266 
267     std::string result("");
268     int ret = DeviceManagerService::GetInstance().DmHiDumper(argsStr, result);
269     if (ret != DM_OK) {
270         LOGE("Dump error, ret = %d", ret);
271     }
272 
273     ret = dprintf(fd, "%s\n", result.c_str());
274     if (ret < 0) {
275         LOGE("HiDumper dprintf error");
276         ret = ERR_DM_FAILED;
277     }
278     return ret;
279 }
280 
OnRemoteDied(const wptr<IRemoteObject> & remote)281 void AppDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
282 {
283     std::string pkgName = IpcServerStub::GetInstance().GetDmListenerPkgName(remote);
284     LOGI("AppDeathRecipient: OnRemoteDied for %s", pkgName.c_str());
285     IpcServerStub::GetInstance().UnRegisterDeviceManagerListener(pkgName);
286 }
287 } // namespace DistributedHardware
288 } // namespace OHOS
289