1 /*
2 * Copyright (c) 2023-2024 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
GetContinueInfo(ContinueInfo & continueInfo)94 int32_t DistributedClient::GetContinueInfo(ContinueInfo &continueInfo)
95 {
96 HILOG_INFO("%{public}s called", __func__);
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
109 MessageOption option;
110 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_CONTINUE_INFO),
111 data, reply, option);
112 if (ret != ERR_NONE) {
113 HILOG_ERROR("sendRequest fail, error: %{public}d", ret);
114 return ret;
115 }
116 continueInfo.dstNetworkId_ = reply.ReadString();
117 continueInfo.srcNetworkId_ = reply.ReadString();
118 if (continueInfo.dstNetworkId_ == "") {
119 HILOG_ERROR("read type failed!");
120 return ERR_FLATTEN_OBJECT;
121 }
122 return ret;
123 }
124
GetDecodeDSchedEventNotify(MessageParcel & reply,EventNotify & event)125 int32_t DistributedClient::GetDecodeDSchedEventNotify(MessageParcel &reply, EventNotify &event)
126 {
127 event.eventResult_ = reply.ReadInt32();
128 event.srcNetworkId_ = reply.ReadString();
129 event.dstNetworkId_ = reply.ReadString();
130 event.srcBundleName_ = reply.ReadString();
131 event.srcModuleName_ = reply.ReadString();
132 event.srcAbilityName_ = reply.ReadString();
133 event.destBundleName_ = reply.ReadString();
134 event.destModuleName_ = reply.ReadString();
135 event.destAbilityName_ = reply.ReadString();
136 event.dSchedEventType_ = static_cast<DSchedEventType>(reply.ReadInt32());
137 event.state_ = static_cast<DSchedEventState>(reply.ReadInt32());
138 return ERR_OK;
139 }
140
GetDSchedEventInfo(const DSchedEventType & type,std::vector<EventNotify> & events)141 int32_t DistributedClient::GetDSchedEventInfo(const DSchedEventType &type, std::vector<EventNotify> &events)
142 {
143 HILOG_INFO("%{public}s called", __func__);
144 sptr<IRemoteObject> remote = GetDmsProxy();
145 if (remote == nullptr) {
146 HILOG_ERROR("remote system ablity is null");
147 return ERR_FLATTEN_OBJECT;
148 }
149 MessageParcel data;
150 MessageParcel reply;
151 if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
152 HILOG_DEBUG("write interface token failed.");
153 return ERR_FLATTEN_OBJECT;
154 }
155 PARCEL_WRITE_HELPER(data, Uint32, type);
156
157 MessageOption option;
158 int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_DSCHED_EVENT_INFO),
159 data, reply, option);
160 if (ret != ERR_OK) {
161 HILOG_ERROR("sendRequest fail, ret: %{public}d", ret);
162 return ret;
163 }
164
165 ret = reply.ReadInt32();
166 if (ret != ERR_OK) {
167 HILOG_ERROR("Proxy get dms eventInfos from dms service fail, ret: %{public}d", ret);
168 return ret;
169 }
170
171 uint32_t eventNum = reply.ReadUint32();
172 if (eventNum > DSCHED_EVENT_MAX_NUM) {
173 HILOG_ERROR("Proxy get dms eventInfos num %{public}u is invalid.", eventNum);
174 return AAFwk::INVALID_PARAMETERS_ERR;
175 }
176 for (uint32_t i = 0; i < eventNum; i++) {
177 EventNotify event;
178 GetDecodeDSchedEventNotify(reply, event);
179 events.emplace_back(event);
180 }
181 return ret;
182 }
183 } // namespace DistributedSchedule
184 } // namespace OHOS