2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
21 using System.Threading.Tasks;
22 using System.Runtime.InteropServices;
23 using System.Collections;
24 using System.Threading;
25 using Tizen.Applications;
27 namespace Tizen.Network.Connection
31 private IntPtr Handle;
36 _tid = Thread.CurrentThread.ManagedThreadId;
37 Log.Info(Globals.LogTag, "PInvoke connection_create for Thread " + _tid);
38 int ret = Interop.Connection.Create(out Handle);
39 Log.Info(Globals.LogTag, "Handle: " + Handle);
40 if(ret != (int)ConnectionError.None)
42 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth " + "http://tizen.org/feature/network.ethernet");
43 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
44 ConnectionErrorFactory.ThrowConnectionException(ret);
53 internal IntPtr GetHandle()
55 Log.Debug(Globals.LogTag, "handleholder handle = " + Handle);
59 private void Destroy()
62 Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + _tid);
63 Interop.Connection.Destroy(Handle);
64 if (Handle != IntPtr.Zero)
71 internal class ConnectionInternalManager
73 private static readonly Lazy<ConnectionInternalManager> s_instance =
74 new Lazy<ConnectionInternalManager>(() => new ConnectionInternalManager());
76 private EventHandler<ConnectionTypeEventArgs> _ConnectionTypeChanged = null;
77 private EventHandler<AddressEventArgs> _IPAddressChanged = null;
78 private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
79 private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
81 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
82 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
83 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
84 private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
86 private Dictionary<IntPtr, Interop.Connection.ConnectionCallback> _callback_map =
87 new Dictionary<IntPtr, Interop.Connection.ConnectionCallback>();
88 private int _requestId = 0;
90 internal static ConnectionInternalManager Instance
94 return s_instance.Value;
98 private HandleHolder _handleHolder;
100 private ConnectionInternalManager()
102 _handleHolder = new HandleHolder();
103 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
106 ~ConnectionInternalManager()
111 internal IntPtr GetHandle()
113 return _handleHolder.GetHandle();
116 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
120 if (_ConnectionTypeChanged == null)
124 ConnectionTypeChangedStart();
128 Log.Error(Globals.LogTag, "Exception on adding ConnectionTypeChanged\n" + e.ToString());
132 _ConnectionTypeChanged += value;
136 _ConnectionTypeChanged -= value;
137 if (_ConnectionTypeChanged == null)
141 ConnectionTypeChangedStop();
145 Log.Error(Globals.LogTag, "Exception on removing ConnectionTypeChanged\n" + e.ToString());
151 private void ConnectionTypeChangedStart()
153 Log.Info(Globals.LogTag, "Register ConnectionTypeChanged");
154 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
156 if (_ConnectionTypeChanged != null)
158 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
162 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
163 if ((ConnectionError)ret != ConnectionError.None)
165 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
166 ConnectionErrorFactory.ThrowConnectionException(ret);
170 private void ConnectionTypeChangedStop()
172 Log.Info(Globals.LogTag, "Unregister ConnectionTypeChanged");
173 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
174 if ((ConnectionError)ret != ConnectionError.None)
176 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
177 ConnectionErrorFactory.ThrowConnectionException(ret);
181 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
185 if (_EthernetCableStateChanged == null)
189 EthernetCableStateChangedStart();
193 Log.Error(Globals.LogTag, "Exception on adding EthernetCableStateChanged\n" + e.ToString());
197 _EthernetCableStateChanged += value;
201 _EthernetCableStateChanged -= value;
202 if (_EthernetCableStateChanged == null)
206 EthernetCableStateChangedStop();
210 Log.Error(Globals.LogTag, "Exception on removing EthernetCableStateChanged\n" + e.ToString());
216 private void EthernetCableStateChangedStart()
218 Log.Info(Globals.LogTag, "Register EthernetCableStateChanged");
219 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
221 if (_EthernetCableStateChanged != null)
223 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
226 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
227 _ethernetCableStateChangedCallback, IntPtr.Zero);
228 if ((ConnectionError)ret != ConnectionError.None)
230 Log.Error(Globals.LogTag,
231 "It failed to register ethernet cable state changed callback, " +
232 (ConnectionError)ret);
233 ConnectionErrorFactory.ThrowConnectionException(ret);
237 private void EthernetCableStateChangedStop()
239 Log.Info(Globals.LogTag, "Unregister EthernetCableStateChanged");
240 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
241 if ((ConnectionError)ret != ConnectionError.None)
243 Log.Error(Globals.LogTag,
244 "It failed to unregister ethernet cable state changed callback, " +
245 (ConnectionError)ret);
246 ConnectionErrorFactory.ThrowConnectionException(ret);
250 internal event EventHandler<AddressEventArgs> IPAddressChanged
254 if (_IPAddressChanged == null)
258 IPAddressChangedStart();
262 Log.Error(Globals.LogTag, "Exception on adding IPAddressChanged\n" + e.ToString());
266 _IPAddressChanged += value;
271 _IPAddressChanged -= value;
272 if (_IPAddressChanged == null)
276 IPAddressChangedStop();
280 Log.Error(Globals.LogTag, "Exception on removing IPAddressChanged\n" + e.ToString());
286 private void IPAddressChangedStart()
288 Log.Info(Globals.LogTag, "Register IPAddressChanged");
289 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
291 if (_IPAddressChanged != null)
293 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
294 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
296 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
298 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
303 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
304 if ((ConnectionError)ret != ConnectionError.None)
306 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
310 private void IPAddressChangedStop()
312 Log.Info(Globals.LogTag, "Unregister IPAddressChanged");
313 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
314 if ((ConnectionError)ret != ConnectionError.None)
316 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
320 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
324 if (_ProxyAddressChanged == null)
328 ProxyAddressChangedStart();
332 Log.Error(Globals.LogTag, "Exception on adding ProxyAddressChanged\n" + e.ToString());
336 _ProxyAddressChanged += value;
340 _ProxyAddressChanged -= value;
341 if (_ProxyAddressChanged == null)
345 ProxyAddressChangedStop();
349 Log.Error(Globals.LogTag, "Exception on removing ProxyAddressChanged\n" + e.ToString());
355 private void ProxyAddressChangedStart()
357 Log.Info(Globals.LogTag, "Register ProxyAddressChanged");
358 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
360 if (_ProxyAddressChanged != null)
362 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
363 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
365 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
367 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
372 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
373 if ((ConnectionError)ret != ConnectionError.None)
375 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
379 private void ProxyAddressChangedStop()
381 Log.Info(Globals.LogTag, "Unregister ProxyAddressChanged");
382 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
383 if ((ConnectionError)ret != ConnectionError.None)
385 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
389 private void UnregisterEvents()
391 if (_ConnectionTypeChanged != null)
393 ConnectionTypeChangedStop();
395 if (_IPAddressChanged != null)
397 IPAddressChangedStop();
399 if (_EthernetCableStateChanged != null)
401 EthernetCableStateChangedStop();
403 if (_ProxyAddressChanged != null)
405 ProxyAddressChangedStop();
409 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
411 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
414 internal bool HasNext(IntPtr iterator)
416 return Interop.Connection.HasNextProfileIterator(iterator);
419 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
421 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
424 internal int DestoryProfileIterator(IntPtr iterator)
426 return Interop.Connection.DestroyProfileIterator(iterator);
429 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
431 Log.Info(Globals.LogTag, "GetIPAddress " + family);
433 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
434 if ((ConnectionError)ret != ConnectionError.None)
436 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
437 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");
438 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
439 ConnectionErrorFactory.ThrowConnectionException(ret);
442 string result = Marshal.PtrToStringAnsi(ip);
443 Interop.Libc.Free(ip);
444 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
445 if (result.Length == 0)
447 if (family == AddressFamily.IPv4)
448 return System.Net.IPAddress.Parse("0.0.0.0");
450 return System.Net.IPAddress.Parse("::");
452 return System.Net.IPAddress.Parse(result);
455 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
457 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
458 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
459 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
461 if (ipv6Address != IntPtr.Zero)
463 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
464 if (ipv6.Length == 0)
465 ipList.Add(System.Net.IPAddress.Parse("::"));
467 ipList.Add(System.Net.IPAddress.Parse(ipv6));
473 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
474 if (ret != (int)ConnectionError.None)
476 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
477 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");
478 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
479 ConnectionErrorFactory.ThrowConnectionException(ret);
485 internal string GetProxy(AddressFamily family)
487 Log.Debug(Globals.LogTag, "GetProxy " + family);
489 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
490 if ((ConnectionError)ret != ConnectionError.None)
492 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
493 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");
494 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
495 ConnectionErrorFactory.ThrowConnectionException(ret);
498 string result = Marshal.PtrToStringAnsi(ip);
499 Interop.Libc.Free(ip);
503 internal string GetMacAddress(ConnectionType type)
505 Log.Info(Globals.LogTag, "GetMacAddress " + type);
507 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
508 if ((ConnectionError)ret != ConnectionError.None)
510 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
511 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
512 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
513 ConnectionErrorFactory.ThrowConnectionException(ret);
516 string result = Marshal.PtrToStringAnsi(mac);
517 Interop.Libc.Free(mac);
521 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
523 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
525 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
526 (int)statisticsType, out size);
527 if ((ConnectionError)ret != ConnectionError.None)
529 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
530 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
531 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
532 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
533 ConnectionErrorFactory.ThrowConnectionException(ret);
538 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
540 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
541 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
542 (int)statisticsType);
543 if ((ConnectionError)ret != ConnectionError.None)
545 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
546 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
547 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
548 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
549 ConnectionErrorFactory.ThrowConnectionException(ret);
553 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
555 if (interfaceName != null && address != null && gateway != null)
557 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
558 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
559 if ((ConnectionError)ret != ConnectionError.None)
561 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
562 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");
563 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.route)");
564 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
565 ConnectionErrorFactory.ThrowConnectionException(ret);
571 throw new ArgumentNullException("Arguments are null");
575 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
577 if (interfaceName != null && address != null && gateway != null)
579 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
580 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
581 if ((ConnectionError)ret != ConnectionError.None)
583 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
584 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");
585 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.route)");
586 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
587 ConnectionErrorFactory.ThrowConnectionException(ret);
593 throw new ArgumentNullException("Arguments are null");
597 internal ConnectionType ConnectionType
601 Log.Info(Globals.LogTag, "get ConnectionType");
603 int ret = Interop.Connection.GetType(GetHandle(), out type);
604 if ((ConnectionError)ret != ConnectionError.None)
606 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
607 ConnectionErrorFactory.ThrowConnectionException(ret);
609 return (ConnectionType)type;
613 internal CellularState CellularState
617 Log.Info(Globals.LogTag, "get CellularState");
619 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
620 if ((ConnectionError)ret != ConnectionError.None)
622 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
623 ConnectionErrorFactory.ThrowConnectionException(ret);
625 return (CellularState)type;
629 internal ConnectionState WiFiState
633 Log.Info(Globals.LogTag, "get WiFiState");
635 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
636 if ((ConnectionError)ret != ConnectionError.None)
638 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
639 ConnectionErrorFactory.ThrowConnectionException(ret);
641 return (ConnectionState)type;
645 internal ConnectionState BluetoothState
649 Log.Info(Globals.LogTag, "get BluetoothState");
651 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
652 if ((ConnectionError)ret != ConnectionError.None)
654 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
655 ConnectionErrorFactory.ThrowConnectionException(ret);
657 return (ConnectionState)type;
661 internal ConnectionState EthernetState
665 Log.Info(Globals.LogTag, "get ConnectionType");
667 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
668 if ((ConnectionError)ret != ConnectionError.None)
670 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
671 ConnectionErrorFactory.ThrowConnectionException(ret);
673 return (ConnectionState)type;
677 internal EthernetCableState EthernetCableState
681 Log.Info(Globals.LogTag, "get EthernetCableState");
683 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
684 if ((ConnectionError)ret != ConnectionError.None)
686 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
687 ConnectionErrorFactory.ThrowConnectionException(ret);
689 return (EthernetCableState)type;
693 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
695 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
698 IntPtr handle = IntPtr.Zero;
699 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
700 if ((ConnectionError)ret != ConnectionError.None)
702 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
703 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
704 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
705 ConnectionErrorFactory.ThrowConnectionException(ret);
713 throw new ArgumentNullException("Keyword is null");
717 internal void AddCellularProfile(CellularProfile profile)
720 Log.Debug(Globals.LogTag, "AddCellularProfile");
723 if (profile.Type == ConnectionProfileType.Cellular)
725 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
726 if ((ConnectionError)ret != ConnectionError.None)
728 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
729 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
730 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
731 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
732 ConnectionErrorFactory.ThrowConnectionException(ret);
738 throw new ArgumentException("Profile type is not cellular");
744 throw new ArgumentNullException("Profile is null");
748 internal void RemoveProfile(ConnectionProfile profile)
750 Log.Debug(Globals.LogTag, "RemoveProfile");
753 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
754 if ((ConnectionError)ret != ConnectionError.None)
756 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
757 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
758 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
759 ConnectionErrorFactory.ThrowConnectionException(ret);
765 throw new ArgumentNullException("Profile is null");
769 internal void UpdateProfile(ConnectionProfile profile)
771 Log.Info(Globals.LogTag, "UpdateProfile");
774 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
775 if ((ConnectionError)ret != ConnectionError.None)
777 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
778 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
779 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
780 ConnectionErrorFactory.ThrowConnectionException(ret);
786 throw new ArgumentNullException("Profile is null");
790 internal ConnectionProfile GetCurrentProfile()
792 Log.Info(Globals.LogTag, "GetCurrentProfile");
793 IntPtr ProfileHandle;
794 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
795 if ((ConnectionError)ret != ConnectionError.None)
797 if ((ConnectionError)ret == ConnectionError.NoConnection)
799 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
802 else if ((ConnectionError)ret == ConnectionError.InvalidParameter)
804 throw new InvalidOperationException("Invalid handle");
808 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
809 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");
810 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
811 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
812 ConnectionErrorFactory.ThrowConnectionException(ret);
816 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
820 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
822 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
823 IntPtr ProfileHandle;
824 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
825 if ((ConnectionError)ret != ConnectionError.None)
827 Log.Error(Globals.LogTag, "Error: " + ret);
828 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
829 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
830 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
831 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
832 ConnectionErrorFactory.ThrowConnectionException(ret);
835 CellularProfile Profile = new CellularProfile(ProfileHandle);
839 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
841 Log.Info(Globals.LogTag, "SetDefaultCellularProfile");
844 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
848 id = (IntPtr)_requestId++;
849 _callback_map[id] = (error, key) =>
851 Log.Info(Globals.LogTag, "SetDefaultCellularProfile done " + profile.Name);
852 if (error != ConnectionError.None)
854 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + error);
855 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + error));
859 task.SetResult(true);
863 _callback_map.Remove(key);
868 Log.Info(Globals.LogTag, "Interop.Connection.SetDefaultCellularServiceProfileAsync " + profile.Name);
871 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, _callback_map[id], id);
873 if ((ConnectionError)ret != ConnectionError.None)
875 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
876 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
877 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
878 ConnectionErrorFactory.ThrowConnectionException(ret);
880 } catch (Exception e)
882 Log.Error(Globals.LogTag, "Exception on SetDefaultCellularServiceProfileAsync\n" + e.ToString());
883 task.SetException(e);
890 throw new ArgumentNullException("Profile is null");
895 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
897 Log.Debug(Globals.LogTag, "GetProfileListAsync");
898 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
900 List<ConnectionProfile> Result = new List<ConnectionProfile>();
902 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
903 if ((ConnectionError)ret != ConnectionError.None)
905 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
906 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");
907 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
908 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
909 ConnectionErrorFactory.ThrowConnectionException(ret);
912 while (Interop.Connection.HasNextProfileIterator(iterator))
916 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
917 Interop.ConnectionProfile.Clone(out profileH, nextH);
920 Interop.ConnectionProfile.GetType(profileH, out profileType);
922 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
924 WiFiProfile cur = new WiFiProfile(profileH);
927 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
929 CellularProfile cur = new CellularProfile(profileH);
933 ConnectionProfile cur = new ConnectionProfile(profileH);
937 Interop.Connection.DestroyProfileIterator(iterator);
938 task.SetResult(Result);
942 internal Task OpenProfileAsync(ConnectionProfile profile)
944 Log.Info(Globals.LogTag, "OpenProfileAsync");
947 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
948 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
952 id = (IntPtr)_requestId++;
953 _callback_map[id] = (error, key) =>
955 Log.Info(Globals.LogTag, "OpenProfileAsync done " + profile.Name);
956 if (error != ConnectionError.None)
958 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + error);
959 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + error));
963 task.SetResult(true);
967 _callback_map.Remove(key);
972 Log.Info(Globals.LogTag, "Interop.Connection.OpenProfile " + profile.Name);
975 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
976 if ((ConnectionError)ret != ConnectionError.None)
978 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
979 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
980 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
981 ConnectionErrorFactory.ThrowConnectionException(ret);
986 Log.Error(Globals.LogTag, "Exception on OpenProfile\n" + e.ToString());
987 task.SetException(e);
995 throw new ArgumentNullException("Profile is null");
999 internal Task CloseProfileAsync(ConnectionProfile profile)
1001 Log.Info(Globals.LogTag, "CloseProfileAsync");
1002 if (profile != null)
1004 Log.Info(Globals.LogTag, "CloseProfileAsync " + profile.Name);
1005 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
1007 lock (_callback_map)
1009 id = (IntPtr)_requestId++;
1010 _callback_map[id] = (error, key) =>
1012 Log.Info(Globals.LogTag, "CloseProfileAsync done " + profile.Name);
1013 if (error!= ConnectionError.None)
1015 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + error);
1016 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + error));
1020 task.SetResult(true);
1022 lock (_callback_map)
1024 _callback_map.Remove(key);
1029 Log.Info(Globals.LogTag, "Interop.Connection.CloseProfile " + profile.Name);
1032 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, _callback_map[id], id);
1033 if ((ConnectionError)ret != ConnectionError.None)
1035 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
1036 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
1037 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
1038 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
1039 ConnectionErrorFactory.ThrowConnectionException(ret);
1044 Log.Error(Globals.LogTag, "Exception on CloseProfile\n" + e.ToString());
1045 task.SetException(e);
1053 throw new ArgumentNullException("Profile is null");