Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiSecurity.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.Network.Connection;
19
20 namespace Tizen.Network.WiFi
21 {
22     /// <summary>
23     /// A class for managing the WiFi security information.
24     /// </summary>
25     /// <since_tizen> 3 </since_tizen>
26     public class WiFiSecurity
27     {
28         private Interop.WiFi.SafeWiFiAPHandle _apHandle;
29         private WiFiEap _eap;
30
31         /// <summary>
32         /// The type of Wi-Fi security.
33         /// </summary>
34         /// <since_tizen> 3 </since_tizen>
35         /// <value>Represents the security type of WiFi.</value>
36         /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
37         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
38         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
39         public WiFiSecurityType SecurityType
40         {
41             get
42             {
43                 int type;
44                 int ret = Interop.WiFi.AP.GetSecurityType(_apHandle, out type);
45                 if (ret != (int)WiFiError.None)
46                 {
47                     Log.Error(Globals.LogTag, "Failed to get security type, Error - " + (WiFiError)ret);
48                 }
49                 return (WiFiSecurityType)type;
50             }
51             set
52             {
53                 int ret = Interop.WiFi.AP.SetSecurityType(_apHandle, (int)value);
54                 if (ret != (int)WiFiError.None)
55                 {
56                     Log.Error(Globals.LogTag, "Failed to set security type, Error - " + (WiFiError)ret);
57                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
58                 }
59             }
60         }
61
62         /// <summary>
63         /// The type of Wi-Fi encryption
64         /// </summary>
65         /// <since_tizen> 3 </since_tizen>
66         /// <value>Represents the encryption type of WiFi.</value>
67         /// <exception cref="NotSupportedException">Thrown while setting this property 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 WiFiEncryptionType EncryptionType
71         {
72             get
73             {
74                 int type;
75                 int ret = Interop.WiFi.AP.GetEncryptionType(_apHandle, out type);
76                 if (ret != (int)WiFiError.None)
77                 {
78                     Log.Error(Globals.LogTag, "Failed to get encryption type, Error - " + (WiFiError)ret);
79                 }
80                 return (WiFiEncryptionType)type;
81             }
82             set
83             {
84                 int ret = Interop.WiFi.AP.SetEncryptionType(_apHandle, (int)value);
85                 if (ret != (int)WiFiError.None)
86                 {
87                     Log.Error(Globals.LogTag, "Failed to set encryption type, Error - " + (WiFiError)ret);
88                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
89                 }
90             }
91         }
92
93         /// <summary>
94         /// The EAP information
95         /// </summary>
96         /// <since_tizen> 3 </since_tizen>
97         /// <value>Eap information of WiFi.</value>
98         public WiFiEap EapInformation
99         {
100             get
101             {
102                 return _eap;
103             }
104         }
105
106         /// <summary>
107         /// A property to check whether the passphrase is required or not.
108         /// </summary>
109         /// <since_tizen> 3 </since_tizen>
110         /// <value>Boolean value to check if passphrase is required or not.</value>
111         public bool IsPassphraseRequired
112         {
113             get
114             {
115                 bool required;
116                 int ret = Interop.WiFi.AP.IsPassphraseRequired(_apHandle, out required);
117                 if (ret != (int)WiFiError.None)
118                 {
119                     Log.Error(Globals.LogTag, "Failed to get isPassportRequired, Error - " + (WiFiError)ret);
120                 }
121                 return required;
122             }
123         }
124
125         /// <summary>
126         /// A property to check whether the Wi-Fi Protected Setup(WPS) is supported or not.
127         /// </summary>
128         /// <since_tizen> 3 </since_tizen>
129         /// <value>Boolean value to check if wps is supported or not.</value>
130         public bool IsWpsSupported
131         {
132             get
133             {
134                 bool supported;
135                 int ret = Interop.WiFi.AP.IsWpsSupported(_apHandle, out supported);
136                 if (ret != (int)WiFiError.None)
137                 {
138                     Log.Error(Globals.LogTag, "Failed to get isWapSupported, Error - " + (WiFiError)ret);
139                 }
140                 return supported;
141             }
142         }
143
144         internal WiFiSecurity(Interop.WiFi.SafeWiFiAPHandle apHandle)
145         {
146             _apHandle = apHandle;
147             _eap = new WiFiEap(apHandle);
148         }
149
150         /// <summary>
151         /// Sets the passphrase.
152         /// </summary>
153         /// <since_tizen> 3 </since_tizen>
154         /// <param name="passphrase">The passphrase of the access point.</param>
155         /// <feature>http://tizen.org/feature/network.wifi</feature>
156         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
157         /// <exception cref="ArgumentNullException">Thrown when passphrase is passed as null.</exception>
158         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
159         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
160         public void SetPassphrase(string passphrase)
161         {
162             if (passphrase == null)
163             {
164                 throw new ArgumentNullException("Passphrase is null");
165             }
166             int ret = Interop.WiFi.AP.SetPassphrase(_apHandle, passphrase);
167             if (ret != (int)WiFiError.None)
168             {
169                 Log.Error(Globals.LogTag, "Failed to set passphrase, Error - " + (WiFiError)ret);
170                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
171             }
172         }
173     } //WiFiSecurityInformation
174 }