1 /*
2 * Copyright (c) 2025 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_client_manager_mini.h"
17
18 #include <set>
19 #include <unistd.h>
20
21 #include "device_manager_ipc_interface_code.h"
22 #include "device_manager_impl_mini.h"
23 #include "dm_error_type.h"
24 #include "dm_log.h"
25 #include "dm_service_load.h"
26 #include "iremote_object.h"
27 #include "iservice_registry.h"
28 #include "system_ability_definition.h"
29
30 namespace OHOS {
31 namespace DistributedHardware {
32
OnRemoteDied(const wptr<IRemoteObject> & remote)33 void DmDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
34 {
35 LOGW("DmDeathRecipient : OnRemoteDied");
36 (void)remote;
37 }
38
ClientInit()39 int32_t IpcClientManagerMini::ClientInit()
40 {
41 LOGI("Start");
42 if (dmInterface_ != nullptr) {
43 LOGI("DeviceManagerService Already Init");
44 return DM_OK;
45 }
46
47 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 if (samgr == nullptr) {
49 LOGE("Get SystemAbilityManager Failed");
50 return ERR_DM_INIT_FAILED;
51 }
52
53 auto object = samgr->CheckSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
54 if (object == nullptr) {
55 LOGE("Get DeviceManager SystemAbility Failed");
56 DmServiceLoad::GetInstance().LoadDMService();
57 return ERR_DM_INIT_FAILED;
58 }
59
60 if (dmRecipient_ == nullptr) {
61 dmRecipient_ = sptr<DmDeathRecipient>(new DmDeathRecipient());
62 }
63 if (!object->AddDeathRecipient(dmRecipient_)) {
64 LOGE("InitDeviceManagerService: AddDeathRecipient Failed");
65 }
66 dmInterface_ = iface_cast<IpcRemoteBroker>(object);
67 LOGI("Completed");
68 return DM_OK;
69 }
70
Init(const std::string & pkgName)71 int32_t IpcClientManagerMini::Init(const std::string &pkgName)
72 {
73 if (pkgName.empty()) {
74 LOGE("Invalid parameter, pkgName is empty.");
75 return ERR_DM_INPUT_PARA_INVALID;
76 }
77 std::lock_guard<std::mutex> autoLock(lock_);
78 SubscribeDMSAChangeListener(pkgName);
79 int32_t ret = ClientInit();
80 if (ret != DM_OK) {
81 LOGE("InitDeviceManager Failed with ret %{public}d", ret);
82 return ret;
83 }
84 return DM_OK;
85 }
86
UnInit(const std::string & pkgName)87 int32_t IpcClientManagerMini::UnInit(const std::string &pkgName)
88 {
89 if (pkgName.empty()) {
90 LOGE("Invalid parameter, pkgName is empty.");
91 return ERR_DM_INPUT_PARA_INVALID;
92 }
93 LOGI("UnInit in, pkgName %{public}s", pkgName.c_str());
94 std::lock_guard<std::mutex> autoLock(lock_);
95 if (dmInterface_ == nullptr) {
96 LOGE("DeviceManager not Init");
97 return ERR_DM_INIT_FAILED;
98 }
99 LOGI("completed, pkgName: %{public}s", pkgName.c_str());
100 return DM_OK;
101 }
102
SendRequest(int32_t cmdCode,std::shared_ptr<IpcReq> req,std::shared_ptr<IpcRsp> rsp)103 int32_t IpcClientManagerMini::SendRequest(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp)
104 {
105 if (cmdCode < 0 || cmdCode >= IPC_MSG_BUTT) {
106 LOGE("IpcClientManagerMini::SendRequest cmdCode param invalid!");
107 return ERR_DM_UNSUPPORTED_IPC_COMMAND;
108 }
109 if (req == nullptr || rsp == nullptr) {
110 return ERR_DM_INPUT_PARA_INVALID;
111 }
112 std::lock_guard<std::mutex> autoLock(lock_);
113 if (dmInterface_ != nullptr) {
114 LOGD("IpcClientManagerMini::SendRequest cmdCode: %{public}d", cmdCode);
115 return dmInterface_->SendCmd(cmdCode, req, rsp);
116 } else {
117 LOGE("dmInterface_ is not init.");
118 return ERR_DM_INIT_FAILED;
119 }
120 }
121
OnDmServiceDied()122 int32_t IpcClientManagerMini::OnDmServiceDied()
123 {
124 LOGI("IpcClientManagerMini::OnDmServiceDied begin");
125 {
126 std::lock_guard<std::mutex> autoLock(lock_);
127 if (dmInterface_ == nullptr) {
128 LOGE("IpcClientManagerMini::OnDmServiceDied, dmInterface_ null");
129 return ERR_DM_POINT_NULL;
130 }
131 if (dmRecipient_ != nullptr) {
132 if (dmInterface_->AsObject() == nullptr) {
133 LOGE("IpcClientManagerMini::OnDmServiceDied, dmInterface_->AsObject() null");
134 return ERR_DM_POINT_NULL;
135 }
136 dmInterface_->AsObject()->RemoveDeathRecipient(dmRecipient_);
137 dmRecipient_ = nullptr;
138 }
139 dmInterface_ = nullptr;
140 }
141 LOGI("IpcClientManagerMini::OnDmServiceDied complete");
142 return DM_OK;
143 }
144
SubscribeDMSAChangeListener(const std::string & pkgName)145 void IpcClientManagerMini::SubscribeDMSAChangeListener(const std::string &pkgName)
146 {
147 saListenerCallback = new (std::nothrow) SystemAbilityListener();
148 if (saListenerCallback == nullptr) {
149 LOGE("saListenerCallback is nullptr.");
150 return;
151 }
152 saListenerCallback->InitPkgName(pkgName);
153 sptr<ISystemAbilityManager> systemAbilityManager =
154 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
155
156 if (systemAbilityManager == nullptr) {
157 LOGE("get system ability manager failed.");
158 return;
159 }
160
161 if (!isSubscribeDMSAChangeListener.load()) {
162 LOGI("try subscribe source sa change listener, sa id: %{public}d", DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
163 int32_t ret = systemAbilityManager->SubscribeSystemAbility(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID,
164 saListenerCallback);
165 if (ret != DM_OK) {
166 LOGE("subscribe source sa change failed: %{public}d", ret);
167 return;
168 }
169 isSubscribeDMSAChangeListener.store(true);
170 }
171 }
172
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)173 void IpcClientManagerMini::SystemAbilityListener::OnRemoveSystemAbility(int32_t systemAbilityId,
174 const std::string &deviceId)
175 {
176 if (systemAbilityId == DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID) {
177 DeviceManagerImplMini::GetInstance().OnDmServiceDied();
178 }
179 LOGI("sa %{public}d is removed.", systemAbilityId);
180 }
181
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)182 void IpcClientManagerMini::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId,
183 const std::string &deviceId)
184 {
185 LOGI("sa %{public}d is added.", systemAbilityId);
186 DeviceManagerImplMini::GetInstance().InitDeviceManager(pkgName_);
187 }
188 } // namespace DistributedHardware
189 } // namespace OHOS
190