blob: 2781564cd74e902217882db41863abcb3a62cd3c [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
Ben Chanf9759462013-05-27 21:50:30 -070068 // error |error|. Returns true if the error type of this object is set to
69 // UsbError::kSuccess, or false otherwise.
70 bool SetFromLibUsbError(libusb_error error);
Ben Chanb6ce1782013-05-27 01:00:02 -070071
72 Type type() const { return type_; }
73 void set_type(Type type) { type_ = type; }
74
75 private:
76 Type type_;
77
78 DISALLOW_COPY_AND_ASSIGN(UsbError);
79};
80
81} // namespace mist
82
83// Output stream operator provided to facilitate logging.
84std::ostream& operator<<(std::ostream& stream, const mist::UsbError& error);
85
86#endif // MIST_USB_ERROR_H_