• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 POWER_SUPPLY_PROVIDER_H
17 #define POWER_SUPPLY_PROVIDER_H
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <climits>
22 #include <map>
23 #include <vector>
24 #include "batteryd_api.h"
25 #include "v1_2/ibattery_interface.h"
26 
27 namespace OHOS {
28 namespace PowerMgr {
29 class PowerSupplyProvider {
30 public:
31     // Keep it same as the BatteryChargeState in battery_info.h
32     enum BatteryChargeState {
33         CHARGE_STATE_NONE = 0,
34         CHARGE_STATE_ENABLE,
35         CHARGE_STATE_DISABLE,
36         CHARGE_STATE_FULL,
37         CHARGE_STATE_RESERVED,
38     };
39 
40     PowerSupplyProvider();
41     virtual ~PowerSupplyProvider() = default;
42 
43     int32_t InitPowerSupplySysfs();
44     void InitDefaultSysfs();
45     int32_t ParseCapacity(int32_t* capacity) const;
46     int32_t ParseTemperature(int32_t* temperature) const;
47     int32_t ParseChargeState(int32_t* chargeState) const;
48     void InitBatteryPath();
49 
50 private:
51     struct BatterySysfsInfo {
52         char* name;
53         std::string capacityPath;
54         std::string temperaturePath;
55         std::string chargeStatePath;
56     } batterySysfsInfo_;
57 
58     static inline int32_t ParseInt(const char* str);
59     static inline void Trim(char* str);
60     static int32_t ChargeStateEnumConverter(const char* str);
61 
62     void TraversalNode();
63     void CheckSubfolderNode(const std::string& path);
64     void FormatPath(std::string& path, size_t size, const char* format, const char* basePath, const char* name) const;
65     void FormatSysfsPaths();
66     int32_t ReadSysfsFile(const char* path, char* buf, size_t size) const;
67     int32_t ReadBatterySysfsToBuff(const char* path, char* buf, size_t size) const;
68     void CreateFile(const std::string& path, const std::string& content);
69     void CreateMockTechPath(std::string& mockTechPath);
70     void CreateMockChargerPath(std::string& mockChargerPath);
71     void CreateMockBatteryPath(std::string& mockBatteryPath);
72     std::vector<std::string> nodeNames_;
73     std::map<std::string, std::string> nodeNamePathMap_;
74     std::string path_;
75     int32_t index_;
76 };
77 }  // namespace PowerMgr
78 }  // namespace OHOS
79 
80 #endif // POWER_SUPPLY_PROVIDER_H
81