• 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 
ConnectDExtAbility(std::string & bundleName,std::string & abilityName,int32_t userId)94 int32_t DistributedClient::ConnectDExtAbility(std::string& bundleName, std::string& abilityName, int32_t userId)
95 {
96     HILOG_INFO("call begin.");
97     sptr<IRemoteObject> remote = GetDmsProxy();
98     if (remote == nullptr) {
99         HILOG_ERROR("remote system ablity is null");
100         return AAFwk::INVALID_PARAMETERS_ERR;
101     }
102     MessageParcel data;
103     MessageParcel reply;
104     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
105         HILOG_DEBUG("write interface token failed.");
106         return ERR_FLATTEN_OBJECT;
107     }
108     PARCEL_WRITE_HELPER(data, String, bundleName);
109     PARCEL_WRITE_HELPER(data, String, abilityName);
110     PARCEL_WRITE_HELPER(data, Int32, userId);
111 
112     MessageOption option;
113     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::DSCHED_START_DEXTENSION),
114         data, reply, option);
115     HILOG_INFO("call end.");
116     return ret;
117 }
118 
GetContinueInfo(ContinueInfo & continueInfo)119 int32_t DistributedClient::GetContinueInfo(ContinueInfo &continueInfo)
120 {
121     HILOG_INFO("%{public}s called", __func__);
122     sptr<IRemoteObject> remote = GetDmsProxy();
123     if (remote == nullptr) {
124         HILOG_ERROR("remote system ablity is null");
125         return AAFwk::INVALID_PARAMETERS_ERR;
126     }
127     MessageParcel data;
128     MessageParcel reply;
129     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
130         HILOG_DEBUG("write interface token failed.");
131         return ERR_FLATTEN_OBJECT;
132     }
133 
134     MessageOption option;
135     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_CONTINUE_INFO),
136         data, reply, option);
137     if (ret != ERR_NONE) {
138         HILOG_ERROR("sendRequest fail, error: %{public}d", ret);
139         return ret;
140     }
141     continueInfo.dstNetworkId_ = reply.ReadString();
142     continueInfo.srcNetworkId_ = reply.ReadString();
143     if (continueInfo.dstNetworkId_ == "") {
144         HILOG_ERROR("read type failed!");
145         return ERR_FLATTEN_OBJECT;
146     }
147     return ret;
148 }
149 
GetDecodeDSchedEventNotify(MessageParcel & reply,EventNotify & event)150 int32_t DistributedClient::GetDecodeDSchedEventNotify(MessageParcel &reply, EventNotify &event)
151 {
152     event.eventResult_ = reply.ReadInt32();
153     event.srcNetworkId_ = reply.ReadString();
154     event.dstNetworkId_ = reply.ReadString();
155     event.srcBundleName_ = reply.ReadString();
156     event.srcModuleName_ = reply.ReadString();
157     event.srcAbilityName_ = reply.ReadString();
158     event.destBundleName_ = reply.ReadString();
159     event.destModuleName_ = reply.ReadString();
160     event.destAbilityName_ = reply.ReadString();
161     event.dSchedEventType_ = static_cast<DSchedEventType>(reply.ReadInt32());
162     event.state_ = static_cast<DSchedEventState>(reply.ReadInt32());
163     return ERR_OK;
164 }
165 
GetDSchedEventInfo(const DSchedEventType & type,std::vector<EventNotify> & events)166 int32_t DistributedClient::GetDSchedEventInfo(const DSchedEventType &type, std::vector<EventNotify> &events)
167 {
168     HILOG_INFO("%{public}s called", __func__);
169     sptr<IRemoteObject> remote = GetDmsProxy();
170     if (remote == nullptr) {
171         HILOG_ERROR("remote system ablity is null");
172         return ERR_FLATTEN_OBJECT;
173     }
174     MessageParcel data;
175     MessageParcel reply;
176     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
177         HILOG_DEBUG("write interface token failed.");
178         return ERR_FLATTEN_OBJECT;
179     }
180     PARCEL_WRITE_HELPER(data, Uint32, type);
181 
182     MessageOption option;
183     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_DSCHED_EVENT_INFO),
184         data, reply, option);
185     if (ret != ERR_OK) {
186         HILOG_ERROR("sendRequest fail, ret: %{public}d", ret);
187         return ret;
188     }
189 
190     ret = reply.ReadInt32();
191     if (ret != ERR_OK) {
192         HILOG_ERROR("Proxy get dms eventInfos from dms service fail, ret: %{public}d", ret);
193         return ret;
194     }
195 
196     uint32_t eventNum = reply.ReadUint32();
197     if (eventNum > DSCHED_EVENT_MAX_NUM) {
198         HILOG_ERROR("Proxy get dms eventInfos num %{public}u is invalid.", eventNum);
199         return AAFwk::INVALID_PARAMETERS_ERR;
200     }
201     for (uint32_t i = 0; i < eventNum; i++) {
202         EventNotify event;
203         GetDecodeDSchedEventNotify(reply, event);
204         events.emplace_back(event);
205     }
206     return ret;
207 }
208 }  // namespace DistributedSchedule
209 }  // namespace OHOS