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