Ejemplo n.º 1
0
        /// <summary>
        /// Connects the access point with WPS without SSID asynchronously.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="info">A WpsInfo instance which is of type WpsPbcInfo or WpsPinInfo.</param>
        /// <returns>A task which contains Connected access point information.</returns>
        /// <remarks>
        /// If WpsPinInfo is used, its object has to be constructed with a pin which must be 4 or 8 characters long.
        /// </remarks>
        /// <feature>http://tizen.org/feature/network.wifi</feature>
        /// <privilege>http://tizen.org/privilege/network.set</privilege>
        /// <privilege>http://tizen.org/privilege/network.get</privilege>
        /// <privilege>http://tizen.org/privilege/network.profile</privilege>
        /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
        /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is not of 4 or 8 characters long.</exception>
        /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
        /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
        public static Task <WiFiAP> ConnectWpsWithoutSsidAsync(WpsInfo info)
        {
            TaskCompletionSource <WiFiAP> task = new TaskCompletionSource <WiFiAP>();
            IntPtr id;

            lock (s_callbackMap)
            {
                id = (IntPtr)s_requestId++;
                s_callbackMap[id] = (error, key) =>
                {
                    Log.Debug(Globals.LogTag, "Connecting by WPS finished");
                    if (error != (int)WiFiError.None)
                    {
                        Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
                        task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
                    }
                    else
                    {
                        WiFiAP ap = WiFiManagerImpl.Instance.GetConnectedAP();
                        task.SetResult(ap);
                    }
                    lock (s_callbackMap)
                    {
                        s_callbackMap.Remove(key);
                    }
                };
            }

            int ret = -1;

            if (info.GetType() == typeof(WpsPbcInfo))
            {
                ret = Interop.WiFi.ConnectByWpsPbcWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), s_callbackMap[id], id);
            }

            else if (info.GetType() == typeof(WpsPinInfo))
            {
                WpsPinInfo pinInfo = (WpsPinInfo)info;
                if (pinInfo.GetWpsPin() == null)
                {
                    throw new ArgumentNullException("Wps pin should not be null");
                }

                if (pinInfo.GetWpsPin().Length != 4 && pinInfo.GetWpsPin().Length != 8)
                {
                    throw new ArgumentOutOfRangeException("Wps pin should be of 4 or 8 characters long");
                }

                ret = Interop.WiFi.ConnectByWpsPinWithoutSsid(WiFiManagerImpl.Instance.GetSafeHandle(), pinInfo.GetWpsPin(), s_callbackMap[id], id);
            }

            if (ret != (int)WiFiError.None)
            {
                Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
                WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle());
            }

            return(task.Task);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Connects the access point with the WPS asynchronously.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="info">A WpsInfo instance which is type of WpsPbcInfo or WpsPinInfo.</param>
        /// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
        /// <remarks>
        /// This method must be called from MainThread.
        /// </remarks>
        /// <feature>http://tizen.org/feature/network.wifi</feature>
        /// <privilege>http://tizen.org/privilege/network.profile</privilege>
        /// <privilege>http://tizen.org/privilege/network.get</privilege>
        /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
        /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
        /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is an empty string or more than 7 characters.</exception>
        /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
        /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
        /// <exception cref="NowInProgressException">Thrown when the Wi-Fi connection is now in progress.</exception>
        /// <exception cref="TimeoutException">Thrown when the timeout of WPS connection is expired.</exception>
        public Task ConnectWpsAsync(WpsInfo info)
        {
            Log.Info(Globals.LogTag, "ConnectWpsAsync");
            if (_disposed)
            {
                throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
            }

            TaskCompletionSource <bool> wpsTask = new TaskCompletionSource <bool>();

            _wpsTaskMap[_apHandle] = wpsTask;

            IntPtr id;

            lock (_callback_map)
            {
                id = (IntPtr)_requestId++;
                _callback_map[id] = (error, key) =>
                {
                    Log.Info(Globals.LogTag, "ConnectWpsAsync done");
                    if (error != (int)WiFiError.None)
                    {
                        Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
                        wpsTask.SetException(WiFiErrorFactory.GetException(error, "Error occurs during WiFi connecting"));
                        Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
                        _wpsTaskMap.Remove(_apHandle);
                    }
                    else
                    {
                        wpsTask.SetResult(true);
                        Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
                        _wpsTaskMap.Remove(_apHandle);
                    }
                    lock (_callback_map)
                    {
                        _callback_map.Remove(key);
                    }
                };
            }

            context.Post((x) =>
            {
                try
                {
                    int ret = -1;
                    if (info.GetType() == typeof(WpsPbcInfo))
                    {
                        Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPb");
                        ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
                    }
                    else if (info.GetType() == typeof(WpsPinInfo))
                    {
                        WpsPinInfo pinInfo = (WpsPinInfo)info;
                        if (pinInfo.GetWpsPin() == null)
                        {
                            throw new ArgumentNullException("Wps pin should not be null");
                        }

                        if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
                        {
                            throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
                        }

                        Log.Info(Globals.LogTag, "Interop.WiFi.ConnectByWpsPin");
                        ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
                    }

                    if (ret != (int)WiFiError.None)
                    {
                        Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
                        WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
                    }
                }
                catch (Exception e)
                {
                    Log.Error(Globals.LogTag, "Exception on ConnectWpsAsync\n" + e);
                    wpsTask.SetException(e);
                    Log.Info(Globals.LogTag, "Remove task for ConnectWpsAsync");
                    _wpsTaskMap.Remove(_apHandle);
                }
            }, null);

            return(wpsTask.Task);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Connects the access point with the WPS asynchronously.
        /// </summary>
        /// <since_tizen> 3 </since_tizen>
        /// <param name="info">A WpsInfo instance which is type of WpsPbcInfo or WpsPinInfo.</param>
        /// <returns>A task indicating whether the ConnectWps method is done or not.</returns>
        /// <feature>http://tizen.org/feature/network.wifi</feature>
        /// <privilege>http://tizen.org/privilege/network.profile</privilege>
        /// <privilege>http://tizen.org/privilege/network.get</privilege>
        /// <exception cref="NotSupportedException">Thrown when the Wi-Fi is not supported.</exception>
        /// <exception cref="UnauthorizedAccessException">Thrown when permission is denied.</exception>
        /// <exception cref="ObjectDisposedException">Thrown when the object instance is disposed or released.</exception>
        /// <exception cref="ArgumentNullException">Thrown when the WpsPinInfo object is constructed with a null pin.</exception>
        /// <exception cref="ArgumentOutOfRangeException">Thrown when the WpsPinInfo object is constructed with a pin which is an empty string or more than 7 characters.</exception>
        /// <exception cref="OutOfMemoryException">Thrown when the system is out of memory.</exception>
        /// <exception cref="ArgumentException">Thrown when the method failed due to an invalid parameter.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the method failed due to an invalid operation.</exception>
        public Task ConnectWpsAsync(WpsInfo info)
        {
            Log.Debug(Globals.LogTag, "ConnectWpsAsync");
            if (_disposed)
            {
                throw new ObjectDisposedException("Invalid AP instance (Object may have been disposed or released)");
            }
            TaskCompletionSource <bool> task = new TaskCompletionSource <bool>();
            IntPtr id;

            lock (_callback_map)
            {
                id = (IntPtr)_requestId++;
                _callback_map[id] = (error, key) =>
                {
                    Log.Debug(Globals.LogTag, "Connecting by WPS finished");
                    if (error != (int)WiFiError.None)
                    {
                        Log.Error(Globals.LogTag, "Error occurs during WiFi connecting, " + (WiFiError)error);
                        task.SetException(new InvalidOperationException("Error occurs during WiFi connecting, " + (WiFiError)error));
                    }
                    else
                    {
                        task.SetResult(true);
                    }
                    lock (_callback_map)
                    {
                        _callback_map.Remove(key);
                    }
                };
            }

            int ret = -1;

            if (info.GetType() == typeof(WpsPbcInfo))
            {
                ret = Interop.WiFi.ConnectByWpsPbc(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, _callback_map[id], id);
            }

            else if (info.GetType() == typeof(WpsPinInfo))
            {
                WpsPinInfo pinInfo = (WpsPinInfo)info;
                if (pinInfo.GetWpsPin() == null)
                {
                    throw new ArgumentNullException("Wps pin should not be null");
                }

                if (pinInfo.GetWpsPin().Length == 0 || pinInfo.GetWpsPin().Length > 8)
                {
                    throw new ArgumentOutOfRangeException("Wps pin should not be empty or more than 7 characters");
                }

                ret = Interop.WiFi.ConnectByWpsPin(WiFiManagerImpl.Instance.GetSafeHandle(), _apHandle, pinInfo.GetWpsPin(), _callback_map[id], id);
            }

            if (ret != (int)WiFiError.None)
            {
                Log.Error(Globals.LogTag, "Failed to connect wifi, Error - " + (WiFiError)ret);
                WiFiErrorFactory.ThrowWiFiException(ret, WiFiManagerImpl.Instance.GetSafeHandle().DangerousGetHandle(), _apHandle);
            }

            return(task.Task);
        }