1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "powerhal-libperfmgr"
18 
19 #include <errno.h>
20 #include <unistd.h>
21 
22 #include <cutils/sockets.h>
23 #include <log/log.h>
24 
25 #include "DisplayLowPower.h"
26 
27 namespace aidl {
28 namespace google {
29 namespace hardware {
30 namespace power {
31 namespace impl {
32 namespace pixel {
33 
DisplayLowPower()34 DisplayLowPower::DisplayLowPower() : mFossStatus(false) {}
35 
Init()36 void DisplayLowPower::Init() {
37     ConnectPpsDaemon();
38 }
39 
SetDisplayLowPower(bool enable)40 void DisplayLowPower::SetDisplayLowPower(bool enable) {
41     SetFoss(enable);
42 }
43 
ConnectPpsDaemon()44 void DisplayLowPower::ConnectPpsDaemon() {
45     constexpr const char kPpsDaemon[] = "pps";
46 
47     mPpsSocket.reset(
48             socket_local_client(kPpsDaemon, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
49     if (mPpsSocket.get() < 0) {
50         ALOGW("Connecting to PPS daemon failed (%s)", strerror(errno));
51     }
52 }
53 
SendPpsCommand(const std::string_view cmd)54 int DisplayLowPower::SendPpsCommand(const std::string_view cmd) {
55     if (TEMP_FAILURE_RETRY(write(mPpsSocket.get(), cmd.data(), cmd.size())) < 0) {
56         ALOGE("Failed to send pps command '%s' over socket (%s)", cmd.data(), strerror(errno));
57         return -1;
58     }
59 
60     return 0;
61 }
62 
SetFoss(bool enable)63 void DisplayLowPower::SetFoss(bool enable) {
64     if (mPpsSocket.get() < 0 || mFossStatus == enable) {
65         return;
66     }
67 
68     ALOGI("%s foss", (enable) ? "Enable" : "Disable");
69 
70     std::string_view foss_cmd;
71     if (enable) {
72         foss_cmd = "foss:on";
73     } else {
74         foss_cmd = "foss:off";
75     }
76 
77     if (!SendPpsCommand(foss_cmd)) {
78         mFossStatus = enable;
79     }
80 }
81 
82 }  // namespace pixel
83 }  // namespace impl
84 }  // namespace power
85 }  // namespace hardware
86 }  // namespace google
87 }  // namespace aidl
88