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 #ifndef RESSCHED_EXECUTOR_INTERFACES_INNERKITS_RESSCHED_EXECUTOR_CLIENT_INCLUDE_RES_SCHED_EXE_CLIENT_H
17 #define RESSCHED_EXECUTOR_INTERFACES_INNERKITS_RESSCHED_EXECUTOR_CLIENT_INCLUDE_RES_SCHED_EXE_CLIENT_H
18 
19 #include <cstdint>
20 #include <cstring>
21 #include <mutex>
22 #include <unordered_map>
23 
24 #include "iremote_object.h"
25 #include "nocopyable.h"
26 #include "refbase.h"
27 
28 #include "ires_sched_exe_service.h"
29 
30 namespace OHOS {
31 namespace ResourceSchedule {
32 class ResSchedExeClient {
33 public:
34     /**
35      * @brief Get the Instance object.
36      *
37      * @return Returns ResSchedExeClient&.
38      */
39     static ResSchedExeClient& GetInstance();
40 
41     /**
42      * @brief Send request sync to the ressched_executor through inter-process communication.
43      *
44      * @param resType Indicates the resource type, all of the type have listed in res_exe_type.h.
45      * @param value Indicates the value of the resource type, defined by the developers.
46      * @param context Indicates the context info of the resource type event.
47      * @param reply Indicates the context info of the ipc reply.
48      * @return function result
49      */
50     int32_t SendRequestSync(uint32_t resType, int64_t value, const nlohmann::json& context, nlohmann::json& reply);
51 
52     /**
53      * @brief Send request async to the ressched_executor through inter-process communication.
54      *
55      * @param resType Indicates the resource type, all of the type have listed in res_exe_type.h.
56      * @param value Indicates the value of the resource type, defined by the developers.
57      * @param context Indicates the context info of the resource type event.
58      */
59     void SendRequestAsync(uint32_t resType, int64_t value, const nlohmann::json& context);
60 
61     /**
62      * @brief Send kill process request async to the ressched_executor.
63      *
64      * @param pid the pid whiche will be killed.
65      */
66     int32_t KillProcess(pid_t pid);
67 
68     /**
69      * @brief Stop remote Object, reset ResSchedExeClient.
70      */
71     void StopRemoteObject();
72 
73     /**
74      * @brief Send debug command for debug ipc.
75      *
76      * @param isSync is request sync
77      */
78     void SendDebugCommand(bool isSync);
79 
80 protected:
81     ResSchedExeClient() = default;
82     virtual ~ResSchedExeClient();
83 
84 private:
85     class ResSchedExeDeathRecipient : public IRemoteObject::DeathRecipient {
86     public:
87         explicit ResSchedExeDeathRecipient(ResSchedExeClient& resSchedExeClient);
88 
89         ~ResSchedExeDeathRecipient();
90 
91         void OnRemoteDied(const wptr<IRemoteObject>& object) override;
92 
93     private:
94         ResSchedExeClient& resSchedExeClient_;
95     };
96 
97     sptr<IResSchedExeService> GetProxy();
98     int32_t TryConnect();
99     int32_t SendRequestInner(bool isSync, uint32_t resType, int64_t value,
100         const nlohmann::json& context, nlohmann::json& reply);
101 
102     std::mutex mutex_;
103     sptr<IRemoteObject> remoteObject_;
104     sptr<IResSchedExeService> resSchedExe_;
105     sptr<ResSchedExeDeathRecipient> recipient_;
106     DISALLOW_COPY_AND_MOVE(ResSchedExeClient);
107 };
108 } // namespace ResourceSchedule
109 } // namespace OHOS
110 
111 #endif // RESSCHED_EXECUTOR_INTERFACES_INNERKITS_RESSCHED_EXECUTOR_CLIENT_INCLUDE_RES_SCHED_EXE_CLIENT_H
112