Modify API reference
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / ConnectionManager.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.Linq;
21 using System.Text;
22 using System.Threading.Tasks;
23 using System.Runtime.InteropServices;
24
25 /// <summary>
26 /// The Connection API provides functions, enumerations to get the status of network and current profile and manage profiles.
27 /// </summary>
28 namespace Tizen.Network.Connection
29 {
30     /// <summary>
31     /// This class manages the connection handle resources.
32     /// </summary>
33     [EditorBrowsable(EditorBrowsableState.Never)]
34     public sealed class SafeConnectionHandle : SafeHandle
35     {
36         internal SafeConnectionHandle(IntPtr handle) : base(handle, true)
37         {
38         }
39
40         /// <summary>
41         /// Checks whether the handle value is valid or not.
42         /// </summary>
43         /// <value>True if the handle is invalid, otherwise false.</value>
44         public override bool IsInvalid
45         {
46             get
47             {
48                 return this.handle == IntPtr.Zero;
49             }
50         }
51
52         /// <summary>
53         /// Frees the handle.
54         /// </summary>
55         /// <returns>True if the handle is released successfully, otherwise false.</returns>
56         protected override bool ReleaseHandle()
57         {
58             this.SetHandle(IntPtr.Zero);
59             return true;
60         }
61     }
62
63     /// <summary>
64     /// This class is ConnectionManager. It provides functions to manage data connections.
65     /// </summary>
66     public static class ConnectionManager
67     {
68         private static ConnectionItem _currentConnection = null;
69
70         /// <summary>
71         /// Event that is called when the type of the current connection is changed.
72         /// </summary>
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
79         {
80             add
81             {
82                 ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
83             }
84
85             remove
86             {
87                 ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
88             }
89         }
90
91         /// <summary>
92         /// Event for ethernet cable is plugged [in/out] event.
93         /// </summary>
94         /// <privilege>http://tizen.org/privilege/network.get</privilege>
95         /// <feature>http://tizen.org/feature/network.ethernet</feature>
96         public static event EventHandler EthernetCableStateChanged
97         {
98             add
99             {
100                 ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
101             }
102
103             remove
104             {
105                 ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
106             }
107         }
108
109         /// <summary>
110         /// Event that is called when the IP address is changed.
111         /// </summary>
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
118         {
119             add
120             {
121                 ConnectionInternalManager.Instance.IPAddressChanged += value;
122             }
123
124             remove
125             {
126                 ConnectionInternalManager.Instance.IPAddressChanged -= value;
127             }
128         }
129
130         /// <summary>
131         /// Event that is called when the proxy address is changed.
132         /// </summary>
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
139         {
140             add
141             {
142                 ConnectionInternalManager.Instance.ProxyAddressChanged += value;
143             }
144
145             remove
146             {
147                 ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
148             }
149         }
150
151         /// <summary>
152         /// Gets the connection handle.
153         /// </summary>
154         /// <returns>Instance of SafeConnectionHandle</returns>
155         [EditorBrowsable(EditorBrowsableState.Never)]
156         public static SafeConnectionHandle GetConnectionHandle()
157         {
158             IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
159             return new SafeConnectionHandle(handle);
160         }
161
162         /// <summary>
163         /// Gets the IP address of the current connection.
164         /// </summary>
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)
178         {
179             return ConnectionInternalManager.Instance.GetIPAddress(family);
180         }
181
182         /// <summary>
183         /// Gets the all IPv6 addresses of the current connection.
184         /// </summary>
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)
198         {
199             return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
200         }
201
202         /// <summary>
203         /// Gets the proxy address of the current connection.
204         /// </summary>
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)
218         {
219             return ConnectionInternalManager.Instance.GetProxy(family);
220         }
221
222         /// <summary>
223         /// Gets the MAC address of the Wi-Fi or ethernet.
224         /// </summary>
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)
238         {
239             return ConnectionInternalManager.Instance.GetMacAddress(type);
240         }
241
242         /// <summary>
243         /// Gets the statistics information.
244         /// </summary>
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)
259         {
260             return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
261         }
262
263         /// <summary>
264         /// Resets the statistics information.
265         /// </summary>
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)
280         {
281             ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
282         }
283
284         /// <summary>
285         /// Type and state of the current profile for data connection
286         /// </summary>
287         /// <value>Instance of ConnectionItem</value>
288         public static ConnectionItem CurrentConnection
289         {
290             get
291             {
292                 if (_currentConnection == null)
293                 {
294                     _currentConnection = new ConnectionItem();
295                 }
296
297                 return _currentConnection;
298             }
299         }
300
301         /// <summary>
302         /// Creates a cellular profile handle.
303         /// </summary>
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)
316         {
317             IntPtr profileHandle = IntPtr.Zero;
318             if (type == ConnectionProfileType.Cellular)
319             {
320                 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
321             }
322
323             else
324             {
325                 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
326                 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
327             }
328
329             return new CellularProfile(profileHandle);
330         }
331
332         /// <summary>
333         /// The state of cellular connection.
334         /// </summary>
335         /// <value>Cellular network state.</value>
336         /// <privilege>http://tizen.org/privilege/network.get</privilege>
337         public static CellularState CellularState
338         {
339             get
340             {
341                 return ConnectionInternalManager.Instance.CellularState;
342             }
343         }
344
345         /// <summary>
346         /// The state of the Wi-Fi.
347         /// </summary>
348         /// <value>WiFi connection state.</value>
349         /// <privilege>http://tizen.org/privilege/network.get</privilege>
350         public static ConnectionState WiFiState
351         {
352             get
353             {
354                 return ConnectionInternalManager.Instance.WiFiState;
355             }
356         }
357
358         /// <summary>
359         /// The state of the Bluetooth.
360         /// </summary>
361         /// <value>Bluetooth connection state.</value>
362         /// <privilege>http://tizen.org/privilege/network.get</privilege>
363         public static ConnectionState BluetoothState
364         {
365             get
366             {
367                 return ConnectionInternalManager.Instance.BluetoothState;
368             }
369         }
370
371         /// <summary>
372         /// The Ethernet connection state.
373         /// </summary>
374         /// <value>Ethernet connection state.</value>
375         /// <privilege>http://tizen.org/privilege/network.get</privilege>
376         public static ConnectionState EthernetState
377         {
378             get
379             {
380                 return ConnectionInternalManager.Instance.EthernetState;
381             }
382         }
383
384         /// <summary>
385         /// Checks for ethernet cable is attached or not.
386         /// </summary>
387         /// <value>Ethernet cable state.</value>
388         /// <privilege>http://tizen.org/privilege/network.get</privilege>
389         public static EthernetCableState EthernetCableState
390         {
391             get
392             {
393                 return ConnectionInternalManager.Instance.EthernetCableState;
394             }
395         }
396
397     } // class ConnectionManager
398
399     /// <summary>
400     /// This class contains connection information such as connection type and state.
401     /// </summary>
402     public class ConnectionItem
403     {
404         internal ConnectionItem()
405         {
406         }
407
408         /// <summary>
409         /// The type of the current profile for data connection.
410         /// </summary>
411         /// <value>Data connection current profile.</value>
412         /// <privilege>http://tizen.org/privilege/network.get</privilege>
413         public ConnectionType Type
414         {
415             get
416             {
417                 return ConnectionInternalManager.Instance.ConnectionType;
418             }
419         }
420
421         /// <summary>
422         /// The state of the current profile for data connection.
423         /// </summary>
424         /// <value>Connection state of the current connection type.</value>
425         /// <privilege>http://tizen.org/privilege/network.get</privilege>
426         public ConnectionState State
427         {
428             get
429             {
430                 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
431                 {
432                     if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
433                     {
434                         return ConnectionState.Connected;
435                     }
436                     else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
437                     {
438                         return ConnectionState.Disconnected;
439                     }
440                     else {
441                         return ConnectionState.Deactivated;
442                     }
443                 }
444                 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
445                 {
446                     return ConnectionInternalManager.Instance.BluetoothState;
447                 }
448                 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
449                 {
450                     return ConnectionInternalManager.Instance.WiFiState;
451                 }
452                 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
453                 {
454                     return ConnectionInternalManager.Instance.EthernetState;
455                 }
456                 else { // TO DO : Add Net Proxy
457                     return ConnectionState.Disconnected;
458                 }
459             }
460         }
461     } // class ConnectionItem
462
463     /// <summary>
464     /// An extended EventArgs class which contains changed connection type.
465     /// </summary>
466     public class ConnectionTypeEventArgs : EventArgs
467     {
468         private ConnectionType Type = ConnectionType.Disconnected;
469
470         internal ConnectionTypeEventArgs(ConnectionType type)
471         {
472             Type = type;
473         }
474
475         /// <summary>
476         /// The connection type.
477         /// </summary>
478         /// <value>Type of the connection.</value>
479         public ConnectionType ConnectionType
480         {
481             get
482             {
483                 return Type;
484             }
485         }
486     }
487
488     /// <summary>
489     /// An extended EventArgs class which contains changed ethernet cable state.
490     /// </summary>
491     public class EthernetCableStateEventArgs : EventArgs
492     {
493         private EthernetCableState State;
494
495         internal EthernetCableStateEventArgs(EthernetCableState state)
496         {
497             State = state;
498         }
499
500         /// <summary>
501         /// The ethernet cable state.
502         /// </summary>
503         /// <value>Attached or detached state of the ethernet cable.</value>
504         public EthernetCableState EthernetCableState
505         {
506             get
507             {
508                 return State;
509             }
510         }
511     }
512
513     /// <summary>
514     /// An extended EventArgs class which contains changed address.
515     /// </summary>
516     public class AddressEventArgs : EventArgs
517     {
518         private string IPv4 = "";
519         private string IPv6 = "";
520
521         internal AddressEventArgs(string ipv4, string ipv6)
522         {
523             IPv4 = ipv4;
524             IPv6 = ipv6;
525         }
526
527         /// <summary>
528         /// The  IPV4 address.
529         /// </summary>
530         /// <value>IP address in the format of IPV4 syntax.</value>
531         public string IPv4Address
532         {
533             get
534             {
535                 return IPv4;
536             }
537         }
538
539         /// <summary>
540         /// The  IPV6 address.
541         /// </summary>
542         /// <value>IP address in the format of IPV6 syntax.</value>
543         public string IPv6Address
544         {
545             get
546             {
547                 return IPv6;
548             }
549         }
550     }
551 }