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