• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "dms_client.h"
17 
18 #include "ability_manager_errors.h"
19 #include "if_system_ability_manager.h"
20 #include "ipc_skeleton.h"
21 #include "iservice_registry.h"
22 #include "string_ex.h"
23 #include "system_ability_definition.h"
24 
25 #include "distributed_parcel_helper.h"
26 #include "distributedsched_ipc_interface_code.h"
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 namespace {
31 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
32 constexpr uint32_t DSCHED_EVENT_MAX_NUM = 10000;
33 }
34 
GetDmsProxy()35 sptr<IRemoteObject> DistributedClient::GetDmsProxy()
36 {
37     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38     if (samgrProxy == nullptr) {
39         HILOG_ERROR("fail to get samgr.");
40         return nullptr;
41     }
42     return samgrProxy->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID);
43 }
44 
RegisterDSchedEventListener(const DSchedEventType & type,const sptr<IDSchedEventListener> & obj)45 int32_t DistributedClient::RegisterDSchedEventListener(const DSchedEventType& type,
46     const sptr<IDSchedEventListener>& obj)
47 {
48     HILOG_INFO("RegisterDSchedEventListener called");
49     sptr<IRemoteObject> remote = GetDmsProxy();
50     if (remote == nullptr) {
51         HILOG_ERROR("remote system ablity is nullptr");
52         return AAFwk::INVALID_PARAMETERS_ERR;
53     }
54     MessageParcel data;
55     MessageParcel reply;
56     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
57         return ERR_FLATTEN_OBJECT;
58     }
59     PARCEL_WRITE_HELPER(data, Uint8, type);
60     if (obj == nullptr) {
61         HILOG_ERROR("Received null IDSchedEventListener object");
62         return AAFwk::INVALID_PARAMETERS_ERR;
63     }
64     PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
65     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER),
66         data, reply);
67 }
68 
UnRegisterDSchedEventListener(const DSchedEventType & type,const sptr<IDSchedEventListener> & obj)69 int32_t DistributedClient::UnRegisterDSchedEventListener(const DSchedEventType& type,
70     const sptr<IDSchedEventListener>& obj)
71 {
72     HILOG_INFO("UnRegisterDSchedEventListener called");
73     sptr<IRemoteObject> remote = GetDmsProxy();
74     if (remote == nullptr) {
75         HILOG_ERROR("remote system ablity is null");
76         return AAFwk::INVALID_PARAMETERS_ERR;
77     }
78     MessageParcel data;
79     MessageParcel reply;
80     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
81         HILOG_DEBUG("write interface token failed.");
82         return ERR_FLATTEN_OBJECT;
83     }
84     PARCEL_WRITE_HELPER(data, Uint8, type);
85     if (obj == nullptr) {
86         HILOG_ERROR("Received null IDSchedEventListener object");
87         return AAFwk::INVALID_PARAMETERS_ERR;
88     }
89     PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
90     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER),
91         data, reply);
92 }
93 
ConnectDExtensionFromRemote(const DExtConnectInfo & connectInfo,std::function<void (DExtConnectResultInfo)> callback)94 int32_t DistributedClient::ConnectDExtensionFromRemote(const DExtConnectInfo& connectInfo,
95     std::function<void(DExtConnectResultInfo)> callback)
96 {
97     HILOG_INFO("ConnectDExtensionFromRemote called");
98     sptr<IRemoteObject> remote = GetDmsProxy();
99     if (remote == nullptr) {
100         HILOG_ERROR("Remote system ablity is null");
101         return AAFwk::INVALID_PARAMETERS_ERR;
102     }
103     MessageParcel data;
104     MessageParcel reply;
105     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
106         HILOG_DEBUG("write interface token failed.");
107         return ERR_FLATTEN_OBJECT;
108     }
109     if (!data.WriteParcelable(&connectInfo)) {
110         HILOG_ERROR("Write connect info failed!");
111         return ERR_INVALID_DATA;
112     }
113 
114     MessageOption option;
115     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::DSCHED_START_DEXTENSION),
116         data, reply, option);
117     if (ret != ERR_NONE) {
118         HILOG_ERROR("Send request fail, error: %{public}d", ret);
119         return ret;
120     }
121     int32_t resultCode = reply.ReadInt32();
122     std::unique_ptr<DExtConnectResultInfo> resultInfo(reply.ReadParcelable<DExtConnectResultInfo>());
123     if (resultInfo == nullptr) {
124         HILOG_ERROR("ReadParcelable failed.");
125         return ERR_INVALID_DATA;
126     }
127 
128     if (callback == nullptr) {
129         HILOG_ERROR("Callback is nullptr.");
130         return ERR_INVALID_DATA;
131     }
132     callback(*resultInfo);
133 
134     return resultCode;
135 }
136 
GetContinueInfo(ContinueInfo & continueInfo)137 int32_t DistributedClient::GetContinueInfo(ContinueInfo &continueInfo)
138 {
139     HILOG_INFO("%{public}s called", __func__);
140     sptr<IRemoteObject> remote = GetDmsProxy();
141     if (remote == nullptr) {
142         HILOG_ERROR("remote system ablity is null");
143         return AAFwk::INVALID_PARAMETERS_ERR;
144     }
145     MessageParcel data;
146     MessageParcel reply;
147     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
148         HILOG_DEBUG("write interface token failed.");
149         return ERR_FLATTEN_OBJECT;
150     }
151 
152     MessageOption option;
153     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_CONTINUE_INFO),
154         data, reply, option);
155     if (ret != ERR_NONE) {
156         HILOG_ERROR("sendRequest fail, error: %{public}d", ret);
157         return ret;
158     }
159     continueInfo.dstNetworkId_ = reply.ReadString();
160     continueInfo.srcNetworkId_ = reply.ReadString();
161     if (continueInfo.dstNetworkId_ == "") {
162         HILOG_ERROR("read type failed!");
163         return ERR_FLATTEN_OBJECT;
164     }
165     return ret;
166 }
167 
GetDecodeDSchedEventNotify(MessageParcel & reply,EventNotify & event)168 int32_t DistributedClient::GetDecodeDSchedEventNotify(MessageParcel &reply, EventNotify &event)
169 {
170     event.eventResult_ = reply.ReadInt32();
171     event.srcNetworkId_ = reply.ReadString();
172     event.dstNetworkId_ = reply.ReadString();
173     event.srcBundleName_ = reply.ReadString();
174     event.srcModuleName_ = reply.ReadString();
175     event.srcAbilityName_ = reply.ReadString();
176     event.destBundleName_ = reply.ReadString();
177     event.destModuleName_ = reply.ReadString();
178     event.destAbilityName_ = reply.ReadString();
179     event.dSchedEventType_ = static_cast<DSchedEventType>(reply.ReadInt32());
180     event.state_ = static_cast<DSchedEventState>(reply.ReadInt32());
181     return ERR_OK;
182 }
183 
GetDSchedEventInfo(const DSchedEventType & type,std::vector<EventNotify> & events)184 int32_t DistributedClient::GetDSchedEventInfo(const DSchedEventType &type, std::vector<EventNotify> &events)
185 {
186     HILOG_INFO("%{public}s called", __func__);
187     sptr<IRemoteObject> remote = GetDmsProxy();
188     if (remote == nullptr) {
189         HILOG_ERROR("remote system ablity is null");
190         return ERR_FLATTEN_OBJECT;
191     }
192     MessageParcel data;
193     MessageParcel reply;
194     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
195         HILOG_DEBUG("write interface token failed.");
196         return ERR_FLATTEN_OBJECT;
197     }
198     PARCEL_WRITE_HELPER(data, Uint32, type);
199 
200     MessageOption option;
201     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_DSCHED_EVENT_INFO),
202         data, reply, option);
203     if (ret != ERR_OK) {
204         HILOG_ERROR("sendRequest fail, ret: %{public}d", ret);
205         return ret;
206     }
207 
208     ret = reply.ReadInt32();
209     if (ret != ERR_OK) {
210         HILOG_ERROR("Proxy get dms eventInfos from dms service fail, ret: %{public}d", ret);
211         return ret;
212     }
213 
214     uint32_t eventNum = reply.ReadUint32();
215     if (eventNum > DSCHED_EVENT_MAX_NUM) {
216         HILOG_ERROR("Proxy get dms eventInfos num %{public}u is invalid.", eventNum);
217         return AAFwk::INVALID_PARAMETERS_ERR;
218     }
219     for (uint32_t i = 0; i < eventNum; i++) {
220         EventNotify event;
221         GetDecodeDSchedEventNotify(reply, event);
222         events.emplace_back(event);
223     }
224     return ret;
225 }
226 }  // namespace DistributedSchedule
227 }  // namespace OHOS