modify code style

Signed-off-by: baoguotao <baoguotao@huawei.com>
This commit is contained in:
baoguotao
2021-10-20 15:13:26 +08:00
parent 5fccd12f79
commit ffa6f3b907
187 changed files with 12978 additions and 1420 deletions
Regular → Executable
View File
Regular → Executable
View File
Regular → Executable
View File
+155
View File
@@ -0,0 +1,155 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//base/powermgr/battery_manager/batterymgr.gni")
import("//build/ohos.gni")
import("//drivers/adapter/uhdf2/uhdf.gni")
config("batteryd_private_config") {
include_dirs = [
"//drivers/ability/sbuf/include",
"//drivers/framework/include",
"//drivers/framework/include/core",
]
}
config("batteryd_public_config") {
include_dirs = [
"include",
"${battery_manager_path}/hdi/api/include",
"//third_party/jsoncpp/include",
"//base/startup/syspara_lite/adapter/native/syspara/include",
"//base/powermgr/battery_manager/charger/ui/include",
"//base/powermgr/battery_manager/charger/vibrate",
"//base/powermgr/battery_manager/charger/backlight",
"//base/powermgr/battery_manager/charger/led",
"//drivers/peripheral/input/interfaces/include",
"//third_party/libpng",
]
}
ohos_executable("charger") {
install_enable = true
sources = [
"backlight/battery_backlight.cpp",
"led/battery_led.cpp",
"src/charger.cpp",
"src/charger_thread.cpp",
"ui/src/updater_ui.cpp",
"vibrate/battery_vibrate.cpp",
]
configs = [
"${utils_path}:utils_config",
":batteryd_private_config",
]
public_configs = [ ":batteryd_public_config" ]
deps = [
"${hdf_uhdf_path}/host:libhdf_host",
"${hdf_uhdf_path}/ipc:libhdf_ipc_adapter",
"${hdf_uhdf_path}/utils:libhdf_utils",
"//base/powermgr/battery_manager/charger/ui:libminui",
"//base/powermgr/battery_manager/hdi/service:batteryd",
"//base/startup/syspara_lite/interfaces/innerkits/native/syspara:syspara",
"//third_party/jsoncpp:jsoncpp",
"//utils/native/base:utils",
]
external_deps = [
"hiviewdfx_hilog_native:libhilog",
"ipc:ipc_core",
]
part_name = "battery_manager_native"
}
group("charger_group") {
deps = [ ":charger" ]
}
group("led_service") {
deps = [ "sa_profile:led_config" ]
}
group("resources_service") {
deps = [
"resources:resources_config",
"resources:resources_config1",
"resources:resources_config10",
"resources:resources_config11",
"resources:resources_config12",
"resources:resources_config13",
"resources:resources_config14",
"resources:resources_config15",
"resources:resources_config16",
"resources:resources_config17",
"resources:resources_config18",
"resources:resources_config19",
"resources:resources_config2",
"resources:resources_config20",
"resources:resources_config21",
"resources:resources_config22",
"resources:resources_config23",
"resources:resources_config24",
"resources:resources_config25",
"resources:resources_config26",
"resources:resources_config27",
"resources:resources_config28",
"resources:resources_config29",
"resources:resources_config3",
"resources:resources_config30",
"resources:resources_config31",
"resources:resources_config32",
"resources:resources_config33",
"resources:resources_config34",
"resources:resources_config35",
"resources:resources_config36",
"resources:resources_config37",
"resources:resources_config38",
"resources:resources_config39",
"resources:resources_config4",
"resources:resources_config40",
"resources:resources_config41",
"resources:resources_config42",
"resources:resources_config43",
"resources:resources_config44",
"resources:resources_config45",
"resources:resources_config46",
"resources:resources_config47",
"resources:resources_config48",
"resources:resources_config49",
"resources:resources_config5",
"resources:resources_config50",
"resources:resources_config51",
"resources:resources_config52",
"resources:resources_config53",
"resources:resources_config54",
"resources:resources_config55",
"resources:resources_config56",
"resources:resources_config57",
"resources:resources_config58",
"resources:resources_config59",
"resources:resources_config6",
"resources:resources_config60",
"resources:resources_config61",
"resources:resources_config62",
"resources:resources_config63",
"resources:resources_config64",
"resources:resources_config65",
"resources:resources_config7",
"resources:resources_config8",
"resources:resources_config9",
]
}
+91
View File
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "battery_backlight.h"
#include <hdf_log.h>
#include <hdf_base.h>
#include <string>
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
static const std::string BACKLIGHT_DEVICE_PATH = "/sys/class/backlight/sprd_backlight/brightness";
static const std::string SEMICOLON = ";";
static const int MAX_STR = 255;
static const unsigned int BACKLIGHT_ON = 128;
static const unsigned int BACKLIGHT_OFF = 0;
void BatteryBacklight::TurnOnScreen()
{
HDF_LOGI("%{public}s enter", __func__);
HandleBacklight(BACKLIGHT_ON);
screenOn_ = true;
}
void BatteryBacklight::TurnOffScreen()
{
HDF_LOGI("%{public}s enter", __func__);
HandleBacklight(BACKLIGHT_OFF);
screenOn_ = false;
}
bool BatteryBacklight::GetScreenState()
{
HDF_LOGI("%{public}s enter", __func__);
return screenOn_;
}
int BatteryBacklight::HandleBacklight(const unsigned int backlight)
{
HDF_LOGI("%{public}s enter", __func__);
FILE *fp = nullptr;
int writeFile = -1;
char *path = nullptr;
char *pathGroup = nullptr;
unsigned int bufferLen;
HDF_LOGI("%{public}s: backlight value is %{public}d", __func__, backlight);
bufferLen = strnlen(BACKLIGHT_DEVICE_PATH.c_str(), MAX_STR) + 1;
pathGroup = (char*)malloc(bufferLen);
if (pathGroup == nullptr) {
HDF_LOGD("%{public}s: malloc error.", __func__);
return writeFile;
}
strlcpy(pathGroup, BACKLIGHT_DEVICE_PATH.c_str(), bufferLen);
path = pathGroup;
while ((path = strtok(path, SEMICOLON.c_str())) != nullptr) {
fp = fopen(path, "w");
if (fp != nullptr) {
writeFile = fprintf(fp, "%u\n", backlight);
fclose(fp);
}
if (writeFile <= 0) {
HDF_LOGD("%{public}s: failed to set backlight path=%{pusblic}s.", __func__, path);
}
path = nullptr;
}
free(pathGroup);
HDF_LOGI("%{public}s exit", __func__);
return writeFile;
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+36
View File
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BATTERY_BACKLIGHT_H
#define BATTERY_BACKLIGHT_H
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class BatteryBacklight {
public:
void TurnOnScreen();
void TurnOffScreen();
int HandleBacklight(const unsigned int backlight);
bool GetScreenState();
private:
bool screenOn_ = false;
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif
+93
View File
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_THREAD_H
#define CHARGER_THREAD_H
#include <thread>
#include <linux/input.h>
#include "input_type.h"
#include "battery_thread.h"
#include "battery_config.h"
#include "battery_vibrate.h"
#include "battery_backlight.h"
#include "battery_led.h"
#include "animation_label.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class ChargerThread : public BatteryThread {
public:
friend class BatteryThreadTest;
private:
void Init();
static void Reboot(std::string &reason);
static void UpdateAnimation(const int32_t &capacity);
void ShutDown(std::string &reason);
void Run(void *service) override;
void UpdateBatteryInfo(void *arg, char *msg) override;
void UpdateBatteryInfo(void *arg) override;
void HandleChargingState();
void HandleScreenState();
void HandleTemperature(const int32_t &temperature);
void HandleCapacity(const int32_t &capacity);
void HandleStates() override;
int UpdateWaitInterval() override;
void CycleMatters() override;
void AnimationInit();
void LoadImgs(AnimationLabel *g_animationLabel);
static void SetKeyWait(struct keyState *key, int64_t timeout);
static int SetKeyState(int code, int value, int64_t now);
int InputInit();
static void EventPkgCallback(const EventPackage **pkgs, const uint32_t count, uint32_t devIndex);
void HandlePowerKeyState();
void HandlePowerKey(int keycode, int64_t now);
static void HandleInputEvent(const struct input_event *iev);
std::unique_ptr<BatteryConfig> batteryConfig_ = nullptr;
std::unique_ptr<PowerSupplyProvider> provider_ = nullptr;
std::unique_ptr<BatteryVibrate> vibrate_ = nullptr;
std::unique_ptr<BatteryBacklight> backlight_ = nullptr;
std::unique_ptr<BatteryLed> led_ = nullptr;
int64_t pluginWait_ = -1;
static int64_t keyWait_;
static int64_t backlightWait_;
static int32_t capacity_;
int32_t chargeState_ = -1;
bool started_ = false;
};
constexpr int START_X1 = 0;
constexpr int START_Y1 = 850;
constexpr int WIDTH1 = 480;
constexpr int HEIGHT1 = 30;
constexpr int START_X2 = 0;
constexpr int START_Y2 = 900;
constexpr int WIDTH2 = 480;
constexpr int HEIGHT2 = 30;
constexpr int START_X5 = 0;
constexpr int START_Y5 = 900;
constexpr int HEIGHT5 = 50;
constexpr int START_X_SCALE = 8;
constexpr int START_Y_SCALE = 8;
constexpr int WIDTH_SCALE1 = 3;
constexpr int WIDTH_SCALE2 = 4;
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif
+233
View File
@@ -0,0 +1,233 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "battery_led.h"
#include <hdf_log.h>
#include <hdf_base.h>
#include <fstream>
#include <memory>
#include <string>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#define Hi3516DV300
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
#ifdef Hi3516DV300
static const std::string LED_RED_PATH = "/data/local/tmp/leds/sc27xx:red/brightness";
static const std::string LED_GREEN_PATH = "/data/local/tmp/leds/sc27xx:green/brightness";
static const std::string LED_BLUE_PATH = "/data/local/tmp/leds/sc27xx:blue/brightness";
#else
static const std::string LED_RED_PATH = "/sys/class/leds/sc27xx:red/brightness";
static const std::string LED_GREEN_PATH = "/sys/class/leds/sc27xx:green/brightness";
static const std::string LED_BLUE_PATH = "/sys/class/leds/sc27xx:blue/brightness";
#endif
static const int CAPACITY_FULL = 100;
static const int MKDIR_WAIT_TIME = 1;
static const int LED_COLOR_GREEN = 2;
static const int LED_COLOR_RED = 4;
static const int LED_COLOR_YELLOW = 6;
void BatteryLed::TurnOffLed()
{
HDF_LOGI("%{public}s enter", __func__);
WriteLedInfoToSys(0, 0, 0);
HDF_LOGI("%{public}s exit", __func__);
return;
}
void BatteryLed::UpdateLedColor(const int32_t &chargestate, const int32_t &capacity)
{
HDF_LOGI("%{public}s enter", __func__);
if ((chargestate == PowerSupplyProvider::CHARGE_STATE_NONE) ||
(chargestate == PowerSupplyProvider::CHARGE_STATE_RESERVED)) {
TurnOffLed();
HDF_LOGD("%{public}s: reset led color.", __func__);
return;
}
std::unique_ptr<BatteryConfig> batteryConfig = std::make_unique<BatteryConfig>();
if (batteryConfig == nullptr) {
HDF_LOGD("%{public}s: batteryConfig is nullptr", __func__);
return;
}
batteryConfig->Init();
auto ledConf = batteryConfig->GetLedConf();
for (auto it = ledConf.begin(); it != ledConf.end(); ++it) {
HDF_LOGD("%{public}s: ledConf.begin()=%{public}d, ledConf.end()=%{public}d", __func__, \
it->capacityBegin, it->capacityEnd);
if ((capacity >= it->capacityBegin) && (capacity < it->capacityEnd)) {
switch (it->color) {
case (LED_COLOR_GREEN): {
HDF_LOGD("%{public}s: led color display green.", __func__);
WriteLedInfoToSys(0, it->brightness, 0);
break;
}
case (LED_COLOR_RED): {
HDF_LOGD("%{public}s: led color display red.", __func__);
WriteLedInfoToSys(it->brightness, 0, 0);
break;
}
case (LED_COLOR_YELLOW): {
HDF_LOGD("%{public}s: led color display yellow.", __func__);
WriteLedInfoToSys(it->brightness, it->brightness, 0);
break;
}
default: {
HDF_LOGD("%{public}s: led color display error.", __func__);
break;
}
}
break;
}
if (capacity == CAPACITY_FULL) {
HDF_LOGD("%{public}s: led color display green.", __func__);
WriteLedInfoToSys(0, it->brightness, 0);
break;
}
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void BatteryLed::WriteLedInfoToSys(const int redbrightness, const int greenbrightness, const int bluebrightness)
{
HDF_LOGI("%{public}s enter", __func__);
FILE *file = nullptr;
#ifdef Hi3516DV300
InitMockLedFile();
#endif
file = fopen(LED_RED_PATH.c_str(), "w");
if (file == nullptr) {
HDF_LOGD("%{public}s: red led file open failed.", __func__);
return;
}
int ret = fprintf(file, "%d\n", redbrightness);
if (ret < 0) {
HDF_LOGD("%{public}s: red led file fprintf failed.", __func__);
}
ret = fclose(file);
if (ret < 0) {
return;
}
file = fopen(LED_GREEN_PATH.c_str(), "w");
if (file == nullptr) {
HDF_LOGD("%{public}s: green led file open failed.", __func__);
return;
}
ret = fprintf(file, "%d\n", greenbrightness);
if (ret < 0) {
HDF_LOGD("%{public}s: green led file fprintf failed.", __func__);
}
ret = fclose(file);
if (ret < 0) {
return;
}
file = fopen(LED_BLUE_PATH.c_str(), "w");
if (file == nullptr) {
HDF_LOGD("%{public}s: blue led file open failed.", __func__);
return;
}
ret = fprintf(file, "%d\n", bluebrightness);
if (ret < 0) {
HDF_LOGD("%{public}s: blue led file fprintf failed.", __func__);
}
ret = fclose(file);
if (ret < 0) {
return;
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
const char *BatteryLed::CreateFile(const char *path, const char *content)
{
HDF_LOGI("%{public}s enter", __func__);
std::ofstream stream(path);
if (!stream.is_open()) {
HDF_LOGD("%{public}s: Cannot create file %{public}s", __func__, path);
return nullptr;
}
stream << content << std::endl;
stream.close();
return path;
}
void BatteryLed::InitMockLedFile()
{
HDF_LOGI("%{public}s enter", __func__);
std::string redLedPath = "/data/local/tmp/leds/sc27xx:red";
std::string greenLedPath = "/data/local/tmp/leds/sc27xx:green";
std::string blueLedPath = "/data/local/tmp/leds/sc27xx:blue";
std::string ledsPath = "/data/local/tmp/leds";
int ret = 0;
if (access(ledsPath.c_str(), 0) == -1) {
ret = mkdir("/data/local/tmp/leds", S_IRWXU);
if (ret == -1) {
HDF_LOGD("%{public}s: create leds path fail.", __func__);
return;
}
sleep(MKDIR_WAIT_TIME);
}
if (access(redLedPath.c_str(), 0) == -1) {
ret = mkdir("/data/local/tmp/leds/sc27xx:red", S_IRWXU);
if (ret == -1) {
HDF_LOGD("%{public}s: create red led path fail.", __func__);
return;
}
sleep(MKDIR_WAIT_TIME);
}
if (access(greenLedPath.c_str(), 0) == -1) {
ret = mkdir("/data/local/tmp/leds/sc27xx:green", S_IRWXU);
if (ret == -1) {
HDF_LOGD("%{public}s: create green led path fail.", __func__);
return;
}
sleep(MKDIR_WAIT_TIME);
}
if (access(blueLedPath.c_str(), 0) == -1) {
ret = mkdir("/data/local/tmp/leds/sc27xx:blue", S_IRWXU);
if (ret == -1) {
HDF_LOGD("%{public}s: create blue led path fail.", __func__);
return;
}
sleep(MKDIR_WAIT_TIME);
}
HDF_LOGE("%{public}s: create mock path for Hi3516DV300", __func__);
CreateFile("/data/local/tmp/leds/sc27xx:red/brightness", "0");
CreateFile("/data/local/tmp/leds/sc27xx:green/brightness", "0");
CreateFile("/data/local/tmp/leds/sc27xx:blue/brightness", "0");
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BATTERY_LED_H
#define BATTERY_LED_H
#include "power_supply_provider.h"
#include "battery_config.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class BatteryLed {
public:
void TurnOffLed();
void WriteLedInfoToSys(const int redbrightness, const int greenbrightness, const int bluebrightness);
void UpdateLedColor(const int32_t &chargestate, const int32_t &capacity);
private:
void InitMockLedFile();
const char *CreateFile(const char *path, const char *content);
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif
+477
View File
@@ -0,0 +1,477 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//base/powermgr/battery_manager/batterymgr.gni")
import("//build/ohos.gni")
## Install resources to /system/etc/resources
ohos_prebuilt_etc("resources_config") {
source = "font.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config1") {
source = "icon_error.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config2") {
source = "loop00000.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config3") {
source = "loop00001.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config4") {
source = "loop00002.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config5") {
source = "loop00003.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config6") {
source = "loop00004.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config7") {
source = "loop00005.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config8") {
source = "loop00006.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config9") {
source = "loop00007.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config10") {
source = "loop00008.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config11") {
source = "loop00009.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config12") {
source = "loop00010.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config13") {
source = "loop00011.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config14") {
source = "loop00012.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config15") {
source = "loop00013.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config16") {
source = "loop00014.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config17") {
source = "loop00015.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config18") {
source = "loop00016.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config19") {
source = "loop00017.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config20") {
source = "loop00018.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config21") {
source = "loop00019.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config22") {
source = "loop00020.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config23") {
source = "loop00021.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config24") {
source = "loop00022.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config25") {
source = "loop00023.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config26") {
source = "loop00024.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config27") {
source = "loop00025.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config28") {
source = "loop00026.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config29") {
source = "loop00027.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config30") {
source = "loop00028.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config31") {
source = "loop00029.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config32") {
source = "loop00030.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config33") {
source = "loop00031.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config34") {
source = "loop00032.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config35") {
source = "loop00033.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config36") {
source = "loop00034.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config37") {
source = "loop00035.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config38") {
source = "loop00036.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config39") {
source = "loop00037.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config40") {
source = "loop00038.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config41") {
source = "loop00039.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config42") {
source = "loop00040.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config43") {
source = "loop00041.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config44") {
source = "loop00042.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config45") {
source = "loop00043.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config46") {
source = "loop00044.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config47") {
source = "loop00045.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config48") {
source = "loop00046.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config49") {
source = "loop00047.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config50") {
source = "loop00048.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config51") {
source = "loop00049.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config52") {
source = "loop00050.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config53") {
source = "loop00051.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config54") {
source = "loop00052.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config55") {
source = "loop00053.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config56") {
source = "loop00054.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config57") {
source = "loop00055.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config58") {
source = "loop00056.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config59") {
source = "loop00057.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config60") {
source = "loop00058.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config61") {
source = "loop00059.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config62") {
source = "loop00060.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config63") {
source = "loop00061.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config64") {
source = "progress_empty.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
ohos_prebuilt_etc("resources_config65") {
source = "progress_fill.png"
relative_install_dir = "resources"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
+23
View File
@@ -0,0 +1,23 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//base/powermgr/battery_manager/batterymgr.gni")
import("//build/ohos.gni")
## Install led_config.json to /system/etc/ledconfig/led_config.json
ohos_prebuilt_etc("led_config") {
source = "led_config.json"
relative_install_dir = "ledconfig"
part_name = "battery_manager_native"
subsystem_name = "powermgr"
}
+18
View File
@@ -0,0 +1,18 @@
{
"led":{
"comments":"capacity_begin, capacity_end, led_color, led_brighness",
"table":[
[0, 10, 4, 255],
[10, 90, 6, 255],
[90, 100, 2, 255]
]
},
"temperature":{
"comments":"temperature_lower, temperature_upper",
"table":[-100, 600]
},
"soc":{
"comments":"soc_lower",
"table":[3]
}
}
+15 -14
View File
@@ -13,20 +13,21 @@
* limitations under the License.
*/
#ifndef BATTERYD_PARSER_H
#define BATTERYD_PARSER_H
#include "charger_thread.h"
#include "batteryd_api.h"
#include "hdf_log.h"
int32_t InitBatterydSysfs(void);
int32_t ParseCapacity(int32_t *capacity);
int32_t ParseVoltage(int32_t *voltage);
int32_t ParseTemperature(int32_t *temperature);
int32_t ParseHealthState(int32_t *healthState);
int32_t ParsePluggedType(int32_t *pluggedType);
int32_t ParseChargeState(int32_t *chargeState);
int32_t ParsePresent(int8_t *present);
int32_t ParseTechnology(char *technology, size_t size);
void ParseUeventToBatterydInfo(const char *msg, struct BatterydInfo *info);
int main(int argc, char **argv)
{
HDF_LOGI("%{public}s enter", __func__);
using namespace OHOS::HDI::Battery::V1_0;
std::unique_ptr<ChargerThread> chargerThread = std::make_unique<ChargerThread>();
if (chargerThread == nullptr) {
HDF_LOGE("%{public}s: instantiate loop error", __func__);
return HDF_ERR_MALLOC_FAIL;
}
chargerThread->StartThread(nullptr);
#endif // BATTERYD_PARSER_H
HDF_LOGI("%{public}s exit", __func__);
return 0;
}
+606
View File
@@ -0,0 +1,606 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "charger_thread.h"
#include <parameters.h>
#include <securec.h>
#include <sys/epoll.h>
#include <sys/timerfd.h>
#include <cstdio>
#include <cstdlib>
#include <sys/socket.h>
#include <linux/netlink.h>
#include "utils/hdf_log.h"
#include "updater_ui.h"
#include "text_label.h"
#include "view.h"
#include "input_manager.h"
#define HDF_LOG_TAG Charger
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
struct keyState {
bool up;
bool down;
int64_t timestamp;
};
static const int SHUTDOWN_TIME_MS = 2000;
static const int CMD_MAX_SIZE = 92;
static const long long MAX_INT64 = 9223372036854775807;
static const int SEC_TO_MSEC = 1000;
static const int NSEC_TO_MSEC = 1000000;
static const int REBOOT_TIME = 2000;
static const int BACKLIGHT_OFF_TIME_MS = 10000;
static const uint32_t INIT_DEFAULT_VALUE = 255;
static const int VIBRATE_TIME_MS = 75;
constexpr int MAX_IMGS = 62;
constexpr int MAX_IMGS_NAME_SIZE = 255;
constexpr int LOOP_TOP_PICTURES = 10;
Frame *g_hosFrame;
Frame *g_updateFrame;
AnimationLabel *g_animationLabel;
TextLabel *g_updateInfoLabel;
TextLabel *g_logLabel;
TextLabel *g_logResultLabel;
IInputInterface *g_inputInterface;
InputEventCb g_callback;
struct keyState keys_[KEY_MAX + 1] = {};
int64_t ChargerThread::keyWait_ = -1;
int64_t ChargerThread::backlightWait_ = -1;
int32_t ChargerThread::capacity_ = -1;
static int64_t GetCurrentTime()
{
HDF_LOGI("%{public}s enter", __func__);
timespec tm;
clock_gettime(CLOCK_MONOTONIC, &tm);
HDF_LOGI("%{public}s exit", __func__);
return tm.tv_sec * SEC_TO_MSEC + (tm.tv_nsec / NSEC_TO_MSEC);
}
void ChargerThread::HandleStates()
{
HDF_LOGI("%{public}s enter", __func__);
HandleChargingState();
HandlePowerKeyState();
HandleScreenState();
HDF_LOGI("%{public}s exit", __func__);
return;
}
int ChargerThread::UpdateWaitInterval()
{
HDF_LOGI("%{public}s enter", __func__);
int64_t currentTime = GetCurrentTime();
int64_t nextWait = MAX_INT64;
int64_t timeout;
if (pluginWait_ != -1) {
nextWait = pluginWait_ - currentTime;
}
if (keyWait_ != -1 && keyWait_ < nextWait) {
nextWait = keyWait_;
}
if (backlightWait_ != -1 && backlightWait_ < nextWait) {
nextWait = backlightWait_;
}
if (nextWait != -1 && nextWait != MAX_INT64) {
if (nextWait - currentTime > 0) {
timeout = nextWait - currentTime;
} else {
timeout = 0;
}
} else {
timeout = -1;
}
HDF_LOGI("%{public}s exit", __func__);
return timeout;
}
void ChargerThread::AnimationInit()
{
HDF_LOGI("%{public}s enter", __func__);
constexpr char alpha = 0xff;
int screenH = 0;
int screenW = 0;
auto *sfDev = new SurfaceDev(SurfaceDev::DevType::DRM_DEVICE);
sfDev->GetScreenSize(screenW, screenH);
View::BRGA888Pixel bgColor {0x00, 0x00, 0x00, alpha};
g_hosFrame = new Frame(screenW, screenH, View::PixelFormat::BGRA888, sfDev);
g_hosFrame->SetBackgroundColor(&bgColor);
MenuItemInit(screenH, screenW, bgColor, g_hosFrame);
g_logLabel = new TextLabel(START_X1, START_Y1, WIDTH1, HEIGHT1, g_hosFrame);
struct FocusInfo info {false, false};
struct Bold bold {false, false};
TextLabelInit(g_logLabel, "", bold, info, bgColor);
g_logResultLabel = new TextLabel(START_X2, START_Y2, WIDTH2, HEIGHT2, g_hosFrame);
TextLabelInit(g_logResultLabel, "", bold, info, bgColor);
g_updateFrame = new Frame(screenW, screenH, View::PixelFormat::BGRA888, sfDev);
g_updateFrame->SetBackgroundColor(&bgColor);
g_animationLabel = new AnimationLabel(screenW / START_X_SCALE, screenH / START_Y_SCALE,
screenW * WIDTH_SCALE1 / WIDTH_SCALE2, screenH >> 1, g_updateFrame);
g_animationLabel->SetBackgroundColor(&bgColor);
LoadImgs(g_animationLabel);
g_updateInfoLabel = new TextLabel(START_X5, START_Y5, screenW, HEIGHT5, g_updateFrame);
g_updateInfoLabel->SetOutLineBold(false, false);
g_updateInfoLabel->SetBackgroundColor(&bgColor);
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::LoadImgs(AnimationLabel *g_animationLabel)
{
HDF_LOGD("%{public}s enter", __func__);
char nameBuf[MAX_IMGS_NAME_SIZE];
for (int i = 0; i < MAX_IMGS; i++) {
if (memset_s(nameBuf, MAX_IMGS_NAME_SIZE + 1, 0, MAX_IMGS_NAME_SIZE) != EOK) {
HDF_LOGD("%{public}s: Memset_s failed", __func__);
return;
}
if (i < LOOP_TOP_PICTURES) {
if (snprintf_s(nameBuf, MAX_IMGS_NAME_SIZE, MAX_IMGS_NAME_SIZE - 1,
"/system/etc/resources/loop0000%d.png", i) == -1) {
return;
}
} else {
if (snprintf_s(nameBuf, MAX_IMGS_NAME_SIZE, MAX_IMGS_NAME_SIZE - 1,
"/system/etc/resources/loop000%d.png", i) == -1) {
return;
}
}
g_animationLabel->AddImg(nameBuf);
}
g_animationLabel->AddStaticImg(nameBuf);
}
void ChargerThread::UpdateAnimation(const int32_t &capacity)
{
HDF_LOGI("%{public}s enter", __func__);
AnimationLabel::needStop_ = false;
g_animationLabel->UpdateLoop();
struct FocusInfo info {true, true};
struct Bold bold {true, false};
View::BRGA888Pixel bgColor {0x00, 0x00, 0x00, 0xff};
TextLabelInit(g_updateInfoLabel, std::to_string(capacity), bold, info, bgColor);
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::CycleMatters()
{
HDF_LOGI("%{public}s enter", __func__);
if (!started_) {
started_ = true;
backlightWait_ = GetCurrentTime() - 1;
}
provider_->ParseCapacity(&capacity_);
provider_->ParseChargeState(&chargeState_);
HDF_LOGI("%{public}s: chargeState_ = %{public}d, %{public}d", __func__, chargeState_, capacity_);
UpdateEpollInterval(chargeState_);
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::UpdateBatteryInfo(void *arg, char *msg)
{
HDF_LOGI("%{public}s enter", __func__);
std::unique_ptr<BatterydInfo> batteryInfo = std::make_unique<BatterydInfo>();
if (batteryInfo == nullptr) {
HDF_LOGE("%{public}s: instantiate batteryInfo error", __func__);
return;
}
provider_->ParseUeventToBatterydInfo(msg, batteryInfo.get());
capacity_ = batteryInfo->capacity_;
chargeState_ = batteryInfo->chargeState_;
HandleCapacity(capacity_);
HandleTemperature(batteryInfo->temperature_);
led_->UpdateLedColor(chargeState_, capacity_);
if (backlight_->GetScreenState()) {
UpdateAnimation(capacity_);
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::UpdateBatteryInfo(void *arg)
{
HDF_LOGI("%{public}s enter", __func__);
int32_t temperature = 0;
provider_->ParseTemperature(&temperature);
provider_->ParseCapacity(&capacity_);
provider_->ParseChargeState(&chargeState_);
HDF_LOGD("%{public}s: temperature=%{public}d, capacity_=%{public}d, chargeState_=%{public}d", \
__func__, temperature, capacity_, chargeState_);
HandleTemperature(temperature);
HandleCapacity(capacity_);
led_->UpdateLedColor(chargeState_, capacity_);
if (backlight_->GetScreenState()) {
UpdateAnimation(capacity_);
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::HandleCapacity(const int32_t &capacity)
{
HDF_LOGI("%{public}s enter", __func__);
if ((capacity <= batteryConfig_->GetCapacityConf()) &&
((chargeState_ == PowerSupplyProvider::CHARGE_STATE_NONE) ||
(chargeState_ == PowerSupplyProvider::CHARGE_STATE_RESERVED))) {
std::string reason = "LowCapacity";
ShutDown(reason);
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::HandleTemperature(const int32_t &temperature)
{
HDF_LOGI("%{public}s enter", __func__);
auto tempConf = batteryConfig_->GetTempConf();
HDF_LOGD("%{public}s: temperature=%{public}d, tempConf.lower=%{public}d, tempConf.upper=%{public}d",
__func__, temperature, tempConf.lower, tempConf.upper);
if (((temperature <= tempConf.lower) || (temperature >= tempConf.upper)) &&
(tempConf.lower != tempConf.upper)) {
std::string reason = "TemperatureOutOfRange";
ShutDown(reason);
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::SetKeyWait(struct keyState *key, int64_t timeout)
{
HDF_LOGI("%{public}s enter", __func__);
int64_t nextMoment = key->timestamp + timeout;
if (keyWait_ == -1 || nextMoment < keyWait_) {
keyWait_ = nextMoment;
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::HandleChargingState()
{
HDF_LOGI("%{public}s enter", __func__);
int64_t now = GetCurrentTime();
if ((chargeState_ == PowerSupplyProvider::CHARGE_STATE_NONE) ||
(chargeState_ == PowerSupplyProvider::CHARGE_STATE_RESERVED)) {
if (pluginWait_ == -1) {
backlightWait_ = now - 1;
backlight_->TurnOnScreen();
led_->TurnOffLed();
AnimationLabel::needStop_ = true;
pluginWait_ = now + SHUTDOWN_TIME_MS;
} else if (now >= pluginWait_) {
std::string reason = "charger unplugged";
ShutDown(reason);
} else {
HDF_LOGD("%{public}s: ShutDown timer already in scheduled.", __func__);
}
} else {
if (pluginWait_ != -1) {
backlightWait_ = now - 1;
backlight_->TurnOnScreen();
led_->UpdateLedColor(chargeState_, capacity_);
AnimationLabel::needStop_ = true;
UpdateAnimation(capacity_);
}
pluginWait_ = -1;
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::HandleScreenState()
{
HDF_LOGI("%{public}s enter", __func__);
if (backlightWait_ != -1 && GetCurrentTime() > backlightWait_ + BACKLIGHT_OFF_TIME_MS) {
backlight_->TurnOffScreen();
AnimationLabel::needStop_ = true;
backlightWait_ = -1;
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::ShutDown(std::string &reason)
{
HDF_LOGI("%{public}s enter", __func__);
char updateCmd[CMD_MAX_SIZE];
if (snprintf_s(updateCmd, CMD_MAX_SIZE, CMD_MAX_SIZE - 1, "shutdown,%s", reason.c_str()) < 0) {
HDF_LOGD("%{public}s: snprintf failed.", __func__);
return;
}
HDF_LOGD("%{public}s: Shutdown executing.", __func__);
OHOS::system::SetParameter("sys.powerctl", updateCmd);
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::Reboot(std::string &reason)
{
HDF_LOGI("%{public}s enter", __func__);
char updateCmd[CMD_MAX_SIZE];
if (snprintf_s(updateCmd, CMD_MAX_SIZE, CMD_MAX_SIZE - 1, "reboot,%s", reason.c_str()) < 0) {
HDF_LOGD("%{public}s: snprintf failed.", __func__);
return;
}
HDF_LOGD("%{public}s: Reboot executing.", __func__);
OHOS::system::SetParameter("sys.powerctl", updateCmd);
HDF_LOGI("%{public}s exit", __func__);
return;
}
int ChargerThread::SetKeyState(int code, int value, int64_t now)
{
HDF_LOGI("%{public}s enter", __func__);
bool down;
if (!!value) {
down = true;
} else {
down = false;
}
if (code > KEY_MAX) {
return -1;
}
if (keys_[code].down == down) {
return 0;
}
if (down) {
keys_[code].timestamp = now;
}
keys_[code].down = down;
keys_[code].up = true;
HDF_LOGI("%{public}s exit", __func__);
return 0;
}
void ChargerThread::HandlePowerKeyState()
{
HDF_LOGD("%{public}s enter", __func__);
auto now = GetCurrentTime();
HandlePowerKey(KEY_POWER, now);
if (keyWait_ != -1 && now > keyWait_) {
keyWait_ = -1;
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::HandlePowerKey(int keycode, int64_t now)
{
HDF_LOGI("%{public}s enter", __func__);
keyState *key = &keys_[keycode];
if (keycode == KEY_POWER) {
if (key->down) {
int64_t rebootTime = key->timestamp + REBOOT_TIME;
if (now >= rebootTime) {
HDF_LOGD("%{public}s: reboot machine.", __func__);
backlight_->TurnOffScreen();
AnimationLabel::needStop_ = true;
vibrate_->HandleVibrate(VIBRATE_TIME_MS);
std::string reason = "Reboot";
Reboot(reason);
} else {
SetKeyWait(key, REBOOT_TIME);
backlight_->TurnOnScreen();
AnimationLabel::needStop_ = true;
UpdateAnimation(capacity_);
backlightWait_ = now - 1;
HDF_LOGD("%{public}s: turn on the screen.", __func__);
}
} else {
if (key->up) {
backlight_->TurnOnScreen();
AnimationLabel::needStop_ = true;
UpdateAnimation(capacity_);
backlightWait_ = now - 1;
}
}
}
key->up = false;
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::HandleInputEvent(const struct input_event *iev)
{
HDF_LOGD("%{public}s enter", __func__);
input_event ev {};
ev.type = iev->type;
ev.code = iev->code;
ev.value = iev->value;
HDF_LOGD("%{public}s: ev.type=%{public}d, ev.code=%{public}d, ev.value=%{public}d", \
__func__, ev.type, ev.code, ev.value);
if (ev.type != EV_KEY) {
return;
}
SetKeyState(ev.code, ev.value, GetCurrentTime());
HDF_LOGI("%{public}s exit", __func__);
return;
}
void ChargerThread::EventPkgCallback(const EventPackage **pkgs, const uint32_t count, uint32_t devIndex)
{
HDF_LOGD("%{public}s enter", __func__);
if (pkgs == nullptr || *pkgs == nullptr) {
return;
}
for (uint32_t i = 0; i < count; i++) {
struct input_event ev = {
.type = static_cast<__u16>(pkgs[i]->type),
.code = static_cast<__u16>(pkgs[i]->code),
.value = pkgs[i]->value,
};
HandleInputEvent(&ev);
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
int ChargerThread::InputInit()
{
HDF_LOGD("%{public}s enter", __func__);
int ret = GetInputInterface(&g_inputInterface);
if (ret != INPUT_SUCCESS) {
HDF_LOGD("%{public}s: get input driver interface failed.", __func__);
return ret;
}
ret = g_inputInterface->iInputManager->OpenInputDevice(1);
if (ret) {
HDF_LOGD("%{public}s: open device1 failed.", __func__);
return ret;
}
uint32_t devType = 0;
ret = g_inputInterface->iInputController->GetDeviceType(1, &devType);
if (ret) {
HDF_LOGD("%{public}s: get device1's type failed.", __func__);
return ret;
}
g_callback.EventPkgCallback = EventPkgCallback;
ret = g_inputInterface->iInputReporter->RegisterReportCallback(1, &g_callback);
if (ret) {
HDF_LOGD("%{public}s: register callback failed for device 1.", __func__);
return ret;
}
devType = INIT_DEFAULT_VALUE;
ret = g_inputInterface->iInputController->GetDeviceType(1, &devType);
HDF_LOGI("%{public}s exit", __func__);
return 0;
}
void ChargerThread::Init()
{
HDF_LOGI("%{public}s enter", __func__);
batteryConfig_ = std::make_unique<BatteryConfig>();
if (batteryConfig_ == nullptr) {
HDF_LOGD("%{public}s: init batteryconfig error.", __func__);
return;
}
batteryConfig_->Init();
provider_ = std::make_unique<PowerSupplyProvider>();
if (provider_ == nullptr) {
HDF_LOGE("%{public}s: instantiate PowerSupplyProvider error.", __func__);
return;
}
provider_->InitPowerSupplySysfs();
vibrate_ = std::make_unique<BatteryVibrate>();
if (vibrate_ == nullptr) {
HDF_LOGE("%{public}s: instantiate BatteryVibrate error.", __func__);
return;
}
if (vibrate_->VibrateInit() < 0) {
HDF_LOGE("%{public}s: VibrateInit failed, vibration does not work.", __func__);
}
backlight_ = std::make_unique<BatteryBacklight>();
if (backlight_ == nullptr) {
HDF_LOGE("%{public}s: instantiate BatteryBacklight error.", __func__);
return;
}
backlight_->TurnOnScreen();
led_ = std::make_unique<BatteryLed>();
if (led_ == nullptr) {
HDF_LOGE("%{public}s: instantiate BatteryLed error.", __func__);
return;
}
led_->TurnOffLed();
AnimationInit();
InputInit();
}
void ChargerThread::Run(void *service)
{
HDF_LOGI("%{public}s enter", __func__);
Init();
std::make_unique<std::thread>(&ChargerThread::LoopingThreadEntry, this, service)->join();
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+64
View File
@@ -0,0 +1,64 @@
# Copyright (c) 2021 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
ohos_static_library("libminui") {
sources = [
"src/animation_label.cpp",
"src/frame.cpp",
"src/surface_dev.cpp",
"src/text_label.cpp",
"src/updater_ui.cpp",
"src/view.cpp",
]
include_dirs = [
"//third_party/libpng",
"//third_party/libdrm/include/drm",
"//third_party/libdrm",
"//drivers/framework/ability/sbuf/include",
"//drivers/framework/include/platform",
"//drivers/framework/include/core",
"//drivers/framework/include/osal",
"//drivers/framework/include/utils",
"//drivers/framework/include/config",
"//drivers/framework/include",
"//drivers/adapter/uhdf2/hdi",
"//drivers/adapter/uhdf2/osal/include",
"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",
"//drivers/peripheral/input/hal/include",
"//drivers/peripheral/input/interfaces/include",
"//base/update/updater/utils/include",
"//base/update/updater/services",
"//base/update/updater/interfaces/kits/include",
"//base/powermgr/battery_manager/hdi/service/include",
"//base/powermgr/battery_manager/hdi/api/include",
"//drivers/framework/core/common/include/host",
"//third_party/jsoncpp/include",
"//base/powermgr/battery_manager/charger/vibrate",
"//base/powermgr/battery_manager/charger/backlight",
"//base/powermgr/battery_manager/charger/led",
"//base/powermgr/battery_manager/charger/include",
"//base/powermgr/battery_manager/charger/ui/include",
]
deps = [
"//base/update/updater/interfaces/kits/packages:libpackageExt",
"//base/update/updater/services/log:libupdaterlog",
"//base/update/updater/utils:libutils",
"//drivers/peripheral/input/hal:hdi_input",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/libdrm:libdrm",
"//third_party/libpng:libpng",
]
}
+75
View File
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_ANIMATION_LABLE_H
#define CHARGER_UI_ANIMATION_LABLE_H
#include <ctime>
#include <thread>
#include <unistd.h>
#include <vector>
#include <memory>
#include "frame.h"
#include "png.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class AnimationLabel : public View {
public:
enum class PlayMode {
ANIMATION_MODE = 0,
STATIC_MODE,
};
AnimationLabel(int startX, int startY, int w, int h, Frame *mParent);
AnimationLabel() {}
~AnimationLabel() override;
public:
void AddImg(const std::string &imgFileName);
int AddStaticImg(const std::string &imgFileName);
void SetStaticImg(int picId);
void SetPlayMode(AnimationLabel::PlayMode mode);
void SetInterval(int ms);
std::thread updateThread;
bool selectable = false;
static void SetIsVisible(const bool visible);
void UpdateLoop();
static bool needStop_;
private:
struct PictureAttr {
png_uint_32 pictureWidth;
png_uint_32 pictureHeight;
png_byte pictureChannels;
int bitDepth;
int colorType;
};
void *LoadPng(const std::string &imgFileName);
View::BRGA888Pixel *HandleLoadPng(FILE **fp, char **pictureBufferTmp, struct PictureAttr &attr);
int LoadPngInternalWithFile(FILE *fp, png_structpp pngPtr, png_infopp pngInfoPtr, struct PictureAttr &attr);
void CopyPictureBuffer(struct PictureAttr &attr, char *pictureBufferTmp, BRGA888Pixel *pictureBuffer) const;
Frame *parent_ {};
int intervalMs_ = 50;
std::vector<char*> imgList_ {};
char *staticImgList_[255] {};
int staticShowId_ = 0;
bool showStatic_ = false;
int staticImgSize_ = 0;
static bool isVisible_;
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif // CHARGER_UI_ANIMATION_LABLE_H
+41
View File
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_FAULT_CODE_H
#define CHARGER_UI_FAULT_CODE_H
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
enum UpdaterErrorCode {
CODE_VERIFY_FAIL = 1000,
CODE_MOUNT_FAIL,
CODE_UMOUNT_FAIL,
CODE_DECOMPRESSION_FAIL,
CODE_COMPRESS_FAIL,
CODE_FORK_FAIL,
CODE_ANALYSIS_SCRIPT_FAIL,
CODE_MEMORY_FAIL,
CODE_MISC_OP_FAIL,
CODE_FACTORY_RESET_FAIL,
CODE_INCREMENT_UPDATER_FAIL,
CODE_FULL_UPDATER_FAIL,
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif // CHARGER_UI_FAULT_CODE_H
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_FRAME_H
#define CHARGER_UI_FRAME_H
#include <map>
#include <condition_variable>
#include "surface_dev.h"
#include "view.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
struct CmpByStartY {
bool operator()(const View *v1, const View *v2) const
{
return v1->startY_ < v2->startY_;
}
};
class Frame : public View {
public:
Frame(unsigned int w, unsigned int h, View::PixelFormat pixType, SurfaceDev *sfDev);
~Frame() override;
void OnDraw() override;
void ViewRegister(View *view);
private:
void FlushThreadLoop();
int currentActionIndex_ = 0;
int maxActionIndex_ = 0;
int listIndex_ = 0;
int frameViewId = 0;
bool flushFlag_ = false;
bool needStop_ = false;
SurfaceDev *sfDev_ = nullptr;
std::mutex frameMutex_;
std::mutex keyMutex_;
std::condition_variable_any mCondFlush_;
std::condition_variable_any mCondKey_;
std::map<View*, int, CmpByStartY> viewMapList_;
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif // CHARGER_UI_FRAME_H
+42
View File
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_LOG_H__
#define CHARGER_UI_LOG_H__
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
enum class LogInfo {
VERBOSE,
DEBUG,
INFO,
WARNING,
ERROR,
FATAL,
};
enum class UpdateState {
UPDATE_STAGE_BEGIN,
UPDATE_STAGE_SUCCESS,
UPDATE_STAGE_FAIL,
UPDATE_STAGE_OUT,
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif /* CHARGER_UI_LOG_H__ */
+43
View File
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_SURFACE_DEV_H
#define CHARGER_UI_SURFACE_DEV_H
#include <mutex>
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class SurfaceDev {
public:
enum class DevType {
FB_DEVICE = 0,
DRM_DEVICE,
};
explicit SurfaceDev(SurfaceDev::DevType devType);
~SurfaceDev();
void Flip(char *buf);
void GetScreenSize(int &w, int &h);
private:
std::mutex mMutex;
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif // CHARGER_UI_SURFACE_DEV_H
+79
View File
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_TEXT_LABLE_H
#define CHARGER_UI_TEXT_LABLE_H
#include <iostream>
#include <string>
#include "frame.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class TextLabel : public View {
using ClickCallback = std::function<void(int id)>;
enum class FontType {
DEFAULT_FONT,
};
enum class AlignmentMethod {
ALIGN_CENTER,
ALIGN_TO_LEFT,
ALIGN_TO_TOP,
};
public:
TextLabel(int mStartX, int mStartY, int w, int h, Frame *mparent);
~TextLabel() override {};
void SetText(const char *str);
void SetTextColor(BRGA888Pixel color);
void SetFont(FontType fType);
void SetOutLineBold(bool topBold, bool bottomBold);
void SetTextAlignmentMethod(AlignmentMethod methodH, AlignmentMethod methodV);
void OnDraw() override;
private:
void InitFont();
FILE *InitFontType();
void DrawText();
void DrawTextLoop(unsigned char ch, char *tmpBuf, int textSx, int textSy);
void DrawOutline();
void DrawFocus();
ClickCallback callBack_;
char textBuf_[512 + 1] {};
Frame *parent_ {};
AlignmentMethod fontAligMethodLevel_ = AlignmentMethod::ALIGN_TO_LEFT;
AlignmentMethod fontAligMethodUpright_ = AlignmentMethod::ALIGN_CENTER;
BRGA888Pixel outlineColor_ {};
BRGA888Pixel actionBgColor_ {};
BRGA888Pixel normalBgColor_ {};
BRGA888Pixel textColor_ {};
bool boldTopLine_ = false;
bool boldBottomLine_ = false;
FontType fontType_ { FontType::DEFAULT_FONT };
char fontBuf_[16 * 16] {};
unsigned int fontWidth_ = 0;
unsigned int fontHeight_ = 0;
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif // CHARGER_UI_
+45
View File
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_UPDATER_UI_H
#define CHARGER_UI_UPDATER_UI_H
#include "view.h"
#include "text_label.h"
#include "animation_label.h"
#include "frame.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
struct FocusInfo {
bool focus;
bool focusable;
};
struct Bold {
bool top;
bool bottom;
};
void MenuItemInit(int height, int width, View::BRGA888Pixel bgColor, Frame *g_hosFrame);
void TextLabelInit(TextLabel *t, const std::string &text, struct Bold bold, struct FocusInfo focus,
View::BRGA888Pixel color);
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif /* CHARGER_UI_HOS_UPDATER_H */
+82
View File
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef CHARGER_UI_VIEW_H
#define CHARGER_UI_VIEW_H
#include <mutex>
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class View {
public:
struct BRGA888Pixel {
unsigned char r;
unsigned char g;
unsigned char b;
unsigned char a;
};
struct RGB888Pixel {
unsigned char r;
unsigned char g;
unsigned char b;
};
enum class PixelFormat {
BGRA888,
};
View() {};
virtual ~View() {};
void *CreateBuffer(int w, int h, View::PixelFormat pixelFormat);
virtual void SetBackgroundColor(BRGA888Pixel *color);
virtual void DrawSubView(int x, int y, int w, int h, char *buf);
virtual void OnDraw();
virtual void OnFocus(bool foucsed);
char *GetBuffer() const;
void *GetRawBuffer() const;
int GetBufferSize() const
{
return bufferSize_;
}
void SyncBuffer();
void SetViewId(int id);
int GetViewId() const;
void FreeBuffer();
bool IsVisiable() const;
bool IsSelected() const;
bool IsFocusAble() const;
void SetFocusAble(bool focusable);
int startX_ = 0;
int startY_ = 0;
int viewWidth_ = 0;
int viewHeight_ = 0;
std::mutex mutex_;
private:
char *viewBuffer_ = nullptr;
char *shadowBuffer_ = nullptr;
int bufferSize_ = 0;
bool isVisiable_ = true;
int viewId_ = 0;
bool focusable_ = false;
bool isFocused_ = false;
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif // CHARGER_UI_VIEW_H
+316
View File
@@ -0,0 +1,316 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "animation_label.h"
#include <cerrno>
#include <cstdio>
#include <string>
#include <sys/epoll.h>
#include "frame.h"
#include "log.h"
#include "png.h"
#include "securec.h"
#include "view.h"
#include "utils/hdf_log.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
static constexpr int PNG_HEADER_SIZE = 8;
static constexpr int MAX_PICTURE_CHANNELS = 3;
static constexpr int MAX_BIT_DEPTH = 8;
static constexpr useconds_t SECOND_PER_MS = 1000;
bool AnimationLabel::isVisible_ = true;
bool AnimationLabel::needStop_ = false;
AnimationLabel::AnimationLabel(int startX, int startY, int w, int h, Frame *mParent)
{
startX_ = startX;
startY_ = startY;
this->CreateBuffer(w, h, View::PixelFormat::BGRA888);
parent_ = mParent;
SetFocusAble(false);
needStop_ = false;
parent_->ViewRegister(this);
}
AnimationLabel::~AnimationLabel()
{
needStop_ = true;
FreeBuffer();
int imgSize = imgList_.size();
for (int i = 0; i < imgSize; i++) {
free(imgList_[i]);
}
imgList_.clear();
}
void AnimationLabel::SetStaticImg(int picId)
{
staticShowId_ = picId;
}
void AnimationLabel::SetIsVisible(const bool visible)
{
isVisible_ = visible;
}
void AnimationLabel::SetPlayMode(AnimationLabel::PlayMode mode)
{
HDF_LOGD("%{public}s enter", __func__);
if (mode == AnimationLabel::PlayMode::ANIMATION_MODE) {
showStatic_ = false;
} else if (mode == AnimationLabel::PlayMode::STATIC_MODE) {
showStatic_ = true;
}
}
void AnimationLabel::UpdateLoop()
{
HDF_LOGD("%{public}s enter", __func__);
unsigned int index = 0;
while (!needStop_) {
if (showStatic_) {
usleep(SECOND_PER_MS * SECOND_PER_MS);
} else {
usleep(intervalMs_ * SECOND_PER_MS);
}
if (imgList_.size() <= 0) {
continue;
}
HDF_LOGD("%{public}s, isVisible_ = %{public}d", __func__, isVisible_);
if (!isVisible_) {
continue;
}
if (imgList_.size() <= index) {
index = 0;
needStop_ = true;
}
SyncBuffer();
mutex_.lock();
if (showStatic_) {
if (staticShowId_ < staticImgSize_) {
DrawSubView(0, 0, viewWidth_, viewHeight_, staticImgList_[staticShowId_]);
}
} else {
DrawSubView(0, 0, viewWidth_, viewHeight_, imgList_[index]);
}
mutex_.unlock();
if (parent_ != nullptr) {
parent_->OnDraw();
}
index++;
}
HDF_LOGD("%{public}s loop end.", __func__);
}
void AnimationLabel::AddImg(const std::string &imgFileName)
{
HDF_LOGD("%{public}s enter", __func__);
mutex_.lock();
char *buf = static_cast<char*>(LoadPng(imgFileName));
imgList_.push_back(buf);
mutex_.unlock();
}
int AnimationLabel::AddStaticImg(const std::string &imgFileName)
{
HDF_LOGD("%{public}s enter", __func__);
int id = staticImgSize_;
mutex_.lock();
staticImgList_[id] = static_cast<char*>(LoadPng(imgFileName));
staticImgSize_++;
mutex_.unlock();
return id;
}
int AnimationLabel::LoadPngInternalWithFile(FILE *fp, png_structpp pngPtr, png_infopp pngInfoPtr,
struct PictureAttr &attr)
{
HDF_LOGD("%{public}s enter", __func__);
if (fp == nullptr) {
return -1;
}
uint8_t header[PNG_HEADER_SIZE];
size_t bytesRead = fread(header, 1, sizeof(header), fp);
if (bytesRead != sizeof(header)) {
HDF_LOGE("%{public}s, read header from file failed, errno=%{public}d", __func__, errno);
return -1;
}
if (png_sig_cmp(header, 0, sizeof(header))) {
HDF_LOGE("%{public}s, png file header is not valid.", __func__);
return -1;
}
*pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (*pngPtr == nullptr) {
HDF_LOGE("%{public}s, creat png struct failed.", __func__);
return -1;
}
*pngInfoPtr = png_create_info_struct(*pngPtr);
if (*pngInfoPtr == nullptr) {
HDF_LOGE("%{public}s, creat png info failed.", __func__);
return -1;
}
png_init_io(*pngPtr, fp);
png_set_sig_bytes(*pngPtr, sizeof(header));
png_read_info(*pngPtr, *pngInfoPtr);
png_get_IHDR(*pngPtr, *pngInfoPtr, &attr.pictureWidth, &attr.pictureHeight, &attr.bitDepth, &attr.colorType,
nullptr, nullptr, nullptr);
attr.pictureChannels = png_get_channels(*pngPtr, *pngInfoPtr);
if (attr.bitDepth <= MAX_BIT_DEPTH && attr.pictureChannels == 1 && attr.colorType == PNG_COLOR_TYPE_PALETTE) {
// paletted images: expand to 8-bit RGB. Note that we DON'T
// currently expand the tRNS chunk (if any) to an alpha
// channel, because minui doesn't support alpha channels in
// general.
png_set_palette_to_rgb(*pngPtr);
attr.pictureChannels = MAX_PICTURE_CHANNELS;
}
if (attr.pictureChannels < MAX_PICTURE_CHANNELS) {
HDF_LOGE("%{public}s, need rgb format pic.", __func__);
return -1;
}
return 0;
}
void AnimationLabel::CopyPictureBuffer(struct PictureAttr &attr, char *pictureBufferTmp,
BRGA888Pixel *pictureBuffer) const
{
HDF_LOGD("%{public}s enter", __func__);
int copyHeight = (viewHeight_ < static_cast<int>(attr.pictureHeight)) ? viewHeight_ :
static_cast<int>(attr.pictureHeight);
int copyWidth = (viewWidth_ < static_cast<int>(attr.pictureWidth)) ? viewWidth_ :
static_cast<int>(attr.pictureWidth);
auto *rgb = reinterpret_cast<RGB888Pixel*>(pictureBufferTmp);
for (int y = 0; y < copyHeight; y++) {
for (int x = 0; x < copyWidth; x++) {
unsigned int colorValue = rgb[x + y * attr.pictureWidth].r +
rgb[x + y * attr.pictureWidth].g + rgb[x + y * attr.pictureWidth].b;
if (colorValue > 0) {
pictureBuffer[x + y * viewWidth_].r = rgb[x + y * attr.pictureWidth].r;
pictureBuffer[x + y * viewWidth_].g = rgb[x + y * attr.pictureWidth].g;
pictureBuffer[x + y * viewWidth_].b = rgb[x + y * attr.pictureWidth].b;
pictureBuffer[x + y * viewWidth_].a = 0xff;
}
}
}
}
void *AnimationLabel::LoadPng(const std::string &imgFileName)
{
HDF_LOGD("%{public}s enter", __func__);
png_structp pngPtr = nullptr;
png_infop pngInfoPtr = nullptr;
struct PictureAttr attr {};
char *pictureBufferTmp = nullptr;
uint8_t *pictureRow = nullptr;
FILE *fp = fopen(imgFileName.c_str(), "rb");
if (fp == nullptr) {
HDF_LOGD("%{public}s: open font file failed.", __func__);
return nullptr;
}
if (LoadPngInternalWithFile(fp, &pngPtr, &pngInfoPtr, attr) < 0) {
png_destroy_read_struct(&pngPtr, &pngInfoPtr, 0);
fclose(fp);
fp = nullptr;
return nullptr;
}
unsigned int pictureRowSize = attr.pictureWidth * attr.pictureChannels;
pictureBufferTmp = static_cast<char *>(malloc(pictureRowSize * attr.pictureHeight));
if (pictureBufferTmp == nullptr) {
HDF_LOGD("%{public}s: Allocate memory failed.", __func__);
if (fp != nullptr) {
fclose(fp);
fp = nullptr;
}
return nullptr;
}
for (unsigned int y = 0; y < attr.pictureHeight; y++) {
pictureRow = reinterpret_cast<uint8_t *>((pictureBufferTmp) + y * pictureRowSize);
png_read_row(pngPtr, pictureRow, nullptr);
}
BRGA888Pixel *pictureBuffer = HandleLoadPng(&fp, &pictureBufferTmp, attr);
return static_cast<void *>(pictureBuffer);
}
View::BRGA888Pixel *AnimationLabel::HandleLoadPng(FILE **fp, char **pictureBufferTmp, struct PictureAttr &attr)
{
HDF_LOGD("%{public}s enter", __func__);
int pictureBufferSize = viewHeight_ * viewWidth_ * sizeof(BRGA888Pixel);
BRGA888Pixel *pictureBuffer = nullptr;
char *backgroundBuffer = static_cast<char*>(GetRawBuffer());
pictureBuffer = static_cast<BRGA888Pixel*>(malloc(pictureBufferSize));
if (pictureBuffer == nullptr) {
HDF_LOGD("%{public}s: Allocate memory failed.", __func__);
if (*pictureBufferTmp != nullptr) {
free(*pictureBufferTmp);
*pictureBufferTmp = nullptr;
}
if (*fp != nullptr) {
fclose(*fp);
*fp = nullptr;
}
return nullptr;
}
if (memcpy_s(reinterpret_cast<char *>(pictureBuffer), pictureBufferSize,
backgroundBuffer, pictureBufferSize) != EOK) {
if (*pictureBufferTmp != nullptr) {
free(*pictureBufferTmp);
*pictureBufferTmp = nullptr;
}
if (pictureBuffer != nullptr) {
free(pictureBuffer);
pictureBuffer = nullptr;
}
if (*fp != nullptr) {
fclose(*fp);
*fp = nullptr;
}
return nullptr;
}
CopyPictureBuffer(attr, *pictureBufferTmp, pictureBuffer);
free(*pictureBufferTmp);
*pictureBufferTmp = nullptr;
int ret = fclose(*fp);
if (ret < 0) {
HDF_LOGD("%{public}s: fp file close failed.", __func__);
return nullptr;
}
*fp = nullptr;
return pictureBuffer;
}
void AnimationLabel::SetInterval(int ms)
{
HDF_LOGD("%{public}s enter", __func__);
intervalMs_ = ms;
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+96
View File
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "frame.h"
#include <thread>
#include "log.h"
#include "utils/hdf_log.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
using namespace std;
Frame::Frame(unsigned int w, unsigned int h, View::PixelFormat pixType, SurfaceDev *sfDev)
{
this->CreateBuffer(w, h, pixType);
this->startX_ = 0;
this->startY_ = 0;
sfDev_ = sfDev;
listIndex_ = 0;
flushFlag_ = false;
std::thread(&Frame::FlushThreadLoop, this).detach();
currentActionIndex_ = 0;
}
Frame::~Frame()
{
needStop_ = true;
FreeBuffer();
}
void Frame::FlushThreadLoop()
{
HDF_LOGD("%{public}s enter", __func__);
while (!needStop_) {
std::unique_lock<std::mutex> locker(mutex_);
if (!flushFlag_) {
mCondFlush_.wait(mutex_, [&] {
return flushFlag_;
});
}
SyncBuffer();
frameMutex_.lock();
std::map<View*, int>::iterator iter;
for (iter = viewMapList_.begin(); iter != viewMapList_.end(); ++iter) {
View *tmpView = (*iter).first;
HDF_LOGD("%{public}s enter, tmpView->IsVisiable()=%{public}d", __func__, tmpView->IsVisiable());
if (tmpView->IsVisiable()) {
char *bufTmp = static_cast<char *>(tmpView->GetBuffer());
DrawSubView(tmpView->startX_, tmpView->startY_, tmpView->viewWidth_, tmpView->viewHeight_, bufTmp);
}
}
frameMutex_.unlock();
sfDev_->Flip(this->GetBuffer());
flushFlag_ = false;
}
}
void Frame::ViewRegister(View *view)
{
HDF_LOGD("%{public}s enter", __func__);
std::unique_lock<std::mutex> locker(frameMutex_);
view->SetViewId(frameViewId + listIndex_);
viewMapList_.insert(std::make_pair(view, frameViewId + listIndex_));
if (view->IsFocusAble()) {
maxActionIndex_++;
HDF_LOGD("%{public}s enter, maxActionIndex_=%{public}d", __func__, maxActionIndex_);
}
listIndex_++;
}
void Frame::OnDraw()
{
HDF_LOGD("%{public}s enter", __func__);
std::unique_lock<std::mutex> locker(mutex_);
flushFlag_ = true;
mCondFlush_.notify_all();
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+165
View File
@@ -0,0 +1,165 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "surface_dev.h"
#include <cerrno>
#include <cstring>
#include <drm_fourcc.h>
#include <fcntl.h>
#include <poll.h>
#include <sys/mman.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <linux/fb.h>
#include "log.h"
#include "securec.h"
#include "utils/hdf_log.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
struct BufferObject {
uint32_t width;
uint32_t height;
uint32_t pitch;
uint32_t handle;
uint32_t size;
uint8_t *vaddr;
uint32_t fbId;
};
struct BufferObject g_buff;
void SurfaceDev::Flip(char *buf)
{
HDF_LOGD("%{public}s enter", __func__);
if (!buf) {
HDF_LOGE("%{public}s, buf is null.", __func__);
return;
}
if (memcpy_s(g_buff.vaddr, g_buff.size, static_cast<void*>(buf), g_buff.size) != EOK) {
HDF_LOGE("%{public}s, memcpy_s fail.", __func__);
return;
}
}
static int ModesetCreateFb(int fd, struct BufferObject *bo)
{
HDF_LOGD("%{public}s enter", __func__);
struct drm_mode_create_dumb create = {};
struct drm_mode_map_dumb map = {};
const int offsetNumber = 4;
uint32_t handles[offsetNumber] = {0};
uint32_t pitches[offsetNumber] = {0};
uint32_t offsets[offsetNumber] = {0};
int ret;
/* create a dumb-buffer, the pixel format is XRGB888 */
const int pixelDepth = 32;
create.width = bo->width;
create.height = bo->height;
create.bpp = pixelDepth;
drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &create);
/* bind the dumb-buffer to an FB object */
bo->pitch = create.pitch;
bo->size = create.size;
bo->handle = create.handle;
handles[0] = bo->handle;
pitches[0] = bo->pitch;
offsets[0] = 0;
ret = drmModeAddFB2(fd, bo->width, bo->height, DRM_FORMAT_ARGB8888, handles, pitches, offsets, &bo->fbId, 0);
if (ret) {
HDF_LOGD("%{public}s, [fbtest]failed to add fb %{public}d x %{public}d: errno %{public}s", __func__, \
bo->width, bo->height, strerror(errno));
return -1;
}
/* map the dumb-buffer to userspace */
map.handle = create.handle;
drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &map);
bo->vaddr = static_cast<uint8_t*>(mmap(0, create.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, map.offset));
const int newColor = 0xff00ff00;
uint32_t i = 0;
uint32_t color = newColor;
while (i < bo->size) {
if (memcpy_s(&bo->vaddr[i], bo->size, &color, sizeof(color)) != EOK) {
HDF_LOGE("%{public}s, memcpy_s fail.", __func__);
return -1;
}
i += sizeof(color);
}
return 0;
}
int DrmInit(void)
{
HDF_LOGD("%{public}s enter", __func__);
int fd = -1;
drmModeConnector *conn;
uint32_t connId;
uint32_t crtcId;
fd = open("/dev/dri/card0", O_RDWR | O_CLOEXEC);
if (fd < 0) {
HDF_LOGE("%{public}s, open failed.", __func__);
return -1;
}
drmModeRes *res = drmModeGetResources(fd);
if (res == nullptr) {
HDF_LOGE("%{public}s, drmModeGetResources.", __func__);
return -1;
}
crtcId = res->crtcs[0];
connId = res->connectors[1];
conn = drmModeGetConnector(fd, connId);
if (conn == nullptr) {
HDF_LOGE("%{public}s, drmModeGetConnector.", __func__);
return -1;
}
g_buff.width = conn->modes[0].hdisplay;
g_buff.height = conn->modes[0].vdisplay;
ModesetCreateFb(fd, &g_buff);
drmModeSetCrtc(fd, crtcId, g_buff.fbId, 0, 0, &connId, 1, &conn->modes[0]);
HDF_LOGD("%{public}s, drm init succcess.", __func__);
return 0;
}
SurfaceDev::SurfaceDev(SurfaceDev::DevType devType)
{
HDF_LOGD("%{public}s enter", __func__);
if (devType == SurfaceDev::DevType::DRM_DEVICE) {
DrmInit();
} else {
HDF_LOGE("%{public}s, Only Support drm driver.", __func__);
}
}
void SurfaceDev::GetScreenSize(int &w, int &h)
{
HDF_LOGD("%{public}s enter", __func__);
const int screenSizeW = 480;
const int screenSizeH = 960;
w = screenSizeW;
h = screenSizeH;
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+358
View File
@@ -0,0 +1,358 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "text_label.h"
#include <cstdio>
#include <iostream>
#include <string>
#include <linux/input.h>
#include "log.h"
#include "png.h"
#include "securec.h"
#include "utils/hdf_log.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
const std::string DEFAULT_FONT_NAME = "font";
constexpr int MAX_TEXT_SIZE = 512;
constexpr int MAX_FONT_BUFFER_SIZE_HW = 4096;
TextLabel::TextLabel(int mStartX, int mStartY, int w, int h, Frame *mparent)
{
startX_ = mStartX;
startY_ = mStartY;
this->CreateBuffer(w, h, View::PixelFormat::BGRA888);
parent_ = mparent;
parent_->ViewRegister(this);
SetFocusAble(true);
outlineColor_.r = 0x00;
outlineColor_.g = 0x00;
outlineColor_.b = 0x00;
outlineColor_.a = 0x00;
boldTopLine_ = false;
boldTopLine_ = false;
const char midAlpha = 0xAA;
actionBgColor_.r = 0x00;
actionBgColor_.g = 0x00;
actionBgColor_.b = 0x00;
actionBgColor_.a = midAlpha;
const char maxAlpha = 0xff;
normalBgColor_.r = 0x00;
normalBgColor_.g = 0x00;
normalBgColor_.b = 0x00;
normalBgColor_.a = maxAlpha;
const char maxLevel = 0xff;
textColor_.r = maxLevel;
textColor_.g = maxLevel;
textColor_.b = maxLevel;
textColor_.a = maxLevel;
if (memset_s(textBuf_, MAX_TEXT_SIZE + 1, 0, MAX_TEXT_SIZE) != 0) {
HDF_LOGE("%{public}s, memset_s failed!", __func__);
return;
}
InitFont();
}
void TextLabel::SetFont(FontType fType)
{
HDF_LOGD("%{public}s enter", __func__);
fontType_ = fType;
InitFont();
OnDraw();
}
static void PngInitSet(png_structp fontPngPtr, FILE *fp, int size, png_infop fontInfoPtr)
{
HDF_LOGD("%{public}s enter", __func__);
png_init_io(fontPngPtr, fp);
png_set_sig_bytes(fontPngPtr, size);
png_read_info(fontPngPtr, fontInfoPtr);
return;
}
static void PNGReadRow(png_uint_32 fontWidth, png_uint_32 fontHeight, png_structp fontPngPtr, char *fontBuf)
{
HDF_LOGD("%{public}s enter", __func__);
if ((fontWidth > MAX_FONT_BUFFER_SIZE_HW) || (fontHeight > MAX_FONT_BUFFER_SIZE_HW)) {
HDF_LOGE("%{public}s, font file size is too big!", __func__);
return;
}
for (unsigned int y = 0; y < fontHeight; y++) {
uint8_t *pRow = reinterpret_cast<uint8_t *>((fontBuf) + y * MAX_FONT_BUFFER_SIZE_HW);
png_read_row(fontPngPtr, pRow, nullptr);
}
return;
}
void TextLabel::InitFont()
{
HDF_LOGD("%{public}s enter", __func__);
png_infop fontInfoPtr = nullptr;
png_uint_32 fontWidth = 0;
png_uint_32 fontHeight = 0;
png_byte fontChannels = 0;
png_structp fontPngPtr = nullptr;
int fontBitDepth = 0;
int fontColorType = 0;
FILE *fp = InitFontType();
if (fp == nullptr) {
return;
}
const int headerNumber = 8;
uint8_t header[headerNumber];
size_t bytesRead = fread(header, 1, sizeof(header), fp);
if (bytesRead == sizeof(header)) {
HDF_LOGE("%{public}s, read header failed!", __func__);
return;
}
if (png_sig_cmp(header, 0, sizeof(header))) {
HDF_LOGE("%{public}s, cmp header failed!", __func__);
return;
}
fontPngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (fontPngPtr == nullptr) {
HDF_LOGE("%{public}s, creat font ptr_ failed!", __func__);
return;
}
fontInfoPtr = png_create_info_struct(fontPngPtr);
if ((!fontInfoPtr) || (setjmp(png_jmpbuf(fontPngPtr)))) {
return;
}
PngInitSet(fontPngPtr, fp, sizeof(header), fontInfoPtr);
png_get_IHDR(fontPngPtr, fontInfoPtr, &fontWidth, &fontHeight, &fontBitDepth, &fontColorType,
nullptr, nullptr, nullptr);
fontChannels = png_get_channels(fontPngPtr, fontInfoPtr);
const int defaultFontBitDepth = 8;
if (fontBitDepth <= defaultFontBitDepth && fontChannels == 1 && fontColorType == PNG_COLOR_TYPE_GRAY) {
png_set_expand_gray_1_2_4_to_8(fontPngPtr);
}
const int defaultFontWidth = 96;
fontWidth_ = fontWidth / defaultFontWidth;
fontHeight_ = fontHeight >> 1;
PNGReadRow(fontWidth_, fontHeight_, fontPngPtr, fontBuf_);
}
FILE *TextLabel::InitFontType()
{
HDF_LOGD("%{public}s enter", __func__);
char resPath[MAX_TEXT_SIZE + 1];
uint32_t offset = 2;
if (memset_s(resPath, MAX_TEXT_SIZE + offset, 0, MAX_TEXT_SIZE + 1) != EOK) {
return nullptr;
}
switch (fontType_) {
case FontType::DEFAULT_FONT:
if (snprintf_s(resPath, sizeof(resPath), sizeof(resPath) - 1,
"/system/etc/resources/%s.png", DEFAULT_FONT_NAME.c_str()) == -1) {
return nullptr;
}
break;
default:
if (snprintf_s(resPath, sizeof(resPath), sizeof(resPath) - 1,
"/system/etc/resources/%s.png", DEFAULT_FONT_NAME.c_str()) == -1) {
return nullptr;
}
break;
}
FILE *fp = fopen(resPath, "rb");
if (fp == nullptr) {
HDF_LOGD("%{public}s: open font failed.", __func__);
return nullptr;
}
return fp;
}
void TextLabel::SetText(const char *str)
{
HDF_LOGD("%{public}s enter", __func__);
if (memset_s(textBuf_, MAX_TEXT_SIZE + 1, 0, MAX_TEXT_SIZE) != EOK) {
HDF_LOGD("%{public}s: memset_s fail.", __func__);
return;
}
OnDraw();
if (memcpy_s(textBuf_, MAX_TEXT_SIZE + 1, str, strlen(const_cast<char*>(str))) != EOK) {
HDF_LOGD("%{public}s: mmecpy_s fail.", __func__);
return;
}
OnDraw();
}
void TextLabel::OnDraw()
{
HDF_LOGD("%{public}s enter", __func__);
std::unique_lock<std::mutex> locker(mutex_);
SyncBuffer();
if (IsSelected()) {
DrawFocus();
}
DrawOutline();
DrawText();
if (parent_ != nullptr) {
parent_->OnDraw();
}
}
void TextLabel::SetOutLineBold(bool topBold, bool bottomBold)
{
HDF_LOGD("%{public}s enter", __func__);
boldTopLine_ = topBold;
boldBottomLine_ = bottomBold;
OnDraw();
}
void TextLabel::DrawOutline()
{
HDF_LOGD("%{public}s enter", __func__);
void *tmpBuf = GetBuffer();
auto *pixelBuf = static_cast<BRGA888Pixel*>(tmpBuf);
for (int i = 0; i < viewWidth_; i++) {
if (boldTopLine_) {
pixelBuf[i].r = outlineColor_.r;
pixelBuf[i].g = outlineColor_.g;
pixelBuf[i].b = outlineColor_.b;
pixelBuf[i].a = outlineColor_.a;
pixelBuf[viewWidth_ + i].r = outlineColor_.r;
pixelBuf[viewWidth_ + i].g = outlineColor_.g;
pixelBuf[viewWidth_ + i].b = outlineColor_.b;
pixelBuf[viewWidth_ + i].a = outlineColor_.a;
}
const int lines = 2;
if (boldBottomLine_) {
pixelBuf[(viewHeight_ - lines) * viewWidth_ + i].r = outlineColor_.r;
pixelBuf[(viewHeight_ - lines) * viewWidth_ + i].g = outlineColor_.g;
pixelBuf[(viewHeight_ - lines) * viewWidth_ + i].b = outlineColor_.b;
pixelBuf[(viewHeight_ - lines) * viewWidth_ + i].a = outlineColor_.a;
pixelBuf[(viewHeight_ - 1) * viewWidth_ + i].r = outlineColor_.r;
pixelBuf[(viewHeight_ - 1) * viewWidth_ + i].g = outlineColor_.g;
pixelBuf[(viewHeight_ - 1) * viewWidth_ + i].b = outlineColor_.b;
pixelBuf[(viewHeight_ - 1) * viewWidth_ + i].a = outlineColor_.a;
}
}
}
void TextLabel::SetTextColor(BRGA888Pixel color)
{
HDF_LOGD("%{public}s enter", __func__);
textColor_.r = color.r;
textColor_.g = color.g;
textColor_.b = color.b;
textColor_.a = color.a;
}
void TextLabel::SetTextAlignmentMethod(AlignmentMethod methodH, AlignmentMethod methodV)
{
HDF_LOGD("%{public}s enter", __func__);
fontAligMethodLevel_ = methodH;
fontAligMethodUpright_ = methodV;
}
void TextLabel::DrawText()
{
HDF_LOGD("%{public}s enter", __func__);
void *tmpBuf = GetBuffer();
int textSx = 0;
int textSy = 0;
switch (fontAligMethodUpright_) {
case AlignmentMethod::ALIGN_CENTER:
textSy = (viewHeight_ - fontHeight_) >> 1;
break;
case AlignmentMethod::ALIGN_TO_TOP:
textSy = 0;
break;
default:
break;
}
const int minPosition = 10;
const int average = 2;
switch (fontAligMethodLevel_) {
case AlignmentMethod::ALIGN_CENTER:
textSx = (viewWidth_ - (strlen(textBuf_) * fontWidth_)) / average;
if (textSx < minPosition) {
textSx = minPosition;
}
break;
case AlignmentMethod::ALIGN_TO_LEFT:
textSx = minPosition;
break;
default:
break;
}
unsigned char ch;
char *s = textBuf_;
while ((ch = *s++)) {
if ((ch < ' ') || (ch > '~')) {
ch = '?';
}
DrawTextLoop(ch, static_cast<char*>(tmpBuf), textSx, textSy);
textSx += fontWidth_;
}
}
void TextLabel::DrawTextLoop(unsigned char ch, char *tmpBuf, int textSx, int textSy)
{
HDF_LOGD("%{public}s enter", __func__);
auto *srcP = reinterpret_cast<uint8_t*>(static_cast<char*>(fontBuf_) + ((ch - ' ') * fontWidth_));
auto *dstP = reinterpret_cast<BRGA888Pixel*>(tmpBuf + (textSy * viewWidth_ + textSx) * sizeof(BRGA888Pixel));
for (unsigned int j = 0; j < fontHeight_; j++) {
for (unsigned int i = 0; i < fontWidth_; i++) {
uint8_t a = srcP[i];
if (a > 0) {
dstP[i].r = textColor_.r;
dstP[i].g = textColor_.g;
dstP[i].b = textColor_.b;
dstP[i].a = textColor_.a;
}
}
srcP += MAX_FONT_BUFFER_SIZE_HW;
dstP = dstP + viewWidth_;
}
}
void TextLabel::DrawFocus()
{
HDF_LOGD("%{public}s enter", __func__);
BRGA888Pixel pixBuf[viewWidth_];
for (int a = 0; a < viewWidth_; a++) {
pixBuf[a].r = actionBgColor_.r;
pixBuf[a].g = actionBgColor_.g;
pixBuf[a].b = actionBgColor_.b;
pixBuf[a].a = actionBgColor_.a;
}
void *viewBgBuf = GetBuffer();
for (int i = 0; i < viewHeight_; i++) {
if (memcpy_s(static_cast<char*>(static_cast<char*>(viewBgBuf) + i * viewWidth_ *
sizeof(BRGA888Pixel)), viewWidth_ * sizeof(BRGA888Pixel) + 1, reinterpret_cast<char*>(pixBuf),
viewWidth_ * sizeof(BRGA888Pixel)) != EOK) {
HDF_LOGD("%{public}s: memcpy_s failed.", __func__);
return;
}
}
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+83
View File
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "updater_ui.h"
#include <cstdio>
#ifdef HAVE_TERMIOS_H
#include <termios.h>
#elif defined(HAVE_TERMIO_H)
#include <termio.h>
#endif
#include "log.h"
#include "securec.h"
#include "surface_dev.h"
#include "utils/hdf_log.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
constexpr int LABEL_HEIGHT = 15;
constexpr int LABEL0_OFFSET = 0;
constexpr int LABEL2_OFFSET = 1;
void TextLabelInit(TextLabel *t, const std::string &text, struct Bold bold,
struct FocusInfo focus, View::BRGA888Pixel color)
{
HDF_LOGD("%{public}s enter", __func__);
if (t != nullptr) {
t->SetText(text.c_str());
t->SetOutLineBold(bold.top, bold.bottom);
t->OnFocus(focus.focus);
t->SetBackgroundColor(&color);
t->SetFocusAble(focus.focusable);
}
}
void MenuItemInit(int height, int width, View::BRGA888Pixel bgColor, Frame *g_hosFrame)
{
HDF_LOGD("%{public}s enter", __func__);
TextLabel *g_textLabel0;
TextLabel *g_textLabel2;
if (g_hosFrame == nullptr) {
HDF_LOGD("%{public}s enter, Frame is null.", __func__);
return;
}
g_textLabel0 = new TextLabel(0, height * LABEL0_OFFSET / LABEL_HEIGHT, width, height /
LABEL_HEIGHT, g_hosFrame);
struct FocusInfo info {true, true};
struct Bold bold {true, false};
TextLabelInit(g_textLabel0, "Reboot to normal system", bold, info, bgColor);
if (!g_textLabel0) {
HDF_LOGD("%{public}s enter, g_textLabel0 is null.", __func__);
return;
}
g_textLabel2 = new TextLabel(0, height * LABEL2_OFFSET / LABEL_HEIGHT, width, height /
LABEL_HEIGHT, g_hosFrame);
info = {false, true};
bold = {false, false};
TextLabelInit(g_textLabel2, "Userdata reset", bold, info, bgColor);
if (!g_textLabel2) {
HDF_LOGD("%{public}s enter, g_textLabel2 is null.", __func__);
return;
}
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+184
View File
@@ -0,0 +1,184 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "view.h"
#include "log.h"
#include "securec.h"
#include "utils/hdf_log.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
constexpr int RGBA_PIXEL_SIZE = 4;
void *View::CreateBuffer(int w, int h, View::PixelFormat pixelFormat)
{
HDF_LOGD("%{public}s enter", __func__);
int pixelSize = -1;
switch (int(pixelFormat)) {
case int(View::PixelFormat::BGRA888):
pixelSize = RGBA_PIXEL_SIZE;
break;
default:
HDF_LOGD("%{public}s, Unsupported pixel_format: %{public}d", __func__, int(pixelFormat));
HDF_LOGD("%{public}s, Use default BGRA888.", __func__);
pixelSize = RGBA_PIXEL_SIZE;
break;
}
bufferSize_ = w * h * pixelSize;
viewBuffer_ = static_cast<char *>(malloc(bufferSize_));
if (viewBuffer_ == nullptr) {
HDF_LOGE("%{public}s, Allocate memory for view failed: %{public}d", __func__, errno);
return nullptr;
}
shadowBuffer_ = static_cast<char *>(malloc(bufferSize_));
if (shadowBuffer_ == nullptr) {
HDF_LOGE("%{public}s, Allocate memory for shadow failed: %{public}d", __func__, errno);
free(viewBuffer_);
viewBuffer_ = nullptr;
return nullptr;
}
if (memset_s(viewBuffer_, bufferSize_, 0, bufferSize_) != EOK) {
HDF_LOGE("%{public}s, Clean view buffer failed.", __func__);
free(viewBuffer_);
viewBuffer_ = nullptr;
return nullptr;
}
viewWidth_ = w;
viewHeight_ = h;
return viewBuffer_;
}
void View::SetBackgroundColor(BRGA888Pixel *color)
{
HDF_LOGD("%{public}s enter", __func__);
BRGA888Pixel pixelBuffer[viewWidth_];
for (int w = 0; w < viewWidth_; w++) {
pixelBuffer[w].r = color->r;
pixelBuffer[w].g = color->g;
pixelBuffer[w].b = color->b;
pixelBuffer[w].a = color->a;
}
for (int h = 0; h < viewHeight_; h++) {
if (memcpy_s(viewBuffer_ + h * viewWidth_ * sizeof(BRGA888Pixel), viewWidth_ * sizeof(BRGA888Pixel) + 1,
reinterpret_cast<char*>(pixelBuffer), viewWidth_ * sizeof(BRGA888Pixel)) != EOK) {
return;
}
}
if (isVisiable_) {
OnDraw();
HDF_LOGD("%{public}s, view---visable", __func__);
}
}
void View::DrawSubView(int x, int y, int w, int h, char *buf)
{
HDF_LOGD("%{public}s enter", __func__);
int minWidth = ((x + w) <= viewWidth_) ? w : (viewWidth_ - x);
int minHeight = ((y + h) <= viewHeight_) ? h : (viewHeight_ - y);
HDF_LOGD("%{public}s, x = %{public}d, y = %{public}d, w = %{public}d, h = %{public}d", __func__, x, y, w, h);
HDF_LOGD("%{public}s, minWidth = %{public}d, minHeight = %{public}d", __func__, minWidth, minHeight);
for (int i = 0; i < minHeight; i++) {
char *src = buf + i * w * sizeof(BRGA888Pixel);
char *dst = shadowBuffer_ + (i + y) * viewWidth_ * sizeof(BRGA888Pixel) + x * sizeof(BRGA888Pixel);
if (memcpy_s(dst, minWidth * sizeof(BRGA888Pixel) + 1, src, minWidth * sizeof(BRGA888Pixel)) != EOK) {
return;
}
}
}
void View::OnDraw()
{
HDF_LOGD("%{public}s enter", __func__);
std::unique_lock<std::mutex> locker(mutex_);
SyncBuffer();
}
void View::SyncBuffer()
{
HDF_LOGD("%{public}s enter", __func__);
if (memcpy_s(shadowBuffer_, bufferSize_, viewBuffer_, bufferSize_) != EOK) {
HDF_LOGD("%{public}s, Sync buffer failed.", __func__);
}
}
char *View::GetBuffer() const
{
HDF_LOGD("%{public}s enter", __func__);
return shadowBuffer_;
}
void *View::GetRawBuffer() const
{
HDF_LOGD("%{public}s enter", __func__);
return viewBuffer_;
}
void View::OnFocus(bool foucsed)
{
HDF_LOGD("%{public}s enter", __func__);
isFocused_ = foucsed;
OnDraw();
}
void View::SetViewId(int id)
{
HDF_LOGD("%{public}s enter", __func__);
viewId_ = id;
}
int View::GetViewId() const
{
HDF_LOGD("%{public}s enter", __func__);
return viewId_;
}
void View::FreeBuffer()
{
HDF_LOGD("%{public}s enter", __func__);
free(viewBuffer_);
free(shadowBuffer_);
viewBuffer_ = nullptr;
shadowBuffer_ = nullptr;
}
bool View::IsVisiable() const
{
HDF_LOGD("%{public}s enter", __func__);
return isVisiable_;
}
bool View::IsSelected() const
{
HDF_LOGD("%{public}s enter", __func__);
return isFocused_;
}
bool View::IsFocusAble() const
{
HDF_LOGD("%{public}s enter", __func__);
return focusable_;
}
void View::SetFocusAble(bool focusable)
{
HDF_LOGD("%{public}s enter", __func__);
focusable_ = focusable;
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+195
View File
@@ -0,0 +1,195 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "battery_vibrate.h"
#include <hdf_log.h>
#include <hdf_base.h>
#include <unistd.h>
#include "sys/stat.h"
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
static const std::string VIBRATOR_PLAYMODE_PATH = "/sys/class/leds/vibrator/play_mode";
static const std::string VIBRATOR_DURATIONMODE_PATH = "/sys/class/leds/vibrator/duration";
static const std::string VIBRATOR_ACTIVATEMODE_PATH = "/sys/class/leds/vibrator/activate";
static const int VIBRATION_PLAYMODE = 0;
static const int VIBRATION_DURATIONMODE = 1;
static const int VIBRATION_UNKNOWNMODE = -1;
static int VIBRANTION_MODE = VIBRATION_UNKNOWNMODE;
static const int VIBRATE_DELAY_MS = 5;
static const int USEC_TO_MSEC = 1000;
int BatteryVibrate::VibrateInit()
{
HDF_LOGI("%{public}s enter", __func__);
struct stat st;
if (!stat(VIBRATOR_PLAYMODE_PATH.c_str(), &st)) {
HDF_LOGD("%{public}s: vibrate path is play mode path", __func__);
VIBRANTION_MODE = VIBRATION_PLAYMODE;
return 0;
}
if (!stat(VIBRATOR_DURATIONMODE_PATH.c_str(), &st)) {
HDF_LOGD("%{public}s: vibrate path is duration path", __func__);
VIBRANTION_MODE = VIBRATION_DURATIONMODE;
return 0;
}
HDF_LOGI("%{public}s exit", __func__);
return -1;
}
FILE *BatteryVibrate::HandlePlayModePath()
{
HDF_LOGI("%{public}s enter", __func__);
FILE *file = nullptr;
file = fopen(VIBRATOR_PLAYMODE_PATH.c_str(), "w");
if (file == nullptr) {
return nullptr;
}
if (fprintf(file, "%s\n", "direct") < 0) {
HDF_LOGD("%{public}s: fprintf direct failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return nullptr;
}
return file;
}
void BatteryVibrate::HandlePlayMode(const int time)
{
HDF_LOGI("%{public}s enter", __func__);
FILE *file = nullptr;
file = HandlePlayModePath();
if (file == nullptr) {
return;
}
file = fopen(VIBRATOR_DURATIONMODE_PATH.c_str(), "w");
if (file == nullptr) {
return;
}
if (fprintf(file, "%d\n", time) < 0) {
HDF_LOGD("%{public}s: fprintf time failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return;
}
file = fopen(VIBRATOR_ACTIVATEMODE_PATH.c_str(), "w");
if (file == nullptr) {
return;
}
if (fprintf(file, "%d\n", 1) < 0) {
HDF_LOGD("%{public}s: fprintf 1 failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return;
}
usleep((time + VIBRATE_DELAY_MS) * USEC_TO_MSEC);
file = fopen(VIBRATOR_PLAYMODE_PATH.c_str(), "w");
if (file == nullptr) {
return;
}
if (fprintf(file, "%s\n", "audio") < 0) {
HDF_LOGD("%{public}s: fprintf audio failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return;
}
}
void BatteryVibrate::HandleDurationMode(const int time)
{
HDF_LOGI("%{public}s enter", __func__);
FILE *file = nullptr;
file = fopen(VIBRATOR_DURATIONMODE_PATH.c_str(), "w");
if (file == nullptr) {
return;
}
if (fprintf(file, "%d\n", time) < 0) {
HDF_LOGD("%{public}s: fprintf time failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return;
}
file = fopen(VIBRATOR_ACTIVATEMODE_PATH.c_str(), "w");
if (file == nullptr) {
return;
}
if (fprintf(file, "%d\n", 1) < 0) {
HDF_LOGD("%{public}s: fprintf 1 failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return;
}
usleep((time + VIBRATE_DELAY_MS) * USEC_TO_MSEC);
file = fopen(VIBRATOR_ACTIVATEMODE_PATH.c_str(), "w");
if (file == nullptr) {
return;
}
if (fprintf(file, "%d\n", 0) < 0) {
HDF_LOGD("%{public}s: fprintf 0 failed.", __func__);
}
if (fclose(file) < 0) {
HDF_LOGD("%{public}s: fclose failed.", __func__);
return;
}
}
void BatteryVibrate::HandleVibrate(const int time)
{
HDF_LOGI("%{public}s enter", __func__);
switch (VIBRANTION_MODE) {
case VIBRATION_PLAYMODE: {
HDF_LOGD("%{public}s: vibrate mode sysfs1", __func__);
HandlePlayMode(time);
break;
}
case VIBRATION_DURATIONMODE: {
HDF_LOGD("%{public}s: vibrate mode sysfs2", __func__);
HandleDurationMode(time);
break;
}
default: {
HDF_LOGD("%{public}s: vibrate mode unknown", __func__);
break;
}
}
HDF_LOGI("%{public}s exit", __func__);
return;
}
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
+38
View File
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef BATTERY_VIBRATE_H
#define BATTERY_VIBRATE_H
#include <fstream>
namespace OHOS {
namespace HDI {
namespace Battery {
namespace V1_0 {
class BatteryVibrate {
public:
int VibrateInit();
void HandleVibrate(const int time);
private:
void HandlePlayMode(const int time);
void HandleDurationMode(const int time);
FILE *HandlePlayModePath();
};
} // namespace V1_0
} // namespace Battery
} // namespace HDI
} // namespace OHOS
#endif

Some files were not shown because too many files have changed in this diff Show More