blob: 6b299b535e0e18dec009ec07309c24983b8afc61 [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);
56
Ben Chana4c8c962014-08-13 20:36:10 -070057 ~UsbError() = default;
Ben Chanb6ce1782013-05-27 01:00:02 -070058
59 // Returns true if the error type of this object is set to UsbError::kSuccess,
60 // or false otherwise.
61 bool IsSuccess() const;
62
63 // Returns a string describing the error type of this object for logging
64 // purpose.
65 const char* ToString() const;
66
67 // Resets the error type of this object to UsbError::kSuccess.
68 void Clear();
69
70 // Sets the error type of this object to a value equivalent to the libusb
Ben Chanf9759462013-05-27 21:50:30 -070071 // error |error|. Returns true if the error type of this object is set to
72 // UsbError::kSuccess, or false otherwise.
73 bool SetFromLibUsbError(libusb_error error);
Ben Chanb6ce1782013-05-27 01:00:02 -070074
75 Type type() const { return type_; }
76 void set_type(Type type) { type_ = type; }
77
78 private:
79 Type type_;
80
81 DISALLOW_COPY_AND_ASSIGN(UsbError);
82};
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_