Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(169)

Side by Side Diff: device/test/usb_test_gadget_impl.cc

Issue 803653003: Use USB hotplug events from libusb when possible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | device/usb/usb_service_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
12 #include "base/files/file.h" 12 #include "base/files/file.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h" 16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h" 17 #include "base/memory/scoped_ptr.h"
18 #include "base/path_service.h" 18 #include "base/path_service.h"
19 #include "base/process/process_handle.h" 19 #include "base/process/process_handle.h"
20 #include "base/run_loop.h" 20 #include "base/run_loop.h"
21 #include "base/strings/string_number_conversions.h" 21 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/strings/utf_string_conversions.h" 23 #include "base/strings/utf_string_conversions.h"
24 #include "base/threading/platform_thread.h"
25 #include "base/time/time.h" 24 #include "base/time/time.h"
26 #include "device/usb/usb_device.h" 25 #include "device/usb/usb_device.h"
27 #include "device/usb/usb_device_handle.h" 26 #include "device/usb/usb_device_handle.h"
28 #include "device/usb/usb_service.h" 27 #include "device/usb/usb_service.h"
29 #include "net/proxy/proxy_service.h" 28 #include "net/proxy/proxy_service.h"
30 #include "net/url_request/url_fetcher.h" 29 #include "net/url_request/url_fetcher.h"
31 #include "net/url_request/url_fetcher_delegate.h" 30 #include "net/url_request/url_fetcher_delegate.h"
32 #include "net/url_request/url_request_context.h" 31 #include "net/url_request/url_request_context.h"
33 #include "net/url_request/url_request_context_builder.h" 32 #include "net/url_request/url_request_context_builder.h"
34 #include "net/url_request/url_request_context_getter.h" 33 #include "net/url_request/url_request_context_getter.h"
35 #include "url/gurl.h" 34 #include "url/gurl.h"
36 35
37 using ::base::PlatformThread; 36 using ::base::PlatformThread;
38 using ::base::TimeDelta; 37 using ::base::TimeDelta;
39 38
40 namespace device { 39 namespace device {
41 40
42 namespace { 41 namespace {
43 42
44 static const char kCommandLineSwitch[] = "enable-gadget-tests"; 43 static const char kCommandLineSwitch[] = "enable-gadget-tests";
45 static const int kClaimRetries = 100; // 5 seconds 44 static const int kClaimRetries = 100; // 5 seconds
46 static const int kDisconnectRetries = 100; // 5 seconds 45 static const int kDisconnectRetries = 100; // 5 seconds
47 static const int kRetryPeriod = 50; // 0.05 seconds 46 static const int kRetryPeriod = 50; // 0.05 seconds
48 static const int kReconnectRetries = 100; // 5 seconds 47 static const int kReconnectRetries = 100; // 5 seconds
49 static const int kUpdateRetries = 100; // 5 seconds 48 static const int kUpdateRetries = 100; // 5 seconds
50 49
50 // Wait for the given time delta while still running the main loop. This is
51 // necessary so that device add/remove events are processed by the UsbService.
52 void SleepWithRunLoop(base::TimeDelta delta) {
53 base::RunLoop run_loop;
54 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
55 run_loop.QuitClosure(), delta);
56 run_loop.Run();
57 }
58
51 struct UsbTestGadgetConfiguration { 59 struct UsbTestGadgetConfiguration {
52 UsbTestGadget::Type type; 60 UsbTestGadget::Type type;
53 const char* http_resource; 61 const char* http_resource;
54 uint16 product_id; 62 uint16 product_id;
55 }; 63 };
56 64
57 static const struct UsbTestGadgetConfiguration kConfigurations[] = { 65 static const struct UsbTestGadgetConfiguration kConfigurations[] = {
58 {UsbTestGadget::DEFAULT, "/unconfigure", 0x58F0}, 66 {UsbTestGadget::DEFAULT, "/unconfigure", 0x58F0},
59 {UsbTestGadget::KEYBOARD, "/keyboard/configure", 0x58F1}, 67 {UsbTestGadget::KEYBOARD, "/keyboard/configure", 0x58F1},
60 {UsbTestGadget::MOUSE, "/mouse/configure", 0x58F2}, 68 {UsbTestGadget::MOUSE, "/mouse/configure", 0x58F2},
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 137
130 scoped_ptr<UsbTestGadget> UsbTestGadget::Claim() { 138 scoped_ptr<UsbTestGadget> UsbTestGadget::Claim() {
131 scoped_ptr<UsbTestGadgetImpl> gadget(new UsbTestGadgetImpl); 139 scoped_ptr<UsbTestGadgetImpl> gadget(new UsbTestGadgetImpl);
132 140
133 int retries = kClaimRetries; 141 int retries = kClaimRetries;
134 while (!gadget->FindUnclaimed()) { 142 while (!gadget->FindUnclaimed()) {
135 if (--retries == 0) { 143 if (--retries == 0) {
136 LOG(ERROR) << "Failed to find an unclaimed device."; 144 LOG(ERROR) << "Failed to find an unclaimed device.";
137 return scoped_ptr<UsbTestGadget>(); 145 return scoped_ptr<UsbTestGadget>();
138 } 146 }
139 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); 147 SleepWithRunLoop(TimeDelta::FromMilliseconds(kRetryPeriod));
140 } 148 }
141 VLOG(1) << "It took " << (kClaimRetries - retries) 149 VLOG(1) << "It took " << (kClaimRetries - retries)
142 << " retries to find an unclaimed device."; 150 << " retries to find an unclaimed device.";
143 151
144 return gadget.Pass(); 152 return gadget.Pass();
145 } 153 }
146 154
147 UsbTestGadgetImpl::UsbTestGadgetImpl() { 155 UsbTestGadgetImpl::UsbTestGadgetImpl() {
148 net::URLRequestContextBuilder context_builder; 156 net::URLRequestContextBuilder context_builder;
149 context_builder.set_proxy_service(net::ProxyService::CreateDirect()); 157 context_builder.set_proxy_service(net::ProxyService::CreateDirect());
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 return false; 310 return false;
303 } 311 }
304 312
305 int retries = kUpdateRetries; 313 int retries = kUpdateRetries;
306 std::string new_version; 314 std::string new_version;
307 while (!GetVersion(&new_version) || new_version != version) { 315 while (!GetVersion(&new_version) || new_version != version) {
308 if (--retries == 0) { 316 if (--retries == 0) {
309 LOG(ERROR) << "Device not responding with new version."; 317 LOG(ERROR) << "Device not responding with new version.";
310 return false; 318 return false;
311 } 319 }
312 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); 320 SleepWithRunLoop(TimeDelta::FromMilliseconds(kRetryPeriod));
313 } 321 }
314 VLOG(1) << "It took " << (kUpdateRetries - retries) 322 VLOG(1) << "It took " << (kUpdateRetries - retries)
315 << " retries to see the new version."; 323 << " retries to see the new version.";
316 324
317 // Release the old reference to the device and try to open a new one. 325 // Release the old reference to the device and try to open a new one.
318 device_ = NULL; 326 device_ = NULL;
319 retries = kReconnectRetries; 327 retries = kReconnectRetries;
320 while (!FindClaimed()) { 328 while (!FindClaimed()) {
321 if (--retries == 0) { 329 if (--retries == 0) {
322 LOG(ERROR) << "Failed to find updated device."; 330 LOG(ERROR) << "Failed to find updated device.";
323 return false; 331 return false;
324 } 332 }
325 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); 333 SleepWithRunLoop(TimeDelta::FromMilliseconds(kRetryPeriod));
326 } 334 }
327 VLOG(1) << "It took " << (kReconnectRetries - retries) 335 VLOG(1) << "It took " << (kReconnectRetries - retries)
328 << " retries to find the updated device."; 336 << " retries to find the updated device.";
329 337
330 return true; 338 return true;
331 } 339 }
332 340
333 bool UsbTestGadgetImpl::FindClaimed() { 341 bool UsbTestGadgetImpl::FindClaimed() {
334 CHECK(!device_.get()); 342 CHECK(!device_.get());
335 343
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 int retries = kReconnectRetries; 459 int retries = kReconnectRetries;
452 while (true) { 460 while (true) {
453 device_ = NULL; 461 device_ = NULL;
454 if (FindClaimed() && device_->product_id() == config->product_id) { 462 if (FindClaimed() && device_->product_id() == config->product_id) {
455 break; 463 break;
456 } 464 }
457 if (--retries == 0) { 465 if (--retries == 0) {
458 LOG(ERROR) << "Failed to find updated device."; 466 LOG(ERROR) << "Failed to find updated device.";
459 return false; 467 return false;
460 } 468 }
461 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); 469 SleepWithRunLoop(TimeDelta::FromMilliseconds(kRetryPeriod));
462 } 470 }
463 VLOG(1) << "It took " << (kReconnectRetries - retries) 471 VLOG(1) << "It took " << (kReconnectRetries - retries)
464 << " retries to find the updated device."; 472 << " retries to find the updated device.";
465 473
466 return true; 474 return true;
467 } 475 }
468 476
469 bool UsbTestGadgetImpl::Disconnect() { 477 bool UsbTestGadgetImpl::Disconnect() {
470 const GURL url("http://" + device_address_ + "/disconnect"); 478 const GURL url("http://" + device_address_ + "/disconnect");
471 const int response_code = SimplePOSTRequest(url, ""); 479 const int response_code = SimplePOSTRequest(url, "");
472 480
473 if (response_code != 200) { 481 if (response_code != 200) {
474 LOG(ERROR) << "Unexpected HTTP " << response_code << " from /disconnect."; 482 LOG(ERROR) << "Unexpected HTTP " << response_code << " from /disconnect.";
475 return false; 483 return false;
476 } 484 }
477 485
478 // Release the old reference to the device and wait until it can't be found. 486 // Release the old reference to the device and wait until it can't be found.
479 int retries = kDisconnectRetries; 487 int retries = kDisconnectRetries;
480 while (true) { 488 while (true) {
481 device_ = NULL; 489 device_ = NULL;
482 if (!FindClaimed()) { 490 if (!FindClaimed()) {
483 break; 491 break;
484 } 492 }
485 if (--retries == 0) { 493 if (--retries == 0) {
486 LOG(ERROR) << "Device did not disconnect."; 494 LOG(ERROR) << "Device did not disconnect.";
487 return false; 495 return false;
488 } 496 }
489 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); 497 SleepWithRunLoop(TimeDelta::FromMilliseconds(kRetryPeriod));
490 } 498 }
491 VLOG(1) << "It took " << (kDisconnectRetries - retries) 499 VLOG(1) << "It took " << (kDisconnectRetries - retries)
492 << " retries for the device to disconnect."; 500 << " retries for the device to disconnect.";
493 501
494 return true; 502 return true;
495 } 503 }
496 504
497 bool UsbTestGadgetImpl::Reconnect() { 505 bool UsbTestGadgetImpl::Reconnect() {
498 const GURL url("http://" + device_address_ + "/reconnect"); 506 const GURL url("http://" + device_address_ + "/reconnect");
499 const int response_code = SimplePOSTRequest(url, ""); 507 const int response_code = SimplePOSTRequest(url, "");
500 508
501 if (response_code != 200) { 509 if (response_code != 200) {
502 LOG(ERROR) << "Unexpected HTTP " << response_code << " from /reconnect."; 510 LOG(ERROR) << "Unexpected HTTP " << response_code << " from /reconnect.";
503 return false; 511 return false;
504 } 512 }
505 513
506 int retries = kDisconnectRetries; 514 int retries = kDisconnectRetries;
507 while (true) { 515 while (true) {
508 if (FindClaimed()) { 516 if (FindClaimed()) {
509 break; 517 break;
510 } 518 }
511 if (--retries == 0) { 519 if (--retries == 0) {
512 LOG(ERROR) << "Device did not reconnect."; 520 LOG(ERROR) << "Device did not reconnect.";
513 return false; 521 return false;
514 } 522 }
515 PlatformThread::Sleep(TimeDelta::FromMilliseconds(kRetryPeriod)); 523 SleepWithRunLoop(TimeDelta::FromMilliseconds(kRetryPeriod));
516 } 524 }
517 VLOG(1) << "It took " << (kDisconnectRetries - retries) 525 VLOG(1) << "It took " << (kDisconnectRetries - retries)
518 << " retries for the device to reconnect."; 526 << " retries for the device to reconnect.";
519 527
520 return true; 528 return true;
521 } 529 }
522 530
523 } // namespace device 531 } // namespace device
OLDNEW
« no previous file with comments | « no previous file | device/usb/usb_service_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698