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;
19 using System.ComponentModel;
22 using System.Threading.Tasks;
23 using System.Runtime.InteropServices;
26 /// The Connection API provides functions and enumerations to get the status of network and current profile and manages profiles.
28 namespace Tizen.Network.Connection
31 /// This class manages the connection handle resources.
33 [EditorBrowsable(EditorBrowsableState.Never)]
34 public sealed class SafeConnectionHandle : SafeHandle
36 internal SafeConnectionHandle(IntPtr handle) : base(handle, true)
41 /// Checks whether the handle value is valid or not.
43 /// <value>True if the handle is invalid, otherwise false.</value>
44 public override bool IsInvalid
48 return this.handle == IntPtr.Zero;
55 /// <returns>True if the handle is released successfully, otherwise false.</returns>
56 protected override bool ReleaseHandle()
58 this.SetHandle(IntPtr.Zero);
64 /// This is the ConnectionManager class. It provides functions to manage data connections.
66 /// <since_tizen> 3 </since_tizen>
67 public static class ConnectionManager
69 private static ConnectionItem _currentConnection = null;
72 /// An event is called when the type of the current connection is changed.
74 /// <since_tizen> 3 </since_tizen>
75 /// <privilege>http://tizen.org/privilege/network.get</privilege>
76 /// <feature>http://tizen.org/feature/network.ethernet</feature>
77 /// <feature>http://tizen.org/feature/network.telephony</feature>
78 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
79 /// <feature>http://tizen.org/feature/network.wifi</feature>
80 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
81 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
82 public static event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
86 ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
91 ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
96 /// An event for the ethernet cable is plugged [in/out] event.
98 /// <since_tizen> 3 </since_tizen>
99 /// <privilege>http://tizen.org/privilege/network.get</privilege>
100 /// <feature>http://tizen.org/feature/network.ethernet</feature>
101 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
102 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
103 public static event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
107 ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
112 ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
117 /// An event is called when the IP address is changed.
119 /// <since_tizen> 3 </since_tizen>
120 /// <privilege>http://tizen.org/privilege/network.get</privilege>
121 /// <feature>http://tizen.org/feature/network.ethernet</feature>
122 /// <feature>http://tizen.org/feature/network.telephony</feature>
123 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
124 /// <feature>http://tizen.org/feature/network.wifi</feature>
125 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
126 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
127 public static event EventHandler<AddressEventArgs> IPAddressChanged
131 ConnectionInternalManager.Instance.IPAddressChanged += value;
136 ConnectionInternalManager.Instance.IPAddressChanged -= value;
141 /// An event is called when the proxy address is changed.
143 /// <since_tizen> 3 </since_tizen>
144 /// <privilege>http://tizen.org/privilege/network.get</privilege>
145 /// <feature>http://tizen.org/feature/network.ethernet</feature>
146 /// <feature>http://tizen.org/feature/network.telephony</feature>
147 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
148 /// <feature>http://tizen.org/feature/network.wifi</feature>
149 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
150 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
151 public static event EventHandler<AddressEventArgs> ProxyAddressChanged
155 ConnectionInternalManager.Instance.ProxyAddressChanged += value;
160 ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
165 /// Gets the connection handle.
167 /// <since_tizen> 3 </since_tizen>
168 /// <returns>Instance of SafeConnectionHandle.</returns>
169 [EditorBrowsable(EditorBrowsableState.Never)]
170 public static SafeConnectionHandle GetConnectionHandle()
172 IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
173 return new SafeConnectionHandle(handle);
177 /// Gets the IP address of the current connection.
179 /// <since_tizen> 3 </since_tizen>
180 /// <param name="family">The address family.</param>
181 /// <returns>IP address of the connection (global address in case of IPv6).</returns>
182 /// <privilege>http://tizen.org/privilege/network.get</privilege>
183 /// <feature>http://tizen.org/feature/network.ethernet</feature>
184 /// <feature>http://tizen.org/feature/network.telephony</feature>
185 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
186 /// <feature>http://tizen.org/feature/network.wifi</feature>
187 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
188 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
189 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
190 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
191 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
192 public static System.Net.IPAddress GetIPAddress(AddressFamily family)
194 return ConnectionInternalManager.Instance.GetIPAddress(family);
198 /// Gets all the IPv6 addresses of the current connection.
200 /// <since_tizen> 3 </since_tizen>
201 /// <param name="type">The type of current network connection.</param>
202 /// <returns>A list of IPv6 addresses of the connection.</returns>
203 /// <privilege>http://tizen.org/privilege/network.get</privilege>
204 /// <feature>http://tizen.org/feature/network.ethernet</feature>
205 /// <feature>http://tizen.org/feature/network.telephony</feature>
206 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
207 /// <feature>http://tizen.org/feature/network.wifi</feature>
208 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
209 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
210 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
211 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
212 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
213 public static IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
215 return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
219 /// Gets the proxy address of the current connection.
221 /// <since_tizen> 3 </since_tizen>
222 /// <param name="family">The address family.</param>
223 /// <returns>Proxy address of the connection.</returns>
224 /// <privilege>http://tizen.org/privilege/network.get</privilege>
225 /// <feature>http://tizen.org/feature/network.ethernet</feature>
226 /// <feature>http://tizen.org/feature/network.telephony</feature>
227 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
228 /// <feature>http://tizen.org/feature/network.wifi</feature>
229 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
230 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
231 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
232 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
233 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
234 public static string GetProxy(AddressFamily family)
236 return ConnectionInternalManager.Instance.GetProxy(family);
240 /// Gets the MAC address of the Wi-Fi or ethernet.
242 /// <since_tizen> 3 </since_tizen>
243 /// <param name="type">The type of current network connection.</param>
244 /// <returns>MAC address of the Wi-Fi or ethernet.</returns>
245 /// <privilege>http://tizen.org/privilege/network.get</privilege>
246 /// <feature>http://tizen.org/feature/network.ethernet</feature>
247 /// <feature>http://tizen.org/feature/network.telephony</feature>
248 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
249 /// <feature>http://tizen.org/feature/network.wifi</feature>
250 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
251 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
252 /// <exception cref="System.ArgumentException">Thrown when value is an invalid parameter.</exception>
253 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
254 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
255 public static string GetMacAddress(ConnectionType type)
257 return ConnectionInternalManager.Instance.GetMacAddress(type);
261 /// Gets the statistics information.
263 /// <since_tizen> 3 </since_tizen>
264 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported).</param>
265 /// <param name="statisticsType">The type of statistics.</param>
266 /// <returns>The statistics information associated with statisticsType.</returns>
267 /// <privilege>http://tizen.org/privilege/network.get</privilege>
268 /// <feature>http://tizen.org/feature/network.ethernet</feature>
269 /// <feature>http://tizen.org/feature/network.telephony</feature>
270 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
271 /// <feature>http://tizen.org/feature/network.wifi</feature>
272 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
273 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
274 /// <exception cref="System.ArgumentException">Thrown when value is an invalid parameter.</exception>
275 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
276 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to invalid operation.</exception>
277 public static long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
279 return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
283 /// Resets the statistics information.
285 /// <since_tizen> 3 </since_tizen>
286 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported).</param>
287 /// <param name="statisticsType">The type of statistics.</param>
288 /// <privilege>http://tizen.org/privilege/network.get</privilege>
289 /// <privilege>http://tizen.org/privilege/network.set</privilege>
290 /// <feature>http://tizen.org/feature/network.ethernet</feature>
291 /// <feature>http://tizen.org/feature/network.telephony</feature>
292 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
293 /// <feature>http://tizen.org/feature/network.wifi</feature>
294 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
295 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
296 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
297 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
298 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
299 public static void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
301 ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
305 /// Adds a route to the routing table.
307 /// <param name="family">The address family.</param>
308 /// <param name="interfaceName">The name of the network interface.</param>
309 /// <param name="hostAddress">The IP address of the host.</param>
310 /// <param name="gateway">The gateway address.</param>
311 /// <privilege>http://tizen.org/privilege/network.get</privilege>
312 /// <privilege>http://tizen.org/privilege/network.set</privilege>
313 /// <feature>http://tizen.org/feature/network.telephony</feature>
314 /// <feature>http://tizen.org/feature/network.wifi</feature>
315 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
316 /// <feature>http://tizen.org/feature/network.ethernet</feature>
317 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
318 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
319 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
320 /// <exception cref="System.ArgumentNullException">Thrown when an interfaceName or a hostAddress or a gateway is null.</exception>
321 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
322 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
323 public static void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
325 ConnectionInternalManager.Instance.AddRoute(family, interfaceName, hostAddress, gateway);
329 /// Removes a route from the routing table.
331 /// <param name="family">The address family.</param>
332 /// <param name="interfaceName">The name of network interface.</param>
333 /// <param name="hostAddress">The IP address of the host.</param>
334 /// <param name="gateway">The gateway address.</param>
335 /// <privilege>http://tizen.org/privilege/network.get</privilege>
336 /// <privilege>http://tizen.org/privilege/network.set</privilege>
337 /// <feature>http://tizen.org/feature/network.telephony</feature>
338 /// <feature>http://tizen.org/feature/network.wifi</feature>
339 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
340 /// <feature>http://tizen.org/feature/network.ethernet</feature>
341 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
342 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
343 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
344 /// <exception cref="System.ArgumentNullException">Thrown when an interfaceName or a hostAddress or a gateway is null.</exception>
345 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
346 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
347 public static void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
349 ConnectionInternalManager.Instance.RemoveRoute(family, interfaceName, hostAddress, gateway);
353 /// The type and state of the current profile for data connection.
355 /// <since_tizen> 3 </since_tizen>
356 /// <value>Instance of ConnectionItem.</value>
357 public static ConnectionItem CurrentConnection
361 if (_currentConnection == null)
363 _currentConnection = new ConnectionItem();
366 return _currentConnection;
371 /// Creates a cellular profile handle.
373 /// <since_tizen> 3 </since_tizen>
374 /// <param name="type">The type of profile. Cellular profile type is supported.</param>
375 /// <param name="keyword">The keyword included in profile name.</param>
376 /// <returns>CellularProfile object.</returns>
377 /// <privilege>http://tizen.org/privilege/network.get</privilege>
378 /// <feature>http://tizen.org/feature/network.telephony</feature>
379 /// <feature>http://tizen.org/feature/network.wifi</feature>
380 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
381 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
382 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
383 /// <exception cref="System.ArgumentNullException">Thrown when a keyword value is null.</exception>
384 /// <exception cref="System.InvalidOperationException">Thrown when a method fails due to invalid operation.</exception>
385 public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
387 IntPtr profileHandle = IntPtr.Zero;
388 if (type == ConnectionProfileType.Cellular)
390 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
395 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
396 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
399 return new CellularProfile(profileHandle);
403 /// The state of the cellular connection.
405 /// <since_tizen> 3 </since_tizen>
406 /// <value>Cellular network state.</value>
407 /// <privilege>http://tizen.org/privilege/network.get</privilege>
408 public static CellularState CellularState
412 return ConnectionInternalManager.Instance.CellularState;
417 /// The state of the Wi-Fi connection.
419 /// <since_tizen> 3 </since_tizen>
420 /// <value>WiFi connection state.</value>
421 /// <privilege>http://tizen.org/privilege/network.get</privilege>
422 public static ConnectionState WiFiState
426 return ConnectionInternalManager.Instance.WiFiState;
431 /// The state of the Bluetooth connection.
433 /// <since_tizen> 3 </since_tizen>
434 /// <value>Bluetooth connection state.</value>
435 /// <privilege>http://tizen.org/privilege/network.get</privilege>
436 public static ConnectionState BluetoothState
440 return ConnectionInternalManager.Instance.BluetoothState;
445 /// The Ethernet connection state.
447 /// <since_tizen> 3 </since_tizen>
448 /// <value>Ethernet connection state.</value>
449 /// <privilege>http://tizen.org/privilege/network.get</privilege>
450 public static ConnectionState EthernetState
454 return ConnectionInternalManager.Instance.EthernetState;
459 /// Checks if the ethernet cable is attached or not.
461 /// <since_tizen> 3 </since_tizen>
462 /// <value>Ethernet cable state.</value>
463 /// <privilege>http://tizen.org/privilege/network.get</privilege>
464 public static EthernetCableState EthernetCableState
468 return ConnectionInternalManager.Instance.EthernetCableState;
472 } // class ConnectionManager
475 /// This class contains connection information, such as connection type and state.
477 /// <since_tizen> 3 </since_tizen>
478 public class ConnectionItem
480 internal ConnectionItem()
485 /// The type of the current profile for data connection.
487 /// <since_tizen> 3 </since_tizen>
488 /// <value>Data connection current profile.</value>
489 /// <privilege>http://tizen.org/privilege/network.get</privilege>
490 public ConnectionType Type
494 return ConnectionInternalManager.Instance.ConnectionType;
499 /// The state of the current profile for data connection.
501 /// <since_tizen> 3 </since_tizen>
502 /// <value>Connection state of the current connection type.</value>
503 /// <privilege>http://tizen.org/privilege/network.get</privilege>
504 public ConnectionState State
508 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
510 if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
512 return ConnectionState.Connected;
514 else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
516 return ConnectionState.Disconnected;
519 return ConnectionState.Deactivated;
522 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
524 return ConnectionInternalManager.Instance.BluetoothState;
526 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
528 return ConnectionInternalManager.Instance.WiFiState;
530 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
532 return ConnectionInternalManager.Instance.EthernetState;
534 else { // TO DO : Add Net Proxy
535 return ConnectionState.Disconnected;
539 } // class ConnectionItem
542 /// An extended EventArgs class, which contains changed connection type.
544 /// <since_tizen> 3 </since_tizen>
545 public class ConnectionTypeEventArgs : EventArgs
547 private ConnectionType Type = ConnectionType.Disconnected;
549 internal ConnectionTypeEventArgs(ConnectionType type)
555 /// The connection type.
557 /// <since_tizen> 3 </since_tizen>
558 /// <value>Type of the connection.</value>
559 public ConnectionType ConnectionType
569 /// An extended EventArgs class, which contains changed ethernet cable state.
571 /// <since_tizen> 3 </since_tizen>
572 public class EthernetCableStateEventArgs : EventArgs
574 private EthernetCableState State;
576 internal EthernetCableStateEventArgs(EthernetCableState state)
582 /// The ethernet cable state.
584 /// <since_tizen> 3 </since_tizen>
585 /// <value>Attached or detached state of the ethernet cable.</value>
586 public EthernetCableState EthernetCableState
596 /// An extended EventArgs class, which contains changed address.
598 /// <since_tizen> 3 </since_tizen>
599 public class AddressEventArgs : EventArgs
601 private string IPv4 = "";
602 private string IPv6 = "";
604 internal AddressEventArgs(string ipv4, string ipv6)
611 /// The IPV4 address.
613 /// <since_tizen> 3 </since_tizen>
614 /// <value>IP address in the format of the IPV4 syntax.</value>
615 public string IPv4Address
624 /// The IPV6 address.
626 /// <since_tizen> 3 </since_tizen>
627 /// <value>IP address in the format of the IPV6 syntax.</value>
628 public string IPv6Address