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 /// Type and state of the current profile for data connection
299 /// <since_tizen> 3 </since_tizen>
300 /// <value>Instance of ConnectionItem</value>
301 public static ConnectionItem CurrentConnection
305 if (_currentConnection == null)
307 _currentConnection = new ConnectionItem();
310 return _currentConnection;
315 /// Creates a cellular profile handle.
317 /// <since_tizen> 3 </since_tizen>
318 /// <param name="type">The type of profile. Cellular profile type is supported.</param>
319 /// <param name="keyword">The keyword included in profile name.</param>
320 /// <returns>CellularProfile object</returns>
321 /// <privilege>http://tizen.org/privilege/network.get</privilege>
322 /// <feature>http://tizen.org/feature/network.telephony</feature>
323 /// <feature>http://tizen.org/feature/network.wifi</feature>
324 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
325 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
326 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
327 /// <exception cref="System.ArgumentNullException">Thrown when keyword value is null.</exception>
328 /// <exception cref="System.InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
329 public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
331 IntPtr profileHandle = IntPtr.Zero;
332 if (type == ConnectionProfileType.Cellular)
334 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
339 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
340 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
343 return new CellularProfile(profileHandle);
347 /// The state of cellular connection.
349 /// <since_tizen> 3 </since_tizen>
350 /// <value>Cellular network state.</value>
351 /// <privilege>http://tizen.org/privilege/network.get</privilege>
352 public static CellularState CellularState
356 return ConnectionInternalManager.Instance.CellularState;
361 /// The state of the Wi-Fi.
363 /// <since_tizen> 3 </since_tizen>
364 /// <value>WiFi connection state.</value>
365 /// <privilege>http://tizen.org/privilege/network.get</privilege>
366 public static ConnectionState WiFiState
370 return ConnectionInternalManager.Instance.WiFiState;
375 /// The state of the Bluetooth.
377 /// <since_tizen> 3 </since_tizen>
378 /// <value>Bluetooth connection state.</value>
379 /// <privilege>http://tizen.org/privilege/network.get</privilege>
380 public static ConnectionState BluetoothState
384 return ConnectionInternalManager.Instance.BluetoothState;
389 /// The Ethernet connection state.
391 /// <since_tizen> 3 </since_tizen>
392 /// <value>Ethernet connection state.</value>
393 /// <privilege>http://tizen.org/privilege/network.get</privilege>
394 public static ConnectionState EthernetState
398 return ConnectionInternalManager.Instance.EthernetState;
403 /// Checks for ethernet cable is attached or not.
405 /// <since_tizen> 3 </since_tizen>
406 /// <value>Ethernet cable state.</value>
407 /// <privilege>http://tizen.org/privilege/network.get</privilege>
408 public static EthernetCableState EthernetCableState
412 return ConnectionInternalManager.Instance.EthernetCableState;
416 } // class ConnectionManager
419 /// This class contains connection information such as connection type and state.
421 /// <since_tizen> 3 </since_tizen>
422 public class ConnectionItem
424 internal ConnectionItem()
429 /// The type of the current profile for data connection.
431 /// <since_tizen> 3 </since_tizen>
432 /// <value>Data connection current profile.</value>
433 /// <privilege>http://tizen.org/privilege/network.get</privilege>
434 public ConnectionType Type
438 return ConnectionInternalManager.Instance.ConnectionType;
443 /// The state of the current profile for data connection.
445 /// <since_tizen> 3 </since_tizen>
446 /// <value>Connection state of the current connection type.</value>
447 /// <privilege>http://tizen.org/privilege/network.get</privilege>
448 public ConnectionState State
452 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
454 if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
456 return ConnectionState.Connected;
458 else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
460 return ConnectionState.Disconnected;
463 return ConnectionState.Deactivated;
466 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
468 return ConnectionInternalManager.Instance.BluetoothState;
470 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
472 return ConnectionInternalManager.Instance.WiFiState;
474 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
476 return ConnectionInternalManager.Instance.EthernetState;
478 else { // TO DO : Add Net Proxy
479 return ConnectionState.Disconnected;
483 } // class ConnectionItem
486 /// An extended EventArgs class which contains changed connection type.
488 /// <since_tizen> 3 </since_tizen>
489 public class ConnectionTypeEventArgs : EventArgs
491 private ConnectionType Type = ConnectionType.Disconnected;
493 internal ConnectionTypeEventArgs(ConnectionType type)
499 /// The connection type.
501 /// <since_tizen> 3 </since_tizen>
502 /// <value>Type of the connection.</value>
503 public ConnectionType ConnectionType
513 /// An extended EventArgs class which contains changed ethernet cable state.
515 /// <since_tizen> 3 </since_tizen>
516 public class EthernetCableStateEventArgs : EventArgs
518 private EthernetCableState State;
520 internal EthernetCableStateEventArgs(EthernetCableState state)
526 /// The ethernet cable state.
528 /// <since_tizen> 3 </since_tizen>
529 /// <value>Attached or detached state of the ethernet cable.</value>
530 public EthernetCableState EthernetCableState
540 /// An extended EventArgs class which contains changed address.
542 /// <since_tizen> 3 </since_tizen>
543 public class AddressEventArgs : EventArgs
545 private string IPv4 = "";
546 private string IPv6 = "";
548 internal AddressEventArgs(string ipv4, string ipv6)
555 /// The IPV4 address.
557 /// <since_tizen> 3 </since_tizen>
558 /// <value>IP address in the format of IPV4 syntax.</value>
559 public string IPv4Address
568 /// The IPV6 address.
570 /// <since_tizen> 3 </since_tizen>
571 /// <value>IP address in the format of IPV6 syntax.</value>
572 public string IPv6Address