Merge remote-tracking branch 'origin/API8' into tizen_6.0
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiErrorFactory.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 Tizen.Internals.Errors;
19
20 namespace Tizen.Network.WiFi
21 {
22     static internal class WiFiErrorValue
23     {
24         internal const int Base = -0x01CE0000;
25     }
26
27     internal enum WiFiError
28     {
29         None = ErrorCode.None,
30         InvalidParameterError = ErrorCode.InvalidParameter,
31         OutOfMemoryError = ErrorCode.OutOfMemory,
32         InvalidOperationError = ErrorCode.InvalidOperation,
33         AddressFamilyNotSupportedError = ErrorCode.AddressFamilyNotSupported,
34         OperationFailedError = WiFiErrorValue.Base | 0x01,
35         NoConnectionError = WiFiErrorValue.Base | 0x02,
36         NowInProgressError = ErrorCode.NowInProgress,
37         AlreadyExistsError = WiFiErrorValue.Base | 0x03,
38         OperationAbortedError = WiFiErrorValue.Base | 0x04,
39         DhcpFailedError = WiFiErrorValue.Base | 0x05,
40         InvalidKeyError = WiFiErrorValue.Base | 0x06,
41         NoReplyError = WiFiErrorValue.Base | 0x07,
42         SecurityRestrictedError = WiFiErrorValue.Base | 0x08,
43         WpsTimeoutError = WiFiErrorValue.Base | 0x10,
44         PermissionDeniedError = ErrorCode.PermissionDenied,
45         NotSupportedError = ErrorCode.NotSupported
46     }
47
48     internal static class WiFiErrorFactory
49     {
50         static internal void ThrowWiFiException(int e, IntPtr handle)
51         {
52             ThrowException(e, (handle == IntPtr.Zero), false, "");
53         }
54
55         static internal void ThrowWiFiException(int e, IntPtr handle1, IntPtr handle2)
56         {
57             ThrowException(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), "");
58         }
59
60         static internal void ThrowWiFiException(int e, string message)
61         {
62             ThrowException(e, false, false, message);
63         }
64
65         static internal void ThrowWiFiException(int e, IntPtr handle, string message)
66         {
67             ThrowException(e, (handle == IntPtr.Zero), false, message);
68         }
69
70         static internal void ThrowWiFiException(int e, IntPtr handle1, IntPtr handle2, string message)
71         {
72             ThrowException(e, (handle1 == IntPtr.Zero), (handle2 == IntPtr.Zero), message);
73         }
74
75         // Used for return value of native API
76         static private void ThrowException(int e, bool isHandle1Null, bool isHandle2Null, string message)
77         {
78             WiFiError err = (WiFiError)e;
79             switch (err)
80             {
81                 case WiFiError.NotSupportedError:
82                     throw new NotSupportedException("Unsupported feature http://tizen.org/feature/network.wifi");
83                 case WiFiError.PermissionDeniedError:
84                     throw new UnauthorizedAccessException("Permission denied " + message);
85                 case WiFiError.SecurityRestrictedError:
86                     throw new UnauthorizedAccessException("Disabled by the device policy");
87                 case WiFiError.OutOfMemoryError:
88                     throw new OutOfMemoryException("Out of memory");
89                 case WiFiError.InvalidParameterError:
90                     if (isHandle1Null || isHandle2Null)
91                         throw new InvalidOperationException("Invalid instance (object may have been disposed or released)");
92                     throw new ArgumentException("Invalid parameter");
93                 case WiFiError.InvalidKeyError:
94                     if (isHandle1Null || isHandle2Null)
95                         throw new InvalidOperationException("Invalid instance (object may have been disposed or released)");
96                     throw new InvalidKeyException(message);
97                 case WiFiError.NowInProgressError:
98                     throw new NowInProgressException(message);
99                 default:
100                     throw new InvalidOperationException(err.ToString());
101             }
102         }
103
104         // Used in callback
105         static internal Exception GetException(int e, string message)
106         {
107             WiFiError err = (WiFiError)e;
108             switch (err)
109             {
110                 case WiFiError.NowInProgressError:
111                     return new NowInProgressException(message);
112                 case WiFiError.InvalidKeyError:
113                     return new InvalidKeyException(message);
114                 case WiFiError.SecurityRestrictedError:
115                     return new UnauthorizedAccessException("Disabled by the device policy");
116                 case WiFiError.WpsTimeoutError:
117                     return new TimeoutException("WPS connection is timed out");
118             }
119             return new InvalidOperationException(message + " " + err.ToString());
120         }
121     }
122 }