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 #include <android-base/logging.h>
17 #include <android-base/strings.h>
18 #include <sys/socket.h>
19 
20 #include "common/libs/fs/shared_buf.h"
21 #include "common/libs/fs/shared_fd.h"
22 #include "host/libs/config/cuttlefish_config.h"
23 
24 // Messages are always 128 bytes.
25 #define MESSAGE_SIZE 128
26 
27 using cuttlefish::SharedFD;
28 
main(int argc,char ** argv)29 int main(int argc, char** argv) {
30   if (argc <= 1) {
31     return 1;
32   }
33 
34   // Connect to WebRTC
35   int fd = std::atoi(argv[1]);
36   LOG(INFO) << "Connecting to WebRTC server...";
37   SharedFD webrtc_socket = SharedFD::Dup(fd);
38   close(fd);
39   if (webrtc_socket->IsOpen()) {
40     LOG(INFO) << "Connected";
41   } else {
42     LOG(ERROR) << "Could not connect, exiting...";
43     return 1;
44   }
45 
46   // Track state for our two commands.
47   bool statusbar_expanded = false;
48   bool dnd_on = false;
49 
50   char buf[MESSAGE_SIZE];
51   while (1) {
52     // Read the command message from the socket.
53     if (!webrtc_socket->IsOpen()) {
54       LOG(WARNING) << "WebRTC was closed.";
55       break;
56     }
57     if (cuttlefish::ReadExact(webrtc_socket, buf, MESSAGE_SIZE) !=
58         MESSAGE_SIZE) {
59       LOG(WARNING) << "Failed to read the correct number of bytes.";
60       break;
61     }
62     auto split = android::base::Split(buf, ":");
63     std::string command = split[0];
64     std::string state = split[1];
65 
66     // Ignore button-release events, when state != down.
67     if (state != "down") {
68       continue;
69     }
70 
71     // Demonstrate two commands. For demonstration purposes these two
72     // commands use adb shell, but commands can execute any action you choose.
73     std::string adb_shell_command =
74         cuttlefish::HostBinaryPath("adb");
75     if (command == "settings") {
76       adb_shell_command += " shell cmd statusbar ";
77       adb_shell_command += statusbar_expanded ? "collapse" : "expand-settings";
78       statusbar_expanded = !statusbar_expanded;
79     } else if (command == "alert") {
80       adb_shell_command += " shell cmd notification set_dnd ";
81       adb_shell_command += dnd_on ? "off" : "on";
82       dnd_on = !dnd_on;
83     } else {
84       LOG(WARNING) << "Unexpected command: " << buf;
85     }
86 
87     if (!adb_shell_command.empty()) {
88       if (system(adb_shell_command.c_str()) != 0) {
89         LOG(ERROR) << "Failed to run command: " << adb_shell_command;
90       }
91     }
92   }
93 }
94