2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
21 using System.Runtime.InteropServices;
22 using Tizen.Applications;
24 namespace Tizen.Network.Connection
27 /// This is the ConnectionProfile class. It provides event and properties of the connection profile.
29 /// <since_tizen> 3 </since_tizen>
30 public class ConnectionProfile : IDisposable
32 internal IntPtr ProfileHandle = IntPtr.Zero;
33 private IAddressInformation IPv4;
34 private IAddressInformation IPv6;
35 private bool disposed = false;
36 private EventHandler<ProfileStateEventArgs> _ProfileStateChanged = null;
38 private Interop.ConnectionProfile.ProfileStateChangedCallback _profileChangedCallback;
40 private TizenSynchronizationContext context = new TizenSynchronizationContext();
42 internal IntPtr GetHandle()
48 /// The event is called when the state of profile is changed.
50 /// <since_tizen> 3 </since_tizen>
51 /// <feature>http://tizen.org/feature/network.ethernet</feature>
52 /// <feature>http://tizen.org/feature/network.telephony</feature>
53 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
54 /// <feature>http://tizen.org/feature/network.wifi</feature>
55 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
56 public event EventHandler<ProfileStateEventArgs> ProfileStateChanged
60 Log.Info(Globals.LogTag, "ProfileStateChanged add");
63 if (_ProfileStateChanged == null)
67 ProfileStateChangedStart();
70 Log.Error(Globals.LogTag, "Exception on adding ProfileStateChanged\n" + e.ToString());
74 _ProfileStateChanged += value;
79 Log.Info(Globals.LogTag, "ProfileStateChanged remove");
82 _ProfileStateChanged -= value;
83 if (_ProfileStateChanged == null)
87 ProfileStateChangedStop();
91 Log.Error(Globals.LogTag, "Exception on removing ProfileStateChanged\n" + e.ToString());
98 private void ProfileStateChangedStart()
100 _profileChangedCallback = (ProfileState state, IntPtr userData) =>
102 if (_ProfileStateChanged != null)
104 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
108 Log.Info(Globals.LogTag, "ProfileStateChangedStart");
109 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
110 if ((ConnectionError)ret != ConnectionError.None)
112 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
113 ConnectionErrorFactory.ThrowConnectionException(ret);
117 private void ProfileStateChangedStop()
119 Log.Info(Globals.LogTag, "ProfileStateChangedStop");
120 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
121 if ((ConnectionError)ret != ConnectionError.None)
123 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
124 ConnectionErrorFactory.ThrowConnectionException(ret);
128 internal ConnectionProfile(IntPtr handle)
130 ProfileHandle = handle;
131 IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
132 IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
136 /// Destroy the ConnectionProfile object
144 /// Disposes the memory allocated to unmanaged resources.
146 /// <since_tizen> 3 </since_tizen>
147 public void Dispose()
150 GC.SuppressFinalize(this);
153 private void Dispose(bool disposing)
158 // Free unmanaged objects
164 private void UnregisterEvents()
166 if (_ProfileStateChanged != null)
168 ProfileStateChangedStop();
172 private void Destroy()
174 int ret = Interop.ConnectionProfile.Destroy(ProfileHandle);
175 if ((ConnectionError)ret == ConnectionError.None)
177 ProfileHandle = IntPtr.Zero;
182 internal void CheckDisposed()
186 throw new ObjectDisposedException(GetType().FullName);
193 /// <since_tizen> 3 </since_tizen>
194 /// <value>Unique ID of the profile.</value>
200 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
201 if ((ConnectionError)ret != ConnectionError.None)
203 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
205 string result = Marshal.PtrToStringAnsi(Value);
206 Interop.Libc.Free(Value);
212 /// The profile name.
214 /// <since_tizen> 3 </since_tizen>
215 /// <value>User friendly name of the profile.</value>
221 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
222 if ((ConnectionError)ret != ConnectionError.None)
224 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
226 string result = Marshal.PtrToStringAnsi(Value);
227 Interop.Libc.Free(Value);
233 /// The network type.
235 /// <since_tizen> 3 </since_tizen>
236 /// <value>Profile type of the network connection.</value>
237 public ConnectionProfileType Type
242 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
243 if ((ConnectionError)ret != ConnectionError.None)
245 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
247 return (ConnectionProfileType)Value;
252 /// The name of the network interface.
254 /// <since_tizen> 3 </since_tizen>
255 /// <value>Network interface name, for example, eth0 and pdp0.</value>
256 public string InterfaceName
261 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
262 if ((ConnectionError)ret != ConnectionError.None)
264 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
266 string result = Marshal.PtrToStringAnsi(Value);
267 Interop.Libc.Free(Value);
273 /// Refreshes the profile information.
275 /// <since_tizen> 3 </since_tizen>
276 /// <privilege>http://tizen.org/privilege/network.get</privilege>
277 /// <feature>http://tizen.org/feature/network.ethernet</feature>
278 /// <feature>http://tizen.org/feature/network.telephony</feature>
279 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
280 /// <feature>http://tizen.org/feature/network.wifi</feature>
281 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
282 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
283 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
284 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
285 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
286 public void Refresh()
289 int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
290 if ((ConnectionError)ret != ConnectionError.None)
292 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
293 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
294 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
295 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
296 ConnectionErrorFactory.ThrowConnectionException(ret);
301 /// Gets the network state.
303 /// <since_tizen> 3 </since_tizen>
304 /// <param name="family">The address family.</param>
305 /// <returns>The network state.</returns>
306 /// <feature>http://tizen.org/feature/network.ethernet</feature>
307 /// <feature>http://tizen.org/feature/network.telephony</feature>
308 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
309 /// <feature>http://tizen.org/feature/network.wifi</feature>
310 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
311 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
312 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
313 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
314 public ProfileState GetState(AddressFamily family)
318 int ret = (int)ConnectionError.None;
319 if (family == AddressFamily.IPv4)
321 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
326 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
329 if ((ConnectionError)ret != ConnectionError.None)
331 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
332 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
333 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
334 ConnectionErrorFactory.ThrowConnectionException(ret);
337 return (ProfileState)Value;
343 /// <since_tizen> 3 </since_tizen>
344 /// <value>Proxy type of the connection.</value>
345 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
346 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
347 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
348 /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
349 public ProxyType ProxyType
354 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
355 if ((ConnectionError)ret != ConnectionError.None)
357 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
359 return (ProxyType)Value;
366 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
367 if ((ConnectionError)ret != ConnectionError.None)
369 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
370 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
371 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
372 ConnectionErrorFactory.ThrowConnectionException(ret);
378 /// The proxy address.
380 /// <since_tizen> 3 </since_tizen>
381 /// <value>Proxy address of the connection.</value>
382 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
383 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
384 /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
385 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
386 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
387 public string ProxyAddress
392 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
393 if ((ConnectionError)ret != ConnectionError.None)
395 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
397 string result = Marshal.PtrToStringAnsi(Value);
398 Interop.Libc.Free(Value);
408 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
409 if ((ConnectionError)ret != ConnectionError.None)
411 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
412 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
413 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
414 ConnectionErrorFactory.ThrowConnectionException(ret);
420 throw new ArgumentNullException("ProxyAddress is null");
426 /// The address information (IPv4).
428 /// <since_tizen> 3 </since_tizen>
429 /// <value>Instance of IAddressInformation with IPV4 address.</value>
430 public IAddressInformation IPv4Settings
440 /// The address information (IPv6).
442 /// <since_tizen> 3 </since_tizen>
443 /// <value>Instance of IAddressInformation with IPV6 address.</value>
444 public IAddressInformation IPv6Settings
454 /// An extended EventArgs class, which contains changed profile state.
456 /// <since_tizen> 3 </since_tizen>
457 public class ProfileStateEventArgs : EventArgs
459 private ProfileState _State = ProfileState.Disconnected;
461 internal ProfileStateEventArgs(ProfileState state)
467 /// The profile state.
469 /// <since_tizen> 3 </since_tizen>
470 /// <value>State type of the connection profile.</value>
471 public ProfileState State