blob: f00ff5f1ce6a375ed122ebfc6d83c4d0369c7b2f [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#include "mist/usb_error.h"
6
Ben Chanb6ce1782013-05-27 01:00:02 -07007namespace mist {
8
9namespace {
10
11UsbError::Type ConvertFromLibUsbErrorToUsbErrorType(libusb_error error) {
12 switch (error) {
13 case LIBUSB_SUCCESS:
14 return UsbError::kSuccess;
15 case LIBUSB_ERROR_IO:
16 return UsbError::kErrorIO;
17 case LIBUSB_ERROR_INVALID_PARAM:
18 return UsbError::kErrorInvalidParameter;
19 case LIBUSB_ERROR_ACCESS:
20 return UsbError::kErrorAccess;
21 case LIBUSB_ERROR_NO_DEVICE:
22 return UsbError::kErrorNoDevice;
23 case LIBUSB_ERROR_NOT_FOUND:
24 return UsbError::kErrorNotFound;
25 case LIBUSB_ERROR_BUSY:
26 return UsbError::kErrorBusy;
27 case LIBUSB_ERROR_TIMEOUT:
28 return UsbError::kErrorTimeout;
29 case LIBUSB_ERROR_OVERFLOW:
30 return UsbError::kErrorOverflow;
31 case LIBUSB_ERROR_PIPE:
32 return UsbError::kErrorPipe;
33 case LIBUSB_ERROR_INTERRUPTED:
34 return UsbError::kErrorInterrupted;
35 case LIBUSB_ERROR_NO_MEM:
36 return UsbError::kErrorNoMemory;
37 case LIBUSB_ERROR_NOT_SUPPORTED:
38 return UsbError::kErrorNotSupported;
39 default:
40 return UsbError::kErrorOther;
41 }
42}
43
44} // namespace
45
46UsbError::UsbError() : type_(kSuccess) {}
47
48UsbError::UsbError(Type type) : type_(type) {}
49
50UsbError::UsbError(libusb_error error)
51 : type_(ConvertFromLibUsbErrorToUsbErrorType(error)) {}
52
Ben Chanb6ce1782013-05-27 01:00:02 -070053bool UsbError::IsSuccess() const {
54 return type_ == kSuccess;
55}
56
57const char* UsbError::ToString() const {
58 switch (type_) {
59 case kSuccess:
60 return "Success";
61 case kErrorIO:
62 return "ErrorIO";
63 case kErrorInvalidParameter:
64 return "ErrorInvalidParameter";
65 case kErrorAccess:
66 return "ErrorAccess";
67 case kErrorNoDevice:
68 return "ErrorNoDevice";
69 case kErrorNotFound:
70 return "ErrorNotFound";
71 case kErrorBusy:
72 return "ErrorBusy";
73 case kErrorTimeout:
74 return "ErrorTimeout";
75 case kErrorOverflow:
76 return "ErrorOverflow";
77 case kErrorPipe:
78 return "ErrorPipe";
79 case kErrorInterrupted:
80 return "ErrorInterrupted";
81 case kErrorNoMemory:
82 return "ErrorNoMemory";
83 case kErrorNotSupported:
84 return "ErrorNotSupported";
85 case kErrorOther:
86 return "ErrorOther";
87 case kErrorDeviceNotOpen:
88 return "ErrorDeviceNotOpen";
89 case kErrorTransferAlreadyAllocated:
90 return "ErrorTransferAlreadyAllocated";
91 case kErrorTransferNotAllocated:
92 return "ErrorTransferNotAllocated";
Ben Chande490152013-05-29 17:05:49 -070093 case kErrorTransferAlreadySubmitted:
94 return "ErrorTransferAlreadySubmitted";
95 case kErrorTransferNotSubmitted:
96 return "ErrorTransferNotSubmitted";
97 case kErrorTransferBeingCancelled:
98 return "ErrorTransferBeingCancelled";
Ben Chanb6ce1782013-05-27 01:00:02 -070099 default:
100 return "Unknown";
101 }
102}
103
104void UsbError::Clear() {
105 type_ = kSuccess;
106}
107
Ben Chanf9759462013-05-27 21:50:30 -0700108bool UsbError::SetFromLibUsbError(libusb_error error) {
Ben Chanb6ce1782013-05-27 01:00:02 -0700109 type_ = ConvertFromLibUsbErrorToUsbErrorType(error);
Ben Chanf9759462013-05-27 21:50:30 -0700110 return type_ == kSuccess;
Ben Chanb6ce1782013-05-27 01:00:02 -0700111}
112
113} // namespace mist
114
Ben Chan9870f8b2019-06-06 17:31:35 -0700115std::ostream& operator<<(std::ostream& stream, const mist::UsbError& error) {
Ben Chanb6ce1782013-05-27 01:00:02 -0700116 stream << error.ToString();
117 return stream;
118}