[C# Connection] Adding C# Connection code
[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
27     /// </summary>
28     public class ConnectionProfile : IDisposable
29     {
30         internal IntPtr ProfileHandle = IntPtr.Zero;
31         private IAddressInformation Ipv4;
32         private IAddressInformation Ipv6;
33         private bool disposed = false;
34         private EventHandler _ProfileStateChanged;
35
36         /// <summary>
37         /// The event that is called when the state of profile is changed.
38         /// </summary>
39         public event EventHandler ProfileStateChanged
40         {
41             add
42             {
43                 if (_ProfileStateChanged == null)
44                 {
45                     ProfileStateChangedStart();
46                 }
47                 _ProfileStateChanged += value;
48             }
49             remove
50             {
51                 _ProfileStateChanged -= value;
52                 if (_ProfileStateChanged == null)
53                 {
54                     ProfileStateChangedStop();
55                 }
56             }
57         }
58
59         private void TypeChangedCallback(ProfileState state, IntPtr userData)
60         {
61             if (_ProfileStateChanged != null)
62             {
63                 _ProfileStateChanged(null, new ProfileStateEventArgs(state));
64             }
65         }
66
67         private void ProfileStateChangedStart()
68         {
69             int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, TypeChangedCallback, IntPtr.Zero);
70             if ((ConnectionError)ret != ConnectionError.None)
71             {
72                 Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret);
73             }
74         }
75
76         private void ProfileStateChangedStop()
77         {
78             int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle);
79             if ((ConnectionError)ret != ConnectionError.None)
80             {
81                 Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret);
82             }
83         }
84
85         public ConnectionProfile(IntPtr handle)
86         {
87             ProfileHandle = handle;
88             Ipv4 = AddressFactory.CreateAddressInformation(handle, AddressFamily.Ipv4, AddressInformationType.Connection);
89             Ipv6 = AddressFactory.CreateAddressInformation(handle, AddressFamily.Ipv6, AddressInformationType.Connection);
90         }
91
92         ~ConnectionProfile()
93         {
94             Dispose(false);
95         }
96
97         public void Dispose()
98         {
99             Dispose(true);
100             GC.SuppressFinalize(this);
101         }
102
103         private void Dispose(bool disposing)
104         {
105             if (disposed)
106                 return;
107
108             if (disposing)
109             {
110                 // Free managed objects.
111             }
112             Interop.ConnectionProfile.Destroy(ProfileHandle);
113             ProfileStateChangedStop();
114             disposed = true;
115         }
116
117         /// <summary>
118         /// Gets the profile ID.
119         /// </summary>
120         public string Id
121         {
122             get
123             {
124                 IntPtr Value;
125                 int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value);
126                 if ((ConnectionError)ret != ConnectionError.None)
127                 {
128                     Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret);
129                 }
130                 string result = Marshal.PtrToStringAnsi(Value);
131                 Interop.Libc.Free(Value);
132                 return result;
133             }
134         }
135
136         /// <summary>
137         /// Gets the profile name.
138         /// </summary>
139         /// <privilege>http://tizen.org/privilege/network.get</privilege>
140         public string Name
141         {
142             get
143             {
144                 IntPtr Value;
145                 int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value);
146                 if ((ConnectionError)ret != ConnectionError.None)
147                 {
148                     Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret);
149                 }
150                 string result = Marshal.PtrToStringAnsi(Value);
151                 Interop.Libc.Free(Value);
152                 return result;
153             }
154         }
155
156         /// <summary>
157         /// Gets the network type.
158         /// </summary>
159         public ConnectionProfileType Type
160         {
161             get
162             {
163                 int Value;
164                 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
165                 if ((ConnectionError)ret != ConnectionError.None)
166                 {
167                     Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret);
168                 }
169                 return (ConnectionProfileType)Value;
170             }
171         }
172
173         /// <summary>
174         /// Gets the name of the network interface, e.g. eth0 and pdp0.
175         /// </summary>
176         public string InterfaceName
177         {
178             get
179             {
180                 IntPtr Value;
181                 int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value);
182                 if ((ConnectionError)ret != ConnectionError.None)
183                 {
184                     Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret);
185                 }
186                 string result = Marshal.PtrToStringAnsi(Value);
187                 Interop.Libc.Free(Value);
188                 return result;
189             }
190         }
191
192         /// <summary>
193         /// Gets the profile state.
194         /// </summary>
195         public ProfileState State
196         {
197             get
198             {
199                 int Value;
200                 int ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value);
201                 if ((ConnectionError)ret != ConnectionError.None)
202                 {
203                     Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret);
204                 }
205                 return (ProfileState)Value;
206             }
207         }
208
209         /// <summary>
210         /// Gets the Proxy type.
211         /// </summary>
212         public ProxyType ProxyType
213         {
214             get
215             {
216                 int Value;
217                 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
218                 if ((ConnectionError)ret != ConnectionError.None)
219                 {
220                     Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
221                 }
222                 return (ProxyType)Value;
223
224             }
225             set
226             {
227                 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
228                 if ((ConnectionError)ret != ConnectionError.None)
229                 {
230                     Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
231                     ConnectionErrorFactory.ThrowConnectionException(ret);
232                 }
233             }
234         }
235
236         /// <summary>
237         /// The proxy address.
238         /// </summary>
239         public String ProxyAddress
240         {
241             get
242             {
243                 IntPtr Value;
244                 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value);
245                 if ((ConnectionError)ret != ConnectionError.None)
246                 {
247                     Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
248                 }
249                 string result = Marshal.PtrToStringAnsi(Value);
250                 Interop.Libc.Free(Value);
251                 return result;
252
253             }
254             set
255             {
256                 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString());
257                 if ((ConnectionError)ret != ConnectionError.None)
258                 {
259                     Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
260                     ConnectionErrorFactory.ThrowConnectionException(ret);
261                 }
262             }
263         }
264
265         /// <summary>
266         /// The subnet mask address(Ipv4).
267         /// </summary>
268         public IAddressInformation Ipv4Settings
269         {
270             get
271             {
272                 return Ipv4;
273
274             }
275         }
276
277         /// <summary>
278         /// The subnet mask address(Ipv4).
279         /// </summary>
280         public IAddressInformation Ipv6Settings
281         {
282             get
283             {
284                 return Ipv6;
285             }
286         }
287     }
288
289     /// <summary>
290     /// An extended EventArgs class which contains changed profile state.
291     /// </summary>
292     public class ProfileStateEventArgs : EventArgs
293     {
294         private ProfileState _State = ProfileState.Disconnected;
295
296         internal ProfileStateEventArgs(ProfileState state)
297         {
298             _State = state;
299         }
300
301         /// <summary>
302         /// The profile state.
303         /// </summary>
304         public ProfileState State
305         {
306             get
307             {
308                 return _State;
309             }
310         }
311     }
312 }