[WiFi] Fixes for design issues reported in confluence
[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 : IDisposable
29     {
30         private IntPtr _apHandle = IntPtr.Zero;
31         private IAddressInformation _ipv4;
32         private IAddressInformation _ipv6;
33         private bool disposed = false;
34         private string _essid;
35
36         /// <summary>
37         /// The Extended Service Set Identifier(ESSID).
38         /// </summary>
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         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
79         /// <summary>
80         /// The address informaiton for IPv4.
81         /// </summary>
82         public IAddressInformation IPv4Setting
83         {
84             get
85             {
86                 return _ipv4;
87             }
88         }
89
90         /// <summary>
91         /// The address ainformation for IPv6.
92         /// </summary>
93         public IAddressInformation IPv6Setting
94         {
95             get
96             {
97                 return _ipv6;
98             }
99         }
100
101         /// <summary>
102         /// The proxy address.
103         /// </summary>
104         public string ProxyAddress
105         {
106             get
107             {
108                 IntPtr strPtr;
109                 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
110                 if (ret != (int)WiFiError.None)
111                 {
112                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
113                     return "";
114                 }
115                 return Marshal.PtrToStringAnsi(strPtr);
116             }
117             set
118             {
119                 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
120                 if (ret != (int)WiFiError.None)
121                 {
122                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
123                 }
124             }
125         }
126
127         /// <summary>
128         /// The proxy type(IPv6).
129         /// </summary>
130         public WiFiProxyType ProxyType
131         {
132             get
133             {
134                 int type;
135                 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
136                 if (ret != (int)WiFiError.None)
137                 {
138                     Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
139                 }
140                 return (WiFiProxyType)type;
141             }
142             set
143             {
144                 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
145                 if (ret != (int)WiFiError.None)
146                 {
147                     Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
148                 }
149             }
150         }
151
152         /// <summary>
153         /// The frequency band(MHz).
154         /// </summary>
155         public int Frequency
156         {
157             get
158             {
159                 int freq;
160                 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
161                 if (ret != (int)WiFiError.None)
162                 {
163                     Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
164                 }
165                 return freq;
166             }
167         }
168
169         /// <summary>
170         /// The Received signal strength indication(RSSI).
171         /// </summary>
172         public WiFiRssiLevel Rssi
173         {
174             get
175             {
176                 int rssi;
177                 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
178                 if (ret != (int)WiFiError.None)
179                 {
180                     Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
181                 }
182                 return (WiFiRssiLevel)rssi;
183             }
184         }
185
186         /// <summary>
187         /// The max speed (Mbps).
188         /// </summary>
189         public int MaxSpeed
190         {
191             get
192             {
193                 int maxSpeed;
194                 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
195                 if (ret != (int)WiFiError.None)
196                 {
197                     Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
198                 }
199                 return maxSpeed;
200             }
201         }
202
203         /// <summary>
204         /// A property to check whether the access point is favorite or not.
205         /// </summary>
206         public bool IsFavorite
207         {
208             get
209             {
210                 bool isFavorite;
211                 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
212                 if (ret != (int)WiFiError.None)
213                 {
214                     Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
215                 }
216                 return isFavorite;
217             }
218         }
219
220         /// <summary>
221         /// A property to check whether the access point is passpoint or not.
222         /// </summary>
223         public bool IsPasspoint
224         {
225             get
226             {
227                 bool isPasspoint;
228                 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
229                 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
230                 if (ret != (int)WiFiError.None)
231                 {
232                     Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
233                 }
234                 return isPasspoint;
235             }
236         }
237
238         /// <summary>
239         /// The connection state.
240         /// </summary>
241         public WiFiConnectionState ConnectionState
242         {
243             get
244             {
245                 int state;
246                 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
247                 if (ret != (int)WiFiError.None)
248                 {
249                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
250                 }
251                 return (WiFiConnectionState)state;
252             }
253         }
254
255         internal WiFiNetwork(IntPtr apHandle)
256         {
257             _apHandle = apHandle;
258             _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
259             _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
260
261             IntPtr strPtr;
262             int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
263             if (ret != (int)WiFiError.None)
264             {
265                 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
266             }
267             _essid = Marshal.PtrToStringAnsi(strPtr);
268         }
269
270         ~WiFiNetwork()
271         {
272             Dispose(false);
273         }
274
275         /// <summary>
276         /// A method to destroy managed WiFiNetwork objects.
277         /// </summary>
278         public void Dispose()
279         {
280             Dispose(true);
281             GC.SuppressFinalize(this);
282         }
283
284         private void Dispose(bool disposing)
285         {
286             if (disposed)
287                 return;
288
289             if (disposing)
290             {
291                 _ipv4.Dispose();
292                 _ipv6.Dispose();
293             }
294             _apHandle = IntPtr.Zero;
295             disposed = true;
296         }
297
298     } //WiFiNetworkInformation
299 }