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.Threading.Tasks;
22 using System.Runtime.InteropServices;
25 /// The Connection API provides functions, enumerations to get the status of network and current profile and manage profiles.
27 namespace Tizen.Network.Connection
30 /// This class is ConnectionManager
32 public class ConnectionManager : IDisposable
34 static internal ConnectionItem CurConnction = new ConnectionItem();
35 private bool disposed = false;
37 static private EventHandler _ConnectionTypeChanged = null;
38 static private EventHandler _IPAddressChanged = null;
39 static private EventHandler _EthernetCableStateChanged = null;
40 static private EventHandler _ProxyAddressChanged = null;
43 /// Event that is called when the type of the current connection is changed.
45 static public event EventHandler ConnectionTypeChanged
49 if (_ConnectionTypeChanged == null)
51 ConnectionTypeChangedStart();
53 _ConnectionTypeChanged += value;
57 _ConnectionTypeChanged -= value;
58 if (_ConnectionTypeChanged == null)
60 ConnectionTypeChangedStop();
65 static private void ConnectionTypeChangedStart()
67 int ret = Interop.Connection.SetTypeChangedCallback(ConnectionInternalManager.GetHandle(), TypeChangedCallback, IntPtr.Zero);
68 if ((ConnectionError)ret != ConnectionError.None)
70 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
74 static private void ConnectionTypeChangedStop()
76 int ret = Interop.Connection.UnsetTypeChangedCallback(ConnectionInternalManager.GetHandle());
77 if ((ConnectionError)ret != ConnectionError.None)
79 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
83 static private void TypeChangedCallback(ConnectionType type, IntPtr user_data)
85 if (_ConnectionTypeChanged != null)
87 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
92 /// Event for ethernet cable is plugged [in/out] event.
94 static public event EventHandler EthernetCableStateChanged
98 if (_EthernetCableStateChanged == null)
100 EthernetCableStateChangedStart();
102 _EthernetCableStateChanged += value;
106 _EthernetCableStateChanged -= value;
107 if (_EthernetCableStateChanged == null)
109 EthernetCableStateChangedtop();
114 static private void EthernetCableStateChangedStart()
116 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(ConnectionInternalManager.GetHandle(), EthernetCableStateChangedCallback, IntPtr.Zero);
117 if ((ConnectionError)ret != ConnectionError.None)
119 Log.Error(Globals.LogTag, "It failed to register ethernet cable state changed callback, " + (ConnectionError)ret);
123 static private void EthernetCableStateChangedtop()
125 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(ConnectionInternalManager.GetHandle());
126 if ((ConnectionError)ret != ConnectionError.None)
128 Log.Error(Globals.LogTag, "It failed to unregister ethernet cable state changed callback, " + (ConnectionError)ret);
132 static private void EthernetCableStateChangedCallback(EthernetCableState state, IntPtr user_data)
134 if (_EthernetCableStateChanged != null)
136 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
141 /// Event that is called when the IP address is changed.
143 static public event EventHandler IpAddressChanged
147 if (_IPAddressChanged == null)
149 IpAddressChangedStart();
151 _IPAddressChanged += value;
155 _IPAddressChanged -= value;
156 if (_IPAddressChanged == null)
158 IpAddressChangedStop();
163 static private void IpAddressChangedStart()
165 int ret = Interop.Connection.SetIpAddressChangedCallback(ConnectionInternalManager.GetHandle(), IPAddressChangedCallback, IntPtr.Zero);
166 if ((ConnectionError)ret != ConnectionError.None)
168 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
172 static private void IpAddressChangedStop()
174 int ret = Interop.Connection.UnsetIpAddressChangedCallback(ConnectionInternalManager.GetHandle());
175 if ((ConnectionError)ret != ConnectionError.None)
177 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
181 static private void IPAddressChangedCallback(IntPtr Ipv4, IntPtr Ipv6, IntPtr UserData)
183 if (_IPAddressChanged != null)
185 string ipv4 = Marshal.PtrToStringAnsi(Ipv4);
186 string ipv6 = Marshal.PtrToStringAnsi(Ipv6);
188 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
190 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
196 /// Event that is called when the proxy address is changed.
198 static public event EventHandler ProxyAddressChanged
202 //Console.WriteLine("ProxyAddressChanged Add **");
203 if (_ProxyAddressChanged == null)
205 ProxyAddressChangedStart();
207 _ProxyAddressChanged += value;
211 //Console.WriteLine("ProxyAddressChanged Remove");
212 _ProxyAddressChanged -= value;
213 if (_ProxyAddressChanged == null)
215 ProxyAddressChangedStop();
220 static private void ProxyAddressChangedStart()
222 int ret = Interop.Connection.SetProxyAddressChangedCallback(ConnectionInternalManager.GetHandle(), IPAddressChangedCallback, IntPtr.Zero);
223 if ((ConnectionError)ret != ConnectionError.None)
225 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
229 static private void ProxyAddressChangedStop()
231 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(ConnectionInternalManager.GetHandle());
232 if ((ConnectionError)ret != ConnectionError.None)
234 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
238 static private void ProxyAddressChangedCallback(IntPtr Ipv4, IntPtr Ipv6, IntPtr UserData)
240 if (_ProxyAddressChanged != null)
242 string ipv4 = Marshal.PtrToStringAnsi(Ipv4);
243 string ipv6 = Marshal.PtrToStringAnsi(Ipv6);
244 Interop.Libc.Free(Ipv4);
245 Interop.Libc.Free(Ipv6);
247 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
251 internal ConnectionManager()
260 public void Dispose()
263 GC.SuppressFinalize(this);
266 private void Dispose(bool disposing)
273 // Free managed objects.
275 ProxyAddressChangedStop();
276 ConnectionTypeChangedStop();
277 EthernetCableStateChangedtop();
278 IpAddressChangedStop();
284 /// Gets the IP address of the current connection.
286 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
287 static public string GetIpAddress(AddressFamily family)
289 return ConnectionInternalManager.GetIpAddress(family);
293 /// Gets the proxy address of the current connection.
295 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
296 static public string GetProxy(AddressFamily family)
298 return ConnectionInternalManager.GetProxy(family);
302 /// Gets the MAC address of the Wi-Fi or ethernet.
304 /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
305 static public string GetMacAddress(ConnectionType type)
307 return ConnectionInternalManager.GetMacAddress(type);
311 /// Gets the type of the current profile for data connection.
313 static public ConnectionItem CurrentConnection
322 /// Gets the state of cellular connection.
324 static public CellularState CellularState
328 return ConnectionInternalManager.CellularState;
333 /// Gets the state of the Wi-Fi.
335 /// <privilege>http://tizen.org/privilege/network.get</privilege>
336 static public ConnectionState WiFiState
340 return ConnectionInternalManager.WiFiState;
345 /// The state of the Bluetooth.
347 /// <privilege>http://tizen.org/privilege/network.get</privilege>
348 static public ConnectionState BluetoothState
352 return ConnectionInternalManager.BluetoothState;
357 /// The Ethernet connection state.
359 /// <privilege>http://tizen.org/privilege/network.get</privilege>
360 static public ConnectionState EthernetState
364 return ConnectionInternalManager.EthernetState;
369 /// Checks for ethernet cable is attached or not.
371 /// <privilege>http://tizen.org/privilege/network.get</privilege>
372 static public EthernetCableState EthernetCableState
376 return ConnectionInternalManager.EthernetCableState;
384 public class ConnectionItem
386 internal ConnectionItem()
391 /// Gets the type of the current profile for data connection.
393 public ConnectionType Type
397 return ConnectionInternalManager.ConnectionType;
402 /// Gets the type of the current profile for data connection.
404 public ConnectionState State
408 if (ConnectionInternalManager.ConnectionType == ConnectionType.Cellular)
410 if (ConnectionInternalManager.CellularState == CellularState.Connected)
412 return ConnectionState.Connected;
414 else if (ConnectionInternalManager.CellularState == CellularState.Available)
416 return ConnectionState.Disconnected;
419 return ConnectionState.Deactivated;
422 else if (ConnectionInternalManager.ConnectionType == ConnectionType.Bluetooth)
424 return ConnectionInternalManager.BluetoothState;
426 else if (ConnectionInternalManager.ConnectionType == ConnectionType.WiFi)
428 return ConnectionInternalManager.WiFiState;
430 else if (ConnectionInternalManager.ConnectionType == ConnectionType.Ethernet)
432 return ConnectionInternalManager.EthernetState;
434 else { // TO DO : Add Net Proxy
435 return ConnectionState.Disconnected;
443 /// An extended EventArgs class which contains changed connection type.
445 public class ConnectionTypeEventArgs : EventArgs
447 private ConnectionType Type = ConnectionType.Disconnected;
449 internal ConnectionTypeEventArgs(ConnectionType type)
455 /// The connection type.
457 public ConnectionType ConnectionType
467 /// An extended EventArgs class which contains changed ethernet cable state.
469 public class EthernetCableStateEventArgs : EventArgs
471 private EthernetCableState State;
473 internal EthernetCableStateEventArgs(EthernetCableState state)
479 /// The ethernet cable state.
481 public EthernetCableState EthernetCableState
491 /// An extended EventArgs class which contains changed address.
493 public class AddressEventArgs : EventArgs
495 private string Ipv4 = "";
496 private string Ipv6 = "";
498 internal AddressEventArgs(string ipv4, string ipv6)
505 /// The IPV4 address.
507 public string Ipv4Address
516 /// The IPV6 address.
518 public string Ipv6Address