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
28 public class ConnectionProfile : IDisposable
30 internal IntPtr ProfileHandle = IntPtr.Zero;
31 private IAddressInformation Ipv4;
32 private IAddressInformation Ipv6;
33 private bool disposed = false;
34 private EventHandler _ProfileStateChanged;
36 internal IntPtr GetHandle()
42 /// The event that is called when the state of profile is changed.
44 public event EventHandler ProfileStateChanged
48 if (_ProfileStateChanged == null)
50 ProfileStateChangedStart();
52 _ProfileStateChanged += value;
56 _ProfileStateChanged -= value;
57 if (_ProfileStateChanged == null)
59 ProfileStateChangedStop();
64 private void TypeChangedCallback(ProfileState state, IntPtr userData)
66 if (_ProfileStateChanged != null)
68 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
72 private void ProfileStateChangedStart()
74 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, TypeChangedCallback, IntPtr.Zero);
75 if ((ConnectionError)ret != ConnectionError.None)
77 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
81 private void ProfileStateChangedStop()
83 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
84 if ((ConnectionError)ret != ConnectionError.None)
86 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
90 internal ConnectionProfile(IntPtr handle)
92 ProfileHandle = handle;
93 Ipv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.Ipv4);
94 Ipv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.Ipv6);
102 public void Dispose()
105 GC.SuppressFinalize(this);
108 private void Dispose(bool disposing)
110 Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
116 // Free managed objects.
123 private void UnregisterEvents()
125 if (_ProfileStateChanged != null)
127 ProfileStateChangedStop();
131 private void Destroy()
133 Interop.ConnectionProfile.Destroy(ProfileHandle);
137 /// Gets the profile ID.
144 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
145 if ((ConnectionError)ret != ConnectionError.None)
147 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
149 string result = Marshal.PtrToStringAnsi(Value);
150 Interop.Libc.Free(Value);
156 /// Gets the profile name.
158 /// <privilege>http://tizen.org/privilege/network.get</privilege>
164 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
165 if ((ConnectionError)ret != ConnectionError.None)
167 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
169 string result = Marshal.PtrToStringAnsi(Value);
170 Interop.Libc.Free(Value);
176 /// Gets the network type.
178 public ConnectionProfileType Type
183 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
184 if ((ConnectionError)ret != ConnectionError.None)
186 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
188 return (ConnectionProfileType)Value;
193 /// Gets the name of the network interface, e.g. eth0 and pdp0.
195 public string InterfaceName
200 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
201 if ((ConnectionError)ret != ConnectionError.None)
203 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
205 string result = Marshal.PtrToStringAnsi(Value);
206 Interop.Libc.Free(Value);
212 /// Gets the profile state.
214 public ProfileState State
219 int ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
220 if ((ConnectionError)ret != ConnectionError.None)
222 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
224 return (ProfileState)Value;
229 /// Gets the Proxy type.
231 public ProxyType ProxyType
236 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
237 if ((ConnectionError)ret != ConnectionError.None)
239 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
241 return (ProxyType)Value;
246 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
247 if ((ConnectionError)ret != ConnectionError.None)
249 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
250 ConnectionErrorFactory.ThrowConnectionException(ret);
256 /// The proxy address.
258 public String ProxyAddress
263 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value);
264 if ((ConnectionError)ret != ConnectionError.None)
266 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
268 string result = Marshal.PtrToStringAnsi(Value);
269 Interop.Libc.Free(Value);
275 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString());
276 if ((ConnectionError)ret != ConnectionError.None)
278 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
279 ConnectionErrorFactory.ThrowConnectionException(ret);
285 /// The subnet mask address(Ipv4).
287 public IAddressInformation Ipv4Settings
297 /// The subnet mask address(Ipv4).
299 public IAddressInformation Ipv6Settings
309 /// An extended EventArgs class which contains changed profile state.
311 public class ProfileStateEventArgs : EventArgs
313 private ProfileState _State = ProfileState.Disconnected;
315 internal ProfileStateEventArgs(ProfileState state)
321 /// The profile state.
323 public ProfileState State