Commit a8eaa189 authored by zvorygin@chromium.org's avatar zvorygin@chromium.org

Extracted UsbConfigDescriptor, UsbInterfaceDescriptor and helper classes

as interfaces.

BUG=367094
R=rockot@chromium.org

Review URL: https://codereview.chromium.org/270323003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269767 0039d316-1c4b-4281-b951-d872f2087c98
parent 7a4c78e0
......@@ -27,8 +27,9 @@
'usb_service/usb_device.h',
'usb_service/usb_device_handle.cc',
'usb_service/usb_device_handle.h',
'usb_service/usb_interface.cc',
'usb_service/usb_interface.h',
'usb_service/usb_interface_impl.cc',
'usb_service/usb_interface_impl.h',
'usb_service/usb_service.h',
'usb_service/usb_service_impl.cc',
],
......
......@@ -9,6 +9,7 @@
#include "base/stl_util.h"
#include "components/usb_service/usb_context.h"
#include "components/usb_service/usb_device_handle.h"
#include "components/usb_service/usb_interface_impl.h"
#include "content/public/browser/browser_thread.h"
#include "third_party/libusb/src/libusb/libusb.h"
......@@ -126,7 +127,7 @@ scoped_refptr<UsbConfigDescriptor> UsbDeviceImpl::ListInterfaces() {
const int list_result =
libusb_get_active_config_descriptor(platform_device_, &platform_config);
if (list_result == 0)
return new UsbConfigDescriptor(platform_config);
return new UsbConfigDescriptorImpl(platform_config);
return NULL;
}
......
......@@ -13,6 +13,7 @@
#include "components/usb_service/usb_device.h"
struct libusb_device;
struct libusb_config_descriptor;
namespace usb_service {
......@@ -20,6 +21,7 @@ class UsbDeviceHandle;
class UsbContext;
typedef libusb_device* PlatformUsbDevice;
typedef libusb_config_descriptor* PlatformUsbConfigDescriptor;
class UsbDeviceImpl : public UsbDevice {
public:
......
......@@ -8,18 +8,8 @@
#include "base/memory/ref_counted.h"
#include "components/usb_service/usb_service_export.h"
struct libusb_config_descriptor;
struct libusb_endpoint_descriptor;
struct libusb_interface;
struct libusb_interface_descriptor;
namespace usb_service {
typedef libusb_config_descriptor* PlatformUsbConfigDescriptor;
typedef const libusb_endpoint_descriptor* PlatformUsbEndpointDescriptor;
typedef const libusb_interface* PlatformUsbInterface;
typedef const libusb_interface_descriptor* PlatformUsbInterfaceDescriptor;
enum UsbTransferType {
USB_TRANSFER_CONTROL = 0,
USB_TRANSFER_ISOCHRONOUS,
......@@ -45,32 +35,22 @@ enum UsbUsageType {
USB_USAGE_EXPLICIT_FEEDBACK
};
class UsbDevice;
class UsbConfigDescriptor;
class UsbInterfaceDescriptor;
class UsbInterfaceAltSettingDescriptor;
class USB_SERVICE_EXPORT UsbEndpointDescriptor
: public base::RefCounted<const UsbEndpointDescriptor> {
public:
int GetAddress() const;
UsbEndpointDirection GetDirection() const;
int GetMaximumPacketSize() const;
UsbSynchronizationType GetSynchronizationType() const;
UsbTransferType GetTransferType() const;
UsbUsageType GetUsageType() const;
int GetPollingInterval() const;
private:
friend class base::RefCounted<const UsbEndpointDescriptor>;
friend class UsbInterfaceAltSettingDescriptor;
virtual int GetAddress() const = 0;
virtual UsbEndpointDirection GetDirection() const = 0;
virtual int GetMaximumPacketSize() const = 0;
virtual UsbSynchronizationType GetSynchronizationType() const = 0;
virtual UsbTransferType GetTransferType() const = 0;
virtual UsbUsageType GetUsageType() const = 0;
virtual int GetPollingInterval() const = 0;
UsbEndpointDescriptor(scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbEndpointDescriptor descriptor);
~UsbEndpointDescriptor();
protected:
friend class base::RefCounted<const UsbEndpointDescriptor>;
scoped_refptr<const UsbConfigDescriptor> config_;
PlatformUsbEndpointDescriptor descriptor_;
UsbEndpointDescriptor() {};
virtual ~UsbEndpointDescriptor() {};
DISALLOW_COPY_AND_ASSIGN(UsbEndpointDescriptor);
};
......@@ -78,26 +58,21 @@ class USB_SERVICE_EXPORT UsbEndpointDescriptor
class USB_SERVICE_EXPORT UsbInterfaceAltSettingDescriptor
: public base::RefCounted<const UsbInterfaceAltSettingDescriptor> {
public:
size_t GetNumEndpoints() const;
scoped_refptr<const UsbEndpointDescriptor> GetEndpoint(size_t index) const;
virtual size_t GetNumEndpoints() const = 0;
virtual scoped_refptr<const UsbEndpointDescriptor> GetEndpoint(
size_t index) const = 0;
int GetInterfaceNumber() const;
int GetAlternateSetting() const;
int GetInterfaceClass() const;
int GetInterfaceSubclass() const;
int GetInterfaceProtocol() const;
virtual int GetInterfaceNumber() const = 0;
virtual int GetAlternateSetting() const = 0;
virtual int GetInterfaceClass() const = 0;
virtual int GetInterfaceSubclass() const = 0;
virtual int GetInterfaceProtocol() const = 0;
private:
protected:
friend class base::RefCounted<const UsbInterfaceAltSettingDescriptor>;
friend class UsbInterfaceDescriptor;
UsbInterfaceAltSettingDescriptor(
scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbInterfaceDescriptor descriptor);
~UsbInterfaceAltSettingDescriptor();
scoped_refptr<const UsbConfigDescriptor> config_;
PlatformUsbInterfaceDescriptor descriptor_;
UsbInterfaceAltSettingDescriptor() {};
virtual ~UsbInterfaceAltSettingDescriptor() {};
DISALLOW_COPY_AND_ASSIGN(UsbInterfaceAltSettingDescriptor);
};
......@@ -105,20 +80,15 @@ class USB_SERVICE_EXPORT UsbInterfaceAltSettingDescriptor
class USB_SERVICE_EXPORT UsbInterfaceDescriptor
: public base::RefCounted<const UsbInterfaceDescriptor> {
public:
size_t GetNumAltSettings() const;
scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting(
size_t index) const;
virtual size_t GetNumAltSettings() const = 0;
virtual scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting(
size_t index) const = 0;
private:
protected:
friend class base::RefCounted<const UsbInterfaceDescriptor>;
friend class UsbConfigDescriptor;
UsbInterfaceDescriptor(scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbInterface usbInterface);
~UsbInterfaceDescriptor();
scoped_refptr<const UsbConfigDescriptor> config_;
PlatformUsbInterface interface_;
UsbInterfaceDescriptor() {};
virtual ~UsbInterfaceDescriptor() {};
DISALLOW_COPY_AND_ASSIGN(UsbInterfaceDescriptor);
};
......@@ -126,22 +96,15 @@ class USB_SERVICE_EXPORT UsbInterfaceDescriptor
class USB_SERVICE_EXPORT UsbConfigDescriptor
: public base::RefCounted<UsbConfigDescriptor> {
public:
virtual size_t GetNumInterfaces() const;
virtual size_t GetNumInterfaces() const = 0;
virtual scoped_refptr<const UsbInterfaceDescriptor> GetInterface(
size_t index) const;
size_t index) const = 0;
protected:
// Constructor called in test only
UsbConfigDescriptor();
virtual ~UsbConfigDescriptor();
private:
friend class base::RefCounted<UsbConfigDescriptor>;
friend class UsbDeviceImpl;
explicit UsbConfigDescriptor(PlatformUsbConfigDescriptor config);
PlatformUsbConfigDescriptor config_;
UsbConfigDescriptor() {};
virtual ~UsbConfigDescriptor() {};
DISALLOW_COPY_AND_ASSIGN(UsbConfigDescriptor);
};
......
......@@ -2,27 +2,27 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/usb_service/usb_interface.h"
#include "components/usb_service/usb_interface_impl.h"
#include "base/logging.h"
#include "third_party/libusb/src/libusb/libusb.h"
namespace usb_service {
UsbEndpointDescriptor::UsbEndpointDescriptor(
UsbEndpointDescriptorImpl::UsbEndpointDescriptorImpl(
scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbEndpointDescriptor descriptor)
: config_(config), descriptor_(descriptor) {
}
UsbEndpointDescriptor::~UsbEndpointDescriptor() {
UsbEndpointDescriptorImpl::~UsbEndpointDescriptorImpl() {
}
int UsbEndpointDescriptor::GetAddress() const {
int UsbEndpointDescriptorImpl::GetAddress() const {
return descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_ADDRESS_MASK;
}
UsbEndpointDirection UsbEndpointDescriptor::GetDirection() const {
UsbEndpointDirection UsbEndpointDescriptorImpl::GetDirection() const {
switch (descriptor_->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) {
case LIBUSB_ENDPOINT_IN:
return USB_DIRECTION_INBOUND;
......@@ -34,11 +34,12 @@ UsbEndpointDirection UsbEndpointDescriptor::GetDirection() const {
}
}
int UsbEndpointDescriptor::GetMaximumPacketSize() const {
int UsbEndpointDescriptorImpl::GetMaximumPacketSize() const {
return descriptor_->wMaxPacketSize;
}
UsbSynchronizationType UsbEndpointDescriptor::GetSynchronizationType() const {
UsbSynchronizationType
UsbEndpointDescriptorImpl::GetSynchronizationType() const {
switch (descriptor_->bmAttributes & LIBUSB_ISO_SYNC_TYPE_MASK) {
case LIBUSB_ISO_SYNC_TYPE_NONE:
return USB_SYNCHRONIZATION_NONE;
......@@ -54,7 +55,7 @@ UsbSynchronizationType UsbEndpointDescriptor::GetSynchronizationType() const {
}
}
UsbTransferType UsbEndpointDescriptor::GetTransferType() const {
UsbTransferType UsbEndpointDescriptorImpl::GetTransferType() const {
switch (descriptor_->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) {
case LIBUSB_TRANSFER_TYPE_CONTROL:
return USB_TRANSFER_CONTROL;
......@@ -70,7 +71,7 @@ UsbTransferType UsbEndpointDescriptor::GetTransferType() const {
}
}
UsbUsageType UsbEndpointDescriptor::GetUsageType() const {
UsbUsageType UsbEndpointDescriptorImpl::GetUsageType() const {
switch (descriptor_->bmAttributes & LIBUSB_ISO_USAGE_TYPE_MASK) {
case LIBUSB_ISO_USAGE_TYPE_DATA:
return USB_USAGE_DATA;
......@@ -84,91 +85,84 @@ UsbUsageType UsbEndpointDescriptor::GetUsageType() const {
}
}
int UsbEndpointDescriptor::GetPollingInterval() const {
int UsbEndpointDescriptorImpl::GetPollingInterval() const {
return descriptor_->bInterval;
}
UsbInterfaceAltSettingDescriptor::UsbInterfaceAltSettingDescriptor(
UsbInterfaceAltSettingDescriptorImpl::UsbInterfaceAltSettingDescriptorImpl(
scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbInterfaceDescriptor descriptor)
: config_(config), descriptor_(descriptor) {
}
UsbInterfaceAltSettingDescriptor::~UsbInterfaceAltSettingDescriptor() {
UsbInterfaceAltSettingDescriptorImpl::~UsbInterfaceAltSettingDescriptorImpl() {
}
size_t UsbInterfaceAltSettingDescriptor::GetNumEndpoints() const {
size_t UsbInterfaceAltSettingDescriptorImpl::GetNumEndpoints() const {
return descriptor_->bNumEndpoints;
}
scoped_refptr<const UsbEndpointDescriptor>
UsbInterfaceAltSettingDescriptor::GetEndpoint(size_t index) const {
return new UsbEndpointDescriptor(config_, &descriptor_->endpoint[index]);
UsbInterfaceAltSettingDescriptorImpl::GetEndpoint(size_t index) const {
return new UsbEndpointDescriptorImpl(config_, &descriptor_->endpoint[index]);
}
int UsbInterfaceAltSettingDescriptor::GetInterfaceNumber() const {
int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceNumber() const {
return descriptor_->bInterfaceNumber;
}
int UsbInterfaceAltSettingDescriptor::GetAlternateSetting() const {
int UsbInterfaceAltSettingDescriptorImpl::GetAlternateSetting() const {
return descriptor_->bAlternateSetting;
}
int UsbInterfaceAltSettingDescriptor::GetInterfaceClass() const {
int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceClass() const {
return descriptor_->bInterfaceClass;
}
int UsbInterfaceAltSettingDescriptor::GetInterfaceSubclass() const {
int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceSubclass() const {
return descriptor_->bInterfaceSubClass;
}
int UsbInterfaceAltSettingDescriptor::GetInterfaceProtocol() const {
int UsbInterfaceAltSettingDescriptorImpl::GetInterfaceProtocol() const {
return descriptor_->bInterfaceProtocol;
}
UsbInterfaceDescriptor::UsbInterfaceDescriptor(
UsbInterfaceDescriptorImpl::UsbInterfaceDescriptorImpl(
scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbInterface usbInterface)
: config_(config), interface_(usbInterface) {
}
UsbInterfaceDescriptor::~UsbInterfaceDescriptor() {
UsbInterfaceDescriptorImpl::~UsbInterfaceDescriptorImpl() {
}
size_t UsbInterfaceDescriptor::GetNumAltSettings() const {
size_t UsbInterfaceDescriptorImpl::GetNumAltSettings() const {
return interface_->num_altsetting;
}
scoped_refptr<const UsbInterfaceAltSettingDescriptor>
UsbInterfaceDescriptor::GetAltSetting(size_t index) const {
return new UsbInterfaceAltSettingDescriptor(config_,
&interface_->altsetting[index]);
UsbInterfaceDescriptorImpl::GetAltSetting(size_t index) const {
return new UsbInterfaceAltSettingDescriptorImpl(
config_, &interface_->altsetting[index]);
}
UsbConfigDescriptor::UsbConfigDescriptor(PlatformUsbConfigDescriptor config)
UsbConfigDescriptorImpl::UsbConfigDescriptorImpl(
PlatformUsbConfigDescriptor config)
: config_(config) {
DCHECK(config);
}
// TODO(zvorygin): Used for tests only. Should be removed when
// all interfaces are extracted properly.
UsbConfigDescriptor::UsbConfigDescriptor() {
config_ = NULL;
UsbConfigDescriptorImpl::~UsbConfigDescriptorImpl() {
libusb_free_config_descriptor(config_);
}
UsbConfigDescriptor::~UsbConfigDescriptor() {
if (config_ != NULL) {
libusb_free_config_descriptor(config_);
config_ = NULL;
}
}
size_t UsbConfigDescriptor::GetNumInterfaces() const {
size_t UsbConfigDescriptorImpl::GetNumInterfaces() const {
return config_->bNumInterfaces;
}
scoped_refptr<const UsbInterfaceDescriptor> UsbConfigDescriptor::GetInterface(
size_t index) const {
return new UsbInterfaceDescriptor(this, &config_->interface[index]);
scoped_refptr<const UsbInterfaceDescriptor>
UsbConfigDescriptorImpl::GetInterface(size_t index) const {
return new UsbInterfaceDescriptorImpl(this, &config_->interface[index]);
}
} // namespace usb_service
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_USB_SERVICE_USB_INTERFACE_IMPL_H_
#define COMPONENTS_USB_SERVICE_USB_INTERFACE_IMPL_H_
#include "base/memory/ref_counted.h"
#include "components/usb_service/usb_interface.h"
#include "components/usb_service/usb_service_export.h"
struct libusb_config_descriptor;
struct libusb_endpoint_descriptor;
struct libusb_interface;
struct libusb_interface_descriptor;
namespace usb_service {
typedef libusb_config_descriptor* PlatformUsbConfigDescriptor;
typedef const libusb_endpoint_descriptor* PlatformUsbEndpointDescriptor;
typedef const libusb_interface* PlatformUsbInterface;
typedef const libusb_interface_descriptor* PlatformUsbInterfaceDescriptor;
class UsbConfigDescriptorImpl;
class UsbInterfaceAltSettingDescriptor;
class UsbEndpointDescriptorImpl : public UsbEndpointDescriptor {
public:
virtual int GetAddress() const OVERRIDE;
virtual UsbEndpointDirection GetDirection() const OVERRIDE;
virtual int GetMaximumPacketSize() const OVERRIDE;
virtual UsbSynchronizationType GetSynchronizationType() const OVERRIDE;
virtual UsbTransferType GetTransferType() const OVERRIDE;
virtual UsbUsageType GetUsageType() const OVERRIDE;
virtual int GetPollingInterval() const OVERRIDE;
private:
friend class base::RefCounted<const UsbEndpointDescriptorImpl>;
friend class UsbInterfaceAltSettingDescriptorImpl;
UsbEndpointDescriptorImpl(scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbEndpointDescriptor descriptor);
virtual ~UsbEndpointDescriptorImpl();
scoped_refptr<const UsbConfigDescriptor> config_;
PlatformUsbEndpointDescriptor descriptor_;
DISALLOW_COPY_AND_ASSIGN(UsbEndpointDescriptorImpl);
};
class UsbInterfaceAltSettingDescriptorImpl
: public UsbInterfaceAltSettingDescriptor {
public:
virtual size_t GetNumEndpoints() const OVERRIDE;
virtual scoped_refptr<const UsbEndpointDescriptor> GetEndpoint(
size_t index) const OVERRIDE;
virtual int GetInterfaceNumber() const OVERRIDE;
virtual int GetAlternateSetting() const OVERRIDE;
virtual int GetInterfaceClass() const OVERRIDE;
virtual int GetInterfaceSubclass() const OVERRIDE;
virtual int GetInterfaceProtocol() const OVERRIDE;
private:
friend class UsbInterfaceDescriptorImpl;
UsbInterfaceAltSettingDescriptorImpl(
scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbInterfaceDescriptor descriptor);
virtual ~UsbInterfaceAltSettingDescriptorImpl();
scoped_refptr<const UsbConfigDescriptor> config_;
PlatformUsbInterfaceDescriptor descriptor_;
DISALLOW_COPY_AND_ASSIGN(UsbInterfaceAltSettingDescriptorImpl);
};
class UsbInterfaceDescriptorImpl : public UsbInterfaceDescriptor {
public:
virtual size_t GetNumAltSettings() const OVERRIDE;
virtual scoped_refptr<const UsbInterfaceAltSettingDescriptor> GetAltSetting(
size_t index) const OVERRIDE;
private:
friend class base::RefCounted<const UsbInterfaceDescriptorImpl>;
friend class UsbConfigDescriptorImpl;
UsbInterfaceDescriptorImpl(scoped_refptr<const UsbConfigDescriptor> config,
PlatformUsbInterface usbInterface);
virtual ~UsbInterfaceDescriptorImpl();
scoped_refptr<const UsbConfigDescriptor> config_;
PlatformUsbInterface interface_;
DISALLOW_COPY_AND_ASSIGN(UsbInterfaceDescriptorImpl);
};
class UsbConfigDescriptorImpl : public UsbConfigDescriptor {
public:
virtual size_t GetNumInterfaces() const OVERRIDE;
virtual scoped_refptr<const UsbInterfaceDescriptor> GetInterface(
size_t index) const OVERRIDE;
private:
friend class base::RefCounted<UsbConfigDescriptor>;
friend class UsbDeviceImpl;
explicit UsbConfigDescriptorImpl(PlatformUsbConfigDescriptor config);
virtual ~UsbConfigDescriptorImpl();
PlatformUsbConfigDescriptor config_;
DISALLOW_COPY_AND_ASSIGN(UsbConfigDescriptorImpl);
};
} // namespace usb_service;
#endif // COMPONENTS_USB_SERVICE_USB_INTERFACE_IMPL_H_
......@@ -19,6 +19,7 @@ using usb_service::UsbConfigDescriptor;
using usb_service::UsbDevice;
using usb_service::UsbDeviceHandle;
using usb_service::UsbEndpointDirection;
using usb_service::UsbInterfaceDescriptor;
using usb_service::UsbService;
using usb_service::UsbTransferCallback;
......@@ -95,6 +96,8 @@ class MockUsbDeviceHandle : public UsbDeviceHandle {
class MockUsbConfigDescriptor : public UsbConfigDescriptor {
public:
MOCK_CONST_METHOD0(GetNumInterfaces, size_t());
MOCK_CONST_METHOD1(GetInterface,
scoped_refptr<const UsbInterfaceDescriptor>(size_t index));
protected:
virtual ~MockUsbConfigDescriptor() {}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment