• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_framework.h"
17 #include "battery_info.h"
18 #include "battery_interface.h"
19 #include "ibattery.h"
20 
21 
22 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
23 static BatteryInterface *g_intf = NULL;
24 
GetBatteryInterface(void)25 static BatteryInterface *GetBatteryInterface(void)
26 {
27     pthread_mutex_lock(&g_mutex);
28     if (g_intf != NULL) {
29         pthread_mutex_unlock(&g_mutex);
30         return g_intf;
31     }
32     IUnknown *iUnknown = GetBatteryIUnknown();
33     if (iUnknown == NULL) {
34         return NULL;
35     }
36 
37     int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf);
38     if ((ret != EC_SUCCESS) || (g_intf == NULL)) {
39         pthread_mutex_unlock(&g_mutex);
40         return NULL;
41     }
42     pthread_mutex_unlock(&g_mutex);
43     return g_intf;
44 }
45 
GetBatSoc(void)46 int32_t GetBatSoc(void)
47 {
48     int32_t ret = EC_FAILURE;
49     BatteryInterface *intf = GetBatteryInterface();
50     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
51         ret = intf->GetBatSocFunc((IUnknown *)intf);
52     }
53     return ret;
54 }
GetChargingStatus(void)55 BatteryChargeState GetChargingStatus(void)
56 {
57     BatteryChargeState state = CHARGE_STATE_NONE;
58     BatteryInterface *intf = GetBatteryInterface();
59     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
60         state = intf->GetChargingStatusFunc((IUnknown *)intf);
61     }
62     return state;
63 }
GetHealthStatus(void)64 BatteryHealthState GetHealthStatus(void)
65 {
66     BatteryHealthState state = HEALTH_STATE_UNKNOWN;
67     BatteryInterface *intf = GetBatteryInterface();
68     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
69         state = intf->GetHealthStatusFunc((IUnknown *)intf);
70     }
71     return state;
72 }
GetPluggedType(void)73 BatteryPluggedType GetPluggedType(void)
74 {
75     BatteryPluggedType state = PLUGGED_TYPE_NONE;
76     BatteryInterface *intf = GetBatteryInterface();
77     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
78         state = intf->GetPluggedTypeFunc((IUnknown *)intf);
79     }
80     return state;
81 }
GetBatVoltage(void)82 int32_t GetBatVoltage(void)
83 {
84     int32_t ret = EC_FAILURE;
85     BatteryInterface *intf = GetBatteryInterface();
86     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
87         ret = intf->GetBatVoltageFunc((IUnknown *)intf);
88     }
89     return ret;
90 }
91 
GetBatTechnology(void)92 char *GetBatTechnology(void)
93 {
94     char *strBuff = NULL;
95     BatteryInterface *intf = GetBatteryInterface();
96     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
97         strBuff = intf->GetBatTechnologyFunc((IUnknown *)intf);
98     }
99     return strBuff;
100 }
GetBatTemperature(void)101 int32_t GetBatTemperature(void)
102 {
103     int32_t ret = EC_FAILURE;
104     BatteryInterface *intf = GetBatteryInterface();
105     if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) {
106         ret = intf->GetBatTemperatureFunc((IUnknown *)intf);
107     }
108     return ret;
109 }
110