1 /*
2  * Copyright (c) 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 "res_sched_systemload_notifier_stub.h"
17 
18 #include "errors.h"
19 #include "ipc_object_stub.h"
20 #include "ipc_types.h"
21 #include "ipc_util.h"
22 #include "res_sched_errors.h"
23 #include "res_sched_ipc_interface_code.h"
24 #include "res_sched_log.h"
25 #include "res_type.h"
26 
27 namespace OHOS {
28 namespace ResourceSchedule {
29 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t ResSchedSystemloadNotifierStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
31                                                         MessageParcel& reply, MessageOption& option)
32 {
33     RESSCHED_LOGI("systemload notifier ipc code = %{public}u", code);
34     switch (code) {
35         case static_cast<uint32_t>(ResourceSceduleSystemloadNotifierCode::SYSTEMLOAD_LEVEL): {
36             return OnSystemloadLevelInner(data, reply);
37         }
38         default: {
39             RESSCHED_LOGE("unknown request code, please check");
40             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
41         }
42     }
43 }
44 
OnSystemloadLevelInner(MessageParcel & data,MessageParcel & reply)45 int32_t ResSchedSystemloadNotifierStub::OnSystemloadLevelInner(MessageParcel& data, MessageParcel& reply)
46 {
47     std::u16string descriptor = IResSchedSystemloadNotifier::GetDescriptor();
48     std::u16string remoteDescriptor = data.ReadInterfaceToken();
49     if (descriptor != remoteDescriptor) {
50         RESSCHED_LOGE("descriptor check failed");
51         return ERR_INVALID_VALUE;
52     }
53     int32_t level = static_cast<int32_t>(ResType::SystemloadLevel::LOW);
54     READ_PARCEL(data, Int32, level, ERR_RES_SCHED_PARCEL_ERROR, ResSchedSystemloadNotifierStub);
55     if ((level > static_cast<int32_t>(ResType::SystemloadLevel::ESCAPE))
56         || (level < static_cast<int32_t>(ResType::SystemloadLevel::LOW))) {
57         RESSCHED_LOGE("wrong level type.");
58         return ERR_INVALID_VALUE;
59     }
60     OnSystemloadLevel(level);
61     return ERR_OK;
62 }
63 } // namespace ResourceSchedule
64 } // namespace OHOS
65