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