1f16e0440Sopenharmony_ci/* 2f16e0440Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3f16e0440Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f16e0440Sopenharmony_ci * you may not use this file except in compliance with the License. 5f16e0440Sopenharmony_ci * You may obtain a copy of the License at 6f16e0440Sopenharmony_ci * 7f16e0440Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f16e0440Sopenharmony_ci * 9f16e0440Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f16e0440Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f16e0440Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f16e0440Sopenharmony_ci * See the License for the specific language governing permissions and 13f16e0440Sopenharmony_ci * limitations under the License. 14f16e0440Sopenharmony_ci */ 15f16e0440Sopenharmony_ci 16f16e0440Sopenharmony_ci#include <vector> 17f16e0440Sopenharmony_ci 18f16e0440Sopenharmony_ci#include <errors.h> 19f16e0440Sopenharmony_ci 20f16e0440Sopenharmony_ci#include "battery_config.h" 21f16e0440Sopenharmony_ci#include "battery_light.h" 22f16e0440Sopenharmony_ci#include "battery_log.h" 23f16e0440Sopenharmony_ci#include "power_common.h" 24f16e0440Sopenharmony_ci#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART 25f16e0440Sopenharmony_ci#include "light_agent.h" 26f16e0440Sopenharmony_ci#include "light_agent_type.h" 27f16e0440Sopenharmony_ci#endif 28f16e0440Sopenharmony_ciusing namespace std; 29f16e0440Sopenharmony_ci 30f16e0440Sopenharmony_cinamespace OHOS { 31f16e0440Sopenharmony_cinamespace PowerMgr { 32f16e0440Sopenharmony_ci#ifdef HAS_SENSORS_MISCDEVICE_PART 33f16e0440Sopenharmony_cinamespace { 34f16e0440Sopenharmony_ciconstexpr uint32_t MOVE_RIGHT_16 = 16; 35f16e0440Sopenharmony_ciconstexpr uint32_t MOVE_RIGHT_8 = 8; 36f16e0440Sopenharmony_ci} 37f16e0440Sopenharmony_ci#endif 38f16e0440Sopenharmony_civoid BatteryLight::InitLight() 39f16e0440Sopenharmony_ci{ 40f16e0440Sopenharmony_ci#ifdef HAS_SENSORS_MISCDEVICE_PART 41f16e0440Sopenharmony_ci LightInfo* lightInfo = nullptr; 42f16e0440Sopenharmony_ci int32_t count = 0; 43f16e0440Sopenharmony_ci int32_t ret = OHOS::Sensors::GetLightList(&lightInfo, count); 44f16e0440Sopenharmony_ci if (ret < ERR_OK || lightInfo == nullptr || count <= 0) { 45f16e0440Sopenharmony_ci BATTERY_HILOGW(FEATURE_BATT_LIGHT, "Light info is null"); 46f16e0440Sopenharmony_ci return; 47f16e0440Sopenharmony_ci } 48f16e0440Sopenharmony_ci 49f16e0440Sopenharmony_ci for (int32_t i = 0; i < count; ++i) { 50f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_LIGHT, 51f16e0440Sopenharmony_ci "LightInfo name: %{public}s, id: %{public}d, number: %{public}d, type: %{public}d", lightInfo[i].lightName, 52f16e0440Sopenharmony_ci lightInfo[i].lightId, lightInfo[i].lightNumber, lightInfo[i].lightType); 53f16e0440Sopenharmony_ci // lightName is associated by the light hdi driver 54f16e0440Sopenharmony_ci if (std::string(lightInfo[i].lightName) == "battery") { 55f16e0440Sopenharmony_ci available_ = true; 56f16e0440Sopenharmony_ci lightId_ = lightInfo[i].lightId; 57f16e0440Sopenharmony_ci BATTERY_HILOGI(FEATURE_BATT_LIGHT, "Battery light is available"); 58f16e0440Sopenharmony_ci break; 59f16e0440Sopenharmony_ci } 60f16e0440Sopenharmony_ci } 61f16e0440Sopenharmony_ci#endif 62f16e0440Sopenharmony_ci} 63f16e0440Sopenharmony_ci 64f16e0440Sopenharmony_civoid BatteryLight::TurnOff() 65f16e0440Sopenharmony_ci{ 66f16e0440Sopenharmony_ci#ifdef HAS_SENSORS_MISCDEVICE_PART 67f16e0440Sopenharmony_ci RETURN_IF(!available_); 68f16e0440Sopenharmony_ci int32_t ret = OHOS::Sensors::TurnOff(lightId_); 69f16e0440Sopenharmony_ci if (ret < ERR_OK) { 70f16e0440Sopenharmony_ci BATTERY_HILOGW(FEATURE_BATT_LIGHT, "Failed to turn off the battery light"); 71f16e0440Sopenharmony_ci } 72f16e0440Sopenharmony_ci lightColor_ = (ret < ERR_OK) ? lightColor_ : 0; 73f16e0440Sopenharmony_ci#endif 74f16e0440Sopenharmony_ci} 75f16e0440Sopenharmony_ci 76f16e0440Sopenharmony_civoid BatteryLight::TurnOn(uint32_t color) 77f16e0440Sopenharmony_ci{ 78f16e0440Sopenharmony_ci#ifdef HAS_SENSORS_MISCDEVICE_PART 79f16e0440Sopenharmony_ci RETURN_IF(!available_); 80f16e0440Sopenharmony_ci union LightColor lightColor; 81f16e0440Sopenharmony_ci lightColor.rgbColor.r = (color >> MOVE_RIGHT_16) & 0xFF; 82f16e0440Sopenharmony_ci lightColor.rgbColor.g = (color >> MOVE_RIGHT_8) & 0xFF; 83f16e0440Sopenharmony_ci lightColor.rgbColor.b = color & 0xFF; 84f16e0440Sopenharmony_ci LightAnimation animation = { 85f16e0440Sopenharmony_ci .mode = FlashMode::LIGHT_MODE_DEFAULT 86f16e0440Sopenharmony_ci }; 87f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_LIGHT, "battery light color is %{public}u", color); 88f16e0440Sopenharmony_ci int32_t ret = OHOS::Sensors::TurnOn(lightId_, lightColor, animation); 89f16e0440Sopenharmony_ci if (ret < ERR_OK) { 90f16e0440Sopenharmony_ci BATTERY_HILOGW(FEATURE_BATT_LIGHT, "Failed to turn on the battery light"); 91f16e0440Sopenharmony_ci } 92f16e0440Sopenharmony_ci lightColor_ = (ret < ERR_OK) ? lightColor_ : color; 93f16e0440Sopenharmony_ci#endif 94f16e0440Sopenharmony_ci} 95f16e0440Sopenharmony_ci 96f16e0440Sopenharmony_cibool BatteryLight::UpdateColor(BatteryChargeState chargeState, int32_t capacity) 97f16e0440Sopenharmony_ci{ 98f16e0440Sopenharmony_ci if ((chargeState == BatteryChargeState::CHARGE_STATE_NONE) || 99f16e0440Sopenharmony_ci (chargeState == BatteryChargeState::CHARGE_STATE_BUTT)) { 100f16e0440Sopenharmony_ci BATTERY_HILOGD(FEATURE_BATT_LIGHT, "not in charging state, turn off battery light"); 101f16e0440Sopenharmony_ci#ifdef HAS_SENSORS_MISCDEVICE_PART 102f16e0440Sopenharmony_ci TurnOff(); 103f16e0440Sopenharmony_ci#endif 104f16e0440Sopenharmony_ci return false; 105f16e0440Sopenharmony_ci } 106f16e0440Sopenharmony_ci 107f16e0440Sopenharmony_ci RETURN_IF_WITH_RET(!available_, false); 108f16e0440Sopenharmony_ci const auto& lightConf = BatteryConfig::GetInstance().GetLightConf(); 109f16e0440Sopenharmony_ci for (const auto& it : lightConf) { 110f16e0440Sopenharmony_ci if ((capacity >= it.beginSoc) && (capacity <= it.endSoc)) { 111f16e0440Sopenharmony_ci RETURN_IF_WITH_RET(lightColor_ == it.rgb, true); 112f16e0440Sopenharmony_ci#ifdef HAS_SENSORS_MISCDEVICE_PART 113f16e0440Sopenharmony_ci TurnOff(); 114f16e0440Sopenharmony_ci TurnOn(it.rgb); 115f16e0440Sopenharmony_ci#endif 116f16e0440Sopenharmony_ci return true; 117f16e0440Sopenharmony_ci } 118f16e0440Sopenharmony_ci } 119f16e0440Sopenharmony_ci return false; 120f16e0440Sopenharmony_ci} 121f16e0440Sopenharmony_ci 122f16e0440Sopenharmony_cibool BatteryLight::isAvailable() const 123f16e0440Sopenharmony_ci{ 124f16e0440Sopenharmony_ci return available_; 125f16e0440Sopenharmony_ci} 126f16e0440Sopenharmony_ci 127f16e0440Sopenharmony_ciuint32_t BatteryLight::GetLightColor() const 128f16e0440Sopenharmony_ci{ 129f16e0440Sopenharmony_ci return lightColor_; 130f16e0440Sopenharmony_ci} 131f16e0440Sopenharmony_ci} // namespace PowerMgr 132f16e0440Sopenharmony_ci} // namespace OHOS 133