[C# Connection] Adding C# Connection code
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.Connection / Tizen.Network.Connection / RequestWiFiProfile.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.Linq;
20 using System.Text;
21 using System.Threading.Tasks;
22 using System.Runtime.InteropServices;
23
24 namespace Tizen.Network.Connection
25 {
26     /// <summary>
27     /// This Class is RequestWiFiProfile
28     /// </summary>
29     public class RequestWiFiProfile : RequestProfile
30     {
31         internal IntPtr ProfileHandle = IntPtr.Zero;
32         private IAddressInformation Ipv4;
33         private IAddressInformation Ipv6;
34         private bool disposed = false;
35
36         /// <summary>
37         /// The constructor of WiFiProfile class with profile type and keyword.
38         /// </summary>
39         /// <privilege>http://tizen.org/privilege/network.get</privilege>
40         public RequestWiFiProfile(string keyword)
41         {
42             Log.Debug(Globals.LogTag, "RequestWiFiProfile : " + keyword);
43             ProfileHandle = ConnectionInternalManager.CreateRequestProfile(ConnectionProfileType.WiFi, keyword);
44
45             Ipv4 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv4, AddressInformationType.Connection);
46             Ipv6 = AddressFactory.CreateAddressInformation(ProfileHandle, AddressFamily.Ipv6, AddressInformationType.Connection);
47         }
48
49         /// <summary>
50         /// The destructor of WiFiProfile class
51         /// </summary>
52         ~RequestWiFiProfile()
53         {
54             Dispose(false);
55         }
56         public void Dispose()
57         {
58             Dispose(true);
59             GC.SuppressFinalize(this);
60         }
61
62         private void Dispose(bool disposing)
63         {
64             if (disposed)
65                 return;
66
67             if (disposing)
68             {
69                 // Free managed objects.
70             }
71             Interop.ConnectionProfile.Destroy(ProfileHandle);
72             disposed = true;
73         }
74
75         /// <summary>
76         /// Gets the network type.
77         /// </summary>
78         public ConnectionProfileType Type
79         {
80             get
81             {
82                 int Value;
83                 int ret = Interop.ConnectionProfile.GetType(ProfileHandle, out Value);
84                 if ((ConnectionError)ret != ConnectionError.None)
85                 {
86                     Log.Error(Globals.LogTag, "It failed to get profile type, " + (ConnectionError)ret);
87                 }
88                 return (ConnectionProfileType)Value;
89             }
90         }
91
92         /// <summary>
93         /// Gets the Proxy type.
94         /// </summary>
95         public ProxyType ProxyType
96         {
97             get
98             {
99                 int Value;
100                 int ret = Interop.ConnectionProfile.GetProxyType(ProfileHandle, out Value);
101                 if ((ConnectionError)ret != ConnectionError.None)
102                 {
103                     Log.Error(Globals.LogTag, "It failed to get proxy type, " + (ConnectionError)ret);
104                 }
105                 return (ProxyType)Value;
106
107             }
108             set
109             {
110                 int ret = Interop.ConnectionProfile.SetProxyType(ProfileHandle, (int)value);
111                 if ((ConnectionError)ret != ConnectionError.None)
112                 {
113                     Log.Error(Globals.LogTag, "It failed to set proxy type, " + (ConnectionError)ret);
114                     ConnectionErrorFactory.ThrowConnectionException(ret);
115                 }
116             }
117         }
118
119         /// <summary>
120         /// The proxy address.
121         /// </summary>
122         public System.Net.IPAddress ProxyAddress
123         {
124             get
125             {
126                 IntPtr Value;
127                 int ret = Interop.ConnectionProfile.GetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, out Value);
128                 if ((ConnectionError)ret != ConnectionError.None)
129                 {
130                     Log.Error(Globals.LogTag, "It failed to get proxy address, " + (ConnectionError)ret);
131                 }
132                 string result = Marshal.PtrToStringAnsi(Value);
133                 Interop.Libc.Free(Value);
134                 if (result == null)
135                     return System.Net.IPAddress.Parse("0.0.0.0");
136                 return System.Net.IPAddress.Parse(result);
137
138             }
139             set
140             {
141                 int ret = Interop.ConnectionProfile.SetProxyAddress(ProfileHandle, (int)AddressFamily.Ipv4, value.ToString());
142                 if ((ConnectionError)ret != ConnectionError.None)
143                 {
144                     Log.Error(Globals.LogTag, "It failed to set proxy address, " + (ConnectionError)ret);
145                     ConnectionErrorFactory.ThrowConnectionException(ret);
146                 }
147             }
148         }
149
150         /// <summary>
151         /// The subnet mask address(Ipv4).
152         /// </summary>
153         public IAddressInformation Ipv4Settings
154         {
155             get
156             {
157                 return Ipv4;
158
159             }
160         }
161
162         /// <summary>
163         /// The subnet mask address(Ipv4).
164         /// </summary>
165         public IAddressInformation Ipv6Settings
166         {
167             get
168             {
169                 return Ipv6;
170
171             }
172         }
173
174         /// <summary>
175         /// Sets the passphrase of the Wi-Fi WPA.
176         /// </summary>
177         public int SetPassphrase(string passphrase)
178         {
179             int ret = Interop.ConnectionWiFiProfile.SetPassphrase(ProfileHandle, (string)passphrase);
180             if ((ConnectionError)ret != ConnectionError.None)
181             {
182                 Log.Error(Globals.LogTag, "It failed to set passphrase, " + (ConnectionError)ret);
183                 ConnectionErrorFactory.ThrowConnectionException(ret);
184             }
185             return ret;
186         }
187     }
188 }