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;
37 /// The event that is called when the state of profile is changed.
39 public event EventHandler ProfileStateChanged
43 if (_ProfileStateChanged == null)
45 ProfileStateChangedStart();
47 _ProfileStateChanged += value;
51 _ProfileStateChanged -= value;
52 if (_ProfileStateChanged == null)
54 ProfileStateChangedStop();
59 private void TypeChangedCallback(ProfileState state, IntPtr userData)
61 if (_ProfileStateChanged != null)
63 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
67 private void ProfileStateChangedStart()
69 int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, TypeChangedCallback, IntPtr.Zero);
70 if ((ConnectionError)ret != ConnectionError.None)
72 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
76 private void ProfileStateChangedStop()
78 int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
79 if ((ConnectionError)ret != ConnectionError.None)
81 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
85 public ConnectionProfile(IntPtr handle)
87 ProfileHandle = handle;
88 Ipv4 = AddressFactory.CreateAddressInformation(handle, AddressFamily.Ipv4, AddressInformationType.Connection);
89 Ipv6 = AddressFactory.CreateAddressInformation(handle, AddressFamily.Ipv6, AddressInformationType.Connection);
100 GC.SuppressFinalize(this);
103 private void Dispose(bool disposing)
110 // Free managed objects.
112 Interop.ConnectionProfile.Destroy(ProfileHandle);
113 ProfileStateChangedStop();
118 /// Gets the profile ID.
125 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
126 if ((ConnectionError)ret != ConnectionError.None)
128 Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
130 string result = Marshal.PtrToStringAnsi(Value);
131 Interop.Libc.Free(Value);
137 /// Gets the profile name.
139 /// <privilege>http://tizen.org/privilege/network.get</privilege>
145 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
146 if ((ConnectionError)ret != ConnectionError.None)
148 Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
150 string result = Marshal.PtrToStringAnsi(Value);
151 Interop.Libc.Free(Value);
157 /// Gets the network type.
159 public ConnectionProfileType Type
164 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
165 if ((ConnectionError)ret != ConnectionError.None)
167 Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
169 return (ConnectionProfileType)Value;
174 /// Gets the name of the network interface, e.g. eth0 and pdp0.
176 public string InterfaceName
181 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
182 if ((ConnectionError)ret != ConnectionError.None)
184 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
186 string result = Marshal.PtrToStringAnsi(Value);
187 Interop.Libc.Free(Value);
193 /// Gets the profile state.
195 public ProfileState State
200 int ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
201 if ((ConnectionError)ret != ConnectionError.None)
203 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
205 return (ProfileState)Value;
210 /// Gets the Proxy type.
212 public ProxyType ProxyType
217 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
218 if ((ConnectionError)ret != ConnectionError.None)
220 Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
222 return (ProxyType)Value;
227 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
228 if ((ConnectionError)ret != ConnectionError.None)
230 Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
231 ConnectionErrorFactory.ThrowConnectionException(ret);
237 /// The proxy address.
239 public String ProxyAddress
244 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value);
245 if ((ConnectionError)ret != ConnectionError.None)
247 Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
249 string result = Marshal.PtrToStringAnsi(Value);
250 Interop.Libc.Free(Value);
256 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString());
257 if ((ConnectionError)ret != ConnectionError.None)
259 Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
260 ConnectionErrorFactory.ThrowConnectionException(ret);
266 /// The subnet mask address(Ipv4).
268 public IAddressInformation Ipv4Settings
278 /// The subnet mask address(Ipv4).
280 public IAddressInformation Ipv6Settings
290 /// An extended EventArgs class which contains changed profile state.
292 public class ProfileStateEventArgs : EventArgs
294 private ProfileState _State = ProfileState.Disconnected;
296 internal ProfileStateEventArgs(ProfileState state)
302 /// The profile state.
304 public ProfileState State