• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "distributed_sched_adapter.h"
17 
18 #include "datetime_ex.h"
19 #include "distributed_sched_service.h"
20 #include "dtbschedmgr_device_info_storage.h"
21 #include "dtbschedmgr_log.h"
22 #include "ipc_skeleton.h"
23 #include "ipc_types.h"
24 #include "mission/distributed_sched_mission_manager.h"
25 #include "mission/mission_info_converter.h"
26 #include "os_account_manager.h"
27 #include "parcel_helper.h"
28 #include "string_ex.h"
29 
30 namespace OHOS {
31 namespace DistributedSchedule {
32 using namespace std;
33 using namespace AAFwk;
34 using namespace AccountSA;
35 using namespace AppExecFwk;
36 using DstbMissionChangeListener = DistributedMissionChangeListener;
37 namespace {
38 // set a non-zero value on need later
39 constexpr int64_t DEVICE_OFFLINE_DELAY_TIME = 0;
40 const std::string TAG = "DistributedSchedAdapter";
41 }
42 
43 IMPLEMENT_SINGLE_INSTANCE(DistributedSchedAdapter);
44 
Init()45 void DistributedSchedAdapter::Init()
46 {
47     if (dmsAdapterHandler_ == nullptr) {
48         shared_ptr<EventRunner> runner = EventRunner::Create("dmsAdapter");
49         if (runner == nullptr) {
50             HILOGE("create runner failed");
51             return;
52         }
53         dmsAdapterHandler_ = make_shared<EventHandler>(runner);
54     }
55 }
56 
UnInit()57 void DistributedSchedAdapter::UnInit()
58 {
59     dmsAdapterHandler_ = nullptr;
60 }
61 
ConnectAbility(const OHOS::AAFwk::Want & want,const sptr<IRemoteObject> & connect,const sptr<IRemoteObject> & callerToken)62 int32_t DistributedSchedAdapter::ConnectAbility(const OHOS::AAFwk::Want& want,
63     const sptr<IRemoteObject>& connect, const sptr<IRemoteObject>& callerToken)
64 {
65     HILOGD("ConnectAbility");
66     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
67     if (errCode != ERR_OK) {
68         HILOGE("connect ability server failed, errCode=%{public}d", errCode);
69         return errCode;
70     }
71     std::vector<int> ids;
72     ErrCode ret = OsAccountManager::QueryActiveOsAccountIds(ids);
73     if (ret != ERR_OK || ids.empty()) {
74         return INVALID_PARAMETERS_ERR;
75     }
76     ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want,
77         iface_cast<AAFwk::IAbilityConnection>(connect), callerToken, ids[0]);
78     return ret;
79 }
80 
DisconnectAbility(const sptr<IRemoteObject> & connect)81 int32_t DistributedSchedAdapter::DisconnectAbility(const sptr<IRemoteObject>& connect)
82 {
83     HILOGD("DisconnectAbility");
84     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
85     if (errCode != ERR_OK) {
86         HILOGE("connect ability server failed, errCode=%{public}d", errCode);
87         return errCode;
88     }
89     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(
90         iface_cast<AAFwk::IAbilityConnection>(connect));
91     return ret;
92 }
93 
DeviceOnline(const std::string & deviceId)94 void DistributedSchedAdapter::DeviceOnline(const std::string& deviceId)
95 {
96     if (dmsAdapterHandler_ == nullptr) {
97         HILOGE("DeviceOnline dmsAdapterHandler is null");
98         return;
99     }
100 
101     if (deviceId.empty()) {
102         HILOGW("DeviceOnline deviceId is empty");
103         return;
104     }
105     HILOGD("process DeviceOnline deviceId is %s", deviceId.c_str());
106     dmsAdapterHandler_->RemoveTask(deviceId);
107 }
108 
DeviceOffline(const std::string & deviceId)109 void DistributedSchedAdapter::DeviceOffline(const std::string& deviceId)
110 {
111     if (dmsAdapterHandler_ == nullptr) {
112         HILOGE("DeviceOffline dmsAdapterHandler is null");
113         return;
114     }
115 
116     if (deviceId.empty()) {
117         HILOGW("DeviceOffline deviceId is empty");
118         return;
119     }
120     HILOGD("process DeviceOffline deviceId is %s", deviceId.c_str());
121     auto callback = [deviceId, this] () {
122         ProcessDeviceOffline(deviceId);
123     };
124     if (!dmsAdapterHandler_->PostTask(callback, deviceId, DEVICE_OFFLINE_DELAY_TIME)) {
125         HILOGW("DeviceOffline PostTask failed");
126     }
127 }
128 
ProcessDeviceOffline(const std::string & deviceId)129 void DistributedSchedAdapter::ProcessDeviceOffline(const std::string& deviceId)
130 {
131     HILOGD("ProcessDeviceOffline");
132     DistributedSchedService::GetInstance().ProcessDeviceOffline(deviceId);
133 }
134 
ProcessConnectDied(const sptr<IRemoteObject> & connect)135 void DistributedSchedAdapter::ProcessConnectDied(const sptr<IRemoteObject>& connect)
136 {
137     if (dmsAdapterHandler_ == nullptr) {
138         HILOGE("ProcessConnectDied dmsAdapterHandler is null");
139         return;
140     }
141 
142     if (connect == nullptr) {
143         HILOGE("ProcessConnectDied connect is null");
144         return;
145     }
146     HILOGD("process connect died");
147     auto callback = [connect] () {
148         DistributedSchedService::GetInstance().ProcessConnectDied(connect);
149     };
150     if (!dmsAdapterHandler_->PostTask(callback)) {
151         HILOGW("ProcessConnectDied PostTask failed");
152     }
153 }
154 
ProcessCalleeDied(const sptr<IRemoteObject> & connect)155 void DistributedSchedAdapter::ProcessCalleeDied(const sptr<IRemoteObject>& connect)
156 {
157     if (dmsAdapterHandler_ == nullptr) {
158         HILOGE("ProcessCalleeDied dmsAdapterHandler is null");
159         return;
160     }
161     if (connect == nullptr) {
162         HILOGE("ProcessCalleeDied connect is null");
163         return;
164     }
165     HILOGD("process callee died");
166     auto callback = [connect] () {
167         DistributedSchedService::GetInstance().ProcessCalleeDied(connect);
168     };
169     if (!dmsAdapterHandler_->PostTask(callback)) {
170         HILOGE("ProcessCalleeDied PostTask failed");
171     }
172 }
173 
ProcessCallerDied(const sptr<IRemoteObject> & connect)174 void DistributedSchedAdapter::ProcessCallerDied(const sptr<IRemoteObject>& connect)
175 {
176     if (dmsAdapterHandler_ == nullptr) {
177         HILOGE("ProcessCallerDied dmsAdapterHandler is null");
178         return;
179     }
180     if (connect == nullptr) {
181         HILOGE("ProcessCallerDied connect is null");
182         return;
183     }
184     HILOGD("process caller died");
185     auto callback = [connect] () {
186         DistributedSchedService::GetInstance().ProcessCallerDied(connect);
187     };
188     if (!dmsAdapterHandler_->PostTask(callback)) {
189         HILOGE("ProcessCallerDied PostTask failed");
190     }
191 }
192 
ReleaseAbility(const sptr<IRemoteObject> & connect,const AppExecFwk::ElementName & element)193 int32_t DistributedSchedAdapter::ReleaseAbility(const sptr<IRemoteObject>& connect,
194     const AppExecFwk::ElementName &element)
195 {
196     HILOGD("ReleaseAbility called");
197     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
198     if (errCode != ERR_OK) {
199         HILOGE("ReleaseAbility:connect ability server failed, errCode=%{public}d", errCode);
200         return errCode;
201     }
202     AppExecFwk::ElementName elementWithoutDeviceId("", element.GetBundleName(), element.GetAbilityName());
203     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ReleaseAbility(
204         iface_cast<AAFwk::IAbilityConnection>(connect), elementWithoutDeviceId);
205     return ret;
206 }
207 
StartAbilityByCall(const OHOS::AAFwk::Want & want,const sptr<IRemoteObject> & connect,const sptr<IRemoteObject> & callerToken)208 int32_t DistributedSchedAdapter::StartAbilityByCall(const OHOS::AAFwk::Want& want,
209     const sptr<IRemoteObject>& connect, const sptr<IRemoteObject>& callerToken)
210 {
211     HILOGD("ResolveAbility called");
212     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
213     if (errCode != ERR_OK) {
214         HILOGE("ResolveAbility:connect ability server failed, errCode=%{public}d", errCode);
215         return errCode;
216     }
217     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbilityByCall(want,
218         iface_cast<AAFwk::IAbilityConnection>(connect), callerToken);
219     return ret;
220 }
221 
InitHichainService()222 bool DistributedSchedAdapter::InitHichainService()
223 {
224     if (hichainGmInstance_ != nullptr) {
225         HILOGI("hichain GmInstance is already exist");
226         return true;
227     }
228     if (InitDeviceAuthService() != ERR_OK) {
229         HILOGE("hichain init DeviceAuthService failed");
230         return false;
231     }
232     hichainGmInstance_ = GetGmInstance();
233     if (hichainGmInstance_ == nullptr) {
234         HILOGE("hichain get GmInstance failed");
235         return false;
236     }
237     return true;
238 }
239 
CheckAccessToGroup(const std::string & groupId,const std::string & targetBundleName)240 bool DistributedSchedAdapter::CheckAccessToGroup(const std::string& groupId, const std::string& targetBundleName)
241 {
242     std::lock_guard<std::mutex> autoLock(hichainLock_);
243     int64_t begin = GetTickCount();
244     if (!InitHichainService()) {
245         return false;
246     }
247     int32_t ret = hichainGmInstance_->checkAccessToGroup(ANY_OS_ACCOUNT, targetBundleName.c_str(),
248         groupId.c_str());
249     HILOGI("[PerformanceTest] checkAccessToGroup spend %{public}" PRId64 " ms", GetTickCount() - begin);
250     if (ret != ERR_OK) {
251         HILOGE("hichain checkAccessToGroup failed, ret:%{public}d", ret);
252         return false;
253     }
254     HILOGD("hichain checkAccessToGroup success");
255     return true;
256 }
257 
GetRelatedGroups(const std::string & udid,const std::string & bundleName,std::string & returnGroups)258 bool DistributedSchedAdapter::GetRelatedGroups(const std::string& udid, const std::string& bundleName,
259     std::string& returnGroups)
260 {
261     std::lock_guard<std::mutex> autoLock(hichainLock_);
262     int64_t begin = GetTickCount();
263     if (!InitHichainService()) {
264         return false;
265     }
266     uint32_t groupNum = 0;
267     char* groupsJsonStr = nullptr;
268     int32_t ret = hichainGmInstance_->getRelatedGroups(ANY_OS_ACCOUNT, bundleName.c_str(), udid.c_str(),
269         &groupsJsonStr, &groupNum);
270     HILOGI("[PerformanceTest] getRelatedGroups spend %{public}" PRId64 " ms", GetTickCount() - begin);
271     if (ret != ERR_OK) {
272         HILOGE("hichain getRelatedGroups failed, ret:%{public}d", ret);
273         return false;
274     }
275     if (groupsJsonStr == nullptr || groupNum == 0) {
276         HILOGE("groupsJsonStr is nullptr");
277         return false;
278     }
279     returnGroups = groupsJsonStr;
280     return true;
281 }
282 
GetLocalMissionInfos(int32_t numMissions,std::vector<DstbMissionInfo> & missionInfos)283 int32_t DistributedSchedAdapter::GetLocalMissionInfos(int32_t numMissions,
284     std::vector<DstbMissionInfo>& missionInfos)
285 
286 {
287     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
288     if (errCode != ERR_OK) {
289         HILOGE("get ability server failed, errCode = %{public}d", errCode);
290         return errCode;
291     }
292     std::vector<MissionInfo> amsMissions;
293     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->GetMissionInfos("", numMissions, amsMissions);
294     if (ret != ERR_OK) {
295         HILOGE("GetMissionInfos failed, ret = %{public}d", ret);
296         return ret;
297     }
298     if (amsMissions.empty()) {
299         HILOGI("empty missions");
300         return ERR_OK;
301     }
302     HILOGI("GetMissionInfos size:%{public}zu", amsMissions.size());
303     return MissionInfoConverter::ConvertToDstbMissionInfos(amsMissions, missionInfos);
304 }
305 
AllowMissionUid(int32_t uid)306 bool DistributedSchedAdapter::AllowMissionUid(int32_t uid)
307 {
308     if (uid < 0) {
309         HILOGE("AllowMissionUid invalid params!");
310         return false;
311     }
312     return true;
313 }
314 
RegisterMissionListener(const sptr<DstbMissionChangeListener> & listener)315 int32_t DistributedSchedAdapter::RegisterMissionListener(const sptr<DstbMissionChangeListener>& listener)
316 {
317     HILOGD("called.");
318     if (listener == nullptr) {
319         HILOGE("listener is null");
320         return INVALID_PARAMETERS_ERR;
321     }
322     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
323     if (errCode != ERR_OK) {
324         HILOGE("get ability server failed, errCode=%{public}d", errCode);
325         return errCode;
326     }
327     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->RegisterMissionListener(listener);
328     if (ret != ERR_OK) {
329         HILOGE("RegisterMissionListener failed, ret=%{public}d", ret);
330         return ret;
331     }
332     return ERR_OK;
333 }
334 
UnRegisterMissionListener(const sptr<DstbMissionChangeListener> & listener)335 int32_t DistributedSchedAdapter::UnRegisterMissionListener(const sptr<DstbMissionChangeListener>& listener)
336 {
337     if (listener == nullptr) {
338         HILOGE("listener is null");
339         return INVALID_PARAMETERS_ERR;
340     }
341     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
342     if (errCode != ERR_OK) {
343         HILOGE("get ability server failed, errCode=%{public}d", errCode);
344         return errCode;
345     }
346 
347     ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->UnRegisterMissionListener(listener);
348     if (ret != ERR_OK) {
349         HILOGE("UnRegisterMissionListener failed, ret=%{public}d", ret);
350         return ret;
351     }
352     return ERR_OK;
353 }
354 
GetLocalMissionSnapshotInfo(const std::string & networkId,int32_t missionId,MissionSnapshot & missionSnapshot)355 int32_t DistributedSchedAdapter::GetLocalMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
356     MissionSnapshot& missionSnapshot)
357 {
358     int64_t begin = GetTickCount();
359     ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
360     if (errCode != ERR_OK) {
361         HILOGE("get ability server failed, errCode=%{public}d", errCode);
362         return errCode;
363     }
364     errCode = AAFwk::AbilityManagerClient::GetInstance()->GetMissionSnapshot(networkId,
365         missionId, missionSnapshot);
366     HILOGI("[PerformanceTest] GetMissionSnapshot spend %{public}" PRId64 " ms", GetTickCount() - begin);
367     if (errCode != ERR_OK) {
368         HILOGE("get mission snapshot failed, missionId=%{public}d, errCode=%{public}d", missionId, errCode);
369         return errCode;
370     }
371     if (missionSnapshot.snapshot == nullptr) {
372         HILOGE("pixel map is nullptr!");
373         return ERR_NULL_OBJECT;
374     }
375     HILOGD("pixelMap size:%{public}d", missionSnapshot.snapshot->GetCapacity());
376     return ERR_OK;
377 }
378 
GetOsdSwitch()379 int32_t DistributedSchedAdapter::GetOsdSwitch()
380 {
381     return MISSION_OSD_ENABLED;
382 }
383 
OnOsdEventOccur(int32_t flag)384 void DistributedSchedAdapter::OnOsdEventOccur(int32_t flag)
385 {
386 }
387 } // namespace DistributedSchedule
388 } // namespace OHOS
389