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_destroy 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)
125 ConnectionTypeChangedStart();
127 _ConnectionTypeChanged += value;
134 _ConnectionTypeChanged -= value;
135 if (_ConnectionTypeChanged == null)
137 ConnectionTypeChangedStop();
143 private void ConnectionTypeChangedStart()
145 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
147 if (_ConnectionTypeChanged != null)
149 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
153 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
154 if ((ConnectionError)ret != ConnectionError.None)
156 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
157 ConnectionErrorFactory.ThrowConnectionException(ret);
161 private void ConnectionTypeChangedStop()
163 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
164 if ((ConnectionError)ret != ConnectionError.None)
166 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
167 ConnectionErrorFactory.ThrowConnectionException(ret);
171 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
177 if (_EthernetCableStateChanged == null)
179 EthernetCableStateChangedStart();
181 _EthernetCableStateChanged += value;
188 _EthernetCableStateChanged -= value;
189 if (_EthernetCableStateChanged == null)
191 EthernetCableStateChangedStop();
197 private void EthernetCableStateChangedStart()
199 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
201 if (_EthernetCableStateChanged != null)
203 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
206 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
207 _ethernetCableStateChangedCallback, IntPtr.Zero);
208 if ((ConnectionError)ret != ConnectionError.None)
210 Log.Error(Globals.LogTag,
211 "It failed to register ethernet cable state changed callback, " +
212 (ConnectionError)ret);
213 ConnectionErrorFactory.ThrowConnectionException(ret);
217 private void EthernetCableStateChangedStop()
219 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
220 if ((ConnectionError)ret != ConnectionError.None)
222 Log.Error(Globals.LogTag,
223 "It failed to unregister ethernet cable state changed callback, " +
224 (ConnectionError)ret);
225 ConnectionErrorFactory.ThrowConnectionException(ret);
229 internal event EventHandler<AddressEventArgs> IPAddressChanged
235 if (_IPAddressChanged == null)
237 IPAddressChangedStart();
239 _IPAddressChanged += value;
247 _IPAddressChanged -= value;
248 if (_IPAddressChanged == null)
250 IPAddressChangedStop();
256 private void IPAddressChangedStart()
258 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
260 if (_IPAddressChanged != null)
262 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
263 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
265 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
267 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
272 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
273 if ((ConnectionError)ret != ConnectionError.None)
275 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
279 private void IPAddressChangedStop()
281 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
282 if ((ConnectionError)ret != ConnectionError.None)
284 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
288 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
294 if (_ProxyAddressChanged == null)
296 ProxyAddressChangedStart();
298 _ProxyAddressChanged += value;
305 _ProxyAddressChanged -= value;
306 if (_ProxyAddressChanged == null)
308 ProxyAddressChangedStop();
314 private void ProxyAddressChangedStart()
316 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
318 if (_ProxyAddressChanged != null)
320 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
321 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
323 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
325 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
330 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
331 if ((ConnectionError)ret != ConnectionError.None)
333 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
337 private void ProxyAddressChangedStop()
339 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
340 if ((ConnectionError)ret != ConnectionError.None)
342 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
346 private void UnregisterEvents()
348 if (_ConnectionTypeChanged != null)
350 ConnectionTypeChangedStop();
352 if (_IPAddressChanged != null)
354 IPAddressChangedStop();
356 if (_EthernetCableStateChanged != null)
358 EthernetCableStateChangedStop();
360 if (_ProxyAddressChanged != null)
362 ProxyAddressChangedStop();
366 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
368 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
371 internal bool HasNext(IntPtr iterator)
373 return Interop.Connection.HasNextProfileIterator(iterator);
376 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
378 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
381 internal int DestoryProfileIterator(IntPtr iterator)
383 return Interop.Connection.DestroyProfileIterator(iterator);
386 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
388 Log.Info(Globals.LogTag, "GetIPAddress " + family);
390 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
391 if ((ConnectionError)ret != ConnectionError.None)
393 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
394 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");
395 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
396 ConnectionErrorFactory.ThrowConnectionException(ret);
399 string result = Marshal.PtrToStringAnsi(ip);
400 Interop.Libc.Free(ip);
401 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
402 if (result.Length == 0)
404 if (family == AddressFamily.IPv4)
405 return System.Net.IPAddress.Parse("0.0.0.0");
407 return System.Net.IPAddress.Parse("::");
409 return System.Net.IPAddress.Parse(result);
412 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
414 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
415 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
416 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
418 if (ipv6Address != IntPtr.Zero)
420 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
421 if (ipv6.Length == 0)
422 ipList.Add(System.Net.IPAddress.Parse("::"));
424 ipList.Add(System.Net.IPAddress.Parse(ipv6));
430 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
431 if (ret != (int)ConnectionError.None)
433 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
434 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");
435 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
436 ConnectionErrorFactory.ThrowConnectionException(ret);
442 internal string GetProxy(AddressFamily family)
444 Log.Debug(Globals.LogTag, "GetProxy " + family);
446 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
447 if ((ConnectionError)ret != ConnectionError.None)
449 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
450 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");
451 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
452 ConnectionErrorFactory.ThrowConnectionException(ret);
455 string result = Marshal.PtrToStringAnsi(ip);
456 Interop.Libc.Free(ip);
460 internal string GetMacAddress(ConnectionType type)
462 Log.Info(Globals.LogTag, "GetMacAddress " + type);
464 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
465 if ((ConnectionError)ret != ConnectionError.None)
467 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
468 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
469 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
470 ConnectionErrorFactory.ThrowConnectionException(ret);
473 string result = Marshal.PtrToStringAnsi(mac);
474 Interop.Libc.Free(mac);
478 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
480 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
482 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
483 (int)statisticsType, out size);
484 if ((ConnectionError)ret != ConnectionError.None)
486 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
487 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
488 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
489 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
490 ConnectionErrorFactory.ThrowConnectionException(ret);
495 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
497 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
498 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
499 (int)statisticsType);
500 if ((ConnectionError)ret != ConnectionError.None)
502 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
503 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
504 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
505 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
506 ConnectionErrorFactory.ThrowConnectionException(ret);
510 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
512 if (interfaceName != null && address != null && gateway != null)
514 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
515 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
516 if ((ConnectionError)ret != ConnectionError.None)
518 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
519 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");
520 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
521 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
522 ConnectionErrorFactory.ThrowConnectionException(ret);
528 throw new ArgumentNullException("Arguments are null");
532 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
534 if (interfaceName != null && address != null && gateway != null)
536 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
537 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
538 if ((ConnectionError)ret != ConnectionError.None)
540 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
541 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");
542 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
543 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
544 ConnectionErrorFactory.ThrowConnectionException(ret);
550 throw new ArgumentNullException("Arguments are null");
554 internal ConnectionType ConnectionType
558 Log.Info(Globals.LogTag, "get ConnectionType");
560 int ret = Interop.Connection.GetType(GetHandle(), out type);
561 if ((ConnectionError)ret != ConnectionError.None)
563 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
564 ConnectionErrorFactory.ThrowConnectionException(ret);
566 return (ConnectionType)type;
570 internal CellularState CellularState
574 Log.Info(Globals.LogTag, "get CellularState");
576 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
577 if ((ConnectionError)ret != ConnectionError.None)
579 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
580 ConnectionErrorFactory.ThrowConnectionException(ret);
582 return (CellularState)type;
586 internal ConnectionState WiFiState
590 Log.Info(Globals.LogTag, "get WiFiState");
592 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
593 if ((ConnectionError)ret != ConnectionError.None)
595 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
596 ConnectionErrorFactory.ThrowConnectionException(ret);
598 return (ConnectionState)type;
602 internal ConnectionState BluetoothState
606 Log.Info(Globals.LogTag, "get BluetoothState");
608 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
609 if ((ConnectionError)ret != ConnectionError.None)
611 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
612 ConnectionErrorFactory.ThrowConnectionException(ret);
614 return (ConnectionState)type;
618 internal ConnectionState EthernetState
622 Log.Info(Globals.LogTag, "get ConnectionType");
624 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
625 if ((ConnectionError)ret != ConnectionError.None)
627 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
628 ConnectionErrorFactory.ThrowConnectionException(ret);
630 return (ConnectionState)type;
634 internal EthernetCableState EthernetCableState
638 Log.Info(Globals.LogTag, "get EthernetCableState");
640 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
641 if ((ConnectionError)ret != ConnectionError.None)
643 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
644 ConnectionErrorFactory.ThrowConnectionException(ret);
646 return (EthernetCableState)type;
650 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
652 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
655 IntPtr handle = IntPtr.Zero;
656 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
657 if ((ConnectionError)ret != ConnectionError.None)
659 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
660 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
661 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
662 ConnectionErrorFactory.ThrowConnectionException(ret);
670 throw new ArgumentNullException("Keyword is null");
674 internal void AddCellularProfile(CellularProfile profile)
677 Log.Debug(Globals.LogTag, "AddCellularProfile");
680 if (profile.Type == ConnectionProfileType.Cellular)
682 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
683 if ((ConnectionError)ret != ConnectionError.None)
685 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
686 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
687 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
688 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
689 ConnectionErrorFactory.ThrowConnectionException(ret);
695 throw new ArgumentException("Profile type is not cellular");
701 throw new ArgumentNullException("Profile is null");
705 internal void RemoveProfile(ConnectionProfile profile)
707 Log.Debug(Globals.LogTag, "RemoveProfile");
710 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
711 if ((ConnectionError)ret != ConnectionError.None)
713 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
714 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
715 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
716 ConnectionErrorFactory.ThrowConnectionException(ret);
722 throw new ArgumentNullException("Profile is null");
726 internal void UpdateProfile(ConnectionProfile profile)
728 Log.Info(Globals.LogTag, "UpdateProfile");
731 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
732 if ((ConnectionError)ret != ConnectionError.None)
734 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
735 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
736 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
737 ConnectionErrorFactory.ThrowConnectionException(ret);
743 throw new ArgumentNullException("Profile is null");
747 internal ConnectionProfile GetCurrentProfile()
749 Log.Info(Globals.LogTag, "GetCurrentProfile");
750 IntPtr ProfileHandle;
751 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
752 if ((ConnectionError)ret != ConnectionError.None)
754 if ((ConnectionError)ret == ConnectionError.NoConnection)
756 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
761 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
762 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");
763 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
764 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
765 ConnectionErrorFactory.ThrowConnectionException(ret);
769 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
773 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
775 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
776 IntPtr ProfileHandle;
777 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
778 if ((ConnectionError)ret != ConnectionError.None)
780 Log.Error(Globals.LogTag, "Error: " + ret);
781 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
782 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
783 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
784 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
785 ConnectionErrorFactory.ThrowConnectionException(ret);
788 CellularProfile Profile = new CellularProfile(ProfileHandle);
792 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
794 Log.Debug(Globals.LogTag, "SetDefaultCellularProfile");
797 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
798 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
800 if (Result != ConnectionError.None)
802 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
803 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result));
807 task.SetResult(true);
811 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
812 if ((ConnectionError)ret != ConnectionError.None)
814 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
815 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
816 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
817 ConnectionErrorFactory.ThrowConnectionException(ret);
825 throw new ArgumentNullException("Profile is null");
830 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
832 Log.Debug(Globals.LogTag, "GetProfileListAsync");
833 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
835 List<ConnectionProfile> Result = new List<ConnectionProfile>();
837 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
838 if ((ConnectionError)ret != ConnectionError.None)
840 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
841 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");
842 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
843 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
844 ConnectionErrorFactory.ThrowConnectionException(ret);
847 while (Interop.Connection.HasNextProfileIterator(iterator))
851 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
852 Interop.ConnectionProfile.Clone(out profileH, nextH);
855 Interop.ConnectionProfile.GetType(profileH, out profileType);
857 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
859 WiFiProfile cur = new WiFiProfile(profileH);
862 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
864 CellularProfile cur = new CellularProfile(profileH);
868 ConnectionProfile cur = new ConnectionProfile(profileH);
872 Interop.Connection.DestroyProfileIterator(iterator);
873 task.SetResult(Result);
877 internal Task OpenProfileAsync(ConnectionProfile profile)
879 Log.Debug(Globals.LogTag, "OpenProfileAsync");
882 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
883 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
884 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
886 Log.Debug(Globals.LogTag, "Connected " + profile.Name);
887 if (Result != ConnectionError.None)
889 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
890 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result));
894 task.SetResult(true);
898 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
899 if ((ConnectionError)ret != ConnectionError.None)
901 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
902 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
903 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
904 ConnectionErrorFactory.ThrowConnectionException(ret);
912 throw new ArgumentNullException("Profile is null");
916 internal Task CloseProfileAsync(ConnectionProfile profile)
918 Log.Debug(Globals.LogTag, "CloseProfileAsync");
921 Log.Debug(Globals.LogTag, "CloseProfileAsync " + profile.Name);
922 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
923 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
925 if (Result != ConnectionError.None)
927 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
928 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result));
932 task.SetResult(true);
936 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
937 if ((ConnectionError)ret != ConnectionError.None)
939 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
940 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
941 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
942 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
943 ConnectionErrorFactory.ThrowConnectionException(ret);
951 throw new ArgumentNullException("Profile is null");