2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
21 using System.Threading.Tasks;
22 using System.Runtime.InteropServices;
23 using System.Collections;
24 using System.Threading;
26 namespace Tizen.Network.Connection
28 class HandleHolder : IDisposable
30 private IntPtr Handle;
31 private bool disposed = false;
35 Log.Debug(Globals.LogTag, "Handle: " + Handle);
36 int ret = Interop.Connection.Create(out Handle);
37 if(ret != (int)ConnectionError.None)
39 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");
40 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
41 ConnectionErrorFactory.ThrowConnectionException(ret);
50 internal IntPtr GetHandle()
52 Log.Debug(Globals.LogTag, "handleholder handle = " + Handle);
58 GC.SuppressFinalize(this);
61 private void Dispose(bool disposing)
63 Log.Debug(Globals.LogTag, ">>> HandleHolder Dispose with " + disposing);
64 Log.Debug(Globals.LogTag, ">>> Handle: " + Handle);
70 // Free managed objects.
76 private void Destroy()
78 Interop.Connection.Destroy(Handle);
79 if (Handle != IntPtr.Zero)
86 internal class ConnectionInternalManager
88 private bool disposed = false;
89 private static ConnectionInternalManager s_instance = null;
91 private EventHandler<ConnectionTypeEventArgs> _ConnectionTypeChanged = null;
92 private EventHandler<AddressEventArgs> _IPAddressChanged = null;
93 private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
94 private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
96 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
97 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
98 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
100 internal static ConnectionInternalManager Instance
104 if (s_instance == null)
106 s_instance = new ConnectionInternalManager();
113 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
115 Log.Info(Globals.LogTag, "In threadlocal delegate");
116 return new HandleHolder();
119 private ConnectionInternalManager()
124 ~ConnectionInternalManager()
129 public void Dispose()
132 GC.SuppressFinalize(this);
135 private void Dispose(bool disposing)
137 Log.Debug(Globals.LogTag, ">>> ConnectionInternalManager Dispose with disposing " + disposing + ", disposed " + disposed);
138 Log.Debug(Globals.LogTag, ">>> Handle: " + GetHandle());
144 // Free managed objects.
151 internal IntPtr GetHandle()
153 return s_threadName.Value.GetHandle();
156 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
160 if (_ConnectionTypeChanged == null)
162 ConnectionTypeChangedStart();
165 _ConnectionTypeChanged += value;
169 _ConnectionTypeChanged -= value;
170 if (_ConnectionTypeChanged == null)
172 ConnectionTypeChangedStop();
177 private void ConnectionTypeChangedStart()
179 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
181 if (_ConnectionTypeChanged != null)
183 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
187 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
188 if ((ConnectionError)ret != ConnectionError.None)
190 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
191 ConnectionErrorFactory.ThrowConnectionException(ret);
195 private void ConnectionTypeChangedStop()
197 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
198 if ((ConnectionError)ret != ConnectionError.None)
200 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
201 ConnectionErrorFactory.ThrowConnectionException(ret);
205 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
209 if (_EthernetCableStateChanged == null)
211 EthernetCableStateChangedStart();
213 _EthernetCableStateChanged += value;
217 _EthernetCableStateChanged -= value;
218 if (_EthernetCableStateChanged == null)
220 EthernetCableStateChangedStop();
225 private void EthernetCableStateChangedStart()
227 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(), EthernetCableStateChangedCallback, IntPtr.Zero);
228 if ((ConnectionError)ret != ConnectionError.None)
230 Log.Error(Globals.LogTag, "It failed to register ethernet cable state changed callback, " + (ConnectionError)ret);
231 ConnectionErrorFactory.ThrowConnectionException(ret);
235 private void EthernetCableStateChangedStop()
237 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
238 if ((ConnectionError)ret != ConnectionError.None)
240 Log.Error(Globals.LogTag, "It failed to unregister ethernet cable state changed callback, " + (ConnectionError)ret);
241 ConnectionErrorFactory.ThrowConnectionException(ret);
245 private void EthernetCableStateChangedCallback(EthernetCableState state, IntPtr user_data)
247 if (_EthernetCableStateChanged != null)
249 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
253 internal event EventHandler<AddressEventArgs> IPAddressChanged
257 if (_IPAddressChanged == null)
259 IPAddressChangedStart();
261 _IPAddressChanged += value;
266 _IPAddressChanged -= value;
267 if (_IPAddressChanged == null)
269 IPAddressChangedStop();
274 private void IPAddressChangedStart()
276 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
278 if (_IPAddressChanged != null)
280 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
281 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
283 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
285 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
290 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
291 if ((ConnectionError)ret != ConnectionError.None)
293 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
297 private void IPAddressChangedStop()
299 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
300 if ((ConnectionError)ret != ConnectionError.None)
302 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
306 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
310 //Console.WriteLine("ProxyAddressChanged Add **");
311 if (_ProxyAddressChanged == null)
313 ProxyAddressChangedStart();
316 _ProxyAddressChanged += value;
320 //Console.WriteLine("ProxyAddressChanged Remove");
321 _ProxyAddressChanged -= value;
322 if (_ProxyAddressChanged == null)
324 ProxyAddressChangedStop();
329 private void ProxyAddressChangedStart()
331 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
333 if (_ProxyAddressChanged != null)
335 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
336 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
338 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
340 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
345 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
346 if ((ConnectionError)ret != ConnectionError.None)
348 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
352 private void ProxyAddressChangedStop()
354 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
355 if ((ConnectionError)ret != ConnectionError.None)
357 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
361 private void UnregisterEvents()
363 if (_ConnectionTypeChanged != null)
365 ConnectionTypeChangedStop();
367 if (_IPAddressChanged != null)
369 IPAddressChangedStop();
371 if (_EthernetCableStateChanged != null)
373 EthernetCableStateChangedStop();
375 if (_ProxyAddressChanged != null)
377 ProxyAddressChangedStop();
381 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
383 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
386 internal bool HasNext(IntPtr iterator)
388 return Interop.Connection.HasNextProfileIterator(iterator);
391 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
393 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
396 internal int DestoryProfileIterator(IntPtr iterator)
398 return Interop.Connection.DestroyProfileIterator(iterator);
401 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
403 Log.Debug(Globals.LogTag, "GetIPAddress " + family);
405 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
406 if ((ConnectionError)ret != ConnectionError.None)
408 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
409 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");
410 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
411 ConnectionErrorFactory.ThrowConnectionException(ret);
414 string result = Marshal.PtrToStringAnsi(ip);
415 Interop.Libc.Free(ip);
416 return System.Net.IPAddress.Parse(result);;
419 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
421 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
422 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
423 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
425 if (ipv6Address != IntPtr.Zero)
427 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
428 ipList.Add(System.Net.IPAddress.Parse(ipv6));
434 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
435 if (ret != (int)ConnectionError.None)
437 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (ConnectionError)ret);
438 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");
439 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
440 ConnectionErrorFactory.ThrowConnectionException(ret);
446 internal string GetProxy(AddressFamily family)
448 Log.Debug(Globals.LogTag, "GetProxy " + family);
450 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
451 if ((ConnectionError)ret != ConnectionError.None)
453 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
454 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");
455 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
456 ConnectionErrorFactory.ThrowConnectionException(ret);
459 string result = Marshal.PtrToStringAnsi(ip);
460 Interop.Libc.Free(ip);
464 internal string GetMacAddress(ConnectionType type)
466 Log.Debug(Globals.LogTag, "GetMacAddress " + type);
468 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
469 if ((ConnectionError)ret != ConnectionError.None)
471 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
472 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
473 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
474 ConnectionErrorFactory.ThrowConnectionException(ret);
477 string result = Marshal.PtrToStringAnsi(mac);
478 Interop.Libc.Free(mac);
482 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
484 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
486 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
487 (int)statisticsType, out size);
488 if ((ConnectionError)ret != ConnectionError.None)
490 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
491 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
492 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
493 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
494 ConnectionErrorFactory.ThrowConnectionException(ret);
499 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
501 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
502 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
503 (int)statisticsType);
504 if ((ConnectionError)ret != ConnectionError.None)
506 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
507 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
508 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
509 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
510 ConnectionErrorFactory.ThrowConnectionException(ret);
514 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
516 if (interfaceName != null && address != null && gateway != null)
518 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
519 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
520 if ((ConnectionError)ret != ConnectionError.None)
522 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
523 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");
524 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
525 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
526 ConnectionErrorFactory.ThrowConnectionException(ret);
532 throw new ArgumentNullException("Arguments are null");
536 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
538 if (interfaceName != null && address != null && gateway != null)
540 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
541 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
542 if ((ConnectionError)ret != ConnectionError.None)
544 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
545 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");
546 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
547 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
548 ConnectionErrorFactory.ThrowConnectionException(ret);
554 throw new ArgumentNullException("Arguments are null");
558 internal ConnectionType ConnectionType
562 Log.Debug(Globals.LogTag, "get ConnectionType");
564 int ret = Interop.Connection.GetType(GetHandle(), out type);
565 if ((ConnectionError)ret != ConnectionError.None)
567 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
568 ConnectionErrorFactory.ThrowConnectionException(ret);
570 return (ConnectionType)type;
574 internal CellularState CellularState
578 Log.Debug(Globals.LogTag, "get CellularState");
580 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
581 if ((ConnectionError)ret != ConnectionError.None)
583 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
584 ConnectionErrorFactory.ThrowConnectionException(ret);
586 return (CellularState)type;
590 internal ConnectionState WiFiState
594 Log.Debug(Globals.LogTag, "get WiFiState");
596 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
597 if ((ConnectionError)ret != ConnectionError.None)
599 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
600 ConnectionErrorFactory.ThrowConnectionException(ret);
602 return (ConnectionState)type;
606 internal ConnectionState BluetoothState
610 Log.Debug(Globals.LogTag, "get BluetoothState");
612 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
613 if ((ConnectionError)ret != ConnectionError.None)
615 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
616 ConnectionErrorFactory.ThrowConnectionException(ret);
618 return (ConnectionState)type;
622 internal ConnectionState EthernetState
626 Log.Debug(Globals.LogTag, "get ConnectionType");
628 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
629 if ((ConnectionError)ret != ConnectionError.None)
631 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
632 ConnectionErrorFactory.ThrowConnectionException(ret);
634 return (ConnectionState)type;
638 internal EthernetCableState EthernetCableState
642 Log.Debug(Globals.LogTag, "get EthernetCableState");
644 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
645 if ((ConnectionError)ret != ConnectionError.None)
647 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
648 ConnectionErrorFactory.ThrowConnectionException(ret);
650 return (EthernetCableState)type;
654 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
656 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
659 IntPtr handle = IntPtr.Zero;
660 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
661 if ((ConnectionError)ret != ConnectionError.None)
663 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
664 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
665 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
666 ConnectionErrorFactory.ThrowConnectionException(ret);
674 throw new ArgumentNullException("Keyword is null");
678 internal void AddCellularProfile(CellularProfile profile)
681 Log.Debug(Globals.LogTag, "AddCellularProfile");
684 if (profile.Type == ConnectionProfileType.Cellular)
686 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
687 if ((ConnectionError)ret != ConnectionError.None)
689 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
690 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
691 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
692 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
693 ConnectionErrorFactory.ThrowConnectionException(ret);
699 throw new ArgumentException("Profile type is not cellular");
705 throw new ArgumentNullException("Profile is null");
709 internal void RemoveProfile(ConnectionProfile profile)
711 Log.Debug(Globals.LogTag, "RemoveProfile");
714 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
715 if ((ConnectionError)ret != ConnectionError.None)
717 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
718 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
719 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
720 ConnectionErrorFactory.ThrowConnectionException(ret);
726 throw new ArgumentNullException("Profile is null");
730 internal void UpdateProfile(ConnectionProfile profile)
732 Log.Debug(Globals.LogTag, "UpdateProfile");
735 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
736 if ((ConnectionError)ret != ConnectionError.None)
738 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
739 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
740 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
741 ConnectionErrorFactory.ThrowConnectionException(ret);
747 throw new ArgumentNullException("Profile is null");
751 internal ConnectionProfile GetCurrentProfile()
753 Log.Debug(Globals.LogTag, "GetCurrentProfile");
754 IntPtr ProfileHandle;
755 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
756 if ((ConnectionError)ret != ConnectionError.None)
758 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
759 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");
760 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
761 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
762 ConnectionErrorFactory.ThrowConnectionException(ret);
765 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
769 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
771 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
772 IntPtr ProfileHandle;
773 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
774 if ((ConnectionError)ret != ConnectionError.None)
776 Log.Error(Globals.LogTag, "Error: " + ret);
777 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
778 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
779 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
780 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
781 ConnectionErrorFactory.ThrowConnectionException(ret);
784 CellularProfile Profile = new CellularProfile(ProfileHandle);
788 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
790 Log.Debug(Globals.LogTag, "SetDefaultCellularProfile");
793 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
794 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
796 if (Result != ConnectionError.None)
798 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
799 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result));
802 task.SetResult(true);
805 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
806 if ((ConnectionError)ret != ConnectionError.None)
808 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
809 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
810 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
811 ConnectionErrorFactory.ThrowConnectionException(ret);
819 throw new ArgumentNullException("Profile is null");
824 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
826 Log.Debug(Globals.LogTag, "GetProfileListAsync");
827 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
829 List<ConnectionProfile> Result = new List<ConnectionProfile>();
831 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
832 if ((ConnectionError)ret != ConnectionError.None)
834 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
835 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");
836 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
837 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
838 ConnectionErrorFactory.ThrowConnectionException(ret);
841 while (Interop.Connection.HasNextProfileIterator(iterator))
845 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
846 Interop.ConnectionProfile.Clone(out profileH, nextH);
849 Interop.ConnectionProfile.GetType(profileH, out profileType);
851 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
853 WiFiProfile cur = new WiFiProfile(profileH);
856 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
858 CellularProfile cur = new CellularProfile(profileH);
862 ConnectionProfile cur = new ConnectionProfile(profileH);
866 Interop.Connection.DestroyProfileIterator(iterator);
867 task.SetResult(Result);
871 internal Task OpenProfileAsync(ConnectionProfile profile)
873 Log.Debug(Globals.LogTag, "OpenProfileAsync");
876 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
877 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
878 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
880 Log.Debug(Globals.LogTag, "Connected " + profile.Name);
881 if (Result != ConnectionError.None)
883 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
884 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result));
887 task.SetResult(true);
890 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
891 if ((ConnectionError)ret != ConnectionError.None)
893 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
894 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
895 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
896 ConnectionErrorFactory.ThrowConnectionException(ret);
904 throw new ArgumentNullException("Profile is null");
908 internal Task CloseProfileAsync(ConnectionProfile profile)
910 Log.Debug(Globals.LogTag, "CloseProfileAsync");
913 Log.Debug(Globals.LogTag, "CloseProfileAsync " + profile.Name);
914 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
915 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
917 if (Result != ConnectionError.None)
919 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
920 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result));
923 task.SetResult(true);
926 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
927 if ((ConnectionError)ret != ConnectionError.None)
929 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
930 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
931 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
932 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
933 ConnectionErrorFactory.ThrowConnectionException(ret);
941 throw new ArgumentNullException("Profile is null");