c10ca5718a48458933434cd167c7ea71f114c4d7
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiAP.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
19 namespace Tizen.Network.WiFi
20 {
21     /// <summary>
22     /// A class for manager the network information of the access point(AP). It allows applications to manager network informaiton.
23     /// </summary>
24     public class WiFiAP : IDisposable
25     {
26         private IntPtr _apHandle = IntPtr.Zero;
27         private WiFiNetwork _network;
28         private WiFiSecurity _security;
29         private bool disposed = false;
30
31         /// <summary>
32         /// The network information of the access point(AP).
33         /// </summary>
34         public WiFiNetwork NetworkInformation
35         {
36             get
37             {
38                 return _network;
39             }
40         }
41         /// <summary>
42         /// The security information of the access point(AP).
43         /// </summary>
44         public WiFiSecurity SecurityInformation
45         {
46             get
47             {
48                 return _security;
49             }
50         }
51
52         internal WiFiAP(IntPtr handle)
53         {
54             Log.Debug(Globals.LogTag, "New WiFiAP. Handle: " + handle);
55             _apHandle = handle;
56             Initialize();
57         }
58         /// <summary>
59         /// Creates a object for the access point.
60         /// </summary>
61         /// <param name="essid">The ESSID (Extended Service Set Identifier) can be UTF-8 encoded </param>
62         public WiFiAP(string essid)
63         {
64             Log.Debug(Globals.LogTag, "New WiFiAP. Essid: " + essid);
65             createHandle(essid, true);
66             Initialize();
67         }
68         /// <summary>
69         /// Creates a object for the hidden access point.
70         /// </summary>
71         /// <param name="essid">The ESSID (Extended Service Set Identifier) can be UTF-8 encoded </param>
72         /// <param name="hidden">The value to set hidden AP</param>
73         public WiFiAP(string essid, bool hidden)
74         {
75             createHandle(essid, hidden);
76             Initialize();
77         }
78
79         ~WiFiAP()
80         {
81             Dispose(false);
82         }
83
84         public void Dispose()
85         {
86             Dispose(true);
87             GC.SuppressFinalize(this);
88         }
89
90         private void Dispose(bool disposing)
91         {
92             if (disposed)
93                 return;
94
95             if (disposing)
96             {
97                 _network.Dispose();
98                 _security.Dispose();
99                 Interop.WiFi.AP.Destroy(_apHandle);
100                 _apHandle = IntPtr.Zero;
101             }
102             disposed = true;
103         }
104
105         private void createHandle(string id, bool hidden)
106         {
107             int ret = -1;
108             if (hidden)
109             {
110                 ret = Interop.WiFi.AP.CreateHiddenAP(WiFiManagerImpl.Instance.GetHandle(), id, out _apHandle);
111             }
112             else
113             {
114                 ret = Interop.WiFi.AP.Create(WiFiManagerImpl.Instance.GetHandle(), id, out _apHandle);
115             }
116
117             if (ret != (int)WiFiError.None)
118             {
119                 Log.Error(Globals.LogTag, "Failed to create handle, Error - " + (WiFiError)ret);
120                 WiFiErrorFactory.ThrowWiFiException(ret);
121             }
122         }
123
124         private void Initialize()
125         {
126             _network = new WiFiNetwork(_apHandle);
127             _security = new WiFiSecurity(_apHandle);
128         }
129
130         /// <summary>
131         /// Refreshes the access point information.
132         /// </summary>
133         public void Refresh()
134         {
135             int ret = Interop.WiFi.AP.Refresh(_apHandle);
136             if (ret != (int)WiFiError.None)
137             {
138                 Log.Error(Globals.LogTag, "Failed to refresh ap handle, Error - " + (WiFiError)ret);
139                 WiFiErrorFactory.ThrowWiFiException(ret, _apHandle);
140             }
141         }
142
143         internal IntPtr GetHandle()
144         {
145             return _apHandle;
146         }
147     }
148 }