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;
25 namespace Tizen.Network.Connection
28 /// This class manages the connection handle resources.
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 public sealed class SafeConnectionHandle : SafeHandle
33 internal SafeConnectionHandle(IntPtr handle) : base(handle, true)
38 /// Checks whether the handle value is valid or not.
40 /// <value>True if the handle is invalid, otherwise false.</value>
41 public override bool IsInvalid
45 return this.handle == IntPtr.Zero;
52 /// <returns>True if the handle is released successfully, otherwise false.</returns>
53 protected override bool ReleaseHandle()
55 this.SetHandle(IntPtr.Zero);
61 /// This is the ConnectionManager class. It provides functions to manage data connections.
63 /// <since_tizen> 3 </since_tizen>
64 public static class ConnectionManager
66 private static ConnectionItem _currentConnection = null;
69 /// An event is called when the type of the current connection is changed.
71 /// <since_tizen> 3 </since_tizen>
72 /// <privilege>http://tizen.org/privilege/network.get</privilege>
73 /// <feature>http://tizen.org/feature/network.ethernet</feature>
74 /// <feature>http://tizen.org/feature/network.telephony</feature>
75 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
76 /// <feature>http://tizen.org/feature/network.wifi</feature>
77 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
78 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
79 public static event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
83 ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
88 ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
93 /// An event for the ethernet cable is plugged [in/out] event.
95 /// <since_tizen> 3 </since_tizen>
96 /// <privilege>http://tizen.org/privilege/network.get</privilege>
97 /// <feature>http://tizen.org/feature/network.ethernet</feature>
98 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
99 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
100 public static event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
104 ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
109 ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
114 /// An event is called when the IP address is changed.
116 /// <since_tizen> 3 </since_tizen>
117 /// <privilege>http://tizen.org/privilege/network.get</privilege>
118 /// <feature>http://tizen.org/feature/network.ethernet</feature>
119 /// <feature>http://tizen.org/feature/network.telephony</feature>
120 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
121 /// <feature>http://tizen.org/feature/network.wifi</feature>
122 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
123 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
124 public static event EventHandler<AddressEventArgs> IPAddressChanged
128 ConnectionInternalManager.Instance.IPAddressChanged += value;
133 ConnectionInternalManager.Instance.IPAddressChanged -= value;
138 /// An event is called when the proxy address is changed.
140 /// <since_tizen> 3 </since_tizen>
141 /// <privilege>http://tizen.org/privilege/network.get</privilege>
142 /// <feature>http://tizen.org/feature/network.ethernet</feature>
143 /// <feature>http://tizen.org/feature/network.telephony</feature>
144 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
145 /// <feature>http://tizen.org/feature/network.wifi</feature>
146 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
147 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
148 public static event EventHandler<AddressEventArgs> ProxyAddressChanged
152 ConnectionInternalManager.Instance.ProxyAddressChanged += value;
157 ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
162 /// Gets the connection handle.
164 /// <since_tizen> 3 </since_tizen>
165 /// <returns>Instance of SafeConnectionHandle.</returns>
166 [EditorBrowsable(EditorBrowsableState.Never)]
167 public static SafeConnectionHandle GetConnectionHandle()
169 IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
170 return new SafeConnectionHandle(handle);
174 /// Gets the IP address of the current connection.
176 /// <since_tizen> 3 </since_tizen>
177 /// <param name="family">The address family.</param>
178 /// <returns>IP address of the connection (global address in case of IPv6).</returns>
179 /// <privilege>http://tizen.org/privilege/network.get</privilege>
180 /// <feature>http://tizen.org/feature/network.ethernet</feature>
181 /// <feature>http://tizen.org/feature/network.telephony</feature>
182 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
183 /// <feature>http://tizen.org/feature/network.wifi</feature>
184 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
185 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
186 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
187 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
188 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
189 public static System.Net.IPAddress GetIPAddress(AddressFamily family)
191 return ConnectionInternalManager.Instance.GetIPAddress(family);
195 /// Gets all the IPv6 addresses of the current connection.
197 /// <since_tizen> 3 </since_tizen>
198 /// <param name="type">The type of current network connection.</param>
199 /// <returns>A list of IPv6 addresses of the connection.</returns>
200 /// <privilege>http://tizen.org/privilege/network.get</privilege>
201 /// <feature>http://tizen.org/feature/network.ethernet</feature>
202 /// <feature>http://tizen.org/feature/network.telephony</feature>
203 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
204 /// <feature>http://tizen.org/feature/network.wifi</feature>
205 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
206 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
207 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
208 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
209 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
210 public static IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
212 return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
216 /// Gets the proxy address of the current connection.
218 /// <since_tizen> 3 </since_tizen>
219 /// <param name="family">The address family.</param>
220 /// <returns>Proxy address of the connection.</returns>
221 /// <privilege>http://tizen.org/privilege/network.get</privilege>
222 /// <feature>http://tizen.org/feature/network.ethernet</feature>
223 /// <feature>http://tizen.org/feature/network.telephony</feature>
224 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
225 /// <feature>http://tizen.org/feature/network.wifi</feature>
226 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
227 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
228 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
229 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
230 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
231 public static string GetProxy(AddressFamily family)
233 return ConnectionInternalManager.Instance.GetProxy(family);
237 /// Gets the MAC address of the Wi-Fi or ethernet.
239 /// <since_tizen> 3 </since_tizen>
240 /// <param name="type">The type of current network connection.</param>
241 /// <returns>MAC address of the Wi-Fi or ethernet.</returns>
242 /// <privilege>http://tizen.org/privilege/network.get</privilege>
243 /// <feature>http://tizen.org/feature/network.ethernet</feature>
244 /// <feature>http://tizen.org/feature/network.telephony</feature>
245 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
246 /// <feature>http://tizen.org/feature/network.wifi</feature>
247 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
248 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
249 /// <exception cref="System.ArgumentException">Thrown when value is an invalid parameter.</exception>
250 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
251 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
252 public static string GetMacAddress(ConnectionType type)
254 return ConnectionInternalManager.Instance.GetMacAddress(type);
258 /// Gets the statistics information.
260 /// <since_tizen> 3 </since_tizen>
261 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported).</param>
262 /// <param name="statisticsType">The type of statistics.</param>
263 /// <returns>The statistics information associated with statisticsType.</returns>
264 /// <privilege>http://tizen.org/privilege/network.get</privilege>
265 /// <feature>http://tizen.org/feature/network.ethernet</feature>
266 /// <feature>http://tizen.org/feature/network.telephony</feature>
267 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
268 /// <feature>http://tizen.org/feature/network.wifi</feature>
269 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
270 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
271 /// <exception cref="System.ArgumentException">Thrown when value is an invalid parameter.</exception>
272 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
273 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to invalid operation.</exception>
274 public static long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
276 return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
280 /// Resets the statistics information.
282 /// <since_tizen> 3 </since_tizen>
283 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported).</param>
284 /// <param name="statisticsType">The type of statistics.</param>
285 /// <privilege>http://tizen.org/privilege/network.get</privilege>
286 /// <privilege>http://tizen.org/privilege/network.set</privilege>
287 /// <feature>http://tizen.org/feature/network.ethernet</feature>
288 /// <feature>http://tizen.org/feature/network.telephony</feature>
289 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
290 /// <feature>http://tizen.org/feature/network.wifi</feature>
291 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
292 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
293 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
294 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
295 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
296 public static void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
298 ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
302 /// Adds a route to the routing table.
304 /// <since_tizen> 4 </since_tizen>
305 /// <param name="family">The address family.</param>
306 /// <param name="interfaceName">The name of the network interface.</param>
307 /// <param name="hostAddress">The IP address of the host.</param>
308 /// <param name="gateway">The gateway address.</param>
309 /// <privilege>http://tizen.org/privilege/network.get</privilege>
310 /// <privilege>http://tizen.org/privilege/network.set</privilege>
311 /// <feature>http://tizen.org/feature/network.telephony</feature>
312 /// <feature>http://tizen.org/feature/network.wifi</feature>
313 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
314 /// <feature>http://tizen.org/feature/network.ethernet</feature>
315 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
316 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
317 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
318 /// <exception cref="System.ArgumentNullException">Thrown when an interfaceName or a hostAddress or a gateway is null.</exception>
319 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
320 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
321 public static void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
323 ConnectionInternalManager.Instance.AddRoute(family, interfaceName, hostAddress, gateway);
327 /// Removes a route from the routing table.
329 /// <since_tizen> 4 </since_tizen>
330 /// <param name="family">The address family.</param>
331 /// <param name="interfaceName">The name of network interface.</param>
332 /// <param name="hostAddress">The IP address of the host.</param>
333 /// <param name="gateway">The gateway address.</param>
334 /// <privilege>http://tizen.org/privilege/network.get</privilege>
335 /// <privilege>http://tizen.org/privilege/network.set</privilege>
336 /// <feature>http://tizen.org/feature/network.telephony</feature>
337 /// <feature>http://tizen.org/feature/network.wifi</feature>
338 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
339 /// <feature>http://tizen.org/feature/network.ethernet</feature>
340 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
341 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
342 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
343 /// <exception cref="System.ArgumentNullException">Thrown when an interfaceName or a hostAddress or a gateway is null.</exception>
344 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
345 /// <exception cref="System.InvalidOperationException">Thrown when a connection instance is invalid or when a method fails due to an invalid operation.</exception>
346 public static void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
348 ConnectionInternalManager.Instance.RemoveRoute(family, interfaceName, hostAddress, gateway);
352 /// The type and state of the current profile for data connection.
354 /// <since_tizen> 3 </since_tizen>
355 /// <value>Instance of ConnectionItem.</value>
356 public static ConnectionItem CurrentConnection
360 if (_currentConnection == null)
362 _currentConnection = new ConnectionItem();
365 return _currentConnection;
370 /// Creates a cellular profile handle.
372 /// <since_tizen> 3 </since_tizen>
373 /// <param name="type">The type of profile. Cellular profile type is supported.</param>
374 /// <param name="keyword">The keyword included in profile name.</param>
375 /// <returns>CellularProfile object.</returns>
376 /// <privilege>http://tizen.org/privilege/network.get</privilege>
377 /// <feature>http://tizen.org/feature/network.telephony</feature>
378 /// <feature>http://tizen.org/feature/network.wifi</feature>
379 /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
380 /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
381 /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
382 /// <exception cref="System.ArgumentNullException">Thrown when a keyword value is null.</exception>
383 /// <exception cref="System.InvalidOperationException">Thrown when a method fails due to invalid operation.</exception>
384 public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
386 IntPtr profileHandle = IntPtr.Zero;
387 if (type == ConnectionProfileType.Cellular)
389 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
394 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
395 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
398 return new CellularProfile(profileHandle);
402 /// The state of the cellular connection.
404 /// <since_tizen> 3 </since_tizen>
405 /// <value>Cellular network state.</value>
406 /// <privilege>http://tizen.org/privilege/network.get</privilege>
407 public static CellularState CellularState
411 return ConnectionInternalManager.Instance.CellularState;
416 /// The state of the Wi-Fi connection.
418 /// <since_tizen> 3 </since_tizen>
419 /// <value>WiFi connection state.</value>
420 /// <privilege>http://tizen.org/privilege/network.get</privilege>
421 public static ConnectionState WiFiState
425 return ConnectionInternalManager.Instance.WiFiState;
430 /// The state of the Bluetooth connection.
432 /// <since_tizen> 3 </since_tizen>
433 /// <value>Bluetooth connection state.</value>
434 /// <privilege>http://tizen.org/privilege/network.get</privilege>
435 public static ConnectionState BluetoothState
439 return ConnectionInternalManager.Instance.BluetoothState;
444 /// The Ethernet connection state.
446 /// <since_tizen> 3 </since_tizen>
447 /// <value>Ethernet connection state.</value>
448 /// <privilege>http://tizen.org/privilege/network.get</privilege>
449 public static ConnectionState EthernetState
453 return ConnectionInternalManager.Instance.EthernetState;
458 /// Checks if the ethernet cable is attached or not.
460 /// <since_tizen> 3 </since_tizen>
461 /// <value>Ethernet cable state.</value>
462 /// <privilege>http://tizen.org/privilege/network.get</privilege>
463 public static EthernetCableState EthernetCableState
467 return ConnectionInternalManager.Instance.EthernetCableState;
471 } // class ConnectionManager
474 /// This class contains connection information, such as connection type and state.
476 /// <since_tizen> 3 </since_tizen>
477 public class ConnectionItem
479 internal ConnectionItem()
484 /// The type of the current profile for data connection.
486 /// <since_tizen> 3 </since_tizen>
487 /// <value>Data connection current profile.</value>
488 /// <privilege>http://tizen.org/privilege/network.get</privilege>
489 public ConnectionType Type
493 return ConnectionInternalManager.Instance.ConnectionType;
498 /// The state of the current profile for data connection.
500 /// <since_tizen> 3 </since_tizen>
501 /// <value>Connection state of the current connection type.</value>
502 /// <privilege>http://tizen.org/privilege/network.get</privilege>
503 public ConnectionState State
507 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
509 if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
511 return ConnectionState.Connected;
513 else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
515 return ConnectionState.Disconnected;
518 return ConnectionState.Deactivated;
521 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
523 return ConnectionInternalManager.Instance.BluetoothState;
525 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
527 return ConnectionInternalManager.Instance.WiFiState;
529 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
531 return ConnectionInternalManager.Instance.EthernetState;
533 else { // TO DO : Add Net Proxy
534 return ConnectionState.Disconnected;
538 } // class ConnectionItem
541 /// An extended EventArgs class, which contains changed connection type.
543 /// <since_tizen> 3 </since_tizen>
544 public class ConnectionTypeEventArgs : EventArgs
546 private ConnectionType Type = ConnectionType.Disconnected;
548 internal ConnectionTypeEventArgs(ConnectionType type)
554 /// The connection type.
556 /// <since_tizen> 3 </since_tizen>
557 /// <value>Type of the connection.</value>
558 public ConnectionType ConnectionType
568 /// An extended EventArgs class, which contains changed ethernet cable state.
570 /// <since_tizen> 3 </since_tizen>
571 public class EthernetCableStateEventArgs : EventArgs
573 private EthernetCableState State;
575 internal EthernetCableStateEventArgs(EthernetCableState state)
581 /// The ethernet cable state.
583 /// <since_tizen> 3 </since_tizen>
584 /// <value>Attached or detached state of the ethernet cable.</value>
585 public EthernetCableState EthernetCableState
595 /// An extended EventArgs class, which contains changed address.
597 /// <since_tizen> 3 </since_tizen>
598 public class AddressEventArgs : EventArgs
600 private string IPv4 = "";
601 private string IPv6 = "";
603 internal AddressEventArgs(string ipv4, string ipv6)
610 /// The IPV4 address.
612 /// <since_tizen> 3 </since_tizen>
613 /// <value>IP address in the format of the IPV4 syntax.</value>
614 public string IPv4Address
623 /// The IPV6 address.
625 /// <since_tizen> 3 </since_tizen>
626 /// <value>IP address in the format of the IPV6 syntax.</value>
627 public string IPv6Address