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);
48 if (addrStr == null || addrStr.Length == 0)
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());
60 WiFiManagerImpl.Instance.UpdateAP(_handle);
64 public System.Net.IPAddress Dns2
69 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
70 if (ret != (int)WiFiError.None)
72 Log.Error(Globals.LogTag, "Failed to get second dns address, Error - " + (WiFiError)ret);
73 return System.Net.IPAddress.Parse("0.0.0.0");
75 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
76 Interop.Libc.Free(addrPtr);
77 if (addrStr == null || addrStr.Length == 0)
78 return System.Net.IPAddress.Parse("0.0.0.0");
79 return System.Net.IPAddress.Parse(addrStr);
83 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
84 if (ret != (int)WiFiError.None)
86 Log.Error(Globals.LogTag, "Failed to set second dns address, Error - " + (WiFiError)ret);
87 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
89 WiFiManagerImpl.Instance.UpdateAP(_handle);
93 public System.Net.IPAddress Gateway
98 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
99 if (ret != (int)WiFiError.None)
101 Log.Error(Globals.LogTag, "Failed to get gateway address, Error - " + (WiFiError)ret);
102 return System.Net.IPAddress.Parse("0.0.0.0");
104 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
105 Interop.Libc.Free(addrPtr);
106 if (addrStr == null || addrStr.Length == 0)
107 return System.Net.IPAddress.Parse("0.0.0.0");
108 return System.Net.IPAddress.Parse(addrStr);
112 int ret = Interop.WiFi.AP.SetGatewayAddress(_handle, (int)_family, value.ToString());
113 if (ret != (int)WiFiError.None)
115 Log.Error(Globals.LogTag, "Failed to set gateway address, Error - " + (WiFiError)ret);
116 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
118 WiFiManagerImpl.Instance.UpdateAP(_handle);
122 public System.Net.IPAddress SubnetMask
127 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
128 if (ret != (int)WiFiError.None)
130 Log.Error(Globals.LogTag, "Failed to get subnet mask, Error - " + (WiFiError)ret);
131 return System.Net.IPAddress.Parse("0.0.0.0");
133 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
134 Interop.Libc.Free(addrPtr);
135 if (addrStr == null || addrStr.Length == 0)
136 return System.Net.IPAddress.Parse("0.0.0.0");
137 return System.Net.IPAddress.Parse(addrStr);
141 int ret = Interop.WiFi.AP.SetSubnetMask(_handle, (int)_family, value.ToString());
142 if (ret != (int)WiFiError.None)
144 Log.Error(Globals.LogTag, "Failed to set subnet mask, Error - " + (WiFiError)ret);
145 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
147 WiFiManagerImpl.Instance.UpdateAP(_handle);
151 public System.Net.IPAddress IP
156 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
157 if (ret != (int)WiFiError.None)
159 Log.Error(Globals.LogTag, "Failed to get ip address, Error - " + (WiFiError)ret);
160 return System.Net.IPAddress.Parse("0.0.0.0");
162 string addrStr = Marshal.PtrToStringAnsi(addrPtr);
163 Interop.Libc.Free(addrPtr);
164 if (addrStr == null || addrStr.Length == 0)
165 return System.Net.IPAddress.Parse("0.0.0.0");
166 return System.Net.IPAddress.Parse(addrStr);
170 int ret = Interop.WiFi.AP.SetIPAddress(_handle, (int)_family, value.ToString());
171 if (ret != (int)WiFiError.None)
173 Log.Error(Globals.LogTag, "Failed to set ip address, Error - " + (WiFiError)ret);
174 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
176 WiFiManagerImpl.Instance.UpdateAP(_handle);
180 public IPConfigType IPConfigType
185 int ret = Interop.WiFi.AP.GetIPConfigType(_handle, (int)_family, out type);
186 if (ret != (int)WiFiError.None)
188 Log.Error(Globals.LogTag, "Failed to get ip config type, Error - " + (WiFiError)ret);
190 return (IPConfigType)type;
194 int ret = Interop.WiFi.AP.SetIPConfigType(_handle, (int)_family, (int)value);
195 if (ret != (int)WiFiError.None)
197 Log.Error(Globals.LogTag, "Failed to set ip config type, Error - " + (WiFiError)ret);
198 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
200 WiFiManagerImpl.Instance.UpdateAP(_handle);
204 public int PrefixLength
209 int ret = Interop.WiFi.AP.GetPrefixLength(_handle, (int)_family, out Value);
210 if (ret != (int)WiFiError.None)
212 Log.Error(Globals.LogTag, "It failed to get prefix length, " + (WiFiError)ret);
220 int ret = Interop.WiFi.AP.SetPrefixLength(_handle, (int)_family, value);
221 if (ret != (int)WiFiError.None)
223 Log.Error(Globals.LogTag, "It failed to set prefix length, " + (WiFiError)ret);
224 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
226 WiFiManagerImpl.Instance.UpdateAP(_handle);
230 public DnsConfigType DnsConfigType
235 int ret = Interop.WiFi.AP.GetDnsConfigType(_handle, (int)_family, out Value);
236 if ((WiFiError)ret != WiFiError.None)
238 Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (WiFiError)ret);
240 return (DnsConfigType)Value;
244 int ret = Interop.WiFi.AP.SetDnsConfigType(_handle, (int)_family, (int)value);
245 if ((WiFiError)ret != WiFiError.None)
247 Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (WiFiError)ret);
248 WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
250 WiFiManagerImpl.Instance.UpdateAP(_handle);
255 /// DHCP server address. It is only supported for IPv4 address family.
257 /// <value>Represents DHCP server address.</value>
258 public System.Net.IPAddress DhcpServerAddress
263 int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, AddressFamily.IPv4, out dhcpServer);
264 if (ret != (int)WiFiError.None)
266 Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
269 if (dhcpServer == null || dhcpServer.Length == 0)
271 return IPAddress.Parse("0.0.0.0");
274 return IPAddress.Parse(dhcpServer);