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