Add API level
[platform/core/csapi/connection.git] / 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     /// <since_tizen> 3 </since_tizen>
67     public static class ConnectionManager
68     {
69         private static ConnectionItem _currentConnection = null;
70
71         /// <summary>
72         /// Event that is called when the type of the current connection is changed.
73         /// </summary>
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
81         {
82             add
83             {
84                 ConnectionInternalManager.Instance.ConnectionTypeChanged += value;
85             }
86
87             remove
88             {
89                 ConnectionInternalManager.Instance.ConnectionTypeChanged -= value;
90             }
91         }
92
93         /// <summary>
94         /// Event for ethernet cable is plugged [in/out] event.
95         /// </summary>
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
100         {
101             add
102             {
103                 ConnectionInternalManager.Instance.EthernetCableStateChanged += value;
104             }
105
106             remove
107             {
108                 ConnectionInternalManager.Instance.EthernetCableStateChanged -= value;
109             }
110         }
111
112         /// <summary>
113         /// Event that is called when the IP address is changed.
114         /// </summary>
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
122         {
123             add
124             {
125                 ConnectionInternalManager.Instance.IPAddressChanged += value;
126             }
127
128             remove
129             {
130                 ConnectionInternalManager.Instance.IPAddressChanged -= value;
131             }
132         }
133
134         /// <summary>
135         /// Event that is called when the proxy address is changed.
136         /// </summary>
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
144         {
145             add
146             {
147                 ConnectionInternalManager.Instance.ProxyAddressChanged += value;
148             }
149
150             remove
151             {
152                 ConnectionInternalManager.Instance.ProxyAddressChanged -= value;
153             }
154         }
155
156         /// <summary>
157         /// Gets the connection handle.
158         /// </summary>
159         /// <since_tizen> 3 </since_tizen>
160         /// <returns>Instance of SafeConnectionHandle</returns>
161         [EditorBrowsable(EditorBrowsableState.Never)]
162         public static SafeConnectionHandle GetConnectionHandle()
163         {
164             IntPtr handle = ConnectionInternalManager.Instance.GetHandle();
165             return new SafeConnectionHandle(handle);
166         }
167
168         /// <summary>
169         /// Gets the IP address of the current connection.
170         /// </summary>
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)
185         {
186             return ConnectionInternalManager.Instance.GetIPAddress(family);
187         }
188
189         /// <summary>
190         /// Gets the all IPv6 addresses of the current connection.
191         /// </summary>
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)
206         {
207             return ConnectionInternalManager.Instance.GetAllIPv6Addresses(type);
208         }
209
210         /// <summary>
211         /// Gets the proxy address of the current connection.
212         /// </summary>
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)
227         {
228             return ConnectionInternalManager.Instance.GetProxy(family);
229         }
230
231         /// <summary>
232         /// Gets the MAC address of the Wi-Fi or ethernet.
233         /// </summary>
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)
248         {
249             return ConnectionInternalManager.Instance.GetMacAddress(type);
250         }
251
252         /// <summary>
253         /// Gets the statistics information.
254         /// </summary>
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)
270         {
271             return ConnectionInternalManager.Instance.GetStatistics(connectionType, statisticsType);
272         }
273
274         /// <summary>
275         /// Resets the statistics information.
276         /// </summary>
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)
292         {
293             ConnectionInternalManager.Instance.ResetStatistics(connectionType, statisticsType);
294         }
295
296         /// <summary>
297         /// Type and state of the current profile for data connection
298         /// </summary>
299         /// <since_tizen> 3 </since_tizen>
300         /// <value>Instance of ConnectionItem</value>
301         public static ConnectionItem CurrentConnection
302         {
303             get
304             {
305                 if (_currentConnection == null)
306                 {
307                     _currentConnection = new ConnectionItem();
308                 }
309
310                 return _currentConnection;
311             }
312         }
313
314         /// <summary>
315         /// Creates a cellular profile handle.
316         /// </summary>
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)
330         {
331             IntPtr profileHandle = IntPtr.Zero;
332             if (type == ConnectionProfileType.Cellular)
333             {
334                 profileHandle = ConnectionInternalManager.Instance.CreateCellularProfile(type, keyword);
335             }
336
337             else
338             {
339                 Log.Error(Globals.LogTag, "ConnectionProfile Type is not supported");
340                 ConnectionErrorFactory.ThrowConnectionException((int)ConnectionError.InvalidParameter);
341             }
342
343             return new CellularProfile(profileHandle);
344         }
345
346         /// <summary>
347         /// The state of cellular connection.
348         /// </summary>
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
353         {
354             get
355             {
356                 return ConnectionInternalManager.Instance.CellularState;
357             }
358         }
359
360         /// <summary>
361         /// The state of the Wi-Fi.
362         /// </summary>
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
367         {
368             get
369             {
370                 return ConnectionInternalManager.Instance.WiFiState;
371             }
372         }
373
374         /// <summary>
375         /// The state of the Bluetooth.
376         /// </summary>
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
381         {
382             get
383             {
384                 return ConnectionInternalManager.Instance.BluetoothState;
385             }
386         }
387
388         /// <summary>
389         /// The Ethernet connection state.
390         /// </summary>
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
395         {
396             get
397             {
398                 return ConnectionInternalManager.Instance.EthernetState;
399             }
400         }
401
402         /// <summary>
403         /// Checks for ethernet cable is attached or not.
404         /// </summary>
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
409         {
410             get
411             {
412                 return ConnectionInternalManager.Instance.EthernetCableState;
413             }
414         }
415
416     } // class ConnectionManager
417
418     /// <summary>
419     /// This class contains connection information such as connection type and state.
420     /// </summary>
421     /// <since_tizen> 3 </since_tizen>
422     public class ConnectionItem
423     {
424         internal ConnectionItem()
425         {
426         }
427
428         /// <summary>
429         /// The type of the current profile for data connection.
430         /// </summary>
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
435         {
436             get
437             {
438                 return ConnectionInternalManager.Instance.ConnectionType;
439             }
440         }
441
442         /// <summary>
443         /// The state of the current profile for data connection.
444         /// </summary>
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
449         {
450             get
451             {
452                 if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Cellular)
453                 {
454                     if (ConnectionInternalManager.Instance.CellularState == CellularState.Connected)
455                     {
456                         return ConnectionState.Connected;
457                     }
458                     else if (ConnectionInternalManager.Instance.CellularState == CellularState.Available)
459                     {
460                         return ConnectionState.Disconnected;
461                     }
462                     else {
463                         return ConnectionState.Deactivated;
464                     }
465                 }
466                 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Bluetooth)
467                 {
468                     return ConnectionInternalManager.Instance.BluetoothState;
469                 }
470                 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.WiFi)
471                 {
472                     return ConnectionInternalManager.Instance.WiFiState;
473                 }
474                 else if (ConnectionInternalManager.Instance.ConnectionType == ConnectionType.Ethernet)
475                 {
476                     return ConnectionInternalManager.Instance.EthernetState;
477                 }
478                 else { // TO DO : Add Net Proxy
479                     return ConnectionState.Disconnected;
480                 }
481             }
482         }
483     } // class ConnectionItem
484
485     /// <summary>
486     /// An extended EventArgs class which contains changed connection type.
487     /// </summary>
488     /// <since_tizen> 3 </since_tizen>
489     public class ConnectionTypeEventArgs : EventArgs
490     {
491         private ConnectionType Type = ConnectionType.Disconnected;
492
493         internal ConnectionTypeEventArgs(ConnectionType type)
494         {
495             Type = type;
496         }
497
498         /// <summary>
499         /// The connection type.
500         /// </summary>
501         /// <since_tizen> 3 </since_tizen>
502         /// <value>Type of the connection.</value>
503         public ConnectionType ConnectionType
504         {
505             get
506             {
507                 return Type;
508             }
509         }
510     }
511
512     /// <summary>
513     /// An extended EventArgs class which contains changed ethernet cable state.
514     /// </summary>
515     /// <since_tizen> 3 </since_tizen>
516     public class EthernetCableStateEventArgs : EventArgs
517     {
518         private EthernetCableState State;
519
520         internal EthernetCableStateEventArgs(EthernetCableState state)
521         {
522             State = state;
523         }
524
525         /// <summary>
526         /// The ethernet cable state.
527         /// </summary>
528         /// <since_tizen> 3 </since_tizen>
529         /// <value>Attached or detached state of the ethernet cable.</value>
530         public EthernetCableState EthernetCableState
531         {
532             get
533             {
534                 return State;
535             }
536         }
537     }
538
539     /// <summary>
540     /// An extended EventArgs class which contains changed address.
541     /// </summary>
542     /// <since_tizen> 3 </since_tizen>
543     public class AddressEventArgs : EventArgs
544     {
545         private string IPv4 = "";
546         private string IPv6 = "";
547
548         internal AddressEventArgs(string ipv4, string ipv6)
549         {
550             IPv4 = ipv4;
551             IPv6 = ipv6;
552         }
553
554         /// <summary>
555         /// The  IPV4 address.
556         /// </summary>
557         /// <since_tizen> 3 </since_tizen>
558         /// <value>IP address in the format of IPV4 syntax.</value>
559         public string IPv4Address
560         {
561             get
562             {
563                 return IPv4;
564             }
565         }
566
567         /// <summary>
568         /// The  IPV6 address.
569         /// </summary>
570         /// <since_tizen> 3 </since_tizen>
571         /// <value>IP address in the format of IPV6 syntax.</value>
572         public string IPv6Address
573         {
574             get
575             {
576                 return IPv6;
577             }
578         }
579     }
580 }