[WiFi][TCSACR-155] Remove ArgumentException descriptions (#323)
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiEap.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.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using System.Threading.Tasks;
22 using System.Runtime.InteropServices;
23
24 namespace Tizen.Network.WiFi
25 {
26     /// <summary>
27     /// A class for managing the EAP information of the access point (AP).
28     /// </summary>
29     /// <since_tizen> 3 </since_tizen>
30     public class WiFiEap : IWiFiEap
31     {
32         private Interop.WiFi.SafeWiFiAPHandle _apHandle;
33
34         /// <summary>
35         /// The file path of CA Certificate of EAP.
36         /// </summary>
37         /// <since_tizen> 3 </since_tizen>
38         /// <value>CA certification file of EAP.</value>
39         /// <exception cref="NotSupportedException">Thrown while setting this value when Wi-Fi is not supported.</exception>
40         /// <exception cref="ArgumentNullException">Thrown while setting this value when the file value is null.</exception>
41         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
42         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
43         public string CaCertificationFile
44         {
45             get
46             {
47                 IntPtr strPtr;
48                 int ret = Interop.WiFi.AP.GetEapCaCertFile(_apHandle, out strPtr);
49                 if (ret != (int)WiFiError.None)
50                 {
51                     Log.Error(Globals.LogTag, "Failed to get caCertFile, Error - " + (WiFiError)ret);
52                     return "";
53                 }
54                 return Marshal.PtrToStringAnsi(strPtr);
55             }
56             set
57             {
58                 if (value == null)
59                 {
60                     throw new ArgumentNullException("File value is null");
61                 }
62                 int ret = Interop.WiFi.AP.SetEapCaCertFile(_apHandle, value);
63                 if (ret != (int)WiFiError.None)
64                 {
65                     Log.Error(Globals.LogTag, "Failed to set caCertFile, Error - " + (WiFiError)ret);
66                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
67                 }
68             }
69         }
70
71         /// <summary>
72         /// The EAP type of Wi-Fi.
73         /// </summary>
74         /// <since_tizen> 3 </since_tizen>
75         /// <value>Type of EAP.</value>
76         /// <exception cref="NotSupportedException">Thrown while setting this value when Wi-Fi is not supported.</exception>
77         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
78         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
79         public WiFiEapType EapType
80         {
81             get
82             {
83                 int type;
84                 int ret = Interop.WiFi.AP.GetEapType(_apHandle, out type);
85                 if (ret != (int)WiFiError.None)
86                 {
87                     Log.Error(Globals.LogTag, "Failed to get eap type, Error - " + (WiFiError)ret);
88                 }
89                 return (WiFiEapType)type;
90             }
91             set
92             {
93                 int ret = Interop.WiFi.AP.SetEapType(_apHandle, (int)value);
94                 if (ret != (int)WiFiError.None)
95                 {
96                     Log.Error(Globals.LogTag, "Failed to set eap type, Error - " + (WiFiError)ret);
97                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
98                 }
99             }
100         }
101
102         /// <summary>
103         /// The type of EAP phase 2 authentication of the Wi-Fi.
104         /// </summary>
105         /// <since_tizen> 3 </since_tizen>
106         /// <value>Authentication type of the Wi-Fi.</value>
107         /// <exception cref="NotSupportedException">Thrown while setting this value when Wi-Fi is not supported.</exception>
108         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
109         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
110         public WiFiAuthenticationType AuthenticationType
111         {
112             get
113             {
114                 int type;
115                 int ret = Interop.WiFi.AP.GetEapAuthType(_apHandle, out type);
116                 if (ret != (int)WiFiError.None)
117                 {
118                     Log.Error(Globals.LogTag, "Failed to get auth type, Error - " + (WiFiError)ret);
119                 }
120                 return (WiFiAuthenticationType)type;
121             }
122             set
123             {
124                 int ret = Interop.WiFi.AP.SetEapAuthType(_apHandle, (int)value);
125                 if (ret != (int)WiFiError.None)
126                 {
127                     Log.Error(Globals.LogTag, "Failed to set eap auth type, Error - " + (WiFiError)ret);
128                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
129                 }
130             }
131         }
132
133         internal WiFiEap(Interop.WiFi.SafeWiFiAPHandle apHandle)
134         {
135             _apHandle = apHandle;
136         }
137
138         /// <summary>
139         /// Gets the private key file of EAP.
140         /// </summary>
141         /// <since_tizen> 3 </since_tizen>
142         /// <returns>The file path of private key.</returns>
143         /// <feature>http://tizen.org/feature/network.wifi</feature>
144         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
145         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
146         /// <exception cref="ArgumentException">Thrown when the method fails due to an invalid parameter.</exception>
147         /// <exception cref="InvalidOperationException">Thrown when the method fails due to an invalid operation.</exception>
148         public string GetPrivateKeyFile()
149         {
150             IntPtr strPtr;
151             int ret = Interop.WiFi.AP.GetEapPrivateKeyFile(_apHandle, out strPtr);
152             if (ret != (int)WiFiError.None)
153             {
154                 Log.Error(Globals.LogTag, "Failed to get private key file, Error - " + (WiFiError)ret);
155                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
156             }
157             return Marshal.PtrToStringAnsi(strPtr);
158         }
159
160         /// <summary>
161         /// Sets the private key information of EAP.
162         /// </summary>
163         /// <since_tizen> 3 </since_tizen>
164         /// <param name="privateKeyFile">The file path of private key.</param>
165         /// <param name="password">The password.</param>
166         /// <feature>http://tizen.org/feature/network.wifi</feature>
167         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
168         /// <exception cref="ArgumentNullException">Thrown when the file path of private key is null.</exception>
169         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
170         /// <exception cref="InvalidOperationException">Thrown when the method failed due an to invalid operation.</exception>
171         public void SetPrivateKeyFile(string privateKeyFile, string password)
172         {
173             if (privateKeyFile == null)
174             {
175                 throw new ArgumentNullException("File path of private key is null");
176             }
177             int ret = Interop.WiFi.AP.SetEapPrivateKeyFile(_apHandle, privateKeyFile, password);
178             if (ret != (int)WiFiError.None)
179             {
180                 Log.Error(Globals.LogTag, "Failed to set private key file, Error - " + (WiFiError)ret);
181                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
182             }
183         }
184
185         /// <summary>
186         /// Gets the client certificate of EAP.
187         /// </summary>
188         /// <since_tizen> 3 </since_tizen>
189         /// <returns>The file path of client certificate.</returns>
190         /// <feature>http://tizen.org/feature/network.wifi</feature>
191         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
192         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
193         /// <exception cref="InvalidOperationException">Thrown when the method failed due an to invalid operation.</exception>
194         public string GetClientCertFile()
195         {
196             IntPtr strPtr;
197             int ret = Interop.WiFi.AP.GetEapClientCertFile(_apHandle, out strPtr);
198             if (ret != (int)WiFiError.None)
199             {
200                 Log.Error(Globals.LogTag, "Failed to get client cert file, Error - " + (WiFiError)ret);
201                 if (ret == (int)WiFiError.InvalidParameterError)
202                 {
203                     throw new InvalidOperationException("Invalid handle");
204                 }
205                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
206             }
207             return Marshal.PtrToStringAnsi(strPtr);
208         }
209
210         /// <summary>
211         /// Sets the CA certificate of EAP.
212         /// </summary>
213         /// <since_tizen> 3 </since_tizen>
214         /// <param name="clientCertFile">The file path of client certificate.</param>
215         /// <feature>http://tizen.org/feature/network.wifi</feature>
216         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
217         /// <exception cref="ArgumentNullException">Thrown when the file path of client certificate is null.</exception>
218         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
219         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
220         public void SetClientCertFile(string clientCertFile)
221         {
222             if (clientCertFile == null)
223             {
224                 throw new ArgumentNullException("File path of Client certificate is null");
225             }
226             int ret = Interop.WiFi.AP.SetEapClientCertFile(_apHandle, clientCertFile);
227             if (ret != (int)WiFiError.None)
228             {
229                 Log.Error(Globals.LogTag, "Failed to set client cert file, Error - " + (WiFiError)ret);
230                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
231             }
232         }
233
234         /// <summary>
235         /// Gets the username of EAP passphrase.
236         /// </summary>
237         /// <since_tizen> 3 </since_tizen>
238         /// <returns>The user name</returns>
239         /// <feature>http://tizen.org/feature/network.wifi</feature>
240         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
241         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
242         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
243         public string GetUserName()
244         {
245             IntPtr strptr;
246             bool passwordSet;
247             int ret = Interop.WiFi.AP.GetEapPassphrase(_apHandle, out strptr, out passwordSet);
248             if (ret != (int)WiFiError.None)
249             {
250                 Log.Error(Globals.LogTag, "Failed to get user name in eap passphrase, Error - " + (WiFiError)ret);
251                 if (ret == (int)WiFiError.InvalidParameterError)
252                 {
253                     throw new InvalidOperationException("Invalid handle");
254                 }
255                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
256             }
257             return Marshal.PtrToStringAnsi(strptr);
258         }
259
260         /// <summary>
261         /// Returns whether the password is set or not.
262         /// </summary>
263         /// <since_tizen> 3 </since_tizen>
264         /// <returns>True if password is set, false if password is not set.</returns>
265         /// <feature>http://tizen.org/feature/network.wifi</feature>
266         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
267         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
268         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
269         public bool IsPasswordSet()
270         {
271             IntPtr strptr;
272             bool passwordSet;
273             int ret = Interop.WiFi.AP.GetEapPassphrase(_apHandle, out strptr, out passwordSet);
274             if (ret != (int)WiFiError.None)
275             {
276                 Log.Error(Globals.LogTag, "Failed to get IsPasswordSet in passphrase, Error - " + (WiFiError)ret);
277                 if (ret == (int)WiFiError.InvalidParameterError)
278                 {
279                     throw new InvalidOperationException("Invalid handle");
280                 }
281                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
282             }
283             return passwordSet;
284         }
285
286         /// <summary>
287         /// Sets the user name of EAP.
288         /// </summary>
289         /// <since_tizen> 3 </since_tizen>
290         /// <param name="userName">The user name</param>
291         /// <feature>http://tizen.org/feature/network.wifi</feature>
292         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
293         /// <exception cref="ArgumentNullException">Thrown when the user name is passed as null.</exception>
294         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
295         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
296         public void SetUserName(string userName)
297         {
298             if (userName == null)
299             {
300                 throw new ArgumentNullException("User name is null");
301             }
302             int ret = Interop.WiFi.AP.SetEapPassphrase(_apHandle, userName, null);
303             if (ret != (int)WiFiError.None)
304             {
305                 Log.Error(Globals.LogTag, "Failed to set username, Error - " + (WiFiError)ret);
306                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
307             }
308         }
309
310         /// <summary>
311         /// Sets the password of EAP.
312         /// </summary>
313         /// <since_tizen> 3 </since_tizen>
314         /// <param name="password">The password</param>
315         /// <feature>http://tizen.org/feature/network.wifi</feature>
316         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
317         /// <exception cref="ArgumentNullException">Thrown when the password is passed as null.</exception>
318         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
319         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
320         public void SetPassword(string password)
321         {
322             if (password == null)
323             {
324                 throw new ArgumentNullException("Password is null");
325             }
326             int ret = Interop.WiFi.AP.SetEapPassphrase(_apHandle, null, password);
327             if (ret != (int)WiFiError.None)
328             {
329                 Log.Error(Globals.LogTag, "Failed to set password, Error - " + (WiFiError)ret);
330                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
331             }
332         }
333     } //WiFiEapInformation
334 }