• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "distributed_client.h"
16 
17 #include "ability_manager_errors.h"
18 #include "distributed_parcel_helper.h"
19 #include "ipc_skeleton.h"
20 #include "iservice_registry.h"
21 #include "string_ex.h"
22 #include "system_ability_definition.h"
23 #include "window_manager_hilog.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
29 }
GetDmsProxy()30 sptr<IRemoteObject> DistributedClient::GetDmsProxy()
31 {
32     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33     if (samgrProxy == nullptr) {
34         WLOGFE("fail to get samgr.");
35         return nullptr;
36     }
37     return samgrProxy->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID);
38 }
39 
GetMissionInfos(const std::string & deviceId,int32_t numMissions,std::vector<AAFwk::MissionInfo> & missionInfos)40 int32_t DistributedClient::GetMissionInfos(const std::string& deviceId, int32_t numMissions,
41                                            std::vector<AAFwk::MissionInfo>& missionInfos)
42 {
43     WLOGFI("called");
44     sptr<IRemoteObject> remote = GetDmsProxy();
45     if (remote == nullptr) {
46         WLOGFE("remote system abiity is null");
47         return AAFwk::INVALID_PARAMETERS_ERR;
48     }
49 
50     MessageParcel data;
51     MessageParcel reply;
52     MessageOption option;
53     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
54         return ERR_FLATTEN_OBJECT;
55     }
56     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(deviceId));
57     PARCEL_WRITE_HELPER(data, Int32, numMissions);
58     int32_t ret = remote->SendRequest(GET_MISSION_INFOS, data, reply, option);
59     if (ret != ERR_NONE) {
60         WLOGFW("sendRequest fail, error: %{public}d", ret);
61         return ret;
62     }
63     return ReadMissionInfosFromParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
64 }
65 
GetRemoteMissionSnapshotInfo(const std::string & deviceId,int32_t missionId,std::unique_ptr<AAFwk::MissionSnapshot> & missionSnapshot)66 int32_t DistributedClient::GetRemoteMissionSnapshotInfo(const std::string& deviceId, int32_t missionId,
67                                                         std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot)
68 {
69     if (deviceId.empty()) {
70         WLOGFE("deviceId is null");
71         return ERR_NULL_OBJECT;
72     }
73     sptr<IRemoteObject> remote = GetDmsProxy();
74     if (remote == nullptr) {
75         WLOGFE("remote is null");
76         return AAFwk::INVALID_PARAMETERS_ERR;
77     }
78     MessageParcel data;
79     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
80         return ERR_FLATTEN_OBJECT;
81     }
82     PARCEL_WRITE_HELPER(data, String, deviceId);
83     PARCEL_WRITE_HELPER(data, Int32, missionId);
84     MessageParcel reply;
85     MessageOption option;
86     int32_t error = remote->SendRequest(GET_REMOTE_MISSION_SNAPSHOT_INFO, data, reply, option);
87     if (error != ERR_NONE) {
88         WLOGFE("transact failed, error: %{public}d", error);
89         return error;
90     }
91     std::unique_ptr<AAFwk::MissionSnapshot> missionSnapshotPtr(reply.ReadParcelable<AAFwk::MissionSnapshot>());
92     missionSnapshot = std::move(missionSnapshotPtr);
93     return ERR_NONE;
94 }
95 
ReadMissionInfosFromParcel(Parcel & parcel,std::vector<AAFwk::MissionInfo> & missionInfos)96 bool DistributedClient::ReadMissionInfosFromParcel(Parcel& parcel,
97                                                    std::vector<AAFwk::MissionInfo>& missionInfos)
98 {
99     int32_t hasMissions = parcel.ReadInt32();
100     if (hasMissions == 1) {
101         int32_t len = parcel.ReadInt32();
102         WLOGFD("readLength is:%{public}d", len);
103         if (len < 0) {
104             return false;
105         }
106         size_t size = static_cast<size_t>(len);
107         if ((size > parcel.GetReadableBytes()) || (missionInfos.max_size() < size)) {
108             WLOGFE("Failed to read MissionInfo vector, size = %{public}zu", size);
109             return false;
110         }
111         missionInfos.clear();
112         for (size_t i = 0; i < size; i++) {
113             AAFwk::MissionInfo *ptr = parcel.ReadParcelable<AAFwk::MissionInfo>();
114             if (ptr == nullptr) {
115                 WLOGFW("read MissionInfo failed");
116                 return false;
117             }
118             missionInfos.emplace_back(*ptr);
119             delete ptr;
120         }
121     }
122     WLOGFI("info size is:%{public}zu", missionInfos.size());
123     return true;
124 }
125 
SetMissionContinueState(int32_t missionId,const AAFwk::ContinueState & state)126 int32_t DistributedClient::SetMissionContinueState(int32_t missionId, const AAFwk::ContinueState &state)
127 {
128     WLOGFI("SetMissionContinueState called. Mission id: %{public}d, state: %{public}d", missionId, state);
129     sptr<IRemoteObject> remote = GetDmsProxy();
130     if (remote == nullptr) {
131         WLOGFI("remote system ablity is null");
132         return AAFwk::INVALID_PARAMETERS_ERR;
133     }
134     MessageParcel data;
135     MessageParcel reply;
136     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
137         return ERR_FLATTEN_OBJECT;
138     }
139     PARCEL_WRITE_HELPER(data, Int32, missionId);
140     PARCEL_WRITE_HELPER(data, Int32, static_cast<int32_t>(state));
141     PARCEL_TRANSACT_SYNC_RET_INT(remote, SET_MISSION_CONTINUE_STATE, data, reply);
142 }
143 } // namespace Rosen
144 } // namespace OHOS
145