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
30 private IntPtr Handle;
35 tid = Thread.CurrentThread.ManagedThreadId;
36 Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + tid);
37 int ret = Interop.Connection.Create(tid, out Handle);
38 Log.Info(Globals.LogTag, "Handle: " + Handle);
39 if(ret != (int)ConnectionError.None)
41 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");
42 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
43 ConnectionErrorFactory.ThrowConnectionException(ret);
52 internal IntPtr GetHandle()
54 Log.Debug(Globals.LogTag, "handleholder handle = " + Handle);
58 private void Destroy()
60 Log.Info(Globals.LogTag, "PInvoke connection_destroy for Thread " + tid);
61 Interop.Connection.Destroy(tid, Handle);
62 if (Handle != IntPtr.Zero)
69 internal class ConnectionInternalManager
71 private static readonly Lazy<ConnectionInternalManager> s_instance =
72 new Lazy<ConnectionInternalManager>(() => new ConnectionInternalManager());
74 private EventHandler<ConnectionTypeEventArgs> _ConnectionTypeChanged = null;
75 private EventHandler<AddressEventArgs> _IPAddressChanged = null;
76 private EventHandler<EthernetCableStateEventArgs> _EthernetCableStateChanged = null;
77 private EventHandler<AddressEventArgs> _ProxyAddressChanged = null;
79 private Interop.Connection.ConnectionAddressChangedCallback _connectionAddressChangedCallback;
80 private Interop.Connection.ConnectionTypeChangedCallback _connectionTypeChangedCallback;
81 private Interop.Connection.ConnectionAddressChangedCallback _proxyAddressChangedCallback;
82 private Interop.Connection.EthernetCableStateChangedCallback _ethernetCableStateChangedCallback;
84 internal static ConnectionInternalManager Instance
88 return s_instance.Value;
92 private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
94 Log.Info(Globals.LogTag, "In threadlocal delegate");
95 return new HandleHolder();
98 private ConnectionInternalManager()
100 Log.Info(Globals.LogTag, "ConnectionInternalManager constructor");
103 ~ConnectionInternalManager()
107 internal IntPtr GetHandle()
109 return s_threadName.Value.GetHandle();
112 internal event EventHandler<ConnectionTypeEventArgs> ConnectionTypeChanged
116 if (_ConnectionTypeChanged == null)
118 ConnectionTypeChangedStart();
121 _ConnectionTypeChanged += value;
125 _ConnectionTypeChanged -= value;
126 if (_ConnectionTypeChanged == null)
128 ConnectionTypeChangedStop();
133 private void ConnectionTypeChangedStart()
135 _connectionTypeChangedCallback = (ConnectionType type, IntPtr user_data) =>
137 if (_ConnectionTypeChanged != null)
139 _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type));
143 int ret = Interop.Connection.SetTypeChangedCallback(GetHandle(), _connectionTypeChangedCallback, IntPtr.Zero);
144 if ((ConnectionError)ret != ConnectionError.None)
146 Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret);
147 ConnectionErrorFactory.ThrowConnectionException(ret);
151 private void ConnectionTypeChangedStop()
153 int ret = Interop.Connection.UnsetTypeChangedCallback(GetHandle());
154 if ((ConnectionError)ret != ConnectionError.None)
156 Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret);
157 ConnectionErrorFactory.ThrowConnectionException(ret);
161 internal event EventHandler<EthernetCableStateEventArgs> EthernetCableStateChanged
165 if (_EthernetCableStateChanged == null)
167 EthernetCableStateChangedStart();
169 _EthernetCableStateChanged += value;
173 _EthernetCableStateChanged -= value;
174 if (_EthernetCableStateChanged == null)
176 EthernetCableStateChangedStop();
181 private void EthernetCableStateChangedStart()
183 _ethernetCableStateChangedCallback = (EthernetCableState state, IntPtr user_data) =>
185 if (_EthernetCableStateChanged != null)
187 _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state));
190 int ret = Interop.Connection.SetEthernetCableStateChagedCallback(GetHandle(),
191 _ethernetCableStateChangedCallback, IntPtr.Zero);
192 if ((ConnectionError)ret != ConnectionError.None)
194 Log.Error(Globals.LogTag,
195 "It failed to register ethernet cable state changed callback, " +
196 (ConnectionError)ret);
197 ConnectionErrorFactory.ThrowConnectionException(ret);
201 private void EthernetCableStateChangedStop()
203 int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(GetHandle());
204 if ((ConnectionError)ret != ConnectionError.None)
206 Log.Error(Globals.LogTag,
207 "It failed to unregister ethernet cable state changed callback, " +
208 (ConnectionError)ret);
209 ConnectionErrorFactory.ThrowConnectionException(ret);
213 internal event EventHandler<AddressEventArgs> IPAddressChanged
217 if (_IPAddressChanged == null)
219 IPAddressChangedStart();
221 _IPAddressChanged += value;
226 _IPAddressChanged -= value;
227 if (_IPAddressChanged == null)
229 IPAddressChangedStop();
234 private void IPAddressChangedStart()
236 _connectionAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
238 if (_IPAddressChanged != null)
240 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
241 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
243 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
245 _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
250 int ret = Interop.Connection.SetIPAddressChangedCallback(GetHandle(), _connectionAddressChangedCallback, IntPtr.Zero);
251 if ((ConnectionError)ret != ConnectionError.None)
253 Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret);
257 private void IPAddressChangedStop()
259 int ret = Interop.Connection.UnsetIPAddressChangedCallback(GetHandle());
260 if ((ConnectionError)ret != ConnectionError.None)
262 Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret);
266 internal event EventHandler<AddressEventArgs> ProxyAddressChanged
270 //Console.WriteLine("ProxyAddressChanged Add **");
271 if (_ProxyAddressChanged == null)
273 ProxyAddressChangedStart();
276 _ProxyAddressChanged += value;
280 //Console.WriteLine("ProxyAddressChanged Remove");
281 _ProxyAddressChanged -= value;
282 if (_ProxyAddressChanged == null)
284 ProxyAddressChangedStop();
289 private void ProxyAddressChangedStart()
291 _proxyAddressChangedCallback = (IntPtr IPv4, IntPtr IPv6, IntPtr UserData) =>
293 if (_ProxyAddressChanged != null)
295 string ipv4 = Marshal.PtrToStringAnsi(IPv4);
296 string ipv6 = Marshal.PtrToStringAnsi(IPv6);
298 if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false))
300 _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6));
305 int ret = Interop.Connection.SetProxyAddressChangedCallback(GetHandle(), _proxyAddressChangedCallback, IntPtr.Zero);
306 if ((ConnectionError)ret != ConnectionError.None)
308 Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret);
312 private void ProxyAddressChangedStop()
314 int ret = Interop.Connection.UnsetProxyAddressChangedCallback(GetHandle());
315 if ((ConnectionError)ret != ConnectionError.None)
317 Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret);
321 private void UnregisterEvents()
323 if (_ConnectionTypeChanged != null)
325 ConnectionTypeChangedStop();
327 if (_IPAddressChanged != null)
329 IPAddressChangedStop();
331 if (_EthernetCableStateChanged != null)
333 EthernetCableStateChangedStop();
335 if (_ProxyAddressChanged != null)
337 ProxyAddressChangedStop();
341 internal int GetProfileIterator(ProfileListType type, out IntPtr iterator)
343 return Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
346 internal bool HasNext(IntPtr iterator)
348 return Interop.Connection.HasNextProfileIterator(iterator);
351 internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle)
353 return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle);
356 internal int DestoryProfileIterator(IntPtr iterator)
358 return Interop.Connection.DestroyProfileIterator(iterator);
361 internal System.Net.IPAddress GetIPAddress(AddressFamily family)
363 Log.Info(Globals.LogTag, "GetIPAddress " + family);
365 int ret = Interop.Connection.GetIPAddress(GetHandle(), (int)family, out ip);
366 if ((ConnectionError)ret != ConnectionError.None)
368 Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret);
369 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");
370 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
371 ConnectionErrorFactory.ThrowConnectionException(ret);
374 string result = Marshal.PtrToStringAnsi(ip);
375 Interop.Libc.Free(ip);
376 Log.Info(Globals.LogTag, "IPAddress " + result + " (" + result.Length + ")");
377 if (result.Length == 0)
379 if (family == AddressFamily.IPv4)
380 return System.Net.IPAddress.Parse("0.0.0.0");
382 return System.Net.IPAddress.Parse("::");
384 return System.Net.IPAddress.Parse(result);
387 internal IEnumerable<System.Net.IPAddress> GetAllIPv6Addresses(ConnectionType type)
389 Log.Debug(Globals.LogTag, "GetAllIPv6Addresses");
390 List<System.Net.IPAddress> ipList = new List<System.Net.IPAddress>();
391 Interop.Connection.IPv6AddressCallback callback = (IntPtr ipv6Address, IntPtr userData) =>
393 if (ipv6Address != IntPtr.Zero)
395 string ipv6 = Marshal.PtrToStringAnsi(ipv6Address);
396 if (ipv6.Length == 0)
397 ipList.Add(System.Net.IPAddress.Parse("::"));
399 ipList.Add(System.Net.IPAddress.Parse(ipv6));
405 int ret = Interop.Connection.GetAllIPv6Addresses(GetHandle(), (int)type, callback, IntPtr.Zero);
406 if (ret != (int)ConnectionError.None)
408 Log.Error(Globals.LogTag, "Failed to get all IPv6 addresses, Error - " + (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);
417 internal string GetProxy(AddressFamily family)
419 Log.Debug(Globals.LogTag, "GetProxy " + family);
421 int ret = Interop.Connection.GetProxy(GetHandle(), (int)family, out ip);
422 if ((ConnectionError)ret != ConnectionError.None)
424 Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret);
425 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");
426 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
427 ConnectionErrorFactory.ThrowConnectionException(ret);
430 string result = Marshal.PtrToStringAnsi(ip);
431 Interop.Libc.Free(ip);
435 internal string GetMacAddress(ConnectionType type)
437 Log.Info(Globals.LogTag, "GetMacAddress " + type);
439 int ret = Interop.Connection.GetMacAddress(GetHandle(), (int)type, out mac);
440 if ((ConnectionError)ret != ConnectionError.None)
442 Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret);
443 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
444 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
445 ConnectionErrorFactory.ThrowConnectionException(ret);
448 string result = Marshal.PtrToStringAnsi(mac);
449 Interop.Libc.Free(mac);
453 internal long GetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
455 Log.Debug(Globals.LogTag, "GetStatistics " + connectionType + ", " + statisticsType);
457 int ret = Interop.Connection.GetStatistics(GetHandle(), (int)connectionType,
458 (int)statisticsType, out size);
459 if ((ConnectionError)ret != ConnectionError.None)
461 Log.Error(Globals.LogTag, "It failed to get statistics, " + (ConnectionError)ret);
462 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
463 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
464 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
465 ConnectionErrorFactory.ThrowConnectionException(ret);
470 internal void ResetStatistics(ConnectionType connectionType, StatisticsType statisticsType)
472 Log.Debug(Globals.LogTag, "ResetStatistics " + connectionType + ", " + statisticsType);
473 int ret = Interop.Connection.ResetStatistics(GetHandle(), (int)connectionType,
474 (int)statisticsType);
475 if ((ConnectionError)ret != ConnectionError.None)
477 Log.Error(Globals.LogTag, "It failed to reset statistics, " + (ConnectionError)ret);
478 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
479 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
480 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
481 ConnectionErrorFactory.ThrowConnectionException(ret);
485 internal void AddRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
487 if (interfaceName != null && address != null && gateway != null)
489 Log.Debug(Globals.LogTag, "AddRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
490 int ret = Interop.Connection.AddRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
491 if ((ConnectionError)ret != ConnectionError.None)
493 Log.Error(Globals.LogTag, "It failed to add route to the routing table, " + (ConnectionError)ret);
494 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");
495 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
496 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
497 ConnectionErrorFactory.ThrowConnectionException(ret);
503 throw new ArgumentNullException("Arguments are null");
507 internal void RemoveRoute(AddressFamily family, string interfaceName, System.Net.IPAddress address, System.Net.IPAddress gateway)
509 if (interfaceName != null && address != null && gateway != null)
511 Log.Debug(Globals.LogTag, "RemoveRoute " + family + ", " + interfaceName + ", " + address + ", " + gateway);
512 int ret = Interop.Connection.RemoveRoute(GetHandle(), family, interfaceName, address.ToString(), gateway.ToString());
513 if ((ConnectionError)ret != ConnectionError.None)
515 Log.Error(Globals.LogTag, "It failed to remove route from the routing table, " + (ConnectionError)ret);
516 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");
517 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
518 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
519 ConnectionErrorFactory.ThrowConnectionException(ret);
525 throw new ArgumentNullException("Arguments are null");
529 internal ConnectionType ConnectionType
533 Log.Info(Globals.LogTag, "get ConnectionType");
535 int ret = Interop.Connection.GetType(GetHandle(), out type);
536 if ((ConnectionError)ret != ConnectionError.None)
538 Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret);
539 ConnectionErrorFactory.ThrowConnectionException(ret);
541 return (ConnectionType)type;
545 internal CellularState CellularState
549 Log.Info(Globals.LogTag, "get CellularState");
551 int ret = Interop.Connection.GetCellularState(GetHandle(), out type);
552 if ((ConnectionError)ret != ConnectionError.None)
554 Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret);
555 ConnectionErrorFactory.ThrowConnectionException(ret);
557 return (CellularState)type;
561 internal ConnectionState WiFiState
565 Log.Info(Globals.LogTag, "get WiFiState");
567 int ret = Interop.Connection.GetWiFiState(GetHandle(), out type);
568 if ((ConnectionError)ret != ConnectionError.None)
570 Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret);
571 ConnectionErrorFactory.ThrowConnectionException(ret);
573 return (ConnectionState)type;
577 internal ConnectionState BluetoothState
581 Log.Info(Globals.LogTag, "get BluetoothState");
583 int ret = Interop.Connection.GetBtState(GetHandle(), out type);
584 if ((ConnectionError)ret != ConnectionError.None)
586 Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret);
587 ConnectionErrorFactory.ThrowConnectionException(ret);
589 return (ConnectionState)type;
593 internal ConnectionState EthernetState
597 Log.Info(Globals.LogTag, "get ConnectionType");
599 int ret = Interop.Connection.GetEthernetState(GetHandle(), out type);
600 if ((ConnectionError)ret != ConnectionError.None)
602 Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret);
603 ConnectionErrorFactory.ThrowConnectionException(ret);
605 return (ConnectionState)type;
609 internal EthernetCableState EthernetCableState
613 Log.Info(Globals.LogTag, "get EthernetCableState");
615 int ret = Interop.Connection.GetEthernetCableState(GetHandle(), out type);
616 if ((ConnectionError)ret != ConnectionError.None)
618 Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret);
619 ConnectionErrorFactory.ThrowConnectionException(ret);
621 return (EthernetCableState)type;
625 internal IntPtr CreateCellularProfile(ConnectionProfileType type, string keyword)
627 Log.Debug(Globals.LogTag, "CreateCellularProfile, " + type + ", " + keyword);
630 IntPtr handle = IntPtr.Zero;
631 int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle);
632 if ((ConnectionError)ret != ConnectionError.None)
634 Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret);
635 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
636 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
637 ConnectionErrorFactory.ThrowConnectionException(ret);
645 throw new ArgumentNullException("Keyword is null");
649 internal void AddCellularProfile(CellularProfile profile)
652 Log.Debug(Globals.LogTag, "AddCellularProfile");
655 if (profile.Type == ConnectionProfileType.Cellular)
657 int ret = Interop.Connection.AddProfile(GetHandle(), profile.ProfileHandle);
658 if ((ConnectionError)ret != ConnectionError.None)
660 Log.Error(Globals.LogTag, "Failed to add cellular profile, " + (ConnectionError)ret);
661 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
662 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.profile)");
663 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
664 ConnectionErrorFactory.ThrowConnectionException(ret);
670 throw new ArgumentException("Profile type is not cellular");
676 throw new ArgumentNullException("Profile is null");
680 internal void RemoveProfile(ConnectionProfile profile)
682 Log.Debug(Globals.LogTag, "RemoveProfile");
685 int ret = Interop.Connection.RemoveProfile(GetHandle(), profile.ProfileHandle);
686 if ((ConnectionError)ret != ConnectionError.None)
688 Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret);
689 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.telephony");
690 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
691 ConnectionErrorFactory.ThrowConnectionException(ret);
697 throw new ArgumentNullException("Profile is null");
701 internal void UpdateProfile(ConnectionProfile profile)
703 Log.Info(Globals.LogTag, "UpdateProfile");
706 int ret = Interop.Connection.UpdateProfile(GetHandle(), profile.ProfileHandle);
707 if ((ConnectionError)ret != ConnectionError.None)
709 Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret);
710 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.ethernet");
711 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
712 ConnectionErrorFactory.ThrowConnectionException(ret);
718 throw new ArgumentNullException("Profile is null");
722 internal ConnectionProfile GetCurrentProfile()
724 Log.Info(Globals.LogTag, "GetCurrentProfile");
725 IntPtr ProfileHandle;
726 int ret = Interop.Connection.GetCurrentProfile(GetHandle(), out ProfileHandle);
727 if ((ConnectionError)ret != ConnectionError.None)
729 if ((ConnectionError)ret == ConnectionError.NoConnection)
731 Log.Error(Globals.LogTag, "No connection " + (ConnectionError)ret);
736 Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret);
737 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");
738 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
739 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
740 ConnectionErrorFactory.ThrowConnectionException(ret);
744 ConnectionProfile Profile = new ConnectionProfile(ProfileHandle);
748 internal ConnectionProfile GetDefaultCellularProfile(CellularServiceType type)
750 Log.Debug(Globals.LogTag, "GetDefaultCellularProfile");
751 IntPtr ProfileHandle;
752 int ret = Interop.Connection.GetDefaultCellularServiceProfile(GetHandle(), (int)type, out ProfileHandle);
753 if ((ConnectionError)ret != ConnectionError.None)
755 Log.Error(Globals.LogTag, "Error: " + ret);
756 Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret);
757 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
758 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
759 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
760 ConnectionErrorFactory.ThrowConnectionException(ret);
763 CellularProfile Profile = new CellularProfile(ProfileHandle);
767 internal Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile)
769 Log.Debug(Globals.LogTag, "SetDefaultCellularProfile");
772 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
773 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
775 if (Result != ConnectionError.None)
777 Log.Error(Globals.LogTag, "Error occurs during set default cellular profile, " + Result);
778 task.SetException(new InvalidOperationException("Error occurs during set default cellular profile, " + Result));
782 task.SetResult(true);
786 int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0);
787 if ((ConnectionError)ret != ConnectionError.None)
789 Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret);
790 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony");
791 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
792 ConnectionErrorFactory.ThrowConnectionException(ret);
800 throw new ArgumentNullException("Profile is null");
805 internal Task<IEnumerable<ConnectionProfile>> GetProfileListAsync(ProfileListType type)
807 Log.Debug(Globals.LogTag, "GetProfileListAsync");
808 var task = new TaskCompletionSource<IEnumerable<ConnectionProfile>>();
810 List<ConnectionProfile> Result = new List<ConnectionProfile>();
812 int ret = Interop.Connection.GetProfileIterator(GetHandle(), (int)type, out iterator);
813 if ((ConnectionError)ret != ConnectionError.None)
815 Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret);
816 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");
817 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.get)");
818 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero), "Connection Handle may have been disposed or released");
819 ConnectionErrorFactory.ThrowConnectionException(ret);
822 while (Interop.Connection.HasNextProfileIterator(iterator))
826 Interop.Connection.GetNextProfileIterator(iterator, out nextH);
827 Interop.ConnectionProfile.Clone(out profileH, nextH);
830 Interop.ConnectionProfile.GetType(profileH, out profileType);
832 if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi)
834 WiFiProfile cur = new WiFiProfile(profileH);
837 else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular)
839 CellularProfile cur = new CellularProfile(profileH);
843 ConnectionProfile cur = new ConnectionProfile(profileH);
847 Interop.Connection.DestroyProfileIterator(iterator);
848 task.SetResult(Result);
852 internal Task OpenProfileAsync(ConnectionProfile profile)
854 Log.Debug(Globals.LogTag, "OpenProfileAsync");
857 Log.Debug(Globals.LogTag, "OpenProfileAsync " + profile.Name);
858 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
859 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
861 Log.Debug(Globals.LogTag, "Connected " + profile.Name);
862 if (Result != ConnectionError.None)
864 Log.Error(Globals.LogTag, "Error occurs during connecting profile, " + Result);
865 task.SetException(new InvalidOperationException("Error occurs during connecting profile, " + Result));
869 task.SetResult(true);
873 int ret = Interop.Connection.OpenProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
874 if ((ConnectionError)ret != ConnectionError.None)
876 Log.Error(Globals.LogTag, "It failed to connect profile, " + (ConnectionError)ret);
877 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
878 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
879 ConnectionErrorFactory.ThrowConnectionException(ret);
887 throw new ArgumentNullException("Profile is null");
891 internal Task CloseProfileAsync(ConnectionProfile profile)
893 Log.Debug(Globals.LogTag, "CloseProfileAsync");
896 Log.Debug(Globals.LogTag, "CloseProfileAsync " + profile.Name);
897 TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
898 Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) =>
900 if (Result != ConnectionError.None)
902 Log.Error(Globals.LogTag, "Error occurs during disconnecting profile, " + Result);
903 task.SetException(new InvalidOperationException("Error occurs during disconnecting profile, " + Result));
907 task.SetResult(true);
911 int ret = Interop.Connection.CloseProfile(GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero);
912 if ((ConnectionError)ret != ConnectionError.None)
914 Log.Error(Globals.LogTag, "It failed to disconnect profile, " + (ConnectionError)ret);
915 ConnectionErrorFactory.CheckFeatureUnsupportedException(ret, "http://tizen.org/feature/network.telephony " + "http://tizen.org/feature/network.wifi " + "http://tizen.org/feature/network.tethering.bluetooth");
916 ConnectionErrorFactory.CheckPermissionDeniedException(ret, "(http://tizen.org/privilege/network.set)");
917 ConnectionErrorFactory.CheckHandleNullException(ret, (GetHandle() == IntPtr.Zero || profile.ProfileHandle == IntPtr.Zero), "Connection or Profile Handle may have been disposed or released");
918 ConnectionErrorFactory.ThrowConnectionException(ret);
926 throw new ArgumentNullException("Profile is null");