bef2729f54362254840d4a3b3ae4fddd1c85f887
[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                         Interop.Libc.Free(strPtr);
58                     }
59                 }
60                 return _essid;
61             }
62         }
63
64         /// <summary>
65         /// The raw Service Set Identifier (SSID).
66         /// </summary>
67         /// <since_tizen> 5 </since_tizen>
68         /// <value>Raw SSID of the Wi-Fi.</value>
69         public byte[] RawSsid
70         {
71             get
72             {
73                 IntPtr ptr;
74                 byte[] rawSsid;
75                 int length;
76                 int ret = Interop.WiFi.AP.GetRawSsid(_apHandle, out ptr, out length);
77                 if (ret != (int)WiFiError.None || length == 0)
78                 {
79                     Log.Error(Globals.LogTag, "Failed to get raw essid, Error - " + (WiFiError)ret);
80                     rawSsid = null;
81                 }
82                 else
83                 {
84                     rawSsid = new byte[length];
85                     Marshal.Copy(ptr, rawSsid, 0, length);
86                     Interop.Libc.Free(ptr);
87                 }
88                 return rawSsid;
89             }
90         }
91
92         /// <summary>
93         /// The Basic Service Set Identifier (BSSID).
94         /// </summary>
95         /// <since_tizen> 3 </since_tizen>
96         /// <value>BSSID of the Wi-Fi.</value>
97         public string Bssid
98         {
99             get
100             {
101                 string bssid;
102                 IntPtr strPtr;
103                 int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
104                 if (ret != (int)WiFiError.None)
105                 {
106                     Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
107                     bssid = "";
108                 }
109                 else
110                 {
111                     bssid = Marshal.PtrToStringAnsi(strPtr);
112                 }
113                 return bssid;
114             }
115         }
116
117         /// <summary>
118         /// The address information for IPv4.
119         /// </summary>
120         /// <since_tizen> 3 </since_tizen>
121         /// <value>IP address information for IPv4 type.</value>
122         public IAddressInformation IPv4Setting
123         {
124             get
125             {
126                 return _ipv4;
127             }
128         }
129
130         /// <summary>
131         /// The address information for IPv6.
132         /// </summary>
133         /// <since_tizen> 3 </since_tizen>
134         /// <value>IP address information for IPv6 type.</value>
135         public IAddressInformation IPv6Setting
136         {
137             get
138             {
139                 return _ipv6;
140             }
141         }
142
143         /// <summary>
144         /// The proxy address.
145         /// </summary>
146         /// <since_tizen> 3 </since_tizen>
147         /// <value>Represents the proxy address of the Wi-Fi.</value>
148         /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
149         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
150         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
151         public string ProxyAddress
152         {
153             get
154             {
155                 string proxy;
156                 IntPtr strPtr;
157                 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
158                 if (ret != (int)WiFiError.None)
159                 {
160                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
161                     proxy = "";
162                 }
163                 else
164                 {
165                     proxy = Marshal.PtrToStringAnsi(strPtr);
166                 }
167                 return proxy;
168             }
169             set
170             {
171                 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
172                 if (ret != (int)WiFiError.None)
173                 {
174                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
175                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
176                 }
177             }
178         }
179
180         /// <summary>
181         /// The proxy type (IPv6).
182         /// </summary>
183         /// <since_tizen> 3 </since_tizen>
184         /// <value>Represents the proxy type of the Wi-Fi.</value>
185         /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
186         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
187         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
188         public WiFiProxyType ProxyType
189         {
190             get
191             {
192                 int type;
193                 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
194                 if (ret != (int)WiFiError.None)
195                 {
196                     Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
197                 }
198                 return (WiFiProxyType)type;
199             }
200             set
201             {
202                 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
203                 if (ret != (int)WiFiError.None)
204                 {
205                     Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
206                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
207                 }
208             }
209         }
210
211         /// <summary>
212         /// The frequency band (MHz).
213         /// </summary>
214         /// <since_tizen> 3 </since_tizen>
215         /// <value>Represents the frequency band value.</value>
216         public int Frequency
217         {
218             get
219             {
220                 int freq;
221                 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
222                 if (ret != (int)WiFiError.None)
223                 {
224                     Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
225                 }
226                 return freq;
227             }
228         }
229
230         /// <summary>
231         /// The received signal strength indicator (RSSI).
232         /// </summary>
233         /// <since_tizen> 3 </since_tizen>
234         /// <value>Represents RSSI of Wi-Fi (dbm).</value>
235         public int Rssi
236         {
237             get
238             {
239                 int rssi;
240                 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
241                 if (ret != (int)WiFiError.None)
242                 {
243                     Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
244                 }
245                 return rssi;
246             }
247         }
248
249         /// <summary>
250         /// The Received signal strength indication(RSSI).
251         /// </summary>
252         /// <since_tizen> 4 </since_tizen>
253         /// <value>Represents Rssi level of WiFi.</value>
254         /// <feature>http://tizen.org/feature/network.wifi</feature>
255         /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
256         public WiFiRssiLevel RssiLevel
257         {
258             get
259             {
260                 int rssi;
261                 int ret = Interop.WiFi.AP.GetRssiLevel(_apHandle, out rssi);
262                 if (ret != (int)WiFiError.None)
263                 {
264                     Log.Error(Globals.LogTag, "Failed to get rssi level, Error - " + (WiFiError)ret);
265                 }
266                 return (WiFiRssiLevel)rssi;
267             }
268         }
269
270         /// <summary>
271         /// The max speed (Mbps).
272         /// </summary>
273         /// <since_tizen> 3 </since_tizen>
274         /// <value>Represents the max speed value.</value>
275         public int MaxSpeed
276         {
277             get
278             {
279                 int maxSpeed;
280                 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
281                 if (ret != (int)WiFiError.None)
282                 {
283                     Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
284                 }
285                 return maxSpeed;
286             }
287         }
288
289         /// <summary>
290         /// A property to check whether the access point is a favorite or not.
291         /// </summary>
292         /// <since_tizen> 3 </since_tizen>
293         /// <value>Boolean value to check if the access point is a favorite or not.</value>
294         public bool IsFavorite
295         {
296             get
297             {
298                 bool isFavorite;
299                 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
300                 if (ret != (int)WiFiError.None)
301                 {
302                     Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
303                 }
304                 return isFavorite;
305             }
306         }
307
308         /// <summary>
309         /// A property to check whether the access point is a passpoint or not.
310         /// </summary>
311         /// <since_tizen> 3 </since_tizen>
312         /// <value>Boolean value to check if the access point is a passpoint or not.</value>
313         public bool IsPasspoint
314         {
315             get
316             {
317                 bool isPasspoint;
318                 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
319                 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
320                 if (ret != (int)WiFiError.None)
321                 {
322                     Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
323                 }
324                 return isPasspoint;
325             }
326         }
327
328         /// <summary>
329         /// The connection state.
330         /// </summary>
331         /// <since_tizen> 3 </since_tizen>
332         /// <value>Represents the connection state of the Wi-Fi.</value>
333         public WiFiConnectionState ConnectionState
334         {
335             get
336             {
337                 int state;
338                 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
339                 if (ret != (int)WiFiError.None)
340                 {
341                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
342                 }
343                 return (WiFiConnectionState)state;
344             }
345         }
346
347         /// <summary>
348         /// The raw country code
349         /// </summary>
350         /// <since_tizen> 5 </since_tizen>
351         /// <value>Represents the raw country code of the Wi-Fi.</value>
352         public string CountryCode
353         {
354             get
355             {
356                 string code;
357                 IntPtr strPtr;
358                 int ret = Interop.WiFi.AP.GetCountryCode(_apHandle, out strPtr);
359                 if (ret != (int)WiFiError.None)
360                 {
361                     Log.Error(Globals.LogTag, "Failed to get country code, Error - " + (WiFiError)ret);
362                     code = "";
363                 }
364                 else
365                 {
366                     code = Marshal.PtrToStringAnsi(strPtr);
367                     Interop.Libc.Free(strPtr);
368                 }
369                 return code;
370             }
371         }
372
373         /// <summary>
374         /// Gets all IPv6 addresses of the access point.
375         /// </summary>
376         /// <since_tizen> 3 </since_tizen>
377         /// <returns>A list of IPv6 addresses of the access point.</returns>
378         /// <feature>http://tizen.org/feature/network.wifi</feature>
379         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
380         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
381         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
382         public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
383         {
384             Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
385             List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
386             Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
387             {
388                 if (ipv6Address != IntPtr.Zero)
389                 {
390                     string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
391                     if (ipv6.Length == 0)
392                         ipList.Add(System.Net.IPAddress.Parse("::"));
393                     else
394                         ipList.Add(System.Net.IPAddress.Parse(ipv6));
395                     return true;
396                 }
397                 return false;
398             };
399
400             int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
401             if (ret != (int)WiFiError.None)
402             {
403                 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
404                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
405             }
406
407             return ipList;
408         }
409
410         /// <summary>
411         /// Gets the Bssid list
412         /// </summary>
413         /// <since_tizen> 5 </since_tizen>
414         /// <returns>A list of BSSIDs of access points with the same SSID as that of this access point.</returns>
415         /// <feature>http://tizen.org/feature/network.wifi</feature>
416         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
417         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
418         public IEnumerable<string> GetBssids()
419         {
420             Log.Debug(Globals.LogTag, "GetBssids");
421             List<string> bssidList = new List<string>();
422             Interop.WiFi.AP.FoundBssidCallback callback = (string bssid, int rssi, int freq, IntPtr userData) =>
423             {
424                 if (string.IsNullOrEmpty(bssid))
425                 {
426                     bssidList.Add(bssid);
427                     return true;
428                 }
429                 return false;
430             };
431
432             int ret = Interop.WiFi.AP.GetBssids(_apHandle, callback, IntPtr.Zero);
433             if (ret != (int)WiFiError.None)
434             {
435                 if (ret == (int)WiFiError.InvalidParameterError)
436                 {
437                     throw new InvalidOperationException("Invalid handle");
438                 }
439                 Log.Error(Globals.LogTag, "Failed to get BSSIDs, Error - " + (WiFiError)ret);
440                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
441             }
442
443             return bssidList;
444         }
445
446         internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
447         {
448             _apHandle = apHandle;
449             _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
450             _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
451
452             IntPtr strPtr;
453             int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
454             if (ret != (int)WiFiError.None)
455             {
456                 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
457                 _essid = "";
458             }
459             else
460             {
461                 _essid = Marshal.PtrToStringAnsi(strPtr);
462             }
463         }
464     } //WiFiNetworkInformation
465 }