• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 BATTERY_THREAD_H
17 #define BATTERY_THREAD_H
18 
19 #include <map>
20 #include <memory>
21 #include <thread>
22 #include <vector>
23 #include "battery_callback_impl.h"
24 #include "power_supply_provider.h"
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace Battery {
29 namespace V1_0 {
30 enum EventType {
31     EVENT_UEVENT_FD,
32     EVENT_TIMER_FD,
33 };
34 
35 class BatteryThread {
36 public:
~BatteryThread()37     virtual ~BatteryThread() {}
38 
39     void StartThread(void* service);
40     void InitCallback(const sptr<OHOS::HDI::Battery::V1_0::IBatteryCallback>& event);
41 protected:
42     int LoopingThreadEntry(void* arg);
43     virtual void Run(void* service);
44     virtual void UpdateBatteryInfo(void* service, char* msg);
45     virtual void UpdateBatteryInfo(void* service);
HandleStates()46     virtual void HandleStates() {}
47     virtual int UpdateWaitInterval();
48     void UpdateEpollInterval(const int32_t chargeState);
CycleMatters()49     virtual void CycleMatters() {}
50 private:
51     int32_t OpenUeventSocket(void) const;
52     bool IsPowerSupplyEvent(const char* msg) const;
53     int32_t Init(void* service);
54     int32_t InitUevent();
55     int32_t InitTimer();
56     int32_t InitBacklightTimer();
57     void TimerCallback(void* service);
58     void UeventCallback(void* service);
59     void SetTimerInterval(int interval);
60     int RegisterCallback(const int fd, const EventType et);
61     int timerInterval_ = -1;
62     int32_t ueventFd_ = -1;
63     int32_t timerFd_ = -1;
64     int32_t epFd_ = -1;
65     int epollInterval_ = -1;
66     using Callback = std::function<void(BatteryThread*, void*)>;
67     std::map<int32_t, Callback> callbacks_;
68     std::unique_ptr<PowerSupplyProvider> giver_ = nullptr;
69 };
70 }  // namespace V1_0
71 }  // namespace Battery
72 }  // namespace HDI
73 }  // namespace OHOS
74 
75 #endif // BATTERY_THREAD_H
76