Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / WiFiProfile.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.Runtime.InteropServices;
22
23 namespace Tizen.Network.Connection
24 {
25     /// <summary>
26     /// This Class is WiFiProfile. It provides functions to manage the WiFi profile.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class WiFiProfile : ConnectionProfile
30     {
31         internal WiFiProfile(IntPtr Handle) : base(Handle)
32         {
33         }
34
35         ~WiFiProfile()
36         {
37         }
38
39         /// <summary>
40         /// The ESSID (Extended Service Set Identifier).
41         /// </summary>
42         /// <since_tizen> 3 </since_tizen>
43         /// <value>ESSID of the WiFi.</value>
44         public string Essid
45         {
46             get
47             {
48                 IntPtr value;
49                 int ret = Interop.ConnectionWiFiProfile.GetEssid(ProfileHandle, out value);
50                 if ((ConnectionError)ret != ConnectionError.None)
51                 {
52                     Log.Error(Globals.LogTag, "It failed to create profile handle, " + (ConnectionError)ret);
53                 }
54                 string result = Marshal.PtrToStringAnsi(value);
55                 Interop.Libc.Free(value);
56                 return result;
57             }
58         }
59
60         /// <summary>
61         /// The BSSID (Basic Service Set Identifier).
62         /// </summary>
63         /// <since_tizen> 3 </since_tizen>
64         /// <value>BSSID of the WiFi.</value>
65         public string Bssid
66         {
67             get
68             {
69                 IntPtr value;
70                 int ret = Interop.ConnectionWiFiProfile.GetBssid(ProfileHandle, out value);
71                 if ((ConnectionError)ret != ConnectionError.None)
72                 {
73                     Log.Error(Globals.LogTag, "It failed to get bssid, " + (ConnectionError)ret);
74                 }
75                 string result = Marshal.PtrToStringAnsi(value);
76                 Interop.Libc.Free(value);
77                 return result;
78             }
79         }
80
81         /// <summary>
82         /// The RSSI.
83         /// </summary>
84         /// <since_tizen> 3 </since_tizen>
85         /// <value>RSSI of the WiFi.</value>
86         public int Rssi
87         {
88             get
89             {
90                 int value;
91                 int ret = Interop.ConnectionWiFiProfile.GetRssi(ProfileHandle, out value);
92                 if ((ConnectionError)ret != ConnectionError.None)
93                 {
94                     Log.Error(Globals.LogTag, "It failed to get rssi, " + (ConnectionError)ret);
95                 }
96                 return value;
97             }
98         }
99
100         /// <summary>
101         /// The frequency (MHz).
102         /// </summary>
103         /// <since_tizen> 3 </since_tizen>
104         /// <value>Frequency of the WiFi.</value>
105         public int Frequency
106         {
107             get
108             {
109                 int value;
110                 int ret = Interop.ConnectionWiFiProfile.GetFrequency(ProfileHandle, out value);
111                 if ((ConnectionError)ret != ConnectionError.None)
112                 {
113                     Log.Error(Globals.LogTag, "It failed to get frequency, " + (ConnectionError)ret);
114                 }
115                 return value;
116             }
117         }
118
119         /// <summary>
120         /// The max speed (Mbps).
121         /// </summary>
122         /// <since_tizen> 3 </since_tizen>
123         /// <value>Maximum speed of the WiFi.</value>
124         public int MaxSpeed
125         {
126             get
127             {
128                 int value;
129                 int ret = Interop.ConnectionWiFiProfile.GetMaxSpeed(ProfileHandle, out value);
130                 if ((ConnectionError)ret != ConnectionError.None)
131                 {
132                     Log.Error(Globals.LogTag, "It failed to get max speed, " + (ConnectionError)ret);
133                 }
134                 return value;
135             }
136         }
137
138         /// <summary>
139         /// The security type of WiFi.
140         /// </summary>
141         /// <since_tizen> 3 </since_tizen>
142         /// <value>Security type of the WiFi.</value>
143         public WiFiSecurityType SecurityType
144         {
145             get
146             {
147                 int value;
148                 int ret = Interop.ConnectionWiFiProfile.GetSecurityType(ProfileHandle, out value);
149                 if ((ConnectionError)ret != ConnectionError.None)
150                 {
151                     Log.Error(Globals.LogTag, "It failed to get security type, " + (ConnectionError)ret);
152                 }
153                 return (WiFiSecurityType)value;
154             }
155         }
156
157         /// <summary>
158         /// The encryption type of WiFi.
159         /// </summary>
160         /// <since_tizen> 3 </since_tizen>
161         /// <value>Encryption mode of the WiFi.</value>
162         public WiFiEncryptionType EncryptionType
163         {
164             get
165             {
166                 int value;
167                 int ret = Interop.ConnectionWiFiProfile.GetEncryptionType(ProfileHandle, out value);
168                 if ((ConnectionError)ret != ConnectionError.None)
169                 {
170                     Log.Error(Globals.LogTag, "It failed to get encryption type, " + (ConnectionError)ret);
171                 }
172                 return (WiFiEncryptionType)value;
173             }
174         }
175
176         /// <summary>
177         /// Checks whether passphrase is required.
178         /// </summary>
179         /// <since_tizen> 3 </since_tizen>
180         /// <value>True if a passphrase is required, otherwise false.</value>
181         /// <remarks>This property is not valid if <c>WiFiSecurityType</c> is <c>Eap</c>.</remarks>
182         public bool PassphraseRequired
183         {
184             get
185             {
186                 bool value;
187                 int ret = Interop.ConnectionWiFiProfile.IsRequiredPassphrase(ProfileHandle, out value);
188                 if ((ConnectionError)ret != ConnectionError.None)
189                 {
190                     Log.Error(Globals.LogTag, "It failed to get PassphraseRequired, " + (ConnectionError)ret);
191                 }
192                 return value;
193             }
194         }
195
196         /// <summary>
197         /// Checks whether the WPS (Wi-Fi Protected Setup) is supported.
198         /// </summary>
199         /// <since_tizen> 3 </since_tizen>
200         /// <value>True if WPS is supported, otherwise false.</value>
201         public bool WpsSupported
202         {
203             get
204             {
205                 bool value;
206                 int ret = Interop.ConnectionWiFiProfile.IsSupportedWps(ProfileHandle, out value);
207                 if ((ConnectionError)ret != ConnectionError.None)
208                 {
209                     Log.Error(Globals.LogTag, "It failed to get IsSupportedWps, " + (ConnectionError)ret);
210                 }
211                 return value;
212             }
213         }
214
215         /// <summary>
216         /// Sets the passphrase of the Wi-Fi WPA.
217         /// </summary>
218         /// <since_tizen> 3 </since_tizen>
219         /// <param name="passphrase">The passphrase of Wi-Fi security</param>
220         /// <feature>http://tizen.org/feature/network.wifi</feature>
221         /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
222         /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
223         /// <exception cref="System.ArgumentNullException">Thrown when passphrase is null.</exception>
224         /// <exception cref="System.InvalidOperationException">Thrown when profile instance is invalid or when method failed due to invalid operation.</exception>
225         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
226         public void SetPassphrase(string passphrase)
227         {
228             CheckDisposed();
229             if (passphrase != null)
230             {
231                 int ret = Interop.ConnectionWiFiProfile.SetPassphrase(ProfileHandle, passphrase);
232                 if ((ConnectionError)ret != ConnectionError.None)
233                 {
234                     Log.Error(Globals.LogTag, "It failed to set passphrase, " + (ConnectionError)ret);
235                     ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi");
236                     ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
237                     ConnectionErrorFactory.ThrowConnectionException(ret);
238                 }
239             }
240
241             else
242             {
243                 throw new ArgumentNullException("Value of passphrase is null");
244             }
245         }
246     }
247 }