1 // 2 // Copyright 2015 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 "test_channel_transport" 18 19 #include "vendor_libs/test_vendor_lib/include/test_channel_transport.h" 20 21 #include "base/logging.h" 22 23 extern "C" { 24 #include "osi/include/log.h" 25 26 #include <sys/socket.h> 27 #include <netinet/in.h> 28 } // extern "C" 29 30 namespace test_vendor_lib { 31 32 TestChannelTransport::TestChannelTransport(bool enabled, int port) 33 : enabled_(enabled), port_(port) {} 34 35 bool TestChannelTransport::SetUp() { 36 CHECK(enabled_); 37 38 struct sockaddr_in listen_address, test_channel_address; 39 int sockaddr_in_size = sizeof(struct sockaddr_in); 40 int listen_fd = -1; 41 int accept_fd = -1; 42 memset(&listen_address, 0, sockaddr_in_size); 43 memset(&test_channel_address, 0, sockaddr_in_size); 44 45 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { 46 LOG_INFO(LOG_TAG, "Error creating socket for test channel."); 47 return false; 48 } 49 50 LOG_INFO(LOG_TAG, "port: %d", port_); 51 listen_address.sin_family = AF_INET; 52 listen_address.sin_port = htons(port_); 53 listen_address.sin_addr.s_addr = htonl(INADDR_ANY); 54 55 if (bind(listen_fd, reinterpret_cast<sockaddr*>(&listen_address), 56 sockaddr_in_size) < 0) { 57 LOG_INFO(LOG_TAG, "Error binding test channel listener socket to address."); 58 close(listen_fd); 59 return false; 60 } 61 62 if (listen(listen_fd, 1) < 0) { 63 LOG_INFO(LOG_TAG, "Error listening for test channel."); 64 close(listen_fd); 65 return false; 66 } 67 68 if ((accept_fd = 69 accept(listen_fd, reinterpret_cast<sockaddr*>(&test_channel_address), 70 &sockaddr_in_size)) < 0) { 71 LOG_INFO(LOG_TAG, "Error accepting test channel connection."); 72 close(listen_fd); 73 return false; 74 } 75 76 fd_.reset(new base::ScopedFD(accept_fd)); 77 return GetFd() >= 0; 78 } 79 80 int TestChannelTransport::GetFd() { 81 return fd_->get(); 82 } 83 84 bool TestChannelTransport::IsEnabled() { 85 return enabled_; 86 } 87 88 // base::MessageLoopForIO::Watcher overrides: 89 void TestChannelTransport::OnFileCanReadWithoutBlocking(int fd) { 90 CHECK(fd == GetFd()); 91 92 LOG_INFO(LOG_TAG, "Event ready in TestChannelTransport on fd: %d", fd); 93 uint8_t command_name_size = 0; 94 read(fd, &command_name_size, 1); 95 std::vector<uint8_t> command_name_raw; 96 command_name_raw.resize(command_name_size); 97 read(fd, &command_name_raw[0], command_name_size); 98 std::string command_name(command_name_raw.begin(), command_name_raw.end()); 99 LOG_INFO(LOG_TAG, "Received command from test channel: %s", 100 command_name.data()); 101 102 if (command_name == "CLOSE_TEST_CHANNEL") { 103 fd_.reset(nullptr); 104 return; 105 } 106 107 uint8_t num_args = 0; 108 read(fd, &num_args, 1); 109 LOG_INFO(LOG_TAG, "num_args: %d", num_args); 110 std::vector<std::string> args; 111 for (uint8_t i = 0; i < num_args; ++i) { 112 uint8_t arg_size = 0; 113 read(fd, &arg_size, 1); 114 std::vector<uint8_t> arg; 115 arg.resize(arg_size); 116 read(fd, &arg[0], arg_size); 117 args.push_back(std::string(arg.begin(), arg.end())); 118 } 119 120 for (size_t i = 0; i < args.size(); ++i) 121 LOG_INFO(LOG_TAG, "Command argument %zu: %s", i, args[i].data()); 122 123 command_handler_(command_name, args); 124 } 125 126 void TestChannelTransport::OnFileCanWriteWithoutBlocking(int fd) {} 127 128 void TestChannelTransport::RegisterCommandHandler( 129 std::function<void(const std::string&, const std::vector<std::string>&)> 130 callback) { 131 command_handler_ = callback; 132 } 133 134 void TestChannelTransport::Disable() { 135 enabled_ = false; 136 } 137 138 } // namespace test_vendor_lib { 139