2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
21 using System.Threading.Tasks;
22 using System.Runtime.InteropServices;
23 using System.Collections;
25 namespace Tizen.Network.Connection
27 class HandleHolder : IDisposable
29 private IntPtr Handle;
30 private bool disposed = false;
34 Log.Debug(Globals.LogTag, "HandleHolder() ^^");
35 Interop.Connection.Create(out Handle);
36 Log.Debug(Globals.LogTag, "Handle: " + Handle);
44 internal IntPtr GetHandle()
51 GC.SuppressFinalize(this);
54 private void Dispose(bool disposing)
61 // Free managed objects.
63 Interop.Connection.Destroy(Handle);
68 static class ConnectionInternalManager
70 private static HandleHolder Holder = new HandleHolder();
72 static public IntPtr GetHandle()
74 return Holder.GetHandle();
77 static internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
79 return Interop.Connection.GetProfileIterator(Holder.GetHandle(), (int)type, out iterator);
82 static internal bool HasNext(IntPtr iterator)
84 return Interop.Connection.HasNextProfileIterator(iterator);
87 static internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
89 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
92 static internal int DestoryProfileIterator(IntPtr iterator)
94 return Interop.Connection.DestroyProfileIterator(iterator);
97 static public string GetIpAddress(AddressFamily family)
100 int ret = Interop.Connection.GetIpAddress(Holder.GetHandle(), (int)family, out ip);
101 if ((ConnectionError)ret != ConnectionError.None)
103 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
104 ConnectionErrorFactory.ThrowConnectionException(ret);
106 string result = Marshal.PtrToStringAnsi(ip);
107 Interop.Libc.Free(ip);
111 static public string GetProxy(AddressFamily family)
114 int ret = Interop.Connection.GetProxy(Holder.GetHandle(), (int)family, out ip);
115 if ((ConnectionError)ret != ConnectionError.None)
117 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
118 ConnectionErrorFactory.ThrowConnectionException(ret);
120 string result = Marshal.PtrToStringAnsi(ip);
121 Interop.Libc.Free(ip);
125 static public string GetMacAddress(ConnectionType type)
128 int ret = Interop.Connection.GetMacAddress(Holder.GetHandle(), (int)type, out ip);
129 if ((ConnectionError)ret != ConnectionError.None)
131 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
132 ConnectionErrorFactory.ThrowConnectionException(ret);
134 string result = Marshal.PtrToStringAnsi(ip);
135 Interop.Libc.Free(ip);
139 static public ConnectionType ConnectionType
144 Log.Debug(Globals.LogTag, "Handle: " + Holder.GetHandle());
145 int ret = Interop.Connection.GetType(Holder.GetHandle(), out type);
146 if ((ConnectionError)ret != ConnectionError.None)
148 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
150 return (ConnectionType)type;
154 static public CellularState CellularState
159 int ret = Interop.Connection.GetCellularState(Holder.GetHandle(), out type);
160 if ((ConnectionError)ret != ConnectionError.None)
162 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
164 return (CellularState)type;
168 static public ConnectionState WiFiState
173 int ret = Interop.Connection.GetWiFiState(Holder.GetHandle(), out type);
174 if ((ConnectionError)ret != ConnectionError.None)
176 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
178 return (ConnectionState)type;
182 static public ConnectionState BluetoothState
187 int ret = Interop.Connection.GetBtState(Holder.GetHandle(), out type);
188 if ((ConnectionError)ret != ConnectionError.None)
190 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
192 return (ConnectionState)type;
196 static public ConnectionState EthernetState
201 int ret = Interop.Connection.GetEthernetState(Holder.GetHandle(), out type);
202 if ((ConnectionError)ret != ConnectionError.None)
204 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
206 return (ConnectionState)type;
210 static public EthernetCableState EthernetCableState
215 int ret = Interop.Connection.GetEthernetCableState(Holder.GetHandle(), out type);
216 if ((ConnectionError)ret != ConnectionError.None)
218 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
220 return (EthernetCableState)type;
224 static public IntPtr CreateRequestProfile(ConnectionProfileType type, string keyword)
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)
232 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
233 ConnectionErrorFactory.ThrowConnectionException(ret);
239 static public int AddProfile(RequestProfile profile)
242 if (profile.Type == ConnectionProfileType.Cellular)
244 ret = Interop.Connection.AddProfile(Holder.GetHandle(), ((RequestCellularProfile)profile).ProfileHandle);
245 if ((ConnectionError)ret != ConnectionError.None)
247 Log.Error(Globals.LogTag, "It failed to add profile, " + (ConnectionError)ret);
248 ConnectionErrorFactory.ThrowConnectionException(ret);
251 else if (profile.Type == ConnectionProfileType.WiFi)
253 ret = Interop.Connection.AddProfile(Holder.GetHandle(), ((RequestWiFiProfile)profile).ProfileHandle);
254 if ((ConnectionError)ret != ConnectionError.None)
256 Log.Error(Globals.LogTag, "It failed to add profile, " + (ConnectionError)ret);
257 ConnectionErrorFactory.ThrowConnectionException(ret);
263 static public int RemoveProfile(ConnectionProfile profile)
265 int ret = Interop.Connection.RemoveProfile(Holder.GetHandle(), profile.ProfileHandle);
266 if ((ConnectionError)ret != ConnectionError.None)
268 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
269 ConnectionErrorFactory.ThrowConnectionException(ret);
274 static public int UpdateProfile(ConnectionProfile profile)
276 int ret = Interop.Connection.UpdateProfile(Holder.GetHandle(), profile.ProfileHandle);
277 if ((ConnectionError)ret != ConnectionError.None)
279 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
280 ConnectionErrorFactory.ThrowConnectionException(ret);
285 static public ConnectionProfile GetCurrentProfile()
287 IntPtr ProfileHandle;
288 int ret = Interop.Connection.GetCurrentProfile(Holder.GetHandle(), out ProfileHandle);
289 if ((ConnectionError)ret != ConnectionError.None)
291 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
292 ConnectionErrorFactory.ThrowConnectionException(ret);
294 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
298 static public ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
300 IntPtr ProfileHandle;
301 int ret = Interop.Connection.GetDefaultCellularServiceProfile(Holder.GetHandle(), (int)type, out ProfileHandle);
302 if ((ConnectionError)ret != ConnectionError.None)
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);
308 CellularProfile Profile = new CellularProfile(ProfileHandle);
312 static public Task<ConnectionError> SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
314 var task = new TaskCompletionSource<ConnectionError>();
315 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
317 task.SetResult((ConnectionError)Result);
320 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(Holder.GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
321 if ((ConnectionError)ret != ConnectionError.None)
323 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
324 ConnectionErrorFactory.ThrowConnectionException(ret);
330 static public Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
332 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
334 List<ConnectionProfile> Result = new List<ConnectionProfile>();
336 int ret = Interop.Connection.GetProfileIterator(Holder.GetHandle(), (int)type, out iterator);
337 if ((ConnectionError)ret != ConnectionError.None)
339 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
342 while (Interop.Connection.HasNextProfileIterator(iterator))
346 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
347 Interop.ConnectionProfile.Clone(out profileH, nextH);
350 Interop.ConnectionProfile.GetType(profileH, out profileType);
352 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
354 WiFiProfile cur = new WiFiProfile(profileH);
357 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
359 CellularProfile cur = new CellularProfile(profileH);
363 ConnectionProfile cur = new ConnectionProfile(profileH);
367 Interop.Connection.DestroyProfileIterator(iterator);
368 task.SetResult(Result);
372 static public Task<ConnectionError> OpenProfileAsync(ConnectionProfile profile)
374 var task = new TaskCompletionSource<ConnectionError>();
375 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
377 task.SetResult((ConnectionError)Result);
380 int ret = Interop.Connection.OpenProfile(Holder.GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
381 if ((ConnectionError)ret != ConnectionError.None)
383 Log.Error(Globals.LogTag, "It failed to oepn profile, " + (ConnectionError)ret);
384 ConnectionErrorFactory.ThrowConnectionException(ret);
389 static public Task<ConnectionError> CloseProfileAsync(ConnectionProfile profile)
391 var task = new TaskCompletionSource<ConnectionError>();
392 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
394 task.SetResult((ConnectionError)Result);
397 int ret = Interop.Connection.CloseProfile(Holder.GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
398 if ((ConnectionError)ret != ConnectionError.None)
400 Log.Error(Globals.LogTag, "It failed to close profile, " + (ConnectionError)ret);
401 ConnectionErrorFactory.ThrowConnectionException(ret);