1/*
2 * Copyright (c) 2022-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 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 "v2_0/ibattery_callback.h"
26
27namespace OHOS {
28namespace PowerMgr {
29enum EventType {
30    EVENT_UEVENT_FD,
31    EVENT_TIMER_FD,
32};
33
34class BatteryThread {
35public:
36    virtual ~BatteryThread() = default;
37
38    void StartThread(void* service);
39protected:
40    int32_t LoopingThreadEntry(void* arg);
41    virtual void Run(void* service);
42    virtual void UpdateBatteryInfo(void* service);
43    virtual void HandleStates() {}
44    virtual int32_t UpdateWaitInterval();
45    void UpdateEpollInterval(int32_t chargeState);
46    virtual void CycleMatters() {}
47private:
48    int32_t OpenUeventSocket();
49    bool IsPowerSupplyEvent(const char* msg);
50    int32_t Init([[maybe_unused]]void* service);
51    int32_t InitUevent();
52    void UeventCallback(void* service);
53    int32_t RegisterCallback(int32_t fd, EventType et);
54    static constexpr int32_t INVALID_FD = -1;
55    int32_t ueventFd_ = INVALID_FD;
56    int32_t epFd_ = INVALID_FD;
57    int32_t epollInterval_ = -1;
58    using Callback = std::function<void(BatteryThread*, void*)>;
59    std::map<int32_t, Callback> callbacks_;
60    std::unique_ptr<PowerSupplyProvider> provider_ = nullptr;
61};
62}  // namespace PowerMgr
63}  // namespace OHOS
64
65#endif // BATTERY_THREAD_H
66