Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiAddressInformation.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.Runtime.InteropServices;
19 using System.Net;
20 using Tizen.Network.Connection;
21
22 namespace Tizen.Network.WiFi
23 {
24     internal class WiFiAddressInformation : IAddressInformation
25     {
26         private Interop.WiFi.SafeWiFiAPHandle _handle;
27         private AddressFamily _family;
28
29         internal WiFiAddressInformation(Interop.WiFi.SafeWiFiAPHandle handle, AddressFamily family)
30         {
31             _handle = handle;
32             _family = family;
33         }
34
35         public System.Net.IPAddress Dns1
36         {
37             get
38             {
39                 IntPtr addrPtr;
40                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 1, (int)_family, out addrPtr);
41                 if (ret != (int)WiFiError.None)
42                 {
43                     Log.Error(Globals.LogTag, "Failed to get first dns address, Error - " + (WiFiError)ret);
44                     return System.Net.IPAddress.Parse("0.0.0.0");
45                 }
46                 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
47                 Interop.Libc.Free(addrPtr);
48                 if (addrStr == null)
49                     return System.Net.IPAddress.Parse("0.0.0.0");
50                 return System.Net.IPAddress.Parse(addrStr);
51             }
52             set
53             {
54                 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 1, (int)_family, value.ToString());
55                 if (ret != (int)WiFiError.None)
56                 {
57                     Log.Error(Globals.LogTag, "Failed to set first dns address, Error - " + (WiFiError)ret);
58                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
59                 }
60             }
61         }
62
63         public System.Net.IPAddress Dns2
64         {
65             get
66             {
67                 IntPtr addrPtr;
68                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
69                 if (ret != (int)WiFiError.None)
70                 {
71                     Log.Error(Globals.LogTag, "Failed to get second dns address, Error - " + (WiFiError)ret);
72                     return System.Net.IPAddress.Parse("0.0.0.0");
73                 }
74                 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
75                 Interop.Libc.Free(addrPtr);
76                 if (addrStr == null)
77                     return System.Net.IPAddress.Parse("0.0.0.0");
78                 return System.Net.IPAddress.Parse(addrStr);
79             }
80             set
81             {
82                 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
83                 if (ret != (int)WiFiError.None)
84                 {
85                     Log.Error(Globals.LogTag, "Failed to set second dns address, Error - " + (WiFiError)ret);
86                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
87                 }
88             }
89         }
90
91         public System.Net.IPAddress Gateway
92         {
93             get
94             {
95                 IntPtr addrPtr;
96                 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
97                 if (ret != (int)WiFiError.None)
98                 {
99                     Log.Error(Globals.LogTag, "Failed to get gateway address, Error - " + (WiFiError)ret);
100                     return System.Net.IPAddress.Parse("0.0.0.0");
101                 }
102                 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
103                 Interop.Libc.Free(addrPtr);
104                 if (addrStr == null)
105                     return System.Net.IPAddress.Parse("0.0.0.0");
106                 return System.Net.IPAddress.Parse(addrStr);
107             }
108             set
109             {
110                 int ret = Interop.WiFi.AP.SetGatewayAddress(_handle, (int)_family, value.ToString());
111                 if (ret != (int)WiFiError.None)
112                 {
113                     Log.Error(Globals.LogTag, "Failed to set gateway address, Error - " + (WiFiError)ret);
114                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
115                 }
116             }
117         }
118
119         public System.Net.IPAddress SubnetMask
120         {
121             get
122             {
123                 IntPtr addrPtr;
124                 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
125                 if (ret != (int)WiFiError.None)
126                 {
127                     Log.Error(Globals.LogTag, "Failed to get subnet mask, Error - " + (WiFiError)ret);
128                     return System.Net.IPAddress.Parse("0.0.0.0");
129                 }
130                 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
131                 Interop.Libc.Free(addrPtr);
132                 if (addrStr == null)
133                     return System.Net.IPAddress.Parse("0.0.0.0");
134                 return System.Net.IPAddress.Parse(addrStr);
135             }
136             set
137             {
138                 int ret = Interop.WiFi.AP.SetSubnetMask(_handle, (int)_family, value.ToString());
139                 if (ret != (int)WiFiError.None)
140                 {
141                     Log.Error(Globals.LogTag, "Failed to set subnet mask, Error - " + (WiFiError)ret);
142                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
143                 }
144             }
145         }
146
147         public System.Net.IPAddress IP
148         {
149             get
150             {
151                 IntPtr addrPtr;
152                 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
153                 if (ret != (int)WiFiError.None)
154                 {
155                     Log.Error(Globals.LogTag, "Failed to get ip address, Error - " + (WiFiError)ret);
156                     return System.Net.IPAddress.Parse("0.0.0.0");
157                 }
158                 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
159                 Interop.Libc.Free(addrPtr);
160                 if (addrStr == null)
161                     return System.Net.IPAddress.Parse("0.0.0.0");
162                 return System.Net.IPAddress.Parse(addrStr);
163             }
164             set
165             {
166                 int ret = Interop.WiFi.AP.SetIPAddress(_handle, (int)_family, value.ToString());
167                 if (ret != (int)WiFiError.None)
168                 {
169                     Log.Error(Globals.LogTag, "Failed to set ip address, Error - " + (WiFiError)ret);
170                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
171                 }
172             }
173         }
174
175         public IPConfigType IPConfigType
176         {
177             get
178             {
179                 int type;
180                 int ret = Interop.WiFi.AP.GetIPConfigType(_handle, (int)_family, out type);
181                 if (ret != (int)WiFiError.None)
182                 {
183                     Log.Error(Globals.LogTag, "Failed to get ip config type, Error - " + (WiFiError)ret);
184                 }
185                 return (IPConfigType)type;
186             }
187             set
188             {
189                 int ret = Interop.WiFi.AP.SetIPConfigType(_handle, (int)_family, (int)value);
190                 if (ret != (int)WiFiError.None)
191                 {
192                     Log.Error(Globals.LogTag, "Failed to set ip config type, Error - " + (WiFiError)ret);
193                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
194                 }
195             }
196         }
197
198         public int PrefixLength
199         {
200             get
201             {
202                 int Value;
203                 int ret = Interop.WiFi.AP.GetPrefixLength(_handle, (int)_family, out Value);
204                 if (ret != (int)WiFiError.None)
205                 {
206                     Log.Error(Globals.LogTag, "It failed to get prefix length, " + (WiFiError)ret);
207                     return -1;
208                 }
209                 return Value;
210             }
211
212             set
213             {
214                 int ret = Interop.WiFi.AP.SetPrefixLength(_handle, (int)_family, value);
215                 if (ret != (int)WiFiError.None)
216                 {
217                     Log.Error(Globals.LogTag, "It failed to set prefix length, " + (WiFiError)ret);
218                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
219                 }
220             }
221         }
222
223         public DnsConfigType DnsConfigType
224         {
225             get
226             {
227                 int Value;
228                 int ret = Interop.WiFi.AP.GetDnsConfigType(_handle, (int)_family, out Value);
229                 if ((WiFiError)ret != WiFiError.None)
230                 {
231                     Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (WiFiError)ret);
232                 }
233                 return (DnsConfigType)Value;
234             }
235             set
236             {
237                 int ret = Interop.WiFi.AP.SetDnsConfigType(_handle, (int)_family, (int)value);
238                 if ((WiFiError)ret != WiFiError.None)
239                 {
240                     Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (WiFiError)ret);
241                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
242                 }
243             }
244         }
245
246         /// <summary>
247         /// DHCP server address. It is only supported for IPv4 address family.
248         /// </summary>
249         /// <value>Represents DHCP server address.</value>
250         public System.Net.IPAddress DhcpServerAddress
251         {
252             get
253             {
254                 string dhcpServer;
255                 int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, AddressFamily.IPv4, out dhcpServer);
256                 if (ret != (int)WiFiError.None)
257                 {
258                     Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
259                 }
260
261                 if (dhcpServer == null)
262                 {
263                     return IPAddress.Parse("0.0.0.0");
264                 }
265
266                 return IPAddress.Parse(dhcpServer);
267             }
268         }
269
270     }
271 }