2 * Copyright (c) 2018 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.Debug(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.Debug(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.Debug(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.Debug(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)
155 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
159 // Free unmanaged objects
165 private void UnregisterEvents()
167 if (_ProfileStateChanged != null)
169 ProfileStateChangedStop();
173 private void Destroy()
175 int ret = Interop.ConnectionProfile.Destroy(ProfileHandle);
176 if ((ConnectionError)ret == ConnectionError.None)
178 ProfileHandle = IntPtr.Zero;
183 internal void CheckDisposed()
187 throw new ObjectDisposedException(GetType().FullName);
194 /// <since_tizen> 3 </since_tizen>
195 /// <value>Unique ID of the profile.</value>
201 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
202 if ((ConnectionError)ret != ConnectionError.None)
204 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
206 string result = Marshal.PtrToStringAnsi(Value);
207 Interop.Libc.Free(Value);
213 /// The profile name.
215 /// <since_tizen> 3 </since_tizen>
216 /// <value>User friendly name of the profile.</value>
222 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
223 if ((ConnectionError)ret != ConnectionError.None)
225 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
227 string result = Marshal.PtrToStringAnsi(Value);
228 Interop.Libc.Free(Value);
234 /// The network type.
236 /// <since_tizen> 3 </since_tizen>
237 /// <value>Profile type of the network connection.</value>
238 public ConnectionProfileType Type
243 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
244 if ((ConnectionError)ret != ConnectionError.None)
246 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
248 return (ConnectionProfileType)Value;
253 /// The name of the network interface.
255 /// <since_tizen> 3 </since_tizen>
256 /// <value>Network interface name, for example, eth0 and pdp0.</value>
257 public string InterfaceName
262 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
263 if ((ConnectionError)ret != ConnectionError.None)
265 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
267 string result = Marshal.PtrToStringAnsi(Value);
268 Interop.Libc.Free(Value);
274 /// Refreshes the profile information.
276 /// <since_tizen> 3 </since_tizen>
277 /// <privilege>http://tizen.org/privilege/network.get</privilege>
278 /// <feature>http://tizen.org/feature/network.ethernet</feature>
279 /// <feature>http://tizen.org/feature/network.telephony</feature>
280 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
281 /// <feature>http://tizen.org/feature/network.wifi</feature>
282 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
283 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</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 if ((ConnectionError)ret == ConnectionError.InvalidParameter)
295 throw new InvalidOperationException("Invalid handle");
297 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");
298 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
299 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
300 ConnectionErrorFactory.ThrowConnectionException(ret);
305 /// Gets the network state.
307 /// <since_tizen> 3 </since_tizen>
308 /// <param name="family">The address family.</param>
309 /// <returns>The network state.</returns>
310 /// <feature>http://tizen.org/feature/network.ethernet</feature>
311 /// <feature>http://tizen.org/feature/network.telephony</feature>
312 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
313 /// <feature>http://tizen.org/feature/network.wifi</feature>
314 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
315 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
316 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
317 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
318 public ProfileState GetState(AddressFamily family)
322 int ret = (int)ConnectionError.None;
323 if (family == AddressFamily.IPv4)
325 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
330 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
333 if ((ConnectionError)ret != ConnectionError.None)
335 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
336 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");
337 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
338 ConnectionErrorFactory.ThrowConnectionException(ret);
341 return (ProfileState)Value;
347 /// <since_tizen> 3 </since_tizen>
348 /// <value>Proxy type of the connection.</value>
349 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
350 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
351 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
352 /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
353 public ProxyType ProxyType
358 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
359 if ((ConnectionError)ret != ConnectionError.None)
361 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
363 return (ProxyType)Value;
370 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
371 if ((ConnectionError)ret != ConnectionError.None)
373 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
374 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");
375 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
376 ConnectionErrorFactory.ThrowConnectionException(ret);
382 /// The proxy address.
384 /// <since_tizen> 3 </since_tizen>
385 /// <value>Proxy address of the connection.</value>
386 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
387 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
388 /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
389 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
390 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
391 public string ProxyAddress
396 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
397 if ((ConnectionError)ret != ConnectionError.None)
399 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
401 string result = Marshal.PtrToStringAnsi(Value);
402 Interop.Libc.Free(Value);
412 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
413 if ((ConnectionError)ret != ConnectionError.None)
415 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
416 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");
417 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
418 ConnectionErrorFactory.ThrowConnectionException(ret);
424 throw new ArgumentNullException("ProxyAddress is null");
430 /// The address information (IPv4).
432 /// <since_tizen> 3 </since_tizen>
433 /// <value>Instance of IAddressInformation with IPV4 address.</value>
434 public IAddressInformation IPv4Settings
444 /// The address information (IPv6).
446 /// <since_tizen> 3 </since_tizen>
447 /// <value>Instance of IAddressInformation with IPV6 address.</value>
448 public IAddressInformation IPv6Settings
458 /// An extended EventArgs class, which contains changed profile state.
460 /// <since_tizen> 3 </since_tizen>
461 public class ProfileStateEventArgs : EventArgs
463 private ProfileState _State = ProfileState.Disconnected;
465 internal ProfileStateEventArgs(ProfileState state)
471 /// The profile state.
473 /// <since_tizen> 3 </since_tizen>
474 /// <value>State type of the connection profile.</value>
475 public ProfileState State