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;
22 using System.Threading.Tasks;
23 using System.Runtime.InteropServices;
24 using System.Collections;
25 using Tizen.Applications;
27 namespace Tizen.Network.Connection
31 private IntPtr Handle;
36 _tid = Thread.CurrentThread.ManagedThreadId;
38 Log.Info(Globals.LogTag, "PInvoke connection_create for Thread " + _tid);
39 int ret = Interop.Connection.Create(_tid, out Handle);
40 Log.Info(Globals.LogTag, "Handle: " + Handle);
41 if(ret != (int)ConnectionError.None)
43 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");
44 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
45 ConnectionErrorFactory.ThrowConnectionException(ret);
54 internal IntPtr GetHandle()
56 Log.Debug(Globals.LogTag, "handleholder handle = " + Handle);
60 private void Destroy()
63 Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + _tid);
64 Interop.Connection.Destroy(_tid, Handle);
65 if (Handle != IntPtr.Zero)
72 internal class ConnectionInternalManager
74 private static readonly Lazy<ConnectionInternalManager> s_instance =
75 new Lazy<ConnectionInternalManager>(() => new ConnectionInternalManager());
77 private EventHandler<ConnectionTypeEventArgs> _ConnectionTypeChanged = null;
78 private EventHandler<AddressEventArgs> _IPAddressChanged = null;
79 private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
80 private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
82 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
83 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
84 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
85 private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
87 private TizenSynchronizationContext context = new TizenSynchronizationContext();
89 internal static ConnectionInternalManager Instance
93 return s_instance.Value;
97 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
99 Log.Info(Globals.LogTag, "In threadlocal delegate");
100 return new HandleHolder();
103 private ConnectionInternalManager()
105 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
108 ~ConnectionInternalManager()
113 internal IntPtr GetHandle()
115 return s_threadName.Value.GetHandle();
118 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
124 if (_ConnectionTypeChanged == null)
126 ConnectionTypeChangedStart();
128 _ConnectionTypeChanged += value;
135 _ConnectionTypeChanged -= value;
136 if (_ConnectionTypeChanged == null)
138 ConnectionTypeChangedStop();
144 private void ConnectionTypeChangedStart()
146 Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
147 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
149 if (_ConnectionTypeChanged != null)
151 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
155 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
156 if ((ConnectionError)ret != ConnectionError.None)
158 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
159 ConnectionErrorFactory.ThrowConnectionException(ret);
163 private void ConnectionTypeChangedStop()
165 Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
166 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
167 if ((ConnectionError)ret != ConnectionError.None)
169 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
170 ConnectionErrorFactory.ThrowConnectionException(ret);
174 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
180 if (_EthernetCableStateChanged == null)
182 EthernetCableStateChangedStart();
184 _EthernetCableStateChanged += value;
191 _EthernetCableStateChanged -= value;
192 if (_EthernetCableStateChanged == null)
194 EthernetCableStateChangedStop();
200 private void EthernetCableStateChangedStart()
202 Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
203 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
205 if (_EthernetCableStateChanged != null)
207 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
210 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
211 _ethernetCableStateChangedCallback, IntPtr.Zero);
212 if ((ConnectionError)ret != ConnectionError.None)
214 Log.Error(Globals.LogTag,
215 "It failed to register ethernet cable state changed callback, " +
216 (ConnectionError)ret);
217 ConnectionErrorFactory.ThrowConnectionException(ret);
221 private void EthernetCableStateChangedStop()
223 Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
224 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
225 if ((ConnectionError)ret != ConnectionError.None)
227 Log.Error(Globals.LogTag,
228 "It failed to unregister ethernet cable state changed callback, " +
229 (ConnectionError)ret);
230 ConnectionErrorFactory.ThrowConnectionException(ret);
234 internal event EventHandler<AddressEventArgs> IPAddressChanged
240 if (_IPAddressChanged == null)
242 IPAddressChangedStart();
244 _IPAddressChanged += value;
252 _IPAddressChanged -= value;
253 if (_IPAddressChanged == null)
255 IPAddressChangedStop();
261 private void IPAddressChangedStart()
263 Log.Info(Globals.LogTag, "Register IPAddressChanged");
264 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
266 if (_IPAddressChanged != null)
268 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
269 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
271 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
273 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
278 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
279 if ((ConnectionError)ret != ConnectionError.None)
281 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
285 private void IPAddressChangedStop()
287 Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
288 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
289 if ((ConnectionError)ret != ConnectionError.None)
291 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
295 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
301 if (_ProxyAddressChanged == null)
303 ProxyAddressChangedStart();
305 _ProxyAddressChanged += value;
312 _ProxyAddressChanged -= value;
313 if (_ProxyAddressChanged == null)
315 ProxyAddressChangedStop();
321 private void ProxyAddressChangedStart()
323 Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
324 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
326 if (_ProxyAddressChanged != null)
328 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
329 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
331 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
333 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
338 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
339 if ((ConnectionError)ret != ConnectionError.None)
341 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
345 private void ProxyAddressChangedStop()
347 Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
348 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
349 if ((ConnectionError)ret != ConnectionError.None)
351 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
355 private void UnregisterEvents()
357 if (_ConnectionTypeChanged != null)
359 ConnectionTypeChangedStop();
361 if (_IPAddressChanged != null)
363 IPAddressChangedStop();
365 if (_EthernetCableStateChanged != null)
367 EthernetCableStateChangedStop();
369 if (_ProxyAddressChanged != null)
371 ProxyAddressChangedStop();
375 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
377 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
380 internal bool HasNext(IntPtr iterator)
382 return Interop.Connection.HasNextProfileIterator(iterator);
385 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
387 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
390 internal int DestoryProfileIterator(IntPtr iterator)
392 return Interop.Connection.DestroyProfileIterator(iterator);
395 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
397 Log.Info(Globals.LogTag, "GetIPAddress " + family);
399 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
400 if ((ConnectionError)ret != ConnectionError.None)
402 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
403 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");
404 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
405 ConnectionErrorFactory.ThrowConnectionException(ret);
408 string result = Marshal.PtrToStringAnsi(ip);
409 Interop.Libc.Free(ip);
410 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
411 if (result.Length == 0)
413 if (family == AddressFamily.IPv4)
414 return System.Net.IPAddress.Parse("0.0.0.0");
416 return System.Net.IPAddress.Parse("::");
418 return System.Net.IPAddress.Parse(result);
421 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
423 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
424 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
425 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
427 if (ipv6Address != IntPtr.Zero)
429 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
430 if (ipv6.Length == 0)
431 ipList.Add(System.Net.IPAddress.Parse("::"));
433 ipList.Add(System.Net.IPAddress.Parse(ipv6));
439 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
440 if (ret != (int)ConnectionError.None)
442 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
443 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");
444 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
445 ConnectionErrorFactory.ThrowConnectionException(ret);
451 internal string GetProxy(AddressFamily family)
453 Log.Debug(Globals.LogTag, "GetProxy " + family);
455 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
456 if ((ConnectionError)ret != ConnectionError.None)
458 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
459 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");
460 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
461 ConnectionErrorFactory.ThrowConnectionException(ret);
464 string result = Marshal.PtrToStringAnsi(ip);
465 Interop.Libc.Free(ip);
469 internal string GetMacAddress(ConnectionType type)
471 Log.Info(Globals.LogTag, "GetMacAddress " + type);
473 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
474 if ((ConnectionError)ret != ConnectionError.None)
476 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
477 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
478 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
479 ConnectionErrorFactory.ThrowConnectionException(ret);
482 string result = Marshal.PtrToStringAnsi(mac);
483 Interop.Libc.Free(mac);
487 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
489 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
491 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
492 (int)statisticsType, out size);
493 if ((ConnectionError)ret != ConnectionError.None)
495 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
496 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
497 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
498 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
499 ConnectionErrorFactory.ThrowConnectionException(ret);
504 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
506 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
507 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
508 (int)statisticsType);
509 if ((ConnectionError)ret != ConnectionError.None)
511 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
512 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
513 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
514 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
515 ConnectionErrorFactory.ThrowConnectionException(ret);
519 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
521 if (interfaceName != null && address != null && gateway != null)
523 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
524 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
525 if ((ConnectionError)ret != ConnectionError.None)
527 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
528 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");
529 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
530 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
531 ConnectionErrorFactory.ThrowConnectionException(ret);
537 throw new ArgumentNullException("Arguments are null");
541 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
543 if (interfaceName != null && address != null && gateway != null)
545 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
546 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
547 if ((ConnectionError)ret != ConnectionError.None)
549 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
550 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");
551 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
552 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
553 ConnectionErrorFactory.ThrowConnectionException(ret);
559 throw new ArgumentNullException("Arguments are null");
563 internal ConnectionType ConnectionType
567 Log.Info(Globals.LogTag, "get ConnectionType");
569 int ret = Interop.Connection.GetType(GetHandle(), out type);
570 if ((ConnectionError)ret != ConnectionError.None)
572 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
573 ConnectionErrorFactory.ThrowConnectionException(ret);
575 return (ConnectionType)type;
579 internal CellularState CellularState
583 Log.Info(Globals.LogTag, "get CellularState");
585 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
586 if ((ConnectionError)ret != ConnectionError.None)
588 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
589 ConnectionErrorFactory.ThrowConnectionException(ret);
591 return (CellularState)type;
595 internal ConnectionState WiFiState
599 Log.Info(Globals.LogTag, "get WiFiState");
601 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
602 if ((ConnectionError)ret != ConnectionError.None)
604 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
605 ConnectionErrorFactory.ThrowConnectionException(ret);
607 return (ConnectionState)type;
611 internal ConnectionState BluetoothState
615 Log.Info(Globals.LogTag, "get BluetoothState");
617 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
618 if ((ConnectionError)ret != ConnectionError.None)
620 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
621 ConnectionErrorFactory.ThrowConnectionException(ret);
623 return (ConnectionState)type;
627 internal ConnectionState EthernetState
631 Log.Info(Globals.LogTag, "get ConnectionType");
633 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
634 if ((ConnectionError)ret != ConnectionError.None)
636 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
637 ConnectionErrorFactory.ThrowConnectionException(ret);
639 return (ConnectionState)type;
643 internal EthernetCableState EthernetCableState
647 Log.Info(Globals.LogTag, "get EthernetCableState");
649 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
650 if ((ConnectionError)ret != ConnectionError.None)
652 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
653 ConnectionErrorFactory.ThrowConnectionException(ret);
655 return (EthernetCableState)type;
659 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
661 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
664 IntPtr handle = IntPtr.Zero;
665 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
666 if ((ConnectionError)ret != ConnectionError.None)
668 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
669 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
670 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
671 ConnectionErrorFactory.ThrowConnectionException(ret);
679 throw new ArgumentNullException("Keyword is null");
683 internal void AddCellularProfile(CellularProfile profile)
686 Log.Debug(Globals.LogTag, "AddCellularProfile");
689 if (profile.Type == ConnectionProfileType.Cellular)
691 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
692 if ((ConnectionError)ret != ConnectionError.None)
694 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
695 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
696 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
697 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
698 ConnectionErrorFactory.ThrowConnectionException(ret);
704 throw new ArgumentException("Profile type is not cellular");
710 throw new ArgumentNullException("Profile is null");
714 internal void RemoveProfile(ConnectionProfile profile)
716 Log.Debug(Globals.LogTag, "RemoveProfile");
719 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
720 if ((ConnectionError)ret != ConnectionError.None)
722 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
723 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
724 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
725 ConnectionErrorFactory.ThrowConnectionException(ret);
731 throw new ArgumentNullException("Profile is null");
735 internal void UpdateProfile(ConnectionProfile profile)
737 Log.Info(Globals.LogTag, "UpdateProfile");
740 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
741 if ((ConnectionError)ret != ConnectionError.None)
743 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
744 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
745 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
746 ConnectionErrorFactory.ThrowConnectionException(ret);
752 throw new ArgumentNullException("Profile is null");
756 internal ConnectionProfile GetCurrentProfile()
758 Log.Info(Globals.LogTag, "GetCurrentProfile");
759 IntPtr ProfileHandle;
760 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
761 if ((ConnectionError)ret != ConnectionError.None)
763 if ((ConnectionError)ret == ConnectionError.NoConnection)
765 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
770 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
771 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");
772 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
773 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
774 ConnectionErrorFactory.ThrowConnectionException(ret);
778 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
782 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
784 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
785 IntPtr ProfileHandle;
786 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
787 if ((ConnectionError)ret != ConnectionError.None)
789 Log.Error(Globals.LogTag, "Error: " + ret);
790 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
791 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
792 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
793 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
794 ConnectionErrorFactory.ThrowConnectionException(ret);
797 CellularProfile Profile = new CellularProfile(ProfileHandle);
801 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
803 Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
806 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
807 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
809 Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
810 if (Result != ConnectionError.None)
812 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
813 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result));
817 task.SetResult(true);
823 Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
824 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
825 if ((ConnectionError)ret != ConnectionError.None)
827 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
828 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
829 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
830 ConnectionErrorFactory.ThrowConnectionException(ret);
838 throw new ArgumentNullException("Profile is null");
843 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
845 Log.Debug(Globals.LogTag, "GetProfileListAsync");
846 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
848 List<ConnectionProfile> Result = new List<ConnectionProfile>();
850 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
851 if ((ConnectionError)ret != ConnectionError.None)
853 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
854 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");
855 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
856 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
857 ConnectionErrorFactory.ThrowConnectionException(ret);
860 while (Interop.Connection.HasNextProfileIterator(iterator))
864 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
865 Interop.ConnectionProfile.Clone(out profileH, nextH);
868 Interop.ConnectionProfile.GetType(profileH, out profileType);
870 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
872 WiFiProfile cur = new WiFiProfile(profileH);
875 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
877 CellularProfile cur = new CellularProfile(profileH);
881 ConnectionProfile cur = new ConnectionProfile(profileH);
885 Interop.Connection.DestroyProfileIterator(iterator);
886 task.SetResult(Result);
890 internal Task OpenProfileAsync(ConnectionProfile profile)
892 Log.Info(Globals.LogTag, "OpenProfileAsync");
895 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
896 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
897 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
899 Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
900 if (Result != ConnectionError.None)
902 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
903 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result));
907 task.SetResult(true);
913 Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
914 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
915 if ((ConnectionError)ret != ConnectionError.None)
917 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
918 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
919 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
920 ConnectionErrorFactory.ThrowConnectionException(ret);
929 throw new ArgumentNullException("Profile is null");
933 internal Task CloseProfileAsync(ConnectionProfile profile)
935 Log.Info(Globals.LogTag, "CloseProfileAsync");
938 Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
939 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
940 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
942 Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
943 if (Result != ConnectionError.None)
945 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
946 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result));
950 task.SetResult(true);
956 Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
957 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
958 if ((ConnectionError)ret != ConnectionError.None)
960 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
961 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
962 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
963 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
964 ConnectionErrorFactory.ThrowConnectionException(ret);
973 throw new ArgumentNullException("Profile is null");