blob: b8084487b865a01733394f29e905e026c0392899 [file] [log] [blame]
Garrick Evans3cbac7c2019-04-18 15:31:31 +09001// Copyright 2019 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Garrick Evans3388a032020-03-24 11:25:55 +09005#ifndef PATCHPANEL_ADB_PROXY_H_
6#define PATCHPANEL_ADB_PROXY_H_
Garrick Evans3cbac7c2019-04-18 15:31:31 +09007
8#include <deque>
9#include <memory>
10
Hidehiko Abede129222019-08-16 00:55:04 +090011#include <base/files/file_descriptor_watcher_posix.h>
Garrick Evans96e03042019-05-28 14:30:52 +090012#include <base/files/scoped_file.h>
Garrick Evans3cbac7c2019-04-18 15:31:31 +090013#include <base/memory/weak_ptr.h>
14#include <brillo/daemons/daemon.h>
Garrick Evans3cbac7c2019-04-18 15:31:31 +090015
Garrick Evans3388a032020-03-24 11:25:55 +090016#include "patchpanel/message_dispatcher.h"
17#include "patchpanel/socket.h"
18#include "patchpanel/socket_forwarder.h"
Garrick Evans3cbac7c2019-04-18 15:31:31 +090019
Garrick Evans3388a032020-03-24 11:25:55 +090020namespace patchpanel {
Garrick Evans3cbac7c2019-04-18 15:31:31 +090021
Jason Jeremy Imanfa8b6d22020-02-20 03:44:21 +000022// ADB gets confused if we listen on 5555 and thinks there is an emulator
23// running, which in turn ends up confusing our integration test libraries
24// because multiple devices show up.
25constexpr uint16_t kAdbProxyTcpListenPort = 5550;
26
Garrick Evans3cbac7c2019-04-18 15:31:31 +090027// Subprocess for proxying ADB traffic.
Hidehiko Abede129222019-08-16 00:55:04 +090028class AdbProxy : public brillo::Daemon {
Garrick Evans3cbac7c2019-04-18 15:31:31 +090029 public:
Garrick Evans96e03042019-05-28 14:30:52 +090030 explicit AdbProxy(base::ScopedFD control_fd);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090031 virtual ~AdbProxy();
32
33 protected:
Garrick Evans3cbac7c2019-04-18 15:31:31 +090034 int OnInit() override;
35
Garrick Evans96e03042019-05-28 14:30:52 +090036 void OnParentProcessExit();
37 void OnGuestMessage(const GuestMessage& msg);
Garrick Evans3cbac7c2019-04-18 15:31:31 +090038
Garrick Evans96e03042019-05-28 14:30:52 +090039 private:
Garrick Evansbdf1f982019-06-07 09:46:49 +090040 void Reset();
Hidehiko Abede129222019-08-16 00:55:04 +090041 void OnFileCanReadWithoutBlocking();
Garrick Evansbdf1f982019-06-07 09:46:49 +090042
Garrick Evans3cbac7c2019-04-18 15:31:31 +090043 // Attempts to establish a connection to ADB at well-known destinations.
44 std::unique_ptr<Socket> Connect() const;
45
Garrick Evans96e03042019-05-28 14:30:52 +090046 MessageDispatcher msg_dispatcher_;
Garrick Evans3cbac7c2019-04-18 15:31:31 +090047 std::unique_ptr<Socket> src_;
48 std::deque<std::unique_ptr<SocketForwarder>> fwd_;
Hidehiko Abede129222019-08-16 00:55:04 +090049 std::unique_ptr<base::FileDescriptorWatcher::Controller> src_watcher_;
Garrick Evans3cbac7c2019-04-18 15:31:31 +090050
Garrick Evansbdf1f982019-06-07 09:46:49 +090051 GuestMessage::GuestType arc_type_;
Garrick Evans1cce71a2019-06-21 10:43:14 +090052 uint32_t arcvm_vsock_cid_;
Garrick Evansbdf1f982019-06-07 09:46:49 +090053
Garrick Evans3cbac7c2019-04-18 15:31:31 +090054 base::WeakPtrFactory<AdbProxy> weak_factory_{this};
55 DISALLOW_COPY_AND_ASSIGN(AdbProxy);
56};
57
Garrick Evans3388a032020-03-24 11:25:55 +090058} // namespace patchpanel
Garrick Evans3cbac7c2019-04-18 15:31:31 +090059
Garrick Evans3388a032020-03-24 11:25:55 +090060#endif // PATCHPANEL_ADB_PROXY_H_