1 /*
2  * Copyright (c) 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 #ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_INTERFACE_INNERKITS_INCLUDE_STANDBY_SERVICE_CLIENT_H
17 #define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_INTERFACE_INNERKITS_INCLUDE_STANDBY_SERVICE_CLIENT_H
18 
19 #include <iremote_proxy.h>
20 #include <nocopyable.h>
21 
22 #include "istandby_service.h"
23 #include "istandby_service_subscriber.h"
24 #include "standby_res_data.h"
25 
26 namespace OHOS {
27 namespace DevStandbyMgr {
28 
29 enum class PowerOverusedLevel : uint32_t {
30     NORMAL = 0,
31     MINOR,
32     WARNING,
33     SERIOUS,
34     EXTREME,
35     FATAL,
36 };
37 
38 class StandbyServiceClient {
39 public:
40     StandbyServiceClient();
41 
42     virtual ~StandbyServiceClient();
43 
44     static StandbyServiceClient& GetInstance();
45 
46     /**
47      * @brief Subscribes sleep state change event.
48      *
49      * @param subscriber Subscriber token.
50      * @return ERR_OK if success, else fail.
51      */
52     ErrCode SubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber);
53 
54     /**
55      * @brief Unsubscribes sleep state change event.
56      *
57      * @param subscriber Subscriber token.
58      * @return ERR_OK if success, else fail.
59      */
60     ErrCode UnsubscribeStandbyCallback(const sptr<IStandbyServiceSubscriber>& subscriber);
61 
62     /**
63      * @brief add allow list for some services or apps.
64      *
65      * @param resourceRequest resource to be added.
66      * @return ErrCode ERR_OK if success, others if fail.
67      */
68     ErrCode ApplyAllowResource(const sptr<ResourceRequest>& resourceRequest);
69 
70     /**
71      * @brief remove uid with allow type from allow list.
72      *
73      * @param resourceRequest resource to be removed.
74      * @return ErrCode ErrCode ERR_OK if success, others if fail.
75      */
76     ErrCode UnapplyAllowResource(const sptr<ResourceRequest>& resourceRequest);
77 
78     /**
79      * @brief Get the Allow List object.
80      *
81      * @param allowType the allow type to be retrieved.
82      * @param allowInfoList result represents allowed types and apps.
83      * @param reasonCode represents the reason why invoke the api.
84      * @return ErrCode ERR_OK if success, else fail.
85      */
86     ErrCode GetAllowList(uint32_t allowType, std::vector<AllowInfo>& allowInfoList,
87         uint32_t reasonCode);
88 
89     /**
90      * @brief Get the Restrict List object.
91      *
92      * @param restrictType the restrict type to be retrieved.
93      * @param restrictInfoList result represents restricted types and apps.
94      * @param reasonCode represents the reason why invoke the api.
95      * @return ErrCode ERR_OK if success, others if fail.
96      */
97     ErrCode GetRestrictList(uint32_t restrictType, std::vector<AllowInfo>& restrictInfoList,
98         uint32_t reasonCode);
99 
100     /**
101      * @brief Construct a new Report Work Scheduler Status object.
102      *
103      * @param started true if the work is triggered, else false.
104      * @param uid uid of the applicatoin.
105      * @param bundleName bundleName of the application.
106      * @return ErrCode ERR_OK if success, others if fail.
107      */
108     ErrCode ReportWorkSchedulerStatus(bool started, int32_t uid, const std::string& bundleName);
109 
110     /**
111      * @brief Whether the restriction strategy enbaled or not.
112      *
113      * @param strategyName the strategy name.
114      * @param enabled true if the strategy is enabled.
115      * @return ErrCode ERR_OK if success, others if fail.
116      */
117     ErrCode IsStrategyEnabled(const std::string& strategyName, bool& isEnabled);
118 
119     /**
120      * @brief Report event when device state change, such as discomponent device, bluetooth socket..
121      *
122      * @param type type of device state.
123      * @param enabled true if the device state is on.
124      * @return ErrCode ERR_OK if success, others if fail.
125      */
126     ErrCode ReportDeviceStateChanged(DeviceStateType type, bool enabled);
127 
128     /**
129      * @brief query if the device is in standby mode;
130      *
131      * @param isStandby true if device in standby, else false.
132      * @return ErrCode ERR_OK if success, else fail.
133      */
134     ErrCode IsDeviceInStandby(bool& isStandby);
135 
136     /**
137      * @brief set nat timeout interval;
138      *
139      * @param type detect type.
140      * @param enable adjust or not.
141      * @param interval nat timeout interval.
142      * @return ErrCode ERR_OK if success, else fail.
143      */
144     ErrCode SetNatInterval(uint32_t& type, bool& enable, uint32_t& interval);
145 
146     /**
147      * @brief Unified handling of events;
148      *
149      * @param resData event data.
150      * @return ErrCode ERR_OK if success, else fail.
151      */
152     ErrCode HandleEvent(const std::shared_ptr<ResData> &resData);
153 
154     /**
155      * @brief Report event when a module power energy is overused according to the information from XPower
156      *
157      * @param module module name
158      * @param level rate of power overused
159      * @return ErrCode ERR_OK if success, others if fail.
160      */
161     ErrCode ReportPowerOverused(const std::string &module, uint32_t level);
162 
163 private:
164     bool GetStandbyServiceProxy();
165     void ResetStandbyServiceClient();
166 
167     class StandbyServiceDeathRecipient : public IRemoteObject::DeathRecipient {
168     public:
169         explicit StandbyServiceDeathRecipient(StandbyServiceClient& standbyServiceClient);
170 
171         ~StandbyServiceDeathRecipient() override;
172 
173         void OnRemoteDied(const wptr<IRemoteObject>& object) override;
174 
175     private:
176         StandbyServiceClient& standbyServiceClient_;
177     };
178 
179 private:
180     std::mutex mutex_;
181     sptr<IStandbyService> standbyServiceProxy_;
182     sptr<StandbyServiceDeathRecipient> deathRecipient_;
183 };
184 }  // namespace DevStandbyMgr
185 }  // namespace OHOS
186 #endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_INTERFACE_INNERKITS_INCLUDE_STANDBY_SERVICE_CLIENT_H
187