2 * Copyright (c) 2018 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(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(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 Dictionary<IntPtr, Interop.Connection.ConnectionCallback> _callback_map =
87 new Dictionary<IntPtr, Interop.Connection.ConnectionCallback>();
88 private int _requestId = 0;
90 internal static ConnectionInternalManager Instance
94 Log.Info(Globals.LogTag, "ConnectionInternalManager.Instance");
95 return s_instance.Value;
99 private HandleHolder _handleHolder;
101 private ConnectionInternalManager()
103 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
104 _handleHolder = new HandleHolder();
105 Log.Info(Globals.LogTag, "Success to get handle");
108 ~ConnectionInternalManager()
113 internal IntPtr GetHandle()
115 return _handleHolder.GetHandle();
118 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
122 if (_ConnectionTypeChanged == null)
126 ConnectionTypeChangedStart();
130 Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
134 _ConnectionTypeChanged += value;
138 _ConnectionTypeChanged -= value;
139 if (_ConnectionTypeChanged == null)
143 ConnectionTypeChangedStop();
147 Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
153 private void ConnectionTypeChangedStart()
155 Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
156 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
158 if (_ConnectionTypeChanged != null)
160 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
164 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
165 if ((ConnectionError)ret != ConnectionError.None)
167 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
168 ConnectionErrorFactory.ThrowConnectionException(ret);
172 private void ConnectionTypeChangedStop()
174 Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
175 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
176 if ((ConnectionError)ret != ConnectionError.None)
178 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
179 ConnectionErrorFactory.ThrowConnectionException(ret);
183 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
187 if (_EthernetCableStateChanged == null)
191 EthernetCableStateChangedStart();
195 Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
199 _EthernetCableStateChanged += value;
203 _EthernetCableStateChanged -= value;
204 if (_EthernetCableStateChanged == null)
208 EthernetCableStateChangedStop();
212 Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
218 private void EthernetCableStateChangedStart()
220 Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
221 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
223 if (_EthernetCableStateChanged != null)
225 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
228 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
229 _ethernetCableStateChangedCallback, IntPtr.Zero);
230 if ((ConnectionError)ret != ConnectionError.None)
232 Log.Error(Globals.LogTag,
233 "It failed to register ethernet cable state changed callback, " +
234 (ConnectionError)ret);
235 ConnectionErrorFactory.ThrowConnectionException(ret);
239 private void EthernetCableStateChangedStop()
241 Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
242 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
243 if ((ConnectionError)ret != ConnectionError.None)
245 Log.Error(Globals.LogTag,
246 "It failed to unregister ethernet cable state changed callback, " +
247 (ConnectionError)ret);
248 ConnectionErrorFactory.ThrowConnectionException(ret);
252 internal event EventHandler<AddressEventArgs> IPAddressChanged
256 if (_IPAddressChanged == null)
260 IPAddressChangedStart();
264 Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
268 _IPAddressChanged += value;
273 _IPAddressChanged -= value;
274 if (_IPAddressChanged == null)
278 IPAddressChangedStop();
282 Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
288 private void IPAddressChangedStart()
290 Log.Info(Globals.LogTag, "Register IPAddressChanged");
291 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
293 if (_IPAddressChanged != null)
295 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
296 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
298 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
300 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
305 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
306 if ((ConnectionError)ret != ConnectionError.None)
308 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
312 private void IPAddressChangedStop()
314 Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
315 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
316 if ((ConnectionError)ret != ConnectionError.None)
318 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
322 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
326 if (_ProxyAddressChanged == null)
330 ProxyAddressChangedStart();
334 Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
338 _ProxyAddressChanged += value;
342 _ProxyAddressChanged -= value;
343 if (_ProxyAddressChanged == null)
347 ProxyAddressChangedStop();
351 Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
357 private void ProxyAddressChangedStart()
359 Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
360 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
362 if (_ProxyAddressChanged != null)
364 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
365 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
367 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
369 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
374 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
375 if ((ConnectionError)ret != ConnectionError.None)
377 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
381 private void ProxyAddressChangedStop()
383 Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
384 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
385 if ((ConnectionError)ret != ConnectionError.None)
387 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
391 private void UnregisterEvents()
393 if (_ConnectionTypeChanged != null)
395 ConnectionTypeChangedStop();
397 if (_IPAddressChanged != null)
399 IPAddressChangedStop();
401 if (_EthernetCableStateChanged != null)
403 EthernetCableStateChangedStop();
405 if (_ProxyAddressChanged != null)
407 ProxyAddressChangedStop();
411 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
413 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
416 internal bool HasNext(IntPtr iterator)
418 return Interop.Connection.HasNextProfileIterator(iterator);
421 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
423 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
426 internal int DestoryProfileIterator(IntPtr iterator)
428 return Interop.Connection.DestroyProfileIterator(iterator);
431 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
433 Log.Info(Globals.LogTag, "GetIPAddress " + family);
435 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
436 if ((ConnectionError)ret != ConnectionError.None)
438 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
439 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");
440 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
441 ConnectionErrorFactory.ThrowConnectionException(ret);
444 string result = Marshal.PtrToStringAnsi(ip);
445 Interop.Libc.Free(ip);
446 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
447 if (result.Length == 0)
449 if (family == AddressFamily.IPv4)
450 return System.Net.IPAddress.Parse("0.0.0.0");
452 return System.Net.IPAddress.Parse("::");
454 return System.Net.IPAddress.Parse(result);
457 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
459 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
460 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
461 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
463 if (ipv6Address != IntPtr.Zero)
465 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
466 if (ipv6.Length == 0)
467 ipList.Add(System.Net.IPAddress.Parse("::"));
469 ipList.Add(System.Net.IPAddress.Parse(ipv6));
475 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
476 if (ret != (int)ConnectionError.None)
478 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
479 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");
480 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
481 ConnectionErrorFactory.ThrowConnectionException(ret);
487 internal string GetProxy(AddressFamily family)
489 Log.Debug(Globals.LogTag, "GetProxy " + family);
491 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
492 if ((ConnectionError)ret != ConnectionError.None)
494 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
495 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");
496 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
497 ConnectionErrorFactory.ThrowConnectionException(ret);
500 string result = Marshal.PtrToStringAnsi(ip);
501 Interop.Libc.Free(ip);
505 internal string GetMacAddress(ConnectionType type)
507 Log.Info(Globals.LogTag, "GetMacAddress " + type);
509 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
510 if ((ConnectionError)ret != ConnectionError.None)
512 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
513 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
514 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
515 ConnectionErrorFactory.ThrowConnectionException(ret);
518 string result = Marshal.PtrToStringAnsi(mac);
519 Interop.Libc.Free(mac);
523 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
525 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
527 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
528 (int)statisticsType, out size);
529 if ((ConnectionError)ret != ConnectionError.None)
531 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
532 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
533 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
534 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
535 ConnectionErrorFactory.ThrowConnectionException(ret);
540 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
542 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
543 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
544 (int)statisticsType);
545 if ((ConnectionError)ret != ConnectionError.None)
547 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
548 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
549 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
550 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
551 ConnectionErrorFactory.ThrowConnectionException(ret);
555 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
557 if (interfaceName != null && address != null && gateway != null)
559 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
560 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
561 if ((ConnectionError)ret != ConnectionError.None)
563 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
564 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");
565 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.route)");
566 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
567 ConnectionErrorFactory.ThrowConnectionException(ret);
573 throw new ArgumentNullException("Arguments are null");
577 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
579 if (interfaceName != null && address != null && gateway != null)
581 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
582 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
583 if ((ConnectionError)ret != ConnectionError.None)
585 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
586 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");
587 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.route)");
588 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
589 ConnectionErrorFactory.ThrowConnectionException(ret);
595 throw new ArgumentNullException("Arguments are null");
599 internal ConnectionType ConnectionType
603 Log.Info(Globals.LogTag, "get ConnectionType");
605 int ret = Interop.Connection.GetType(GetHandle(), out type);
606 if ((ConnectionError)ret != ConnectionError.None)
608 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
609 ConnectionErrorFactory.ThrowConnectionException(ret);
611 return (ConnectionType)type;
615 internal CellularState CellularState
619 Log.Info(Globals.LogTag, "get CellularState");
621 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
622 if ((ConnectionError)ret != ConnectionError.None)
624 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
625 ConnectionErrorFactory.ThrowConnectionException(ret);
627 return (CellularState)type;
631 internal ConnectionState WiFiState
635 Log.Info(Globals.LogTag, "get WiFiState");
637 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
638 if ((ConnectionError)ret != ConnectionError.None)
640 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
641 ConnectionErrorFactory.ThrowConnectionException(ret);
643 return (ConnectionState)type;
647 internal ConnectionState BluetoothState
651 Log.Info(Globals.LogTag, "get BluetoothState");
653 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
654 if ((ConnectionError)ret != ConnectionError.None)
656 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
657 ConnectionErrorFactory.ThrowConnectionException(ret);
659 return (ConnectionState)type;
663 internal ConnectionState EthernetState
667 Log.Info(Globals.LogTag, "get ConnectionType");
669 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
670 if ((ConnectionError)ret != ConnectionError.None)
672 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
673 ConnectionErrorFactory.ThrowConnectionException(ret);
675 return (ConnectionState)type;
679 internal EthernetCableState EthernetCableState
683 Log.Info(Globals.LogTag, "get EthernetCableState");
685 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
686 if ((ConnectionError)ret != ConnectionError.None)
688 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
689 ConnectionErrorFactory.ThrowConnectionException(ret);
691 return (EthernetCableState)type;
695 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
697 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
700 IntPtr handle = IntPtr.Zero;
701 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
702 if ((ConnectionError)ret != ConnectionError.None)
704 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
705 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
706 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
707 ConnectionErrorFactory.ThrowConnectionException(ret);
715 throw new ArgumentNullException("Keyword is null");
719 internal void AddCellularProfile(CellularProfile profile)
722 Log.Debug(Globals.LogTag, "AddCellularProfile");
725 if (profile.Type == ConnectionProfileType.Cellular)
727 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
728 if ((ConnectionError)ret != ConnectionError.None)
730 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
731 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
732 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
733 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
734 ConnectionErrorFactory.ThrowConnectionException(ret);
740 throw new ArgumentException("Profile type is not cellular");
746 throw new ArgumentNullException("Profile is null");
750 internal void RemoveProfile(ConnectionProfile profile)
752 Log.Debug(Globals.LogTag, "RemoveProfile");
755 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
756 if ((ConnectionError)ret != ConnectionError.None)
758 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
759 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
760 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
761 ConnectionErrorFactory.ThrowConnectionException(ret);
767 throw new ArgumentNullException("Profile is null");
771 internal void UpdateProfile(ConnectionProfile profile)
773 Log.Info(Globals.LogTag, "UpdateProfile");
776 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
777 if ((ConnectionError)ret != ConnectionError.None)
779 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
780 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
781 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
782 ConnectionErrorFactory.ThrowConnectionException(ret);
788 throw new ArgumentNullException("Profile is null");
792 internal ConnectionProfile GetCurrentProfile()
794 Log.Info(Globals.LogTag, "GetCurrentProfile");
795 IntPtr ProfileHandle;
796 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
797 if ((ConnectionError)ret != ConnectionError.None)
799 if ((ConnectionError)ret == ConnectionError.NoConnection)
801 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
804 else if ((ConnectionError)ret == ConnectionError.InvalidParameter)
806 throw new InvalidOperationException("Invalid handle");
810 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
811 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");
812 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
813 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
814 ConnectionErrorFactory.ThrowConnectionException(ret);
818 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
822 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
824 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
825 IntPtr ProfileHandle;
826 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
827 if ((ConnectionError)ret != ConnectionError.None)
829 Log.Error(Globals.LogTag, "Error: " + ret);
830 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
831 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
832 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
833 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
834 ConnectionErrorFactory.ThrowConnectionException(ret);
837 CellularProfile Profile = new CellularProfile(ProfileHandle);
841 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
843 Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
846 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
850 id = (IntPtr)_requestId++;
851 _callback_map[id] = (error, key) =>
853 Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
854 if (error != ConnectionError.None)
856 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + error);
857 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + error));
861 task.SetResult(true);
865 _callback_map.Remove(key);
870 Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
873 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, _callback_map[id], id);
875 if ((ConnectionError)ret != ConnectionError.None)
877 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
878 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
879 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
880 ConnectionErrorFactory.ThrowConnectionException(ret);
882 } catch (Exception e)
884 Log.Error(Globals.LogTag, "Exception on SetDefaultCellularServiceProfileAsync\n" + e.ToString());
885 task.SetException(e);
892 throw new ArgumentNullException("Profile is null");
897 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
899 Log.Debug(Globals.LogTag, "GetProfileListAsync");
900 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
902 List<ConnectionProfile> Result = new List<ConnectionProfile>();
904 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
905 if ((ConnectionError)ret != ConnectionError.None)
907 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
908 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");
909 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
910 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
911 ConnectionErrorFactory.ThrowConnectionException(ret);
914 while (Interop.Connection.HasNextProfileIterator(iterator))
918 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
919 Interop.ConnectionProfile.Clone(out profileH, nextH);
922 Interop.ConnectionProfile.GetType(profileH, out profileType);
924 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
926 WiFiProfile cur = new WiFiProfile(profileH);
929 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
931 CellularProfile cur = new CellularProfile(profileH);
935 ConnectionProfile cur = new ConnectionProfile(profileH);
939 Interop.Connection.DestroyProfileIterator(iterator);
940 task.SetResult(Result);
944 internal Task OpenProfileAsync(ConnectionProfile profile)
946 Log.Info(Globals.LogTag, "OpenProfileAsync");
949 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
950 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
954 id = (IntPtr)_requestId++;
955 _callback_map[id] = (error, key) =>
957 Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
958 if (error != ConnectionError.None)
960 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + error);
961 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + error));
965 task.SetResult(true);
969 _callback_map.Remove(key);
974 Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
977 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
978 if ((ConnectionError)ret != ConnectionError.None)
980 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
981 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
982 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
983 ConnectionErrorFactory.ThrowConnectionException(ret);
988 Log.Error(Globals.LogTag, "Exception on OpenProfile\n" + e.ToString());
989 task.SetException(e);
997 throw new ArgumentNullException("Profile is null");
1001 internal Task CloseProfileAsync(ConnectionProfile profile)
1003 Log.Info(Globals.LogTag, "CloseProfileAsync");
1004 if (profile != null)
1006 Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
1007 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
1009 lock (_callback_map)
1011 id = (IntPtr)_requestId++;
1012 _callback_map[id] = (error, key) =>
1014 Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
1015 if (error!= ConnectionError.None)
1017 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + error);
1018 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + error));
1022 task.SetResult(true);
1024 lock (_callback_map)
1026 _callback_map.Remove(key);
1031 Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
1034 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
1035 if ((ConnectionError)ret != ConnectionError.None)
1037 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
1038 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1039 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
1040 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1041 ConnectionErrorFactory.ThrowConnectionException(ret);
1046 Log.Error(Globals.LogTag, "Exception on CloseProfile\n" + e.ToString());
1047 task.SetException(e);
1055 throw new ArgumentNullException("Profile is null");