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;
23 namespace Tizen.Network.Connection
26 /// This is the ConnectionProfile class. It provides event and properties of the connection profile.
28 /// <since_tizen> 3 </since_tizen>
29 public class ConnectionProfile : IDisposable
31 internal IntPtr ProfileHandle = IntPtr.Zero;
32 private IAddressInformation IPv4;
33 private IAddressInformation IPv6;
34 private bool disposed = false;
35 private EventHandler<ProfileStateEventArgs> _ProfileStateChanged = null;
37 private Interop.ConnectionProfile.ProfileStateChangedCallback _profileChangedCallback;
39 internal IntPtr GetHandle()
45 /// The event is called when the state of profile is changed.
47 /// <since_tizen> 3 </since_tizen>
48 /// <feature>http://tizen.org/feature/network.ethernet</feature>
49 /// <feature>http://tizen.org/feature/network.telephony</feature>
50 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
51 /// <feature>http://tizen.org/feature/network.wifi</feature>
52 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
53 public event EventHandler<ProfileStateEventArgs> ProfileStateChanged
57 Log.Debug(Globals.LogTag, "ProfileStateChanged add");
58 if (_ProfileStateChanged == null)
60 ProfileStateChangedStart();
62 _ProfileStateChanged += value;
66 Log.Debug(Globals.LogTag, "ProfileStateChanged remove");
67 _ProfileStateChanged -= value;
68 if (_ProfileStateChanged == null)
70 ProfileStateChangedStop();
75 private void ProfileStateChangedStart()
77 _profileChangedCallback = (ProfileState state, IntPtr userData) =>
79 if (_ProfileStateChanged != null)
81 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
85 Log.Debug(Globals.LogTag, "ProfileStateChangedStart");
86 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
87 if ((ConnectionError)ret != ConnectionError.None)
89 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
93 private void ProfileStateChangedStop()
95 Log.Debug(Globals.LogTag, "ProfileStateChangedStop");
96 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
97 if ((ConnectionError)ret != ConnectionError.None)
99 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
103 internal ConnectionProfile(IntPtr handle)
105 ProfileHandle = handle;
106 IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
107 IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
111 /// Destroy the ConnectionProfile object
119 /// Disposes the memory allocated to unmanaged resources.
121 /// <since_tizen> 3 </since_tizen>
122 public void Dispose()
125 GC.SuppressFinalize(this);
128 private void Dispose(bool disposing)
130 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
136 // Free managed objects.
143 private void UnregisterEvents()
145 if (_ProfileStateChanged != null)
147 ProfileStateChangedStop();
151 private void Destroy()
153 Interop.ConnectionProfile.Destroy(ProfileHandle);
154 ProfileHandle = IntPtr.Zero;
157 internal void CheckDisposed()
161 throw new ObjectDisposedException(GetType().FullName);
168 /// <since_tizen> 3 </since_tizen>
169 /// <value>Unique ID of the profile.</value>
175 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
176 if ((ConnectionError)ret != ConnectionError.None)
178 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
180 string result = Marshal.PtrToStringAnsi(Value);
181 Interop.Libc.Free(Value);
187 /// The profile name.
189 /// <since_tizen> 3 </since_tizen>
190 /// <value>User friendly name of the profile.</value>
196 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
197 if ((ConnectionError)ret != ConnectionError.None)
199 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
201 string result = Marshal.PtrToStringAnsi(Value);
202 Interop.Libc.Free(Value);
208 /// The network type.
210 /// <since_tizen> 3 </since_tizen>
211 /// <value>Profile type of the network connection.</value>
212 public ConnectionProfileType Type
217 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
218 if ((ConnectionError)ret != ConnectionError.None)
220 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
222 return (ConnectionProfileType)Value;
227 /// The name of the network interface.
229 /// <since_tizen> 3 </since_tizen>
230 /// <value>Network interface name, for example, eth0 and pdp0.</value>
231 public string InterfaceName
236 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
237 if ((ConnectionError)ret != ConnectionError.None)
239 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
241 string result = Marshal.PtrToStringAnsi(Value);
242 Interop.Libc.Free(Value);
248 /// Refreshes the profile information.
250 /// <since_tizen> 3 </since_tizen>
251 /// <privilege>http://tizen.org/privilege/network.get</privilege>
252 /// <feature>http://tizen.org/feature/network.ethernet</feature>
253 /// <feature>http://tizen.org/feature/network.telephony</feature>
254 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
255 /// <feature>http://tizen.org/feature/network.wifi</feature>
256 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
257 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
258 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
259 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
260 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
261 public void Refresh()
264 int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
265 if ((ConnectionError)ret != ConnectionError.None)
267 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
268 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");
269 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
270 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
271 ConnectionErrorFactory.ThrowConnectionException(ret);
276 /// Gets the network state.
278 /// <since_tizen> 3 </since_tizen>
279 /// <param name="family">The address family.</param>
280 /// <returns>The network state.</returns>
281 /// <feature>http://tizen.org/feature/network.ethernet</feature>
282 /// <feature>http://tizen.org/feature/network.telephony</feature>
283 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
284 /// <feature>http://tizen.org/feature/network.wifi</feature>
285 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
286 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
287 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
288 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
289 public ProfileState GetState(AddressFamily family)
293 int ret = (int)ConnectionError.None;
294 if (family == AddressFamily.IPv4)
296 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
301 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
304 if ((ConnectionError)ret != ConnectionError.None)
306 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
307 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");
308 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
309 ConnectionErrorFactory.ThrowConnectionException(ret);
312 return (ProfileState)Value;
318 /// <since_tizen> 3 </since_tizen>
319 /// <value>Proxy type of the connection.</value>
320 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
321 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
322 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
323 /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
324 public ProxyType ProxyType
329 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
330 if ((ConnectionError)ret != ConnectionError.None)
332 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
334 return (ProxyType)Value;
341 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
342 if ((ConnectionError)ret != ConnectionError.None)
344 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
345 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");
346 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
347 ConnectionErrorFactory.ThrowConnectionException(ret);
353 /// The proxy address.
355 /// <since_tizen> 3 </since_tizen>
356 /// <value>Proxy address of the connection.</value>
357 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
358 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
359 /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
360 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
361 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
362 public string ProxyAddress
367 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
368 if ((ConnectionError)ret != ConnectionError.None)
370 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
372 string result = Marshal.PtrToStringAnsi(Value);
373 Interop.Libc.Free(Value);
383 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
384 if ((ConnectionError)ret != ConnectionError.None)
386 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
387 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");
388 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
389 ConnectionErrorFactory.ThrowConnectionException(ret);
395 throw new ArgumentNullException("ProxyAddress is null");
401 /// The address information (IPv4).
403 /// <since_tizen> 3 </since_tizen>
404 /// <value>Instance of IAddressInformation with IPV4 address.</value>
405 public IAddressInformation IPv4Settings
415 /// The address information (IPv6).
417 /// <since_tizen> 3 </since_tizen>
418 /// <value>Instance of IAddressInformation with IPV6 address.</value>
419 public IAddressInformation IPv6Settings
429 /// An extended EventArgs class, which contains changed profile state.
431 /// <since_tizen> 3 </since_tizen>
432 public class ProfileStateEventArgs : EventArgs
434 private ProfileState _State = ProfileState.Disconnected;
436 internal ProfileStateEventArgs(ProfileState state)
442 /// The profile state.
444 /// <since_tizen> 3 </since_tizen>
445 /// <value>State type of the connection profile.</value>
446 public ProfileState State