Exemple #1
0
 internal WlanInterface(WlanClient client, Wlan.WlanInterfaceInfo info)
 {
     this.client = client;
     this.info = info;
 }
        ///// <summary>
        ///// The UI calls this functions to get the list of devices that the HomeHub can see
        ///// </summary>
        ///// <returns></returns>
        //public Tuple<List<string>, string> GetVisibleWifiNetworks()
        //{
        //     logger.Log("UICalled:GetVisibleWifiNetworks ");
        //    List<string> retList = new List<string>();

        //    try
        //    {
        //        WlanClient client = new WlanClient();
        //        foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
        //        {
        //            Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
        //            foreach (Wlan.WlanAvailableNetwork network in networks)
        //            {
        //                string ssid = WifiHelper.GetStringForSSID(network.dot11Ssid);

        //                if (!retList.Contains(ssid) && !String.IsNullOrWhiteSpace(ssid))
        //                {
        //                    retList.Add(WifiHelper.GetStringForSSID(network.dot11Ssid));
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception e)
        //    {
        //        logger.Log("Error while scanning for Wifi networks: {0}", e.ToString());
        //        return new Tuple<List<string>, string>(retList, "Error while scanning for Wifi networks");
        //    }

        //    return new Tuple<List<string>, string>(retList, "");
        //}

        /// <summary>
        /// The UI calls this functions to get the list of devices that the HomeHub can see
        /// </summary>
        /// <returns></returns>
        public List<string> GetVisibleWifiNetworksWeb()
        {
            logger.Log("UICalled:GetVisibleWifiNetworksWeb ");
            List<string> retList = new List<string>();

            try
            {
                WlanClient client = new WlanClient();
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {
                    Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                    foreach (Wlan.WlanAvailableNetwork network in networks)
                    {
                        string ssid = WifiHelper.GetStringForSSID(network.dot11Ssid);

                        if (!retList.Contains(ssid) && !String.IsNullOrWhiteSpace(ssid))
                        {
                            retList.Add(WifiHelper.GetStringForSSID(network.dot11Ssid));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.Log("Error while scanning for Wifi networks: {0}", e.ToString());
                retList.Insert(0, "Error while scanning WiFi networks");
                return retList;
            }

            retList.Insert(0, "");
            return retList;
        }
        /// <summary>
        /// The UI calls this function to ask the hub to connect to a particular WiFi network
        /// </summary>
        /// <param name="targetSsid"></param>
        /// <param name="passPhrase"></param>
        /// <returns></returns>
        public List<string> ConnectToWifiNetworkWeb(string targetSsid, string passPhrase)
        {
            try
            {
                bool result = false;
                string retString = "Network " + targetSsid + " not found";

                logger.Log("UICalled:ConnectToWifiNetworkWeb ");
                WlanClient client = new WlanClient();
                foreach (WlanClient.WlanInterface wlanIface in client.Interfaces)
                {
                    // Lists all networks with WEP security
                    Wlan.WlanAvailableNetwork[] networks = wlanIface.GetAvailableNetworkList(0);
                    foreach (Wlan.WlanAvailableNetwork network in networks)
                    {
                        string ssid = WifiHelper.GetStringForSSID(network.dot11Ssid);

                        if (targetSsid.Equals(ssid))
                        {
                            string profileName = ssid;
                            string profile = WifiHelper.MakeProfile(profileName, network, passPhrase);

                            //    Console.WriteLine("Made profile:\n{0}", profile);

                            Wlan.WlanReasonCode reasonCode = wlanIface.SetProfile(Wlan.WlanProfileFlags.AllUser, profile, true);

                            if (reasonCode == Wlan.WlanReasonCode.Success)
                            {
                                result = wlanIface.ConnectSynchronously(Wlan.WlanConnectionMode.Profile, Wlan.Dot11BssType.Any, profileName, 10000);

                                if (result == true)
                                {
                                    retString = "Connection succeeded!";

                                    config.UpdatePrivateConfSetting("WifiSsid", targetSsid);
                                    config.UpdatePrivateConfSetting("WifiKey", passPhrase);

                                    return new List<string>() { "", "true" };
                                }
                                else
                                {
                                    return new List<string>() { "Could not connect. Is the passphrase correct?", "false" };
                                }
                            }
                            else
                            {
                                return new List<string>() { "Could not set profile. ReasonCode = " + reasonCode.ToString(), "false" };
                            }
                        }
                    }
                }

                return new List<string>() { retString, result.ToString() };
            }
            catch (Exception e)
            {
                logger.Log("Exception in ConnectToWifiNetworkWeb: " + e.ToString());

                return new List<string>() { "Got exception: " + e.Message };
            }

        }
Exemple #4
0
 internal WlanInterface(WlanClient client, Wlan.WlanInterfaceInfo info)
 {
     this.client = client;
     this.info   = info;
 }