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;
24 using System.Threading;
26 namespace Tizen.Network.Connection
28 class HandleHolder : IDisposable
30 private IntPtr Handle;
31 private bool disposed = false;
35 Log.Debug(Globals.LogTag, "Handle: " + Handle);
36 int ret = Interop.Connection.Create(out Handle);
37 if(ret != (int)ConnectionError.None)
39 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");
40 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
41 ConnectionErrorFactory.ThrowConnectionException(ret);
50 internal IntPtr GetHandle()
52 Log.Debug(Globals.LogTag, "handleholder handle = " + Handle);
58 GC.SuppressFinalize(this);
61 private void Dispose(bool disposing)
63 Log.Debug(Globals.LogTag, ">>> HandleHolder Dispose with " + disposing);
64 Log.Debug(Globals.LogTag, ">>> Handle: " + Handle);
70 // Free managed objects.
76 private void Destroy()
78 Interop.Connection.Destroy(Handle);
79 if (Handle != IntPtr.Zero)
86 internal class ConnectionInternalManager
88 private bool disposed = false;
89 private static ConnectionInternalManager s_instance = null;
91 private EventHandler<ConnectionTypeEventArgs> _ConnectionTypeChanged = null;
92 private EventHandler<AddressEventArgs> _IPAddressChanged = null;
93 private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
94 private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
96 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
97 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
98 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
100 internal static ConnectionInternalManager Instance
104 if (s_instance == null)
106 s_instance = new ConnectionInternalManager();
113 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
115 Log.Info(Globals.LogTag, "In threadlocal delegate");
116 return new HandleHolder();
119 private ConnectionInternalManager()
124 ~ConnectionInternalManager()
129 public void Dispose()
132 GC.SuppressFinalize(this);
135 private void Dispose(bool disposing)
137 Log.Debug(Globals.LogTag, ">>> ConnectionInternalManager Dispose with disposing " + disposing + ", disposed " + disposed);
138 Log.Debug(Globals.LogTag, ">>> Handle: " + GetHandle());
144 // Free managed objects.
151 internal IntPtr GetHandle()
153 return s_threadName.Value.GetHandle();
156 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
160 if (_ConnectionTypeChanged == null)
162 ConnectionTypeChangedStart();
165 _ConnectionTypeChanged += value;
169 _ConnectionTypeChanged -= value;
170 if (_ConnectionTypeChanged == null)
172 ConnectionTypeChangedStop();
177 private void ConnectionTypeChangedStart()
179 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
181 if (_ConnectionTypeChanged != null)
183 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
187 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
188 if ((ConnectionError)ret != ConnectionError.None)
190 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
191 ConnectionErrorFactory.ThrowConnectionException(ret);
195 private void ConnectionTypeChangedStop()
197 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
198 if ((ConnectionError)ret != ConnectionError.None)
200 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
201 ConnectionErrorFactory.ThrowConnectionException(ret);
205 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
209 if (_EthernetCableStateChanged == null)
211 EthernetCableStateChangedStart();
213 _EthernetCableStateChanged += value;
217 _EthernetCableStateChanged -= value;
218 if (_EthernetCableStateChanged == null)
220 EthernetCableStateChangedStop();
225 private void EthernetCableStateChangedStart()
227 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(), EthernetCableStateChangedCallback, IntPtr.Zero);
228 if ((ConnectionError)ret != ConnectionError.None)
230 Log.Error(Globals.LogTag, "It failed to register ethernet cable state changed callback, " + (ConnectionError)ret);
231 ConnectionErrorFactory.ThrowConnectionException(ret);
235 private void EthernetCableStateChangedStop()
237 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
238 if ((ConnectionError)ret != ConnectionError.None)
240 Log.Error(Globals.LogTag, "It failed to unregister ethernet cable state changed callback, " + (ConnectionError)ret);
241 ConnectionErrorFactory.ThrowConnectionException(ret);
245 private void EthernetCableStateChangedCallback(EthernetCableState state, IntPtr user_data)
247 if (_EthernetCableStateChanged != null)
249 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
253 internal event EventHandler<AddressEventArgs> IPAddressChanged
257 if (_IPAddressChanged == null)
259 IPAddressChangedStart();
261 _IPAddressChanged += value;
266 _IPAddressChanged -= value;
267 if (_IPAddressChanged == null)
269 IPAddressChangedStop();
274 private void IPAddressChangedStart()
276 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
278 if (_IPAddressChanged != null)
280 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
281 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
283 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
285 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
290 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
291 if ((ConnectionError)ret != ConnectionError.None)
293 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
297 private void IPAddressChangedStop()
299 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
300 if ((ConnectionError)ret != ConnectionError.None)
302 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
306 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
310 //Console.WriteLine("ProxyAddressChanged Add **");
311 if (_ProxyAddressChanged == null)
313 ProxyAddressChangedStart();
316 _ProxyAddressChanged += value;
320 //Console.WriteLine("ProxyAddressChanged Remove");
321 _ProxyAddressChanged -= value;
322 if (_ProxyAddressChanged == null)
324 ProxyAddressChangedStop();
329 private void ProxyAddressChangedStart()
331 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
333 if (_ProxyAddressChanged != null)
335 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
336 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
338 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
340 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
345 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
346 if ((ConnectionError)ret != ConnectionError.None)
348 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
352 private void ProxyAddressChangedStop()
354 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
355 if ((ConnectionError)ret != ConnectionError.None)
357 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
361 private void UnregisterEvents()
363 if (_ConnectionTypeChanged != null)
365 ConnectionTypeChangedStop();
367 if (_IPAddressChanged != null)
369 IPAddressChangedStop();
371 if (_EthernetCableStateChanged != null)
373 EthernetCableStateChangedStop();
375 if (_ProxyAddressChanged != null)
377 ProxyAddressChangedStop();
381 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
383 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
386 internal bool HasNext(IntPtr iterator)
388 return Interop.Connection.HasNextProfileIterator(iterator);
391 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
393 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
396 internal int DestoryProfileIterator(IntPtr iterator)
398 return Interop.Connection.DestroyProfileIterator(iterator);
401 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
403 Log.Debug(Globals.LogTag, "GetIPAddress " + family);
405 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
406 if ((ConnectionError)ret != ConnectionError.None)
408 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
409 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");
410 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
411 ConnectionErrorFactory.ThrowConnectionException(ret);
414 string result = Marshal.PtrToStringAnsi(ip);
415 Interop.Libc.Free(ip);
416 Log.Debug(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
417 if (result.Length == 0)
419 if (family == AddressFamily.IPv4)
420 return System.Net.IPAddress.Parse("0.0.0.0");
422 return System.Net.IPAddress.Parse("::");
424 return System.Net.IPAddress.Parse(result);
427 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
429 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
430 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
431 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
433 if (ipv6Address != IntPtr.Zero)
435 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
436 if (ipv6.Length == 0)
437 ipList.Add(System.Net.IPAddress.Parse("::"));
439 ipList.Add(System.Net.IPAddress.Parse(ipv6));
445 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
446 if (ret != (int)ConnectionError.None)
448 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
449 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");
450 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
451 ConnectionErrorFactory.ThrowConnectionException(ret);
457 internal string GetProxy(AddressFamily family)
459 Log.Debug(Globals.LogTag, "GetProxy " + family);
461 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
462 if ((ConnectionError)ret != ConnectionError.None)
464 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
465 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");
466 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
467 ConnectionErrorFactory.ThrowConnectionException(ret);
470 string result = Marshal.PtrToStringAnsi(ip);
471 Interop.Libc.Free(ip);
475 internal string GetMacAddress(ConnectionType type)
477 Log.Debug(Globals.LogTag, "GetMacAddress " + type);
479 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
480 if ((ConnectionError)ret != ConnectionError.None)
482 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
483 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
484 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
485 ConnectionErrorFactory.ThrowConnectionException(ret);
488 string result = Marshal.PtrToStringAnsi(mac);
489 Interop.Libc.Free(mac);
493 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
495 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
497 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
498 (int)statisticsType, out size);
499 if ((ConnectionError)ret != ConnectionError.None)
501 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
502 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
503 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
504 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
505 ConnectionErrorFactory.ThrowConnectionException(ret);
510 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
512 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
513 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
514 (int)statisticsType);
515 if ((ConnectionError)ret != ConnectionError.None)
517 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
518 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
519 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
520 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
521 ConnectionErrorFactory.ThrowConnectionException(ret);
525 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
527 if (interfaceName != null && address != null && gateway != null)
529 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
530 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
531 if ((ConnectionError)ret != ConnectionError.None)
533 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
534 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");
535 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
536 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
537 ConnectionErrorFactory.ThrowConnectionException(ret);
543 throw new ArgumentNullException("Arguments are null");
547 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
549 if (interfaceName != null && address != null && gateway != null)
551 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
552 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
553 if ((ConnectionError)ret != ConnectionError.None)
555 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
556 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");
557 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
558 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
559 ConnectionErrorFactory.ThrowConnectionException(ret);
565 throw new ArgumentNullException("Arguments are null");
569 internal ConnectionType ConnectionType
573 Log.Debug(Globals.LogTag, "get ConnectionType");
575 int ret = Interop.Connection.GetType(GetHandle(), out type);
576 if ((ConnectionError)ret != ConnectionError.None)
578 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
579 ConnectionErrorFactory.ThrowConnectionException(ret);
581 return (ConnectionType)type;
585 internal CellularState CellularState
589 Log.Debug(Globals.LogTag, "get CellularState");
591 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
592 if ((ConnectionError)ret != ConnectionError.None)
594 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
595 ConnectionErrorFactory.ThrowConnectionException(ret);
597 return (CellularState)type;
601 internal ConnectionState WiFiState
605 Log.Debug(Globals.LogTag, "get WiFiState");
607 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
608 if ((ConnectionError)ret != ConnectionError.None)
610 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
611 ConnectionErrorFactory.ThrowConnectionException(ret);
613 return (ConnectionState)type;
617 internal ConnectionState BluetoothState
621 Log.Debug(Globals.LogTag, "get BluetoothState");
623 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
624 if ((ConnectionError)ret != ConnectionError.None)
626 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
627 ConnectionErrorFactory.ThrowConnectionException(ret);
629 return (ConnectionState)type;
633 internal ConnectionState EthernetState
637 Log.Debug(Globals.LogTag, "get ConnectionType");
639 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
640 if ((ConnectionError)ret != ConnectionError.None)
642 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
643 ConnectionErrorFactory.ThrowConnectionException(ret);
645 return (ConnectionState)type;
649 internal EthernetCableState EthernetCableState
653 Log.Debug(Globals.LogTag, "get EthernetCableState");
655 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
656 if ((ConnectionError)ret != ConnectionError.None)
658 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
659 ConnectionErrorFactory.ThrowConnectionException(ret);
661 return (EthernetCableState)type;
665 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
667 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
670 IntPtr handle = IntPtr.Zero;
671 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
672 if ((ConnectionError)ret != ConnectionError.None)
674 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
675 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
676 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
677 ConnectionErrorFactory.ThrowConnectionException(ret);
685 throw new ArgumentNullException("Keyword is null");
689 internal void AddCellularProfile(CellularProfile profile)
692 Log.Debug(Globals.LogTag, "AddCellularProfile");
695 if (profile.Type == ConnectionProfileType.Cellular)
697 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
698 if ((ConnectionError)ret != ConnectionError.None)
700 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
701 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
702 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
703 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
704 ConnectionErrorFactory.ThrowConnectionException(ret);
710 throw new ArgumentException("Profile type is not cellular");
716 throw new ArgumentNullException("Profile is null");
720 internal void RemoveProfile(ConnectionProfile profile)
722 Log.Debug(Globals.LogTag, "RemoveProfile");
725 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
726 if ((ConnectionError)ret != ConnectionError.None)
728 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
729 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
730 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
731 ConnectionErrorFactory.ThrowConnectionException(ret);
737 throw new ArgumentNullException("Profile is null");
741 internal void UpdateProfile(ConnectionProfile profile)
743 Log.Debug(Globals.LogTag, "UpdateProfile");
746 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
747 if ((ConnectionError)ret != ConnectionError.None)
749 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
750 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
751 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
752 ConnectionErrorFactory.ThrowConnectionException(ret);
758 throw new ArgumentNullException("Profile is null");
762 internal ConnectionProfile GetCurrentProfile()
764 Log.Debug(Globals.LogTag, "GetCurrentProfile");
765 IntPtr ProfileHandle;
766 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
767 if ((ConnectionError)ret != ConnectionError.None)
769 if ((ConnectionError)ret == ConnectionError.NoConnection)
771 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
776 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
777 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");
778 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
779 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
780 ConnectionErrorFactory.ThrowConnectionException(ret);
784 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
788 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
790 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
791 IntPtr ProfileHandle;
792 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
793 if ((ConnectionError)ret != ConnectionError.None)
795 Log.Error(Globals.LogTag, "Error: " + ret);
796 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
797 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
798 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
799 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
800 ConnectionErrorFactory.ThrowConnectionException(ret);
803 CellularProfile Profile = new CellularProfile(ProfileHandle);
807 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
809 Log.Debug(Globals.LogTag, "SetDefaultCellularProfile");
812 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
813 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
815 if (Result != ConnectionError.None)
817 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
818 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result));
822 task.SetResult(true);
826 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
827 if ((ConnectionError)ret != ConnectionError.None)
829 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
830 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
831 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
832 ConnectionErrorFactory.ThrowConnectionException(ret);
840 throw new ArgumentNullException("Profile is null");
845 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
847 Log.Debug(Globals.LogTag, "GetProfileListAsync");
848 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
850 List<ConnectionProfile> Result = new List<ConnectionProfile>();
852 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
853 if ((ConnectionError)ret != ConnectionError.None)
855 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
856 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");
857 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
858 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
859 ConnectionErrorFactory.ThrowConnectionException(ret);
862 while (Interop.Connection.HasNextProfileIterator(iterator))
866 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
867 Interop.ConnectionProfile.Clone(out profileH, nextH);
870 Interop.ConnectionProfile.GetType(profileH, out profileType);
872 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
874 WiFiProfile cur = new WiFiProfile(profileH);
877 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
879 CellularProfile cur = new CellularProfile(profileH);
883 ConnectionProfile cur = new ConnectionProfile(profileH);
887 Interop.Connection.DestroyProfileIterator(iterator);
888 task.SetResult(Result);
892 internal Task OpenProfileAsync(ConnectionProfile profile)
894 Log.Debug(Globals.LogTag, "OpenProfileAsync");
897 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
898 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
899 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
901 Log.Debug(Globals.LogTag, "Connected " + profile.Name);
902 if (Result != ConnectionError.None)
904 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
905 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result));
909 task.SetResult(true);
913 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
914 if ((ConnectionError)ret != ConnectionError.None)
916 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
917 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
918 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
919 ConnectionErrorFactory.ThrowConnectionException(ret);
927 throw new ArgumentNullException("Profile is null");
931 internal Task CloseProfileAsync(ConnectionProfile profile)
933 Log.Debug(Globals.LogTag, "CloseProfileAsync");
936 Log.Debug(Globals.LogTag, "CloseProfileAsync " + profile.Name);
937 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
938 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
940 if (Result != ConnectionError.None)
942 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
943 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result));
947 task.SetResult(true);
951 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
952 if ((ConnectionError)ret != ConnectionError.None)
954 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
955 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
956 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
957 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
958 ConnectionErrorFactory.ThrowConnectionException(ret);
966 throw new ArgumentNullException("Profile is null");