Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_USB_USB_DEVICE_H_ | |
| 6 #define CHROME_BROWSER_USB_USB_DEVICE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "net/base/completion_callback.h" | |
| 14 #include "net/base/io_buffer.h" | |
| 15 #include "third_party/libusb/libusb/libusb.h" | |
| 16 | |
| 17 typedef libusb_device *PlatformUsbDevice; | |
| 18 typedef libusb_device_handle *PlatformUsbDeviceHandle; | |
| 19 typedef libusb_transfer *PlatformUsbTransferHandle; | |
|
miket_OOO
2012/04/25 21:24:36
style: whitespace should be after *.
Garret Kelly
2012/04/26 02:21:42
Done.
| |
| 20 | |
| 21 class UsbService; | |
| 22 | |
| 23 namespace net { | |
| 24 class IOBuffer; | |
| 25 } // namespace net | |
| 26 | |
| 27 // A UsbDevice wraps the platform's underlying representation of what a USB | |
| 28 // device actually is, and provides accessors for performing many of the | |
| 29 // standard USB operations. | |
| 30 class UsbDevice : public base::RefCounted<UsbDevice> { | |
| 31 public: | |
| 32 enum TransferDirection { IN, OUT }; | |
| 33 enum TransferRequestType { STANDARD, CLASS, VENDOR, RESERVED }; | |
| 34 enum TransferRecipient { DEVICE, INTERFACE, ENDPOINT, OTHER }; | |
| 35 | |
| 36 // Usually you will not want to directly create a UsbDevice, favouring to let | |
|
miket_OOO
2012/04/25 21:24:36
"favouring": I'll let this spelling slide in light
Garret Kelly
2012/04/26 02:21:42
gdk@determinist:~/sandbox/chrome/src$ git grep col
| |
| 37 // the UsbService take care of the logistics of getting a platform device | |
| 38 // handle and handling events for it. | |
| 39 UsbDevice(UsbService *service, PlatformUsbDeviceHandle handle); | |
| 40 | |
| 41 PlatformUsbDeviceHandle handle() { return handle_; } | |
| 42 | |
| 43 // Close the USB device and release the underlying platform device. | |
| 44 void Close(); | |
| 45 | |
| 46 void ControlTransfer(const TransferDirection direction, | |
| 47 const TransferRequestType request_type, | |
| 48 const TransferRecipient recipient, | |
| 49 const uint8 request, | |
| 50 const uint16 value, | |
| 51 const uint16 index, | |
| 52 net::IOBuffer *buffer, | |
|
miket_OOO
2012/04/25 21:24:36
Pointer placement (et seq.)
Garret Kelly
2012/04/26 02:21:42
Done.
| |
| 53 const size_t length, | |
| 54 const unsigned int timeout, | |
| 55 const net::CompletionCallback &callback); | |
| 56 | |
| 57 void BulkTransfer(const TransferDirection direction, | |
| 58 const uint8 endpoint, | |
| 59 net::IOBuffer *buffer, | |
| 60 const size_t length, | |
| 61 const unsigned int timeout, | |
| 62 const net::CompletionCallback &callback); | |
| 63 | |
| 64 void InterruptTransfer(const TransferDirection direction, | |
| 65 const uint8 endpoint, | |
| 66 net::IOBuffer *buffer, | |
| 67 const size_t length, | |
| 68 const unsigned int timeout, | |
| 69 const net::CompletionCallback &callback); | |
| 70 | |
| 71 // Normal code should not call this function. It is called by the platform's | |
| 72 // callback mechanism in such a way that it cannot be made private. Invokes | |
| 73 // the callbacks associated with a given transfer, and removes it from the | |
| 74 // in-flight transfer set. | |
| 75 void TransferComplete(PlatformUsbTransferHandle transfer); | |
| 76 | |
| 77 private: | |
| 78 struct Transfer { | |
| 79 scoped_refptr<net::IOBuffer> buffer; | |
| 80 net::CompletionCallback callback; | |
| 81 }; | |
| 82 | |
| 83 friend class base::RefCounted<UsbDevice>; | |
| 84 virtual ~UsbDevice(); | |
| 85 | |
| 86 // Checks that the device has not yet been closed. | |
| 87 void CheckDevice(); | |
| 88 | |
| 89 // Starts tracking the USB transfer associated with a platform transfer | |
| 90 // handle. Retains the buffer and copies the completion callback until the | |
| 91 // transfer finishes, whereupon it invokes the callback then releases the | |
| 92 // buffer. | |
| 93 void AddTransfer(PlatformUsbTransferHandle handle, net::IOBuffer *buffer, | |
| 94 const net::CompletionCallback &callback); | |
| 95 | |
| 96 UsbService *const service_; | |
|
miket_OOO
2012/04/25 21:24:36
It's nice to leave a comment explaining why a nake
Garret Kelly
2012/04/26 02:21:42
Done.
| |
| 97 PlatformUsbDeviceHandle handle_; | |
| 98 | |
| 99 // transfers_ tracks all in-flight transfers associated with this device, | |
| 100 // allowing the device to retain the buffer and callback associated with a | |
| 101 // transfer until such time that it completes. It is protected by lock_. | |
| 102 base::Lock lock_; | |
| 103 std::map<PlatformUsbTransferHandle, Transfer> transfers_; | |
| 104 | |
| 105 DISALLOW_EVIL_CONSTRUCTORS(UsbDevice); | |
| 106 }; | |
| 107 | |
| 108 #endif // CHROME_BROWSER_USB_USB_DEVICE_H_ | |
| OLD | NEW |