Merge "[WebView] Update API reference"
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiNetwork.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.Net;
20 using System.Runtime.InteropServices;
21 using Tizen.Network.Connection;
22
23 namespace Tizen.Network.WiFi
24 {
25     /// <summary>
26     /// A class for managing the Wi-Fi network information.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class WiFiNetwork
30     {
31         private Interop.WiFi.SafeWiFiAPHandle _apHandle;
32         private IAddressInformation _ipv4;
33         private IAddressInformation _ipv6;
34         private string _essid;
35
36         /// <summary>
37         /// The Extended Service Set Identifier (ESSID).
38         /// </summary>
39         /// <since_tizen> 3 </since_tizen>
40         /// <value>ESSID of the Wi-Fi.</value>
41         public string Essid
42         {
43             get
44             {
45                 if (string.IsNullOrEmpty(_essid))
46                 {
47                     IntPtr strPtr;
48                     int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
49                     if (ret != (int)WiFiError.None)
50                     {
51                         Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
52                         _essid = "";
53                     }
54                     else
55                     {
56                         _essid = Marshal.PtrToStringAnsi(strPtr);
57                     }
58                 }
59                 return _essid;
60             }
61         }
62
63         /// <summary>
64         /// The Basic Service Set Identifier (BSSID).
65         /// </summary>
66         /// <since_tizen> 3 </since_tizen>
67         /// <value>BSSID of the Wi-Fi.</value>
68         public string Bssid
69         {
70             get
71             {
72                 IntPtr strPtr;
73                 int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
74                 if (ret != (int)WiFiError.None)
75                 {
76                     Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
77                     return "";
78                 }
79                 return Marshal.PtrToStringAnsi(strPtr);
80             }
81         }
82
83         /// <summary>
84         /// The address information for IPv4.
85         /// </summary>
86         /// <since_tizen> 3 </since_tizen>
87         /// <value>IP address information for IPv4 type.</value>
88         public IAddressInformation IPv4Setting
89         {
90             get
91             {
92                 return _ipv4;
93             }
94         }
95
96         /// <summary>
97         /// The address information for IPv6.
98         /// </summary>
99         /// <since_tizen> 3 </since_tizen>
100         /// <value>IP address information for IPv6 type.</value>
101         public IAddressInformation IPv6Setting
102         {
103             get
104             {
105                 return _ipv6;
106             }
107         }
108
109         /// <summary>
110         /// The proxy address.
111         /// </summary>
112         /// <since_tizen> 3 </since_tizen>
113         /// <value>Represents the proxy address of the Wi-Fi.</value>
114         /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
115         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
116         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
117         public string ProxyAddress
118         {
119             get
120             {
121                 IntPtr strPtr;
122                 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
123                 if (ret != (int)WiFiError.None)
124                 {
125                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
126                     return "";
127                 }
128                 return Marshal.PtrToStringAnsi(strPtr);
129             }
130             set
131             {
132                 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
133                 if (ret != (int)WiFiError.None)
134                 {
135                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
136                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
137                 }
138             }
139         }
140
141         /// <summary>
142         /// The proxy type (IPv6).
143         /// </summary>
144         /// <since_tizen> 3 </since_tizen>
145         /// <value>Represents the proxy type of the Wi-Fi.</value>
146         /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
147         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
148         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
149         public WiFiProxyType ProxyType
150         {
151             get
152             {
153                 int type;
154                 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
155                 if (ret != (int)WiFiError.None)
156                 {
157                     Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
158                 }
159                 return (WiFiProxyType)type;
160             }
161             set
162             {
163                 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
164                 if (ret != (int)WiFiError.None)
165                 {
166                     Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
167                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
168                 }
169             }
170         }
171
172         /// <summary>
173         /// The frequency band (MHz).
174         /// </summary>
175         /// <since_tizen> 3 </since_tizen>
176         /// <value>Represents the frequency band value.</value>
177         public int Frequency
178         {
179             get
180             {
181                 int freq;
182                 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
183                 if (ret != (int)WiFiError.None)
184                 {
185                     Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
186                 }
187                 return freq;
188             }
189         }
190
191         /// <summary>
192         /// The received signal strength indicator (RSSI).
193         /// </summary>
194         /// <since_tizen> 3 </since_tizen>
195         /// <value>Represents RSSI of Wi-Fi (dbm).</value>
196         public int Rssi
197         {
198             get
199             {
200                 int rssi;
201                 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
202                 if (ret != (int)WiFiError.None)
203                 {
204                     Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
205                 }
206                 return rssi;
207             }
208         }
209
210         /// <summary>
211         /// The Received signal strength indication(RSSI).
212         /// </summary>
213         /// <since_tizen> 4 </since_tizen>
214         /// <value>Represents Rssi level of WiFi.</value>
215         /// <feature>http://tizen.org/feature/network.wifi</feature>
216         /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
217         public WiFiRssiLevel RssiLevel
218         {
219             get
220             {
221                 int rssi;
222                 int ret = Interop.WiFi.AP.GetRssiLevel(_apHandle, out rssi);
223                 if (ret != (int)WiFiError.None)
224                 {
225                     Log.Error(Globals.LogTag, "Failed to get rssi level, Error - " + (WiFiError)ret);
226                 }
227                 return (WiFiRssiLevel)rssi;
228             }
229         }
230
231         /// <summary>
232         /// The max speed (Mbps).
233         /// </summary>
234         /// <since_tizen> 3 </since_tizen>
235         /// <value>Represents the max speed value.</value>
236         public int MaxSpeed
237         {
238             get
239             {
240                 int maxSpeed;
241                 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
242                 if (ret != (int)WiFiError.None)
243                 {
244                     Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
245                 }
246                 return maxSpeed;
247             }
248         }
249
250         /// <summary>
251         /// A property to check whether the access point is a favorite or not.
252         /// </summary>
253         /// <since_tizen> 3 </since_tizen>
254         /// <value>Boolean value to check if the access point is a favorite or not.</value>
255         public bool IsFavorite
256         {
257             get
258             {
259                 bool isFavorite;
260                 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
261                 if (ret != (int)WiFiError.None)
262                 {
263                     Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
264                 }
265                 return isFavorite;
266             }
267         }
268
269         /// <summary>
270         /// A property to check whether the access point is a passpoint or not.
271         /// </summary>
272         /// <since_tizen> 3 </since_tizen>
273         /// <value>Boolean value to check if the access point is a passpoint or not.</value>
274         public bool IsPasspoint
275         {
276             get
277             {
278                 bool isPasspoint;
279                 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
280                 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
281                 if (ret != (int)WiFiError.None)
282                 {
283                     Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
284                 }
285                 return isPasspoint;
286             }
287         }
288
289         /// <summary>
290         /// The connection state.
291         /// </summary>
292         /// <since_tizen> 3 </since_tizen>
293         /// <value>Represents the connection state of the Wi-Fi.</value>
294         public WiFiConnectionState ConnectionState
295         {
296             get
297             {
298                 int state;
299                 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
300                 if (ret != (int)WiFiError.None)
301                 {
302                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
303                 }
304                 return (WiFiConnectionState)state;
305             }
306         }
307
308         /// <summary>
309         /// Gets all IPv6 addresses of the access point.
310         /// </summary>
311         /// <since_tizen> 3 </since_tizen>
312         /// <returns>A list of IPv6 addresses of the access point.</returns>
313         /// <feature>http://tizen.org/feature/network.wifi</feature>
314         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
315         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
316         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
317         public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
318         {
319             Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
320             List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
321             Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
322             {
323                 if (ipv6Address != IntPtr.Zero)
324                 {
325                     string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
326                     if (ipv6.Length == 0)
327                         ipList.Add(System.Net.IPAddress.Parse("::"));
328                     else
329                         ipList.Add(System.Net.IPAddress.Parse(ipv6));
330                     return true;
331                 }
332                 return false;
333             };
334
335             int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
336             if (ret != (int)WiFiError.None)
337             {
338                 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
339                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
340             }
341
342             return ipList;
343         }
344
345         internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
346         {
347             _apHandle = apHandle;
348             _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
349             _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
350
351             IntPtr strPtr;
352             int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
353             if (ret != (int)WiFiError.None)
354             {
355                 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
356             }
357             _essid = Marshal.PtrToStringAnsi(strPtr);
358         }
359     } //WiFiNetworkInformation
360 }