blob: b74efdd0a1ccfd65076b06fdcc473ad5efaf3280 [file] [log] [blame]
Greg Hartman92045f62017-10-31 17:12:24 -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#include <glog/logging.h>
17
Jorge E. Moreirad8430662018-07-22 23:52:33 -070018#include "host/commands/virtual_usb_manager/vadb/usb_cmd_device_list.h"
Greg Hartman92045f62017-10-31 17:12:24 -070019
20namespace vadb {
Greg Hartman7d5e0bf2017-12-19 23:48:34 -080021bool USBCmdDeviceList::OnRequest(const cvd::SharedFD& /*data*/) {
Greg Hartman92045f62017-10-31 17:12:24 -070022 LOG(INFO) << "Requesting device list from Cuttlefish...";
23 // No action required.
24 return true;
25}
26
Greg Hartman153b1062017-11-11 12:09:21 -080027bool USBCmdDeviceList::OnResponse(bool is_success, const cvd::SharedFD& fd) {
Greg Hartman92045f62017-10-31 17:12:24 -070028 // 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 Hartman7d5e0bf2017-12-19 23:48:34 -080049 if (static_cast<size_t>(
50 fd->Read(ifaces.data(),
51 ifaces.size() * sizeof(usb_forward::InterfaceInfo))) !=
Greg Hartman92045f62017-10-31 17:12:24 -070052 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