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;
25 using Tizen.Applications;
27 namespace Tizen.Network.Connection
31 private IntPtr Handle;
36 _tid = Thread.CurrentThread.ManagedThreadId;
37 Log.Info(Globals.LogTag, "PInvoke connection_create for Thread " + _tid);
38 int ret = Interop.Connection.Create(_tid, out Handle);
39 Log.Info(Globals.LogTag, "Handle: " + Handle);
40 if(ret != (int)ConnectionError.None)
42 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");
43 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
44 ConnectionErrorFactory.ThrowConnectionException(ret);
53 internal IntPtr GetHandle()
55 Log.Debug(Globals.LogTag, "handleholder handle = " + Handle);
59 private void Destroy()
62 Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + _tid);
63 Interop.Connection.Destroy(_tid, Handle);
64 if (Handle != IntPtr.Zero)
71 internal class ConnectionInternalManager
73 private static readonly Lazy<ConnectionInternalManager> s_instance =
74 new Lazy<ConnectionInternalManager>(() => new ConnectionInternalManager());
76 private EventHandler<ConnectionTypeEventArgs> _ConnectionTypeChanged = null;
77 private EventHandler<AddressEventArgs> _IPAddressChanged = null;
78 private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
79 private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
81 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
82 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
83 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
84 private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
86 private TizenSynchronizationContext context = new TizenSynchronizationContext();
88 internal static ConnectionInternalManager Instance
92 return s_instance.Value;
96 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
98 Log.Info(Globals.LogTag, "In threadlocal delegate");
99 return new HandleHolder();
102 private ConnectionInternalManager()
104 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
107 ~ConnectionInternalManager()
112 internal IntPtr GetHandle()
114 return s_threadName.Value.GetHandle();
117 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
123 if (_ConnectionTypeChanged == null)
127 ConnectionTypeChangedStart();
131 Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
135 _ConnectionTypeChanged += value;
142 _ConnectionTypeChanged -= value;
143 if (_ConnectionTypeChanged == null)
147 ConnectionTypeChangedStop();
151 Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
158 private void ConnectionTypeChangedStart()
160 Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
161 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
163 if (_ConnectionTypeChanged != null)
165 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
169 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
170 if ((ConnectionError)ret != ConnectionError.None)
172 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
173 ConnectionErrorFactory.ThrowConnectionException(ret);
177 private void ConnectionTypeChangedStop()
179 Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
180 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
181 if ((ConnectionError)ret != ConnectionError.None)
183 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
184 ConnectionErrorFactory.ThrowConnectionException(ret);
188 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
194 if (_EthernetCableStateChanged == null)
198 EthernetCableStateChangedStart();
202 Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
206 _EthernetCableStateChanged += value;
213 _EthernetCableStateChanged -= value;
214 if (_EthernetCableStateChanged == null)
218 EthernetCableStateChangedStop();
222 Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
229 private void EthernetCableStateChangedStart()
231 Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
232 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
234 if (_EthernetCableStateChanged != null)
236 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
239 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
240 _ethernetCableStateChangedCallback, IntPtr.Zero);
241 if ((ConnectionError)ret != ConnectionError.None)
243 Log.Error(Globals.LogTag,
244 "It failed to register ethernet cable state changed callback, " +
245 (ConnectionError)ret);
246 ConnectionErrorFactory.ThrowConnectionException(ret);
250 private void EthernetCableStateChangedStop()
252 Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
253 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
254 if ((ConnectionError)ret != ConnectionError.None)
256 Log.Error(Globals.LogTag,
257 "It failed to unregister ethernet cable state changed callback, " +
258 (ConnectionError)ret);
259 ConnectionErrorFactory.ThrowConnectionException(ret);
263 internal event EventHandler<AddressEventArgs> IPAddressChanged
269 if (_IPAddressChanged == null)
273 IPAddressChangedStart();
277 Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
281 _IPAddressChanged += value;
289 _IPAddressChanged -= value;
290 if (_IPAddressChanged == null)
294 IPAddressChangedStop();
298 Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
305 private void IPAddressChangedStart()
307 Log.Info(Globals.LogTag, "Register IPAddressChanged");
308 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
310 if (_IPAddressChanged != null)
312 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
313 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
315 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
317 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
322 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
323 if ((ConnectionError)ret != ConnectionError.None)
325 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
329 private void IPAddressChangedStop()
331 Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
332 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
333 if ((ConnectionError)ret != ConnectionError.None)
335 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
339 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
345 if (_ProxyAddressChanged == null)
349 ProxyAddressChangedStart();
353 Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
357 _ProxyAddressChanged += value;
364 _ProxyAddressChanged -= value;
365 if (_ProxyAddressChanged == null)
369 ProxyAddressChangedStop();
373 Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
380 private void ProxyAddressChangedStart()
382 Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
383 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
385 if (_ProxyAddressChanged != null)
387 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
388 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
390 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
392 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
397 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
398 if ((ConnectionError)ret != ConnectionError.None)
400 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
404 private void ProxyAddressChangedStop()
406 Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
407 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
408 if ((ConnectionError)ret != ConnectionError.None)
410 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
414 private void UnregisterEvents()
416 if (_ConnectionTypeChanged != null)
418 ConnectionTypeChangedStop();
420 if (_IPAddressChanged != null)
422 IPAddressChangedStop();
424 if (_EthernetCableStateChanged != null)
426 EthernetCableStateChangedStop();
428 if (_ProxyAddressChanged != null)
430 ProxyAddressChangedStop();
434 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
436 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
439 internal bool HasNext(IntPtr iterator)
441 return Interop.Connection.HasNextProfileIterator(iterator);
444 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
446 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
449 internal int DestoryProfileIterator(IntPtr iterator)
451 return Interop.Connection.DestroyProfileIterator(iterator);
454 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
456 Log.Info(Globals.LogTag, "GetIPAddress " + family);
458 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
459 if ((ConnectionError)ret != ConnectionError.None)
461 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
462 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");
463 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
464 ConnectionErrorFactory.ThrowConnectionException(ret);
467 string result = Marshal.PtrToStringAnsi(ip);
468 Interop.Libc.Free(ip);
469 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
470 if (result.Length == 0)
472 if (family == AddressFamily.IPv4)
473 return System.Net.IPAddress.Parse("0.0.0.0");
475 return System.Net.IPAddress.Parse("::");
477 return System.Net.IPAddress.Parse(result);
480 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
482 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
483 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
484 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
486 if (ipv6Address != IntPtr.Zero)
488 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
489 if (ipv6.Length == 0)
490 ipList.Add(System.Net.IPAddress.Parse("::"));
492 ipList.Add(System.Net.IPAddress.Parse(ipv6));
498 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
499 if (ret != (int)ConnectionError.None)
501 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
502 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");
503 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
504 ConnectionErrorFactory.ThrowConnectionException(ret);
510 internal string GetProxy(AddressFamily family)
512 Log.Debug(Globals.LogTag, "GetProxy " + family);
514 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
515 if ((ConnectionError)ret != ConnectionError.None)
517 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
518 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");
519 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
520 ConnectionErrorFactory.ThrowConnectionException(ret);
523 string result = Marshal.PtrToStringAnsi(ip);
524 Interop.Libc.Free(ip);
528 internal string GetMacAddress(ConnectionType type)
530 Log.Info(Globals.LogTag, "GetMacAddress " + type);
532 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
533 if ((ConnectionError)ret != ConnectionError.None)
535 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
536 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
537 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
538 ConnectionErrorFactory.ThrowConnectionException(ret);
541 string result = Marshal.PtrToStringAnsi(mac);
542 Interop.Libc.Free(mac);
546 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
548 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
550 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
551 (int)statisticsType, out size);
552 if ((ConnectionError)ret != ConnectionError.None)
554 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
555 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
556 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
557 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
558 ConnectionErrorFactory.ThrowConnectionException(ret);
563 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
565 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
566 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
567 (int)statisticsType);
568 if ((ConnectionError)ret != ConnectionError.None)
570 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
571 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
572 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
573 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
574 ConnectionErrorFactory.ThrowConnectionException(ret);
578 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
580 if (interfaceName != null && address != null && gateway != null)
582 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
583 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
584 if ((ConnectionError)ret != ConnectionError.None)
586 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
587 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");
588 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
589 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
590 ConnectionErrorFactory.ThrowConnectionException(ret);
596 throw new ArgumentNullException("Arguments are null");
600 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
602 if (interfaceName != null && address != null && gateway != null)
604 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
605 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
606 if ((ConnectionError)ret != ConnectionError.None)
608 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
609 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");
610 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
611 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
612 ConnectionErrorFactory.ThrowConnectionException(ret);
618 throw new ArgumentNullException("Arguments are null");
622 internal ConnectionType ConnectionType
626 Log.Info(Globals.LogTag, "get ConnectionType");
628 int ret = Interop.Connection.GetType(GetHandle(), out type);
629 if ((ConnectionError)ret != ConnectionError.None)
631 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
632 ConnectionErrorFactory.ThrowConnectionException(ret);
634 return (ConnectionType)type;
638 internal CellularState CellularState
642 Log.Info(Globals.LogTag, "get CellularState");
644 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
645 if ((ConnectionError)ret != ConnectionError.None)
647 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
648 ConnectionErrorFactory.ThrowConnectionException(ret);
650 return (CellularState)type;
654 internal ConnectionState WiFiState
658 Log.Info(Globals.LogTag, "get WiFiState");
660 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
661 if ((ConnectionError)ret != ConnectionError.None)
663 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
664 ConnectionErrorFactory.ThrowConnectionException(ret);
666 return (ConnectionState)type;
670 internal ConnectionState BluetoothState
674 Log.Info(Globals.LogTag, "get BluetoothState");
676 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
677 if ((ConnectionError)ret != ConnectionError.None)
679 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
680 ConnectionErrorFactory.ThrowConnectionException(ret);
682 return (ConnectionState)type;
686 internal ConnectionState EthernetState
690 Log.Info(Globals.LogTag, "get ConnectionType");
692 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
693 if ((ConnectionError)ret != ConnectionError.None)
695 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
696 ConnectionErrorFactory.ThrowConnectionException(ret);
698 return (ConnectionState)type;
702 internal EthernetCableState EthernetCableState
706 Log.Info(Globals.LogTag, "get EthernetCableState");
708 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
709 if ((ConnectionError)ret != ConnectionError.None)
711 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
712 ConnectionErrorFactory.ThrowConnectionException(ret);
714 return (EthernetCableState)type;
718 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
720 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
723 IntPtr handle = IntPtr.Zero;
724 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
725 if ((ConnectionError)ret != ConnectionError.None)
727 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
728 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
729 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
730 ConnectionErrorFactory.ThrowConnectionException(ret);
738 throw new ArgumentNullException("Keyword is null");
742 internal void AddCellularProfile(CellularProfile profile)
745 Log.Debug(Globals.LogTag, "AddCellularProfile");
748 if (profile.Type == ConnectionProfileType.Cellular)
750 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
751 if ((ConnectionError)ret != ConnectionError.None)
753 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
754 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
755 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
756 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
757 ConnectionErrorFactory.ThrowConnectionException(ret);
763 throw new ArgumentException("Profile type is not cellular");
769 throw new ArgumentNullException("Profile is null");
773 internal void RemoveProfile(ConnectionProfile profile)
775 Log.Debug(Globals.LogTag, "RemoveProfile");
778 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
779 if ((ConnectionError)ret != ConnectionError.None)
781 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
782 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
783 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
784 ConnectionErrorFactory.ThrowConnectionException(ret);
790 throw new ArgumentNullException("Profile is null");
794 internal void UpdateProfile(ConnectionProfile profile)
796 Log.Info(Globals.LogTag, "UpdateProfile");
799 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
800 if ((ConnectionError)ret != ConnectionError.None)
802 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
803 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
804 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
805 ConnectionErrorFactory.ThrowConnectionException(ret);
811 throw new ArgumentNullException("Profile is null");
815 internal ConnectionProfile GetCurrentProfile()
817 Log.Info(Globals.LogTag, "GetCurrentProfile");
818 IntPtr ProfileHandle;
819 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
820 if ((ConnectionError)ret != ConnectionError.None)
822 if ((ConnectionError)ret == ConnectionError.NoConnection)
824 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
829 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
830 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");
831 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
832 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
833 ConnectionErrorFactory.ThrowConnectionException(ret);
837 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
841 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
843 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
844 IntPtr ProfileHandle;
845 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
846 if ((ConnectionError)ret != ConnectionError.None)
848 Log.Error(Globals.LogTag, "Error: " + ret);
849 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
850 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
851 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
852 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
853 ConnectionErrorFactory.ThrowConnectionException(ret);
856 CellularProfile Profile = new CellularProfile(ProfileHandle);
860 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
862 Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
865 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
866 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
868 Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
869 if (Result != ConnectionError.None)
871 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
872 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result));
876 task.SetResult(true);
882 Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
883 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
884 if ((ConnectionError)ret != ConnectionError.None)
886 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
887 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
888 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
889 ConnectionErrorFactory.ThrowConnectionException(ret);
897 throw new ArgumentNullException("Profile is null");
902 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
904 Log.Debug(Globals.LogTag, "GetProfileListAsync");
905 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
907 List<ConnectionProfile> Result = new List<ConnectionProfile>();
909 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
910 if ((ConnectionError)ret != ConnectionError.None)
912 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
913 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");
914 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
915 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
916 ConnectionErrorFactory.ThrowConnectionException(ret);
919 while (Interop.Connection.HasNextProfileIterator(iterator))
923 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
924 Interop.ConnectionProfile.Clone(out profileH, nextH);
927 Interop.ConnectionProfile.GetType(profileH, out profileType);
929 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
931 WiFiProfile cur = new WiFiProfile(profileH);
934 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
936 CellularProfile cur = new CellularProfile(profileH);
940 ConnectionProfile cur = new ConnectionProfile(profileH);
944 Interop.Connection.DestroyProfileIterator(iterator);
945 task.SetResult(Result);
949 internal Task OpenProfileAsync(ConnectionProfile profile)
951 Log.Info(Globals.LogTag, "OpenProfileAsync");
954 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
955 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
956 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
958 Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
959 if (Result != ConnectionError.None)
961 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
962 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result));
966 task.SetResult(true);
972 Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
973 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
974 if ((ConnectionError)ret != ConnectionError.None)
976 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
977 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
978 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
979 ConnectionErrorFactory.ThrowConnectionException(ret);
988 throw new ArgumentNullException("Profile is null");
992 internal Task CloseProfileAsync(ConnectionProfile profile)
994 Log.Info(Globals.LogTag, "CloseProfileAsync");
997 Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
998 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
999 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
1001 Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
1002 if (Result != ConnectionError.None)
1004 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
1005 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result));
1009 task.SetResult(true);
1015 Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
1016 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
1017 if ((ConnectionError)ret != ConnectionError.None)
1019 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
1020 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1021 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
1022 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1023 ConnectionErrorFactory.ThrowConnectionException(ret);
1032 throw new ArgumentNullException("Profile is null");