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