Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 1 | // 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 Chan | abfa15a | 2014-06-16 20:07:35 -0700 | [diff] [blame] | 8 | #include <ostream> // NOLINT(readability/streams) |
Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 9 | |
| 10 | #include <libusb.h> |
| 11 | |
Ben Chan | 4746c8a | 2014-09-02 20:34:58 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 13 | |
| 14 | namespace 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. |
| 18 | class 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 Chan | de49015 | 2013-05-29 17:05:49 -0700 | [diff] [blame] | 41 | kErrorTransferNotAllocated, |
| 42 | kErrorTransferAlreadySubmitted, |
| 43 | kErrorTransferNotSubmitted, |
| 44 | kErrorTransferBeingCancelled |
Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 45 | }; |
| 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 Fan | 6bc59e1 | 2020-11-11 02:51:06 +0900 | [diff] [blame^] | 56 | UsbError(const UsbError&) = delete; |
| 57 | UsbError& operator=(const UsbError&) = delete; |
Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 58 | |
Ben Chan | a4c8c96 | 2014-08-13 20:36:10 -0700 | [diff] [blame] | 59 | ~UsbError() = default; |
Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 60 | |
| 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 Chan | f975946 | 2013-05-27 21:50:30 -0700 | [diff] [blame] | 73 | // 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 Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 76 | |
| 77 | Type type() const { return type_; } |
| 78 | void set_type(Type type) { type_ = type; } |
| 79 | |
| 80 | private: |
| 81 | Type type_; |
Ben Chan | b6ce178 | 2013-05-27 01:00:02 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace mist |
| 85 | |
| 86 | // Output stream operator provided to facilitate logging. |
| 87 | std::ostream& operator<<(std::ostream& stream, const mist::UsbError& error); |
| 88 | |
| 89 | #endif // MIST_USB_ERROR_H_ |