1 /* 2 * Copyright (c) 2021-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 #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)25static 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 pthread_mutex_unlock(&g_mutex); 35 return NULL; 36 } 37 38 int ret = iUnknown->QueryInterface(iUnknown, DEFAULT_VERSION, (void **)&g_intf); 39 if ((ret != EC_SUCCESS) || (g_intf == NULL)) { 40 pthread_mutex_unlock(&g_mutex); 41 return NULL; 42 } 43 pthread_mutex_unlock(&g_mutex); 44 return g_intf; 45 } 46 GetBatSoc(void)47int32_t GetBatSoc(void) 48 { 49 int32_t ret = EC_FAILURE; 50 BatteryInterface *intf = GetBatteryInterface(); 51 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 52 ret = intf->GetBatSocFunc((IUnknown *)intf); 53 } 54 return ret; 55 } 56 GetChargingStatus(void)57BatteryChargeState GetChargingStatus(void) 58 { 59 BatteryChargeState state = CHARGE_STATE_NONE; 60 BatteryInterface *intf = GetBatteryInterface(); 61 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 62 state = intf->GetChargingStatusFunc((IUnknown *)intf); 63 } 64 return state; 65 } 66 GetHealthStatus(void)67BatteryHealthState GetHealthStatus(void) 68 { 69 BatteryHealthState state = HEALTH_STATE_UNKNOWN; 70 BatteryInterface *intf = GetBatteryInterface(); 71 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 72 state = intf->GetHealthStatusFunc((IUnknown *)intf); 73 } 74 return state; 75 } 76 GetPluggedType(void)77BatteryPluggedType GetPluggedType(void) 78 { 79 BatteryPluggedType state = PLUGGED_TYPE_NONE; 80 BatteryInterface *intf = GetBatteryInterface(); 81 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 82 state = intf->GetPluggedTypeFunc((IUnknown *)intf); 83 } 84 return state; 85 } 86 GetBatVoltage(void)87int32_t GetBatVoltage(void) 88 { 89 int32_t ret = EC_FAILURE; 90 BatteryInterface *intf = GetBatteryInterface(); 91 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 92 ret = intf->GetBatVoltageFunc((IUnknown *)intf); 93 } 94 return ret; 95 } 96 GetBatTechnology(void)97char *GetBatTechnology(void) 98 { 99 char *strBuff = NULL; 100 BatteryInterface *intf = GetBatteryInterface(); 101 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 102 strBuff = intf->GetBatTechnologyFunc((IUnknown *)intf); 103 } 104 return strBuff; 105 } 106 GetBatTemperature(void)107int32_t GetBatTemperature(void) 108 { 109 int32_t ret = EC_FAILURE; 110 BatteryInterface *intf = GetBatteryInterface(); 111 if ((intf != NULL) && (intf->GetBatSocFunc != NULL)) { 112 ret = intf->GetBatTemperatureFunc((IUnknown *)intf); 113 } 114 return ret; 115 } 116