2 // Copyright 2015 The Android Open Source Project
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "test_channel_transport"
19 #include "vendor_libs/test_vendor_lib/include/test_channel_transport.h"
21 #include "base/logging.h"
24 #include "osi/include/log.h"
26 #include <sys/socket.h>
27 #include <netinet/in.h>
30 namespace test_vendor_lib {
32 TestChannelTransport::TestChannelTransport(bool enabled, int port)
33 : enabled_(enabled), port_(port) {}
35 bool TestChannelTransport::SetUp() {
38 struct sockaddr_in listen_address, test_channel_address;
39 int sockaddr_in_size = sizeof(struct sockaddr_in);
42 memset(&listen_address, 0, sockaddr_in_size);
43 memset(&test_channel_address, 0, sockaddr_in_size);
45 if ((listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
46 LOG_INFO(LOG_TAG, "Error creating socket for test channel.");
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);
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.");
62 if (listen(listen_fd, 1) < 0) {
63 LOG_INFO(LOG_TAG, "Error listening for test channel.");
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.");
76 fd_.reset(new base::ScopedFD(accept_fd));
80 int TestChannelTransport::GetFd() {
84 bool TestChannelTransport::IsEnabled() {
88 // base::MessageLoopForIO::Watcher overrides:
89 void TestChannelTransport::OnFileCanReadWithoutBlocking(int fd) {
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());
102 if (command_name == "CLOSE_TEST_CHANNEL") {
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()));
120 for (size_t i = 0; i < args.size(); ++i)
121 LOG_INFO(LOG_TAG, "Command argument %d: %s", i, args[i].data());
123 command_handler_(command_name, args);
126 void TestChannelTransport::OnFileCanWriteWithoutBlocking(int fd) {}
128 void TestChannelTransport::RegisterCommandHandler(
129 std::function<void(const std::string&, const std::vector<std::string>&)>
131 command_handler_ = callback;
134 void TestChannelTransport::Disable() {
138 } // namespace test_vendor_lib {