Home | History | Annotate | Download | only in vadb
      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 
     18 #include "host/libs/vadb/usb_cmd_device_list.h"
     19 
     20 namespace vadb {
     21 bool USBCmdDeviceList::OnRequest(const cvd::SharedFD& /*data*/) {
     22   LOG(INFO) << "Requesting device list from Cuttlefish...";
     23   // No action required.
     24   return true;
     25 }
     26 
     27 bool USBCmdDeviceList::OnResponse(bool is_success, const cvd::SharedFD& fd) {
     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);
     49     if (static_cast<size_t>(
     50             fd->Read(ifaces.data(),
     51                      ifaces.size() * sizeof(usb_forward::InterfaceInfo))) !=
     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
     66