1 /*
2 * Copyright (C) 2019 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 #include "host/frontend/webrtc/adb_handler.h"
18
19 #include <unistd.h>
20
21 #include <android-base/logging.h>
22
23 using namespace android;
24
25 namespace cuttlefish {
26 namespace webrtc_streaming {
27
28 namespace {
29
SetupAdbSocket(const std::string & adb_host_and_port)30 SharedFD SetupAdbSocket(const std::string &adb_host_and_port) {
31 auto colonPos = adb_host_and_port.find(':');
32 if (colonPos == std::string::npos) {
33 return SharedFD();
34 }
35
36 auto host = adb_host_and_port.substr(0, colonPos);
37
38 const char *portString = adb_host_and_port.c_str() + colonPos + 1;
39 char *end;
40 unsigned long port = strtoul(portString, &end, 10);
41
42 if (end == portString || *end != '\0' || port > 65535) {
43 return SharedFD();
44 }
45
46 auto local_client = SharedFD::SocketLocalClient(port, SOCK_STREAM);
47 CHECK(local_client->IsOpen()) << "Failed to connect to adb socket: " << local_client->StrError();
48 return local_client;
49 }
50
51 } // namespace
52
AdbHandler(const std::string & adb_host_and_port,std::function<void (const uint8_t *,size_t)> send_to_client)53 AdbHandler::AdbHandler(
54 const std::string &adb_host_and_port,
55 std::function<void(const uint8_t *, size_t)> send_to_client)
56 : send_to_client_(send_to_client),
57 adb_socket_(SetupAdbSocket(adb_host_and_port)),
58 shutdown_(SharedFD::Event(0,0))
59 {
60 std::thread loop([this]() { ReadLoop(); });
61 read_thread_.swap(loop);
62 }
63
64
~AdbHandler()65 AdbHandler::~AdbHandler() {
66 // Send a message to the looper to shut down.
67 uint64_t v = 1;
68 shutdown_->Write(&v, sizeof(v));
69 // Shut down the socket as well. Not srictly necessary.
70 adb_socket_->Shutdown(SHUT_RDWR);
71 read_thread_.join();
72 }
73
ReadLoop()74 void AdbHandler::ReadLoop() {
75 while (1) {
76 uint8_t buffer[4096];
77
78 read_set_.Set(shutdown_);
79 read_set_.Set(adb_socket_);
80 Select(&read_set_, nullptr, nullptr, nullptr);
81
82 if (read_set_.IsSet(adb_socket_)) {
83 auto read = adb_socket_->Read(buffer, sizeof(buffer));
84 if (read < 0) {
85 LOG(ERROR) << "Error on reading from ADB socket: " << strerror(adb_socket_->GetErrno());
86 break;
87 }
88 if (read) {
89 send_to_client_(buffer, read);
90 }
91 }
92
93 if (read_set_.IsSet(shutdown_)) {
94 LOG(INFO) << "AdbHandler is shutting down.";
95 break;
96 }
97 }
98 }
99
handleMessage(const uint8_t * msg,size_t len)100 void AdbHandler::handleMessage(const uint8_t *msg, size_t len) {
101 size_t sent = 0;
102 while (sent < len) {
103 auto this_sent = adb_socket_->Write(&msg[sent], len - sent);
104 if (this_sent < 0) {
105 LOG(FATAL) << "Error writing to adb socket: " << adb_socket_->StrError();
106 return;
107 }
108 sent += this_sent;
109 }
110 }
111
112 } // namespace webrtc_streaming
113 } // namespace cuttlefish
114