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