1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "dms_client.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "ability_manager_errors.h"
19686862fbSopenharmony_ci#include "if_system_ability_manager.h"
20686862fbSopenharmony_ci#include "ipc_skeleton.h"
21686862fbSopenharmony_ci#include "iservice_registry.h"
22686862fbSopenharmony_ci#include "string_ex.h"
23686862fbSopenharmony_ci#include "system_ability_definition.h"
24686862fbSopenharmony_ci
25686862fbSopenharmony_ci#include "distributed_parcel_helper.h"
26686862fbSopenharmony_ci#include "distributedsched_ipc_interface_code.h"
27686862fbSopenharmony_ci
28686862fbSopenharmony_cinamespace OHOS {
29686862fbSopenharmony_cinamespace DistributedSchedule {
30686862fbSopenharmony_cinamespace {
31686862fbSopenharmony_ciconst std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
32686862fbSopenharmony_ciconstexpr uint32_t DSCHED_EVENT_MAX_NUM = 10000;
33686862fbSopenharmony_ci}
34686862fbSopenharmony_ci
35686862fbSopenharmony_cisptr<IRemoteObject> DistributedClient::GetDmsProxy()
36686862fbSopenharmony_ci{
37686862fbSopenharmony_ci    auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38686862fbSopenharmony_ci    if (samgrProxy == nullptr) {
39686862fbSopenharmony_ci        HILOG_ERROR("fail to get samgr.");
40686862fbSopenharmony_ci        return nullptr;
41686862fbSopenharmony_ci    }
42686862fbSopenharmony_ci    return samgrProxy->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID);
43686862fbSopenharmony_ci}
44686862fbSopenharmony_ci
45686862fbSopenharmony_ciint32_t DistributedClient::RegisterDSchedEventListener(const DSchedEventType& type,
46686862fbSopenharmony_ci    const sptr<IDSchedEventListener>& obj)
47686862fbSopenharmony_ci{
48686862fbSopenharmony_ci    HILOG_INFO("RegisterDSchedEventListener called");
49686862fbSopenharmony_ci    sptr<IRemoteObject> remote = GetDmsProxy();
50686862fbSopenharmony_ci    if (remote == nullptr) {
51686862fbSopenharmony_ci        HILOG_ERROR("remote system ablity is nullptr");
52686862fbSopenharmony_ci        return AAFwk::INVALID_PARAMETERS_ERR;
53686862fbSopenharmony_ci    }
54686862fbSopenharmony_ci    MessageParcel data;
55686862fbSopenharmony_ci    MessageParcel reply;
56686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
57686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
58686862fbSopenharmony_ci    }
59686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Uint8, type);
60686862fbSopenharmony_ci    if (obj == nullptr) {
61686862fbSopenharmony_ci        HILOG_ERROR("Received null IDSchedEventListener object");
62686862fbSopenharmony_ci        return AAFwk::INVALID_PARAMETERS_ERR;
63686862fbSopenharmony_ci    }
64686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
65686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER),
66686862fbSopenharmony_ci        data, reply);
67686862fbSopenharmony_ci}
68686862fbSopenharmony_ci
69686862fbSopenharmony_ciint32_t DistributedClient::UnRegisterDSchedEventListener(const DSchedEventType& type,
70686862fbSopenharmony_ci    const sptr<IDSchedEventListener>& obj)
71686862fbSopenharmony_ci{
72686862fbSopenharmony_ci    HILOG_INFO("UnRegisterDSchedEventListener called");
73686862fbSopenharmony_ci    sptr<IRemoteObject> remote = GetDmsProxy();
74686862fbSopenharmony_ci    if (remote == nullptr) {
75686862fbSopenharmony_ci        HILOG_ERROR("remote system ablity is null");
76686862fbSopenharmony_ci        return AAFwk::INVALID_PARAMETERS_ERR;
77686862fbSopenharmony_ci    }
78686862fbSopenharmony_ci    MessageParcel data;
79686862fbSopenharmony_ci    MessageParcel reply;
80686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
81686862fbSopenharmony_ci        HILOG_DEBUG("write interface token failed.");
82686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
83686862fbSopenharmony_ci    }
84686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Uint8, type);
85686862fbSopenharmony_ci    if (obj == nullptr) {
86686862fbSopenharmony_ci        HILOG_ERROR("Received null IDSchedEventListener object");
87686862fbSopenharmony_ci        return AAFwk::INVALID_PARAMETERS_ERR;
88686862fbSopenharmony_ci    }
89686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, RemoteObject, obj->AsObject());
90686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER),
91686862fbSopenharmony_ci        data, reply);
92686862fbSopenharmony_ci}
93686862fbSopenharmony_ci
94686862fbSopenharmony_ciint32_t DistributedClient::GetContinueInfo(ContinueInfo &continueInfo)
95686862fbSopenharmony_ci{
96686862fbSopenharmony_ci    HILOG_INFO("%{public}s called", __func__);
97686862fbSopenharmony_ci    sptr<IRemoteObject> remote = GetDmsProxy();
98686862fbSopenharmony_ci    if (remote == nullptr) {
99686862fbSopenharmony_ci        HILOG_ERROR("remote system ablity is null");
100686862fbSopenharmony_ci        return AAFwk::INVALID_PARAMETERS_ERR;
101686862fbSopenharmony_ci    }
102686862fbSopenharmony_ci    MessageParcel data;
103686862fbSopenharmony_ci    MessageParcel reply;
104686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
105686862fbSopenharmony_ci        HILOG_DEBUG("write interface token failed.");
106686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
107686862fbSopenharmony_ci    }
108686862fbSopenharmony_ci
109686862fbSopenharmony_ci    MessageOption option;
110686862fbSopenharmony_ci    int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_CONTINUE_INFO),
111686862fbSopenharmony_ci        data, reply, option);
112686862fbSopenharmony_ci    if (ret != ERR_NONE) {
113686862fbSopenharmony_ci        HILOG_ERROR("sendRequest fail, error: %{public}d", ret);
114686862fbSopenharmony_ci        return ret;
115686862fbSopenharmony_ci    }
116686862fbSopenharmony_ci    continueInfo.dstNetworkId_ = reply.ReadString();
117686862fbSopenharmony_ci    continueInfo.srcNetworkId_ = reply.ReadString();
118686862fbSopenharmony_ci    if (continueInfo.dstNetworkId_ == "") {
119686862fbSopenharmony_ci        HILOG_ERROR("read type failed!");
120686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
121686862fbSopenharmony_ci    }
122686862fbSopenharmony_ci    return ret;
123686862fbSopenharmony_ci}
124686862fbSopenharmony_ci
125686862fbSopenharmony_ciint32_t DistributedClient::GetDecodeDSchedEventNotify(MessageParcel &reply, EventNotify &event)
126686862fbSopenharmony_ci{
127686862fbSopenharmony_ci    event.eventResult_ = reply.ReadInt32();
128686862fbSopenharmony_ci    event.srcNetworkId_ = reply.ReadString();
129686862fbSopenharmony_ci    event.dstNetworkId_ = reply.ReadString();
130686862fbSopenharmony_ci    event.srcBundleName_ = reply.ReadString();
131686862fbSopenharmony_ci    event.srcModuleName_ = reply.ReadString();
132686862fbSopenharmony_ci    event.srcAbilityName_ = reply.ReadString();
133686862fbSopenharmony_ci    event.destBundleName_ = reply.ReadString();
134686862fbSopenharmony_ci    event.destModuleName_ = reply.ReadString();
135686862fbSopenharmony_ci    event.destAbilityName_ = reply.ReadString();
136686862fbSopenharmony_ci    event.dSchedEventType_ = static_cast<DSchedEventType>(reply.ReadInt32());
137686862fbSopenharmony_ci    event.state_ = static_cast<DSchedEventState>(reply.ReadInt32());
138686862fbSopenharmony_ci    return ERR_OK;
139686862fbSopenharmony_ci}
140686862fbSopenharmony_ci
141686862fbSopenharmony_ciint32_t DistributedClient::GetDSchedEventInfo(const DSchedEventType &type, std::vector<EventNotify> &events)
142686862fbSopenharmony_ci{
143686862fbSopenharmony_ci    HILOG_INFO("%{public}s called", __func__);
144686862fbSopenharmony_ci    sptr<IRemoteObject> remote = GetDmsProxy();
145686862fbSopenharmony_ci    if (remote == nullptr) {
146686862fbSopenharmony_ci        HILOG_ERROR("remote system ablity is null");
147686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
148686862fbSopenharmony_ci    }
149686862fbSopenharmony_ci    MessageParcel data;
150686862fbSopenharmony_ci    MessageParcel reply;
151686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
152686862fbSopenharmony_ci        HILOG_DEBUG("write interface token failed.");
153686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
154686862fbSopenharmony_ci    }
155686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Uint32, type);
156686862fbSopenharmony_ci
157686862fbSopenharmony_ci    MessageOption option;
158686862fbSopenharmony_ci    int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_DSCHED_EVENT_INFO),
159686862fbSopenharmony_ci        data, reply, option);
160686862fbSopenharmony_ci    if (ret != ERR_OK) {
161686862fbSopenharmony_ci        HILOG_ERROR("sendRequest fail, ret: %{public}d", ret);
162686862fbSopenharmony_ci        return ret;
163686862fbSopenharmony_ci    }
164686862fbSopenharmony_ci
165686862fbSopenharmony_ci    ret = reply.ReadInt32();
166686862fbSopenharmony_ci    if (ret != ERR_OK) {
167686862fbSopenharmony_ci        HILOG_ERROR("Proxy get dms eventInfos from dms service fail, ret: %{public}d", ret);
168686862fbSopenharmony_ci        return ret;
169686862fbSopenharmony_ci    }
170686862fbSopenharmony_ci
171686862fbSopenharmony_ci    uint32_t eventNum = reply.ReadUint32();
172686862fbSopenharmony_ci    if (eventNum > DSCHED_EVENT_MAX_NUM) {
173686862fbSopenharmony_ci        HILOG_ERROR("Proxy get dms eventInfos num %{public}u is invalid.", eventNum);
174686862fbSopenharmony_ci        return AAFwk::INVALID_PARAMETERS_ERR;
175686862fbSopenharmony_ci    }
176686862fbSopenharmony_ci    for (uint32_t i = 0; i < eventNum; i++) {
177686862fbSopenharmony_ci        EventNotify event;
178686862fbSopenharmony_ci        GetDecodeDSchedEventNotify(reply, event);
179686862fbSopenharmony_ci        events.emplace_back(event);
180686862fbSopenharmony_ci    }
181686862fbSopenharmony_ci    return ret;
182686862fbSopenharmony_ci}
183686862fbSopenharmony_ci}  // namespace DistributedSchedule
184686862fbSopenharmony_ci}  // namespace OHOS