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 Class is ConnectionProfile. It provides event and propeties 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 _ProfileStateChanged = null;
37 private Interop.ConnectionProfile.ProfileStateChangedCallback _profileChangedCallback;
39 internal IntPtr GetHandle()
45 /// The event that 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 public event EventHandler ProfileStateChanged
56 Log.Debug(Globals.LogTag, "ProfileStateChanged add");
57 if (_ProfileStateChanged == null)
59 ProfileStateChangedStart();
61 _ProfileStateChanged += value;
65 Log.Debug(Globals.LogTag, "ProfileStateChanged remove");
66 _ProfileStateChanged -= value;
67 if (_ProfileStateChanged == null)
69 ProfileStateChangedStop();
74 private void ProfileStateChangedStart()
76 _profileChangedCallback = (ProfileState state, IntPtr userData) =>
78 if (_ProfileStateChanged != null)
80 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
84 Log.Debug(Globals.LogTag, "ProfileStateChangedStart");
85 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
86 if ((ConnectionError)ret != ConnectionError.None)
88 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
92 private void ProfileStateChangedStop()
94 Log.Debug(Globals.LogTag, "ProfileStateChangedStop");
95 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
96 if ((ConnectionError)ret != ConnectionError.None)
98 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
102 internal ConnectionProfile(IntPtr handle)
104 ProfileHandle = handle;
105 IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
106 IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
115 /// Disposes the memory allocated to unmanaged resources.
117 /// <since_tizen> 3 </since_tizen>
118 public void Dispose()
121 GC.SuppressFinalize(this);
124 private void Dispose(bool disposing)
126 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
132 // Free managed objects.
139 private void UnregisterEvents()
141 if (_ProfileStateChanged != null)
143 ProfileStateChangedStop();
147 private void Destroy()
149 Interop.ConnectionProfile.Destroy(ProfileHandle);
150 ProfileHandle = IntPtr.Zero;
153 internal void CheckDisposed()
157 throw new ObjectDisposedException(GetType().FullName);
164 /// <since_tizen> 3 </since_tizen>
165 /// <value>Unique ID of the profile.</value>
171 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
172 if ((ConnectionError)ret != ConnectionError.None)
174 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
176 string result = Marshal.PtrToStringAnsi(Value);
177 Interop.Libc.Free(Value);
183 /// The profile name.
185 /// <since_tizen> 3 </since_tizen>
186 /// <value>User friendly name of the profile.</value>
192 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
193 if ((ConnectionError)ret != ConnectionError.None)
195 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
197 string result = Marshal.PtrToStringAnsi(Value);
198 Interop.Libc.Free(Value);
204 /// The network type.
206 /// <since_tizen> 3 </since_tizen>
207 /// <value>Profile type of the network connection.</value>
208 public ConnectionProfileType Type
213 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
214 if ((ConnectionError)ret != ConnectionError.None)
216 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
218 return (ConnectionProfileType)Value;
223 /// The name of the network interface.
225 /// <since_tizen> 3 </since_tizen>
226 /// <value>Network interface name, e.g. eth0 and pdp0.</value>
227 public string InterfaceName
232 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
233 if ((ConnectionError)ret != ConnectionError.None)
235 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
237 string result = Marshal.PtrToStringAnsi(Value);
238 Interop.Libc.Free(Value);
244 /// Refreshes the profile information.
246 /// <since_tizen> 3 </since_tizen>
247 /// <privilege>http://tizen.org/privilege/network.get</privilege>
248 /// <feature>http://tizen.org/feature/network.ethernet</feature>
249 /// <feature>http://tizen.org/feature/network.telephony</feature>
250 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
251 /// <feature>http://tizen.org/feature/network.wifi</feature>
252 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
253 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
254 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
255 /// <exception cref="System.InvalidOperationException">Thrown when profile instance is invalid or when method failed due to invalid operation</exception>
256 /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
257 public void Refresh()
260 int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
261 if ((ConnectionError)ret != ConnectionError.None)
263 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
264 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");
265 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
266 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
267 ConnectionErrorFactory.ThrowConnectionException(ret);
272 /// Get the network state.
274 /// <since_tizen> 3 </since_tizen>
275 /// <param name="family">The address family</param>
276 /// <returns>The network state.</returns>
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 feature is not supported.</exception>
282 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
283 /// <exception cref="System.InvalidOperationException">Thrown when profile instance is invalid or when method failed due to invalid operation</exception>
284 /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
285 public ProfileState GetState(AddressFamily family)
289 int ret = (int)ConnectionError.None;
290 if (family == AddressFamily.IPv4)
292 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
297 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
300 if ((ConnectionError)ret != ConnectionError.None)
302 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
303 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");
304 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
305 ConnectionErrorFactory.ThrowConnectionException(ret);
308 return (ProfileState)Value;
314 /// <since_tizen> 3 </since_tizen>
315 /// <value>Proxy type of the connection.</value>
316 /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
317 /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
318 /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
319 /// <exception cref="System.ObjectDisposedException">Thrown during set when operation is performed on a disposed object.</exception>
320 public ProxyType ProxyType
325 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
326 if ((ConnectionError)ret != ConnectionError.None)
328 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
330 return (ProxyType)Value;
337 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
338 if ((ConnectionError)ret != ConnectionError.None)
340 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
341 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");
342 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
343 ConnectionErrorFactory.ThrowConnectionException(ret);
349 /// The proxy address.
351 /// <since_tizen> 3 </since_tizen>
352 /// <value>Proxy address of the connection.</value>
353 /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
354 /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
355 /// <exception cref="System.ArgumentNullException">Thrown during set when value is null.</exception>
356 /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
357 /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
358 public string ProxyAddress
363 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
364 if ((ConnectionError)ret != ConnectionError.None)
366 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
368 string result = Marshal.PtrToStringAnsi(Value);
369 Interop.Libc.Free(Value);
379 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
380 if ((ConnectionError)ret != ConnectionError.None)
382 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
383 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");
384 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
385 ConnectionErrorFactory.ThrowConnectionException(ret);
391 throw new ArgumentNullException("ProxyAddress is null");
397 /// The address information (IPv4)
399 /// <since_tizen> 3 </since_tizen>
400 /// <value>Instance of IAddressInformation with IPV4 address.</value>
401 public IAddressInformation IPv4Settings
411 /// The address information (IPv6)
413 /// <since_tizen> 3 </since_tizen>
414 /// <value>Instance of IAddressInformation with IPV6 address.</value>
415 public IAddressInformation IPv6Settings
425 /// An extended EventArgs class which contains changed profile state.
427 /// <since_tizen> 3 </since_tizen>
428 public class ProfileStateEventArgs : EventArgs
430 private ProfileState _State = ProfileState.Disconnected;
432 internal ProfileStateEventArgs(ProfileState state)
438 /// The profile state.
440 /// <since_tizen> 3 </since_tizen>
441 /// <value>State type of the connection profile.</value>
442 public ProfileState State