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(_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 private Dictionary<IntPtr, Interop.Connection.ConnectionCallback> _callback_map =
89 new Dictionary<IntPtr, Interop.Connection.ConnectionCallback>();
90 private int _requestId = 0;
92 internal static ConnectionInternalManager Instance
96 return s_instance.Value;
100 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
102 Log.Info(Globals.LogTag, "In threadlocal delegate");
103 return new HandleHolder();
106 private ConnectionInternalManager()
108 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
111 ~ConnectionInternalManager()
116 internal IntPtr GetHandle()
118 return s_threadName.Value.GetHandle();
121 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
127 if (_ConnectionTypeChanged == null)
131 ConnectionTypeChangedStart();
135 Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
139 _ConnectionTypeChanged += value;
146 _ConnectionTypeChanged -= value;
147 if (_ConnectionTypeChanged == null)
151 ConnectionTypeChangedStop();
155 Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
162 private void ConnectionTypeChangedStart()
164 Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
165 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
167 if (_ConnectionTypeChanged != null)
169 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
173 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
174 if ((ConnectionError)ret != ConnectionError.None)
176 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
177 ConnectionErrorFactory.ThrowConnectionException(ret);
181 private void ConnectionTypeChangedStop()
183 Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
184 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
185 if ((ConnectionError)ret != ConnectionError.None)
187 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
188 ConnectionErrorFactory.ThrowConnectionException(ret);
192 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
198 if (_EthernetCableStateChanged == null)
202 EthernetCableStateChangedStart();
206 Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
210 _EthernetCableStateChanged += value;
217 _EthernetCableStateChanged -= value;
218 if (_EthernetCableStateChanged == null)
222 EthernetCableStateChangedStop();
226 Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
233 private void EthernetCableStateChangedStart()
235 Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
236 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
238 if (_EthernetCableStateChanged != null)
240 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
243 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
244 _ethernetCableStateChangedCallback, IntPtr.Zero);
245 if ((ConnectionError)ret != ConnectionError.None)
247 Log.Error(Globals.LogTag,
248 "It failed to register ethernet cable state changed callback, " +
249 (ConnectionError)ret);
250 ConnectionErrorFactory.ThrowConnectionException(ret);
254 private void EthernetCableStateChangedStop()
256 Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
257 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
258 if ((ConnectionError)ret != ConnectionError.None)
260 Log.Error(Globals.LogTag,
261 "It failed to unregister ethernet cable state changed callback, " +
262 (ConnectionError)ret);
263 ConnectionErrorFactory.ThrowConnectionException(ret);
267 internal event EventHandler<AddressEventArgs> IPAddressChanged
273 if (_IPAddressChanged == null)
277 IPAddressChangedStart();
281 Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
285 _IPAddressChanged += value;
293 _IPAddressChanged -= value;
294 if (_IPAddressChanged == null)
298 IPAddressChangedStop();
302 Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
309 private void IPAddressChangedStart()
311 Log.Info(Globals.LogTag, "Register IPAddressChanged");
312 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
314 if (_IPAddressChanged != null)
316 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
317 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
319 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
321 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
326 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
327 if ((ConnectionError)ret != ConnectionError.None)
329 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
333 private void IPAddressChangedStop()
335 Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
336 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
337 if ((ConnectionError)ret != ConnectionError.None)
339 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
343 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
349 if (_ProxyAddressChanged == null)
353 ProxyAddressChangedStart();
357 Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
361 _ProxyAddressChanged += value;
368 _ProxyAddressChanged -= value;
369 if (_ProxyAddressChanged == null)
373 ProxyAddressChangedStop();
377 Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
384 private void ProxyAddressChangedStart()
386 Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
387 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
389 if (_ProxyAddressChanged != null)
391 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
392 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
394 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
396 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
401 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
402 if ((ConnectionError)ret != ConnectionError.None)
404 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
408 private void ProxyAddressChangedStop()
410 Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
411 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
412 if ((ConnectionError)ret != ConnectionError.None)
414 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
418 private void UnregisterEvents()
420 if (_ConnectionTypeChanged != null)
422 ConnectionTypeChangedStop();
424 if (_IPAddressChanged != null)
426 IPAddressChangedStop();
428 if (_EthernetCableStateChanged != null)
430 EthernetCableStateChangedStop();
432 if (_ProxyAddressChanged != null)
434 ProxyAddressChangedStop();
438 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
440 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
443 internal bool HasNext(IntPtr iterator)
445 return Interop.Connection.HasNextProfileIterator(iterator);
448 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
450 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
453 internal int DestoryProfileIterator(IntPtr iterator)
455 return Interop.Connection.DestroyProfileIterator(iterator);
458 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
460 Log.Info(Globals.LogTag, "GetIPAddress " + family);
462 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
463 if ((ConnectionError)ret != ConnectionError.None)
465 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
466 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");
467 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
468 ConnectionErrorFactory.ThrowConnectionException(ret);
471 string result = Marshal.PtrToStringAnsi(ip);
472 Interop.Libc.Free(ip);
473 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
474 if (result.Length == 0)
476 if (family == AddressFamily.IPv4)
477 return System.Net.IPAddress.Parse("0.0.0.0");
479 return System.Net.IPAddress.Parse("::");
481 return System.Net.IPAddress.Parse(result);
484 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
486 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
487 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
488 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
490 if (ipv6Address != IntPtr.Zero)
492 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
493 if (ipv6.Length == 0)
494 ipList.Add(System.Net.IPAddress.Parse("::"));
496 ipList.Add(System.Net.IPAddress.Parse(ipv6));
502 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
503 if (ret != (int)ConnectionError.None)
505 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
506 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");
507 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
508 ConnectionErrorFactory.ThrowConnectionException(ret);
514 internal string GetProxy(AddressFamily family)
516 Log.Debug(Globals.LogTag, "GetProxy " + family);
518 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
519 if ((ConnectionError)ret != ConnectionError.None)
521 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
522 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");
523 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
524 ConnectionErrorFactory.ThrowConnectionException(ret);
527 string result = Marshal.PtrToStringAnsi(ip);
528 Interop.Libc.Free(ip);
532 internal string GetMacAddress(ConnectionType type)
534 Log.Info(Globals.LogTag, "GetMacAddress " + type);
536 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
537 if ((ConnectionError)ret != ConnectionError.None)
539 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
540 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
541 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
542 ConnectionErrorFactory.ThrowConnectionException(ret);
545 string result = Marshal.PtrToStringAnsi(mac);
546 Interop.Libc.Free(mac);
550 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
552 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
554 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
555 (int)statisticsType, out size);
556 if ((ConnectionError)ret != ConnectionError.None)
558 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
559 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
560 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
561 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
562 ConnectionErrorFactory.ThrowConnectionException(ret);
567 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
569 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
570 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
571 (int)statisticsType);
572 if ((ConnectionError)ret != ConnectionError.None)
574 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
575 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
576 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
577 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
578 ConnectionErrorFactory.ThrowConnectionException(ret);
582 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
584 if (interfaceName != null && address != null && gateway != null)
586 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
587 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
588 if ((ConnectionError)ret != ConnectionError.None)
590 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
591 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");
592 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
593 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
594 ConnectionErrorFactory.ThrowConnectionException(ret);
600 throw new ArgumentNullException("Arguments are null");
604 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
606 if (interfaceName != null && address != null && gateway != null)
608 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
609 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
610 if ((ConnectionError)ret != ConnectionError.None)
612 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
613 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");
614 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
615 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
616 ConnectionErrorFactory.ThrowConnectionException(ret);
622 throw new ArgumentNullException("Arguments are null");
626 internal ConnectionType ConnectionType
630 Log.Info(Globals.LogTag, "get ConnectionType");
632 int ret = Interop.Connection.GetType(GetHandle(), out type);
633 if ((ConnectionError)ret != ConnectionError.None)
635 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
636 ConnectionErrorFactory.ThrowConnectionException(ret);
638 return (ConnectionType)type;
642 internal CellularState CellularState
646 Log.Info(Globals.LogTag, "get CellularState");
648 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
649 if ((ConnectionError)ret != ConnectionError.None)
651 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
652 ConnectionErrorFactory.ThrowConnectionException(ret);
654 return (CellularState)type;
658 internal ConnectionState WiFiState
662 Log.Info(Globals.LogTag, "get WiFiState");
664 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
665 if ((ConnectionError)ret != ConnectionError.None)
667 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
668 ConnectionErrorFactory.ThrowConnectionException(ret);
670 return (ConnectionState)type;
674 internal ConnectionState BluetoothState
678 Log.Info(Globals.LogTag, "get BluetoothState");
680 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
681 if ((ConnectionError)ret != ConnectionError.None)
683 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
684 ConnectionErrorFactory.ThrowConnectionException(ret);
686 return (ConnectionState)type;
690 internal ConnectionState EthernetState
694 Log.Info(Globals.LogTag, "get ConnectionType");
696 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
697 if ((ConnectionError)ret != ConnectionError.None)
699 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
700 ConnectionErrorFactory.ThrowConnectionException(ret);
702 return (ConnectionState)type;
706 internal EthernetCableState EthernetCableState
710 Log.Info(Globals.LogTag, "get EthernetCableState");
712 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
713 if ((ConnectionError)ret != ConnectionError.None)
715 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
716 ConnectionErrorFactory.ThrowConnectionException(ret);
718 return (EthernetCableState)type;
722 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
724 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
727 IntPtr handle = IntPtr.Zero;
728 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
729 if ((ConnectionError)ret != ConnectionError.None)
731 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
732 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
733 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
734 ConnectionErrorFactory.ThrowConnectionException(ret);
742 throw new ArgumentNullException("Keyword is null");
746 internal void AddCellularProfile(CellularProfile profile)
749 Log.Debug(Globals.LogTag, "AddCellularProfile");
752 if (profile.Type == ConnectionProfileType.Cellular)
754 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
755 if ((ConnectionError)ret != ConnectionError.None)
757 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
758 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
759 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
760 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
761 ConnectionErrorFactory.ThrowConnectionException(ret);
767 throw new ArgumentException("Profile type is not cellular");
773 throw new ArgumentNullException("Profile is null");
777 internal void RemoveProfile(ConnectionProfile profile)
779 Log.Debug(Globals.LogTag, "RemoveProfile");
782 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
783 if ((ConnectionError)ret != ConnectionError.None)
785 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
786 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
787 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
788 ConnectionErrorFactory.ThrowConnectionException(ret);
794 throw new ArgumentNullException("Profile is null");
798 internal void UpdateProfile(ConnectionProfile profile)
800 Log.Info(Globals.LogTag, "UpdateProfile");
803 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
804 if ((ConnectionError)ret != ConnectionError.None)
806 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
807 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
808 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
809 ConnectionErrorFactory.ThrowConnectionException(ret);
815 throw new ArgumentNullException("Profile is null");
819 internal ConnectionProfile GetCurrentProfile()
821 Log.Info(Globals.LogTag, "GetCurrentProfile");
822 IntPtr ProfileHandle;
823 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
824 if ((ConnectionError)ret != ConnectionError.None)
826 if ((ConnectionError)ret == ConnectionError.NoConnection)
828 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
831 else if ((ConnectionError)ret == ConnectionError.InvalidParameter)
833 throw new InvalidOperationException("Invalid handle");
837 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
838 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");
839 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
840 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
841 ConnectionErrorFactory.ThrowConnectionException(ret);
845 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
849 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
851 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
852 IntPtr ProfileHandle;
853 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
854 if ((ConnectionError)ret != ConnectionError.None)
856 Log.Error(Globals.LogTag, "Error: " + ret);
857 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
858 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
859 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
860 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
861 ConnectionErrorFactory.ThrowConnectionException(ret);
864 CellularProfile Profile = new CellularProfile(ProfileHandle);
868 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
870 Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
873 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
877 id = (IntPtr)_requestId++;
878 _callback_map[id] = (error, key) =>
880 Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
881 if (error != ConnectionError.None)
883 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + error);
884 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + error));
888 task.SetResult(true);
892 _callback_map.Remove(key);
899 Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
902 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, _callback_map[id], id);
904 if ((ConnectionError)ret != ConnectionError.None)
906 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
907 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
908 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
909 ConnectionErrorFactory.ThrowConnectionException(ret);
911 } catch (Exception e)
913 Log.Error(Globals.LogTag, "Exception on SetDefaultCellularServiceProfileAsync\n" + e.ToString());
914 task.SetException(e);
922 throw new ArgumentNullException("Profile is null");
927 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
929 Log.Debug(Globals.LogTag, "GetProfileListAsync");
930 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
932 List<ConnectionProfile> Result = new List<ConnectionProfile>();
934 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
935 if ((ConnectionError)ret != ConnectionError.None)
937 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
938 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");
939 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
940 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
941 ConnectionErrorFactory.ThrowConnectionException(ret);
944 while (Interop.Connection.HasNextProfileIterator(iterator))
948 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
949 Interop.ConnectionProfile.Clone(out profileH, nextH);
952 Interop.ConnectionProfile.GetType(profileH, out profileType);
954 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
956 WiFiProfile cur = new WiFiProfile(profileH);
959 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
961 CellularProfile cur = new CellularProfile(profileH);
965 ConnectionProfile cur = new ConnectionProfile(profileH);
969 Interop.Connection.DestroyProfileIterator(iterator);
970 task.SetResult(Result);
974 internal Task OpenProfileAsync(ConnectionProfile profile)
976 Log.Info(Globals.LogTag, "OpenProfileAsync");
979 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
980 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
984 id = (IntPtr)_requestId++;
985 _callback_map[id] = (error, key) =>
987 Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
988 if (error != ConnectionError.None)
990 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + error);
991 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + error));
995 task.SetResult(true);
999 _callback_map.Remove(key);
1006 Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
1009 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
1010 if ((ConnectionError)ret != ConnectionError.None)
1012 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
1013 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1014 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1015 ConnectionErrorFactory.ThrowConnectionException(ret);
1020 Log.Error(Globals.LogTag, "Exception on OpenProfile\n" + e.ToString());
1021 task.SetException(e);
1030 throw new ArgumentNullException("Profile is null");
1034 internal Task CloseProfileAsync(ConnectionProfile profile)
1036 Log.Info(Globals.LogTag, "CloseProfileAsync");
1037 if (profile != null)
1039 Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
1040 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
1042 lock (_callback_map)
1044 id = (IntPtr)_requestId++;
1045 _callback_map[id] = (error, key) =>
1047 Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
1048 if (error!= ConnectionError.None)
1050 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + error);
1051 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + error));
1055 task.SetResult(true);
1057 lock (_callback_map)
1059 _callback_map.Remove(key);
1066 Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
1069 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
1070 if ((ConnectionError)ret != ConnectionError.None)
1072 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
1073 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1074 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
1075 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1076 ConnectionErrorFactory.ThrowConnectionException(ret);
1081 Log.Error(Globals.LogTag, "Exception on CloseProfile\n" + e.ToString());
1082 task.SetException(e);
1091 throw new ArgumentNullException("Profile is null");