Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -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 | */ |
| 16 | #include <glog/logging.h> |
| 17 | |
Jorge E. Moreira | d843066 | 2018-07-22 23:52:33 -0700 | [diff] [blame] | 18 | #include "host/commands/virtual_usb_manager/vadb/usb_cmd_device_list.h" |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 19 | |
| 20 | namespace vadb { |
Greg Hartman | 7d5e0bf | 2017-12-19 23:48:34 -0800 | [diff] [blame] | 21 | bool USBCmdDeviceList::OnRequest(const cvd::SharedFD& /*data*/) { |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 22 | LOG(INFO) << "Requesting device list from Cuttlefish..."; |
| 23 | // No action required. |
| 24 | return true; |
| 25 | } |
| 26 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 27 | bool USBCmdDeviceList::OnResponse(bool is_success, const cvd::SharedFD& fd) { |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 28 | // This should never happen. If this command fails, something is very wrong. |
| 29 | if (!is_success) return false; |
| 30 | |
| 31 | int32_t count; |
| 32 | if (fd->Read(&count, sizeof(count)) != sizeof(count)) { |
| 33 | LOG(ERROR) << "Short read: " << fd->StrError(); |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | LOG(INFO) << "Device list completed with " << count << " devices."; |
| 38 | |
| 39 | while (count-- > 0) { |
| 40 | usb_forward::DeviceInfo dev; |
| 41 | std::vector<usb_forward::InterfaceInfo> ifaces; |
| 42 | |
| 43 | if (fd->Read(&dev, sizeof(dev)) != sizeof(dev)) { |
| 44 | LOG(ERROR) << "Short read: " << fd->StrError(); |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | ifaces.resize(dev.num_interfaces); |
Greg Hartman | 7d5e0bf | 2017-12-19 23:48:34 -0800 | [diff] [blame] | 49 | if (static_cast<size_t>( |
| 50 | fd->Read(ifaces.data(), |
| 51 | ifaces.size() * sizeof(usb_forward::InterfaceInfo))) != |
Greg Hartman | 92045f6 | 2017-10-31 17:12:24 -0700 | [diff] [blame] | 52 | ifaces.size() * sizeof(usb_forward::InterfaceInfo)) { |
| 53 | LOG(ERROR) << "Short read: " << fd->StrError(); |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | LOG(INFO) << "Found remote device 0x" << std::hex << dev.vendor_id << ":" |
| 58 | << dev.product_id; |
| 59 | |
| 60 | on_device_discovered_(dev, ifaces); |
| 61 | } |
| 62 | |
| 63 | return true; |
| 64 | } |
| 65 | } // namespace vadb |