[C# Connection] Adding C# Connection code
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / ConnectionInternalManager.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 using System.Collections;
24
25 namespace Tizen.Network.Connection
26 {
27     class HandleHolder : IDisposable
28     {
29         private IntPtr Handle;
30         private bool disposed = false;
31
32         public HandleHolder()
33         {
34             Log.Debug(Globals.LogTag, "HandleHolder() ^^");
35             Interop.Connection.Create(out Handle);
36             Log.Debug(Globals.LogTag, "Handle: " + Handle);
37         }
38
39         ~HandleHolder()
40         {
41             Dispose(false);
42         }
43
44         internal IntPtr GetHandle()
45         {
46             return Handle;
47         }
48         public void Dispose()
49         {
50             Dispose(true);
51             GC.SuppressFinalize(this);
52         }
53
54         private void Dispose(bool disposing)
55         {
56             if (disposed)
57                 return;
58
59             if (disposing)
60             {
61                 // Free managed objects.
62             }
63             Interop.Connection.Destroy(Handle);
64             disposed = true;
65         }
66     }
67
68     static class ConnectionInternalManager
69     {
70         private static HandleHolder Holder = new HandleHolder();
71
72         static public IntPtr GetHandle()
73         {
74             return Holder.GetHandle();
75         }
76
77         static internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
78         {
79             return Interop.Connection.GetProfileIterator(Holder.GetHandle(), (int)type, out iterator);
80         }
81
82         static internal bool HasNext(IntPtr iterator)
83         {
84             return Interop.Connection.HasNextProfileIterator(iterator);
85         }
86
87         static internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
88         {
89             return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
90         }
91
92         static internal int DestoryProfileIterator(IntPtr iterator)
93         {
94             return Interop.Connection.DestroyProfileIterator(iterator);
95         }
96
97         static public string GetIpAddress(AddressFamily family)
98         {
99             IntPtr ip;
100             int ret = Interop.Connection.GetIpAddress(Holder.GetHandle(), (int)family, out ip);
101             if ((ConnectionError)ret != ConnectionError.None)
102             {
103                 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
104                 ConnectionErrorFactory.ThrowConnectionException(ret);
105             }
106             string result = Marshal.PtrToStringAnsi(ip);
107             Interop.Libc.Free(ip);
108             return result;
109         }
110
111         static public string GetProxy(AddressFamily family)
112         {
113             IntPtr ip;
114             int ret = Interop.Connection.GetProxy(Holder.GetHandle(), (int)family, out ip);
115             if ((ConnectionError)ret != ConnectionError.None)
116             {
117                 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
118                 ConnectionErrorFactory.ThrowConnectionException(ret);
119             }
120             string result = Marshal.PtrToStringAnsi(ip);
121             Interop.Libc.Free(ip);
122             return result;
123         }
124
125         static public string GetMacAddress(ConnectionType type)
126         {
127             IntPtr ip;
128             int ret = Interop.Connection.GetMacAddress(Holder.GetHandle(), (int)type, out ip);
129             if ((ConnectionError)ret != ConnectionError.None)
130             {
131                 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
132                 ConnectionErrorFactory.ThrowConnectionException(ret);
133             }
134             string result = Marshal.PtrToStringAnsi(ip);
135             Interop.Libc.Free(ip);
136             return result;
137         }
138
139         static public ConnectionType ConnectionType
140         {
141             get
142             {
143                 int type = 0;
144                 Log.Debug(Globals.LogTag, "Handle: " + Holder.GetHandle());
145                 int ret = Interop.Connection.GetType(Holder.GetHandle(), out type);
146                 if ((ConnectionError)ret != ConnectionError.None)
147                 {
148                     Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
149                 }
150                 return (ConnectionType)type;
151             }
152         }
153
154         static public CellularState CellularState
155         {
156             get
157             {
158                 int type = 0;
159                 int ret = Interop.Connection.GetCellularState(Holder.GetHandle(), out type);
160                 if ((ConnectionError)ret != ConnectionError.None)
161                 {
162                     Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
163                 }
164                 return (CellularState)type;
165             }
166         }
167
168         static public ConnectionState WiFiState
169         {
170             get
171             {
172                 int type = 0;
173                 int ret = Interop.Connection.GetWiFiState(Holder.GetHandle(), out type);
174                 if ((ConnectionError)ret != ConnectionError.None)
175                 {
176                     Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
177                 }
178                 return (ConnectionState)type;
179             }
180         }
181
182         static public ConnectionState BluetoothState
183         {
184             get
185             {
186                 int type = 0;
187                 int ret = Interop.Connection.GetBtState(Holder.GetHandle(), out type);
188                 if ((ConnectionError)ret != ConnectionError.None)
189                 {
190                     Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
191                 }
192                 return (ConnectionState)type;
193             }
194         }
195
196         static public ConnectionState EthernetState
197         {
198             get
199             {
200                 int type = 0;
201                 int ret = Interop.Connection.GetEthernetState(Holder.GetHandle(), out type);
202                 if ((ConnectionError)ret != ConnectionError.None)
203                 {
204                     Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
205                 }
206                 return (ConnectionState)type;
207             }
208         }
209
210         static public EthernetCableState EthernetCableState
211         {
212             get
213             {
214                 int type = 0;
215                 int ret = Interop.Connection.GetEthernetCableState(Holder.GetHandle(), out type);
216                 if ((ConnectionError)ret != ConnectionError.None)
217                 {
218                     Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
219                 }
220                 return (EthernetCableState)type;
221             }
222         }
223
224         static public IntPtr CreateRequestProfile(ConnectionProfileType type, string keyword)
225         {
226             Log.Error(Globals.LogTag, "CreateRequestProfile, " + type + ", " + keyword);
227             Log.Debug(Globals.LogTag, "Handle: " + GetHandle());
228             IntPtr handle = IntPtr.Zero;
229             int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
230             if ((ConnectionError)ret != ConnectionError.None)
231             {
232                 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
233                 ConnectionErrorFactory.ThrowConnectionException(ret);
234             }
235
236             return handle;
237         }
238
239         static public int AddProfile(RequestProfile profile)
240         {
241             int ret = 0;
242             if (profile.Type == ConnectionProfileType.Cellular)
243             {
244                 ret = Interop.Connection.AddProfile(Holder.GetHandle(), ((RequestCellularProfile)profile).ProfileHandle);
245                 if ((ConnectionError)ret != ConnectionError.None)
246                 {
247                     Log.Error(Globals.LogTag, "It failed to add profile, " + (ConnectionError)ret);
248                     ConnectionErrorFactory.ThrowConnectionException(ret);
249                 }
250             }
251             else if (profile.Type == ConnectionProfileType.WiFi)
252             {
253                 ret = Interop.Connection.AddProfile(Holder.GetHandle(), ((RequestWiFiProfile)profile).ProfileHandle);
254                 if ((ConnectionError)ret != ConnectionError.None)
255                 {
256                     Log.Error(Globals.LogTag, "It failed to add profile, " + (ConnectionError)ret);
257                     ConnectionErrorFactory.ThrowConnectionException(ret);
258                 }
259             }
260             return ret;
261         }
262
263         static public int RemoveProfile(ConnectionProfile profile)
264         {
265             int ret = Interop.Connection.RemoveProfile(Holder.GetHandle(), profile.ProfileHandle);
266             if ((ConnectionError)ret != ConnectionError.None)
267             {
268                 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
269                 ConnectionErrorFactory.ThrowConnectionException(ret);
270             }
271             return ret;
272         }
273
274         static public int UpdateProfile(ConnectionProfile profile)
275         {
276             int ret = Interop.Connection.UpdateProfile(Holder.GetHandle(), profile.ProfileHandle);
277             if ((ConnectionError)ret != ConnectionError.None)
278             {
279                 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
280                 ConnectionErrorFactory.ThrowConnectionException(ret);
281             }
282             return ret;
283         }
284
285         static public ConnectionProfile GetCurrentProfile()
286         {
287             IntPtr ProfileHandle;
288             int ret = Interop.Connection.GetCurrentProfile(Holder.GetHandle(), out ProfileHandle);
289             if ((ConnectionError)ret != ConnectionError.None)
290             {
291                 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
292                 ConnectionErrorFactory.ThrowConnectionException(ret);
293             }
294             ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
295             return Profile;
296         }
297
298         static public ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
299         {
300             IntPtr ProfileHandle;
301             int ret = Interop.Connection.GetDefaultCellularServiceProfile(Holder.GetHandle(), (int)type, out ProfileHandle);
302             if ((ConnectionError)ret != ConnectionError.None)
303             {
304                 Log.Error(Globals.LogTag, "Error: " + ret);
305                 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
306                 ConnectionErrorFactory.ThrowConnectionException(ret);
307             }
308             CellularProfile Profile = new CellularProfile(ProfileHandle);
309             return Profile;
310         }
311
312         static public Task<ConnectionError> SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
313         {
314             var task = new TaskCompletionSource<ConnectionError>();
315             Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
316             {
317                 task.SetResult((ConnectionError)Result);
318                 return;
319             };
320             int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(Holder.GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
321             if ((ConnectionError)ret != ConnectionError.None)
322             {
323                 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
324                 ConnectionErrorFactory.ThrowConnectionException(ret);
325             }
326             return task.Task;
327         }
328
329
330         static public Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
331         {
332             var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
333
334             List<ConnectionProfile> Result = new List<ConnectionProfile>();
335             IntPtr iterator;
336             int ret = Interop.Connection.GetProfileIterator(Holder.GetHandle(), (int)type, out iterator);
337             if ((ConnectionError)ret != ConnectionError.None)
338             {
339                 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
340             }
341
342             while (Interop.Connection.HasNextProfileIterator(iterator))
343             {
344                 IntPtr nextH;
345                 IntPtr profileH;
346                 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
347                 Interop.ConnectionProfile.Clone(out profileH, nextH);
348
349                 int profileType;
350                 Interop.ConnectionProfile.GetType(profileH, out profileType);
351
352                 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
353                 {
354                     WiFiProfile cur = new WiFiProfile(profileH);
355                     Result.Add(cur);
356                 }
357                 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
358                 {
359                     CellularProfile cur = new CellularProfile(profileH);
360                     Result.Add(cur);
361                 }
362                 else {
363                     ConnectionProfile cur = new ConnectionProfile(profileH);
364                     Result.Add(cur);
365                 }
366             }
367             Interop.Connection.DestroyProfileIterator(iterator);
368             task.SetResult(Result);
369             return task.Task;
370         }
371
372         static public Task<ConnectionError> OpenProfileAsync(ConnectionProfile profile)
373         {
374             var task = new TaskCompletionSource<ConnectionError>();
375             Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
376             {
377                 task.SetResult((ConnectionError)Result);
378                 return;
379             };
380             int ret = Interop.Connection.OpenProfile(Holder.GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
381             if ((ConnectionError)ret != ConnectionError.None)
382             {
383                 Log.Error(Globals.LogTag, "It failed to oepn profile, " + (ConnectionError)ret);
384                 ConnectionErrorFactory.ThrowConnectionException(ret);
385             }
386             return task.Task;
387         }
388
389         static public Task<ConnectionError> CloseProfileAsync(ConnectionProfile profile)
390         {
391             var task = new TaskCompletionSource<ConnectionError>();
392             Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
393             {
394                 task.SetResult((ConnectionError)Result);
395                 return;
396             };
397             int ret = Interop.Connection.CloseProfile(Holder.GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
398             if ((ConnectionError)ret != ConnectionError.None)
399             {
400                 Log.Error(Globals.LogTag, "It failed to close profile, " + (ConnectionError)ret);
401                 ConnectionErrorFactory.ThrowConnectionException(ret);
402             }
403             return task.Task;
404         }
405     }
406 }