| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/test/usb_test_gadget.h" | 5 #include "device/test/usb_test_gadget.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 | 64 |
| 65 class UsbTestGadgetImpl : public UsbTestGadget { | 65 class UsbTestGadgetImpl : public UsbTestGadget { |
| 66 public: | 66 public: |
| 67 virtual ~UsbTestGadgetImpl(); | 67 virtual ~UsbTestGadgetImpl(); |
| 68 | 68 |
| 69 virtual bool Unclaim() OVERRIDE; | 69 virtual bool Unclaim() OVERRIDE; |
| 70 virtual bool Disconnect() OVERRIDE; | 70 virtual bool Disconnect() OVERRIDE; |
| 71 virtual bool Reconnect() OVERRIDE; | 71 virtual bool Reconnect() OVERRIDE; |
| 72 virtual bool SetType(Type type) OVERRIDE; | 72 virtual bool SetType(Type type) OVERRIDE; |
| 73 virtual UsbDevice* GetDevice() const OVERRIDE; | 73 virtual UsbDevice* GetDevice() const OVERRIDE; |
| 74 virtual std::string GetSerial() const OVERRIDE; | 74 virtual std::string GetSerialNumber() const OVERRIDE; |
| 75 | 75 |
| 76 protected: | 76 protected: |
| 77 UsbTestGadgetImpl(); | 77 UsbTestGadgetImpl(); |
| 78 | 78 |
| 79 private: | 79 private: |
| 80 scoped_ptr<net::URLFetcher> CreateURLFetcher( | 80 scoped_ptr<net::URLFetcher> CreateURLFetcher( |
| 81 const GURL& url, | 81 const GURL& url, |
| 82 net::URLFetcher::RequestType request_type, | 82 net::URLFetcher::RequestType request_type, |
| 83 net::URLFetcherDelegate* delegate); | 83 net::URLFetcherDelegate* delegate); |
| 84 int SimplePOSTRequest(const GURL& url, const std::string& form_data); | 84 int SimplePOSTRequest(const GURL& url, const std::string& form_data); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 159 UsbTestGadgetImpl::~UsbTestGadgetImpl() { | 159 UsbTestGadgetImpl::~UsbTestGadgetImpl() { |
| 160 if (!device_address_.empty()) { | 160 if (!device_address_.empty()) { |
| 161 Unclaim(); | 161 Unclaim(); |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 UsbDevice* UsbTestGadgetImpl::GetDevice() const { | 165 UsbDevice* UsbTestGadgetImpl::GetDevice() const { |
| 166 return device_.get(); | 166 return device_.get(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 std::string UsbTestGadgetImpl::GetSerial() const { | 169 std::string UsbTestGadgetImpl::GetSerialNumber() const { |
| 170 return device_address_; | 170 return device_address_; |
| 171 } | 171 } |
| 172 | 172 |
| 173 scoped_ptr<net::URLFetcher> UsbTestGadgetImpl::CreateURLFetcher( | 173 scoped_ptr<net::URLFetcher> UsbTestGadgetImpl::CreateURLFetcher( |
| 174 const GURL& url, net::URLFetcher::RequestType request_type, | 174 const GURL& url, net::URLFetcher::RequestType request_type, |
| 175 net::URLFetcherDelegate* delegate) { | 175 net::URLFetcherDelegate* delegate) { |
| 176 scoped_ptr<net::URLFetcher> url_fetcher( | 176 scoped_ptr<net::URLFetcher> url_fetcher( |
| 177 net::URLFetcher::Create(url, request_type, delegate)); | 177 net::URLFetcher::Create(url, request_type, delegate)); |
| 178 | 178 |
| 179 url_fetcher->SetRequestContext( | 179 url_fetcher->SetRequestContext( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 198 } | 198 } |
| 199 | 199 |
| 200 bool UsbTestGadgetImpl::FindUnclaimed() { | 200 bool UsbTestGadgetImpl::FindUnclaimed() { |
| 201 std::vector<scoped_refptr<UsbDevice> > devices; | 201 std::vector<scoped_refptr<UsbDevice> > devices; |
| 202 usb_service_->GetDevices(&devices); | 202 usb_service_->GetDevices(&devices); |
| 203 | 203 |
| 204 for (std::vector<scoped_refptr<UsbDevice> >::const_iterator iter = | 204 for (std::vector<scoped_refptr<UsbDevice> >::const_iterator iter = |
| 205 devices.begin(); iter != devices.end(); ++iter) { | 205 devices.begin(); iter != devices.end(); ++iter) { |
| 206 const scoped_refptr<UsbDevice> &device = *iter; | 206 const scoped_refptr<UsbDevice> &device = *iter; |
| 207 if (device->vendor_id() == 0x18D1 && device->product_id() == 0x58F0) { | 207 if (device->vendor_id() == 0x18D1 && device->product_id() == 0x58F0) { |
| 208 scoped_refptr<UsbDeviceHandle> handle = device->Open(); | 208 base::string16 serial_utf16; |
| 209 if (handle.get() == NULL) { | 209 if (!device->GetSerialNumber(&serial_utf16)) { |
| 210 continue; | 210 continue; |
| 211 } | 211 } |
| 212 | 212 |
| 213 base::string16 serial_utf16; | |
| 214 if (!handle->GetSerial(&serial_utf16)) { | |
| 215 continue; | |
| 216 } | |
| 217 | |
| 218 const std::string serial = base::UTF16ToUTF8(serial_utf16); | 213 const std::string serial = base::UTF16ToUTF8(serial_utf16); |
| 219 const GURL url("http://" + serial + "/claim"); | 214 const GURL url("http://" + serial + "/claim"); |
| 220 const std::string form_data = base::StringPrintf( | 215 const std::string form_data = base::StringPrintf( |
| 221 "session_id=%s", | 216 "session_id=%s", |
| 222 net::EscapeUrlEncodedData(session_id_, true).c_str()); | 217 net::EscapeUrlEncodedData(session_id_, true).c_str()); |
| 223 const int response_code = SimplePOSTRequest(url, form_data); | 218 const int response_code = SimplePOSTRequest(url, form_data); |
| 224 | 219 |
| 225 if (response_code == 200) { | 220 if (response_code == 200) { |
| 226 device_address_ = serial; | 221 device_address_ = serial; |
| 227 device_ = device; | 222 device_ = device; |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 } | 328 } |
| 334 VLOG(1) << "It took " << (kReconnectRetries - retries) | 329 VLOG(1) << "It took " << (kReconnectRetries - retries) |
| 335 << " retries to find the updated device."; | 330 << " retries to find the updated device."; |
| 336 | 331 |
| 337 return true; | 332 return true; |
| 338 } | 333 } |
| 339 | 334 |
| 340 bool UsbTestGadgetImpl::FindClaimed() { | 335 bool UsbTestGadgetImpl::FindClaimed() { |
| 341 CHECK(!device_.get()); | 336 CHECK(!device_.get()); |
| 342 | 337 |
| 343 std::string expected_serial = GetSerial(); | 338 std::string expected_serial = GetSerialNumber(); |
| 344 | 339 |
| 345 std::vector<scoped_refptr<UsbDevice> > devices; | 340 std::vector<scoped_refptr<UsbDevice> > devices; |
| 346 usb_service_->GetDevices(&devices); | 341 usb_service_->GetDevices(&devices); |
| 347 | 342 |
| 348 for (std::vector<scoped_refptr<UsbDevice> >::iterator iter = | 343 for (std::vector<scoped_refptr<UsbDevice> >::iterator iter = |
| 349 devices.begin(); iter != devices.end(); ++iter) { | 344 devices.begin(); iter != devices.end(); ++iter) { |
| 350 scoped_refptr<UsbDevice> &device = *iter; | 345 scoped_refptr<UsbDevice> &device = *iter; |
| 351 | 346 |
| 352 if (device->vendor_id() == 0x18D1) { | 347 if (device->vendor_id() == 0x18D1) { |
| 353 const uint16 product_id = device->product_id(); | 348 const uint16 product_id = device->product_id(); |
| 354 bool found = false; | 349 bool found = false; |
| 355 for (size_t i = 0; i < arraysize(kConfigurations); ++i) { | 350 for (size_t i = 0; i < arraysize(kConfigurations); ++i) { |
| 356 if (product_id == kConfigurations[i].product_id) { | 351 if (product_id == kConfigurations[i].product_id) { |
| 357 found = true; | 352 found = true; |
| 358 break; | 353 break; |
| 359 } | 354 } |
| 360 } | 355 } |
| 361 if (!found) { | 356 if (!found) { |
| 362 continue; | 357 continue; |
| 363 } | 358 } |
| 364 | 359 |
| 365 scoped_refptr<UsbDeviceHandle> handle(device->Open()); | |
| 366 if (handle.get() == NULL) { | |
| 367 continue; | |
| 368 } | |
| 369 | |
| 370 base::string16 serial_utf16; | 360 base::string16 serial_utf16; |
| 371 if (!handle->GetSerial(&serial_utf16)) { | 361 if (!device->GetSerialNumber(&serial_utf16)) { |
| 372 continue; | 362 continue; |
| 373 } | 363 } |
| 374 | 364 |
| 375 std::string serial = base::UTF16ToUTF8(serial_utf16); | 365 std::string serial = base::UTF16ToUTF8(serial_utf16); |
| 376 if (serial != expected_serial) { | 366 if (serial != expected_serial) { |
| 377 continue; | 367 continue; |
| 378 } | 368 } |
| 379 | 369 |
| 380 device_ = device; | 370 device_ = device; |
| 381 return true; | 371 return true; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 } | 514 } |
| 525 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); | 515 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); |
| 526 } | 516 } |
| 527 VLOG(1) << "It took " << (kDisconnectRetries - retries) | 517 VLOG(1) << "It took " << (kDisconnectRetries - retries) |
| 528 << " retries for the device to reconnect."; | 518 << " retries for the device to reconnect."; |
| 529 | 519 |
| 530 return true; | 520 return true; |
| 531 } | 521 } |
| 532 | 522 |
| 533 } // namespace device | 523 } // namespace device |
| OLD | NEW |