From: Yu Jiung Date: Thu, 22 Dec 2016 08:47:28 +0000 (+0900) Subject: [C# Connection] Adding C# Connection code X-Git-Tag: submit/trunk/20170823.075128~116^2~25 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=dbeb8e57a80231a52f5bd01e4621964c40f8ebeb;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [C# Connection] Adding C# Connection code Change-Id: I35a2ab6334624d254d58fed9167edfea0550f1f9 Signed-off-by: Yu jiung --- diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/packaging/csapi-network-connection.manifest b/packaging/csapi-network-connection.manifest new file mode 100644 index 0000000..75b0fa5 --- /dev/null +++ b/packaging/csapi-network-connection.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/packaging/csapi-network-connection.spec b/packaging/csapi-network-connection.spec new file mode 100644 index 0000000..34e5bbf --- /dev/null +++ b/packaging/csapi-network-connection.spec @@ -0,0 +1,44 @@ +Name: csapi-network-connection +Summary: Tizen Connection API for C# +Version: 1.0.2 +Release: 1 +Group: Development/Libraries +License: Apache-2.0 +URL: https://www.tizen.org +Source0: %{name}-%{version}.tar.gz +Source1: %{name}.manifest + +AutoReqProv: no +ExcludeArch: aarch64 %ix86 + +BuildRequires: dotnet-build-tools + +# C# API Requires +BuildRequires: csapi-tizen-nuget + +%define Assemblies Tizen.Network.Connection + +%description +%{summary} + +%dotnet_import_sub_packages + +%prep +%setup -q +cp %{SOURCE1} . + +%build +for ASM in %{Assemblies}; do +%dotnet_build $ASM +%dotnet_pack $ASM/$ASM.nuspec %{version} +done + +%install +for ASM in %{Assemblies}; do +%dotnet_install $ASM +done + +%files +%manifest %{name}.manifest +%license LICENSE +%attr(644,root,root) %{dotnet_assembly_files} diff --git a/src/Tizen.Network.Connection/Interop/Interop.Connection.cs b/src/Tizen.Network.Connection/Interop/Interop.Connection.cs new file mode 100644 index 0000000..04cde64 --- /dev/null +++ b/src/Tizen.Network.Connection/Interop/Interop.Connection.cs @@ -0,0 +1,283 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Runtime.InteropServices; +using Tizen.Network.Connection; + +internal static partial class Interop +{ + internal static partial class Connection + { + public delegate void ConnectionTypeChangedCallback(ConnectionType type, IntPtr userData); + public delegate void EthernetCableStateChangedCallback(EthernetCableState state, IntPtr userData); + public delegate void ConnectionAddressChangedCallback(IntPtr ipv4, IntPtr ipv6, IntPtr userData); + public delegate void ConnectionCallback(ConnectionError result, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_create")] + public static extern int Create(out IntPtr handle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_destroy")] + public static extern int Destroy(IntPtr handle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_type")] + public static extern int GetType(IntPtr handle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_ip_address")] + public static extern int GetIpAddress(IntPtr handle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_proxy")] + public static extern int GetProxy(IntPtr handle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_mac_address")] + public static extern int GetMacAddress(IntPtr handle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_wifi_state")] + public static extern int GetWiFiState(IntPtr handle, out int state); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_cellular_state")] + public static extern int GetCellularState(IntPtr handle, out int state); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_ethernet_state")] + public static extern int GetEthernetState(IntPtr handle, out int state); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_ethernet_cable_state")] + public static extern int GetEthernetCableState(IntPtr handle, out int state); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_bt_state")] + public static extern int GetBtState(IntPtr handle, out int state); + + [DllImport(Libraries.Connection, EntryPoint = "connection_set_type_changed_cb")] + public static extern int SetTypeChangedCallback(IntPtr handle, ConnectionTypeChangedCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_set_ip_address_changed_cb")] + public static extern int SetIpAddressChangedCallback(IntPtr handle, ConnectionAddressChangedCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_set_ethernet_cable_state_chaged_cb")] + public static extern int SetEthernetCableStateChagedCallback(IntPtr handle, EthernetCableStateChangedCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_set_proxy_address_changed_cb")] + public static extern int SetProxyAddressChangedCallback(IntPtr handle, ConnectionAddressChangedCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_unset_type_changed_cb")] + public static extern int UnsetTypeChangedCallback(IntPtr handle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_unset_ip_address_changed_cb")] + public static extern int UnsetIpAddressChangedCallback(IntPtr handle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_unset_ethernet_cable_state_chaged_cb")] + public static extern int UnsetEthernetCableStateChagedCallback(IntPtr handle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_unset_proxy_address_changed_cb")] + public static extern int UnsetProxyAddressChangedCallback(IntPtr handle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_add_profile")] + public static extern int AddProfile(IntPtr handle, IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_remove_profile")] + public static extern int RemoveProfile(IntPtr handle, IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_update_profile")] + public static extern int UpdateProfile(IntPtr handle, IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_default_cellular_service_profile")] + public static extern int GetDefaultCellularServiceProfile(IntPtr handle, int type, out IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_current_profile")] + public static extern int GetCurrentProfile(IntPtr handle, out IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_open_profile")] + public static extern int OpenProfile(IntPtr handle, IntPtr profileHandle, ConnectionCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_close_profile")] + public static extern int CloseProfile(IntPtr handle, IntPtr profileHandle, ConnectionCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_reset_profile")] + public static extern int ResetProfile(IntPtr handle, int Option, int Id, ConnectionCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_set_default_cellular_service_profile_async")] + public static extern int SetDefaultCellularServiceProfileAsync(IntPtr handle, int Type, IntPtr profileHandle, ConnectionCallback callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_get_profile_iterator")] + public static extern int GetProfileIterator(IntPtr handle, int type, out IntPtr iterHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_iterator_next")] + public static extern int GetNextProfileIterator(IntPtr iterHandle, out IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_iterator_has_next")] + public static extern bool HasNextProfileIterator(IntPtr iterHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_destroy_profile_iterator")] + public static extern int DestroyProfileIterator(IntPtr iterHandle); + } + + internal static partial class ConnectionProfile + { + public delegate void profile_state_changed_cb(ProfileState type, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_create")] + public static extern int Create(int ProfileType, string Keyword, out IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_clone")] + public static extern int Clone(out IntPtr cloneHandle, IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_destroy")] + public static extern int Destroy(IntPtr profileHandle); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_id")] + public static extern int GetId(IntPtr profileHandle, out IntPtr profileId); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_name")] + public static extern int GetName(IntPtr profileHandle, out IntPtr name); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_type")] + public static extern int GetType(IntPtr profileHandle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_network_interface_name")] + public static extern int GetNetworkInterfaceName(IntPtr profileHandle, out IntPtr interfaceName); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_state")] + public static extern int GetState(IntPtr profileHandle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_ip_config_type")] + public static extern int GetIpConfigType(IntPtr profileHandle, int family, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_proxy_type")] + public static extern int GetProxyType(IntPtr profileHandle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_ip_address")] + public static extern int GetIpAddress(IntPtr profileHandle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_subnet_mask")] + public static extern int GetSubnetMask(IntPtr profileHandle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_gateway_address")] + public static extern int GetGatewayAddress(IntPtr profileHandle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_dns_address")] + public static extern int GetDnsAddress(IntPtr profileHandle, int order, int Family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_proxy_address")] + public static extern int GetProxyAddress(IntPtr profileHandle, int family, out IntPtr address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_ip_config_type")] + public static extern int SetIpConfigType(IntPtr profileHandle, int family, int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_proxy_type")] + public static extern int SetProxyType(IntPtr profileHandle, int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_ip_address")] + public static extern int SetIpAddress(IntPtr profileHandle, int family, string address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_subnet_mask")] + public static extern int SetSubnetMask(IntPtr profileHandle, int family, string address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_gateway_address")] + public static extern int SetGatewayAddress(IntPtr profileHandle, int family, string address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_dns_address")] + public static extern int SetDnsAddress(IntPtr profileHandle, int family, string address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_proxy_address")] + public static extern int SetProxyAddress(IntPtr profileHandle, int family, string address); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_state_changed_cb")] + public static extern int SetStateChangeCallback(IntPtr profileHandle, profile_state_changed_cb callback, IntPtr userData); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_unset_state_changed_cb")] + public static extern int UnsetStateChangeCallback(IntPtr profileHandle); + } + + internal static partial class ConnectionCellularProfile + { + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_cellular_service_type")] + public static extern int GetServiceType(IntPtr profileHandle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_cellular_apn")] + public static extern int GetApn(IntPtr profileHandle, out IntPtr apn); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_cellular_auth_info")] + public static extern int GetAuthInfo(IntPtr profileHandle, out int authType, out IntPtr name, out IntPtr password); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_cellular_home_url")] + public static extern int GetHomeUrl(IntPtr profileHandle, out IntPtr homeUrl); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_is_cellular_roaming")] + public static extern int IsRoaming(IntPtr profileHandle, out bool roaming); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_is_cellular_hidden")] + public static extern int IsHidden(IntPtr profileHandle, out bool hidden); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_is_cellular_editable")] + public static extern int IsEditable(IntPtr profileHandle, out bool editable); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_is_cellular_default")] + public static extern int IsDefault(IntPtr profileHandle, out bool isDefault); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_cellular_service_type")] + public static extern int SetServiceType(IntPtr profileHandle, int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_cellular_apn")] + public static extern int SetApn(IntPtr profileHandle, string apn); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_cellular_auth_info")] + public static extern int SetAuthInfo(IntPtr profileHandle, int authType, string name, string password); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_cellular_home_url")] + public static extern int SetHomeUrl(IntPtr profileHandle, string homeUrl); + + } + + internal static partial class ConnectionWiFiProfile + { + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_essid")] + public static extern int GetEssid(IntPtr profileHandle, out IntPtr essid); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_bssid")] + public static extern int GetBssid(IntPtr profileHandle, out IntPtr bssid); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_rssi")] + public static extern int GetRssi(IntPtr profileHandle, out int rssi); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_frequency")] + public static extern int GetFrequency(IntPtr profileHandle, out int frequency); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_max_speed")] + public static extern int GetMaxSpeed(IntPtr profileHandle, out int maxSpeed); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_security_type")] + public static extern int GetSecurityType(IntPtr profileHandle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_get_wifi_encryption_type")] + public static extern int GetEncryptionType(IntPtr profileHandle, out int type); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_is_wifi_passphrase_required")] + public static extern int IsRequiredPassphrase(IntPtr profileHandle, out bool required); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_set_wifi_passphrase")] + public static extern int SetPassphrase(IntPtr profileHandle, string passphrase); + + [DllImport(Libraries.Connection, EntryPoint = "connection_profile_is_wifi_wps_supported")] + public static extern int IsSupportedWps(IntPtr profileHandle, out bool supported); + } + + internal static partial class Libc + { + [DllImport(Libraries.Libc, EntryPoint = "free")] + public static extern void Free(IntPtr userData); + + } +} diff --git a/src/Tizen.Network.Connection/Interop/Interop.Libraries.cs b/src/Tizen.Network.Connection/Interop/Interop.Libraries.cs new file mode 100644 index 0000000..03ee0cd --- /dev/null +++ b/src/Tizen.Network.Connection/Interop/Interop.Libraries.cs @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string Connection = "libcapi-network-connection.so.1"; + public const string WiFi = "libcapi-network-wifi.so.1"; + public const string Bluetooth = "libcapi-network-bluetooth.so.0"; + public const string Smartcard = "libcapi-network-smartcard.so.0"; + public const string Nfc = "libcapi-network-nfc.so.0"; + public const string WiFiDirect = "libwifi-direct.so.1"; + public const string Glib = "libglib-2.0.so.0"; + public const string Libc = "libc.so.6"; + } +} diff --git a/src/Tizen.Network.Connection/Properties/AssemblyInfo.cs b/src/Tizen.Network.Connection/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..3d7bbbd --- /dev/null +++ b/src/Tizen.Network.Connection/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("Tizen.Network.Connection")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("Samsung Electronics")] +[assembly: AssemblyProduct("Tizen.Network.Connection")] +[assembly: AssemblyCopyright("Copyright © Samsung Electronics 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("d9b186b9-bff3-4bd5-9dc0-3706390caaf9")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.Net45.csproj b/src/Tizen.Network.Connection/Tizen.Network.Connection.Net45.csproj new file mode 100644 index 0000000..7f2ed68 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection.Net45.csproj @@ -0,0 +1,77 @@ + + + + Debug + AnyCPU + {3E8CB60C-B3E5-41B6-B58C-996515C6B4B2} + Library + Properties + Tizen.Network.Connection + Tizen.Network.Connection + 512 + v4.5 + + + true + full + false + bin\Debug\Net45\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\Net45\ + TRACE + prompt + 4 + + + true + + + Tizen.Network.Connection.snk + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.Net45.project.json b/src/Tizen.Network.Connection/Tizen.Network.Connection.Net45.project.json new file mode 100644 index 0000000..1ec1af2 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection.Net45.project.json @@ -0,0 +1,11 @@ +{ + "dependencies": { + "Tizen": "1.0.2" + }, + "frameworks": { + "net45": {} + }, + "runtimes": { + "win": {} + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.csproj b/src/Tizen.Network.Connection/Tizen.Network.Connection.csproj new file mode 100644 index 0000000..f1a1f9a --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection.csproj @@ -0,0 +1,86 @@ + + + + Debug + AnyCPU + {D9B186B9-BFF3-4BD5-9DC0-3706390CAAF9} + Library + Properties + Tizen.Network.Connection + Tizen.Network.Connection + 512 + + + .NETStandard + v1.3 + .NETStandard,Version=v1.3 + false + true + $(NoWarn);1701;1702 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + true + + + Tizen.Network.Connection.snk + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_TargetFrameworkDirectories>$(MSBuildThisFileDirectory) + <_FullFrameworkReferenceAssemblyPaths>$(MSBuildThisFileDirectory) + true + + \ No newline at end of file diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.nuspec b/src/Tizen.Network.Connection/Tizen.Network.Connection.nuspec new file mode 100644 index 0000000..eecb8f3 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection.nuspec @@ -0,0 +1,21 @@ + + + + Tizen.Network.Connection + $version$ + Samsung Electronics + false + https://www.apache.org/licenses/LICENSE-2.0 + https://www.tizen.org/ + https://developer.tizen.org/sites/default/files/images/tizen-pinwheel-on-light-rgb_64_64.png + © Samsung Electronics Co., Ltd All Rights Reserved + Provides the Connection APIs for Tizen.Network.Connection + + + + + + + + + diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.project.json b/src/Tizen.Network.Connection/Tizen.Network.Connection.project.json new file mode 100644 index 0000000..9da3e4b --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection.project.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + "NETStandard.Library": "1.6.0", + "Tizen": "1.0.2" + }, + "frameworks": { + "netstandard1.3": {} + } +} \ No newline at end of file diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.sln b/src/Tizen.Network.Connection/Tizen.Network.Connection.sln new file mode 100644 index 0000000..4b682c3 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tizen.Network.Connection", "Tizen.Network.Connection.csproj", "{D9B186B9-BFF3-4BD5-9DC0-3706390CAAF9}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D9B186B9-BFF3-4BD5-9DC0-3706390CAAF9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D9B186B9-BFF3-4BD5-9DC0-3706390CAAF9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D9B186B9-BFF3-4BD5-9DC0-3706390CAAF9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D9B186B9-BFF3-4BD5-9DC0-3706390CAAF9}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection.snk b/src/Tizen.Network.Connection/Tizen.Network.Connection.snk new file mode 100644 index 0000000..c2e0c4c Binary files /dev/null and b/src/Tizen.Network.Connection/Tizen.Network.Connection.snk differ diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/CellularProfile.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/CellularProfile.cs new file mode 100644 index 0000000..d6c9c12 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/CellularProfile.cs @@ -0,0 +1,316 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace Tizen.Network.Connection +{ + /// + /// This Class is CellularProfile + /// + public class CellularProfile : ConnectionProfile + { + private CellularAuthInformation AuthInfo; + + internal CellularProfile(IntPtr handle): base(handle) + { + AuthInfo = new CellularAuthInformation(handle); + } + + ~CellularProfile() + { + } + + /// + /// Gets the APN (access point name). + /// + public string Apn + { + get + { + IntPtr Value; + int ret = Interop.ConnectionCellularProfile.GetApn(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get apn, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + set + { + int ret = Interop.ConnectionCellularProfile.SetApn(ProfileHandle, (string)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set apn, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// Gets the home URL. + /// + public string HomeUri + { + get + { + IntPtr Value; + int ret = Interop.ConnectionCellularProfile.GetHomeUrl(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get home url, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + set + { + int ret = Interop.ConnectionCellularProfile.SetHomeUrl(ProfileHandle, (string)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set home url, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + + } + } + + /// + /// Gets the service type. + /// + public CellularServiceType ServiceType + { + get + { + int value; + int ret = Interop.ConnectionCellularProfile.GetServiceType(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get service type, " + (ConnectionError)ret); + } + return (CellularServiceType)value; + } + set + { + int ret = Interop.ConnectionCellularProfile.SetServiceType(ProfileHandle, (int)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set service type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// Gets cellular Authentification Information. + /// + public CellularAuthInformation CellularAuthInfo + { + get + { + return AuthInfo; + } + } + + /// + /// Checks whether the profile is hidden. + /// + public bool Hidden + { + get + { + bool value; + int ret = Interop.ConnectionCellularProfile.IsHidden(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get hidden value, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Checks whether the profile is editable. + /// + public bool Editable + { + get + { + bool value; + int ret = Interop.ConnectionCellularProfile.IsEditable(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get editable value, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Checks whether the profile is default. + /// + public bool IsDefault + { + get + { + bool value; + int ret = Interop.ConnectionCellularProfile.IsDefault(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get IsDefault value, " + (ConnectionError)ret); + } + return value; + } + } + } + + /// + /// The authentication information. + /// + public class CellularAuthInformation + { + private IntPtr ProfileHandle; + + private string Name = ""; + private string Passwd = ""; + private CellularAuthType AuthenType = CellularAuthType.None; + + internal CellularAuthInformation(IntPtr handle) + { + ProfileHandle = handle; + } + + /// + /// The user name. + /// + public string UserName + { + get + { + int type; + IntPtr name; + IntPtr password; + + int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get auth information, " + (ConnectionError)ret); + } + + Name = Marshal.PtrToStringAnsi(name); + Passwd = Marshal.PtrToStringAnsi(name); + AuthenType = (CellularAuthType)type; + + Interop.Libc.Free(name); + Interop.Libc.Free(password); + + return Name; + } + set + { + Name = value; + int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, (int)AuthenType, (string)value, Passwd); + Log.Error(Globals.LogTag, "UserName : "+ value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The password + /// + public string Password + { + get + { + int type; + IntPtr name; + IntPtr password; + + int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get auth information, " + (ConnectionError)ret); + } + Name = Marshal.PtrToStringAnsi(name); + Passwd = Marshal.PtrToStringAnsi(password); + AuthenType = (CellularAuthType)type; + + Interop.Libc.Free(name); + Interop.Libc.Free(password); + + return Passwd; + } + set + { + Passwd = value; + int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, (int)AuthenType, Name, (string)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The authentication type + /// + public CellularAuthType AuthType + { + get + { + int type; + IntPtr name; + IntPtr password; + + int ret = Interop.ConnectionCellularProfile.GetAuthInfo(ProfileHandle, out type, out name, out password); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get auth information, " + (ConnectionError)ret); + } + + Name = Marshal.PtrToStringAnsi(name); + Passwd = Marshal.PtrToStringAnsi(name); + AuthenType = (CellularAuthType)type; + + Interop.Libc.Free(name); + Interop.Libc.Free(password); + return AuthenType; + } + set + { + AuthenType = value; + int ret = Interop.ConnectionCellularProfile.SetAuthInfo(ProfileHandle, (int)value, Name, Passwd); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set auth information, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionError.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionError.cs new file mode 100644 index 0000000..97fdf95 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionError.cs @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Tizen.Network.Connection +{ + internal class ConnectionErrorFactory + { + static public void ThrowConnectionException(int errno) + { + ConnectionError error = (ConnectionError)errno; + if (error == ConnectionError.AddressFamilyNotSupported) + { + throw new InvalidOperationException("Address Family Not Supported"); + } + else if (error == ConnectionError.AlreadyExists) + { + throw new InvalidOperationException("Already Exists"); + } + else if (error == ConnectionError.DhcpFailed) + { + throw new InvalidOperationException("DHCP Failed"); + } + else if (error == ConnectionError.EndOfIteration) + { + throw new InvalidOperationException("End Of Iteration"); + } + else if (error == ConnectionError.InvalidKey) + { + throw new InvalidOperationException("Invalid Key"); + } + else if (error == ConnectionError.InvalidOperation) + { + throw new InvalidOperationException("Invalid Operation"); + } + else if (error == ConnectionError.InvalidParameter) + { + throw new InvalidOperationException("Invalid Parameter"); + } + else if (error == ConnectionError.NoConnection) + { + throw new InvalidOperationException("No Connection"); + } + else if (error == ConnectionError.NoReply) + { + throw new InvalidOperationException("No Reply"); + } + else if (error == ConnectionError.NotSupported) + { + throw new NotSupportedException("Not Supported"); + } + else if (error == ConnectionError.NowInProgress) + { + throw new InvalidOperationException("Now In Progress"); + } + else if (error == ConnectionError.OperationAborted) + { + throw new InvalidOperationException("Operation Aborted"); + } + else if (error == ConnectionError.OperationFailed) + { + throw new InvalidOperationException("Operation Failed"); + } + else if (error == ConnectionError.OutOfMemoryError) + { + throw new InvalidOperationException("Out Of Memory Error"); + } + else if (error == ConnectionError.PermissionDenied) + { + throw new InvalidOperationException("Permission Denied"); + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs new file mode 100644 index 0000000..03b9463 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionInternalManager.cs @@ -0,0 +1,406 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; +using System.Collections; + +namespace Tizen.Network.Connection +{ + class HandleHolder : IDisposable + { + private IntPtr Handle; + private bool disposed = false; + + public HandleHolder() + { + Log.Debug(Globals.LogTag, "HandleHolder() ^^"); + Interop.Connection.Create(out Handle); + Log.Debug(Globals.LogTag, "Handle: " + Handle); + } + + ~HandleHolder() + { + Dispose(false); + } + + internal IntPtr GetHandle() + { + return Handle; + } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + } + Interop.Connection.Destroy(Handle); + disposed = true; + } + } + + static class ConnectionInternalManager + { + private static HandleHolder Holder = new HandleHolder(); + + static public IntPtr GetHandle() + { + return Holder.GetHandle(); + } + + static internal int GetProfileIterator(ProfileListType type, out IntPtr iterator) + { + return Interop.Connection.GetProfileIterator(Holder.GetHandle(), (int)type, out iterator); + } + + static internal bool HasNext(IntPtr iterator) + { + return Interop.Connection.HasNextProfileIterator(iterator); + } + + static internal int NextProfileIterator(IntPtr iterator, out IntPtr profileHandle) + { + return Interop.Connection.GetNextProfileIterator(iterator, out profileHandle); + } + + static internal int DestoryProfileIterator(IntPtr iterator) + { + return Interop.Connection.DestroyProfileIterator(iterator); + } + + static public string GetIpAddress(AddressFamily family) + { + IntPtr ip; + int ret = Interop.Connection.GetIpAddress(Holder.GetHandle(), (int)family, out ip); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get IP address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(ip); + Interop.Libc.Free(ip); + return result; + } + + static public string GetProxy(AddressFamily family) + { + IntPtr ip; + int ret = Interop.Connection.GetProxy(Holder.GetHandle(), (int)family, out ip); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(ip); + Interop.Libc.Free(ip); + return result; + } + + static public string GetMacAddress(ConnectionType type) + { + IntPtr ip; + int ret = Interop.Connection.GetMacAddress(Holder.GetHandle(), (int)type, out ip); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get mac address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(ip); + Interop.Libc.Free(ip); + return result; + } + + static public ConnectionType ConnectionType + { + get + { + int type = 0; + Log.Debug(Globals.LogTag, "Handle: " + Holder.GetHandle()); + int ret = Interop.Connection.GetType(Holder.GetHandle(), out type); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get connection type, " + (ConnectionError)ret); + } + return (ConnectionType)type; + } + } + + static public CellularState CellularState + { + get + { + int type = 0; + int ret = Interop.Connection.GetCellularState(Holder.GetHandle(), out type); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get cellular state, " + (ConnectionError)ret); + } + return (CellularState)type; + } + } + + static public ConnectionState WiFiState + { + get + { + int type = 0; + int ret = Interop.Connection.GetWiFiState(Holder.GetHandle(), out type); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get wifi state, " + (ConnectionError)ret); + } + return (ConnectionState)type; + } + } + + static public ConnectionState BluetoothState + { + get + { + int type = 0; + int ret = Interop.Connection.GetBtState(Holder.GetHandle(), out type); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get bluetooth state, " + (ConnectionError)ret); + } + return (ConnectionState)type; + } + } + + static public ConnectionState EthernetState + { + get + { + int type = 0; + int ret = Interop.Connection.GetEthernetState(Holder.GetHandle(), out type); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get ethernet state, " + (ConnectionError)ret); + } + return (ConnectionState)type; + } + } + + static public EthernetCableState EthernetCableState + { + get + { + int type = 0; + int ret = Interop.Connection.GetEthernetCableState(Holder.GetHandle(), out type); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get ethernet cable state, " + (ConnectionError)ret); + } + return (EthernetCableState)type; + } + } + + static public IntPtr CreateRequestProfile(ConnectionProfileType type, string keyword) + { + Log.Error(Globals.LogTag, "CreateRequestProfile, " + type + ", " + keyword); + Log.Debug(Globals.LogTag, "Handle: " + GetHandle()); + IntPtr handle = IntPtr.Zero; + int ret = Interop.ConnectionProfile.Create((int)type, keyword, out handle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to Create profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + + return handle; + } + + static public int AddProfile(RequestProfile profile) + { + int ret = 0; + if (profile.Type == ConnectionProfileType.Cellular) + { + ret = Interop.Connection.AddProfile(Holder.GetHandle(), ((RequestCellularProfile)profile).ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to add profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + else if (profile.Type == ConnectionProfileType.WiFi) + { + ret = Interop.Connection.AddProfile(Holder.GetHandle(), ((RequestWiFiProfile)profile).ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to add profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + return ret; + } + + static public int RemoveProfile(ConnectionProfile profile) + { + int ret = Interop.Connection.RemoveProfile(Holder.GetHandle(), profile.ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to remove profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return ret; + } + + static public int UpdateProfile(ConnectionProfile profile) + { + int ret = Interop.Connection.UpdateProfile(Holder.GetHandle(), profile.ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to update profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return ret; + } + + static public ConnectionProfile GetCurrentProfile() + { + IntPtr ProfileHandle; + int ret = Interop.Connection.GetCurrentProfile(Holder.GetHandle(), out ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get current profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + ConnectionProfile Profile = new ConnectionProfile(ProfileHandle); + return Profile; + } + + static public ConnectionProfile GetDefaultCellularProfile(CellularServiceType type) + { + IntPtr ProfileHandle; + int ret = Interop.Connection.GetDefaultCellularServiceProfile(Holder.GetHandle(), (int)type, out ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "Error: " + ret); + Log.Error(Globals.LogTag, "It failed to get default cellular profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + CellularProfile Profile = new CellularProfile(ProfileHandle); + return Profile; + } + + static public Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile) + { + var task = new TaskCompletionSource(); + Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) => + { + task.SetResult((ConnectionError)Result); + return; + }; + int ret = Interop.Connection.SetDefaultCellularServiceProfileAsync(Holder.GetHandle(), (int)type, profile.ProfileHandle, Callback, (IntPtr)0); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set default cellular profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return task.Task; + } + + + static public Task> GetProfileListAsync(ProfileListType type) + { + var task = new TaskCompletionSource>(); + + List Result = new List(); + IntPtr iterator; + int ret = Interop.Connection.GetProfileIterator(Holder.GetHandle(), (int)type, out iterator); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get profile iterator, " + (ConnectionError)ret); + } + + while (Interop.Connection.HasNextProfileIterator(iterator)) + { + IntPtr nextH; + IntPtr profileH; + Interop.Connection.GetNextProfileIterator(iterator, out nextH); + Interop.ConnectionProfile.Clone(out profileH, nextH); + + int profileType; + Interop.ConnectionProfile.GetType(profileH, out profileType); + + if ((ConnectionProfileType)profileType == ConnectionProfileType.WiFi) + { + WiFiProfile cur = new WiFiProfile(profileH); + Result.Add(cur); + } + else if ((ConnectionProfileType)profileType == ConnectionProfileType.Cellular) + { + CellularProfile cur = new CellularProfile(profileH); + Result.Add(cur); + } + else { + ConnectionProfile cur = new ConnectionProfile(profileH); + Result.Add(cur); + } + } + Interop.Connection.DestroyProfileIterator(iterator); + task.SetResult(Result); + return task.Task; + } + + static public Task OpenProfileAsync(ConnectionProfile profile) + { + var task = new TaskCompletionSource(); + Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) => + { + task.SetResult((ConnectionError)Result); + return; + }; + int ret = Interop.Connection.OpenProfile(Holder.GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to oepn profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return task.Task; + } + + static public Task CloseProfileAsync(ConnectionProfile profile) + { + var task = new TaskCompletionSource(); + Interop.Connection.ConnectionCallback Callback = (ConnectionError Result, IntPtr Data) => + { + task.SetResult((ConnectionError)Result); + return; + }; + int ret = Interop.Connection.CloseProfile(Holder.GetHandle(), profile.ProfileHandle, Callback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to close profile, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return task.Task; + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionManager.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionManager.cs new file mode 100644 index 0000000..8b28568 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionManager.cs @@ -0,0 +1,526 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +/// +/// The Connection API provides functions, enumerations to get the status of network and current profile and manage profiles. +/// +namespace Tizen.Network.Connection +{ + /// + /// This class is ConnectionManager + /// + public class ConnectionManager : IDisposable + { + static internal ConnectionItem CurConnction = new ConnectionItem(); + private bool disposed = false; + + static private EventHandler _ConnectionTypeChanged = null; + static private EventHandler _IPAddressChanged = null; + static private EventHandler _EthernetCableStateChanged = null; + static private EventHandler _ProxyAddressChanged = null; + + /// + /// Event that is called when the type of the current connection is changed. + /// + static public event EventHandler ConnectionTypeChanged + { + add + { + if (_ConnectionTypeChanged == null) + { + ConnectionTypeChangedStart(); + } + _ConnectionTypeChanged += value; + } + remove + { + _ConnectionTypeChanged -= value; + if (_ConnectionTypeChanged == null) + { + ConnectionTypeChangedStop(); + } + } + } + + static private void ConnectionTypeChangedStart() + { + int ret = Interop.Connection.SetTypeChangedCallback(ConnectionInternalManager.GetHandle(), TypeChangedCallback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to register connection type changed callback, " + (ConnectionError)ret); + } + } + + static private void ConnectionTypeChangedStop() + { + int ret = Interop.Connection.UnsetTypeChangedCallback(ConnectionInternalManager.GetHandle()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to unregister connection type changed callback, " + (ConnectionError)ret); + } + } + + static private void TypeChangedCallback(ConnectionType type, IntPtr user_data) + { + if (_ConnectionTypeChanged != null) + { + _ConnectionTypeChanged(null, new ConnectionTypeEventArgs(type)); + } + } + + /// + /// Event for ethernet cable is plugged [in/out] event. + /// + static public event EventHandler EthernetCableStateChanged + { + add + { + if (_EthernetCableStateChanged == null) + { + EthernetCableStateChangedStart(); + } + _EthernetCableStateChanged += value; + } + remove + { + _EthernetCableStateChanged -= value; + if (_EthernetCableStateChanged == null) + { + EthernetCableStateChangedtop(); + } + } + } + + static private void EthernetCableStateChangedStart() + { + int ret = Interop.Connection.SetEthernetCableStateChagedCallback(ConnectionInternalManager.GetHandle(), EthernetCableStateChangedCallback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to register ethernet cable state changed callback, " + (ConnectionError)ret); + } + } + + static private void EthernetCableStateChangedtop() + { + int ret = Interop.Connection.UnsetEthernetCableStateChagedCallback(ConnectionInternalManager.GetHandle()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to unregister ethernet cable state changed callback, " + (ConnectionError)ret); + } + } + + static private void EthernetCableStateChangedCallback(EthernetCableState state, IntPtr user_data) + { + if (_EthernetCableStateChanged != null) + { + _EthernetCableStateChanged(null, new EthernetCableStateEventArgs(state)); + } + } + + /// + /// Event that is called when the IP address is changed. + /// + static public event EventHandler IpAddressChanged + { + add + { + if (_IPAddressChanged == null) + { + IpAddressChangedStart(); + } + _IPAddressChanged += value; + } + remove + { + _IPAddressChanged -= value; + if (_IPAddressChanged == null) + { + IpAddressChangedStop(); + } + } + } + + static private void IpAddressChangedStart() + { + int ret = Interop.Connection.SetIpAddressChangedCallback(ConnectionInternalManager.GetHandle(), IPAddressChangedCallback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to register callback for changing IP address, " + (ConnectionError)ret); + } + } + + static private void IpAddressChangedStop() + { + int ret = Interop.Connection.UnsetIpAddressChangedCallback(ConnectionInternalManager.GetHandle()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to unregister callback for changing IP address, " + (ConnectionError)ret); + } + } + + static private void IPAddressChangedCallback(IntPtr Ipv4, IntPtr Ipv6, IntPtr UserData) + { + if (_IPAddressChanged != null) + { + string ipv4 = Marshal.PtrToStringAnsi(Ipv4); + string ipv6 = Marshal.PtrToStringAnsi(Ipv6); + + if ((string.IsNullOrEmpty(ipv4) == false) || (string.IsNullOrEmpty(ipv6) == false)) + { + _IPAddressChanged(null, new AddressEventArgs(ipv4, ipv6)); + } + } + } + + /// + /// Event that is called when the proxy address is changed. + /// + static public event EventHandler ProxyAddressChanged + { + add + { + //Console.WriteLine("ProxyAddressChanged Add **"); + if (_ProxyAddressChanged == null) + { + ProxyAddressChangedStart(); + } + _ProxyAddressChanged += value; + } + remove + { + //Console.WriteLine("ProxyAddressChanged Remove"); + _ProxyAddressChanged -= value; + if (_ProxyAddressChanged == null) + { + ProxyAddressChangedStop(); + } + } + } + + static private void ProxyAddressChangedStart() + { + int ret = Interop.Connection.SetProxyAddressChangedCallback(ConnectionInternalManager.GetHandle(), IPAddressChangedCallback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to register callback for changing proxy address, " + (ConnectionError)ret); + } + } + + static private void ProxyAddressChangedStop() + { + int ret = Interop.Connection.UnsetProxyAddressChangedCallback(ConnectionInternalManager.GetHandle()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to unregister callback for changing proxy address, " + (ConnectionError)ret); + } + } + + static private void ProxyAddressChangedCallback(IntPtr Ipv4, IntPtr Ipv6, IntPtr UserData) + { + if (_ProxyAddressChanged != null) + { + string ipv4 = Marshal.PtrToStringAnsi(Ipv4); + string ipv6 = Marshal.PtrToStringAnsi(Ipv6); + Interop.Libc.Free(Ipv4); + Interop.Libc.Free(Ipv6); + + _ProxyAddressChanged(null, new AddressEventArgs(ipv4, ipv6)); + } + } + + internal ConnectionManager() + { + } + + ~ConnectionManager() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + } + ProxyAddressChangedStop(); + ConnectionTypeChangedStop(); + EthernetCableStateChangedtop(); + IpAddressChangedStop(); + disposed = true; + } + + + /// + /// Gets the IP address of the current connection. + /// + /// Thrown when method failed due to invalid operation + static public string GetIpAddress(AddressFamily family) + { + return ConnectionInternalManager.GetIpAddress(family); + } + + /// + /// Gets the proxy address of the current connection. + /// + /// Thrown when method failed due to invalid operation + static public string GetProxy(AddressFamily family) + { + return ConnectionInternalManager.GetProxy(family); + } + + /// + /// Gets the MAC address of the Wi-Fi or ethernet. + /// + /// Thrown when method failed due to invalid operation + static public string GetMacAddress(ConnectionType type) + { + return ConnectionInternalManager.GetMacAddress(type); + } + + /// + /// Gets the type of the current profile for data connection. + /// + static public ConnectionItem CurrentConnection + { + get + { + return CurConnction; + } + } + + /// + /// Gets the state of cellular connection. + /// + static public CellularState CellularState + { + get + { + return ConnectionInternalManager.CellularState; + } + } + + /// + /// Gets the state of the Wi-Fi. + /// + /// http://tizen.org/privilege/network.get + static public ConnectionState WiFiState + { + get + { + return ConnectionInternalManager.WiFiState; + } + } + + /// + /// The state of the Bluetooth. + /// + /// http://tizen.org/privilege/network.get + static public ConnectionState BluetoothState + { + get + { + return ConnectionInternalManager.BluetoothState; + } + } + + /// + /// The Ethernet connection state. + /// + /// http://tizen.org/privilege/network.get + static public ConnectionState EthernetState + { + get + { + return ConnectionInternalManager.EthernetState; + } + } + + /// + /// Checks for ethernet cable is attached or not. + /// + /// http://tizen.org/privilege/network.get + static public EthernetCableState EthernetCableState + { + get + { + return ConnectionInternalManager.EthernetCableState; + } + } + } + + /// + /// + /// + public class ConnectionItem + { + internal ConnectionItem() + { + } + + /// + /// Gets the type of the current profile for data connection. + /// + public ConnectionType Type + { + get + { + return ConnectionInternalManager.ConnectionType; + } + } + + /// + /// Gets the type of the current profile for data connection. + /// + public ConnectionState State + { + get + { + if (ConnectionInternalManager.ConnectionType == ConnectionType.Cellular) + { + if (ConnectionInternalManager.CellularState == CellularState.Connected) + { + return ConnectionState.Connected; + } + else if (ConnectionInternalManager.CellularState == CellularState.Available) + { + return ConnectionState.Disconnected; + } + else { + return ConnectionState.Deactivated; + } + } + else if (ConnectionInternalManager.ConnectionType == ConnectionType.Bluetooth) + { + return ConnectionInternalManager.BluetoothState; + } + else if (ConnectionInternalManager.ConnectionType == ConnectionType.WiFi) + { + return ConnectionInternalManager.WiFiState; + } + else if (ConnectionInternalManager.ConnectionType == ConnectionType.Ethernet) + { + return ConnectionInternalManager.EthernetState; + } + else { // TO DO : Add Net Proxy + return ConnectionState.Disconnected; + } + } + } + + } + + /// + /// An extended EventArgs class which contains changed connection type. + /// + public class ConnectionTypeEventArgs : EventArgs + { + private ConnectionType Type = ConnectionType.Disconnected; + + internal ConnectionTypeEventArgs(ConnectionType type) + { + Type = type; + } + + /// + /// The connection type. + /// + public ConnectionType ConnectionType + { + get + { + return Type; + } + } + } + + /// + /// An extended EventArgs class which contains changed ethernet cable state. + /// + public class EthernetCableStateEventArgs : EventArgs + { + private EthernetCableState State; + + internal EthernetCableStateEventArgs(EthernetCableState state) + { + State = state; + } + + /// + /// The ethernet cable state. + /// + public EthernetCableState EthernetCableState + { + get + { + return State; + } + } + } + + /// + /// An extended EventArgs class which contains changed address. + /// + public class AddressEventArgs : EventArgs + { + private string Ipv4 = ""; + private string Ipv6 = ""; + + internal AddressEventArgs(string ipv4, string ipv6) + { + Ipv4 = ipv4; + Ipv6 = ipv6; + } + + /// + /// The IPV4 address. + /// + public string Ipv4Address + { + get + { + return Ipv4; + } + } + + /// + /// The IPV6 address. + /// + public string Ipv6Address + { + get + { + return Ipv6; + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfile.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfile.cs new file mode 100644 index 0000000..beb6092 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfile.cs @@ -0,0 +1,312 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace Tizen.Network.Connection +{ + /// + /// This Class is ConnectionProfile + /// + public class ConnectionProfile : IDisposable + { + internal IntPtr ProfileHandle = IntPtr.Zero; + private IAddressInformation Ipv4; + private IAddressInformation Ipv6; + private bool disposed = false; + private EventHandler _ProfileStateChanged; + + /// + /// The event that is called when the state of profile is changed. + /// + public event EventHandler ProfileStateChanged + { + add + { + if (_ProfileStateChanged == null) + { + ProfileStateChangedStart(); + } + _ProfileStateChanged += value; + } + remove + { + _ProfileStateChanged -= value; + if (_ProfileStateChanged == null) + { + ProfileStateChangedStop(); + } + } + } + + private void TypeChangedCallback(ProfileState state, IntPtr userData) + { + if (_ProfileStateChanged != null) + { + _ProfileStateChanged(null, new ProfileStateEventArgs(state)); + } + } + + private void ProfileStateChangedStart() + { + int ret = Interop.ConnectionProfile.SetStateChangeCallback(ProfileHandle, TypeChangedCallback, IntPtr.Zero); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to register callback for changing profile state, " + (ConnectionError)ret); + } + } + + private void ProfileStateChangedStop() + { + int ret = Interop.ConnectionProfile.UnsetStateChangeCallback(ProfileHandle); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to unregister callback for changing profile state, " + (ConnectionError)ret); + } + } + + public ConnectionProfile(IntPtr handle) + { + ProfileHandle = handle; + Ipv4 = AddressFactory.CreateAddressInformation(handle, AddressFamily.Ipv4, AddressInformationType.Connection); + Ipv6 = AddressFactory.CreateAddressInformation(handle, AddressFamily.Ipv6, AddressInformationType.Connection); + } + + ~ConnectionProfile() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + } + Interop.ConnectionProfile.Destroy(ProfileHandle); + ProfileStateChangedStop(); + disposed = true; + } + + /// + /// Gets the profile ID. + /// + public string Id + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetId(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get id of connection profile, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + } + + /// + /// Gets the profile name. + /// + /// http://tizen.org/privilege/network.get + public string Name + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetName(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get name of connection profile, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + } + + /// + /// Gets the network type. + /// + public ConnectionProfileType Type + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get type of connection profile, " + (ConnectionError)ret); + } + return (ConnectionProfileType)Value; + } + } + + /// + /// Gets the name of the network interface, e.g. eth0 and pdp0. + /// + public string InterfaceName + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetNetworkInterfaceName(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get network interface name, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + } + + /// + /// Gets the profile state. + /// + public ProfileState State + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetState(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get profile state, " + (ConnectionError)ret); + } + return (ProfileState)Value; + } + } + + /// + /// Gets the Proxy type. + /// + public ProxyType ProxyType + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret); + } + return (ProxyType)Value; + + } + set + { + int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The proxy address. + /// + public String ProxyAddress + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + + } + set + { + int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The subnet mask address(Ipv4). + /// + public IAddressInformation Ipv4Settings + { + get + { + return Ipv4; + + } + } + + /// + /// The subnet mask address(Ipv4). + /// + public IAddressInformation Ipv6Settings + { + get + { + return Ipv6; + } + } + } + + /// + /// An extended EventArgs class which contains changed profile state. + /// + public class ProfileStateEventArgs : EventArgs + { + private ProfileState _State = ProfileState.Disconnected; + + internal ProfileStateEventArgs(ProfileState state) + { + _State = state; + } + + /// + /// The profile state. + /// + public ProfileState State + { + get + { + return _State; + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfileManager.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfileManager.cs new file mode 100644 index 0000000..ada4cc6 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionProfileManager.cs @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections; + +namespace Tizen.Network.Connection +{ + /// + /// This class is ConnectionProfileManager + /// + public class ConnectionProfileManager + { + /// + /// Adds a new profile + /// + /// http://tizen.org/privilege/network.profile + /// Thrown when method failed due to invalid operation + static public int AddProfile(RequestProfile profile) + { + return ConnectionInternalManager.AddProfile(profile); + } + + /// + /// Gets the list of profile with profile list type + /// + /// http://tizen.org/privilege/network.get + static public Task> GetProfileListAsync(ProfileListType type) + { + return ConnectionInternalManager.GetProfileListAsync(type); + } + + /// + /// Opens a connection of profile, asynchronously. + /// + /// http://tizen.org/privilege/network.get + /// http://tizen.org/privilege/network.set + static public Task ConnectProfileAsync(ConnectionProfile profile) + { + return ConnectionInternalManager.OpenProfileAsync(profile); + } + + /// + /// Closes a connection of profile. + /// + /// http://tizen.org/privilege/network.set + static public Task DisconnectProfileAsync(ConnectionProfile profile) + { + return ConnectionInternalManager.CloseProfileAsync(profile); + } + + /// + /// Removes an existing profile. + /// + /// http://tizen.org/privilege/network.get + /// http://tizen.org/privilege/network.profile + /// Thrown when method failed due to invalid operation + static public int RemoveProfile(ConnectionProfile profile) + { + Log.Debug(Globals.LogTag, "RemoveProfile. Id: " + profile.Id + ", Name: " + profile.Name + ", Type: " + profile.Type); + return ConnectionInternalManager.RemoveProfile(profile); + } + + /// + /// Updates an existing profile. + /// When a profile is changed, these changes will be not applied to the ConnectionProfileManager immediately. + /// When you call this function, your changes affect the ConnectionProfileManager and the existing profile is updated. + /// + /// http://tizen.org/privilege/network.get + /// http://tizen.org/privilege/network.profile + /// Thrown when method failed due to invalid operation + static public int UpdateProfile(ConnectionProfile profile) + { + return ConnectionInternalManager.UpdateProfile(profile); + } + + /// + /// Gets the name of the default profile. + /// + /// http://tizen.org/privilege/network.get + /// Thrown when method failed due to invalid operation + static public ConnectionProfile GetCurrentProfile() + { + return ConnectionInternalManager.GetCurrentProfile(); + } + + /// + /// Gets the default profile which provides the given cellular service. + /// + /// http://tizen.org/privilege/network.get + /// Thrown when method failed due to invalid operation + static public ConnectionProfile GetDefaultCellularProfile(CellularServiceType type) + { + return ConnectionInternalManager.GetDefaultCellularProfile(type); + } + + /// + /// Sets the default profile which provides the given cellular service. + /// + /// http://tizen.org/privilege/network.get + /// http://tizen.org/privilege/network.profile + /// Thrown when method failed due to invalid operation + static public Task SetDefaultCellularProfile(CellularServiceType type, ConnectionProfile profile) + { + return ConnectionInternalManager.SetDefaultCellularProfile(type, profile); + } + } + + /// + /// An extended EventArgs class which contains the state of changed connection profile. + /// + public class ConnectionProfileStateEventArgs : EventArgs + { + private ConnectionProfileState State; + + internal ConnectionProfileStateEventArgs(ConnectionProfileState state) + { + State = state; + } + + /// + /// The connection profile state. + /// + public ConnectionProfileState ConnectionProfileState + { + get + { + return State; + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionTypes.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionTypes.cs new file mode 100644 index 0000000..4b6ac3d --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/ConnectionTypes.cs @@ -0,0 +1,233 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Tizen.Internals.Errors; + + +namespace Tizen.Network.Connection +{ + /// + /// Enumeration for connection type. + /// + public enum ConnectionType + { + Disconnected = 0, /**< Disconnected */ + WiFi = 1, /**< Wi-Fi type */ + Cellular = 2, /**< Cellular type */ + Ethernet = 3, /**< Ethernet type */ + Bluetooth = 4, /**< Bluetooth type */ + NetProxy = 5 /**< Proxy type for internet connection (Since 3.0) */ + } + + /// + /// Enumeration for address family. + /// + public enum AddressFamily + { + Ipv4 = 0, + Ipv6 = 1 + } + + /// + /// Enumeration for cellular network state. + /// + public enum CellularState + { + OutOfService = 0, /**< Out of service */ + FlightMode = 1, /**< Flight mode */ + RoamingOff = 2, /**< Roaming is turned off */ + CallOnlyAvailable = 3, /**< Call is only available */ + Available = 4, /**< Available but not connected yet */ + Connected = 5, /**< Connected */ + } + + /// + /// Enumeration for connection state. + /// + public enum ConnectionState + { + Deactivated = 0, /**< Deactivated */ + Disconnected = 1, /**< Disconnected */ + Connected = 2, /**< Connected */ + } + + /// + /// This enumeration defines the attached or detached state of ethernet cable. + /// + public enum EthernetCableState + { + Detached = 0, /**< Ethernet cable is detached */ + Attached = 1, /**< Ethernet cable is attached */ + } + + /// + /// Enumeration for network connection type. + /// + public enum ConnectionProfileType + { + Cellular = 0, /**< Cellular type */ + WiFi = 1, /**< Wi-Fi type */ + Ethernet = 2, /**< Ethernet type */ + Bt = 3, /**< Bluetooth type */ + } + + /// + /// Enumeration for profile state type. + /// + public enum ProfileState + { + Disconnected = 0, /**< Disconnected state */ + Association = 1, /**< Association state */ + Configuration = 2, /**< Configuration state */ + Connected = 3, /**< Connected state */ + } + + /// + /// Enumeration for proxy method type. + /// + public enum ProxyType + { + Direct = 0, /**< Direct connection */ + Auto = 1, /**< Auto configuration(Use PAC file). If URL property is not set, DHCP/WPAD auto-discover will be tried */ + Manual = 2, /**< Manual configuration */ + } + + /// + /// Enumeration for IP configuration type. + /// + public enum IpConfigType + { + None = 0, /**< Not defined */ + Static = 1, /**< Manual IP configuration */ + Dynamic = 2, /**< Config IP using DHCP client*/ + Auto = 3, /**< Config IP from Auto IP pool (169.254/16). Later with DHCP client, if available */ + Fixed = 4, /**< Indicates an IP address that can not be modified */ + } + + /// + /// Enumeration for cellular service type. + /// + public enum CellularServiceType + { + Unknown = 0, /**< Unknown */ + Internet = 1, /**< Internet */ + MMS = 2, /**< MMS */ + PrepaidInternet = 3, /**< Prepaid internet */ + PrepaidMMS = 4, /**< Prepaid MMS */ + Tethering = 5, /**< Tethering */ + Application = 6, /**< Specific application */ + } + + static internal class ConnectionErrorValue + { + internal const int Base = -0x01C10000; + } + + + /// + /// Enumeration for connection errors. + /// + // To do : have to assign valid error code + public enum ConnectionError + { + None = ErrorCode.None, /**< Successful */ + InvalidParameter = ErrorCode.InvalidParameter, /**< Invalid parameter */ + OutOfMemoryError = ErrorCode.OutOfMemory, /**< Out of memory error */ + InvalidOperation = ErrorCode.InvalidOperation, /**< Invalid Operation */ + AddressFamilyNotSupported = ErrorCode.AddressFamilyNotSupported, /**< Address family not supported */ + OperationFailed = ConnectionErrorValue.Base | 0x0401, /**< Operation failed */ + EndOfIteration = ConnectionErrorValue.Base | 0x0402, /**< End of iteration */ + NoConnection = ConnectionErrorValue.Base | 0x0403, /**< There is no connection */ + NowInProgress = ErrorCode.NowInProgress, /** Now in progress */ + AlreadyExists = ConnectionErrorValue.Base | 0x0404, /**< Already exists */ + OperationAborted = ConnectionErrorValue.Base | 0x0405, /**< Operation is aborted */ + DhcpFailed = ConnectionErrorValue.Base | 0x0406, /**< DHCP failed */ + InvalidKey = ConnectionErrorValue.Base | 0x0407, /**< Invalid key */ + NoReply = ConnectionErrorValue.Base | 0x0408, /**< No reply */ + PermissionDenied = ErrorCode.PermissionDenied, /**< Permission denied */ + NotSupported = ErrorCode.NotSupported /**< Not Supported */ + } + + /// + /// Enumeration for profile list type. + /// + public enum ProfileListType + { + Registered = 0, /**< The iterator of the registered profile */ + Connected = 1, /**< The iterator of the connected profile */ + Default = 2, /**< The iterator of the default profile */ + } + + /// + /// Enumeration for security type of Wi-Fi. + /// + public enum WiFiSecureType + { + None = 0, /**< Security disabled */ + Wep = 1, /**< WEP */ + WpaPsk = 2, /**< WPA-PSK */ + Wpa2Psk = 3, /**< WPA2-PSK */ + Eap = 4, /**< EAP */ + } + + /// + /// Enumeration for encryption modes. + /// + public enum WiFiEncryptionType + { + None = 0, /**< Encryption disabled */ + Wep = 1, /**< WEP */ + Tkip = 2, /**< TKIP */ + Aes = 3, /**< AES */ + TkipAesMixed = 4, /**< TKIP and AES are both supported */ + } + + /// + /// Enumeration for connection profile state. + /// + public enum ConnectionProfileState + { + Disconnected = 0, /**< Disconnected state */ + Association = 1, /**< Association state */ + Configuration = 2, /**< Configuration state */ + Connected = 3, /**< Connected state */ + } + + /// + /// Enumeration for cellular authentication type. + /// + public enum CellularAuthType + { + None = 0, /**< No authentication */ + Pap = 1, /**< PAP authentication */ + Chap = 2, /**< CHAP authentication */ + } + + public enum AddressInformationType + { + Connection = 0, + WiFi = 1 + } + + static internal class Globals + { + internal const string LogTag = "Tizen.Network.Connection"; + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/IAddressInformation.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/IAddressInformation.cs new file mode 100644 index 0000000..5d3d4a8 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/IAddressInformation.cs @@ -0,0 +1,271 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Collections; +using System.Runtime.InteropServices; + +namespace Tizen.Network.Connection +{ + /// + /// This Class is ConnectionAddress + /// + public interface IAddressInformation : IDisposable + { + /// + /// The DNS address. + /// + System.Net.IPAddress Dns1 { get; set; } + + /// + /// The DNS address. + /// + System.Net.IPAddress Dns2 { get; set; } + + /// + /// The gateway address. + /// + System.Net.IPAddress Gateway { get; set; } + + /// + /// The subnet mask address. + /// + System.Net.IPAddress SubnetMask { get; set; } + + /// + /// The IP address. + /// + System.Net.IPAddress Ip { get; set; } + + /// + /// The type of IP config. + /// + IpConfigType IpConfigType { get; set; } + } + + public static class AddressFactory + { + public static IAddressInformation CreateAddressInformation(IntPtr handle, AddressFamily family, AddressInformationType infoType) + { + return new ConnectionAddressInformation(handle, family); + } + } + + internal class ConnectionAddressInformation : IAddressInformation + { + private IntPtr _profileHandle; + private AddressFamily _family; + private bool _disposed = false; + + internal ConnectionAddressInformation(IntPtr handle, AddressFamily family) + { + _profileHandle = handle; + _family = family; + } + + ~ConnectionAddressInformation() + { + Dispose(false); + } + + protected void Dispose(bool disposing) + { + if (_disposed) + return; + + if (disposing) + { + // Release managed resources. + } + // Release unmanaged resources. + _disposed = true; + } + + public void Dispose() + { + throw new NotImplementedException(); + } + + public System.Net.IPAddress Dns1 + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 1, (int)_family, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get dns1 address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + } + set + { + int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, (int)_family, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set dns1 address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + public System.Net.IPAddress Dns2 + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetDnsAddress(_profileHandle, 2, (int)_family, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get dns2 address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + } + set + { + int ret = Interop.ConnectionProfile.SetDnsAddress(_profileHandle, (int)_family, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set dns2 address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + public System.Net.IPAddress Gateway + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetGatewayAddress(_profileHandle, (int)_family, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get gateway, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + } + set + { + int ret = Interop.ConnectionProfile.SetGatewayAddress(_profileHandle, (int)_family, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set gateway, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + + public System.Net.IPAddress SubnetMask + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetSubnetMask(_profileHandle, (int)_family, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get subnet mask, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + } + set + { + int ret = Interop.ConnectionProfile.SetSubnetMask(_profileHandle, (int)_family, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set subnet mask, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + + public System.Net.IPAddress Ip + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetIpAddress(_profileHandle, (int)_family, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get ip, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + } + set + { + int ret = Interop.ConnectionProfile.SetIpAddress(_profileHandle, (int)_family, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set ip, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + public IpConfigType IpConfigType + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetIpConfigType(_profileHandle, (int)_family, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get ip config type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return (IpConfigType)Value; + } + set + { + int ret = Interop.ConnectionProfile.SetIpConfigType(_profileHandle, (int)_family, (int)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set ip config type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestCellularProfile.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestCellularProfile.cs new file mode 100644 index 0000000..ea0c56a --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestCellularProfile.cs @@ -0,0 +1,275 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + + +namespace Tizen.Network.Connection +{ + /// + /// This Class is RequestCellularProfile. + /// CellularServiceType should be set before AddProfile method of ConnectionProfileManager is called. + /// + public class RequestCellularProfile : RequestProfile + { + internal IntPtr ProfileHandle = IntPtr.Zero; + private IAddressInformation Ipv4; + private IAddressInformation Ipv6; + private bool disposed = false; + + private CellularAuthInformation AuthInfo; + /// + /// The constructor of CellularProfile class with profile type and keyword. + /// + /// http://tizen.org/privilege/network.get + public RequestCellularProfile(string keyword) + { + Log.Debug(Globals.LogTag, "RequestCellularProfile : " + keyword); + ProfileHandle = ConnectionInternalManager.CreateRequestProfile(ConnectionProfileType.Cellular, keyword); + Log.Debug(Globals.LogTag, "RequestCellularProfile is created : " + ProfileHandle); + + Ipv4 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv4, AddressInformationType.Connection); + Ipv6 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv6, AddressInformationType.Connection); + + AuthInfo = new CellularAuthInformation(ProfileHandle); + } + + /// + /// The destructor of CellularProfile class + /// + ~RequestCellularProfile() + { + Dispose(false); + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + } + Interop.ConnectionProfile.Destroy(ProfileHandle); + disposed = true; + } + + /// + /// Gets the network type. + /// + public ConnectionProfileType Type + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get profile type, " + (ConnectionError)ret); + } + return (ConnectionProfileType)Value; + } + } + + /// + /// Gets the Proxy type. + /// + public ProxyType ProxyType + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret); + } + return (ProxyType)Value; + + } + set + { + int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The proxy address. + /// + public System.Net.IPAddress ProxyAddress + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + + } + set + { + int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + + } + + /// + /// The subnet mask address(Ipv4). + /// + public IAddressInformation Ipv4Settings + { + get + { + return Ipv4; + } + } + + + /// + /// The subnet mask address(Ipv4). + /// + public IAddressInformation Ipv6Settings + { + get + { + return Ipv6; + } + } + + /// + /// Gets the APN (access point name). + /// + public string Apn + { + get + { + IntPtr Value; + int ret = Interop.ConnectionCellularProfile.GetApn(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get apn, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + set + { + int ret = Interop.ConnectionCellularProfile.SetApn(ProfileHandle, (string)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set apn, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// Gets the home URL. + /// + public string HomeUri + { + get + { + IntPtr Value; + int ret = Interop.ConnectionCellularProfile.GetHomeUrl(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get home uri, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + return result; + } + set + { + int ret = Interop.ConnectionCellularProfile.SetHomeUrl(ProfileHandle, value); + Log.Error(Globals.LogTag, "home uri " + value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set home uri, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// Gets the service type. + /// + public CellularServiceType ServiceType + { + get + { + int value; + int ret = Interop.ConnectionCellularProfile.GetServiceType(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get service type, " + (ConnectionError)ret); + } + return (CellularServiceType)value; + } + set + { + int ret = Interop.ConnectionCellularProfile.SetServiceType(ProfileHandle, (int)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set service type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// Gets cellular Authentification Information. + /// + public CellularAuthInformation CellularAuthInfo + { + get + { + return AuthInfo; + } + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestProfile.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestProfile.cs new file mode 100644 index 0000000..80c69829 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestProfile.cs @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +namespace Tizen.Network.Connection +{ + /// + /// RequestProfile is interface for RequestCellularProfile and RequestWiFiProfile + /// + public interface RequestProfile : IDisposable + { + /// + /// Gets the network type. + /// + ConnectionProfileType Type{ get; } + + /// + /// Gets the Proxy type. + /// + ProxyType ProxyType { get; set; } + + /// + /// The proxy address. + /// + System.Net.IPAddress ProxyAddress { get; set; } + + /// + /// The subnet mask address(Ipv4). + /// + IAddressInformation Ipv4Settings { get; } + + /// + /// The subnet mask address(Ipv4). + /// + IAddressInformation Ipv6Settings { get; } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestWiFiProfile.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestWiFiProfile.cs new file mode 100644 index 0000000..4c35092 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/RequestWiFiProfile.cs @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Runtime.InteropServices; + +namespace Tizen.Network.Connection +{ + /// + /// This Class is RequestWiFiProfile + /// + public class RequestWiFiProfile : RequestProfile + { + internal IntPtr ProfileHandle = IntPtr.Zero; + private IAddressInformation Ipv4; + private IAddressInformation Ipv6; + private bool disposed = false; + + /// + /// The constructor of WiFiProfile class with profile type and keyword. + /// + /// http://tizen.org/privilege/network.get + public RequestWiFiProfile(string keyword) + { + Log.Debug(Globals.LogTag, "RequestWiFiProfile : " + keyword); + ProfileHandle = ConnectionInternalManager.CreateRequestProfile(ConnectionProfileType.WiFi, keyword); + + Ipv4 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv4, AddressInformationType.Connection); + Ipv6 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv6, AddressInformationType.Connection); + } + + /// + /// The destructor of WiFiProfile class + /// + ~RequestWiFiProfile() + { + Dispose(false); + } + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + private void Dispose(bool disposing) + { + if (disposed) + return; + + if (disposing) + { + // Free managed objects. + } + Interop.ConnectionProfile.Destroy(ProfileHandle); + disposed = true; + } + + /// + /// Gets the network type. + /// + public ConnectionProfileType Type + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get profile type, " + (ConnectionError)ret); + } + return (ConnectionProfileType)Value; + } + } + + /// + /// Gets the Proxy type. + /// + public ProxyType ProxyType + { + get + { + int Value; + int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret); + } + return (ProxyType)Value; + + } + set + { + int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The proxy address. + /// + public System.Net.IPAddress ProxyAddress + { + get + { + IntPtr Value; + int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(Value); + Interop.Libc.Free(Value); + if (result == null) + return System.Net.IPAddress.Parse("0.0.0.0"); + return System.Net.IPAddress.Parse(result); + + } + set + { + int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString()); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + } + } + + /// + /// The subnet mask address(Ipv4). + /// + public IAddressInformation Ipv4Settings + { + get + { + return Ipv4; + + } + } + + /// + /// The subnet mask address(Ipv4). + /// + public IAddressInformation Ipv6Settings + { + get + { + return Ipv6; + + } + } + + /// + /// Sets the passphrase of the Wi-Fi WPA. + /// + public int SetPassphrase(string passphrase) + { + int ret = Interop.ConnectionWiFiProfile.SetPassphrase(ProfileHandle, (string)passphrase); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to set passphrase, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return ret; + } + } +} diff --git a/src/Tizen.Network.Connection/Tizen.Network.Connection/WiFiProfile.cs b/src/Tizen.Network.Connection/Tizen.Network.Connection/WiFiProfile.cs new file mode 100644 index 0000000..9b08cf0 --- /dev/null +++ b/src/Tizen.Network.Connection/Tizen.Network.Connection/WiFiProfile.cs @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Runtime.InteropServices; + +namespace Tizen.Network.Connection +{ + /// + /// This Class is WiFiProfile + /// + public class WiFiProfile : ConnectionProfile + { + internal WiFiProfile(IntPtr Handle) : base(Handle) + { + } + + ~WiFiProfile() + { + } + + /// + /// Gets the ESSID (Extended Service Set Identifier). + /// + public string Essid + { + get + { + IntPtr value; + int ret = Interop.ConnectionWiFiProfile.GetEssid(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to create profile handle, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(value); + Interop.Libc.Free(value); + return result; + } + } + + /// + /// Gets the BSSID (Basic Service Set Identifier). + /// + public string Bssid + { + get + { + IntPtr value; + int ret = Interop.ConnectionWiFiProfile.GetBssid(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get bssid, " + (ConnectionError)ret); + } + string result = Marshal.PtrToStringAnsi(value); + Interop.Libc.Free(value); + return result; + } + } + + /// + /// Gets the RSSI. + /// + public int Rssi + { + get + { + int value; + int ret = Interop.ConnectionWiFiProfile.GetRssi(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get rssi, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Gets the frequency (MHz). + /// + public int Frequency + { + get + { + int value; + int ret = Interop.ConnectionWiFiProfile.GetFrequency(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get frequency, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Gets the max speed (Mbps). + /// + public int MaxSpeed + { + get + { + int value; + int ret = Interop.ConnectionWiFiProfile.GetMaxSpeed(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get max speed, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Gets the security type of Wi-Fi. + /// + public WiFiSecureType SecureType + { + get + { + int value; + int ret = Interop.ConnectionWiFiProfile.GetSecurityType(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get security type, " + (ConnectionError)ret); + } + return (WiFiSecureType)value; + } + } + + /// + /// Gets the encryption type of Wi-Fi. + /// + public WiFiEncryptionType EncryptionType + { + get + { + int value; + int ret = Interop.ConnectionWiFiProfile.GetEncryptionType(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get encryption type, " + (ConnectionError)ret); + } + return (WiFiEncryptionType)value; + } + } + + /// + /// Checks whether passphrase is required. + /// + public bool PassphraseRequired + { + get + { + bool value; + int ret = Interop.ConnectionWiFiProfile.IsRequiredPassphrase(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get PassphraseRequired, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Checks whether the WPS (Wi-Fi Protected Setup) is supported. + /// + public bool WpsSupported + { + get + { + bool value; + int ret = Interop.ConnectionWiFiProfile.IsSupportedWps(ProfileHandle, out value); + if ((ConnectionError)ret != ConnectionError.None) + { + Log.Error(Globals.LogTag, "It failed to get IsSupportedWps, " + (ConnectionError)ret); + } + return value; + } + } + + /// + /// Sets the passphrase of the Wi-Fi WPA. + /// + public int SetPassphrase(string passphrase) + { + int ret = Interop.ConnectionWiFiProfile.SetPassphrase(ProfileHandle, (string)passphrase); + if ((ConnectionError)ret != ConnectionError.NoConnection) + { + Log.Error(Globals.LogTag, "It failed to set passphrase, " + (ConnectionError)ret); + ConnectionErrorFactory.ThrowConnectionException(ret); + } + return ret; + } + } +}