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 /// <since_tizen> 4 </since_tizen>
308 /// <param name="family">The address family.</param>
309 /// <param name="interfaceName">The name of the network interface.</param>
310 /// <param name="hostAddress">The IP address of the host.</param>
311 /// <param name="gateway">The gateway address.</param>
312 /// <privilege>http://tizen.org/privilege/network.get</privilege>
313 /// <privilege>http://tizen.org/privilege/network.set</privilege>
314 /// <feature>http://tizen.org/feature/network.telephony</feature>
315 /// <feature>http://tizen.org/feature/network.wifi</feature>
316 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
317 /// <feature>http://tizen.org/feature/network.ethernet</feature>
318 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
319 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
320 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
321 /// <exception cref="System.ArgumentNullException">Thrown when an interfaceName or a hostAddress or a gateway is null.</exception>
322 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
323 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
324 public static void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
326 ConnectionInternalManager.Instance.AddRoute(family, interfaceName, hostAddress, gateway);
330 /// Removes a route from the routing table.
332 /// <since_tizen> 4 </since_tizen>
333 /// <param name="family">The address family.</param>
334 /// <param name="interfaceName">The name of network interface.</param>
335 /// <param name="hostAddress">The IP address of the host.</param>
336 /// <param name="gateway">The gateway address.</param>
337 /// <privilege>http://tizen.org/privilege/network.get</privilege>
338 /// <privilege>http://tizen.org/privilege/network.set</privilege>
339 /// <feature>http://tizen.org/feature/network.telephony</feature>
340 /// <feature>http://tizen.org/feature/network.wifi</feature>
341 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
342 /// <feature>http://tizen.org/feature/network.ethernet</feature>
343 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
344 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
345 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
346 /// <exception cref="System.ArgumentNullException">Thrown when an interfaceName or a hostAddress or a gateway is null.</exception>
347 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
348 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
349 public static void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
351 ConnectionInternalManager.Instance.RemoveRoute(family, interfaceName, hostAddress, gateway);
355 /// The type and state of the current profile for data connection.
357 /// <since_tizen> 3 </since_tizen>
358 /// <value>Instance of ConnectionItem.</value>
359 public static ConnectionItem CurrentConnection
363 if (_currentConnection == null)
365 _currentConnection = new ConnectionItem();
368 return _currentConnection;
373 /// Creates a cellular profile handle.
375 /// <since_tizen> 3 </since_tizen>
376 /// <param name="type">The type of profile. Cellular profile type is supported.</param>
377 /// <param name="keyword">The keyword included in profile name.</param>
378 /// <returns>CellularProfile object.</returns>
379 /// <privilege>http://tizen.org/privilege/network.get</privilege>
380 /// <feature>http://tizen.org/feature/network.telephony</feature>
381 /// <feature>http://tizen.org/feature/network.wifi</feature>
382 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
383 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
384 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
385 /// <exception cref="System.ArgumentNullException">Thrown when a keyword value is null.</exception>
386 /// <exception cref="System.InvalidOperationException">Thrown when a method fails due to invalid operation.</exception>
387 public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
389 IntPtr profileHandle = IntPtr.Zero;
390 if (type == ConnectionProfileType.Cellular)
392 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
397 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
398 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
401 return new CellularProfile(profileHandle);
405 /// The state of the cellular connection.
407 /// <since_tizen> 3 </since_tizen>
408 /// <value>Cellular network state.</value>
409 /// <privilege>http://tizen.org/privilege/network.get</privilege>
410 public static CellularState CellularState
414 return ConnectionInternalManager.Instance.CellularState;
419 /// The state of the Wi-Fi connection.
421 /// <since_tizen> 3 </since_tizen>
422 /// <value>WiFi connection state.</value>
423 /// <privilege>http://tizen.org/privilege/network.get</privilege>
424 public static ConnectionState WiFiState
428 return ConnectionInternalManager.Instance.WiFiState;
433 /// The state of the Bluetooth connection.
435 /// <since_tizen> 3 </since_tizen>
436 /// <value>Bluetooth connection state.</value>
437 /// <privilege>http://tizen.org/privilege/network.get</privilege>
438 public static ConnectionState BluetoothState
442 return ConnectionInternalManager.Instance.BluetoothState;
447 /// The Ethernet connection state.
449 /// <since_tizen> 3 </since_tizen>
450 /// <value>Ethernet connection state.</value>
451 /// <privilege>http://tizen.org/privilege/network.get</privilege>
452 public static ConnectionState EthernetState
456 return ConnectionInternalManager.Instance.EthernetState;
461 /// Checks if the ethernet cable is attached or not.
463 /// <since_tizen> 3 </since_tizen>
464 /// <value>Ethernet cable state.</value>
465 /// <privilege>http://tizen.org/privilege/network.get</privilege>
466 public static EthernetCableState EthernetCableState
470 return ConnectionInternalManager.Instance.EthernetCableState;
474 } // class ConnectionManager
477 /// This class contains connection information, such as connection type and state.
479 /// <since_tizen> 3 </since_tizen>
480 public class ConnectionItem
482 internal ConnectionItem()
487 /// The type of the current profile for data connection.
489 /// <since_tizen> 3 </since_tizen>
490 /// <value>Data connection current profile.</value>
491 /// <privilege>http://tizen.org/privilege/network.get</privilege>
492 public ConnectionType Type
496 return ConnectionInternalManager.Instance.ConnectionType;
501 /// The state of the current profile for data connection.
503 /// <since_tizen> 3 </since_tizen>
504 /// <value>Connection state of the current connection type.</value>
505 /// <privilege>http://tizen.org/privilege/network.get</privilege>
506 public ConnectionState State
510 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
512 if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
514 return ConnectionState.Connected;
516 else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
518 return ConnectionState.Disconnected;
521 return ConnectionState.Deactivated;
524 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
526 return ConnectionInternalManager.Instance.BluetoothState;
528 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
530 return ConnectionInternalManager.Instance.WiFiState;
532 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
534 return ConnectionInternalManager.Instance.EthernetState;
536 else { // TO DO : Add Net Proxy
537 return ConnectionState.Disconnected;
541 } // class ConnectionItem
544 /// An extended EventArgs class, which contains changed connection type.
546 /// <since_tizen> 3 </since_tizen>
547 public class ConnectionTypeEventArgs : EventArgs
549 private ConnectionType Type = ConnectionType.Disconnected;
551 internal ConnectionTypeEventArgs(ConnectionType type)
557 /// The connection type.
559 /// <since_tizen> 3 </since_tizen>
560 /// <value>Type of the connection.</value>
561 public ConnectionType ConnectionType
571 /// An extended EventArgs class, which contains changed ethernet cable state.
573 /// <since_tizen> 3 </since_tizen>
574 public class EthernetCableStateEventArgs : EventArgs
576 private EthernetCableState State;
578 internal EthernetCableStateEventArgs(EthernetCableState state)
584 /// The ethernet cable state.
586 /// <since_tizen> 3 </since_tizen>
587 /// <value>Attached or detached state of the ethernet cable.</value>
588 public EthernetCableState EthernetCableState
598 /// An extended EventArgs class, which contains changed address.
600 /// <since_tizen> 3 </since_tizen>
601 public class AddressEventArgs : EventArgs
603 private string IPv4 = "";
604 private string IPv6 = "";
606 internal AddressEventArgs(string ipv4, string ipv6)
613 /// The IPV4 address.
615 /// <since_tizen> 3 </since_tizen>
616 /// <value>IP address in the format of the IPV4 syntax.</value>
617 public string IPv4Address
626 /// The IPV6 address.
628 /// <since_tizen> 3 </since_tizen>
629 /// <value>IP address in the format of the IPV6 syntax.</value>
630 public string IPv6Address