1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "healthd_mode_charger_hidl.h"
18 
19 #include <android/hardware/health/2.0/types.h>
20 #include <charger.sysprop.h>
21 #include <cutils/klog.h>
22 
23 #include "charger_utils.h"
24 
25 using android::hardware::health::GetHealthServiceOrDefault;
26 using android::hardware::health::V2_0::Result;
27 
28 namespace android {
29 
ChargerHidl(const sp<android::hardware::health::V2_1::IHealth> & service)30 ChargerHidl::ChargerHidl(const sp<android::hardware::health::V2_1::IHealth>& service)
31     : HalHealthLoop("charger", service), charger_(std::make_unique<Charger>(this)) {}
32 
OnHealthInfoChanged(const HealthInfo_2_1 & health_info)33 void ChargerHidl::OnHealthInfoChanged(const HealthInfo_2_1& health_info) {
34     set_charger_online(health_info);
35 
36     charger_->OnHealthInfoChanged(ChargerHealthInfo{
37             .battery_level = health_info.legacy.legacy.batteryLevel,
38             .battery_status = static_cast<::aidl::android::hardware::health::BatteryStatus>(
39                     health_info.legacy.legacy.batteryStatus),
40     });
41 
42     AdjustWakealarmPeriods(charger_online());
43 }
44 
ChargerShouldKeepScreenOn()45 std::optional<bool> ChargerHidl::ChargerShouldKeepScreenOn() {
46     std::optional<bool> out_screen_on;
47     service()->shouldKeepScreenOn([&](Result res, bool screen_on) {
48         if (res == Result::SUCCESS) {
49             *out_screen_on = screen_on;
50         }
51     });
52     return out_screen_on;
53 }
54 
ChargerEnableSuspend()55 bool ChargerHidl::ChargerEnableSuspend() {
56     return android::sysprop::ChargerProperties::enable_suspend().value_or(false);
57 }
58 
59 }  // namespace android
60 
healthd_charger_main(int argc,char ** argv)61 int healthd_charger_main(int argc, char** argv) {
62     int ch;
63 
64     while ((ch = getopt(argc, argv, "cr")) != -1) {
65         switch (ch) {
66             case 'c':
67                 // -c is now a noop
68                 break;
69             case 'r':
70                 // -r is now a noop
71                 break;
72             case '?':
73             default:
74                 KLOG_ERROR("charger", "Unrecognized charger option: %c\n", optopt);
75                 exit(1);
76         }
77     }
78 
79     android::ChargerHidl charger(GetHealthServiceOrDefault());
80     return charger.StartLoop();
81 }
82