Release 4.0.0-preview1-00051
[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 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 WiFi is not supported.</exception>
40         /// <exception cref="ArgumentNullException">Thrown while setting this value when 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 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 wifi.
73         /// </summary>
74         /// <since_tizen> 3 </since_tizen>
75         /// <value>Type of EAP.</value>
76         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi 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 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 phase2 authentication of Wi-Fi.
104         /// </summary>
105         /// <since_tizen> 3 </since_tizen>
106         /// <value>Authentication type of WiFi.</value>
107         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi 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 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 WiFi is not supported.</exception>
145         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory. </exception>
146         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
147         /// <exception cref="InvalidOperationException">Thrown when the method failed due to 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 WiFi is not supported.</exception>
168         /// <exception cref="ArgumentNullException">Thrown when file path of private key is null.</exception>
169         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
170         /// <exception cref="InvalidOperationException">Thrown when the method failed due 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 WiFi is not supported.</exception>
192         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory. </exception>
193         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
194         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
195         public string GetClientCertFile()
196         {
197             IntPtr strPtr;
198             int ret = Interop.WiFi.AP.GetEapClientCertFile(_apHandle, out strPtr);
199             if (ret != (int)WiFiError.None)
200             {
201                 Log.Error(Globals.LogTag, "Failed to get client cert file, Error - " + (WiFiError)ret);
202                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
203             }
204             return Marshal.PtrToStringAnsi(strPtr);
205         }
206
207         /// <summary>
208         /// Sets the CA Certificate of EAP.
209         /// </summary>
210         /// <since_tizen> 3 </since_tizen>
211         /// <param name="clientCertFile">The file path of Client Certificate.</param>
212         /// <feature>http://tizen.org/feature/network.wifi</feature>
213         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
214         /// <exception cref="ArgumentNullException">Thrown when file path of Client Certificate is null.</exception>
215         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
216         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
217         public void SetClientCertFile(string clientCertFile)
218         {
219             if (clientCertFile == null)
220             {
221                 throw new ArgumentNullException("File path of Client certificate is null");
222             }
223             int ret = Interop.WiFi.AP.SetEapClientCertFile(_apHandle, clientCertFile);
224             if (ret != (int)WiFiError.None)
225             {
226                 Log.Error(Globals.LogTag, "Failed to set client cert file, Error - " + (WiFiError)ret);
227                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
228             }
229         }
230
231         /// <summary>
232         /// Gets the username of EAP passphrase.
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         /// <returns>The user name</returns>
236         /// <feature>http://tizen.org/feature/network.wifi</feature>
237         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
238         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory. </exception>
239         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
240         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
241         public string GetUserName()
242         {
243             IntPtr strptr;
244             bool passwordSet;
245             int ret = Interop.WiFi.AP.GetEapPassphrase(_apHandle, out strptr, out passwordSet);
246             if (ret != (int)WiFiError.None)
247             {
248                 Log.Error(Globals.LogTag, "Failed to get user name in eap passphrase, Error - " + (WiFiError)ret);
249                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
250             }
251             return Marshal.PtrToStringAnsi(strptr);
252         }
253
254         /// <summary>
255         /// Returns whether the password is set or not.
256         /// </summary>
257         /// <since_tizen> 3 </since_tizen>
258         /// <returns>True if password is set, false if password is not set.</returns>
259         /// <feature>http://tizen.org/feature/network.wifi</feature>
260         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
261         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory. </exception>
262         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
263         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
264         public bool IsPasswordSet()
265         {
266             IntPtr strptr;
267             bool passwordSet;
268             int ret = Interop.WiFi.AP.GetEapPassphrase(_apHandle, out strptr, out passwordSet);
269             if (ret != (int)WiFiError.None)
270             {
271                 Log.Error(Globals.LogTag, "Failed to get IsPasswordSet in passphrase, Error - " + (WiFiError)ret);
272                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
273             }
274             return passwordSet;
275         }
276
277         /// <summary>
278         /// Sets the user name of EAP.
279         /// </summary>
280         /// <since_tizen> 3 </since_tizen>
281         /// <param name="userName">The user name</param>
282         /// <feature>http://tizen.org/feature/network.wifi</feature>
283         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
284         /// <exception cref="ArgumentNullException">Thrown when the user name is passed as null. </exception>
285         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
286         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
287         public void SetUserName(string userName)
288         {
289             if (userName == null)
290             {
291                 throw new ArgumentNullException("User name is null");
292             }
293             int ret = Interop.WiFi.AP.SetEapPassphrase(_apHandle, userName, null);
294             if (ret != (int)WiFiError.None)
295             {
296                 Log.Error(Globals.LogTag, "Failed to set username, Error - " + (WiFiError)ret);
297                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
298             }
299         }
300
301         /// <summary>
302         /// Sets the password of EAP.
303         /// </summary>
304         /// <since_tizen> 3 </since_tizen>
305         /// <param name="password">The password</param>
306         /// <feature>http://tizen.org/feature/network.wifi</feature>
307         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
308         /// <exception cref="ArgumentNullException">Thrown when the password is passed as null. </exception>
309         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
310         /// <exception cref="InvalidOperationException">Thrown when the method failed due to invalid operation.</exception>
311         public void SetPassword(string password)
312         {
313             if (password == null)
314             {
315                 throw new ArgumentNullException("Password is null");
316             }
317             int ret = Interop.WiFi.AP.SetEapPassphrase(_apHandle, null, password);
318             if (ret != (int)WiFiError.None)
319             {
320                 Log.Error(Globals.LogTag, "Failed to set password, Error - " + (WiFiError)ret);
321                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
322             }
323         }
324     } //WiFiEapInformation
325 }