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 internal IntPtr GetHandle()
46 /// The event is called when the state of profile is changed.
48 /// <since_tizen> 3 </since_tizen>
49 /// <feature>http://tizen.org/feature/network.ethernet</feature>
50 /// <feature>http://tizen.org/feature/network.telephony</feature>
51 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
52 /// <feature>http://tizen.org/feature/network.wifi</feature>
53 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
54 public event EventHandler<ProfileStateEventArgs> ProfileStateChanged
58 Log.Debug(Globals.LogTag, "ProfileStateChanged add");
59 if (_ProfileStateChanged == null)
63 ProfileStateChangedStart();
66 Log.Error(Globals.LogTag, "Exception on adding ProfileStateChanged\n" + e.ToString());
70 _ProfileStateChanged += value;
74 Log.Debug(Globals.LogTag, "ProfileStateChanged remove");
75 _ProfileStateChanged -= value;
76 if (_ProfileStateChanged == null)
80 ProfileStateChangedStop();
84 Log.Error(Globals.LogTag, "Exception on removing ProfileStateChanged\n" + e.ToString());
90 private void ProfileStateChangedStart()
92 _profileChangedCallback = (ProfileState state, IntPtr userData) =>
94 if (_ProfileStateChanged != null)
96 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
100 Log.Debug(Globals.LogTag, "ProfileStateChangedStart");
101 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
102 if ((ConnectionError)ret != ConnectionError.None)
104 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
105 ConnectionErrorFactory.ThrowConnectionException(ret);
109 private void ProfileStateChangedStop()
111 Log.Debug(Globals.LogTag, "ProfileStateChangedStop");
112 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
113 if ((ConnectionError)ret != ConnectionError.None)
115 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
116 ConnectionErrorFactory.ThrowConnectionException(ret);
120 internal ConnectionProfile(IntPtr handle)
122 ProfileHandle = handle;
123 IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
124 IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
128 /// Destroy the ConnectionProfile object
136 /// Disposes the memory allocated to unmanaged resources.
138 /// <since_tizen> 3 </since_tizen>
139 public void Dispose()
142 GC.SuppressFinalize(this);
145 private void Dispose(bool disposing)
147 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
151 // Free unmanaged objects
157 private void UnregisterEvents()
159 if (_ProfileStateChanged != null)
161 ProfileStateChangedStop();
165 private void Destroy()
167 int ret = Interop.ConnectionProfile.Destroy(ProfileHandle);
168 if ((ConnectionError)ret == ConnectionError.None)
170 ProfileHandle = IntPtr.Zero;
175 internal void CheckDisposed()
179 throw new ObjectDisposedException(GetType().FullName);
186 /// <since_tizen> 3 </since_tizen>
187 /// <value>Unique ID of the profile.</value>
193 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
194 if ((ConnectionError)ret != ConnectionError.None)
196 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
198 string result = Marshal.PtrToStringAnsi(Value);
199 Interop.Libc.Free(Value);
205 /// The profile name.
207 /// <since_tizen> 3 </since_tizen>
208 /// <value>User friendly name of the profile.</value>
214 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
215 if ((ConnectionError)ret != ConnectionError.None)
217 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
219 string result = Marshal.PtrToStringAnsi(Value);
220 Interop.Libc.Free(Value);
226 /// The network type.
228 /// <since_tizen> 3 </since_tizen>
229 /// <value>Profile type of the network connection.</value>
230 public ConnectionProfileType Type
235 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
236 if ((ConnectionError)ret != ConnectionError.None)
238 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
240 return (ConnectionProfileType)Value;
245 /// The name of the network interface.
247 /// <since_tizen> 3 </since_tizen>
248 /// <value>Network interface name, for example, eth0 and pdp0.</value>
249 public string InterfaceName
254 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
255 if ((ConnectionError)ret != ConnectionError.None)
257 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
259 string result = Marshal.PtrToStringAnsi(Value);
260 Interop.Libc.Free(Value);
266 /// Refreshes the profile information.
268 /// <since_tizen> 3 </since_tizen>
269 /// <privilege>http://tizen.org/privilege/network.get</privilege>
270 /// <feature>http://tizen.org/feature/network.ethernet</feature>
271 /// <feature>http://tizen.org/feature/network.telephony</feature>
272 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
273 /// <feature>http://tizen.org/feature/network.wifi</feature>
274 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
275 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
276 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
277 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
278 public void Refresh()
281 int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
282 if ((ConnectionError)ret != ConnectionError.None)
284 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
285 if ((ConnectionError)ret == ConnectionError.InvalidParameter)
287 throw new InvalidOperationException("Invalid handle");
289 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");
290 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
291 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
292 ConnectionErrorFactory.ThrowConnectionException(ret);
297 /// Gets the network state.
299 /// <since_tizen> 3 </since_tizen>
300 /// <param name="family">The address family.</param>
301 /// <returns>The network state.</returns>
302 /// <feature>http://tizen.org/feature/network.ethernet</feature>
303 /// <feature>http://tizen.org/feature/network.telephony</feature>
304 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
305 /// <feature>http://tizen.org/feature/network.wifi</feature>
306 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
307 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
308 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
309 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
310 public ProfileState GetState(AddressFamily family)
314 int ret = (int)ConnectionError.None;
315 if (family == AddressFamily.IPv4)
317 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
322 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
325 if ((ConnectionError)ret != ConnectionError.None)
327 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
328 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");
329 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
330 ConnectionErrorFactory.ThrowConnectionException(ret);
333 return (ProfileState)Value;
339 /// <since_tizen> 3 </since_tizen>
340 /// <value>Proxy type of the connection.</value>
341 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
342 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
343 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
344 /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
345 public ProxyType ProxyType
350 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
351 if ((ConnectionError)ret != ConnectionError.None)
353 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
355 return (ProxyType)Value;
362 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
363 if ((ConnectionError)ret != ConnectionError.None)
365 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
366 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");
367 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
368 ConnectionErrorFactory.ThrowConnectionException(ret);
374 /// The proxy address.
376 /// <since_tizen> 3 </since_tizen>
377 /// <value>Proxy address of the connection.</value>
378 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
379 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
380 /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
381 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
382 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
383 public string ProxyAddress
388 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
389 if ((ConnectionError)ret != ConnectionError.None)
391 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
393 string result = Marshal.PtrToStringAnsi(Value);
394 Interop.Libc.Free(Value);
404 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
405 if ((ConnectionError)ret != ConnectionError.None)
407 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
408 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");
409 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
410 ConnectionErrorFactory.ThrowConnectionException(ret);
416 throw new ArgumentNullException("ProxyAddress is null");
422 /// The address information (IPv4).
424 /// <since_tizen> 3 </since_tizen>
425 /// <value>Instance of IAddressInformation with IPV4 address.</value>
426 public IAddressInformation IPv4Settings
436 /// The address information (IPv6).
438 /// <since_tizen> 3 </since_tizen>
439 /// <value>Instance of IAddressInformation with IPV6 address.</value>
440 public IAddressInformation IPv6Settings
450 /// An extended EventArgs class, which contains changed profile state.
452 /// <since_tizen> 3 </since_tizen>
453 public class ProfileStateEventArgs : EventArgs
455 private ProfileState _State = ProfileState.Disconnected;
457 internal ProfileStateEventArgs(ProfileState state)
463 /// The profile state.
465 /// <since_tizen> 3 </since_tizen>
466 /// <value>State type of the connection profile.</value>
467 public ProfileState State