7e94e37091f5177b4255c6d1010439695723af47
[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 information. It allows applications to manager network information.
27     /// This class is not intended to create instance directly from applications.
28     /// </summary>
29     public class WiFiNetwork : IDisposable
30     {
31         private IntPtr _apHandle = IntPtr.Zero;
32         private IAddressInformation _ipv4;
33         private IAddressInformation _ipv6;
34         private bool disposed = false;
35         private string _essid;
36
37         /// <summary>
38         /// The Extended Service Set Identifier(ESSID).
39         /// </summary>
40         public string Essid
41         {
42             get
43             {
44                 if (string.IsNullOrEmpty(_essid))
45                 {
46                     IntPtr strPtr;
47                     int ret = Interop.WiFi.Ap.GetEssid(_apHandle, out strPtr);
48                     if (ret != (int)WiFiError.None)
49                     {
50                         Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
51                         _essid = "";
52                     }
53                     else
54                     {
55                         _essid = Marshal.PtrToStringAnsi(strPtr);
56                     }
57                 }
58                 return _essid;
59             }
60         }
61         /// <summary>
62         /// The Basic Service Set Identifier(BSSID).
63         /// </summary>
64         public string Bssid
65         {
66             get
67             {
68                 IntPtr strPtr;
69                 int ret = Interop.WiFi.Ap.GetBssid(_apHandle, out strPtr);
70                 if (ret != (int)WiFiError.None)
71                 {
72                     Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
73                     return "";
74                 }
75                 return Marshal.PtrToStringAnsi(strPtr);
76             }
77         }
78         /// <summary>
79         /// The address informaiton for Ipv4.
80         /// </summary>
81         public IAddressInformation Ipv4Setting
82         {
83             get
84             {
85                 return _ipv4;
86             }
87         }
88         /// <summary>
89         /// The address ainformation for Ipv6.
90         /// </summary>
91         public IAddressInformation Ipv6Setting
92         {
93             get
94             {
95                 return _ipv6;
96             }
97         }
98         /// <summary>
99         /// The proxy address.
100         /// </summary>
101         public string ProxyAddress
102         {
103             get
104             {
105                 IntPtr strPtr;
106                 int ret = Interop.WiFi.Ap.GetProxyAddress(_apHandle, (int)AddressFamily.Ipv4, out strPtr);
107                 if (ret != (int)WiFiError.None)
108                 {
109                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
110                     return "";
111                 }
112                 return Marshal.PtrToStringAnsi(strPtr);
113             }
114             set
115             {
116                 int ret = Interop.WiFi.Ap.SetProxyAddress(_apHandle, (int)AddressFamily.Ipv4, value);
117                 if (ret != (int)WiFiError.None)
118                 {
119                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
120                 }
121             }
122         }
123         /// <summary>
124         /// The proxy type(Ipv6).
125         /// </summary>
126         public WiFiProxyType ProxyType
127         {
128             get
129             {
130                 int type;
131                 int ret = Interop.WiFi.Ap.GetProxyType(_apHandle, out type);
132                 if (ret != (int)WiFiError.None)
133                 {
134                     Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
135                 }
136                 return (WiFiProxyType)type;
137             }
138             set
139             {
140                 int ret = Interop.WiFi.Ap.SetProxyType(_apHandle, (int)value);
141                 if (ret != (int)WiFiError.None)
142                 {
143                     Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
144                 }
145             }
146         }
147         /// <summary>
148         /// The frequency band(MHz).
149         /// </summary>
150         public int Frequency
151         {
152             get
153             {
154                 int freq;
155                 int ret = Interop.WiFi.Ap.GetFrequency(_apHandle, out freq);
156                 if (ret != (int)WiFiError.None)
157                 {
158                     Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
159                 }
160                 return freq;
161             }
162         }
163         /// <summary>
164         /// The Received signal strength indication(RSSI).
165         /// </summary>
166         public int Rssi
167         {
168             get
169             {
170                 int rssi;
171                 int ret = Interop.WiFi.Ap.GetRssi(_apHandle, out rssi);
172                 if (ret != (int)WiFiError.None)
173                 {
174                     Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
175                 }
176                 return rssi;
177             }
178         }
179         /// <summary>
180         /// Rhe max speed (Mbps).
181         /// </summary>
182         public int MaxSpeed
183         {
184             get
185             {
186                 int maxSpeed;
187                 int ret = Interop.WiFi.Ap.GetMaxSpeed(_apHandle, out maxSpeed);
188                 if (ret != (int)WiFiError.None)
189                 {
190                     Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
191                 }
192                 return maxSpeed;
193             }
194         }
195         /// <summary>
196         /// A property to check whether the access point is favorite or not.
197         /// </summary>
198         public bool IsFavorite
199         {
200             get
201             {
202                 bool isFavorite;
203                 int ret = Interop.WiFi.Ap.IsFavorite(_apHandle, out isFavorite);
204                 if (ret != (int)WiFiError.None)
205                 {
206                     Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
207                 }
208                 return isFavorite;
209             }
210         }
211         /// <summary>
212         /// A property to check whether the access point is passpoint or not.
213         /// </summary>
214         public bool IsPasspoint
215         {
216             get
217             {
218                 bool isPasspoint;
219                 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
220                 int ret = Interop.WiFi.Ap.IsPasspoint(_apHandle, out isPasspoint);
221                 if (ret != (int)WiFiError.None)
222                 {
223                     Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
224                 }
225                 return isPasspoint;
226             }
227         }
228         /// <summary>
229         /// The connection state.
230         /// </summary>
231         public WiFiConnectionState ConnectionState
232         {
233             get
234             {
235                 int state;
236                 int ret = Interop.WiFi.Ap.GetConnectionState(_apHandle, out state);
237                 if (ret != (int)WiFiError.None)
238                 {
239                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
240                 }
241                 return (WiFiConnectionState)state;
242             }
243         }
244
245         internal WiFiNetwork(IntPtr apHandle)
246         {
247             _apHandle = apHandle;
248             _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.Ipv4);
249             _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.Ipv6);
250
251             IntPtr strPtr;
252             int ret = Interop.WiFi.Ap.GetEssid(_apHandle, out strPtr);
253             if (ret != (int)WiFiError.None)
254             {
255                 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
256             }
257             _essid = Marshal.PtrToStringAnsi(strPtr);
258         }
259
260         ~WiFiNetwork()
261         {
262             Dispose(false);
263         }
264
265         public void Dispose()
266         {
267             Dispose(true);
268             GC.SuppressFinalize(this);
269         }
270
271         private void Dispose(bool disposing)
272         {
273             if (disposed)
274                 return;
275
276             if (disposing)
277             {
278                 _ipv4.Dispose();
279                 _ipv6.Dispose();
280             }
281             _apHandle = IntPtr.Zero;
282             disposed = true;
283         }
284
285     } //WiFiNetworkInformation
286 }