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 private const string DefaultIPv4 = "0.0.0.0";
30 private const string DefaultIPv6 = "::";
32 internal WiFiAddressInformation(Interop.WiFi.SafeWiFiAPHandle handle, AddressFamily family)
38 public System.Net.IPAddress Dns1
43 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 1, (int)_family, out addrPtr);
44 return ParseIPAddress(ret, addrPtr);
48 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 1, (int)_family, value.ToString());
49 if (ret != (int)WiFiError.None)
51 Log.Error(Globals.LogTag, "Failed to set first dns address, Error - " + (WiFiError)ret);
52 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
57 public System.Net.IPAddress Dns2
62 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
63 return ParseIPAddress(ret, addrPtr);
67 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
68 if (ret != (int)WiFiError.None)
70 Log.Error(Globals.LogTag, "Failed to set second dns address, Error - " + (WiFiError)ret);
71 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
76 public System.Net.IPAddress Gateway
81 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
82 return ParseIPAddress(ret, addrPtr);
86 int ret = Interop.WiFi.AP.SetGatewayAddress(_handle, (int)_family, value.ToString());
87 if (ret != (int)WiFiError.None)
89 Log.Error(Globals.LogTag, "Failed to set gateway address, Error - " + (WiFiError)ret);
90 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
95 public System.Net.IPAddress SubnetMask
100 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
101 return ParseIPAddress(ret, addrPtr);
105 int ret = Interop.WiFi.AP.SetSubnetMask(_handle, (int)_family, value.ToString());
106 if (ret != (int)WiFiError.None)
108 Log.Error(Globals.LogTag, "Failed to set subnet mask, Error - " + (WiFiError)ret);
109 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
114 public System.Net.IPAddress IP
119 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
120 return ParseIPAddress(ret, addrPtr);
124 int ret = Interop.WiFi.AP.SetIPAddress(_handle, (int)_family, value.ToString());
125 if (ret != (int)WiFiError.None)
127 Log.Error(Globals.LogTag, "Failed to set ip address, Error - " + (WiFiError)ret);
128 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
133 public IPConfigType IPConfigType
138 int ret = Interop.WiFi.AP.GetIPConfigType(_handle, (int)_family, out type);
139 if (ret != (int)WiFiError.None)
141 Log.Error(Globals.LogTag, "Failed to get ip config type, Error - " + (WiFiError)ret);
143 return (IPConfigType)type;
147 int ret = Interop.WiFi.AP.SetIPConfigType(_handle, (int)_family, (int)value);
148 if (ret != (int)WiFiError.None)
150 Log.Error(Globals.LogTag, "Failed to set ip config type, Error - " + (WiFiError)ret);
151 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
156 public int PrefixLength
161 int ret = Interop.WiFi.AP.GetPrefixLength(_handle, (int)_family, out Value);
162 if (ret != (int)WiFiError.None)
164 Log.Error(Globals.LogTag, "It failed to get prefix length, " + (WiFiError)ret);
172 int ret = Interop.WiFi.AP.SetPrefixLength(_handle, (int)_family, value);
173 if (ret != (int)WiFiError.None)
175 Log.Error(Globals.LogTag, "It failed to set prefix length, " + (WiFiError)ret);
176 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
181 public DnsConfigType DnsConfigType
186 int ret = Interop.WiFi.AP.GetDnsConfigType(_handle, (int)_family, out Value);
187 if ((WiFiError)ret != WiFiError.None)
189 Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (WiFiError)ret);
191 return (DnsConfigType)Value;
195 int ret = Interop.WiFi.AP.SetDnsConfigType(_handle, (int)_family, (int)value);
196 if ((WiFiError)ret != WiFiError.None)
198 Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (WiFiError)ret);
199 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
205 /// DHCP server address. It is only supported for IPv4 address family.
207 /// <value>Represents DHCP server address.</value>
208 public System.Net.IPAddress DhcpServerAddress
213 int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, _family, out dhcpServer);
214 if (ret != (int)WiFiError.None || dhcpServer == null || dhcpServer.Length == 0)
216 Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
217 return DefaultIPAddress();
220 return IPAddress.Parse(dhcpServer);
225 /// DHCP lease duration. It is only supported for IPv4 address family.
227 /// <value>Represents DHCP lease duration.</value>
228 public int DhcpLeaseDuration
233 int ret = Interop.WiFi.AP.GetDhcpLeaseDuration(_handle, AddressFamily.IPv4, out leaseDuration);
234 if (ret != (int)WiFiError.None)
236 Log.Error(Globals.LogTag, "Failed to get DHCP lease duration, Error - " + (WiFiError)ret);
240 return leaseDuration;
244 private System.Net.IPAddress ParseIPAddress(int ret, IntPtr addrPtr)
246 if (ret != (int)WiFiError.None)
248 Log.Error(Globals.LogTag, "Failed to get address, Error - " + (WiFiError)ret);
249 return DefaultIPAddress();
252 string addr = Marshal.PtrToStringAnsi(addrPtr);
253 if (addr == null || addr.Length == 0)
254 return DefaultIPAddress();
255 Interop.Libc.Free(addrPtr);
256 return System.Net.IPAddress.Parse(addr);
259 private System.Net.IPAddress DefaultIPAddress()
261 if (_family == AddressFamily.IPv4)
262 return System.Net.IPAddress.Parse(DefaultIPv4);
264 return System.Net.IPAddress.Parse(DefaultIPv6);