Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiConfiguration.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.Net;
19 using System.Threading.Tasks;
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 configuration of Wi-Fi.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class WiFiConfiguration : IDisposable
30     {
31         private IntPtr _configHandle = IntPtr.Zero;
32         private bool _disposed = false;
33         private WiFiEapConfiguration _eapConfig;
34
35         /// <summary>
36         /// The name of access point(AP).
37         /// </summary>
38         /// <since_tizen> 3 </since_tizen>
39         /// <value>Name assigned to AP in WiFi configuration.</value>
40         public string Name
41         {
42             get
43             {
44                 IntPtr strPtr;
45                 int ret = Interop.WiFi.Config.GetName(_configHandle, out strPtr);
46                 if (ret != (int)WiFiError.None)
47                 {
48                     Log.Error(Globals.LogTag, "Failed to get name, Error - " + (WiFiError)ret);
49                     return "";
50                 }
51                 return Marshal.PtrToStringAnsi(strPtr);
52             }
53         }
54
55         /// <summary>
56         /// The security type of access point(AP).
57         /// </summary>
58         /// <since_tizen> 3 </since_tizen>
59         /// <value>Security type of AP in WiFi configuration.</value>
60         public WiFiSecurityType SecurityType
61         {
62             get
63             {
64                 int type;
65                 int ret = Interop.WiFi.Config.GetSecurityType(_configHandle, out type);
66                 if (ret != (int)WiFiError.None)
67                 {
68                     Log.Error(Globals.LogTag, "Failed to get security type, Error - " + (WiFiError)ret);
69                 }
70                 return (WiFiSecurityType)type;
71             }
72         }
73
74         /// <summary>
75         /// The proxy address.
76         /// </summary>
77         /// <since_tizen> 3 </since_tizen>
78         /// <value>Proxy address of the access point.</value>
79         /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
80         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
81         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
82         public string ProxyAddress
83         {
84             get
85             {
86                 IntPtr strPtr;
87                 int addressFamily;
88                 int ret = Interop.WiFi.Config.GetProxyAddress(_configHandle, out addressFamily, out strPtr);
89                 if (ret != (int)WiFiError.None)
90                 {
91                     Log.Error(Globals.LogTag, "Failed to get proxy address, Error - " + (WiFiError)ret);
92                     return "";
93                 }
94                 return Marshal.PtrToStringAnsi(strPtr);
95             }
96             set
97             {
98                 if (_disposed)
99                 {
100                     throw new ObjectDisposedException("Invalid WiFiConfiguration instance (Object may have been disposed or released)");
101                 }
102                 int ret = Interop.WiFi.Config.SetProxyAddress(_configHandle, (int)AddressFamily.IPv4, value);
103                 if (ret != (int)WiFiError.None)
104                 {
105                     Log.Error(Globals.LogTag, "Failed to set proxy address, Error - " + (WiFiError)ret);
106                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle);
107                 }
108             }
109         }
110
111         /// <summary>
112         /// A property check whether the access point(AP) is hidden or not.
113         /// </summary>
114         /// <since_tizen> 3 </since_tizen>
115         /// <value>Boolean value indicating whether AP is hidden or not.</value>
116         /// <exception cref="NotSupportedException">Thrown while setting this property when WiFi is not supported.</exception>
117         /// <exception cref="ArgumentException">Thrown while setting this property due to an invalid parameter.</exception>
118         /// <exception cref="InvalidOperationException">Thrown while setting this value due to invalid operation.</exception>
119         public bool IsHidden
120         {
121             get
122             {
123                 bool hidden;
124                 int ret = Interop.WiFi.Config.GetHiddenAPProperty(_configHandle, out hidden);
125                 if (ret != (int)WiFiError.None)
126                 {
127                     Log.Error(Globals.LogTag, "Failed to get isHidden, Error - " + (WiFiError)ret);
128                 }
129                 return hidden;
130             }
131             set
132             {
133                 if (_disposed)
134                 {
135                     throw new ObjectDisposedException("Invalid WiFiConfiguration instance (Object may have been disposed or released)");
136                 }
137                 int ret = Interop.WiFi.Config.SetHiddenAPProperty(_configHandle, value);
138                 if (ret != (int)WiFiError.None)
139                 {
140                     Log.Error(Globals.LogTag, "Failed to set IsHidden, Error - " + (WiFiError)ret);
141                     WiFiErrorFactory.ThrowWiFiException(ret, _configHandle);
142                 }
143             }
144         }
145
146         /// <summary>
147         /// The EAP Configuration.
148         /// </summary>
149         /// <since_tizen> 3 </since_tizen>
150         /// <value>EAP configuration assigned to WiFi.</value>
151         public WiFiEapConfiguration EapConfiguration
152         {
153             get
154             {
155                 return _eapConfig;
156             }
157         }
158
159         internal WiFiConfiguration(IntPtr handle)
160         {
161             _configHandle = handle;
162             Interop.WiFi.SafeWiFiConfigHandle configHandle = new Interop.WiFi.SafeWiFiConfigHandle(handle);
163             _eapConfig = new WiFiEapConfiguration(configHandle);
164         }
165
166         /// <summary>
167         /// Creates a WiFiConfiguration object with the given name, passphrase and securetype.
168         /// </summary>
169         /// <since_tizen> 3 </since_tizen>
170         /// <param name="name">Name of the WiFi.</param>
171         /// <param name="passPhrase">Password to access the WiFi.</param>
172         /// <param name="type">Security type of the WiFi.</param>
173         /// <feature>http://tizen.org/feature/network.wifi</feature>
174         /// <privilege>http://tizen.org/privilege/network.get</privilege>
175         /// <exception cref="NotSupportedException">Thrown when WiFi is not supported.</exception>
176         /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
177         /// <exception cref="ArgumentNullException">Thrown when the object is constructed with name as null.</exception>
178         /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
179         /// <exception cref="ArgumentException">Thrown when method is failed due to an invalid parameter.</exception>
180         /// <exception cref="InvalidOperationException">Thrown when it is failed due to invalid operation.</exception>
181         public WiFiConfiguration(string name, string passPhrase, WiFiSecurityType type)
182         {
183             if (name == null)
184             {
185                 throw new ArgumentNullException("Name of the WiFi is null");
186             }
187
188             int ret = Interop.WiFi.Config.Create(WiFiManagerImpl.Instance.GetSafeHandle(), name, passPhrase, (int)type, out _configHandle);
189             if (ret != (int)WiFiError.None)
190             {
191                 Log.Error(Globals.LogTag, "Failed to create config handle, Error - " + (WiFiError)ret);
192                 WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
193             }
194
195             Interop.WiFi.SafeWiFiConfigHandle configHandle = new Interop.WiFi.SafeWiFiConfigHandle(_configHandle);
196             _eapConfig = new WiFiEapConfiguration(configHandle);
197         }
198
199         ~WiFiConfiguration()
200         {
201             Dispose(false);
202         }
203
204         /// <summary>
205         /// A method to destroy the managed objects in WiFiConfiguration.
206         /// </summary>
207         /// <since_tizen> 3 </since_tizen>
208         public void Dispose()
209         {
210             Dispose(true);
211             GC.SuppressFinalize(this);
212         }
213
214         private void Dispose(bool disposing)
215         {
216             if (_disposed)
217                 return;
218
219             if (disposing)
220             {
221                 Interop.WiFi.Config.Destroy(_configHandle);
222                 _configHandle = IntPtr.Zero;
223             }
224             _disposed = true;
225         }
226
227         internal IntPtr GetHandle()
228         {
229             return _configHandle;
230         }
231     } //WiFiNetworkConfiguratin
232 }