[C# Connection] Adding C# Connection code
[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.Linq;
20 using System.Text;
21 using System.Threading.Tasks;
22 using System.Runtime.InteropServices;
23
24 /// <summary>
25 /// The Connection API provides functions, enumerations to get the status of network and current profile and manage profiles.
26 /// </summary>
27 namespace Tizen.Network.Connection
28 {
29     /// <summary>
30     /// This class is ConnectionManager
31     /// </summary>
32     public class ConnectionManager : IDisposable
33     {
34         static internal ConnectionItem CurConnction = new ConnectionItem();
35         private bool disposed = false;
36
37         static private EventHandler _ConnectionTypeChanged = null;
38         static private EventHandler _IPAddressChanged = null;
39         static private EventHandler _EthernetCableStateChanged = null;
40         static private EventHandler _ProxyAddressChanged = null;
41
42         /// <summary>
43         /// Event that is called when the type of the current connection is changed.
44         /// </summary>
45         static public event EventHandler ConnectionTypeChanged
46         {
47             add
48             {
49                 if (_ConnectionTypeChanged == null)
50                 {
51                     ConnectionTypeChangedStart();
52                 }
53                 _ConnectionTypeChanged += value;
54             }
55             remove
56             {
57                 _ConnectionTypeChanged -= value;
58                 if (_ConnectionTypeChanged == null)
59                 {
60                     ConnectionTypeChangedStop();
61                 }
62             }
63         }
64
65         static private void ConnectionTypeChangedStart()
66         {
67             int ret = Interop.Connection.SetTypeChangedCallback(ConnectionInternalManager.GetHandle(), TypeChangedCallback, IntPtr.Zero);
68             if ((ConnectionError)ret != ConnectionError.None)
69             {
70                 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
71             }
72         }
73
74         static private void ConnectionTypeChangedStop()
75         {
76             int ret = Interop.Connection.UnsetTypeChangedCallback(ConnectionInternalManager.GetHandle());
77             if ((ConnectionError)ret != ConnectionError.None)
78             {
79                 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
80             }
81         }
82
83         static private void TypeChangedCallback(ConnectionType type, IntPtr user_data)
84         {
85             if (_ConnectionTypeChanged != null)
86             {
87                 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
88             }
89         }
90
91         /// <summary>
92         /// Event for ethernet cable is plugged [in/out] event.
93         /// </summary>
94         static public event EventHandler EthernetCableStateChanged
95         {
96             add
97             {
98                 if (_EthernetCableStateChanged == null)
99                 {
100                     EthernetCableStateChangedStart();
101                 }
102                 _EthernetCableStateChanged += value;
103             }
104             remove
105             {
106                 _EthernetCableStateChanged -= value;
107                 if (_EthernetCableStateChanged == null)
108                 {
109                     EthernetCableStateChangedtop();
110                 }
111             }
112         }
113
114         static private void EthernetCableStateChangedStart()
115         {
116             int ret = Interop.Connection.SetEthernetCableStateChagedCallback(ConnectionInternalManager.GetHandle(), EthernetCableStateChangedCallback, IntPtr.Zero);
117             if ((ConnectionError)ret != ConnectionError.None)
118             {
119                 Log.Error(Globals.LogTag, "It failed to register ethernet cable state changed callback, " + (ConnectionError)ret);
120             }
121         }
122
123         static private void EthernetCableStateChangedtop()
124         {
125             int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(ConnectionInternalManager.GetHandle());
126             if ((ConnectionError)ret != ConnectionError.None)
127             {
128                 Log.Error(Globals.LogTag, "It failed to unregister ethernet cable state changed callback, " + (ConnectionError)ret);
129             }
130         }
131
132         static private void EthernetCableStateChangedCallback(EthernetCableState state, IntPtr user_data)
133         {
134             if (_EthernetCableStateChanged != null)
135             {
136                 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
137             }
138         }
139
140         /// <summary>
141         /// Event that is called when the IP address is changed.
142         /// </summary>
143         static public event EventHandler IpAddressChanged
144         {
145             add
146             {
147                 if (_IPAddressChanged == null)
148                 {
149                     IpAddressChangedStart();
150                 }
151                 _IPAddressChanged += value;
152             }
153             remove
154             {
155                 _IPAddressChanged -= value;
156                 if (_IPAddressChanged == null)
157                 {
158                     IpAddressChangedStop();
159                 }
160             }
161         }
162
163         static private void IpAddressChangedStart()
164         {
165             int ret = Interop.Connection.SetIpAddressChangedCallback(ConnectionInternalManager.GetHandle(), IPAddressChangedCallback, IntPtr.Zero);
166             if ((ConnectionError)ret != ConnectionError.None)
167             {
168                 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
169             }
170         }
171
172         static private void IpAddressChangedStop()
173         {
174             int ret = Interop.Connection.UnsetIpAddressChangedCallback(ConnectionInternalManager.GetHandle());
175             if ((ConnectionError)ret != ConnectionError.None)
176             {
177                 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
178             }
179         }
180
181         static private void IPAddressChangedCallback(IntPtr Ipv4, IntPtr Ipv6, IntPtr UserData)
182         {
183             if (_IPAddressChanged != null)
184             {
185                 string ipv4 = Marshal.PtrToStringAnsi(Ipv4);
186                 string ipv6 = Marshal.PtrToStringAnsi(Ipv6);
187
188                 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
189                 {
190                     _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
191                 }
192             }
193         }
194
195         /// <summary>
196         /// Event that is called when the proxy address is changed.
197         /// </summary>
198         static public event EventHandler ProxyAddressChanged
199         {
200             add
201             {
202                 //Console.WriteLine("ProxyAddressChanged Add **");
203                 if (_ProxyAddressChanged == null)
204                 {
205                     ProxyAddressChangedStart();
206                 }
207                 _ProxyAddressChanged += value;
208             }
209             remove
210             {
211                 //Console.WriteLine("ProxyAddressChanged Remove");
212                 _ProxyAddressChanged -= value;
213                 if (_ProxyAddressChanged == null)
214                 {
215                     ProxyAddressChangedStop();
216                 }
217             }
218         }
219
220         static private void ProxyAddressChangedStart()
221         {
222             int ret = Interop.Connection.SetProxyAddressChangedCallback(ConnectionInternalManager.GetHandle(), IPAddressChangedCallback, IntPtr.Zero);
223             if ((ConnectionError)ret != ConnectionError.None)
224             {
225                 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
226             }
227         }
228
229         static private void ProxyAddressChangedStop()
230         {
231             int ret = Interop.Connection.UnsetProxyAddressChangedCallback(ConnectionInternalManager.GetHandle());
232             if ((ConnectionError)ret != ConnectionError.None)
233             {
234                 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
235             }
236         }
237
238         static private void ProxyAddressChangedCallback(IntPtr Ipv4, IntPtr Ipv6, IntPtr UserData)
239         {
240             if (_ProxyAddressChanged != null)
241             {
242                 string ipv4 = Marshal.PtrToStringAnsi(Ipv4);
243                 string ipv6 = Marshal.PtrToStringAnsi(Ipv6);
244                 Interop.Libc.Free(Ipv4);
245                 Interop.Libc.Free(Ipv6);
246
247                 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
248             }
249         }
250
251         internal ConnectionManager()
252         {
253         }
254
255         ~ConnectionManager()
256         {
257             Dispose(false);
258         }
259
260         public void Dispose()
261         {
262             Dispose(true);
263             GC.SuppressFinalize(this);
264         }
265
266         private void Dispose(bool disposing)
267         {
268             if (disposed)
269                 return;
270
271             if (disposing)
272             {
273                 // Free managed objects.
274             }
275             ProxyAddressChangedStop();
276             ConnectionTypeChangedStop();
277             EthernetCableStateChangedtop();
278             IpAddressChangedStop();
279             disposed = true;
280         }
281
282
283         /// <summary>
284         /// Gets the IP address of the current connection.
285         /// </summary>
286         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
287         static public string GetIpAddress(AddressFamily family)
288         {
289             return ConnectionInternalManager.GetIpAddress(family);
290         }
291
292         /// <summary>
293         /// Gets the proxy address of the current connection.
294         /// </summary>
295         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
296         static public string GetProxy(AddressFamily family)
297         {
298             return ConnectionInternalManager.GetProxy(family);
299         }
300
301         /// <summary>
302         /// Gets the MAC address of the Wi-Fi or ethernet.
303         /// </summary>
304         /// <exception cref="InvalidOperationException">Thrown when method failed due to invalid operation</exception>
305         static public string GetMacAddress(ConnectionType type)
306         {
307             return ConnectionInternalManager.GetMacAddress(type);
308         }
309
310         /// <summary>
311         /// Gets the type of the current profile for data connection.
312         /// </summary>
313         static public ConnectionItem CurrentConnection
314         {
315             get
316             {
317                 return CurConnction;
318             }
319         }
320
321         /// <summary>
322         /// Gets the state of cellular connection.
323         /// </summary>
324         static public CellularState CellularState
325         {
326             get
327             {
328                 return ConnectionInternalManager.CellularState;
329             }
330         }
331
332         /// <summary>
333         /// Gets the state of the Wi-Fi.
334         /// </summary>
335         /// <privilege>http://tizen.org/privilege/network.get</privilege>
336         static public ConnectionState WiFiState
337         {
338             get
339             {
340                 return ConnectionInternalManager.WiFiState;
341             }
342         }
343
344         /// <summary>
345         /// The state of the Bluetooth.
346         /// </summary>
347         /// <privilege>http://tizen.org/privilege/network.get</privilege>
348         static public ConnectionState BluetoothState
349         {
350             get
351             {
352                 return ConnectionInternalManager.BluetoothState;
353             }
354         }
355
356         /// <summary>
357         /// The Ethernet connection state.
358         /// </summary>
359         /// <privilege>http://tizen.org/privilege/network.get</privilege>
360         static public ConnectionState EthernetState
361         {
362             get
363             {
364                 return ConnectionInternalManager.EthernetState;
365             }
366         }
367
368         /// <summary>
369         /// Checks for ethernet cable is attached or not.
370         /// </summary>
371         /// <privilege>http://tizen.org/privilege/network.get</privilege>
372         static public EthernetCableState EthernetCableState
373         {
374             get
375             {
376                 return ConnectionInternalManager.EthernetCableState;
377             }
378         }
379     }
380
381     /// <summary>
382     ///
383     /// </summary>
384     public class ConnectionItem
385     {
386         internal ConnectionItem()
387         {
388         }
389
390         /// <summary>
391         /// Gets the type of the current profile for data connection.
392         /// </summary>
393         public ConnectionType Type
394         {
395             get
396             {
397                 return ConnectionInternalManager.ConnectionType;
398             }
399         }
400
401         /// <summary>
402         /// Gets the type of the current profile for data connection.
403         /// </summary>
404         public ConnectionState State
405         {
406             get
407             {
408                 if (ConnectionInternalManager.ConnectionType == ConnectionType.Cellular)
409                 {
410                     if (ConnectionInternalManager.CellularState == CellularState.Connected)
411                     {
412                         return ConnectionState.Connected;
413                     }
414                     else if (ConnectionInternalManager.CellularState == CellularState.Available)
415                     {
416                         return ConnectionState.Disconnected;
417                     }
418                     else {
419                         return ConnectionState.Deactivated;
420                     }
421                 }
422                 else if (ConnectionInternalManager.ConnectionType == ConnectionType.Bluetooth)
423                 {
424                     return ConnectionInternalManager.BluetoothState;
425                 }
426                 else if (ConnectionInternalManager.ConnectionType == ConnectionType.WiFi)
427                 {
428                     return ConnectionInternalManager.WiFiState;
429                 }
430                 else if (ConnectionInternalManager.ConnectionType == ConnectionType.Ethernet)
431                 {
432                     return ConnectionInternalManager.EthernetState;
433                 }
434                 else { // TO DO : Add Net Proxy
435                     return ConnectionState.Disconnected;
436                 }
437             }
438         }
439
440     }
441
442     /// <summary>
443     /// An extended EventArgs class which contains changed connection type.
444     /// </summary>
445     public class ConnectionTypeEventArgs : EventArgs
446     {
447         private ConnectionType Type = ConnectionType.Disconnected;
448
449         internal ConnectionTypeEventArgs(ConnectionType type)
450         {
451             Type = type;
452         }
453
454         /// <summary>
455         /// The connection type.
456         /// </summary>
457         public ConnectionType ConnectionType
458         {
459             get
460             {
461                 return Type;
462             }
463         }
464     }
465
466     /// <summary>
467     /// An extended EventArgs class which contains changed ethernet cable state.
468     /// </summary>
469     public class EthernetCableStateEventArgs : EventArgs
470     {
471         private EthernetCableState State;
472
473         internal EthernetCableStateEventArgs(EthernetCableState state)
474         {
475             State = state;
476         }
477
478         /// <summary>
479         /// The ethernet cable state.
480         /// </summary>
481         public EthernetCableState EthernetCableState
482         {
483             get
484             {
485                 return State;
486             }
487         }
488     }
489
490     /// <summary>
491     /// An extended EventArgs class which contains changed address.
492     /// </summary>
493     public class AddressEventArgs : EventArgs
494     {
495         private string Ipv4 = "";
496         private string Ipv6 = "";
497
498         internal AddressEventArgs(string ipv4, string ipv6)
499         {
500             Ipv4 = ipv4;
501             Ipv6 = ipv6;
502         }
503
504         /// <summary>
505         /// The  IPV4 address.
506         /// </summary>
507         public string Ipv4Address
508         {
509             get
510             {
511                 return Ipv4;
512             }
513         }
514
515         /// <summary>
516         /// The  IPV6 address.
517         /// </summary>
518         public string Ipv6Address
519         {
520             get
521             {
522                 return Ipv6;
523             }
524         }
525     }
526 }