• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 POWERMGR_BATTERY_SERVICE_H
17 #define POWERMGR_BATTERY_SERVICE_H
18 
19 #include "sp_singleton.h"
20 #include "system_ability.h"
21 #include "iremote_object.h"
22 #include "ibattery_srv.h"
23 #include "batteryd_api.h"
24 #include "battery_service_subscriber.h"
25 #include "battery_srv_stub.h"
26 #include "battery_service_event_handler.h"
27 #include "battery_callback_impl.h"
28 #include "types.h"
29 #include "battery_service_subscriber.h"
30 #include "battery_config.h"
31 #include "battery_led.h"
32 #include "window_manager.h"
33 
34 namespace OHOS {
35 namespace PowerMgr {
36 using namespace OHOS::HDI::Battery::V1_0;
37 
38 class BatteryService final : public SystemAbility,
39     public BatterySrvStub {
40 DECLARE_SYSTEM_ABILITY(BatteryService)
41 
42 DECLARE_DELAYED_SP_SINGLETON(BatteryService);
43 public:
44     virtual void OnDump() override;
45     virtual void OnStart() override;
46     virtual void OnStop() override;
47 
IsServiceReady()48     bool IsServiceReady() const
49     {
50         return ready_;
51     }
52 
GetHandler()53     std::shared_ptr<BatteryServiceEventHandler> GetHandler() const
54     {
55         return handler_;
56     }
57 
58     int32_t Dump(int fd, const std::vector<std::u16string> &args) override;
59     int32_t GetCapacity() override;
60     BatteryChargeState GetChargingStatus() override;
61     BatteryHealthState GetHealthStatus() override;
62     BatteryPluggedType GetPluggedType() override;
63     int32_t GetVoltage() override;
64     bool GetPresent() override;
65     std::string GetTechnology() override;
66     int32_t GetBatteryTemperature() override;
67     int32_t GetBatteryLevel() override;
68     int64_t GetRemainingChargeTime() override;
69     void ChangePath(const std::string path);
70     void InitConfig();
71     void WakeupDevice(const int32_t& chargestate);
72     void HandleTemperature(const int32_t& temperature);
73 private:
74     bool Init();
75     bool InitBatteryd();
76     int32_t HandleBatteryCallbackEvent(const OHOS::HDI::Battery::V1_0::BatteryInfo& event);
77     void CalculateRemainingChargeTime(int32_t capacity);
78     void HandlePopupEvent(const int32_t capacity);
79     void HandleCapacity(const int32_t& capacity, const int32_t& chargeState);
80     bool ShowDialog(const std::string &params);
81     void GetDisplayPosition(int32_t& offsetX, int32_t& offsetY, int32_t& width, int32_t& height, bool& wideScreen);
82     bool ready_ {false};
83     int32_t commEventRetryTimes_ {0};
84     std::mutex mutex_;
85     std::shared_ptr<AppExecFwk::EventRunner> eventRunner_;
86     std::shared_ptr<BatteryServiceEventHandler> handler_;
87     sptr<BatteryServiceSubscriber> batterydSubscriber_;
88     std::unique_ptr<HDI::Battery::V1_0::BatteryConfig> batteryConfig_ = nullptr;
89     std::unique_ptr<HDI::Battery::V1_0::BatteryLed> batteryLed_ = nullptr;
90     int32_t lastCapacity_ = 0;
91     int64_t lastTime_ = 0;
92     int64_t remainTime_ = 0;
93 };
94 } // namespace PowerMgr
95 } // namespace OHOS
96 
97 #endif // POWERMGR_BATTERY_SERVICE_H
98