Merge "Changed DllIport entry point for new system-settings API for callback."
[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                 WiFiManagerImpl.Instance.UpdateAP(_apHandle);
139             }
140         }
141
142         /// <summary>
143         /// The proxy type (IPv6).
144         /// </summary>
145         /// <since_tizen> 3 </since_tizen>
146         /// <value>Represents the proxy type of the Wi-Fi.</value>
147         /// <exception cref="NotSupportedException">Thrown while setting this property when Wi-Fi is not supported.</exception>
148         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
149         /// <exception cref="InvalidOperationException">Thrown while setting this value due to an invalid operation.</exception>
150         public WiFiProxyType ProxyType
151         {
152             get
153             {
154                 int type;
155                 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
156                 if (ret != (int)WiFiError.None)
157                 {
158                     Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
159                 }
160                 return (WiFiProxyType)type;
161             }
162             set
163             {
164                 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
165                 if (ret != (int)WiFiError.None)
166                 {
167                     Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
168                     WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
169                 }
170                 WiFiManagerImpl.Instance.UpdateAP(_apHandle);
171             }
172         }
173
174         /// <summary>
175         /// The frequency band (MHz).
176         /// </summary>
177         /// <since_tizen> 3 </since_tizen>
178         /// <value>Represents the frequency band value.</value>
179         public int Frequency
180         {
181             get
182             {
183                 int freq;
184                 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
185                 if (ret != (int)WiFiError.None)
186                 {
187                     Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
188                 }
189                 return freq;
190             }
191         }
192
193         /// <summary>
194         /// The received signal strength indicator (RSSI).
195         /// </summary>
196         /// <since_tizen> 3 </since_tizen>
197         /// <value>Represents RSSI of Wi-Fi (dbm).</value>
198         public int Rssi
199         {
200             get
201             {
202                 int rssi;
203                 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
204                 if (ret != (int)WiFiError.None)
205                 {
206                     Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
207                 }
208                 return rssi;
209             }
210         }
211
212         /// <summary>
213         /// The Received signal strength indication(RSSI).
214         /// </summary>
215         /// <since_tizen> 4 </since_tizen>
216         /// <value>Represents Rssi level of WiFi.</value>
217         /// <feature>http://tizen.org/feature/network.wifi</feature>
218         /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
219         public WiFiRssiLevel RssiLevel
220         {
221             get
222             {
223                 int rssi;
224                 int ret = Interop.WiFi.AP.GetRssiLevel(_apHandle, out rssi);
225                 if (ret != (int)WiFiError.None)
226                 {
227                     Log.Error(Globals.LogTag, "Failed to get rssi level, Error - " + (WiFiError)ret);
228                 }
229                 return (WiFiRssiLevel)rssi;
230             }
231         }
232
233         /// <summary>
234         /// The max speed (Mbps).
235         /// </summary>
236         /// <since_tizen> 3 </since_tizen>
237         /// <value>Represents the max speed value.</value>
238         public int MaxSpeed
239         {
240             get
241             {
242                 int maxSpeed;
243                 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
244                 if (ret != (int)WiFiError.None)
245                 {
246                     Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
247                 }
248                 return maxSpeed;
249             }
250         }
251
252         /// <summary>
253         /// A property to check whether the access point is a favorite or not.
254         /// </summary>
255         /// <since_tizen> 3 </since_tizen>
256         /// <value>Boolean value to check if the access point is a favorite or not.</value>
257         public bool IsFavorite
258         {
259             get
260             {
261                 bool isFavorite;
262                 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
263                 if (ret != (int)WiFiError.None)
264                 {
265                     Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
266                 }
267                 return isFavorite;
268             }
269         }
270
271         /// <summary>
272         /// A property to check whether the access point is a passpoint or not.
273         /// </summary>
274         /// <since_tizen> 3 </since_tizen>
275         /// <value>Boolean value to check if the access point is a passpoint or not.</value>
276         public bool IsPasspoint
277         {
278             get
279             {
280                 bool isPasspoint;
281                 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
282                 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
283                 if (ret != (int)WiFiError.None)
284                 {
285                     Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
286                 }
287                 return isPasspoint;
288             }
289         }
290
291         /// <summary>
292         /// The connection state.
293         /// </summary>
294         /// <since_tizen> 3 </since_tizen>
295         /// <value>Represents the connection state of the Wi-Fi.</value>
296         public WiFiConnectionState ConnectionState
297         {
298             get
299             {
300                 int state;
301                 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
302                 if (ret != (int)WiFiError.None)
303                 {
304                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
305                 }
306                 return (WiFiConnectionState)state;
307             }
308         }
309
310         /// <summary>
311         /// Gets all IPv6 addresses of the access point.
312         /// </summary>
313         /// <since_tizen> 3 </since_tizen>
314         /// <returns>A list of IPv6 addresses of the access point.</returns>
315         /// <feature>http://tizen.org/feature/network.wifi</feature>
316         /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
317         /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
318         /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
319         public IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses()
320         {
321             Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
322             List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
323             Interop.WiFi.HandleCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
324             {
325                 if (ipv6Address != IntPtr.Zero)
326                 {
327                     string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
328                     if (ipv6.Length == 0)
329                         ipList.Add(System.Net.IPAddress.Parse("::"));
330                     else
331                         ipList.Add(System.Net.IPAddress.Parse(ipv6));
332                     return true;
333                 }
334                 return false;
335             };
336
337             int ret = Interop.WiFi.AP.GetAllIPv6Addresses(_apHandle, callback, IntPtr.Zero);
338             if (ret != (int)WiFiError.None)
339             {
340                 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (WiFiError)ret);
341                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle.DangerousGetHandle());
342             }
343
344             return ipList;
345         }
346
347         internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
348         {
349             _apHandle = apHandle;
350             _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
351             _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
352
353             IntPtr strPtr;
354             int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
355             if (ret != (int)WiFiError.None)
356             {
357                 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
358             }
359             _essid = Marshal.PtrToStringAnsi(strPtr);
360         }
361     } //WiFiNetworkInformation
362 }