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.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);
116 private void ProfileStateChangedStop()
118 Log.Debug(Globals.LogTag, "ProfileStateChangedStop");
119 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
120 if ((ConnectionError)ret != ConnectionError.None)
122 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
126 internal ConnectionProfile(IntPtr handle)
128 ProfileHandle = handle;
129 IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
130 IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
134 /// Destroy the ConnectionProfile object
142 /// Disposes the memory allocated to unmanaged resources.
144 /// <since_tizen> 3 </since_tizen>
145 public void Dispose()
148 GC.SuppressFinalize(this);
151 private void Dispose(bool disposing)
153 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
157 // Free unmanaged objects
163 private void UnregisterEvents()
165 if (_ProfileStateChanged != null)
167 ProfileStateChangedStop();
171 private void Destroy()
173 Interop.ConnectionProfile.Destroy(ProfileHandle);
174 ProfileHandle = IntPtr.Zero;
177 internal void CheckDisposed()
181 throw new ObjectDisposedException(GetType().FullName);
188 /// <since_tizen> 3 </since_tizen>
189 /// <value>Unique ID of the profile.</value>
195 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
196 if ((ConnectionError)ret != ConnectionError.None)
198 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
200 string result = Marshal.PtrToStringAnsi(Value);
201 Interop.Libc.Free(Value);
207 /// The profile name.
209 /// <since_tizen> 3 </since_tizen>
210 /// <value>User friendly name of the profile.</value>
216 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
217 if ((ConnectionError)ret != ConnectionError.None)
219 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
221 string result = Marshal.PtrToStringAnsi(Value);
222 Interop.Libc.Free(Value);
228 /// The network type.
230 /// <since_tizen> 3 </since_tizen>
231 /// <value>Profile type of the network connection.</value>
232 public ConnectionProfileType Type
237 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
238 if ((ConnectionError)ret != ConnectionError.None)
240 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
242 return (ConnectionProfileType)Value;
247 /// The name of the network interface.
249 /// <since_tizen> 3 </since_tizen>
250 /// <value>Network interface name, for example, eth0 and pdp0.</value>
251 public string InterfaceName
256 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
257 if ((ConnectionError)ret != ConnectionError.None)
259 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
261 string result = Marshal.PtrToStringAnsi(Value);
262 Interop.Libc.Free(Value);
268 /// Refreshes the profile information.
270 /// <since_tizen> 3 </since_tizen>
271 /// <privilege>http://tizen.org/privilege/network.get</privilege>
272 /// <feature>http://tizen.org/feature/network.ethernet</feature>
273 /// <feature>http://tizen.org/feature/network.telephony</feature>
274 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
275 /// <feature>http://tizen.org/feature/network.wifi</feature>
276 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
277 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
278 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
279 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
280 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
281 public void Refresh()
284 int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
285 if ((ConnectionError)ret != ConnectionError.None)
287 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
288 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");
289 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
290 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
291 ConnectionErrorFactory.ThrowConnectionException(ret);
296 /// Gets the network state.
298 /// <since_tizen> 3 </since_tizen>
299 /// <param name="family">The address family.</param>
300 /// <returns>The network state.</returns>
301 /// <feature>http://tizen.org/feature/network.ethernet</feature>
302 /// <feature>http://tizen.org/feature/network.telephony</feature>
303 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
304 /// <feature>http://tizen.org/feature/network.wifi</feature>
305 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
306 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
307 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
308 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
309 public ProfileState GetState(AddressFamily family)
313 int ret = (int)ConnectionError.None;
314 if (family == AddressFamily.IPv4)
316 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
321 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
324 if ((ConnectionError)ret != ConnectionError.None)
326 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
327 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");
328 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
329 ConnectionErrorFactory.ThrowConnectionException(ret);
332 return (ProfileState)Value;
338 /// <since_tizen> 3 </since_tizen>
339 /// <value>Proxy type of the connection.</value>
340 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
341 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
342 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
343 /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
344 public ProxyType ProxyType
349 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
350 if ((ConnectionError)ret != ConnectionError.None)
352 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
354 return (ProxyType)Value;
361 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
362 if ((ConnectionError)ret != ConnectionError.None)
364 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
365 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");
366 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
367 ConnectionErrorFactory.ThrowConnectionException(ret);
373 /// The proxy address.
375 /// <since_tizen> 3 </since_tizen>
376 /// <value>Proxy address of the connection.</value>
377 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
378 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
379 /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
380 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
381 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
382 public string ProxyAddress
387 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
388 if ((ConnectionError)ret != ConnectionError.None)
390 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
392 string result = Marshal.PtrToStringAnsi(Value);
393 Interop.Libc.Free(Value);
403 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
404 if ((ConnectionError)ret != ConnectionError.None)
406 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
407 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");
408 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
409 ConnectionErrorFactory.ThrowConnectionException(ret);
415 throw new ArgumentNullException("ProxyAddress is null");
421 /// The address information (IPv4).
423 /// <since_tizen> 3 </since_tizen>
424 /// <value>Instance of IAddressInformation with IPV4 address.</value>
425 public IAddressInformation IPv4Settings
435 /// The address information (IPv6).
437 /// <since_tizen> 3 </since_tizen>
438 /// <value>Instance of IAddressInformation with IPV6 address.</value>
439 public IAddressInformation IPv6Settings
449 /// An extended EventArgs class, which contains changed profile state.
451 /// <since_tizen> 3 </since_tizen>
452 public class ProfileStateEventArgs : EventArgs
454 private ProfileState _State = ProfileState.Disconnected;
456 internal ProfileStateEventArgs(ProfileState state)
462 /// The profile state.
464 /// <since_tizen> 3 </since_tizen>
465 /// <value>State type of the connection profile.</value>
466 public ProfileState State