[WiFi] Modified the framework code to use SafeHandle for CAPI handles.
[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         public string Essid
39         {
40             get
41             {
42                 if (string.IsNullOrEmpty(_essid))
43                 {
44                     IntPtr strPtr;
45                     int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
46                     if (ret != (int)WiFiError.None)
47                     {
48                         Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
49                         _essid = "";
50                     }
51                     else
52                     {
53                         _essid = Marshal.PtrToStringAnsi(strPtr);
54                     }
55                 }
56                 return _essid;
57             }
58         }
59
60         /// <summary>
61         /// The Basic Service Set Identifier(BSSID).
62         /// </summary>
63         public string Bssid
64         {
65             get
66             {
67                 IntPtr strPtr;
68                 int ret = Interop.WiFi.AP.GetBssid(_apHandle, out strPtr);
69                 if (ret != (int)WiFiError.None)
70                 {
71                     Log.Error(Globals.LogTag, "Failed to get bssid, Error - " + (WiFiError)ret);
72                     return "";
73                 }
74                 return Marshal.PtrToStringAnsi(strPtr);
75             }
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
89         /// <summary>
90         /// The address ainformation for IPv6.
91         /// </summary>
92         public IAddressInformation IPv6Setting
93         {
94             get
95             {
96                 return _ipv6;
97             }
98         }
99
100         /// <summary>
101         /// The proxy address.
102         /// </summary>
103         public string ProxyAddress
104         {
105             get
106             {
107                 IntPtr strPtr;
108                 int ret = Interop.WiFi.AP.GetProxyAddress(_apHandle, (int)AddressFamily.IPv4, out strPtr);
109                 if (ret != (int)WiFiError.None)
110                 {
111                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
112                     return "";
113                 }
114                 return Marshal.PtrToStringAnsi(strPtr);
115             }
116             set
117             {
118                 int ret = Interop.WiFi.AP.SetProxyAddress(_apHandle, (int)AddressFamily.IPv4, value);
119                 if (ret != (int)WiFiError.None)
120                 {
121                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
122                 }
123             }
124         }
125
126         /// <summary>
127         /// The proxy type(IPv6).
128         /// </summary>
129         public WiFiProxyType ProxyType
130         {
131             get
132             {
133                 int type;
134                 int ret = Interop.WiFi.AP.GetProxyType(_apHandle, out type);
135                 if (ret != (int)WiFiError.None)
136                 {
137                     Log.Error(Globals.LogTag, "Failed to get proxy type, Error - " + (WiFiError)ret);
138                 }
139                 return (WiFiProxyType)type;
140             }
141             set
142             {
143                 int ret = Interop.WiFi.AP.SetProxyType(_apHandle, (int)value);
144                 if (ret != (int)WiFiError.None)
145                 {
146                     Log.Error(Globals.LogTag, "Failed to set proxy type, Error - " + (WiFiError)ret);
147                 }
148             }
149         }
150
151         /// <summary>
152         /// The frequency band(MHz).
153         /// </summary>
154         public int Frequency
155         {
156             get
157             {
158                 int freq;
159                 int ret = Interop.WiFi.AP.GetFrequency(_apHandle, out freq);
160                 if (ret != (int)WiFiError.None)
161                 {
162                     Log.Error(Globals.LogTag, "Failed to get frequency, Error - " + (WiFiError)ret);
163                 }
164                 return freq;
165             }
166         }
167
168         /// <summary>
169         /// The Received signal strength indication(RSSI).
170         /// </summary>
171         public WiFiRssiLevel Rssi
172         {
173             get
174             {
175                 int rssi;
176                 int ret = Interop.WiFi.AP.GetRssi(_apHandle, out rssi);
177                 if (ret != (int)WiFiError.None)
178                 {
179                     Log.Error(Globals.LogTag, "Failed to get rssi, Error - " + (WiFiError)ret);
180                 }
181                 return (WiFiRssiLevel)rssi;
182             }
183         }
184
185         /// <summary>
186         /// The max speed (Mbps).
187         /// </summary>
188         public int MaxSpeed
189         {
190             get
191             {
192                 int maxSpeed;
193                 int ret = Interop.WiFi.AP.GetMaxSpeed(_apHandle, out maxSpeed);
194                 if (ret != (int)WiFiError.None)
195                 {
196                     Log.Error(Globals.LogTag, "Failed to get max speed, Error - " + (WiFiError)ret);
197                 }
198                 return maxSpeed;
199             }
200         }
201
202         /// <summary>
203         /// A property to check whether the access point is favorite or not.
204         /// </summary>
205         public bool IsFavorite
206         {
207             get
208             {
209                 bool isFavorite;
210                 int ret = Interop.WiFi.AP.IsFavorite(_apHandle, out isFavorite);
211                 if (ret != (int)WiFiError.None)
212                 {
213                     Log.Error(Globals.LogTag, "Failed to get favorite, Error - " + (WiFiError)ret);
214                 }
215                 return isFavorite;
216             }
217         }
218
219         /// <summary>
220         /// A property to check whether the access point is passpoint or not.
221         /// </summary>
222         public bool IsPasspoint
223         {
224             get
225             {
226                 bool isPasspoint;
227                 Log.Debug(Globals.LogTag, "Handle: " + _apHandle);
228                 int ret = Interop.WiFi.AP.IsPasspoint(_apHandle, out isPasspoint);
229                 if (ret != (int)WiFiError.None)
230                 {
231                     Log.Error(Globals.LogTag, "Failed to get isPassport, Error - " + (WiFiError)ret);
232                 }
233                 return isPasspoint;
234             }
235         }
236
237         /// <summary>
238         /// The connection state.
239         /// </summary>
240         public WiFiConnectionState ConnectionState
241         {
242             get
243             {
244                 int state;
245                 int ret = Interop.WiFi.AP.GetConnectionState(_apHandle, out state);
246                 if (ret != (int)WiFiError.None)
247                 {
248                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
249                 }
250                 return (WiFiConnectionState)state;
251             }
252         }
253
254         internal WiFiNetwork(Interop.WiFi.SafeWiFiAPHandle apHandle)
255         {
256             _apHandle = apHandle;
257             _ipv4 = new WiFiAddressInformation(apHandle, AddressFamily.IPv4);
258             _ipv6 = new WiFiAddressInformation(apHandle, AddressFamily.IPv6);
259
260             IntPtr strPtr;
261             int ret = Interop.WiFi.AP.GetEssid(_apHandle, out strPtr);
262             if (ret != (int)WiFiError.None)
263             {
264                 Log.Error(Globals.LogTag, "Failed to get essid, Error - " + (WiFiError)ret);
265             }
266             _essid = Marshal.PtrToStringAnsi(strPtr);
267         }
268     } //WiFiNetworkInformation
269 }