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.Info(Globals.LogTag, "ProfileStateChanged add");
63 if (_ProfileStateChanged == null)
65 ProfileStateChangedStart();
67 _ProfileStateChanged += value;
72 Log.Info(Globals.LogTag, "ProfileStateChanged remove");
75 _ProfileStateChanged -= value;
76 if (_ProfileStateChanged == null)
78 ProfileStateChangedStop();
84 private void ProfileStateChangedStart()
86 _profileChangedCallback = (ProfileState state, IntPtr userData) =>
88 Log.Info(Globals.LogTag, "***** MOON ProfileStateChanged occur");
89 if (_ProfileStateChanged != null)
91 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
95 Log.Info(Globals.LogTag, "ProfileStateChangedStart");
96 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
97 if ((ConnectionError)ret != ConnectionError.None)
99 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
103 private void ProfileStateChangedStop()
105 Log.Info(Globals.LogTag, "ProfileStateChangedStop");
106 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
107 if ((ConnectionError)ret != ConnectionError.None)
109 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
113 internal ConnectionProfile(IntPtr handle)
115 ProfileHandle = handle;
116 IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
117 IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
121 /// Destroy the ConnectionProfile object
129 /// Disposes the memory allocated to unmanaged resources.
131 /// <since_tizen> 3 </since_tizen>
132 public void Dispose()
135 GC.SuppressFinalize(this);
138 private void Dispose(bool disposing)
140 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
144 // Free unmanaged objects
150 private void UnregisterEvents()
152 if (_ProfileStateChanged != null)
154 ProfileStateChangedStop();
158 private void Destroy()
160 Interop.ConnectionProfile.Destroy(ProfileHandle);
161 ProfileHandle = IntPtr.Zero;
164 internal void CheckDisposed()
168 throw new ObjectDisposedException(GetType().FullName);
175 /// <since_tizen> 3 </since_tizen>
176 /// <value>Unique ID of the profile.</value>
182 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
183 if ((ConnectionError)ret != ConnectionError.None)
185 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
187 string result = Marshal.PtrToStringAnsi(Value);
188 Interop.Libc.Free(Value);
194 /// The profile name.
196 /// <since_tizen> 3 </since_tizen>
197 /// <value>User friendly name of the profile.</value>
203 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
204 if ((ConnectionError)ret != ConnectionError.None)
206 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
208 string result = Marshal.PtrToStringAnsi(Value);
209 Interop.Libc.Free(Value);
215 /// The network type.
217 /// <since_tizen> 3 </since_tizen>
218 /// <value>Profile type of the network connection.</value>
219 public ConnectionProfileType Type
224 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
225 if ((ConnectionError)ret != ConnectionError.None)
227 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
229 return (ConnectionProfileType)Value;
234 /// The name of the network interface.
236 /// <since_tizen> 3 </since_tizen>
237 /// <value>Network interface name, for example, eth0 and pdp0.</value>
238 public string InterfaceName
243 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
244 if ((ConnectionError)ret != ConnectionError.None)
246 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
248 string result = Marshal.PtrToStringAnsi(Value);
249 Interop.Libc.Free(Value);
255 /// Refreshes the profile information.
257 /// <since_tizen> 3 </since_tizen>
258 /// <privilege>http://tizen.org/privilege/network.get</privilege>
259 /// <feature>http://tizen.org/feature/network.ethernet</feature>
260 /// <feature>http://tizen.org/feature/network.telephony</feature>
261 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
262 /// <feature>http://tizen.org/feature/network.wifi</feature>
263 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
264 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
265 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
266 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
267 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
268 public void Refresh()
271 int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
272 if ((ConnectionError)ret != ConnectionError.None)
274 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
275 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");
276 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
277 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
278 ConnectionErrorFactory.ThrowConnectionException(ret);
283 /// Gets the network state.
285 /// <since_tizen> 3 </since_tizen>
286 /// <param name="family">The address family.</param>
287 /// <returns>The network state.</returns>
288 /// <feature>http://tizen.org/feature/network.ethernet</feature>
289 /// <feature>http://tizen.org/feature/network.telephony</feature>
290 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
291 /// <feature>http://tizen.org/feature/network.wifi</feature>
292 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
293 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
294 /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
295 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
296 public ProfileState GetState(AddressFamily family)
300 int ret = (int)ConnectionError.None;
301 if (family == AddressFamily.IPv4)
303 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
308 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
311 if ((ConnectionError)ret != ConnectionError.None)
313 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
314 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");
315 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
316 ConnectionErrorFactory.ThrowConnectionException(ret);
319 return (ProfileState)Value;
325 /// <since_tizen> 3 </since_tizen>
326 /// <value>Proxy type of the connection.</value>
327 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
328 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
329 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
330 /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
331 public ProxyType ProxyType
336 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
337 if ((ConnectionError)ret != ConnectionError.None)
339 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
341 return (ProxyType)Value;
348 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
349 if ((ConnectionError)ret != ConnectionError.None)
351 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
352 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");
353 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
354 ConnectionErrorFactory.ThrowConnectionException(ret);
360 /// The proxy address.
362 /// <since_tizen> 3 </since_tizen>
363 /// <value>Proxy address of the connection.</value>
364 /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
365 /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
366 /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
367 /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
368 /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
369 public string ProxyAddress
374 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
375 if ((ConnectionError)ret != ConnectionError.None)
377 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
379 string result = Marshal.PtrToStringAnsi(Value);
380 Interop.Libc.Free(Value);
390 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
391 if ((ConnectionError)ret != ConnectionError.None)
393 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
394 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");
395 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
396 ConnectionErrorFactory.ThrowConnectionException(ret);
402 throw new ArgumentNullException("ProxyAddress is null");
408 /// The address information (IPv4).
410 /// <since_tizen> 3 </since_tizen>
411 /// <value>Instance of IAddressInformation with IPV4 address.</value>
412 public IAddressInformation IPv4Settings
422 /// The address information (IPv6).
424 /// <since_tizen> 3 </since_tizen>
425 /// <value>Instance of IAddressInformation with IPV6 address.</value>
426 public IAddressInformation IPv6Settings
436 /// An extended EventArgs class, which contains changed profile state.
438 /// <since_tizen> 3 </since_tizen>
439 public class ProfileStateEventArgs : EventArgs
441 private ProfileState _State = ProfileState.Disconnected;
443 internal ProfileStateEventArgs(ProfileState state)
449 /// The profile state.
451 /// <since_tizen> 3 </since_tizen>
452 /// <value>State type of the connection profile.</value>
453 public ProfileState State