blob: 942e16ed3d3e7063e356c14fb5124d5920f31272 [file] [log] [blame]
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -07001/*
2 * Copyright (C) 2017 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#pragma once
17
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070018#include <map>
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070019#include <memory>
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070020#include <string>
21#include <libusb/libusb.h>
22
23#include "common/libs/fs/shared_fd.h"
Greg Hartmana4ff2482017-10-03 16:35:00 -070024#include "common/libs/threads/cuttlefish_thread.h"
Tomasz Wiszkowski93ea8052017-09-21 10:02:45 -070025#include "guest/commands/usbforward/transport_request.h"
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070026
27namespace usb_forward {
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070028
29// USBServer exposes access to USB devices over pipe (virtio channel etc).
30// Usage:
31//
32// avd::SharedFD pipe = avd::SharedFD::Open(pipe_path, O_RDWR);
33// USBServer server(pipe);
34// CHECK(server.Init());
35// server.Serve();
36class USBServer final {
37 public:
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070038 USBServer(const avd::SharedFD& fd);
Tomasz Wiszkowski85521942017-07-31 12:42:41 -070039 ~USBServer() = default;
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070040
41 // Serve incoming USB requests.
42 void Serve();
43
44 private:
Tomasz Wiszkowski482292e2017-08-04 16:33:00 -070045 // HandleDeviceEvent opens and closes Android Gadget device, whenever it
46 // appears / disappears.
47 static int HandleDeviceEvent(libusb_context*, libusb_device*,
48 libusb_hotplug_event event, void* self_raw);
49
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070050 // Handle CmdDeviceList request.
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070051 void HandleDeviceList(uint32_t tag);
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070052
53 // Handle CmdAttach request.
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070054 void HandleAttach(uint32_t tag);
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070055
Tomasz Wiszkowski25530102017-07-28 09:33:07 -070056 // Handle CmdControlTransfer request.
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070057 void HandleControlTransfer(uint32_t tag);
Tomasz Wiszkowski25530102017-07-28 09:33:07 -070058
59 // Handle CmdDataTransfer request.
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070060 void HandleDataTransfer(uint32_t tag);
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070061
Tomasz Wiszkowskicdf57482017-08-14 12:47:25 -070062 // Handle CmdHeartbeat request.
63 void HandleHeartbeat(uint32_t tag);
64
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070065 // OnAsyncDataTransferComplete handles end of asynchronous data transfer cycle
66 // and sends response back to caller.
67 void OnTransferComplete(uint32_t tag, bool is_data_in, bool is_success,
68 const uint8_t* buffer, int32_t actual_length);
69
Tomasz Wiszkowski8a40b252017-09-14 14:46:39 -070070 // Initialize, Configure and start libusb.
71 void InitLibUSB();
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070072
Tomasz Wiszkowski8a40b252017-09-14 14:46:39 -070073 // Stop, Deconfigure and Clean up libusb.
74 void ExitLibUSB();
75
76 // Extract device info, if device is available.
77 bool GetDeviceInfo(DeviceInfo* info, std::vector<InterfaceInfo>* ifaces);
78
79 // Handle asynchronous libusb events.
80 static void* ProcessLibUSBRequests(void* self_ptr);
81
82 std::shared_ptr<libusb_device_handle> handle_;
83 libusb_hotplug_callback_handle hotplug_handle_;
84
85 std::unique_ptr<avd::ScopedThread> libusb_thread_;
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070086 avd::Mutex write_mutex_;
87 avd::SharedFD fd_;
Tomasz Wiszkowski8a40b252017-09-14 14:46:39 -070088 avd::SharedFD device_event_fd_;
89 avd::SharedFD thread_event_fd_;
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070090
91 avd::Mutex requests_mutex_;
92 std::map<uint32_t, std::unique_ptr<TransportRequest>> requests_in_flight_;
93
Tomasz Wiszkowskiae4ff632017-07-20 14:56:16 -070094 USBServer(const USBServer& other) = delete;
95 USBServer& operator=(const USBServer& other) = delete;
96};
Tomasz Wiszkowski9cbe5552017-08-01 16:11:05 -070097
Tomasz Wiszkowskicdf57482017-08-14 12:47:25 -070098} // namespace usb_forward