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, enumerations to get the status of network and current profile and manage 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 class is ConnectionManager. 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 /// Event that 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 public static event EventHandler ConnectionTypeChanged
84 ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
89 ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
94 /// Event for ethernet cable is plugged [in/out] event.
96 /// <since_tizen> 3 </since_tizen>
97 /// <privilege>http://tizen.org/privilege/network.get</privilege>
98 /// <feature>http://tizen.org/feature/network.ethernet</feature>
99 public static event EventHandler EthernetCableStateChanged
103 ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
108 ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
113 /// Event that is called when the IP address is changed.
115 /// <since_tizen> 3 </since_tizen>
116 /// <privilege>http://tizen.org/privilege/network.get</privilege>
117 /// <feature>http://tizen.org/feature/network.ethernet</feature>
118 /// <feature>http://tizen.org/feature/network.telephony</feature>
119 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
120 /// <feature>http://tizen.org/feature/network.wifi</feature>
121 public static event EventHandler IPAddressChanged
125 ConnectionInternalManager.Instance.IPAddressChanged += value;
130 ConnectionInternalManager.Instance.IPAddressChanged -= value;
135 /// Event that is called when the proxy address is changed.
137 /// <since_tizen> 3 </since_tizen>
138 /// <privilege>http://tizen.org/privilege/network.get</privilege>
139 /// <feature>http://tizen.org/feature/network.ethernet</feature>
140 /// <feature>http://tizen.org/feature/network.telephony</feature>
141 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
142 /// <feature>http://tizen.org/feature/network.wifi</feature>
143 public static event EventHandler ProxyAddressChanged
147 ConnectionInternalManager.Instance.ProxyAddressChanged += value;
152 ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
157 /// Gets the connection handle.
159 /// <since_tizen> 3 </since_tizen>
160 /// <returns>Instance of SafeConnectionHandle</returns>
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 public static SafeConnectionHandle GetConnectionHandle()
164 IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
165 return new SafeConnectionHandle(handle);
169 /// Gets the IP address of the current connection.
171 /// <since_tizen> 3 </since_tizen>
172 /// <param name="family">The address family</param>
173 /// <returns>IP address of the connection (global address in case of IPv6).</returns>
174 /// <privilege>http://tizen.org/privilege/network.get</privilege>
175 /// <feature>http://tizen.org/feature/network.ethernet</feature>
176 /// <feature>http://tizen.org/feature/network.telephony</feature>
177 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
178 /// <feature>http://tizen.org/feature/network.wifi</feature>
179 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
180 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
181 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
182 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
183 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
184 public static System.Net.IPAddress GetIPAddress(AddressFamily family)
186 return ConnectionInternalManager.Instance.GetIPAddress(family);
190 /// Gets the all IPv6 addresses of the current connection.
192 /// <since_tizen> 3 </since_tizen>
193 /// <param name="type">The type of current network connection</param>
194 /// <returns>A list of IPv6 addresses of the connection.</returns>
195 /// <privilege>http://tizen.org/privilege/network.get</privilege>
196 /// <feature>http://tizen.org/feature/network.ethernet</feature>
197 /// <feature>http://tizen.org/feature/network.telephony</feature>
198 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
199 /// <feature>http://tizen.org/feature/network.wifi</feature>
200 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
201 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
202 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
203 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
204 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
205 public static IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
207 return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
211 /// Gets the proxy address of the current connection.
213 /// <since_tizen> 3 </since_tizen>
214 /// <param name="family">The address family</param>
215 /// <returns>Proxy address of the connection.</returns>
216 /// <privilege>http://tizen.org/privilege/network.get</privilege>
217 /// <feature>http://tizen.org/feature/network.ethernet</feature>
218 /// <feature>http://tizen.org/feature/network.telephony</feature>
219 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
220 /// <feature>http://tizen.org/feature/network.wifi</feature>
221 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
222 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
223 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
224 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
225 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
226 public static string GetProxy(AddressFamily family)
228 return ConnectionInternalManager.Instance.GetProxy(family);
232 /// Gets the MAC address of the Wi-Fi or ethernet.
234 /// <since_tizen> 3 </since_tizen>
235 /// <param name="type">The type of current network connection</param>
236 /// <returns>MAC address of the Wi-Fi or ethernet.</returns>
237 /// <privilege>http://tizen.org/privilege/network.get</privilege>
238 /// <feature>http://tizen.org/feature/network.ethernet</feature>
239 /// <feature>http://tizen.org/feature/network.telephony</feature>
240 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
241 /// <feature>http://tizen.org/feature/network.wifi</feature>
242 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
243 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
244 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
245 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
246 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
247 public static string GetMacAddress(ConnectionType type)
249 return ConnectionInternalManager.Instance.GetMacAddress(type);
253 /// Gets the statistics information.
255 /// <since_tizen> 3 </since_tizen>
256 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported)</param>
257 /// <param name="statisticsType">The type of statistics</param>
258 /// <returns>The statistics information associated with statisticsType</returns>
259 /// <privilege>http://tizen.org/privilege/network.get</privilege>
260 /// <feature>http://tizen.org/feature/network.ethernet</feature>
261 /// <feature>http://tizen.org/feature/network.telephony</feature>
262 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
263 /// <feature>http://tizen.org/feature/network.wifi</feature>
264 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
265 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
266 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
267 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
268 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
269 public static long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
271 return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
275 /// Resets the statistics information.
277 /// <since_tizen> 3 </since_tizen>
278 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported)</param>
279 /// <param name="statisticsType">The type of statistics</param>
280 /// <privilege>http://tizen.org/privilege/network.get</privilege>
281 /// <privilege>http://tizen.org/privilege/network.set</privilege>
282 /// <feature>http://tizen.org/feature/network.ethernet</feature>
283 /// <feature>http://tizen.org/feature/network.telephony</feature>
284 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
285 /// <feature>http://tizen.org/feature/network.wifi</feature>
286 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
287 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
288 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
289 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
290 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
291 public static void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
293 ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
297 /// Adds a route to the routing table.
299 /// <param name="family">The address family</param>
300 /// <param name="interfaceName">The name of network interface</param>
301 /// <param name="hostAddress">The IP address of the host</param>
302 /// <param name="gateway">The gateway address</param>
303 /// <privilege>http://tizen.org/privilege/network.get</privilege>
304 /// <privilege>http://tizen.org/privilege/network.set</privilege>
305 /// <feature>http://tizen.org/feature/network.telephony</feature>
306 /// <feature>http://tizen.org/feature/network.wifi</feature>
307 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
308 /// <feature>http://tizen.org/feature/network.ethernet</feature>
309 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
310 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
311 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
312 /// <exception cref="System.ArgumentNullException">Thrown when interfaceName or hostAddress or gateway is null.</exception>
313 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
314 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
315 public static void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
317 ConnectionInternalManager.Instance.AddRoute(family, interfaceName, hostAddress, gateway);
321 /// Removes a route from the routing table.
323 /// <param name="family">The address family</param>
324 /// <param name="interfaceName">The name of network interface</param>
325 /// <param name="hostAddress">The IP address of the host</param>
326 /// <param name="gateway">The gateway address</param>
327 /// <privilege>http://tizen.org/privilege/network.get</privilege>
328 /// <privilege>http://tizen.org/privilege/network.set</privilege>
329 /// <feature>http://tizen.org/feature/network.telephony</feature>
330 /// <feature>http://tizen.org/feature/network.wifi</feature>
331 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
332 /// <feature>http://tizen.org/feature/network.ethernet</feature>
333 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
334 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
335 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
336 /// <exception cref="System.ArgumentNullException">Thrown when interfaceName or hostAddress or gateway is null.</exception>
337 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
338 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
339 public static void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress hostAddress, System.Net.IPAddress gateway)
341 ConnectionInternalManager.Instance.RemoveRoute(family, interfaceName, hostAddress, gateway);
345 /// Type and state of the current profile for data connection
347 /// <since_tizen> 3 </since_tizen>
348 /// <value>Instance of ConnectionItem</value>
349 public static ConnectionItem CurrentConnection
353 if (_currentConnection == null)
355 _currentConnection = new ConnectionItem();
358 return _currentConnection;
363 /// Creates a cellular profile handle.
365 /// <since_tizen> 3 </since_tizen>
366 /// <param name="type">The type of profile. Cellular profile type is supported.</param>
367 /// <param name="keyword">The keyword included in profile name.</param>
368 /// <returns>CellularProfile object</returns>
369 /// <privilege>http://tizen.org/privilege/network.get</privilege>
370 /// <feature>http://tizen.org/feature/network.telephony</feature>
371 /// <feature>http://tizen.org/feature/network.wifi</feature>
372 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
373 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
374 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
375 /// <exception cref="System.ArgumentNullException">Thrown when keyword value is null.</exception>
376 /// <exception cref="System.InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
377 public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
379 IntPtr profileHandle = IntPtr.Zero;
380 if (type == ConnectionProfileType.Cellular)
382 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
387 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
388 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
391 return new CellularProfile(profileHandle);
395 /// The state of cellular connection.
397 /// <since_tizen> 3 </since_tizen>
398 /// <value>Cellular network state.</value>
399 /// <privilege>http://tizen.org/privilege/network.get</privilege>
400 public static CellularState CellularState
404 return ConnectionInternalManager.Instance.CellularState;
409 /// The state of the Wi-Fi.
411 /// <since_tizen> 3 </since_tizen>
412 /// <value>WiFi connection state.</value>
413 /// <privilege>http://tizen.org/privilege/network.get</privilege>
414 public static ConnectionState WiFiState
418 return ConnectionInternalManager.Instance.WiFiState;
423 /// The state of the Bluetooth.
425 /// <since_tizen> 3 </since_tizen>
426 /// <value>Bluetooth connection state.</value>
427 /// <privilege>http://tizen.org/privilege/network.get</privilege>
428 public static ConnectionState BluetoothState
432 return ConnectionInternalManager.Instance.BluetoothState;
437 /// The Ethernet connection state.
439 /// <since_tizen> 3 </since_tizen>
440 /// <value>Ethernet connection state.</value>
441 /// <privilege>http://tizen.org/privilege/network.get</privilege>
442 public static ConnectionState EthernetState
446 return ConnectionInternalManager.Instance.EthernetState;
451 /// Checks for ethernet cable is attached or not.
453 /// <since_tizen> 3 </since_tizen>
454 /// <value>Ethernet cable state.</value>
455 /// <privilege>http://tizen.org/privilege/network.get</privilege>
456 public static EthernetCableState EthernetCableState
460 return ConnectionInternalManager.Instance.EthernetCableState;
464 } // class ConnectionManager
467 /// This class contains connection information such as connection type and state.
469 /// <since_tizen> 3 </since_tizen>
470 public class ConnectionItem
472 internal ConnectionItem()
477 /// The type of the current profile for data connection.
479 /// <since_tizen> 3 </since_tizen>
480 /// <value>Data connection current profile.</value>
481 /// <privilege>http://tizen.org/privilege/network.get</privilege>
482 public ConnectionType Type
486 return ConnectionInternalManager.Instance.ConnectionType;
491 /// The state of the current profile for data connection.
493 /// <since_tizen> 3 </since_tizen>
494 /// <value>Connection state of the current connection type.</value>
495 /// <privilege>http://tizen.org/privilege/network.get</privilege>
496 public ConnectionState State
500 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
502 if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
504 return ConnectionState.Connected;
506 else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
508 return ConnectionState.Disconnected;
511 return ConnectionState.Deactivated;
514 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
516 return ConnectionInternalManager.Instance.BluetoothState;
518 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
520 return ConnectionInternalManager.Instance.WiFiState;
522 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
524 return ConnectionInternalManager.Instance.EthernetState;
526 else { // TO DO : Add Net Proxy
527 return ConnectionState.Disconnected;
531 } // class ConnectionItem
534 /// An extended EventArgs class which contains changed connection type.
536 /// <since_tizen> 3 </since_tizen>
537 public class ConnectionTypeEventArgs : EventArgs
539 private ConnectionType Type = ConnectionType.Disconnected;
541 internal ConnectionTypeEventArgs(ConnectionType type)
547 /// The connection type.
549 /// <since_tizen> 3 </since_tizen>
550 /// <value>Type of the connection.</value>
551 public ConnectionType ConnectionType
561 /// An extended EventArgs class which contains changed ethernet cable state.
563 /// <since_tizen> 3 </since_tizen>
564 public class EthernetCableStateEventArgs : EventArgs
566 private EthernetCableState State;
568 internal EthernetCableStateEventArgs(EthernetCableState state)
574 /// The ethernet cable state.
576 /// <since_tizen> 3 </since_tizen>
577 /// <value>Attached or detached state of the ethernet cable.</value>
578 public EthernetCableState EthernetCableState
588 /// An extended EventArgs class which contains changed address.
590 /// <since_tizen> 3 </since_tizen>
591 public class AddressEventArgs : EventArgs
593 private string IPv4 = "";
594 private string IPv6 = "";
596 internal AddressEventArgs(string ipv4, string ipv6)
603 /// The IPV4 address.
605 /// <since_tizen> 3 </since_tizen>
606 /// <value>IP address in the format of IPV4 syntax.</value>
607 public string IPv4Address
616 /// The IPV6 address.
618 /// <since_tizen> 3 </since_tizen>
619 /// <value>IP address in the format of IPV6 syntax.</value>
620 public string IPv6Address