[WiFi] Add WiFiAP.Update
[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 || addrStr.Length == 0)
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                 WiFiManagerImpl.Instance.UpdateAP(_handle);
61             }
62         }
63
64         public System.Net.IPAddress Dns2
65         {
66             get
67             {
68                 IntPtr addrPtr;
69                 int ret = Interop.WiFi.AP.GetDnsAddress(_handle, 2, (int)_family, out addrPtr);
70                 if (ret != (int)WiFiError.None)
71                 {
72                     Log.Error(Globals.LogTag, "Failed to get second dns address, Error - " + (WiFiError)ret);
73                     return System.Net.IPAddress.Parse("0.0.0.0");
74                 }
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);
80             }
81             set
82             {
83                 int ret = Interop.WiFi.AP.SetDnsAddress(_handle, 2, (int)_family, value.ToString());
84                 if (ret != (int)WiFiError.None)
85                 {
86                     Log.Error(Globals.LogTag, "Failed to set second dns address, Error - " + (WiFiError)ret);
87                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
88                 }
89                 WiFiManagerImpl.Instance.UpdateAP(_handle);
90             }
91         }
92
93         public System.Net.IPAddress Gateway
94         {
95             get
96             {
97                 IntPtr addrPtr;
98                 int ret = Interop.WiFi.AP.GetGatewayAddress(_handle, (int)_family, out addrPtr);
99                 if (ret != (int)WiFiError.None)
100                 {
101                     Log.Error(Globals.LogTag, "Failed to get gateway address, Error - " + (WiFiError)ret);
102                     return System.Net.IPAddress.Parse("0.0.0.0");
103                 }
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);
109             }
110             set
111             {
112                 int ret = Interop.WiFi.AP.SetGatewayAddress(_handle, (int)_family, value.ToString());
113                 if (ret != (int)WiFiError.None)
114                 {
115                     Log.Error(Globals.LogTag, "Failed to set gateway address, Error - " + (WiFiError)ret);
116                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
117                 }
118                 WiFiManagerImpl.Instance.UpdateAP(_handle);
119             }
120         }
121
122         public System.Net.IPAddress SubnetMask
123         {
124             get
125             {
126                 IntPtr addrPtr;
127                 int ret = Interop.WiFi.AP.GetSubnetMask(_handle, (int)_family, out addrPtr);
128                 if (ret != (int)WiFiError.None)
129                 {
130                     Log.Error(Globals.LogTag, "Failed to get subnet mask, Error - " + (WiFiError)ret);
131                     return System.Net.IPAddress.Parse("0.0.0.0");
132                 }
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);
138             }
139             set
140             {
141                 int ret = Interop.WiFi.AP.SetSubnetMask(_handle, (int)_family, value.ToString());
142                 if (ret != (int)WiFiError.None)
143                 {
144                     Log.Error(Globals.LogTag, "Failed to set subnet mask, Error - " + (WiFiError)ret);
145                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
146                 }
147                 WiFiManagerImpl.Instance.UpdateAP(_handle);
148             }
149         }
150
151         public System.Net.IPAddress IP
152         {
153             get
154             {
155                 IntPtr addrPtr;
156                 int ret = Interop.WiFi.AP.GetIPAddress(_handle, (int)_family, out addrPtr);
157                 if (ret != (int)WiFiError.None)
158                 {
159                     Log.Error(Globals.LogTag, "Failed to get ip address, Error - " + (WiFiError)ret);
160                     return System.Net.IPAddress.Parse("0.0.0.0");
161                 }
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);
167             }
168             set
169             {
170                 int ret = Interop.WiFi.AP.SetIPAddress(_handle, (int)_family, value.ToString());
171                 if (ret != (int)WiFiError.None)
172                 {
173                     Log.Error(Globals.LogTag, "Failed to set ip address, Error - " + (WiFiError)ret);
174                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
175                 }
176                 WiFiManagerImpl.Instance.UpdateAP(_handle);
177             }
178         }
179
180         public IPConfigType IPConfigType
181         {
182             get
183             {
184                 int type;
185                 int ret = Interop.WiFi.AP.GetIPConfigType(_handle, (int)_family, out type);
186                 if (ret != (int)WiFiError.None)
187                 {
188                     Log.Error(Globals.LogTag, "Failed to get ip config type, Error - " + (WiFiError)ret);
189                 }
190                 return (IPConfigType)type;
191             }
192             set
193             {
194                 int ret = Interop.WiFi.AP.SetIPConfigType(_handle, (int)_family, (int)value);
195                 if (ret != (int)WiFiError.None)
196                 {
197                     Log.Error(Globals.LogTag, "Failed to set ip config type, Error - " + (WiFiError)ret);
198                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
199                 }
200                 WiFiManagerImpl.Instance.UpdateAP(_handle);
201             }
202         }
203
204         public int PrefixLength
205         {
206             get
207             {
208                 int Value;
209                 int ret = Interop.WiFi.AP.GetPrefixLength(_handle, (int)_family, out Value);
210                 if (ret != (int)WiFiError.None)
211                 {
212                     Log.Error(Globals.LogTag, "It failed to get prefix length, " + (WiFiError)ret);
213                     return -1;
214                 }
215                 return Value;
216             }
217
218             set
219             {
220                 int ret = Interop.WiFi.AP.SetPrefixLength(_handle, (int)_family, value);
221                 if (ret != (int)WiFiError.None)
222                 {
223                     Log.Error(Globals.LogTag, "It failed to set prefix length, " + (WiFiError)ret);
224                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
225                 }
226                 WiFiManagerImpl.Instance.UpdateAP(_handle);
227             }
228         }
229
230         public DnsConfigType DnsConfigType
231         {
232             get
233             {
234                 int Value;
235                 int ret = Interop.WiFi.AP.GetDnsConfigType(_handle, (int)_family, out Value);
236                 if ((WiFiError)ret != WiFiError.None)
237                 {
238                     Log.Error(Globals.LogTag, "It failed to get DNS config type, " + (WiFiError)ret);
239                 }
240                 return (DnsConfigType)Value;
241             }
242             set
243             {
244                 int ret = Interop.WiFi.AP.SetDnsConfigType(_handle, (int)_family, (int)value);
245                 if ((WiFiError)ret != WiFiError.None)
246                 {
247                     Log.Error(Globals.LogTag, "It failed to set DNS config type, " + (WiFiError)ret);
248                     WiFiErrorFactory.ThrowWiFiException(ret, _handle.DangerousGetHandle());
249                 }
250                 WiFiManagerImpl.Instance.UpdateAP(_handle);
251             }
252         }
253
254         /// <summary>
255         /// DHCP server address. It is only supported for IPv4 address family.
256         /// </summary>
257         /// <value>Represents DHCP server address.</value>
258         public System.Net.IPAddress DhcpServerAddress
259         {
260             get
261             {
262                 string dhcpServer;
263                 int ret = Interop.WiFi.AP.GetDhcpServerAddress(_handle, AddressFamily.IPv4, out dhcpServer);
264                 if (ret != (int)WiFiError.None)
265                 {
266                     Log.Error(Globals.LogTag, "Failed to get DHCP server address, Error - " + (WiFiError)ret);
267                 }
268
269                 if (dhcpServer == null || dhcpServer.Length == 0)
270                 {
271                     return IPAddress.Parse("0.0.0.0");
272                 }
273
274                 return IPAddress.Parse(dhcpServer);
275             }
276         }
277
278     }
279 }