-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathusb_error.cc
118 lines (104 loc) · 3.16 KB
/
usb_error.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mist/usb_error.h"
namespace mist {
namespace {
UsbError::Type ConvertFromLibUsbErrorToUsbErrorType(libusb_error error) {
switch (error) {
case LIBUSB_SUCCESS:
return UsbError::kSuccess;
case LIBUSB_ERROR_IO:
return UsbError::kErrorIO;
case LIBUSB_ERROR_INVALID_PARAM:
return UsbError::kErrorInvalidParameter;
case LIBUSB_ERROR_ACCESS:
return UsbError::kErrorAccess;
case LIBUSB_ERROR_NO_DEVICE:
return UsbError::kErrorNoDevice;
case LIBUSB_ERROR_NOT_FOUND:
return UsbError::kErrorNotFound;
case LIBUSB_ERROR_BUSY:
return UsbError::kErrorBusy;
case LIBUSB_ERROR_TIMEOUT:
return UsbError::kErrorTimeout;
case LIBUSB_ERROR_OVERFLOW:
return UsbError::kErrorOverflow;
case LIBUSB_ERROR_PIPE:
return UsbError::kErrorPipe;
case LIBUSB_ERROR_INTERRUPTED:
return UsbError::kErrorInterrupted;
case LIBUSB_ERROR_NO_MEM:
return UsbError::kErrorNoMemory;
case LIBUSB_ERROR_NOT_SUPPORTED:
return UsbError::kErrorNotSupported;
default:
return UsbError::kErrorOther;
}
}
} // namespace
UsbError::UsbError() : type_(kSuccess) {}
UsbError::UsbError(Type type) : type_(type) {}
UsbError::UsbError(libusb_error error)
: type_(ConvertFromLibUsbErrorToUsbErrorType(error)) {}
bool UsbError::IsSuccess() const {
return type_ == kSuccess;
}
const char* UsbError::ToString() const {
switch (type_) {
case kSuccess:
return "Success";
case kErrorIO:
return "ErrorIO";
case kErrorInvalidParameter:
return "ErrorInvalidParameter";
case kErrorAccess:
return "ErrorAccess";
case kErrorNoDevice:
return "ErrorNoDevice";
case kErrorNotFound:
return "ErrorNotFound";
case kErrorBusy:
return "ErrorBusy";
case kErrorTimeout:
return "ErrorTimeout";
case kErrorOverflow:
return "ErrorOverflow";
case kErrorPipe:
return "ErrorPipe";
case kErrorInterrupted:
return "ErrorInterrupted";
case kErrorNoMemory:
return "ErrorNoMemory";
case kErrorNotSupported:
return "ErrorNotSupported";
case kErrorOther:
return "ErrorOther";
case kErrorDeviceNotOpen:
return "ErrorDeviceNotOpen";
case kErrorTransferAlreadyAllocated:
return "ErrorTransferAlreadyAllocated";
case kErrorTransferNotAllocated:
return "ErrorTransferNotAllocated";
case kErrorTransferAlreadySubmitted:
return "ErrorTransferAlreadySubmitted";
case kErrorTransferNotSubmitted:
return "ErrorTransferNotSubmitted";
case kErrorTransferBeingCancelled:
return "ErrorTransferBeingCancelled";
default:
return "Unknown";
}
}
void UsbError::Clear() {
type_ = kSuccess;
}
bool UsbError::SetFromLibUsbError(libusb_error error) {
type_ = ConvertFromLibUsbErrorToUsbErrorType(error);
return type_ == kSuccess;
}
} // namespace mist
std::ostream& operator<<(std::ostream& stream, const mist::UsbError& error) {
stream << error.ToString();
return stream;
}