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 ConnectionInternalManager s_instance = null;
74 private static readonly object _lock = new object();
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 static readonly object _ConnectionTypeChangedLock = new object();
82 private static readonly object _IPAddressChangedLock = new object();
83 private static readonly object _EthernetCableStateChangedLock = new object();
84 private static readonly object _ProxyAddressChangedLock = new object();
87 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
88 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
89 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
90 private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
92 private Dictionary<IntPtr, Interop.Connection.ConnectionCallback> _callback_map =
93 new Dictionary<IntPtr, Interop.Connection.ConnectionCallback>();
94 private int _requestId = 0;
96 internal static ConnectionInternalManager Instance
102 if (s_instance == null)
104 s_instance = new ConnectionInternalManager();
106 Log.Info(Globals.LogTag, "ConnectionInternalManager.Instance");
112 private HandleHolder _handleHolder;
114 private ConnectionInternalManager()
116 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
117 _handleHolder = new HandleHolder();
118 Log.Info(Globals.LogTag, "Success to get handle");
121 ~ConnectionInternalManager()
126 internal IntPtr GetHandle()
128 return _handleHolder.GetHandle();
131 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
135 lock (_ConnectionTypeChangedLock)
137 if (_ConnectionTypeChanged == null)
141 ConnectionTypeChangedStart();
145 Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
149 _ConnectionTypeChanged += value;
154 lock (_ConnectionTypeChangedLock)
156 _ConnectionTypeChanged -= value;
157 if (_ConnectionTypeChanged == null)
161 ConnectionTypeChangedStop();
165 Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
172 private void ConnectionTypeChangedStart()
174 Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
175 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
177 if (_ConnectionTypeChanged != null)
179 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
183 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
184 if ((ConnectionError)ret != ConnectionError.None)
186 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
187 ConnectionErrorFactory.ThrowConnectionException(ret);
191 private void ConnectionTypeChangedStop()
193 Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
194 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
195 if ((ConnectionError)ret != ConnectionError.None)
197 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
198 ConnectionErrorFactory.ThrowConnectionException(ret);
202 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
206 lock(_EthernetCableStateChangedLock)
208 if (_EthernetCableStateChanged == null)
212 EthernetCableStateChangedStart();
216 Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
220 _EthernetCableStateChanged += value;
225 lock(_EthernetCableStateChangedLock)
227 _EthernetCableStateChanged -= value;
228 if (_EthernetCableStateChanged == null)
232 EthernetCableStateChangedStop();
236 Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
243 private void EthernetCableStateChangedStart()
245 Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
246 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
248 if (_EthernetCableStateChanged != null)
250 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
253 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
254 _ethernetCableStateChangedCallback, IntPtr.Zero);
255 if ((ConnectionError)ret != ConnectionError.None)
257 Log.Error(Globals.LogTag,
258 "It failed to register ethernet cable state changed callback, " +
259 (ConnectionError)ret);
260 ConnectionErrorFactory.ThrowConnectionException(ret);
264 private void EthernetCableStateChangedStop()
266 Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
267 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
268 if ((ConnectionError)ret != ConnectionError.None)
270 Log.Error(Globals.LogTag,
271 "It failed to unregister ethernet cable state changed callback, " +
272 (ConnectionError)ret);
273 ConnectionErrorFactory.ThrowConnectionException(ret);
277 internal event EventHandler<AddressEventArgs> IPAddressChanged
281 lock (_IPAddressChangedLock)
283 if (_IPAddressChanged == null)
287 IPAddressChangedStart();
291 Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
295 _IPAddressChanged += value;
301 lock (_IPAddressChangedLock)
303 _IPAddressChanged -= value;
304 if (_IPAddressChanged == null)
308 IPAddressChangedStop();
312 Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
319 private void IPAddressChangedStart()
321 Log.Info(Globals.LogTag, "Register IPAddressChanged");
322 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
324 if (_IPAddressChanged != null)
326 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
327 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
329 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
331 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
336 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
337 if ((ConnectionError)ret != ConnectionError.None)
339 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
343 private void IPAddressChangedStop()
345 Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
346 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
347 if ((ConnectionError)ret != ConnectionError.None)
349 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
353 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
357 lock (_ProxyAddressChangedLock)
359 if (_ProxyAddressChanged == null)
363 ProxyAddressChangedStart();
367 Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
371 _ProxyAddressChanged += value;
376 lock (_ProxyAddressChangedLock)
378 _ProxyAddressChanged -= value;
379 if (_ProxyAddressChanged == null)
383 ProxyAddressChangedStop();
387 Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
394 private void ProxyAddressChangedStart()
396 Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
397 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
399 if (_ProxyAddressChanged != null)
401 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
402 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
404 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
406 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
411 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
412 if ((ConnectionError)ret != ConnectionError.None)
414 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
418 private void ProxyAddressChangedStop()
420 Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
421 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
422 if ((ConnectionError)ret != ConnectionError.None)
424 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
428 private void UnregisterEvents()
430 if (_ConnectionTypeChanged != null)
432 ConnectionTypeChangedStop();
434 if (_IPAddressChanged != null)
436 IPAddressChangedStop();
438 if (_EthernetCableStateChanged != null)
440 EthernetCableStateChangedStop();
442 if (_ProxyAddressChanged != null)
444 ProxyAddressChangedStop();
448 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
450 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
453 internal bool HasNext(IntPtr iterator)
455 return Interop.Connection.HasNextProfileIterator(iterator);
458 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
460 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
463 internal int DestoryProfileIterator(IntPtr iterator)
465 return Interop.Connection.DestroyProfileIterator(iterator);
468 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
470 Log.Info(Globals.LogTag, "GetIPAddress " + family);
472 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
473 if ((ConnectionError)ret != ConnectionError.None)
475 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
476 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");
477 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
478 ConnectionErrorFactory.ThrowConnectionException(ret);
481 string result = Marshal.PtrToStringAnsi(ip);
482 Interop.Libc.Free(ip);
483 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
484 if (result.Length == 0)
486 if (family == AddressFamily.IPv4)
487 return System.Net.IPAddress.Parse("0.0.0.0");
489 return System.Net.IPAddress.Parse("::");
491 return System.Net.IPAddress.Parse(result);
494 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
496 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
497 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
498 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
500 if (ipv6Address != IntPtr.Zero)
502 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
503 if (ipv6.Length == 0)
504 ipList.Add(System.Net.IPAddress.Parse("::"));
506 ipList.Add(System.Net.IPAddress.Parse(ipv6));
512 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
513 if (ret != (int)ConnectionError.None)
515 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
516 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");
517 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
518 ConnectionErrorFactory.ThrowConnectionException(ret);
524 internal string GetProxy(AddressFamily family)
526 Log.Debug(Globals.LogTag, "GetProxy " + family);
528 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
529 if ((ConnectionError)ret != ConnectionError.None)
531 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
532 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");
533 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
534 ConnectionErrorFactory.ThrowConnectionException(ret);
537 string result = Marshal.PtrToStringAnsi(ip);
538 Interop.Libc.Free(ip);
542 internal string GetMacAddress(ConnectionType type)
544 Log.Info(Globals.LogTag, "GetMacAddress " + type);
546 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
547 if ((ConnectionError)ret != ConnectionError.None)
549 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
550 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
551 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
552 ConnectionErrorFactory.ThrowConnectionException(ret);
555 string result = Marshal.PtrToStringAnsi(mac);
556 Interop.Libc.Free(mac);
560 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
562 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
564 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
565 (int)statisticsType, out size);
566 if ((ConnectionError)ret != ConnectionError.None)
568 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
569 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
570 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
571 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
572 ConnectionErrorFactory.ThrowConnectionException(ret);
577 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
579 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
580 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
581 (int)statisticsType);
582 if ((ConnectionError)ret != ConnectionError.None)
584 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
585 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
586 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
587 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
588 ConnectionErrorFactory.ThrowConnectionException(ret);
592 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
594 if (interfaceName != null && address != null && gateway != null)
596 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
597 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
598 if ((ConnectionError)ret != ConnectionError.None)
600 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
601 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");
602 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.route)");
603 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
604 ConnectionErrorFactory.ThrowConnectionException(ret);
610 throw new ArgumentNullException("Arguments are null");
614 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
616 if (interfaceName != null && address != null && gateway != null)
618 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
619 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
620 if ((ConnectionError)ret != ConnectionError.None)
622 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
623 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");
624 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.route)");
625 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
626 ConnectionErrorFactory.ThrowConnectionException(ret);
632 throw new ArgumentNullException("Arguments are null");
636 internal ConnectionType ConnectionType
640 Log.Info(Globals.LogTag, "get ConnectionType");
642 int ret = Interop.Connection.GetType(GetHandle(), out type);
643 if ((ConnectionError)ret != ConnectionError.None)
645 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
646 ConnectionErrorFactory.ThrowConnectionException(ret);
648 return (ConnectionType)type;
652 internal CellularState CellularState
656 Log.Info(Globals.LogTag, "get CellularState");
658 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
659 if ((ConnectionError)ret != ConnectionError.None)
661 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
662 ConnectionErrorFactory.ThrowConnectionException(ret);
664 return (CellularState)type;
668 internal ConnectionState WiFiState
672 Log.Info(Globals.LogTag, "get WiFiState");
674 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
675 if ((ConnectionError)ret != ConnectionError.None)
677 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
678 ConnectionErrorFactory.ThrowConnectionException(ret);
680 return (ConnectionState)type;
684 internal ConnectionState BluetoothState
688 Log.Info(Globals.LogTag, "get BluetoothState");
690 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
691 if ((ConnectionError)ret != ConnectionError.None)
693 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
694 ConnectionErrorFactory.ThrowConnectionException(ret);
696 return (ConnectionState)type;
700 internal ConnectionState EthernetState
704 Log.Info(Globals.LogTag, "get ConnectionType");
706 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
707 if ((ConnectionError)ret != ConnectionError.None)
709 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
710 ConnectionErrorFactory.ThrowConnectionException(ret);
712 return (ConnectionState)type;
716 internal EthernetCableState EthernetCableState
720 Log.Info(Globals.LogTag, "get EthernetCableState");
722 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
723 if ((ConnectionError)ret != ConnectionError.None)
725 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
726 ConnectionErrorFactory.ThrowConnectionException(ret);
728 return (EthernetCableState)type;
732 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
734 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
737 IntPtr handle = IntPtr.Zero;
738 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
739 if ((ConnectionError)ret != ConnectionError.None)
741 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
742 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
743 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
744 ConnectionErrorFactory.ThrowConnectionException(ret);
752 throw new ArgumentNullException("Keyword is null");
756 internal void AddCellularProfile(CellularProfile profile)
759 Log.Debug(Globals.LogTag, "AddCellularProfile");
762 if (profile.Type == ConnectionProfileType.Cellular)
764 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
765 if ((ConnectionError)ret != ConnectionError.None)
767 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
768 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
769 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
770 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
771 ConnectionErrorFactory.ThrowConnectionException(ret);
777 throw new ArgumentException("Profile type is not cellular");
783 throw new ArgumentNullException("Profile is null");
787 internal void RemoveProfile(ConnectionProfile profile)
789 Log.Debug(Globals.LogTag, "RemoveProfile");
792 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
793 if ((ConnectionError)ret != ConnectionError.None)
795 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
796 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
797 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
798 ConnectionErrorFactory.ThrowConnectionException(ret);
804 throw new ArgumentNullException("Profile is null");
808 internal void UpdateProfile(ConnectionProfile profile)
810 Log.Info(Globals.LogTag, "UpdateProfile");
813 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
814 if ((ConnectionError)ret != ConnectionError.None)
816 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
817 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
818 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
819 ConnectionErrorFactory.ThrowConnectionException(ret);
825 throw new ArgumentNullException("Profile is null");
829 internal ConnectionProfile GetCurrentProfile()
831 Log.Info(Globals.LogTag, "GetCurrentProfile");
832 IntPtr ProfileHandle;
833 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
834 if ((ConnectionError)ret != ConnectionError.None)
836 if ((ConnectionError)ret == ConnectionError.NoConnection)
838 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
841 else if ((ConnectionError)ret == ConnectionError.InvalidParameter)
843 throw new InvalidOperationException("Invalid handle");
847 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
848 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");
849 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
850 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
851 ConnectionErrorFactory.ThrowConnectionException(ret);
855 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
859 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
861 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
862 IntPtr ProfileHandle;
863 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
864 if ((ConnectionError)ret != ConnectionError.None)
866 Log.Error(Globals.LogTag, "Error: " + ret);
867 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
868 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
869 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
870 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
871 ConnectionErrorFactory.ThrowConnectionException(ret);
874 CellularProfile Profile = new CellularProfile(ProfileHandle);
878 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
880 Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
883 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
887 id = (IntPtr)_requestId++;
888 _callback_map[id] = (error, key) =>
890 Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
891 if (error != ConnectionError.None)
893 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + error);
894 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + error));
898 task.SetResult(true);
902 _callback_map.Remove(key);
907 Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
910 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, _callback_map[id], id);
912 if ((ConnectionError)ret != ConnectionError.None)
914 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
915 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
916 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
917 ConnectionErrorFactory.ThrowConnectionException(ret);
919 } catch (Exception e)
921 Log.Error(Globals.LogTag, "Exception on SetDefaultCellularServiceProfileAsync\n" + e.ToString());
922 task.SetException(e);
929 throw new ArgumentNullException("Profile is null");
934 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
936 Log.Debug(Globals.LogTag, "GetProfileListAsync");
937 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
939 List<ConnectionProfile> Result = new List<ConnectionProfile>();
941 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
942 if ((ConnectionError)ret != ConnectionError.None)
944 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
945 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");
946 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
947 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
948 ConnectionErrorFactory.ThrowConnectionException(ret);
951 while (Interop.Connection.HasNextProfileIterator(iterator))
955 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
956 Interop.ConnectionProfile.Clone(out profileH, nextH);
959 Interop.ConnectionProfile.GetType(profileH, out profileType);
961 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
963 WiFiProfile cur = new WiFiProfile(profileH);
966 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
968 CellularProfile cur = new CellularProfile(profileH);
972 ConnectionProfile cur = new ConnectionProfile(profileH);
976 Interop.Connection.DestroyProfileIterator(iterator);
977 task.SetResult(Result);
981 internal Task OpenProfileAsync(ConnectionProfile profile)
983 Log.Info(Globals.LogTag, "OpenProfileAsync");
986 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
987 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
991 id = (IntPtr)_requestId++;
992 _callback_map[id] = (error, key) =>
994 Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
995 if (error != ConnectionError.None)
997 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + error);
998 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + error));
1002 task.SetResult(true);
1004 lock (_callback_map)
1006 _callback_map.Remove(key);
1011 Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
1014 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
1015 if ((ConnectionError)ret != ConnectionError.None)
1017 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
1018 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1019 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1020 ConnectionErrorFactory.ThrowConnectionException(ret);
1025 Log.Error(Globals.LogTag, "Exception on OpenProfile\n" + e.ToString());
1026 task.SetException(e);
1034 throw new ArgumentNullException("Profile is null");
1038 internal Task CloseProfileAsync(ConnectionProfile profile)
1040 Log.Info(Globals.LogTag, "CloseProfileAsync");
1041 if (profile != null)
1043 Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
1044 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
1046 lock (_callback_map)
1048 id = (IntPtr)_requestId++;
1049 _callback_map[id] = (error, key) =>
1051 Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
1052 if (error!= ConnectionError.None)
1054 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + error);
1055 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + error));
1059 task.SetResult(true);
1061 lock (_callback_map)
1063 _callback_map.Remove(key);
1068 Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
1071 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
1072 if ((ConnectionError)ret != ConnectionError.None)
1074 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
1075 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1076 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
1077 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1078 ConnectionErrorFactory.ThrowConnectionException(ret);
1083 Log.Error(Globals.LogTag, "Exception on CloseProfile\n" + e.ToString());
1084 task.SetException(e);
1092 throw new ArgumentNullException("Profile is null");