blob: f6aeebed6cb7c5ed8ac9c25c92480c25f57b8988 [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
Ben Chanabfa15a2014-06-16 20:07:35 -07008#include <ostream> // NOLINT(readability/streams)
Ben Chanb6ce1782013-05-27 01:00:02 -07009
10#include <libusb.h>
11
Ben Chan4746c8a2014-09-02 20:34:58 -070012#include <base/macros.h>
Ben Chanb6ce1782013-05-27 01:00:02 -070013
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,
Ben Chande490152013-05-29 17:05:49 -070041 kErrorTransferNotAllocated,
42 kErrorTransferAlreadySubmitted,
43 kErrorTransferNotSubmitted,
44 kErrorTransferBeingCancelled
Ben Chanb6ce1782013-05-27 01:00:02 -070045 };
46
47 // Constructs a UsbError object with its error type set to UsbError::kSuccess.
48 UsbError();
49
50 // Constructs a UsbError object with its error type set to |type|.
51 explicit UsbError(Type type);
52
53 // Constructs a UsbError object with its error type set to a value equivalent
54 // to the libusb error |error|.
55 explicit UsbError(libusb_error error);
Qijiang Fan6bc59e12020-11-11 02:51:06 +090056 UsbError(const UsbError&) = delete;
57 UsbError& operator=(const UsbError&) = delete;
Ben Chanb6ce1782013-05-27 01:00:02 -070058
Ben Chana4c8c962014-08-13 20:36:10 -070059 ~UsbError() = default;
Ben Chanb6ce1782013-05-27 01:00:02 -070060
61 // Returns true if the error type of this object is set to UsbError::kSuccess,
62 // or false otherwise.
63 bool IsSuccess() const;
64
65 // Returns a string describing the error type of this object for logging
66 // purpose.
67 const char* ToString() const;
68
69 // Resets the error type of this object to UsbError::kSuccess.
70 void Clear();
71
72 // Sets the error type of this object to a value equivalent to the libusb
Ben Chanf9759462013-05-27 21:50:30 -070073 // error |error|. Returns true if the error type of this object is set to
74 // UsbError::kSuccess, or false otherwise.
75 bool SetFromLibUsbError(libusb_error error);
Ben Chanb6ce1782013-05-27 01:00:02 -070076
77 Type type() const { return type_; }
78 void set_type(Type type) { type_ = type; }
79
80 private:
81 Type type_;
Ben Chanb6ce1782013-05-27 01:00:02 -070082};
83
84} // namespace mist
85
86// Output stream operator provided to facilitate logging.
87std::ostream& operator<<(std::ostream& stream, const mist::UsbError& error);
88
89#endif // MIST_USB_ERROR_H_