Add API level
[platform/core/csapi/connection.git] / 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                 int Value;
288                 int ret = (int)ConnectionError.None;
289                 if (family == AddressFamily.IPv4)
290                 {
291                     ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
292                 }
293                 else
294                 {
295                     ret = Interop.ConnectionProfile.GetIPv6State(ProfileHandle, out Value);
296                 }
297
298                 if ((ConnectionError)ret != ConnectionError.None)
299                 {
300                     Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
301                 }
302                 return (ProfileState)Value;
303         }
304
305         /// <summary>
306         /// The Proxy type.
307         /// </summary>
308         /// <since_tizen> 3 </since_tizen>
309         /// <value>Proxy type of the connection.</value>
310         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
311         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
312         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
313         /// <exception cref="System.ObjectDisposedException">Thrown during set when operation is performed on a disposed object.</exception>
314         public ProxyType ProxyType
315         {
316             get
317             {
318                 int Value;
319                 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
320                 if ((ConnectionError)ret != ConnectionError.None)
321                 {
322                     Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
323                 }
324                 return (ProxyType)Value;
325
326             }
327
328             set
329             {
330                 CheckDisposed();
331                 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
332                 if ((ConnectionError)ret != ConnectionError.None)
333                 {
334                     Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
335                     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");
336                     ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
337                     ConnectionErrorFactory.ThrowConnectionException(ret);
338                 }
339             }
340         }
341
342         /// <summary>
343         /// The proxy address.
344         /// </summary>
345         /// <since_tizen> 3 </since_tizen>
346         /// <value>Proxy address of the connection.</value>
347         /// <exception cref="System.NotSupportedException">Thrown during set when feature is not supported.</exception>
348         /// <exception cref="System.ArgumentException">Thrown during set when value is invalid parameter.</exception>
349         /// <exception cref="System.ArgumentNullException">Thrown during set when value is null.</exception>
350         /// <exception cref="System.InvalidOperationException">Thrown during set when profile instance is invalid or when method failed due to invalid operation.</exception>
351         /// <exception cref="System.ObjectDisposedException">Thrown when operation is performed on a disposed object.</exception>
352         public string ProxyAddress
353         {
354             get
355             {
356                 IntPtr Value;
357                 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, out Value);
358                 if ((ConnectionError)ret != ConnectionError.None)
359                 {
360                     Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
361                 }
362                 string result = Marshal.PtrToStringAnsi(Value);
363                 Interop.Libc.Free(Value);
364                 return result;
365
366             }
367
368             set
369             {
370                 CheckDisposed();
371                 if (value != null)
372                 {
373                     int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.IPv4, value);
374                     if ((ConnectionError)ret != ConnectionError.None)
375                     {
376                         Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
377                         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");
378                         ConnectionErrorFactory.CheckHandleNullException(ret, (ProfileHandle == IntPtr.Zero), "ProfileHandle may have been disposed or released");
379                         ConnectionErrorFactory.ThrowConnectionException(ret);
380                     }
381                 }
382
383                 else
384                 {
385                     throw new ArgumentNullException("ProxyAddress is null");
386                 }
387             }
388         }
389
390         /// <summary>
391         /// The address information (IPv4)
392         /// </summary>
393         /// <since_tizen> 3 </since_tizen>
394         /// <value>Instance of IAddressInformation with IPV4 address.</value>
395         public IAddressInformation IPv4Settings
396         {
397             get
398             {
399                 return IPv4;
400
401             }
402         }
403
404         /// <summary>
405         /// The address information (IPv6)
406         /// </summary>
407         /// <since_tizen> 3 </since_tizen>
408         /// <value>Instance of IAddressInformation with IPV6 address.</value>
409         public IAddressInformation IPv6Settings
410         {
411             get
412             {
413                 return IPv6;
414             }
415         }
416     }
417
418     /// <summary>
419     /// An extended EventArgs class which contains changed profile state.
420     /// </summary>
421     /// <since_tizen> 3 </since_tizen>
422     public class ProfileStateEventArgs : EventArgs
423     {
424         private ProfileState _State = ProfileState.Disconnected;
425
426         internal ProfileStateEventArgs(ProfileState state)
427         {
428             _State = state;
429         }
430
431         /// <summary>
432         /// The profile state.
433         /// </summary>
434         /// <since_tizen> 3 </since_tizen>
435         /// <value>State type of the connection profile.</value>
436         public ProfileState State
437         {
438             get
439             {
440                 return _State;
441             }
442         }
443     }
444 }