Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 16 | // #undef NDEBUG |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 17 | |
Tomasz Wiszkowski | 93ea805 | 2017-09-21 10:02:45 -0700 | [diff] [blame] | 18 | #include "guest/commands/usbforward/usb_server.h" |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 | #include <strings.h> |
| 23 | #include <cutils/log.h> |
| 24 | #include <libusb/libusb.h> |
| 25 | #include "common/libs/fs/shared_select.h" |
Greg Hartman | 6588a2d | 2017-09-29 16:36:52 -0700 | [diff] [blame] | 26 | #include "common/libs/usbforward/protocol.h" |
Tomasz Wiszkowski | 93ea805 | 2017-09-21 10:02:45 -0700 | [diff] [blame] | 27 | #include "guest/commands/usbforward/transport_request.h" |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 28 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 29 | namespace usb_forward { |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 30 | namespace { |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 31 | // USBServer exports device kExportedVendorID:kExportedProductID to the server. |
| 32 | // We will not support exporting multiple USB devices as there's no practical |
| 33 | // need for this. |
| 34 | constexpr uint16_t kExportedVendorID = 0x18d1; |
| 35 | constexpr uint16_t kExportedProductID = 0x4ee7; |
| 36 | |
| 37 | // Use default BUS and DEVICE IDs so that it's easier to attach over USB/IP. |
| 38 | constexpr uint8_t kDefaultBusID = 1; |
| 39 | constexpr uint8_t kDefaultDevID = 1; |
| 40 | |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 41 | std::shared_ptr<libusb_device_handle> GetDevice() { |
| 42 | std::shared_ptr<libusb_device_handle> res( |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 43 | libusb_open_device_with_vid_pid(nullptr, kExportedVendorID, |
| 44 | kExportedProductID), |
| 45 | [](libusb_device_handle* h) { |
Tomasz Wiszkowski | a48e745 | 2017-09-05 15:21:07 -0700 | [diff] [blame] | 46 | // Apparently, deleter is called even on an uninitialized shared_ptr. |
| 47 | if (h != nullptr) { |
| 48 | libusb_release_interface(h, 0); |
| 49 | libusb_close(h); |
| 50 | } |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 51 | }); |
| 52 | |
| 53 | if (res) libusb_claim_interface(res.get(), 0); |
| 54 | |
| 55 | return res; |
| 56 | } |
| 57 | |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 58 | } // anonymous namespace |
| 59 | |
| 60 | bool USBServer::GetDeviceInfo( |
| 61 | DeviceInfo* info, std::vector<InterfaceInfo>* ifaces) { |
| 62 | if (!handle_) return false; |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 63 | |
| 64 | // This function does not modify the reference count of the returned device, |
| 65 | // so do not feel compelled to unreference it when you are done. |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 66 | libusb_device* dev = libusb_get_device(handle_.get()); |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 67 | |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 68 | libusb_device_descriptor desc; |
| 69 | libusb_config_descriptor* conf; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 70 | memset(info, 0, sizeof(*info)); |
| 71 | |
| 72 | int res = libusb_get_device_descriptor(dev, &desc); |
| 73 | if (res < 0) { |
| 74 | // This shouldn't really happen. |
| 75 | ALOGE("libusb_get_device_descriptor failed %d", res); |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 76 | return false; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | res = libusb_get_active_config_descriptor(dev, &conf); |
| 80 | if (res < 0) { |
| 81 | // This shouldn't really happen. |
| 82 | ALOGE("libusb_get_active_config_descriptor failed %d", res); |
| 83 | libusb_free_config_descriptor(conf); |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 84 | return false; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | info->vendor_id = desc.idVendor; |
| 88 | info->product_id = desc.idProduct; |
| 89 | info->dev_version = desc.bcdDevice; |
| 90 | info->dev_class = desc.bDeviceClass; |
| 91 | info->dev_subclass = desc.bDeviceSubClass; |
| 92 | info->dev_protocol = desc.bDeviceProtocol; |
| 93 | info->speed = libusb_get_device_speed(dev); |
| 94 | info->num_configurations = desc.bNumConfigurations; |
| 95 | info->num_interfaces = conf->bNumInterfaces; |
| 96 | info->cur_configuration = conf->bConfigurationValue; |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 97 | info->bus_id = kDefaultBusID; |
| 98 | info->dev_id = kDefaultDevID; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 99 | |
| 100 | if (ifaces != nullptr) { |
| 101 | for (int ifidx = 0; ifidx < conf->bNumInterfaces; ++ifidx) { |
| 102 | const libusb_interface& iface = conf->interface[ifidx]; |
| 103 | for (int altidx = 0; altidx < iface.num_altsetting; ++altidx) { |
| 104 | const libusb_interface_descriptor& alt = iface.altsetting[altidx]; |
| 105 | ifaces->push_back(InterfaceInfo{alt.bInterfaceClass, |
| 106 | alt.bInterfaceSubClass, |
| 107 | alt.bInterfaceProtocol, 0}); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | libusb_free_config_descriptor(conf); |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 112 | return true; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 113 | } |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 114 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 115 | USBServer::USBServer(const cvd::SharedFD& fd) |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 116 | : fd_{fd}, |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 117 | device_event_fd_{cvd::SharedFD::Event(0, 0)}, |
| 118 | thread_event_fd_{cvd::SharedFD::Event(0, 0)} {} |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 119 | |
| 120 | void USBServer::HandleDeviceList(uint32_t tag) { |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 121 | // Iterate all devices and send structure for every found device. |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 122 | // Write header: number of devices. |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 123 | DeviceInfo info; |
| 124 | std::vector<InterfaceInfo> ifaces; |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 125 | bool found = GetDeviceInfo(&info, &ifaces); |
| 126 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 127 | cvd::LockGuard<cvd::Mutex> lock(write_mutex_); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 128 | ResponseHeader rsp{StatusSuccess, tag}; |
| 129 | fd_->Write(&rsp, sizeof(rsp)); |
| 130 | if (found) { |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 131 | uint32_t cnt = 1; |
| 132 | fd_->Write(&cnt, sizeof(cnt)); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 133 | fd_->Write(&info, sizeof(info)); |
| 134 | fd_->Write(ifaces.data(), ifaces.size() * sizeof(InterfaceInfo)); |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 135 | } else { |
| 136 | // No devices. |
| 137 | uint32_t cnt = 0; |
| 138 | fd_->Write(&cnt, sizeof(cnt)); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 142 | void USBServer::HandleAttach(uint32_t tag) { |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 143 | // We read the request, but it no longer plays any significant role here. |
| 144 | AttachRequest req; |
| 145 | if (fd_->Read(&req, sizeof(req)) != sizeof(req)) return; |
| 146 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 147 | cvd::LockGuard<cvd::Mutex> lock(write_mutex_); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 148 | ResponseHeader rsp{handle_ ? StatusSuccess : StatusFailure, tag}; |
| 149 | fd_->Write(&rsp, sizeof(rsp)); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Tomasz Wiszkowski | cdf5748 | 2017-08-14 12:47:25 -0700 | [diff] [blame] | 152 | void USBServer::HandleHeartbeat(uint32_t tag) { |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 153 | cvd::LockGuard<cvd::Mutex> lock(write_mutex_); |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 154 | ResponseHeader rsp{handle_ ? StatusSuccess : StatusFailure, tag}; |
Tomasz Wiszkowski | cdf5748 | 2017-08-14 12:47:25 -0700 | [diff] [blame] | 155 | fd_->Write(&rsp, sizeof(rsp)); |
| 156 | } |
| 157 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 158 | void USBServer::HandleControlTransfer(uint32_t tag) { |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 159 | ControlTransfer req; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 160 | // If disconnected prematurely, don't send response. |
| 161 | if (fd_->Read(&req, sizeof(req)) != sizeof(req)) return; |
| 162 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 163 | // Technically speaking this isn't endpoint, but names, masks, values and |
| 164 | // meaning here is exactly same. |
| 165 | bool is_data_in = |
| 166 | ((req.type & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 167 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 168 | std::unique_ptr<TransportRequest> treq(new TransportRequest( |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 169 | handle_, |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 170 | [this, is_data_in, tag](bool is_success, const uint8_t* data, |
| 171 | int32_t length) { |
| 172 | OnTransferComplete(tag, is_data_in, is_success, data, length); |
| 173 | }, |
| 174 | req)); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 175 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 176 | if (!is_data_in && req.length) { |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 177 | // If disconnected prematurely, don't send response. |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 178 | int32_t got = 0; |
| 179 | while (got < req.length) { |
| 180 | auto read = fd_->Read(&treq->Buffer()[got], req.length - got); |
| 181 | if (fd_->GetErrno() != 0) { |
| 182 | ALOGE("Failed to read from client: %s", fd_->StrError()); |
| 183 | return; |
| 184 | } else if (read == 0) { |
| 185 | ALOGE("Failed to read from client: short read"); |
| 186 | return; |
| 187 | } |
| 188 | got += read; |
| 189 | } |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 192 | // At this point we store transport request internally until it completes. |
| 193 | TransportRequest* treq_ptr = treq.get(); |
| 194 | { |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 195 | cvd::LockGuard<cvd::Mutex> lock(requests_mutex_); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 196 | requests_in_flight_[tag] = std::move(treq); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 199 | if (!treq_ptr->Submit()) { |
| 200 | OnTransferComplete(tag, is_data_in, false, nullptr, 0); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 204 | void USBServer::HandleDataTransfer(uint32_t tag) { |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 205 | DataTransfer req; |
| 206 | // If disconnected prematurely, don't send response. |
| 207 | if (fd_->Read(&req, sizeof(req)) != sizeof(req)) return; |
| 208 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 209 | bool is_data_in = !req.is_host_to_device; |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 210 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 211 | std::unique_ptr<TransportRequest> treq(new TransportRequest( |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 212 | handle_, |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 213 | [this, is_data_in, tag](bool is_success, const uint8_t* data, |
| 214 | int32_t length) { |
| 215 | OnTransferComplete(tag, is_data_in, is_success, data, length); |
| 216 | }, |
| 217 | req)); |
| 218 | |
| 219 | if (!is_data_in && req.length) { |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 220 | // If disconnected prematurely, don't send response. |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 221 | int32_t got = 0; |
| 222 | while (got < req.length) { |
| 223 | auto read = fd_->Read(&treq->Buffer()[got], req.length - got); |
| 224 | if (fd_->GetErrno() != 0) { |
| 225 | ALOGE("Failed to read from client: %s", fd_->StrError()); |
| 226 | return; |
| 227 | } else if (read == 0) { |
| 228 | ALOGE("Failed to read from client: short read"); |
| 229 | return; |
| 230 | } |
| 231 | got += read; |
| 232 | } |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 235 | // At this point we store transport request internally until it completes. |
| 236 | TransportRequest* treq_ptr = treq.get(); |
| 237 | { |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 238 | cvd::LockGuard<cvd::Mutex> lock(requests_mutex_); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 239 | requests_in_flight_[tag] = std::move(treq); |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 242 | if (!treq_ptr->Submit()) { |
| 243 | OnTransferComplete(tag, is_data_in, false, nullptr, 0); |
| 244 | } |
| 245 | } |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 246 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 247 | void USBServer::OnTransferComplete(uint32_t tag, bool is_data_in, |
| 248 | bool is_success, const uint8_t* buffer, |
| 249 | int32_t actual_length) { |
| 250 | ResponseHeader rsp{is_success ? StatusSuccess : StatusFailure, tag}; |
| 251 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 252 | cvd::LockGuard<cvd::Mutex> lock(write_mutex_); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 253 | fd_->Write(&rsp, sizeof(rsp)); |
| 254 | if (is_success && is_data_in) { |
| 255 | fd_->Write(&actual_length, sizeof(actual_length)); |
| 256 | if (actual_length > 0) { |
| 257 | // NOTE: don't use buffer_ here directly, as libusb uses first few bytes |
| 258 | // to store control data there. |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 259 | int32_t sent = 0; |
| 260 | while (sent < actual_length) { |
| 261 | int packet_size = fd_->Write(&buffer[sent], actual_length - sent); |
| 262 | sent += packet_size; |
Greg Hartman | 41e939e | 2017-08-17 12:43:00 -0700 | [diff] [blame] | 263 | ALOGV("Sending response, %d / %d bytes sent", sent, actual_length); |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 264 | if (fd_->GetErrno() != 0) { |
| 265 | ALOGE("Send failed: %s", fd_->StrError()); |
| 266 | return; |
| 267 | } |
| 268 | } |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 271 | |
| 272 | { |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 273 | cvd::LockGuard<cvd::Mutex> lock(requests_mutex_); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 274 | requests_in_flight_.erase(tag); |
| 275 | } |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 278 | int USBServer::HandleDeviceEvent(libusb_context*, libusb_device*, |
| 279 | libusb_hotplug_event event, void* self_raw) { |
| 280 | auto self = reinterpret_cast<USBServer*>(self_raw); |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 281 | int64_t dummy = 1; |
| 282 | self->device_event_fd_->Write(&dummy, sizeof(dummy)); |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 286 | void* USBServer::ProcessLibUSBRequests(void* self_raw) { |
| 287 | USBServer* self = reinterpret_cast<USBServer*>(self_raw); |
| 288 | ALOGI("Starting hotplug thread."); |
Tomasz Wiszkowski | 482292e | 2017-08-04 16:33:00 -0700 | [diff] [blame] | 289 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 290 | cvd::SharedFDSet rset; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 291 | while (true) { |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 292 | // Do not wait if there's no event. |
| 293 | timeval select_timeout{0, 0}; |
| 294 | rset.Zero(); |
| 295 | rset.Set(self->thread_event_fd_); |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 296 | int ret = cvd::Select(&rset, nullptr, nullptr, &select_timeout); |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 297 | if (ret > 0) break; |
| 298 | |
| 299 | timeval libusb_timeout{1, 0}; |
| 300 | libusb_handle_events_timeout_completed(nullptr, &libusb_timeout, nullptr); |
| 301 | } |
| 302 | |
| 303 | int64_t dummy; |
| 304 | self->thread_event_fd_->Read(&dummy, sizeof(dummy)); |
| 305 | ALOGI("Shutting down hotplug thread."); |
| 306 | return nullptr; |
| 307 | } |
| 308 | |
| 309 | void USBServer::InitLibUSB() { |
| 310 | if (libusb_init(nullptr) != 0) return; |
| 311 | libusb_hotplug_register_callback( |
| 312 | nullptr, |
| 313 | libusb_hotplug_event(LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT), |
| 314 | libusb_hotplug_flag(0), kExportedVendorID, kExportedProductID, |
| 315 | LIBUSB_HOTPLUG_MATCH_ANY, &USBServer::HandleDeviceEvent, this, |
| 316 | &hotplug_handle_); |
| 317 | handle_ = GetDevice(); |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 318 | libusb_thread_.reset(new cvd::ScopedThread(&ProcessLibUSBRequests, this)); |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void USBServer::ExitLibUSB() { |
| 322 | if (!libusb_thread_) return; |
| 323 | libusb_hotplug_deregister_callback(nullptr, hotplug_handle_); |
| 324 | int64_t dummy = 1; |
| 325 | thread_event_fd_->Write(&dummy, sizeof(dummy)); |
| 326 | libusb_thread_.reset(); |
| 327 | handle_.reset(); |
| 328 | libusb_exit(nullptr); |
| 329 | } |
| 330 | |
| 331 | void USBServer::Serve() { |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 332 | cvd::SharedFDSet rset; |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 333 | while (true) { |
| 334 | timeval retry_timeout{1, 0}; |
| 335 | timeval* select_timeout = nullptr; |
| 336 | if (!handle_) { |
| 337 | select_timeout = &retry_timeout; |
| 338 | } |
| 339 | |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 340 | rset.Zero(); |
| 341 | rset.Set(fd_); |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 342 | rset.Set(device_event_fd_); |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame^] | 343 | int ret = cvd::Select(&rset, nullptr, nullptr, select_timeout); |
Tomasz Wiszkowski | 8a40b25 | 2017-09-14 14:46:39 -0700 | [diff] [blame] | 344 | |
| 345 | // device_event_fd_ is reset each time libusb notices device has re-appeared |
| 346 | // or is gone. In both cases, the existing handle is no longer valid. |
| 347 | if (rset.IsSet(device_event_fd_)) { |
| 348 | int64_t dummy; |
| 349 | device_event_fd_->Read(&dummy, sizeof(dummy)); |
| 350 | handle_.reset(); |
| 351 | } |
| 352 | |
| 353 | if (!handle_) { |
| 354 | ExitLibUSB(); |
| 355 | InitLibUSB(); |
| 356 | if (handle_) { |
| 357 | ALOGI("Device present."); |
| 358 | } |
| 359 | } |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 360 | |
| 361 | if (ret < 0) continue; |
| 362 | |
| 363 | if (rset.IsSet(fd_)) { |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 364 | RequestHeader req; |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 365 | if (fd_->Read(&req, sizeof(req)) != sizeof(req)) { |
Tomasz Wiszkowski | 8552194 | 2017-07-31 12:42:41 -0700 | [diff] [blame] | 366 | // There's nobody on the other side. |
| 367 | sleep(3); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 368 | continue; |
| 369 | } |
| 370 | |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 371 | switch (req.command) { |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 372 | case CmdDeviceList: |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 373 | ALOGV("Processing DeviceList command, tag=%d", req.tag); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 374 | HandleDeviceList(req.tag); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 375 | break; |
| 376 | |
| 377 | case CmdAttach: |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 378 | ALOGV("Processing Attach command, tag=%d", req.tag); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 379 | HandleAttach(req.tag); |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 380 | break; |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 381 | |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 382 | case CmdControlTransfer: |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 383 | ALOGV("Processing ControlTransfer command, tag=%d", req.tag); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 384 | HandleControlTransfer(req.tag); |
Tomasz Wiszkowski | 2553010 | 2017-07-28 09:33:07 -0700 | [diff] [blame] | 385 | break; |
| 386 | |
| 387 | case CmdDataTransfer: |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 388 | ALOGV("Processing DataTransfer command, tag=%d", req.tag); |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 389 | HandleDataTransfer(req.tag); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 390 | break; |
| 391 | |
Tomasz Wiszkowski | cdf5748 | 2017-08-14 12:47:25 -0700 | [diff] [blame] | 392 | case CmdHeartbeat: |
| 393 | ALOGV("Processing Heartbeat command, tag=%d", req.tag); |
| 394 | HandleHeartbeat(req.tag); |
| 395 | break; |
| 396 | |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 397 | default: |
Tomasz Wiszkowski | 9915c6c | 2017-08-03 14:20:43 -0700 | [diff] [blame] | 398 | ALOGE("Discarding unknown command %08x, tag=%d", req.command, |
| 399 | req.tag); |
Tomasz Wiszkowski | ae4ff63 | 2017-07-20 14:56:16 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | } |
| 403 | } |
Tomasz Wiszkowski | 9cbe555 | 2017-08-01 16:11:05 -0700 | [diff] [blame] | 404 | |
Tomasz Wiszkowski | cdf5748 | 2017-08-14 12:47:25 -0700 | [diff] [blame] | 405 | } // namespace usb_forward |