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