2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Runtime.InteropServices;
20 using Tizen.Network.Connection;
22 namespace Tizen.Network.WiFi
24 internal class WiFiAddressInformation : IAddressInformation
26 private Interop.WiFi.SafeWiFiAPHandle _handle;
27 private AddressFamily _family;
29 internal WiFiAddressInformation(Interop.WiFi.SafeWiFiAPHandle handle, AddressFamily family)
35 public System.Net.IPAddress Dns1
40 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 1, (int)_family, out addrPtr);
41 if (ret != (int)WiFiError.None)
43 Log.Error(Globals.LogTag, "Failed to get first dns address, Error - " + (WiFiError)ret);
44 return System.Net.IPAddress.Parse("0.0.0.0");
46 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
47 Interop.Libc.Free(addrPtr);
49 return System.Net.IPAddress.Parse("0.0.0.0");
50 return System.Net.IPAddress.Parse(addrStr);
54 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 1, (int)_family, value.ToString());
55 if (ret != (int)WiFiError.None)
57 Log.Error(Globals.LogTag, "Failed to set first dns address, Error - " + (WiFiError)ret);
58 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
63 public System.Net.IPAddress Dns2
68 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
69 if (ret != (int)WiFiError.None)
71 Log.Error(Globals.LogTag, "Failed to get second dns address, Error - " + (WiFiError)ret);
72 return System.Net.IPAddress.Parse("0.0.0.0");
74 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
75 Interop.Libc.Free(addrPtr);
77 return System.Net.IPAddress.Parse("0.0.0.0");
78 return System.Net.IPAddress.Parse(addrStr);
82 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
83 if (ret != (int)WiFiError.None)
85 Log.Error(Globals.LogTag, "Failed to set second dns address, Error - " + (WiFiError)ret);
86 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
91 public System.Net.IPAddress Gateway
96 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
97 if (ret != (int)WiFiError.None)
99 Log.Error(Globals.LogTag, "Failed to get gateway address, Error - " + (WiFiError)ret);
100 return System.Net.IPAddress.Parse("0.0.0.0");
102 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
103 Interop.Libc.Free(addrPtr);
105 return System.Net.IPAddress.Parse("0.0.0.0");
106 return System.Net.IPAddress.Parse(addrStr);
110 int ret = Interop.WiFi.AP.SetGatewayAddress(_handle, (int)_family, value.ToString());
111 if (ret != (int)WiFiError.None)
113 Log.Error(Globals.LogTag, "Failed to set gateway address, Error - " + (WiFiError)ret);
114 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
119 public System.Net.IPAddress SubnetMask
124 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
125 if (ret != (int)WiFiError.None)
127 Log.Error(Globals.LogTag, "Failed to get subnet mask, Error - " + (WiFiError)ret);
128 return System.Net.IPAddress.Parse("0.0.0.0");
130 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
131 Interop.Libc.Free(addrPtr);
133 return System.Net.IPAddress.Parse("0.0.0.0");
134 return System.Net.IPAddress.Parse(addrStr);
138 int ret = Interop.WiFi.AP.SetSubnetMask(_handle, (int)_family, value.ToString());
139 if (ret != (int)WiFiError.None)
141 Log.Error(Globals.LogTag, "Failed to set subnet mask, Error - " + (WiFiError)ret);
142 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
147 public System.Net.IPAddress IP
152 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
153 if (ret != (int)WiFiError.None)
155 Log.Error(Globals.LogTag, "Failed to get ip address, Error - " + (WiFiError)ret);
156 return System.Net.IPAddress.Parse("0.0.0.0");
158 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
159 Interop.Libc.Free(addrPtr);
161 return System.Net.IPAddress.Parse("0.0.0.0");
162 return System.Net.IPAddress.Parse(addrStr);
166 int ret = Interop.WiFi.AP.SetIPAddress(_handle, (int)_family, value.ToString());
167 if (ret != (int)WiFiError.None)
169 Log.Error(Globals.LogTag, "Failed to set ip address, Error - " + (WiFiError)ret);
170 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
175 public IPConfigType IPConfigType
180 int ret = Interop.WiFi.AP.GetIPConfigType(_handle, (int)_family, out type);
181 if (ret != (int)WiFiError.None)
183 Log.Error(Globals.LogTag, "Failed to get ip config type, Error - " + (WiFiError)ret);
185 return (IPConfigType)type;
189 int ret = Interop.WiFi.AP.SetIPConfigType(_handle, (int)_family, (int)value);
190 if (ret != (int)WiFiError.None)
192 Log.Error(Globals.LogTag, "Failed to set ip config type, Error - " + (WiFiError)ret);
193 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
198 public int PrefixLength
203 int ret = Interop.WiFi.AP.GetPrefixLength(_handle, (int)_family, out Value);
204 if (ret != (int)WiFiError.None)
206 Log.Error(Globals.LogTag, "It failed to get prefix length, " + (WiFiError)ret);
214 int ret = Interop.WiFi.AP.SetPrefixLength(_handle, (int)_family, value);
215 if (ret != (int)WiFiError.None)
217 Log.Error(Globals.LogTag, "It failed to set prefix length, " + (WiFiError)ret);
218 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
223 public DnsConfigType DnsConfigType
228 int ret = Interop.WiFi.AP.GetDnsConfigType(_handle, (int)_family, out Value);
229 if ((WiFiError)ret != WiFiError.None)
231 Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (WiFiError)ret);
233 return (DnsConfigType)Value;
237 int ret = Interop.WiFi.AP.SetDnsConfigType(_handle, (int)_family, (int)value);
238 if ((WiFiError)ret != WiFiError.None)
240 Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (WiFiError)ret);
241 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
247 /// DHCP server address. It is only supported for IPv4 address family.
249 /// <value>Represents DHCP server address.</value>
250 public System.Net.IPAddress DhcpServerAddress
255 int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, AddressFamily.IPv4, out dhcpServer);
256 if (ret != (int)WiFiError.None)
258 Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
261 if (dhcpServer == null)
263 return IPAddress.Parse("0.0.0.0");
266 return IPAddress.Parse(dhcpServer);