Merge "Add TC for IsOpaque(evas_object_image_alpha_set)"
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / ConnectionProfile.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.Runtime.InteropServices;
22
23 namespace Tizen.Network.Connection
24 {
25     /// <summary>
26     /// This is the ConnectionProfile class. It provides event and properties of the connection profile.
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class ConnectionProfile : IDisposable
30     {
31         internal IntPtr ProfileHandle = IntPtr.Zero;
32         private IAddressInformation IPv4;
33         private IAddressInformation IPv6;
34         private bool disposed = false;
35         private EventHandler<ProfileStateEventArgs> _ProfileStateChanged = null;
36
37         private Interop.ConnectionProfile.ProfileStateChangedCallback _profileChangedCallback;
38
39         internal IntPtr GetHandle()
40         {
41             return ProfileHandle;
42         }
43
44         /// <summary>
45         /// The event is called when the state of profile is changed.
46         /// </summary>
47         /// <since_tizen> 3 </since_tizen>
48         /// <feature>http://tizen.org/feature/network.ethernet</feature>
49         /// <feature>http://tizen.org/feature/network.telephony</feature>
50         /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
51         /// <feature>http://tizen.org/feature/network.wifi</feature>
52         /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
53         public event EventHandler<ProfileStateEventArgs> ProfileStateChanged
54         {
55             add
56             {
57                 Log.Debug(Globals.LogTag, "ProfileStateChanged add");
58                 if (_ProfileStateChanged == null)
59                 {
60                     ProfileStateChangedStart();
61                 }
62                 _ProfileStateChanged += value;
63             }
64             remove
65             {
66                 Log.Debug(Globals.LogTag, "ProfileStateChanged remove");
67                 _ProfileStateChanged -= value;
68                 if (_ProfileStateChanged == null)
69                 {
70                     ProfileStateChangedStop();
71                 }
72             }
73         }
74
75         private void ProfileStateChangedStart()
76         {
77             _profileChangedCallback = (ProfileState state, IntPtr userData) =>
78             {
79                 if (_ProfileStateChanged != null)
80                 {
81                     _ProfileStateChanged(null, new ProfileStateEventArgs(state));
82                 }
83             };
84
85             Log.Debug(Globals.LogTag, "ProfileStateChangedStart");
86             int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, _profileChangedCallback, IntPtr.Zero);
87             if ((ConnectionError)ret != ConnectionError.None)
88             {
89                 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
90             }
91         }
92
93         private void ProfileStateChangedStop()
94         {
95             Log.Debug(Globals.LogTag, "ProfileStateChangedStop");
96             int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
97             if ((ConnectionError)ret != ConnectionError.None)
98             {
99                 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
100             }
101         }
102
103         internal ConnectionProfile(IntPtr handle)
104         {
105             ProfileHandle = handle;
106             IPv4 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv4);
107             IPv6 = new ConnectionAddressInformation(ProfileHandle, AddressFamily.IPv6);
108         }
109
110         /// <summary>
111         /// Destroy the ConnectionProfile object
112         /// </summary>
113         ~ConnectionProfile()
114         {
115             Dispose(false);
116         }
117
118         /// <summary>
119         /// Disposes the memory allocated to unmanaged resources.
120         /// </summary>
121         /// <since_tizen> 3 </since_tizen>
122         public void Dispose()
123         {
124             Dispose(true);
125             GC.SuppressFinalize(this);
126         }
127
128         private void Dispose(bool disposing)
129         {
130             Log.Debug(Globals.LogTag, ">>> ConnectionProfile Dispose with " + disposing);
131             if (disposed)
132                 return;
133
134             if (disposing)
135             {
136                 // Free managed objects.
137                 UnregisterEvents();
138                 Destroy();
139             }
140             disposed = true;
141         }
142
143         private void UnregisterEvents()
144         {
145             if (_ProfileStateChanged != null)
146             {
147                 ProfileStateChangedStop();
148             }
149         }
150
151         private void Destroy()
152         {
153             Interop.ConnectionProfile.Destroy(ProfileHandle);
154             ProfileHandle = IntPtr.Zero;
155         }
156
157         internal void CheckDisposed()
158         {
159             if (disposed)
160             {
161                 throw new ObjectDisposedException(GetType().FullName);
162             }
163         }
164
165         /// <summary>
166         /// The profile ID.
167         /// </summary>
168         /// <since_tizen> 3 </since_tizen>
169         /// <value>Unique ID of the profile.</value>
170         public string Id
171         {
172             get
173             {
174                 IntPtr Value;
175                 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
176                 if ((ConnectionError)ret != ConnectionError.None)
177                 {
178                     Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
179                 }
180                 string result = Marshal.PtrToStringAnsi(Value);
181                 Interop.Libc.Free(Value);
182                 return result;
183             }
184         }
185
186         /// <summary>
187         /// The profile name.
188         /// </summary>
189         /// <since_tizen> 3 </since_tizen>
190         /// <value>User friendly name of the profile.</value>
191         public string Name
192         {
193             get
194             {
195                 IntPtr Value;
196                 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
197                 if ((ConnectionError)ret != ConnectionError.None)
198                 {
199                     Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
200                 }
201                 string result = Marshal.PtrToStringAnsi(Value);
202                 Interop.Libc.Free(Value);
203                 return result;
204             }
205         }
206
207         /// <summary>
208         /// The network type.
209         /// </summary>
210         /// <since_tizen> 3 </since_tizen>
211         /// <value>Profile type of the network connection.</value>
212         public ConnectionProfileType Type
213         {
214             get
215             {
216                 int Value;
217                 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
218                 if ((ConnectionError)ret != ConnectionError.None)
219                 {
220                     Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
221                 }
222                 return (ConnectionProfileType)Value;
223             }
224         }
225
226         /// <summary>
227         /// The name of the network interface.
228         /// </summary>
229         /// <since_tizen> 3 </since_tizen>
230         /// <value>Network interface name, for example, eth0 and pdp0.</value>
231         public string InterfaceName
232         {
233             get
234             {
235                 IntPtr Value;
236                 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
237                 if ((ConnectionError)ret != ConnectionError.None)
238                 {
239                     Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
240                 }
241                 string result = Marshal.PtrToStringAnsi(Value);
242                 Interop.Libc.Free(Value);
243                 return result;
244             }
245         }
246
247         /// <summary>
248         /// Refreshes the profile information.
249         /// </summary>
250         /// <since_tizen> 3 </since_tizen>
251         /// <privilege>http://tizen.org/privilege/network.get</privilege>
252         /// <feature>http://tizen.org/feature/network.ethernet</feature>
253         /// <feature>http://tizen.org/feature/network.telephony</feature>
254         /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
255         /// <feature>http://tizen.org/feature/network.wifi</feature>
256         /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
257         /// <exception cref="System.UnauthorizedAccessException">Thrown when a permission is denied.</exception>
258         /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
259         /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
260         /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
261         public void Refresh()
262         {
263             CheckDisposed();
264             int ret = Interop.ConnectionProfile.Refresh(ProfileHandle);
265             if ((ConnectionError)ret != ConnectionError.None)
266             {
267                 Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
268                 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
269                 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
270                 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
271                 ConnectionErrorFactory.ThrowConnectionException(ret);
272             }
273         }
274
275         /// <summary>
276         /// Gets the network state.
277         /// </summary>
278         /// <since_tizen> 3 </since_tizen>
279         /// <param name="family">The address family.</param>
280         /// <returns>The network state.</returns>
281         /// <feature>http://tizen.org/feature/network.ethernet</feature>
282         /// <feature>http://tizen.org/feature/network.telephony</feature>
283         /// <feature>http://tizen.org/feature/network.tethering.bluetooth</feature>
284         /// <feature>http://tizen.org/feature/network.wifi</feature>
285         /// <exception cref="System.NotSupportedException">Thrown when a feature is not supported.</exception>
286         /// <exception cref="System.ArgumentException">Thrown when a value is an invalid parameter.</exception>
287         /// <exception cref="System.InvalidOperationException">Thrown when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
288         /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
289         public ProfileState GetState(AddressFamily family)
290         {
291             CheckDisposed();
292             int Value;
293             int ret = (int)ConnectionError.None;
294             if (family == AddressFamily.IPv4)
295             {
296                 ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
297             }
298
299             else
300             {
301                 ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
302             }
303
304             if ((ConnectionError)ret != ConnectionError.None)
305             {
306                 Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
307                 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
308                 ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
309                 ConnectionErrorFactory.ThrowConnectionException(ret);
310             }
311
312             return (ProfileState)Value;
313         }
314
315         /// <summary>
316         /// The Proxy type.
317         /// </summary>
318         /// <since_tizen> 3 </since_tizen>
319         /// <value>Proxy type of the connection.</value>
320         /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
321         /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
322         /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
323         /// <exception cref="System.ObjectDisposedException">Thrown during set when a operation is performed on a disposed object.</exception>
324         public ProxyType ProxyType
325         {
326             get
327             {
328                 int Value;
329                 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
330                 if ((ConnectionError)ret != ConnectionError.None)
331                 {
332                     Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
333                 }
334                 return (ProxyType)Value;
335
336             }
337
338             set
339             {
340                 CheckDisposed();
341                 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
342                 if ((ConnectionError)ret != ConnectionError.None)
343                 {
344                     Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
345                     ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
346                     ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
347                     ConnectionErrorFactory.ThrowConnectionException(ret);
348                 }
349             }
350         }
351
352         /// <summary>
353         /// The proxy address.
354         /// </summary>
355         /// <since_tizen> 3 </since_tizen>
356         /// <value>Proxy address of the connection.</value>
357         /// <exception cref="System.NotSupportedException">Thrown during set when a feature is not supported.</exception>
358         /// <exception cref="System.ArgumentException">Thrown during set when a value is an invalid parameter.</exception>
359         /// <exception cref="System.ArgumentNullException">Thrown during set when a value is null.</exception>
360         /// <exception cref="System.InvalidOperationException">Thrown during set when a profile instance is invalid or when a method fails due to an invalid operation.</exception>
361         /// <exception cref="System.ObjectDisposedException">Thrown when an operation is performed on a disposed object.</exception>
362         public string ProxyAddress
363         {
364             get
365             {
366                 IntPtr Value;
367                 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
368                 if ((ConnectionError)ret != ConnectionError.None)
369                 {
370                     Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
371                 }
372                 string result = Marshal.PtrToStringAnsi(Value);
373                 Interop.Libc.Free(Value);
374                 return result;
375
376             }
377
378             set
379             {
380                 CheckDisposed();
381                 if (value != null)
382                 {
383                     int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
384                     if ((ConnectionError)ret != ConnectionError.None)
385                     {
386                         Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
387                         ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
388                         ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
389                         ConnectionErrorFactory.ThrowConnectionException(ret);
390                     }
391                 }
392
393                 else
394                 {
395                     throw new ArgumentNullException("ProxyAddress is null");
396                 }
397             }
398         }
399
400         /// <summary>
401         /// The address information (IPv4).
402         /// </summary>
403         /// <since_tizen> 3 </since_tizen>
404         /// <value>Instance of IAddressInformation with IPV4 address.</value>
405         public IAddressInformation IPv4Settings
406         {
407             get
408             {
409                 return IPv4;
410
411             }
412         }
413
414         /// <summary>
415         /// The address information (IPv6).
416         /// </summary>
417         /// <since_tizen> 3 </since_tizen>
418         /// <value>Instance of IAddressInformation with IPV6 address.</value>
419         public IAddressInformation IPv6Settings
420         {
421             get
422             {
423                 return IPv6;
424             }
425         }
426     }
427
428     /// <summary>
429     /// An extended EventArgs class, which contains changed profile state.
430     /// </summary>
431     /// <since_tizen> 3 </since_tizen>
432     public class ProfileStateEventArgs : EventArgs
433     {
434         private ProfileState _State = ProfileState.Disconnected;
435
436         internal ProfileStateEventArgs(ProfileState state)
437         {
438             _State = state;
439         }
440
441         /// <summary>
442         /// The profile state.
443         /// </summary>
444         /// <since_tizen> 3 </since_tizen>
445         /// <value>State type of the connection profile.</value>
446         public ProfileState State
447         {
448             get
449             {
450                 return _State;
451             }
452         }
453     }
454 }