[TCSACR-93] Add USB Host C# API
[platform/core/csapi/tizenfx.git] / src / Tizen.System.Usb / Interop / Interop.ErrorCode.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Runtime.CompilerServices;
19 using Tizen;
20
21 internal static partial class Interop
22 {
23     internal enum ErrorCode
24     {
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,
37
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
41     }
42 }
43
44 internal static class ErrorCodeExtensions
45 {
46     private const string LogTag = "Tizen.System.Usb";
47
48     internal static bool IsSuccess(this Interop.ErrorCode err)
49     {
50         return err == Interop.ErrorCode.None;
51     }
52
53     internal static bool IsFailed(this Interop.ErrorCode err)
54     {
55         return !err.IsSuccess();
56     }
57
58     /// <summary>
59     /// Utility method to check for error, returns false if failed and print warning messages
60     /// </summary>
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)
63     {
64         if (err.IsFailed())
65         {
66             Log.Debug(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
67             return false;
68         }
69         return true;
70     }
71
72     /// <summary>
73     /// Utility method to check for error, returns false if failed and throw exception
74     /// </summary>
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)
77     {
78         if (err.IsFailed())
79         {
80             Log.Error(LogTag, $"{msg}, err: {err.ToString()}", file, func, line);
81             throw err.GetException(msg);
82         }
83         return true;
84     }
85
86     internal static Exception GetException(this Interop.ErrorCode err, string message)
87     {
88         string errMessage = $"{message}, err: {err.ToString()}";
89         switch (err)
90         {
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);
102
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:
109
110             case Interop.ErrorCode.NotFound:
111             case Interop.ErrorCode.Overflow:
112             case Interop.ErrorCode.DeviceNotOpened:
113
114             default: return new InvalidOperationException(errMessage);
115         }
116     }
117 }