blob: 1373c07415ba1344c6d33deb7628afe5fcb8f349 [file] [log] [blame]
Ben Chanb6ce1782013-05-27 01:00:02 -07001// Copyright (c) 2013 The Chromium OS 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 MIST_USB_ERROR_H_
6#define MIST_USB_ERROR_H_
7
8#include <ostream>
9
10#include <libusb.h>
11
12#include <base/basictypes.h>
13
14namespace mist {
15
16// A USB error, which represents one of the errors defined by libusb 1.0 in the
17// libusb_error enum and some additional errors defined by mist.
18class UsbError {
19 public:
20 enum Type {
21 // Errors that correspond to those in the libusb_error enum defined by
22 // libusb.
23 kSuccess,
24 kErrorIO,
25 kErrorInvalidParameter,
26 kErrorAccess,
27 kErrorNoDevice,
28 kErrorNotFound,
29 kErrorBusy,
30 kErrorTimeout,
31 kErrorOverflow,
32 kErrorPipe,
33 kErrorInterrupted,
34 kErrorNoMemory,
35 kErrorNotSupported,
36 kErrorOther,
37
38 // Additional errors.
39 kErrorDeviceNotOpen,
40 kErrorTransferAlreadyAllocated,
41 kErrorTransferNotAllocated
42 };
43
44 // Constructs a UsbError object with its error type set to UsbError::kSuccess.
45 UsbError();
46
47 // Constructs a UsbError object with its error type set to |type|.
48 explicit UsbError(Type type);
49
50 // Constructs a UsbError object with its error type set to a value equivalent
51 // to the libusb error |error|.
52 explicit UsbError(libusb_error error);
53
54 ~UsbError();
55
56 // Returns true if the error type of this object is set to UsbError::kSuccess,
57 // or false otherwise.
58 bool IsSuccess() const;
59
60 // Returns a string describing the error type of this object for logging
61 // purpose.
62 const char* ToString() const;
63
64 // Resets the error type of this object to UsbError::kSuccess.
65 void Clear();
66
67 // Sets the error type of this object to a value equivalent to the libusb
68 // error |error|.
69 void SetFromLibUsbError(libusb_error error);
70
71 Type type() const { return type_; }
72 void set_type(Type type) { type_ = type; }
73
74 private:
75 Type type_;
76
77 DISALLOW_COPY_AND_ASSIGN(UsbError);
78};
79
80} // namespace mist
81
82// Output stream operator provided to facilitate logging.
83std::ostream& operator<<(std::ostream& stream, const mist::UsbError& error);
84
85#endif // MIST_USB_ERROR_H_