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_stub.h"
17
18 #include "ability_info.h"
19 #include "adapter/dnetwork_adapter.h"
20 #include "caller_info.h"
21 #include "datetime_ex.h"
22 #include "distributed_sched_permission.h"
23 #include "dtbschedmgr_log.h"
24 #include "dtbschedmgr_device_info_storage.h"
25 #include "image_source.h"
26 #include "ipc_skeleton.h"
27 #include "message_parcel.h"
28 #include "mission/distributed_sched_mission_manager.h"
29 #include "mission/mission_info_converter.h"
30 #include "mission/snapshot_converter.h"
31 #include "parcel_helper.h"
32
33 namespace OHOS {
34 namespace DistributedSchedule {
35 using namespace std;
36 using namespace AAFwk;
37 using namespace AppExecFwk;
38
39 namespace {
40 constexpr int32_t HID_HAP = 10000; /* first hap user */
41 const std::string TAG = "DistributedSchedStub";
42 const std::u16string DMS_STUB_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
43 const std::string EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
44 const std::string PERMISSION_DISTRIBUTED_DATASYNC = "ohos.permission.DISTRIBUTED_DATASYNC";
45 }
46
DistributedSchedStub()47 DistributedSchedStub::DistributedSchedStub()
48 {
49 localFuncsMap_[START_REMOTE_ABILITY] = &DistributedSchedStub::StartRemoteAbilityInner;
50 localFuncsMap_[CONTINUE_MISSION] = &DistributedSchedStub::ContinueMissionInner;
51 localFuncsMap_[START_CONTINUATION] = &DistributedSchedStub::StartContinuationInner;
52 localFuncsMap_[NOTIFY_COMPLETE_CONTINUATION] = &DistributedSchedStub::NotifyCompleteContinuationInner;
53 localFuncsMap_[CONNECT_REMOTE_ABILITY] = &DistributedSchedStub::ConnectRemoteAbilityInner;
54 localFuncsMap_[DISCONNECT_REMOTE_ABILITY] = &DistributedSchedStub::DisconnectRemoteAbilityInner;
55 // request codes for mission mananger
56 localFuncsMap_[CHECK_SUPPORTED_OSD] = &DistributedSchedStub::CheckSupportOsdInner;
57 localFuncsMap_[STORE_SNAPSHOT_INFO] = &DistributedSchedStub::StoreSnapshotInfoInner;
58 localFuncsMap_[REMOVE_SNAPSHOT_INFO] = &DistributedSchedStub::RemoveSnapshotInfoInner;
59 localFuncsMap_[GET_REMOTE_SNAPSHOT_INFO] = &DistributedSchedStub::GetRemoteSnapshotInfoInner;
60 localFuncsMap_[GET_REMOTE_MISSION_SNAPSHOT_INFO] = &DistributedSchedStub::GetRemoteMissionSnapshotInfoInner;
61 localFuncsMap_[REGISTER_MISSION_LISTENER] = &DistributedSchedStub::RegisterMissionListenerInner;
62 localFuncsMap_[UNREGISTER_MISSION_LISTENER] = &DistributedSchedStub::UnRegisterMissionListenerInner;
63 localFuncsMap_[GET_MISSION_INFOS] = &DistributedSchedStub::GetMissionInfosInner;
64 localFuncsMap_[START_SYNC_MISSIONS] = &DistributedSchedStub::StartSyncRemoteMissionsInner;
65 localFuncsMap_[STOP_SYNC_MISSIONS] = &DistributedSchedStub::StopSyncRemoteMissionsInner;
66 localFuncsMap_[SWITCH_CHANGED] = &DistributedSchedStub::NotifyOsdSwitchChangedInner;
67 localFuncsMap_[GET_CACHED_SUPPORTED_OSD] = &DistributedSchedStub::GetCachedOsdSwitchInner;
68
69 remoteFuncsMap_[START_ABILITY_FROM_REMOTE] = &DistributedSchedStub::StartAbilityFromRemoteInner;
70 remoteFuncsMap_[NOTIFY_CONTINUATION_RESULT_FROM_REMOTE] =
71 &DistributedSchedStub::NotifyContinuationResultFromRemoteInner;
72 remoteFuncsMap_[CONNECT_ABILITY_FROM_REMOTE] = &DistributedSchedStub::ConnectAbilityFromRemoteInner;
73 remoteFuncsMap_[DISCONNECT_ABILITY_FROM_REMOTE] = &DistributedSchedStub::DisconnectAbilityFromRemoteInner;
74 remoteFuncsMap_[NOTIFY_PROCESS_DIED_FROM_REMOTE] = &DistributedSchedStub::NotifyProcessDiedFromRemoteInner;
75 // request codes for mission mananger
76 remoteFuncsMap_[CHECK_SUPPORT_OSD_FROM_REMOTE] = &DistributedSchedStub::GetOsdSwitchValueFromRemoteInner;
77 remoteFuncsMap_[START_SYNC_MISSIONS_FROM_REMOTE] =
78 &DistributedSchedStub::StartSyncMissionsFromRemoteInner;
79 remoteFuncsMap_[STOP_SYNC_MISSIONS_FROM_REMOTE] =
80 &DistributedSchedStub::StopSyncMissionsFromRemoteInner;
81 remoteFuncsMap_[NOTIFY_MISSIONS_CHANGED_FROM_REMOTE] = &DistributedSchedStub::NotifyMissionsChangedFromRemoteInner;
82 remoteFuncsMap_[NOTIFY_SWITCH_CHANGED_FROM_REMOTE] = &DistributedSchedStub::UpdateOsdSwitchValueFromRemoteInner;
83 remoteFuncsMap_[CONTINUE_MISSION] = &DistributedSchedStub::ContinueMissionInner;
84
85 // request codes for call ability
86 localFuncsMap_[START_REMOTE_ABILITY_BY_CALL] = &DistributedSchedStub::StartRemoteAbilityByCallInner;
87 localFuncsMap_[RELEASE_REMOTE_ABILITY] = &DistributedSchedStub::ReleaseRemoteAbilityInner;
88 remoteFuncsMap_[START_ABILITY_BY_CALL_FROM_REMOTE] = &DistributedSchedStub::StartAbilityByCallFromRemoteInner;
89 remoteFuncsMap_[RELEASE_ABILITY_FROM_REMOTE] = &DistributedSchedStub::ReleaseAbilityFromRemoteInner;
90 }
91
~DistributedSchedStub()92 DistributedSchedStub::~DistributedSchedStub()
93 {
94 remoteFuncsMap_.clear();
95 localFuncsMap_.clear();
96 }
97
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)98 int32_t DistributedSchedStub::OnRemoteRequest(uint32_t code,
99 MessageParcel& data, MessageParcel& reply, MessageOption& option)
100 {
101 bool IsLocalCalling = IPCSkeleton::IsLocalCalling();
102 HILOGI("OnRemoteRequest, code = %{public}d, flags = %{public}d, IsLocalCalling = %{public}d.",
103 code, option.GetFlags(), IsLocalCalling);
104
105 const auto& funcsMap = IsLocalCalling ? localFuncsMap_ : remoteFuncsMap_;
106 auto iter = funcsMap.find(code);
107 if (iter != funcsMap.end()) {
108 auto func = iter->second;
109 if (!EnforceInterfaceToken(data)) {
110 HILOGW("OnRemoteRequest interface token check failed!");
111 return DMS_PERMISSION_DENIED;
112 }
113 if (func != nullptr) {
114 return (this->*func)(data, reply);
115 }
116 }
117
118 HILOGW("OnRemoteRequest default case, need check.");
119 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
120 }
121
StartRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)122 int32_t DistributedSchedStub::StartRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
123 {
124 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
125 if (want == nullptr) {
126 HILOGW("START_ABILITY want readParcelable failed!");
127 return ERR_NULL_OBJECT;
128 }
129 int32_t callerUid = 0;
130 PARCEL_READ_HELPER(data, Int32, callerUid);
131 HILOGI("get callerUid = %{public}d", callerUid);
132 int32_t requestCode = 0;
133 PARCEL_READ_HELPER(data, Int32, requestCode);
134 uint32_t accessToken = 0;
135 PARCEL_READ_HELPER(data, Uint32, accessToken);
136 HILOGI("get AccessTokenID = %{public}u", accessToken);
137 if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
138 PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
139 HILOGE("check data_sync permission failed!");
140 return DMS_PERMISSION_DENIED;
141 }
142 int32_t result = StartRemoteAbility(*want, callerUid, requestCode, accessToken);
143 HILOGI("StartRemoteAbilityInner result = %{public}d", result);
144 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
145 }
146
StartAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)147 int32_t DistributedSchedStub::StartAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
148 {
149 if (!CheckCallingUid()) {
150 HILOGW("request DENIED!");
151 return DMS_PERMISSION_DENIED;
152 }
153
154 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
155 if (want == nullptr) {
156 HILOGW("want readParcelable failed!");
157 return ERR_NULL_OBJECT;
158 }
159 unique_ptr<CompatibleAbilityInfo> cmpAbilityInfo(data.ReadParcelable<CompatibleAbilityInfo>());
160 if (cmpAbilityInfo == nullptr) {
161 HILOGW("AbilityInfo readParcelable failed!");
162 return ERR_NULL_OBJECT;
163 }
164 AbilityInfo abilityInfo;
165 cmpAbilityInfo->ConvertToAbilityInfo(abilityInfo);
166 std::string package = abilityInfo.bundleName;
167 std::string deviceId = abilityInfo.deviceId;
168 int64_t begin = GetTickCount();
169 int32_t requestCode = 0;
170 PARCEL_READ_HELPER(data, Int32, requestCode);
171 CallerInfo callerInfo;
172 PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
173 PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
174 callerInfo.callerType = CALLER_TYPE_HARMONY;
175 AccountInfo accountInfo;
176 accountInfo.accountType = data.ReadInt32();
177 PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
178 callerInfo.callerAppId = data.ReadString();
179 std::string extraInfo = data.ReadString();
180 if (extraInfo.empty()) {
181 HILOGD("extra info is empty!");
182 }
183 nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
184 if (!extraInfoJson.is_discarded()) {
185 uint32_t accessToken = extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN];
186 callerInfo.accessToken = accessToken;
187 HILOGD("parse extra info, accessTokenID = %{public}u", accessToken);
188 }
189 int32_t result = StartAbilityFromRemote(*want, abilityInfo, requestCode, callerInfo, accountInfo);
190 HILOGI("result = %{public}d", result);
191 PARCEL_WRITE_HELPER(reply, Int32, result);
192 int64_t end = GetTickCount();
193 PARCEL_WRITE_HELPER(reply, Int64, end - begin);
194 PARCEL_WRITE_HELPER(reply, String, package);
195 PARCEL_WRITE_HELPER(reply, String, deviceId);
196 return ERR_NONE;
197 }
198
ContinueMissionInner(MessageParcel & data,MessageParcel & reply)199 int32_t DistributedSchedStub::ContinueMissionInner(MessageParcel& data, MessageParcel& reply)
200 {
201 std::string srcDevId;
202 std::string dstDevId;
203 PARCEL_READ_HELPER(data, String, srcDevId);
204 PARCEL_READ_HELPER(data, String, dstDevId);
205 int32_t missionId = 0;
206 PARCEL_READ_HELPER(data, Int32, missionId);
207 sptr<IRemoteObject> callback = data.ReadRemoteObject();
208 if (callback == nullptr) {
209 HILOGW("read callback failed!");
210 return ERR_NULL_OBJECT;
211 }
212 shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
213 if (wantParams == nullptr) {
214 HILOGW("wantParams readParcelable failed!");
215 return ERR_NULL_OBJECT;
216 }
217 int32_t result = ContinueMission(srcDevId, dstDevId, missionId, callback, *wantParams);
218 HILOGI("result = %{public}d", result);
219 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
220 }
221
StartContinuationInner(MessageParcel & data,MessageParcel & reply)222 int32_t DistributedSchedStub::StartContinuationInner(MessageParcel& data, MessageParcel& reply)
223 {
224 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
225 if (want == nullptr) {
226 HILOGW("want readParcelable failed!");
227 return ERR_NULL_OBJECT;
228 }
229 int32_t missionId = data.ReadInt32();
230 int32_t callerUid = data.ReadInt32();
231 int32_t status = data.ReadInt32();
232 uint32_t accessToken = 0;
233 PARCEL_READ_HELPER(data, Uint32, accessToken);
234 HILOGI("get AccessTokenID = %{public}u", accessToken);
235 if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
236 PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
237 HILOGE("check data_sync permission failed!");
238 return DMS_PERMISSION_DENIED;
239 }
240 int32_t result = StartContinuation(*want, missionId, callerUid, status, accessToken);
241 HILOGI("result = %{public}d", result);
242 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
243 }
244
NotifyCompleteContinuationInner(MessageParcel & data,MessageParcel & reply)245 int32_t DistributedSchedStub::NotifyCompleteContinuationInner(MessageParcel& data,
246 [[maybe_unused]] MessageParcel& reply)
247 {
248 u16string devId = data.ReadString16();
249 if (devId.empty()) {
250 HILOGE("devId is empty!");
251 return INVALID_PARAMETERS_ERR;
252 }
253 int32_t sessionId = 0;
254 PARCEL_READ_HELPER(data, Int32, sessionId);
255 bool continuationResult = false;
256 PARCEL_READ_HELPER(data, Bool, continuationResult);
257 NotifyCompleteContinuation(devId, sessionId, continuationResult);
258 return ERR_OK;
259 }
260
NotifyContinuationResultFromRemoteInner(MessageParcel & data,MessageParcel & reply)261 int32_t DistributedSchedStub::NotifyContinuationResultFromRemoteInner(MessageParcel& data,
262 [[maybe_unused]] MessageParcel& reply)
263 {
264 if (!CheckCallingUid()) {
265 HILOGW("request DENIED!");
266 return DMS_PERMISSION_DENIED;
267 }
268
269 int32_t sessionId = 0;
270 PARCEL_READ_HELPER(data, Int32, sessionId);
271 bool continuationResult = false;
272 PARCEL_READ_HELPER(data, Bool, continuationResult);
273 return NotifyContinuationResultFromRemote(sessionId, continuationResult);
274 }
275
ConnectRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)276 int32_t DistributedSchedStub::ConnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
277 {
278 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
279 if (want == nullptr) {
280 HILOGW("want readParcelable failed!");
281 return ERR_NULL_OBJECT;
282 }
283 sptr<IRemoteObject> connect = data.ReadRemoteObject();
284 int32_t callerUid = 0;
285 PARCEL_READ_HELPER(data, Int32, callerUid);
286 HILOGI("get callerUid = %{public}d", callerUid);
287 int32_t callerPid = 0;
288 PARCEL_READ_HELPER(data, Int32, callerPid);
289 HILOGI("get callerPid = %{public}d", callerPid);
290 uint32_t accessToken = 0;
291 PARCEL_READ_HELPER(data, Uint32, accessToken);
292 HILOGI("get AccessTokenID = %{public}u", accessToken);
293 if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
294 PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
295 HILOGE("check data_sync permission failed!");
296 return DMS_PERMISSION_DENIED;
297 }
298 int32_t result = ConnectRemoteAbility(*want, connect, callerUid, callerPid, accessToken);
299 HILOGI("result = %{public}d", result);
300 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
301 }
302
DisconnectRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)303 int32_t DistributedSchedStub::DisconnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
304 {
305 sptr<IRemoteObject> connect = data.ReadRemoteObject();
306 int32_t callerUid = 0;
307 PARCEL_READ_HELPER(data, Int32, callerUid);
308 HILOGI("get callerUid = %{public}d", callerUid);
309 uint32_t accessToken = 0;
310 PARCEL_READ_HELPER(data, Uint32, accessToken);
311 HILOGI("get AccessTokenID = %{public}u", accessToken);
312 if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
313 PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
314 HILOGE("check data_sync permission failed!");
315 return DMS_PERMISSION_DENIED;
316 }
317 int32_t result = DisconnectRemoteAbility(connect, callerUid, accessToken);
318 HILOGI("result = %{public}d", result);
319 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
320 }
321
ConnectAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)322 int32_t DistributedSchedStub::ConnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
323 {
324 if (!CheckCallingUid()) {
325 HILOGW("request DENIED!");
326 return DMS_PERMISSION_DENIED;
327 }
328
329 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
330 if (want == nullptr) {
331 HILOGW("want readParcelable failed!");
332 return ERR_NULL_OBJECT;
333 }
334 unique_ptr<CompatibleAbilityInfo> cmpAbilityInfo(data.ReadParcelable<CompatibleAbilityInfo>());
335 if (cmpAbilityInfo == nullptr) {
336 HILOGW("abilityInfo readParcelable failed!");
337 return ERR_NULL_OBJECT;
338 }
339 AbilityInfo abilityInfo;
340 cmpAbilityInfo->ConvertToAbilityInfo(abilityInfo);
341 sptr<IRemoteObject> connect = data.ReadRemoteObject();
342 CallerInfo callerInfo;
343 PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
344 PARCEL_READ_HELPER(data, Int32, callerInfo.pid);
345 PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
346 callerInfo.callerType = CALLER_TYPE_HARMONY;
347 AccountInfo accountInfo;
348 accountInfo.accountType = data.ReadInt32();
349 PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
350 callerInfo.callerAppId = data.ReadString();
351 std::string extraInfo = data.ReadString();
352 if (extraInfo.empty()) {
353 HILOGD("extra info is empty!");
354 }
355 nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
356 if (!extraInfoJson.is_discarded()) {
357 uint32_t accessToken = extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN];
358 callerInfo.accessToken = accessToken;
359 HILOGD("parse extra info, accessTokenID = %{public}u", accessToken);
360 }
361 std::string package = abilityInfo.bundleName;
362 std::string deviceId = abilityInfo.deviceId;
363 int64_t begin = GetTickCount();
364 int32_t result = ConnectAbilityFromRemote(*want, abilityInfo, connect, callerInfo, accountInfo);
365 HILOGW("result = %{public}d", result);
366 int64_t end = GetTickCount();
367 PARCEL_WRITE_HELPER(reply, Int32, result);
368 PARCEL_WRITE_HELPER(reply, Int64, end - begin);
369 PARCEL_WRITE_HELPER(reply, String, package);
370 PARCEL_WRITE_HELPER(reply, String, deviceId);
371 return ERR_NONE;
372 }
373
DisconnectAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)374 int32_t DistributedSchedStub::DisconnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
375 {
376 if (!CheckCallingUid()) {
377 HILOGW("request DENIED!");
378 return DMS_PERMISSION_DENIED;
379 }
380
381 sptr<IRemoteObject> connect = data.ReadRemoteObject();
382 int32_t uid = 0;
383 PARCEL_READ_HELPER(data, Int32, uid);
384 string sourceDeviceId;
385 PARCEL_READ_HELPER(data, String, sourceDeviceId);
386 int32_t result = DisconnectAbilityFromRemote(connect, uid, sourceDeviceId);
387 HILOGI("result %{public}d", result);
388 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
389 }
390
NotifyProcessDiedFromRemoteInner(MessageParcel & data,MessageParcel & reply)391 int32_t DistributedSchedStub::NotifyProcessDiedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
392 {
393 if (!CheckCallingUid()) {
394 HILOGW("request DENIED!");
395 return DMS_PERMISSION_DENIED;
396 }
397
398 int32_t uid = 0;
399 PARCEL_READ_HELPER(data, Int32, uid);
400 int32_t pid = 0;
401 PARCEL_READ_HELPER(data, Int32, pid);
402 string sourceDeviceId;
403 PARCEL_READ_HELPER(data, String, sourceDeviceId);
404 CallerInfo callerInfo;
405 callerInfo.uid = uid;
406 callerInfo.pid = pid;
407 callerInfo.sourceDeviceId = sourceDeviceId;
408 callerInfo.callerType = CALLER_TYPE_HARMONY;
409 int32_t result = NotifyProcessDiedFromRemote(callerInfo);
410 HILOGI("result %{public}d", result);
411 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
412 }
413
CheckCallingUid()414 bool DistributedSchedStub::CheckCallingUid()
415 {
416 // never allow non-system uid for distributed request
417 auto callingUid = IPCSkeleton::GetCallingUid();
418 return callingUid < HID_HAP;
419 }
420
EnforceInterfaceToken(MessageParcel & data)421 bool DistributedSchedStub::EnforceInterfaceToken(MessageParcel& data)
422 {
423 u16string interfaceToken = data.ReadInterfaceToken();
424 return interfaceToken == DMS_STUB_INTERFACE_TOKEN;
425 }
426
GetMissionInfosInner(MessageParcel & data,MessageParcel & reply)427 int32_t DistributedSchedStub::GetMissionInfosInner(MessageParcel& data, MessageParcel& reply)
428 {
429 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
430 std::u16string deviceId = data.ReadString16();
431 int32_t numMissions = 0;
432 PARCEL_READ_HELPER(data, Int32, numMissions);
433
434 std::vector<MissionInfo> missionInfos;
435 int32_t result = GetMissionInfos(Str16ToStr8(deviceId), numMissions, missionInfos);
436 HILOGI("result = %{public}d", result);
437 if (result == ERR_NONE) {
438 result = MissionInfoConverter::WriteMissionInfosToParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
439 }
440 return result;
441 }
442
GetRemoteSnapshotInfoInner(MessageParcel & data,MessageParcel & reply)443 int32_t DistributedSchedStub::GetRemoteSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)
444 {
445 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
446 u16string deviceId = data.ReadString16();
447 int32_t missionId = 0;
448 PARCEL_READ_HELPER(data, Int32, missionId);
449 if (deviceId.empty()) {
450 HILOGE("deviceId is empty!");
451 return ERR_FLATTEN_OBJECT;
452 }
453 unique_ptr<Snapshot> snapShot = GetRemoteSnapshotInfo(deviceId, missionId);
454 if (snapShot == nullptr) {
455 HILOGE("snapShot object nullptr!");
456 return ERR_NULL_OBJECT;
457 }
458 bool ret = snapShot->WriteToParcel(reply);
459 if (!ret) {
460 HILOGE("snapShot WriteToParcel failed!");
461 return ERR_NULL_OBJECT;
462 }
463 std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(Str16ToStr8(deviceId));
464 if (uuid.empty()) {
465 HILOGE("uuid is empty!");
466 return ERR_NULL_OBJECT;
467 }
468 DistributedSchedMissionManager::GetInstance().EnqueueCachedSnapshotInfo(uuid,
469 missionId, std::move(snapShot));
470 return ERR_NONE;
471 }
472
GetRemoteMissionSnapshotInfoInner(MessageParcel & data,MessageParcel & reply)473 int32_t DistributedSchedStub::GetRemoteMissionSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)
474 {
475 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
476 string networkId = data.ReadString();
477 if (networkId.empty()) {
478 HILOGE("networkId is empty!");
479 return ERR_FLATTEN_OBJECT;
480 }
481 int32_t missionId = 0;
482 PARCEL_READ_HELPER(data, Int32, missionId);
483 if (missionId < 0) {
484 HILOGE("missionId is invalid");
485 return INVALID_PARAMETERS_ERR;
486 }
487 std::unique_ptr<MissionSnapshot> missionSnapshotPtr = std::make_unique<MissionSnapshot>();
488 int32_t errCode = GetRemoteMissionSnapshotInfo(networkId, missionId, missionSnapshotPtr);
489 if (errCode != ERR_NONE) {
490 HILOGE("get mission snapshot failed!");
491 return ERR_NULL_OBJECT;
492 }
493 PARCEL_WRITE_HELPER(reply, Parcelable, missionSnapshotPtr.get());
494 std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId);
495 if (uuid.empty()) {
496 HILOGE("uuid is empty!");
497 return ERR_NULL_OBJECT;
498 }
499 std::unique_ptr<Snapshot> snapshotPtr = make_unique<Snapshot>();
500 SnapshotConverter::ConvertToSnapshot(*missionSnapshotPtr, snapshotPtr);
501 DistributedSchedMissionManager::GetInstance().EnqueueCachedSnapshotInfo(uuid,
502 missionId, std::move(snapshotPtr));
503 return ERR_NONE;
504 }
505
RegisterMissionListenerInner(MessageParcel & data,MessageParcel & reply)506 int32_t DistributedSchedStub::RegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)
507 {
508 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
509 u16string devId = data.ReadString16();
510 if (devId.empty()) {
511 HILOGW("read deviceId failed!");
512 return ERR_FLATTEN_OBJECT;
513 }
514 sptr<IRemoteObject> missionChangedListener = data.ReadRemoteObject();
515 if (missionChangedListener == nullptr) {
516 HILOGW("read IRemoteObject failed!");
517 return ERR_FLATTEN_OBJECT;
518 }
519 int32_t result = RegisterMissionListener(devId, missionChangedListener);
520 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
521 }
522
UnRegisterMissionListenerInner(MessageParcel & data,MessageParcel & reply)523 int32_t DistributedSchedStub::UnRegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)
524 {
525 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
526 u16string devId = data.ReadString16();
527 if (devId.empty()) {
528 HILOGW("read deviceId failed!");
529 return ERR_FLATTEN_OBJECT;
530 }
531 sptr<IRemoteObject> missionChangedListener = data.ReadRemoteObject();
532 if (missionChangedListener == nullptr) {
533 HILOGW("read IRemoteObject failed!");
534 return ERR_FLATTEN_OBJECT;
535 }
536 int32_t result = UnRegisterMissionListener(devId, missionChangedListener);
537 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
538 }
539
StartSyncMissionsFromRemoteInner(MessageParcel & data,MessageParcel & reply)540 int32_t DistributedSchedStub::StartSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)
541 {
542 CallerInfo callerInfo;
543 if (!CallerInfoUnmarshalling(callerInfo, data)) {
544 HILOGW("read callerInfo failed!");
545 return ERR_FLATTEN_OBJECT;
546 }
547 std::vector<DstbMissionInfo> missionInfos;
548 if (StartSyncMissionsFromRemote(callerInfo, missionInfos) != ERR_NONE) {
549 return ERR_FLATTEN_OBJECT;
550 }
551 if (!reply.WriteInt32(VERSION)) {
552 return ERR_FLATTEN_OBJECT;
553 }
554 if (!DstbMissionInfo::WriteDstbMissionInfosToParcel(reply, missionInfos)) {
555 HILOGE("write mission info failed!");
556 return ERR_FLATTEN_OBJECT;
557 }
558 return ERR_NONE;
559 }
560
StopSyncRemoteMissionsInner(MessageParcel & data,MessageParcel & reply)561 int32_t DistributedSchedStub::StopSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)
562 {
563 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
564 u16string devId = data.ReadString16();
565 if (devId.empty()) {
566 HILOGW("read deviceId failed!");
567 return ERR_FLATTEN_OBJECT;
568 }
569 int32_t result = StopSyncRemoteMissions(Str16ToStr8(devId));
570 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
571 }
572
StopSyncMissionsFromRemoteInner(MessageParcel & data,MessageParcel & reply)573 int32_t DistributedSchedStub::StopSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)
574 {
575 CallerInfo callerInfo;
576 if (!CallerInfoUnmarshalling(callerInfo, data)) {
577 HILOGW("read callerInfo failed!");
578 return ERR_FLATTEN_OBJECT;
579 }
580 int32_t result = StopSyncMissionsFromRemote(callerInfo);
581 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
582 }
583
CheckSupportOsdInner(MessageParcel & data,MessageParcel & reply)584 int32_t DistributedSchedStub::CheckSupportOsdInner(MessageParcel& data, MessageParcel& reply)
585 {
586 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
587 int64_t begin = GetTickCount();
588 std::u16string deviceId = data.ReadString16();
589 std::string u8DeviceId = Str16ToStr8(deviceId);
590 int32_t result = CheckSupportOsd(u8DeviceId);
591 HILOGI("result = %{public}d, deviceId = %{public}s spend %{public}" PRId64 " ms",
592 result, DnetworkAdapter::AnonymizeDeviceId(u8DeviceId).c_str(), GetTickCount() - begin);
593 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
594 }
595
GetCachedOsdSwitchInner(MessageParcel & data,MessageParcel & reply)596 int32_t DistributedSchedStub::GetCachedOsdSwitchInner(MessageParcel& data, MessageParcel& reply)
597 {
598 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
599 std::vector<std::u16string> deviceIds;
600 std::vector<int32_t> values;
601 int64_t begin = GetTickCount();
602 GetCachedOsdSwitch(deviceIds, values);
603 HILOGI("spend %{public}" PRId64 " ms", GetTickCount() - begin);
604 PARCEL_WRITE_HELPER(reply, String16Vector, deviceIds);
605 PARCEL_WRITE_HELPER(reply, Int32Vector, values);
606 return ERR_NONE;
607 }
608
NotifyMissionsChangedFromRemoteInner(MessageParcel & data,MessageParcel & reply)609 int32_t DistributedSchedStub::NotifyMissionsChangedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
610 {
611 int32_t version = data.ReadInt32();
612 HILOGD("version is %{public}d", version);
613 std::vector<DstbMissionInfo> missionInfos;
614 if (!DstbMissionInfo::ReadDstbMissionInfosFromParcel(data, missionInfos)) {
615 return ERR_FLATTEN_OBJECT;
616 }
617 CallerInfo callerInfo;
618 callerInfo.sourceDeviceId = data.ReadString();
619 callerInfo.uid = data.ReadInt32();
620 callerInfo.pid = data.ReadInt32();
621 callerInfo.dmsVersion = data.ReadInt32();
622 int32_t result = NotifyMissionsChangedFromRemote(missionInfos, callerInfo);
623 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
624 }
625
GetOsdSwitchValueFromRemoteInner(MessageParcel & data,MessageParcel & reply)626 int32_t DistributedSchedStub::GetOsdSwitchValueFromRemoteInner(MessageParcel& data, MessageParcel& reply)
627 {
628 int32_t result = GetOsdSwitchValueFromRemote();
629 HILOGI("result = %{public}d", result);
630 PARCEL_WRITE_HELPER(reply, Int32, result);
631 return ERR_NONE;
632 }
633
StoreSnapshotInfoInner(MessageParcel & data,MessageParcel & reply)634 int32_t DistributedSchedStub::StoreSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)
635 {
636 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
637 int32_t missionId = 0;
638 if (!data.ReadInt32(missionId)) {
639 return ERR_FLATTEN_OBJECT;
640 }
641 size_t len = data.GetReadableBytes();
642 const uint8_t* byteSteam = data.ReadBuffer(len);
643 std::string deviceId;
644 DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(deviceId);
645 if (deviceId.empty()) {
646 HILOGE("get deviceId failed!");
647 return INVALID_PARAMETERS_ERR;
648 }
649 int32_t result = StoreSnapshotInfo(deviceId, missionId, byteSteam, len);
650 PARCEL_WRITE_HELPER(reply, Int32, result);
651 return ERR_NONE;
652 }
653
RemoveSnapshotInfoInner(MessageParcel & data,MessageParcel & reply)654 int32_t DistributedSchedStub::RemoveSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)
655 {
656 HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
657 int32_t missionId = 0;
658 if (!data.ReadInt32(missionId)) {
659 return ERR_FLATTEN_OBJECT;
660 }
661 std::string deviceId;
662 DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(deviceId);
663 if (deviceId.empty()) {
664 HILOGE("get deviceId failed!");
665 return INVALID_PARAMETERS_ERR;
666 }
667 int32_t result = RemoveSnapshotInfo(deviceId, missionId);
668 PARCEL_WRITE_HELPER(reply, Int32, result);
669 return ERR_NONE;
670 }
671
NotifyOsdSwitchChangedInner(MessageParcel & data,MessageParcel & reply)672 int32_t DistributedSchedStub::NotifyOsdSwitchChangedInner(MessageParcel& data, MessageParcel& reply)
673 {
674 int32_t result = DistributedSchedMissionManager::GetInstance().UpdateSwitchValueToRemote();
675 HILOGI("result: %{public}d!", result);
676 return ERR_NONE;
677 }
678
UpdateOsdSwitchValueFromRemoteInner(MessageParcel & data,MessageParcel & reply)679 int32_t DistributedSchedStub::UpdateOsdSwitchValueFromRemoteInner(MessageParcel& data, MessageParcel& reply)
680 {
681 int32_t switchVal = data.ReadInt32();
682 std::string srcUuid = data.ReadString();
683 int32_t result = UpdateOsdSwitchValueFromRemote(switchVal, srcUuid);
684 HILOGD("srcUuid: %s, result: %d!",
685 srcUuid.c_str(), result);
686 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
687 }
688
StartSyncRemoteMissionsInner(MessageParcel & data,MessageParcel & reply)689 int32_t DistributedSchedStub::StartSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)
690 {
691 HILOGI("[PerformanceTest], IPC end = %{public}" PRId64, GetTickCount());
692 u16string devId = data.ReadString16();
693 if (devId.empty()) {
694 HILOGW("read deviceId failed!");
695 return ERR_FLATTEN_OBJECT;
696 }
697 string deviceId = Str16ToStr8(devId);
698 bool fixConflict = data.ReadBool();
699 long tag = data.ReadInt64();
700 int32_t result = StartSyncRemoteMissions(deviceId, fixConflict, tag);
701 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
702 }
703
CallerInfoUnmarshalling(CallerInfo & callerInfo,MessageParcel & data)704 bool DistributedSchedStub::CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data)
705 {
706 int32_t uid = -1;
707 PARCEL_READ_HELPER_RET(data, Int32, uid, false);
708 int32_t pid = -1;
709 PARCEL_READ_HELPER_RET(data, Int32, pid, false);
710 int32_t callerType = CALLER_TYPE_NONE;
711 PARCEL_READ_HELPER_RET(data, Int32, callerType, false);
712 std::string sourceDeviceId;
713 PARCEL_READ_HELPER_RET(data, String, sourceDeviceId, false);
714 HILOGI("sourceDeviceId = %{public}s",
715 DnetworkAdapter::AnonymizeDeviceId(sourceDeviceId).c_str());
716 int32_t duid = -1;
717 PARCEL_READ_HELPER_RET(data, Int32, duid, false);
718 std::string callerAppId;
719 PARCEL_READ_HELPER_RET(data, String, callerAppId, false);
720 int32_t version = -1;
721 PARCEL_READ_HELPER_RET(data, Int32, version, false);
722 callerInfo.uid = uid;
723 callerInfo.pid = pid;
724 callerInfo.callerType = callerType;
725 callerInfo.sourceDeviceId = sourceDeviceId;
726 callerInfo.duid = duid;
727 callerInfo.callerAppId = callerAppId;
728 callerInfo.dmsVersion = version;
729 return true;
730 }
731
StartRemoteAbilityByCallInner(MessageParcel & data,MessageParcel & reply)732 int32_t DistributedSchedStub::StartRemoteAbilityByCallInner(MessageParcel& data, MessageParcel& reply)
733 {
734 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
735 if (want == nullptr) {
736 HILOGW("want readParcelable failed!");
737 return ERR_NULL_OBJECT;
738 }
739 sptr<IRemoteObject> connect = data.ReadRemoteObject();
740 int32_t callerUid = 0;
741 PARCEL_READ_HELPER(data, Int32, callerUid);
742 int32_t callerPid = 0;
743 PARCEL_READ_HELPER(data, Int32, callerPid);
744 uint32_t accessToken = 0;
745 PARCEL_READ_HELPER(data, Uint32, accessToken);
746 if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
747 PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
748 HILOGE("check data_sync permission failed!");
749 return DMS_PERMISSION_DENIED;
750 }
751 int32_t result = StartRemoteAbilityByCall(*want, connect, callerUid, callerPid, accessToken);
752 HILOGI("result = %{public}d", result);
753 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
754 }
755
ReleaseRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)756 int32_t DistributedSchedStub::ReleaseRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
757 {
758 sptr<IRemoteObject> connect = data.ReadRemoteObject();
759 shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
760 if (element == nullptr) {
761 HILOGE("ReleaseRemoteAbilityInner receive element is nullptr");
762 return ERR_INVALID_VALUE;
763 }
764 int32_t result = ReleaseRemoteAbility(connect, *element);
765 HILOGI("result = %{public}d", result);
766 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
767 }
768
StartAbilityByCallFromRemoteInner(MessageParcel & data,MessageParcel & reply)769 int32_t DistributedSchedStub::StartAbilityByCallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
770 {
771 if (!CheckCallingUid()) {
772 HILOGW("request DENIED!");
773 return DMS_PERMISSION_DENIED;
774 }
775
776 sptr<IRemoteObject> connect = data.ReadRemoteObject();
777 CallerInfo callerInfo;
778 PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
779 PARCEL_READ_HELPER(data, Int32, callerInfo.pid);
780 PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
781 AccountInfo accountInfo;
782 accountInfo.accountType = data.ReadInt32();
783 PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
784 callerInfo.callerAppId = data.ReadString();
785 std::string extraInfo = data.ReadString();
786 if (extraInfo.empty()) {
787 HILOGW("read extraInfo failed!");
788 return ERR_NULL_OBJECT;
789 }
790 nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
791 if (!extraInfoJson.is_discarded()) {
792 uint32_t accessToken = extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN];
793 callerInfo.accessToken = accessToken;
794 HILOGD("parse extra info, accessToken = %{public}u", accessToken);
795 }
796 shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
797 if (want == nullptr) {
798 HILOGW("want readParcelable failed!");
799 return ERR_NULL_OBJECT;
800 }
801 int32_t result = StartAbilityByCallFromRemote(*want, connect, callerInfo, accountInfo);
802 HILOGI("result = %{public}d", result);
803 PARCEL_WRITE_HELPER(reply, Int32, result);
804 return ERR_NONE;
805 }
806
ReleaseAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)807 int32_t DistributedSchedStub::ReleaseAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
808 {
809 if (!CheckCallingUid()) {
810 HILOGW("request DENIED!");
811 return DMS_PERMISSION_DENIED;
812 }
813
814 sptr<IRemoteObject> connect = data.ReadRemoteObject();
815 shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
816 if (element == nullptr) {
817 HILOGE("ReleaseAbilityFromRemoteInner receive element is nullptr");
818 return ERR_INVALID_VALUE;
819 }
820 CallerInfo callerInfo;
821 PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
822 std::string extraInfo;
823 PARCEL_READ_HELPER(data, String, extraInfo);
824 int32_t result = ReleaseAbilityFromRemote(connect, *element, callerInfo);
825 HILOGI("result %{public}d", result);
826 PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
827 }
828 } // namespace DistributedSchedule
829 } // namespace OHOS
830