Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiEapConfiguration.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.InteropServices;
19
20 namespace Tizen.Network.WiFi
21 {
22     /// <summary>
23     /// A class for managing the EAP configuration.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class WiFiEapConfiguration : IWiFiEap
27     {
28         private Interop.WiFi.SafeWiFiConfigHandle _configHandle;
29
30         /// <summary>
31         /// The file path of CA Certificate of EAP.
32         /// </summary>
33         /// <since_tizen> 3 </since_tizen>
34         /// <value>CA certification file of EAP.</value>
35         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi is not supported.</exception>
36         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
37         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
38         public string CaCertificationFile
39         {
40             get
41             {
42                 IntPtr strPtr;
43                 int ret = Interop.WiFi.Config.GetEapCaCertFile(_configHandle, out strPtr);
44                 if (ret != (int)WiFiError.None)
45                 {
46                     Log.Error(Globals.LogTag, "Failed to get caCertFile Error - " + (WiFiError)ret);
47                     return "";
48                 }
49                 return Marshal.PtrToStringAnsi(strPtr);
50             }
51             set
52             {
53                 int ret = Interop.WiFi.Config.SetEapCaCertFile(_configHandle, value);
54                 if (ret != (int)WiFiError.None)
55                 {
56                     Log.Error(Globals.LogTag, "Failed to set caCertFile, Error - " + (WiFiError)ret);
57                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
58                 }
59             }
60         }
61
62         /// <summary>
63         /// The EAP type of wifi.
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         /// <value>Type of EAP.</value>
67         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi is not supported.</exception>
68         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
69         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
70         public WiFiEapType EapType
71         {
72             get
73             {
74                 int type;
75                 int ret = Interop.WiFi.Config.GetEapType(_configHandle, out type);
76                 if (ret != (int)WiFiError.None)
77                 {
78                     Log.Error(Globals.LogTag, "Failed to eap type Error - " + (WiFiError)ret);
79                 }
80                 return (WiFiEapType)type;
81             }
82             set
83             {
84                 int ret = Interop.WiFi.Config.SetEapType(_configHandle, (int)value);
85                 if (ret != (int)WiFiError.None)
86                 {
87                     Log.Error(Globals.LogTag, "Failed to set eap type, Error - " + (WiFiError)ret);
88                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
89                 }
90             }
91         }
92
93         /// <summary>
94         /// The type of EAP phase2 authentication of Wi-Fi.
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         /// <value>Authentication type of WiFi.</value>
98         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi is not supported.</exception>
99         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
100         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
101         public WiFiAuthenticationType AuthenticationType
102         {
103             get
104             {
105                 int type;
106                 int ret = Interop.WiFi.Config.GetEapAuthType(_configHandle, out type);
107                 if (ret != (int)WiFiError.None)
108                 {
109                     Log.Error(Globals.LogTag, "Failed to get auth type Error - " + (WiFiError)ret);
110                 }
111                 return (WiFiAuthenticationType)type;
112             }
113             set
114             {
115                 int ret = Interop.WiFi.Config.SetEapAuthType(_configHandle, (int)value);
116                 if (ret != (int)WiFiError.None)
117                 {
118                     Log.Error(Globals.LogTag, "Failed to set eap auth type, Error - " + (WiFiError)ret);
119                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
120                 }
121             }
122         }
123
124         /// <summary>
125         /// The anonymous identity of access point(AP).
126         /// </summary>
127         /// <since_tizen> 3 </since_tizen>
128         /// <value>Represents the anonymous identity of the access point.</value>
129         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi is not supported.</exception>
130         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
131         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
132         public string AnonymousIdentify
133         {
134             get
135             {
136                 IntPtr strPtr;
137                 int ret = Interop.WiFi.Config.GetEapAnonymousIdentity(_configHandle, out strPtr);
138                 if (ret != (int)WiFiError.None)
139                 {
140                     Log.Error(Globals.LogTag, "Failed to get anonymous identify Error - " + (WiFiError)ret);
141                     return "";
142                 }
143                 return Marshal.PtrToStringAnsi(strPtr);
144             }
145             set
146             {
147                 int ret = Interop.WiFi.Config.SetEapAnonymousIdentity(_configHandle, value);
148                 if (ret != (int)WiFiError.None)
149                 {
150                     Log.Error(Globals.LogTag, "Failed to set anonymous identify, Error - " + (WiFiError)ret);
151                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
152                 }
153             }
154         }
155
156         /// <summary>
157         /// The identity of access point(AP).
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         /// <value>Represents the identity of the access point.</value>
161         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi is not supported.</exception>
162         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
163         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
164         public string Identity
165         {
166             get
167             {
168                 IntPtr strPtr;
169                 int ret = Interop.WiFi.Config.GetEapIdentity(_configHandle, out strPtr);
170                 if (ret != (int)WiFiError.None)
171                 {
172                     Log.Error(Globals.LogTag, "Failed to get identify Error - " + (WiFiError)ret);
173                     return "";
174                 }
175                 return Marshal.PtrToStringAnsi(strPtr);
176             }
177             set
178             {
179                 int ret = Interop.WiFi.Config.SetEapIdentity(_configHandle, value);
180                 if (ret != (int)WiFiError.None)
181                 {
182                     Log.Error(Globals.LogTag, "Failed to set identify, Error - " + (WiFiError)ret);
183                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
184                 }
185             }
186         }
187
188         /// <summary>
189         /// The subject match of access point(AP).
190         /// </summary>
191         /// <since_tizen> 3 </since_tizen>
192         /// <value>Represents the subject match of AP.</value>
193         /// <exception cref="NotSupportedException">Thrown while setting this value when WiFi is not supported.</exception>
194         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
195         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
196         public string SubjectMatch
197         {
198             get
199             {
200                 IntPtr strPtr;
201                 int ret = Interop.WiFi.Config.GetEapSubjectMatch(_configHandle, out strPtr);
202                 if (ret != (int)WiFiError.None)
203                 {
204                     Log.Error(Globals.LogTag, "Failed to get subject match Error - " + (WiFiError)ret);
205                     return "";
206                 }
207                 return Marshal.PtrToStringAnsi(strPtr);
208             }
209             set
210             {
211                 int ret = Interop.WiFi.Config.SetEapSubjectMatch(_configHandle, value);
212                 if (ret != (int)WiFiError.None)
213                 {
214                     Log.Error(Globals.LogTag, "Failed to set subject match, Error - " + (WiFiError)ret);
215                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
216                 }
217             }
218         }
219
220         internal WiFiEapConfiguration(Interop.WiFi.SafeWiFiConfigHandle handle)
221         {
222             _configHandle = handle;
223         }
224
225         /// <summary>
226         /// Gets access point client cert file from configuration.
227         /// </summary>
228         /// <since_tizen> 3 </since_tizen>
229         /// <returns>The certification authority(CA) certificates file of access point.</returns>
230         /// <feature>http://tizen.org/feature/network.wifi</feature>
231         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
232         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
233         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
234         public string GetClientCertFile()
235         {
236             IntPtr strPtr;
237             int ret = Interop.WiFi.Config.GetEapClientCertFile(_configHandle, out strPtr);
238             if (ret != (int)WiFiError.None)
239             {
240                 Log.Error(Globals.LogTag, "Failed to get client cert file, Error - " + (WiFiError)ret);
241                 WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
242             }
243             return Marshal.PtrToStringAnsi(strPtr);
244         }
245
246         /// <summary>
247         /// Sets access point client cert file to configuration.
248         /// </summary>
249         /// <since_tizen> 3 </since_tizen>
250         /// <param name="privateKey">The private key file.</param>
251         /// <param name="clientCert">The certification authority(CA) certificates file of access point.</param>
252         /// <feature>http://tizen.org/feature/network.wifi</feature>
253         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
254         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
255         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
256         public void SetClientCertFile(string privateKey, string clientCert)
257         {
258             int ret = Interop.WiFi.Config.SetEapClientCertFile(_configHandle, privateKey, clientCert);
259             if (ret != (int)WiFiError.None)
260             {
261                 Log.Error(Globals.LogTag, "Failed to set client cert file, Error - " + (WiFiError)ret);
262                 WiFiErrorFactory.ThrowWiFiException(ret, _configHandle.DangerousGetHandle());
263             }
264         }
265     } //WiFiEapConfiguration
266 }