1 /*
2  * Copyright (C) 2018 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_NIDEBUG 0
18 #define LOG_TAG "android.hardware.power@1.3-service.pixel-libperfmgr"
19 
20 #include <dlfcn.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <inttypes.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 
30 #include <cutils/sockets.h>
31 #include <log/log.h>
32 
33 #include "display-helper.h"
34 
35 #define DAEMON_SOCKET "pps"
36 
37 static int daemon_socket = -1;
38 
connectPPDaemon()39 static int connectPPDaemon() {
40     // Setup socket connection, if not already done.
41     if (daemon_socket < 0)
42         daemon_socket =
43                 socket_local_client(DAEMON_SOCKET, ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
44 
45     if (daemon_socket < 0) {
46         ALOGE("Connecting to socket failed: %s", strerror(errno));
47         return -1;
48     }
49     return 0;
50 }
51 
ppdComm(const char * cmd)52 static int ppdComm(const char *cmd) {
53     int ret = -1;
54 
55     ret = connectPPDaemon();
56     if (ret < 0)
57         return ret;
58 
59     ret = write(daemon_socket, cmd, strlen(cmd));
60     if (ret < 0) {
61         ALOGE("Failed to send data over socket, %s", strerror(errno));
62         return ret;
63     }
64     return 0;
65 }
66 
set_display_lpm(int enable)67 void set_display_lpm(int enable) {
68     ALOGI("set_display_lpm state: %d", enable);
69     if (enable) {
70         ppdComm("foss:on");
71     } else {
72         ppdComm("foss:off");
73     }
74 }
75