• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "battery_info_ffi.h"
17 #include "battery_log.h"
18 #include "battery_srv_client.h"
19 
20 using namespace OHOS::FFI;
21 
22 namespace OHOS {
23 namespace CJSystemapi {
24 namespace BatteryInfo {
25     using namespace OHOS::PowerMgr;
26 
27     thread_local static BatterySrvClient& g_battClient = BatterySrvClient::GetInstance();
28 
29 extern "C" {
FfiBatteryInfoBatterySOC()30 int32_t FfiBatteryInfoBatterySOC()
31 {
32     int32_t capacity = g_battClient.GetCapacity();
33     BATTERY_HILOGD(FEATURE_BATT_INFO, "capacity %{public}d", capacity);
34     return capacity;
35 }
36 
FfiBatteryInfoGetChargingState()37 int32_t FfiBatteryInfoGetChargingState()
38 {
39     int32_t chargingState = static_cast<int32_t>(g_battClient.GetChargingStatus());
40     BATTERY_HILOGD(FEATURE_BATT_INFO, "chargingState %{public}d", chargingState);
41     return chargingState;
42 }
43 
FfiBatteryInfoGetHealthState()44 int32_t FfiBatteryInfoGetHealthState()
45 {
46     int32_t healthStatus = static_cast<int32_t>(g_battClient.GetHealthStatus());
47     BATTERY_HILOGD(FEATURE_BATT_INFO, "healthStatus %{public}d", healthStatus);
48     return healthStatus;
49 }
50 
FfiBatteryInfoGetPluggedType()51 int32_t FfiBatteryInfoGetPluggedType()
52 {
53     int32_t pluggedType = static_cast<int32_t>(g_battClient.GetPluggedType());
54     BATTERY_HILOGD(FEATURE_BATT_INFO, "pluggedType %{public}d", pluggedType);
55     return pluggedType;
56 }
57 
FfiBatteryInfoGetVoltage()58 int32_t FfiBatteryInfoGetVoltage()
59 {
60     int32_t voltage = g_battClient.GetVoltage();
61     BATTERY_HILOGD(FEATURE_BATT_INFO, "voltage %{public}d", voltage);
62     return voltage;
63 }
64 
FfiBatteryInfoGetBatteryNowCurrent()65 int32_t FfiBatteryInfoGetBatteryNowCurrent()
66 {
67     int32_t curNow = g_battClient.GetNowCurrent();
68     BATTERY_HILOGD(FEATURE_BATT_INFO, "curNow %{public}d", curNow);
69     return curNow;
70 }
71 
FfiBatteryInfoGetTechnology()72 const char* FfiBatteryInfoGetTechnology()
73 {
74     auto technology = g_battClient.GetTechnology();
75     if (technology.empty()) {
76         return nullptr;
77     }
78     auto len = technology.length() + 1;
79     char *value = static_cast<char *>(malloc(sizeof(char) * len));
80     if (value == nullptr) {
81         return nullptr;
82     }
83     std::char_traits<char>::copy(value, technology.c_str(), len);
84     BATTERY_HILOGD(FEATURE_BATT_INFO, "technology %{public}s", value);
85     return value;
86 }
87 
FfiBatteryInfoGetBatteryTemperature()88 int32_t FfiBatteryInfoGetBatteryTemperature()
89 {
90     int32_t temperature = g_battClient.GetBatteryTemperature();
91     BATTERY_HILOGD(FEATURE_BATT_INFO, "temperature %{public}d", temperature);
92     return temperature;
93 }
94 
FfiBatteryInfoGetBatteryPresent()95 bool FfiBatteryInfoGetBatteryPresent()
96 {
97     bool present = g_battClient.GetPresent();
98     BATTERY_HILOGD(FEATURE_BATT_INFO, "present %{public}d", present);
99     return present;
100 }
101 
FfiBatteryInfoGetCapacityLevel()102 int32_t FfiBatteryInfoGetCapacityLevel()
103 {
104     int32_t batteryCapacityLevel = static_cast<int32_t>(g_battClient.GetCapacityLevel());
105     BATTERY_HILOGD(FEATURE_BATT_INFO, "batteryCapacityLevel %{public}d", batteryCapacityLevel);
106     return batteryCapacityLevel;
107 }
108 }
109 
110 } // namespace BatteryInfo
111 } // namespace CJSystemapi
112 } // namespace OHOS