2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.CompilerServices;
21 internal static partial class Interop
23 internal enum ErrorCode
25 None = Tizen.Internals.Errors.ErrorCode.None,
26 IoError = Tizen.Internals.Errors.ErrorCode.IoError,
27 InvalidParameter = Tizen.Internals.Errors.ErrorCode.InvalidParameter,
28 PermissionDenied = Tizen.Internals.Errors.ErrorCode.PermissionDenied,
29 NoSuchDevice = Tizen.Internals.Errors.ErrorCode.NoSuchDevice,
30 ResourceBusy = Tizen.Internals.Errors.ErrorCode.ResourceBusy,
31 TimedOut = Tizen.Internals.Errors.ErrorCode.TimedOut,
32 BrokenPipe = Tizen.Internals.Errors.ErrorCode.BrokenPipe,
33 InterruptedSysCall = Tizen.Internals.Errors.ErrorCode.InterruptedSysCall,
34 OutOfMemory = Tizen.Internals.Errors.ErrorCode.OutOfMemory,
35 NotSupported = Tizen.Internals.Errors.ErrorCode.NotSupported,
36 Unknown = Tizen.Internals.Errors.ErrorCode.Unknown,
38 NotFound = -0x024B0000 | 0x01, // USB_HOST_ERROR_NOT_FOUND,
39 Overflow = -0x024B0000 | 0x02, // USB_HOST_ERROR_OVERFLOW
40 DeviceNotOpened = -0x024B0000 | 0x03, // USB_HOST_ERROR_DEVICE_NOT_OPENED
44 internal static class ErrorCodeExtensions
46 private const string LogTag = "Tizen.System.Usb";
48 internal static bool IsSuccess(this Interop.ErrorCode err)
50 return err == Interop.ErrorCode.None;
53 internal static bool IsFailed(this Interop.ErrorCode err)
55 return !err.IsSuccess();
59 /// Utility method to check for error, returns false if failed and print warning messages
61 /// <returns>true in case of no error, false otherwise</returns>
62 internal static bool WarnIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
66 Log.Debug(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
73 /// Utility method to check for error, returns false if failed and throw exception
75 /// <returns>true in case of no error</returns>
76 internal static bool ThrowIfFailed(this Interop.ErrorCode err, string msg, [CallerFilePath] string file = "", [CallerMemberName] string func = "", [CallerLineNumber] int line = 0)
80 Log.Error(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
81 throw err.GetException(msg);
86 internal static Exception GetException(this Interop.ErrorCode err, string message)
88 string errMessage = $"{message}, err: {err.ToString()}";
91 //case Interop.ErrorCode.None:
92 case Interop.ErrorCode.PermissionDenied:
93 return new UnauthorizedAccessException(errMessage);
94 case Interop.ErrorCode.InvalidParameter:
95 return new ArgumentException(errMessage);
96 case Interop.ErrorCode.TimedOut:
97 return new TimeoutException(errMessage);
98 case Interop.ErrorCode.OutOfMemory:
99 return new OutOfMemoryException(errMessage);
100 case Interop.ErrorCode.NotSupported:
101 return new NotSupportedException(errMessage);
103 case Interop.ErrorCode.IoError:
104 case Interop.ErrorCode.NoSuchDevice:
105 case Interop.ErrorCode.ResourceBusy:
106 case Interop.ErrorCode.BrokenPipe:
107 case Interop.ErrorCode.InterruptedSysCall:
108 case Interop.ErrorCode.Unknown:
110 case Interop.ErrorCode.NotFound:
111 case Interop.ErrorCode.Overflow:
112 case Interop.ErrorCode.DeviceNotOpened:
114 default: return new InvalidOperationException(errMessage);