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 "[email protected]"
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 
DisplayLowPower()27 DisplayLowPower::DisplayLowPower() : mFossStatus(false) {}
28 
Init()29 void DisplayLowPower::Init() {
30     ConnectPpsDaemon();
31 }
32 
SetDisplayLowPower(bool enable)33 void DisplayLowPower::SetDisplayLowPower(bool enable) {
34     SetFoss(enable);
35 }
36 
ConnectPpsDaemon()37 void DisplayLowPower::ConnectPpsDaemon() {
38     constexpr const char kPpsDaemon[] = "pps";
39 
40     mPpsSocket.reset(
41             socket_local_client(kPpsDaemon, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM));
42     if (mPpsSocket.get() < 0) {
43         ALOGW("Connecting to PPS daemon failed (%s)", strerror(errno));
44     }
45 }
46 
SendPpsCommand(const std::string_view cmd)47 int DisplayLowPower::SendPpsCommand(const std::string_view cmd) {
48     if (TEMP_FAILURE_RETRY(write(mPpsSocket.get(), cmd.data(), cmd.size())) < 0) {
49         ALOGE("Failed to send pps command '%s' over socket (%s)", cmd.data(), strerror(errno));
50         return -1;
51     }
52 
53     return 0;
54 }
55 
SetFoss(bool enable)56 void DisplayLowPower::SetFoss(bool enable) {
57     if (mPpsSocket.get() < 0 || mFossStatus == enable) {
58         return;
59     }
60 
61     ALOGI("%s foss", (enable) ? "Enable" : "Disable");
62 
63     std::string_view foss_cmd;
64     if (enable) {
65         foss_cmd = "foss:on";
66     } else {
67         foss_cmd = "foss:off";
68     }
69 
70     if (!SendPpsCommand(foss_cmd)) {
71         mFossStatus = enable;
72     }
73 }
74