1/*
2 * Copyright (c) 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 <vector>
17
18#include <errors.h>
19
20#include "battery_config.h"
21#include "battery_light.h"
22#include "battery_log.h"
23#include "power_common.h"
24#ifdef HAS_HIVIEWDFX_HISYSEVENT_PART
25#include "light_agent.h"
26#include "light_agent_type.h"
27#endif
28using namespace std;
29
30namespace OHOS {
31namespace PowerMgr {
32#ifdef HAS_SENSORS_MISCDEVICE_PART
33namespace {
34constexpr uint32_t MOVE_RIGHT_16 = 16;
35constexpr uint32_t MOVE_RIGHT_8 = 8;
36}
37#endif
38void BatteryLight::InitLight()
39{
40#ifdef HAS_SENSORS_MISCDEVICE_PART
41    LightInfo* lightInfo = nullptr;
42    int32_t count = 0;
43    int32_t ret = OHOS::Sensors::GetLightList(&lightInfo, count);
44    if (ret < ERR_OK || lightInfo == nullptr || count <= 0) {
45        BATTERY_HILOGW(FEATURE_BATT_LIGHT, "Light info is null");
46        return;
47    }
48
49    for (int32_t i = 0; i < count; ++i) {
50        BATTERY_HILOGD(FEATURE_BATT_LIGHT,
51            "LightInfo name: %{public}s, id: %{public}d, number: %{public}d, type: %{public}d", lightInfo[i].lightName,
52            lightInfo[i].lightId, lightInfo[i].lightNumber, lightInfo[i].lightType);
53        // lightName is associated by the light hdi driver
54        if (std::string(lightInfo[i].lightName) == "battery") {
55            available_ = true;
56            lightId_ = lightInfo[i].lightId;
57            BATTERY_HILOGI(FEATURE_BATT_LIGHT, "Battery light is available");
58            break;
59        }
60    }
61#endif
62}
63
64void BatteryLight::TurnOff()
65{
66#ifdef HAS_SENSORS_MISCDEVICE_PART
67    RETURN_IF(!available_);
68    int32_t ret = OHOS::Sensors::TurnOff(lightId_);
69    if (ret < ERR_OK) {
70        BATTERY_HILOGW(FEATURE_BATT_LIGHT, "Failed to turn off the battery light");
71    }
72    lightColor_ = (ret < ERR_OK) ? lightColor_ : 0;
73#endif
74}
75
76void BatteryLight::TurnOn(uint32_t color)
77{
78#ifdef HAS_SENSORS_MISCDEVICE_PART
79    RETURN_IF(!available_);
80    union LightColor lightColor;
81    lightColor.rgbColor.r = (color >> MOVE_RIGHT_16) & 0xFF;
82    lightColor.rgbColor.g = (color >> MOVE_RIGHT_8) & 0xFF;
83    lightColor.rgbColor.b = color & 0xFF;
84    LightAnimation animation = {
85        .mode = FlashMode::LIGHT_MODE_DEFAULT
86    };
87    BATTERY_HILOGD(FEATURE_BATT_LIGHT, "battery light color is %{public}u", color);
88    int32_t ret = OHOS::Sensors::TurnOn(lightId_, lightColor, animation);
89    if (ret < ERR_OK) {
90        BATTERY_HILOGW(FEATURE_BATT_LIGHT, "Failed to turn on the battery light");
91    }
92    lightColor_ = (ret < ERR_OK) ? lightColor_ : color;
93#endif
94}
95
96bool BatteryLight::UpdateColor(BatteryChargeState chargeState, int32_t capacity)
97{
98    if ((chargeState == BatteryChargeState::CHARGE_STATE_NONE) ||
99        (chargeState == BatteryChargeState::CHARGE_STATE_BUTT)) {
100        BATTERY_HILOGD(FEATURE_BATT_LIGHT, "not in charging state, turn off battery light");
101#ifdef HAS_SENSORS_MISCDEVICE_PART
102        TurnOff();
103#endif
104        return false;
105    }
106
107    RETURN_IF_WITH_RET(!available_, false);
108    const auto& lightConf = BatteryConfig::GetInstance().GetLightConf();
109    for (const auto& it : lightConf) {
110        if ((capacity >= it.beginSoc) && (capacity <= it.endSoc)) {
111            RETURN_IF_WITH_RET(lightColor_ == it.rgb, true);
112#ifdef HAS_SENSORS_MISCDEVICE_PART
113            TurnOff();
114            TurnOn(it.rgb);
115#endif
116            return true;
117        }
118    }
119    return false;
120}
121
122bool BatteryLight::isAvailable() const
123{
124    return available_;
125}
126
127uint32_t BatteryLight::GetLightColor() const
128{
129    return lightColor_;
130}
131} // namespace PowerMgr
132} // namespace OHOS
133