1 /*
2 * Copyright (c) 2022-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
16 #include "res_sched_service_proxy.h"
17 #include "ipc_types.h" // for NO_ERROR
18 #include "ipc_util.h" // for WRITE_PARCEL
19 #include "iremote_object.h" // for IRemoteObject
20 #include "message_option.h" // for MessageOption, MessageOption::TF_ASYNC
21 #include "message_parcel.h" // for MessageParcel
22 #include "res_sched_errors.h" // for GET_RES_SCHED_SERVICE_FAILED
23 #include "res_sched_log.h" // for RESSCHED_LOGD, RESSCHED_LOGE
24 #include "res_sched_ipc_interface_code.h" // for ResourceScheduleInterfaceCode
25
26 namespace OHOS {
27 namespace ResourceSchedule {
ReportData(uint32_t resType,int64_t value,const nlohmann::json & payload)28 void ResSchedServiceProxy::ReportData(uint32_t resType, int64_t value, const nlohmann::json& payload)
29 {
30 int32_t error;
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option = { MessageOption::TF_ASYNC };
34 WRITE_PARCEL(data, InterfaceToken, ResSchedServiceProxy::GetDescriptor(), , ResSchedServiceProxy);
35 WRITE_PARCEL(data, Uint32, resType, , ResSchedServiceProxy);
36 WRITE_PARCEL(data, Int64, value, , ResSchedServiceProxy);
37 WRITE_PARCEL(data, String, payload.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace), ,
38 ResSchedServiceProxy);
39 error = Remote()->SendRequest(static_cast<uint32_t>(ResourceScheduleInterfaceCode::REPORT_DATA),
40 data, reply, option);
41 if (error != NO_ERROR) {
42 RESSCHED_LOGE("Send request error: %{public}d.", error);
43 return;
44 }
45 RESSCHED_LOGD("%{public}s, success.", __func__);
46 }
47
KillProcess(const nlohmann::json & payload)48 int32_t ResSchedServiceProxy::KillProcess(const nlohmann::json& payload)
49 {
50 int32_t error;
51 MessageParcel data;
52 MessageParcel reply;
53 MessageOption option = { MessageOption::TF_SYNC };
54 WRITE_PARCEL(data, InterfaceToken, ResSchedServiceProxy::GetDescriptor(), RES_SCHED_DATA_ERROR,
55 ResSchedServiceProxy);
56 WRITE_PARCEL(data, String, payload.dump(-1, ' ', false, nlohmann::detail::error_handler_t::replace),
57 RES_SCHED_DATA_ERROR, ResSchedServiceProxy);
58 error = Remote()->SendRequest(static_cast<uint32_t>(ResourceScheduleInterfaceCode::KILL_PROCESS),
59 data, reply, option);
60 if (error != NO_ERROR) {
61 RESSCHED_LOGE("Send request error: %{public}d.", error);
62 return RES_SCHED_REQUEST_FAIL;
63 }
64 RESSCHED_LOGD("%{public}s, success.", __func__);
65 return reply.ReadInt32();
66 }
67 } // namespace ResourceSchedule
68 } // namespace OHOS
69