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 public static class ConnectionManager
68 private static ConnectionItem _currentConnection = null;
71 /// Event that is called when the type of the current connection is changed.
73 /// <privilege>http://tizen.org/privilege/network.get</privilege>
74 /// <feature>http://tizen.org/feature/network.ethernet</feature>
75 /// <feature>http://tizen.org/feature/network.telephony</feature>
76 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
77 /// <feature>http://tizen.org/feature/network.wifi</feature>
78 public static event EventHandler ConnectionTypeChanged
82 ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
87 ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
92 /// Event for ethernet cable is plugged [in/out] event.
94 /// <privilege>http://tizen.org/privilege/network.get</privilege>
95 /// <feature>http://tizen.org/feature/network.ethernet</feature>
96 public static event EventHandler EthernetCableStateChanged
100 ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
105 ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
110 /// Event that is called when the IP address is changed.
112 /// <privilege>http://tizen.org/privilege/network.get</privilege>
113 /// <feature>http://tizen.org/feature/network.ethernet</feature>
114 /// <feature>http://tizen.org/feature/network.telephony</feature>
115 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
116 /// <feature>http://tizen.org/feature/network.wifi</feature>
117 public static event EventHandler IPAddressChanged
121 ConnectionInternalManager.Instance.IPAddressChanged += value;
126 ConnectionInternalManager.Instance.IPAddressChanged -= value;
131 /// Event that is called when the proxy address is changed.
133 /// <privilege>http://tizen.org/privilege/network.get</privilege>
134 /// <feature>http://tizen.org/feature/network.ethernet</feature>
135 /// <feature>http://tizen.org/feature/network.telephony</feature>
136 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
137 /// <feature>http://tizen.org/feature/network.wifi</feature>
138 public static event EventHandler ProxyAddressChanged
142 ConnectionInternalManager.Instance.ProxyAddressChanged += value;
147 ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
152 /// Gets the connection handle.
154 /// <returns>Instance of SafeConnectionHandle</returns>
155 [EditorBrowsable(EditorBrowsableState.Never)]
156 public static SafeConnectionHandle GetConnectionHandle()
158 IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
159 return new SafeConnectionHandle(handle);
163 /// Gets the IP address of the current connection.
165 /// <param name="family">The address family</param>
166 /// <returns>IP address of the connection (global address in case of IPv6).</returns>
167 /// <privilege>http://tizen.org/privilege/network.get</privilege>
168 /// <feature>http://tizen.org/feature/network.ethernet</feature>
169 /// <feature>http://tizen.org/feature/network.telephony</feature>
170 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
171 /// <feature>http://tizen.org/feature/network.wifi</feature>
172 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
173 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
174 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
175 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
176 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
177 public static System.Net.IPAddress GetIPAddress(AddressFamily family)
179 return ConnectionInternalManager.Instance.GetIPAddress(family);
183 /// Gets the all IPv6 addresses of the current connection.
185 /// <param name="type">The type of current network connection</param>
186 /// <returns>A list of IPv6 addresses of the connection.</returns>
187 /// <privilege>http://tizen.org/privilege/network.get</privilege>
188 /// <feature>http://tizen.org/feature/network.ethernet</feature>
189 /// <feature>http://tizen.org/feature/network.telephony</feature>
190 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
191 /// <feature>http://tizen.org/feature/network.wifi</feature>
192 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
193 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
194 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
195 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
196 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
197 public static IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
199 return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
203 /// Gets the proxy address of the current connection.
205 /// <param name="family">The address family</param>
206 /// <returns>Proxy address of the connection.</returns>
207 /// <privilege>http://tizen.org/privilege/network.get</privilege>
208 /// <feature>http://tizen.org/feature/network.ethernet</feature>
209 /// <feature>http://tizen.org/feature/network.telephony</feature>
210 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
211 /// <feature>http://tizen.org/feature/network.wifi</feature>
212 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
213 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
214 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
215 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
216 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
217 public static string GetProxy(AddressFamily family)
219 return ConnectionInternalManager.Instance.GetProxy(family);
223 /// Gets the MAC address of the Wi-Fi or ethernet.
225 /// <param name="type">The type of current network connection</param>
226 /// <returns>MAC address of the Wi-Fi or ethernet.</returns>
227 /// <privilege>http://tizen.org/privilege/network.get</privilege>
228 /// <feature>http://tizen.org/feature/network.ethernet</feature>
229 /// <feature>http://tizen.org/feature/network.telephony</feature>
230 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
231 /// <feature>http://tizen.org/feature/network.wifi</feature>
232 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
233 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
234 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
235 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
236 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
237 public static string GetMacAddress(ConnectionType type)
239 return ConnectionInternalManager.Instance.GetMacAddress(type);
243 /// Gets the statistics information.
245 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported)</param>
246 /// <param name="statisticsType">The type of statistics</param>
247 /// <returns>The statistics information associated with statisticsType</returns>
248 /// <privilege>http://tizen.org/privilege/network.get</privilege>
249 /// <feature>http://tizen.org/feature/network.ethernet</feature>
250 /// <feature>http://tizen.org/feature/network.telephony</feature>
251 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
252 /// <feature>http://tizen.org/feature/network.wifi</feature>
253 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
254 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
255 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
256 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
257 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
258 public static long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
260 return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
264 /// Resets the statistics information.
266 /// <param name="connectionType">The type of connection (only WiFi and Cellular are supported)</param>
267 /// <param name="statisticsType">The type of statistics</param>
268 /// <privilege>http://tizen.org/privilege/network.get</privilege>
269 /// <privilege>http://tizen.org/privilege/network.set</privilege>
270 /// <feature>http://tizen.org/feature/network.ethernet</feature>
271 /// <feature>http://tizen.org/feature/network.telephony</feature>
272 /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
273 /// <feature>http://tizen.org/feature/network.wifi</feature>
274 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
275 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
276 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
277 /// <exception cref="System.OutOfMemoryException">Thrown when memory is not enough to continue execution.</exception>
278 /// <exception cref="System.InvalidOperationException">Thrown when connection instance is invalid or when method failed due to invalid operation.</exception>
279 public static void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
281 ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
285 /// Type and state of the current profile for data connection
287 /// <value>Instance of ConnectionItem</value>
288 public static ConnectionItem CurrentConnection
292 if (_currentConnection == null)
294 _currentConnection = new ConnectionItem();
297 return _currentConnection;
302 /// Creates a cellular profile handle.
304 /// <param name="type">The type of profile. Cellular profile type is supported.</param>
305 /// <param name="keyword">The keyword included in profile name.</param>
306 /// <returns>CellularProfile object</returns>
307 /// <privilege>http://tizen.org/privilege/network.get</privilege>
308 /// <feature>http://tizen.org/feature/network.telephony</feature>
309 /// <feature>http://tizen.org/feature/network.wifi</feature>
310 /// <exception cref="System.NotSupportedException">Thrown when feature is not supported.</exception>
311 /// <exception cref="System.UnauthorizedAccessException">Thrown when permission is denied.</exception>
312 /// <exception cref="System.ArgumentException">Thrown when value is invalid parameter.</exception>
313 /// <exception cref="System.ArgumentNullException">Thrown when keyword value is null.</exception>
314 /// <exception cref="System.InvalidOperationException">Thrown when method failed due to invalid operation.</exception>
315 public static CellularProfile CreateCellularProfile(ConnectionProfileType type, string keyword)
317 IntPtr profileHandle = IntPtr.Zero;
318 if (type == ConnectionProfileType.Cellular)
320 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
325 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
326 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
329 return new CellularProfile(profileHandle);
333 /// The state of cellular connection.
335 /// <value>Cellular network state.</value>
336 /// <privilege>http://tizen.org/privilege/network.get</privilege>
337 public static CellularState CellularState
341 return ConnectionInternalManager.Instance.CellularState;
346 /// The state of the Wi-Fi.
348 /// <value>WiFi connection state.</value>
349 /// <privilege>http://tizen.org/privilege/network.get</privilege>
350 public static ConnectionState WiFiState
354 return ConnectionInternalManager.Instance.WiFiState;
359 /// The state of the Bluetooth.
361 /// <value>Bluetooth connection state.</value>
362 /// <privilege>http://tizen.org/privilege/network.get</privilege>
363 public static ConnectionState BluetoothState
367 return ConnectionInternalManager.Instance.BluetoothState;
372 /// The Ethernet connection state.
374 /// <value>Ethernet connection state.</value>
375 /// <privilege>http://tizen.org/privilege/network.get</privilege>
376 public static ConnectionState EthernetState
380 return ConnectionInternalManager.Instance.EthernetState;
385 /// Checks for ethernet cable is attached or not.
387 /// <value>Ethernet cable state.</value>
388 /// <privilege>http://tizen.org/privilege/network.get</privilege>
389 public static EthernetCableState EthernetCableState
393 return ConnectionInternalManager.Instance.EthernetCableState;
397 } // class ConnectionManager
400 /// This class contains connection information such as connection type and state.
402 public class ConnectionItem
404 internal ConnectionItem()
409 /// The type of the current profile for data connection.
411 /// <value>Data connection current profile.</value>
412 /// <privilege>http://tizen.org/privilege/network.get</privilege>
413 public ConnectionType Type
417 return ConnectionInternalManager.Instance.ConnectionType;
422 /// The state of the current profile for data connection.
424 /// <value>Connection state of the current connection type.</value>
425 /// <privilege>http://tizen.org/privilege/network.get</privilege>
426 public ConnectionState State
430 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
432 if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
434 return ConnectionState.Connected;
436 else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
438 return ConnectionState.Disconnected;
441 return ConnectionState.Deactivated;
444 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
446 return ConnectionInternalManager.Instance.BluetoothState;
448 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
450 return ConnectionInternalManager.Instance.WiFiState;
452 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
454 return ConnectionInternalManager.Instance.EthernetState;
456 else { // TO DO : Add Net Proxy
457 return ConnectionState.Disconnected;
461 } // class ConnectionItem
464 /// An extended EventArgs class which contains changed connection type.
466 public class ConnectionTypeEventArgs : EventArgs
468 private ConnectionType Type = ConnectionType.Disconnected;
470 internal ConnectionTypeEventArgs(ConnectionType type)
476 /// The connection type.
478 /// <value>Type of the connection.</value>
479 public ConnectionType ConnectionType
489 /// An extended EventArgs class which contains changed ethernet cable state.
491 public class EthernetCableStateEventArgs : EventArgs
493 private EthernetCableState State;
495 internal EthernetCableStateEventArgs(EthernetCableState state)
501 /// The ethernet cable state.
503 /// <value>Attached or detached state of the ethernet cable.</value>
504 public EthernetCableState EthernetCableState
514 /// An extended EventArgs class which contains changed address.
516 public class AddressEventArgs : EventArgs
518 private string IPv4 = "";
519 private string IPv6 = "";
521 internal AddressEventArgs(string ipv4, string ipv6)
528 /// The IPV4 address.
530 /// <value>IP address in the format of IPV4 syntax.</value>
531 public string IPv4Address
540 /// The IPV6 address.
542 /// <value>IP address in the format of IPV6 syntax.</value>
543 public string IPv6Address