• 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 <refbase.h>
22 #include <thread>
23 #include <vector>
24 #include "power_supply_provider.h"
25 #include "v1_1/ibattery_callback.h"
26 
27 namespace OHOS {
28 namespace HDI {
29 namespace Battery {
30 namespace V1_1 {
31 enum EventType {
32     EVENT_UEVENT_FD,
33     EVENT_TIMER_FD,
34 };
35 
36 class BatteryThread {
37 public:
38     virtual ~BatteryThread() = default;
39 
40     void StartThread(void* service);
41     void InitCallback(const sptr<OHOS::HDI::Battery::V1_1::IBatteryCallback>& callback);
42 protected:
43     int32_t LoopingThreadEntry(void* arg);
44     virtual void Run(void* service);
45     virtual void UpdateBatteryInfo(void* service);
HandleStates()46     virtual void HandleStates() {}
47     virtual int32_t UpdateWaitInterval();
48     void UpdateEpollInterval(int32_t chargeState);
CycleMatters()49     virtual void CycleMatters() {}
50 private:
51     int32_t OpenUeventSocket();
52     bool IsPowerSupplyEvent(const char* msg);
53     int32_t Init([[maybe_unused]]void* service);
54     int32_t InitUevent();
55     void UeventCallback(void* service);
56     void SetTimerInterval(int32_t interval);
57     int32_t RegisterCallback(int32_t fd, EventType et);
58     static constexpr int32_t INVALID_FD = -1;
59     int32_t ueventFd_ = INVALID_FD;
60     int32_t epFd_ = INVALID_FD;
61     int32_t epollInterval_ = -1;
62     using Callback = std::function<void(BatteryThread*, void*)>;
63     std::map<int32_t, Callback> callbacks_;
64     std::unique_ptr<PowerSupplyProvider> provider_ = nullptr;
65 };
66 }  // namespace V1_1
67 }  // namespace Battery
68 }  // namespace HDI
69 }  // namespace OHOS
70 
71 #endif // BATTERY_THREAD_H
72