Revert before adding new apis
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.WiFi / Tizen.Network.WiFi / WiFiManagerImpl.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.Collections.Generic;
19 using System.Threading;
20 using System.Threading.Tasks;
21 using System.Runtime.InteropServices;
22
23 namespace Tizen.Network.WiFi
24 {
25     static internal class Globals
26     {
27         internal const string LogTag = "Tizen.Network.WiFi";
28     }
29
30     internal class HandleHolder
31     {
32         private Interop.WiFi.SafeWiFiManagerHandle _handle;
33
34         internal HandleHolder()
35         {
36             _handle = WiFiManagerImpl.Instance.Initialize();
37             Log.Debug(Globals.LogTag, "Handle: " + _handle);
38         }
39
40         internal Interop.WiFi.SafeWiFiManagerHandle GetSafeHandle()
41         {
42             Log.Debug(Globals.LogTag, "Handleholder safehandle = " + _handle);
43             return _handle;
44         }
45     }
46
47     internal partial class WiFiManagerImpl
48     {
49         private static WiFiManagerImpl _instance = null;
50         private Dictionary<IntPtr, Interop.WiFi.VoidCallback> _callback_map = new Dictionary<IntPtr, Interop.WiFi.VoidCallback>();
51         private int _requestId = 0;
52         private string _macAddress;
53
54         internal string MacAddress
55         {
56             get
57             {
58                 if (String.IsNullOrEmpty(_macAddress))
59                 {
60                     string address;
61                     int ret = Interop.WiFi.GetMacAddress(GetSafeHandle(), out address);
62                     if (ret != (int)WiFiError.None)
63                     {
64                         Log.Error(Globals.LogTag, "Failed to get mac address, Error - " + (WiFiError)ret);
65                         _macAddress = "";
66                     }
67                     else
68                     {
69                         _macAddress = address;
70                     }
71                 }
72                 return _macAddress;
73             }
74         }
75
76         internal string InterfaceName
77         {
78             get
79             {
80                 string name;
81                 int ret = Interop.WiFi.GetNetworkInterfaceName(GetSafeHandle(), out name);
82                 if (ret != (int)WiFiError.None)
83                 {
84                     Log.Error(Globals.LogTag, "Failed to get interface name, Error - " + (WiFiError)ret);
85                     return "";
86                 }
87                 return name;
88             }
89         }
90
91         internal WiFiConnectionState ConnectionState
92         {
93             get
94             {
95                 int state;
96                 int ret = Interop.WiFi.GetConnectionState(GetSafeHandle(), out state);
97                 if (ret != (int)WiFiError.None)
98                 {
99                     Log.Error(Globals.LogTag, "Failed to get connection state, Error - " + (WiFiError)ret);
100                     return WiFiConnectionState.Failure;
101                 }
102                 return (WiFiConnectionState)state;
103             }
104         }
105
106         internal bool IsActive
107         {
108             get
109             {
110                 bool active;
111                 int ret = Interop.WiFi.IsActive(GetSafeHandle(), out active);
112                 if (ret != (int)WiFiError.None)
113                 {
114                     Log.Error(Globals.LogTag, "Failed to get isActive, Error - " + (WiFiError)ret);
115                 }
116                 return active;
117             }
118         }
119
120         internal static WiFiManagerImpl Instance
121         {
122             get
123             {
124                 if (_instance == null)
125                 {
126                     Log.Debug(Globals.LogTag, "Instance is null");
127                     _instance = new WiFiManagerImpl();
128                 }
129
130                 return _instance;
131             }
132         }
133
134         private static ThreadLocal<HandleHolder> s_threadName = new ThreadLocal<HandleHolder>(() =>
135         {
136             Log.Info(Globals.LogTag, "In threadlocal delegate");
137             return new HandleHolder();
138         });
139
140         private WiFiManagerImpl()
141         {
142         }
143
144         internal Interop.WiFi.SafeWiFiManagerHandle GetSafeHandle()
145         {
146             return s_threadName.Value.GetSafeHandle();
147         }
148
149         internal Interop.WiFi.SafeWiFiManagerHandle Initialize()
150         {
151             Interop.WiFi.SafeWiFiManagerHandle handle;
152             int ret = Interop.WiFi.Initialize(out handle);
153             if (ret != (int)WiFiError.None)
154             {
155                 Log.Error(Globals.LogTag, "Failed to initialize wifi, Error - " + (WiFiError)ret);
156                 WiFiErrorFactory.ThrowWiFiException(ret);
157             }
158             return handle;
159         }
160
161         internal IEnumerable<WiFiAP> GetFoundAPs()
162         {
163             Log.Debug(Globals.LogTag, "GetFoundAPs");
164             List<WiFiAP> apList = new List<WiFiAP>();
165             Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
166             {
167                 if (apHandle != IntPtr.Zero)
168                 {
169                     IntPtr clonedHandle;
170                     Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
171                     WiFiAP apItem = new WiFiAP(clonedHandle);
172                     apList.Add(apItem);
173                     return true;
174                 }
175                 return false;
176             };
177
178             int ret = Interop.WiFi.GetForeachFoundAPs(GetSafeHandle(), callback, IntPtr.Zero);
179             if (ret != (int)WiFiError.None)
180             {
181                 Log.Error(Globals.LogTag, "Failed to get all APs, Error - " + (WiFiError)ret);
182                 WiFiErrorFactory.ThrowWiFiException(ret);
183             }
184
185             return apList;
186         }
187
188         internal IEnumerable<WiFiAP> GetFoundSpecificAPs()
189         {
190             Log.Debug(Globals.LogTag, "GetFoundSpecificAPs");
191             List<WiFiAP> apList = new List<WiFiAP>();
192             Interop.WiFi.HandleCallback callback = (IntPtr apHandle, IntPtr userData) =>
193             {
194                 if (apHandle != IntPtr.Zero)
195                 {
196                     IntPtr clonedHandle;
197                     Interop.WiFi.AP.Clone(out clonedHandle, apHandle);
198                     WiFiAP apItem = new WiFiAP(clonedHandle);
199                     apList.Add(apItem);
200                     return true;
201                 }
202                 return false;
203
204             };
205
206             int ret = Interop.WiFi.GetForeachFoundSpecificAPs(GetSafeHandle(), callback, IntPtr.Zero);
207             if (ret != (int)WiFiError.None)
208             {
209                 Log.Error(Globals.LogTag, "Failed to get specific APs, Error - " + (WiFiError)ret);
210                 WiFiErrorFactory.ThrowWiFiException(ret);
211             }
212
213             return apList;
214         }
215
216         internal IEnumerable<WiFiConfiguration> GetWiFiConfigurations()
217         {
218             Log.Debug(Globals.LogTag, "GetWiFiConfigurations");
219             List<WiFiConfiguration> configList = new List<WiFiConfiguration>();
220             Interop.WiFi.HandleCallback callback = (IntPtr configHandle, IntPtr userData) =>
221             {
222                 if (configHandle != IntPtr.Zero)
223                 {
224                     IntPtr clonedConfig;
225                     Interop.WiFi.Config.Clone(configHandle, out clonedConfig);
226                     WiFiConfiguration configItem = new WiFiConfiguration(clonedConfig);
227                     configList.Add(configItem);
228                     return true;
229                 }
230                 return false;
231             };
232
233             int ret = Interop.WiFi.Config.GetForeachConfiguration(GetSafeHandle(), callback, IntPtr.Zero);
234             if (ret != (int)WiFiError.None)
235             {
236                 Log.Error(Globals.LogTag, "Failed to get configurations, Error - " + (WiFiError)ret);
237                 WiFiErrorFactory.ThrowWiFiException(ret);
238             }
239
240             return configList;
241         }
242
243         internal void SaveWiFiNetworkConfiguration(WiFiConfiguration config)
244         {
245             Log.Debug(Globals.LogTag, "SaveWiFiNetworkConfiguration");
246             IntPtr configHandle = config.GetHandle();
247             int ret = Interop.WiFi.Config.SaveConfiguration(GetSafeHandle(), configHandle);
248             if (ret != (int)WiFiError.None)
249             {
250                 Log.Error(Globals.LogTag, "Failed to save configuration, Error - " + (WiFiError)ret);
251                 WiFiErrorFactory.ThrowWiFiException(ret);
252             }
253         }
254
255         internal WiFiAP GetConnectedAP()
256         {
257             Log.Debug(Globals.LogTag, "GetConnectedAP");
258             IntPtr apHandle;
259             int ret = Interop.WiFi.GetConnectedAP(GetSafeHandle(), out apHandle);
260             if (ret != (int)WiFiError.None)
261             {
262                 Log.Error(Globals.LogTag, "Failed to connect with AP, Error - " + (WiFiError)ret);
263                 WiFiErrorFactory.ThrowWiFiException(ret);
264             }
265             WiFiAP ap = new WiFiAP(apHandle);
266             return ap;
267         }
268
269         internal Task ActivateAsync()
270         {
271             Log.Debug(Globals.LogTag, "ActivateAsync");
272             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
273             IntPtr id;
274             lock (_callback_map)
275             {
276                 id = (IntPtr)_requestId++;
277                 _callback_map[id] = (error, key) =>
278                 {
279                     Log.Debug(Globals.LogTag, "wifi activated");
280                     if (error != (int)WiFiError.None)
281                     {
282                         Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
283                         task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
284                     }
285                     task.SetResult(true);
286                     lock (_callback_map)
287                     {
288                         _callback_map.Remove(key);
289                     }
290                 };
291             }
292             int ret = Interop.WiFi.Activate(GetSafeHandle(), _callback_map[id], id);
293             if (ret != (int)WiFiError.None)
294             {
295                 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
296                 WiFiErrorFactory.ThrowWiFiException(ret);
297             }
298             return task.Task;
299         }
300
301         internal Task ActivateWithWiFiPickerTestedAsync()
302         {
303             Log.Debug(Globals.LogTag, "ActivateWithWiFiPickerTestedAsync");
304             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
305             IntPtr id;
306             lock (_callback_map)
307             {
308                 id = (IntPtr)_requestId++;
309                 _callback_map[id] = (error, key) =>
310                 {
311                     Log.Debug(Globals.LogTag, "Activation finished");
312                     if (error != (int)WiFiError.None)
313                     {
314                         Log.Error(Globals.LogTag, "Error occurs during WiFi activating, " + (WiFiError)error);
315                         task.SetException(new InvalidOperationException("Error occurs during WiFi activating, " + (WiFiError)error));
316                     }
317                     task.SetResult(true);
318                     lock (_callback_map)
319                     {
320                         _callback_map.Remove(key);
321                     }
322                 };
323             }
324             int ret = Interop.WiFi.ActivateWithWiFiPickerTested(GetSafeHandle(), _callback_map[id], id);
325             if (ret != (int)WiFiError.None)
326             {
327                 Log.Error(Globals.LogTag, "Failed to activate wifi, Error - " + (WiFiError)ret);
328                 WiFiErrorFactory.ThrowWiFiException(ret);
329             }
330             return task.Task;
331         }
332
333         internal Task DeactivateAsync()
334         {
335             Log.Debug(Globals.LogTag, "DeactivateAsync");
336             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
337             IntPtr id;
338             lock (_callback_map)
339             {
340                 id = (IntPtr)_requestId++;
341                 _callback_map[id] = (error, key) =>
342                 {
343                     Log.Debug(Globals.LogTag, "Deactivation finished");
344                     if (error != (int)WiFiError.None)
345                     {
346                         Log.Error(Globals.LogTag, "Error occurs during WiFi deactivating, " + (WiFiError)error);
347                         task.SetException(new InvalidOperationException("Error occurs during WiFi deactivating, " + (WiFiError)error));
348                     }
349                     task.SetResult(true);
350                     lock (_callback_map)
351                     {
352                         _callback_map.Remove(key);
353                     }
354                 };
355             }
356             int ret = Interop.WiFi.Deactivate(GetSafeHandle(), _callback_map[id], id);
357             if (ret != (int)WiFiError.None)
358             {
359                 Log.Error(Globals.LogTag, "Failed to deactivate wifi, Error - " + (WiFiError)ret);
360                 WiFiErrorFactory.ThrowWiFiException(ret);
361             }
362             return task.Task;
363         }
364
365         internal Task ScanAsync()
366         {
367             Log.Debug(Globals.LogTag, "ScanAsync");
368             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
369             IntPtr id;
370             lock (_callback_map)
371             {
372                 id = (IntPtr)_requestId++;
373                 _callback_map[id] = (error, key) =>
374                 {
375                     Log.Debug(Globals.LogTag, "Scanning finished");
376                     if (error != (int)WiFiError.None)
377                     {
378                         Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
379                         task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
380                     }
381                     task.SetResult(true);
382                     lock (_callback_map)
383                     {
384                         _callback_map.Remove(key);
385                     }
386                 };
387             }
388             int ret = Interop.WiFi.Scan(GetSafeHandle(), _callback_map[id], id);
389             if (ret != (int)WiFiError.None)
390             {
391                 Log.Error(Globals.LogTag, "Failed to scan all AP, Error - " + (WiFiError)ret);
392                 WiFiErrorFactory.ThrowWiFiException(ret);
393             }
394             return task.Task;
395         }
396
397         internal Task ScanSpecificAPAsync(string essid)
398         {
399             Log.Debug(Globals.LogTag, "ScanSpecificAPAsync " + essid);
400             TaskCompletionSource<bool> task = new TaskCompletionSource<bool>();
401             IntPtr id;
402             lock (_callback_map)
403             {
404                 id = (IntPtr)_requestId++;
405                 _callback_map[id] = (error, key) =>
406                 {
407                     Log.Debug(Globals.LogTag, "Scanning with specific AP finished");
408                     if (error != (int)WiFiError.None)
409                     {
410                         Log.Error(Globals.LogTag, "Error occurs during WiFi scanning, " + (WiFiError)error);
411                         task.SetException(new InvalidOperationException("Error occurs during WiFi scanning, " + (WiFiError)error));
412                     }
413                     task.SetResult(true);
414                     lock (_callback_map)
415                     {
416                         _callback_map.Remove(key);
417                     }
418                 };
419             }
420             int ret = Interop.WiFi.ScanSpecificAP(GetSafeHandle(), essid, _callback_map[id], id);
421             if (ret != (int)WiFiError.None)
422             {
423                 Log.Error(Globals.LogTag, "Failed to scan with specific AP, Error - " + (WiFiError)ret);
424                 WiFiErrorFactory.ThrowWiFiException(ret);
425             }
426             return task.Task;
427         }
428     }
429 }